@astroscope/wormhole 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -32
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +4 -2
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +6 -5
- package/dist/react/index.js.map +1 -0
- package/dist/server.d.ts +4 -2
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +14 -13
- package/dist/server.js.map +1 -0
- package/dist/types-DHysnZE3.d.ts +21 -0
- package/dist/types-DHysnZE3.d.ts.map +1 -0
- package/package.json +10 -9
- package/dist/types-hCfcC5bZ.d.ts +0 -19
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as Wormhole } from "./types-DHysnZE3.js";
|
|
2
2
|
|
|
3
|
+
//#region src/define.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* Define a wormhole that transfers state from server middleware to client components.
|
|
5
6
|
*
|
|
@@ -7,5 +8,6 @@ import { W as Wormhole } from './types-hCfcC5bZ.js';
|
|
|
7
8
|
* Never store secrets (tokens, API keys, credentials) in a wormhole.
|
|
8
9
|
*/
|
|
9
10
|
declare function defineWormhole<T>(name: string): Wormhole<T>;
|
|
10
|
-
|
|
11
|
-
export { Wormhole, defineWormhole };
|
|
11
|
+
//#endregion
|
|
12
|
+
export { type Wormhole, defineWormhole };
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/define.ts"],"mappings":";;;;;AAQA;;;;iBAAgB,cAAA,IAAkB,IAAA,WAAe,QAAQ,CAAC,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,34 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/define.ts
|
|
2
|
+
/**
|
|
3
|
+
* Define a wormhole that transfers state from server middleware to client components.
|
|
4
|
+
*
|
|
5
|
+
* **Security:** wormhole data is serialized into the HTML and sent to the browser.
|
|
6
|
+
* Never store secrets (tokens, API keys, credentials) in a wormhole.
|
|
7
|
+
*/
|
|
2
8
|
function defineWormhole(name) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
listeners.delete(fn);
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
};
|
|
9
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
10
|
+
const key = `__wormhole_${name}__`;
|
|
11
|
+
let store;
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
key,
|
|
15
|
+
get() {
|
|
16
|
+
if (store !== void 0) return store;
|
|
17
|
+
const getter = globalThis[key];
|
|
18
|
+
if (typeof getter === "function") return getter();
|
|
19
|
+
throw new Error(`wormhole "${name}" is not initialized`);
|
|
20
|
+
},
|
|
21
|
+
set(data) {
|
|
22
|
+
if (typeof globalThis.window === "undefined") throw new Error(`wormhole "${name}" set() cannot be called on the server as it is not request-scoped; use open(wormhole, data, fn) from "@astroscope/wormhole/server" instead`);
|
|
23
|
+
store = data;
|
|
24
|
+
for (const fn of listeners) fn(data);
|
|
25
|
+
},
|
|
26
|
+
subscribe(fn) {
|
|
27
|
+
listeners.add(fn);
|
|
28
|
+
return () => {
|
|
29
|
+
listeners.delete(fn);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
};
|
|
31
33
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
//#endregion
|
|
35
|
+
export { defineWormhole };
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/define.ts"],"sourcesContent":["import type { Wormhole } from './types.js';\n\n/**\n * Define a wormhole that transfers state from server middleware to client components.\n *\n * **Security:** wormhole data is serialized into the HTML and sent to the browser.\n * Never store secrets (tokens, API keys, credentials) in a wormhole.\n */\nexport function defineWormhole<T>(name: string): Wormhole<T> {\n const listeners = new Set<(data: T) => void>();\n const key = `__wormhole_${name}__`;\n let store: T | undefined;\n\n return {\n name,\n key,\n\n get(): T {\n if (store !== undefined) return store;\n\n const getter = (globalThis as any)[key];\n\n if (typeof getter === 'function') return getter() as T;\n\n throw new Error(`wormhole \"${name}\" is not initialized`);\n },\n\n set(data: T): void {\n if (typeof (globalThis as any).window === 'undefined') {\n throw new Error(\n `wormhole \"${name}\" set() cannot be called on the server as it is not request-scoped; use open(wormhole, data, fn) from \"@astroscope/wormhole/server\" instead`,\n );\n }\n\n store = data;\n\n for (const fn of listeners) fn(data);\n },\n\n subscribe(fn: (data: T) => void): () => void {\n listeners.add(fn);\n\n return () => {\n listeners.delete(fn);\n };\n },\n };\n}\n"],"mappings":";;;;;;;AAQA,SAAgB,eAAkB,MAA2B;CAC3D,MAAM,4BAAY,IAAI,IAAuB;CAC7C,MAAM,MAAM,cAAc,KAAK;CAC/B,IAAI;CAEJ,OAAO;EACL;EACA;EAEA,MAAS;GACP,IAAI,UAAU,KAAA,GAAW,OAAO;GAEhC,MAAM,SAAU,WAAmB;GAEnC,IAAI,OAAO,WAAW,YAAY,OAAO,OAAO;GAEhD,MAAM,IAAI,MAAM,aAAa,KAAK,qBAAqB;EACzD;EAEA,IAAI,MAAe;GACjB,IAAI,OAAQ,WAAmB,WAAW,aACxC,MAAM,IAAI,MACR,aAAa,KAAK,4IACpB;GAGF,QAAQ;GAER,KAAK,MAAM,MAAM,WAAW,GAAG,IAAI;EACrC;EAEA,UAAU,IAAmC;GAC3C,UAAU,IAAI,EAAE;GAEhB,aAAa;IACX,UAAU,OAAO,EAAE;GACrB;EACF;CACF;AACF"}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as Wormhole } from "../types-DHysnZE3.js";
|
|
2
2
|
|
|
3
|
+
//#region src/react/index.d.ts
|
|
3
4
|
declare function useWormhole<T>(wormhole: Wormhole<T>): T;
|
|
4
|
-
|
|
5
|
+
//#endregion
|
|
5
6
|
export { useWormhole };
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/react/index.ts"],"mappings":";;;iBAGgB,WAAA,IAAe,QAAA,EAAU,QAAA,CAAS,CAAA,IAAK,CAAA"}
|
package/dist/react/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// src/react/index.ts
|
|
2
1
|
import { useSyncExternalStore } from "react";
|
|
2
|
+
//#region src/react/index.ts
|
|
3
3
|
function useWormhole(wormhole) {
|
|
4
|
-
|
|
4
|
+
return useSyncExternalStore(wormhole.subscribe, wormhole.get, wormhole.get);
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
//#endregion
|
|
7
|
+
export { useWormhole };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/react/index.ts"],"sourcesContent":["import { useSyncExternalStore } from 'react';\nimport type { Wormhole } from '../types.js';\n\nexport function useWormhole<T>(wormhole: Wormhole<T>): T {\n return useSyncExternalStore(wormhole.subscribe, wormhole.get, wormhole.get);\n}\n"],"mappings":";;AAGA,SAAgB,YAAe,UAA0B;CACvD,OAAO,qBAAqB,SAAS,WAAW,SAAS,KAAK,SAAS,GAAG;AAC5E"}
|
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as Wormhole } from "./types-DHysnZE3.js";
|
|
2
2
|
|
|
3
|
+
//#region src/server.d.ts
|
|
3
4
|
declare function open<T, R>(wh: Wormhole<T>, data: T, fn: () => R): R;
|
|
4
|
-
|
|
5
|
+
//#endregion
|
|
5
6
|
export { open };
|
|
7
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","names":[],"sources":["../src/server.ts"],"mappings":";;;iBAKgB,IAAA,OAAW,EAAA,EAAI,QAAA,CAAS,CAAA,GAAI,IAAA,EAAM,CAAA,EAAG,EAAA,QAAU,CAAA,GAAI,CAAA"}
|
package/dist/server.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
//#region src/server.ts
|
|
3
|
+
const stores = /* @__PURE__ */ new Map();
|
|
4
4
|
function open(wh, data, fn) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
let als = stores.get(wh.key);
|
|
6
|
+
if (!als) {
|
|
7
|
+
als = new AsyncLocalStorage();
|
|
8
|
+
stores.set(wh.key, als);
|
|
9
|
+
}
|
|
10
|
+
globalThis[wh.key] = () => als.getStore();
|
|
11
|
+
return als.run(data, fn);
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
//#endregion
|
|
14
|
+
export { open };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","names":[],"sources":["../src/server.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { Wormhole } from './types.js';\n\nconst stores = new Map<string, AsyncLocalStorage<unknown>>();\n\nexport function open<T, R>(wh: Wormhole<T>, data: T, fn: () => R): R {\n let als = stores.get(wh.key) as AsyncLocalStorage<T> | undefined;\n\n if (!als) {\n als = new AsyncLocalStorage<T>();\n stores.set(wh.key, als as AsyncLocalStorage<unknown>);\n }\n\n (globalThis as any)[wh.key] = () => als!.getStore();\n\n return als.run(data, fn);\n}\n"],"mappings":";;AAGA,MAAM,yBAAS,IAAI,IAAwC;AAE3D,SAAgB,KAAW,IAAiB,MAAS,IAAgB;CACnE,IAAI,MAAM,OAAO,IAAI,GAAG,GAAG;CAE3B,IAAI,CAAC,KAAK;EACR,MAAM,IAAI,kBAAqB;EAC/B,OAAO,IAAI,GAAG,KAAK,GAAiC;CACtD;CAEA,WAAoB,GAAG,aAAa,IAAK,SAAS;CAElD,OAAO,IAAI,IAAI,MAAM,EAAE;AACzB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A wormhole transfers state from server middleware to client-side components.
|
|
4
|
+
*
|
|
5
|
+
* **Security:** wormhole data is serialized into an inline `<script>` tag and sent to the browser.
|
|
6
|
+
* Never store secrets (tokens, API keys, credentials) in a wormhole.
|
|
7
|
+
*/
|
|
8
|
+
interface Wormhole<T> {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly key: string;
|
|
11
|
+
get(): T;
|
|
12
|
+
/**
|
|
13
|
+
* Update the wormhole value on the **client only**.
|
|
14
|
+
* Throws on the server — use `open(wormhole, data, fn)` from `@astroscope/wormhole/server` instead.
|
|
15
|
+
*/
|
|
16
|
+
set(data: T): void;
|
|
17
|
+
subscribe(fn: (data: T) => void): () => void;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { Wormhole as t };
|
|
21
|
+
//# sourceMappingURL=types-DHysnZE3.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-DHysnZE3.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";;AAMA;;;;;UAAiB,QAAA;EAAA,SACN,IAAA;EAAA,SACA,GAAA;EACT,GAAA,IAAO,CAAA;EAFE;;;;EAOT,GAAA,CAAI,IAAA,EAAM,CAAA;EACV,SAAA,CAAU,EAAA,GAAK,IAAA,EAAM,CAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astroscope/wormhole",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Share dynamic server data with React islands and client scripts — typed, reactive",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -50,14 +50,15 @@
|
|
|
50
50
|
},
|
|
51
51
|
"homepage": "https://github.com/entwico/astroscope/tree/main/packages/wormhole#readme",
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@types/
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
53
|
+
"@types/node": "^26.0.1",
|
|
54
|
+
"@types/react": "^19.2.17",
|
|
55
|
+
"astro": "^7.0.3",
|
|
56
|
+
"react": "^19.2.7",
|
|
57
|
+
"tsdown": "^0.22.3",
|
|
58
|
+
"typescript": "^6.0.3"
|
|
58
59
|
},
|
|
59
60
|
"peerDependencies": {
|
|
60
|
-
"astro": "^
|
|
61
|
+
"astro": "^7.0.0",
|
|
61
62
|
"react": "^18.0.0 || ^19.0.0"
|
|
62
63
|
},
|
|
63
64
|
"peerDependenciesMeta": {
|
|
@@ -66,9 +67,9 @@
|
|
|
66
67
|
}
|
|
67
68
|
},
|
|
68
69
|
"scripts": {
|
|
69
|
-
"build": "
|
|
70
|
+
"build": "tsdown src/index.ts src/server.ts src/react/index.ts --format esm --dts --no-fixed-extension",
|
|
70
71
|
"typecheck": "tsc --noEmit",
|
|
71
|
-
"lint": "eslint src",
|
|
72
|
+
"lint": "eslint src --cache",
|
|
72
73
|
"lint:fix": "eslint src --fix"
|
|
73
74
|
}
|
|
74
75
|
}
|
package/dist/types-hCfcC5bZ.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A wormhole transfers state from server middleware to client-side components.
|
|
3
|
-
*
|
|
4
|
-
* **Security:** wormhole data is serialized into an inline `<script>` tag and sent to the browser.
|
|
5
|
-
* Never store secrets (tokens, API keys, credentials) in a wormhole.
|
|
6
|
-
*/
|
|
7
|
-
interface Wormhole<T> {
|
|
8
|
-
readonly name: string;
|
|
9
|
-
readonly key: string;
|
|
10
|
-
get(): T;
|
|
11
|
-
/**
|
|
12
|
-
* Update the wormhole value on the **client only**.
|
|
13
|
-
* Throws on the server — use `open(wormhole, data, fn)` from `@astroscope/wormhole/server` instead.
|
|
14
|
-
*/
|
|
15
|
-
set(data: T): void;
|
|
16
|
-
subscribe(fn: (data: T) => void): () => void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export type { Wormhole as W };
|