postalworker-es 4.0.8 → 5.1.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 +171 -35
- package/dist/PostalSharedWorker.js +415 -511
- 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 +113 -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 +901 -0
- package/dist/index.js.map +7 -0
- package/dist/protocol.d.ts +154 -0
- package/dist/types.d.ts +92 -0
- package/dist/worker-script.d.ts +48 -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/dist/core.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostalWorker v5 core: one instance per tab (or per channel name).
|
|
3
|
+
*
|
|
4
|
+
* tab ──┐ ┌── tab
|
|
5
|
+
* ├── BroadcastChannel ───┤
|
|
6
|
+
* tab ──┘ │ └── tab
|
|
7
|
+
* SharedWorker (or one elected dedicated Worker when SharedWorker is unavailable)
|
|
8
|
+
*
|
|
9
|
+
* iframes / popups / other origins ── window.postMessage (see cross.ts)
|
|
10
|
+
*/
|
|
11
|
+
import { type CrossTarget } from "./cross";
|
|
12
|
+
import { WORKER_ID } from "./protocol";
|
|
13
|
+
import type { AnySchema, Audience, BoxItem, BoxesOf, CrossMeta, EventsOf, FetchPackageOptions, KeyOf, MessageMeta, PackageItem, PackagesOf, Schema, Unsubscribe } from "./types";
|
|
14
|
+
export type WorkerMode = "shared" | "dedicated" | "none";
|
|
15
|
+
export interface PostalConfig {
|
|
16
|
+
/**
|
|
17
|
+
* URL of the worker script (`dist/PostalSharedWorker.js`, served from your site).
|
|
18
|
+
* `false` runs without a worker: tabs, iframes and popups still talk, but
|
|
19
|
+
* postBox, package and load() need the worker.
|
|
20
|
+
*/
|
|
21
|
+
workerUrl: string | URL | false;
|
|
22
|
+
/** Name of the BroadcastChannel and worker. Instances with different names are isolated. Default `"PostalWorker"`. */
|
|
23
|
+
channelName?: string;
|
|
24
|
+
/**
|
|
25
|
+
* What to do where SharedWorker is unavailable (e.g. Samsung Internet, iOS < 16).
|
|
26
|
+
* `"dedicated"` (default) elects one tab with Web Locks to host a dedicated Worker
|
|
27
|
+
* that every tab shares; another tab takes over when it closes.
|
|
28
|
+
* `"none"` runs without a worker there.
|
|
29
|
+
*/
|
|
30
|
+
fallback?: "dedicated" | "none";
|
|
31
|
+
/**
|
|
32
|
+
* Extra origins allowed to send cross-window messages, or `"*"` to accept any origin.
|
|
33
|
+
* Your own origin, your parent/opener, iframes you embed and windows you open are trusted already.
|
|
34
|
+
*/
|
|
35
|
+
allowedOrigins?: "*" | readonly string[];
|
|
36
|
+
/** Milliseconds to wait for the worker before sending queued messages anyway. Default 5000. */
|
|
37
|
+
readyTimeout?: number;
|
|
38
|
+
/** Called for worker errors (e.g. a load() script failed). Defaults to console.error. */
|
|
39
|
+
onError?: (error: Error) => void;
|
|
40
|
+
}
|
|
41
|
+
export type EventHandler<T> = (message: T, meta: MessageMeta) => void;
|
|
42
|
+
export type CrossHandler<T> = (message: T, meta: CrossMeta) => void;
|
|
43
|
+
export interface CrossOnOptions {
|
|
44
|
+
/** Window name passed to window.open (default `"_blank"`). */
|
|
45
|
+
name?: string;
|
|
46
|
+
/** window.open features, as a string or object: `{ popup: true, width: 400 }`. Do not set `noopener`. */
|
|
47
|
+
features?: string | Record<string, string | number | boolean>;
|
|
48
|
+
}
|
|
49
|
+
/** The PostalWorker instance API. */
|
|
50
|
+
export interface PostalWorker<S extends Schema = AnySchema> {
|
|
51
|
+
/** Unique id of this tab's instance. Use it with `fire(…, { to: id })`. */
|
|
52
|
+
readonly id: string;
|
|
53
|
+
/** How this tab reaches the worker. */
|
|
54
|
+
readonly mode: WorkerMode;
|
|
55
|
+
/** True once the worker has answered. */
|
|
56
|
+
readonly workerOnline: boolean;
|
|
57
|
+
/** True when this window is inside an iframe. */
|
|
58
|
+
readonly isFramed: boolean;
|
|
59
|
+
/** Resolves `true` when the worker is online, `false` if there is none or it never answered. */
|
|
60
|
+
ready(): Promise<boolean>;
|
|
61
|
+
/** Listen for a message class. Returns an unsubscribe function. */
|
|
62
|
+
on<K extends KeyOf<EventsOf<S>>>(msgClass: K, handler: EventHandler<EventsOf<S>[K]>): Unsubscribe;
|
|
63
|
+
/** Remove one handler, or every handler for the class. */
|
|
64
|
+
off<K extends KeyOf<EventsOf<S>>>(msgClass: K, handler?: EventHandler<EventsOf<S>[K]>): void;
|
|
65
|
+
/** Send a message. Default audience `"all"`: this tab, other tabs and the worker. */
|
|
66
|
+
fire<K extends KeyOf<EventsOf<S>>>(msgClass: K, message: EventsOf<S>[K], audience?: Audience): void;
|
|
67
|
+
/**
|
|
68
|
+
* Listen for cross-window messages (iframes, popups, other origins).
|
|
69
|
+
* With `url`, also opens that window and trusts it.
|
|
70
|
+
*/
|
|
71
|
+
crossOn<K extends KeyOf<EventsOf<S>>>(msgClass: K, handler: CrossHandler<EventsOf<S>[K]>, url?: string | URL, options?: CrossOnOptions): Unsubscribe;
|
|
72
|
+
/** Remove one cross handler, or every cross handler for the class. */
|
|
73
|
+
crossOff<K extends KeyOf<EventsOf<S>>>(msgClass: K, handler?: CrossHandler<EventsOf<S>[K]>): void;
|
|
74
|
+
/** Send to every connected iframe, parent, popup and opener. */
|
|
75
|
+
crossFire<K extends KeyOf<EventsOf<S>>>(msgClass: K, message: EventsOf<S>[K]): void;
|
|
76
|
+
/** Send to the parent window only (inside an iframe). */
|
|
77
|
+
backFire<K extends KeyOf<EventsOf<S>>>(msgClass: K, message: EventsOf<S>[K]): void;
|
|
78
|
+
/** `fire(…, "all")` plus `crossFire`. */
|
|
79
|
+
fireAll<K extends KeyOf<EventsOf<S>>>(msgClass: K, message: EventsOf<S>[K]): void;
|
|
80
|
+
/** Close (if opened) and forget windows at this url's origin. */
|
|
81
|
+
closeWindows(urlOrOrigin: string | URL): void;
|
|
82
|
+
/** Windows currently connected through cross-window messaging. */
|
|
83
|
+
windows(): ReadonlyArray<Pick<CrossTarget, "id" | "origin" | "relation">>;
|
|
84
|
+
/** Load a classic script into the worker. It must define `self.init = (pw) => { … }`. */
|
|
85
|
+
load(url: string | URL): void;
|
|
86
|
+
/** Open a shared value box. The handler gets the current value, then every change. Returns closeBox. */
|
|
87
|
+
postBox<K extends KeyOf<BoxesOf<S>>>(address: K, handler: (item: BoxItem<BoxesOf<S>, K>) => void): Unsubscribe;
|
|
88
|
+
/** Set a box's value for everyone. */
|
|
89
|
+
post<K extends KeyOf<BoxesOf<S>>>(address: K, value: BoxesOf<S>[K]): void;
|
|
90
|
+
/** Close a box in this tab. Its handlers get one last call with `value: null, closed: true`. */
|
|
91
|
+
closeBox<K extends KeyOf<BoxesOf<S>>>(address: K): void;
|
|
92
|
+
/** Receive packaged content (any structured-cloneable value, e.g. a Blob). Returns an unsubscribe function. */
|
|
93
|
+
package<K extends KeyOf<PackagesOf<S>>>(address: K, handler: (item: PackageItem<PackagesOf<S>, K>) => void): Unsubscribe;
|
|
94
|
+
/** Store content in a package and deliver it to every subscriber. */
|
|
95
|
+
pack<K extends KeyOf<PackagesOf<S>>>(address: K, content: PackagesOf<S>[K]): void;
|
|
96
|
+
/**
|
|
97
|
+
* Have the worker fetch `url` and cache the result as the package's content.
|
|
98
|
+
* However many tabs ask, the url is fetched once; later subscribers get the cached copy.
|
|
99
|
+
* Content is delivered only to tabs subscribed with `package()`. A failed fetch is
|
|
100
|
+
* reported through `onError` in the tabs that asked.
|
|
101
|
+
*/
|
|
102
|
+
fetchPackage<K extends KeyOf<PackagesOf<S>>>(address: K, url: string | URL, options?: FetchPackageOptions): void;
|
|
103
|
+
/** Release a package's cached content in the worker. Subscribers get `content: null`. */
|
|
104
|
+
unpack<K extends KeyOf<PackagesOf<S>>>(address: K): void;
|
|
105
|
+
/** Stop everything: leave the network, close the channel, release the worker. */
|
|
106
|
+
destroy(): void;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Create a PostalWorker instance. Call once per tab (per channel name) and share it.
|
|
110
|
+
* Safe to call during server-side rendering: it returns an instance that does nothing.
|
|
111
|
+
*/
|
|
112
|
+
export declare function createPostalWorker<S extends Schema = AnySchema>(config: PostalConfig): PostalWorker<S>;
|
|
113
|
+
export { WORKER_ID };
|
package/dist/cross.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface CrossTarget {
|
|
2
|
+
id: string;
|
|
3
|
+
win: Window;
|
|
4
|
+
origin: string;
|
|
5
|
+
relation: "parent" | "opener" | "child" | "opened" | "peer";
|
|
6
|
+
}
|
|
7
|
+
export interface CrossWindowsOptions {
|
|
8
|
+
selfId: string;
|
|
9
|
+
allowedOrigins: "*" | readonly string[];
|
|
10
|
+
onMessage(msgClass: string, message: unknown, from: string, origin: string, back: boolean): void;
|
|
11
|
+
}
|
|
12
|
+
export declare class CrossWindows {
|
|
13
|
+
#private;
|
|
14
|
+
constructor(opts: CrossWindowsOptions);
|
|
15
|
+
get isFramed(): boolean;
|
|
16
|
+
/** Ask every child frame to say hello. */
|
|
17
|
+
probeFrames(): void;
|
|
18
|
+
/** Open a window and trust it (only that window, only at that url's origin). */
|
|
19
|
+
open(url: string | URL, name: string, features: string): Window | null;
|
|
20
|
+
/** Send to every registered window (crossFire). */
|
|
21
|
+
send(msgClass: string, message: unknown): void;
|
|
22
|
+
/** Send to the parent window only (backFire). Returns false when not framed. */
|
|
23
|
+
sendBack(msgClass: string, message: unknown): boolean;
|
|
24
|
+
/** Close and forget every window registered at this origin (or url's origin). */
|
|
25
|
+
closeOrigin(urlOrOrigin: string | URL): void;
|
|
26
|
+
targets(): CrossTarget[];
|
|
27
|
+
/** Tell every registered window we are leaving. */
|
|
28
|
+
sayGoodbye(): void;
|
|
29
|
+
destroy(): void;
|
|
30
|
+
}
|
|
31
|
+
/** `{ width: 400, popup: true }` -> `"width=400,popup=yes"` */
|
|
32
|
+
export declare function windowFeatures(features: Record<string, string | number | boolean> | string | undefined): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal handler registry: any number of handlers per key.
|
|
3
|
+
* Shared by the window library and the worker. A throwing handler is
|
|
4
|
+
* reported but never stops the other handlers.
|
|
5
|
+
*/
|
|
6
|
+
export type AnyHandler = (...args: never[]) => void;
|
|
7
|
+
export declare class Registry<H extends AnyHandler> {
|
|
8
|
+
#private;
|
|
9
|
+
add(key: string, handler: H): () => void;
|
|
10
|
+
remove(key: string, handler?: H): void;
|
|
11
|
+
has(key: string): boolean;
|
|
12
|
+
keys(): string[];
|
|
13
|
+
clear(): void;
|
|
14
|
+
emit(key: string, ...args: Parameters<H>): void;
|
|
15
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostalWorker v5: typed messaging between tabs, iframes, popups and a SharedWorker.
|
|
3
|
+
*
|
|
4
|
+
* import { createPostalWorker } from "postalworker-es"; // v5
|
|
5
|
+
* import PostalWorker from "postalworker-es"; // v4-compatible singleton
|
|
6
|
+
*/
|
|
7
|
+
export { createPostalWorker, WORKER_ID } from "./core";
|
|
8
|
+
export type { PostalWorker, PostalConfig, WorkerMode, EventHandler, CrossHandler, CrossOnOptions, } from "./core";
|
|
9
|
+
export type { Schema, AnySchema, EventMap, EventsOf, BoxesOf, PackagesOf, Audience, MessageMeta, CrossMeta, BoxItem, PackageItem, FetchPackageOptions, Unsubscribe, KeyOf, } from "./types";
|
|
10
|
+
export type { LegacyPostalWorker, LegacyConfig, LegacyAudience } from "./compat";
|
|
11
|
+
export { default } from "./compat";
|