kkrpc 0.0.10 → 0.0.12

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 (82) hide show
  1. package/dist/browser-mod.cjs +501 -17
  2. package/dist/{src/adapters/iframe.d.ts → browser-mod.d.cts} +10 -4
  3. package/dist/browser-mod.d.ts +63 -7
  4. package/dist/browser-mod.js +158 -6
  5. package/dist/{src/channel.d.ts → channel-CGr_xSbe.d.cts} +26 -3
  6. package/dist/channel-CGr_xSbe.d.ts +64 -0
  7. package/dist/chunk-CXUFWGRI.js +93 -0
  8. package/dist/chunk-KUE6DDOO.js +245 -0
  9. package/dist/chunk-XU7DWWSJ.js +88 -0
  10. package/dist/{src/adapters/http.d.ts → http-BEfQiILG.d.cts} +6 -5
  11. package/dist/http-CDA69pfL.d.ts +33 -0
  12. package/dist/http.cjs +355 -15
  13. package/dist/http.d.cts +13 -0
  14. package/dist/http.d.ts +8 -7
  15. package/dist/http.js +21 -10
  16. package/dist/mod.cjs +644 -28
  17. package/dist/mod.d.cts +79 -0
  18. package/dist/mod.d.ts +79 -10
  19. package/dist/mod.js +204 -9
  20. package/dist/serialization-BrfKixNU.d.cts +48 -0
  21. package/dist/serialization-C0yxGsSr.d.ts +48 -0
  22. package/package.json +12 -14
  23. package/dist/__tests__/bun.worker.test.d.ts +0 -2
  24. package/dist/__tests__/bun.worker.test.d.ts.map +0 -1
  25. package/dist/__tests__/http.test.d.ts +0 -2
  26. package/dist/__tests__/http.test.d.ts.map +0 -1
  27. package/dist/__tests__/scripts/api.d.ts +0 -19
  28. package/dist/__tests__/scripts/api.d.ts.map +0 -1
  29. package/dist/__tests__/scripts/deno-api.d.ts +0 -2
  30. package/dist/__tests__/scripts/deno-api.d.ts.map +0 -1
  31. package/dist/__tests__/scripts/node-api.d.ts +0 -2
  32. package/dist/__tests__/scripts/node-api.d.ts.map +0 -1
  33. package/dist/__tests__/scripts/worker.d.ts +0 -2
  34. package/dist/__tests__/scripts/worker.d.ts.map +0 -1
  35. package/dist/__tests__/serialization.test.d.ts +0 -2
  36. package/dist/__tests__/serialization.test.d.ts.map +0 -1
  37. package/dist/__tests__/stdio-rpc.test.d.ts +0 -2
  38. package/dist/__tests__/stdio-rpc.test.d.ts.map +0 -1
  39. package/dist/__tests__/websocket.test.d.ts +0 -2
  40. package/dist/__tests__/websocket.test.d.ts.map +0 -1
  41. package/dist/adapters/deno.cjs +0 -38
  42. package/dist/adapters/deno.js +0 -36
  43. package/dist/adapters/http.cjs +0 -104
  44. package/dist/adapters/http.js +0 -101
  45. package/dist/adapters/iframe.cjs +0 -163
  46. package/dist/adapters/iframe.js +0 -160
  47. package/dist/adapters/node.cjs +0 -66
  48. package/dist/adapters/node.js +0 -64
  49. package/dist/adapters/websocket.cjs +0 -110
  50. package/dist/adapters/websocket.js +0 -107
  51. package/dist/adapters/worker.cjs +0 -101
  52. package/dist/adapters/worker.js +0 -98
  53. package/dist/browser-mod.d.ts.map +0 -1
  54. package/dist/channel.cjs +0 -229
  55. package/dist/channel.js +0 -227
  56. package/dist/http.d.ts.map +0 -1
  57. package/dist/mod.d.ts.map +0 -1
  58. package/dist/scripts/prepare.d.ts +0 -2
  59. package/dist/scripts/prepare.d.ts.map +0 -1
  60. package/dist/scripts/test.d.ts +0 -2
  61. package/dist/scripts/test.d.ts.map +0 -1
  62. package/dist/serialization.cjs +0 -41
  63. package/dist/serialization.js +0 -36
  64. package/dist/src/adapters/deno.d.ts +0 -17
  65. package/dist/src/adapters/deno.d.ts.map +0 -1
  66. package/dist/src/adapters/http.d.ts.map +0 -1
  67. package/dist/src/adapters/iframe.d.ts.map +0 -1
  68. package/dist/src/adapters/node.d.ts +0 -22
  69. package/dist/src/adapters/node.d.ts.map +0 -1
  70. package/dist/src/adapters/websocket.d.ts +0 -38
  71. package/dist/src/adapters/websocket.d.ts.map +0 -1
  72. package/dist/src/adapters/worker.d.ts +0 -25
  73. package/dist/src/adapters/worker.d.ts.map +0 -1
  74. package/dist/src/channel.d.ts.map +0 -1
  75. package/dist/src/interface.d.ts +0 -21
  76. package/dist/src/interface.d.ts.map +0 -1
  77. package/dist/src/serialization.d.ts +0 -19
  78. package/dist/src/serialization.d.ts.map +0 -1
  79. package/dist/src/utils.d.ts +0 -2
  80. package/dist/src/utils.d.ts.map +0 -1
  81. package/dist/utils.cjs +0 -10
  82. package/dist/utils.js +0 -8
