@tinyshed/tinystore 0.4.1 → 0.5.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/errors.d.ts CHANGED
@@ -37,9 +37,10 @@ export declare class InvalidError extends TinystoreError {
37
37
  constructor(message: string, what?: What);
38
38
  }
39
39
  /**
40
- * A bound: memory, size or count. Later, or smaller, it may go through. A
41
- * limit the store names says which, what the call would have taken of it,
42
- * and the bound: `decoded samples`, 120000, 100000.
40
+ * A bound: memory, size or count. Later, or smaller, it may go through. The
41
+ * error names the bound, what the call would have taken of it, and the
42
+ * bound: `set_many documents`, 1001, 1000. `limits` holds the bounds a call
43
+ * meets by those names.
43
44
  */
44
45
  export declare class LimitError extends TinystoreError {
45
46
  readonly limit: string | undefined;
@@ -47,29 +48,87 @@ export declare class LimitError extends TinystoreError {
47
48
  readonly bound: number | undefined;
48
49
  constructor(message: string, what?: What);
49
50
  }
51
+ /** A bound one call meets: its name, as a LimitError's `limit` says it, and the most it takes. */
52
+ export interface Limit {
53
+ readonly name: string;
54
+ readonly bound: number;
55
+ }
50
56
  /**
51
- * The names a LimitError's `limit` holds, as Go's constants and Python's
52
- * `tinystore.limits` spell them, to map a limit without matching its text.
57
+ * The bounds one call meets, the same in every SDK and in Rust's
58
+ * `tinystore::limits`. A program cuts its batches to them before it calls,
59
+ * and tells which bound a LimitError met without reading its text:
53
60
  *
54
61
  * ```ts
55
- * if (err instanceof LimitError && err.limit === limits.decodedSamples) { … }
62
+ * for (let at = 0; at < courses.length; at += limits.setManyDocuments.bound) { … }
63
+ * if (err instanceof LimitError && err.limit === limits.requestBytes.name) { … }
56
64
  * ```
65
+ *
66
+ * A bound the store's options set, its memory (`store memory`) or its
67
+ * `maxSeries` (`series in a store`), is named the same way and is not here.
57
68
  */
58
69
  export declare const limits: Readonly<{
59
- readonly storeMemory: 'store memory';
60
- readonly storeMemoryNow: 'store memory, now';
61
- readonly matchedSeries: 'matched series';
62
- readonly decodedBlocks: 'decoded blocks';
63
- readonly fetchedBytes: 'fetched bytes';
64
- readonly decodedSamples: 'decoded samples';
65
- readonly outputSamples: 'output samples';
66
- readonly outputBuckets: 'output buckets';
67
- readonly recordBytes: "bytes of a record, a block's";
68
- readonly appendBytes: "bytes of records in one Append, a segment's; split it";
69
- readonly jobValueBytes: "bytes of a job's value";
70
- readonly stepBytes: "bytes of a step's answer";
71
- readonly objectBytes: "bytes of an object, the bucket's MaxSize";
72
- readonly rowBytes: "bytes of a call's rows";
70
+ /** A bucket's value, as it is stored. */
71
+ valueBytes: Limit;
72
+ /** A live setting's change, as JSON. */
73
+ settingBytes: Limit;
74
+ /** Every change a config keeps, together. */
75
+ configBytes: Limit;
76
+ /** The callers that wait for one lock or semaphore at once. */
77
+ lockWaiters: Limit;
78
+ /** The subscriptions to the store's channels at once. */
79
+ subscriptions: Limit;
80
+ /** A job's payload, its result or a step's answer, as JSON. */
81
+ jobBytes: Limit;
82
+ /** A run's progress, as JSON. */
83
+ progressBytes: Limit;
84
+ /** The values one call reads from a database: past them, read a page at a time. */
85
+ rowsBytes: Limit;
86
+ /** One event, its name, fields and text together. */
87
+ eventBytes: Limit;
88
+ /** The events of one append together. */
89
+ appendBytes: Limit;
90
+ /** The rows one `aggregate` answers. */
91
+ aggregateRows: Limit;
92
+ /** The different values one `top` or `distinct` holds, as JSON. */
93
+ countBytes: Limit;
94
+ /** The samples of one `ingest`. */
95
+ ingestSamples: Limit;
96
+ /** The bytes of one `ingest`'s samples, each with its labels. */
97
+ ingestBytes: Limit;
98
+ /** The samples of one exporter's text. */
99
+ textSamples: Limit;
100
+ /** One exporter's text, and the bytes of its samples. */
101
+ textBytes: Limit;
102
+ /** The series one query finds. */
103
+ querySeries: Limit;
104
+ /** What one query reads from the file. */
105
+ queryBytes: Limit;
106
+ /** The samples one query decodes. */
107
+ querySamples: Limit;
108
+ /** The samples or buckets one query answers. */
109
+ queryAnswer: Limit;
110
+ /** The documents of one `setMany`. */
111
+ setManyDocuments: Limit;
112
+ /** The documents of one `setMany` as JSON; one request body, `requestBytes`, bounds it first. */
113
+ setManyBytes: Limit;
114
+ /** One document as JSON, or a table's row as its index reads it. */
115
+ documentBytes: Limit;
116
+ /** The hits a search reaches, its offset and its limit together. */
117
+ searchHits: Limit;
118
+ /** A search's text, in bytes. */
119
+ searchText: Limit;
120
+ /** The conditions of one search's filter. */
121
+ filterConditions: Limit;
122
+ /** The values the lists of one search's conditions hold together. */
123
+ filterValues: Limit;
124
+ /** A text a filter keeps or asks for, in bytes. */
125
+ filterText: Limit;
126
+ /** A vector's dimensions. */
127
+ vectorDimensions: Limit;
128
+ /** One request, its message as the SDK sends it: a larger one fails before it is sent. */
129
+ requestBytes: Limit;
130
+ /** One answer, its message as the store sends it. */
131
+ answerBytes: Limit;
73
132
  }>;
74
133
  /** What the call needs is not there: a file to copy or rename. */
75
134
  export declare class NotFoundError extends TinystoreError {
package/dist/errors.js CHANGED
@@ -43,9 +43,10 @@ export class InvalidError extends TinystoreError {
43
43
  }
44
44
  }
45
45
  /**
46
- * A bound: memory, size or count. Later, or smaller, it may go through. A
47
- * limit the store names says which, what the call would have taken of it,
48
- * and the bound: `decoded samples`, 120000, 100000.
46
+ * A bound: memory, size or count. Later, or smaller, it may go through. The
47
+ * error names the bound, what the call would have taken of it, and the
48
+ * bound: `set_many documents`, 1001, 1000. `limits` holds the bounds a call
49
+ * meets by those names.
49
50
  */
50
51
  export class LimitError extends TinystoreError {
51
52
  limit;
@@ -58,29 +59,83 @@ export class LimitError extends TinystoreError {
58
59
  this.bound = typeof what?.bound === 'string' ? Number(what.bound) : undefined;
59
60
  }
60
61
  }
62
+ const limit = (name, bound) => Object.freeze({ name, bound });
61
63
  /**
62
- * The names a LimitError's `limit` holds, as Go's constants and Python's
63
- * `tinystore.limits` spell them, to map a limit without matching its text.
64
+ * The bounds one call meets, the same in every SDK and in Rust's
65
+ * `tinystore::limits`. A program cuts its batches to them before it calls,
66
+ * and tells which bound a LimitError met without reading its text:
64
67
  *
65
68
  * ```ts
66
- * if (err instanceof LimitError && err.limit === limits.decodedSamples) { … }
69
+ * for (let at = 0; at < courses.length; at += limits.setManyDocuments.bound) { … }
70
+ * if (err instanceof LimitError && err.limit === limits.requestBytes.name) { … }
67
71
  * ```
72
+ *
73
+ * A bound the store's options set, its memory (`store memory`) or its
74
+ * `maxSeries` (`series in a store`), is named the same way and is not here.
68
75
  */
69
76
  export const limits = Object.freeze({
70
- storeMemory: 'store memory',
71
- storeMemoryNow: 'store memory, now',
72
- matchedSeries: 'matched series',
73
- decodedBlocks: 'decoded blocks',
74
- fetchedBytes: 'fetched bytes',
75
- decodedSamples: 'decoded samples',
76
- outputSamples: 'output samples',
77
- outputBuckets: 'output buckets',
78
- recordBytes: "bytes of a record, a block's",
79
- appendBytes: "bytes of records in one Append, a segment's; split it",
80
- jobValueBytes: "bytes of a job's value",
81
- stepBytes: "bytes of a step's answer",
82
- objectBytes: "bytes of an object, the bucket's MaxSize",
83
- rowBytes: "bytes of a call's rows",
77
+ /** A bucket's value, as it is stored. */
78
+ valueBytes: limit('value bytes', 1 << 20),
79
+ /** A live setting's change, as JSON. */
80
+ settingBytes: limit('setting bytes', 16 << 10),
81
+ /** Every change a config keeps, together. */
82
+ configBytes: limit('config bytes', 256 << 10),
83
+ /** The callers that wait for one lock or semaphore at once. */
84
+ lockWaiters: limit('lock waiters', 10_000),
85
+ /** The subscriptions to the store's channels at once. */
86
+ subscriptions: limit('subscriptions', 10_000),
87
+ /** A job's payload, its result or a step's answer, as JSON. */
88
+ jobBytes: limit('job bytes', 1 << 20),
89
+ /** A run's progress, as JSON. */
90
+ progressBytes: limit('progress bytes', 4 << 10),
91
+ /** The values one call reads from a database: past them, read a page at a time. */
92
+ rowsBytes: limit("bytes of a call's rows", 64 << 20),
93
+ /** One event, its name, fields and text together. */
94
+ eventBytes: limit('event bytes', 256 << 10),
95
+ /** The events of one append together. */
96
+ appendBytes: limit('append bytes', 4 << 20),
97
+ /** The rows one `aggregate` answers. */
98
+ aggregateRows: limit('aggregate rows', 10_000),
99
+ /** The different values one `top` or `distinct` holds, as JSON. */
100
+ countBytes: limit('count bytes', 64 << 20),
101
+ /** The samples of one `ingest`. */
102
+ ingestSamples: limit('ingest samples', 10_000),
103
+ /** The bytes of one `ingest`'s samples, each with its labels. */
104
+ ingestBytes: limit('ingest bytes', 4 << 20),
105
+ /** The samples of one exporter's text. */
106
+ textSamples: limit('text samples', 100_000),
107
+ /** One exporter's text, and the bytes of its samples. */
108
+ textBytes: limit('text bytes', 32 << 20),
109
+ /** The series one query finds. */
110
+ querySeries: limit('series', 1000),
111
+ /** What one query reads from the file. */
112
+ queryBytes: limit('bytes read', 16 << 20),
113
+ /** The samples one query decodes. */
114
+ querySamples: limit('samples decoded', 1 << 20),
115
+ /** The samples or buckets one query answers. */
116
+ queryAnswer: limit('answer', 100_000),
117
+ /** The documents of one `setMany`. */
118
+ setManyDocuments: limit('set_many documents', 1000),
119
+ /** The documents of one `setMany` as JSON; one request body, `requestBytes`, bounds it first. */
120
+ setManyBytes: limit('set_many bytes', 16 << 20),
121
+ /** One document as JSON, or a table's row as its index reads it. */
122
+ documentBytes: limit('document bytes', 1 << 20),
123
+ /** The hits a search reaches, its offset and its limit together. */
124
+ searchHits: limit('search hits', 1000),
125
+ /** A search's text, in bytes. */
126
+ searchText: limit('search text', 1 << 10),
127
+ /** The conditions of one search's filter. */
128
+ filterConditions: limit('filter conditions', 8),
129
+ /** The values the lists of one search's conditions hold together. */
130
+ filterValues: limit('filter values', 100),
131
+ /** A text a filter keeps or asks for, in bytes. */
132
+ filterText: limit('filter text', 256),
133
+ /** A vector's dimensions. */
134
+ vectorDimensions: limit('vector dimensions', 4096),
135
+ /** One request, its message as the SDK sends it: a larger one fails before it is sent. */
136
+ requestBytes: limit('request bytes', 2 << 20),
137
+ /** One answer, its message as the store sends it. */
138
+ answerBytes: limit('answer bytes', 2 << 20),
84
139
  });
85
140
  /** What the call needs is not there: a file to copy or rename. */
86
141
  export class NotFoundError extends TinystoreError {
package/dist/index.d.ts CHANGED
@@ -18,7 +18,7 @@ export type { Once, OnceOptions } from './once.ts';
18
18
  export type { ColumnType, Query, RowPage, Table, TableOptions, UpsertOptions } from './query.ts';
19
19
  export type { Aggregate as EventsAggregate, AllEvents, AppendOptions, Counted, Damage, Event, EventLevel, Events, EventsOptions, Fields, Level, Levels, Lines, NewEvent, Page as EventsPage, PollBatch, Query as EventsQuery, Row as EventsRow, Stats, StoreLoggerOptions, StreamInfo, TailBatch, TailQuery, Top, } from './records.ts';
20
20
  export type { StandardSchemaV1 } from './schema.ts';
21
- export { type Found as SearchFound, type Hit as SearchHit, type IndexDocument, type IndexInfo, type IndexOptions, type IndexStatus, type Query as SearchQuery, SearchIndex, search, TableIndex, type TableIndexOptions, type Vector, type Where, type WhereValue, } from './search.ts';
21
+ export { type Found as SearchFound, type Hit as SearchHit, type IndexDocument, type IndexInfo, type IndexOptions, type IndexStatus, type KeyboardLayout, type Match as SearchMatch, type Query as SearchQuery, SearchIndex, search, TableIndex, type TableIndexOptions, type Vector, type Where, type WhereValue, } from './search.ts';
22
22
  export { type Columns, type Database, type DatabaseOptions, type Done, type Migrations, type Row, Sql, type SqlArg, type SqlTag, type SqlTx, type SqlValue, type Statement, sql, } from './sql.ts';
23
23
  export { type ConnectOptions, connect, type NodeOptions, type OpenOptions, open, type Status, Store, } from './store.ts';
24
24
  export type { Calendar, Duration, DurationText, Time } from './time.ts';
package/dist/kv.d.ts CHANGED
@@ -102,6 +102,21 @@ export declare class Bucket<T> {
102
102
  set(key: Key, value: T, options?: WriteOptions): Promise<void>;
103
103
  /** Writes the value only where no live key is, and says whether it did. */
104
104
  create(key: Key, value: T, options?: Omit<WriteOptions, 'ifVersion'>): Promise<boolean>;
105
+ /**
106
+ * Writes what `change` makes of the key's value without losing a write
107
+ * made meanwhile: it reads the key, calls `change` with its value, or
108
+ * undefined where it holds none, and writes what `change` returns at the
109
+ * version it read, or creates the key. When another write came first it
110
+ * reads again and calls `change` again, eight times at most, then throws
111
+ * ConflictError: `change` may run more than once, so it must do nothing
112
+ * else. A change that returns undefined, or the very value it was given,
113
+ * writes nothing. It returns the value written, or the one there when
114
+ * nothing was. A key that is there keeps its expiry, and one it creates
115
+ * takes the bucket's, as set and create do.
116
+ *
117
+ * await pinned.update(studentId, courses => [...(courses ?? []), 2518])
118
+ */
119
+ update(key: Key, change: (current: T | undefined) => T | undefined): Promise<T | undefined>;
105
120
  /** Puts the key in a bucket used as a set; false when it was there. */
106
121
  add(key: Key, options?: Omit<WriteOptions, 'ifVersion'>): Promise<boolean>;
107
122
  /**
package/dist/kv.js CHANGED
@@ -2,11 +2,13 @@
2
2
  // of one value type by key, keys in branches, expiry by the store's clock and
3
3
  // versions that never repeat, with counters beside them. A handle opens with
4
4
  // its first call on a connection.
5
- import { CorruptError, InvalidError } from './errors.js';
5
+ import { ConflictError, CorruptError, InvalidError } from './errors.js';
6
6
  import { checkName, handleOn, ownerText, viaLink, } from './handles.js';
7
7
  import { dateOf, ms, unixMs } from './time.js';
8
8
  import { textOf } from './wire/codec.js';
9
9
  import { KvBranch, KvBucketOpen, KvCall, KvCount, KvCountersOpen, KvEntry, KvFound, KvList, KvPage, KvResealed, KvWritten, methods, } from './wire/protocol.js';
10
+ /** The tries an update makes while other writes come first, as many as a config's change makes. */
11
+ const updateAttempts = 8;
10
12
  // a handle's parts, kept out of its public shape, for store.tx
11
13
  const parts = new WeakMap();
12
14
  export function partsOf(handle) {
@@ -69,6 +71,34 @@ export class Bucket {
69
71
  const raw = this.#values.encode(value);
70
72
  return this.#ask('kv.create', { key, value: raw, ...writeFields(options) }, 'write', written);
71
73
  }
74
+ /**
75
+ * Writes what `change` makes of the key's value without losing a write
76
+ * made meanwhile: it reads the key, calls `change` with its value, or
77
+ * undefined where it holds none, and writes what `change` returns at the
78
+ * version it read, or creates the key. When another write came first it
79
+ * reads again and calls `change` again, eight times at most, then throws
80
+ * ConflictError: `change` may run more than once, so it must do nothing
81
+ * else. A change that returns undefined, or the very value it was given,
82
+ * writes nothing. It returns the value written, or the one there when
83
+ * nothing was. A key that is there keeps its expiry, and one it creates
84
+ * takes the bucket's, as set and create do.
85
+ *
86
+ * await pinned.update(studentId, courses => [...(courses ?? []), 2518])
87
+ */
88
+ async update(key, change) {
89
+ for (let attempt = 0; attempt < updateAttempts; attempt++) {
90
+ const entry = await this.entry(key);
91
+ const next = change(entry?.value);
92
+ if (next === undefined || (entry !== undefined && next === entry.value)) {
93
+ return entry?.value;
94
+ }
95
+ if (await this.#written(key, next, entry?.version)) {
96
+ return next;
97
+ }
98
+ }
99
+ const shown = [this.#home?.describe, `kv bucket ${this.name}`, this.#shownKey(key)];
100
+ throw new ConflictError(`${shown.filter(part => part !== undefined).join(': ')}: other writes came first each time it was tried`);
101
+ }
72
102
  /** Puts the key in a bucket used as a set; false when it was there. */
73
103
  async add(key, options) {
74
104
  return this.#ask('kv.create', { key, ...writeFields(options) }, 'write', written);
@@ -135,6 +165,26 @@ export class Bucket {
135
165
  after = page.next;
136
166
  }
137
167
  }
168
+ // one try of an update: false when another write came first, at the key or creating it
169
+ async #written(key, next, version) {
170
+ try {
171
+ if (version === undefined) {
172
+ return await this.create(key, next);
173
+ }
174
+ await this.set(key, next, { ifVersion: version });
175
+ return true;
176
+ }
177
+ catch (err) {
178
+ if (err instanceof ConflictError) {
179
+ return false;
180
+ }
181
+ throw err;
182
+ }
183
+ }
184
+ // the key as the core's errors show it, after its branch's owners
185
+ #shownKey(key) {
186
+ return `key ${JSON.stringify([...this.#under, keyText(key)].join('/'))}`;
187
+ }
138
188
  // a call on one key: its fields written where they leave from, its answer read where it arrived
139
189
  #ask(method, fields, idempotence, read) {
140
190
  // the caller's own fields, made the call's: an object more a call is one too many
@@ -354,106 +404,114 @@ export const jsonValues = {
354
404
  }
355
405
  },
356
406
  };
407
+ /** A row of nothing reads as the empty string. */
408
+ const stringValues = {
409
+ encode: v => {
410
+ if (typeof v !== 'string') {
411
+ throw new InvalidError(`a ${typeof v} in a bucket of strings`);
412
+ }
413
+ return tagged(kinds.text, utf8.encode(v));
414
+ },
415
+ decode: raw => {
416
+ if (raw === null) {
417
+ return '';
418
+ }
419
+ const text = textOf(bodyOf(raw, kinds.text, 'text'));
420
+ if (text === undefined) {
421
+ throw new CorruptError('a stored value is text that is not UTF-8');
422
+ }
423
+ return text;
424
+ },
425
+ };
426
+ /** A row of nothing reads as no bytes. */
427
+ const bytesValues = {
428
+ keeps: true,
429
+ encode: v => {
430
+ if (!(v instanceof Uint8Array)) {
431
+ throw new InvalidError(`a ${typeof v} in a bucket of bytes`);
432
+ }
433
+ return tagged(kinds.bytes, v);
434
+ },
435
+ decode: raw => (raw === null ? new Uint8Array(0) : bodyOf(raw, kinds.bytes, 'bytes')),
436
+ };
437
+ const intValues = {
438
+ encode: v => {
439
+ if (typeof v !== 'number' || !Number.isSafeInteger(v)) {
440
+ throw new InvalidError(`${String(v)} is not an integer a number holds exactly; open the bucket with type 'bigint'`);
441
+ }
442
+ return BigInt(v);
443
+ },
444
+ decode: raw => {
445
+ if (typeof raw !== 'bigint') {
446
+ throw new CorruptError(`a stored value is ${describe(raw)} where an integer was written`);
447
+ }
448
+ if (raw > BigInt(Number.MAX_SAFE_INTEGER) || raw < BigInt(Number.MIN_SAFE_INTEGER)) {
449
+ throw new CorruptError(`${raw} does not fit a number exactly; open the bucket with type 'bigint'`);
450
+ }
451
+ return Number(raw);
452
+ },
453
+ };
454
+ const bigintValues = {
455
+ encode: v => {
456
+ if (typeof v !== 'bigint' && !(typeof v === 'number' && Number.isSafeInteger(v))) {
457
+ throw new InvalidError(`${String(v)} is not an integer`);
458
+ }
459
+ return BigInt(v);
460
+ },
461
+ decode: raw => {
462
+ if (typeof raw !== 'bigint') {
463
+ throw new CorruptError(`a stored value is ${describe(raw)} where an integer was written`);
464
+ }
465
+ return raw;
466
+ },
467
+ };
468
+ const floatValues = {
469
+ encode: v => {
470
+ if (typeof v !== 'number') {
471
+ throw new InvalidError(`a ${typeof v} in a bucket of floats`);
472
+ }
473
+ const bytes = new Uint8Array(8);
474
+ new DataView(bytes.buffer).setFloat64(0, v);
475
+ return tagged(kinds.float, bytes);
476
+ },
477
+ decode: raw => {
478
+ const bits = bodyOf(raw, kinds.float, 'a float');
479
+ if (bits.length !== 8) {
480
+ throw new CorruptError(`a float from ${bits.length} bytes rather than 8`);
481
+ }
482
+ return new DataView(bits.buffer, bits.byteOffset, 8).getFloat64(0);
483
+ },
484
+ };
485
+ const boolValues = {
486
+ encode: v => {
487
+ if (typeof v !== 'boolean') {
488
+ throw new InvalidError(`a ${typeof v} in a bucket of booleans`);
489
+ }
490
+ return v ? 1n : 0n;
491
+ },
492
+ decode: raw => {
493
+ if (typeof raw !== 'bigint') {
494
+ throw new CorruptError(`a stored value is ${describe(raw)} where a boolean was written`);
495
+ }
496
+ return raw !== 0n;
497
+ },
498
+ };
357
499
  export function valuesOf(type) {
358
500
  switch (type) {
359
501
  case undefined:
360
502
  return jsonValues;
361
503
  case 'string':
362
- return {
363
- encode: v => {
364
- if (typeof v !== 'string') {
365
- throw new InvalidError(`a ${typeof v} in a bucket of strings`);
366
- }
367
- return tagged(kinds.text, utf8.encode(v));
368
- },
369
- decode: raw => {
370
- if (raw === null) {
371
- return '';
372
- }
373
- const text = textOf(bodyOf(raw, kinds.text, 'text'));
374
- if (text === undefined) {
375
- throw new CorruptError('a stored value is text that is not UTF-8');
376
- }
377
- return text;
378
- },
379
- };
504
+ return stringValues;
380
505
  case 'bytes':
381
- return {
382
- keeps: true,
383
- encode: v => {
384
- if (!(v instanceof Uint8Array)) {
385
- throw new InvalidError(`a ${typeof v} in a bucket of bytes`);
386
- }
387
- return tagged(kinds.bytes, v);
388
- },
389
- decode: raw => (raw === null ? new Uint8Array(0) : bodyOf(raw, kinds.bytes, 'bytes')),
390
- };
506
+ return bytesValues;
391
507
  case 'int':
392
- return {
393
- encode: v => {
394
- if (typeof v !== 'number' || !Number.isSafeInteger(v)) {
395
- throw new InvalidError(`${String(v)} is not an integer a number holds exactly; open the bucket with type 'bigint'`);
396
- }
397
- return BigInt(v);
398
- },
399
- decode: raw => {
400
- if (typeof raw !== 'bigint') {
401
- throw new CorruptError(`a stored value is ${describe(raw)} where an integer was written`);
402
- }
403
- if (raw > BigInt(Number.MAX_SAFE_INTEGER) || raw < BigInt(Number.MIN_SAFE_INTEGER)) {
404
- throw new CorruptError(`${raw} does not fit a number exactly; open the bucket with type 'bigint'`);
405
- }
406
- return Number(raw);
407
- },
408
- };
508
+ return intValues;
409
509
  case 'bigint':
410
- return {
411
- encode: v => {
412
- if (typeof v !== 'bigint' && !(typeof v === 'number' && Number.isSafeInteger(v))) {
413
- throw new InvalidError(`${String(v)} is not an integer`);
414
- }
415
- return BigInt(v);
416
- },
417
- decode: raw => {
418
- if (typeof raw !== 'bigint') {
419
- throw new CorruptError(`a stored value is ${describe(raw)} where an integer was written`);
420
- }
421
- return raw;
422
- },
423
- };
510
+ return bigintValues;
424
511
  case 'float':
425
- return {
426
- encode: v => {
427
- if (typeof v !== 'number') {
428
- throw new InvalidError(`a ${typeof v} in a bucket of floats`);
429
- }
430
- const bytes = new Uint8Array(8);
431
- new DataView(bytes.buffer).setFloat64(0, v);
432
- return tagged(kinds.float, bytes);
433
- },
434
- decode: raw => {
435
- const bits = bodyOf(raw, kinds.float, 'a float');
436
- if (bits.length !== 8) {
437
- throw new CorruptError(`a float from ${bits.length} bytes rather than 8`);
438
- }
439
- return new DataView(bits.buffer, bits.byteOffset, 8).getFloat64(0);
440
- },
441
- };
512
+ return floatValues;
442
513
  case 'bool':
443
- return {
444
- encode: v => {
445
- if (typeof v !== 'boolean') {
446
- throw new InvalidError(`a ${typeof v} in a bucket of booleans`);
447
- }
448
- return v ? 1n : 0n;
449
- },
450
- decode: raw => {
451
- if (typeof raw !== 'bigint') {
452
- throw new CorruptError(`a stored value is ${describe(raw)} where a boolean was written`);
453
- }
454
- return raw !== 0n;
455
- },
456
- };
514
+ return boolValues;
457
515
  }
458
516
  throw new InvalidError(`a bucket's type is string, bytes, int, bigint, float or bool, not ${String(type)}`);
459
517
  }
@@ -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
- // What the session writes while it reads: its frames wait for the read's
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
+ }