speexjs 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,809 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
+ }) : x)(function(x) {
10
+ if (typeof require !== "undefined") return require.apply(this, arguments);
11
+ throw Error('Dynamic require of "' + x + '" is not supported');
12
+ });
13
+ var __commonJS = (cb, mod) => function __require2() {
14
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
+ mod
31
+ ));
32
+
33
+ // node_modules/better-sqlite3/lib/util.js
34
+ var require_util = __commonJS({
35
+ "node_modules/better-sqlite3/lib/util.js"(exports) {
36
+ "use strict";
37
+ exports.getBooleanOption = (options, key) => {
38
+ let value = false;
39
+ if (key in options && typeof (value = options[key]) !== "boolean") {
40
+ throw new TypeError(`Expected the "${key}" option to be a boolean`);
41
+ }
42
+ return value;
43
+ };
44
+ exports.cppdb = /* @__PURE__ */ Symbol();
45
+ exports.inspect = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
46
+ }
47
+ });
48
+
49
+ // node_modules/better-sqlite3/lib/sqlite-error.js
50
+ var require_sqlite_error = __commonJS({
51
+ "node_modules/better-sqlite3/lib/sqlite-error.js"(exports, module) {
52
+ "use strict";
53
+ var descriptor = { value: "SqliteError", writable: true, enumerable: false, configurable: true };
54
+ function SqliteError(message, code) {
55
+ if (new.target !== SqliteError) {
56
+ return new SqliteError(message, code);
57
+ }
58
+ if (typeof code !== "string") {
59
+ throw new TypeError("Expected second argument to be a string");
60
+ }
61
+ Error.call(this, message);
62
+ descriptor.value = "" + message;
63
+ Object.defineProperty(this, "message", descriptor);
64
+ Error.captureStackTrace(this, SqliteError);
65
+ this.code = code;
66
+ }
67
+ Object.setPrototypeOf(SqliteError, Error);
68
+ Object.setPrototypeOf(SqliteError.prototype, Error.prototype);
69
+ Object.defineProperty(SqliteError.prototype, "name", descriptor);
70
+ module.exports = SqliteError;
71
+ }
72
+ });
73
+
74
+ // node_modules/file-uri-to-path/index.js
75
+ var require_file_uri_to_path = __commonJS({
76
+ "node_modules/file-uri-to-path/index.js"(exports, module) {
77
+ "use strict";
78
+ var sep = __require("path").sep || "/";
79
+ module.exports = fileUriToPath;
80
+ function fileUriToPath(uri) {
81
+ if ("string" != typeof uri || uri.length <= 7 || "file://" != uri.substring(0, 7)) {
82
+ throw new TypeError("must pass in a file:// URI to convert to a file path");
83
+ }
84
+ var rest = decodeURI(uri.substring(7));
85
+ var firstSlash = rest.indexOf("/");
86
+ var host = rest.substring(0, firstSlash);
87
+ var path3 = rest.substring(firstSlash + 1);
88
+ if ("localhost" == host) host = "";
89
+ if (host) {
90
+ host = sep + sep + host;
91
+ }
92
+ path3 = path3.replace(/^(.+)\|/, "$1:");
93
+ if (sep == "\\") {
94
+ path3 = path3.replace(/\//g, "\\");
95
+ }
96
+ if (/^.+\:/.test(path3)) {
97
+ } else {
98
+ path3 = sep + path3;
99
+ }
100
+ return host + path3;
101
+ }
102
+ }
103
+ });
104
+
105
+ // node_modules/bindings/bindings.js
106
+ var require_bindings = __commonJS({
107
+ "node_modules/bindings/bindings.js"(exports, module) {
108
+ "use strict";
109
+ var fs3 = __require("fs");
110
+ var path3 = __require("path");
111
+ var fileURLToPath = require_file_uri_to_path();
112
+ var join5 = path3.join;
113
+ var dirname4 = path3.dirname;
114
+ var exists = fs3.accessSync && function(path4) {
115
+ try {
116
+ fs3.accessSync(path4);
117
+ } catch (e) {
118
+ return false;
119
+ }
120
+ return true;
121
+ } || fs3.existsSync || path3.existsSync;
122
+ var defaults = {
123
+ arrow: process.env.NODE_BINDINGS_ARROW || " \u2192 ",
124
+ compiled: process.env.NODE_BINDINGS_COMPILED_DIR || "compiled",
125
+ platform: process.platform,
126
+ arch: process.arch,
127
+ nodePreGyp: "node-v" + process.versions.modules + "-" + process.platform + "-" + process.arch,
128
+ version: process.versions.node,
129
+ bindings: "bindings.node",
130
+ try: [
131
+ // node-gyp's linked version in the "build" dir
132
+ ["module_root", "build", "bindings"],
133
+ // node-waf and gyp_addon (a.k.a node-gyp)
134
+ ["module_root", "build", "Debug", "bindings"],
135
+ ["module_root", "build", "Release", "bindings"],
136
+ // Debug files, for development (legacy behavior, remove for node v0.9)
137
+ ["module_root", "out", "Debug", "bindings"],
138
+ ["module_root", "Debug", "bindings"],
139
+ // Release files, but manually compiled (legacy behavior, remove for node v0.9)
140
+ ["module_root", "out", "Release", "bindings"],
141
+ ["module_root", "Release", "bindings"],
142
+ // Legacy from node-waf, node <= 0.4.x
143
+ ["module_root", "build", "default", "bindings"],
144
+ // Production "Release" buildtype binary (meh...)
145
+ ["module_root", "compiled", "version", "platform", "arch", "bindings"],
146
+ // node-qbs builds
147
+ ["module_root", "addon-build", "release", "install-root", "bindings"],
148
+ ["module_root", "addon-build", "debug", "install-root", "bindings"],
149
+ ["module_root", "addon-build", "default", "install-root", "bindings"],
150
+ // node-pre-gyp path ./lib/binding/{node_abi}-{platform}-{arch}
151
+ ["module_root", "lib", "binding", "nodePreGyp", "bindings"]
152
+ ]
153
+ };
154
+ function bindings(opts) {
155
+ if (typeof opts == "string") {
156
+ opts = { bindings: opts };
157
+ } else if (!opts) {
158
+ opts = {};
159
+ }
160
+ Object.keys(defaults).map(function(i2) {
161
+ if (!(i2 in opts)) opts[i2] = defaults[i2];
162
+ });
163
+ if (!opts.module_root) {
164
+ opts.module_root = exports.getRoot(exports.getFileName());
165
+ }
166
+ if (path3.extname(opts.bindings) != ".node") {
167
+ opts.bindings += ".node";
168
+ }
169
+ var requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : __require;
170
+ var tries = [], i = 0, l = opts.try.length, n, b, err;
171
+ for (; i < l; i++) {
172
+ n = join5.apply(
173
+ null,
174
+ opts.try[i].map(function(p) {
175
+ return opts[p] || p;
176
+ })
177
+ );
178
+ tries.push(n);
179
+ try {
180
+ b = opts.path ? requireFunc.resolve(n) : requireFunc(n);
181
+ if (!opts.path) {
182
+ b.path = n;
183
+ }
184
+ return b;
185
+ } catch (e) {
186
+ if (e.code !== "MODULE_NOT_FOUND" && e.code !== "QUALIFIED_PATH_RESOLUTION_FAILED" && !/not find/i.test(e.message)) {
187
+ throw e;
188
+ }
189
+ }
190
+ }
191
+ err = new Error(
192
+ "Could not locate the bindings file. Tried:\n" + tries.map(function(a) {
193
+ return opts.arrow + a;
194
+ }).join("\n")
195
+ );
196
+ err.tries = tries;
197
+ throw err;
198
+ }
199
+ module.exports = exports = bindings;
200
+ exports.getFileName = function getFileName(calling_file) {
201
+ var origPST = Error.prepareStackTrace, origSTL = Error.stackTraceLimit, dummy = {}, fileName;
202
+ Error.stackTraceLimit = 10;
203
+ Error.prepareStackTrace = function(e, st) {
204
+ for (var i = 0, l = st.length; i < l; i++) {
205
+ fileName = st[i].getFileName();
206
+ if (fileName !== __filename) {
207
+ if (calling_file) {
208
+ if (fileName !== calling_file) {
209
+ return;
210
+ }
211
+ } else {
212
+ return;
213
+ }
214
+ }
215
+ }
216
+ };
217
+ Error.captureStackTrace(dummy);
218
+ dummy.stack;
219
+ Error.prepareStackTrace = origPST;
220
+ Error.stackTraceLimit = origSTL;
221
+ var fileSchema = "file://";
222
+ if (fileName.indexOf(fileSchema) === 0) {
223
+ fileName = fileURLToPath(fileName);
224
+ }
225
+ return fileName;
226
+ };
227
+ exports.getRoot = function getRoot(file) {
228
+ var dir = dirname4(file), prev;
229
+ while (true) {
230
+ if (dir === ".") {
231
+ dir = process.cwd();
232
+ }
233
+ if (exists(join5(dir, "package.json")) || exists(join5(dir, "node_modules"))) {
234
+ return dir;
235
+ }
236
+ if (prev === dir) {
237
+ throw new Error(
238
+ 'Could not find module root given file: "' + file + '". Do you have a `package.json` file? '
239
+ );
240
+ }
241
+ prev = dir;
242
+ dir = join5(dir, "..");
243
+ }
244
+ };
245
+ }
246
+ });
247
+
248
+ // node_modules/better-sqlite3/lib/methods/wrappers.js
249
+ var require_wrappers = __commonJS({
250
+ "node_modules/better-sqlite3/lib/methods/wrappers.js"(exports) {
251
+ "use strict";
252
+ var { cppdb } = require_util();
253
+ exports.prepare = function prepare(sql) {
254
+ return this[cppdb].prepare(sql, this, false);
255
+ };
256
+ exports.exec = function exec(sql) {
257
+ this[cppdb].exec(sql);
258
+ return this;
259
+ };
260
+ exports.close = function close() {
261
+ this[cppdb].close();
262
+ return this;
263
+ };
264
+ exports.loadExtension = function loadExtension(...args) {
265
+ this[cppdb].loadExtension(...args);
266
+ return this;
267
+ };
268
+ exports.defaultSafeIntegers = function defaultSafeIntegers(...args) {
269
+ this[cppdb].defaultSafeIntegers(...args);
270
+ return this;
271
+ };
272
+ exports.unsafeMode = function unsafeMode(...args) {
273
+ this[cppdb].unsafeMode(...args);
274
+ return this;
275
+ };
276
+ exports.getters = {
277
+ name: {
278
+ get: function name() {
279
+ return this[cppdb].name;
280
+ },
281
+ enumerable: true
282
+ },
283
+ open: {
284
+ get: function open() {
285
+ return this[cppdb].open;
286
+ },
287
+ enumerable: true
288
+ },
289
+ inTransaction: {
290
+ get: function inTransaction() {
291
+ return this[cppdb].inTransaction;
292
+ },
293
+ enumerable: true
294
+ },
295
+ readonly: {
296
+ get: function readonly() {
297
+ return this[cppdb].readonly;
298
+ },
299
+ enumerable: true
300
+ },
301
+ memory: {
302
+ get: function memory() {
303
+ return this[cppdb].memory;
304
+ },
305
+ enumerable: true
306
+ }
307
+ };
308
+ }
309
+ });
310
+
311
+ // node_modules/better-sqlite3/lib/methods/transaction.js
312
+ var require_transaction = __commonJS({
313
+ "node_modules/better-sqlite3/lib/methods/transaction.js"(exports, module) {
314
+ "use strict";
315
+ var { cppdb } = require_util();
316
+ var controllers = /* @__PURE__ */ new WeakMap();
317
+ module.exports = function transaction(fn) {
318
+ if (typeof fn !== "function") throw new TypeError("Expected first argument to be a function");
319
+ const db = this[cppdb];
320
+ const controller = getController(db, this);
321
+ const { apply } = Function.prototype;
322
+ const properties = {
323
+ default: { value: wrapTransaction(apply, fn, db, controller.default) },
324
+ deferred: { value: wrapTransaction(apply, fn, db, controller.deferred) },
325
+ immediate: { value: wrapTransaction(apply, fn, db, controller.immediate) },
326
+ exclusive: { value: wrapTransaction(apply, fn, db, controller.exclusive) },
327
+ database: { value: this, enumerable: true }
328
+ };
329
+ Object.defineProperties(properties.default.value, properties);
330
+ Object.defineProperties(properties.deferred.value, properties);
331
+ Object.defineProperties(properties.immediate.value, properties);
332
+ Object.defineProperties(properties.exclusive.value, properties);
333
+ return properties.default.value;
334
+ };
335
+ var getController = (db, self) => {
336
+ let controller = controllers.get(db);
337
+ if (!controller) {
338
+ const shared = {
339
+ commit: db.prepare("COMMIT", self, false),
340
+ rollback: db.prepare("ROLLBACK", self, false),
341
+ savepoint: db.prepare("SAVEPOINT ` _bs3. `", self, false),
342
+ release: db.prepare("RELEASE ` _bs3. `", self, false),
343
+ rollbackTo: db.prepare("ROLLBACK TO ` _bs3. `", self, false)
344
+ };
345
+ controllers.set(db, controller = {
346
+ default: Object.assign({ begin: db.prepare("BEGIN", self, false) }, shared),
347
+ deferred: Object.assign({ begin: db.prepare("BEGIN DEFERRED", self, false) }, shared),
348
+ immediate: Object.assign({ begin: db.prepare("BEGIN IMMEDIATE", self, false) }, shared),
349
+ exclusive: Object.assign({ begin: db.prepare("BEGIN EXCLUSIVE", self, false) }, shared)
350
+ });
351
+ }
352
+ return controller;
353
+ };
354
+ var wrapTransaction = (apply, fn, db, { begin, commit, rollback, savepoint, release, rollbackTo }) => function sqliteTransaction() {
355
+ let before, after, undo;
356
+ if (db.inTransaction) {
357
+ before = savepoint;
358
+ after = release;
359
+ undo = rollbackTo;
360
+ } else {
361
+ before = begin;
362
+ after = commit;
363
+ undo = rollback;
364
+ }
365
+ before.run();
366
+ try {
367
+ const result = apply.call(fn, this, arguments);
368
+ if (result && typeof result.then === "function") {
369
+ throw new TypeError("Transaction function cannot return a promise");
370
+ }
371
+ after.run();
372
+ return result;
373
+ } catch (ex) {
374
+ if (db.inTransaction) {
375
+ undo.run();
376
+ if (undo !== rollback) after.run();
377
+ }
378
+ throw ex;
379
+ }
380
+ };
381
+ }
382
+ });
383
+
384
+ // node_modules/better-sqlite3/lib/methods/pragma.js
385
+ var require_pragma = __commonJS({
386
+ "node_modules/better-sqlite3/lib/methods/pragma.js"(exports, module) {
387
+ "use strict";
388
+ var { getBooleanOption, cppdb } = require_util();
389
+ module.exports = function pragma(source, options) {
390
+ if (options == null) options = {};
391
+ if (typeof source !== "string") throw new TypeError("Expected first argument to be a string");
392
+ if (typeof options !== "object") throw new TypeError("Expected second argument to be an options object");
393
+ const simple = getBooleanOption(options, "simple");
394
+ const stmt = this[cppdb].prepare(`PRAGMA ${source}`, this, true);
395
+ return simple ? stmt.pluck().get() : stmt.all();
396
+ };
397
+ }
398
+ });
399
+
400
+ // node_modules/better-sqlite3/lib/methods/backup.js
401
+ var require_backup = __commonJS({
402
+ "node_modules/better-sqlite3/lib/methods/backup.js"(exports, module) {
403
+ "use strict";
404
+ var fs3 = __require("fs");
405
+ var path3 = __require("path");
406
+ var { promisify: promisify2 } = __require("util");
407
+ var { cppdb } = require_util();
408
+ var fsAccess = promisify2(fs3.access);
409
+ module.exports = async function backup(filename, options) {
410
+ if (options == null) options = {};
411
+ if (typeof filename !== "string") throw new TypeError("Expected first argument to be a string");
412
+ if (typeof options !== "object") throw new TypeError("Expected second argument to be an options object");
413
+ filename = filename.trim();
414
+ const attachedName = "attached" in options ? options.attached : "main";
415
+ const handler = "progress" in options ? options.progress : null;
416
+ if (!filename) throw new TypeError("Backup filename cannot be an empty string");
417
+ if (filename === ":memory:") throw new TypeError('Invalid backup filename ":memory:"');
418
+ if (typeof attachedName !== "string") throw new TypeError('Expected the "attached" option to be a string');
419
+ if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
420
+ if (handler != null && typeof handler !== "function") throw new TypeError('Expected the "progress" option to be a function');
421
+ await fsAccess(path3.dirname(filename)).catch(() => {
422
+ throw new TypeError("Cannot save backup because the directory does not exist");
423
+ });
424
+ const isNewFile = await fsAccess(filename).then(() => false, () => true);
425
+ return runBackup(this[cppdb].backup(this, attachedName, filename, isNewFile), handler || null);
426
+ };
427
+ var runBackup = (backup, handler) => {
428
+ let rate = 0;
429
+ let useDefault = true;
430
+ return new Promise((resolve4, reject) => {
431
+ setImmediate(function step() {
432
+ try {
433
+ const progress = backup.transfer(rate);
434
+ if (!progress.remainingPages) {
435
+ backup.close();
436
+ resolve4(progress);
437
+ return;
438
+ }
439
+ if (useDefault) {
440
+ useDefault = false;
441
+ rate = 100;
442
+ }
443
+ if (handler) {
444
+ const ret = handler(progress);
445
+ if (ret !== void 0) {
446
+ if (typeof ret === "number" && ret === ret) rate = Math.max(0, Math.min(2147483647, Math.round(ret)));
447
+ else throw new TypeError("Expected progress callback to return a number or undefined");
448
+ }
449
+ }
450
+ setImmediate(step);
451
+ } catch (err) {
452
+ backup.close();
453
+ reject(err);
454
+ }
455
+ });
456
+ });
457
+ };
458
+ }
459
+ });
460
+
461
+ // node_modules/better-sqlite3/lib/methods/serialize.js
462
+ var require_serialize = __commonJS({
463
+ "node_modules/better-sqlite3/lib/methods/serialize.js"(exports, module) {
464
+ "use strict";
465
+ var { cppdb } = require_util();
466
+ module.exports = function serialize(options) {
467
+ if (options == null) options = {};
468
+ if (typeof options !== "object") throw new TypeError("Expected first argument to be an options object");
469
+ const attachedName = "attached" in options ? options.attached : "main";
470
+ if (typeof attachedName !== "string") throw new TypeError('Expected the "attached" option to be a string');
471
+ if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
472
+ return this[cppdb].serialize(attachedName);
473
+ };
474
+ }
475
+ });
476
+
477
+ // node_modules/better-sqlite3/lib/methods/function.js
478
+ var require_function = __commonJS({
479
+ "node_modules/better-sqlite3/lib/methods/function.js"(exports, module) {
480
+ "use strict";
481
+ var { getBooleanOption, cppdb } = require_util();
482
+ module.exports = function defineFunction(name, options, fn) {
483
+ if (options == null) options = {};
484
+ if (typeof options === "function") {
485
+ fn = options;
486
+ options = {};
487
+ }
488
+ if (typeof name !== "string") throw new TypeError("Expected first argument to be a string");
489
+ if (typeof fn !== "function") throw new TypeError("Expected last argument to be a function");
490
+ if (typeof options !== "object") throw new TypeError("Expected second argument to be an options object");
491
+ if (!name) throw new TypeError("User-defined function name cannot be an empty string");
492
+ const safeIntegers = "safeIntegers" in options ? +getBooleanOption(options, "safeIntegers") : 2;
493
+ const deterministic = getBooleanOption(options, "deterministic");
494
+ const directOnly = getBooleanOption(options, "directOnly");
495
+ const varargs = getBooleanOption(options, "varargs");
496
+ let argCount = -1;
497
+ if (!varargs) {
498
+ argCount = fn.length;
499
+ if (!Number.isInteger(argCount) || argCount < 0) throw new TypeError("Expected function.length to be a positive integer");
500
+ if (argCount > 100) throw new RangeError("User-defined functions cannot have more than 100 arguments");
501
+ }
502
+ this[cppdb].function(fn, name, argCount, safeIntegers, deterministic, directOnly);
503
+ return this;
504
+ };
505
+ }
506
+ });
507
+
508
+ // node_modules/better-sqlite3/lib/methods/aggregate.js
509
+ var require_aggregate = __commonJS({
510
+ "node_modules/better-sqlite3/lib/methods/aggregate.js"(exports, module) {
511
+ "use strict";
512
+ var { getBooleanOption, cppdb } = require_util();
513
+ module.exports = function defineAggregate(name, options) {
514
+ if (typeof name !== "string") throw new TypeError("Expected first argument to be a string");
515
+ if (typeof options !== "object" || options === null) throw new TypeError("Expected second argument to be an options object");
516
+ if (!name) throw new TypeError("User-defined function name cannot be an empty string");
517
+ const start = "start" in options ? options.start : null;
518
+ const step = getFunctionOption(options, "step", true);
519
+ const inverse = getFunctionOption(options, "inverse", false);
520
+ const result = getFunctionOption(options, "result", false);
521
+ const safeIntegers = "safeIntegers" in options ? +getBooleanOption(options, "safeIntegers") : 2;
522
+ const deterministic = getBooleanOption(options, "deterministic");
523
+ const directOnly = getBooleanOption(options, "directOnly");
524
+ const varargs = getBooleanOption(options, "varargs");
525
+ let argCount = -1;
526
+ if (!varargs) {
527
+ argCount = Math.max(getLength(step), inverse ? getLength(inverse) : 0);
528
+ if (argCount > 0) argCount -= 1;
529
+ if (argCount > 100) throw new RangeError("User-defined functions cannot have more than 100 arguments");
530
+ }
531
+ this[cppdb].aggregate(start, step, inverse, result, name, argCount, safeIntegers, deterministic, directOnly);
532
+ return this;
533
+ };
534
+ var getFunctionOption = (options, key, required) => {
535
+ const value = key in options ? options[key] : null;
536
+ if (typeof value === "function") return value;
537
+ if (value != null) throw new TypeError(`Expected the "${key}" option to be a function`);
538
+ if (required) throw new TypeError(`Missing required option "${key}"`);
539
+ return null;
540
+ };
541
+ var getLength = ({ length }) => {
542
+ if (Number.isInteger(length) && length >= 0) return length;
543
+ throw new TypeError("Expected function.length to be a positive integer");
544
+ };
545
+ }
546
+ });
547
+
548
+ // node_modules/better-sqlite3/lib/methods/table.js
549
+ var require_table = __commonJS({
550
+ "node_modules/better-sqlite3/lib/methods/table.js"(exports, module) {
551
+ "use strict";
552
+ var { cppdb } = require_util();
553
+ module.exports = function defineTable(name, factory) {
554
+ if (typeof name !== "string") throw new TypeError("Expected first argument to be a string");
555
+ if (!name) throw new TypeError("Virtual table module name cannot be an empty string");
556
+ let eponymous = false;
557
+ if (typeof factory === "object" && factory !== null) {
558
+ eponymous = true;
559
+ factory = defer(parseTableDefinition(factory, "used", name));
560
+ } else {
561
+ if (typeof factory !== "function") throw new TypeError("Expected second argument to be a function or a table definition object");
562
+ factory = wrapFactory(factory);
563
+ }
564
+ this[cppdb].table(factory, name, eponymous);
565
+ return this;
566
+ };
567
+ function wrapFactory(factory) {
568
+ return function virtualTableFactory(moduleName, databaseName, tableName, ...args) {
569
+ const thisObject = {
570
+ module: moduleName,
571
+ database: databaseName,
572
+ table: tableName
573
+ };
574
+ const def = apply.call(factory, thisObject, args);
575
+ if (typeof def !== "object" || def === null) {
576
+ throw new TypeError(`Virtual table module "${moduleName}" did not return a table definition object`);
577
+ }
578
+ return parseTableDefinition(def, "returned", moduleName);
579
+ };
580
+ }
581
+ function parseTableDefinition(def, verb, moduleName) {
582
+ if (!hasOwnProperty.call(def, "rows")) {
583
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "rows" property`);
584
+ }
585
+ if (!hasOwnProperty.call(def, "columns")) {
586
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "columns" property`);
587
+ }
588
+ const rows = def.rows;
589
+ if (typeof rows !== "function" || Object.getPrototypeOf(rows) !== GeneratorFunctionPrototype) {
590
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "rows" property (should be a generator function)`);
591
+ }
592
+ let columns = def.columns;
593
+ if (!Array.isArray(columns) || !(columns = [...columns]).every((x) => typeof x === "string")) {
594
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "columns" property (should be an array of strings)`);
595
+ }
596
+ if (columns.length !== new Set(columns).size) {
597
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate column names`);
598
+ }
599
+ if (!columns.length) {
600
+ throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with zero columns`);
601
+ }
602
+ let parameters;
603
+ if (hasOwnProperty.call(def, "parameters")) {
604
+ parameters = def.parameters;
605
+ if (!Array.isArray(parameters) || !(parameters = [...parameters]).every((x) => typeof x === "string")) {
606
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "parameters" property (should be an array of strings)`);
607
+ }
608
+ } else {
609
+ parameters = inferParameters(rows);
610
+ }
611
+ if (parameters.length !== new Set(parameters).size) {
612
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate parameter names`);
613
+ }
614
+ if (parameters.length > 32) {
615
+ throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with more than the maximum number of 32 parameters`);
616
+ }
617
+ for (const parameter of parameters) {
618
+ if (columns.includes(parameter)) {
619
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with column "${parameter}" which was ambiguously defined as both a column and parameter`);
620
+ }
621
+ }
622
+ let safeIntegers = 2;
623
+ if (hasOwnProperty.call(def, "safeIntegers")) {
624
+ const bool = def.safeIntegers;
625
+ if (typeof bool !== "boolean") {
626
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "safeIntegers" property (should be a boolean)`);
627
+ }
628
+ safeIntegers = +bool;
629
+ }
630
+ let directOnly = false;
631
+ if (hasOwnProperty.call(def, "directOnly")) {
632
+ directOnly = def.directOnly;
633
+ if (typeof directOnly !== "boolean") {
634
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "directOnly" property (should be a boolean)`);
635
+ }
636
+ }
637
+ const columnDefinitions = [
638
+ ...parameters.map(identifier).map((str) => `${str} HIDDEN`),
639
+ ...columns.map(identifier)
640
+ ];
641
+ return [
642
+ `CREATE TABLE x(${columnDefinitions.join(", ")});`,
643
+ wrapGenerator(rows, new Map(columns.map((x, i) => [x, parameters.length + i])), moduleName),
644
+ parameters,
645
+ safeIntegers,
646
+ directOnly
647
+ ];
648
+ }
649
+ function wrapGenerator(generator, columnMap, moduleName) {
650
+ return function* virtualTable(...args) {
651
+ const output = args.map((x) => Buffer.isBuffer(x) ? Buffer.from(x) : x);
652
+ for (let i = 0; i < columnMap.size; ++i) {
653
+ output.push(null);
654
+ }
655
+ for (const row of generator(...args)) {
656
+ if (Array.isArray(row)) {
657
+ extractRowArray(row, output, columnMap.size, moduleName);
658
+ yield output;
659
+ } else if (typeof row === "object" && row !== null) {
660
+ extractRowObject(row, output, columnMap, moduleName);
661
+ yield output;
662
+ } else {
663
+ throw new TypeError(`Virtual table module "${moduleName}" yielded something that isn't a valid row object`);
664
+ }
665
+ }
666
+ };
667
+ }
668
+ function extractRowArray(row, output, columnCount, moduleName) {
669
+ if (row.length !== columnCount) {
670
+ throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an incorrect number of columns`);
671
+ }
672
+ const offset = output.length - columnCount;
673
+ for (let i = 0; i < columnCount; ++i) {
674
+ output[i + offset] = row[i];
675
+ }
676
+ }
677
+ function extractRowObject(row, output, columnMap, moduleName) {
678
+ let count = 0;
679
+ for (const key of Object.keys(row)) {
680
+ const index = columnMap.get(key);
681
+ if (index === void 0) {
682
+ throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an undeclared column "${key}"`);
683
+ }
684
+ output[index] = row[key];
685
+ count += 1;
686
+ }
687
+ if (count !== columnMap.size) {
688
+ throw new TypeError(`Virtual table module "${moduleName}" yielded a row with missing columns`);
689
+ }
690
+ }
691
+ function inferParameters({ length }) {
692
+ if (!Number.isInteger(length) || length < 0) {
693
+ throw new TypeError("Expected function.length to be a positive integer");
694
+ }
695
+ const params = [];
696
+ for (let i = 0; i < length; ++i) {
697
+ params.push(`$${i + 1}`);
698
+ }
699
+ return params;
700
+ }
701
+ var { hasOwnProperty } = Object.prototype;
702
+ var { apply } = Function.prototype;
703
+ var GeneratorFunctionPrototype = Object.getPrototypeOf(function* () {
704
+ });
705
+ var identifier = (str) => `"${str.replace(/"/g, '""')}"`;
706
+ var defer = (x) => () => x;
707
+ }
708
+ });
709
+
710
+ // node_modules/better-sqlite3/lib/methods/inspect.js
711
+ var require_inspect = __commonJS({
712
+ "node_modules/better-sqlite3/lib/methods/inspect.js"(exports, module) {
713
+ "use strict";
714
+ var DatabaseInspection = function Database() {
715
+ };
716
+ module.exports = function inspect(depth, opts) {
717
+ return Object.assign(new DatabaseInspection(), this);
718
+ };
719
+ }
720
+ });
721
+
722
+ // node_modules/better-sqlite3/lib/database.js
723
+ var require_database = __commonJS({
724
+ "node_modules/better-sqlite3/lib/database.js"(exports, module) {
725
+ "use strict";
726
+ var fs3 = __require("fs");
727
+ var path3 = __require("path");
728
+ var util = require_util();
729
+ var SqliteError = require_sqlite_error();
730
+ var DEFAULT_ADDON;
731
+ function Database(filenameGiven, options) {
732
+ if (new.target == null) {
733
+ return new Database(filenameGiven, options);
734
+ }
735
+ let buffer;
736
+ if (Buffer.isBuffer(filenameGiven)) {
737
+ buffer = filenameGiven;
738
+ filenameGiven = ":memory:";
739
+ }
740
+ if (filenameGiven == null) filenameGiven = "";
741
+ if (options == null) options = {};
742
+ if (typeof filenameGiven !== "string") throw new TypeError("Expected first argument to be a string");
743
+ if (typeof options !== "object") throw new TypeError("Expected second argument to be an options object");
744
+ if ("readOnly" in options) throw new TypeError('Misspelled option "readOnly" should be "readonly"');
745
+ if ("memory" in options) throw new TypeError('Option "memory" was removed in v7.0.0 (use ":memory:" filename instead)');
746
+ const filename = filenameGiven.trim();
747
+ const anonymous = filename === "" || filename === ":memory:";
748
+ const readonly = util.getBooleanOption(options, "readonly");
749
+ const fileMustExist = util.getBooleanOption(options, "fileMustExist");
750
+ const timeout = "timeout" in options ? options.timeout : 5e3;
751
+ const verbose = "verbose" in options ? options.verbose : null;
752
+ const nativeBinding = "nativeBinding" in options ? options.nativeBinding : null;
753
+ if (readonly && anonymous && !buffer) throw new TypeError("In-memory/temporary databases cannot be readonly");
754
+ if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
755
+ if (timeout > 2147483647) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
756
+ if (verbose != null && typeof verbose !== "function") throw new TypeError('Expected the "verbose" option to be a function');
757
+ if (nativeBinding != null && typeof nativeBinding !== "string" && typeof nativeBinding !== "object") throw new TypeError('Expected the "nativeBinding" option to be a string or addon object');
758
+ let addon;
759
+ if (nativeBinding == null) {
760
+ addon = DEFAULT_ADDON || (DEFAULT_ADDON = require_bindings()("better_sqlite3.node"));
761
+ } else if (typeof nativeBinding === "string") {
762
+ const requireFunc = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : __require;
763
+ addon = requireFunc(path3.resolve(nativeBinding).replace(/(\.node)?$/, ".node"));
764
+ } else {
765
+ addon = nativeBinding;
766
+ }
767
+ if (!addon.isInitialized) {
768
+ addon.setErrorConstructor(SqliteError);
769
+ addon.isInitialized = true;
770
+ }
771
+ if (!anonymous && !filename.startsWith("file:") && !fs3.existsSync(path3.dirname(filename))) {
772
+ throw new TypeError("Cannot open database because the directory does not exist");
773
+ }
774
+ Object.defineProperties(this, {
775
+ [util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) },
776
+ ...wrappers.getters
777
+ });
778
+ }
779
+ var wrappers = require_wrappers();
780
+ Database.prototype.prepare = wrappers.prepare;
781
+ Database.prototype.transaction = require_transaction();
782
+ Database.prototype.pragma = require_pragma();
783
+ Database.prototype.backup = require_backup();
784
+ Database.prototype.serialize = require_serialize();
785
+ Database.prototype.function = require_function();
786
+ Database.prototype.aggregate = require_aggregate();
787
+ Database.prototype.table = require_table();
788
+ Database.prototype.loadExtension = wrappers.loadExtension;
789
+ Database.prototype.exec = wrappers.exec;
790
+ Database.prototype.close = wrappers.close;
791
+ Database.prototype.defaultSafeIntegers = wrappers.defaultSafeIntegers;
792
+ Database.prototype.unsafeMode = wrappers.unsafeMode;
793
+ Database.prototype[util.inspect] = require_inspect();
794
+ module.exports = Database;
795
+ }
796
+ });
797
+
798
+ // node_modules/better-sqlite3/lib/index.js
799
+ var require_lib = __commonJS({
800
+ "node_modules/better-sqlite3/lib/index.js"(exports, module) {
801
+ "use strict";
802
+ module.exports = require_database();
803
+ module.exports.SqliteError = require_sqlite_error();
804
+ }
805
+ });
806
+
1
807
  // src/server/container/index.ts