@@ -1,21 +1,505 @@
1
- 'use strict';
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);
2
19
 
3
- var worker = require('./adapters/worker.cjs');
4
- require('node:buffer');
5
- var channel = require('./channel.cjs');
6
- var utils = require('./utils.cjs');
7
- var serialization = require('./serialization.cjs');
8
- var iframe = require('./adapters/iframe.cjs');
20
+ // browser-mod.ts
21
+ var browser_mod_exports = {};
22
+ __export(browser_mod_exports, {
23
+ IframeChildIO: () => IframeChildIO,
24
+ IframeParentIO: () => IframeParentIO,
25
+ RPCChannel: () => RPCChannel,
26
+ WorkerChildIO: () => WorkerChildIO,
27
+ WorkerParentIO: () => WorkerParentIO,
28
+ deserializeMessage: () => deserializeMessage,
29
+ deserializeResponse: () => deserializeResponse,
30
+ generateUUID: () => generateUUID,
31
+ serializeMessage: () => serializeMessage,
32
+ serializeResponse: () => serializeResponse
33
+ });
34
+ module.exports = __toCommonJS(browser_mod_exports);
9
35
 
36
+ // src/adapters/worker.ts
37
+ var DESTROY_SIGNAL = "__DESTROY__";
38
+ var WorkerParentIO = class {
39
+ name = "worker-parent-io";
40
+ messageQueue = [];
41
+ resolveRead = null;
42
+ worker;
43
+ constructor(worker) {
44
+ this.worker = worker;
45
+ this.worker.onmessage = this.handleMessage;
46
+ }
47
+ handleMessage = (event) => {
48
+ const message = event.data;
49
+ if (message === DESTROY_SIGNAL) {
50
+ this.destroy();
51
+ return;
52
+ }
53
+ if (this.resolveRead) {
54
+ this.resolveRead(message);
55
+ this.resolveRead = null;
56
+ } else {
57
+ this.messageQueue.push(message);
58
+ }
59
+ };
60
+ read() {
61
+ if (this.messageQueue.length > 0) {
62
+ return Promise.resolve(this.messageQueue.shift() ?? null);
63
+ }
64
+ return new Promise((resolve) => {
65
+ this.resolveRead = resolve;
66
+ });
67
+ }
68
+ write(data) {
69
+ this.worker.postMessage(data);
70
+ return Promise.resolve();
71
+ }
72
+ destroy() {
73
+ this.worker.postMessage(DESTROY_SIGNAL);
74
+ this.worker.terminate();
75
+ }
76
+ signalDestroy() {
77
+ this.worker.postMessage(DESTROY_SIGNAL);
78
+ }
79
+ };
80
+ var WorkerChildIO = class {
81
+ name = "worker-child-io";
82
+ messageQueue = [];
83
+ resolveRead = null;
84
+ constructor() {
85
+ self.onmessage = this.handleMessage;
86
+ }
87
+ handleMessage = (event) => {
88
+ const message = event.data;
89
+ if (message === DESTROY_SIGNAL) {
90
+ this.destroy();
91
+ return;
92
+ }
93
+ if (this.resolveRead) {
94
+ this.resolveRead(message);
95
+ this.resolveRead = null;
96
+ } else {
97
+ this.messageQueue.push(message);
98
+ }
99
+ };
100
+ async read() {
101
+ if (this.messageQueue.length > 0) {
102
+ return this.messageQueue.shift() ?? null;
103
+ }
104
+ return new Promise((resolve) => {
105
+ this.resolveRead = resolve;
106
+ });
107
+ }
108
+ async write(data) {
109
+ self.postMessage(data);
110
+ }
111
+ destroy() {
112
+ self.postMessage(DESTROY_SIGNAL);
113
+ self.close();
114
+ }
115
+ signalDestroy() {
116
+ self.postMessage(DESTROY_SIGNAL);
117
+ }
118
+ };
10
119
 
