@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.
- package/LICENSE +21 -0
- package/dist/fetch-node.d.ts +11 -0
- package/dist/fetch-node.js +342 -0
- package/dist/fetch-node.js.map +1 -0
- package/dist/fetch.d.ts +36 -0
- package/dist/fetch.js +369 -0
- package/dist/fetch.js.map +1 -0
- package/dist/messenger.d.ts +50 -0
- package/dist/messenger.js +540 -0
- package/dist/messenger.js.map +1 -0
- package/dist/stream.d.ts +46 -0
- package/dist/stream.js +601 -0
- package/dist/stream.js.map +1 -0
- package/dist/types-4d4495dd.d.ts +40 -0
- package/package.json +42 -0
- package/src/fetch/index.ts +84 -0
- package/src/fetch/node.ts +44 -0
- package/src/message-protocol.ts +57 -0
- package/src/messenger.ts +176 -0
- package/src/server-send-events/index.ts +129 -0
- package/src/stream/encoding.ts +362 -0
- package/src/stream/index.ts +162 -0
- package/src/types.ts +104 -0
- package/src/utils.ts +159 -0
- package/test/encoding.test.ts +413 -0
- package/test/fetch.test.ts +310 -0
- package/test/message-protocol.test.ts +166 -0
- package/test/messenger.test.ts +316 -0
- package/test/sse.test.ts +356 -0
- package/test/stream.test.ts +351 -0
- package/test/utils.test.ts +336 -0
- package/tsconfig.json +23 -0
- package/tsup.config.ts +17 -0
package/dist/fetch.js
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
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 object(entries, message) {
|
|
171
|
+
return {
|
|
172
|
+
kind: "schema",
|
|
173
|
+
type: "object",
|
|
174
|
+
reference: object,
|
|
175
|
+
expects: "Object",
|
|
176
|
+
async: false,
|
|
177
|
+
entries,
|
|
178
|
+
message,
|
|
179
|
+
get "~standard"() {
|
|
180
|
+
return _getStandardProps(this);
|
|
181
|
+
},
|
|
182
|
+
"~run"(dataset, config2) {
|
|
183
|
+
const input = dataset.value;
|
|
184
|
+
if (input && typeof input === "object") {
|
|
185
|
+
dataset.typed = true;
|
|
186
|
+
dataset.value = {};
|
|
187
|
+
for (const key in this.entries) {
|
|
188
|
+
const valueSchema = this.entries[key];
|
|
189
|
+
if (key in input || (valueSchema.type === "exact_optional" || valueSchema.type === "optional" || valueSchema.type === "nullish") && // @ts-expect-error
|
|
190
|
+
valueSchema.default !== void 0) {
|
|
191
|
+
const value2 = key in input ? (
|
|
192
|
+
// @ts-expect-error
|
|
193
|
+
input[key]
|
|
194
|
+
) : getDefault(valueSchema);
|
|
195
|
+
const valueDataset = valueSchema["~run"]({ value: value2 }, config2);
|
|
196
|
+
if (valueDataset.issues) {
|
|
197
|
+
const pathItem = {
|
|
198
|
+
type: "object",
|
|
199
|
+
origin: "value",
|
|
200
|
+
input,
|
|
201
|
+
key,
|
|
202
|
+
value: value2
|
|
203
|
+
};
|
|
204
|
+
for (const issue of valueDataset.issues) {
|
|
205
|
+
if (issue.path) {
|
|
206
|
+
issue.path.unshift(pathItem);
|
|
207
|
+
} else {
|
|
208
|
+
issue.path = [pathItem];
|
|
209
|
+
}
|
|
210
|
+
dataset.issues?.push(issue);
|
|
211
|
+
}
|
|
212
|
+
if (!dataset.issues) {
|
|
213
|
+
dataset.issues = valueDataset.issues;
|
|
214
|
+
}
|
|
215
|
+
if (config2.abortEarly) {
|
|
216
|
+
dataset.typed = false;
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (!valueDataset.typed) {
|
|
221
|
+
dataset.typed = false;
|
|
222
|
+
}
|
|
223
|
+
dataset.value[key] = valueDataset.value;
|
|
224
|
+
} else if (valueSchema.fallback !== void 0) {
|
|
225
|
+
dataset.value[key] = getFallback(valueSchema);
|
|
226
|
+
} else if (valueSchema.type !== "exact_optional" && valueSchema.type !== "optional" && valueSchema.type !== "nullish") {
|
|
227
|
+
_addIssue(this, "key", dataset, config2, {
|
|
228
|
+
input: void 0,
|
|
229
|
+
expected: `"${key}"`,
|
|
230
|
+
path: [
|
|
231
|
+
{
|
|
232
|
+
type: "object",
|
|
233
|
+
origin: "key",
|
|
234
|
+
input,
|
|
235
|
+
key,
|
|
236
|
+
// @ts-expect-error
|
|
237
|
+
value: input[key]
|
|
238
|
+
}
|
|
239
|
+
]
|
|
240
|
+
});
|
|
241
|
+
if (config2.abortEarly) {
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
_addIssue(this, "type", dataset, config2);
|
|
248
|
+
}
|
|
249
|
+
return dataset;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function string(message) {
|
|
254
|
+
return {
|
|
255
|
+
kind: "schema",
|
|
256
|
+
type: "string",
|
|
257
|
+
reference: string,
|
|
258
|
+
expects: "string",
|
|
259
|
+
async: false,
|
|
260
|
+
message,
|
|
261
|
+
get "~standard"() {
|
|
262
|
+
return _getStandardProps(this);
|
|
263
|
+
},
|
|
264
|
+
"~run"(dataset, config2) {
|
|
265
|
+
if (typeof dataset.value === "string") {
|
|
266
|
+
dataset.typed = true;
|
|
267
|
+
} else {
|
|
268
|
+
_addIssue(this, "type", dataset, config2);
|
|
269
|
+
}
|
|
270
|
+
return dataset;
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function safeParse(schema, input, config2) {
|
|
275
|
+
const dataset = schema["~run"]({ value: input }, getGlobalConfig(config2));
|
|
276
|
+
return {
|
|
277
|
+
typed: dataset.typed,
|
|
278
|
+
success: !dataset.issues,
|
|
279
|
+
output: dataset.value,
|
|
280
|
+
issues: dataset.issues
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// src/utils.ts
|
|
285
|
+
function createCommander(apply) {
|
|
286
|
+
function _createCommander(topics, apply2) {
|
|
287
|
+
return new Proxy(function() {
|
|
288
|
+
}, {
|
|
289
|
+
get(_, topic) {
|
|
290
|
+
if (typeof topic === "symbol")
|
|
291
|
+
return void 0;
|
|
292
|
+
return _createCommander([...topics, topic], apply2);
|
|
293
|
+
},
|
|
294
|
+
apply(_, __, args) {
|
|
295
|
+
return apply2(topics, args);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
return _createCommander([], apply);
|
|
300
|
+
}
|
|
301
|
+
function createShape(schema, create) {
|
|
302
|
+
return {
|
|
303
|
+
validate: (value) => safeParse(schema, value).success,
|
|
304
|
+
create
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
function callMethod(methods, topics, args) {
|
|
308
|
+
const method = topics.reduce((acc, topic) => {
|
|
309
|
+
const result = acc?.[topic];
|
|
310
|
+
return result;
|
|
311
|
+
}, methods);
|
|
312
|
+
if (typeof method !== "function") {
|
|
313
|
+
throw new Error(`Topics did not resolve to a function: [${topics.join(",")}]`);
|
|
314
|
+
}
|
|
315
|
+
return method(...args);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// src/fetch/index.ts
|
|
319
|
+
var $FETCH_HEADER = "RPC_RR_PROXY";
|
|
320
|
+
var isFetchRequest = (event) => event.request.headers.has($FETCH_HEADER);
|
|
321
|
+
var Payload = createShape(
|
|
322
|
+
object({ args: array(any()), topics: array(string()) }),
|
|
323
|
+
(topics, args) => ({ topics, args })
|
|
324
|
+
);
|
|
325
|
+
function expose(methods) {
|
|
326
|
+
return async (event) => {
|
|
327
|
+
try {
|
|
328
|
+
const json = await event.request.json();
|
|
329
|
+
if (!Payload.validate(json)) {
|
|
330
|
+
throw new Error(`Incorrect shape`);
|
|
331
|
+
}
|
|
332
|
+
const { args, topics } = json;
|
|
333
|
+
const payload = await callMethod(methods, topics, args);
|
|
334
|
+
return new Response(JSON.stringify({ payload }), {
|
|
335
|
+
status: 200,
|
|
336
|
+
headers: { "Content-Type": "application/json" }
|
|
337
|
+
});
|
|
338
|
+
} catch (error) {
|
|
339
|
+
return new Response(null, {
|
|
340
|
+
statusText: typeof error === "string" ? error : typeof error === "object" && error && "message" in error && typeof error.message === "string" ? error.message : void 0,
|
|
341
|
+
status: 500,
|
|
342
|
+
headers: { "Content-Type": "application/json" }
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
function rpc(base) {
|
|
348
|
+
return createCommander(async (topics, args) => {
|
|
349
|
+
const result = await fetch(
|
|
350
|
+
new Request(`${base}/${topics.join("/")}`, {
|
|
351
|
+
method: topics.length > 0 ? "POST" : "GET",
|
|
352
|
+
headers: { [$FETCH_HEADER]: "true" },
|
|
353
|
+
body: JSON.stringify(Payload.create(topics, args))
|
|
354
|
+
})
|
|
355
|
+
);
|
|
356
|
+
if (result.status !== 200) {
|
|
357
|
+
throw result.statusText;
|
|
358
|
+
}
|
|
359
|
+
const { payload } = await result.json();
|
|
360
|
+
return payload;
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
export {
|
|
364
|
+
Payload,
|
|
365
|
+
expose,
|
|
366
|
+
isFetchRequest,
|
|
367
|
+
rpc
|
|
368
|
+
};
|
|
369
|
+
//# sourceMappingURL=fetch.js.map
|