@tinyshed/tinystore 0.5.0 → 0.6.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/config.d.ts +46 -1
- package/dist/config.js +94 -7
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/kv.js +98 -90
- package/dist/logger.d.ts +10 -0
- package/dist/logger.js +16 -6
- package/dist/metrics.d.ts +17 -0
- package/dist/metrics.js +22 -1
- package/dist/records.d.ts +90 -5
- package/dist/records.js +114 -6
- package/dist/runtime/addon.js +51 -41
- package/dist/runtime/pipe.js +116 -87
- package/dist/sql.d.ts +19 -3
- package/dist/sql.js +31 -4
- package/dist/wire/protocol.d.ts +105 -9
- package/dist/wire/protocol.js +296 -8
- package/package.json +8 -8
- package/src/config.ts +147 -17
- package/src/index.ts +12 -1
- package/src/kv.ts +108 -98
- package/src/logger.ts +17 -7
- package/src/metrics.ts +25 -0
- package/src/records.ts +178 -7
- package/src/runtime/addon.ts +52 -41
- package/src/runtime/pipe.ts +136 -104
- package/src/sql.ts +48 -7
- package/src/wire/protocol.ts +321 -8
package/dist/records.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { type Link } from './connection.ts';
|
|
1
|
+
import { type Connection, type Link } from './connection.ts';
|
|
2
2
|
import { type ConsoleLine } from './console.ts';
|
|
3
|
+
import { type Home, type Open, type Via } from './handles.ts';
|
|
3
4
|
import { type LogConfig, type Logger, type LoggerOptions } from './logger.ts';
|
|
4
5
|
import type { Stream } from './session.ts';
|
|
5
6
|
import { type Calendar, type Duration, type DurationText } from './time.ts';
|
|
6
|
-
import { methods } from './wire/protocol.ts';
|
|
7
|
+
import { methods, type RecordsNewEvent } from './wire/protocol.ts';
|
|
7
8
|
/** A level a logger logs at: one of its four names, or slog's number between them. */
|
|
8
9
|
export type Level = 'debug' | 'info' | 'warn' | 'error' | number;
|
|
9
10
|
/** An event's level: what loggers and the programs whose lines are kept write. */
|
|
@@ -126,6 +127,27 @@ export interface StreamInfo {
|
|
|
126
127
|
retainedFrom: Date;
|
|
127
128
|
newest: Date;
|
|
128
129
|
}
|
|
130
|
+
/** What a stream's events carry, each by name. */
|
|
131
|
+
export interface StreamFields {
|
|
132
|
+
eventNames: NameCount[];
|
|
133
|
+
fields: FieldInfo[];
|
|
134
|
+
context: FieldInfo[];
|
|
135
|
+
}
|
|
136
|
+
/** A name a stream's events have, and how many have it. */
|
|
137
|
+
export interface NameCount {
|
|
138
|
+
name: string;
|
|
139
|
+
count: number;
|
|
140
|
+
}
|
|
141
|
+
/** A key of a stream's events' fields or contexts. */
|
|
142
|
+
export interface FieldInfo {
|
|
143
|
+
name: string;
|
|
144
|
+
/** the kinds of value it holds, in the order `ValueKind` lists them */
|
|
145
|
+
kinds: ValueKind[];
|
|
146
|
+
/** the events that carry it: one that writes it twice, once */
|
|
147
|
+
count: number;
|
|
148
|
+
}
|
|
149
|
+
/** What a value is, as its JSON is spelled: `float` has a fraction or an exponent, `integer` is whole. */
|
|
150
|
+
export type ValueKind = 'float' | 'integer' | 'text' | 'bool' | 'object' | 'list' | 'null';
|
|
129
151
|
export interface PollBatch {
|
|
130
152
|
events: Event[];
|
|
131
153
|
/** where the next poll begins, kept once these events are safe */
|
|
@@ -160,7 +182,10 @@ declare abstract class Reads {
|
|
|
160
182
|
protected abstract opened(method: Method, body: (handle: number | undefined) => Uint8Array): Promise<Stream>;
|
|
161
183
|
/** Keeps a stream that follows the store for its close to cancel; it returns what lets it go. */
|
|
162
184
|
protected abstract follow(stream: Stream): () => void;
|
|
163
|
-
/**
|
|
185
|
+
/**
|
|
186
|
+
* One page: `{ events, next }`, its `limit` of events that match, fewer
|
|
187
|
+
* only on the last, which has no `next`.
|
|
188
|
+
*/
|
|
164
189
|
list(query?: Query): Promise<Page>;
|
|
165
190
|
/** Every event the query finds, a page after another. */
|
|
166
191
|
all(query?: Query): AsyncGenerator<Event>;
|
|
@@ -190,12 +215,28 @@ declare abstract class Reads {
|
|
|
190
215
|
}
|
|
191
216
|
/** Opens a stream of events: lazily, at its first call on a connection. */
|
|
192
217
|
export declare function openEvents(link: Link, stream: string, options?: EventsOptions): Events;
|
|
218
|
+
/** A stream's open, through the database of `database`, a handle on the connection, when given. */
|
|
219
|
+
export declare function eventsOpen(stream: string, options?: EventsOptions, database?: number): Uint8Array;
|
|
220
|
+
/** What a stream's appends in a database's transaction and batch need of it. */
|
|
221
|
+
interface EventsParts {
|
|
222
|
+
stream: string;
|
|
223
|
+
open: Open;
|
|
224
|
+
home: Home | undefined;
|
|
225
|
+
}
|
|
193
226
|
/** A stream of events: written with `append`, which waits for the file, and read back. */
|
|
194
227
|
export declare class Events extends Reads {
|
|
195
228
|
#private;
|
|
196
229
|
readonly stream: string;
|
|
197
|
-
/** Events come from `store.events(stream)
|
|
198
|
-
constructor(link: Link, stream: string, open:
|
|
230
|
+
/** Events come from `store.events(stream)`, or `db.events(stream)` through a database. */
|
|
231
|
+
constructor(link: Link, stream: string, open: Open, home?: Home);
|
|
232
|
+
/**
|
|
233
|
+
* An event's append for a batch of the database the stream was opened
|
|
234
|
+
* from, `db.events`: it is kept with the batch's rows and reaches the
|
|
235
|
+
* stream once they commit, before the batch returns, or never.
|
|
236
|
+
*
|
|
237
|
+
* await db.batch([sql`update roles set role = ${role} where user_id = ${id}`, audit.appending('role.changed', { userId: id, role })])
|
|
238
|
+
*/
|
|
239
|
+
appending(name: string, fields?: Fields, options?: AppendOptions): Appending;
|
|
199
240
|
/**
|
|
200
241
|
* Writes an event, or a list of them whole or not at all: it returns once
|
|
201
242
|
* they are written, and a read finds them at once. An event whose id the
|
|
@@ -221,6 +262,44 @@ export declare class Events extends Reads {
|
|
|
221
262
|
protected opened(method: Method, body: (handle: number | undefined) => Uint8Array): Promise<Stream>;
|
|
222
263
|
protected follow(stream: Stream): () => void;
|
|
223
264
|
}
|
|
265
|
+
/** The parts of a stream, when `handle` is one. */
|
|
266
|
+
export declare function eventsParts(handle: unknown): EventsParts | undefined;
|
|
267
|
+
/**
|
|
268
|
+
* A stream's appends inside a transaction of the database it was opened
|
|
269
|
+
* from, `tx.with(audit)`: each is kept with the transaction's rows and
|
|
270
|
+
* reaches the stream once it commits, before `db.tx` returns. A read goes
|
|
271
|
+
* through the stream itself, and finds what committed.
|
|
272
|
+
*/
|
|
273
|
+
export declare class TxEvents {
|
|
274
|
+
#private;
|
|
275
|
+
readonly stream: string;
|
|
276
|
+
/** TxEvents come from `tx.with(events)`. */
|
|
277
|
+
constructor(parts: EventsParts, via: Via);
|
|
278
|
+
/**
|
|
279
|
+
* Appends an event once the transaction commits, and says whether it
|
|
280
|
+
* will be written: false when its stream, or this transaction, holds its id.
|
|
281
|
+
*/
|
|
282
|
+
append(name: string, fields?: Fields, options?: AppendOptions): Promise<boolean>;
|
|
283
|
+
}
|
|
284
|
+
interface AppendingParts extends EventsParts {
|
|
285
|
+
event: Parameters<typeof RecordsNewEvent.encode>[0];
|
|
286
|
+
}
|
|
287
|
+
/** An event's append for a database's batch, from `events.appending`. */
|
|
288
|
+
export declare class Appending {
|
|
289
|
+
#private;
|
|
290
|
+
/** Comes from `events.appending`. */
|
|
291
|
+
constructor(parts: AppendingParts);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* What a batch of `home` sends of an append, on the connection it goes on:
|
|
295
|
+
* the append's call, its stream's handle opened there first. A stream opened
|
|
296
|
+
* from the store or another database is refused, since its event would not
|
|
297
|
+
* commit with the rows.
|
|
298
|
+
*/
|
|
299
|
+
export declare function appendingIn(appending: Appending, home: Home): (connection: Connection) => Promise<{
|
|
300
|
+
method: number;
|
|
301
|
+
body: Uint8Array;
|
|
302
|
+
}>;
|
|
224
303
|
/** Every stream's events, to read: `store.events()`. */
|
|
225
304
|
export declare class AllEvents extends Reads {
|
|
226
305
|
#private;
|
|
@@ -228,6 +307,12 @@ export declare class AllEvents extends Reads {
|
|
|
228
307
|
constructor(link: Link);
|
|
229
308
|
/** Every stream with events, by name, with what it keeps, where that begins and its newest event's time. */
|
|
230
309
|
streams(): Promise<StreamInfo[]>;
|
|
310
|
+
/**
|
|
311
|
+
* What the events of `stream` carry, none of it read from a packed event: each name with how
|
|
312
|
+
* many events have it, and each key of their fields and contexts with the kinds of value it
|
|
313
|
+
* holds and how many events carry it. A stream with no events carries nothing.
|
|
314
|
+
*/
|
|
315
|
+
fields(stream: string): Promise<StreamFields>;
|
|
231
316
|
/**
|
|
232
317
|
* Every event once, a batch a call, from a cursor the program keeps, the
|
|
233
318
|
* oldest event kept without one: `{ events, cursor, expired }`. With no
|
package/dist/records.js
CHANGED
|
@@ -12,7 +12,7 @@ import { parseJson } from './json.js';
|
|
|
12
12
|
import { encodeFields, newStoreLogger, } from './logger.js';
|
|
13
13
|
import { ms } from './time.js';
|
|
14
14
|
import { currentTrace } from './trace.js';
|
|
15
|
-
import { Empty, Handle, methods, RecordsAggregateAsked, RecordsAppend, RecordsAppended, RecordsBatch, RecordsCount, RecordsDamage, RecordsDamages, RecordsDropped, RecordsFieldNamesAsked, RecordsNames, RecordsOpen, RecordsPage, RecordsPiece, RecordsPollAsked, RecordsQuery, RecordsRows, RecordsStreams, RecordsTailBatch, RecordsTopAsked, RecordsTops, } from './wire/protocol.js';
|
|
15
|
+
import { Empty, Handle, methods, RecordsAggregateAsked, RecordsAppend, RecordsAppended, RecordsBatch, RecordsCount, RecordsDamage, RecordsDamages, RecordsDropped, RecordsFieldNamesAsked, RecordsFieldsAsked, RecordsNames, RecordsOpen, RecordsPage, RecordsPiece, RecordsPollAsked, RecordsQuery, RecordsRows, RecordsStreamFields, RecordsStreams, RecordsTailBatch, RecordsTopAsked, RecordsTops, } from './wire/protocol.js';
|
|
16
16
|
const newline = 0x0a;
|
|
17
17
|
/** What a writer of lines holds for the server at most: past it, lines are dropped and counted. */
|
|
18
18
|
const linesHeld = 1 << 20;
|
|
@@ -30,7 +30,10 @@ const levelNumbers = {
|
|
|
30
30
|
const levelNames = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
|
|
31
31
|
/** What every read takes: a stream's handle, or every stream without one. */
|
|
32
32
|
class Reads {
|
|
33
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* One page: `{ events, next }`, its `limit` of events that match, fewer
|
|
35
|
+
* only on the last, which has no `next`.
|
|
36
|
+
*/
|
|
34
37
|
async list(query) {
|
|
35
38
|
const asked = queryOf(query);
|
|
36
39
|
const answer = await this.ask('records.list', handle => RecordsQuery.encode({ ...asked, handle }));
|
|
@@ -132,23 +135,44 @@ class Reads {
|
|
|
132
135
|
}
|
|
133
136
|
/** Opens a stream of events: lazily, at its first call on a connection. */
|
|
134
137
|
export function openEvents(link, stream, options) {
|
|
138
|
+
return new Events(link, stream, eventsOpen(stream, options));
|
|
139
|
+
}
|
|
140
|
+
/** A stream's open, through the database of `database`, a handle on the connection, when given. */
|
|
141
|
+
export function eventsOpen(stream, options, database) {
|
|
135
142
|
checkName(stream, 'stream');
|
|
136
143
|
const keep = options?.keep === undefined ? undefined : ms(options.keep);
|
|
137
|
-
return
|
|
144
|
+
return RecordsOpen.encode({ stream, keep, database });
|
|
138
145
|
}
|
|
146
|
+
let eventsPartsOf;
|
|
139
147
|
/** A stream of events: written with `append`, which waits for the file, and read back. */
|
|
140
148
|
export class Events extends Reads {
|
|
141
149
|
stream;
|
|
142
150
|
#link;
|
|
143
151
|
#via;
|
|
144
152
|
#open;
|
|
145
|
-
|
|
146
|
-
|
|
153
|
+
#home;
|
|
154
|
+
/** Events come from `store.events(stream)`, or `db.events(stream)` through a database. */
|
|
155
|
+
constructor(link, stream, open, home) {
|
|
147
156
|
super();
|
|
148
157
|
this.stream = stream;
|
|
149
158
|
this.#link = link;
|
|
150
|
-
this.#via = viaLink(link);
|
|
159
|
+
this.#via = home === undefined ? viaLink(link) : home.via(`events ${stream}`);
|
|
151
160
|
this.#open = open;
|
|
161
|
+
this.#home = home;
|
|
162
|
+
}
|
|
163
|
+
static {
|
|
164
|
+
eventsPartsOf = events => ({ stream: events.stream, open: events.#open, home: events.#home });
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* An event's append for a batch of the database the stream was opened
|
|
168
|
+
* from, `db.events`: it is kept with the batch's rows and reaches the
|
|
169
|
+
* stream once they commit, before the batch returns, or never.
|
|
170
|
+
*
|
|
171
|
+
* await db.batch([sql`update roles set role = ${role} where user_id = ${id}`, audit.appending('role.changed', { userId: id, role })])
|
|
172
|
+
*/
|
|
173
|
+
appending(name, fields, options) {
|
|
174
|
+
const event = newEventOf(oneEvent(name, fields, options), 0);
|
|
175
|
+
return new Appending({ ...eventsPartsOf(this), event });
|
|
152
176
|
}
|
|
153
177
|
async append(first, fields, options) {
|
|
154
178
|
const events = typeof first === 'string' ? [oneEvent(first, fields, options)] : first;
|
|
@@ -204,6 +228,68 @@ export class Events extends Reads {
|
|
|
204
228
|
return this.#via.call(methods['records.open'], this.#open, methods[method], body, idempotence);
|
|
205
229
|
}
|
|
206
230
|
}
|
|
231
|
+
/** The parts of a stream, when `handle` is one. */
|
|
232
|
+
export function eventsParts(handle) {
|
|
233
|
+
return handle instanceof Events ? eventsPartsOf(handle) : undefined;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* A stream's appends inside a transaction of the database it was opened
|
|
237
|
+
* from, `tx.with(audit)`: each is kept with the transaction's rows and
|
|
238
|
+
* reaches the stream once it commits, before `db.tx` returns. A read goes
|
|
239
|
+
* through the stream itself, and finds what committed.
|
|
240
|
+
*/
|
|
241
|
+
export class TxEvents {
|
|
242
|
+
stream;
|
|
243
|
+
#open;
|
|
244
|
+
#via;
|
|
245
|
+
/** TxEvents come from `tx.with(events)`. */
|
|
246
|
+
constructor(parts, via) {
|
|
247
|
+
this.stream = parts.stream;
|
|
248
|
+
this.#open = parts.open;
|
|
249
|
+
this.#via = via;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Appends an event once the transaction commits, and says whether it
|
|
253
|
+
* will be written: false when its stream, or this transaction, holds its id.
|
|
254
|
+
*/
|
|
255
|
+
async append(name, fields, options) {
|
|
256
|
+
const event = newEventOf(oneEvent(name, fields, options), 0);
|
|
257
|
+
const answer = await this.#via.call(methods['records.open'], this.#open, methods['records.append'], handle => RecordsAppend.encode({ handle, events: [event] }), 'write');
|
|
258
|
+
return (RecordsAppended.decode(answer).held ?? 0) === 0;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
let appendingPartsOf;
|
|
262
|
+
/** An event's append for a database's batch, from `events.appending`. */
|
|
263
|
+
export class Appending {
|
|
264
|
+
#parts;
|
|
265
|
+
/** Comes from `events.appending`. */
|
|
266
|
+
constructor(parts) {
|
|
267
|
+
this.#parts = parts;
|
|
268
|
+
}
|
|
269
|
+
static {
|
|
270
|
+
appendingPartsOf = appending => appending.#parts;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* What a batch of `home` sends of an append, on the connection it goes on:
|
|
275
|
+
* the append's call, its stream's handle opened there first. A stream opened
|
|
276
|
+
* from the store or another database is refused, since its event would not
|
|
277
|
+
* commit with the rows.
|
|
278
|
+
*/
|
|
279
|
+
export function appendingIn(appending, home) {
|
|
280
|
+
const { stream, open, home: kept, event } = appendingPartsOf(appending);
|
|
281
|
+
if (kept !== home) {
|
|
282
|
+
const where = kept === undefined ? 'records.db' : kept.describe;
|
|
283
|
+
throw new InvalidError(`${home.describe}: events ${stream}: kept in ${where}, outside this batch of ${home.describe}`);
|
|
284
|
+
}
|
|
285
|
+
return async (connection) => {
|
|
286
|
+
const handle = await handleOn(connection, methods['records.open'], open);
|
|
287
|
+
return {
|
|
288
|
+
method: methods['records.append'],
|
|
289
|
+
body: RecordsAppend.encode({ handle, events: [event] }),
|
|
290
|
+
};
|
|
291
|
+
};
|
|
292
|
+
}
|
|
207
293
|
/** Every stream's events, to read: `store.events()`. */
|
|
208
294
|
export class AllEvents extends Reads {
|
|
209
295
|
#link;
|
|
@@ -222,6 +308,28 @@ export class AllEvents extends Reads {
|
|
|
222
308
|
newest: dateOf(stream.newest ?? 0n),
|
|
223
309
|
}));
|
|
224
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* What the events of `stream` carry, none of it read from a packed event: each name with how
|
|
313
|
+
* many events have it, and each key of their fields and contexts with the kinds of value it
|
|
314
|
+
* holds and how many events carry it. A stream with no events carries nothing.
|
|
315
|
+
*/
|
|
316
|
+
async fields(stream) {
|
|
317
|
+
const answer = await this.ask('records.fields', () => RecordsFieldsAsked.encode({ stream }));
|
|
318
|
+
const carried = RecordsStreamFields.decode(answer);
|
|
319
|
+
const info = (field) => ({
|
|
320
|
+
name: field.name ?? '',
|
|
321
|
+
kinds: (field.kinds ?? []),
|
|
322
|
+
count: field.count ?? 0,
|
|
323
|
+
});
|
|
324
|
+
return {
|
|
325
|
+
eventNames: (carried.eventNames ?? []).map(name => ({
|
|
326
|
+
name: name.name ?? '',
|
|
327
|
+
count: name.count ?? 0,
|
|
328
|
+
})),
|
|
329
|
+
fields: (carried.fields ?? []).map(info),
|
|
330
|
+
context: (carried.context ?? []).map(info),
|
|
331
|
+
};
|
|
332
|
+
}
|
|
225
333
|
/**
|
|
226
334
|
* Every event once, a batch a call, from a cursor the program keeps, the
|
|
227
335
|
* oldest event kept without one: `{ events, cursor, expired }`. With no
|
package/dist/runtime/addon.js
CHANGED
|
@@ -12,59 +12,69 @@ import { openFailure, turning } from './shared.js';
|
|
|
12
12
|
*/
|
|
13
13
|
export function openAddon(path, dir, options, events) {
|
|
14
14
|
const addon = createRequire(import.meta.url)(path);
|
|
15
|
-
|
|
16
|
-
// end, since an answer handed back meanwhile would reach the session in
|
|
17
|
-
// the middle of the bytes it is reading.
|
|
18
|
-
let reading = false;
|
|
19
|
-
const waiting = [];
|
|
20
|
-
const take = (bytes) => {
|
|
21
|
-
reading = true;
|
|
22
|
-
try {
|
|
23
|
-
let next = bytes;
|
|
24
|
-
while (next !== null && pipe !== undefined) {
|
|
25
|
-
events.data(next);
|
|
26
|
-
next = pipe.recv();
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
finally {
|
|
30
|
-
reading = false;
|
|
31
|
-
}
|
|
32
|
-
const queued = waiting.shift();
|
|
33
|
-
if (queued !== undefined) {
|
|
34
|
-
send(queued);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
const send = (bytes) => {
|
|
38
|
-
if (pipe === undefined) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
if (reading) {
|
|
42
|
-
waiting.push(bytes);
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
take(pipe.send(bytes));
|
|
46
|
-
};
|
|
47
|
-
let pipe;
|
|
15
|
+
const reader = new PipeReader(events);
|
|
48
16
|
try {
|
|
49
|
-
pipe = addon.open(dir, options, () =>
|
|
50
|
-
if (!reading) {
|
|
51
|
-
take(pipe?.recv() ?? null);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
17
|
+
reader.pipe = addon.open(dir, options, () => reader.woken());
|
|
54
18
|
}
|
|
55
19
|
catch (err) {
|
|
56
20
|
throw openFailure(dir, err.message);
|
|
57
21
|
}
|
|
58
22
|
return {
|
|
59
|
-
write: turning(send),
|
|
23
|
+
write: turning(bytes => reader.send(bytes)),
|
|
60
24
|
close() {
|
|
61
|
-
const closing = pipe;
|
|
25
|
+
const closing = reader.pipe;
|
|
62
26
|
if (closing === undefined) {
|
|
63
27
|
return;
|
|
64
28
|
}
|
|
65
|
-
pipe = undefined;
|
|
29
|
+
reader.pipe = undefined;
|
|
66
30
|
closing.close();
|
|
67
31
|
events.end(new ClosedError('the store closed'));
|
|
68
32
|
},
|
|
69
33
|
};
|
|
70
34
|
}
|
|
35
|
+
/** The addon's pipe as the session reads and writes it. */
|
|
36
|
+
class PipeReader {
|
|
37
|
+
pipe;
|
|
38
|
+
#events;
|
|
39
|
+
// What the session writes while it reads: its frames wait for the read's
|
|
40
|
+
// end, since an answer handed back meanwhile would reach the session in
|
|
41
|
+
// the middle of the bytes it is reading.
|
|
42
|
+
#reading = false;
|
|
43
|
+
#waiting = [];
|
|
44
|
+
constructor(events) {
|
|
45
|
+
this.#events = events;
|
|
46
|
+
}
|
|
47
|
+
/** The core's wake: what it has ready, unless a read under way, which reads until nothing is, takes it. */
|
|
48
|
+
woken() {
|
|
49
|
+
if (!this.#reading) {
|
|
50
|
+
this.#take(this.pipe?.recv() ?? null);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
send(bytes) {
|
|
54
|
+
if (this.pipe === undefined) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (this.#reading) {
|
|
58
|
+
this.#waiting.push(bytes);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
this.#take(this.pipe.send(bytes));
|
|
62
|
+
}
|
|
63
|
+
#take(bytes) {
|
|
64
|
+
this.#reading = true;
|
|
65
|
+
try {
|
|
66
|
+
let next = bytes;
|
|
67
|
+
while (next !== null && this.pipe !== undefined) {
|
|
68
|
+
this.#events.data(next);
|
|
69
|
+
next = this.pipe.recv();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
this.#reading = false;
|
|
74
|
+
}
|
|
75
|
+
const queued = this.#waiting.shift();
|
|
76
|
+
if (queued !== undefined) {
|
|
77
|
+
this.send(queued);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
package/dist/runtime/pipe.js
CHANGED
|
@@ -40,119 +40,148 @@ const room = 1 << 16;
|
|
|
40
40
|
*/
|
|
41
41
|
export function openPipe(library, dir, options, events) {
|
|
42
42
|
const lib = dlopen(library, symbols);
|
|
43
|
+
const pipe = new CorePipe(lib, new Inbox(events));
|
|
44
|
+
const wake = new JSCallback(() => pipe.readOn(undefined), {
|
|
45
|
+
args: [FFIType.ptr],
|
|
46
|
+
returns: FFIType.void,
|
|
47
|
+
threadsafe: true,
|
|
48
|
+
});
|
|
49
|
+
pipe.connection = connect(lib, dir, options, wake);
|
|
50
|
+
// Bun's loop with no timer armed spins while it waits for a threadsafe
|
|
51
|
+
// callback from another thread: a write waiting for its sync burned a
|
|
52
|
+
// whole processor (research release-2026-10-23). A timer armed, however
|
|
53
|
+
// far off, lets the loop sleep until the core wakes it; it also keeps the
|
|
54
|
+
// process alive while the store is open, as a sidecar's socket does.
|
|
55
|
+
const sleeps = setInterval(() => { }, 2 ** 30);
|
|
56
|
+
return {
|
|
57
|
+
write: turning(bytes => pipe.send(bytes)),
|
|
58
|
+
close() {
|
|
59
|
+
const closing = pipe.connection;
|
|
60
|
+
if (closing === null) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
pipe.connection = null;
|
|
64
|
+
clearInterval(sleeps);
|
|
65
|
+
lib.symbols.tinystore_close(closing);
|
|
66
|
+
wake.close();
|
|
67
|
+
events.end(new ClosedError('the store closed'));
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** Opens the store through the core; a store that does not open lets the core and the wake go. */
|
|
72
|
+
function connect(lib, dir, options, wake) {
|
|
43
73
|
const name = new TextEncoder().encode(dir);
|
|
44
74
|
const opened = new BigUint64Array(1);
|
|
45
75
|
const failure = new Uint8Array(1024);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
76
|
+
const failed = Number(lib.symbols.tinystore_open(ptr(name), name.length, ptr(options), options.length, wake, null, ptr(opened), ptr(failure), failure.length));
|
|
77
|
+
if (failed > 0) {
|
|
78
|
+
const message = new TextDecoder().decode(failure.subarray(0, failed));
|
|
79
|
+
wake.close();
|
|
80
|
+
lib.close();
|
|
81
|
+
throw openFailure(dir, message);
|
|
82
|
+
}
|
|
83
|
+
return Number(opened[0]);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Where the core writes what it owes: whole frames are read from the front,
|
|
87
|
+
* and a frame's first part is held there for its rest.
|
|
88
|
+
*/
|
|
89
|
+
class Inbox {
|
|
90
|
+
#events;
|
|
91
|
+
#inbox = new Uint8Array(room);
|
|
92
|
+
#held = 0;
|
|
51
93
|
// A frame longer than the inbox, in bytes as long as it is, and how much
|
|
52
94
|
// of it came: the core writes its rest there, and the session keeps them,
|
|
53
95
|
// so that a file's mebibyte is written once here rather than copied out
|
|
54
96
|
// of an inbox grown for it (issue #10).
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
97
|
+
#large;
|
|
98
|
+
#filledLarge = 0;
|
|
99
|
+
constructor(events) {
|
|
100
|
+
this.#events = events;
|
|
101
|
+
}
|
|
102
|
+
/** Where the core writes next, and how many bytes it may. */
|
|
103
|
+
into() {
|
|
104
|
+
return this.#large === undefined
|
|
105
|
+
? [this.#inbox, this.#held, this.#inbox.length - this.#held]
|
|
106
|
+
: [this.#large, this.#filledLarge, this.#large.length - this.#filledLarge];
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Reads what the core wrote where `into` said, and says whether it filled
|
|
110
|
+
* that room, which means more may wait.
|
|
111
|
+
*/
|
|
112
|
+
read(written) {
|
|
113
|
+
if (this.#large !== undefined) {
|
|
114
|
+
this.#filledLarge += written;
|
|
115
|
+
if (this.#filledLarge < this.#large.length) {
|
|
67
116
|
return false;
|
|
68
117
|
}
|
|
69
|
-
const whole = large;
|
|
70
|
-
large = undefined;
|
|
71
|
-
events.frame(whole);
|
|
118
|
+
const whole = this.#large;
|
|
119
|
+
this.#large = undefined;
|
|
120
|
+
this.#events.frame(whole);
|
|
72
121
|
return true;
|
|
73
122
|
}
|
|
74
|
-
const length = held + written;
|
|
75
|
-
const filled = length === inbox.length;
|
|
76
|
-
const taken = written === 0 ? 0 : events.frames(inbox, length);
|
|
77
|
-
inbox.copyWithin(0, taken, length);
|
|
78
|
-
held = length - taken;
|
|
79
|
-
const frameLength = held < headerSize ? 0 : headerSize + bodyLength(inbox);
|
|
80
|
-
if (frameLength > inbox.length) {
|
|
81
|
-
large = new Uint8Array(frameLength);
|
|
82
|
-
large.set(inbox.subarray(0, held));
|
|
83
|
-
filledLarge = held;
|
|
84
|
-
held = 0;
|
|
123
|
+
const length = this.#held + written;
|
|
124
|
+
const filled = length === this.#inbox.length;
|
|
125
|
+
const taken = written === 0 ? 0 : this.#events.frames(this.#inbox, length);
|
|
126
|
+
this.#inbox.copyWithin(0, taken, length);
|
|
127
|
+
this.#held = length - taken;
|
|
128
|
+
const frameLength = this.#held < headerSize ? 0 : headerSize + bodyLength(this.#inbox);
|
|
129
|
+
if (frameLength > this.#inbox.length) {
|
|
130
|
+
this.#large = new Uint8Array(frameLength);
|
|
131
|
+
this.#large.set(this.#inbox.subarray(0, this.#held));
|
|
132
|
+
this.#filledLarge = this.#held;
|
|
133
|
+
this.#held = 0;
|
|
85
134
|
}
|
|
86
135
|
return filled;
|
|
87
|
-
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** One connection's calls into the core, which write what it owes into the inbox. */
|
|
139
|
+
class CorePipe {
|
|
140
|
+
connection = null;
|
|
141
|
+
#lib;
|
|
142
|
+
#inbox;
|
|
88
143
|
// What the session writes while it reads: its frames wait for the read's
|
|
89
144
|
// end, since a call of the core made meanwhile would write over the bytes
|
|
90
145
|
// being read.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
146
|
+
#reading = false;
|
|
147
|
+
#waiting = [];
|
|
148
|
+
constructor(lib, inbox) {
|
|
149
|
+
this.#lib = lib;
|
|
150
|
+
this.#inbox = inbox;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Reads what a call of the core wrote, and on while the core fills the
|
|
154
|
+
* room, since no wake comes for what waits; then sends what was written
|
|
155
|
+
* meanwhile.
|
|
156
|
+
*/
|
|
157
|
+
readOn(written) {
|
|
158
|
+
this.#reading = true;
|
|
98
159
|
try {
|
|
99
|
-
let filled = written === undefined || read(written);
|
|
100
|
-
while (filled && connection !== null) {
|
|
101
|
-
const [bytes, at, free] = into();
|
|
102
|
-
|
|
160
|
+
let filled = written === undefined || this.#inbox.read(written);
|
|
161
|
+
while (filled && this.connection !== null) {
|
|
162
|
+
const [bytes, at, free] = this.#inbox.into();
|
|
163
|
+
const read = this.#lib.symbols.tinystore_recv(this.connection, 0, ptr(bytes, at), free);
|
|
164
|
+
filled = this.#inbox.read(Number(read));
|
|
103
165
|
}
|
|
104
166
|
}
|
|
105
167
|
finally {
|
|
106
|
-
reading = false;
|
|
168
|
+
this.#reading = false;
|
|
107
169
|
}
|
|
108
|
-
const next = waiting.shift();
|
|
170
|
+
const next = this.#waiting.shift();
|
|
109
171
|
if (next !== undefined) {
|
|
110
|
-
send(next);
|
|
172
|
+
this.send(next);
|
|
111
173
|
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (connection === null) {
|
|
174
|
+
}
|
|
175
|
+
send(bytes) {
|
|
176
|
+
if (this.connection === null) {
|
|
115
177
|
return;
|
|
116
178
|
}
|
|
117
|
-
if (reading) {
|
|
118
|
-
waiting.push(bytes);
|
|
179
|
+
if (this.#reading) {
|
|
180
|
+
this.#waiting.push(bytes);
|
|
119
181
|
return;
|
|
120
182
|
}
|
|
121
|
-
const [owed, at, free] = into();
|
|
122
|
-
const written = lib.symbols.tinystore_send(connection, ptr(bytes), bytes.length, ptr(owed, at), free);
|
|
123
|
-
readOn(Number(written));
|
|
124
|
-
};
|
|
125
|
-
const wake = new JSCallback(() => readOn(undefined), {
|
|
126
|
-
args: [FFIType.ptr],
|
|
127
|
-
returns: FFIType.void,
|
|
128
|
-
threadsafe: true,
|
|
129
|
-
});
|
|
130
|
-
const failed = Number(lib.symbols.tinystore_open(ptr(name), name.length, ptr(options), options.length, wake, null, ptr(opened), ptr(failure), failure.length));
|
|
131
|
-
if (failed > 0) {
|
|
132
|
-
const message = new TextDecoder().decode(failure.subarray(0, failed));
|
|
133
|
-
wake.close();
|
|
134
|
-
lib.close();
|
|
135
|
-
throw openFailure(dir, message);
|
|
183
|
+
const [owed, at, free] = this.#inbox.into();
|
|
184
|
+
const written = this.#lib.symbols.tinystore_send(this.connection, ptr(bytes), bytes.length, ptr(owed, at), free);
|
|
185
|
+
this.readOn(Number(written));
|
|
136
186
|
}
|
|
137
|
-
connection = Number(opened[0]);
|
|
138
|
-
// Bun's loop with no timer armed spins while it waits for a threadsafe
|
|
139
|
-
// callback from another thread: a write waiting for its sync burned a
|
|
140
|
-
// whole processor (research release-2026-10-23). A timer armed, however
|
|
141
|
-
// far off, lets the loop sleep until the core wakes it; it also keeps the
|
|
142
|
-
// process alive while the store is open, as a sidecar's socket does.
|
|
143
|
-
const sleeps = setInterval(() => { }, 2 ** 30);
|
|
144
|
-
return {
|
|
145
|
-
write: turning(send),
|
|
146
|
-
close() {
|
|
147
|
-
if (connection === null) {
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
const closing = connection;
|
|
151
|
-
connection = null;
|
|
152
|
-
clearInterval(sleeps);
|
|
153
|
-
lib.symbols.tinystore_close(closing);
|
|
154
|
-
wake.close();
|
|
155
|
-
events.end(new ClosedError('the store closed'));
|
|
156
|
-
},
|
|
157
|
-
};
|
|
158
187
|
}
|