reflected 0.1.3 → 0.1.5

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.
Files changed (88) hide show
  1. package/README.md +2 -2
  2. package/dist/{async-CYIlyalV.js → async-CDZL7tYL.js} +1 -1
  3. package/dist/async.js +1 -1
  4. package/dist/{broadcast-BDDwfg2T.js → broadcast-D1fpCELk.js} +1 -1
  5. package/dist/broadcast.js +1 -1
  6. package/dist/ffi/{async-GRzyB2di.js → async-CwB3HVh4.js} +1 -1
  7. package/dist/ffi/{async-BNBXm5a_.js → async-DhVtZ4wl.js} +1 -1
  8. package/dist/ffi/{broadcast-By0cJHF9.js → broadcast-BAquhdtc.js} +1 -1
  9. package/dist/ffi/{broadcast-BqHn8hZt.js → broadcast-C0yTW057.js} +1 -1
  10. package/dist/ffi/main.js +1 -1
  11. package/dist/ffi/{message-HrRNTxf_.js → message-BDLi9xtr.js} +1 -1
  12. package/dist/ffi/{message-BDoj0Sat.js → message-CJgJHezK.js} +1 -1
  13. package/dist/ffi/{sender-BT8LRjVR.js → sender-BskapjNv.js} +1 -1
  14. package/dist/ffi/{sender-Cg6GoMam.js → sender-oKBWXO4e.js} +1 -1
  15. package/dist/ffi/{shared-BGovulqy.js → shared-CEDblaxF.js} +1 -1
  16. package/dist/ffi/{shared-DO64_ikX.js → shared-CgVCTgQi.js} +1 -1
  17. package/dist/ffi/worker.js +1 -1
  18. package/dist/ffi/{xhr-DncbZcB-.js → xhr-Bph_vID8.js} +1 -1
  19. package/dist/ffi/{xhr-mth_w1Qg.js → xhr-DpdgA5CQ.js} +1 -1
  20. package/dist/index.js +1 -1
  21. package/dist/{message-Dt36xGK4.js → message-CnhrtE83.js} +1 -1
  22. package/dist/message.js +1 -1
  23. package/dist/{sender-DL3isFNg.js → sender-D2z-45zI.js} +1 -1
  24. package/dist/{shared-DPsM-o7V.js → shared-d1h2fx_s.js} +1 -1
  25. package/dist/{xhr-1Wu8YOTt.js → xhr-CHdFcg-8.js} +1 -1
  26. package/dist/xhr.js +1 -1
  27. package/package.json +12 -8
  28. package/reflected.tar.gz +0 -0
  29. package/rollup.js +0 -75
  30. package/src/async.js +0 -8
  31. package/src/broadcast.js +0 -8
  32. package/src/channel.js +0 -1
  33. package/src/ffi/main.js +0 -37
  34. package/src/ffi/worker.js +0 -24
  35. package/src/index.js +0 -57
  36. package/src/main/async.js +0 -29
  37. package/src/main/broadcast.js +0 -21
  38. package/src/main/message.js +0 -20
  39. package/src/main/sab.js +0 -12
  40. package/src/main/sender.js +0 -38
  41. package/src/main/shared.js +0 -99
  42. package/src/main/xhr.js +0 -102
  43. package/src/message.js +0 -8
  44. package/src/service/listeners.js +0 -47
  45. package/src/service/worker.js +0 -5
  46. package/src/shared.js +0 -12
  47. package/src/worker/async.js +0 -40
  48. package/src/worker/broadcast.js +0 -11
  49. package/src/worker/message.js +0 -11
  50. package/src/worker/sender.js +0 -16
  51. package/src/worker/shared.js +0 -52
  52. package/src/worker/xhr.js +0 -40
  53. package/src/xhr.js +0 -8
  54. package/test/README/index.html +0 -11
  55. package/test/README/index.js +0 -50
  56. package/test/README/worker.js +0 -26
  57. package/test/ffi/index.html +0 -10
  58. package/test/ffi/index.js +0 -8
  59. package/test/ffi/sw.js +0 -1
  60. package/test/ffi/worker.js +0 -13
  61. package/test/index.html +0 -17
  62. package/test/index.js +0 -35
  63. package/test/mini-coi.js +0 -28
  64. package/test/sw.js +0 -1
  65. package/test/worker.js +0 -22
  66. package/types/reflected/src/async.d.ts +0 -3
  67. package/types/reflected/src/broadcast.d.ts +0 -3
  68. package/types/reflected/src/channel.d.ts +0 -2
  69. package/types/reflected/src/index.d.ts +0 -3
  70. package/types/reflected/src/main/async.d.ts +0 -7
  71. package/types/reflected/src/main/broadcast.d.ts +0 -2
  72. package/types/reflected/src/main/message.d.ts +0 -2
  73. package/types/reflected/src/main/sab.d.ts +0 -5
  74. package/types/reflected/src/main/sender.d.ts +0 -5
  75. package/types/reflected/src/main/shared.d.ts +0 -44
  76. package/types/reflected/src/main/xhr.d.ts +0 -2
  77. package/types/reflected/src/message.d.ts +0 -3
  78. package/types/reflected/src/service/listeners.d.ts +0 -3
  79. package/types/reflected/src/service/worker.d.ts +0 -1
  80. package/types/reflected/src/shared.d.ts +0 -1
  81. package/types/reflected/src/worker/async.d.ts +0 -3
  82. package/types/reflected/src/worker/broadcast.d.ts +0 -3
  83. package/types/reflected/src/worker/message.d.ts +0 -3
  84. package/types/reflected/src/worker/sender.d.ts +0 -2
  85. package/types/reflected/src/worker/shared.d.ts +0 -11
  86. package/types/reflected/src/worker/xhr.d.ts +0 -3
  87. package/types/reflected/src/xhr.d.ts +0 -3
  88. package/types/weak-id/i32.d.ts +0 -2
