@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2021 EGOIST (https://github.com/sponsors/egoist)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ServerResponse, IncomingMessage } from 'http';
|
|
2
|
+
|
|
3
|
+
declare function exposeNode<T extends object>(methods: T, { wrap, unwrap, }?: {
|
|
4
|
+
wrap?: (res: ServerResponse, result: any) => void;
|
|
5
|
+
unwrap?: (req: IncomingMessage) => Promise<{
|
|
6
|
+
topics: string[];
|
|
7
|
+
args: any[];
|
|
8
|
+
}>;
|
|
9
|
+
}): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
10
|
+
|
|
11
|
+
export { exposeNode };
|
|
@@ -0,0 +1,342 @@
|
|
|
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 createShape(schema, create) {
|
|
286
|
+
return {
|
|
287
|
+
validate: (value) => safeParse(schema, value).success,
|
|
288
|
+
create
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
function callMethod(methods, topics, args) {
|
|
292
|
+
const method = topics.reduce((acc, topic) => {
|
|
293
|
+
const result = acc?.[topic];
|
|
294
|
+
return result;
|
|
295
|
+
}, methods);
|
|
296
|
+
if (typeof method !== "function") {
|
|
297
|
+
throw new Error(`Topics did not resolve to a function: [${topics.join(",")}]`);
|
|
298
|
+
}
|
|
299
|
+
return method(...args);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// src/fetch/index.ts
|
|
303
|
+
var Payload = createShape(
|
|
304
|
+
object({ args: array(any()), topics: array(string()) }),
|
|
305
|
+
(topics, args) => ({ topics, args })
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
// src/fetch/node.ts
|
|
309
|
+
function exposeNode(methods, {
|
|
310
|
+
wrap = (res, result) => {
|
|
311
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
312
|
+
res.end(JSON.stringify(result));
|
|
313
|
+
},
|
|
314
|
+
unwrap = async (req) => {
|
|
315
|
+
const body = await new Promise((resolve, reject) => {
|
|
316
|
+
let data = "";
|
|
317
|
+
req.on("data", (chunk) => data += chunk);
|
|
318
|
+
req.on("end", () => resolve(data));
|
|
319
|
+
req.on("error", reject);
|
|
320
|
+
});
|
|
321
|
+
const json = JSON.parse(body);
|
|
322
|
+
if (!Payload.validate(json)) {
|
|
323
|
+
throw new Error(`Incorrect shape`);
|
|
324
|
+
}
|
|
325
|
+
return json;
|
|
326
|
+
}
|
|
327
|
+
} = {}) {
|
|
328
|
+
return async (req, res) => {
|
|
329
|
+
try {
|
|
330
|
+
const { topics, args } = await unwrap(req);
|
|
331
|
+
const result = await callMethod(methods, topics, args);
|
|
332
|
+
wrap(res, result);
|
|
333
|
+
} catch (err) {
|
|
334
|
+
res.writeHead(500);
|
|
335
|
+
res.statusMessage = err?.message;
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
export {
|
|
340
|
+
exposeNode
|
|
341
|
+
};
|
|
342
|
+
//# sourceMappingURL=fetch-node.js.map
|