token-goat 2.9.13 → 2.9.15

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.
Files changed (52) hide show
  1. package/README.md +28 -1
  2. package/dist/token-goat-chunk-2X2EBBC6.mjs +277 -0
  3. package/dist/token-goat-chunk-3BTK54F3.mjs +1733 -0
  4. package/dist/token-goat-chunk-3NSDDTGL.mjs +34 -0
  5. package/dist/token-goat-chunk-4EXFN2AW.mjs +29 -0
  6. package/dist/token-goat-chunk-5V7DAC7V.mjs +123 -0
  7. package/dist/token-goat-chunk-6DLVZDB6.mjs +34 -0
  8. package/dist/token-goat-chunk-7ZYK25AO.mjs +24 -0
  9. package/dist/token-goat-chunk-A4JYKD5H.mjs +144 -0
  10. package/dist/token-goat-chunk-AH6QILZM.mjs +26 -0
  11. package/dist/token-goat-chunk-ASYEPR3S.mjs +212 -0
  12. package/dist/{token-goat-chunk-RDITECDL.mjs → token-goat-chunk-ATIFTMRC.mjs} +31 -13
  13. package/dist/{token-goat-chunk-ZZI3IDQZ.mjs → token-goat-chunk-BL5LNGBG.mjs} +3935 -11949
  14. package/dist/{token-goat-chunk-4NXUKV7D.mjs → token-goat-chunk-C5JIO6HK.mjs} +8 -4
  15. package/dist/{token-goat-chunk-FZU7GMUS.mjs → token-goat-chunk-DQ4J5AFF.mjs} +50 -18
  16. package/dist/token-goat-chunk-ERTXEKB6.mjs +228 -0
  17. package/dist/{token-goat-chunk-6B44WLIF.mjs → token-goat-chunk-GIIHUSZX.mjs} +142 -21
  18. package/dist/token-goat-chunk-GMOUBOX4.mjs +386 -0
  19. package/dist/{token-goat-chunk-3ESRORNM.mjs → token-goat-chunk-GMQQA7E4.mjs} +12576 -12326
  20. package/dist/token-goat-chunk-IVUQLQWN.mjs +2046 -0
  21. package/dist/token-goat-chunk-K7F2BFIK.mjs +2430 -0
  22. package/dist/{token-goat-chunk-U7X6LQD2.mjs → token-goat-chunk-LCZBPOIN.mjs} +10197 -9717
  23. package/dist/token-goat-chunk-LMFO66YD.mjs +331 -0
  24. package/dist/token-goat-chunk-LT7JRU6K.mjs +22 -0
  25. package/dist/token-goat-chunk-MZDIJJ3R.mjs +420 -0
  26. package/dist/token-goat-chunk-NDPO7GAH.mjs +177 -0
  27. package/dist/token-goat-chunk-NDRP4KJQ.mjs +4371 -0
  28. package/dist/token-goat-chunk-NEI4NC54.mjs +424 -0
  29. package/dist/token-goat-chunk-NU7TLMQK.mjs +585 -0
  30. package/dist/token-goat-chunk-OSUFN2FV.mjs +326 -0
  31. package/dist/token-goat-chunk-OUGNPMDA.mjs +959 -0
  32. package/dist/token-goat-chunk-PM76YS22.mjs +1341 -0
  33. package/dist/token-goat-chunk-S4XRY446.mjs +2637 -0
  34. package/dist/{token-goat-chunk-YOA4N6WA.mjs → token-goat-chunk-SFAS46RE.mjs} +5 -3
  35. package/dist/{token-goat-chunk-QWSUZWFP.mjs → token-goat-chunk-SZWYESBS.mjs} +793 -650
  36. package/dist/{token-goat-chunk-B3CTCQTH.mjs → token-goat-chunk-XEPXYDPI.mjs} +3 -2
  37. package/dist/token-goat-chunk-XTQAOTSO.mjs +89 -0
  38. package/dist/token-goat-chunk-Y4AFKTHK.mjs +22 -0
  39. package/dist/token-goat-chunk-YKG35VHC.mjs +228 -0
  40. package/dist/{token-goat-chunk-JOXLE672.mjs → token-goat-chunk-YQ7WI2CO.mjs} +990 -106
  41. package/dist/{token-goat-chunk-2WC4ZUXN.mjs → token-goat-chunk-YZX7EFG4.mjs} +1 -1
  42. package/dist/token-goat-chunk-Z6UXPYJA.mjs +62 -0
  43. package/dist/token-goat-hook.mjs +16 -8
  44. package/dist/token-goat.core.mjs +27 -10
  45. package/docs/cli.md +9 -6
  46. package/package.json +4 -2
  47. package/dist/token-goat-chunk-FQCNJV4V.mjs +0 -693
  48. package/dist/token-goat-chunk-JVNPCQB7.mjs +0 -31
  49. package/dist/token-goat-chunk-P2PU4CR5.mjs +0 -26
  50. package/dist/token-goat-chunk-QKXBGBQR.mjs +0 -3653
  51. package/dist/token-goat-chunk-UM47DRD3.mjs +0 -242
  52. package/dist/token-goat-chunk-UMXJN7DI.mjs +0 -5521
@@ -0,0 +1,326 @@
1
+ import { createRequire as __cjsRequire } from 'node:module';
2
+ const require = __cjsRequire(import.meta.url);
3
+ import {
4
+ init_define_import_meta_env
5
+ } from "./token-goat-chunk-A37V4PBF.mjs";
6
+
7
+ // src/sqlite_driver.ts
8
+ init_define_import_meta_env();
9
+ import * as fs from "node:fs";
10
+ import { createRequire } from "node:module";
11
+ var _require = createRequire(import.meta.url);
12
+ function suppressSqliteExperimentalWarning() {
13
+ const original = process.emit;
14
+ let armed = true;
15
+ const restore = () => {
16
+ if (!armed) return;
17
+ armed = false;
18
+ process.emit = original;
19
+ };
20
+ process.emit = function patched(name, ...rest) {
21
+ const data = rest[0];
22
+ if (armed && name === "warning" && data instanceof Error && data.name === "ExperimentalWarning" && /sqlite/i.test(data.message)) {
23
+ restore();
24
+ return false;
25
+ }
26
+ return original.call(this, name, ...rest);
27
+ };
28
+ setImmediate(restore);
29
+ return restore;
30
+ }
31
+ var restoreWarnings = suppressSqliteExperimentalWarning();
32
+ var nodeSqlite;
33
+ try {
34
+ nodeSqlite = _require("node:sqlite");
35
+ } catch (e) {
36
+ restoreWarnings();
37
+ throw e;
38
+ }
39
+ var { DatabaseSync } = nodeSqlite;
40
+ var SQLITE_PRIMARY_CODES = [
41
+ "SQLITE_OK",
42
+ "SQLITE_ERROR",
43
+ "SQLITE_INTERNAL",
44
+ "SQLITE_PERM",
45
+ "SQLITE_ABORT",
46
+ "SQLITE_BUSY",
47
+ "SQLITE_LOCKED",
48
+ "SQLITE_NOMEM",
49
+ "SQLITE_READONLY",
50
+ "SQLITE_INTERRUPT",
51
+ "SQLITE_IOERR",
52
+ "SQLITE_CORRUPT",
53
+ "SQLITE_NOTFOUND",
54
+ "SQLITE_FULL",
55
+ "SQLITE_CANTOPEN",
56
+ "SQLITE_PROTOCOL",
57
+ "SQLITE_EMPTY",
58
+ "SQLITE_SCHEMA",
59
+ "SQLITE_TOOBIG",
60
+ "SQLITE_CONSTRAINT",
61
+ "SQLITE_MISMATCH",
62
+ "SQLITE_MISUSE",
63
+ "SQLITE_NOLFS",
64
+ "SQLITE_AUTH",
65
+ "SQLITE_FORMAT",
66
+ "SQLITE_RANGE",
67
+ "SQLITE_NOTADB",
68
+ "SQLITE_NOTICE",
69
+ "SQLITE_WARNING"
70
+ ];
71
+ var SQLITE_EXTENDED_SUFFIXES = {
72
+ SQLITE_OK: ["LOAD_PERMANENTLY", "SYMLINK"],
73
+ SQLITE_ERROR: ["MISSING_COLLSEQ", "RETRY", "SNAPSHOT"],
74
+ SQLITE_ABORT: [null, "ROLLBACK"],
75
+ SQLITE_BUSY: ["RECOVERY", "SNAPSHOT", "TIMEOUT"],
76
+ SQLITE_LOCKED: ["SHAREDCACHE", "VTAB"],
77
+ SQLITE_READONLY: ["RECOVERY", "CANTLOCK", "ROLLBACK", "DBMOVED", "CANTINIT", "DIRECTORY"],
78
+ SQLITE_IOERR: [
79
+ "READ",
80
+ "SHORT_READ",
81
+ "WRITE",
82
+ "FSYNC",
83
+ "DIR_FSYNC",
84
+ "TRUNCATE",
85
+ "FSTAT",
86
+ "UNLOCK",
87
+ "RDLOCK",
88
+ "DELETE",
89
+ "BLOCKED",
90
+ "NOMEM",
91
+ "ACCESS",
92
+ "CHECKRESERVEDLOCK",
93
+ "LOCK",
94
+ "CLOSE",
95
+ "DIR_CLOSE",
96
+ "SHMOPEN",
97
+ "SHMSIZE",
98
+ "SHMLOCK",
99
+ "SHMMAP",
100
+ "SEEK",
101
+ "DELETE_NOENT",
102
+ "MMAP",
103
+ "GETTEMPPATH",
104
+ "CONVPATH",
105
+ "VNODE",
106
+ "AUTH",
107
+ "BEGIN_ATOMIC",
108
+ "COMMIT_ATOMIC",
109
+ "ROLLBACK_ATOMIC",
110
+ "DATA",
111
+ "CORRUPTFS",
112
+ "IN_PAGE"
113
+ ],
114
+ SQLITE_CORRUPT: ["VTAB", "SEQUENCE", "INDEX"],
115
+ SQLITE_CANTOPEN: ["NOTEMPDIR", "ISDIR", "FULLPATH", "CONVPATH", "DIRTYWAL", "SYMLINK"],
116
+ SQLITE_CONSTRAINT: [
117
+ "CHECK",
118
+ "COMMITHOOK",
119
+ "FOREIGNKEY",
120
+ "FUNCTION",
121
+ "NOTNULL",
122
+ "PRIMARYKEY",
123
+ "TRIGGER",
124
+ "UNIQUE",
125
+ "VTAB",
126
+ "ROWID",
127
+ "PINNED",
128
+ "DATATYPE"
129
+ ],
130
+ SQLITE_AUTH: ["USER"],
131
+ SQLITE_NOTICE: ["RECOVER_WAL", "RECOVER_ROLLBACK", "RBU"],
132
+ SQLITE_WARNING: ["AUTOINDEX"]
133
+ };
134
+ function sqliteResultCodeName(errcode) {
135
+ if (!Number.isInteger(errcode) || errcode < 0) return "ERR_SQLITE_ERROR";
136
+ if (errcode === 100) return "SQLITE_ROW";
137
+ if (errcode === 101) return "SQLITE_DONE";
138
+ const primary = SQLITE_PRIMARY_CODES[errcode & 255];
139
+ if (primary === void 0) return "ERR_SQLITE_ERROR";
140
+ const subcode = errcode >> 8;
141
+ if (subcode === 0) return primary;
142
+ const suffix = SQLITE_EXTENDED_SUFFIXES[primary]?.[subcode - 1];
143
+ return suffix === void 0 || suffix === null ? primary : `${primary}_${suffix}`;
144
+ }
145
+ function attempt(fn) {
146
+ try {
147
+ return fn();
148
+ } catch (e) {
149
+ const err = e;
150
+ if (err.code === "ERR_SQLITE_ERROR" && typeof err.errcode === "number") {
151
+ err.code = sqliteResultCodeName(err.errcode);
152
+ }
153
+ throw e;
154
+ }
155
+ }
156
+ var Statement = class {
157
+ #stmt;
158
+ #pluck = false;
159
+ constructor(stmt) {
160
+ this.#stmt = stmt;
161
+ }
162
+ get source() {
163
+ return this.#stmt.sourceSQL;
164
+ }
165
+ /**
166
+ * better-sqlite3's `reader` flag: does this statement return rows?
167
+ *
168
+ * `node:sqlite` has no equivalent, so it is derived from the prepared statement's own column
169
+ * count -- SQLite gives a row-producing statement its result columns at prepare time and gives a
170
+ * non-producing one none. That is a derivation, and this is the third defence-in-depth layer in
171
+ * `sqlite_query.ts`'s read-only guard, so it is not taken on faith: the driver tests run both
172
+ * libraries side by side over SELECT, a CTE, VALUES, EXPLAIN, an empty-result SELECT, INSERT,
173
+ * UPDATE, DELETE, CREATE, a reading PRAGMA and an assigning PRAGMA, and require every verdict to
174
+ * agree. If a future SQLite statement form ever breaks the equivalence, that test fails rather
175
+ * than the guard quietly weakening.
176
+ */
177
+ get reader() {
178
+ return attempt(() => this.#stmt.columns()).length > 0;
179
+ }
180
+ // A plucked row is "the first column", which for an object row means the first *inserted* key. V8 preserves insertion order for string keys, and node:sqlite builds the row by walking the result columns left to right, so Object.values()[0] is the leftmost column -- not the column named in the SQL text, the same rule better-sqlite3 applies.
181
+ #shape(row) {
182
+ if (!this.#pluck || row === void 0 || row === null) return row;
183
+ const values = Object.values(row);
184
+ return values.length === 0 ? void 0 : values[0];
185
+ }
186
+ get(...params) {
187
+ return this.#shape(attempt(() => this.#stmt.get(...params)));
188
+ }
189
+ all(...params) {
190
+ const rows = attempt(() => this.#stmt.all(...params));
191
+ return this.#pluck ? rows.map((r) => this.#shape(r)) : rows;
192
+ }
193
+ run(...params) {
194
+ return attempt(() => this.#stmt.run(...params));
195
+ }
196
+ // Wrapped rather than returned directly so pluck applies lazily, one row at a time: the whole point of iterate() here is that sqlite_query.ts caps the row count without buffering the rest, and mapping the iterator through .all() first would defeat that.
197
+ *iterate(...params) {
198
+ const rows = attempt(() => this.#stmt.iterate(...params))[Symbol.iterator]();
199
+ for (; ; ) {
200
+ const next = attempt(() => rows.next());
201
+ if (next.done === true) return;
202
+ yield this.#shape(next.value);
203
+ }
204
+ }
205
+ pluck(toggle = true) {
206
+ this.#pluck = toggle;
207
+ return this;
208
+ }
209
+ safeIntegers(toggle = true) {
210
+ this.#stmt.setReadBigInts(toggle);
211
+ return this;
212
+ }
213
+ columns() {
214
+ return attempt(() => this.#stmt.columns());
215
+ }
216
+ };
217
+ var Database = class {
218
+ #db;
219
+ #path;
220
+ #readonly;
221
+ #savepoints = 0;
222
+ constructor(dbPath, options = {}) {
223
+ const wantsExisting = options.readonly === true || options.fileMustExist === true;
224
+ if (wantsExisting && dbPath !== ":memory:" && !fs.existsSync(dbPath)) {
225
+ throw new Error("unable to open database file");
226
+ }
227
+ this.#db = attempt(() => new DatabaseSync(dbPath, {
228
+ readOnly: options.readonly === true,
229
+ // sqlite-vec is loaded through db.loadExtension by initConnection, which node:sqlite refuses unless the connection opted in at construction. Harmless when no extension is ever loaded.
230
+ allowExtension: true,
231
+ // better-sqlite3 opens every connection with busy_timeout at 5000ms; node:sqlite opens at 0, so a connection that named no timeout would silently go from five seconds of patience to none. db.ts overrides this to 15000 in initConnection, but sqlite_query.ts opens a user's arbitrary database readonly and takes whatever the default is -- which would have turned ordinary contention with another writer into an immediate "database is locked".
232
+ timeout: options.timeout ?? 5e3
233
+ }));
234
+ this.#path = dbPath;
235
+ this.#readonly = options.readonly === true;
236
+ }
237
+ get open() {
238
+ return this.#db.isOpen;
239
+ }
240
+ get inTransaction() {
241
+ return this.#db.isTransaction;
242
+ }
243
+ get readonly() {
244
+ return this.#readonly;
245
+ }
246
+ get name() {
247
+ return this.#path;
248
+ }
249
+ prepare(sql) {
250
+ return new Statement(attempt(() => this.#db.prepare(sql)));
251
+ }
252
+ exec(sql) {
253
+ attempt(() => this.#db.exec(sql));
254
+ }
255
+ pragma(source, options = {}) {
256
+ const rows = attempt(() => this.#db.prepare(`PRAGMA ${source}`).all());
257
+ if (options.simple !== true) return rows;
258
+ const first = rows[0];
259
+ if (first === void 0) return void 0;
260
+ const values = Object.values(first);
261
+ return values.length === 0 ? void 0 : values[0];
262
+ }
263
+ function(name, options, fn) {
264
+ attempt(() => this.#db.function(name, options, fn));
265
+ }
266
+ loadExtension(extensionPath) {
267
+ attempt(() => this.#db.loadExtension(extensionPath));
268
+ }
269
+ close() {
270
+ attempt(() => this.#db.close());
271
+ }
272
+ /**
273
+ * Wrap `fn` so it runs inside a transaction, committing on return and rolling back on throw.
274
+ *
275
+ * Nesting uses SAVEPOINT, which is what makes it safe for a transactional helper to call another
276
+ * one: an inner `BEGIN` would throw ("cannot start a transaction within a transaction"), an inner
277
+ * SAVEPOINT composes. Whether we are nested is read from SQLite via `isTransaction` rather than
278
+ * tracked in a counter here, so a transaction some other code path opened still nests correctly.
279
+ *
280
+ * The rollback is best-effort and never replaces the caller's error: if the ROLLBACK itself fails
281
+ * -- the connection died, the transaction was already unwound -- the original failure is still
282
+ * what propagates, because that is the one that explains what went wrong.
283
+ */
284
+ transaction(fn) {
285
+ const build = (beginSql) => (...args) => {
286
+ if (this.#db.isTransaction) {
287
+ const name = `tg_sp_${this.#savepoints++}`;
288
+ attempt(() => this.#db.exec(`SAVEPOINT ${name}`));
289
+ try {
290
+ const result = fn(...args);
291
+ attempt(() => this.#db.exec(`RELEASE ${name}`));
292
+ return result;
293
+ } catch (e) {
294
+ try {
295
+ attempt(() => this.#db.exec(`ROLLBACK TO ${name}`));
296
+ attempt(() => this.#db.exec(`RELEASE ${name}`));
297
+ } catch {
298
+ }
299
+ throw e;
300
+ }
301
+ }
302
+ attempt(() => this.#db.exec(beginSql));
303
+ try {
304
+ const result = fn(...args);
305
+ attempt(() => this.#db.exec("COMMIT"));
306
+ return result;
307
+ } catch (e) {
308
+ try {
309
+ attempt(() => this.#db.exec("ROLLBACK"));
310
+ } catch {
311
+ }
312
+ throw e;
313
+ }
314
+ };
315
+ const wrapped = build("BEGIN");
316
+ wrapped.default = wrapped;
317
+ wrapped.deferred = build("BEGIN");
318
+ wrapped.immediate = build("BEGIN IMMEDIATE");
319
+ wrapped.exclusive = build("BEGIN EXCLUSIVE");
320
+ return wrapped;
321
+ }
322
+ };
323
+
324
+ export {
325
+ Database
326
+ };