@@ -1,11 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <style>body{font-family:sans-serif}</style>
7
- <title>reflected README</title>
8
- <script src="../mini-coi.js"></script>
9
- <script type="module" src="./index.js"></script>
10
- </head>
11
- </html>
@@ -1,50 +0,0 @@
1
- import reflect, { channel } from '../../dist/index.js';
2
-
3
- function test_sum(...args) {
4
- let i = 0;
5
- while (args.length)
6
- i += args.pop();
7
- return i;
8
- }
9
-
10
- // ℹ️ must await the initialization
11
- const worker = await reflect(
12
- // Worker scriptURL
13
- './worker.js',
14
- // Worker options + required utilities / helpers
15
- // ℹ️ type is enforced to be 'module' due top-level await
16
- {
17
- // invoked when the worker asks to synchronize a call
18
- // ℹ️ works even if synchronous but it's resolved asynchronously
19
- // ⚠️ the worker is not responsive until this returns so
20
- // be sure you handle errors gracefully to still provide
21
- // a result the worker can consume out of the shared buffer!
22
- async onsync(payload) {
23
- const { invoke, args } = payload;
24
-
25
- if (invoke === 'test_sum') {
26
- // just demoing this can be async too
27
- return await test_sum(...args);
28
- }
29
-
30
- // it is trivial to return no result or even errors
31
- return new Error('unknown ' + invoke);
32
- },
33
-
34
- // optional: the initial SharedArrayBuffer length
35
- initByteLength: 1024,
36
-
37
- // optional: the max possible SharedArrayBuffer growth
38
- maxByteLength: 8192,
39
-
40
- // optional: the service worker as fallback
41
- // * if it's a string, it's used to register it
42
- // * if it's an object, it's used to initialize it
43
- // but it must contain a `url` field to register it
44
- // ℹ️ if already registered it will not try to register it
45
- serviceWorker: undefined,
46
- }
47
- );
48
-
49
- document.body.append(channel, ' ');
50
- document.body.append(JSON.stringify(await worker.send({ any: 'payload' })));
@@ -1,26 +0,0 @@
1
- import reflect, { channel } from '../../dist/index.js';
2
-
3
- // ℹ️ must await the initialization
4
- const reflected = await reflect({
5
- // receives the returned data from the main thread.
6
- // use this helper to transform such data into something
7
- // that the worker can use/understand after invoke
8
- // ℹ️ must be synchronous and it's invoked synchronously
9
- onsync(response) {
10
- return response;
11
- },
12
-
13
- async onsend(payload) {
14
- return payload;
15
- },
16
- });
17
-
18
- // retrive the result of `test_sum(1, 2, 3)`
19
- // directly from the main thread.
20
- // only the async channel variant would need to await
21
- const value = reflected({
22
- invoke: 'test_sum',
23
- args: [1, 2, 3]
24
- });
25
-
26
- console.log({ channel, value });
@@ -1,10 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <style>body{font-family:sans-serif}</style>
7
- <title>reflected ffi</title>
8
- <script type="module" src="./index.js"></script>
9
- </head>
10
- </html>
package/test/ffi/index.js DELETED
@@ -1,8 +0,0 @@
1
- import worker from '../../dist/ffi/main.js';
2
-
3
- const w = await worker('./worker.js', { serviceWorker: './sw.js' });
4
-
5
- document.body.append('channel: ', w.channel, ' - ');
6
-
7
- console.log(w);
8
-
package/test/ffi/sw.js DELETED
@@ -1 +0,0 @@
1
- const e=Promise.withResolvers||function(){var e,t,n=new this((n,s)=>{e=n,t=s});return{resolve:e,reject:t,promise:n}};var t=e.bind(Promise);const[n,s]=((e=e=>e)=>{const n=new Map,s=(()=>{const e=new Int32Array(1);return()=>e[0]++})();return[()=>{let a;do{a=e(s())}while(n.has(a));const o=t();return n.set(a,o),[a,o.promise]},(e,t,s)=>{const a=n.get(e);n.delete(e),s?a?.reject(s):a?.resolve(t)}]})(),{protocol:a,host:o,pathname:r}=location,i=`${a}//${o}${r}`,c=new BroadcastChannel("fc260aad-4404-43b8-ae9d-2c06554bb294");c.addEventListener("message",({data:[e,t]})=>{if("response"===e){const[e,n]=t;s(e,`[${n.join(",")}]`)}});const d={status:200,statusText:"OK",headers:new Headers({"Cache-Control":"no-cache, must-revalidate",Expires:"Mon, 26 Jul 1997 05:00:00 GMT","Content-type":"application/json"})},l=async e=>{const[t,s]=n();return c.postMessage(["request",[t,e]]),new Response(await s,d)};addEventListener("activate",e=>e.waitUntil(clients.claim())),addEventListener("fetch",async e=>{const{request:t}=e;"POST"===t.method&&t.url.startsWith(i)&&(e.stopImmediatePropagation(),e.respondWith(t.json().then(l)),e.preventDefault())}),addEventListener("install",()=>skipWaiting());
@@ -1,13 +0,0 @@
1
- import worker from '../../dist/ffi/worker.js';
2
-
3
- const ffi = await worker();
4
-
5
- console.log(ffi);
6
-
7
- const body = ffi.query(ffi.global, 'document.body');
8
-
9
- body.addEventListener('click', event => {
10
- console.log(event.type);
11
- });
12
-
13
- body.append('Hello World');
package/test/index.html DELETED
@@ -1,17 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <style>body{font-family:sans-serif}</style>
7
- <title>reflected</title>
8
- <!-- <script src="mini-coi.js"></script> -->
9
- <script type="module">
10
- document.body.prepend(navigator.userAgent);
11
- import('./index.js');
12
- </script>
13
- </head>
14
- <body>
15
- <pre id="log"></pre>
16
- </body>
17
- </html>
package/test/index.js DELETED
@@ -1,35 +0,0 @@
1
- import reflected, { channel } from '../dist/index.js';
2
- log.append(`bootstrapping: ${channel}\n`);
3
-
4
- try {
5
- const worker = await reflected('./worker.js', {
6
- serviceWorker: location.search === '?async' ? undefined : './sw.js',
7
- onsend: async (data, ...rest) => {
8
- // console.log('onsend', data, rest);
9
- return data;
10
- },
11
- onsync: async (data, ...rest) => {
12
- // console.log('main', data, rest);
13
- // await new Promise(resolve => setTimeout(resolve, 1000));
14
- return [6, 7, 8, 9, 10];
15
- },
16
- onerror: console.error,
17
- });
18
-
19
- log.append(`consuming: ${worker.channel}\n`);
20
-
21
- worker.addEventListener(
22
- 'message',
23
- (event) => {
24
- log.append(`reflected channel: ${event.data}\n`);
25
- },
26
- { once: true }
27
- );
28
-
29
- worker.send([1, 2, 3, 4, 5]).then(data => {
30
- log.append(`send: [${data}]\n`);
31
- });
32
- } catch (error) {
33
- log.append(`error: ${error.message}\n`);
34
- }
35
-
package/test/mini-coi.js DELETED
@@ -1,28 +0,0 @@
1
- /*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */
2
- /*! mini-coi - Andrea Giammarchi and contributors, licensed under MIT */
3
- (({ document: d, navigator: { serviceWorker: s } }) => {
4
- if (d) {
5
- const { currentScript: c } = d;
6
- s.register(c.src, { scope: c.getAttribute('scope') || '.' }).then(r => {
7
- r.addEventListener('updatefound', () => location.reload());
8
- if (r.active && !s.controller) location.reload();
9
- });
10
- }
11
- else {
12
- addEventListener('install', () => skipWaiting());
13
- addEventListener('activate', e => e.waitUntil(clients.claim()));
14
- addEventListener('fetch', e => {
15
- const { request: r } = e;
16
- if (r.cache === 'only-if-cached' && r.mode !== 'same-origin') return;
17
- e.respondWith(fetch(r).then(r => {
18
- const { body, status, statusText } = r;
19
- if (!status || status > 399) return r;
20
- const h = new Headers(r.headers);
21
- h.set('Cross-Origin-Opener-Policy', 'same-origin');
22
- h.set('Cross-Origin-Embedder-Policy', 'require-corp');
23
- h.set('Cross-Origin-Resource-Policy', 'cross-origin');
24
- return new Response(status == 204 ? null : body, { status, statusText, headers: h });
25
- }));
26
- });
27
- }
28
- })(self);
package/test/sw.js DELETED
@@ -1 +0,0 @@
1
- const e=Promise.withResolvers||function(){var e,t,n=new this((n,s)=>{e=n,t=s});return{resolve:e,reject:t,promise:n}};var t=e.bind(Promise);const[n,s]=((e=e=>e)=>{const n=new Map,s=(()=>{const e=new Int32Array(1);return()=>e[0]++})();return[()=>{let a;do{a=e(s())}while(n.has(a));const o=t();return n.set(a,o),[a,o.promise]},(e,t,s)=>{const a=n.get(e);n.delete(e),s?a?.reject(s):a?.resolve(t)}]})(),{protocol:a,host:o,pathname:r}=location,i=`${a}//${o}${r}`,c=new BroadcastChannel("fc260aad-4404-43b8-ae9d-2c06554bb294");c.addEventListener("message",({data:[e,t]})=>{if("response"===e){const[e,n]=t;s(e,`[${n.join(",")}]`)}});const d={status:200,statusText:"OK",headers:new Headers({"Cache-Control":"no-cache, must-revalidate",Expires:"Mon, 26 Jul 1997 05:00:00 GMT","Content-type":"application/json"})},l=async e=>{const[t,s]=n();return c.postMessage(["request",[t,e]]),new Response(await s,d)};addEventListener("activate",e=>e.waitUntil(clients.claim())),addEventListener("fetch",async e=>{const{request:t}=e;"POST"===t.method&&t.url.startsWith(i)&&(e.stopImmediatePropagation(),e.respondWith(t.json().then(l)),e.preventDefault())}),addEventListener("install",()=>skipWaiting());
package/test/worker.js DELETED
@@ -1,22 +0,0 @@
1
- import reflected, { channel } from '../dist/index.js';
2
-
3
- const sync = await reflected({
4
- onsend: (data, ...rest) => {
5
- // console.log('worker', [...data], rest);
6
- return data;
7
- },
8
- onsync: (data, ...rest) => {
9
- // console.log('worker', [...data], rest);
10
- return data;
11
- }
12
- });
13
-
14
- for (let i = 0; i < 4; i++) sync('test');
15
-
16
- console.time('sync');
17
- const result = await sync('test');
18
- console.timeEnd('sync');
19
-
20
- console.log(channel, result);
21
-
22
- postMessage(`${channel} - [${result?.join?.(',')}]`);
@@ -1,3 +0,0 @@
1
- export { channel } from "./worker/async.js";
2
- declare const _default: ((scriptURL: string, options: Options) => Promise<Worker>) | ((options: any) => any);
3
- export default _default;
@@ -1,3 +0,0 @@
1
- export { channel } from "./worker/broadcast.js";
2
- declare const _default: ((scriptURL: string, options: Options) => Promise<Worker>) | ((options: import("./worker/shared.js").Options) => Promise<(data: unknown, ...rest: unknown[]) => unknown>);
3
- export default _default;
@@ -1,2 +0,0 @@
1
- declare const _default: "50c53a30-e088-443f-a277-88c390dbdc0c";
2
- export default _default;
@@ -1,3 +0,0 @@
1
- export default module;
2
- export let channel: any;
3
- declare let module: any;
@@ -1,7 +0,0 @@
1
- export class Worker extends Sender {
2
- constructor(scriptURL: any, options: any, resolve: any);
3
- get channel(): string;
4
- }
5
- declare const _default: (scriptURL: string, options: Options) => Promise<globalThis.Worker>;
6
- export default _default;
7
- import Sender from './sender.js';
@@ -1,2 +0,0 @@
1
- declare const _default: (scriptURL: string, options: Options) => Promise<Worker>;
2
- export default _default;
@@ -1,2 +0,0 @@
1
- declare const _default: (scriptURL: string, options: Options) => Promise<Worker>;
2
- export default _default;
@@ -1,5 +0,0 @@
1
- declare function _default({ initByteLength, maxByteLength }: {
2
- initByteLength?: number;
3
- maxByteLength?: number;
4
- }): any;
5
- export default _default;
@@ -1,5 +0,0 @@
1
- export default class Sender extends Worker {
2
- constructor(scriptURL: any, options: any);
3
- send(payload: any, ...rest: any[]): any;
4
- #private;
5
- }
@@ -1,44 +0,0 @@
1
- export const minByteLength: number;
2
- export function SAB({ initByteLength, maxByteLength }: {
3
- initByteLength?: number;
4
- maxByteLength?: number;
5
- }): SharedArrayBuffer;
6
- export function bootstrap(Worker: Worker): (scriptURL: string, options: Options) => Promise<Worker>;
7
- export function handler(sab: any, options: any, useAtomics: any): ({ data }: {
8
- data: any;
9
- }) => Promise<void>;
10
- export function post(sab: any, options: any): any[];
11
- export function url(scriptURL: any, reflected: any, options: any): any[];
12
- /**
13
- * see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#options
14
- */
15
- export type ServiceWorkerOptions = {
16
- /**
17
- * will use the `serviceWorker` value if that is a `string`, otherwise it refers to where the service worker file is located.
18
- */
19
- url?: string;
20
- type?: "classic" | "module";
21
- updateViaCache?: "all" | "imports" | "none";
22
- };
23
- export type Options = {
24
- /**
25
- * invoked when the worker expect a response as `Int32Array` to populate the SharedArrayBuffer with.
26
- */
27
- onsync: (data: unknown) => Promise<Int32Array>;
28
- /**
29
- * invoked when the worker replies to a `worker.send(data)` call.
30
- */
31
- onsend?: (data: unknown) => unknown;
32
- /**
33
- * defines the initial byte length of the SharedArrayBuffer.
34
- */
35
- initByteLength?: number;
36
- /**
37
- * defines the maximum byte length (growth) of the SharedArrayBuffer.
38
- */
39
- maxByteLength?: number;
40
- /**
41
- * defines the service worker to use as fallback if SharedArrayBuffer is not supported. If not defined, the `async` fallback will be used so that no `sync` operations from the worker will be possible.
42
- */
43
- serviceWorker?: string | ServiceWorkerOptions;
44
- };
@@ -1,2 +0,0 @@
1
- declare function _default(scriptURL: any, options: any): any;
2
- export default _default;
@@ -1,3 +0,0 @@
1
- export { channel } from "./worker/message.js";
2
- declare const _default: ((scriptURL: string, options: Options) => Promise<Worker>) | ((options: import("./worker/shared.js").Options) => Promise<(data: unknown, ...rest: unknown[]) => unknown>);
3
- export default _default;
@@ -1,3 +0,0 @@
1
- export function activate(event: any): any;
2
- export function fetch(event: any): Promise<void>;
3
- export function install(): any;
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export function randomUUID(): string;
@@ -1,3 +0,0 @@
1
- export const channel: "async";
2
- declare function _default(options: any): any;
3
- export default _default;
@@ -1,3 +0,0 @@
1
- export const channel: "broadcast";
2
- declare const _default: (options: import("./shared.js").Options) => Promise<(data: unknown, ...rest: unknown[]) => unknown>;
3
- export default _default;
@@ -1,3 +0,0 @@
1
- export const channel: "message";
2
- declare const _default: (options: import("./shared.js").Options) => Promise<(data: unknown, ...rest: unknown[]) => unknown>;
3
- export default _default;
@@ -1,2 +0,0 @@
1
- declare function _default(options: any): any;
2
- export default _default;
@@ -1,11 +0,0 @@
1
- export function handler(promise: Promise<[SharedArrayBuffer, Options, MessageChannel | BroadcastChannel]>, listener: (event: WorkerEvent) => void): (options: Options) => Promise<(data: unknown, ...rest: unknown[]) => unknown>;
2
- export type Options = {
3
- /**
4
- * transforms the resulting `Int32Array` from *main* thread into a value usable within the worker.
5
- */
6
- onsync: (data: Int32Array) => unknown;
7
- /**
8
- * invoked to define what to return to the *main* thread when it calls `worker.send(data)`.
9
- */
10
- onsend: (data: unknown) => unknown;
11
- };
@@ -1,3 +0,0 @@
1
- export const channel: "xhr";
2
- declare function _default(options: any): any;
3
- export default _default;
@@ -1,3 +0,0 @@
1
- export { channel } from "./worker/xhr.js";
2
- declare const _default: (scriptURL: any, options: any) => any;
3
- export default _default;
@@ -1,2 +0,0 @@
1
- declare function _default(start?: number): () => number;
2
- export default _default;