@zukhruf/sqlite-open 0.8.15 → 0.8.17

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/README.md CHANGED
@@ -67,7 +67,7 @@ true
67
67
  - A database in memory (`:memory:`, `''`, or a `file::memory:` URI) has no file, so it gets no journal.
68
68
  - The busy timeout is 5000 milliseconds, unless the caller gives `timeout`. With `timeout: 0`, a lock of another connection fails at once with `SQLITE_BUSY` (5).
69
69
  - The owner opens with extension loading on, so that a journal can load the native library. After the journal step, extension loading is as the caller asked with `allowExtension`. The default is off.
70
- - When the open or the journal step fails, `open` closes the connection and throws. An unavailable file throws `UnavailableError`. All other failures throw the error of SQLite.
70
+ - When the open or the journal step fails, `open` closes the connection and throws. An unavailable file throws `UnavailableError`: a `MissingError` when the file or a folder on its path is not there, a `DeniedError` when the system denies access. All other failures throw the error of SQLite.
71
71
  - The package throws its own `UnavailableError`. A host that needs its own error translates the error of the package at its boundary, and keeps the error of the package as `cause`.
72
72
 
73
73
  `open` replaces `new DatabaseSync` at the call site, with three differences:
@@ -80,25 +80,29 @@ true
80
80
 
81
81
  Each journal is a strategy object: the caller gives an instance, for example `open(path, { journal: new RollbackJournal() })`. A new journal is a class that implements `Journal`, with one method: `apply(database)`.
82
82
 
83
- | Journal | What it does | Use it for |
84
- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------- |
85
- | `WriteAheadLog` (default) | `PRAGMA journal_mode = WAL` and `PRAGMA journal_size_limit = 67108864` (64 MiB). SQLite deletes `-wal` and `-shm` at the last close. | An owner whose readers can write the folder. |
86
- | `PersistentWriteAheadLog` | The same steps, after the native library turns on persistent WAL. `-wal` and `-shm` stay after the last close. | An owner whose readers cannot write the folder, for example agents that run `sqlite3 -readonly` in a sandbox. |
87
- | `RollbackJournal` | `PRAGMA journal_mode = DELETE`. The first access rolls back a hot journal. | A file that the owner builds and then renames or deletes. A copy that must lose a hot journal. |
88
- | `UnchangedJournal` | Nothing. | An owner that sets its own journal, for example a lock file whose `PRAGMA` must wait for the lock. |
83
+ | Journal | What it does | Use it for |
84
+ | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
85
+ | `WriteAheadLog` (default) | `PRAGMA journal_mode = WAL`, `PRAGMA journal_size_limit = 67108864` (64 MiB) and `PRAGMA synchronous = NORMAL`. SQLite deletes `-wal` and `-shm` at the last close. | An owner whose readers can write the folder. |
86
+ | `PersistentWriteAheadLog` | The same steps, after the native library turns on persistent WAL. `-wal` and `-shm` stay after the last close. | An owner whose readers cannot write the folder, for example agents that run `sqlite3 -readonly` in a sandbox. |
87
+ | `RollbackJournal` | `PRAGMA journal_mode = DELETE`. The first access rolls back a hot journal. | A file that the owner builds and then renames or deletes. A copy that must lose a hot journal. |
88
+ | `UnchangedJournal` | Nothing. | An owner that sets its own journal, for example a lock file whose `PRAGMA` must wait for the lock. |
89
89
 
90
90
  The size limit keeps a write-ahead log small after a large commit. Without it, the `-wal` file of a 20 MB commit stays 20 MB.
91
91
 
92
92
  `WriteAheadLog` and `PersistentWriteAheadLog` take an optional page size, for example `new WriteAheadLog({ pageSize: 32768 })`. SQLite sets the page size of a file only before its first write, and never in a write-ahead log. The write-ahead log step is that first write, so the journal sets the page size just before it. An existing file keeps its own page size.
93
93
 
94
+ A write-ahead log syncs the `-wal` file to the disk before each checkpoint, not at each commit (ADR 0007). A crash of the program loses no commit. A power loss or a crash of the operating system can roll back the commits after the last checkpoint. The database file stays consistent. An owner whose commits must survive a power loss gives `synchronous: 'full'`, for example `new WriteAheadLog({ synchronous: 'full' })`. SQLite then syncs at each commit. On Linux, a commit of one row is about 12 times slower with `'full'`.
95
+
94
96
  ## Errors
