Skip to content

Instantly share code, notes, and snippets.

View ScriptedAlchemy's full-sized avatar
🎯
Focusing

Zack Jackson ScriptedAlchemy

🎯
Focusing
View GitHub Profile
@jacob-ebey
jacob-ebey / react-use.ts
Last active December 31, 2022 00:46
A very naive implementation of React's use() hook
// A very naive implementation of React's use() hook you can copy and paste into your app today
// to use with solutions such as remix's experimental `defer()` feature.
function use<T>(useable: Promise<T> | T) {
if (typeof useable != "object" || !useable || !("then" in useable)) {
return useable;
}
let promise = useable as Promise<T> & { _data?: T; _error?: unknown };
if ("_data" in promise) {
@nodkz
nodkz / getNextPages.ts
Created August 4, 2022 19:25
Pages map generation for @module-federation/nextjs-mf
import fg from 'fast-glob';
/**
* From provided ROOT_DIR `scan` pages directory
* and return list of user defined pages
* (except special ones, like _app, _document, _error)
*/
export function getNextPages(cwd: string) {
// scan all files in pages folder except pages/api
let pageList = fg.sync('pages/**/*.{ts,tsx,js,jsx}', {
@jacob-ebey
jacob-ebey / dynamic-import-cache-bust-loader.js
Created June 5, 2022 21:10
Node.js dynamic import cache buster
import { readFile } from "fs/promises";
import { createRequire } from "module";
import * as URL from "url";
import { readConfig } from "./config.mjs";
let require = createRequire(import.meta.url);
let config = await readConfig(process.cwd(), "development");
let outputFile = URL.pathToFileURL(config.serverBuildPath);
@JLHwung
JLHwung / ast-examples.md
Last active December 25, 2022 03:01
AST examples for ESTree pattern-matching proposal

Note: The ExpressionStatement and Program node are omitted in the examples.

1. Object Match Pattern and "else" Match Clause

match (x) {
    when ({ colour: "#000000" }) { "yes" }
    else {
        "no"
@jacob-ebey
jacob-ebey / FederatedStartupCodePlugin.js
Created December 9, 2020 08:27
FederatedStartupCodePlugin.js
const fs = require("fs");
const path = require("path");
const Template = require("webpack/lib/Template");
const SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin");
const VirtualModulesPlugin = require("webpack-virtual-modules");
const PLUGIN_NAME = "FederatedStartupCodePlugin";
/**
@wilsonpage
wilsonpage / swr.ts
Last active March 28, 2025 09:35
An implementation of stale-while-revalidate for Cloudflare Workers
export const CACHE_STALE_AT_HEADER = 'x-edge-cache-stale-at';
export const CACHE_STATUS_HEADER = 'x-edge-cache-status';
export const CACHE_CONTROL_HEADER = 'Cache-Control';
export const CLIENT_CACHE_CONTROL_HEADER = 'x-client-cache-control';
export const ORIGIN_CACHE_CONTROL_HEADER = 'x-edge-origin-cache-control';
enum CacheStatus {
HIT = 'HIT',
MISS = 'MISS',
REVALIDATING = 'REVALIDATING',
import React from 'react';
import { FederatedProvider } from './federated-provider';
import { scopes } from './scopes';
// This is an example app on how you would setup your Nextjs app
const App = ({ Component }) => {
return (
<FederatedProvider scopes={scopes}>
<Component />
@sibelius
sibelius / webpack.config.js
Last active July 14, 2020 14:45
run node files using webpack
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const cwd = process.cwd();
export const outputPath = path.join(cwd, '.webpack');
export const outputFilename = 'bundle.js';
export default {
@sohamkamani
sohamkamani / rsa.js
Last active December 24, 2024 15:57
An example of RSA Encryption implemented in Node.js
const crypto = require("crypto")
// The `generateKeyPairSync` method accepts two arguments:
// 1. The type ok keys we want, which in this case is "rsa"
// 2. An object with the properties of the key
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
// The standard secure default length for RSA keys is 2048 bits
modulusLength: 2048,
})
@kherock
kherock / mini-css-extract-plugin-cleanup.js
Last active January 26, 2024 20:20
Remove empty CSS modules workaround
const EMPTY_MODULES_CLEANUP_DEP_TYPES = new Set([
'harmony side effect evaluation',
'import()',
'single entry',
]);
class SetMap extends Map {
add (key, value) {
const set = this.get(key);
if (set === undefined) {