120
+ // src/interface.ts
121
+ var import_node_buffer = require("buffer");
11
122
 
12
- exports.WorkerChildIO = worker.WorkerChildIO;
13
- exports.WorkerParentIO = worker.WorkerParentIO;
14
- exports.RPCChannel = channel.RPCChannel;
15
- exports.generateUUID = utils.generateUUID;
16
- exports.deserializeMessage = serialization.deserializeMessage;
17
- exports.deserializeResponse = serialization.deserializeResponse;
18
- exports.serializeMessage = serialization.serializeMessage;
19
- exports.serializeResponse = serialization.serializeResponse;
20
- exports.IframeChildIO = iframe.IframeChildIO;
21
- exports.IframeParentIO = iframe.IframeParentIO;
123
+ // src/serialization.ts
124
+ function serializeMessage(message) {
125
+ return JSON.stringify(message) + "\n";
126
+ }
127
+ function deserializeMessage(message) {
128
+ return new Promise((resolve, reject) => {
129
+ try {
130
+ const parsed = JSON.parse(message);
131
+ resolve(parsed);
132
+ } catch (error) {
133
+ console.error("failed to parse message", typeof message, message, error);
134
+ reject(error);
135
+ }
136
+ });
137
+ }
138
+ function serializeResponse(response) {
139
+ return JSON.stringify(response) + "\n";
140
+ }
141
+ function deserializeResponse(response) {
142
+ return new Promise((resolve, reject) => {
143
+ try {
144
+ const parsed = JSON.parse(response);
145
+ resolve(parsed);
146
+ } catch (error) {
147
+ console.error("failed to parse response", response);
148
+ reject(error);
149
+ }
150
+ });
151
+ }
152
+
153
+ // src/utils.ts
154
+ function generateUUID() {
155
+ return new Array(4).fill(0).map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)).join("-");
156
+ }
157
+
158
+ // src/channel.ts
159
+ var RPCChannel = class {
160
+ constructor(io, options) {
161
+ this.io = io;
162
+ this.apiImplementation = options?.expose;
163
+ this.listen();
164
+ }
165
+ pendingRequests = {};
166
+ callbacks = {};
167
+ callbackCache = /* @__PURE__ */ new Map();
168
+ count = 0;
169
+ messageStr = "";
170
+ apiImplementation;
171
+ expose(api) {
172
+ this.apiImplementation = api;
173
+ }
174
+ getIO() {
175
+ return this.io;
176
+ }
177
+ async listen() {
178
+ while (true) {
179
+ const buffer = await this.io.read();
180
+ if (!buffer) {
181
+ continue;
182
+ }
183
+ const bufferStr = buffer.toString("utf-8");
184
+ if (bufferStr.trim().length === 0) {
185
+ continue;
186
+ }
187
+ this.messageStr += bufferStr;
188
+ const lastChar = this.messageStr[this.messageStr.length - 1];
189
+ const msgsSplit = this.messageStr.split("\n");
190
+ const msgs = lastChar === "\n" ? msgsSplit : msgsSplit.slice(0, -1);
191
+ this.messageStr = lastChar === "\n" ? "" : msgsSplit.at(-1) ?? "";
192
+ for (const msgStr of msgs.map((msg) => msg.trim()).filter(Boolean)) {
193
+ this.handleMessageStr(msgStr);
194
+ }
195
+ }
196
+ }
197
+ async handleMessageStr(messageStr) {
198
+ this.count++;
199
+ const parsedMessage = await deserializeMessage(messageStr);
200
+ if (parsedMessage.type === "response") {
201
+ this.handleResponse(parsedMessage);
202
+ } else if (parsedMessage.type === "request") {
203
+ this.handleRequest(parsedMessage);
204
+ } else if (parsedMessage.type === "callback") {
205
+ this.handleCallback(parsedMessage);
206
+ } else {
207
+ console.error("received unknown message type", parsedMessage, typeof parsedMessage);
208
+ }
209
+ }
210
+ // Send a method call to the other process
211
+ callMethod(method, args) {
212
+ return new Promise((resolve, reject) => {
213
+ const messageId = generateUUID();
214
+ this.pendingRequests[messageId] = { resolve, reject };
215
+ const callbackIds = [];
216
+ const processedArgs = args.map((arg) => {
217
+ if (typeof arg === "function") {
218
+ let callbackId = this.callbackCache.get(arg);
219
+ if (!callbackId) {
220
+ callbackId = generateUUID();
221
+ this.callbacks[callbackId] = arg;
222
+ this.callbackCache.set(arg, callbackId);
223
+ } else {
224
+ }
225
+ callbackIds.push(callbackId);
226
+ return `__callback__${callbackId}`;
227
+ }
228
+ return arg;
229
+ });
230
+ const message = {
231
+ id: messageId,
232
+ method,
233
+ args: processedArgs,
234
+ type: "request",
235
+ callbackIds: callbackIds.length > 0 ? callbackIds : void 0
236
+ };
237
+ this.io.write(serializeMessage(message));
238
+ });
239
+ }
240
+ // Handle response to a request we sent
241
+ handleResponse(response) {
242
+ const { id } = response;
243
+ const { result, error } = response.args;
244
+ if (this.pendingRequests[id]) {
245
+ if (error) {
246
+ this.pendingRequests[id].reject(new Error(error));
247
+ } else {
248
+ this.pendingRequests[id].resolve(result);
249
+ }
250
+ delete this.pendingRequests[id];
251
+ }
252
+ }
253
+ // Handle incoming requests from the other process using a Proxy
254
+ handleRequest(request) {
255
+ const { id, method, args } = request;
256
+ const methodPath = method.split(".");
257
+ if (!this.apiImplementation) return;
258
+ let target = this.apiImplementation;
259
+ for (let i = 0; i < methodPath.length - 1; i++) {
260
+ target = target[methodPath[i]];
261
+ if (!target) {
262
+ this.sendError(id, `Method path ${method} not found at ${methodPath[i]}`);
263
+ return;
264
+ }
265
+ }
266
+ const finalMethod = methodPath[methodPath.length - 1];
267
+ const targetMethod = target[finalMethod];
268
+ if (typeof targetMethod !== "function") {
269
+ this.sendError(id, `Method ${method} is not a function`);
270
+ return;
271
+ }
272
+ const processedArgs = args.map((arg) => {
273
+ if (typeof arg === "string" && arg.startsWith("__callback__")) {
274
+ const callbackId = arg.slice(12);
275
+ return (...callbackArgs) => {
276
+ this.invokeCallback(callbackId, callbackArgs);
277
+ };
278
+ }
279
+ return arg;
280
+ });
281
+ try {
282
+ const result = targetMethod.apply(target, processedArgs);
283
+ Promise.resolve(result).then((res) => {
284
+ return this.sendResponse(id, res);
285
+ }).catch((err) => this.sendError(id, err.message));
286
+ } catch (error) {
287
+ this.sendError(id, error.message ?? error.toString());
288
+ }
289
+ }
290
+ invokeCallback(callbackId, args) {
291
+ const message = {
292
+ id: generateUUID(),
293
+ method: callbackId,
294
+ args,
295
+ type: "callback"
296
+ };
297
+ this.io.write(serializeMessage(message));
298
+ }
299
+ handleCallback(message) {
300
+ const { method: callbackId, args } = message;
301
+ const callback = this.callbacks[callbackId];
302
+ if (callback) {
303
+ callback(...args);
304
+ } else {
305
+ console.error(`Callback with id ${callbackId} not found`);
306
+ }
307
+ }
308
+ // Send a response to a request
309
+ sendResponse(id, result) {
310
+ const response = {
311
+ id,
312
+ method: "",
313
+ args: { result },
314
+ type: "response"
315
+ };
316
+ this.io.write(serializeMessage(response));
317
+ }
318
+ // Send an error response
319
+ sendError(id, error) {
320
+ const response = {
321
+ id,
322
+ method: "",
323
+ args: { error },
324
+ type: "response"
325
+ };
326
+ this.io.write(serializeMessage(response));
327
+ }
328
+ createNestedProxy(chain = []) {
329
+ return new Proxy(() => {
330
+ }, {
331
+ get: (_target, prop) => {
332
+ if (typeof prop === "string" && prop !== "then") {
333
+ return this.createNestedProxy([...chain, prop]);
334
+ }
335
+ return void 0;
336
+ },
337
+ apply: (_target, _thisArg, args) => {
338
+ const method = chain.join(".");
339
+ return this.callMethod(method, args);
340
+ }
341
+ });
342
+ }
343
+ getAPI() {
344
+ return this.createNestedProxy();
345
+ }
346
+ /**
347
+ * Free up the callback map and cache
348
+ * If you use callbacks a lot, you could get memory leak.
349
+ * e.g. If you use anonymous callback function in a 5000 iterations loop,
350
+ * you will get 5000 callbacks in cache. It's a better idea to free them.
351
+ *
352
+ * If you use a named callback function, there will be only one entry in the cache.
353
+ */
354
+ freeCallbacks() {
355
+ this.callbacks = {};
356
+ this.callbackCache.clear();
357
+ }
358
+ };
359
+
360
+ // src/adapters/iframe.ts
361
+ var DESTROY_SIGNAL2 = "__DESTROY__";
362
+ var PORT_INIT_SIGNAL = "__PORT_INIT__";
363
+ var IframeParentIO = class {
364
+ /**
365
+ * @example
366
+ * ```ts
367
+ * const io = new IframeParentIO(iframeRef.contentWindow);
368
+ * const rpc = new RPCChannel(io, {
369
+ * expose: {
370
+ * add: (a: number, b: number) => Promise.resolve(a + b),
371
+ * },
372
+ * });
373
+ * ```
374
+ */
375
+ constructor(targetWindow) {
376
+ this.targetWindow = targetWindow;
377
+ this.port = null;
378
+ window.addEventListener("message", (event) => {
379
+ if (event.source !== this.targetWindow) return;
380
+ if (event.data === PORT_INIT_SIGNAL && event.ports.length > 0) {
381
+ this.port = event.ports[0];
382
+ this.port.onmessage = this.handleMessage;
383
+ while (this.messageQueue.length > 0) {
384
+ const message = this.messageQueue.shift();
385
+ if (message) this.port.postMessage(message);
386
+ }
387
+ }
388
+ });
389
+ }
390
+ name = "iframe-parent-io";
391
+ messageQueue = [];
392
+ resolveRead = null;
393
+ port = null;
394
+ handleMessage = (event) => {
395
+ const message = event.data;
396
+ if (message === DESTROY_SIGNAL2) {
397
+ this.destroy();
398
+ return;
399
+ }
400
+ if (this.resolveRead) {
401
+ this.resolveRead(message);
402
+ this.resolveRead = null;
403
+ } else {
404
+ this.messageQueue.push(message);
405
+ }
406
+ };
407
+ async read() {
408
+ if (this.messageQueue.length > 0) {
409
+ return this.messageQueue.shift() ?? null;
410
+ }
411
+ return new Promise((resolve) => {
412
+ this.resolveRead = resolve;
413
+ });
414
+ }
415
+ async write(data) {
416
+ if (!this.port) {
417
+ this.messageQueue.push(data);
418
+ return;
419
+ }
420
+ this.port.postMessage(data);
421
+ }
422
+ destroy() {
423
+ if (this.port) {
424
+ this.port.postMessage(DESTROY_SIGNAL2);
425
+ this.port.close();
426
+ }
427
+ }
428
+ signalDestroy() {
429
+ if (this.port) {
430
+ this.port.postMessage(DESTROY_SIGNAL2);
431
+ }
432
+ }
433
+ };
434
+ var IframeChildIO = class {
435
+ name = "iframe-child-io";
436
+ messageQueue = [];
437
+ resolveRead = null;
438
+ port = null;
439
+ pendingMessages = [];
440
+ initialized;
441
+ channel;
442
+ constructor() {
443
+ this.channel = new MessageChannel();
444
+ this.port = this.channel.port1;
445
+ this.port.onmessage = this.handleMessage;
446
+ window.parent.postMessage(PORT_INIT_SIGNAL, "*", [this.channel.port2]);
447
+ this.initialized = Promise.resolve();
448
+ }
449
+ handleMessage = (event) => {
450
+ const message = event.data;
451
+ if (message === DESTROY_SIGNAL2) {
452
+ this.destroy();
453
+ return;
454
+ }
455
+ if (this.resolveRead) {
456
+ this.resolveRead(message);
457
+ this.resolveRead = null;
458
+ } else {
459
+ this.messageQueue.push(message);
460
+ }
461
+ };
462
+ async read() {
463
+ await this.initialized;
464
+ if (this.messageQueue.length > 0) {
465
+ return this.messageQueue.shift() ?? null;
466
+ }
467
+ return new Promise((resolve) => {
468
+ this.resolveRead = resolve;
469
+ });
470
+ }
471
+ async write(data) {
472
+ await this.initialized;
473
+ if (this.port) {
474
+ this.port.postMessage(data);
475
+ } else {
476
+ this.pendingMessages.push(data);
477
+ }
478
+ }
479
+ destroy() {
480
+ if (this.port) {
481
+ this.port.postMessage(DESTROY_SIGNAL2);
482
+ this.port.close();
483
+ }
484
+ }
485
+ signalDestroy() {
486
+ if (this.port) {
487
+ this.port.postMessage(DESTROY_SIGNAL2);
488
+ } else {
489
+ this.pendingMessages.push(DESTROY_SIGNAL2);
490
+ }
491
+ }
492
+ };
493
+ // Annotate the CommonJS export names for ESM import in node:
494
+ 0 && (module.exports = {
495
+ IframeChildIO,
496
+ IframeParentIO,
497
+ RPCChannel,
498
+ WorkerChildIO,
499
+ WorkerParentIO,
500
+ deserializeMessage,
501
+ deserializeResponse,
502
+ generateUUID,
503
+ serializeMessage,
504
+ serializeResponse
505
+ });
@@ -1,8 +1,13 @@
1
+ export { M as Message, R as Response, a as WorkerChildIO, W as WorkerParentIO, d as deserializeMessage, c as deserializeResponse, g as generateUUID, s as serializeMessage, b as serializeResponse } from './serialization-BrfKixNU.cjs';
2
+ import { D as DestroyableIoInterface } from './channel-CGr_xSbe.cjs';
3
+ export { I as IoInterface, R as RPCChannel } from './channel-CGr_xSbe.cjs';
4
+ import 'node:buffer';
5
+
1
6
  /**
2
7
  * This file contains the implementation of the IframeParentIO and IframeChildIO classes.
3
8
  * They are used to create a bidirectional communication channel between a parent window and a child iframe.
4
9
  */
