zerobucket 0.6.1 → 0.7.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.
package/dist/index.js CHANGED
@@ -1880,6 +1880,711 @@ var SQLiteBackend = class {
1880
1880
  }
1881
1881
  };
1882
1882
 
1883
+ // src/adapters/mysql.ts
1884
+ import { randomUUID as randomUUID2 } from "crypto";
1885
+ var SCHEMA3 = `
1886
+ CREATE TABLE IF NOT EXISTS zerobucket_images (
1887
+ id CHAR(36) PRIMARY KEY,
1888
+ data LONGBLOB NOT NULL,
1889
+ mime_type VARCHAR(255) NOT NULL,
1890
+ original_filename TEXT,
1891
+ size_bytes INT NOT NULL,
1892
+ width INT,
1893
+ height INT,
1894
+ checksum_sha256 CHAR(64) NOT NULL,
1895
+ created_at DATETIME(6) NOT NULL,
1896
+ updated_at DATETIME(6) NOT NULL,
1897
+ INDEX idx_zerobucket_checksum (checksum_sha256),
1898
+ INDEX idx_zerobucket_created_at (created_at)
1899
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
1900
+ var TIERING_CHECK = "chk_zerobucket_storage";
1901
+ var MIGRATE_LOCK = "zerobucket_migrate";
1902
+ var COLS2 = `id, data, mime_type, original_filename, size_bytes, width, height, checksum_sha256, storage_backend, object_storage_bucket, object_storage_key`;
1903
+ var INSERT3 = `INSERT INTO zerobucket_images (id, data, mime_type, original_filename, size_bytes, width, height, checksum_sha256, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
1904
+ var SELECT_FULL3 = `SELECT ${COLS2} FROM zerobucket_images WHERE id = ?`;
1905
+ var SELECT_MANY2 = `SELECT ${COLS2} FROM zerobucket_images WHERE id IN (?)`;
1906
+ var SELECT_METADATA3 = `SELECT id, mime_type, original_filename, size_bytes, width, height, checksum_sha256 FROM zerobucket_images WHERE id = ?`;
1907
+ var STREAM_INFO2 = `SELECT size_bytes, storage_backend, object_storage_key FROM zerobucket_images WHERE id = ?`;
1908
+ var CHUNK2 = `SELECT SUBSTRING(data FROM ? FOR ?) AS chunk FROM zerobucket_images WHERE id = ?`;
1909
+ var EXISTS3 = `SELECT 1 FROM zerobucket_images WHERE id = ?`;
1910
+ var LOCK_ONE = `SELECT storage_backend, object_storage_key FROM zerobucket_images WHERE id = ? FOR UPDATE`;
1911
+ var DELETE_ONE2 = `DELETE FROM zerobucket_images WHERE id = ?`;
1912
+ var LOCK_MANY = `SELECT id, storage_backend, object_storage_key FROM zerobucket_images WHERE id IN (?) ORDER BY id FOR UPDATE`;
1913
+ var DELETE_MANY2 = `DELETE FROM zerobucket_images WHERE id IN (?)`;
1914
+ var SELECT_FOR_TIERING3 = `SELECT data, mime_type, size_bytes, storage_backend FROM zerobucket_images WHERE id = ? FOR UPDATE`;
1915
+ var UPDATE_AFTER_TIERING3 = `UPDATE zerobucket_images SET data = NULL, storage_backend = 'object_storage', object_storage_bucket = ?, object_storage_key = ?, updated_at = ? WHERE id = ?`;
1916
+ var LOCAL = "mysql";
1917
+ var RETRYABLE_ERRNOS = /* @__PURE__ */ new Set([
1918
+ 1205,
1919
+ // ER_LOCK_WAIT_TIMEOUT
1920
+ 1213,
1921
+ // ER_LOCK_DEADLOCK
1922
+ 1040,
1923
+ // ER_CON_COUNT_ERROR (too many connections)
1924
+ 1053,
1925
+ // ER_SERVER_SHUTDOWN
1926
+ 1927,
1927
+ // ER_CONNECTION_KILLED (MariaDB)
1928
+ 2002,
1929
+ // CR_CONNECTION_ERROR
1930
+ 2003,
1931
+ // CR_CONN_HOST_ERROR
1932
+ 2006,
1933
+ // CR_SERVER_GONE_ERROR
1934
+ 2013
1935
+ // CR_SERVER_LOST
1936
+ ]);
1937
+ var CONNECTION_CODES = /* @__PURE__ */ new Set([
1938
+ "PROTOCOL_CONNECTION_LOST",
1939
+ "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR",
1940
+ "PROTOCOL_ENQUEUE_AFTER_QUIT",
1941
+ "ECONNRESET",
1942
+ "ECONNREFUSED",
1943
+ "ETIMEDOUT",
1944
+ "EPIPE",
1945
+ "ECONNABORTED"
1946
+ ]);
1947
+ function errnoOf(exc) {
1948
+ const n = exc?.errno;
1949
+ return typeof n === "number" ? n : void 0;
1950
+ }
1951
+ var PoolTimeoutError = class extends StorageError {
1952
+ };
1953
+ function isRetryable2(exc) {
1954
+ if (!(exc instanceof Error) || exc instanceof PoolTimeoutError) return false;
1955
+ const e = exc;
1956
+ const n = errnoOf(exc);
1957
+ if (n !== void 0 && RETRYABLE_ERRNOS.has(n)) return true;
1958
+ if (typeof e.code === "string") {
1959
+ if (CONNECTION_CODES.has(e.code)) return true;
1960
+ if (e.code === "ER_LOCK_WAIT_TIMEOUT" || e.code === "ER_LOCK_DEADLOCK" || e.code === "ER_CON_COUNT_ERROR" || e.code === "ER_SERVER_SHUTDOWN" || e.code === "ER_CONNECTION_KILLED")
1961
+ return true;
1962
+ }
1963
+ return false;
1964
+ }
1965
+ function isConnectionLevel2(exc) {
1966
+ if (!(exc instanceof Error)) return false;
1967
+ const e = exc;
1968
+ if (e.fatal === true) return true;
1969
+ if (typeof e.code === "string" && CONNECTION_CODES.has(e.code)) return true;
1970
+ const n = errnoOf(exc);
1971
+ return n === 2006 || n === 2013 || n === 1927 || n === 1053;
1972
+ }
1973
+ var MAX_BACKOFF_MS2 = 2e3;
1974
+ function backoffDelayMs2(attempt, baseDelayMs) {
1975
+ const exponential = baseDelayMs * 2 ** (attempt - 1);
1976
+ const jitter = Math.random() * baseDelayMs;
1977
+ return Math.min(exponential + jitter, MAX_BACKOFF_MS2);
1978
+ }
1979
+ function friendly(prefix, exc) {
1980
+ if (errnoOf(exc) === 1153) {
1981
+ return new StorageError(
1982
+ `${prefix}: the image is larger than the server's max_allowed_packet. Raise it (for example \`SET GLOBAL max_allowed_packet = 67108864\`, and set it in my.cnf to make it permanent), or lower \`maxBytes\`.`,
1983
+ { cause: exc }
1984
+ );
1985
+ }
1986
+ return wrap(prefix, exc);
1987
+ }
1988
+ function mysqlNow() {
1989
+ return `${(/* @__PURE__ */ new Date()).toISOString().replace("T", " ").replace("Z", "")}000`;
1990
+ }
1991
+ function normalizeUrl(url) {
1992
+ return url.replace(/^mariadb:\/\//i, "mysql://");
1993
+ }
1994
+ function isMysqlConnection(c) {
1995
+ const m = c;
1996
+ return typeof m.query === "function" && typeof m.execute === "function";
1997
+ }
1998
+ var MySQLBackend = class {
1999
+ constructor(opts) {
2000
+ this.opts = opts;
2001
+ if (!opts.connectionString && !opts.pool) {
2002
+ throw new TypeError(
2003
+ "MySQLBackend requires either `connectionString` or `pool`"
2004
+ );
2005
+ }
2006
+ if (opts.dedup) {
2007
+ throw new TypeError(
2008
+ "dedup mode is not available for MySQL in the npm package yet (the Python package supports it). Use classic mode, or Postgres/SQLite for dedup."
2009
+ );
2010
+ }
2011
+ this.ownsPool = !opts.pool;
2012
+ this.pool = opts.pool;
2013
+ this.maxRetries = opts.maxRetries ?? 3;
2014
+ this.retryBaseDelayMs = opts.retryBaseDelayMs ?? 100;
2015
+ this.poolTimeoutMs = opts.poolTimeoutMs ?? 1e4;
2016
+ this.autoMigrate = opts.autoMigrate ?? true;
2017
+ }
2018
+ opts;
2019
+ pool;
2020
+ ownsPool;
2021
+ readyPromise;
2022
+ closed = false;
2023
+ maxRetries;
2024
+ retryBaseDelayMs;
2025
+ poolTimeoutMs;
2026
+ autoMigrate;
2027
+ // ---- lifecycle -------------------------------------------------------
2028
+ ready() {
2029
+ if (this.closed)
2030
+ return Promise.reject(
2031
+ new StorageError("This ZeroBucket instance has been closed")
2032
+ );
2033
+ this.readyPromise ??= this.init().catch((exc) => {
2034
+ this.readyPromise = void 0;
2035
+ throw exc;
2036
+ });
2037
+ return this.readyPromise;
2038
+ }
2039
+ async init() {
2040
+ if (!this.pool) {
2041
+ let mod;
2042
+ try {
2043
+ mod = await import("mysql2/promise");
2044
+ } catch (cause) {
2045
+ throw new StorageError(
2046
+ "MySQLBackend requires the 'mysql2' package. Install it with: npm install mysql2",
2047
+ { cause }
2048
+ );
2049
+ }
2050
+ const createPool = (mod.default ?? mod).createPool;
2051
+ this.pool = createPool({
2052
+ uri: normalizeUrl(this.opts.connectionString),
2053
+ connectionLimit: this.opts.poolMaxSize ?? 5,
2054
+ waitForConnections: true,
2055
+ queueLimit: 0,
2056
+ connectTimeout: this.poolTimeoutMs,
2057
+ charset: "utf8mb4",
2058
+ timezone: "Z",
2059
+ enableKeepAlive: true
2060
+ });
2061
+ }
2062
+ try {
2063
+ if (this.autoMigrate) {
2064
+ await this.runPooled("migrate", (q) => this.migrate(q), false);
2065
+ } else {
2066
+ await this.runPooled("ping", (q) => q.query("SELECT 1"), false);
2067
+ }
2068
+ } catch (exc) {
2069
+ if (this.ownsPool) {
2070
+ await this.pool.end().catch(() => {
2071
+ });
2072
+ this.pool = void 0;
2073
+ }
2074
+ throw exc instanceof StorageError ? exc : new StorageError(`Could not connect to MySQL/MariaDB: ${msg(exc)}`, {
2075
+ cause: exc
2076
+ });
2077
+ }
2078
+ }
2079
+ /**
2080
+ * Create the schema if missing, under a server-side named lock so concurrent first callers (in this
2081
+ * or any other process) run the DDL one at a time. Every step checks before it changes anything, so
2082
+ * re-running is a no-op.
2083
+ */
2084
+ async migrate(q) {
2085
+ const locked = (await q.query("SELECT GET_LOCK(?, 30) AS got", [MIGRATE_LOCK]))[0][0]?.got;
2086
+ if (locked !== 1) {
2087
+ throw new StorageError(
2088
+ "Timed out waiting for another process to finish migrating the ZeroBucket schema."
2089
+ );
2090
+ }
2091
+ try {
2092
+ const tableExists = (await q.query(
2093
+ "SELECT 1 FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'zerobucket_images'"
2094
+ ))[0].length > 0;
2095
+ if (!tableExists) await q.query(SCHEMA3);
2096
+ const cols = new Map(
2097
+ (await q.query(
2098
+ "SELECT COLUMN_NAME AS c, IS_NULLABLE AS nullable FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'zerobucket_images'"
2099
+ ))[0].map((r) => [r.c, r.nullable])
2100
+ );
2101
+ if (!cols.has("storage_backend"))
2102
+ await q.query(
2103
+ `ALTER TABLE zerobucket_images ADD COLUMN storage_backend VARCHAR(20) NOT NULL DEFAULT '${LOCAL}'`
2104
+ );
2105
+ if (!cols.has("object_storage_bucket"))
2106
+ await q.query(
2107
+ "ALTER TABLE zerobucket_images ADD COLUMN object_storage_bucket VARCHAR(255)"
2108
+ );
2109
+ if (!cols.has("object_storage_key"))
2110
+ await q.query(
2111
+ "ALTER TABLE zerobucket_images ADD COLUMN object_storage_key VARCHAR(512)"
2112
+ );
2113
+ if (cols.get("data") === "NO")
2114
+ await q.query(
2115
+ "ALTER TABLE zerobucket_images MODIFY data LONGBLOB NULL"
2116
+ );
2117
+ const hasCheck = (await q.query(
2118
+ "SELECT 1 FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'zerobucket_images' AND CONSTRAINT_NAME = ?",
2119
+ [TIERING_CHECK]
2120
+ ))[0].length > 0;
2121
+ if (!hasCheck) {
2122
+ await q.query(
2123
+ `ALTER TABLE zerobucket_images ADD CONSTRAINT ${TIERING_CHECK} CHECK ((storage_backend = '${LOCAL}' AND data IS NOT NULL AND object_storage_bucket IS NULL AND object_storage_key IS NULL) OR (storage_backend = 'object_storage' AND data IS NULL AND object_storage_bucket IS NOT NULL AND object_storage_key IS NOT NULL))`
2124
+ );
2125
+ }
2126
+ } catch (exc) {
2127
+ throw new StorageError(`Migration failed: ${msg(exc)}`, { cause: exc });
2128
+ } finally {
2129
+ await q.query("SELECT RELEASE_LOCK(?)", [MIGRATE_LOCK]).catch(() => {
2130
+ });
2131
+ }
2132
+ }
2133
+ async close() {
2134
+ this.closed = true;
2135
+ const pool = this.pool;
2136
+ this.pool = void 0;
2137
+ this.readyPromise = void 0;
2138
+ if (pool && this.ownsPool) await pool.end();
2139
+ }
2140
+ // ---- execution core --------------------------------------------------
2141
+ emit(operation, start, success, error, retryCount) {
2142
+ const cb = this.opts.onOperation;
2143
+ if (!cb) return;
2144
+ try {
2145
+ cb({
2146
+ operation,
2147
+ durationMs: performance.now() - start,
2148
+ success,
2149
+ error,
2150
+ retryCount
2151
+ });
2152
+ } catch {
2153
+ }
2154
+ }
2155
+ /** Wait for a pool slot, but never longer than poolTimeoutMs. */
2156
+ acquire() {
2157
+ const pool = this.pool;
2158
+ if (this.closed || !pool)
2159
+ return Promise.reject(
2160
+ new StorageError("This ZeroBucket instance has been closed")
2161
+ );
2162
+ return new Promise((resolve, reject) => {
2163
+ let settled = false;
2164
+ const timer = setTimeout(() => {
2165
+ settled = true;
2166
+ reject(
2167
+ new PoolTimeoutError(
2168
+ `Timed out after ${this.poolTimeoutMs}ms waiting for a MySQL/MariaDB connection (poolMaxSize=${this.opts.poolMaxSize ?? 5}; all connections are in use).`
2169
+ )
2170
+ );
2171
+ }, this.poolTimeoutMs);
2172
+ pool.getConnection().then(
2173
+ (conn) => {
2174
+ if (settled) {
2175
+ conn.release();
2176
+ return;
2177
+ }
2178
+ settled = true;
2179
+ clearTimeout(timer);
2180
+ resolve(conn);
2181
+ },
2182
+ (exc) => {
2183
+ if (settled) return;
2184
+ settled = true;
2185
+ clearTimeout(timer);
2186
+ reject(exc);
2187
+ }
2188
+ );
2189
+ });
2190
+ }
2191
+ /**
2192
+ * Run `work` on a pooled connection. `tx` wraps it in a transaction. Transient failures retry the
2193
+ * WHOLE closure on a fresh connection after a full rollback.
2194
+ */
2195
+ async runPooled(operation, work, tx) {
2196
+ const start = performance.now();
2197
+ let attempt = 0;
2198
+ for (; ; ) {
2199
+ let conn;
2200
+ let destroy = false;
2201
+ try {
2202
+ conn = await this.acquire();
2203
+ if (tx) await conn.beginTransaction();
2204
+ const result = await work(conn);
2205
+ if (tx) await conn.commit();
2206
+ this.emit(operation, start, true, null, attempt);
2207
+ return result;
2208
+ } catch (exc) {
2209
+ if (conn && tx) {
2210
+ try {
2211
+ await conn.rollback();
2212
+ } catch {
2213
+ destroy = true;
2214
+ }
2215
+ }
2216
+ if (isConnectionLevel2(exc)) destroy = true;
2217
+ attempt += 1;
2218
+ if (attempt > this.maxRetries || !isRetryable2(exc)) {
2219
+ this.emit(operation, start, false, msg(exc), attempt - 1);
2220
+ throw exc;
2221
+ }
2222
+ } finally {
2223
+ if (conn) {
2224
+ if (destroy) conn.destroy();
2225
+ else conn.release();
2226
+ }
2227
+ }
2228
+ await sleep(backoffDelayMs2(attempt, this.retryBaseDelayMs));
2229
+ }
2230
+ }
2231
+ /**
2232
+ * Run `work` and return its result, emitting exactly one OperationEvent.
2233
+ *
2234
+ * connection given -> run once on it; no retry, and no commit/rollback of the caller's transaction.
2235
+ * connection absent -> pooled, retried (see runPooled).
2236
+ */
2237
+ async run(operation, connection, work, { tx = false } = {}) {
2238
+ if (connection) {
2239
+ if (!isMysqlConnection(connection)) {
2240
+ throw new TypeError(
2241
+ "MySQLBackend expects a mysql2/promise connection as `connection` (an object with query() and execute())."
2242
+ );
2243
+ }
2244
+ const start = performance.now();
2245
+ try {
2246
+ const result = tx ? await this.atomic(connection, work) : await work(connection);
2247
+ this.emit(operation, start, true, null, 0);
2248
+ return result;
2249
+ } catch (exc) {
2250
+ this.emit(operation, start, false, msg(exc), 0);
2251
+ throw exc;
2252
+ }
2253
+ }
2254
+ await this.ready();
2255
+ return this.runPooled(operation, work, tx);
2256
+ }
2257
+ /**
2258
+ * Make multi-statement work all-or-nothing on a CALLER-supplied connection.
2259
+ *
2260
+ * Is a transaction open? SAVEPOINT followed by ROLLBACK TO SAVEPOINT succeeds only inside one (in
2261
+ * autocommit mode the savepoint is gone before the next statement and the rollback fails with 1305).
2262
+ * - Open -> reuse that SAVEPOINT: a failure undoes only our work; success joins the caller's transaction.
2263
+ * - None -> START TRANSACTION and COMMIT our own, so a failure can never leave half-applied state.
2264
+ */
2265
+ async atomic(conn, work) {
2266
+ let inTransaction;
2267
+ await conn.query("SAVEPOINT zerobucket_atomic");
2268
+ try {
2269
+ await conn.query("ROLLBACK TO SAVEPOINT zerobucket_atomic");
2270
+ inTransaction = true;
2271
+ } catch (exc) {
2272
+ if (errnoOf(exc) !== 1305) throw exc;
2273
+ inTransaction = false;
2274
+ await conn.query("START TRANSACTION");
2275
+ }
2276
+ try {
2277
+ const result = await work(conn);
2278
+ await conn.query(
2279
+ inTransaction ? "RELEASE SAVEPOINT zerobucket_atomic" : "COMMIT"
2280
+ );
2281
+ return result;
2282
+ } catch (exc) {
2283
+ try {
2284
+ await conn.query(
2285
+ inTransaction ? "ROLLBACK TO SAVEPOINT zerobucket_atomic" : "ROLLBACK"
2286
+ );
2287
+ if (inTransaction)
2288
+ await conn.query("RELEASE SAVEPOINT zerobucket_atomic");
2289
+ } catch {
2290
+ }
2291
+ throw exc;
2292
+ }
2293
+ }
2294
+ // ---- StorageBackend --------------------------------------------------
2295
+ async put(row, { connection } = {}) {
2296
+ try {
2297
+ const id = randomUUID2();
2298
+ await this.run("put", connection, async (q) => {
2299
+ await q.execute(INSERT3, rowParams2(id, row, mysqlNow()));
2300
+ });
2301
+ return id;
2302
+ } catch (exc) {
2303
+ throw friendly("Failed to store image", exc);
2304
+ }
2305
+ }
2306
+ async putMany(rows, { connection } = {}) {
2307
+ if (rows.length === 0) return [];
2308
+ try {
2309
+ const ids = rows.map(() => randomUUID2());
2310
+ await this.run(
2311
+ "put_many",
2312
+ connection,
2313
+ async (q) => {
2314
+ const now = mysqlNow();
2315
+ for (let i = 0; i < rows.length; i++)
2316
+ await q.execute(INSERT3, rowParams2(ids[i], rows[i], now));
2317
+ },
2318
+ { tx: true }
2319
+ );
2320
+ return ids;
2321
+ } catch (exc) {
2322
+ throw friendly("Failed to store image batch", exc);
2323
+ }
2324
+ }
2325
+ async get(id, { connection } = {}) {
2326
+ if (!isValidId(id)) return null;
2327
+ let row;
2328
+ try {
2329
+ row = await this.run(
2330
+ "get",
2331
+ connection,
2332
+ async (q) => (await q.query(SELECT_FULL3, [id]))[0][0]
2333
+ );
2334
+ } catch (exc) {
2335
+ throw wrap("Failed to retrieve image", exc);
2336
+ }
2337
+ return row ? this.hydrate(row, id) : null;
2338
+ }
2339
+ async getMany(ids, { connection } = {}) {
2340
+ const valid = ids.filter(isValidId);
2341
+ if (valid.length === 0) return [];
2342
+ let rows;
2343
+ try {
2344
+ rows = await this.run(
2345
+ "get_many",
2346
+ connection,
2347
+ async (q) => (await q.query(SELECT_MANY2, [valid]))[0]
2348
+ );
2349
+ } catch (exc) {
2350
+ throw wrap("Failed to retrieve image batch", exc);
2351
+ }
2352
+ return mapLimit(rows, 4, (r) => this.hydrate(r, String(r.id)));
2353
+ }
2354
+ async getMetadata(id, { connection } = {}) {
2355
+ if (!isValidId(id)) return null;
2356
+ let row;
2357
+ try {
2358
+ row = await this.run(
2359
+ "get_metadata",
2360
+ connection,
2361
+ async (q) => (await q.query(SELECT_METADATA3, [id]))[0][0]
2362
+ );
2363
+ } catch (exc) {
2364
+ throw wrap("Failed to retrieve image metadata", exc);
2365
+ }
2366
+ if (!row) return null;
2367
+ return {
2368
+ id: String(row.id),
2369
+ mimeType: row.mime_type,
2370
+ originalFilename: row.original_filename,
2371
+ sizeBytes: row.size_bytes,
2372
+ width: row.width,
2373
+ height: row.height,
2374
+ checksumSha256: row.checksum_sha256
2375
+ };
2376
+ }
2377
+ async getStream(id, opts) {
2378
+ if (!isValidId(id)) return null;
2379
+ const { connection, chunkSize } = opts;
2380
+ if (!Number.isInteger(chunkSize) || chunkSize <= 0)
2381
+ throw new RangeError("chunkSize must be a positive integer");
2382
+ let info;
2383
+ try {
2384
+ info = await this.run(
2385
+ "get_metadata",
2386
+ connection,
2387
+ async (q) => (await q.query(STREAM_INFO2, [id]))[0][0]
2388
+ );
2389
+ } catch (exc) {
2390
+ throw wrap("Failed to retrieve image metadata", exc);
2391
+ }
2392
+ if (!info) return null;
2393
+ const totalSize = info.size_bytes;
2394
+ const start = opts.range?.start ?? 0;
2395
+ const end = Math.min(opts.range?.end ?? totalSize - 1, totalSize - 1);
2396
+ if (info.storage_backend === "object_storage") {
2397
+ const os = this.opts.objectStorage;
2398
+ if (!os) throw tieredWithoutStorage(id, info.object_storage_key);
2399
+ return streamFromObjectStorage(
2400
+ os,
2401
+ id,
2402
+ info.object_storage_key,
2403
+ start,
2404
+ end,
2405
+ chunkSize
2406
+ );
2407
+ }
2408
+ const self = this;
2409
+ async function* generate() {
2410
+ let offset = start;
2411
+ let delivered = 0;
2412
+ const wanted = Math.max(0, end - start + 1);
2413
+ while (delivered < wanted) {
2414
+ const length = Math.min(chunkSize, wanted - delivered);
2415
+ let row;
2416
+ try {
2417
+ row = await self.run(
2418
+ "get_stream",
2419
+ connection,
2420
+ async (q) => (await q.query(CHUNK2, [offset + 1, length, id]))[0][0]
2421
+ );
2422
+ } catch (exc) {
2423
+ throw wrap("Failed to stream image", exc);
2424
+ }
2425
+ if (!row) {
2426
+ throw new StorageError(
2427
+ `Image ${JSON.stringify(id)} was deleted while streaming (delivered ${delivered} of ${wanted} bytes). Pass connection with your own open transaction if you need a consistent read across concurrent writers.`
2428
+ );
2429
+ }
2430
+ const chunk = row.chunk;
2431
+ if (!chunk || chunk.length === 0) {
2432
+ throw new StorageError(
2433
+ `Image ${JSON.stringify(id)} ended early while streaming (delivered ${delivered} of ${wanted} bytes).`
2434
+ );
2435
+ }
2436
+ yield chunk;
2437
+ offset += chunk.length;
2438
+ delivered += chunk.length;
2439
+ }
2440
+ }
2441
+ return generate();
2442
+ }
2443
+ async delete(id, { connection } = {}) {
2444
+ if (!isValidId(id)) return false;
2445
+ try {
2446
+ const row = await this.run(
2447
+ "delete",
2448
+ connection,
2449
+ async (q) => {
2450
+ const found = (await q.query(LOCK_ONE, [id]))[0][0];
2451
+ if (!found) return void 0;
2452
+ await q.query(DELETE_ONE2, [id]);
2453
+ return found;
2454
+ },
2455
+ { tx: true }
2456
+ );
2457
+ if (row?.storage_backend === "object_storage")
2458
+ await this.cleanupObject([row.object_storage_key], connection);
2459
+ return row !== void 0;
2460
+ } catch (exc) {
2461
+ throw wrap("Failed to delete image", exc);
2462
+ }
2463
+ }
2464
+ async deleteMany(ids, { connection } = {}) {
2465
+ const valid = [...new Set(ids.filter(isValidId))];
2466
+ if (valid.length === 0) return [];
2467
+ try {
2468
+ const rows = await this.run(
2469
+ "delete_many",
2470
+ connection,
2471
+ async (q) => {
2472
+ const found = (await q.query(LOCK_MANY, [valid]))[0];
2473
+ if (found.length === 0) return [];
2474
+ await q.query(DELETE_MANY2, [found.map((r) => r.id)]);
2475
+ return found;
2476
+ },
2477
+ { tx: true }
2478
+ );
2479
+ await this.cleanupObject(
2480
+ rows.filter((r) => r.storage_backend === "object_storage").map((r) => r.object_storage_key),
2481
+ connection
2482
+ );
2483
+ return rows.map((r) => String(r.id));
2484
+ } catch (exc) {
2485
+ throw wrap("Failed to delete image batch", exc);
2486
+ }
2487
+ }
2488
+ async exists(id, { connection } = {}) {
2489
+ if (!isValidId(id)) return false;
2490
+ try {
2491
+ return await this.run(
2492
+ "exists",
2493
+ connection,
2494
+ async (q) => (await q.query(EXISTS3, [id]))[0].length > 0
2495
+ );
2496
+ } catch (exc) {
2497
+ throw wrap("Failed to check image existence", exc);
2498
+ }
2499
+ }
2500
+ // ---- helpers ---------------------------------------------------------
2501
+ hydrate(row, id) {
2502
+ return hydrateRow(row, id, this.opts.objectStorage);
2503
+ }
2504
+ /**
2505
+ * Remove tiered objects AFTER their rows are gone: once the row is deleted the image is correctly
2506
+ * "not found" to everyone, so a failed object delete only leaves a harmless orphan, whereas deleting
2507
+ * the object first and then failing the row delete would leave a row pointing at nothing.
2508
+ *
2509
+ * Never runs inside a caller-owned transaction (`connection`): we cannot know whether it will
2510
+ * commit. In that case the object stays until you remove it yourself after committing. Failures are
2511
+ * reported through onOperation and never fail the delete itself.
2512
+ */
2513
+ async cleanupObject(keys, connection) {
2514
+ const os = this.opts.objectStorage;
2515
+ if (!os || connection || keys.length === 0) return;
2516
+ await Promise.all(
2517
+ keys.map(async (key) => {
2518
+ const start = performance.now();
2519
+ try {
2520
+ await os.delete(key);
2521
+ this.emit("object_storage_delete", start, true, null, 0);
2522
+ } catch (exc) {
2523
+ this.emit("object_storage_delete", start, false, msg(exc), 0);
2524
+ }
2525
+ })
2526
+ );
2527
+ }
2528
+ /**
2529
+ * Move an image's bytes out of the database into object storage, replacing the row's `data` with a
2530
+ * pointer. Returns null (no such image), false (already tiered: a safe no-op, so backfills can be
2531
+ * re-run) or true (tiered just now).
2532
+ *
2533
+ * SAFETY: the upload happens INSIDE the same transaction as the row lock (SELECT ... FOR UPDATE)
2534
+ * and the UPDATE that flips the row. If the upload fails the transaction rolls back and the row is
2535
+ * untouched: there is no moment when the bytes exist nowhere, or when a row claims to be tiered
2536
+ * without a completed upload.
2537
+ *
2538
+ * TRADEOFF: InnoDB row-locks only THIS row for the duration of the upload. Concurrent writes to this
2539
+ * image wait (up to innodb_lock_wait_timeout, 50s by default); every other row is unaffected and
2540
+ * plain reads are never blocked. Meant for explicit maintenance, not hot paths.
2541
+ *
2542
+ * Retry is safe: a transient error replays the whole closure, and the deterministic key (the image
2543
+ * id) makes the re-upload an idempotent overwrite.
2544
+ */
2545
+ async tierToObjectStorage(id, { connection } = {}) {
2546
+ const os = this.opts.objectStorage;
2547
+ if (!os) {
2548
+ throw new StorageError(
2549
+ "tierToObjectStorage() requires objectStorage to be configured (new ZeroBucket({ objectStorage: new ObjectStorage({ bucket }) }))."
2550
+ );
2551
+ }
2552
+ if (!isValidId(id)) return null;
2553
+ const key = id.toLowerCase();
2554
+ try {
2555
+ return await this.run(
2556
+ "tier_to_object_storage",
2557
+ connection,
2558
+ async (q) => {
2559
+ const row = (await q.query(SELECT_FOR_TIERING3, [id]))[0][0];
2560
+ if (!row) return null;
2561
+ if (row.storage_backend !== LOCAL) return false;
2562
+ await os.upload(key, row.data, { mimeType: row.mime_type });
2563
+ await q.query(UPDATE_AFTER_TIERING3, [os.bucket, key, mysqlNow(), id]);
2564
+ return true;
2565
+ },
2566
+ { tx: true }
2567
+ );
2568
+ } catch (exc) {
2569
+ throw wrap("Failed to tier image to object storage", exc);
2570
+ }
2571
+ }
2572
+ };
2573
+ function rowParams2(id, r, now) {
2574
+ return [
2575
+ id,
2576
+ r.data,
2577
+ r.mimeType,
2578
+ r.originalFilename,
2579
+ r.sizeBytes,
2580
+ r.width,
2581
+ r.height,
2582
+ r.checksumSha256,
2583
+ now,
2584
+ now
2585
+ ];
2586
+ }
2587
+
1883
2588
  // src/heic.ts
1884
2589
  import { createRequire } from "module";
1885
2590
  import { join } from "path";
@@ -2268,9 +2973,14 @@ var ZeroBucket = class _ZeroBucket {
2268
2973
  beforeGet;
2269
2974
  beforePut;
2270
2975
  constructor(options = {}) {
2271
- if (options.sqlite !== void 0 && (options.connectionString || options.pool)) {
2976
+ const chosen = [
2977
+ options.sqlite !== void 0 && "`sqlite`",
2978
+ (options.mysql !== void 0 || options.mysqlPool) && "`mysql`/`mysqlPool`",
2979
+ (options.connectionString || options.pool) && "`connectionString`/`pool`"
2980
+ ].filter(Boolean);
2981
+ if (chosen.length > 1) {
2272
2982
  throw new TypeError(
2273
- "Choose one database: `sqlite` OR `connectionString`/`pool`, not both."
2983
+ `Choose one database: ${chosen.join(" OR ")}, not several.`
2274
2984
  );
2275
2985
  }
2276
2986
  this.backend = options.backend ?? (options.sqlite !== void 0 ? new SQLiteBackend({
@@ -2279,6 +2989,18 @@ var ZeroBucket = class _ZeroBucket {
2279
2989
  dedup: options.dedup,
2280
2990
  objectStorage: options.objectStorage,
2281
2991
  onOperation: options.onOperation
2992
+ }) : options.mysql !== void 0 || options.mysqlPool ? new MySQLBackend({
2993
+ connectionString: options.mysql,
2994
+ pool: options.mysqlPool,
2995
+ autoMigrate: options.autoMigrate,
2996
+ maxRetries: options.maxRetries,
2997
+ retryBaseDelayMs: options.retryBaseDelayMs,
2998
+ poolMinSize: options.poolMinSize,
2999
+ poolMaxSize: options.poolMaxSize,
3000
+ poolTimeoutMs: options.poolTimeoutMs,
3001
+ onOperation: options.onOperation,
3002
+ objectStorage: options.objectStorage,
3003
+ dedup: options.dedup
2282
3004
  }) : new PostgresBackend({
2283
3005
  connectionString: options.connectionString,
2284
3006
  pool: options.pool,
@@ -2879,6 +3601,7 @@ export {
2879
3601
  ImageNotFoundError,
2880
3602
  ImageTooLargeError,
2881
3603
  ImageValidationError,
3604
+ MySQLBackend,
2882
3605
  ObjectStorage,
2883
3606
  PDFValidator,
2884
3607
  PostgresBackend,