Skip to content

Instantly share code, notes, and snippets.

View huygn's full-sized avatar

Huy Giang huygn

View GitHub Profile
/**
* @returns `typeof value === 'object' && value != null && !Array.isArray(value)`
* @see {@link UntrustedObject} for type refinement details.
*/
function isUntrustedObject(value: unknown): value is UntrustedObject {
return typeof value === 'object' && value != null && !Array.isArray(value);
}
// Split a string into a union of all it's characters.
type SplitString<S extends string> = S extends `${infer First}${infer Rest}`
import fs from "fs";
import path from "path";
import { pathToFileURL, fileURLToPath } from "url";
import { builtinModules } from "module";
import { init, parse } from "es-module-lexer";
import { moduleResolve } from "import-meta-resolve";
/**
* @param {string} specifier
* @returns {boolean}
@KristofferEriksson
KristofferEriksson / useTextSelection.ts
Last active March 24, 2025 14:37
A React Typescript hook that tracks user text selections & their screen positions
import { useEffect, useState } from "react";
type UseTextSelectionReturn = {
text: string;
rects: DOMRect[];
ranges: Range[];
selection: Selection | null;
};
const getRangesFromSelection = (selection: Selection): Range[] => {

Multiple GitHub accounts (Work vs Personal)

This setup uses some tricks to ensure that the right email/name/ssh-key is used for the right repos without having to think about it ever again.

  • First generate two SSH keys, ~/.ssh/id_ed25519 and ~/.ssh/id_ed25519_work
  • Add one key to your personal account and the other to your work account

.ssh/config

@jacob-ebey
jacob-ebey / event-stream-reload-middleware.ts
Created September 28, 2023 06:51
Event Stream Dev Middleware
export function createDevMiddleware(pathname: string) {
return {
hmr(request: Request) {
return new Response(
new ReadableStream({
start(controller) {
controller.enqueue(
`id:0\nevent: message\ndata: ${JSON.stringify({
type: "connected",
})}\n\n`
@jacob-ebey
jacob-ebey / recursive-rsc-stream-inline.ts
Last active March 25, 2024 07:05
Inline RSC stream with a recursive async component that is streamed as part of the SSR renderToXYZ call.
import * as React from "react";
// @ts-ignore
import * as ReactDOM from "#react-dom-server-implementation";
// @ts-ignore
import * as ReactDOMClient from "#react-server-dom-client-implementation";
export async function fetch(
request: Request,
{
browserEntry,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script type="module">
@jacob-ebey
jacob-ebey / example.js
Last active August 27, 2023 01:47
Webpack Federation Expose Remotes Plugin
const remote = () => "@app/" + "other";
__webpack_chunk_load__(remote()).then(async () => {
const container = __webpack_require__(remote());
const factory = await container.get("./federated");
const mod = factory();
console.log({
APP_NAME,
REMOTE: mod.name,
});
@jacob-ebey
jacob-ebey / fetch-server.d.ts
Created August 23, 2023 07:42
Node Fetch Server
import type { Server } from "node:http";
export type Handler = (request: Request) => Response | Promise<Response>;
export type CreateServerOptions = {
onError?: (error: unknown) => void;
};
export declare function createServer(
handler: Handler,
@jacob-ebey
jacob-ebey / RSC-exploration.md
Created February 28, 2023 18:44
RSC Exploration

React Server Components Exploration

High level:

  • Separation of SSR vs RSC rendering environments is both unnecessary and a pain point for existing application migration to the new paradigms.
  • "Client Component can't import Server Component" seems to be an artificial limitation of the mental model of separate SSR vs RSC env and the lack of built in support for inlining RSC streams into the HTML document
  • Removal of "client only hooks" is also an artificial limitation brought on by trying to make SSR and RSC independent concepts. Zero reason to remove hooks that work in SSR today to support RSC.
  • Inlining of RSC JSON representation should not be needed and the intermediate tree should be able to be revived from the SSR'd DOM, not an inlined RSC format.