@tinkered/bun 0.24.0 → 0.25.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/dist/{client-MAqzflMe.mjs → client-BXRgKu3R.mjs} +213 -92
- package/dist/client.d.mts +4 -4
- package/dist/client.mjs +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +19 -19
- package/package.json +2 -2
|
@@ -1,4 +1,89 @@
|
|
|
1
1
|
import { capsule, data, operation, tag, transport } from "@tinkered/core";
|
|
2
|
+
//#region src/codec.ts
|
|
3
|
+
/** The codec when none is given. */
|
|
4
|
+
const json = {
|
|
5
|
+
serialize: (value) => JSON.stringify(value ?? null),
|
|
6
|
+
deserialize: (text) => JSON.parse(text)
|
|
7
|
+
};
|
|
8
|
+
/** The input of an op only this backend calls: its one caller builds it, so it is typed only. */
|
|
9
|
+
const given = () => (raw) => raw;
|
|
10
|
+
/**
|
|
11
|
+
* On the wire the id rides in `m` as `execution`, as it did before ADR 0154: an open page and a
|
|
12
|
+
* new server still agree. The server takes it back out, so a hook's meta never holds it.
|
|
13
|
+
*/
|
|
14
|
+
const withId = (m, execution) => execution ? {
|
|
15
|
+
...m,
|
|
16
|
+
execution
|
|
17
|
+
} : m;
|
|
18
|
+
const idOff = ({ execution, ...meta }) => ({
|
|
19
|
+
meta,
|
|
20
|
+
execution
|
|
21
|
+
});
|
|
22
|
+
/**
|
|
23
|
+
* The codec, used on one side: a fault keeps its cause in this side's log, and says only the side.
|
|
24
|
+
* `at.log` is read only on a fault, so a call with none makes no logger.
|
|
25
|
+
*/
|
|
26
|
+
const coding = (codec, side, at) => {
|
|
27
|
+
const fault = (kind, address, error) => {
|
|
28
|
+
at.log.error(`protocol ${kind} failed`, {
|
|
29
|
+
address,
|
|
30
|
+
side,
|
|
31
|
+
cause: String(error),
|
|
32
|
+
stack: String(Object(error).stack ?? "")
|
|
33
|
+
});
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
kind,
|
|
37
|
+
payload: { side }
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const step = (kind, fn) => (address, a) => {
|
|
41
|
+
try {
|
|
42
|
+
return {
|
|
43
|
+
ok: true,
|
|
44
|
+
value: fn(a)
|
|
45
|
+
};
|
|
46
|
+
} catch (error) {
|
|
47
|
+
return fault(kind, address, error);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const encode = step("encode", (value) => codec.serialize(value));
|
|
51
|
+
const decode = step("decode", (text) => codec.deserialize(text));
|
|
52
|
+
return {
|
|
53
|
+
encode,
|
|
54
|
+
decode,
|
|
55
|
+
/** A reply as text. A reply the codec cannot carry goes out as the encode fault instead. */
|
|
56
|
+
answerText: (address, reply) => {
|
|
57
|
+
const text = encode(address, reply);
|
|
58
|
+
return text.ok ? text.value : codec.serialize(text);
|
|
59
|
+
},
|
|
60
|
+
replyOf: (address, text) => {
|
|
61
|
+
const reply = decode(address, text);
|
|
62
|
+
return reply.ok ? reply.value : reply;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* One text op of the server: decode, then the protocol's `answer` or `stream`, then encode. A
|
|
68
|
+
* codec that throws rejects the run (async), as a throw in the protocol did.
|
|
69
|
+
*/
|
|
70
|
+
const serveText = ({ side, codec, protocol: p }, hook, answering, { text, ...input }) => {
|
|
71
|
+
const { decode, answerText } = coding(codec, "server", hook);
|
|
72
|
+
const answerOf = async (reply) => reply && answerText(input.address, reply);
|
|
73
|
+
const push = (reply) => input.push(answerText(input.address, reply));
|
|
74
|
+
const read = decode(input.address, text);
|
|
75
|
+
if (!read.ok) return answering ? answerOf(read) : Promise.resolve().then(() => (push(read), true));
|
|
76
|
+
const asked = {
|
|
77
|
+
...input,
|
|
78
|
+
value: read.value
|
|
79
|
+
};
|
|
80
|
+
if (answering) return p.answer(side, hook, asked).then(answerOf);
|
|
81
|
+
return p.stream(side, hook, {
|
|
82
|
+
...asked,
|
|
83
|
+
push
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
//#endregion
|
|
2
87
|
//#region src/tools.ts
|
|
3
88
|
/**
|
|
4
89
|
* A run hook's tools, as the protocol's `Hook`. Core makes an event's logger on the first read
|
|
@@ -19,7 +104,7 @@ var Tools = class {
|
|
|
19
104
|
};
|
|
20
105
|
//#endregion
|
|
21
106
|
//#region src/client.ts
|
|
22
|
-
const { callInput, kept,
|
|
107
|
+
const { callInput, kept, lost, protocol, watchInput } = transport;
|
|
23
108
|
/**
|
|
24
109
|
* How a send or open ends when no text came (ADR 0141): a value, never a throw. A 404 is the
|
|
25
110
|
* server's `missing`; a drop before the message left is `sent: false`, nothing ran.
|
|
@@ -55,7 +140,10 @@ function createWs() {
|
|
|
55
140
|
default: {}
|
|
56
141
|
});
|
|
57
142
|
const keys = {
|
|
58
|
-
codec: tag({
|
|
143
|
+
codec: tag({
|
|
144
|
+
label: "ws.codec",
|
|
145
|
+
default: json
|
|
146
|
+
}),
|
|
59
147
|
meta: tag({ label: "ws.meta" }).optional,
|
|
60
148
|
renew: tag({ label: "ws.renew" }).optional
|
|
61
149
|
};
|
|
@@ -63,113 +151,147 @@ function createWs() {
|
|
|
63
151
|
label: "ws.link",
|
|
64
152
|
initial: null
|
|
65
153
|
});
|
|
154
|
+
/**
|
|
155
|
+
* Bytes go on their own POST beside the socket: `<url>/<address>`, the value as text in
|
|
156
|
+
* `x-input`, the meta in `x-meta`, the bytes as the body, streamed by fetch.
|
|
157
|
+
* A fetch that rejects is the network (MDN: a TypeError), so a link that may have sent.
|
|
158
|
+
*/
|
|
159
|
+
const postBytes = async (to, { address, text, m, signal }, body) => {
|
|
160
|
+
const answer = await fetch(`${to.url.replace(/^ws/, "http")}/${address}`, {
|
|
161
|
+
method: "POST",
|
|
162
|
+
credentials: "same-origin",
|
|
163
|
+
headers: {
|
|
164
|
+
...to.headers,
|
|
165
|
+
"content-type": body.type,
|
|
166
|
+
"x-input": text,
|
|
167
|
+
"x-meta": JSON.stringify(m)
|
|
168
|
+
},
|
|
169
|
+
body,
|
|
170
|
+
signal
|
|
171
|
+
}).catch(() => null);
|
|
172
|
+
if (!answer) return exitOf(null);
|
|
173
|
+
if (!answer.ok) {
|
|
174
|
+
answer.body?.cancel();
|
|
175
|
+
return exitOf({
|
|
176
|
+
i: 0,
|
|
177
|
+
e: answer.status
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return answer.text().then((got) => ({
|
|
181
|
+
ok: true,
|
|
182
|
+
text: got
|
|
183
|
+
}), () => exitOf(null));
|
|
184
|
+
};
|
|
185
|
+
/** One call on the socket: its answer's text, or the exit. */
|
|
186
|
+
const onSocket = (own, { address, text, m, signal }) => new Promise((got) => {
|
|
187
|
+
const i = own.next++;
|
|
188
|
+
const stop = () => {
|
|
189
|
+
own.waits.delete(i);
|
|
190
|
+
own.post({
|
|
191
|
+
i,
|
|
192
|
+
x: 1
|
|
193
|
+
});
|
|
194
|
+
got(exitOf(null));
|
|
195
|
+
};
|
|
196
|
+
own.waits.set(i, (message, sent) => {
|
|
197
|
+
own.waits.delete(i);
|
|
198
|
+
signal.removeEventListener("abort", stop);
|
|
199
|
+
got(message?.t === void 0 ? exitOf(message, sent) : {
|
|
200
|
+
ok: true,
|
|
201
|
+
text: message.t
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
signal.addEventListener("abort", stop);
|
|
205
|
+
own.post({
|
|
206
|
+
i,
|
|
207
|
+
a: address,
|
|
208
|
+
t: text,
|
|
209
|
+
m
|
|
210
|
+
});
|
|
211
|
+
});
|
|
66
212
|
const send = operation({
|
|
67
213
|
label: "ws.send",
|
|
68
|
-
input:
|
|
214
|
+
input: callInput,
|
|
69
215
|
depends: {
|
|
70
216
|
link: link.controller,
|
|
71
217
|
protocol,
|
|
72
218
|
url: url.required,
|
|
73
|
-
headers
|
|
219
|
+
headers,
|
|
220
|
+
codec: keys.codec
|
|
74
221
|
},
|
|
75
|
-
run: async ({ link, protocol, url, headers
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
const
|
|
222
|
+
run: async ({ link, protocol, url, headers, codec }, ctx) => {
|
|
223
|
+
const { input, signal } = ctx;
|
|
224
|
+
const { encode, replyOf } = coding(codec, "client", ctx);
|
|
225
|
+
const text = encode(input.address, input.value);
|
|
226
|
+
if (!text.ok) return text;
|
|
227
|
+
const m = withId({
|
|
79
228
|
...input.meta,
|
|
80
|
-
...protocol.traceparent(obs.span)
|
|
229
|
+
...protocol.traceparent(ctx.obs.span)
|
|
230
|
+
}, input.execution);
|
|
231
|
+
const asked = {
|
|
232
|
+
address: input.address,
|
|
233
|
+
text: text.value,
|
|
234
|
+
m,
|
|
235
|
+
signal
|
|
81
236
|
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
...headers,
|
|
88
|
-
"content-type": input.body.type,
|
|
89
|
-
"x-input": input.text,
|
|
90
|
-
"x-meta": JSON.stringify(m)
|
|
91
|
-
},
|
|
92
|
-
body: input.body,
|
|
93
|
-
signal
|
|
94
|
-
}).catch(() => null);
|
|
95
|
-
if (!answer) return exitOf(null);
|
|
96
|
-
if (!answer.ok) {
|
|
97
|
-
answer.body?.cancel();
|
|
98
|
-
return exitOf({
|
|
99
|
-
i,
|
|
100
|
-
e: answer.status
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
return answer.text().then((text) => ({
|
|
104
|
-
ok: true,
|
|
105
|
-
text
|
|
106
|
-
}), () => exitOf(null));
|
|
107
|
-
}
|
|
108
|
-
return new Promise((got) => {
|
|
109
|
-
const stop = () => {
|
|
110
|
-
own.waits.delete(i);
|
|
111
|
-
own.post({
|
|
112
|
-
i,
|
|
113
|
-
x: 1
|
|
114
|
-
});
|
|
115
|
-
got(exitOf(null));
|
|
116
|
-
};
|
|
117
|
-
own.waits.set(i, (message, sent) => {
|
|
118
|
-
own.waits.delete(i);
|
|
119
|
-
signal.removeEventListener("abort", stop);
|
|
120
|
-
got(message?.t === void 0 ? exitOf(message, sent) : {
|
|
121
|
-
ok: true,
|
|
122
|
-
text: message.t
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
signal.addEventListener("abort", stop);
|
|
126
|
-
own.post({
|
|
127
|
-
i,
|
|
128
|
-
a: input.address,
|
|
129
|
-
t: input.text,
|
|
130
|
-
m
|
|
131
|
-
});
|
|
132
|
-
});
|
|
237
|
+
const got = await (input.body ? postBytes({
|
|
238
|
+
url,
|
|
239
|
+
headers
|
|
240
|
+
}, asked, input.body) : onSocket(link.get(), asked));
|
|
241
|
+
return got.ok ? replyOf(input.address, got.text) : lost(got);
|
|
133
242
|
}
|
|
134
243
|
});
|
|
244
|
+
/** One watch on the socket: each push's text to `push`, until it ends (`ok`) or its exit. */
|
|
245
|
+
const watchOnSocket = (own, { address, text, m, signal }, push) => new Promise((ended) => {
|
|
246
|
+
const i = own.next++;
|
|
247
|
+
const stop = () => {
|
|
248
|
+
own.waits.delete(i);
|
|
249
|
+
own.post({
|
|
250
|
+
i,
|
|
251
|
+
x: 1
|
|
252
|
+
});
|
|
253
|
+
ended({ ok: true });
|
|
254
|
+
};
|
|
255
|
+
own.waits.set(i, (message, sent) => {
|
|
256
|
+
if (message?.t !== void 0) return push(message.t);
|
|
257
|
+
own.waits.delete(i);
|
|
258
|
+
signal.removeEventListener("abort", stop);
|
|
259
|
+
ended(message?.end ? { ok: true } : exitOf(message, sent));
|
|
260
|
+
});
|
|
261
|
+
signal.addEventListener("abort", stop);
|
|
262
|
+
own.post({
|
|
263
|
+
i,
|
|
264
|
+
a: address,
|
|
265
|
+
t: text,
|
|
266
|
+
m,
|
|
267
|
+
w: 1
|
|
268
|
+
});
|
|
269
|
+
});
|
|
135
270
|
const open = operation({
|
|
136
271
|
label: "ws.open",
|
|
137
|
-
input:
|
|
272
|
+
input: watchInput,
|
|
138
273
|
depends: {
|
|
139
274
|
link: link.controller,
|
|
140
|
-
protocol
|
|
275
|
+
protocol,
|
|
276
|
+
codec: keys.codec
|
|
141
277
|
},
|
|
142
|
-
run: ({ link, protocol
|
|
143
|
-
const
|
|
144
|
-
const
|
|
278
|
+
run: async ({ link, protocol, codec }, ctx) => {
|
|
279
|
+
const { input, signal } = ctx;
|
|
280
|
+
const { encode, replyOf } = coding(codec, "client", ctx);
|
|
281
|
+
const text = encode(input.address, input.value);
|
|
282
|
+
if (!text.ok) return text;
|
|
145
283
|
const m = {
|
|
146
284
|
...input.meta,
|
|
147
|
-
...protocol.traceparent(obs.span)
|
|
285
|
+
...protocol.traceparent(ctx.obs.span)
|
|
148
286
|
};
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
};
|
|
158
|
-
own.waits.set(i, (message, sent) => {
|
|
159
|
-
if (message?.t !== void 0) return input.push(message.t);
|
|
160
|
-
own.waits.delete(i);
|
|
161
|
-
signal.removeEventListener("abort", stop);
|
|
162
|
-
ended(message?.end ? { ok: true } : exitOf(message, sent));
|
|
163
|
-
});
|
|
164
|
-
signal.addEventListener("abort", stop);
|
|
165
|
-
own.post({
|
|
166
|
-
i,
|
|
167
|
-
a: input.address,
|
|
168
|
-
t: input.text,
|
|
169
|
-
m,
|
|
170
|
-
w: 1
|
|
171
|
-
});
|
|
172
|
-
});
|
|
287
|
+
const push = (t) => input.push(replyOf(input.address, t));
|
|
288
|
+
const end = await watchOnSocket(link.get(), {
|
|
289
|
+
...input,
|
|
290
|
+
text: text.value,
|
|
291
|
+
m,
|
|
292
|
+
signal
|
|
293
|
+
}, push);
|
|
294
|
+
return end.ok ? end : lost(end);
|
|
173
295
|
}
|
|
174
296
|
});
|
|
175
297
|
/**
|
|
@@ -279,7 +401,6 @@ function createWs() {
|
|
|
279
401
|
side: {
|
|
280
402
|
send,
|
|
281
403
|
open,
|
|
282
|
-
codec: p.given(config.codec) ?? p.json,
|
|
283
404
|
meta: p.given(config.meta),
|
|
284
405
|
renew: p.given(config.renew)
|
|
285
406
|
}
|
|
@@ -305,4 +426,4 @@ function createWs() {
|
|
|
305
426
|
/** The default WebSocket client. */
|
|
306
427
|
const ws = createWs();
|
|
307
428
|
//#endregion
|
|
308
|
-
export { Tools as i, frameMax as n, ws as r, createWs as t };
|
|
429
|
+
export { given as a, serveText as c, Tools as i, frameMax as n, idOff as o, ws as r, json as s, createWs as t };
|
package/dist/client.d.mts
CHANGED
|
@@ -18,7 +18,7 @@ export declare const frameMax = 32;
|
|
|
18
18
|
/** Makes one WebSocket client. */
|
|
19
19
|
export declare function createWs(): {
|
|
20
20
|
client: Omit<import("@tinkered/core").Capsule.Handle<{
|
|
21
|
-
readonly codec: import("@tinkered/core").
|
|
21
|
+
readonly codec: import("@tinkered/core").Tag.Defaulted<transport.Codec>;
|
|
22
22
|
readonly meta: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.MetaOp>>;
|
|
23
23
|
readonly renew: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.RenewOp>>;
|
|
24
24
|
readonly url: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
|
|
@@ -32,7 +32,7 @@ export declare function createWs(): {
|
|
|
32
32
|
readonly watch: Watch;
|
|
33
33
|
readonly reset: import("@tinkered/core").Operation.Handle<void | undefined, void>;
|
|
34
34
|
(values: ClientConfig): ReturnType<import("@tinkered/core").Capsule.Handle<{
|
|
35
|
-
readonly codec: import("@tinkered/core").
|
|
35
|
+
readonly codec: import("@tinkered/core").Tag.Defaulted<transport.Codec>;
|
|
36
36
|
readonly meta: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.MetaOp>>;
|
|
37
37
|
readonly renew: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.RenewOp>>;
|
|
38
38
|
readonly url: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
|
|
@@ -47,7 +47,7 @@ export declare function createWs(): {
|
|
|
47
47
|
/** The default WebSocket client. */
|
|
48
48
|
export declare const ws: {
|
|
49
49
|
client: Omit<import("@tinkered/core").Capsule.Handle<{
|
|
50
|
-
readonly codec: import("@tinkered/core").
|
|
50
|
+
readonly codec: import("@tinkered/core").Tag.Defaulted<transport.Codec>;
|
|
51
51
|
readonly meta: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.MetaOp>>;
|
|
52
52
|
readonly renew: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.RenewOp>>;
|
|
53
53
|
readonly url: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
|
|
@@ -61,7 +61,7 @@ export declare const ws: {
|
|
|
61
61
|
readonly watch: Watch;
|
|
62
62
|
readonly reset: import("@tinkered/core").Operation.Handle<void | undefined, void>;
|
|
63
63
|
(values: ClientConfig): ReturnType<import("@tinkered/core").Capsule.Handle<{
|
|
64
|
-
readonly codec: import("@tinkered/core").
|
|
64
|
+
readonly codec: import("@tinkered/core").Tag.Defaulted<transport.Codec>;
|
|
65
65
|
readonly meta: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.MetaOp>>;
|
|
66
66
|
readonly renew: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.RenewOp>>;
|
|
67
67
|
readonly url: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
|
package/dist/client.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as frameMax, r as ws, t as createWs } from "./client-
|
|
1
|
+
import { n as frameMax, r as ws, t as createWs } from "./client-BXRgKu3R.mjs";
|
|
2
2
|
export { createWs, frameMax, ws };
|
package/dist/index.d.mts
CHANGED
|
@@ -39,7 +39,7 @@ export declare function createWs(): {
|
|
|
39
39
|
server: Omit<import("@tinkered/core").Capsule.Handle<{
|
|
40
40
|
readonly path: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
|
|
41
41
|
readonly origin: import("@tinkered/core").Tag.Defaulted<string | null>;
|
|
42
|
-
readonly codec: import("@tinkered/core").
|
|
42
|
+
readonly codec: import("@tinkered/core").Tag.Defaulted<transport.Codec>;
|
|
43
43
|
readonly services: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<readonly transport.Service[]>>;
|
|
44
44
|
readonly strict: import("@tinkered/core").Tag.Defaulted<boolean>;
|
|
45
45
|
readonly maxPayload: import("@tinkered/core").Tag.Defaulted<number>;
|
|
@@ -56,7 +56,7 @@ export declare function createWs(): {
|
|
|
56
56
|
(values: ServerConfig): ReturnType<import("@tinkered/core").Capsule.Handle<{
|
|
57
57
|
readonly path: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
|
|
58
58
|
readonly origin: import("@tinkered/core").Tag.Defaulted<string | null>;
|
|
59
|
-
readonly codec: import("@tinkered/core").
|
|
59
|
+
readonly codec: import("@tinkered/core").Tag.Defaulted<transport.Codec>;
|
|
60
60
|
readonly services: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<readonly transport.Service[]>>;
|
|
61
61
|
readonly strict: import("@tinkered/core").Tag.Defaulted<boolean>;
|
|
62
62
|
readonly maxPayload: import("@tinkered/core").Tag.Defaulted<number>;
|
|
@@ -77,7 +77,7 @@ export declare const ws: {
|
|
|
77
77
|
server: Omit<import("@tinkered/core").Capsule.Handle<{
|
|
78
78
|
readonly path: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
|
|
79
79
|
readonly origin: import("@tinkered/core").Tag.Defaulted<string | null>;
|
|
80
|
-
readonly codec: import("@tinkered/core").
|
|
80
|
+
readonly codec: import("@tinkered/core").Tag.Defaulted<transport.Codec>;
|
|
81
81
|
readonly services: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<readonly transport.Service[]>>;
|
|
82
82
|
readonly strict: import("@tinkered/core").Tag.Defaulted<boolean>;
|
|
83
83
|
readonly maxPayload: import("@tinkered/core").Tag.Defaulted<number>;
|
|
@@ -94,7 +94,7 @@ export declare const ws: {
|
|
|
94
94
|
(values: ServerConfig): ReturnType<import("@tinkered/core").Capsule.Handle<{
|
|
95
95
|
readonly path: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
|
|
96
96
|
readonly origin: import("@tinkered/core").Tag.Defaulted<string | null>;
|
|
97
|
-
readonly codec: import("@tinkered/core").
|
|
97
|
+
readonly codec: import("@tinkered/core").Tag.Defaulted<transport.Codec>;
|
|
98
98
|
readonly services: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<readonly transport.Service[]>>;
|
|
99
99
|
readonly strict: import("@tinkered/core").Tag.Defaulted<boolean>;
|
|
100
100
|
readonly maxPayload: import("@tinkered/core").Tag.Defaulted<number>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { i as Tools } from "./client-
|
|
1
|
+
import { a as given, c as serveText, i as Tools, o as idOff, s as json } from "./client-BXRgKu3R.mjs";
|
|
2
2
|
import { capsule, data, operation, tag, transport } from "@tinkered/core";
|
|
3
3
|
//#region src/ws.ts
|
|
4
|
-
const { kept,
|
|
5
|
-
/** The input of an op only this transport calls: its one caller builds it, so it is typed only. */
|
|
6
|
-
const given = () => (raw) => raw;
|
|
4
|
+
const { kept, protocol, request, requestStop, traceparentOf } = transport;
|
|
7
5
|
/** True for an object of string values only: the `x-meta` header of a bytes POST. */
|
|
8
6
|
const isTextRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v) && Object.values(v).every((each) => typeof each === "string");
|
|
9
7
|
/** Makes one WebSocket server. */
|
|
10
8
|
function createWs() {
|
|
11
9
|
const path = tag({ label: "ws.path" });
|
|
12
|
-
const codec = tag({
|
|
10
|
+
const codec = tag({
|
|
11
|
+
label: "ws.codec",
|
|
12
|
+
default: json
|
|
13
|
+
});
|
|
13
14
|
const services = tag({ label: "ws.services" }).required;
|
|
14
15
|
const strict = tag({
|
|
15
16
|
label: "ws.strict",
|
|
@@ -46,14 +47,14 @@ function createWs() {
|
|
|
46
47
|
});
|
|
47
48
|
const serve = operation({
|
|
48
49
|
label: "ws.serve",
|
|
49
|
-
input:
|
|
50
|
+
input: given(),
|
|
50
51
|
run: () => {
|
|
51
52
|
throw new Error("ws: no server here");
|
|
52
53
|
}
|
|
53
54
|
});
|
|
54
55
|
const serveStream = operation({
|
|
55
56
|
label: "ws.serveStream",
|
|
56
|
-
input:
|
|
57
|
+
input: given(),
|
|
57
58
|
run: () => {
|
|
58
59
|
throw new Error("ws: no server here");
|
|
59
60
|
}
|
|
@@ -153,17 +154,14 @@ function createWs() {
|
|
|
153
154
|
});
|
|
154
155
|
const p = await load(protocol, kept);
|
|
155
156
|
const routesOf = p.routesOf(config.services, raise, log, config.strict);
|
|
156
|
-
const side = {
|
|
157
|
-
codec: p.given(config.codec) ?? p.json,
|
|
158
|
-
routes: routesOf
|
|
159
|
-
};
|
|
160
157
|
/** The addresses that take bytes, with their limits, from the service that owns each. */
|
|
161
158
|
const uploadsAt = new Map([...routesOf].flatMap(([address, one]) => {
|
|
162
159
|
const limit = one.uploads?.[address];
|
|
163
160
|
return limit ? [[address, limit]] : [];
|
|
164
161
|
}));
|
|
165
162
|
load(held, kept).set({
|
|
166
|
-
side,
|
|
163
|
+
side: { routes: routesOf },
|
|
164
|
+
codec: config.codec,
|
|
167
165
|
protocol: p
|
|
168
166
|
});
|
|
169
167
|
await next();
|
|
@@ -203,7 +201,7 @@ function createWs() {
|
|
|
203
201
|
},
|
|
204
202
|
/** A call or a watch, in its own session of the scope. */
|
|
205
203
|
async run(ws, m) {
|
|
206
|
-
const meta = Object(m.m);
|
|
204
|
+
const { meta, execution } = idOff(Object(m.m));
|
|
207
205
|
const trace = traceparentOf(meta.traceparent);
|
|
208
206
|
const stop = m.w ? new AbortController() : void 0;
|
|
209
207
|
const left = AbortSignal.any(stop ? [stop.signal, own.gone.signal] : [own.gone.signal]);
|
|
@@ -219,7 +217,8 @@ function createWs() {
|
|
|
219
217
|
const input = {
|
|
220
218
|
address: String(m.a),
|
|
221
219
|
text: String(m.t),
|
|
222
|
-
meta
|
|
220
|
+
meta,
|
|
221
|
+
execution
|
|
223
222
|
};
|
|
224
223
|
if (!m.w) {
|
|
225
224
|
const reply = await session.run(serve, { input }).finally(() => session.close());
|
|
@@ -277,7 +276,8 @@ function createWs() {
|
|
|
277
276
|
busy
|
|
278
277
|
} });
|
|
279
278
|
if (admitted instanceof Response) return admitted;
|
|
280
|
-
const { text,
|
|
279
|
+
const { text, type } = admitted;
|
|
280
|
+
const { meta, execution } = idOff(admitted.meta);
|
|
281
281
|
const stop = AbortSignal.any([req.signal, AbortSignal.timeout(config.uploadDeadline)]);
|
|
282
282
|
const trace = traceparentOf(meta.traceparent);
|
|
283
283
|
const tags = [request(req), requestStop(stop)];
|
|
@@ -297,7 +297,8 @@ function createWs() {
|
|
|
297
297
|
address,
|
|
298
298
|
text,
|
|
299
299
|
meta,
|
|
300
|
-
body
|
|
300
|
+
body,
|
|
301
|
+
execution
|
|
301
302
|
} }).finally(() => {
|
|
302
303
|
uploading -= 1;
|
|
303
304
|
req.body?.cancel().catch(() => void 0);
|
|
@@ -345,10 +346,9 @@ function createWs() {
|
|
|
345
346
|
run(event) {
|
|
346
347
|
const { op, call: invocation } = event;
|
|
347
348
|
if (op !== serve && op !== serveStream) return event.next();
|
|
348
|
-
const
|
|
349
|
-
const hook = new Tools(event);
|
|
349
|
+
const own = event.load(held, kept).get();
|
|
350
350
|
const input = invocation?.input ?? invocation?.rawInput;
|
|
351
|
-
return
|
|
351
|
+
return serveText(own, new Tools(event), op === serve, input);
|
|
352
352
|
}
|
|
353
353
|
}
|
|
354
354
|
}) };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinkered/bun",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "Tinker on Bun: the host on Bun.serve, rpc over one WebSocket, and Bun's own databases (Bun.SQL, bun:sqlite).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@tinkered/core": "^0.
|
|
27
|
+
"@tinkered/core": "^0.25.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@hughescr/stryker-bun-runner": "1.6.0",
|