@tinyshed/tinystore 0.1.0 → 0.2.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.
- package/dist/blobs.d.ts +1 -1
- package/dist/blobs.js +13 -5
- package/dist/cancel.d.ts +16 -2
- package/dist/cancel.js +36 -2
- package/dist/channels.js +20 -6
- package/dist/config.d.ts +49 -1
- package/dist/config.js +86 -7
- package/dist/connection.d.ts +52 -10
- package/dist/connection.js +305 -73
- package/dist/handles.d.ts +10 -1
- package/dist/handles.js +27 -4
- package/dist/index.d.ts +5 -5
- package/dist/jobs.d.ts +35 -1
- package/dist/jobs.js +46 -0
- package/dist/logger.d.ts +22 -10
- package/dist/logger.js +118 -28
- package/dist/metrics.d.ts +24 -1
- package/dist/metrics.js +24 -1
- package/dist/query.js +33 -9
- package/dist/records.d.ts +48 -7
- package/dist/records.js +22 -3
- package/dist/runtime/addon.js +3 -2
- package/dist/runtime/bun.js +33 -12
- package/dist/runtime/node.js +30 -8
- package/dist/runtime/pipe.d.ts +2 -0
- package/dist/runtime/pipe.js +45 -20
- package/dist/runtime/shared.d.ts +28 -4
- package/dist/runtime/shared.js +125 -13
- package/dist/runtime.d.ts +28 -6
- package/dist/session.d.ts +9 -5
- package/dist/session.js +113 -31
- package/dist/sql.d.ts +18 -5
- package/dist/sql.js +44 -11
- package/dist/store.d.ts +25 -11
- package/dist/store.js +67 -9
- package/dist/time.d.ts +2 -0
- package/dist/wire/codec.d.ts +5 -0
- package/dist/wire/codec.js +10 -0
- package/dist/wire/frame.d.ts +2 -0
- package/dist/wire/frame.js +4 -0
- package/dist/wire/msgpack.d.ts +7 -3
- package/dist/wire/msgpack.js +11 -4
- package/dist/wire/protocol.d.ts +280 -36
- package/dist/wire/protocol.js +510 -102
- package/package.json +9 -8
- package/src/blobs.ts +13 -5
- package/src/cancel.ts +44 -2
- package/src/channels.ts +21 -6
- package/src/config.ts +138 -12
- package/src/connection.ts +377 -77
- package/src/handles.ts +32 -4
- package/src/index.ts +6 -1
- package/src/jobs.ts +66 -0
- package/src/logger.ts +158 -40
- package/src/metrics.ts +53 -2
- package/src/query.ts +33 -13
- package/src/records.ts +73 -10
- package/src/runtime/addon.ts +3 -2
- package/src/runtime/bun.ts +44 -9
- package/src/runtime/node.ts +30 -5
- package/src/runtime/pipe.ts +49 -19
- package/src/runtime/shared.ts +137 -16
- package/src/runtime.ts +31 -6
- package/src/session.ts +129 -30
- package/src/sql.ts +48 -14
- package/src/store.ts +81 -14
- package/src/time.ts +3 -0
- package/src/wire/codec.ts +11 -0
- package/src/wire/frame.ts +5 -0
- package/src/wire/msgpack.ts +11 -4
- package/src/wire/protocol.ts +532 -104
package/src/query.ts
CHANGED
|
@@ -868,18 +868,31 @@ function render(parts: Parts): Sql {
|
|
|
868
868
|
return new Sql(text, values)
|
|
869
869
|
}
|
|
870
870
|
|
|
871
|
-
/**
|
|
871
|
+
/**
|
|
872
|
+
* The condition that starts a page after a row's order values. SQLite orders
|
|
873
|
+
* a null before every value, first in ascending order and last in descending,
|
|
874
|
+
* and a comparison with a null is never true: equal is `is`, and past a null
|
|
875
|
+
* is every value ascending and nothing descending.
|
|
876
|
+
*/
|
|
872
877
|
function keyset(order: Parts['order'], after: readonly SqlValue[]): Sql {
|
|
873
878
|
const alternatives: string[] = []
|
|
874
879
|
const values: SqlArg[] = []
|
|
875
880
|
order.forEach((each, at) => {
|
|
876
|
-
const
|
|
881
|
+
const column = each.piece.text
|
|
882
|
+
const value = after[at] ?? null
|
|
883
|
+
if (value === null && each.direction === 'desc') {
|
|
884
|
+
return
|
|
885
|
+
}
|
|
886
|
+
const equal = order.slice(0, at).map(earlier => `${earlier.piece.text} is ?`)
|
|
877
887
|
values.push(...after.slice(0, at))
|
|
878
|
-
|
|
879
|
-
|
|
888
|
+
let past = `${column} is not null`
|
|
889
|
+
if (value !== null) {
|
|
890
|
+
past = each.direction === 'asc' ? `${column} > ?` : `(${column} < ? or ${column} is null)`
|
|
891
|
+
values.push(value)
|
|
892
|
+
}
|
|
880
893
|
alternatives.push(equal.length === 0 ? past : `(${[...equal, past].join(' and ')})`)
|
|
881
894
|
})
|
|
882
|
-
return new Sql(`(${alternatives.join(' or ')})`, values)
|
|
895
|
+
return new Sql(alternatives.length === 0 ? '0' : `(${alternatives.join(' or ')})`, values)
|
|
883
896
|
}
|
|
884
897
|
|
|
885
898
|
/** A short digest of a query, which a cursor carries so that another query refuses it. */
|
|
@@ -893,10 +906,14 @@ function digestOf(statement: Sql): string {
|
|
|
893
906
|
return hash.toString(36)
|
|
894
907
|
}
|
|
895
908
|
|
|
909
|
+
/** A cursor of order values: a bigint, and an infinity JSON has no number for, as their text. */
|
|
896
910
|
function cursorText(values: readonly SqlValue[], digest: string): string {
|
|
897
|
-
const json = JSON.stringify({ q: digest, v: values }, (_, v) =>
|
|
898
|
-
typeof v === 'bigint'
|
|
899
|
-
|
|
911
|
+
const json = JSON.stringify({ q: digest, v: values }, (_, v) => {
|
|
912
|
+
if (typeof v === 'bigint') {
|
|
913
|
+
return { n: String(v) }
|
|
914
|
+
}
|
|
915
|
+
return typeof v === 'number' && !Number.isFinite(v) ? { r: String(v) } : v
|
|
916
|
+
})
|
|
900
917
|
return Buffer.from(json).toString('base64url')
|
|
901
918
|
}
|
|
902
919
|
|
|
@@ -912,11 +929,14 @@ function cursorOf(cursor: string, digest: string): SqlValue[] {
|
|
|
912
929
|
"a cursor of another query: a page's next reads only the query that made it",
|
|
913
930
|
)
|
|
914
931
|
}
|
|
915
|
-
return read.v.map(v =>
|
|
916
|
-
typeof v === 'object' && v !== null && 'n' in v
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
932
|
+
return read.v.map(v => {
|
|
933
|
+
if (typeof v === 'object' && v !== null && 'n' in v) {
|
|
934
|
+
return BigInt((v as { n: string }).n)
|
|
935
|
+
}
|
|
936
|
+
return typeof v === 'object' && v !== null && 'r' in v
|
|
937
|
+
? Number((v as { r: string }).r)
|
|
938
|
+
: (v as SqlValue)
|
|
939
|
+
})
|
|
920
940
|
}
|
|
921
941
|
|
|
922
942
|
/** An insert of rows of the same columns, the first row's. */
|
package/src/records.ts
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
newStoreLogger,
|
|
19
19
|
} from './logger.ts'
|
|
20
20
|
import type { Stream } from './session.ts'
|
|
21
|
-
import { type Duration, type DurationText, ms } from './time.ts'
|
|
21
|
+
import { type Calendar, type Duration, type DurationText, ms } from './time.ts'
|
|
22
22
|
import { currentTrace, type Trace } from './trace.ts'
|
|
23
23
|
import {
|
|
24
24
|
Empty,
|
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
methods,
|
|
27
27
|
RecordsAggregateAsked,
|
|
28
28
|
RecordsAppend,
|
|
29
|
+
RecordsAppended,
|
|
29
30
|
RecordsBatch,
|
|
30
31
|
RecordsCount,
|
|
31
32
|
RecordsDamage,
|
|
@@ -61,6 +62,8 @@ export interface Event {
|
|
|
61
62
|
/** its time, which the file keeps to the nanosecond */
|
|
62
63
|
at: Date
|
|
63
64
|
stream: string
|
|
65
|
+
/** what its append named it by: its stream holds one event of an id */
|
|
66
|
+
id?: string
|
|
64
67
|
/** what happened; `log` for a logger's line */
|
|
65
68
|
name: string
|
|
66
69
|
level?: EventLevel
|
|
@@ -77,6 +80,11 @@ export interface Event {
|
|
|
77
80
|
/** An event to append: its name, what happened, and what it carries beside. */
|
|
78
81
|
export interface NewEvent {
|
|
79
82
|
name: string
|
|
83
|
+
/**
|
|
84
|
+
* what names it in its stream, a webhook's delivery or a job's key, 1 to 128 bytes:
|
|
85
|
+
* an append of an id the stream holds writes nothing, and the first event of an id stands
|
|
86
|
+
*/
|
|
87
|
+
id?: string
|
|
80
88
|
fields?: Fields
|
|
81
89
|
/** its time: now unless given, from what the stream keeps to ten minutes ahead */
|
|
82
90
|
at?: Date
|
|
@@ -157,6 +165,10 @@ export interface Row {
|
|
|
157
165
|
export interface Aggregate extends Counted {
|
|
158
166
|
/** the span of a row: the range is one without it */
|
|
159
167
|
every?: Duration
|
|
168
|
+
/** rows of a calendar in timeZone, in place of every: 'day' begins each at a local midnight */
|
|
169
|
+
calendar?: Calendar
|
|
170
|
+
/** the IANA zone a calendar's rows are in: 'Europe/Moscow', 'UTC' */
|
|
171
|
+
timeZone?: string
|
|
160
172
|
/** keys whose every value has rows of its own */
|
|
161
173
|
by?: readonly string[]
|
|
162
174
|
/** fields whose numbers a row sums */
|
|
@@ -238,6 +250,9 @@ abstract class Reads {
|
|
|
238
250
|
body: (handle: number | undefined) => Uint8Array,
|
|
239
251
|
): Promise<Stream>
|
|
240
252
|
|
|
253
|
+
/** Keeps a stream that follows the store for its close to cancel; it returns what lets it go. */
|
|
254
|
+
protected abstract follow(stream: Stream): () => void
|
|
255
|
+
|
|
241
256
|
/** One page, from one moment of the file: `{ events, next }`. */
|
|
242
257
|
async list(query?: Query): Promise<Page> {
|
|
243
258
|
const asked = queryOf(query)
|
|
@@ -289,12 +304,14 @@ abstract class Reads {
|
|
|
289
304
|
|
|
290
305
|
/** A row for each span of `every` and each value of the keys of `by`, with its count. */
|
|
291
306
|
async aggregate(query: Aggregate = {}): Promise<Row[]> {
|
|
292
|
-
const { every, by, stats, distinct, ...counted } = query
|
|
307
|
+
const { every, calendar, timeZone, by, stats, distinct, ...counted } = query
|
|
293
308
|
const asked = queryOf(counted)
|
|
294
309
|
const answer = await this.ask('records.aggregate', handle =>
|
|
295
310
|
RecordsAggregateAsked.encode({
|
|
296
311
|
query: { ...asked, handle },
|
|
297
312
|
every: every === undefined ? undefined : ms(every),
|
|
313
|
+
calendar,
|
|
314
|
+
timeZone,
|
|
298
315
|
by: by === undefined ? undefined : [...by],
|
|
299
316
|
stats: stats === undefined ? undefined : [...stats],
|
|
300
317
|
distinct: distinct === undefined ? undefined : [...distinct],
|
|
@@ -338,6 +355,7 @@ abstract class Reads {
|
|
|
338
355
|
const stream = await this.opened('records.tail', handle =>
|
|
339
356
|
RecordsQuery.encode({ ...asked, handle }),
|
|
340
357
|
)
|
|
358
|
+
const unfollow = this.follow(stream)
|
|
341
359
|
try {
|
|
342
360
|
for (;;) {
|
|
343
361
|
const event = await stream.next()
|
|
@@ -350,6 +368,7 @@ abstract class Reads {
|
|
|
350
368
|
}
|
|
351
369
|
} finally {
|
|
352
370
|
stream.cancel()
|
|
371
|
+
unfollow()
|
|
353
372
|
}
|
|
354
373
|
}
|
|
355
374
|
}
|
|
@@ -379,23 +398,27 @@ export class Events extends Reads {
|
|
|
379
398
|
|
|
380
399
|
/**
|
|
381
400
|
* Writes an event, or a list of them whole or not at all: it returns once
|
|
382
|
-
* they are written, and a read finds them at once.
|
|
383
|
-
*
|
|
401
|
+
* they are written, and a read finds them at once. An event whose id the
|
|
402
|
+
* stream holds, or an earlier event of the list has, is written as
|
|
403
|
+
* nothing: one event answers whether it was written, a list how many of
|
|
404
|
+
* its events were. A refused event names its place in the list and its name.
|
|
384
405
|
*/
|
|
385
|
-
append(name: string, fields?: Fields, options?: AppendOptions): Promise<
|
|
386
|
-
append(events: readonly NewEvent[]): Promise<
|
|
406
|
+
append(name: string, fields?: Fields, options?: AppendOptions): Promise<boolean>
|
|
407
|
+
append(events: readonly NewEvent[]): Promise<number>
|
|
387
408
|
async append(
|
|
388
409
|
first: string | readonly NewEvent[],
|
|
389
410
|
fields?: Fields,
|
|
390
411
|
options?: AppendOptions,
|
|
391
|
-
): Promise<
|
|
412
|
+
): Promise<boolean | number> {
|
|
392
413
|
const events = typeof first === 'string' ? [oneEvent(first, fields, options)] : first
|
|
393
414
|
const encoded = events.map(newEventOf)
|
|
394
|
-
await this.#call(
|
|
415
|
+
const answer = await this.#call(
|
|
395
416
|
'records.append',
|
|
396
417
|
handle => RecordsAppend.encode({ handle, events: encoded }),
|
|
397
418
|
'write',
|
|
398
419
|
)
|
|
420
|
+
const written = events.length - (RecordsAppended.decode(answer).held ?? 0)
|
|
421
|
+
return typeof first === 'string' ? written === 1 : written
|
|
399
422
|
}
|
|
400
423
|
|
|
401
424
|
/** A logger's lines, each an event of this stream. */
|
|
@@ -452,6 +475,10 @@ export class Events extends Reads {
|
|
|
452
475
|
})
|
|
453
476
|
}
|
|
454
477
|
|
|
478
|
+
protected follow(stream: Stream): () => void {
|
|
479
|
+
return this.#link.follow(stream)
|
|
480
|
+
}
|
|
481
|
+
|
|
455
482
|
#call(
|
|
456
483
|
method: Method,
|
|
457
484
|
body: (handle: number) => Uint8Array,
|
|
@@ -550,6 +577,10 @@ export class AllEvents extends Reads {
|
|
|
550
577
|
streamOf(connection, methods[method], body(undefined), true),
|
|
551
578
|
)
|
|
552
579
|
}
|
|
580
|
+
|
|
581
|
+
protected follow(stream: Stream): () => void {
|
|
582
|
+
return this.#link.follow(stream)
|
|
583
|
+
}
|
|
553
584
|
}
|
|
554
585
|
|
|
555
586
|
/** A writer's stream, and the bytes a piece of it carries. */
|
|
@@ -713,10 +744,35 @@ function linesIn(bytes: Uint8Array): number {
|
|
|
713
744
|
return lines
|
|
714
745
|
}
|
|
715
746
|
|
|
716
|
-
/** What a store's logger is told: a logger's options,
|
|
717
|
-
export interface StoreLoggerOptions extends LoggerOptions {
|
|
747
|
+
/** What a store's logger is told: a logger's options, how long its stream keeps, and the levels it keeps and prints. */
|
|
748
|
+
export interface StoreLoggerOptions extends Omit<LoggerOptions, 'level'> {
|
|
718
749
|
/** how long the stream keeps its events, as store.events takes it */
|
|
719
750
|
keep?: Duration
|
|
751
|
+
/**
|
|
752
|
+
* the least level kept and printed, every level when absent, or each
|
|
753
|
+
* apart: `{ store: 'info', console: 'warn' }` keeps every line from info
|
|
754
|
+
* and prints warnings and errors. LOG_LEVEL wins over the level kept, and
|
|
755
|
+
* LOG_CONSOLE_LEVEL over the level printed
|
|
756
|
+
*/
|
|
757
|
+
level?: Level | Levels
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* The least levels a store's logger keeps and prints, each part left out as
|
|
762
|
+
* the option is: no store keeps every level, and no console prints what is
|
|
763
|
+
* kept, following the config `log`'s changes of it.
|
|
764
|
+
*
|
|
765
|
+
* ```ts
|
|
766
|
+
* { level: 'info' } // keeps info+, prints info+
|
|
767
|
+
* { level: { store: 'info', console: 'warn' } } // keeps info+, prints warn+
|
|
768
|
+
* { level: { console: 'warn' } } // keeps every level, prints warn+
|
|
769
|
+
* ```
|
|
770
|
+
*/
|
|
771
|
+
export interface Levels {
|
|
772
|
+
/** the least level kept; LOG_LEVEL and the config `log` win over it */
|
|
773
|
+
store?: Level
|
|
774
|
+
/** the least level printed; LOG_CONSOLE_LEVEL wins over it */
|
|
775
|
+
console?: Level
|
|
720
776
|
}
|
|
721
777
|
|
|
722
778
|
/**
|
|
@@ -755,8 +811,12 @@ function newEventOf(event: NewEvent, place: number): Parameters<typeof RecordsNe
|
|
|
755
811
|
event.traceId === undefined
|
|
756
812
|
? traceOf(currentTrace())
|
|
757
813
|
: { traceId: event.traceId, spanId: event.spanId }
|
|
814
|
+
if (event.id !== undefined && (typeof event.id !== 'string' || event.id === '')) {
|
|
815
|
+
throw new InvalidError(`${named}: an id is a string of 1 to 128 bytes`)
|
|
816
|
+
}
|
|
758
817
|
return {
|
|
759
818
|
name: event.name,
|
|
819
|
+
id: event.id,
|
|
760
820
|
at: event.at === undefined ? undefined : nanosOf(event.at),
|
|
761
821
|
level: event.level === undefined ? undefined : levelNumber(event.level, named),
|
|
762
822
|
message: event.message,
|
|
@@ -815,6 +875,9 @@ function eventOf(read: ReturnType<typeof RecordsEvent.decode>): Event {
|
|
|
815
875
|
if (read.message !== undefined) {
|
|
816
876
|
event.message = read.message
|
|
817
877
|
}
|
|
878
|
+
if (read.id !== undefined) {
|
|
879
|
+
event.id = read.id
|
|
880
|
+
}
|
|
818
881
|
if (read.traceId !== undefined) {
|
|
819
882
|
event.traceId = Buffer.from(read.traceId).toString('hex')
|
|
820
883
|
}
|
package/src/runtime/addon.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { createRequire } from 'node:module'
|
|
|
7
7
|
|
|
8
8
|
import { ClosedError } from '../errors.ts'
|
|
9
9
|
import type { Transport } from '../runtime.ts'
|
|
10
|
+
import { openFailure, turning } from './shared.ts'
|
|
10
11
|
|
|
11
12
|
/** What tinystore.node exports: crates/ffi built with `node`. */
|
|
12
13
|
interface Addon {
|
|
@@ -82,10 +83,10 @@ export function openAddon(
|
|
|
82
83
|
}
|
|
83
84
|
})
|
|
84
85
|
} catch (err) {
|
|
85
|
-
throw
|
|
86
|
+
throw openFailure(dir, (err as Error).message)
|
|
86
87
|
}
|
|
87
88
|
return {
|
|
88
|
-
write: send,
|
|
89
|
+
write: turning(send),
|
|
89
90
|
close() {
|
|
90
91
|
const closing = pipe
|
|
91
92
|
if (closing === undefined) {
|
package/src/runtime/bun.ts
CHANGED
|
@@ -8,7 +8,14 @@ import net from 'node:net'
|
|
|
8
8
|
import manifest from '../../package.json' with { type: 'json' }
|
|
9
9
|
import { ClosedError } from '../errors.ts'
|
|
10
10
|
import type { PrivateChild, Runtime, TlsOptions, Transport, TransportEvents } from '../runtime.ts'
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
address,
|
|
13
|
+
connecting,
|
|
14
|
+
exitedWith,
|
|
15
|
+
platformPackage,
|
|
16
|
+
spawnDetached,
|
|
17
|
+
stderrKept,
|
|
18
|
+
} from './shared.ts'
|
|
12
19
|
|
|
13
20
|
export const bunRuntime: Runtime = {
|
|
14
21
|
get client() {
|
|
@@ -37,18 +44,19 @@ async function connect(
|
|
|
37
44
|
endpoint: string,
|
|
38
45
|
events: TransportEvents,
|
|
39
46
|
tls?: TlsOptions,
|
|
47
|
+
signal?: AbortSignal,
|
|
40
48
|
): Promise<Transport> {
|
|
41
49
|
const at = address(endpoint)
|
|
42
50
|
switch (at.kind) {
|
|
43
51
|
case 'pipe':
|
|
44
|
-
return connectPipe(at.path, events)
|
|
52
|
+
return connectPipe(at.path, events, signal)
|
|
45
53
|
case 'unix': {
|
|
46
54
|
const path = at.path
|
|
47
|
-
return connectSocket(socket => Bun.connect({ unix: path, socket }), events)
|
|
55
|
+
return connectSocket(socket => Bun.connect({ unix: path, socket }), events, signal)
|
|
48
56
|
}
|
|
49
57
|
case 'tcp': {
|
|
50
58
|
const { host, port } = at
|
|
51
|
-
return connectSocket(socket => Bun.connect({ hostname: host, port, socket }), events)
|
|
59
|
+
return connectSocket(socket => Bun.connect({ hostname: host, port, socket }), events, signal)
|
|
52
60
|
}
|
|
53
61
|
case 'tls': {
|
|
54
62
|
const { host, port } = at
|
|
@@ -59,6 +67,7 @@ async function connect(
|
|
|
59
67
|
return connectSocket(
|
|
60
68
|
socket => Bun.connect({ hostname: host, port, tls: verify, socket }),
|
|
61
69
|
events,
|
|
70
|
+
signal,
|
|
62
71
|
)
|
|
63
72
|
}
|
|
64
73
|
}
|
|
@@ -76,11 +85,14 @@ interface Handlers {
|
|
|
76
85
|
|
|
77
86
|
/**
|
|
78
87
|
* A socket's write may take part of the bytes: the rest waits for its drain,
|
|
79
|
-
* in order, so the session hands a whole frame over and forgets it.
|
|
88
|
+
* in order, so the session hands a whole frame over and forgets it. Bun's
|
|
89
|
+
* connect takes no signal: one that aborts first rejects at once, and the
|
|
90
|
+
* socket connected after is ended.
|
|
80
91
|
*/
|
|
81
92
|
async function connectSocket(
|
|
82
93
|
dial: (handlers: Handlers) => Promise<Socket>,
|
|
83
94
|
events: TransportEvents,
|
|
95
|
+
signal?: AbortSignal,
|
|
84
96
|
): Promise<Transport> {
|
|
85
97
|
const waiting: Uint8Array[] = []
|
|
86
98
|
let ended = false
|
|
@@ -103,13 +115,14 @@ async function connectSocket(
|
|
|
103
115
|
return
|
|
104
116
|
}
|
|
105
117
|
}
|
|
106
|
-
|
|
118
|
+
const dialled = dial({
|
|
107
119
|
data: (_, data) => events.data(data),
|
|
108
120
|
drain: () => pump(),
|
|
109
121
|
end: () => end(new ClosedError('the server ended the connection')),
|
|
110
122
|
close: (_, err) => end(err ?? new ClosedError('the connection closed')),
|
|
111
123
|
error: (_, err) => end(err),
|
|
112
124
|
})
|
|
125
|
+
socket = await connecting(dialled, signal, late => late.end())
|
|
113
126
|
return {
|
|
114
127
|
write: bytes => {
|
|
115
128
|
waiting.push(bytes)
|
|
@@ -121,7 +134,11 @@ async function connectSocket(
|
|
|
121
134
|
}
|
|
122
135
|
}
|
|
123
136
|
|
|
124
|
-
function connectPipe(
|
|
137
|
+
function connectPipe(
|
|
138
|
+
path: string,
|
|
139
|
+
events: TransportEvents,
|
|
140
|
+
signal?: AbortSignal,
|
|
141
|
+
): Promise<Transport> {
|
|
125
142
|
return new Promise((resolve, reject) => {
|
|
126
143
|
const socket = net.createConnection(path)
|
|
127
144
|
let connected = false
|
|
@@ -132,17 +149,35 @@ function connectPipe(path: string, events: TransportEvents): Promise<Transport>
|
|
|
132
149
|
events.end(err)
|
|
133
150
|
}
|
|
134
151
|
}
|
|
152
|
+
const abort = () => {
|
|
153
|
+
reject(signal?.reason)
|
|
154
|
+
socket.destroy()
|
|
155
|
+
}
|
|
156
|
+
if (signal?.aborted) {
|
|
157
|
+
abort()
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
signal?.addEventListener('abort', abort, { once: true })
|
|
135
161
|
socket.once('connect', () => {
|
|
136
162
|
connected = true
|
|
163
|
+
signal?.removeEventListener('abort', abort)
|
|
137
164
|
resolve({ write: bytes => socket.write(bytes), close: () => socket.end() })
|
|
138
165
|
})
|
|
139
166
|
socket.on('data', (data: Uint8Array) => events.data(data))
|
|
140
|
-
socket.on('error', err =>
|
|
167
|
+
socket.on('error', err => {
|
|
168
|
+
if (connected) {
|
|
169
|
+
end(err)
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
signal?.removeEventListener('abort', abort)
|
|
173
|
+
reject(err)
|
|
174
|
+
})
|
|
141
175
|
socket.on('close', () => end(new ClosedError('the pipe closed')))
|
|
142
176
|
})
|
|
143
177
|
}
|
|
144
178
|
|
|
145
|
-
|
|
179
|
+
// async, so that the throw of a Bun.spawn that cannot start is its rejection
|
|
180
|
+
async function spawnPrivate(argv: string[], events: TransportEvents): Promise<PrivateChild> {
|
|
146
181
|
const proc = Bun.spawn(argv, { stdin: 'pipe', stdout: 'pipe', stderr: 'pipe', windowsHide: true })
|
|
147
182
|
let kept = ''
|
|
148
183
|
// a pipe nobody drains stops the server once it fills, so stderr is read to its end
|
package/src/runtime/node.ts
CHANGED
|
@@ -39,20 +39,22 @@ function connect(
|
|
|
39
39
|
endpoint: string,
|
|
40
40
|
events: TransportEvents,
|
|
41
41
|
options?: TlsOptions,
|
|
42
|
+
signal?: AbortSignal,
|
|
42
43
|
): Promise<Transport> {
|
|
43
44
|
const at = address(endpoint)
|
|
44
45
|
switch (at.kind) {
|
|
45
46
|
case 'pipe':
|
|
46
47
|
case 'unix':
|
|
47
|
-
return connected(net.createConnection(at.path), 'connect', events)
|
|
48
|
+
return connected(net.createConnection(at.path), 'connect', events, signal)
|
|
48
49
|
case 'tcp':
|
|
49
50
|
return connected(
|
|
50
51
|
net.createConnection({ host: at.host, port: at.port, noDelay: true }),
|
|
51
52
|
'connect',
|
|
52
53
|
events,
|
|
54
|
+
signal,
|
|
53
55
|
)
|
|
54
56
|
case 'tls':
|
|
55
|
-
return connected(connectTLS(at.host, at.port, options), 'secureConnect', events)
|
|
57
|
+
return connected(connectTLS(at.host, at.port, options), 'secureConnect', events, signal)
|
|
56
58
|
}
|
|
57
59
|
}
|
|
58
60
|
|
|
@@ -78,12 +80,13 @@ function connectTLS(host: string, port: number, options?: TlsOptions): tls.TLSSo
|
|
|
78
80
|
/**
|
|
79
81
|
* A transport once the socket is ready: a socket's write queues what the
|
|
80
82
|
* kernel does not take yet, in order, so the session hands a whole frame over
|
|
81
|
-
* and forgets it.
|
|
83
|
+
* and forgets it. A signal that aborts before then destroys the socket.
|
|
82
84
|
*/
|
|
83
85
|
function connected(
|
|
84
86
|
socket: net.Socket,
|
|
85
87
|
ready: 'connect' | 'secureConnect',
|
|
86
88
|
events: TransportEvents,
|
|
89
|
+
signal?: AbortSignal,
|
|
87
90
|
): Promise<Transport> {
|
|
88
91
|
return new Promise((resolve, reject) => {
|
|
89
92
|
let open = false
|
|
@@ -94,19 +97,41 @@ function connected(
|
|
|
94
97
|
events.end(err)
|
|
95
98
|
}
|
|
96
99
|
}
|
|
100
|
+
const abort = () => {
|
|
101
|
+
reject(signal?.reason)
|
|
102
|
+
socket.destroy()
|
|
103
|
+
}
|
|
104
|
+
if (signal?.aborted) {
|
|
105
|
+
abort()
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
signal?.addEventListener('abort', abort, { once: true })
|
|
97
109
|
socket.once(ready, () => {
|
|
98
110
|
open = true
|
|
111
|
+
signal?.removeEventListener('abort', abort)
|
|
99
112
|
resolve({ write: bytes => void socket.write(bytes), close: () => void socket.end() })
|
|
100
113
|
})
|
|
101
114
|
socket.on('data', (data: Uint8Array) => events.data(data))
|
|
102
|
-
socket.on('error', err =>
|
|
115
|
+
socket.on('error', err => {
|
|
116
|
+
if (open) {
|
|
117
|
+
end(err)
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
signal?.removeEventListener('abort', abort)
|
|
121
|
+
reject(err)
|
|
122
|
+
})
|
|
103
123
|
socket.on('end', () => end(new ClosedError('the server ended the connection')))
|
|
104
124
|
socket.on('close', () => end(new ClosedError('the connection closed')))
|
|
105
125
|
})
|
|
106
126
|
}
|
|
107
127
|
|
|
108
|
-
function spawnPrivate(argv: string[], events: TransportEvents): PrivateChild {
|
|
128
|
+
async function spawnPrivate(argv: string[], events: TransportEvents): Promise<PrivateChild> {
|
|
109
129
|
const proc = spawn(argv[0]!, argv.slice(1), { stdio: 'pipe', windowsHide: true })
|
|
130
|
+
// a child that cannot start says so after the spawn, by its error
|
|
131
|
+
await new Promise((resolve, reject) => {
|
|
132
|
+
proc.once('spawn', resolve)
|
|
133
|
+
proc.once('error', reject)
|
|
134
|
+
})
|
|
110
135
|
let kept = ''
|
|
111
136
|
// a pipe nobody drains stops the server once it fills, so stderr is read to its end
|
|
112
137
|
proc.stderr.setEncoding('utf8')
|
package/src/runtime/pipe.ts
CHANGED
|
@@ -8,6 +8,8 @@ import { dlopen, FFIType, JSCallback, type Pointer, ptr } from 'bun:ffi'
|
|
|
8
8
|
|
|
9
9
|
import { ClosedError } from '../errors.ts'
|
|
10
10
|
import type { Transport } from '../runtime.ts'
|
|
11
|
+
import { bodyLength, headerSize } from '../wire/frame.ts'
|
|
12
|
+
import { openFailure, turning } from './shared.ts'
|
|
11
13
|
|
|
12
14
|
const symbols = {
|
|
13
15
|
tinystore_open: {
|
|
@@ -43,14 +45,14 @@ export interface PipeEvents {
|
|
|
43
45
|
* read: nothing of them may be kept.
|
|
44
46
|
*/
|
|
45
47
|
frames(bytes: Uint8Array, length: number): number
|
|
48
|
+
/** Reads one whole frame in bytes of its own, which the session may keep. */
|
|
49
|
+
frame(bytes: Uint8Array): void
|
|
46
50
|
/** the store closed; called once */
|
|
47
51
|
end(err: Error): void
|
|
48
52
|
}
|
|
49
53
|
|
|
50
|
-
/** The room for what the core owes
|
|
54
|
+
/** The room for what the core owes: a frame longer than it is written into bytes of its own. */
|
|
51
55
|
const room = 1 << 16
|
|
52
|
-
/** Room past this is given back once nothing is held in it. */
|
|
53
|
-
const kept = 1 << 20
|
|
54
56
|
|
|
55
57
|
/**
|
|
56
58
|
* Opens a pipe to the store in a directory, the core loaded from `library`.
|
|
@@ -70,24 +72,45 @@ export function openPipe(
|
|
|
70
72
|
|
|
71
73
|
// where the core writes what it owes: whole frames are read from the
|
|
72
74
|
// front, and a frame's first part is held there for its rest
|
|
73
|
-
|
|
75
|
+
const inbox = new Uint8Array(room)
|
|
74
76
|
let held = 0
|
|
77
|
+
// A frame longer than the inbox, in bytes as long as it is, and how much
|
|
78
|
+
// of it came: the core writes its rest there, and the session keeps them,
|
|
79
|
+
// so that a file's mebibyte is written once here rather than copied out
|
|
80
|
+
// of an inbox grown for it (issue #10).
|
|
81
|
+
let large: Uint8Array | undefined
|
|
82
|
+
let filledLarge = 0
|
|
75
83
|
|
|
76
|
-
//
|
|
77
|
-
|
|
84
|
+
// where the core writes next, and how many bytes it may
|
|
85
|
+
const into = (): [Uint8Array, number, number] =>
|
|
86
|
+
large === undefined
|
|
87
|
+
? [inbox, held, inbox.length - held]
|
|
88
|
+
: [large, filledLarge, large.length - filledLarge]
|
|
89
|
+
|
|
90
|
+
// reads what the core wrote where `into` said, and says whether it filled
|
|
91
|
+
// that room, which means more may wait
|
|
78
92
|
const read = (written: number): boolean => {
|
|
93
|
+
if (large !== undefined) {
|
|
94
|
+
filledLarge += written
|
|
95
|
+
if (filledLarge < large.length) {
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
const whole = large
|
|
99
|
+
large = undefined
|
|
100
|
+
events.frame(whole)
|
|
101
|
+
return true
|
|
102
|
+
}
|
|
79
103
|
const length = held + written
|
|
80
104
|
const filled = length === inbox.length
|
|
81
105
|
const taken = written === 0 ? 0 : events.frames(inbox, length)
|
|
82
106
|
inbox.copyWithin(0, taken, length)
|
|
83
107
|
held = length - taken
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
inbox = new Uint8Array(room)
|
|
108
|
+
const frameLength = held < headerSize ? 0 : headerSize + bodyLength(inbox)
|
|
109
|
+
if (frameLength > inbox.length) {
|
|
110
|
+
large = new Uint8Array(frameLength)
|
|
111
|
+
large.set(inbox.subarray(0, held))
|
|
112
|
+
filledLarge = held
|
|
113
|
+
held = 0
|
|
91
114
|
}
|
|
92
115
|
return filled
|
|
93
116
|
}
|
|
@@ -106,8 +129,8 @@ export function openPipe(
|
|
|
106
129
|
try {
|
|
107
130
|
let filled = written === undefined || read(written)
|
|
108
131
|
while (filled && connection !== null) {
|
|
109
|
-
const free =
|
|
110
|
-
filled = read(Number(lib.symbols.tinystore_recv(connection, 0, ptr(
|
|
132
|
+
const [bytes, at, free] = into()
|
|
133
|
+
filled = read(Number(lib.symbols.tinystore_recv(connection, 0, ptr(bytes, at), free)))
|
|
111
134
|
}
|
|
112
135
|
} finally {
|
|
113
136
|
reading = false
|
|
@@ -126,12 +149,12 @@ export function openPipe(
|
|
|
126
149
|
waiting.push(bytes)
|
|
127
150
|
return
|
|
128
151
|
}
|
|
129
|
-
const free =
|
|
152
|
+
const [owed, at, free] = into()
|
|
130
153
|
const written = lib.symbols.tinystore_send(
|
|
131
154
|
connection,
|
|
132
155
|
ptr(bytes),
|
|
133
156
|
bytes.length,
|
|
134
|
-
ptr(
|
|
157
|
+
ptr(owed, at),
|
|
135
158
|
free,
|
|
136
159
|
)
|
|
137
160
|
readOn(Number(written))
|
|
@@ -159,17 +182,24 @@ export function openPipe(
|
|
|
159
182
|
const message = new TextDecoder().decode(failure.subarray(0, failed))
|
|
160
183
|
wake.close()
|
|
161
184
|
lib.close()
|
|
162
|
-
throw
|
|
185
|
+
throw openFailure(dir, message)
|
|
163
186
|
}
|
|
164
187
|
connection = Number(opened[0]) as Pointer
|
|
188
|
+
// Bun's loop with no timer armed spins while it waits for a threadsafe
|
|
189
|
+
// callback from another thread: a write waiting for its sync burned a
|
|
190
|
+
// whole processor (research release-2026-10-23). A timer armed, however
|
|
191
|
+
// far off, lets the loop sleep until the core wakes it; it also keeps the
|
|
192
|
+
// process alive while the store is open, as a sidecar's socket does.
|
|
193
|
+
const sleeps = setInterval(() => {}, 2 ** 30)
|
|
165
194
|
return {
|
|
166
|
-
write: send,
|
|
195
|
+
write: turning(send),
|
|
167
196
|
close() {
|
|
168
197
|
if (connection === null) {
|
|
169
198
|
return
|
|
170
199
|
}
|
|
171
200
|
const closing = connection
|
|
172
201
|
connection = null
|
|
202
|
+
clearInterval(sleeps)
|
|
173
203
|
lib.symbols.tinystore_close(closing)
|
|
174
204
|
wake.close()
|
|
175
205
|
events.end(new ClosedError('the store closed'))
|