Skip to content

Instantly share code, notes, and snippets.

View lubieowoce's full-sized avatar

Janka Uryga lubieowoce

View GitHub Profile
export type Thenable<T> = PendingThenable<T> | FulfilledThenable<T> | RejectedThenable<T>
export type PendingThenable<T> = Promise<T> & { status: 'pending' }
export type FulfilledThenable<T> = Promise<T> & { status: 'fulfilled', value: T }
export type RejectedThenable<T> = Promise<T> & { status: 'rejected', reason: unknown }
export function trackThenableState<T>(promise: Promise<T>): Thenable<T> {
const thenable = promise as Thenable<T>;
if ("status" in thenable && typeof thenable.status === "string") {
@lubieowoce
lubieowoce / cache-value.ts
Created February 14, 2024 20:32
We have Record/Tuple at home
/// <reference types="react/canary" />
import React, { cache } from "react";
type Options = { passthrough?: boolean };
const OPTIONS_DEFAULT: Options = { passthrough: false };
type RecordOrTupleArg = Record<string, any> | any[]
export function cacheValue<TObj extends RecordOrTupleArg>(
v: RecordOrTupleArg,
@lubieowoce
lubieowoce / read-rsc-payload.js
Last active March 21, 2024 09:01
Extract the RSC payload from the HTML of a Next.js site
(() => {
const env = { self: {} };
const chunks = Array.from(document.body.querySelectorAll("script"))
.filter((s) => !!s.textContent && s.textContent.includes("self.__next_f"))
.forEach((s) => new Function("self", s.textContent)(env.self));
return env.self.__next_f
.filter((c) => c[0] === 1)
.map((c) => c[1])
.join("");
})();
@lubieowoce
lubieowoce / next+14.0.1.patch
Last active October 30, 2023 22:17
Next 14.0.1: log more details for "This is not allowed due to cyclic module graph between Server and Client"
diff --git a/node_modules/next/dist/build/webpack/loaders/next-flight-loader/index.js b/node_modules/next/dist/build/webpack/loaders/next-flight-loader/index.js
index 59e02e7..0bf857b 100644
--- a/node_modules/next/dist/build/webpack/loaders/next-flight-loader/index.js
+++ b/node_modules/next/dist/build/webpack/loaders/next-flight-loader/index.js
@@ -43,6 +43,7 @@ function transformSource(source, sourceMap) {
// and we shouldn't error for that. In the future we might want to find a way
// to only throw when it's used.
if (!this.resourcePath.includes("node_modules")) {
+ console.log('uh oh:', this._module.resourceResolveData.context.issuer, '->', this._module.resource)
this.callback(new Error(`You're importing a Client Component ("use client") from another Client Component imported Server Action file ("use server"). This is not allowed due to cyclic module graph between Server and Client.\nYou can work around it by defining and passing this
@lubieowoce
lubieowoce / babel-plugin-inline-actions.cjs
Last active October 28, 2023 22:07
a naive and WIP babel transform for inline "use server" closures
// @ts-check
/* eslint-disable @typescript-eslint/no-var-requires */
const { declare: declarePlugin } = require("@babel/helper-plugin-utils");
const { addNamed } = require("@babel/helper-module-imports");
const crypto = require("node:crypto");
const { pathToFileURL } = require("node:url");
// TODO: handle inline actions calling each other...? sounds tricky...

Let's assume we wanna show a tree like this:

ServerRoot # RSC
  html
    body
      MyLayout # RSC
        main
          "Hello"
@lubieowoce
lubieowoce / react-latest.sh
Created June 28, 2023 19:38
A bash function that prints out the latest canary/experimental releases of React and links to the relevant commits
# Usage:
#
# react-latest [tag]
#
function react-latest {
local TAG=${1-experimental}
echo "react@$TAG"
echo
which jq > /dev/null || { echo 'please install jq'; return 1; }
npm view react versions --json \
import asyncio
import bleak
# https://os.mbed.com/teams/Bluetooth-Low-Energy/wiki/UART-access-over-BLE
# https://www.mgsuperlabs.co.in/estore/IoT-pHAT-with-header-for-Raspberry-Pi
RBL_SERVICE_UUID = "713D0000-503E-4C75-BA94-3148F18D941E"
// 2020.03.03-r3
/* eslint-env jquery */
/* eslint no-global-assign: "off" */
/* eslint no-unused-vars: ["warn", { "varsIgnorePattern": "^_", "argsIgnorePattern": "^_" }] */
/* eslint no-prototype-builtins: "off" */
/* global
ich
// 2020.02.25-r1
// url: .*reservise\.com/(?:calendar|clients).*
// F1 - add_benefit_card()
// F2 - edit_popup_price()
// F4 - click_popup_present()
// ShortKeys config
/*