@bigmistqke/rpc 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,540 @@
1
+ // ../../node_modules/.pnpm/valibot@1.0.0_typescript@5.7.2/node_modules/valibot/dist/index.js
2
+ var store;
3
+ function getGlobalConfig(config2) {
4
+ return {
5
+ lang: config2?.lang ?? store?.lang,
6
+ message: config2?.message,
7
+ abortEarly: config2?.abortEarly ?? store?.abortEarly,
8
+ abortPipeEarly: config2?.abortPipeEarly ?? store?.abortPipeEarly
9
+ };
10
+ }
11
+ var store2;
12
+ function getGlobalMessage(lang) {
13
+ return store2?.get(lang);
14
+ }
15
+ var store3;
16
+ function getSchemaMessage(lang) {
17
+ return store3?.get(lang);
18
+ }
19
+ var store4;
20
+ function getSpecificMessage(reference, lang) {
21
+ return store4?.get(reference)?.get(lang);
22
+ }
23
+ function _stringify(input) {
24
+ const type = typeof input;
25
+ if (type === "string") {
26
+ return `"${input}"`;
27
+ }
28
+ if (type === "number" || type === "bigint" || type === "boolean") {
29
+ return `${input}`;
30
+ }
31
+ if (type === "object" || type === "function") {
32
+ return (input && Object.getPrototypeOf(input)?.constructor?.name) ?? "null";
33
+ }
34
+ return type;
35
+ }
36
+ function _addIssue(context, label, dataset, config2, other) {
37
+ const input = other && "input" in other ? other.input : dataset.value;
38
+ const expected = other?.expected ?? context.expects ?? null;
39
+ const received = other?.received ?? _stringify(input);
40
+ const issue = {
41
+ kind: context.kind,
42
+ type: context.type,
43
+ input,
44
+ expected,
45
+ received,
46
+ message: `Invalid ${label}: ${expected ? `Expected ${expected} but r` : "R"}eceived ${received}`,
47
+ requirement: context.requirement,
48
+ path: other?.path,
49
+ issues: other?.issues,
50
+ lang: config2.lang,
51
+ abortEarly: config2.abortEarly,
52
+ abortPipeEarly: config2.abortPipeEarly
53
+ };
54
+ const isSchema = context.kind === "schema";
55
+ const message = other?.message ?? context.message ?? getSpecificMessage(context.reference, issue.lang) ?? (isSchema ? getSchemaMessage(issue.lang) : null) ?? config2.message ?? getGlobalMessage(issue.lang);
56
+ if (message !== void 0) {
57
+ issue.message = typeof message === "function" ? (
58
+ // @ts-expect-error
59
+ message(issue)
60
+ ) : message;
61
+ }
62
+ if (isSchema) {
63
+ dataset.typed = false;
64
+ }
65
+ if (dataset.issues) {
66
+ dataset.issues.push(issue);
67
+ } else {
68
+ dataset.issues = [issue];
69
+ }
70
+ }
71
+ function _getStandardProps(context) {
72
+ return {
73
+ version: 1,
74
+ vendor: "valibot",
75
+ validate(value2) {
76
+ return context["~run"]({ value: value2 }, getGlobalConfig());
77
+ }
78
+ };
79
+ }
80
+ function getFallback(schema, dataset, config2) {
81
+ return typeof schema.fallback === "function" ? (
82
+ // @ts-expect-error
83
+ schema.fallback(dataset, config2)
84
+ ) : (
85
+ // @ts-expect-error
86
+ schema.fallback
87
+ );
88
+ }
89
+ function getDefault(schema, dataset, config2) {
90
+ return typeof schema.default === "function" ? (
91
+ // @ts-expect-error
92
+ schema.default(dataset, config2)
93
+ ) : (
94
+ // @ts-expect-error
95
+ schema.default
96
+ );
97
+ }
98
+ function any() {
99
+ return {
100
+ kind: "schema",
101
+ type: "any",
102
+ reference: any,
103
+ expects: "any",
104
+ async: false,
105
+ get "~standard"() {
106
+ return _getStandardProps(this);
107
+ },
108
+ "~run"(dataset) {
109
+ dataset.typed = true;
110
+ return dataset;
111
+ }
112
+ };
113
+ }
114
+ function array(item, message) {
115
+ return {
116
+ kind: "schema",
117
+ type: "array",
118
+ reference: array,
119
+ expects: "Array",
120
+ async: false,
121
+ item,
122
+ message,
123
+ get "~standard"() {
124
+ return _getStandardProps(this);
125
+ },
126
+ "~run"(dataset, config2) {
127
+ const input = dataset.value;
128
+ if (Array.isArray(input)) {
129
+ dataset.typed = true;
130
+ dataset.value = [];
131
+ for (let key = 0; key < input.length; key++) {
132
+ const value2 = input[key];
133
+ const itemDataset = this.item["~run"]({ value: value2 }, config2);
134
+ if (itemDataset.issues) {
135
+ const pathItem = {
136
+ type: "array",
137
+ origin: "value",
138
+ input,
139
+ key,
140
+ value: value2
141
+ };
142
+ for (const issue of itemDataset.issues) {
143
+ if (issue.path) {
144
+ issue.path.unshift(pathItem);
145
+ } else {
146
+ issue.path = [pathItem];
147
+ }
148
+ dataset.issues?.push(issue);
149
+ }
150
+ if (!dataset.issues) {
151
+ dataset.issues = itemDataset.issues;
152
+ }
153
+ if (config2.abortEarly) {
154
+ dataset.typed = false;
155
+ break;
156
+ }
157
+ }
158
+ if (!itemDataset.typed) {
159
+ dataset.typed = false;
160
+ }
161
+ dataset.value.push(itemDataset.value);
162
+ }
163
+ } else {
164
+ _addIssue(this, "type", dataset, config2);
165
+ }
166
+ return dataset;
167
+ }
168
+ };
169
+ }
170
+ function boolean(message) {
171
+ return {
172
+ kind: "schema",
173
+ type: "boolean",
174
+ reference: boolean,
175
+ expects: "boolean",
176
+ async: false,
177
+ message,
178
+ get "~standard"() {
179
+ return _getStandardProps(this);
180
+ },
181
+ "~run"(dataset, config2) {
182
+ if (typeof dataset.value === "boolean") {
183
+ dataset.typed = true;
184
+ } else {
185
+ _addIssue(this, "type", dataset, config2);
186
+ }
187
+ return dataset;
188
+ }
189
+ };
190
+ }
191
+ function number(message) {
192
+ return {
193
+ kind: "schema",
194
+ type: "number",
195
+ reference: number,
196
+ expects: "number",
197
+ async: false,
198
+ message,
199
+ get "~standard"() {
200
+ return _getStandardProps(this);
201
+ },
202
+ "~run"(dataset, config2) {
203
+ if (typeof dataset.value === "number" && !isNaN(dataset.value)) {
204
+ dataset.typed = true;
205
+ } else {
206
+ _addIssue(this, "type", dataset, config2);
207
+ }
208
+ return dataset;
209
+ }
210
+ };
211
+ }
212
+ function object(entries, message) {
213
+ return {
214
+ kind: "schema",
215
+ type: "object",
216
+ reference: object,
217
+ expects: "Object",
218
+ async: false,
219
+ entries,
220
+ message,
221
+ get "~standard"() {
222
+ return _getStandardProps(this);
223
+ },
224
+ "~run"(dataset, config2) {
225
+ const input = dataset.value;
226
+ if (input && typeof input === "object") {
227
+ dataset.typed = true;
228
+ dataset.value = {};
229
+ for (const key in this.entries) {
230
+ const valueSchema = this.entries[key];
231
+ if (key in input || (valueSchema.type === "exact_optional" || valueSchema.type === "optional" || valueSchema.type === "nullish") && // @ts-expect-error
232
+ valueSchema.default !== void 0) {
233
+ const value2 = key in input ? (
234
+ // @ts-expect-error
235
+ input[key]
236
+ ) : getDefault(valueSchema);
237
+ const valueDataset = valueSchema["~run"]({ value: value2 }, config2);
238
+ if (valueDataset.issues) {
239
+ const pathItem = {
240
+ type: "object",
241
+ origin: "value",
242
+ input,
243
+ key,
244
+ value: value2
245
+ };
246
+ for (const issue of valueDataset.issues) {
247
+ if (issue.path) {
248
+ issue.path.unshift(pathItem);
249
+ } else {
250
+ issue.path = [pathItem];
251
+ }
252
+ dataset.issues?.push(issue);
253
+ }
254
+ if (!dataset.issues) {
255
+ dataset.issues = valueDataset.issues;
256
+ }
257
+ if (config2.abortEarly) {
258
+ dataset.typed = false;
259
+ break;
260
+ }
261
+ }
262
+ if (!valueDataset.typed) {
263
+ dataset.typed = false;
264
+ }
265
+ dataset.value[key] = valueDataset.value;
266
+ } else if (valueSchema.fallback !== void 0) {
267
+ dataset.value[key] = getFallback(valueSchema);
268
+ } else if (valueSchema.type !== "exact_optional" && valueSchema.type !== "optional" && valueSchema.type !== "nullish") {
269
+ _addIssue(this, "key", dataset, config2, {
270
+ input: void 0,
271
+ expected: `"${key}"`,
272
+ path: [
273
+ {
274
+ type: "object",
275
+ origin: "key",
276
+ input,
277
+ key,
278
+ // @ts-expect-error
279
+ value: input[key]
280
+ }
281
+ ]
282
+ });
283
+ if (config2.abortEarly) {
284
+ break;
285
+ }
286
+ }
287
+ }
288
+ } else {
289
+ _addIssue(this, "type", dataset, config2);
290
+ }
291
+ return dataset;
292
+ }
293
+ };
294
+ }
295
+ function string(message) {
296
+ return {
297
+ kind: "schema",
298
+ type: "string",
299
+ reference: string,
300
+ expects: "string",
301
+ async: false,
302
+ message,
303
+ get "~standard"() {
304
+ return _getStandardProps(this);
305
+ },
306
+ "~run"(dataset, config2) {
307
+ if (typeof dataset.value === "string") {
308
+ dataset.typed = true;
309
+ } else {
310
+ _addIssue(this, "type", dataset, config2);
311
+ }
312
+ return dataset;
313
+ }
314
+ };
315
+ }
316
+ function unknown() {
317
+ return {
318
+ kind: "schema",
319
+ type: "unknown",
320
+ reference: unknown,
321
+ expects: "unknown",
322
+ async: false,
323
+ get "~standard"() {
324
+ return _getStandardProps(this);
325
+ },
326
+ "~run"(dataset) {
327
+ dataset.typed = true;
328
+ return dataset;
329
+ }
330
+ };
331
+ }
332
+ function safeParse(schema, input, config2) {
333
+ const dataset = schema["~run"]({ value: input }, getGlobalConfig(config2));
334
+ return {
335
+ typed: dataset.typed,
336
+ success: !dataset.issues,
337
+ output: dataset.value,
338
+ issues: dataset.issues
339
+ };
340
+ }
341
+
342
+ // src/utils.ts
343
+ function createIdAllocator() {
344
+ const freeIds = new Array();
345
+ let id = 0;
346
+ return {
347
+ create() {
348
+ if (freeIds.length) {
349
+ return freeIds.pop();
350
+ }
351
+ return id++;
352
+ },
353
+ free(id2) {
354
+ freeIds.push(id2);
355
+ }
356
+ };
357
+ }
358
+ function createIdRegistry() {
359
+ const map = /* @__PURE__ */ new Map();
360
+ const idFactory = createIdAllocator();
361
+ return {
362
+ register(value) {
363
+ const id = idFactory.create();
364
+ map.set(id, value);
365
+ return id;
366
+ },
367
+ free(id) {
368
+ idFactory.free(id);
369
+ return map.get(id);
370
+ }
371
+ };
372
+ }
373
+ function defer() {
374
+ let resolve = null;
375
+ let reject = null;
376
+ return {
377
+ promise: new Promise((_resolve, _reject) => (resolve = _resolve, reject = _reject)),
378
+ resolve,
379
+ reject
380
+ };
381
+ }
382
+ function createCommander(apply) {
383
+ function _createCommander(topics, apply2) {
384
+ return new Proxy(function() {
385
+ }, {
386
+ get(_, topic) {
387
+ if (typeof topic === "symbol")
388
+ return void 0;
389
+ return _createCommander([...topics, topic], apply2);
390
+ },
391
+ apply(_, __, args) {
392
+ return apply2(topics, args);
393
+ }
394
+ });
395
+ }
396
+ return _createCommander([], apply);
397
+ }
398
+ function createShape(schema, create) {
399
+ return {
400
+ validate: (value) => safeParse(schema, value).success,
401
+ create
402
+ };
403
+ }
404
+ function callMethod(methods, topics, args) {
405
+ const method = topics.reduce((acc, topic) => {
406
+ const result = acc?.[topic];
407
+ return result;
408
+ }, methods);
409
+ if (typeof method !== "function") {
410
+ throw new Error(`Topics did not resolve to a function: [${topics.join(",")}]`);
411
+ }
412
+ return method(...args);
413
+ }
414
+
415
+ // src/message-protocol.ts
416
+ var $MESSENGER_REQUEST = "RPC_PROXY_REQUEST";
417
+ var requestSchema = object({
418
+ [$MESSENGER_REQUEST]: number(),
419
+ payload: unknown()
420
+ });
421
+ var RequestShape = createShape(requestSchema, (id, payload) => ({
422
+ [$MESSENGER_REQUEST]: id,
423
+ payload
424
+ }));
425
+ var $MESSENGER_RESPONSE = "RPC_PROXY_RESPONSE";
426
+ var ResponseShape = createShape(
427
+ object({
428
+ [$MESSENGER_RESPONSE]: number(),
429
+ payload: unknown()
430
+ }),
431
+ (request, payload) => ({
432
+ [$MESSENGER_RESPONSE]: request[$MESSENGER_REQUEST],
433
+ payload
434
+ })
435
+ );
436
+ var $MESSENGER_ERROR = "RPC_PROXY_ERROR";
437
+ var ErrorShape = createShape(
438
+ object({
439
+ [$MESSENGER_ERROR]: number(),
440
+ error: unknown()
441
+ }),
442
+ (data, error) => ({
443
+ [$MESSENGER_ERROR]: data[$MESSENGER_REQUEST],
444
+ error
445
+ })
446
+ );
447
+ var $MESSENGER_RPC_REQUEST = "RPC_PROXY_RPC_REQUEST";
448
+ var RPCPayloadShape = createShape(
449
+ object({
450
+ [$MESSENGER_RPC_REQUEST]: boolean(),
451
+ topics: array(string()),
452
+ args: array(any())
453
+ }),
454
+ (topics, args) => ({ [$MESSENGER_RPC_REQUEST]: true, topics, args })
455
+ );
456
+
457
+ // src/messenger.ts
458
+ var $TRANSFER = "WORKER-TRANSFER";
459
+ function isWindowProxy(target) {
460
+ return typeof target === "object" && typeof target?.postMessage === "function" && typeof target?.closed === "boolean";
461
+ }
462
+ function usePostMessage(messenger) {
463
+ if (isWindowProxy(messenger)) {
464
+ return (message, transferables) => messenger.postMessage(message, "*", transferables);
465
+ } else {
466
+ return (message, transferables) => messenger.postMessage(message, transferables);
467
+ }
468
+ }
469
+ function createRequester(messenger, options = {}) {
470
+ const promiseRegistry = createIdRegistry();
471
+ const postMessage = usePostMessage(messenger);
472
+ messenger.addEventListener(
473
+ "message",
474
+ (event) => {
475
+ const data = event.data;
476
+ if (ErrorShape.validate(data)) {
477
+ promiseRegistry.free(data[$MESSENGER_ERROR])?.reject(data.error);
478
+ } else if (ResponseShape.validate(data)) {
479
+ promiseRegistry.free(data[$MESSENGER_RESPONSE])?.resolve(data.payload);
480
+ }
481
+ },
482
+ options
483
+ );
484
+ if ("start" in messenger) {
485
+ messenger.start?.();
486
+ }
487
+ return (payload, transferables) => {
488
+ const { promise, resolve, reject } = defer();
489
+ const id = promiseRegistry.register({ resolve, reject });
490
+ postMessage(RequestShape.create(id, payload), transferables);
491
+ return promise;
492
+ };
493
+ }
494
+ function createResponder(messenger, callback, options = {}) {
495
+ const postMessage = usePostMessage(messenger);
496
+ messenger.addEventListener(
497
+ "message",
498
+ (event) => {
499
+ const data = event.data;
500
+ if (RequestShape.validate(data)) {
501
+ try {
502
+ postMessage(ResponseShape.create(data, callback(data)));
503
+ } catch (error) {
504
+ postMessage(ErrorShape.create(data, error));
505
+ }
506
+ }
507
+ },
508
+ options
509
+ );
510
+ if ("start" in messenger) {
511
+ messenger.start?.();
512
+ }
513
+ }
514
+ function expose(methods, { to = self, signal } = {}) {
515
+ createResponder(
516
+ to,
517
+ (data) => {
518
+ if (RPCPayloadShape.validate(data.payload)) {
519
+ try {
520
+ const { topics, args } = data.payload;
521
+ return callMethod(methods, topics, args);
522
+ } catch (error) {
523
+ console.error("Error while processing rpc request:", error, data.payload, methods);
524
+ }
525
+ }
526
+ },
527
+ { signal }
528
+ );
529
+ }
530
+ function rpc(messenger, options) {
531
+ const request = createRequester(messenger, options);
532
+ return createCommander((topics, args) => request(RPCPayloadShape.create(topics, args)));
533
+ }
534
+ export {
535
+ $TRANSFER,
536
+ createResponder,
537
+ expose,
538
+ rpc
539
+ };
540
+ //# sourceMappingURL=messenger.js.map