use-worker-directive 0.5.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/LICENSE +7 -0
- package/README.md +140 -0
- package/dist/cjs/development/client.cjs +111 -0
- package/dist/cjs/development/client.cjs.map +7 -0
- package/dist/cjs/development/compiler.cjs +60 -0
- package/dist/cjs/development/compiler.cjs.map +7 -0
- package/dist/cjs/development/runtime.cjs +21 -0
- package/dist/cjs/development/runtime.cjs.map +7 -0
- package/dist/cjs/development/server.cjs +106 -0
- package/dist/cjs/development/server.cjs.map +7 -0
- package/dist/cjs/development/setup.cjs +7 -0
- package/dist/cjs/development/setup.cjs.map +7 -0
- package/dist/cjs/production/client.cjs +1 -0
- package/dist/cjs/production/compiler.cjs +1 -0
- package/dist/cjs/production/runtime.cjs +1 -0
- package/dist/cjs/production/server.cjs +1 -0
- package/dist/cjs/production/setup.cjs +1 -0
- package/dist/esm/development/client.mjs +101 -0
- package/dist/esm/development/client.mjs.map +7 -0
- package/dist/esm/development/compiler.mjs +29 -0
- package/dist/esm/development/compiler.mjs.map +7 -0
- package/dist/esm/development/runtime.mjs +3 -0
- package/dist/esm/development/runtime.mjs.map +7 -0
- package/dist/esm/development/server.mjs +96 -0
- package/dist/esm/development/server.mjs.map +7 -0
- package/dist/esm/development/setup.mjs +5 -0
- package/dist/esm/development/setup.mjs.map +7 -0
- package/dist/esm/production/client.mjs +1 -0
- package/dist/esm/production/compiler.mjs +1 -0
- package/dist/esm/production/runtime.mjs +1 -0
- package/dist/esm/production/server.mjs +1 -0
- package/dist/esm/production/setup.mjs +1 -0
- package/dist/types/client/index.d.ts +3 -0
- package/dist/types/client/index.d.ts.map +1 -0
- package/dist/types/compiler/index.d.ts +10 -0
- package/dist/types/compiler/index.d.ts.map +1 -0
- package/dist/types/runtime/index.d.ts +2 -0
- package/dist/types/runtime/index.d.ts.map +1 -0
- package/dist/types/server/index.d.ts +5 -0
- package/dist/types/server/index.d.ts.map +1 -0
- package/dist/types/setup/index.d.ts +1 -0
- package/dist/types/setup/index.d.ts.map +1 -0
- package/dist/types/shared/data.d.ts +20 -0
- package/dist/types/shared/data.d.ts.map +1 -0
- package/package.json +117 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
MIT License Copyright (c) 2023 Alexis Munsayac
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# `use-worker-directive`
|
|
2
|
+
|
|
3
|
+
> Universal `use worker` functions
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/use-worker-directive) [](https://github.com/airbnb/javascript)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i use-worker-directive
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add use-worker-directive
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add use-worker-directive
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
### Worker functions
|
|
24
|
+
|
|
25
|
+
Like the original `"use worker"` directive, the compiler supports functions.
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
async function doStuff(x, y) {
|
|
29
|
+
"use worker";
|
|
30
|
+
await foo(x);
|
|
31
|
+
await bar(y);
|
|
32
|
+
}
|
|
33
|
+
// also works for arrow functions
|
|
34
|
+
|
|
35
|
+
const doStuff = async (x, y) => {
|
|
36
|
+
"use worker";
|
|
37
|
+
await foo(x);
|
|
38
|
+
await bar(y);
|
|
39
|
+
};
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The compiler also supports async generators
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
|
|
46
|
+
async function* doStuff(x, y) {
|
|
47
|
+
"use worker";
|
|
48
|
+
yield foo(x);
|
|
49
|
+
yield bar(y);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
> **NOTE**
|
|
54
|
+
> Worker functions are only valid for async functions.
|
|
55
|
+
|
|
56
|
+
### Worker blocks
|
|
57
|
+
|
|
58
|
+
The original `"use worker"` is limited to functions, but what if you could mark block statements with the same directives?
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
if (someCond()) {
|
|
62
|
+
'use stuff';
|
|
63
|
+
await doStuff();
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`use-worker-directive` supports worker blocks in almost all statements that supports it:
|
|
68
|
+
|
|
69
|
+
- `if-else`
|
|
70
|
+
- `try-catch-finally`
|
|
71
|
+
- `for`
|
|
72
|
+
- `for-in`
|
|
73
|
+
- `for-of`
|
|
74
|
+
- `for await`
|
|
75
|
+
- `while`
|
|
76
|
+
- `do-while`
|
|
77
|
+
- labeled statements
|
|
78
|
+
|
|
79
|
+
Worker blocks also supports `break`, `continue`, `return` and `throw` statements, as well as `yield` expressions and delegations.
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
for (const item of items) {
|
|
83
|
+
'use worker';
|
|
84
|
+
await processItem(item);
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
> **NOTE**
|
|
89
|
+
> Worker blocks are only supported within async functions and at top-level scope (since modules now support top-level `await`)
|
|
90
|
+
|
|
91
|
+
### Closure extraction
|
|
92
|
+
|
|
93
|
+
`use-worker-directive` supports closure extraction
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
async function foo() {
|
|
97
|
+
const prefix = 'Message: ';
|
|
98
|
+
|
|
99
|
+
async function postMessage(message) {
|
|
100
|
+
'use worker';
|
|
101
|
+
await addMessage(prefix + message);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Streaming worker functions
|
|
107
|
+
|
|
108
|
+
If a worker function returns a value with a `Promise`, `ReadableStream` or `AsyncIterable`, those instances' values are going to be streamed through the response.
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
async function getMessage() {
|
|
112
|
+
'use worker';
|
|
113
|
+
return {
|
|
114
|
+
// `getAsyncData` returns a Promise
|
|
115
|
+
// On the client-side, this object is going to
|
|
116
|
+
// be received immedatiely, but the value
|
|
117
|
+
// to which the Promise resolves into
|
|
118
|
+
// is going to be streamed after.
|
|
119
|
+
message: getAsyncData(),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Advanced serialization
|
|
125
|
+
|
|
126
|
+
`use-worker-directive` supports a wide range of data types, you can check [the compatibility table here](https://github.com/lxsmnsyc/seroval/blob/main/docs/compatibility.md#supported-types)
|
|
127
|
+
|
|
128
|
+
### Customizable directive
|
|
129
|
+
|
|
130
|
+
## Integrations
|
|
131
|
+
|
|
132
|
+
- [Vite](https://github.com/lxsmnsyc/dismantle/tree/main/use-worker-directive/unplugin)
|
|
133
|
+
|
|
134
|
+
## Sponsors
|
|
135
|
+
|
|
136
|
+

|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
MIT © [lxsmnsyc](https://github.com/lxsmnsyc)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// client/index.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
$$server: () => $$server,
|
|
24
|
+
$$worker: () => $$worker
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(client_exports);
|
|
27
|
+
var import_seroval2 = require("seroval");
|
|
28
|
+
|
|
29
|
+
// shared/data.ts
|
|
30
|
+
var import_seroval = require("seroval");
|
|
31
|
+
var import_web = require("seroval-plugins/web");
|
|
32
|
+
function sendWorkerData(target, id, instance, data) {
|
|
33
|
+
return (0, import_seroval.crossSerializeStream)(data, {
|
|
34
|
+
scopeId: instance,
|
|
35
|
+
plugins: [
|
|
36
|
+
import_web.CustomEventPlugin,
|
|
37
|
+
import_web.DOMExceptionPlugin,
|
|
38
|
+
import_web.EventPlugin,
|
|
39
|
+
import_web.FormDataPlugin,
|
|
40
|
+
import_web.HeadersPlugin,
|
|
41
|
+
import_web.ReadableStreamPlugin,
|
|
42
|
+
import_web.RequestPlugin,
|
|
43
|
+
import_web.ResponsePlugin,
|
|
44
|
+
import_web.URLSearchParamsPlugin,
|
|
45
|
+
import_web.URLPlugin
|
|
46
|
+
],
|
|
47
|
+
onSerialize(data2, initial) {
|
|
48
|
+
target.postMessage({
|
|
49
|
+
id,
|
|
50
|
+
instance,
|
|
51
|
+
type: "next",
|
|
52
|
+
initial,
|
|
53
|
+
data: initial ? `(${(0, import_seroval.getCrossReferenceHeader)(instance)},${data2})` : data2
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
onDone() {
|
|
57
|
+
target.postMessage({
|
|
58
|
+
id,
|
|
59
|
+
instance,
|
|
60
|
+
type: "done"
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
onError(error) {
|
|
64
|
+
target.postMessage({
|
|
65
|
+
id,
|
|
66
|
+
instance,
|
|
67
|
+
type: "error",
|
|
68
|
+
data: error
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// client/index.ts
|
|
75
|
+
var WORKER;
|
|
76
|
+
function $$worker(instance) {
|
|
77
|
+
WORKER = instance;
|
|
78
|
+
}
|
|
79
|
+
var INSTANCE = 0;
|
|
80
|
+
function createWorkerPromise(id, instance) {
|
|
81
|
+
return new Promise((resolve, reject) => {
|
|
82
|
+
const onMessage = (event) => {
|
|
83
|
+
if (!(event.data.id === id && event.data.instance === instance)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (event.data.type === "close") {
|
|
87
|
+
WORKER.removeEventListener("message", onMessage);
|
|
88
|
+
delete $R[instance];
|
|
89
|
+
} else if (event.data.type === "error") {
|
|
90
|
+
reject(event.data.data);
|
|
91
|
+
delete $R[instance];
|
|
92
|
+
} else {
|
|
93
|
+
const result = (0, import_seroval2.deserialize)(event.data.data);
|
|
94
|
+
if (event.data.initial) {
|
|
95
|
+
resolve(result);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
WORKER.addEventListener("message", onMessage);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async function handler(id, args) {
|
|
103
|
+
const instance = `use-worker-directive:${INSTANCE++}`;
|
|
104
|
+
const result = createWorkerPromise(id, instance);
|
|
105
|
+
sendWorkerData(WORKER, id, instance, args);
|
|
106
|
+
return await result;
|
|
107
|
+
}
|
|
108
|
+
function $$server(id) {
|
|
109
|
+
return (...args) => handler(id, args);
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=client.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../client/index.ts", "../../../shared/data.ts"],
|
|
4
|
+
"sourcesContent": ["import { deserialize } from 'seroval';\nimport { sendWorkerData, type SerializedWorkerData } from '../shared/data';\n\nlet WORKER: Worker;\n\nexport function $$worker(instance: Worker): void {\n WORKER = instance;\n}\n\nlet INSTANCE = 0;\n\ndeclare const $R: Record<string, unknown>;\n\nfunction createWorkerPromise<R>(id: string, instance: string): Promise<R> {\n return new Promise<R>((resolve, reject) => {\n const onMessage = (event: MessageEvent<SerializedWorkerData>) => {\n if (!(event.data.id === id && event.data.instance === instance)) {\n return;\n }\n if (event.data.type === 'close') {\n WORKER.removeEventListener('message', onMessage);\n delete $R[instance];\n } else if (event.data.type === 'error') {\n reject(event.data.data);\n delete $R[instance];\n } else {\n const result = deserialize(event.data.data);\n if (event.data.initial) {\n resolve(result as R);\n }\n }\n };\n WORKER.addEventListener('message', onMessage);\n });\n}\n\nasync function handler<T extends unknown[], R>(\n id: string,\n args: T,\n): Promise<R> {\n const instance = `use-worker-directive:${INSTANCE++}`;\n\n const result = createWorkerPromise<R>(id, instance);\n\n sendWorkerData(WORKER, id, instance, args);\n\n return await result;\n}\n\nexport function $$server<T extends unknown[], R>(\n id: string,\n): (...args: T) => Promise<R> {\n return (...args: T): Promise<R> => handler(id, args);\n}\n", "import { crossSerializeStream, getCrossReferenceHeader } from 'seroval';\nimport {\n CustomEventPlugin,\n DOMExceptionPlugin,\n EventPlugin,\n FormDataPlugin,\n HeadersPlugin,\n ReadableStreamPlugin,\n RequestPlugin,\n ResponsePlugin,\n URLPlugin,\n URLSearchParamsPlugin,\n} from 'seroval-plugins/web';\n\ninterface SerializedWorkerDataBase {\n id: string;\n instance: string;\n}\n\ninterface SerializedWorkerDataNext extends SerializedWorkerDataBase {\n type: 'next';\n initial: boolean;\n data: string;\n}\n\ninterface SerializedWorkerDataClose extends SerializedWorkerDataBase {\n type: 'close';\n}\n\ninterface SerializedWorkerDataError extends SerializedWorkerDataBase {\n type: 'error';\n data: unknown;\n}\n\nexport type SerializedWorkerData =\n | SerializedWorkerDataNext\n | SerializedWorkerDataClose\n | SerializedWorkerDataError;\n\nexport function sendWorkerData<T>(\n target: (Window & typeof globalThis) | Worker,\n id: string,\n instance: string,\n data: T,\n): () => void {\n return crossSerializeStream(data, {\n scopeId: instance,\n plugins: [\n CustomEventPlugin,\n DOMExceptionPlugin,\n EventPlugin,\n FormDataPlugin,\n HeadersPlugin,\n ReadableStreamPlugin,\n RequestPlugin,\n ResponsePlugin,\n URLSearchParamsPlugin,\n URLPlugin,\n ],\n onSerialize(data, initial) {\n target.postMessage({\n id,\n instance,\n type: 'next',\n initial,\n data: initial ? `(${getCrossReferenceHeader(instance)},${data})` : data,\n });\n },\n onDone() {\n target.postMessage({\n id,\n instance,\n type: 'done',\n });\n },\n onError(error) {\n target.postMessage({\n id,\n instance,\n type: 'error',\n data: error,\n });\n },\n });\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,kBAA4B;;;ACA5B,qBAA8D;AAC9D,iBAWO;AA2BA,SAAS,eACd,QACA,IACA,UACA,MACY;AACZ,aAAO,qCAAqB,MAAM;AAAA,IAChC,SAAS;AAAA,IACT,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,YAAYC,OAAM,SAAS;AACzB,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,QACN;AAAA,QACA,MAAM,UAAU,QAAI,wCAAwB,QAAQ,CAAC,IAAIA,KAAI,MAAMA;AAAA,MACrE,CAAC;AAAA,IACH;AAAA,IACA,SAAS;AACP,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAAA,IACA,QAAQ,OAAO;AACb,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,QACN,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AACH;;;ADjFA,IAAI;AAEG,SAAS,SAAS,UAAwB;AAC/C,WAAS;AACX;AAEA,IAAI,WAAW;AAIf,SAAS,oBAAuB,IAAY,UAA8B;AACxE,SAAO,IAAI,QAAW,CAAC,SAAS,WAAW;AACzC,UAAM,YAAY,CAAC,UAA8C;AAC/D,UAAI,EAAE,MAAM,KAAK,OAAO,MAAM,MAAM,KAAK,aAAa,WAAW;AAC/D;AAAA,MACF;AACA,UAAI,MAAM,KAAK,SAAS,SAAS;AAC/B,eAAO,oBAAoB,WAAW,SAAS;AAC/C,eAAO,GAAG,QAAQ;AAAA,MACpB,WAAW,MAAM,KAAK,SAAS,SAAS;AACtC,eAAO,MAAM,KAAK,IAAI;AACtB,eAAO,GAAG,QAAQ;AAAA,MACpB,OAAO;AACL,cAAM,aAAS,6BAAY,MAAM,KAAK,IAAI;AAC1C,YAAI,MAAM,KAAK,SAAS;AACtB,kBAAQ,MAAW;AAAA,QACrB;AAAA,MACF;AAAA,IACF;AACA,WAAO,iBAAiB,WAAW,SAAS;AAAA,EAC9C,CAAC;AACH;AAEA,eAAe,QACb,IACA,MACY;AACZ,QAAM,WAAW,wBAAwB,UAAU;AAEnD,QAAM,SAAS,oBAAuB,IAAI,QAAQ;AAElD,iBAAe,QAAQ,IAAI,UAAU,IAAI;AAEzC,SAAO,MAAM;AACf;AAEO,SAAS,SACd,IAC4B;AAC5B,SAAO,IAAI,SAAwB,QAAQ,IAAI,IAAI;AACrD;",
|
|
6
|
+
"names": ["import_seroval", "data"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// compiler/index.ts
|
|
31
|
+
var compiler_exports = {};
|
|
32
|
+
__export(compiler_exports, {
|
|
33
|
+
compile: () => compile2
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(compiler_exports);
|
|
36
|
+
var dismantle = __toESM(require("dismantle"), 1);
|
|
37
|
+
var DEFAULT_PREFIX = "__worker";
|
|
38
|
+
var DEFAULT_DIRECTIVE = "use worker";
|
|
39
|
+
async function compile2(id, code, options) {
|
|
40
|
+
return await dismantle.compile(id, code, {
|
|
41
|
+
runtime: "use-worker-directive/runtime",
|
|
42
|
+
key: "use-worker-directive",
|
|
43
|
+
mode: options.mode,
|
|
44
|
+
env: options.env,
|
|
45
|
+
definitions: [
|
|
46
|
+
{
|
|
47
|
+
type: "block-directive",
|
|
48
|
+
directive: options.directive || DEFAULT_DIRECTIVE,
|
|
49
|
+
idPrefix: `/${options.prefix || DEFAULT_PREFIX}/`,
|
|
50
|
+
pure: options.pure,
|
|
51
|
+
target: {
|
|
52
|
+
kind: "named",
|
|
53
|
+
source: `use-worker-directive/${options.mode}`,
|
|
54
|
+
name: "$$server"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=compiler.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../compiler/index.ts"],
|
|
4
|
+
"sourcesContent": ["import * as dismantle from 'dismantle';\n\nconst DEFAULT_PREFIX = '__worker';\nconst DEFAULT_DIRECTIVE = 'use worker';\n\nexport interface Options extends Pick<dismantle.Options, 'mode' | 'env'> {\n directive?: string;\n prefix?: string;\n pure?: boolean;\n}\n\nexport type Output = dismantle.Output;\nexport type CodeOutput = dismantle.CodeOutput;\n\nexport async function compile(\n id: string,\n code: string,\n options: Options,\n): Promise<Output> {\n return await dismantle.compile(id, code, {\n runtime: 'use-worker-directive/runtime',\n key: 'use-worker-directive',\n mode: options.mode,\n env: options.env,\n definitions: [\n {\n type: 'block-directive',\n directive: options.directive || DEFAULT_DIRECTIVE,\n idPrefix: `/${options.prefix || DEFAULT_PREFIX}/`,\n pure: options.pure,\n target: {\n kind: 'named',\n source: `use-worker-directive/${options.mode}`,\n name: '$$server',\n },\n },\n ],\n });\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,iBAAAA;AAAA;AAAA;AAAA,gBAA2B;AAE3B,IAAM,iBAAiB;AACvB,IAAM,oBAAoB;AAW1B,eAAsBA,SACpB,IACA,MACA,SACiB;AACjB,SAAO,MAAgB,kBAAQ,IAAI,MAAM;AAAA,IACvC,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM,QAAQ;AAAA,IACd,KAAK,QAAQ;AAAA,IACb,aAAa;AAAA,MACX;AAAA,QACE,MAAM;AAAA,QACN,WAAW,QAAQ,aAAa;AAAA,QAChC,UAAU,IAAI,QAAQ,UAAU,cAAc;AAAA,QAC9C,MAAM,QAAQ;AAAA,QACd,QAAQ;AAAA,UACN,MAAM;AAAA,UACN,QAAQ,wBAAwB,QAAQ,IAAI;AAAA,UAC5C,MAAM;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACH;",
|
|
6
|
+
"names": ["compile"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
|
|
17
|
+
// runtime/index.ts
|
|
18
|
+
var runtime_exports = {};
|
|
19
|
+
module.exports = __toCommonJS(runtime_exports);
|
|
20
|
+
__reExport(runtime_exports, require("dismantle/runtime"), module.exports);
|
|
21
|
+
//# sourceMappingURL=runtime.cjs.map
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// server/index.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
$$server: () => $$server,
|
|
24
|
+
$$setup: () => $$setup
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(server_exports);
|
|
27
|
+
var import_seroval2 = require("seroval");
|
|
28
|
+
|
|
29
|
+
// shared/data.ts
|
|
30
|
+
var import_seroval = require("seroval");
|
|
31
|
+
var import_web = require("seroval-plugins/web");
|
|
32
|
+
function sendWorkerData(target, id, instance, data) {
|
|
33
|
+
return (0, import_seroval.crossSerializeStream)(data, {
|
|
34
|
+
scopeId: instance,
|
|
35
|
+
plugins: [
|
|
36
|
+
import_web.CustomEventPlugin,
|
|
37
|
+
import_web.DOMExceptionPlugin,
|
|
38
|
+
import_web.EventPlugin,
|
|
39
|
+
import_web.FormDataPlugin,
|
|
40
|
+
import_web.HeadersPlugin,
|
|
41
|
+
import_web.ReadableStreamPlugin,
|
|
42
|
+
import_web.RequestPlugin,
|
|
43
|
+
import_web.ResponsePlugin,
|
|
44
|
+
import_web.URLSearchParamsPlugin,
|
|
45
|
+
import_web.URLPlugin
|
|
46
|
+
],
|
|
47
|
+
onSerialize(data2, initial) {
|
|
48
|
+
target.postMessage({
|
|
49
|
+
id,
|
|
50
|
+
instance,
|
|
51
|
+
type: "next",
|
|
52
|
+
initial,
|
|
53
|
+
data: initial ? `(${(0, import_seroval.getCrossReferenceHeader)(instance)},${data2})` : data2
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
onDone() {
|
|
57
|
+
target.postMessage({
|
|
58
|
+
id,
|
|
59
|
+
instance,
|
|
60
|
+
type: "done"
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
onError(error) {
|
|
64
|
+
target.postMessage({
|
|
65
|
+
id,
|
|
66
|
+
instance,
|
|
67
|
+
type: "error",
|
|
68
|
+
data: error
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// server/index.ts
|
|
75
|
+
var REGISTRATIONS = /* @__PURE__ */ new Map();
|
|
76
|
+
function $$server(id, callback) {
|
|
77
|
+
const reg = [id, callback];
|
|
78
|
+
REGISTRATIONS.set(id, reg);
|
|
79
|
+
return callback;
|
|
80
|
+
}
|
|
81
|
+
function $$setup() {
|
|
82
|
+
self.onmessage = (event) => {
|
|
83
|
+
if (event.data.type === "next") {
|
|
84
|
+
const result = (0, import_seroval2.deserialize)(event.data.data);
|
|
85
|
+
if (event.data.initial) {
|
|
86
|
+
const registration = REGISTRATIONS.get(event.data.id);
|
|
87
|
+
if (registration) {
|
|
88
|
+
const [id, callback] = registration;
|
|
89
|
+
if (id === event.data.id) {
|
|
90
|
+
sendWorkerData(
|
|
91
|
+
self,
|
|
92
|
+
event.data.id,
|
|
93
|
+
event.data.instance,
|
|
94
|
+
callback.apply(null, result)
|
|
95
|
+
);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
throw new Error(`Worker function "${event.data.id}" is not found.`);
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
delete $R[event.data.instance];
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=server.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../server/index.ts", "../../../shared/data.ts"],
|
|
4
|
+
"sourcesContent": ["import { deserialize } from 'seroval';\nimport { sendWorkerData, type SerializedWorkerData } from '../shared/data';\n\ntype ServerHandler<Args extends unknown[], Return> = (\n ...args: Args\n) => Promise<Return>;\n\ntype HandlerRegistration = [\n id: string,\n callback: ServerHandler<unknown[], unknown>,\n];\n\nconst REGISTRATIONS = new Map<string, HandlerRegistration>();\n\nexport function $$server(\n id: string,\n callback: ServerHandler<unknown[], unknown>,\n): ServerHandler<unknown[], unknown> {\n const reg: HandlerRegistration = [id, callback];\n REGISTRATIONS.set(id, reg);\n return callback;\n}\n\ndeclare const $R: Record<string, unknown>;\n\nexport function $$setup(): void {\n self.onmessage = (event: MessageEvent<SerializedWorkerData>) => {\n if (event.data.type === 'next') {\n const result = deserialize(event.data.data);\n if (event.data.initial) {\n const registration = REGISTRATIONS.get(event.data.id);\n if (registration) {\n const [id, callback] = registration;\n if (id === event.data.id) {\n sendWorkerData(\n self,\n event.data.id,\n event.data.instance,\n callback.apply(null, result as unknown[]),\n );\n return;\n }\n }\n throw new Error(`Worker function \"${event.data.id}\" is not found.`);\n }\n } else {\n delete $R[event.data.instance];\n }\n };\n}\n", "import { crossSerializeStream, getCrossReferenceHeader } from 'seroval';\nimport {\n CustomEventPlugin,\n DOMExceptionPlugin,\n EventPlugin,\n FormDataPlugin,\n HeadersPlugin,\n ReadableStreamPlugin,\n RequestPlugin,\n ResponsePlugin,\n URLPlugin,\n URLSearchParamsPlugin,\n} from 'seroval-plugins/web';\n\ninterface SerializedWorkerDataBase {\n id: string;\n instance: string;\n}\n\ninterface SerializedWorkerDataNext extends SerializedWorkerDataBase {\n type: 'next';\n initial: boolean;\n data: string;\n}\n\ninterface SerializedWorkerDataClose extends SerializedWorkerDataBase {\n type: 'close';\n}\n\ninterface SerializedWorkerDataError extends SerializedWorkerDataBase {\n type: 'error';\n data: unknown;\n}\n\nexport type SerializedWorkerData =\n | SerializedWorkerDataNext\n | SerializedWorkerDataClose\n | SerializedWorkerDataError;\n\nexport function sendWorkerData<T>(\n target: (Window & typeof globalThis) | Worker,\n id: string,\n instance: string,\n data: T,\n): () => void {\n return crossSerializeStream(data, {\n scopeId: instance,\n plugins: [\n CustomEventPlugin,\n DOMExceptionPlugin,\n EventPlugin,\n FormDataPlugin,\n HeadersPlugin,\n ReadableStreamPlugin,\n RequestPlugin,\n ResponsePlugin,\n URLSearchParamsPlugin,\n URLPlugin,\n ],\n onSerialize(data, initial) {\n target.postMessage({\n id,\n instance,\n type: 'next',\n initial,\n data: initial ? `(${getCrossReferenceHeader(instance)},${data})` : data,\n });\n },\n onDone() {\n target.postMessage({\n id,\n instance,\n type: 'done',\n });\n },\n onError(error) {\n target.postMessage({\n id,\n instance,\n type: 'error',\n data: error,\n });\n },\n });\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,kBAA4B;;;ACA5B,qBAA8D;AAC9D,iBAWO;AA2BA,SAAS,eACd,QACA,IACA,UACA,MACY;AACZ,aAAO,qCAAqB,MAAM;AAAA,IAChC,SAAS;AAAA,IACT,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,YAAYC,OAAM,SAAS;AACzB,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,QACN;AAAA,QACA,MAAM,UAAU,QAAI,wCAAwB,QAAQ,CAAC,IAAIA,KAAI,MAAMA;AAAA,MACrE,CAAC;AAAA,IACH;AAAA,IACA,SAAS;AACP,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAAA,IACA,QAAQ,OAAO;AACb,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,QACN,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AACH;;;ADxEA,IAAM,gBAAgB,oBAAI,IAAiC;AAEpD,SAAS,SACd,IACA,UACmC;AACnC,QAAM,MAA2B,CAAC,IAAI,QAAQ;AAC9C,gBAAc,IAAI,IAAI,GAAG;AACzB,SAAO;AACT;AAIO,SAAS,UAAgB;AAC9B,OAAK,YAAY,CAAC,UAA8C;AAC9D,QAAI,MAAM,KAAK,SAAS,QAAQ;AAC9B,YAAM,aAAS,6BAAY,MAAM,KAAK,IAAI;AAC1C,UAAI,MAAM,KAAK,SAAS;AACtB,cAAM,eAAe,cAAc,IAAI,MAAM,KAAK,EAAE;AACpD,YAAI,cAAc;AAChB,gBAAM,CAAC,IAAI,QAAQ,IAAI;AACvB,cAAI,OAAO,MAAM,KAAK,IAAI;AACxB;AAAA,cACE;AAAA,cACA,MAAM,KAAK;AAAA,cACX,MAAM,KAAK;AAAA,cACX,SAAS,MAAM,MAAM,MAAmB;AAAA,YAC1C;AACA;AAAA,UACF;AAAA,QACF;AACA,cAAM,IAAI,MAAM,oBAAoB,MAAM,KAAK,EAAE,iBAAiB;AAAA,MACpE;AAAA,IACF,OAAO;AACL,aAAO,GAAG,MAAM,KAAK,QAAQ;AAAA,IAC/B;AAAA,EACF;AACF;",
|
|
6
|
+
"names": ["import_seroval", "data"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../setup/index.ts"],
|
|
4
|
+
"sourcesContent": ["console.warn(\n \"If you see this message, it means that `use-worker-directive`'s plugin or compiler is not configured correctly.\",\n);\n"],
|
|
5
|
+
"mappings": ";;;AAAA,QAAQ;AAAA,EACN;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var l=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var R=(r,e)=>{for(var t in e)l(r,t,{get:e[t],enumerable:!0})},W=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of c(e))!m.call(r,o)&&o!==t&&l(r,o,{get:()=>e[o],enumerable:!(n=k(e,o))||n.enumerable});return r};var f=r=>W(l({},"__esModule",{value:!0}),r);var y={};R(y,{$$server:()=>x,$$worker:()=>S});module.exports=f(y);var g=require("seroval");var s=require("seroval"),a=require("seroval-plugins/web");function u(r,e,t,n){return(0,s.crossSerializeStream)(n,{scopeId:t,plugins:[a.CustomEventPlugin,a.DOMExceptionPlugin,a.EventPlugin,a.FormDataPlugin,a.HeadersPlugin,a.ReadableStreamPlugin,a.RequestPlugin,a.ResponsePlugin,a.URLSearchParamsPlugin,a.URLPlugin],onSerialize(o,i){r.postMessage({id:e,instance:t,type:"next",initial:i,data:i?`(${(0,s.getCrossReferenceHeader)(t)},${o})`:o})},onDone(){r.postMessage({id:e,instance:t,type:"done"})},onError(o){r.postMessage({id:e,instance:t,type:"error",data:o})}})}var d;function S(r){d=r}var D=0;function P(r,e){return new Promise((t,n)=>{let o=i=>{if(i.data.id===r&&i.data.instance===e)if(i.data.type==="close")d.removeEventListener("message",o),delete $R[e];else if(i.data.type==="error")n(i.data.data),delete $R[e];else{let p=(0,g.deserialize)(i.data.data);i.data.initial&&t(p)}};d.addEventListener("message",o)})}async function z(r,e){let t=`use-worker-directive:${D++}`,n=P(r,t);return u(d,r,t,e),await n}function x(r){return(...e)=>z(r,e)}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var s=Object.create;var n=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var c=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty;var v=(e,r)=>{for(var t in r)n(e,t,{get:r[t],enumerable:!0})},u=(e,r,t,d)=>{if(r&&typeof r=="object"||typeof r=="function")for(let i of p(r))!a.call(e,i)&&i!==t&&n(e,i,{get:()=>r[i],enumerable:!(d=m(r,i))||d.enumerable});return e};var k=(e,r,t)=>(t=e!=null?s(c(e)):{},u(r||!e||!e.__esModule?n(t,"default",{value:e,enumerable:!0}):t,e)),l=e=>u(n({},"__esModule",{value:!0}),e);var w={};v(w,{compile:()=>f});module.exports=l(w);var o=k(require("dismantle"),1),x="__worker",O="use worker";async function f(e,r,t){return await o.compile(e,r,{runtime:"use-worker-directive/runtime",key:"use-worker-directive",mode:t.mode,env:t.env,definitions:[{type:"block-directive",directive:t.directive||O,idPrefix:`/${t.prefix||x}/`,pure:t.pure,target:{kind:"named",source:`use-worker-directive/${t.mode}`,name:"$$server"}}]})}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var a=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(r,o,f,x)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of c(o))!d.call(r,e)&&e!==f&&a(r,e,{get:()=>o[e],enumerable:!(x=b(o,e))||x.enumerable});return r},t=(r,o,f)=>(p(r,o,"default"),f&&p(f,o,"default"));var g=r=>p(a({},"__esModule",{value:!0}),r);var m={};module.exports=g(m);t(m,require("dismantle/runtime"),module.exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var s=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var c=(e,a)=>{for(var n in a)s(e,n,{get:a[n],enumerable:!0})},f=(e,a,n,o)=>{if(a&&typeof a=="object"||typeof a=="function")for(let t of k(a))!S.call(e,t)&&t!==n&&s(e,t,{get:()=>a[t],enumerable:!(o=p(a,t))||o.enumerable});return e};var D=e=>f(s({},"__esModule",{value:!0}),e);var R={};c(R,{$$server:()=>W,$$setup:()=>z});module.exports=D(R);var u=require("seroval");var i=require("seroval"),r=require("seroval-plugins/web");function l(e,a,n,o){return(0,i.crossSerializeStream)(o,{scopeId:n,plugins:[r.CustomEventPlugin,r.DOMExceptionPlugin,r.EventPlugin,r.FormDataPlugin,r.HeadersPlugin,r.ReadableStreamPlugin,r.RequestPlugin,r.ResponsePlugin,r.URLSearchParamsPlugin,r.URLPlugin],onSerialize(t,d){e.postMessage({id:a,instance:n,type:"next",initial:d,data:d?`(${(0,i.getCrossReferenceHeader)(n)},${t})`:t})},onDone(){e.postMessage({id:a,instance:n,type:"done"})},onError(t){e.postMessage({id:a,instance:n,type:"error",data:t})}})}var g=new Map;function W(e,a){let n=[e,a];return g.set(e,n),a}function z(){self.onmessage=e=>{if(e.data.type==="next"){let a=(0,u.deserialize)(e.data.data);if(e.data.initial){let n=g.get(e.data.id);if(n){let[o,t]=n;if(o===e.data.id){l(self,e.data.id,e.data.instance,t.apply(null,a));return}}throw new Error(`Worker function "${e.data.id}" is not found.`)}}else delete $R[e.data.instance]}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";console.warn("If you see this message, it means that `use-worker-directive`'s plugin or compiler is not configured correctly.");
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// client/index.ts
|
|
2
|
+
import { deserialize } from "seroval";
|
|
3
|
+
|
|
4
|
+
// shared/data.ts
|
|
5
|
+
import { crossSerializeStream, getCrossReferenceHeader } from "seroval";
|
|
6
|
+
import {
|
|
7
|
+
CustomEventPlugin,
|
|
8
|
+
DOMExceptionPlugin,
|
|
9
|
+
EventPlugin,
|
|
10
|
+
FormDataPlugin,
|
|
11
|
+
HeadersPlugin,
|
|
12
|
+
ReadableStreamPlugin,
|
|
13
|
+
RequestPlugin,
|
|
14
|
+
ResponsePlugin,
|
|
15
|
+
URLPlugin,
|
|
16
|
+
URLSearchParamsPlugin
|
|
17
|
+
} from "seroval-plugins/web";
|
|
18
|
+
function sendWorkerData(target, id, instance, data) {
|
|
19
|
+
return crossSerializeStream(data, {
|
|
20
|
+
scopeId: instance,
|
|
21
|
+
plugins: [
|
|
22
|
+
CustomEventPlugin,
|
|
23
|
+
DOMExceptionPlugin,
|
|
24
|
+
EventPlugin,
|
|
25
|
+
FormDataPlugin,
|
|
26
|
+
HeadersPlugin,
|
|
27
|
+
ReadableStreamPlugin,
|
|
28
|
+
RequestPlugin,
|
|
29
|
+
ResponsePlugin,
|
|
30
|
+
URLSearchParamsPlugin,
|
|
31
|
+
URLPlugin
|
|
32
|
+
],
|
|
33
|
+
onSerialize(data2, initial) {
|
|
34
|
+
target.postMessage({
|
|
35
|
+
id,
|
|
36
|
+
instance,
|
|
37
|
+
type: "next",
|
|
38
|
+
initial,
|
|
39
|
+
data: initial ? `(${getCrossReferenceHeader(instance)},${data2})` : data2
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
onDone() {
|
|
43
|
+
target.postMessage({
|
|
44
|
+
id,
|
|
45
|
+
instance,
|
|
46
|
+
type: "done"
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
onError(error) {
|
|
50
|
+
target.postMessage({
|
|
51
|
+
id,
|
|
52
|
+
instance,
|
|
53
|
+
type: "error",
|
|
54
|
+
data: error
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// client/index.ts
|
|
61
|
+
var WORKER;
|
|
62
|
+
function $$worker(instance) {
|
|
63
|
+
WORKER = instance;
|
|
64
|
+
}
|
|
65
|
+
var INSTANCE = 0;
|
|
66
|
+
function createWorkerPromise(id, instance) {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
const onMessage = (event) => {
|
|
69
|
+
if (!(event.data.id === id && event.data.instance === instance)) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (event.data.type === "close") {
|
|
73
|
+
WORKER.removeEventListener("message", onMessage);
|
|
74
|
+
delete $R[instance];
|
|
75
|
+
} else if (event.data.type === "error") {
|
|
76
|
+
reject(event.data.data);
|
|
77
|
+
delete $R[instance];
|
|
78
|
+
} else {
|
|
79
|
+
const result = deserialize(event.data.data);
|
|
80
|
+
if (event.data.initial) {
|
|
81
|
+
resolve(result);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
WORKER.addEventListener("message", onMessage);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
async function handler(id, args) {
|
|
89
|
+
const instance = `use-worker-directive:${INSTANCE++}`;
|
|
90
|
+
const result = createWorkerPromise(id, instance);
|
|
91
|
+
sendWorkerData(WORKER, id, instance, args);
|
|
92
|
+
return await result;
|
|
93
|
+
}
|
|
94
|
+
function $$server(id) {
|
|
95
|
+
return (...args) => handler(id, args);
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
$$server,
|
|
99
|
+
$$worker
|
|
100
|
+
};
|
|
101
|
+
//# sourceMappingURL=client.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../client/index.ts", "../../../shared/data.ts"],
|
|
4
|
+
"sourcesContent": ["import { deserialize } from 'seroval';\nimport { sendWorkerData, type SerializedWorkerData } from '../shared/data';\n\nlet WORKER: Worker;\n\nexport function $$worker(instance: Worker): void {\n WORKER = instance;\n}\n\nlet INSTANCE = 0;\n\ndeclare const $R: Record<string, unknown>;\n\nfunction createWorkerPromise<R>(id: string, instance: string): Promise<R> {\n return new Promise<R>((resolve, reject) => {\n const onMessage = (event: MessageEvent<SerializedWorkerData>) => {\n if (!(event.data.id === id && event.data.instance === instance)) {\n return;\n }\n if (event.data.type === 'close') {\n WORKER.removeEventListener('message', onMessage);\n delete $R[instance];\n } else if (event.data.type === 'error') {\n reject(event.data.data);\n delete $R[instance];\n } else {\n const result = deserialize(event.data.data);\n if (event.data.initial) {\n resolve(result as R);\n }\n }\n };\n WORKER.addEventListener('message', onMessage);\n });\n}\n\nasync function handler<T extends unknown[], R>(\n id: string,\n args: T,\n): Promise<R> {\n const instance = `use-worker-directive:${INSTANCE++}`;\n\n const result = createWorkerPromise<R>(id, instance);\n\n sendWorkerData(WORKER, id, instance, args);\n\n return await result;\n}\n\nexport function $$server<T extends unknown[], R>(\n id: string,\n): (...args: T) => Promise<R> {\n return (...args: T): Promise<R> => handler(id, args);\n}\n", "import { crossSerializeStream, getCrossReferenceHeader } from 'seroval';\nimport {\n CustomEventPlugin,\n DOMExceptionPlugin,\n EventPlugin,\n FormDataPlugin,\n HeadersPlugin,\n ReadableStreamPlugin,\n RequestPlugin,\n ResponsePlugin,\n URLPlugin,\n URLSearchParamsPlugin,\n} from 'seroval-plugins/web';\n\ninterface SerializedWorkerDataBase {\n id: string;\n instance: string;\n}\n\ninterface SerializedWorkerDataNext extends SerializedWorkerDataBase {\n type: 'next';\n initial: boolean;\n data: string;\n}\n\ninterface SerializedWorkerDataClose extends SerializedWorkerDataBase {\n type: 'close';\n}\n\ninterface SerializedWorkerDataError extends SerializedWorkerDataBase {\n type: 'error';\n data: unknown;\n}\n\nexport type SerializedWorkerData =\n | SerializedWorkerDataNext\n | SerializedWorkerDataClose\n | SerializedWorkerDataError;\n\nexport function sendWorkerData<T>(\n target: (Window & typeof globalThis) | Worker,\n id: string,\n instance: string,\n data: T,\n): () => void {\n return crossSerializeStream(data, {\n scopeId: instance,\n plugins: [\n CustomEventPlugin,\n DOMExceptionPlugin,\n EventPlugin,\n FormDataPlugin,\n HeadersPlugin,\n ReadableStreamPlugin,\n RequestPlugin,\n ResponsePlugin,\n URLSearchParamsPlugin,\n URLPlugin,\n ],\n onSerialize(data, initial) {\n target.postMessage({\n id,\n instance,\n type: 'next',\n initial,\n data: initial ? `(${getCrossReferenceHeader(instance)},${data})` : data,\n });\n },\n onDone() {\n target.postMessage({\n id,\n instance,\n type: 'done',\n });\n },\n onError(error) {\n target.postMessage({\n id,\n instance,\n type: 'error',\n data: error,\n });\n },\n });\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,mBAAmB;;;ACA5B,SAAS,sBAAsB,+BAA+B;AAC9D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AA2BA,SAAS,eACd,QACA,IACA,UACA,MACY;AACZ,SAAO,qBAAqB,MAAM;AAAA,IAChC,SAAS;AAAA,IACT,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,YAAYA,OAAM,SAAS;AACzB,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,QACN;AAAA,QACA,MAAM,UAAU,IAAI,wBAAwB,QAAQ,CAAC,IAAIA,KAAI,MAAMA;AAAA,MACrE,CAAC;AAAA,IACH;AAAA,IACA,SAAS;AACP,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAAA,IACA,QAAQ,OAAO;AACb,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,QACN,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AACH;;;ADjFA,IAAI;AAEG,SAAS,SAAS,UAAwB;AAC/C,WAAS;AACX;AAEA,IAAI,WAAW;AAIf,SAAS,oBAAuB,IAAY,UAA8B;AACxE,SAAO,IAAI,QAAW,CAAC,SAAS,WAAW;AACzC,UAAM,YAAY,CAAC,UAA8C;AAC/D,UAAI,EAAE,MAAM,KAAK,OAAO,MAAM,MAAM,KAAK,aAAa,WAAW;AAC/D;AAAA,MACF;AACA,UAAI,MAAM,KAAK,SAAS,SAAS;AAC/B,eAAO,oBAAoB,WAAW,SAAS;AAC/C,eAAO,GAAG,QAAQ;AAAA,MACpB,WAAW,MAAM,KAAK,SAAS,SAAS;AACtC,eAAO,MAAM,KAAK,IAAI;AACtB,eAAO,GAAG,QAAQ;AAAA,MACpB,OAAO;AACL,cAAM,SAAS,YAAY,MAAM,KAAK,IAAI;AAC1C,YAAI,MAAM,KAAK,SAAS;AACtB,kBAAQ,MAAW;AAAA,QACrB;AAAA,MACF;AAAA,IACF;AACA,WAAO,iBAAiB,WAAW,SAAS;AAAA,EAC9C,CAAC;AACH;AAEA,eAAe,QACb,IACA,MACY;AACZ,QAAM,WAAW,wBAAwB,UAAU;AAEnD,QAAM,SAAS,oBAAuB,IAAI,QAAQ;AAElD,iBAAe,QAAQ,IAAI,UAAU,IAAI;AAEzC,SAAO,MAAM;AACf;AAEO,SAAS,SACd,IAC4B;AAC5B,SAAO,IAAI,SAAwB,QAAQ,IAAI,IAAI;AACrD;",
|
|
6
|
+
"names": ["data"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// compiler/index.ts
|
|
2
|
+
import * as dismantle from "dismantle";
|
|
3
|
+
var DEFAULT_PREFIX = "__worker";
|
|
4
|
+
var DEFAULT_DIRECTIVE = "use worker";
|
|
5
|
+
async function compile2(id, code, options) {
|
|
6
|
+
return await dismantle.compile(id, code, {
|
|
7
|
+
runtime: "use-worker-directive/runtime",
|
|
8
|
+
key: "use-worker-directive",
|
|
9
|
+
mode: options.mode,
|
|
10
|
+
env: options.env,
|
|
11
|
+
definitions: [
|
|
12
|
+
{
|
|
13
|
+
type: "block-directive",
|
|
14
|
+
directive: options.directive || DEFAULT_DIRECTIVE,
|
|
15
|
+
idPrefix: `/${options.prefix || DEFAULT_PREFIX}/`,
|
|
16
|
+
pure: options.pure,
|
|
17
|
+
target: {
|
|
18
|
+
kind: "named",
|
|
19
|
+
source: `use-worker-directive/${options.mode}`,
|
|
20
|
+
name: "$$server"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
compile2 as compile
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=compiler.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../compiler/index.ts"],
|
|
4
|
+
"sourcesContent": ["import * as dismantle from 'dismantle';\n\nconst DEFAULT_PREFIX = '__worker';\nconst DEFAULT_DIRECTIVE = 'use worker';\n\nexport interface Options extends Pick<dismantle.Options, 'mode' | 'env'> {\n directive?: string;\n prefix?: string;\n pure?: boolean;\n}\n\nexport type Output = dismantle.Output;\nexport type CodeOutput = dismantle.CodeOutput;\n\nexport async function compile(\n id: string,\n code: string,\n options: Options,\n): Promise<Output> {\n return await dismantle.compile(id, code, {\n runtime: 'use-worker-directive/runtime',\n key: 'use-worker-directive',\n mode: options.mode,\n env: options.env,\n definitions: [\n {\n type: 'block-directive',\n directive: options.directive || DEFAULT_DIRECTIVE,\n idPrefix: `/${options.prefix || DEFAULT_PREFIX}/`,\n pure: options.pure,\n target: {\n kind: 'named',\n source: `use-worker-directive/${options.mode}`,\n name: '$$server',\n },\n },\n ],\n });\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,YAAY,eAAe;AAE3B,IAAM,iBAAiB;AACvB,IAAM,oBAAoB;AAW1B,eAAsBA,SACpB,IACA,MACA,SACiB;AACjB,SAAO,MAAgB,kBAAQ,IAAI,MAAM;AAAA,IACvC,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM,QAAQ;AAAA,IACd,KAAK,QAAQ;AAAA,IACb,aAAa;AAAA,MACX;AAAA,QACE,MAAM;AAAA,QACN,WAAW,QAAQ,aAAa;AAAA,QAChC,UAAU,IAAI,QAAQ,UAAU,cAAc;AAAA,QAC9C,MAAM,QAAQ;AAAA,QACd,QAAQ;AAAA,UACN,MAAM;AAAA,UACN,QAAQ,wBAAwB,QAAQ,IAAI;AAAA,UAC5C,MAAM;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACH;",
|
|
6
|
+
"names": ["compile"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// server/index.ts
|
|
2
|
+
import { deserialize } from "seroval";
|
|
3
|
+
|
|
4
|
+
// shared/data.ts
|
|
5
|
+
import { crossSerializeStream, getCrossReferenceHeader } from "seroval";
|
|
6
|
+
import {
|
|
7
|
+
CustomEventPlugin,
|
|
8
|
+
DOMExceptionPlugin,
|
|
9
|
+
EventPlugin,
|
|
10
|
+
FormDataPlugin,
|
|
11
|
+
HeadersPlugin,
|
|
12
|
+
ReadableStreamPlugin,
|
|
13
|
+
RequestPlugin,
|
|
14
|
+
ResponsePlugin,
|
|
15
|
+
URLPlugin,
|
|
16
|
+
URLSearchParamsPlugin
|
|
17
|
+
} from "seroval-plugins/web";
|
|
18
|
+
function sendWorkerData(target, id, instance, data) {
|
|
19
|
+
return crossSerializeStream(data, {
|
|
20
|
+
scopeId: instance,
|
|
21
|
+
plugins: [
|
|
22
|
+
CustomEventPlugin,
|
|
23
|
+
DOMExceptionPlugin,
|
|
24
|
+
EventPlugin,
|
|
25
|
+
FormDataPlugin,
|
|
26
|
+
HeadersPlugin,
|
|
27
|
+
ReadableStreamPlugin,
|
|
28
|
+
RequestPlugin,
|
|
29
|
+
ResponsePlugin,
|
|
30
|
+
URLSearchParamsPlugin,
|
|
31
|
+
URLPlugin
|
|
32
|
+
],
|
|
33
|
+
onSerialize(data2, initial) {
|
|
34
|
+
target.postMessage({
|
|
35
|
+
id,
|
|
36
|
+
instance,
|
|
37
|
+
type: "next",
|
|
38
|
+
initial,
|
|
39
|
+
data: initial ? `(${getCrossReferenceHeader(instance)},${data2})` : data2
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
onDone() {
|
|
43
|
+
target.postMessage({
|
|
44
|
+
id,
|
|
45
|
+
instance,
|
|
46
|
+
type: "done"
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
onError(error) {
|
|
50
|
+
target.postMessage({
|
|
51
|
+
id,
|
|
52
|
+
instance,
|
|
53
|
+
type: "error",
|
|
54
|
+
data: error
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// server/index.ts
|
|
61
|
+
var REGISTRATIONS = /* @__PURE__ */ new Map();
|
|
62
|
+
function $$server(id, callback) {
|
|
63
|
+
const reg = [id, callback];
|
|
64
|
+
REGISTRATIONS.set(id, reg);
|
|
65
|
+
return callback;
|
|
66
|
+
}
|
|
67
|
+
function $$setup() {
|
|
68
|
+
self.onmessage = (event) => {
|
|
69
|
+
if (event.data.type === "next") {
|
|
70
|
+
const result = deserialize(event.data.data);
|
|
71
|
+
if (event.data.initial) {
|
|
72
|
+
const registration = REGISTRATIONS.get(event.data.id);
|
|
73
|
+
if (registration) {
|
|
74
|
+
const [id, callback] = registration;
|
|
75
|
+
if (id === event.data.id) {
|
|
76
|
+
sendWorkerData(
|
|
77
|
+
self,
|
|
78
|
+
event.data.id,
|
|
79
|
+
event.data.instance,
|
|
80
|
+
callback.apply(null, result)
|
|
81
|
+
);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
throw new Error(`Worker function "${event.data.id}" is not found.`);
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
delete $R[event.data.instance];
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export {
|
|
93
|
+
$$server,
|
|
94
|
+
$$setup
|
|
95
|
+
};
|
|
96
|
+
//# sourceMappingURL=server.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../server/index.ts", "../../../shared/data.ts"],
|
|
4
|
+
"sourcesContent": ["import { deserialize } from 'seroval';\nimport { sendWorkerData, type SerializedWorkerData } from '../shared/data';\n\ntype ServerHandler<Args extends unknown[], Return> = (\n ...args: Args\n) => Promise<Return>;\n\ntype HandlerRegistration = [\n id: string,\n callback: ServerHandler<unknown[], unknown>,\n];\n\nconst REGISTRATIONS = new Map<string, HandlerRegistration>();\n\nexport function $$server(\n id: string,\n callback: ServerHandler<unknown[], unknown>,\n): ServerHandler<unknown[], unknown> {\n const reg: HandlerRegistration = [id, callback];\n REGISTRATIONS.set(id, reg);\n return callback;\n}\n\ndeclare const $R: Record<string, unknown>;\n\nexport function $$setup(): void {\n self.onmessage = (event: MessageEvent<SerializedWorkerData>) => {\n if (event.data.type === 'next') {\n const result = deserialize(event.data.data);\n if (event.data.initial) {\n const registration = REGISTRATIONS.get(event.data.id);\n if (registration) {\n const [id, callback] = registration;\n if (id === event.data.id) {\n sendWorkerData(\n self,\n event.data.id,\n event.data.instance,\n callback.apply(null, result as unknown[]),\n );\n return;\n }\n }\n throw new Error(`Worker function \"${event.data.id}\" is not found.`);\n }\n } else {\n delete $R[event.data.instance];\n }\n };\n}\n", "import { crossSerializeStream, getCrossReferenceHeader } from 'seroval';\nimport {\n CustomEventPlugin,\n DOMExceptionPlugin,\n EventPlugin,\n FormDataPlugin,\n HeadersPlugin,\n ReadableStreamPlugin,\n RequestPlugin,\n ResponsePlugin,\n URLPlugin,\n URLSearchParamsPlugin,\n} from 'seroval-plugins/web';\n\ninterface SerializedWorkerDataBase {\n id: string;\n instance: string;\n}\n\ninterface SerializedWorkerDataNext extends SerializedWorkerDataBase {\n type: 'next';\n initial: boolean;\n data: string;\n}\n\ninterface SerializedWorkerDataClose extends SerializedWorkerDataBase {\n type: 'close';\n}\n\ninterface SerializedWorkerDataError extends SerializedWorkerDataBase {\n type: 'error';\n data: unknown;\n}\n\nexport type SerializedWorkerData =\n | SerializedWorkerDataNext\n | SerializedWorkerDataClose\n | SerializedWorkerDataError;\n\nexport function sendWorkerData<T>(\n target: (Window & typeof globalThis) | Worker,\n id: string,\n instance: string,\n data: T,\n): () => void {\n return crossSerializeStream(data, {\n scopeId: instance,\n plugins: [\n CustomEventPlugin,\n DOMExceptionPlugin,\n EventPlugin,\n FormDataPlugin,\n HeadersPlugin,\n ReadableStreamPlugin,\n RequestPlugin,\n ResponsePlugin,\n URLSearchParamsPlugin,\n URLPlugin,\n ],\n onSerialize(data, initial) {\n target.postMessage({\n id,\n instance,\n type: 'next',\n initial,\n data: initial ? `(${getCrossReferenceHeader(instance)},${data})` : data,\n });\n },\n onDone() {\n target.postMessage({\n id,\n instance,\n type: 'done',\n });\n },\n onError(error) {\n target.postMessage({\n id,\n instance,\n type: 'error',\n data: error,\n });\n },\n });\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,mBAAmB;;;ACA5B,SAAS,sBAAsB,+BAA+B;AAC9D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AA2BA,SAAS,eACd,QACA,IACA,UACA,MACY;AACZ,SAAO,qBAAqB,MAAM;AAAA,IAChC,SAAS;AAAA,IACT,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,YAAYA,OAAM,SAAS;AACzB,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,QACN;AAAA,QACA,MAAM,UAAU,IAAI,wBAAwB,QAAQ,CAAC,IAAIA,KAAI,MAAMA;AAAA,MACrE,CAAC;AAAA,IACH;AAAA,IACA,SAAS;AACP,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAAA,IACA,QAAQ,OAAO;AACb,aAAO,YAAY;AAAA,QACjB;AAAA,QACA;AAAA,QACA,MAAM;AAAA,QACN,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AACH;;;ADxEA,IAAM,gBAAgB,oBAAI,IAAiC;AAEpD,SAAS,SACd,IACA,UACmC;AACnC,QAAM,MAA2B,CAAC,IAAI,QAAQ;AAC9C,gBAAc,IAAI,IAAI,GAAG;AACzB,SAAO;AACT;AAIO,SAAS,UAAgB;AAC9B,OAAK,YAAY,CAAC,UAA8C;AAC9D,QAAI,MAAM,KAAK,SAAS,QAAQ;AAC9B,YAAM,SAAS,YAAY,MAAM,KAAK,IAAI;AAC1C,UAAI,MAAM,KAAK,SAAS;AACtB,cAAM,eAAe,cAAc,IAAI,MAAM,KAAK,EAAE;AACpD,YAAI,cAAc;AAChB,gBAAM,CAAC,IAAI,QAAQ,IAAI;AACvB,cAAI,OAAO,MAAM,KAAK,IAAI;AACxB;AAAA,cACE;AAAA,cACA,MAAM,KAAK;AAAA,cACX,MAAM,KAAK;AAAA,cACX,SAAS,MAAM,MAAM,MAAmB;AAAA,YAC1C;AACA;AAAA,UACF;AAAA,QACF;AACA,cAAM,IAAI,MAAM,oBAAoB,MAAM,KAAK,EAAE,iBAAiB;AAAA,MACpE;AAAA,IACF,OAAO;AACL,aAAO,GAAG,MAAM,KAAK,QAAQ;AAAA,IAC/B;AAAA,EACF;AACF;",
|
|
6
|
+
"names": ["data"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../setup/index.ts"],
|
|
4
|
+
"sourcesContent": ["console.warn(\n \"If you see this message, it means that `use-worker-directive`'s plugin or compiler is not configured correctly.\",\n);\n"],
|
|
5
|
+
"mappings": ";AAAA,QAAQ;AAAA,EACN;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{deserialize as P}from"seroval";import{crossSerializeStream as l,getCrossReferenceHeader as u}from"seroval";import{CustomEventPlugin as g,DOMExceptionPlugin as p,EventPlugin as k,FormDataPlugin as c,HeadersPlugin as m,ReadableStreamPlugin as R,RequestPlugin as W,ResponsePlugin as f,URLPlugin as S,URLSearchParamsPlugin as D}from"seroval-plugins/web";function s(e,r,a,i){return l(i,{scopeId:a,plugins:[g,p,k,c,m,R,W,f,D,S],onSerialize(o,t){e.postMessage({id:r,instance:a,type:"next",initial:t,data:t?`(${u(a)},${o})`:o})},onDone(){e.postMessage({id:r,instance:a,type:"done"})},onError(o){e.postMessage({id:r,instance:a,type:"error",data:o})}})}var n;function C(e){n=e}var z=0;function x(e,r){return new Promise((a,i)=>{let o=t=>{if(t.data.id===e&&t.data.instance===r)if(t.data.type==="close")n.removeEventListener("message",o),delete $R[r];else if(t.data.type==="error")i(t.data.data),delete $R[r];else{let d=P(t.data.data);t.data.initial&&a(d)}};n.addEventListener("message",o)})}async function y(e,r){let a=`use-worker-directive:${z++}`,i=x(e,a);return s(n,e,a,r),await i}function v(e){return(...r)=>y(e,r)}export{v as $$server,C as $$worker};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as t from"dismantle";var n="__worker",d="use worker";async function u(r,i,e){return await t.compile(r,i,{runtime:"use-worker-directive/runtime",key:"use-worker-directive",mode:e.mode,env:e.env,definitions:[{type:"block-directive",directive:e.directive||d,idPrefix:`/${e.prefix||n}/`,pure:e.pure,target:{kind:"named",source:`use-worker-directive/${e.mode}`,name:"$$server"}}]})}export{u as compile};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"dismantle/runtime";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{deserialize as R}from"seroval";import{crossSerializeStream as d,getCrossReferenceHeader as l}from"seroval";import{CustomEventPlugin as u,DOMExceptionPlugin as g,EventPlugin as p,FormDataPlugin as k,HeadersPlugin as S,ReadableStreamPlugin as c,RequestPlugin as f,ResponsePlugin as D,URLPlugin as W,URLSearchParamsPlugin as z}from"seroval-plugins/web";function i(e,a,r,t){return d(t,{scopeId:r,plugins:[u,g,p,k,S,c,f,D,z,W],onSerialize(n,o){e.postMessage({id:a,instance:r,type:"next",initial:o,data:o?`(${l(r)},${n})`:n})},onDone(){e.postMessage({id:a,instance:r,type:"done"})},onError(n){e.postMessage({id:a,instance:r,type:"error",data:n})}})}var s=new Map;function E(e,a){let r=[e,a];return s.set(e,r),a}function H(){self.onmessage=e=>{if(e.data.type==="next"){let a=R(e.data.data);if(e.data.initial){let r=s.get(e.data.id);if(r){let[t,n]=r;if(t===e.data.id){i(self,e.data.id,e.data.instance,n.apply(null,a));return}}throw new Error(`Worker function "${e.data.id}" is not found.`)}}else delete $R[e.data.instance]}}export{E as $$server,H as $$setup};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
console.warn("If you see this message, it means that `use-worker-directive`'s plugin or compiler is not configured correctly.");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../client/index.ts"],"names":[],"mappings":"AAKA,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAE/C;AA0CD,wBAAgB,QAAQ,CAAC,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAC7C,EAAE,EAAE,MAAM,GACT,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAE5B"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as dismantle from 'dismantle';
|
|
2
|
+
export interface Options extends Pick<dismantle.Options, 'mode' | 'env'> {
|
|
3
|
+
directive?: string;
|
|
4
|
+
prefix?: string;
|
|
5
|
+
pure?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export type Output = dismantle.Output;
|
|
8
|
+
export type CodeOutput = dismantle.CodeOutput;
|
|
9
|
+
export declare function compile(id: string, code: string, options: Options): Promise<Output>;
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../compiler/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,WAAW,CAAC;AAKvC,MAAM,WAAW,OAAQ,SAAQ,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC;IACtE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC;AACtC,MAAM,MAAM,UAAU,GAAG,SAAS,CAAC,UAAU,CAAC;AAE9C,wBAAsB,OAAO,CAC3B,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,MAAM,CAAC,CAoBjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../runtime/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
type ServerHandler<Args extends unknown[], Return> = (...args: Args) => Promise<Return>;
|
|
2
|
+
export declare function $$server(id: string, callback: ServerHandler<unknown[], unknown>): ServerHandler<unknown[], unknown>;
|
|
3
|
+
export declare function $$setup(): void;
|
|
4
|
+
export {};
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../server/index.ts"],"names":[],"mappings":"AAGA,KAAK,aAAa,CAAC,IAAI,SAAS,OAAO,EAAE,EAAE,MAAM,IAAI,CACnD,GAAG,IAAI,EAAE,IAAI,KACV,OAAO,CAAC,MAAM,CAAC,CAAC;AASrB,wBAAgB,QAAQ,CACtB,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,aAAa,CAAC,OAAO,EAAE,EAAE,OAAO,CAAC,GAC1C,aAAa,CAAC,OAAO,EAAE,EAAE,OAAO,CAAC,CAInC;AAID,wBAAgB,OAAO,IAAI,IAAI,CAwB9B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../setup/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface SerializedWorkerDataBase {
|
|
2
|
+
id: string;
|
|
3
|
+
instance: string;
|
|
4
|
+
}
|
|
5
|
+
interface SerializedWorkerDataNext extends SerializedWorkerDataBase {
|
|
6
|
+
type: 'next';
|
|
7
|
+
initial: boolean;
|
|
8
|
+
data: string;
|
|
9
|
+
}
|
|
10
|
+
interface SerializedWorkerDataClose extends SerializedWorkerDataBase {
|
|
11
|
+
type: 'close';
|
|
12
|
+
}
|
|
13
|
+
interface SerializedWorkerDataError extends SerializedWorkerDataBase {
|
|
14
|
+
type: 'error';
|
|
15
|
+
data: unknown;
|
|
16
|
+
}
|
|
17
|
+
export type SerializedWorkerData = SerializedWorkerDataNext | SerializedWorkerDataClose | SerializedWorkerDataError;
|
|
18
|
+
export declare function sendWorkerData<T>(target: (Window & typeof globalThis) | Worker, id: string, instance: string, data: T): () => void;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=data.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../../shared/data.ts"],"names":[],"mappings":"AAcA,UAAU,wBAAwB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,UAAU,wBAAyB,SAAQ,wBAAwB;IACjE,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,UAAU,yBAA0B,SAAQ,wBAAwB;IAClE,IAAI,EAAE,OAAO,CAAC;CACf;AAED,UAAU,yBAA0B,SAAQ,wBAAwB;IAClE,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,EAAE,OAAO,CAAC;CACf;AAED,MAAM,MAAM,oBAAoB,GAC5B,wBAAwB,GACxB,yBAAyB,GACzB,yBAAyB,CAAC;AAE9B,wBAAgB,cAAc,CAAC,CAAC,EAC9B,MAAM,EAAE,CAAC,MAAM,GAAG,OAAO,UAAU,CAAC,GAAG,MAAM,EAC7C,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,CAAC,GACN,MAAM,IAAI,CAwCZ"}
|
package/package.json
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-worker-directive",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"src"
|
|
8
|
+
],
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=10"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"pridepack"
|
|
15
|
+
],
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^22.13.10",
|
|
18
|
+
"pridepack": "2.6.4",
|
|
19
|
+
"tslib": "^2.8.1",
|
|
20
|
+
"typescript": "^5.9.3",
|
|
21
|
+
"vitest": "^2.0.5"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"dismantle": "^0.5.0",
|
|
25
|
+
"seroval": "^1.1.1",
|
|
26
|
+
"seroval-plugins": "^1.1.1"
|
|
27
|
+
},
|
|
28
|
+
"description": "Universal \"use worker\" functions",
|
|
29
|
+
"repository": {
|
|
30
|
+
"url": "https://github.com/lxsmnsyc/dismantle.git",
|
|
31
|
+
"type": "git"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/lxsmnsyc/dismantle/tree/main/use-worker-directive/core",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/lxsmnsyc/dismantle/issues"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"author": "Alexis Munsayac",
|
|
41
|
+
"private": false,
|
|
42
|
+
"typesVersions": {
|
|
43
|
+
"*": {
|
|
44
|
+
"compiler": [
|
|
45
|
+
"./dist/types/compiler/index.d.ts"
|
|
46
|
+
],
|
|
47
|
+
"client": [
|
|
48
|
+
"./dist/types/client/index.d.ts"
|
|
49
|
+
],
|
|
50
|
+
"server": [
|
|
51
|
+
"./dist/types/server/index.d.ts"
|
|
52
|
+
],
|
|
53
|
+
"runtime": [
|
|
54
|
+
"./dist/types/runtime/index.d.ts"
|
|
55
|
+
],
|
|
56
|
+
"setup": [
|
|
57
|
+
"./dist/types/setup/index.d.ts"
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"exports": {
|
|
62
|
+
"./compiler": {
|
|
63
|
+
"types": "./dist/types/compiler/index.d.ts",
|
|
64
|
+
"development": {
|
|
65
|
+
"require": "./dist/cjs/development/compiler.cjs",
|
|
66
|
+
"import": "./dist/esm/development/compiler.mjs"
|
|
67
|
+
},
|
|
68
|
+
"require": "./dist/cjs/production/compiler.cjs",
|
|
69
|
+
"import": "./dist/esm/production/compiler.mjs"
|
|
70
|
+
},
|
|
71
|
+
"./client": {
|
|
72
|
+
"types": "./dist/types/client/index.d.ts",
|
|
73
|
+
"development": {
|
|
74
|
+
"require": "./dist/cjs/development/client.cjs",
|
|
75
|
+
"import": "./dist/esm/development/client.mjs"
|
|
76
|
+
},
|
|
77
|
+
"require": "./dist/cjs/production/client.cjs",
|
|
78
|
+
"import": "./dist/esm/production/client.mjs"
|
|
79
|
+
},
|
|
80
|
+
"./server": {
|
|
81
|
+
"types": "./dist/types/server/index.d.ts",
|
|
82
|
+
"development": {
|
|
83
|
+
"require": "./dist/cjs/development/server.cjs",
|
|
84
|
+
"import": "./dist/esm/development/server.mjs"
|
|
85
|
+
},
|
|
86
|
+
"require": "./dist/cjs/production/server.cjs",
|
|
87
|
+
"import": "./dist/esm/production/server.mjs"
|
|
88
|
+
},
|
|
89
|
+
"./runtime": {
|
|
90
|
+
"types": "./dist/types/runtime/index.d.ts",
|
|
91
|
+
"development": {
|
|
92
|
+
"require": "./dist/cjs/development/runtime.cjs",
|
|
93
|
+
"import": "./dist/esm/development/runtime.mjs"
|
|
94
|
+
},
|
|
95
|
+
"require": "./dist/cjs/production/runtime.cjs",
|
|
96
|
+
"import": "./dist/esm/production/runtime.mjs"
|
|
97
|
+
},
|
|
98
|
+
"./setup": {
|
|
99
|
+
"types": "./dist/types/setup/index.d.ts",
|
|
100
|
+
"development": {
|
|
101
|
+
"require": "./dist/cjs/development/setup.cjs",
|
|
102
|
+
"import": "./dist/esm/development/setup.mjs"
|
|
103
|
+
},
|
|
104
|
+
"require": "./dist/cjs/production/setup.cjs",
|
|
105
|
+
"import": "./dist/esm/production/setup.mjs"
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
"scripts": {
|
|
109
|
+
"build": "pridepack build",
|
|
110
|
+
"type-check": "pridepack check",
|
|
111
|
+
"clean": "pridepack clean",
|
|
112
|
+
"watch": "pridepack watch",
|
|
113
|
+
"start": "pridepack start",
|
|
114
|
+
"dev": "pridepack dev",
|
|
115
|
+
"test": "vitest"
|
|
116
|
+
}
|
|
117
|
+
}
|