@agoric/swing-store 0.9.2-other-dev-d15096d.0.d15096d → 0.9.2-other-dev-ecf44df.0.ecf44df

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/ava.config.js ADDED
@@ -0,0 +1,11 @@
1
+ // This file configures AVA for this package. A config file next to each
2
+ // package.json avoids AVA's warning about using a config file from an
3
+ // ancestor directory:
4
+ // https://github.com/avajs/ava/blob/main/lib/cli.js
5
+ import base from '../../ava.config.js';
6
+
7
+ export default {
8
+ ...base,
9
+ timeout: '2m',
10
+ workerThreads: false,
11
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/swing-store",
3
- "version": "0.9.2-other-dev-d15096d.0.d15096d",
3
+ "version": "0.9.2-other-dev-ecf44df.0.ecf44df",
4
4
  "description": "Persistent storage for SwingSet",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -17,44 +17,35 @@
17
17
  "test:xs": "exit 0",
18
18
  "lint-fix": "yarn lint:eslint --fix",
19
19
  "lint": "yarn run -T run-s --continue-on-error 'lint:*'",
20
- "lint:types": "yarn run -T tsc",
21
- "lint:eslint": "yarn run -T eslint ."
20
+ "lint:types": "yarn run -T tsgo --tsBuildInfoFile tsconfig.tsgo.tsbuildinfo",
21
+ "lint:eslint": "node ../../scripts/eslint-repo.mjs ."
22
22
  },
23
23
  "dependencies": {
24
- "@agoric/internal": "0.3.3-other-dev-d15096d.0.d15096d",
25
- "@endo/base64": "^1.0.12",
26
- "@endo/bundle-source": "^4.1.2",
27
- "@endo/check-bundle": "^1.0.17",
28
- "@endo/errors": "^1.2.13",
29
- "@endo/nat": "^5.1.3",
30
- "better-sqlite3": "^10.1.0"
24
+ "@agoric/internal": "0.3.3-other-dev-ecf44df.0.ecf44df",
25
+ "@endo/base64": "^1.1.1",
26
+ "@endo/bundle-source": "^4.3.2",
27
+ "@endo/check-bundle": "^1.1.1",
28
+ "@endo/errors": "^1.3.1",
29
+ "@endo/nat": "^5.2.0",
30
+ "better-sqlite3": "^12.10.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@endo/init": "^1.1.12",
34
- "@endo/promise-kit": "^1.1.13",
35
- "@endo/stream": "^1.2.13",
33
+ "@endo/init": "^1.1.13",
34
+ "@endo/promise-kit": "^1.2.1",
35
+ "@endo/stream": "^1.3.1",
36
36
  "@types/better-sqlite3": "^7.6.13",
37
- "ava": "^5.3.0",
37
+ "ava": "^8.0.1",
38
38
  "c8": "^10.1.3",
39
39
  "tmp": "^0.2.1"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public"
43
43
  },
44
- "ava": {
45
- "files": [
46
- "test/**/*.test.*"
47
- ],
48
- "require": [
49
- "@endo/init/debug.js"
50
- ],
51
- "timeout": "2m"
52
- },
53
44
  "typeCoverage": {
54
- "atLeast": 80.49
45
+ "atLeast": 80.66
55
46
  },
56
47
  "engines": {
57
- "node": "^20.9 || ^22.11"
48
+ "node": "^22.11 || ^24.14"
58
49
  },
59
- "gitHead": "d15096dc4ff8b96e9b6cd11954c20d3a9efbb393"
50
+ "gitHead": "ecf44df5eadcc7a1d68745f056fca19155fb5231"
60
51
  }
package/src/archiver.js CHANGED
@@ -13,9 +13,9 @@ const streamFinished = promisify(streamFinishedCallback);
13
13
  /**
14
14
  * @param {string} dirPath
15
15
  * @param {object} powers
16
- * @param {Pick<import('fs'), 'createWriteStream' | 'mkdirSync' | 'renameSync'>} powers.fs
17
- * @param {Pick<import('path'), 'join'>} powers.path
18
- * @param {Pick<import('tmp'), 'fileSync'>} powers.tmp
16
+ * @param {Pick<typeof import('fs'), 'createWriteStream' | 'mkdirSync' | 'renameSync'>} powers.fs
17
+ * @param {Pick<typeof import('path'), 'join'>} powers.path
18
+ * @param {Pick<typeof import('tmp'), 'fileSync'>} powers.tmp
19
19
  */
