@tinyshed/tinystore 0.1.0-rc.1

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 (100) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +262 -0
  3. package/bin/tinystore.js +50 -0
  4. package/dist/background.d.ts +30 -0
  5. package/dist/background.js +93 -0
  6. package/dist/blobs.d.ts +98 -0
  7. package/dist/blobs.js +299 -0
  8. package/dist/cancel.d.ts +14 -0
  9. package/dist/cancel.js +22 -0
  10. package/dist/clock.d.ts +21 -0
  11. package/dist/clock.js +35 -0
  12. package/dist/config.d.ts +60 -0
  13. package/dist/config.js +417 -0
  14. package/dist/connection.d.ts +78 -0
  15. package/dist/connection.js +405 -0
  16. package/dist/console.d.ts +48 -0
  17. package/dist/console.js +195 -0
  18. package/dist/errors.d.ts +103 -0
  19. package/dist/errors.js +158 -0
  20. package/dist/handles.d.ts +28 -0
  21. package/dist/handles.js +37 -0
  22. package/dist/index.d.ts +21 -0
  23. package/dist/index.js +7 -0
  24. package/dist/jobs.d.ts +304 -0
  25. package/dist/jobs.js +684 -0
  26. package/dist/kv.d.ts +283 -0
  27. package/dist/kv.js +815 -0
  28. package/dist/limiter.d.ts +45 -0
  29. package/dist/limiter.js +68 -0
  30. package/dist/logger.d.ts +96 -0
  31. package/dist/logger.js +243 -0
  32. package/dist/metrics.d.ts +249 -0
  33. package/dist/metrics.js +610 -0
  34. package/dist/once.d.ts +31 -0
  35. package/dist/once.js +116 -0
  36. package/dist/quota.d.ts +44 -0
  37. package/dist/quota.js +98 -0
  38. package/dist/records.d.ts +182 -0
  39. package/dist/records.js +346 -0
  40. package/dist/runtime/bun.d.ts +2 -0
  41. package/dist/runtime/bun.js +164 -0
  42. package/dist/runtime/node.d.ts +2 -0
  43. package/dist/runtime/node.js +157 -0
  44. package/dist/runtime/shared.d.ts +25 -0
  45. package/dist/runtime/shared.js +45 -0
  46. package/dist/runtime.d.ts +48 -0
  47. package/dist/runtime.js +4 -0
  48. package/dist/schema.d.ts +39 -0
  49. package/dist/schema.js +21 -0
  50. package/dist/session.d.ts +120 -0
  51. package/dist/session.js +546 -0
  52. package/dist/sql.d.ts +121 -0
  53. package/dist/sql.js +342 -0
  54. package/dist/store.d.ts +103 -0
  55. package/dist/store.js +153 -0
  56. package/dist/time.d.ts +35 -0
  57. package/dist/time.js +44 -0
  58. package/dist/trace.d.ts +18 -0
  59. package/dist/trace.js +22 -0
  60. package/dist/wire/codec.d.ts +90 -0
  61. package/dist/wire/codec.js +383 -0
  62. package/dist/wire/frame.d.ts +41 -0
  63. package/dist/wire/frame.js +102 -0
  64. package/dist/wire/messages.d.ts +1403 -0
  65. package/dist/wire/messages.js +472 -0
  66. package/dist/wire/msgpack.d.ts +93 -0
  67. package/dist/wire/msgpack.js +680 -0
  68. package/package.json +40 -0
  69. package/src/background.ts +108 -0
  70. package/src/blobs.ts +437 -0
  71. package/src/cancel.ts +26 -0
  72. package/src/clock.ts +45 -0
  73. package/src/config.ts +501 -0
  74. package/src/connection.ts +512 -0
  75. package/src/console.ts +228 -0
  76. package/src/errors.ts +211 -0
  77. package/src/handles.ts +68 -0
  78. package/src/index.ts +92 -0
  79. package/src/jobs.ts +963 -0
  80. package/src/kv.ts +1093 -0
  81. package/src/limiter.ts +99 -0
  82. package/src/logger.ts +326 -0
  83. package/src/metrics.ts +896 -0
  84. package/src/once.ts +142 -0
  85. package/src/quota.ts +137 -0
  86. package/src/records.ts +537 -0
  87. package/src/runtime/bun.ts +190 -0
  88. package/src/runtime/node.ts +177 -0
  89. package/src/runtime/shared.ts +61 -0
  90. package/src/runtime.ts +57 -0
  91. package/src/schema.ts +69 -0
  92. package/src/session.ts +665 -0
  93. package/src/sql.ts +478 -0
  94. package/src/store.ts +219 -0
  95. package/src/time.ts +87 -0
  96. package/src/trace.ts +32 -0
  97. package/src/wire/codec.ts +445 -0
  98. package/src/wire/frame.ts +139 -0
  99. package/src/wire/messages.ts +562 -0
  100. package/src/wire/msgpack.ts +712 -0
