@astroscope/wormhole 1.0.0 → 2.0.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/README.md +72 -105
- package/dist/define-BO-pYQc1.js +61 -0
- package/dist/define-BO-pYQc1.js.map +1 -0
- package/dist/index.browser.d.ts +9 -0
- package/dist/index.browser.d.ts.map +1 -0
- package/dist/index.browser.js +48 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.d.ts +35 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +166 -27
- package/dist/index.js.map +1 -1
- package/dist/key-BqTy1gM3.js +9 -0
- package/dist/key-BqTy1gM3.js.map +1 -0
- package/dist/react/index.d.ts +1 -2
- package/dist/react/index.d.ts.map +1 -1
- package/dist/scan-4GXufA9_.js +79 -0
- package/dist/scan-4GXufA9_.js.map +1 -0
- package/dist/server.d.ts +55 -4
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +157 -11
- package/dist/server.js.map +1 -1
- package/dist/types-okMd-V3s.d.ts +42 -0
- package/dist/types-okMd-V3s.d.ts.map +1 -0
- package/package.json +20 -16
- package/dist/types-0PQTD8gI.d.ts +0 -31
- package/dist/types-0PQTD8gI.d.ts.map +0 -1
- package/src/astro/WormholeScript.astro +0 -21
- package/src/astro/index.ts +0 -2
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","names":[],"sources":["../src/server.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { DeepReadonly, Wormhole } from './types.js';\n\nconst stores = new Map<string, AsyncLocalStorage<unknown>>();\n\nexport function open<T, R>(wh: Wormhole<T>, data: DeepReadonly<T>, fn: () => R): R {\n let als = stores.get(wh.key) as AsyncLocalStorage<DeepReadonly<T>> | undefined;\n\n if (!als) {\n als = new AsyncLocalStorage<DeepReadonly<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,MAAuB,IAAgB;CACjF,IAAI,MAAM,OAAO,IAAI,GAAG,GAAG;CAE3B,IAAI,CAAC,KAAK;EACR,MAAM,IAAI,kBAAmC;EAC7C,OAAO,IAAI,GAAG,KAAK,GAAiC;CACtD;CAEA,WAAoB,GAAG,aAAa,IAAK,SAAS;CAElD,OAAO,IAAI,IAAI,MAAM,EAAE;AACzB"}
|
|
1
|
+
{"version":3,"file":"server.js","names":["registry"],"sources":["../src/client-state.ts","../src/manifest.ts","../src/request-store.ts","../src/islands-emitter.ts","../src/middleware.ts","../src/server.ts"],"sourcesContent":["/**\n * Script generation for wormhole delivery, shipped through the `@astroscope/node`\n * islands pipeline (see islands-emitter.ts for the placement policy).\n */\n\n/** values may contain `</script>` — escape `<` so they cannot end the tag */\nexport function jsonForScript(value: unknown): string {\n return JSON.stringify(value).replace(/</g, '\\\\u003c');\n}\n\n/**\n * Inline script writing entries onto `self.__wormholes__` and notifying listeners —\n * the counterpart of the browser proxy's entry protocol, safe in either order.\n */\nexport function createWormholeMergeScript(entries: Record<string, unknown>): string {\n return (\n `<script>{const s=self.__wormholes__??={},d=${jsonForScript(entries)};` +\n `for(const n in d){const e=s[n]??={};e.v=d[n];e.l&&e.l.forEach(f=>f(e.v))}}</script>`\n );\n}\n","import { manifest } from 'virtual:@astroscope/wormhole/manifest';\nimport type { WormholeManifest } from './extraction/types.js';\n\n/** null in dev and when the build produced no manifest (broken deploy → full delivery fallback) */\nexport function getWormholeManifest(): WormholeManifest | null {\n return manifest;\n}\n","/**\n * Per-request wormhole values for the islands emitter and the stream-end script —\n * both run while the response streams, outside the middleware's ALS scope, so the\n * request object is the only stable handle (same pattern as the i18n locale).\n */\nexport type RequestWormholes = {\n /** wormhole name → serializable value, as resolved by the middleware */\n values: ReadonlyMap<string, unknown>;\n /** names already written into the document by the islands emitter */\n emitted: Set<string>;\n};\n\nconst requestWormholes = new WeakMap<Request, RequestWormholes>();\n\nexport function setRequestWormholes(request: Request, wormholes: RequestWormholes): void {\n requestWormholes.set(request, wormholes);\n}\n\nexport function getRequestWormholes(request: Request): RequestWormholes | undefined {\n return requestWormholes.get(request);\n}\n","import { type DocumentEmitter, registerDocumentEmitter, registerIslandEmitter } from '@astroscope/node/islands';\nimport { log } from '@astroscope/node/log';\nimport { createWormholeMergeScript } from './client-state.js';\nimport { getWormholeManifest } from './manifest.js';\nimport { getRequestWormholes } from './request-store.js';\n\n/**\n * Contributes each island's wormhole slice: an inline merge script with the values\n * of every open wormhole the island's full chunk closure (dynamic imports included)\n * can reach, emitted before the island tag — parsed strictly before the island\n * connects, even mid-stream. Names already written earlier in the document are\n * skipped; a `*` chunk (dynamic proxy access) degrades to all open wormholes.\n */\n\n// vite defines import.meta.env when bundling the middleware; typed structurally\n// so the package compiles without vite's global client types\nconst IS_DEV = !!(import.meta as { env?: { DEV?: boolean } }).env?.DEV;\n\nlet registered = false;\nlet warnedMissingManifest = false;\n\n/** public url of a chunk → manifest chunk name, e.g. `…/_astro/Counter.abc.js` → `Counter.abc` */\nfunction urlToChunkName(url: string): string {\n return url.slice(url.lastIndexOf('/') + 1).replace(/\\.js$/, '');\n}\n\n/**\n * The document-level wormhole delivery, exported for tests: everything open as\n * `head` content in dev (or without a manifest), script-entry names as `end`\n * content in prod — running after the rewriter, names the islands emitter\n * already wrote are skipped race-free.\n */\nexport function createWormholeDocumentEmitter(dev: boolean = IS_DEV): DocumentEmitter {\n return (context) => {\n const request = getRequestWormholes(context.request);\n\n if (!request || request.values.size === 0) {\n return null;\n }\n\n if (dev) {\n return { head: createWormholeMergeScript(Object.fromEntries(request.values)) };\n }\n\n const manifest = getWormholeManifest();\n\n if (!manifest) {\n if (!warnedMissingManifest) {\n warnedMissingManifest = true;\n log.error(\n 'wormhole manifest not found — delivering all open wormholes with every page (is the integration configured?)',\n );\n }\n\n return { head: createWormholeMergeScript(Object.fromEntries(request.values)) };\n }\n\n const names = manifest.scripts.includes('*')\n ? [...request.values.keys()]\n : manifest.scripts.filter((name) => request.values.has(name));\n\n if (names.length === 0) {\n return null;\n }\n\n return {\n end: () => {\n const fresh = names.filter((name) => !request.emitted.has(name));\n\n if (fresh.length === 0) {\n return null;\n }\n\n return createWormholeMergeScript(Object.fromEntries(fresh.map((name) => [name, request.values.get(name)])));\n },\n };\n };\n}\n\nexport function registerWormholeEmitters(): void {\n if (registered) {\n return;\n }\n\n registered = true;\n\n registerDocumentEmitter(createWormholeDocumentEmitter());\n\n registerIslandEmitter((island, context) => {\n // no request context (prerendered build pass) — values are per-request, nothing to emit\n if (!context) {\n return null;\n }\n\n const request = getRequestWormholes(context.request);\n const manifest = getWormholeManifest();\n\n if (!request || request.values.size === 0 || !manifest) {\n return null;\n }\n\n let all = false;\n const reachable = new Set<string>();\n\n for (const url of island.fullClosure) {\n for (const name of manifest.chunks[urlToChunkName(url)] ?? []) {\n if (name === '*') {\n all = true;\n } else {\n reachable.add(name);\n }\n }\n }\n\n const entries: Record<string, unknown> = {};\n let count = 0;\n\n for (const name of all ? request.values.keys() : reachable) {\n if (!request.values.has(name) || request.emitted.has(name)) {\n continue;\n }\n\n request.emitted.add(name);\n entries[name] = request.values.get(name);\n count++;\n }\n\n if (count === 0) {\n return null;\n }\n\n return { html: createWormholeMergeScript(entries) };\n });\n}\n","import { type ExcludePattern, RECOMMENDED_EXCLUDES, shouldExclude } from '@astroscope/node/excludes';\nimport type { APIContext, MiddlewareHandler } from 'astro';\nimport { wormholes as registry } from 'virtual:@astroscope/wormhole/registry';\nimport { als } from './als.js';\nimport { assignWormholeNames } from './define.js';\nimport { registerWormholeEmitters } from './islands-emitter.js';\nimport { setRequestWormholes } from './request-store.js';\nimport type { DeepReadonly, WormholeRegistry } from './types.js';\n\n/**\n * Per-request values keyed by registry name — omit a key (or pass undefined) to\n * leave that wormhole closed. Typed from the `WormholeRegistry` interface the\n * integration generates from `src/wormholes.ts`.\n */\nexport type WormholeValues = {\n [K in keyof WormholeRegistry]?: DeepReadonly<WormholeRegistry[K]> | undefined;\n};\n\nexport type WormholeMiddlewareOptions = {\n /** resolve the request's wormhole values */\n values: (context: APIContext) => WormholeValues | Promise<WormholeValues>;\n /**\n * Patterns to exclude from wormhole setup. Defaults to RECOMMENDED_EXCLUDES.\n */\n exclude?: ExcludePattern[] | ((context: APIContext) => boolean) | undefined;\n};\n\n/**\n * Create the wormhole middleware: resolves the per-request values, makes them\n * readable on the server (frontmatter, endpoints, SSR islands) for the duration of\n * the request, and delivers them to the client — sliced per island through the\n * `@astroscope/node` islands pipeline, at stream end for astro `<script>` consumers,\n * and in full in dev. The registry comes from `src/wormholes.ts` via the\n * integration, so values are simply keyed by name.\n *\n * @example\n * ```typescript\n * // src/middleware.ts\n * import { createWormholeMiddleware } from '@astroscope/wormhole/server';\n *\n * export const onRequest = createWormholeMiddleware({\n * values: (ctx) => ({\n * session: { loggedIn: ctx.locals.user !== undefined },\n * }),\n * });\n * ```\n */\nexport function createWormholeMiddleware(options: WormholeMiddlewareOptions): MiddlewareHandler {\n assignWormholeNames(registry);\n registerWormholeEmitters();\n\n return async (ctx, next) => {\n if (shouldExclude(ctx, options.exclude ?? RECOMMENDED_EXCLUDES)) {\n return next();\n }\n\n const resolved = await options.values(ctx);\n const open = new Map<string, unknown>();\n const scope = new Map<string, unknown>(als.getStore());\n\n for (const [name, data] of Object.entries(resolved as Record<string, unknown>)) {\n if (data === undefined) {\n continue;\n }\n\n const wormhole = registry[name];\n\n if (!wormhole) {\n throw new Error(`wormhole \"${name}\" is not in the src/wormholes.ts registry`);\n }\n\n open.set(name, data);\n scope.set(wormhole.key, data);\n }\n\n // the islands emitter and the stream-end script run while the response streams,\n // outside this ALS scope — the request object carries the values to them\n setRequestWormholes(ctx.request, { values: open, emitted: new Set() });\n\n // nothing to transform here\n return als.run(scope, () => next());\n };\n}\n","import { als } from './als.js';\nimport type { DeepReadonly, Wormhole } from './types.js';\n\nexport { createWormholeMiddleware } from './middleware.js';\nexport type { WormholeMiddlewareOptions, WormholeValues } from './middleware.js';\n\n/** One [wormhole, data] pair per element — data is checked against its own wormhole's T. */\ntype WormholeEntries<Ts extends readonly unknown[]> = {\n [K in keyof Ts]: readonly [Wormhole<Ts[K]>, DeepReadonly<Ts[K]>];\n};\n\n/**\n * Provide wormhole values to server code that runs outside the request pipeline —\n * tests above all, or out-of-request rendering (emails, jobs). `wormhole.get()`\n * returns `data` anywhere in the async execution rooted at `fn`. Inside the app,\n * the middleware is the way; values opened here are never delivered to the client.\n */\nexport function openWormholes<T, R>(wormhole: Wormhole<T>, data: DeepReadonly<T>, fn: () => R): R;\n/**\n * Open several wormholes at once for the duration of `fn` — pass an array of\n * `[wormhole, data]` pairs, each checked against its own wormhole.\n */\nexport function openWormholes<Ts extends readonly unknown[], R>(\n entries: readonly [...WormholeEntries<Ts>],\n fn: () => R,\n): R;\nexport function openWormholes(...args: unknown[]): unknown {\n const [entries, fn] = (Array.isArray(args[0]) ? args : [[[args[0], args[1]]], args[2]]) as [\n readonly (readonly [Wormhole<unknown>, unknown])[],\n () => unknown,\n ];\n\n const ctx = new Map(als.getStore());\n\n for (const [wormhole, data] of entries) {\n ctx.set(wormhole.key, data);\n }\n\n return als.run(ctx, fn);\n}\n"],"mappings":";;;;;;;;;;;;AAMA,SAAgB,cAAc,OAAwB;CACpD,OAAO,KAAK,UAAU,KAAK,CAAC,CAAC,QAAQ,MAAM,SAAS;AACtD;;;;;AAMA,SAAgB,0BAA0B,SAA0C;CAClF,OACE,8CAA8C,cAAc,OAAO,EAAE;AAGzE;;;;ACfA,SAAgB,sBAA+C;CAC7D,OAAO;AACT;;;ACMA,MAAM,mCAAmB,IAAI,QAAmC;AAEhE,SAAgB,oBAAoB,SAAkB,WAAmC;CACvF,iBAAiB,IAAI,SAAS,SAAS;AACzC;AAEA,SAAgB,oBAAoB,SAAgD;CAClF,OAAO,iBAAiB,IAAI,OAAO;AACrC;;;;;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,MAAM,IAAI;GACnB,MAAM,IAAI,SAAS,KAAK,IAAI;EAC9B;EAIA,oBAAoB,IAAI,SAAS;GAAE,QAAQ;GAAM,yBAAS,IAAI,IAAI;EAAE,CAAC;EAGrE,OAAO,IAAI,IAAI,aAAa,KAAK,CAAC;CACpC;AACF;;;ACxDA,SAAgB,cAAc,GAAG,MAA0B;CACzD,MAAM,CAAC,SAAS,MAAO,MAAM,QAAQ,KAAK,EAAE,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,IAAI,KAAK,EAAE,CAAC,GAAG,KAAK,EAAE;CAKrF,MAAM,MAAM,IAAI,IAAI,IAAI,SAAS,CAAC;CAElC,KAAK,MAAM,CAAC,UAAU,SAAS,SAC7B,IAAI,IAAI,SAAS,KAAK,IAAI;CAG5B,OAAO,IAAI,IAAI,KAAK,EAAE;AACxB"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Recursively marks all properties of `T` as `readonly`.
|
|
4
|
+
*
|
|
5
|
+
* Wormhole data is JSON-serializable by design (it is inlined into a `<script>` tag),
|
|
6
|
+
* so only plain objects, arrays, and primitives need to be handled.
|
|
7
|
+
*/
|
|
8
|
+
type DeepReadonly<T> = T extends ((...args: never[]) => unknown) ? T : T extends object ? { readonly [K in keyof T]: DeepReadonly<T[K]>; } : T;
|
|
9
|
+
/**
|
|
10
|
+
* A wormhole transfers state from server middleware to client-side components.
|
|
11
|
+
*
|
|
12
|
+
* **Security:** wormhole data is serialized into an inline `<script>` tag and sent to the browser.
|
|
13
|
+
* Never store secrets (tokens, API keys, credentials) in a wormhole.
|
|
14
|
+
*
|
|
15
|
+
* The stored value is exposed as deeply readonly — the only way to change it is `set()`
|
|
16
|
+
* (client) or the middleware's per-request values (server), which keeps subscribers in sync.
|
|
17
|
+
*/
|
|
18
|
+
interface Wormhole<in out T> {
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly key: string;
|
|
21
|
+
get(): DeepReadonly<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Update the wormhole value on the **client only** — every subscriber on the page
|
|
24
|
+
* (islands and scripts alike) is notified. Throws on the server, where values are
|
|
25
|
+
* request-scoped and provided by the middleware.
|
|
26
|
+
*/
|
|
27
|
+
set(data: DeepReadonly<T>): void;
|
|
28
|
+
subscribe(fn: (data: DeepReadonly<T>) => void): () => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The project's wormhole names and value types. Empty by default — the integration
|
|
32
|
+
* generates a type stub (via `injectTypes`) that merges the `src/wormholes.ts`
|
|
33
|
+
* registry into this interface, which types the `wormholes` proxy.
|
|
34
|
+
*/
|
|
35
|
+
interface WormholeRegistry {}
|
|
36
|
+
/** maps a registry module's `{ name: Wormhole<T> }` shape to `{ name: T }` */
|
|
37
|
+
type UnwrapWormholes<R> = { [K in keyof R]: R[K] extends Wormhole<infer T> ? T : never; };
|
|
38
|
+
/** the shape of the `wormholes` proxy: one typed accessor per registry entry */
|
|
39
|
+
type WormholeMap = { readonly [K in keyof WormholeRegistry]: Wormhole<WormholeRegistry[K]>; };
|
|
40
|
+
//#endregion
|
|
41
|
+
export { WormholeRegistry as a, WormholeMap as i, UnwrapWormholes as n, Wormhole as r, DeepReadonly as t };
|
|
42
|
+
//# sourceMappingURL=types-okMd-V3s.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-okMd-V3s.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;KAMY,aAAa,KAAK,eAAc,6BACxC,IACA,+BACc,WAAW,IAAI,aAAa,EAAE,SAC1C;;;;;;;;;;UAaW,gBAAgB;WACtB;WACA;EACT,OAAO,aAAa;;;;;;EAMpB,IAAI,MAAM,aAAa;EACvB,UAAU,KAAK,MAAM,aAAa;;;;;;;UASnB;;KAGL,gBAAgB,QAAQ,WAAW,IAAI,EAAE,WAAW,eAAe,KAAK;;KAGxE,0BAA0B,WAAW,mBAAmB,SAAS,iBAAiB"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astroscope/wormhole",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Share dynamic server data with React islands and client scripts — typed,
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Share dynamic server data with React islands and client scripts — typed, streamed with the HTML, sliced per island",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
|
-
"
|
|
11
|
+
"browser": "./dist/index.browser.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
12
13
|
},
|
|
13
14
|
"./server": {
|
|
14
15
|
"types": "./dist/server.d.ts",
|
|
@@ -17,14 +18,10 @@
|
|
|
17
18
|
"./react": {
|
|
18
19
|
"types": "./dist/react/index.d.ts",
|
|
19
20
|
"import": "./dist/react/index.js"
|
|
20
|
-
},
|
|
21
|
-
"./astro": {
|
|
22
|
-
"import": "./src/astro/index.ts"
|
|
23
21
|
}
|
|
24
22
|
},
|
|
25
23
|
"files": [
|
|
26
|
-
"dist"
|
|
27
|
-
"src/astro"
|
|
24
|
+
"dist"
|
|
28
25
|
],
|
|
29
26
|
"sideEffects": false,
|
|
30
27
|
"publishConfig": {
|
|
@@ -49,17 +46,24 @@
|
|
|
49
46
|
"url": "https://github.com/entwico/astroscope/issues"
|
|
50
47
|
},
|
|
51
48
|
"homepage": "https://github.com/entwico/astroscope/tree/main/packages/wormhole#readme",
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@babel/parser": "^8.0.4"
|
|
51
|
+
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@
|
|
54
|
-
"@types/
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
53
|
+
"@astroscope/node": "^2.0.0",
|
|
54
|
+
"@types/node": "^26.3.0",
|
|
55
|
+
"@types/react": "^19.2.18",
|
|
56
|
+
"astro": "^7.2.6",
|
|
57
|
+
"react": "^19.2.8",
|
|
58
|
+
"tsdown": "^0.22.14",
|
|
59
|
+
"typescript": "^6.0.3",
|
|
60
|
+
"vite": "^8.2.2"
|
|
59
61
|
},
|
|
60
62
|
"peerDependencies": {
|
|
63
|
+
"@astroscope/node": "^2.0.0",
|
|
61
64
|
"astro": "^7.0.0",
|
|
62
|
-
"react": "^18.0.0 || ^19.0.0"
|
|
65
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
66
|
+
"vite": "^8.0.0"
|
|
63
67
|
},
|
|
64
68
|
"peerDependenciesMeta": {
|
|
65
69
|
"react": {
|
|
@@ -67,7 +71,7 @@
|
|
|
67
71
|
}
|
|
68
72
|
},
|
|
69
73
|
"scripts": {
|
|
70
|
-
"build": "tsdown
|
|
74
|
+
"build": "tsdown",
|
|
71
75
|
"typecheck": "tsc --noEmit",
|
|
72
76
|
"lint": "eslint src --cache",
|
|
73
77
|
"lint:fix": "eslint src --fix"
|
package/dist/types-0PQTD8gI.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
//#region src/types.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Recursively marks all properties of `T` as `readonly`.
|
|
4
|
-
*
|
|
5
|
-
* Wormhole data is JSON-serializable by design (it is inlined into a `<script>` tag),
|
|
6
|
-
* so only plain objects, arrays, and primitives need to be handled.
|
|
7
|
-
*/
|
|
8
|
-
type DeepReadonly<T> = T extends ((...args: never[]) => unknown) ? T : T extends object ? { readonly [K in keyof T]: DeepReadonly<T[K]> } : T;
|
|
9
|
-
/**
|
|
10
|
-
* A wormhole transfers state from server middleware to client-side components.
|
|
11
|
-
*
|
|
12
|
-
* **Security:** wormhole data is serialized into an inline `<script>` tag and sent to the browser.
|
|
13
|
-
* Never store secrets (tokens, API keys, credentials) in a wormhole.
|
|
14
|
-
*
|
|
15
|
-
* The stored value is exposed as deeply readonly — the only way to change it is `set()`
|
|
16
|
-
* (client) or `open()` (server) with a new value, which keeps subscribers in sync.
|
|
17
|
-
*/
|
|
18
|
-
interface Wormhole<T> {
|
|
19
|
-
readonly name: string;
|
|
20
|
-
readonly key: string;
|
|
21
|
-
get(): DeepReadonly<T>;
|
|
22
|
-
/**
|
|
23
|
-
* Update the wormhole value on the **client only**.
|
|
24
|
-
* Throws on the server — use `open(wormhole, data, fn)` from `@astroscope/wormhole/server` instead.
|
|
25
|
-
*/
|
|
26
|
-
set(data: DeepReadonly<T>): void;
|
|
27
|
-
subscribe(fn: (data: DeepReadonly<T>) => void): () => void;
|
|
28
|
-
}
|
|
29
|
-
//#endregion
|
|
30
|
-
export { Wormhole as n, DeepReadonly as t };
|
|
31
|
-
//# sourceMappingURL=types-0PQTD8gI.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types-0PQTD8gI.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";;AAMA;;;;;KAAY,YAAA,MAAkB,CAAA,cAAc,IAAA,yBACxC,CAAA,GACA,CAAA,yCACyB,CAAA,GAAI,YAAA,CAAa,CAAA,CAAE,CAAA,OAC1C,CAAA;;;;;;;;;;UAWW,QAAA;EAAA,SACN,IAAA;EAAA,SACA,GAAA;EACT,GAAA,IAAO,YAAA,CAAa,CAAA;EAfwB;;;;EAoB5C,GAAA,CAAI,IAAA,EAAM,YAAA,CAAa,CAAA;EACvB,SAAA,CAAU,EAAA,GAAK,IAAA,EAAM,YAAA,CAAa,CAAA;AAAA"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
interface Props {
|
|
3
|
-
wormhole: { readonly name: string; readonly key: string; get(): unknown };
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
const { wormhole } = Astro.props;
|
|
7
|
-
const data = wormhole.get();
|
|
8
|
-
const key = wormhole.key;
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
{
|
|
12
|
-
data ? (
|
|
13
|
-
<script is:inline define:vars={{ data, key }}>
|
|
14
|
-
globalThis[key] = function(){return data;}
|
|
15
|
-
</script>
|
|
16
|
-
) : (
|
|
17
|
-
<script is:inline define:vars={{ wormholeName: wormhole.name }}>
|
|
18
|
-
console.error(`[wormhole] "${wormholeName}" has no data — is the middleware configured?`);
|
|
19
|
-
</script>
|
|
20
|
-
)
|
|
21
|
-
}
|
package/src/astro/index.ts
DELETED