20
20
  export const makeArchiveSnapshot = (dirPath, powers) => {
21
21
  const { fs, path, tmp } = powers;
@@ -42,7 +42,7 @@ export const makeArchiveSnapshot = (dirPath, powers) => {
42
42
  const writer = fs.createWriteStream('', { fd, flush: true });
43
43
  const reader = Readable.from(gzData);
44
44
  const destroyReader = promisify(reader.destroy.bind(reader));
45
- addCleanup(() => destroyReader(null));
45
+ addCleanup(() => destroyReader(undefined));
46
46
  reader.pipe(writer);
47
47
  await streamFinished(writer);
48
48
  fs.renameSync(tmpName, destPath);
@@ -55,9 +55,9 @@ harden(makeArchiveSnapshot);
55
55
  /**
56
56
  * @param {string} dirPath
57
57
  * @param {object} powers
58
- * @param {Pick<import('fs'), 'createWriteStream' | 'mkdirSync' | 'renameSync'>} powers.fs
59
- * @param {Pick<import('path'), 'join'>} powers.path
60
- * @param {Pick<import('tmp'), 'fileSync'>} powers.tmp
58
+ * @param {Pick<typeof import('fs'), 'createWriteStream' | 'mkdirSync' | 'renameSync'>} powers.fs
59
+ * @param {Pick<typeof import('path'), 'join'>} powers.path
60
+ * @param {Pick<typeof import('tmp'), 'fileSync'>} powers.tmp
61
61
  */
62
62
  export const makeArchiveTranscript = (dirPath, powers) => {
63
63
  const { fs, path, tmp } = powers;
@@ -1,3 +1,8 @@
1
+ /**
2
+ * @import {SwingStoreInternal} from './internal.js';
3
+ * @import {ArtifactMode} from './internal.js';
4
+ */
5
+
1
6
  /**
2
7
  * @param {Pick<SwingStoreInternal, 'bundleStore' | 'transcriptStore' | 'snapStore'>} internal
3
8
  * @param {Exclude<ArtifactMode, 'debug'>} checkMode
@@ -7,11 +12,6 @@ export function assertComplete(internal, checkMode) {
7
12
  // every bundle must be populated
8
13
  internal.bundleStore.assertComplete(checkMode);
9
14
 
10
- /**
11
- * @import {SwingStoreInternal} from './internal.js';
12
- * @import {ArtifactMode} from './internal.js';
13
- */
14
-
15
15
  // every 'isCurrent' transcript span must have all items
16
16
  // TODO: every vat with any data must have a isCurrent transcript
17
17
  // span
@@ -1,7 +1,7 @@
1
1
  // @ts-check
2
- import { createHash } from 'crypto';
3
- import { Readable } from 'stream';
4
- import { Buffer } from 'buffer';
2
+ import { createHash } from 'node:crypto';
3
+ import { Readable } from 'node:stream';
4
+ import { Buffer } from 'node:buffer';
5
5
  import { Fail, q } from '@endo/errors';
6
6
  import { encodeBase64, decodeBase64 } from '@endo/base64';
7
7
  import { checkBundle } from '@endo/check-bundle/lite.js';
package/src/exporter.js CHANGED
@@ -2,8 +2,7 @@ import sqlite3 from 'better-sqlite3';
2
2
 
3
3
  import { Fail, q } from '@endo/errors';
4
4
 
5
- import { dbFileInDirectory } from './util.js';
6
- import { getKeyType } from './kvStore.js';
5
+ import { dbFileInDirectory, getKVStoreKeyType } from './util.js';
7
6
  import { makeBundleStore } from './bundleStore.js';
8
7
  import { makeSnapStore } from './snapStore.js';
9
8
  import { makeSnapStoreIO } from './snapStoreIO.js';
@@ -133,7 +132,7 @@ export function makeSwingStoreExporter(dirPath, options = {}) {
133
132
  * section
134
133
  */
135
134
  function getHostKV(key) {
136
- getKeyType(key) === 'host' || Fail`getHostKV requires host keys`;
135
+ getKVStoreKeyType(key) === 'host' || Fail`getHostKV requires host keys`;
137
136
  // @ts-expect-error unknown
138
137
  return sqlKVGet.get(key);
139
138
  }
@@ -151,7 +150,7 @@ export function makeSwingStoreExporter(dirPath, options = {}) {
151
150
  */
152
151
  async function* getExportData() {
153
152
  for (const { key, value } of sqlGetAllKVData.iterate()) {
154
- if (getKeyType(key) === 'consensus') {
153
+ if (getKVStoreKeyType(key) === 'consensus') {
155
154
  yield [`kv.${key}`, value];
156
155
  }
157
156
  }
package/src/hasher.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Fail } from '@endo/errors';
2
2
 
3
- import { createHash } from 'crypto';
3
+ import { createHash } from 'node:crypto';
4
4
 
5
5
  /**
6
6
  * @typedef {{
package/src/index.js CHANGED
@@ -5,7 +5,8 @@ export { importSwingStore } from './importer.js';
5
5
  export { makeArchiveSnapshot, makeArchiveTranscript } from './archiver.js';
6
6
 
7
7
  // for the benefit of tools like SwingSet/misc-tools/replay-transcript.js
8
- export { makeKVStore, getKeyType } from './kvStore.js';
8
+ export { makeKVStore } from '@agoric/internal/src/kv-store.js';
9
+ export { getKVStoreKeyType, getKVStoreKeyType as getKeyType } from './util.js';
9
10
  export { makeTranscriptStore } from './transcriptStore.js';
10
11
  export { makeSnapStore } from './snapStore.js';
11
12
  export { makeSnapStoreIO } from './snapStoreIO.js';
package/src/internal.js CHANGED
@@ -1,17 +1,17 @@
1
1
  import { Fail, q } from '@endo/errors';
2
2
 
3
- /**
4
- * @import {KVStore} from './kvStore.js';
5
- */
6
-
7
3
  /**
8
4
  * @import {SnapStoreInternal} from './snapStore.js';
9
5
  * @import {TranscriptStoreInternal} from './transcriptStore.js';
10
6
  * @import {BundleStoreInternal} from './bundleStore.js';
11
- *
7
+ * @import {KVStore} from '@agoric/internal/src/kv-store.js';
8
+ * @import {Database} from 'better-sqlite3';
9
+ */
10
+
11
+ /**
12
12
  * @typedef {{
13
13
  * dirPath: string | null,
14
- * db: ReturnType<import('better-sqlite3')>,
14
+ * db: Database,
15
15
  * kvStore: KVStore,
16
16
  * transcriptStore: TranscriptStoreInternal,
17
17
  * snapStore: SnapStoreInternal,
package/src/snapStore.js CHANGED
@@ -1,8 +1,12 @@
1
1
  // @ts-check
2
- import { createHash } from 'crypto';
3
- import { finished as finishedCallback, PassThrough, Readable } from 'stream';
4
- import { promisify } from 'util';
5
- import { createGzip, createGunzip } from 'zlib';
2
+ import { createHash } from 'node:crypto';
3
+ import {
4
+ finished as finishedCallback,
5
+ PassThrough,
6
+ Readable,
7
+ } from 'node:stream';
8
+ import { promisify } from 'node:util';
9
+ import { createGzip, createGunzip } from 'node:zlib';
6
10
  import { Fail, q } from '@endo/errors';
7
11
  import { withDeferredCleanup } from '@agoric/internal';
8
12
  import { buffer } from './util.js';
@@ -228,7 +232,7 @@ export function makeSnapStore(
228
232
  await measureSeconds(async () => {
229
233
  const snapReader = Readable.from(snapshotStream);
230
234
  const destroyReader = promisify(snapReader.destroy.bind(snapReader));
231
- addCleanup(() => destroyReader(null));
235
+ addCleanup(() => destroyReader(undefined));
232
236
  snapReader.on('data', chunk => {
233
237
  uncompressedSize += chunk.length;
234
238
  });
@@ -1,4 +1,4 @@
1
- import { performance } from 'perf_hooks';
1
+ import { performance } from 'node:perf_hooks';
2
2
  import { makeMeasureSeconds } from '@agoric/internal';
3
3
 
4
4
  export function makeSnapStoreIO() {
package/src/swingStore.js CHANGED
@@ -1,17 +1,16 @@
1
1
  // @ts-check
2
- /* eslint-env node */
3
- import * as fs from 'fs';
4
- import * as pathlib from 'path';
2
+ import * as fs from 'node:fs';
3
+ import * as pathlib from 'node:path';
5
4
 
6
5
  import sqlite3 from 'better-sqlite3';
7
6
 
8
7
  import { Fail, q } from '@endo/errors';
9
8
 
10
9
  import { attenuate } from '@agoric/internal';
10
+ import { makeKVStore } from '@agoric/internal/src/kv-store.js';
11
11
  import { TRUE } from '@agoric/internal/src/js-utils.js';
12
12
 
13
- import { dbFileInDirectory } from './util.js';
14
- import { makeKVStore, getKeyType } from './kvStore.js';
13
+ import { dbFileInDirectory, getKVStoreKeyType } from './util.js';
15
14
  import { makeTranscriptStore } from './transcriptStore.js';
16
15
  import { makeSnapStore } from './snapStore.js';
17
16
  import { makeBundleStore } from './bundleStore.js';
@@ -35,7 +34,7 @@ const IN_MEMORY = ':memory:';
35
34
  */
36
35
 
37
36
  /**
38
- * @import {KVStore} from './kvStore.js';
37
+ * @import {KVStore} from '@agoric/internal/src/kv-store.js';
39
38
  *
40
39
  * @import {SnapStore} from './snapStore.js';
41
40
  * @import {SnapshotResult} from './snapStore.js';
@@ -358,7 +357,7 @@ export function makeSwingStore(path, forceReset, options = {}) {
358
357
  const kernelKVStore = {
359
358
  ...kvStore,
360
359
  set(key, value) {
361
- const keyType = getKeyType(key);
360
+ const keyType = getKVStoreKeyType(key);
362
361
  keyType !== 'host' || Fail`kernelKVStore refuses host keys`;
363
362
  kvStore.set(key, value);
364
363
  if (keyType === 'consensus') {
@@ -372,7 +371,7 @@ export function makeSwingStore(path, forceReset, options = {}) {
372
371
  }
373
372
  },
374
373
  delete(key) {
375
- const keyType = getKeyType(key);
374
+ const keyType = getKVStoreKeyType(key);
376
375
  keyType !== 'host' || Fail`kernelKVStore refuses host keys`;
377
376
  kvStore.delete(key);
378
377
  if (keyType === 'consensus') {
@@ -388,12 +387,12 @@ export function makeSwingStore(path, forceReset, options = {}) {
388
387
  const hostKVStore = {
389
388
  ...kvStore,
390
389
  set(key, value) {
391
- const keyType = getKeyType(key);
390
+ const keyType = getKVStoreKeyType(key);
392
391
  keyType === 'host' || Fail`hostKVStore requires host keys`;
393
392
  kvStore.set(key, value);
394
393
  },
395
394
  delete(key) {
396
- const keyType = getKeyType(key);
395
+ const keyType = getKVStoreKeyType(key);
397
396
  keyType === 'host' || Fail`hostKVStore requires host keys`;
398
397
  kvStore.delete(key);
399
398
  },
@@ -1,6 +1,6 @@
1
1
  // @ts-check
2
- import { Readable } from 'stream';
3
- import { Buffer } from 'buffer';
2
+ import { Readable } from 'node:stream';
3
+ import { Buffer } from 'node:buffer';
4
4
  import { Fail, q } from '@endo/errors';
5
5
  import BufferLineTransform from '@agoric/internal/src/node/buffer-line-transform.js';
6
6
  import { createSHA256 } from './hasher.js';
@@ -3,7 +3,7 @@ export type {
3
3
  SwingStoreKernelStorage,
4
4
  SwingStoreHostStorage,
5
5
  } from './swingStore.js';
6
- export type { KVStore } from './kvStore.js';
6
+ export type { KVStore } from '@agoric/internal/src/kv-store.js';
7
7
  export type { BundleStore } from './bundleStore.js';
8
8
  export type {
9
9
  SnapStore,
package/src/util.js CHANGED
@@ -1,5 +1,6 @@
1
- import path from 'path';
2
- import { Buffer } from 'buffer';
1
+ import { Buffer } from 'node:buffer';
2
+ import path from 'node:path';
3
+ import { Fail } from '@endo/errors';
3
4
 
4
5
  /**
5
6
  * @import {AnyIterable} from './exporter.js';
@@ -24,3 +25,16 @@ export function dbFileInDirectory(dirPath) {
24
25
  const filePath = path.resolve(dirPath, 'swingstore.sqlite');
25
26
  return filePath;
26
27
  }
28
+
29
+ /**
30
+ * @param {string} key
31
+ */
32
+ export function getKVStoreKeyType(key) {
33
+ typeof key === 'string' || Fail`key must be a string`;
34
+ if (key.startsWith('local.')) {
35
+ return 'local';
36
+ } else if (key.startsWith('host.')) {
37
+ return 'host';
38
+ }
39
+ return 'consensus';
40
+ }
@@ -1,6 +1,8 @@
1
1
  // @ts-check
2
+ import '@endo/init/debug.js';
3
+
2
4
  import test from 'ava';
3
- import { Buffer } from 'buffer';
5
+ import { Buffer } from 'node:buffer';
4
6
  import tmp from 'tmp';
5
7
  import { makeTempDirFactory } from '@agoric/internal/src/tmpDir.js';
6
8
  import { createSHA256 } from '../src/hasher.js';
@@ -157,8 +159,14 @@ test('unknown format', t => {
157
159
  const { kernelStorage } = initSwingStore();
158
160
  const { bundleStore } = kernelStorage;
159
161
  const unknownID = 'b1999-whoa-futuristic';
160
- /** @import {Bundle} from '../src/bundleStore.js' */
161
- t.throws(() => bundleStore.addBundle(unknownID, /** @type {Bundle} */ ({})), {
162
- message: /unsupported BundleID/,
163
- });
162
+ t.throws(
163
+ () =>
164
+ bundleStore.addBundle(
165
+ unknownID,
166
+ /** @type {import('../src/bundleStore.js').Bundle} */ ({}),
167
+ ),
168
+ {
169
+ message: /unsupported BundleID/,
170
+ },
171
+ );
164
172
  });
@@ -1,4 +1,6 @@
1
1
  // @ts-check
2
+ import '@endo/init/debug.js';
3
+
2
4
  import test from 'ava';
3
5
 
4
6
  import { Buffer } from 'node:buffer';
@@ -1,3 +1,5 @@
1
+ import '@endo/init/debug.js';
2
+
1
3
  import test from 'ava';
2
4
  import tmp from 'tmp';
3
5
 
@@ -1,5 +1,7 @@
1
1
  // @ts-check
2
2
 
3
+ import '@endo/init/debug.js';
4
+
3
5
  import { Buffer } from 'node:buffer';
4
6
 
5
7
  import test from 'ava';
package/test/exports.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Buffer } from 'buffer';
1
+ import { Buffer } from 'node:buffer';
2
2
  import { makeB0ID } from './util.js';
3
3
 
4
4
  /**
@@ -1,3 +1,5 @@
1
+ import '@endo/init/debug.js';
2
+
1
3
  import test from 'ava';
2
4
 
3
5
  import { createSHA256 } from '../src/hasher.js';
@@ -1,9 +1,10 @@
1
1
  // @ts-check
2
+ import '@endo/init/debug.js';
2
3
 
3
- import path from 'path';
4
- import { createGunzip } from 'zlib';
5
- import { Readable } from 'stream';
6
- import { Buffer } from 'buffer';
4
+ import path from 'node:path';
5
+ import { createGunzip } from 'node:zlib';
6
+ import { Readable } from 'node:stream';
7
+ import { Buffer } from 'node:buffer';
7
8
 
8
9
  import sqlite3 from 'better-sqlite3';
9
10
  import test from 'ava';
@@ -62,8 +63,8 @@ test('import empty', async t => {
62
63
  });
63
64
 
64
65
  const importTest = test.macro(async (t, mode) => {
65
- /** @import {ArtifactMode} from '../src/internal.js' */
66
- const artifactMode = /** @type {ArtifactMode} */ (mode);
66
+ const artifactMode =
67
+ /** @type {import('../src/internal.js').ArtifactMode} */ (mode);
67
68
 
68
69
  const [dbDir, cleanup] = tmpDir('testdb');
69
70
  t.teardown(cleanup);
@@ -1,6 +1,7 @@
1
1
  // @ts-check
2
+ import '@endo/init/debug.js';
2
3
 
3
- import path from 'path';
4
+ import path from 'node:path';
4
5
  import test from 'ava';
5
6
  import sqlite3 from 'better-sqlite3';
6
7
  import tmp from 'tmp';
@@ -1,4 +1,6 @@
1
1
  // @ts-check
2
+ import '@endo/init/debug.js';
3
+
2
4
  import test from 'ava';
3
5
 
4
6
  import { Buffer } from 'node:buffer';
@@ -1,5 +1,7 @@
1
1
  // @ts-check
2
2
 
3
+ import '@endo/init/debug.js';
4
+
3
5
  import test from 'ava';
4
6
  import tmp from 'tmp';
5
7
 
@@ -151,7 +153,7 @@ const testTranscriptStore = test.macro({
151
153
  return `${prefix.replace(/.$/, '$& ')}${type} transcriptStore ${detail}`;
152
154
  },
153
155
  async exec(t, { ephemeral, keepTranscripts }) {
154
- let dbDir = null;
156
+ let dbDir = /** @type {string | null} */ (null);
155
157
  if (!ephemeral) {
156
158
  const [tmpPath, cleanup] = tmpDir('testdb');
157
159
  t.teardown(cleanup);
@@ -1,4 +1,6 @@
1
1
  // @ts-check
2
+ import '@endo/init/debug.js';
3
+
2
4
  import test from 'ava';
3
5
 
4
6
  import sqlite3 from 'better-sqlite3';
package/tsconfig.json CHANGED
@@ -1,9 +1,8 @@
1
1
  // This file can contain .js-specific Typescript compiler config.
2
2
  {
3
3
  "extends": "../../tsconfig.json",
4
- "compilerOptions": {},
5
4
  "include": [
6
- "src/**/*.js",
7
- "test/**/*.js",
5
+ "src",
6
+ "test",
8
7
  ],
9
8
  }
package/src/kvStore.js DELETED
@@ -1,173 +0,0 @@
1
- // @ts-check
2
- import { Fail } from '@endo/errors';
3
-
4
- /**
5
- * @typedef {{
6
- * has: (key: string) => boolean,
7
- * get: (key: string) => string | undefined,
8
- * getNextKey: (previousKey: string) => string | undefined,
9
- * set: (key: string, value: string, bypassHash?: boolean ) => void,
10
- * delete: (key: string) => void,
11
- * }} KVStore
12
- */
13
-
14
- /**
15
- * @param {string} key
16
- */
17
- export function getKeyType(key) {
18
- typeof key === 'string' || Fail`key must be a string`;
19
- if (key.startsWith('local.')) {
20
- return 'local';
21
- } else if (key.startsWith('host.')) {
22
- return 'host';
23
- }
24
- return 'consensus';
25
- }
26
-
27
- /**
28
- * @param {object} db The SQLite database connection.
29
- * @param {() => void} ensureTxn Called before mutating methods to establish a DB transaction
30
- * @param {(...args: string[]) => void} trace Called after set/delete to record a debug log
31
- * @returns { KVStore }
32
- */
33
-
34
- export function makeKVStore(db, ensureTxn, trace) {
35
- db.exec(`
36
- CREATE TABLE IF NOT EXISTS kvStore (
37
- key TEXT,
38
- value TEXT,
39
- PRIMARY KEY (key)
40
- )
41
- `);
42
-
43
- const sqlKVGet = db.prepare(`
44
- SELECT value
45
- FROM kvStore
46
- WHERE key = ?
47
- `);
48
- sqlKVGet.pluck(true);
49
-
50
- /**
51
- * Obtain the value stored for a given key.
52
- *
53
- * @param {string} key The key whose value is sought.
54
- *
55
- * @returns {string | undefined} the (string) value for the given key, or
56
- * undefined if there is no such value.
57
- *
58
- * @throws if key is not a string.
59
- */
60
- function get(key) {
61
- typeof key === 'string' || Fail`key must be a string`;
62
- return sqlKVGet.get(key);
63
- }
64
-
65
- const sqlKVGetNextKey = db.prepare(`
66
- SELECT key
67
- FROM kvStore
68
- WHERE key > ?
69
- LIMIT 1
70
- `);
71
- sqlKVGetNextKey.pluck(true);
72
-
73
- /**
74
- * getNextKey enables callers to iterate over all keys within a
75
- * given range. To build an iterator of all keys from start
76
- * (inclusive) to end (exclusive), do:
77
- *
78
- * function* iterate(start, end) {
79
- * if (kvStore.has(start)) {
80
- * yield start;
81
- * }
82
- * let prev = start;
83
- * while (true) {
84
- * let next = kvStore.getNextKey(prev);
85
- * if (!next || next >= end) {
86
- * break;
87
- * }
88
- * yield next;
89
- * prev = next;
90
- * }
91
- * }
92
- *
93
- * @param {string} previousKey The key returned will always be later than this one.
94
- *
95
- * @returns {string | undefined} a key string, or undefined if we reach the end of the store
96
- *
97
- * @throws if previousKey is not a string
98
- */
99
-
100
- function getNextKey(previousKey) {
101
- typeof previousKey === 'string' || Fail`previousKey must be a string`;
102
- return sqlKVGetNextKey.get(previousKey);
103
- }
104
-
105
- /**
106
- * Test if the state contains a value for a given key.
107
- *
108
- * @param {string} key The key that is of interest.
109
- *
110
- * @returns {boolean} true if a value is stored for the key, false if not.
111
- *
112
- * @throws if key is not a string.
113
- */
114
- function has(key) {
115
- typeof key === 'string' || Fail`key must be a string`;
116
- return get(key) !== undefined;
117
- }
118
-
119
- const sqlKVSet = db.prepare(`
120
- INSERT INTO kvStore (key, value)
121
- VALUES (?, ?)
122
- ON CONFLICT DO UPDATE SET value = excluded.value
123
- `);
124
-
125
- /**
126
- * Store a value for a given key. The value will replace any prior value if
127
- * there was one.
128
- *
129
- * @param {string} key The key whose value is being set.
130
- * @param {string} value The value to set the key to.
131
- *
132
- * @throws if either parameter is not a string.
133
- */
134
- function set(key, value) {
135
- typeof key === 'string' || Fail`key must be a string`;
136
- typeof value === 'string' || Fail`value must be a string`;
137
- // synchronous read after write within a transaction is safe
138
- // The transaction's overall success will be awaited during commit
139
- ensureTxn();
140
- sqlKVSet.run(key, value);
141
- trace('set', key, value);
142
- }
143
-
144
- const sqlKVDel = db.prepare(`
145
- DELETE FROM kvStore
146
- WHERE key = ?
147
- `);
148
-
149
- /**
150
- * Remove any stored value for a given key. It is permissible for there to
151
- * be no existing stored value for the key.
152
- *
153
- * @param {string} key The key whose value is to be deleted
154
- *
155
- * @throws if key is not a string.
156
- */
157
- function del(key) {
158
- typeof key === 'string' || Fail`key must be a string`;
159
- ensureTxn();
160
- sqlKVDel.run(key);
161
- trace('del', key);
162
- }
163
-
164
- const kvStore = {
165
- has,
166
- get,
167
- getNextKey,
168
- set,
169
- delete: del,
170
- };
171
-
172
- return kvStore;
173
- }