95
97
 
96
- | Error | When |
97
- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
98
- | `UnavailableError` | The file is missing, or the system denies access to it: `SQLITE_CANTOPEN` (14) from the open, a privacy denial too, or `SQLITE_READONLY_DIRECTORY` (1544) from the journal step, when the folder denies the write of the journal. `path` is the path. `cause` is the error of SQLite. `UnavailableError.from(path, error)` also accepts `ENOENT`, `ENOTDIR`, `EACCES` and `EPERM` from the file system. |
99
- | `Error` | `PersistentWriteAheadLog` runs on a platform that has no native library. The message names the platform, and the platforms that have one. |
100
- | `Error` | SQLite did not change the journal to a write-ahead log. The message gives the journal that SQLite kept. |
101
- | The error of SQLite | All other failures, for example a file that is not a database. |
98
+ | Error | When |
99
+ | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
100
+ | `UnavailableError` | The file is missing, or the system denies access to it: `SQLITE_CANTOPEN` (14) from the open, a privacy denial too, or `SQLITE_READONLY_DIRECTORY` (1544) from the journal step, when the folder denies the write of the journal. `path` is the path. `cause` is the error of SQLite. It is one of the two classes below when the system says which case it is. A case that the system does not decide is a plain `UnavailableError`, for example a file that SQLite cannot open but the file system can. `UnavailableError.from(path, error)` also accepts `ENOENT`, `ENOTDIR`, `EACCES` and `EPERM` from the file system. |
101
+ | `MissingError` | An `UnavailableError`: the file, or a folder on its path, is not there. For `SQLITE_CANTOPEN` (14), a read-only open of the file system gave `ENOENT` or `ENOTDIR`. The caller can check the path, or make the database. |
102
+ | `DeniedError` | An `UnavailableError`: the system denies this process access to the file, or to a folder on its path. For `SQLITE_CANTOPEN` (14), a read-only open of the file system gave `EACCES` or `EPERM`. `SQLITE_READONLY_DIRECTORY` (1544) is always a `DeniedError`. The caller can give the process access. |
103
+ | `Error` | `PersistentWriteAheadLog` runs on a platform that has no native library. The message names the platform, and the platforms that have one. |
104
+ | `Error` | SQLite did not change the journal to a write-ahead log. The message gives the journal that SQLite kept. |
105
+ | The error of SQLite | All other failures, for example a file that is not a database. |
102
106
 
103
107
  ## Platforms
104
108
 
@@ -124,7 +128,7 @@ A bundler that bundles this package does not copy the `lib` folder. A bundle tha
124
128
  | Apple's system SQLite | It keeps persistent WAL by default. Its `sqlite3` leaves `-wal` and `-shm` after it closes. |
125
129
  | `better-sqlite3` | Its busy timeout is 5000 milliseconds by default. |
126
130
  | Python `sqlite3` | `sqlite3.connect(path, timeout=5.0)`: its busy timeout is 5 seconds by default. |
127
- | Rails 8 | A SQLite database gets a write-ahead log and `journal_size_limit = 67108864` by default. |
131
+ | Rails 8 | A SQLite database gets a write-ahead log, `journal_size_limit = 67108864` and `synchronous = NORMAL` by default. |
128
132
  | `sqlite3 -readonly` | It opens a reader. A missing file fails with "unable to open database file", the text of CANTOPEN. |
129
133
 
130
134
  ## Documentation
@@ -135,6 +139,8 @@ A bundler that bundles this package does not copy the `lib` folder. A bundle tha
135
139
  - [0004 The journal is a strategy](./docs/adr/0004-the-journal-is-a-strategy.md)
136
140
  - [0005 One map of failures and a busy timeout of 5 seconds](./docs/adr/0005-one-map-of-failures-and-a-busy-timeout-of-5-seconds.md)
137
141
  - [0006 Zig builds every native library in one Linux job](./docs/adr/0006-zig-builds-every-native-library-in-one-linux-job.md)
142
+ - [0007 A write-ahead log syncs at checkpoints](./docs/adr/0007-a-write-ahead-log-syncs-at-checkpoints.md)
143
+ - [0008 A missing file and a denied file are two classes](./docs/adr/0008-a-missing-file-and-a-denied-file-are-two-classes.md)
138
144
 
139
145
  ## Development
140
146
 
@@ -156,7 +162,7 @@ src/
156
162
  persistent-write-ahead-log.ts PersistentWriteAheadLog