2
808
  var Container = class {
3
809
  bindings = /* @__PURE__ */ new Map();
@@ -416,8 +1222,15 @@ var SuperRequest = class {
416
1222
  }
417
1223
  async readBodyFromStream() {
418
1224
  const chunks = [];
1225
+ const MAX_BODY_SIZE = 10 * 1024 * 1024;
1226
+ let totalSize = 0;
419
1227
  for await (const chunk of this.raw) {
420
- chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
1228
+ const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
1229
+ totalSize += buf.length;
1230
+ if (totalSize > MAX_BODY_SIZE) {
1231
+ throw new Error("Request body too large. Maximum size is 10MB.");
1232
+ }
1233
+ chunks.push(buf);
421
1234
  }
422
1235
  const raw = Buffer.concat(chunks);
423
1236
  const text = raw.toString("utf-8");
@@ -682,7 +1495,7 @@ function parseContentType(headerSection) {
682
1495
 
683
1496
  // src/server/http/response.ts
684
1497
  import { createReadStream, stat } from "fs";
685
- import { basename as basename2, extname as extname2 } from "path";
1498
+ import { basename as basename2, extname as extname2, resolve } from "path";
686
1499
  import { promisify } from "util";
687
1500
  var statAsync = promisify(stat);
688
1501
  var MIME_TYPES = {
@@ -765,6 +1578,9 @@ var SuperResponse = class {
765
1578
  return this.send(html, status, "text/html; charset=utf-8");
766
1579
  }
767
1580
  redirect(url2, status = HttpStatus.FOUND) {
1581
+ if (url2.includes("\r") || url2.includes("\n")) {
1582
+ throw new Error("Invalid redirect URL");
1583
+ }
768
1584
  this._statusCode = status;
769
1585
  this._headers.set("location", url2);
770
1586
  this._body = null;
@@ -789,6 +1605,13 @@ var SuperResponse = class {
789
1605
  }
790
1606
  async file(filePath, options) {
791
1607
  const fullPath = options?.root ? joinPath(options.root, filePath) : filePath;
1608
+ const resolved = resolve(fullPath);
1609
+ const root = options?.root ? resolve(options.root) : null;
1610
+ if (root !== null && !resolved.startsWith(root)) {
1611
+ this._statusCode = HttpStatus.FORBIDDEN;
1612
+ this._body = null;
1613
+ return this;
1614
+ }
792
1615
  try {
793
1616
  const stats = await statAsync(fullPath);
794
1617
  if (!stats.isFile()) {
@@ -919,12 +1742,12 @@ var NodeEngine = class {
919
1742
  return {
920
1743
  raw: server,
921
1744
  close: () => {
922
- return new Promise((resolve2, reject) => {
1745
+ return new Promise((resolve4, reject) => {
923
1746
  server.close((err) => {
924
1747
  if (err !== void 0 && err !== null) {
925
1748
  reject(err);
926
1749
  } else {
927
- resolve2();
1750
+ resolve4();
928
1751
  }
929
1752
  });
930
1753
  });
@@ -946,8 +1769,8 @@ var NodeEngine = class {
946
1769
  // src/server/middleware/index.ts
947
1770
  import { randomUUID as randomUUID2 } from "crypto";
948
1771
  import { createReadStream as createReadStream2, existsSync, statSync } from "fs";
949
- import { extname as extname3, join as join2 } from "path";
950
- import { createGzip } from "zlib";
1772
+ import { extname as extname3, join as join2, resolve as resolve2 } from "path";
1773
+ import { gzipSync } from "zlib";
951
1774
  function staticFiles(root, options) {
952
1775
  const opts = {
953
1776
  maxAge: 0,
@@ -986,6 +1809,11 @@ function staticFiles(root, options) {
986
1809
  }
987
1810
  }
988
1811
  let fullPath = join2(root, filePath);
1812
+ const resolvedRoot = resolve2(root);
1813
+ const resolvedPath = resolve2(fullPath);
1814
+ if (!resolvedPath.startsWith(resolvedRoot)) {
1815
+ return next();
1816
+ }
989
1817
  if (!existsSync(fullPath)) {
990
1818
  let found = false;
991
1819
  for (const ext2 of opts.extensions) {
@@ -1017,8 +1845,8 @@ function staticFiles(root, options) {
1017
1845
  const readStream = createReadStream2(fullPath);
1018
1846
  readStream.pipe(response.rawResponse);
1019
1847
  response.rawResponse.statusCode = HttpStatus.OK;
1020
- return new Promise((resolve2, reject) => {
1021
- readStream.on("end", () => resolve2());
1848
+ return new Promise((resolve4, reject) => {
1849
+ readStream.on("end", () => resolve4());
1022
1850
  readStream.on("error", (err) => reject(err));
1023
1851
  });
1024
1852
  };
@@ -1275,12 +2103,6 @@ function singularize(word) {
1275
2103
  if (lastChar === "S") return word.slice(0, -1);
1276
2104
  return word;
1277
2105
  }
1278
- function createControllerInstance(controller, ctx) {
1279
- const instance = new controller();
1280
- instance.__ctx = ctx;
1281
- instance.__container = ctx.container;
1282
- return instance;
1283
- }
1284
2106
 
1285
2107
  // src/server/cache/index.ts
1286
2108
  import * as crypto from "crypto";
@@ -2006,47 +2828,66 @@ async function createMysqlDriver(config) {
2006
2828
  "MySQL driver (mysql2) is not installed. Run: npm install mysql2"
2007
2829
  );
2008
2830
  }
2009
- let connection = null;
2831
+ let pool = null;
2010
2832
  const dialect = new MysqlDialect();
2011
2833
  const driver = {
2012
2834
  async connect() {
2013
- connection = await mysqlPackage.createConnection({
2835
+ pool = mysqlPackage.createPool({
2014
2836
  host: config.host ?? "127.0.0.1",
2015
2837
  port: config.port ?? 3306,
2016
2838
  user: config.username,
2017
2839
  password: config.password,
2018
2840
  database: config.database,
2019
- charset: config.charset ?? "utf8mb4"
2841
+ charset: config.charset ?? "utf8mb4",
2842
+ waitForConnections: true,
2843
+ connectionLimit: 10,
2844
+ queueLimit: 0
2020
2845
  });
2846
+ const conn = await pool.getConnection();
2847
+ conn.release();
2021
2848
  },
2022
2849
  async disconnect() {
2023
- if (connection !== null) {
2024
- await connection.end();
2025
- connection = null;
2850
+ if (pool !== null) {
2851
+ await pool.end();
2852
+ pool = null;
2026
2853
  }
2027
2854
  },
2028
2855
  isConnected() {
2029
- return connection !== null;
2856
+ return pool !== null;
2030
2857
  },
2031
2858
  async raw(sql, bindings) {
2032
- if (connection === null) {
2859
+ if (pool === null) {
2033
2860
  throw new Error("Database not connected. Call connect() first.");
2034
2861
  }
2035
- const [rows, fields] = await connection.execute(sql, bindings ?? []);
2862
+ const [rows, fields] = await pool.execute(sql, bindings ?? []);
2036
2863
  return { rows, fields };
2037
2864
  },
2038
2865
  async transaction(callback) {
2039
- if (connection === null) {
2040
- throw new Error("Database not connected. Call connect() first.");
2041
- }
2042
- await connection.beginTransaction();
2866
+ if (pool === null) throw new Error("Database not connected.");
2867
+ const conn = await pool.getConnection();
2043
2868
  try {
2044
- const result = await callback(driver);
2045
- await connection.commit();
2869
+ await conn.beginTransaction();
2870
+ const trxDriver = {
2871
+ ...driver,
2872
+ async raw(sql, bindings) {
2873
+ const [rows, fields] = await conn.execute(sql, bindings ?? []);
2874
+ return { rows, fields };
2875
+ },
2876
+ getDialect() {
2877
+ return dialect;
2878
+ },
2879
+ getDriver() {
2880
+ return "mysql";
2881
+ }
2882
+ };
2883
+ const result = await callback(trxDriver);
2884
+ await conn.commit();
2046
2885
  return result;
2047
2886
  } catch (err) {
2048
- await connection.rollback();
2887
+ await conn.rollback();
2049
2888
  throw err;
2889
+ } finally {
2890
+ conn.release();
2050
2891
  }
2051
2892
  },
2052
2893
  getDialect() {
@@ -2139,7 +2980,7 @@ async function createPostgresqlDriver(config) {
2139
2980
  async function createSqliteDriver(config) {
2140
2981
  let sqlitePackage;
2141
2982
  try {
2142
- sqlitePackage = await import("better-sqlite3");
2983
+ sqlitePackage = await Promise.resolve().then(() => __toESM(require_lib(), 1));
2143
2984
  } catch {
2144
2985
  throw new Error(
2145
2986
  "SQLite driver (better-sqlite3) is not installed. Run: npm install better-sqlite3"
@@ -2186,10 +3027,15 @@ async function createSqliteDriver(config) {
2186
3027
  if (db === null) {
2187
3028
  throw new Error("Database not connected. Call connect() first.");
2188
3029
  }
2189
- const txn = db.transaction(async () => {
2190
- return await callback(driver);
2191
- });
2192
- return txn();
3030
+ db.exec("BEGIN");
3031
+ try {
3032
+ const result = await callback(driver);
3033
+ db.exec("COMMIT");
3034
+ return result;
3035
+ } catch (err) {
3036
+ db.exec("ROLLBACK");
3037
+ throw err;
3038
+ }
2193
3039
  },
2194
3040
  getDialect() {
2195
3041
  return dialect;
@@ -2456,15 +3302,30 @@ var QueryBuilder = class _QueryBuilder {
2456
3302
  async insert(data) {
2457
3303
  const { sql, bindings } = this.compileInsert(data);
2458
3304
  const driverType = this.connection.getDriver();
3305
+ const dialect = this.connection.getDialect();
2459
3306
  if (driverType === "postgresql") {
2460
- const dialect = this.connection.getDialect();
2461
3307
  const returningSQL = dialect.compileInsertReturning(sql, bindings);
2462
3308
  const result2 = await this.connection.raw(returningSQL, bindings);
2463
3309
  return result2.rows.length > 0 ? Number(result2.rows[0].id) ?? 0 : 0;
2464
3310
  }
2465
3311
  const result = await this.connection.raw(sql, bindings);
2466
- if (result.rows && result.rows.length > 0) {
2467
- return Number(result.rows[0].id) ?? result.rows[0].insertId ?? 0;
3312
+ if (driverType === "mysql") {
3313
+ const header = result.rows;
3314
+ if (header && typeof header === "object" && "insertId" in header) {
3315
+ return Number(header.insertId) ?? 0;
3316
+ }
3317
+ if (Array.isArray(result.rows) && result.rows.length > 0) {
3318
+ const row = result.rows[0];
3319
+ return Number(row?.insertId ?? row?.id ?? 0);
3320
+ }
3321
+ return 0;
3322
+ }
3323
+ if (driverType === "sqlite") {
3324
+ const lastIdResult = await this.connection.raw("SELECT last_insert_rowid() as id");
3325
+ if (lastIdResult.rows.length > 0) {
3326
+ return Number(lastIdResult.rows[0].id) ?? 0;
3327
+ }
3328
+ return 0;
2468
3329
  }
2469
3330
  return 0;
2470
3331
  }
@@ -2573,7 +3434,7 @@ var QueryBuilder = class _QueryBuilder {
2573
3434
  const output = `SQL: ${sql}
2574
3435
  Bindings: ${JSON.stringify(bindings)}`;
2575
3436
  console.error(output);
2576
- process.exit(1);
3437
+ return output;
2577
3438
  }
2578
3439
  compileJoins(dialect) {
2579
3440
  if (this.joins.length === 0) return "";
@@ -2595,7 +3456,11 @@ Bindings: ${JSON.stringify(bindings)}`;
2595
3456
  if (sql !== null) parts.push(sql);
2596
3457
  }
2597
3458
  if (parts.length === 0) return "";
2598
- return parts.join(" AND ");
3459
+ return parts.reduce((acc, part, i) => {
3460
+ if (i === 0) return part;
3461
+ const bool = wheres[i]?.boolean ?? "and";
3462
+ return `${acc} ${bool.toUpperCase()} ${part}`;
3463
+ }, "");
2599
3464
  }
2600
3465
  compileSingleWhere(w, dialect, bindings) {
2601
3466
  const col = w.column ? dialect.wrapIdentifier(w.column) : "";
@@ -2644,15 +3509,18 @@ Bindings: ${JSON.stringify(bindings)}`;
2644
3509
  }
2645
3510
  }
2646
3511
  compileNestedWhere(wheres, dialect, bindings) {
3512
+ if (wheres.length === 0) return "";
2647
3513
  const parts = [];
2648
3514
  for (const w of wheres) {
2649
3515
  const sql = this.compileSingleWhere(w, dialect, bindings);
2650
- if (sql !== null) {
2651
- parts.push(sql);
2652
- }
3516
+ if (sql !== null) parts.push(sql);
2653
3517
  }
2654
3518
  if (parts.length === 0) return "";
2655
- return parts.join(" AND ");
3519
+ return parts.reduce((acc, part, i) => {
3520
+ if (i === 0) return part;
3521
+ const bool = wheres[i]?.boolean ?? "and";
3522
+ return `${acc} ${bool.toUpperCase()} ${part}`;
3523
+ }, "");
2656
3524
  }
2657
3525
  compileHavings(dialect, bindings) {
2658
3526
  if (this.havings.length === 0) return "";
@@ -3477,6 +4345,116 @@ var Seeder = class {
3477
4345
  }
3478
4346
  };
3479
4347
 
4348
+ // src/server/database/model.ts
4349
+ var Model = class {
4350
+ id;
4351
+ static table = "";
4352
+ static connection = null;
4353
+ static queryRunner = null;
4354
+ static setConnection(conn) {
4355
+ this.connection = conn;
4356
+ this.queryRunner = conn;
4357
+ }
4358
+ static query() {
4359
+ if (!this.queryRunner) {
4360
+ throw new Error("Database connection not set. Call Model.setConnection() first.");
4361
+ }
4362
+ return new QueryBuilder(this.queryRunner, this.table);
4363
+ }
4364
+ static async all() {
4365
+ const rows = await this.query().get();
4366
+ return rows.map((row) => this.hydrate(row));
4367
+ }
4368
+ static async find(id) {
4369
+ const row = await this.query().find(id);
4370
+ if (!row) return null;
4371
+ return this.hydrate(row);
4372
+ }
4373
+ static async where(column, value) {
4374
+ return this.query().where(column, value);
4375
+ }
4376
+ static async create(data) {
4377
+ const id = await this.query().insert(data);
4378
+ return this.find(id);
4379
+ }
4380
+ static async updateOrCreate(attributes, values) {
4381
+ const qb = this.query();
4382
+ for (const [key, value] of Object.entries(attributes)) {
4383
+ qb.where(key, value);
4384
+ }
4385
+ const existing = await qb.first();
4386
+ if (existing) {
4387
+ const mergeValues = values ?? attributes;
4388
+ const id = existing.id;
4389
+ const updateQb = new QueryBuilder(this.queryRunner, this.table);
4390
+ for (const [key, value] of Object.entries(attributes)) {
4391
+ updateQb.where(key, value);
4392
+ }
4393
+ await updateQb.update(mergeValues);
4394
+ return this.find(id);
4395
+ }
4396
+ return this.create({ ...attributes, ...values });
4397
+ }
4398
+ async save() {
4399
+ const ModelClass = this.constructor;
4400
+ const id = this.id;
4401
+ if (id !== void 0 && id !== null) {
4402
+ await ModelClass.query().where("id", id).update(this.getData());
4403
+ } else {
4404
+ const newId = await ModelClass.query().insert(this.getData());
4405
+ this.id = newId;
4406
+ }
4407
+ }
4408
+ async delete() {
4409
+ const ModelClass = this.constructor;
4410
+ const id = this.id;
4411
+ if (id !== void 0 && id !== null) {
4412
+ await ModelClass.query().where("id", id).delete();
4413
+ }
4414
+ }
4415
+ static belongsTo(relatedModel, foreignKey, ownerKey) {
4416
+ const related = relatedModel.table;
4417
+ const fk = foreignKey ?? `${related}_id`;
4418
+ const ok = ownerKey ?? "id";
4419
+ return this.query().where(fk, ok);
4420
+ }
4421
+ static hasMany(relatedModel, foreignKey, localKey) {
4422
+ const related = relatedModel.table;
4423
+ const fk = foreignKey ?? `${this.table}_id`;
4424
+ const lk = localKey ?? "id";
4425
+ return this.query().join(related, fk, "=", lk);
4426
+ }
4427
+ static hydrate(data) {
4428
+ const instance = new this();
4429
+ for (const [key, value] of Object.entries(data)) {
4430
+ ;
4431
+ instance[key] = value;
4432
+ }
4433
+ return instance;
4434
+ }
4435
+ getData() {
4436
+ const data = {};
4437
+ const instance = this;
4438
+ const prototype = Object.getPrototypeOf(this);
4439
+ const ownKeys = [
4440
+ ...Object.getOwnPropertyNames(instance),
4441
+ ...Object.keys(instance)
4442
+ ];
4443
+ const classKeys = /* @__PURE__ */ new Set([
4444
+ ...Object.getOwnPropertyNames(prototype),
4445
+ "save",
4446
+ "delete",
4447
+ "getData"
4448
+ ]);
4449
+ for (const key of ownKeys) {
4450
+ if (typeof key === "string" && !classKeys.has(key) && key !== "constructor") {
4451
+ data[key] = instance[key];
4452
+ }
4453
+ }
4454
+ return data;
4455
+ }
4456
+ };
4457
+
3480
4458
  // src/server/events/index.ts
3481
4459
  import { EventEmitter } from "events";
3482
4460
  var Event = class {
@@ -4039,7 +5017,7 @@ var SuperApp = class {
4039
5017
  [route.method],
4040
5018
  handlerPath,
4041
5019
  async (ctx) => {
4042
- const instance = createControllerInstance2(ctrl, ctx);
5020
+ const instance = createControllerInstance(ctrl, ctx);
4043
5021
  const handlerFn = instance[handlerName];
4044
5022
  if (typeof handlerFn === "function") {
4045
5023
  await handlerFn.call(instance, ctx);
@@ -4086,10 +5064,10 @@ var SuperApp = class {
4086
5064
  );
4087
5065
  const listenPort = port ?? 3e3;
4088
5066
  const listenHost = host ?? "0.0.0.0";
4089
- return new Promise((resolve2) => {
5067
+ return new Promise((resolve4) => {
4090
5068
  const raw = this.serverInstance.raw;
4091
5069
  raw.listen(listenPort, listenHost, () => {
4092
- resolve2();
5070
+ resolve4();
4093
5071
  });
4094
5072
  });
4095
5073
  }
@@ -4145,7 +5123,7 @@ var SuperApp = class {
4145
5123
  function speexjs(options) {
4146
5124
  return new SuperApp(options);
4147
5125
  }
4148
- function createControllerInstance2(controller, ctx) {
5126
+ function createControllerInstance(controller, ctx) {
4149
5127
  const instance = new controller();
4150
5128
  instance.__ctx = ctx;
4151
5129
  instance.__container = ctx.container;
@@ -4159,6 +5137,7 @@ export {
4159
5137
  ForeignKeyDefinition,
4160
5138
  LocalDisk,
4161
5139
  Migrator,
5140
+ Model,
4162
5141
  MysqlDialect,
4163
5142
  Pagination,
4164
5143
  PostgresqlDialect,
@@ -4171,6 +5150,7 @@ export {
4171
5150
  TableBlueprint,
4172
5151
  URLBuilder,
4173
5152
  cacheResponse,
5153
+ createControllerInstance,
4174
5154
  createDialect,
4175
5155
  createDriver,
4176
5156
  createEvent,