zerobucket 0.6.1 → 0.8.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.cjs CHANGED
@@ -39,6 +39,7 @@ __export(src_exports, {
39
39
  ImageNotFoundError: () => ImageNotFoundError,
40
40
  ImageTooLargeError: () => ImageTooLargeError,
41
41
  ImageValidationError: () => ImageValidationError,
42
+ MySQLBackend: () => MySQLBackend,
42
43
  ObjectStorage: () => ObjectStorage,
43
44
  PDFValidator: () => PDFValidator,
44
45
  PostgresBackend: () => PostgresBackend,
@@ -59,7 +60,7 @@ var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${_
59
60
  var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
60
61
 
61
62
  // src/client.ts
62
- var import_node_crypto2 = require("crypto");
63
+ var import_node_crypto3 = require("crypto");
63
64
  var import_promises = require("fs/promises");
64
65
  var import_node_path2 = require("path");
65
66
 
@@ -1984,239 +1985,1210 @@ var SQLiteBackend = class {
1984
1985
  }
1985
1986
  };
1986
1987
 
1987
- // src/heic.ts
1988
- var import_node_module = require("module");
1989
- var import_node_path = require("path");
1990
- var import_node_worker_threads = require("worker_threads");
1991
- var HEIC_BRANDS = /* @__PURE__ */ new Set([
1992
- "heic",
1993
- "heix",
1994
- "hevc",
1995
- "heim",
1996
- "heis",
1997
- "mif1",
1998
- "msf1"
1999
- ]);
2000
- function looksLikeHeic(data) {
2001
- if (data.length < 12) return false;
2002
- if (String.fromCharCode(data[4], data[5], data[6], data[7]) !== "ftyp")
2003
- return false;
2004
- return HEIC_BRANDS.has(
2005
- String.fromCharCode(data[8], data[9], data[10], data[11])
2006
- );
2007
- }
2008
- var HEIC_INSTALL_HINT = "npm install libheif-js";
2009
- var WORKER_SOURCE = `
2010
- const { parentPort, workerData } = require("node:worker_threads");
2011
- const libheif = require(workerData.libPath);
2012
- // libheif-js reports decode problems by dumping a raw error object to the console. Capture that text instead:
2013
- // it becomes part of the error we return (the useful diagnosis), and nothing is printed to the host's stderr.
2014
- let diag = "";
2015
- for (const level of ["log", "info", "warn", "error"]) {
2016
- console[level] = (...args) => {
2017
- diag = args.map((a) => (a && a.message) || (typeof a === "string" ? a : "")).filter(Boolean).join(" ").trim();
2018
- };
1988
+ // src/adapters/mysql.ts
1989
+ var import_node_crypto2 = require("crypto");
1990
+ var SCHEMA3 = `
1991
+ CREATE TABLE IF NOT EXISTS zerobucket_images (
1992
+ id CHAR(36) PRIMARY KEY,
1993
+ data LONGBLOB NOT NULL,
1994
+ mime_type VARCHAR(255) NOT NULL,
1995
+ original_filename TEXT,
1996
+ size_bytes INT NOT NULL,
1997
+ width INT,
1998
+ height INT,
1999
+ checksum_sha256 CHAR(64) NOT NULL,
2000
+ created_at DATETIME(6) NOT NULL,
2001
+ updated_at DATETIME(6) NOT NULL,
2002
+ INDEX idx_zerobucket_checksum (checksum_sha256),
2003
+ INDEX idx_zerobucket_created_at (created_at)
2004
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
2005
+ var TIERING_CHECK = "chk_zerobucket_storage";
2006
+ var MIGRATE_LOCK = "zerobucket_migrate";
2007
+ var COLS2 = `id, data, mime_type, original_filename, size_bytes, width, height, checksum_sha256, storage_backend, object_storage_bucket, object_storage_key`;
2008
+ var INSERT3 = `INSERT INTO zerobucket_images (id, data, mime_type, original_filename, size_bytes, width, height, checksum_sha256, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
2009
+ var SELECT_FULL3 = `SELECT ${COLS2} FROM zerobucket_images WHERE id = ?`;
2010
+ var SELECT_MANY2 = `SELECT ${COLS2} FROM zerobucket_images WHERE id IN (?)`;
2011
+ var SELECT_METADATA3 = `SELECT id, mime_type, original_filename, size_bytes, width, height, checksum_sha256 FROM zerobucket_images WHERE id = ?`;
2012
+ var STREAM_INFO2 = `SELECT size_bytes, storage_backend, object_storage_key FROM zerobucket_images WHERE id = ?`;
2013
+ var CHUNK2 = `SELECT SUBSTRING(data FROM ? FOR ?) AS chunk FROM zerobucket_images WHERE id = ?`;
2014
+ var EXISTS3 = `SELECT 1 FROM zerobucket_images WHERE id = ?`;
2015
+ var LOCK_ONE = `SELECT storage_backend, object_storage_key FROM zerobucket_images WHERE id = ? FOR UPDATE`;
2016
+ var DELETE_ONE2 = `DELETE FROM zerobucket_images WHERE id = ?`;
2017
+ var LOCK_MANY = `SELECT id, storage_backend, object_storage_key FROM zerobucket_images FORCE INDEX (PRIMARY) WHERE id IN (?) ORDER BY id FOR UPDATE`;
2018
+ var SELECT_FOR_TIERING3 = `SELECT data, mime_type, size_bytes, storage_backend FROM zerobucket_images WHERE id = ? FOR UPDATE`;
2019
+ var UPDATE_AFTER_TIERING3 = `UPDATE zerobucket_images SET data = NULL, storage_backend = 'object_storage', object_storage_bucket = ?, object_storage_key = ?, updated_at = ? WHERE id = ?`;
2020
+ var LOCAL = "mysql";
2021
+ var DEDUP_BLOBS_SCHEMA = `
2022
+ CREATE TABLE IF NOT EXISTS zerobucket_blobs (
2023
+ checksum_sha256 CHAR(64) PRIMARY KEY,
2024
+ data LONGBLOB NOT NULL,
2025
+ mime_type VARCHAR(255) NOT NULL,
2026
+ size_bytes INT NOT NULL,
2027
+ width INT,
2028
+ height INT,
2029
+ ref_count INT NOT NULL DEFAULT 0,
2030
+ created_at DATETIME(6) NOT NULL
2031
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
2032
+ var DEDUP_REFS_SCHEMA = `
2033
+ CREATE TABLE IF NOT EXISTS zerobucket_image_refs (
2034
+ id CHAR(36) PRIMARY KEY,
2035
+ checksum_sha256 CHAR(64) NOT NULL,
2036
+ original_filename TEXT,
2037
+ created_at DATETIME(6) NOT NULL,
2038
+ updated_at DATETIME(6) NOT NULL,
2039
+ INDEX idx_zerobucket_image_refs_checksum (checksum_sha256),
2040
+ INDEX idx_zerobucket_image_refs_created_at (created_at),
2041
+ FOREIGN KEY (checksum_sha256) REFERENCES zerobucket_blobs(checksum_sha256)
2042
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
2043
+ function upsertBlobSql(n) {
2044
+ if (!Number.isInteger(n) || n < 1) throw new RangeError("invalid ref count");
2045
+ return `INSERT INTO zerobucket_blobs (checksum_sha256, data, mime_type, size_bytes, width, height, ref_count, created_at) VALUES (?, ?, ?, ?, ?, ?, ${n}, ?) ON DUPLICATE KEY UPDATE ref_count = ref_count + ${n}`;
2019
2046
  }
2020
- const describe = (e) => {
2021
- const base = String((e && e.message) || e);
2022
- return diag && !base.includes(diag) ? base + ": " + diag : base;
2047
+ var D_INSERT_REF3 = `INSERT INTO zerobucket_image_refs (id, checksum_sha256, original_filename, created_at, updated_at) VALUES (?, ?, ?, ?, ?)`;
2048
+ var D_COLS3 = `r.id, b.data, b.mime_type, r.original_filename, b.size_bytes, b.width, b.height, r.checksum_sha256, '${LOCAL}' AS storage_backend, NULL AS object_storage_bucket, NULL AS object_storage_key`;
2049
+ var D_FROM3 = `FROM zerobucket_image_refs r JOIN zerobucket_blobs b ON r.checksum_sha256 = b.checksum_sha256`;
2050
+ var D_LOCK_REF = `SELECT checksum_sha256 FROM zerobucket_image_refs WHERE id = ? FOR UPDATE`;
2051
+ var D_DELETE_REF3 = `DELETE FROM zerobucket_image_refs WHERE id = ?`;
2052
+ var D_LOCK_REFS = `SELECT id, checksum_sha256 FROM zerobucket_image_refs FORCE INDEX (PRIMARY) WHERE id IN (?) ORDER BY id FOR UPDATE`;
2053
+ var D_DECREMENT3 = `UPDATE zerobucket_blobs SET ref_count = ref_count - ? WHERE checksum_sha256 = ?`;
2054
+ var D_DELETE_EMPTY3 = `DELETE FROM zerobucket_blobs WHERE checksum_sha256 = ? AND ref_count <= 0`;
2055
+ var CLASSIC_SQL2 = {
2056
+ selectFull: SELECT_FULL3,
2057
+ selectMany: SELECT_MANY2,
2058
+ selectMetadata: SELECT_METADATA3,
2059
+ streamInfo: STREAM_INFO2,
2060
+ chunk: CHUNK2,
2061
+ exists: EXISTS3
2023
2062
  };
2024
- function open(bytes) {
2025
- const images = new libheif.HeifDecoder().decode(bytes);
2026
- if (!images || images.length === 0) throw new Error("no images found in HEIF container");
2027
- return images[0];
2028
- }
2029
- parentPort.on("message", async ({ id, op, data }) => {
2030
- diag = "";
2031
- try {
2032
- const img = open(new Uint8Array(data));
2033
- const width = img.get_width();
2034
- const height = img.get_height();
2035
- if (op === "info") return parentPort.postMessage({ id, ok: true, width, height });
2036
- const out = await new Promise((resolve, reject) =>
2037
- img.display({ data: new Uint8ClampedArray(width * height * 4), width, height }, (d) =>
2038
- d ? resolve(d) : reject(new Error("HEIF processing error")),
2039
- ),
2040
- );
2041
- if (op === "validate") return parentPort.postMessage({ id, ok: true, width, height });
2042
- const rgba = out.data.buffer;
2043
- parentPort.postMessage({ id, ok: true, width, height, rgba }, [rgba]);
2044
- } catch (e) {
2045
- parentPort.postMessage({ id, ok: false, error: describe(e) });
2046
- }
2047
- });
2048
- `;
2049
- var HeicWorkerError = class extends StorageError {
2063
+ var DEDUP_SQL2 = {
2064
+ selectFull: `SELECT ${D_COLS3} ${D_FROM3} WHERE r.id = ?`,
2065
+ selectMany: `SELECT ${D_COLS3} ${D_FROM3} WHERE r.id IN (?)`,
2066
+ selectMetadata: `SELECT r.id, b.mime_type, r.original_filename, b.size_bytes, b.width, b.height, r.checksum_sha256 ${D_FROM3} WHERE r.id = ?`,
2067
+ streamInfo: `SELECT b.size_bytes, '${LOCAL}' AS storage_backend, NULL AS object_storage_key ${D_FROM3} WHERE r.id = ?`,
2068
+ chunk: `SELECT SUBSTRING(b.data FROM ? FOR ?) AS chunk ${D_FROM3} WHERE r.id = ?`,
2069
+ exists: `SELECT 1 FROM zerobucket_image_refs WHERE id = ?`
2050
2070
  };
2051
- var worker;
2052
- var nextId = 1;
2053
- var pending = /* @__PURE__ */ new Map();
2054
- var stats = { info: 0, validate: 0, decode: 0, workersStarted: 0 };
2055
- var resolveLib = () => {
2056
- const attempts = [
2057
- () => (0, import_node_module.createRequire)(importMetaUrl).resolve("libheif-js/wasm-bundle"),
2058
- // next to / above this package
2059
- () => (0, import_node_module.createRequire)((0, import_node_path.join)(process.cwd(), "noop.js")).resolve(
2060
- "libheif-js/wasm-bundle"
2061
- )
2062
- // the app's own node_modules
2063
- ];
2064
- let last;
2065
- for (const attempt of attempts) {
2066
- try {
2067
- return attempt();
2068
- } catch (e) {
2069
- last = e;
2070
- }
2071
- }
2072
- throw last;
2071
+ var RETRYABLE_ERRNOS = /* @__PURE__ */ new Set([
2072
+ 1205,
2073
+ // ER_LOCK_WAIT_TIMEOUT
2074
+ 1213,
2075
+ // ER_LOCK_DEADLOCK
2076
+ 1040,
2077
+ // ER_CON_COUNT_ERROR (too many connections)
2078
+ 1053,
2079
+ // ER_SERVER_SHUTDOWN
2080
+ 1927,
2081
+ // ER_CONNECTION_KILLED (MariaDB)
2082
+ 2002,
2083
+ // CR_CONNECTION_ERROR
2084
+ 2003,
2085
+ // CR_CONN_HOST_ERROR
2086
+ 2006,
2087
+ // CR_SERVER_GONE_ERROR
2088
+ 2013
2089
+ // CR_SERVER_LOST
2090
+ ]);
2091
+ var CONNECTION_CODES = /* @__PURE__ */ new Set([
2092
+ "PROTOCOL_CONNECTION_LOST",
2093
+ "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR",
2094
+ "PROTOCOL_ENQUEUE_AFTER_QUIT",
2095
+ "ECONNRESET",
2096
+ "ECONNREFUSED",
2097
+ "ETIMEDOUT",
2098
+ "EPIPE",
2099
+ "ECONNABORTED"
2100
+ ]);
2101
+ function errnoOf(exc) {
2102
+ const n = exc?.errno;
2103
+ return typeof n === "number" ? n : void 0;
2104
+ }
2105
+ var PoolTimeoutError = class extends StorageError {
2073
2106
  };
2074
- function heicAvailable() {
2075
- try {
2076
- resolveLib();
2077
- return true;
2078
- } catch {
2079
- return false;
2107
+ function isRetryable2(exc) {
2108
+ if (!(exc instanceof Error) || exc instanceof PoolTimeoutError) return false;
2109
+ const e = exc;
2110
+ const n = errnoOf(exc);
2111
+ if (n !== void 0 && RETRYABLE_ERRNOS.has(n)) return true;
2112
+ if (typeof e.code === "string") {
2113
+ if (CONNECTION_CODES.has(e.code)) return true;
2114
+ 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")
2115
+ return true;
2080
2116
  }
2117
+ return false;
2081
2118
  }
2082
- function releaseIfIdle(w) {
2083
- for (const p of pending.values()) if (p.owner === w) return;
2084
- w.unref();
2119
+ function isConnectionLevel2(exc) {
2120
+ if (!(exc instanceof Error)) return false;
2121
+ const e = exc;
2122
+ if (e.fatal === true) return true;
2123
+ if (typeof e.code === "string" && CONNECTION_CODES.has(e.code)) return true;
2124
+ const n = errnoOf(exc);
2125
+ return n === 2006 || n === 2013 || n === 1927 || n === 1053;
2085
2126
  }
2086
- function startWorker() {
2087
- let libPath;
2088
- try {
2089
- libPath = resolveLib();
2090
- } catch (cause) {
2091
- throw new StorageError(
2092
- `HEIC support requires the optional dependency 'libheif-js'. Install it with: ${HEIC_INSTALL_HINT}`,
2093
- { cause }
2127
+ var MAX_BACKOFF_MS2 = 2e3;
2128
+ function backoffDelayMs2(attempt, baseDelayMs) {
2129
+ const exponential = baseDelayMs * 2 ** (attempt - 1);
2130
+ const jitter = Math.random() * baseDelayMs;
2131
+ return Math.min(exponential + jitter, MAX_BACKOFF_MS2);
2132
+ }
2133
+ function friendly(prefix, exc) {
2134
+ if (errnoOf(exc) === 1153) {
2135
+ return new StorageError(
2136
+ `${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\`.`,
2137
+ { cause: exc }
2094
2138
  );
2095
2139
  }
2096
- const w = new import_node_worker_threads.Worker(WORKER_SOURCE, { eval: true, workerData: { libPath } });
2097
- stats.workersStarted += 1;
2098
- w.on(
2099
- "message",
2100
- (m) => {
2101
- const p = pending.get(m.id);
2102
- if (!p) return;
2103
- pending.delete(m.id);
2104
- releaseIfIdle(w);
2105
- if (m.ok) p.resolve(m);
2106
- else p.reject(new Error(m.error));
2107
- }
2108
- );
2109
- const fail = (err) => {
2110
- if (worker === w) worker = void 0;
2111
- for (const [id, p] of pending) {
2112
- if (p.owner !== w) continue;
2113
- pending.delete(id);
2114
- p.reject(err);
2115
- }
2116
- };
2117
- w.on(
2118
- "error",
2119
- (e) => fail(
2120
- new HeicWorkerError(
2121
- `HEIC decoder worker crashed: ${e instanceof Error ? e.message : String(e)}`,
2122
- { cause: e }
2123
- )
2124
- )
2125
- );
2126
- w.on(
2127
- "exit",
2128
- (code) => fail(
2129
- new HeicWorkerError(
2130
- `HEIC decoder worker exited unexpectedly (code ${code})`
2131
- )
2132
- )
2133
- );
2134
- w.unref();
2135
- return w;
2136
- }
2137
- function call(op, data) {
2138
- stats[op] += 1;
2139
- const w = worker ??= startWorker();
2140
- const id = nextId++;
2141
- const copy = new Uint8Array(data).buffer;
2142
- return new Promise((resolve, reject) => {
2143
- pending.set(id, { owner: w, resolve, reject });
2144
- w.ref();
2145
- w.postMessage({ id, op, data: copy }, [copy]);
2146
- });
2140
+ return wrap(prefix, exc);
2147
2141
  }
2148
- async function heicInfo(data) {
2149
- const r = await call("info", data);
2150
- return { width: r.width, height: r.height };
2142
+ function mysqlNow() {
2143
+ return `${(/* @__PURE__ */ new Date()).toISOString().replace("T", " ").replace("Z", "")}000`;
2151
2144
  }
2152
- async function heicValidate(data) {
2153
- const r = await call("validate", data);
2154
- return { width: r.width, height: r.height };
2145
+ function normalizeUrl(url) {
2146
+ return url.replace(/^mariadb:\/\//i, "mysql://");
2155
2147
  }
2156
- async function decodeHeic(data) {
2157
- const r = await call("decode", data);
2158
- return {
2159
- width: r.width,
2160
- height: r.height,
2161
- rgba: Buffer.from(r.rgba)
2162
- };
2148
+ function isMysqlConnection(c) {
2149
+ const m = c;
2150
+ return typeof m.query === "function" && typeof m.execute === "function";
2163
2151
  }
2164
-
2165
- // src/validation.ts
2166
- var FORMAT_TO_MIME = {
2167
- jpeg: "image/jpeg",
2168
- png: "image/png",
2169
- webp: "image/webp",
2170
- heic: "image/heic"
2171
- // optional: needs `npm install libheif-js` (see heic.ts)
2172
- };
2173
- var SUPPORTED_FORMATS = Object.keys(FORMAT_TO_MIME);
2174
- var DEFAULT_MAX_PIXELS = 89e6;
2175
- var sharpPromise;
2176
- function loadSharp() {
2177
- sharpPromise ??= import("sharp").then(
2178
- // ESM gives { default: sharp }; some CJS interop gives sharp directly.
2179
- (m) => m.default ?? m,
2180
- (cause) => {
2181
- sharpPromise = void 0;
2182
- throw new StorageError(
2183
- "Image validation requires the optional dependency 'sharp'. Install it with: npm install sharp",
2184
- { cause }
2152
+ var MySQLBackend = class {
2153
+ constructor(opts) {
2154
+ this.opts = opts;
2155
+ if (!opts.connectionString && !opts.pool) {
2156
+ throw new TypeError(
2157
+ "MySQLBackend requires either `connectionString` or `pool`"
2185
2158
  );
2186
2159
  }
2187
- );
2188
- return sharpPromise;
2189
- }
2190
- async function validateImage(data, opts) {
2191
- const {
2192
- maxBytes,
2193
- maxPixels = DEFAULT_MAX_PIXELS,
2194
- allowedFormats = SUPPORTED_FORMATS
2195
- } = opts;
2196
- const sizeBytes = data.length;
2197
- if (sizeBytes > maxBytes) throw new ImageTooLargeError(sizeBytes, maxBytes);
2198
- if (sizeBytes === 0) throw new CorruptedImageError("Image data is empty");
2199
- if (looksLikeHeic(data))
2200
- return validateHeic(data, { maxBytes, maxPixels, allowedFormats });
2201
- const sharp = await loadSharp();
2202
- const make = () => sharp(data, { limitInputPixels: maxPixels, failOn: "error" });
2203
- let format;
2204
- let width;
2205
- let height;
2206
- try {
2207
- const meta = await make().metadata();
2208
- format = meta.format;
2209
- width = meta.width;
2210
- height = meta.height;
2211
- } catch (exc) {
2212
- if (isPixelLimit(exc)) throw new ImageTooLargeError(sizeBytes, maxBytes);
2213
- throw new CorruptedImageError(
2214
- `Could not decode image: ${errMessage(exc)}`,
2215
- { cause: exc }
2216
- );
2160
+ this.dedup = opts.dedup ?? false;
2161
+ if (this.dedup && opts.objectStorage) {
2162
+ throw new TypeError(
2163
+ "objectStorage is not supported together with dedup: true (same restriction as the Python package). Tiering moves one image's bytes, but a deduplicated blob can be shared by many images."
2164
+ );
2165
+ }
2166
+ this.sql = this.dedup ? DEDUP_SQL2 : CLASSIC_SQL2;
2167
+ this.ownsPool = !opts.pool;
2168
+ this.pool = opts.pool;
2169
+ this.maxRetries = opts.maxRetries ?? 3;
2170
+ this.retryBaseDelayMs = opts.retryBaseDelayMs ?? 100;
2171
+ this.poolTimeoutMs = opts.poolTimeoutMs ?? 1e4;
2172
+ this.autoMigrate = opts.autoMigrate ?? true;
2217
2173
  }
2218
- if (!format || !allowedFormats.includes(format) || !(format in FORMAT_TO_MIME)) {
2219
- throw new UnsupportedFormatError(format ?? null, allowedFormats);
2174
+ opts;
2175
+ pool;
2176
+ ownsPool;
2177
+ readyPromise;
2178
+ closed = false;
2179
+ maxRetries;
2180
+ retryBaseDelayMs;
2181
+ poolTimeoutMs;
2182
+ autoMigrate;
2183
+ dedup;
2184
+ sql;
2185
+ // ---- lifecycle -------------------------------------------------------
2186
+ ready() {
2187
+ if (this.closed)
2188
+ return Promise.reject(
2189
+ new StorageError("This ZeroBucket instance has been closed")
2190
+ );
2191
+ this.readyPromise ??= this.init().catch((exc) => {
2192
+ this.readyPromise = void 0;
2193
+ throw exc;
2194
+ });
2195
+ return this.readyPromise;
2196
+ }
2197
+ async init() {
2198
+ if (!this.pool) {
2199
+ let mod;
2200
+ try {
2201
+ mod = await import("mysql2/promise");
2202
+ } catch (cause) {
2203
+ throw new StorageError(
2204
+ "MySQLBackend requires the 'mysql2' package. Install it with: npm install mysql2",
2205
+ { cause }
2206
+ );
2207
+ }
2208
+ const createPool = (mod.default ?? mod).createPool;
2209
+ this.pool = createPool({
2210
+ uri: normalizeUrl(this.opts.connectionString),
2211
+ connectionLimit: this.opts.poolMaxSize ?? 5,
2212
+ waitForConnections: true,
2213
+ queueLimit: 0,
2214
+ connectTimeout: this.poolTimeoutMs,
2215
+ charset: "utf8mb4",
2216
+ timezone: "Z",
2217
+ enableKeepAlive: true
2218
+ });
2219
+ }
2220
+ try {
2221
+ if (this.autoMigrate) {
2222
+ await this.runPooled("migrate", (q) => this.migrate(q), false);
2223
+ } else {
2224
+ await this.runPooled("ping", (q) => q.query("SELECT 1"), false);
2225
+ }
2226
+ } catch (exc) {
2227
+ if (this.ownsPool) {
2228
+ await this.pool.end().catch(() => {
2229
+ });
2230
+ this.pool = void 0;
2231
+ }
2232
+ throw exc instanceof StorageError ? exc : new StorageError(`Could not connect to MySQL/MariaDB: ${msg(exc)}`, {
2233
+ cause: exc
2234
+ });
2235
+ }
2236
+ }
2237
+ /**
2238
+ * Create the schema if missing, under a server-side named lock so concurrent first callers (in this
2239
+ * or any other process) run the DDL one at a time. Every step checks before it changes anything, so
2240
+ * re-running is a no-op.
2241
+ */
2242
+ async migrate(q) {
2243
+ const locked = (await q.query("SELECT GET_LOCK(?, 30) AS got", [MIGRATE_LOCK]))[0][0]?.got;
2244
+ if (locked !== 1) {
2245
+ throw new StorageError(
2246
+ "Timed out waiting for another process to finish migrating the ZeroBucket schema."
2247
+ );
2248
+ }
2249
+ try {
2250
+ if (this.dedup) {
2251
+ await this.migrateDedup(q);
2252
+ return;
2253
+ }
2254
+ const tableExists = (await q.query(
2255
+ "SELECT 1 FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'zerobucket_images'"
2256
+ ))[0].length > 0;
2257
+ if (!tableExists) await q.query(SCHEMA3);
2258
+ const cols = new Map(
2259
+ (await q.query(
2260
+ "SELECT COLUMN_NAME AS c, IS_NULLABLE AS nullable FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'zerobucket_images'"
2261
+ ))[0].map((r) => [r.c, r.nullable])
2262
+ );
2263
+ if (!cols.has("storage_backend"))
2264
+ await q.query(
2265
+ `ALTER TABLE zerobucket_images ADD COLUMN storage_backend VARCHAR(20) NOT NULL DEFAULT '${LOCAL}'`
2266
+ );
2267
+ if (!cols.has("object_storage_bucket"))
2268
+ await q.query(
2269
+ "ALTER TABLE zerobucket_images ADD COLUMN object_storage_bucket VARCHAR(255)"
2270
+ );
2271
+ if (!cols.has("object_storage_key"))
2272
+ await q.query(
2273
+ "ALTER TABLE zerobucket_images ADD COLUMN object_storage_key VARCHAR(512)"
2274
+ );
2275
+ if (cols.get("data") === "NO")
2276
+ await q.query(
2277
+ "ALTER TABLE zerobucket_images MODIFY data LONGBLOB NULL"
2278
+ );
2279
+ const hasCheck = (await q.query(
2280
+ "SELECT 1 FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'zerobucket_images' AND CONSTRAINT_NAME = ?",
2281
+ [TIERING_CHECK]
2282
+ ))[0].length > 0;
2283
+ if (!hasCheck) {
2284
+ await q.query(
2285
+ `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))`
2286
+ );
2287
+ }
2288
+ } catch (exc) {
2289
+ throw new StorageError(`Migration failed: ${msg(exc)}`, { cause: exc });
2290
+ } finally {
2291
+ await q.query("SELECT RELEASE_LOCK(?)", [MIGRATE_LOCK]).catch(() => {
2292
+ });
2293
+ }
2294
+ }
2295
+ /** Dedup tables, created only if missing (same no-needless-DDL rule as the classic table). */
2296
+ async migrateDedup(q) {
2297
+ const have = new Set(
2298
+ (await q.query(
2299
+ "SELECT TABLE_NAME AS t FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME IN ('zerobucket_blobs', 'zerobucket_image_refs')"
2300
+ ))[0].map((r) => r.t)
2301
+ );
2302
+ if (!have.has("zerobucket_blobs")) await q.query(DEDUP_BLOBS_SCHEMA);
2303
+ if (!have.has("zerobucket_image_refs")) await q.query(DEDUP_REFS_SCHEMA);
2304
+ }
2305
+ async close() {
2306
+ this.closed = true;
2307
+ const pool = this.pool;
2308
+ this.pool = void 0;
2309
+ this.readyPromise = void 0;
2310
+ if (pool && this.ownsPool) await pool.end();
2311
+ }
2312
+ // ---- execution core --------------------------------------------------
2313
+ emit(operation, start, success, error, retryCount) {
2314
+ const cb = this.opts.onOperation;
2315
+ if (!cb) return;
2316
+ try {
2317
+ cb({
2318
+ operation,
2319
+ durationMs: performance.now() - start,
2320
+ success,
2321
+ error,
2322
+ retryCount
2323
+ });
2324
+ } catch {
2325
+ }
2326
+ }
2327
+ /** Wait for a pool slot, but never longer than poolTimeoutMs. */
2328
+ acquire() {
2329
+ const pool = this.pool;
2330
+ if (this.closed || !pool)
2331
+ return Promise.reject(
2332
+ new StorageError("This ZeroBucket instance has been closed")
2333
+ );
2334
+ return new Promise((resolve, reject) => {
2335
+ let settled = false;
2336
+ const timer = setTimeout(() => {
2337
+ settled = true;
2338
+ reject(
2339
+ new PoolTimeoutError(
2340
+ `Timed out after ${this.poolTimeoutMs}ms waiting for a MySQL/MariaDB connection (poolMaxSize=${this.opts.poolMaxSize ?? 5}; all connections are in use).`
2341
+ )
2342
+ );
2343
+ }, this.poolTimeoutMs);
2344
+ pool.getConnection().then(
2345
+ (conn) => {
2346
+ if (settled) {
2347
+ conn.release();
2348
+ return;
2349
+ }
2350
+ settled = true;
2351
+ clearTimeout(timer);
2352
+ resolve(conn);
2353
+ },
2354
+ (exc) => {
2355
+ if (settled) return;
2356
+ settled = true;
2357
+ clearTimeout(timer);
2358
+ reject(exc);
2359
+ }
2360
+ );
2361
+ });
2362
+ }
2363
+ /**
2364
+ * Run `work` on a pooled connection. `tx` wraps it in a transaction. Transient failures retry the
2365
+ * WHOLE closure on a fresh connection after a full rollback.
2366
+ */
2367
+ async runPooled(operation, work, tx) {
2368
+ const start = performance.now();
2369
+ let attempt = 0;
2370
+ for (; ; ) {
2371
+ let conn;
2372
+ let destroy = false;
2373
+ try {
2374
+ conn = await this.acquire();
2375
+ if (tx) await conn.beginTransaction();
2376
+ const result = await work(conn);
2377
+ if (tx) await conn.commit();
2378
+ this.emit(operation, start, true, null, attempt);
2379
+ return result;
2380
+ } catch (exc) {
2381
+ if (conn && tx) {
2382
+ try {
2383
+ await conn.rollback();
2384
+ } catch {
2385
+ destroy = true;
2386
+ }
2387
+ }
2388
+ if (isConnectionLevel2(exc)) destroy = true;
2389
+ attempt += 1;
2390
+ if (attempt > this.maxRetries || !isRetryable2(exc)) {
2391
+ this.emit(operation, start, false, msg(exc), attempt - 1);
2392
+ throw exc;
2393
+ }
2394
+ } finally {
2395
+ if (conn) {
2396
+ if (destroy) conn.destroy();
2397
+ else conn.release();
2398
+ }
2399
+ }
2400
+ await sleep(backoffDelayMs2(attempt, this.retryBaseDelayMs));
2401
+ }
2402
+ }
2403
+ /**
2404
+ * Run `work` and return its result, emitting exactly one OperationEvent.
2405
+ *
2406
+ * connection given -> run once on it; no retry, and no commit/rollback of the caller's transaction.
2407
+ * connection absent -> pooled, retried (see runPooled).
2408
+ */
2409
+ async run(operation, connection, work, { tx = false } = {}) {
2410
+ if (connection) {
2411
+ if (!isMysqlConnection(connection)) {
2412
+ throw new TypeError(
2413
+ "MySQLBackend expects a mysql2/promise connection as `connection` (an object with query() and execute())."
2414
+ );
2415
+ }
2416
+ const start = performance.now();
2417
+ try {
2418
+ const result = tx ? await this.atomic(connection, work) : await work(connection);
2419
+ this.emit(operation, start, true, null, 0);
2420
+ return result;
2421
+ } catch (exc) {
2422
+ this.emit(operation, start, false, msg(exc), 0);
2423
+ throw exc;
2424
+ }
2425
+ }
2426
+ await this.ready();
2427
+ return this.runPooled(operation, work, tx);
2428
+ }
2429
+ /**
2430
+ * Make multi-statement work all-or-nothing on a CALLER-supplied connection.
2431
+ *
2432
+ * Is a transaction open? SAVEPOINT followed by ROLLBACK TO SAVEPOINT succeeds only inside one (in
2433
+ * autocommit mode the savepoint is gone before the next statement and the rollback fails with 1305).
2434
+ * - Open -> reuse that SAVEPOINT: a failure undoes only our work; success joins the caller's transaction.
2435
+ * - None -> START TRANSACTION and COMMIT our own, so a failure can never leave half-applied state.
2436
+ */
2437
+ async atomic(conn, work) {
2438
+ let inTransaction;
2439
+ await conn.query("SAVEPOINT zerobucket_atomic");
2440
+ try {
2441
+ await conn.query("ROLLBACK TO SAVEPOINT zerobucket_atomic");
2442
+ inTransaction = true;
2443
+ } catch (exc) {
2444
+ if (errnoOf(exc) !== 1305) throw exc;
2445
+ inTransaction = false;
2446
+ await conn.query("START TRANSACTION");
2447
+ }
2448
+ try {
2449
+ const result = await work(conn);
2450
+ await conn.query(
2451
+ inTransaction ? "RELEASE SAVEPOINT zerobucket_atomic" : "COMMIT"
2452
+ );
2453
+ return result;
2454
+ } catch (exc) {
2455
+ try {
2456
+ await conn.query(
2457
+ inTransaction ? "ROLLBACK TO SAVEPOINT zerobucket_atomic" : "ROLLBACK"
2458
+ );
2459
+ if (inTransaction)
2460
+ await conn.query("RELEASE SAVEPOINT zerobucket_atomic");
2461
+ } catch {
2462
+ }
2463
+ throw exc;
2464
+ }
2465
+ }
2466
+ // ---- StorageBackend --------------------------------------------------
2467
+ async put(row, { connection } = {}) {
2468
+ try {
2469
+ const id = (0, import_node_crypto2.randomUUID)();
2470
+ if (this.dedup) {
2471
+ await this.run(
2472
+ "put",
2473
+ connection,
2474
+ async (q) => {
2475
+ const now = mysqlNow();
2476
+ await q.execute(upsertBlobSql(1), blobParams2(row, now));
2477
+ await q.execute(D_INSERT_REF3, refParams(id, row, now));
2478
+ },
2479
+ { tx: true }
2480
+ );
2481
+ return id;
2482
+ }
2483
+ await this.run("put", connection, async (q) => {
2484
+ await q.execute(INSERT3, rowParams2(id, row, mysqlNow()));
2485
+ });
2486
+ return id;
2487
+ } catch (exc) {
2488
+ throw friendly("Failed to store image", exc);
2489
+ }
2490
+ }
2491
+ async putMany(rows, { connection } = {}) {
2492
+ if (rows.length === 0) return [];
2493
+ try {
2494
+ const ids = rows.map(() => (0, import_node_crypto2.randomUUID)());
2495
+ await this.run(
2496
+ "put_many",
2497
+ connection,
2498
+ async (q) => {
2499
+ const now = mysqlNow();
2500
+ if (this.dedup) {
2501
+ const groups = /* @__PURE__ */ new Map();
2502
+ for (const r of rows) {
2503
+ const g = groups.get(r.checksumSha256);
2504
+ if (g) g.n += 1;
2505
+ else groups.set(r.checksumSha256, { row: r, n: 1 });
2506
+ }
2507
+ for (const checksum of [...groups.keys()].sort()) {
2508
+ const g = groups.get(checksum);
2509
+ await q.execute(upsertBlobSql(g.n), blobParams2(g.row, now));
2510
+ }
2511
+ for (let i = 0; i < rows.length; i++)
2512
+ await q.execute(D_INSERT_REF3, refParams(ids[i], rows[i], now));
2513
+ return;
2514
+ }
2515
+ for (let i = 0; i < rows.length; i++)
2516
+ await q.execute(INSERT3, rowParams2(ids[i], rows[i], now));
2517
+ },
2518
+ { tx: true }
2519
+ );
2520
+ return ids;
2521
+ } catch (exc) {
2522
+ throw friendly("Failed to store image batch", exc);
2523
+ }
2524
+ }
2525
+ async get(id, { connection } = {}) {
2526
+ if (!isValidId(id)) return null;
2527
+ let row;
2528
+ try {
2529
+ row = await this.run(
2530
+ "get",
2531
+ connection,
2532
+ async (q) => (await q.query(this.sql.selectFull, [id]))[0][0]
2533
+ );
2534
+ } catch (exc) {
2535
+ throw wrap("Failed to retrieve image", exc);
2536
+ }
2537
+ return row ? this.hydrate(row, id) : null;
2538
+ }
2539
+ async getMany(ids, { connection } = {}) {
2540
+ const valid = ids.filter(isValidId);
2541
+ if (valid.length === 0) return [];
2542
+ let rows;
2543
+ try {
2544
+ rows = await this.run(
2545
+ "get_many",
2546
+ connection,
2547
+ async (q) => (await q.query(this.sql.selectMany, [valid]))[0]
2548
+ );
2549
+ } catch (exc) {
2550
+ throw wrap("Failed to retrieve image batch", exc);
2551
+ }
2552
+ return mapLimit(rows, 4, (r) => this.hydrate(r, String(r.id)));
2553
+ }
2554
+ async getMetadata(id, { connection } = {}) {
2555
+ if (!isValidId(id)) return null;
2556
+ let row;
2557
+ try {
2558
+ row = await this.run(
2559
+ "get_metadata",
2560
+ connection,
2561
+ async (q) => (await q.query(this.sql.selectMetadata, [id]))[0][0]
2562
+ );
2563
+ } catch (exc) {
2564
+ throw wrap("Failed to retrieve image metadata", exc);
2565
+ }
2566
+ if (!row) return null;
2567
+ return {
2568
+ id: String(row.id),
2569
+ mimeType: row.mime_type,
2570
+ originalFilename: row.original_filename,
2571
+ sizeBytes: row.size_bytes,
2572
+ width: row.width,
2573
+ height: row.height,
2574
+ checksumSha256: row.checksum_sha256
2575
+ };
2576
+ }
2577
+ async getStream(id, opts) {
2578
+ if (!isValidId(id)) return null;
2579
+ const { connection, chunkSize } = opts;
2580
+ if (!Number.isInteger(chunkSize) || chunkSize <= 0)
2581
+ throw new RangeError("chunkSize must be a positive integer");
2582
+ let info;
2583
+ try {
2584
+ info = await this.run(
2585
+ "get_metadata",
2586
+ connection,
2587
+ async (q) => (await q.query(this.sql.streamInfo, [id]))[0][0]
2588
+ );
2589
+ } catch (exc) {
2590
+ throw wrap("Failed to retrieve image metadata", exc);
2591
+ }
2592
+ if (!info) return null;
2593
+ const totalSize = info.size_bytes;
2594
+ const start = opts.range?.start ?? 0;
2595
+ const end = Math.min(opts.range?.end ?? totalSize - 1, totalSize - 1);
2596
+ if (info.storage_backend === "object_storage") {
2597
+ const os = this.opts.objectStorage;
2598
+ if (!os) throw tieredWithoutStorage(id, info.object_storage_key);
2599
+ return streamFromObjectStorage(
2600
+ os,
2601
+ id,
2602
+ info.object_storage_key,
2603
+ start,
2604
+ end,
2605
+ chunkSize
2606
+ );
2607
+ }
2608
+ const self = this;
2609
+ async function* generate() {
2610
+ let offset = start;
2611
+ let delivered = 0;
2612
+ const wanted = Math.max(0, end - start + 1);
2613
+ while (delivered < wanted) {
2614
+ const length = Math.min(chunkSize, wanted - delivered);
2615
+ let row;
2616
+ try {
2617
+ row = await self.run(
2618
+ "get_stream",
2619
+ connection,
2620
+ async (q) => (await q.query(self.sql.chunk, [offset + 1, length, id]))[0][0]
2621
+ );
2622
+ } catch (exc) {
2623
+ throw wrap("Failed to stream image", exc);
2624
+ }
2625
+ if (!row) {
2626
+ throw new StorageError(
2627
+ `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.`
2628
+ );
2629
+ }
2630
+ const chunk = row.chunk;
2631
+ if (!chunk || chunk.length === 0) {
2632
+ throw new StorageError(
2633
+ `Image ${JSON.stringify(id)} ended early while streaming (delivered ${delivered} of ${wanted} bytes).`
2634
+ );
2635
+ }
2636
+ yield chunk;
2637
+ offset += chunk.length;
2638
+ delivered += chunk.length;
2639
+ }
2640
+ }
2641
+ return generate();
2642
+ }
2643
+ async delete(id, { connection } = {}) {
2644
+ if (!isValidId(id)) return false;
2645
+ if (this.dedup) return this.deleteDedup(id, connection);
2646
+ try {
2647
+ const row = await this.run(
2648
+ "delete",
2649
+ connection,
2650
+ async (q) => {
2651
+ const found = (await q.query(LOCK_ONE, [id]))[0][0];
2652
+ if (!found) return void 0;
2653
+ await q.query(DELETE_ONE2, [id]);
2654
+ return found;
2655
+ },
2656
+ { tx: true }
2657
+ );
2658
+ if (row?.storage_backend === "object_storage")
2659
+ await this.cleanupObject([row.object_storage_key], connection);
2660
+ return row !== void 0;
2661
+ } catch (exc) {
2662
+ throw wrap("Failed to delete image", exc);
2663
+ }
2664
+ }
2665
+ async deleteMany(ids, { connection } = {}) {
2666
+ const valid = [...new Set(ids.filter(isValidId))];
2667
+ if (valid.length === 0) return [];
2668
+ if (this.dedup) return this.deleteManyDedup(valid, connection);
2669
+ try {
2670
+ const rows = await this.run(
2671
+ "delete_many",
2672
+ connection,
2673
+ async (q) => {
2674
+ const found = (await q.query(LOCK_MANY, [valid]))[0];
2675
+ for (const r of found) await q.query(DELETE_ONE2, [r.id]);
2676
+ return found;
2677
+ },
2678
+ { tx: true }
2679
+ );
2680
+ await this.cleanupObject(
2681
+ rows.filter((r) => r.storage_backend === "object_storage").map((r) => r.object_storage_key),
2682
+ connection
2683
+ );
2684
+ return rows.map((r) => String(r.id));
2685
+ } catch (exc) {
2686
+ throw wrap("Failed to delete image batch", exc);
2687
+ }
2688
+ }
2689
+ async exists(id, { connection } = {}) {
2690
+ if (!isValidId(id)) return false;
2691
+ try {
2692
+ return await this.run(
2693
+ "exists",
2694
+ connection,
2695
+ async (q) => (await q.query(this.sql.exists, [id]))[0].length > 0
2696
+ );
2697
+ } catch (exc) {
2698
+ throw wrap("Failed to check image existence", exc);
2699
+ }
2700
+ }
2701
+ // ---- maintenance (CLI) -----------------------------------------------
2702
+ /** Storage statistics, or null if the tables do not exist yet (nothing is created). */
2703
+ async getInfo() {
2704
+ try {
2705
+ return await this.run("info", void 0, async (q) => {
2706
+ const need = this.dedup ? ["zerobucket_image_refs", "zerobucket_blobs"] : ["zerobucket_images"];
2707
+ const present = new Set(
2708
+ (await q.query(
2709
+ "SELECT TABLE_NAME AS t FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME IN (?)",
2710
+ [need]
2711
+ ))[0].map((r) => r.t)
2712
+ );
2713
+ if (!need.every((t) => present.has(t))) return null;
2714
+ const j = this.dedup ? "FROM zerobucket_image_refs r JOIN zerobucket_blobs b ON r.checksum_sha256 = b.checksum_sha256" : "FROM zerobucket_images";
2715
+ const size = this.dedup ? "b.size_bytes" : "size_bytes";
2716
+ const created = this.dedup ? "r.created_at" : "created_at";
2717
+ const mime = this.dedup ? "b.mime_type" : "mime_type";
2718
+ const tot = (await q.query(
2719
+ `SELECT COUNT(*) AS n, COALESCE(SUM(${size}), 0) AS bytes, CAST(MIN(${created}) AS CHAR) AS oldest, CAST(MAX(${created}) AS CHAR) AS newest ${j}`
2720
+ ))[0][0];
2721
+ const disk = (await q.query(
2722
+ "SELECT COALESCE(SUM(DATA_LENGTH + INDEX_LENGTH), 0) AS s FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME IN (?)",
2723
+ [need]
2724
+ ))[0][0];
2725
+ const fmt = (await q.query(
2726
+ `SELECT ${mime} AS m, COUNT(*) AS n, COALESCE(SUM(${size}), 0) AS bytes ${j} GROUP BY ${mime} ORDER BY COUNT(*) DESC, ${mime}`
2727
+ ))[0];
2728
+ const utc = (v) => v ? `${v}+00:00` : null;
2729
+ const info = {
2730
+ mode: this.dedup ? "dedup" : "classic",
2731
+ count: Number(tot.n),
2732
+ totalBytes: Number(tot.bytes),
2733
+ oldest: utc(tot.oldest),
2734
+ newest: utc(tot.newest),
2735
+ onDisk: prettySize(Number(disk.s)),
2736
+ byFormat: fmt.map((r) => ({
2737
+ mimeType: r.m,
2738
+ count: Number(r.n),
2739
+ bytes: Number(r.bytes)
2740
+ }))
2741
+ };
2742
+ if (this.dedup) {
2743
+ const b = (await q.query(
2744
+ "SELECT COUNT(*) AS n, COALESCE(SUM(size_bytes), 0) AS bytes FROM zerobucket_blobs"
2745
+ ))[0][0];
2746
+ info.dedup = { blobs: Number(b.n), storedBytes: Number(b.bytes) };
2747
+ } else {
2748
+ const t = (await q.query(
2749
+ "SELECT COUNT(*) AS n, COALESCE(SUM(size_bytes), 0) AS bytes FROM zerobucket_images WHERE storage_backend = 'object_storage'"
2750
+ ))[0][0];
2751
+ info.tiered = { count: Number(t.n), bytes: Number(t.bytes) };
2752
+ }
2753
+ return info;
2754
+ });
2755
+ } catch (exc) {
2756
+ throw wrap("Failed to read storage info", exc);
2757
+ }
2758
+ }
2759
+ /** Ids matching the filters: oldest first, or a random sample. Used by the CLI's verify and tier. */
2760
+ async listImages(o = {}) {
2761
+ const where = [];
2762
+ const params = [];
2763
+ const created = this.dedup ? "r.created_at" : "created_at";
2764
+ if (o.minSize !== void 0) {
2765
+ where.push(`${this.dedup ? "b.size_bytes" : "size_bytes"} >= ?`);
2766
+ params.push(o.minSize);
2767
+ }
2768
+ if (o.olderThanDays !== void 0) {
2769
+ where.push(`${created} <= UTC_TIMESTAMP(6) - INTERVAL ? DAY`);
2770
+ params.push(o.olderThanDays);
2771
+ }
2772
+ if (o.onlyUntiered && !this.dedup)
2773
+ where.push(`storage_backend = '${LOCAL}'`);
2774
+ const from = this.dedup ? "FROM zerobucket_image_refs r JOIN zerobucket_blobs b ON r.checksum_sha256 = b.checksum_sha256" : "FROM zerobucket_images";
2775
+ const idCol = this.dedup ? "r.id" : "id";
2776
+ const tierCol = this.dedup ? "0 AS tiered" : "(storage_backend = 'object_storage') AS tiered";
2777
+ const order = o.sample !== void 0 ? "ORDER BY RAND()" : `ORDER BY ${created}, ${idCol}`;
2778
+ const lim = o.sample ?? o.limit;
2779
+ let sql = `SELECT ${idCol} AS id, ${tierCol} ${from}${where.length ? " WHERE " + where.join(" AND ") : ""} ${order}`;
2780
+ if (lim !== void 0) {
2781
+ if (!Number.isInteger(lim) || lim < 0)
2782
+ throw new RangeError("limit/sample must be a non-negative integer");
2783
+ sql += ` LIMIT ${lim}`;
2784
+ }
2785
+ try {
2786
+ const rows = await this.run(
2787
+ "list_images",
2788
+ void 0,
2789
+ async (q) => (await q.query(sql, params))[0]
2790
+ );
2791
+ return rows.map((r) => ({
2792
+ id: String(r.id),
2793
+ tiered: Boolean(Number(r.tiered))
2794
+ }));
2795
+ } catch (exc) {
2796
+ throw wrap("Failed to list images", exc);
2797
+ }
2798
+ }
2799
+ // ---- dedup deletes ---------------------------------------------------
2800
+ /**
2801
+ * Lock the ref row, delete it, then decrement its blob (the UPDATE takes the blob row lock, so two
2802
+ * deletes of different refs to one blob serialize instead of racing) and remove the blob once nothing
2803
+ * references it. All in one transaction: a failure can never leave a ref without its count or the reverse.
2804
+ */
2805
+ async deleteDedup(id, connection) {
2806
+ try {
2807
+ return await this.run(
2808
+ "delete",
2809
+ connection,
2810
+ async (q) => {
2811
+ const ref = (await q.query(D_LOCK_REF, [id]))[0][0];
2812
+ if (!ref) return false;
2813
+ const checksum = ref.checksum_sha256;
2814
+ await q.query(D_DELETE_REF3, [id]);
2815
+ await q.query(D_DECREMENT3, [1, checksum]);
2816
+ await q.query(D_DELETE_EMPTY3, [checksum]);
2817
+ return true;
2818
+ },
2819
+ { tx: true }
2820
+ );
2821
+ } catch (exc) {
2822
+ throw wrap("Failed to delete image", exc);
2823
+ }
2824
+ }
2825
+ async deleteManyDedup(ids, connection) {
2826
+ try {
2827
+ return await this.run(
2828
+ "delete_many",
2829
+ connection,
2830
+ async (q) => {
2831
+ const found = (await q.query(D_LOCK_REFS, [ids]))[0];
2832
+ if (found.length === 0) return [];
2833
+ for (const r of found) await q.query(D_DELETE_REF3, [r.id]);
2834
+ const counts = /* @__PURE__ */ new Map();
2835
+ for (const r of found)
2836
+ counts.set(
2837
+ r.checksum_sha256,
2838
+ (counts.get(r.checksum_sha256) ?? 0) + 1
2839
+ );
2840
+ for (const checksum of [...counts.keys()].sort()) {
2841
+ await q.query(D_DECREMENT3, [counts.get(checksum), checksum]);
2842
+ await q.query(D_DELETE_EMPTY3, [checksum]);
2843
+ }
2844
+ return found.map((r) => String(r.id));
2845
+ },
2846
+ { tx: true }
2847
+ );
2848
+ } catch (exc) {
2849
+ throw wrap("Failed to delete image batch", exc);
2850
+ }
2851
+ }
2852
+ // ---- helpers ---------------------------------------------------------
2853
+ hydrate(row, id) {
2854
+ return hydrateRow(row, id, this.opts.objectStorage);
2855
+ }
2856
+ /**
2857
+ * Remove tiered objects AFTER their rows are gone: once the row is deleted the image is correctly
2858
+ * "not found" to everyone, so a failed object delete only leaves a harmless orphan, whereas deleting
2859
+ * the object first and then failing the row delete would leave a row pointing at nothing.
2860
+ *
2861
+ * Never runs inside a caller-owned transaction (`connection`): we cannot know whether it will
2862
+ * commit. In that case the object stays until you remove it yourself after committing. Failures are
2863
+ * reported through onOperation and never fail the delete itself.
2864
+ */
2865
+ async cleanupObject(keys, connection) {
2866
+ const os = this.opts.objectStorage;
2867
+ if (!os || connection || keys.length === 0) return;
2868
+ await Promise.all(
2869
+ keys.map(async (key) => {
2870
+ const start = performance.now();
2871
+ try {
2872
+ await os.delete(key);
2873
+ this.emit("object_storage_delete", start, true, null, 0);
2874
+ } catch (exc) {
2875
+ this.emit("object_storage_delete", start, false, msg(exc), 0);
2876
+ }
2877
+ })
2878
+ );
2879
+ }
2880
+ /**
2881
+ * Move an image's bytes out of the database into object storage, replacing the row's `data` with a
2882
+ * pointer. Returns null (no such image), false (already tiered: a safe no-op, so backfills can be
2883
+ * re-run) or true (tiered just now).
2884
+ *
2885
+ * SAFETY: the upload happens INSIDE the same transaction as the row lock (SELECT ... FOR UPDATE)
2886
+ * and the UPDATE that flips the row. If the upload fails the transaction rolls back and the row is
2887
+ * untouched: there is no moment when the bytes exist nowhere, or when a row claims to be tiered
2888
+ * without a completed upload.
2889
+ *
2890
+ * TRADEOFF: InnoDB row-locks only THIS row for the duration of the upload. Concurrent writes to this
2891
+ * image wait (up to innodb_lock_wait_timeout, 50s by default); every other row is unaffected and
2892
+ * plain reads are never blocked. Meant for explicit maintenance, not hot paths.
2893
+ *
2894
+ * Retry is safe: a transient error replays the whole closure, and the deterministic key (the image
2895
+ * id) makes the re-upload an idempotent overwrite.
2896
+ */
2897
+ async tierToObjectStorage(id, { connection } = {}) {
2898
+ if (this.dedup) {
2899
+ throw new StorageError(
2900
+ "tierToObjectStorage() is not supported in dedup mode (a blob may be shared by many images)."
2901
+ );
2902
+ }
2903
+ const os = this.opts.objectStorage;
2904
+ if (!os) {
2905
+ throw new StorageError(
2906
+ "tierToObjectStorage() requires objectStorage to be configured (new ZeroBucket({ objectStorage: new ObjectStorage({ bucket }) }))."
2907
+ );
2908
+ }
2909
+ if (!isValidId(id)) return null;
2910
+ const key = id.toLowerCase();
2911
+ try {
2912
+ return await this.run(
2913
+ "tier_to_object_storage",
2914
+ connection,
2915
+ async (q) => {
2916
+ const row = (await q.query(SELECT_FOR_TIERING3, [id]))[0][0];
2917
+ if (!row) return null;
2918
+ if (row.storage_backend !== LOCAL) return false;
2919
+ await os.upload(key, row.data, { mimeType: row.mime_type });
2920
+ await q.query(UPDATE_AFTER_TIERING3, [os.bucket, key, mysqlNow(), id]);
2921
+ return true;
2922
+ },
2923
+ { tx: true }
2924
+ );
2925
+ } catch (exc) {
2926
+ throw wrap("Failed to tier image to object storage", exc);
2927
+ }
2928
+ }
2929
+ };
2930
+ function blobParams2(r, now) {
2931
+ return [
2932
+ r.checksumSha256,
2933
+ r.data,
2934
+ r.mimeType,
2935
+ r.sizeBytes,
2936
+ r.width,
2937
+ r.height,
2938
+ now
2939
+ ];
2940
+ }
2941
+ function refParams(id, r, now) {
2942
+ return [id, r.checksumSha256, r.originalFilename, now, now];
2943
+ }
2944
+ function rowParams2(id, r, now) {
2945
+ return [
2946
+ id,
2947
+ r.data,
2948
+ r.mimeType,
2949
+ r.originalFilename,
2950
+ r.sizeBytes,
2951
+ r.width,
2952
+ r.height,
2953
+ r.checksumSha256,
2954
+ now,
2955
+ now
2956
+ ];
2957
+ }
2958
+
2959
+ // src/heic.ts
2960
+ var import_node_module = require("module");
2961
+ var import_node_path = require("path");
2962
+ var import_node_worker_threads = require("worker_threads");
2963
+ var HEIC_BRANDS = /* @__PURE__ */ new Set([
2964
+ "heic",
2965
+ "heix",
2966
+ "hevc",
2967
+ "heim",
2968
+ "heis",
2969
+ "mif1",
2970
+ "msf1"
2971
+ ]);
2972
+ function looksLikeHeic(data) {
2973
+ if (data.length < 12) return false;
2974
+ if (String.fromCharCode(data[4], data[5], data[6], data[7]) !== "ftyp")
2975
+ return false;
2976
+ return HEIC_BRANDS.has(
2977
+ String.fromCharCode(data[8], data[9], data[10], data[11])
2978
+ );
2979
+ }
2980
+ var HEIC_INSTALL_HINT = "npm install libheif-js";
2981
+ var WORKER_SOURCE = `
2982
+ const { parentPort, workerData } = require("node:worker_threads");
2983
+ const libheif = require(workerData.libPath);
2984
+ // libheif-js reports decode problems by dumping a raw error object to the console. Capture that text instead:
2985
+ // it becomes part of the error we return (the useful diagnosis), and nothing is printed to the host's stderr.
2986
+ let diag = "";
2987
+ for (const level of ["log", "info", "warn", "error"]) {
2988
+ console[level] = (...args) => {
2989
+ diag = args.map((a) => (a && a.message) || (typeof a === "string" ? a : "")).filter(Boolean).join(" ").trim();
2990
+ };
2991
+ }
2992
+ const describe = (e) => {
2993
+ const base = String((e && e.message) || e);
2994
+ return diag && !base.includes(diag) ? base + ": " + diag : base;
2995
+ };
2996
+ function open(bytes) {
2997
+ const images = new libheif.HeifDecoder().decode(bytes);
2998
+ if (!images || images.length === 0) throw new Error("no images found in HEIF container");
2999
+ return images[0];
3000
+ }
3001
+ parentPort.on("message", async ({ id, op, data }) => {
3002
+ diag = "";
3003
+ try {
3004
+ const img = open(new Uint8Array(data));
3005
+ const width = img.get_width();
3006
+ const height = img.get_height();
3007
+ if (op === "info") return parentPort.postMessage({ id, ok: true, width, height });
3008
+ const out = await new Promise((resolve, reject) =>
3009
+ img.display({ data: new Uint8ClampedArray(width * height * 4), width, height }, (d) =>
3010
+ d ? resolve(d) : reject(new Error("HEIF processing error")),
3011
+ ),
3012
+ );
3013
+ if (op === "validate") return parentPort.postMessage({ id, ok: true, width, height });
3014
+ const rgba = out.data.buffer;
3015
+ parentPort.postMessage({ id, ok: true, width, height, rgba }, [rgba]);
3016
+ } catch (e) {
3017
+ parentPort.postMessage({ id, ok: false, error: describe(e) });
3018
+ }
3019
+ });
3020
+ `;
3021
+ var HeicWorkerError = class extends StorageError {
3022
+ };
3023
+ var worker;
3024
+ var nextId = 1;
3025
+ var pending = /* @__PURE__ */ new Map();
3026
+ var stats = { info: 0, validate: 0, decode: 0, workersStarted: 0 };
3027
+ var resolveLib = () => {
3028
+ const attempts = [
3029
+ () => (0, import_node_module.createRequire)(importMetaUrl).resolve("libheif-js/wasm-bundle"),
3030
+ // next to / above this package
3031
+ () => (0, import_node_module.createRequire)((0, import_node_path.join)(process.cwd(), "noop.js")).resolve(
3032
+ "libheif-js/wasm-bundle"
3033
+ )
3034
+ // the app's own node_modules
3035
+ ];
3036
+ let last;
3037
+ for (const attempt of attempts) {
3038
+ try {
3039
+ return attempt();
3040
+ } catch (e) {
3041
+ last = e;
3042
+ }
3043
+ }
3044
+ throw last;
3045
+ };
3046
+ function heicAvailable() {
3047
+ try {
3048
+ resolveLib();
3049
+ return true;
3050
+ } catch {
3051
+ return false;
3052
+ }
3053
+ }
3054
+ function releaseIfIdle(w) {
3055
+ for (const p of pending.values()) if (p.owner === w) return;
3056
+ w.unref();
3057
+ }
3058
+ function startWorker() {
3059
+ let libPath;
3060
+ try {
3061
+ libPath = resolveLib();
3062
+ } catch (cause) {
3063
+ throw new StorageError(
3064
+ `HEIC support requires the optional dependency 'libheif-js'. Install it with: ${HEIC_INSTALL_HINT}`,
3065
+ { cause }
3066
+ );
3067
+ }
3068
+ const w = new import_node_worker_threads.Worker(WORKER_SOURCE, { eval: true, workerData: { libPath } });
3069
+ stats.workersStarted += 1;
3070
+ w.on(
3071
+ "message",
3072
+ (m) => {
3073
+ const p = pending.get(m.id);
3074
+ if (!p) return;
3075
+ pending.delete(m.id);
3076
+ releaseIfIdle(w);
3077
+ if (m.ok) p.resolve(m);
3078
+ else p.reject(new Error(m.error));
3079
+ }
3080
+ );
3081
+ const fail = (err) => {
3082
+ if (worker === w) worker = void 0;
3083
+ for (const [id, p] of pending) {
3084
+ if (p.owner !== w) continue;
3085
+ pending.delete(id);
3086
+ p.reject(err);
3087
+ }
3088
+ };
3089
+ w.on(
3090
+ "error",
3091
+ (e) => fail(
3092
+ new HeicWorkerError(
3093
+ `HEIC decoder worker crashed: ${e instanceof Error ? e.message : String(e)}`,
3094
+ { cause: e }
3095
+ )
3096
+ )
3097
+ );
3098
+ w.on(
3099
+ "exit",
3100
+ (code) => fail(
3101
+ new HeicWorkerError(
3102
+ `HEIC decoder worker exited unexpectedly (code ${code})`
3103
+ )
3104
+ )
3105
+ );
3106
+ w.unref();
3107
+ return w;
3108
+ }
3109
+ function call(op, data) {
3110
+ stats[op] += 1;
3111
+ const w = worker ??= startWorker();
3112
+ const id = nextId++;
3113
+ const copy = new Uint8Array(data).buffer;
3114
+ return new Promise((resolve, reject) => {
3115
+ pending.set(id, { owner: w, resolve, reject });
3116
+ w.ref();
3117
+ w.postMessage({ id, op, data: copy }, [copy]);
3118
+ });
3119
+ }
3120
+ async function heicInfo(data) {
3121
+ const r = await call("info", data);
3122
+ return { width: r.width, height: r.height };
3123
+ }
3124
+ async function heicValidate(data) {
3125
+ const r = await call("validate", data);
3126
+ return { width: r.width, height: r.height };
3127
+ }
3128
+ async function decodeHeic(data) {
3129
+ const r = await call("decode", data);
3130
+ return {
3131
+ width: r.width,
3132
+ height: r.height,
3133
+ rgba: Buffer.from(r.rgba)
3134
+ };
3135
+ }
3136
+
3137
+ // src/validation.ts
3138
+ var FORMAT_TO_MIME = {
3139
+ jpeg: "image/jpeg",
3140
+ png: "image/png",
3141
+ webp: "image/webp",
3142
+ heic: "image/heic"
3143
+ // optional: needs `npm install libheif-js` (see heic.ts)
3144
+ };
3145
+ var SUPPORTED_FORMATS = Object.keys(FORMAT_TO_MIME);
3146
+ var DEFAULT_MAX_PIXELS = 89e6;
3147
+ var sharpPromise;
3148
+ function loadSharp() {
3149
+ sharpPromise ??= import("sharp").then(
3150
+ // ESM gives { default: sharp }; some CJS interop gives sharp directly.
3151
+ (m) => m.default ?? m,
3152
+ (cause) => {
3153
+ sharpPromise = void 0;
3154
+ throw new StorageError(
3155
+ "Image validation requires the optional dependency 'sharp'. Install it with: npm install sharp",
3156
+ { cause }
3157
+ );
3158
+ }
3159
+ );
3160
+ return sharpPromise;
3161
+ }
3162
+ async function validateImage(data, opts) {
3163
+ const {
3164
+ maxBytes,
3165
+ maxPixels = DEFAULT_MAX_PIXELS,
3166
+ allowedFormats = SUPPORTED_FORMATS
3167
+ } = opts;
3168
+ const sizeBytes = data.length;
3169
+ if (sizeBytes > maxBytes) throw new ImageTooLargeError(sizeBytes, maxBytes);
3170
+ if (sizeBytes === 0) throw new CorruptedImageError("Image data is empty");
3171
+ if (looksLikeHeic(data))
3172
+ return validateHeic(data, { maxBytes, maxPixels, allowedFormats });
3173
+ const sharp = await loadSharp();
3174
+ const make = () => sharp(data, { limitInputPixels: maxPixels, failOn: "error" });
3175
+ let format;
3176
+ let width;
3177
+ let height;
3178
+ try {
3179
+ const meta = await make().metadata();
3180
+ format = meta.format;
3181
+ width = meta.width;
3182
+ height = meta.height;
3183
+ } catch (exc) {
3184
+ if (isPixelLimit(exc)) throw new ImageTooLargeError(sizeBytes, maxBytes);
3185
+ throw new CorruptedImageError(
3186
+ `Could not decode image: ${errMessage(exc)}`,
3187
+ { cause: exc }
3188
+ );
3189
+ }
3190
+ if (!format || !allowedFormats.includes(format) || !(format in FORMAT_TO_MIME)) {
3191
+ throw new UnsupportedFormatError(format ?? null, allowedFormats);
2220
3192
  }
2221
3193
  if (!width || !height)
2222
3194
  throw new CorruptedImageError("Image has no readable dimensions");
@@ -2372,9 +3344,14 @@ var ZeroBucket = class _ZeroBucket {
2372
3344
  beforeGet;
2373
3345
  beforePut;
2374
3346
  constructor(options = {}) {
2375
- if (options.sqlite !== void 0 && (options.connectionString || options.pool)) {
3347
+ const chosen = [
3348
+ options.sqlite !== void 0 && "`sqlite`",
3349
+ (options.mysql !== void 0 || options.mysqlPool) && "`mysql`/`mysqlPool`",
3350
+ (options.connectionString || options.pool) && "`connectionString`/`pool`"
3351
+ ].filter(Boolean);
3352
+ if (chosen.length > 1) {
2376
3353
  throw new TypeError(
2377
- "Choose one database: `sqlite` OR `connectionString`/`pool`, not both."
3354
+ `Choose one database: ${chosen.join(" OR ")}, not several.`
2378
3355
  );
2379
3356
  }
2380
3357
  this.backend = options.backend ?? (options.sqlite !== void 0 ? new SQLiteBackend({
@@ -2383,6 +3360,18 @@ var ZeroBucket = class _ZeroBucket {
2383
3360
  dedup: options.dedup,
2384
3361
  objectStorage: options.objectStorage,
2385
3362
  onOperation: options.onOperation
3363
+ }) : options.mysql !== void 0 || options.mysqlPool ? new MySQLBackend({
3364
+ connectionString: options.mysql,
3365
+ pool: options.mysqlPool,
3366
+ autoMigrate: options.autoMigrate,
3367
+ maxRetries: options.maxRetries,
3368
+ retryBaseDelayMs: options.retryBaseDelayMs,
3369
+ poolMinSize: options.poolMinSize,
3370
+ poolMaxSize: options.poolMaxSize,
3371
+ poolTimeoutMs: options.poolTimeoutMs,
3372
+ onOperation: options.onOperation,
3373
+ objectStorage: options.objectStorage,
3374
+ dedup: options.dedup
2386
3375
  }) : new PostgresBackend({
2387
3376
  connectionString: options.connectionString,
2388
3377
  pool: options.pool,
@@ -2479,7 +3468,7 @@ var ZeroBucket = class _ZeroBucket {
2479
3468
  sizeBytes,
2480
3469
  width,
2481
3470
  height,
2482
- checksumSha256: (0, import_node_crypto2.createHash)("sha256").update(finalData).digest("hex")
3471
+ checksumSha256: (0, import_node_crypto3.createHash)("sha256").update(finalData).digest("hex")
2483
3472
  };
2484
3473
  }
2485
3474
  /** Validate, optionally optimize, checksum and store one image. Returns its id (a UUID). */
@@ -2984,6 +3973,7 @@ var PDFValidator = class {
2984
3973
  ImageNotFoundError,
2985
3974
  ImageTooLargeError,
2986
3975
  ImageValidationError,
3976
+ MySQLBackend,
2987
3977
  ObjectStorage,
2988
3978
  PDFValidator,
2989
3979
  PostgresBackend,