@@ -0,0 +1,346 @@
1
+ // The application's logs and events in records.db, as Go's records package
2
+ // keeps them: one log of the store's whose calls name their streams, read by
3
+ // time and by what records hold, followed in the order they were sealed.
4
+ import { download } from './connection.js';
5
+ import { InvalidError } from './errors.js';
6
+ import { newLogger } from './logger.js';
7
+ import { ms } from './time.js';
8
+ import { currentTrace } from './trace.js';
9
+ import { Empty, methods, RecordsQuery as QueryMessage, RecordsBatch, RecordsCursor, RecordsDamage, RecordsDamages, RecordsPage, RecordsRecord, RecordsStream, } from './wire/messages.js';
10
+ const levels = { debug: -4, info: 0, warn: 4, error: 8 };
11
+ function levelOf(level) {
12
+ return typeof level === 'number' ? level : levels[level];
13
+ }
14
+ /**
15
+ * A record's fields, its attrs or its context, as an object, each value read
16
+ * as JSON.parse reads the JSON it was kept as; the last of a repeated key is
17
+ * kept. The pairs keep what JSON.parse may not: 1.2300 as written, an integer
18
+ * past 2^53 whole.
19
+ *
20
+ * ```ts
21
+ * for await (const record of store.records.all({ since: '1h', streams: ['readers'] })) {
22
+ * const { page, from } = fields(record.attrs)
23
+ * }
24
+ * ```
25
+ */
26
+ export function fields(pairs) {
27
+ // fromEntries, since an assignment to __proto__ would set the prototype
28
+ return Object.fromEntries(pairs.map(([key, json]) => [key, JSON.parse(textOf(json))]));
29
+ }
30
+ const nsPerMs = 1000000n;
31
+ function nanos(t) {
32
+ return typeof t === 'bigint' ? t : BigInt(t.getTime()) * nsPerMs;
33
+ }
34
+ function idOf(id, bytes, what) {
35
+ if (id === undefined) {
36
+ return undefined;
37
+ }
38
+ const raw = typeof id === 'string' ? new Uint8Array(Buffer.from(id, 'hex')) : id;
39
+ if (raw.length !== bytes) {
40
+ throw new InvalidError(`a ${what} id of ${raw.length} bytes, not ${bytes}`);
41
+ }
42
+ return raw;
43
+ }
44
+ /** Fields as the wire carries them: one array of keys and JSON texts. */
45
+ function fieldsOf(fields) {
46
+ if (fields === undefined) {
47
+ return undefined;
48
+ }
49
+ const pairs = Array.isArray(fields) ? fields : Object.entries(fields);
50
+ const flat = [];
51
+ for (const [key, value] of pairs) {
52
+ const json = JSON.stringify(value);
53
+ if (json === undefined) {
54
+ throw new InvalidError(`the field ${key} holds a value JSON cannot write`);
55
+ }
56
+ flat.push(key, json);
57
+ }
58
+ return flat.length === 0 ? undefined : flat;
59
+ }
60
+ const lossy = new TextDecoder();
61
+ function textOf(text) {
62
+ return text === undefined ? '' : typeof text === 'string' ? text : lossy.decode(text);
63
+ }
64
+ function pairs(flat) {
65
+ const out = [];
66
+ for (let i = 0; flat !== undefined && i + 1 < flat.length; i += 2) {
67
+ out.push([textOf(flat[i]), flat[i + 1]]);
68
+ }
69
+ return out;
70
+ }
71
+ function recordOf(r) {
72
+ return {
73
+ at: r.at ?? 0n,
74
+ stream: textOf(r.stream),
75
+ name: textOf(r.name),
76
+ level: r.level,
77
+ body: r.body,
78
+ traceId: r.traceId,
79
+ spanId: r.spanId,
80
+ context: pairs(r.context),
81
+ attrs: pairs(r.attrs),
82
+ };
83
+ }
84
+ /**
85
+ * A page's next: where the range moved, both ends as nanoseconds, so that a
86
+ * query over the last `since` continues the range it started with.
87
+ *
88
+ * from 1700000000000000000, open end → '1700000000000000000:'
89
+ */
90
+ function cursorOf(from, to) {
91
+ return `${from ?? ''}:${to ?? ''}`;
92
+ }
93
+ function rangeOf(q) {
94
+ if (q?.after !== undefined) {
95
+ const ends = /^(-?\d*):(-?\d*)$/.exec(q.after);
96
+ if (ends === null) {
97
+ throw new InvalidError(`${JSON.stringify(q.after)} is no page's next`);
98
+ }
99
+ return {
100
+ from: ends[1] === '' ? undefined : BigInt(ends[1]),
101
+ to: ends[2] === '' ? undefined : BigInt(ends[2]),
102
+ };
103
+ }
104
+ if (q?.since !== undefined && q.from !== undefined) {
105
+ throw new InvalidError('a range starts since a span before now or from a time, not both');
106
+ }
107
+ const from = q?.since !== undefined
108
+ ? nanos(new Date(Date.now() - ms(q.since)))
109
+ : q?.from === undefined
110
+ ? undefined
111
+ : nanos(q.from);
112
+ return { from, to: q?.to === undefined ? undefined : nanos(q.to) };
113
+ }
114
+ function queryOf(q) {
115
+ return {
116
+ ...rangeOf(q),
117
+ streams: q?.streams !== undefined && q.streams.length > 0 ? q.streams : undefined,
118
+ names: q?.names !== undefined && q.names.length > 0 ? q.names : undefined,
119
+ minLevel: q?.minLevel === undefined ? undefined : levelOf(q.minLevel),
120
+ traceId: idOf(q?.traceId, 16, 'trace'),
121
+ attrs: fieldsOf(q?.attrs),
122
+ context: fieldsOf(q?.context),
123
+ newest: q?.newest === true ? true : undefined,
124
+ limit: q?.limit,
125
+ budgetBlocks: q?.budget?.blocks,
126
+ budgetBytes: q?.budget?.bytes,
127
+ budgetRecords: q?.budget?.records,
128
+ search: q?.search === '' ? undefined : q?.search,
129
+ };
130
+ }
131
+ export class Records {
132
+ #link;
133
+ #loggers = [];
134
+ constructor(link) {
135
+ this.#link = link;
136
+ }
137
+ /**
138
+ * A logger of a stream: its calls return at once, and its lines reach the
139
+ * server every second, as Go's slog handler's and Python's logging.Handler's do.
140
+ */
141
+ logger(stream, options) {
142
+ const logger = newLogger(lines => this.#writeLines(lines), stream, options);
143
+ this.#loggers.push(logger);
144
+ return logger;
145
+ }
146
+ /** Hands over what every logger holds, as the store does when it closes. */
147
+ async stop() {
148
+ await Promise.all(this.#loggers.map(logger => logger.stop()));
149
+ }
150
+ /**
151
+ * Appends records in one transaction, all or none; a refused one names
152
+ * itself as `call`, with its stream and name. A read sees them at once. A
153
+ * record of no trace of its own takes the one withTrace runs the call in.
154
+ */
155
+ async append(records) {
156
+ const carried = currentTrace();
157
+ const list = Array.isArray(records) ? records : [records];
158
+ return this.#write(carried === undefined
159
+ ? list
160
+ : list.map(r => r.traceId === undefined
161
+ ? { ...r, traceId: carried.traceId, spanId: carried.spanId }
162
+ : r));
163
+ }
164
+ async #write(records) {
165
+ const now = BigInt(Date.now()) * nsPerMs;
166
+ await this.#send(records.map(r => ({
167
+ at: r.at === undefined ? now : nanos(r.at),
168
+ stream: r.stream,
169
+ name: r.name,
170
+ level: r.level === undefined ? undefined : levelOf(r.level),
171
+ body: r.body,
172
+ traceId: idOf(r.traceId, 16, 'trace'),
173
+ spanId: idOf(r.spanId, 8, 'span'),
174
+ context: fieldsOf(r.context),
175
+ attrs: fieldsOf(r.attrs),
176
+ })));
177
+ }
178
+ /** Appends a logger's lines, their fields already JSON and their traces taken as each was logged. */
179
+ async #writeLines(lines) {
180
+ await this.#send(lines.map(line => ({
181
+ at: nanos(line.at),
182
+ stream: line.stream,
183
+ name: line.event ?? 'log',
184
+ level: line.level,
185
+ body: line.msg,
186
+ traceId: idOf(line.traceId, 16, 'trace'),
187
+ spanId: idOf(line.spanId, 8, 'span'),
188
+ context: line.context.length === 0 ? undefined : [...line.context],
189
+ attrs: line.attrs.length === 0 ? undefined : [...line.attrs],
190
+ })));
191
+ }
192
+ async #send(records) {
193
+ await this.#link.run('write', connection => connection.session.call(methods['records.append'], RecordsBatch.encode({ records })));
194
+ }
195
+ /**
196
+ * One page of the records a query matches, from one snapshot, in event-time
197
+ * order; next, while a limit or a budget ended the page early, is passed back
198
+ * as `after` with the same query. A page never splits a timestamp.
199
+ */
200
+ async scan(query) {
201
+ const asked = queryOf(query);
202
+ const got = await this.#link.run('read', connection => download(connection, methods['records.read'], QueryMessage.encode(asked)));
203
+ const page = RecordsPage.decode(got.trailer);
204
+ const items = got.items.map(item => recordOf(RecordsRecord.decode(item)));
205
+ if (page.more !== true) {
206
+ return { items, next: undefined };
207
+ }
208
+ const moved = (end, sent) => end !== undefined ? BigInt(end) : sent !== undefined ? BigInt(sent) : undefined;
209
+ return { items, next: cursorOf(moved(page.from, asked.from), moved(page.to, asked.to)) };
210
+ }
211
+ /** Every record a query matches, a page at a time. */
212
+ async *all(query) {
213
+ let page = await this.scan(query);
214
+ yield* page.items;
215
+ while (page.next !== undefined) {
216
+ page = await this.scan({ ...query, after: page.next });
217
+ yield* page.items;
218
+ }
219
+ }
220
+ /**
221
+ * The sealed records after a cursor, in the order they were sealed; the
222
+ * cursor to follow from next, and the segments retention removed before
223
+ * the cursor reached them. A record reaches a follower once it is sealed.
224
+ */
225
+ async follow(cursor, limit) {
226
+ const got = await this.#link.run('read', connection => download(connection, methods['records.follow'], RecordsCursor.encode({ segment: cursor?.segment, row: cursor?.row, limit })));
227
+ const trailer = RecordsCursor.decode(got.trailer);
228
+ return {
229
+ records: got.items.map(item => recordOf(RecordsRecord.decode(item))),
230
+ cursor: { segment: trailer.segment ?? 0, row: trailer.row ?? 0 },
231
+ expired: trailer.expired ?? 0,
232
+ };
233
+ }
234
+ /**
235
+ * A writer of another program's output, pino's lines or a child's stdout,
236
+ * that never waits: each line becomes a record of the stream as the server's
237
+ * Lines makes it, a stack trace's lines joined and a JSON or logfmt line's
238
+ * fields kept. What does not fit its buffer is dropped and counted.
239
+ */
240
+ lines(stream, options) {
241
+ if (stream === '') {
242
+ throw new InvalidError('lines of no stream');
243
+ }
244
+ return new LinesWriter(this.#link, stream, options?.buffer ?? 1 << 20);
245
+ }
246
+ /** The rows the server's records have met that no longer read. */
247
+ async damaged() {
248
+ const body = await this.#link.run('read', connection => connection.session.call(methods['records.damaged'], Empty.encode({})));
249
+ return (RecordsDamages.decode(body).damages ?? []).map(d => ({
250
+ stream: textOf(d.stream),
251
+ segment: d.segment,
252
+ headRow: d.headRow,
253
+ from: d.from ?? 0n,
254
+ to: d.to ?? 0n,
255
+ reason: d.reason ?? '',
256
+ }));
257
+ }
258
+ /** Removes a damaged row, a repair an admin connection alone may make. */
259
+ async drop(damage) {
260
+ await this.#link.run('write', connection => connection.session.call(methods['records.drop'], RecordsDamage.encode(damage)));
261
+ }
262
+ }
263
+ /** The most one DATA of lines carries. */
264
+ const linesChunk = 64 << 10;
265
+ /**
266
+ * An upload of lines that stays open while it is written: write never waits,
267
+ * holding what the stream's credit does not let go yet up to its buffer and
268
+ * dropping past it. A connection lost drops what it held and the next write
269
+ * uploads on the next connection.
270
+ */
271
+ export class LinesWriter {
272
+ #link;
273
+ #stream;
274
+ #most;
275
+ #waiting = [];
276
+ #held = 0;
277
+ #pumping;
278
+ #upload;
279
+ #ended = false;
280
+ /** bytes dropped since the writer began: a full buffer, or a lost connection */
281
+ dropped = 0;
282
+ constructor(link, stream, most) {
283
+ this.#link = link;
284
+ this.#stream = stream;
285
+ this.#most = most;
286
+ }
287
+ /** Takes a piece of output, cut anywhere. It returns at once. */
288
+ write(chunk) {
289
+ if (this.#ended) {
290
+ throw new InvalidError('a write after the lines ended');
291
+ }
292
+ const bytes = typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk;
293
+ if (bytes.length === 0) {
294
+ return true;
295
+ }
296
+ if (this.#held + bytes.length > this.#most) {
297
+ this.dropped += bytes.length;
298
+ return false;
299
+ }
300
+ for (let at = 0; at < bytes.length; at += linesChunk) {
301
+ this.#waiting.push(bytes.subarray(at, at + linesChunk));
302
+ }
303
+ this.#held += bytes.length;
304
+ this.#pumping ??= this.#pump().finally(() => {
305
+ this.#pumping = undefined;
306
+ });
307
+ return true;
308
+ }
309
+ async #pump() {
310
+ while (this.#waiting.length > 0) {
311
+ const piece = this.#waiting[0];
312
+ try {
313
+ const upload = await this.#open();
314
+ await upload.send(piece, false);
315
+ }
316
+ catch {
317
+ this.#upload = undefined;
318
+ this.dropped += this.#held;
319
+ this.#waiting = [];
320
+ this.#held = 0;
321
+ return;
322
+ }
323
+ this.#waiting.shift();
324
+ this.#held -= piece.length;
325
+ }
326
+ }
327
+ async #open() {
328
+ if (this.#upload !== undefined && !this.#upload.finished) {
329
+ return this.#upload;
330
+ }
331
+ const connection = await this.#link.connection();
332
+ this.#upload = await connection.session.open(methods['records.lines'], RecordsStream.encode({ stream: this.#stream }), false);
333
+ return this.#upload;
334
+ }
335
+ /** Hands over what the writer holds, a line cut short included, and ends the upload. */
336
+ async end() {
337
+ this.#ended = true;
338
+ await this.#pumping;
339
+ const upload = this.#upload;
340
+ if (upload === undefined || upload.finished) {
341
+ return;
342
+ }
343
+ await upload.send(new Uint8Array(0), true);
344
+ await upload.next();
345
+ }
346
+ }
@@ -0,0 +1,2 @@
1
+ import type { Runtime } from '../runtime.ts';
2
+ export declare const bunRuntime: Runtime;
@@ -0,0 +1,164 @@
1
+ // The runtime of Bun: sockets through Bun.connect, a Windows named pipe
2
+ // through node:net, which Bun implements, and a private child through
3
+ // Bun.spawn. Nothing here touches Bun until it is called, so that Node can
4
+ // load this file beside its own.
5
+ import net from 'node:net';
6
+ import manifest from '../../package.json' with { type: 'json' };
7
+ import { ClosedError } from '../errors.js';
8
+ import { address, exitedWith, spawnDetached, stderrKept } from './shared.js';
9
+ export const bunRuntime = {
10
+ get client() {
11
+ return `tinystore-js/${manifest.version} bun/${Bun.version}`;
12
+ },
13
+ windows: process.platform === 'win32',
14
+ connect,
15
+ spawnPrivate,
16
+ spawnDetached,
17
+ readFile: async (path) => {
18
+ const file = Bun.file(path);
19
+ try {
20
+ return await file.bytes();
21
+ }
22
+ catch (err) {
23
+ if (err.code === 'ENOENT') {
24
+ return undefined;
25
+ }
26
+ throw err;
27
+ }
28
+ },
29
+ which: name => Bun.which(name) ?? undefined,
30
+ packagedBinary,
31
+ };
32
+ async function connect(endpoint, events, tls) {
33
+ const at = address(endpoint);
34
+ switch (at.kind) {
35
+ case 'pipe':
36
+ return connectPipe(at.path, events);
37
+ case 'unix': {
38
+ const path = at.path;
39
+ return connectSocket(socket => Bun.connect({ unix: path, socket }), events);
40
+ }
41
+ case 'tcp': {
42
+ const { host, port } = at;
43
+ return connectSocket(socket => Bun.connect({ hostname: host, port, socket }), events);
44
+ }
45
+ case 'tls': {
46
+ const { host, port } = at;
47
+ const verify = {
48
+ serverName: tls?.serverName ?? host,
49
+ ...(tls?.ca === undefined ? {} : { ca: tls.ca }),
50
+ };
51
+ return connectSocket(socket => Bun.connect({ hostname: host, port, tls: verify, socket }), events);
52
+ }
53
+ }
54
+ }
55
+ /**
56
+ * A socket's write may take part of the bytes: the rest waits for its drain,
57
+ * in order, so the session hands a whole frame over and forgets it.
58
+ */
59
+ async function connectSocket(dial, events) {
60
+ const waiting = [];
61
+ let ended = false;
62
+ const end = (err) => {
63
+ if (!ended) {
64
+ ended = true;
65
+ events.end(err);
66
+ }
67
+ };
68
+ let socket;
69
+ const pump = () => {
70
+ while (socket !== undefined && waiting.length > 0) {
71
+ const bytes = waiting[0];
72
+ const wrote = socket.write(bytes);
73
+ if (wrote === bytes.length) {
74
+ waiting.shift();
75
+ continue;
76
+ }
77
+ waiting[0] = bytes.subarray(Math.max(wrote, 0));
78
+ return;
79
+ }
80
+ };
81
+ socket = await dial({
82
+ data: (_, data) => events.data(data),
83
+ drain: () => pump(),
84
+ end: () => end(new ClosedError('the server ended the connection')),
85
+ close: (_, err) => end(err ?? new ClosedError('the connection closed')),
86
+ error: (_, err) => end(err),
87
+ });
88
+ return {
89
+ write: bytes => {
90
+ waiting.push(bytes);
91
+ if (waiting.length === 1) {
92
+ pump();
93
+ }
94
+ },
95
+ close: () => socket?.end(),
96
+ };
97
+ }
98
+ function connectPipe(path, events) {
99
+ return new Promise((resolve, reject) => {
100
+ const socket = net.createConnection(path);
101
+ let connected = false;
102
+ let ended = false;
103
+ const end = (err) => {
104
+ if (!ended) {
105
+ ended = true;
106
+ events.end(err);
107
+ }
108
+ };
109
+ socket.once('connect', () => {
110
+ connected = true;
111
+ resolve({ write: bytes => socket.write(bytes), close: () => socket.end() });
112
+ });
113
+ socket.on('data', (data) => events.data(data));
114
+ socket.on('error', err => (connected ? end(err) : reject(err)));
115
+ socket.on('close', () => end(new ClosedError('the pipe closed')));
116
+ });
117
+ }
118
+ function spawnPrivate(argv, events) {
119
+ const proc = Bun.spawn(argv, { stdin: 'pipe', stdout: 'pipe', stderr: 'pipe', windowsHide: true });
120
+ let kept = '';
121
+ // a pipe nobody drains stops the server once it fills, so stderr is read to its end
122
+ const draining = (async () => {
123
+ const decoder = new TextDecoder();
124
+ for await (const chunk of proc.stderr) {
125
+ kept = (kept + decoder.decode(chunk, { stream: true })).slice(-stderrKept);
126
+ }
127
+ })();
128
+ void (async () => {
129
+ try {
130
+ for await (const chunk of proc.stdout) {
131
+ events.data(chunk);
132
+ }
133
+ }
134
+ finally {
135
+ await draining.catch(() => { });
136
+ events.end(exitedWith(await proc.exited, kept));
137
+ }
138
+ })();
139
+ return {
140
+ transport: {
141
+ write: bytes => {
142
+ proc.stdin.write(bytes);
143
+ void proc.stdin.flush();
144
+ },
145
+ close: () => {
146
+ void proc.stdin.end();
147
+ },
148
+ },
149
+ exited: proc.exited,
150
+ kill: () => proc.kill(),
151
+ stderr: () => kept,
152
+ };
153
+ }
154
+ /** The platform's package of the server binary, installed beside this one. */
155
+ function packagedBinary() {
156
+ const name = `@tinyshed/tinystore-${process.platform}-${process.arch}`;
157
+ const binary = process.platform === 'win32' ? 'tinystore.exe' : 'tinystore';
158
+ try {
159
+ return Bun.resolveSync(`${name}/bin/${binary}`, import.meta.dir);
160
+ }
161
+ catch {
162
+ return undefined;
163
+ }
164
+ }
@@ -0,0 +1,2 @@
1
+ import type { Runtime } from '../runtime.ts';
2
+ export declare const nodeRuntime: Runtime;
@@ -0,0 +1,157 @@
1
+ // The runtime of Node: every transport through node:net and node:tls, a
2
+ // Windows named pipe and a Unix socket included, and a private child through
3
+ // node:child_process.
4
+ import { spawn } from 'node:child_process';
5
+ import { accessSync, constants, statSync } from 'node:fs';
6
+ import { readFile } from 'node:fs/promises';
7
+ import { createRequire } from 'node:module';
8
+ import net from 'node:net';
9
+ import { delimiter, join } from 'node:path';
10
+ import tls from 'node:tls';
11
+ import manifest from '../../package.json' with { type: 'json' };
12
+ import { ClosedError } from '../errors.js';
13
+ import { address, exitedWith, spawnDetached, stderrKept } from './shared.js';
14
+ export const nodeRuntime = {
15
+ client: `tinystore-js/${manifest.version} node/${process.versions.node}`,
16
+ windows: process.platform === 'win32',
17
+ connect,
18
+ spawnPrivate,
19
+ spawnDetached,
20
+ readFile: async (path) => {
21
+ try {
22
+ return await readFile(path);
23
+ }
24
+ catch (err) {
25
+ if (err.code === 'ENOENT') {
26
+ return undefined;
27
+ }
28
+ throw err;
29
+ }
30
+ },
31
+ which,
32
+ packagedBinary,
33
+ };
34
+ function connect(endpoint, events, options) {
35
+ const at = address(endpoint);
36
+ switch (at.kind) {
37
+ case 'pipe':
38
+ case 'unix':
39
+ return connected(net.createConnection(at.path), 'connect', events);
40
+ case 'tcp':
41
+ return connected(net.createConnection({ host: at.host, port: at.port, noDelay: true }), 'connect', events);
42
+ case 'tls':
43
+ return connected(connectTLS(at.host, at.port, options), 'secureConnect', events);
44
+ }
45
+ }
46
+ /**
47
+ * A TLS socket checking the certificate against the name asked for, or the
48
+ * host, and trusting the given certificates beside Node's own: Node's ca
49
+ * replaces them otherwise.
50
+ */
51
+ function connectTLS(host, port, options) {
52
+ const name = options?.serverName ?? host;
53
+ const given = options?.ca === undefined ? [] : [options.ca].flat();
54
+ const socket = tls.connect({
55
+ host,
56
+ port,
57
+ // a server name is a DNS name: one that is an address is checked, not sent
58
+ ...(net.isIP(name) === 0 ? { servername: name } : {}),
59
+ checkServerIdentity: (_, certificate) => tls.checkServerIdentity(name, certificate),
60
+ ...(given.length === 0 ? {} : { ca: [...tls.rootCertificates, ...given] }),
61
+ });
62
+ return socket.setNoDelay(true);
63
+ }
64
+ /**
65
+ * A transport once the socket is ready: a socket's write queues what the
66
+ * kernel does not take yet, in order, so the session hands a whole frame over
67
+ * and forgets it.
68
+ */
69
+ function connected(socket, ready, events) {
70
+ return new Promise((resolve, reject) => {
71
+ let open = false;
72
+ let ended = false;
73
+ const end = (err) => {
74
+ if (!ended) {
75
+ ended = true;
76
+ events.end(err);
77
+ }
78
+ };
79
+ socket.once(ready, () => {
80
+ open = true;
81
+ resolve({ write: bytes => void socket.write(bytes), close: () => void socket.end() });
82
+ });
83
+ socket.on('data', (data) => events.data(data));
84
+ socket.on('error', err => (open ? end(err) : reject(err)));
85
+ socket.on('end', () => end(new ClosedError('the server ended the connection')));
86
+ socket.on('close', () => end(new ClosedError('the connection closed')));
87
+ });
88
+ }
89
+ function spawnPrivate(argv, events) {
90
+ const proc = spawn(argv[0], argv.slice(1), { stdio: 'pipe', windowsHide: true });
91
+ let kept = '';
92
+ // a pipe nobody drains stops the server once it fills, so stderr is read to its end
93
+ proc.stderr.setEncoding('utf8');
94
+ proc.stderr.on('data', (text) => {
95
+ kept = (kept + text).slice(-stderrKept);
96
+ });
97
+ proc.stdout.on('data', (data) => events.data(data));
98
+ // a write to a child that has exited fails here; its exit says why
99
+ proc.stdin.on('error', () => { });
100
+ // close comes once the child has exited and its stdout and stderr have ended
101
+ const exited = new Promise(resolve => {
102
+ proc.once('close', code => resolve(code ?? -1));
103
+ proc.once('error', err => {
104
+ kept += err.message;
105
+ resolve(-1);
106
+ });
107
+ });
108
+ void exited.then(code => events.end(exitedWith(code, kept)));
109
+ return {
110
+ transport: {
111
+ write: bytes => void proc.stdin.write(bytes),
112
+ close: () => void proc.stdin.end(),
113
+ },
114
+ exited,
115
+ kill: () => void proc.kill(),
116
+ stderr: () => kept,
117
+ };
118
+ }
119
+ /** The executable a name finds on PATH, with an extension of PATHEXT on Windows. */
120
+ function which(name) {
121
+ const windows = process.platform === 'win32';
122
+ const extensions = windows ? (process.env.PATHEXT ?? '.EXE;.CMD;.BAT;.COM').split(';') : [''];
123
+ for (const dir of (process.env.PATH ?? '').split(delimiter)) {
124
+ for (const extension of dir === '' ? [] : extensions) {
125
+ const path = join(dir, name + extension);
126
+ if (executable(path, windows)) {
127
+ return path;
128
+ }
129
+ }
130
+ }
131
+ return undefined;
132
+ }
133
+ function executable(path, windows) {
134
+ try {
135
+ if (!statSync(path).isFile()) {
136
+ return false;
137
+ }
138
+ if (!windows) {
139
+ accessSync(path, constants.X_OK);
140
+ }
141
+ return true;
142
+ }
143
+ catch {
144
+ return false;
145
+ }
146
+ }
147
+ /** The platform's package of the server binary, installed beside this one. */
148
+ function packagedBinary() {
149
+ const name = `@tinyshed/tinystore-${process.platform}-${process.arch}`;
150
+ const binary = process.platform === 'win32' ? 'tinystore.exe' : 'tinystore';
151
+ try {
152
+ return createRequire(import.meta.url).resolve(`${name}/bin/${binary}`);
153
+ }
154
+ catch {
155
+ return undefined;
156
+ }
157
+ }
@@ -0,0 +1,25 @@
1
+ import { ClosedError } from '../errors.ts';
2
+ import type { Child } from '../runtime.ts';
3
+ /** Where an endpoint named in SERVE, or given to connect, is. */
4
+ export type Address = {
5
+ kind: 'unix';
6
+ path: string;
7
+ } | {
8
+ kind: 'pipe';
9
+ path: string;
10
+ } | {
11
+ kind: 'tcp' | 'tls';
12
+ host: string;
13
+ port: number;
14
+ };
15
+ export declare function address(endpoint: string): Address;
16
+ /**
17
+ * Starts a sidecar that outlives this process, through node:child_process in
18
+ * Bun too, since Bun.spawn starts no process that outlives a Ctrl-C of its
19
+ * parent's group.
20
+ */
21
+ export declare function spawnDetached(argv: string[]): Child;
22
+ /** How much of a child's stderr is kept, its last lines, to say why it ended. */
23
+ export declare const stderrKept: number;
24
+ export declare function lastLines(text: string, n?: number): string;
25
+ export declare function exitedWith(code: number, stderr: string): ClosedError;