157
163
  rollback-journal.ts RollbackJournal
158
164
  unchanged-journal.ts UnchangedJournal
159
- unavailable-error.ts UnavailableError
165
+ unavailable-error.ts UnavailableError, MissingError, DeniedError
160
166
  open.test.ts the tests, on real SQLite files and processes
161
167
  native/
162
168
  persist_wal.c the native library
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export type { Journal } from './journal.ts';
2
2
  export { open, type OwnerOptions, type ReaderOptions } from './open.ts';
3
3
  export { PersistentWriteAheadLog } from './persistent-write-ahead-log.ts';
4
4
  export { RollbackJournal } from './rollback-journal.ts';
5
- export { UnavailableError } from './unavailable-error.ts';
5
+ export { DeniedError, MissingError, UnavailableError, } from './unavailable-error.ts';
6
6
  export { UnchangedJournal } from './unchanged-journal.ts';
7
7
  export { type PageSize, WriteAheadLog } from './write-ahead-log.ts';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,KAAK,QAAQ,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EACL,WAAW,EACX,YAAY,EACZ,gBAAgB,GACjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,KAAK,QAAQ,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC"}
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export { open } from "./open.js";
2
2
  export { PersistentWriteAheadLog } from "./persistent-write-ahead-log.js";
3
3
  export { RollbackJournal } from "./rollback-journal.js";
4
- export { UnavailableError } from "./unavailable-error.js";
4
+ export { DeniedError, MissingError, UnavailableError, } from "./unavailable-error.js";
5
5
  export { UnchangedJournal } from "./unchanged-journal.js";
6
6
  export { WriteAheadLog } from "./write-ahead-log.js";
7
7
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAyC,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAiB,aAAa,EAAE,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAyC,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EACL,WAAW,EACX,YAAY,EACZ,gBAAgB,GACjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAiB,aAAa,EAAE,MAAM,sBAAsB,CAAC"}
@@ -1,15 +1,13 @@
1
1
  import type { DatabaseSync } from 'node:sqlite';
2
2
  import type { Journal } from './journal.ts';
3
- import { type PageSize } from './write-ahead-log.ts';
3
+ import { type WriteAheadLogOptions } from './write-ahead-log.ts';
4
4
  /**
5
5
  * A write-ahead log whose -wal and -shm files stay after the last connection
6
6
  * closes, for readers that cannot write the folder (ADR 0003).
7
7
  */
8
8
  export declare class PersistentWriteAheadLog implements Journal {
9
9
  #private;
10
- constructor(options?: {
11
- readonly pageSize?: PageSize;
12
- });
10
+ constructor(options?: WriteAheadLogOptions);
13
11
  apply(database: DatabaseSync): void;
14
12
  }
15
13
  //# sourceMappingURL=persistent-write-ahead-log.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"persistent-write-ahead-log.d.ts","sourceRoot":"","sources":["../src/persistent-write-ahead-log.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,KAAK,QAAQ,EAAiB,MAAM,sBAAsB,CAAC;AAWpE;;;GAGG;AACH,qBAAa,uBAAwB,YAAW,OAAO;;gBAGzC,OAAO,GAAE;QAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAA;KAAO;IAI1D,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;CAsBpC"}
1
+ {"version":3,"file":"persistent-write-ahead-log.d.ts","sourceRoot":"","sources":["../src/persistent-write-ahead-log.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAWhF;;;GAGG;AACH,qBAAa,uBAAwB,YAAW,OAAO;;gBAGzC,OAAO,GAAE,oBAAyB;IAI9C,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;CAsBpC"}
@@ -1 +1 @@
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BACrB,CAAC,CAAC,MAAM,IAAI,KAAK,CAAC;gBAClB,KAAK,CAAC,IAAI,KAAK,2BAA2B;gBAE1C,MAAM,KAAK,CAAC;YACd,MAAM,IAAI,KAAK,CACb,kDAAkD,QAAQ,oBAAoB,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAC5G,EAAE,KAAK,EAAE,CACV,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC5B,CAAC;CACF"}
@@ -4,8 +4,17 @@ export declare class UnavailableError extends Error {
4
4
  constructor(path: string, cause: unknown);
5
5
  /**
6
6
  * The UnavailableError for an error of SQLite or of the file system that
7
- * means the file is missing or denied (ADR 0005), else undefined.
7
+ * means the file is missing or denied (ADR 0005), else undefined. It is a
8
+ * MissingError or a DeniedError when the error says which (ADR 0008).
8
9
  */
9
10
  static from(path: string, error: unknown): UnavailableError | undefined;
10
11
  }
12
+ /** The file of the database, or a folder on its path, is not there. */
13
+ export declare class MissingError extends UnavailableError {
14
+ constructor(path: string, cause: unknown);
15
+ }
16
+ /** The system keeps this process from the file of the database, or from a folder on its path. */
17
+ export declare class DeniedError extends UnavailableError {
18
+ constructor(path: string, cause: unknown);
19
+ }
11
20
  //# sourceMappingURL=unavailable-error.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"unavailable-error.d.ts","sourceRoot":"","sources":["../src/unavailable-error.ts"],"names":[],"mappings":"AAaA,yEAAyE;AACzE,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO;IASxC;;;OAGG;IACH,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,gBAAgB,GAAG,SAAS;CAiBxE"}
1
+ {"version":3,"file":"unavailable-error.d.ts","sourceRoot":"","sources":["../src/unavailable-error.ts"],"names":[],"mappings":"AAiBA,yEAAyE;AACzE,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO;IASxC;;;;OAIG;IACH,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,gBAAgB,GAAG,SAAS;CAWxE;AAED,uEAAuE;AACvE,qBAAa,YAAa,SAAQ,gBAAgB;gBACpC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO;CAKzC;AAED,iGAAiG;AACjG,qBAAa,WAAY,SAAQ,gBAAgB;gBACnC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO;CAKzC"}
@@ -1,5 +1,8 @@
1
- // The file system codes for a file that is missing, or that the system denies.
2
- const unavailableCodes = new Set(['ENOENT', 'ENOTDIR', 'EACCES', 'EPERM']);
1
+ import { closeSync, openSync } from 'node:fs';
2
+ // The file system codes for a file, or a folder on its path, that is not there.
3
+ const missingCodes = new Set(['ENOENT', 'ENOTDIR']);
4
+ // The file system codes for a file that the system keeps from this process.
5
+ const deniedCodes = new Set(['EACCES', 'EPERM']);
3
6
  // SQLite's CANTOPEN (14). A missing or denied file fails the open() syscall,
4
7
  // and SQLite reports that as CANTOPEN, a privacy denial of macOS too (probed:
5
8
  // a missing file or folder, chmod 000, a symlink loop and a TCC denial all
@@ -19,21 +22,67 @@ export class UnavailableError extends Error {
19
22
  }
20
23
  /**
21
24
  * The UnavailableError for an error of SQLite or of the file system that
22
- * means the file is missing or denied (ADR 0005), else undefined.
25
+ * means the file is missing or denied (ADR 0005), else undefined. It is a
26
+ * MissingError or a DeniedError when the error says which (ADR 0008).
23
27
  */
24
28
  static from(path, error) {
25
29
  if (!Error.isError(error))
26
30
  return undefined;
27
- if ('errcode' in error &&
28
- typeof error.errcode === 'number' &&
29
- (error.errcode === SQLITE_CANTOPEN ||
30
- error.errcode === SQLITE_READONLY_DIRECTORY))
31
- return new UnavailableError(path, error);
32
- if ('code' in error &&
33
- typeof error.code === 'string' &&
34
- unavailableCodes.has(error.code))
35
- return new UnavailableError(path, error);
31
+ if ('errcode' in error && typeof error.errcode === 'number') {
32
+ if (error.errcode === SQLITE_READONLY_DIRECTORY)
33
+ return new DeniedError(path, error);
34
+ if (error.errcode === SQLITE_CANTOPEN)
35
+ return ofCantOpen(path, error);
36
+ }
37
+ if ('code' in error && typeof error.code === 'string')
38
+ return ofFileSystemCode(path, error.code, error);
36
39
  return undefined;
37
40
  }
38
41
  }
42
+ /** The file of the database, or a folder on its path, is not there. */
43
+ export class MissingError extends UnavailableError {
44
+ constructor(path, cause) {
45
+ super(path, cause);
46
+ this.name = 'MissingError';
47
+ this.message = `The database at ${JSON.stringify(path)} cannot be opened: the file, or a folder on its path, is not there. Check the path, or make the database first.`;
48
+ }
49
+ }
50
+ /** The system keeps this process from the file of the database, or from a folder on its path. */
51
+ export class DeniedError extends UnavailableError {
52
+ constructor(path, cause) {
53
+ super(path, cause);
54
+ this.name = 'DeniedError';
55
+ this.message = `The database at ${JSON.stringify(path)} cannot be opened: the system denies this process access to the file, or to a folder on its path. Give the process access.`;
56
+ }
57
+ }
58
+ function ofFileSystemCode(path, code, cause) {
59
+ if (missingCodes.has(code))
60
+ return new MissingError(path, cause);
61
+ if (deniedCodes.has(code))
62
+ return new DeniedError(path, cause);
63
+ return undefined;
64
+ }
65
+ // SQLite gives CANTOPEN for a missing file and for a denied one, so a
66
+ // read-only open of the file system asks again and its code decides. A stat
67
+ // is the wrong question: it succeeds on a file that the system denies.
68
+ function ofCantOpen(path, cause) {
69
+ // SQLite reads a name that starts with "file:" as a URI, and the file
70
+ // system reads it as a file name, so the second open would ask about a
71
+ // different file.
72
+ if (path.startsWith('file:'))
73
+ return new UnavailableError(path, cause);
74
+ let descriptor;
75
+ try {
76
+ descriptor = openSync(path, 'r');
77
+ }
78
+ catch (error) {
79
+ return ((Error.isError(error) && 'code' in error && typeof error.code === 'string'
80
+ ? ofFileSystemCode(path, error.code, cause)
81
+ : undefined) ?? new UnavailableError(path, cause));
82
+ }
83
+ // The file opens, so it is not missing and not denied: it changed between
84
+ // the two opens, or SQLite could not open a file beside it.
85
+ closeSync(descriptor);
86
+ return new UnavailableError(path, cause);
87
+ }
39
88
  //# sourceMappingURL=unavailable-error.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"unavailable-error.js","sourceRoot":"","sources":["../src/unavailable-error.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;AAE3E,6EAA6E;AAC7E,8EAA8E;AAC9E,2EAA2E;AAC3E,oBAAoB;AACpB,MAAM,eAAe,GAAG,EAAE,CAAC;AAC3B,6EAA6E;AAC7E,6EAA6E;AAC7E,kEAAkE;AAClE,MAAM,yBAAyB,GAAG,IAAI,CAAC;AAEvC,yEAAyE;AACzE,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAChC,IAAI,CAAS;IAEtB,YAAY,IAAY,EAAE,KAAc;QACtC,KAAK,CACH,mBAAmB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,0HAA0H,EACjK,EAAE,KAAK,EAAE,CACV,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,IAAI,CAAC,IAAY,EAAE,KAAc;QACtC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YAAE,OAAO,SAAS,CAAC;QAC5C,IACE,SAAS,IAAI,KAAK;YAClB,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ;YACjC,CAAC,KAAK,CAAC,OAAO,KAAK,eAAe;gBAChC,KAA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CAAC;QAAE,OAAO,IAAI,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACjE,IAAI,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC/D,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,sEAAsE;AACtE,4EAA4E;AAC5E,uEAAuE;AACvE,SAAS,UAAU,CAAC,IAAY,EAAE,KAAc;IAC9C,sEAAsE;IACtE,uEAAuE;IACvE,kBAAkB;IAClB,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC;QAAE,OAAO,IAAI,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACvE,IAAI,UAAkB,CAAC;IACvB,IAAI,CAAC;QACH,UAAU,GAAG,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CACL,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,MAAM,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;YACxE,CAAC,CAAC,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC;YAC3C,CAAC,CAAC,SAAS,CAAC,IAAI,IAAI,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,CACpD,CAAC;IACJ,CAAC;IACD,0EAA0E;IAC1E,4DAA4D;IAC5D,SAAS,CAAC,UAAU,CAAC,CAAC;IACtB,OAAO,IAAI,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC3C,CAAC"}
@@ -2,12 +2,20 @@ import type { DatabaseSync } from 'node:sqlite';
2
2
  import type { Journal } from './journal.ts';
3
3
  /** A page size that SQLite accepts: a power of two from 512 to 65536. */
4
4
  export type PageSize = 512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768 | 65536;
5
+ /**
6
+ * When SQLite syncs the -wal file to the disk: `'normal'` before each
7
+ * checkpoint, `'full'` at each commit (ADR 0007).
8
+ */
9
+ export type Synchronous = 'normal' | 'full';
10
+ /** The options of a write-ahead log. */
11
+ export interface WriteAheadLogOptions {
12
+ readonly pageSize?: PageSize;
13
+ readonly synchronous?: Synchronous;
14
+ }
5
15
  /** The default journal of an owner: readers and the owner do not wait for each other. */
