import { createServer } from "http";
import crypto from "crypto";
const PORT = 8001;
// this is from the web-socket specification and not something that is generated
const WEBSOCKET_MAGIC_STRING_KEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
/* | |
The Express body-parser library, which you may be using to parse incoming JSON | |
request bodies, doesn't currently protect against prototype poisoning via the | |
`__proto__` key. | |
The dangers of prototype poisoning are described in detail here: | |
https://hueniverse.com/a-tale-of-prototype-poisoning-2610fa170061 | |
Until body-parser provides its own fix, you can protect yourself by adding a | |
reviver function that throws an error if it sees any key named "__proto__". This |
function SomeCounter() { | |
const [ counter, updateCounter ] = useState(0); | |
useEffect(function listening(){ | |
const btn = document.getElementById("increment-counter-btn"); | |
btn.addEventListener("click",onClick); | |
},[]); | |
useEffect(function logger(){ | |
// this logger() is updated each time `counter` changes, so |
/* | |
Proof of concept: Writing dual-mode (sync and async) code via generators | |
Recommendation: start by reading the example (at the end). | |
API: | |
– The API object is called `def`. | |
– Dual-mode `await`: const unwrapped = yield wrapped; | |
– Dual-mode `yield`: yield def.$one(singleValue) | |
– Dual-mode `yield*`: yield def.$all(iterable) |
class CircuitBreaker { | |
constructor(request) { | |
this.request = request | |
this.state = "CLOSED" | |
this.failureThreshold = 3 | |
this.failureCount = 0 | |
this.successThreshold = 2 | |
this.successCount = 0 | |
this.timeout = 6000 | |
this.nextAttempt = Date.now() |
// Y Combinator | |
const Y = a => (b => b (b)) (b => a (c => b (b) (c))) | |
// isomorphic Church encoding/decoding | |
const Church = { | |
to: n => f => x => Array.from (Array (n)).reduce (f, x), | |
from: f => f (x => x + 1) (0) | |
} | |
const True = a => b => a |
(function () { | |
'use strict'; | |
/** | |
* wraps a promise in a timeout, allowing the promise to reject if not resolve with a specific period of time | |
* @param {integer} ms - milliseconds to wait before rejecting promise if not resolved | |
* @param {Promise} promise to monitor | |
* @example | |
* promiseTimeout(1000, fetch('https://courseof.life/johndoherty.json')) |
import { useState, useCallback } from "react"; | |
function useToggle(initialValue = false){ | |
// State with initial boolean value (true/false) | |
const [state, setState] = useState(initialValue); | |
// Let's create a toggle function | |
// This works, but we're using the state value from above | |
// instead of the current state. Usually they are the same, | |
// but if this hook was triggered multiple times rapidly then |
As soon as I saw the new YouTube Player and its new morphing play/pause button, I wanted to understand how it was made and replicate it myself.
From my analysis it looks like YouTube is using [SMIL animations][1]. I could not get those animations to work on browsers other than Chrome and it appears [that they are deprecated and will be removed][2]. I settled for the following technique:
-
Define the icon
path
elements inside adefs
element so that they are not drawn. -
Draw one icon by defining a
use
element whosexlink:href
attribute points to one of thepath
s defined in the previous step. Simply [changing this attribute to point to the other icon is enough to swap them out][3], but this switch is not animated. To do that, -
Replace the
use
with the actualpath
when the page is loaded.
12th July, 2023. I'm going to try creating an iOS app called Paranovel, using Expo. My environment for mobile app dev (Xcode, Ruby, etc.) should be in reasonably good shape already as I frequently develop with React Native and NativeScript.
Go to https://docs.expo.dev, and see the Quick Start: npx create-expo-app paranovel
This runs with no problem, then I get this macOS system popup: