@angular-helpers/worker-http 0.2.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 +399 -0
- package/fesm2022/angular-helpers-worker-http-backend.mjs +3 -0
- package/fesm2022/angular-helpers-worker-http-crypto.mjs +146 -0
- package/fesm2022/angular-helpers-worker-http-interceptors.mjs +453 -0
- package/fesm2022/angular-helpers-worker-http-serializer.mjs +180 -0
- package/fesm2022/angular-helpers-worker-http-transport.mjs +116 -0
- package/fesm2022/angular-helpers-worker-http.mjs +20 -0
- package/package.json +81 -0
- package/types/angular-helpers-worker-http-backend.d.ts +42 -0
- package/types/angular-helpers-worker-http-crypto.d.ts +127 -0
- package/types/angular-helpers-worker-http-interceptors.d.ts +244 -0
- package/types/angular-helpers-worker-http-serializer.d.ts +89 -0
- package/types/angular-helpers-worker-http-transport.d.ts +115 -0
- package/types/angular-helpers-worker-http.d.ts +16 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serialized payload ready for postMessage transfer.
|
|
3
|
+
*/
|
|
4
|
+
interface SerializedPayload {
|
|
5
|
+
/** Serialized data (may be string, ArrayBuffer, or structured-clone-safe object) */
|
|
6
|
+
data: unknown;
|
|
7
|
+
/** Transferable objects to pass to postMessage (zero-copy) */
|
|
8
|
+
transferables: Transferable[];
|
|
9
|
+
/** Format identifier for deserialization */
|
|
10
|
+
format: 'structured-clone' | 'toon' | 'seroval' | 'custom';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Pluggable serializer interface for crossing the worker boundary.
|
|
14
|
+
*/
|
|
15
|
+
interface WorkerSerializer {
|
|
16
|
+
/** Serialize data for transfer to/from worker */
|
|
17
|
+
serialize(data: unknown): SerializedPayload;
|
|
18
|
+
/** Deserialize data received from worker/main thread */
|
|
19
|
+
deserialize(payload: SerializedPayload): unknown;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Strategy for automatic serializer selection based on payload shape.
|
|
23
|
+
*/
|
|
24
|
+
type SerializerStrategy = 'auto' | 'structured-clone' | 'toon' | 'seroval' | 'custom';
|
|
25
|
+
/**
|
|
26
|
+
* Configuration for the auto-detect serializer.
|
|
27
|
+
*/
|
|
28
|
+
interface AutoSerializerConfig {
|
|
29
|
+
/** Size threshold (bytes) above which to use ArrayBuffer transfer (default: 102400 = 100 KiB) */
|
|
30
|
+
transferThreshold?: number;
|
|
31
|
+
/** Custom serializer to use when strategy is 'custom' */
|
|
32
|
+
custom?: WorkerSerializer;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Default serializer that relies on the browser's native structured clone algorithm.
|
|
37
|
+
* Zero overhead — data is passed directly to postMessage without transformation.
|
|
38
|
+
*
|
|
39
|
+
* Best for: small payloads (< 100 KiB), simple types.
|
|
40
|
+
* Limitations: cannot handle functions, DOM nodes, or class instances with prototype chains.
|
|
41
|
+
*/
|
|
42
|
+
declare const structuredCloneSerializer: WorkerSerializer;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates a `WorkerSerializer` backed by `seroval` for full type fidelity.
|
|
46
|
+
*
|
|
47
|
+
* Supports `Date`, `Map`, `Set`, `BigInt`, `RegExp`, circular references, and more.
|
|
48
|
+
* The factory is async because it dynamically imports the optional `seroval` peer.
|
|
49
|
+
*
|
|
50
|
+
* `seroval` must be installed separately:
|
|
51
|
+
* ```
|
|
52
|
+
* npm install seroval
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const serializer = await createSerovalSerializer();
|
|
58
|
+
* const payload = serializer.serialize({ date: new Date(), map: new Map() });
|
|
59
|
+
* const original = serializer.deserialize(payload);
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @see https://github.com/lxsmnsyc/seroval
|
|
63
|
+
*/
|
|
64
|
+
declare function createSerovalSerializer(): Promise<WorkerSerializer>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Creates an auto-detecting `WorkerSerializer` that picks the best strategy per payload.
|
|
68
|
+
*
|
|
69
|
+
* The factory is async because it pre-loads `seroval` during initialization
|
|
70
|
+
* so the returned serializer methods are fully synchronous (no await in hot path).
|
|
71
|
+
*
|
|
72
|
+
* Strategy selection per `serialize()` call:
|
|
73
|
+
* - Contains `Date`, `Map`, `Set`, or `RegExp` at depth-1 → `seroval` (full fidelity)
|
|
74
|
+
* - Otherwise → structured clone (native, zero overhead)
|
|
75
|
+
*
|
|
76
|
+
* Large payloads (> `transferThreshold`, default 100 KiB) are encoded to
|
|
77
|
+
* `ArrayBuffer` and added to `transferables` for zero-copy `postMessage` transfer.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const auto = await createAutoSerializer();
|
|
82
|
+
* const payload = auto.serialize({ users, fetchedAt: new Date() });
|
|
83
|
+
* worker.postMessage({ payload }, payload.transferables);
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare function createAutoSerializer(config?: AutoSerializerConfig): Promise<WorkerSerializer>;
|
|
87
|
+
|
|
88
|
+
export { createAutoSerializer, createSerovalSerializer, structuredCloneSerializer };
|
|
89
|
+
export type { AutoSerializerConfig, SerializedPayload, SerializerStrategy, WorkerSerializer };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for creating a worker transport.
|
|
5
|
+
*
|
|
6
|
+
* IMPORTANT: Angular CLI's esbuild bundler only detects web workers when
|
|
7
|
+
* `new Worker(new URL(...))` appears directly in application source code.
|
|
8
|
+
* For pre-transpiled workers, use `workerUrl` instead.
|
|
9
|
+
*/
|
|
10
|
+
interface WorkerTransportConfig {
|
|
11
|
+
/**
|
|
12
|
+
* Factory function that creates a new Worker instance.
|
|
13
|
+
* The `new Worker(new URL(...))` call MUST be in your app code (not a library)
|
|
14
|
+
* for Angular CLI to bundle the worker correctly.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* workerFactory: () => new Worker(new URL('./echo.worker.ts', import.meta.url), { type: 'module' })
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
workerFactory?: () => Worker;
|
|
22
|
+
/**
|
|
23
|
+
* URL to a pre-transpiled worker file.
|
|
24
|
+
* Use this when workers are built separately (e.g., with Vite) and distributed
|
|
25
|
+
* as static assets. Resolves against the document's base URI.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* workerUrl: 'assets/workers/echo.worker.js'
|
|
30
|
+
* // or with full URL
|
|
31
|
+
* workerUrl: new URL('workers/echo.worker.js', import.meta.url).href
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
workerUrl?: string | URL;
|
|
35
|
+
/** Maximum number of worker instances in the pool (default: 1) */
|
|
36
|
+
maxInstances?: number;
|
|
37
|
+
/** Transfer strategy for large payloads */
|
|
38
|
+
transferDetection?: 'auto' | 'manual' | 'none';
|
|
39
|
+
/** Timeout in ms for a single request (default: 30000) */
|
|
40
|
+
requestTimeout?: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Message sent from main thread to worker.
|
|
44
|
+
*/
|
|
45
|
+
interface WorkerMessage<TPayload = unknown> {
|
|
46
|
+
type: 'request' | 'cancel';
|
|
47
|
+
requestId: string;
|
|
48
|
+
payload?: TPayload;
|
|
49
|
+
transferables?: Transferable[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Successful response from worker to main thread.
|
|
53
|
+
*/
|
|
54
|
+
interface WorkerResponse<TResult = unknown> {
|
|
55
|
+
type: 'response';
|
|
56
|
+
requestId: string;
|
|
57
|
+
result: TResult;
|
|
58
|
+
transferables?: Transferable[];
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Error response from worker to main thread.
|
|
62
|
+
*/
|
|
63
|
+
interface WorkerErrorResponse {
|
|
64
|
+
type: 'error';
|
|
65
|
+
requestId: string;
|
|
66
|
+
error: {
|
|
67
|
+
message: string;
|
|
68
|
+
name: string;
|
|
69
|
+
stack?: string;
|
|
70
|
+
status?: number;
|
|
71
|
+
statusText?: string;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Typed transport interface for communicating with a web worker.
|
|
76
|
+
* Observable-based: unsubscribing sends a cancel message to the worker.
|
|
77
|
+
*/
|
|
78
|
+
interface WorkerTransport<TRequest = unknown, TResponse = unknown> {
|
|
79
|
+
/** Send a request to the worker and get an Observable response */
|
|
80
|
+
execute(request: TRequest): Observable<TResponse>;
|
|
81
|
+
/** Terminate all workers and release resources */
|
|
82
|
+
terminate(): void;
|
|
83
|
+
/** Whether the transport has active workers */
|
|
84
|
+
readonly isActive: boolean;
|
|
85
|
+
/** Number of currently active worker instances */
|
|
86
|
+
readonly activeInstances: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a typed, Observable-based transport for communicating with a web worker.
|
|
91
|
+
*
|
|
92
|
+
* Features:
|
|
93
|
+
* - Request/response correlation via `requestId`
|
|
94
|
+
* - Automatic cancellation on Observable unsubscribe
|
|
95
|
+
* - Optional worker pool with round-robin dispatch
|
|
96
|
+
* - Lazy worker creation (default)
|
|
97
|
+
* - Transferable auto-detection for ArrayBuffer payloads
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const transport = createWorkerTransport<MyRequest, MyResponse>({
|
|
102
|
+
* workerFactory: () => new Worker(new URL('./my.worker.ts', import.meta.url), { type: 'module' }),
|
|
103
|
+
* maxInstances: 2,
|
|
104
|
+
* });
|
|
105
|
+
*
|
|
106
|
+
* transport.execute(request).subscribe({
|
|
107
|
+
* next: (response) => console.log(response),
|
|
108
|
+
* error: (err) => console.error(err),
|
|
109
|
+
* });
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
declare function createWorkerTransport<TRequest = unknown, TResponse = unknown>(config: WorkerTransportConfig): WorkerTransport<TRequest, TResponse>;
|
|
113
|
+
|
|
114
|
+
export { createWorkerTransport };
|
|
115
|
+
export type { WorkerErrorResponse, WorkerMessage, WorkerResponse, WorkerTransport, WorkerTransportConfig };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @angular-helpers/worker-http
|
|
3
|
+
*
|
|
4
|
+
* Angular HTTP over Web Workers — off-main-thread HTTP pipelines
|
|
5
|
+
* with configurable interceptors, WebCrypto security, and pluggable serialization.
|
|
6
|
+
*
|
|
7
|
+
* Sub-entry points:
|
|
8
|
+
* - @angular-helpers/worker-http/transport (P1: typed RPC bridge)
|
|
9
|
+
* - @angular-helpers/worker-http/serializer (P2: TOON, seroval, auto-detect)
|
|
10
|
+
* - @angular-helpers/worker-http/backend (P3: Angular HttpBackend replacement)
|
|
11
|
+
* - @angular-helpers/worker-http/interceptors (P4: pure-fn interceptors for workers)
|
|
12
|
+
* - @angular-helpers/worker-http/crypto (P5: WebCrypto primitives)
|
|
13
|
+
*/
|
|
14
|
+
declare const WORKER_HTTP_VERSION = "0.0.1";
|
|
15
|
+
|
|
16
|
+
export { WORKER_HTTP_VERSION };
|