6
16
  export declare class WriteAheadLog implements Journal {
7
17
  #private;
8
- constructor(options?: {
9
- readonly pageSize?: PageSize;
10
- });
18
+ constructor(options?: WriteAheadLogOptions);
11
19
  apply(database: DatabaseSync): void;
12
20
  }
13
21
  //# sourceMappingURL=write-ahead-log.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"write-ahead-log.d.ts","sourceRoot":"","sources":["../src/write-ahead-log.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAK5C,yEAAyE;AACzE,MAAM,MAAM,QAAQ,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAE/E,yFAAyF;AACzF,qBAAa,aAAc,YAAW,OAAO;;gBAG/B,OAAO,GAAE;QAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAA;KAAO;IAI1D,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;CAiBpC"}
1
+ {"version":3,"file":"write-ahead-log.d.ts","sourceRoot":"","sources":["../src/write-ahead-log.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAK5C,yEAAyE;AACzE,MAAM,MAAM,QAAQ,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAE/E;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,MAAM,CAAC;AAE5C,wCAAwC;AACxC,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC;CACpC;AAED,yFAAyF;AACzF,qBAAa,aAAc,YAAW,OAAO;;gBAI/B,OAAO,GAAE,oBAAyB;IAK9C,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;CAkBpC"}
@@ -3,8 +3,10 @@ const sizeLimit = 64 * 1024 * 1024;
3
3
  /** The default journal of an owner: readers and the owner do not wait for each other. */
4
4
  export class WriteAheadLog {
5
5
  #pageSize;
6
+ #synchronous;
6
7
  constructor(options = {}) {
7
8
  this.#pageSize = options.pageSize;
9
+ this.#synchronous = options.synchronous ?? 'normal';
8
10
  }
9
11
  apply(database) {
10
12
  // SQLite takes the page size of a file only before its first write, and
@@ -20,6 +22,7 @@ export class WriteAheadLog {
20
22
  if (journal !== 'wal')
21
23
  throw new Error(`SQLite kept the journal ${JSON.stringify(journal)} of ${JSON.stringify(database.location())}, not a write-ahead log.`);
22
24
  database.exec(`PRAGMA journal_size_limit = ${sizeLimit}`);
25
+ database.exec(`PRAGMA synchronous = ${this.#synchronous.toUpperCase()}`);
23
26
  }
24
27
  }
25
28
  //# sourceMappingURL=write-ahead-log.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"write-ahead-log.js","sourceRoot":"","sources":["../src/write-ahead-log.ts"],"names":[],"mappings":"AAIA,2EAA2E;AAC3E,MAAM,SAAS,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;AAKnC,yFAAyF;AACzF,MAAM,OAAO,aAAa;IACf,SAAS,CAAuB;IAEzC,YAAY,UAA4C,EAAE;QACxD,IAAI,CAAC,SA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Q,CAAC,IAAI,CAAC,sBAAsB,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC;QACxD,sEAAsE;QACtE,sEAAsE;QACtE,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,2BAA2B,CAAC,CAAC;QAChE,SAAS,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QACjC,MAAM,OAAO,GAAG,SAAS,CAAC,GAAG,EAAE,EAAE,YAAY,CAAC;QAC9C,IAAI,OAAO,KAAK,KAAK;YACnB,MAAM,IAAI,KAAK,CACb,2BAA2B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,0BAA0B,CACvH,CAAC;QACJ,QAAQ,CAAC,IAAI,CAAC,+BAA+B,SAAS,EAAE,CAAC,CAAC;QAC1D,QAAQ,CAAC,IAAI,CAAC,wBAAwB,IAAI,CAAC,YAAY,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;IAC3E,CAAC;CACF"}
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zukhruf/sqlite-open",
3
- "version": "0.8.15",
3
+ "version": "0.8.17",
4
4
  "description": "One open for every node:sqlite connection: a reader, or an owner whose journal is a strategy (WAL by default, persistent WAL for readers that cannot write the folder), a 5 s busy timeout, and UnavailableError.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -41,6 +41,6 @@
41
41
  "access": "public"
42
42
  },
43
43
  "devDependencies": {
44
- "@zukhruf/testing": "^0.8.15"
44
+ "@zukhruf/testing": "^0.8.17"
45
45
  }
46
46
  }