5
- import type { DestroyableIoInterface } from "../interface.ts";
10
+
6
11
  /**
7
12
  * This design relies on built-in `MessageChannel`, and requires a pairing process to establish the port.
8
13
  * The `PORT_INIT_SIGNAL` is designed to be initiated by the child frame, parent window will wait for the signal and establish the port.
@@ -15,7 +20,7 @@ import type { DestroyableIoInterface } from "../interface.ts";
15
20
  *
16
21
  * It's a good practice to call `destroy()` on either side of the channel to close `MessageChannel` and release resources.
17
22
  */
18
- export declare class IframeParentIO implements DestroyableIoInterface {
23
+ declare class IframeParentIO implements DestroyableIoInterface {
19
24
  private targetWindow;
20
25
  name: string;
21
26
  private messageQueue;
@@ -39,7 +44,7 @@ export declare class IframeParentIO implements DestroyableIoInterface {
39
44
  destroy(): void;
40
45
  signalDestroy(): void;
41
46
  }
42
- export declare class IframeChildIO implements DestroyableIoInterface {
47
+ declare class IframeChildIO implements DestroyableIoInterface {
43
48
  name: string;
44
49
  private messageQueue;
45
50
  private resolveRead;
@@ -54,4 +59,5 @@ export declare class IframeChildIO implements DestroyableIoInterface {
54
59
  destroy(): void;
55
60
  signalDestroy(): void;
56
61
  }
57
- //# sourceMappingURL=iframe.d.ts.map
62
+
63
+ export { DestroyableIoInterface, IframeChildIO, IframeParentIO };
@@ -1,7 +1,63 @@
1
- export * from "./src/adapters/worker.ts";
2
- export * from "./src/interface.ts";
3
- export * from "./src/channel.ts";
4
- export * from "./src/utils.ts";
5
- export * from "./src/serialization.ts";
6
- export * from "./src/adapters/iframe.ts";
7
- //# sourceMappingURL=browser-mod.d.ts.map
1
+ export { M as Message, R as Response, a as WorkerChildIO, W as WorkerParentIO, d as deserializeMessage, c as deserializeResponse, g as generateUUID, s as serializeMessage, b as serializeResponse } from './serialization-C0yxGsSr.js';
2
+ import { D as DestroyableIoInterface } from './channel-CGr_xSbe.js';
3
+ export { I as IoInterface, R as RPCChannel } from './channel-CGr_xSbe.js';
4
+ import 'node:buffer';
5
+
6
+ /**
7
+ * This file contains the implementation of the IframeParentIO and IframeChildIO classes.
8
+ * They are used to create a bidirectional communication channel between a parent window and a child iframe.
9
+ */
10
+
11
+ /**
12
+ * This design relies on built-in `MessageChannel`, and requires a pairing process to establish the port.
13
+ * The `PORT_INIT_SIGNAL` is designed to be initiated by the child frame, parent window will wait for the signal and establish the port.
14
+ *
15
+ * If `PORT_INIT_SIGNAL` is started by the parent window, there has to be a delay (with `setTimeout`) to wait for the child frame to listen to the signal.
16
+ * Parent window can easily listen to iframe onload event, but there is no way to know when child JS is ready to listen to the message without
17
+ * letting child `postMessage` a signal first.
18
+ *
19
+ * It's much easier to make sure parent window is ready (listening) before iframe is loaded, so `MessageChannel` is designed to be created from iframe's side.
20
+ *
21
+ * It's a good practice to call `destroy()` on either side of the channel to close `MessageChannel` and release resources.
22
+ */
23
+ declare class IframeParentIO implements DestroyableIoInterface {
24
+ private targetWindow;
25
+ name: string;
26
+ private messageQueue;
27
+ private resolveRead;
28
+ private port;
29
+ /**
30
+ * @example
31
+ * ```ts
32
+ * const io = new IframeParentIO(iframeRef.contentWindow);
33
+ * const rpc = new RPCChannel(io, {
34
+ * expose: {
35
+ * add: (a: number, b: number) => Promise.resolve(a + b),
36
+ * },
37
+ * });
38
+ * ```
39
+ */
40
+ constructor(targetWindow: Window);
41
+ private handleMessage;
42
+ read(): Promise<string | null>;
43
+ write(data: string): Promise<void>;
44
+ destroy(): void;
45
+ signalDestroy(): void;
46
+ }
47
+ declare class IframeChildIO implements DestroyableIoInterface {
48
+ name: string;
49
+ private messageQueue;
50
+ private resolveRead;
51
+ private port;
52
+ private pendingMessages;
53
+ private initialized;
54
+ private channel;
55
+ constructor();
56
+ private handleMessage;
57
+ read(): Promise<string | null>;
58
+ write(data: string): Promise<void>;
59
+ destroy(): void;
60
+ signalDestroy(): void;
61
+ }
62
+
63
+ export { DestroyableIoInterface, IframeChildIO, IframeParentIO };