@astroscope/wormhole 0.1.0 → 0.1.2
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 +5 -15
- package/dist/index.d.ts +7 -1
- package/dist/index.js +5 -0
- package/dist/react/index.d.ts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/types-hCfcC5bZ.d.ts +19 -0
- package/package.json +3 -3
- package/dist/define.d.ts +0 -5
- package/dist/define.js +0 -29
- package/dist/react.d.ts +0 -5
- package/dist/react.js +0 -8
- package/dist/types-DkH89ULF.d.ts +0 -9
package/README.md
CHANGED
|
@@ -18,6 +18,11 @@ Astro recommends [nanostores](https://docs.astro.build/en/recipes/sharing-state/
|
|
|
18
18
|
- Server-loaded configuration (theme, locale, permissions) bridged to the UI
|
|
19
19
|
- Any request-scoped data that multiple disconnected islands need to read
|
|
20
20
|
|
|
21
|
+
## Important notes
|
|
22
|
+
|
|
23
|
+
- **No secrets in wormholes.** All wormhole data is serialized into an inline `<script>` tag and sent to the browser. Never store tokens, API keys, credentials, or any sensitive data in a wormhole.
|
|
24
|
+
- **`set()` is client-only.** Calling `set()` does not work on the server. Use `open(wormhole, data, fn)` from `@astroscope/wormhole/server` to populate data in middleware.
|
|
25
|
+
|
|
21
26
|
## Examples
|
|
22
27
|
|
|
23
28
|
See the [demo/wormhole](../../demo/wormhole) directory for a working example.
|
|
@@ -218,21 +223,6 @@ import {WormholeScript} from '@astroscope/wormhole/astro';
|
|
|
218
223
|
3. **Client** calls `wh.get()` — reads from `globalThis[key]()` (the serialized getter)
|
|
219
224
|
4. **`set()`** updates a local store and notifies all subscribers — `useWormhole()` hooks and `subscribe()` callbacks re-render/fire
|
|
220
225
|
|
|
221
|
-
## Monorepo note
|
|
222
|
-
|
|
223
|
-
When using this package from a workspace in a monorepo, add `resolve.dedupe` to your Vite config to prevent duplicate React instances:
|
|
224
|
-
|
|
225
|
-
```ts
|
|
226
|
-
// astro.config.ts
|
|
227
|
-
export default defineConfig({
|
|
228
|
-
vite: {
|
|
229
|
-
resolve: {
|
|
230
|
-
dedupe: ['react', 'react-dom'],
|
|
231
|
-
},
|
|
232
|
-
},
|
|
233
|
-
});
|
|
234
|
-
```
|
|
235
|
-
|
|
236
226
|
## License
|
|
237
227
|
|
|
238
228
|
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import { W as Wormhole } from './types-
|
|
1
|
+
import { W as Wormhole } from './types-hCfcC5bZ.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Define a wormhole that transfers state from server middleware to client components.
|
|
5
|
+
*
|
|
6
|
+
* **Security:** wormhole data is serialized into the HTML and sent to the browser.
|
|
7
|
+
* Never store secrets (tokens, API keys, credentials) in a wormhole.
|
|
8
|
+
*/
|
|
3
9
|
declare function defineWormhole<T>(name: string): Wormhole<T>;
|
|
4
10
|
|
|
5
11
|
export { Wormhole, defineWormhole };
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,11 @@ function defineWormhole(name) {
|
|
|
13
13
|
throw new Error(`wormhole "${name}" is not initialized`);
|
|
14
14
|
},
|
|
15
15
|
set(data) {
|
|
16
|
+
if (typeof globalThis.window === "undefined") {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`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`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
16
21
|
store = data;
|
|
17
22
|
for (const fn of listeners) fn(data);
|
|
18
23
|
},
|
package/dist/react/index.d.ts
CHANGED
package/dist/server.d.ts
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astroscope/wormhole",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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",
|
|
@@ -52,8 +52,8 @@
|
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "tsup src/index.ts src/server.ts src/react/index.ts --format esm --dts",
|
|
54
54
|
"typecheck": "tsc --noEmit",
|
|
55
|
-
"lint": "eslint
|
|
56
|
-
"lint:fix": "eslint
|
|
55
|
+
"lint": "eslint src",
|
|
56
|
+
"lint:fix": "eslint src --fix"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@types/react": "^19.2.13",
|
package/dist/define.d.ts
DELETED
package/dist/define.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// src/define.ts
|
|
2
|
-
function defineWormhole(name) {
|
|
3
|
-
const key = `__wormhole_${name}__`;
|
|
4
|
-
let store;
|
|
5
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
6
|
-
return {
|
|
7
|
-
name,
|
|
8
|
-
key,
|
|
9
|
-
get() {
|
|
10
|
-
if (store !== void 0) return store;
|
|
11
|
-
const getter = globalThis[key];
|
|
12
|
-
if (typeof getter === "function") return getter();
|
|
13
|
-
throw new Error(`wormhole "${name}" is not initialized`);
|
|
14
|
-
},
|
|
15
|
-
set(data) {
|
|
16
|
-
store = data;
|
|
17
|
-
for (const fn of listeners) fn(data);
|
|
18
|
-
},
|
|
19
|
-
subscribe(fn) {
|
|
20
|
-
listeners.add(fn);
|
|
21
|
-
return () => {
|
|
22
|
-
listeners.delete(fn);
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
export {
|
|
28
|
-
defineWormhole
|
|
29
|
-
};
|
package/dist/react.d.ts
DELETED
package/dist/react.js
DELETED