Simple middleware stack for next.js getServerSideProps
(and eventually Api Routes)
import { use, wrap, Middleware } from './use';
interface WithUserVars {
users?: User;
}
.box { | |
display: block; | |
background: red; | |
width: 200px; | |
height: 200px; | |
opacity: 1; | |
} | |
.box-hidden { | |
display: none; |
import { parse } from 'cache-control-parser'; | |
export default { | |
async fetch(request: Request, env: {}, ctx: ExecutionContext): Promise<Response> { | |
try { | |
const cache = await caches.default; | |
const cachedResponse = await cache.match(request); | |
if (cachedResponse) { | |
console.log('Cache: HIT'); | |
if (shouldRevalidate(cachedResponse)) { |
import React, { | |
createContext, | |
useState, | |
useCallback, | |
useMemo, | |
useContext, | |
useId, | |
useLayoutEffect, | |
useEffect, | |
} from "react"; |
const requestNextAnimationFrame = (callback: FrameRequestCallback) => { | |
const handles: number[] = []; | |
const requestThisAnimationFrame = (callback: FrameRequestCallback) => { | |
handles.push(window.requestAnimationFrame(callback)); | |
}; | |
requestThisAnimationFrame(() => requestThisAnimationFrame(callback)); | |
return () => handles.forEach(handle => window.cancelAnimationFrame(handle)); | |
}; | |
interface TErrorOptions<Info, Cause> { | |
readonly msg?: string; | |
readonly info?: Info; | |
readonly cause?: Cause; | |
} | |
class TError<Info, Cause extends Error> extends Error { | |
public info?: Info; | |
public cause?: Cause; | |
constructor({ msg, cause, info }: TErrorOptions<Info, Cause>) { |
// The docs for mongodb native driver sessions are pretty bare. The best example I can find is | |
// https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-implement-transactions | |
// but the code is generally not pretty; I think the following is likely the most suitable API: | |
// const doc = withTransaction(async session => { | |
// const doc = await users.updateOne({ _id: id }, { $set: { email } }, { returnOriginal: false, session }); | |
// await orders.updateMany({ email }, { $set: { userId: id } }, { session }); | |
// return doc; | |
// }); | |
const withTransaction = async <T>( | |
fn: (session: ClientSession) => Promise<T> |
const _ = require("highland"); | |
const { Readable } = require("stream"); | |
const wrapAsync = require("./wrap-async"); | |
const sleep = (duration) => | |
new Promise((resolve) => setTimeout(resolve, duration)); | |
class Counter extends Readable { | |
constructor(opt) { | |
super(opt); |
An exploration of the different render methods available in react-enzyme.
const express = require('express'); | |
console.log(process.version); | |
const app = express(); | |
app.get('/hello', (req, res) => { | |
console.log('HANDLING REQUEST'); | |
res.setHeader('content-type', 'application/json'); | |
res.flushHeaders(); // `flushHeaders` to prevent http client from retrying and therefore invoking this handler multiple times. |