postalworker-es 4.0.8 → 5.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 +152 -35
- package/dist/PostalSharedWorker.js +305 -513
- package/dist/PostalSharedWorker.js.map +7 -0
- package/dist/PostalWorker.min.js +2 -171
- package/dist/PostalWorker.min.js.map +7 -0
- package/dist/compat.d.ts +53 -0
- package/dist/core.d.ts +104 -0
- package/dist/cross.d.ts +32 -0
- package/dist/emitter.d.ts +15 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +860 -0
- package/dist/index.js.map +7 -0
- package/dist/protocol.d.ts +125 -0
- package/dist/types.d.ts +77 -0
- package/dist/worker-script.d.ts +44 -0
- package/dist/worker-script.js +8 -0
- package/dist/worker-script.js.map +7 -0
- package/package.json +47 -71
- package/dist/PolyfillSharedWorker.js +0 -525
- package/dist/PostalWorker.es.js +0 -408
package/README.md
CHANGED
|
@@ -1,54 +1,171 @@
|
|
|
1
1
|
# PostalWorker
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Typed messaging between tabs, iframes, popups and a SharedWorker.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- **Tabs** of the same site talk over a `BroadcastChannel`.
|
|
6
|
+
- A **SharedWorker** holds shared state (post boxes, packages) and runs scripts you load into it. Where SharedWorker is missing, one tab is elected to host a dedicated Worker for everyone, and another tab takes over when it closes.
|
|
7
|
+
- **Iframes, popups and other origins** talk over `window.postMessage`, sent only to exact, trusted origins.
|
|
8
|
+
- With a schema type, every message class, payload, box and package is checked by TypeScript.
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
```sh
|
|
11
|
+
npm install postalworker-es
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Serve `node_modules/postalworker-es/dist/PostalSharedWorker.js` from your site (copy it to your public folder, or import it with your bundler's "asset URL" feature).
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createPostalWorker } from "postalworker-es";
|
|
20
|
+
|
|
21
|
+
type AppSchema = {
|
|
22
|
+
events: { "cart:updated": { items: number }; ping: number };
|
|
23
|
+
boxes: { volume: number };
|
|
24
|
+
packages: { avatar: Blob };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const postal = createPostalWorker<AppSchema>({
|
|
28
|
+
workerUrl: "/PostalSharedWorker.js",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// any tab
|
|
32
|
+
const off = postal.on("cart:updated", (msg, { from }) => {
|
|
33
|
+
console.log(msg.items, "from", from);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// any tab
|
|
37
|
+
postal.fire("cart:updated", { items: 3 }); // ✅
|
|
38
|
+
postal.fire("cart:updated", { items: "3" }); // ❌ compile error
|
|
39
|
+
off();
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Create one instance per tab and share it.
|
|
43
|
+
|
|
44
|
+
## Events and audiences
|
|
8
45
|
|
|
9
|
-
|
|
46
|
+
`fire(msgClass, message, audience?)` delivers to:
|
|
10
47
|
|
|
48
|
+
| audience | this tab | other tabs | worker |
|
|
49
|
+
|---|---|---|---|
|
|
50
|
+
| `"all"` (default) | ✓ | ✓ | ✓ |
|
|
51
|
+
| `"others"` | | ✓ | ✓ |
|
|
52
|
+
| `"local"` | ✓ | | |
|
|
53
|
+
| `"worker"` | | | ✓ |
|
|
54
|
+
| `{ to: id }` | only the tab whose `postal.id` is `id` | | |
|
|
55
|
+
|
|
56
|
+
`on()` allows any number of handlers per message class and returns an unsubscribe function. `off(msgClass, handler?)` removes one handler, or all of them. A throwing handler never stops the others. Every delivery is asynchronous, so a tab sees its own messages in the same order as everyone else.
|
|
57
|
+
|
|
58
|
+
## Post boxes: shared values
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const close = postal.postBox("volume", ({ value, box, closed }) => {
|
|
62
|
+
// called with the current value (null if nothing was posted), then on every change
|
|
63
|
+
});
|
|
64
|
+
postal.post("volume", 11); // every tab with the box open is updated
|
|
65
|
+
postal.closeBox("volume"); // or close(): one last call with value null, closed true
|
|
11
66
|
```
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
67
|
+
|
|
68
|
+
Values live in the worker, so a tab opening the box later gets the current value.
|
|
69
|
+
|
|
70
|
+
## Packages: shared content
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
postal.package("avatar", ({ content }) => content && show(URL.createObjectURL(content)));
|
|
74
|
+
postal.pack("avatar", await (await fetch("/me.png")).blob());
|
|
19
75
|
```
|
|
20
76
|
|
|
21
|
-
|
|
77
|
+
Content can be anything structured-cloneable (Blob, ArrayBuffer, objects…).
|
|
22
78
|
|
|
23
|
-
|
|
24
|
-
|
|
79
|
+
## Worker scripts
|
|
80
|
+
|
|
81
|
+
Load classic scripts into the worker. They run once, however many tabs ask, and share the worker's state.
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
postal.load("/workers/echo.js");
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
// workers/echo.ts — bundle as an IIFE / classic script
|
|
89
|
+
import { defineWorkerScript } from "postalworker-es/worker-script";
|
|
90
|
+
import type { AppSchema } from "../schema";
|
|
25
91
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
92
|
+
defineWorkerScript<AppSchema>((pw) => {
|
|
93
|
+
pw.on("ping", (n, { from }) => pw.fire("ping", n + 1, { to: from }));
|
|
94
|
+
pw.subscribe("/events", (e) => pw.post("volume", Number(e.data))); // Server-Sent Events, shared by all tabs
|
|
29
95
|
});
|
|
96
|
+
```
|
|
30
97
|
|
|
31
|
-
|
|
32
|
-
PostalWorker().fire("example", "This is an example");
|
|
98
|
+
Plain JavaScript works too: `self.init = (pw) => { … }`. v4 scripts that call `PostalWorker().on(…)` keep working.
|
|
33
99
|
|
|
34
|
-
|
|
100
|
+
If a script fails to load, the tab that loaded it gets an error (see `onError`).
|
|
35
101
|
|
|
36
|
-
|
|
37
|
-
PostalWorker().crossOn(
|
|
38
|
-
"crossExample",
|
|
39
|
-
function (msg) {
|
|
40
|
-
window.console.info(msg);
|
|
41
|
-
},
|
|
42
|
-
"http://myurl", // This is where you specify the url you want to open to (it must have a copy of PostalWorker hosted too)
|
|
43
|
-
"MyURL", // This is where you name the new window if you want to interact with it directly with Javascript - limited by cross origin rules of course)
|
|
44
|
-
false // The last argument is optional and it includes more specific window parameters like width, height, etc...
|
|
45
|
-
);
|
|
102
|
+
## Iframes, popups and other origins
|
|
46
103
|
|
|
47
|
-
|
|
48
|
-
|
|
104
|
+
```ts
|
|
105
|
+
// open a window and listen for its messages
|
|
106
|
+
postal.crossOn("chat", (msg, { origin }) => {}, "https://other.example/chat", { features: { popup: true, width: 400 } });
|
|
49
107
|
|
|
50
|
-
//
|
|
51
|
-
|
|
108
|
+
postal.crossFire("chat", "hello"); // to every connected iframe, parent, popup and opener
|
|
109
|
+
postal.backFire("chat", "hi parent"); // inside an iframe: to the parent only
|
|
110
|
+
postal.fireAll("chat", "everyone"); // fire("all") + crossFire
|
|
111
|
+
postal.closeWindows("https://other.example");
|
|
112
|
+
postal.windows(); // [{ id, origin, relation }]
|
|
52
113
|
```
|
|
53
114
|
|
|
54
|
-
|
|
115
|
+
The other window must run PostalWorker too. Windows connect with a short handshake, after which messages are sent to each window's exact origin, never `"*"`.
|
|
116
|
+
|
|
117
|
+
**Trusted by default:** your own origin, your parent (inside an iframe), your opener, iframes in your page (at their `src` origin) and windows you opened with `crossOn(url)` (at that url's origin). Messages from anything else are ignored with a console warning. To accept more, pass `allowedOrigins: ["https://partner.example"]`, or `"*"` to accept any origin.
|
|
118
|
+
|
|
119
|
+
## Configuration
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
createPostalWorker({
|
|
123
|
+
workerUrl: "/PostalSharedWorker.js", // or false to run without a worker
|
|
124
|
+
channelName: "PostalWorker", // instances with different names are isolated
|
|
125
|
+
fallback: "dedicated", // or "none" where SharedWorker is missing
|
|
126
|
+
allowedOrigins: [], // extra trusted origins, or "*"
|
|
127
|
+
readyTimeout: 5000, // ms before queued messages are sent anyway
|
|
128
|
+
onError: (error) => console.error(error),
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Other members: `postal.id`, `postal.mode` (`"shared" | "dedicated" | "none"`), `postal.workerOnline`, `await postal.ready()` (true once the worker answers), `postal.isFramed`, `postal.destroy()`.
|
|
133
|
+
|
|
134
|
+
Messages sent before the worker answers are queued and sent once it does. Without a worker, events and cross-window messaging still work; post boxes, packages and `load()` report an error.
|
|
135
|
+
|
|
136
|
+
On the server (SSR) `createPostalWorker` returns an instance that does nothing, so importing it is safe.
|
|
137
|
+
|
|
138
|
+
## Browser support
|
|
139
|
+
|
|
140
|
+
Needs `BroadcastChannel`. SharedWorker is used where available (current Chrome, Edge, Firefox, Safari 16+, including mobile). Elsewhere (e.g. Samsung Internet, iOS 15 and older) the dedicated-worker fallback needs Web Locks; without it the library runs without a worker.
|
|
141
|
+
|
|
142
|
+
## v4 compatibility
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import PostalWorker from "postalworker-es";
|
|
146
|
+
|
|
147
|
+
PostalWorker({ PostalRoute: "/js/" }) // folder serving PostalSharedWorker.js
|
|
148
|
+
.on("example", (msg) => console.info(msg));
|
|
149
|
+
PostalWorker().fire("example", "Hello world");
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
The default export is the v4 API: a singleton with the v4 method names, arguments and return values. `PostalWorker().v5` gives you the v5 instance underneath. For a `<script>` tag, use `dist/PostalWorker.min.js`, which sets `window.PostalWorker` and `window.createPostalWorker`.
|
|
153
|
+
|
|
154
|
+
See [CHANGELOG.md](CHANGELOG.md) for what changed and how to migrate.
|
|
155
|
+
|
|
156
|
+
## Development
|
|
157
|
+
|
|
158
|
+
```sh
|
|
159
|
+
npm install
|
|
160
|
+
npm run build # dist/ (esbuild + tsc declarations)
|
|
161
|
+
npm test # typecheck, type tests, unit tests, browser tests
|
|
162
|
+
npm run demo:react # copy the build into demos/react, then `cd demos/react && npm start`
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The browser tests run headless Chromium with Playwright (`npx playwright install chromium` the first time). They cover several tabs, the worker, the no-SharedWorker fallback including leader handover, cross-origin iframes, popups, origin trust and the v4 API.
|
|
166
|
+
|
|
167
|
+
The other folders in `demos/` are from v4 and have not been updated.
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
MIT
|