@n-dream/builders-sdk 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +35 -15
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -286,6 +286,9 @@ async function devFileLink(secret, g2) {
286
286
  q.set("s", await sign(secret, g2));
287
287
  return `${DEV_FILES_PATH}${g2.path.split("/").map(encodeURIComponent).join("/")}?${q}`;
288
288
  }
289
+ function validDevFilePath(path) {
290
+ return !!path && !path.startsWith("/") && !/[\\\u0000-\u001f\u007f]/.test(path) && path.split("/").every((p) => p !== "" && p !== "." && p !== "..");
291
+ }
289
292
 
290
293
  // src/db-limit.ts
291
294
  var MAX_QUERY_LIMIT = 500;
@@ -419,36 +422,50 @@ var dbFile = () => {
419
422
  const dir = process.env[DEV_DATA_DIR_ENV];
420
423
  return dir ? join2(dir, "db.json") : null;
421
424
  };
425
+ var TAG = "__builders";
426
+ function revive(_k, v) {
427
+ if (!v || typeof v !== "object" || Array.isArray(v)) return v;
428
+ const t = v;
429
+ if (Object.keys(t).length !== 2 || typeof t.value !== "string") return v;
430
+ if (t[TAG] === "date") return new Date(t.value);
431
+ if (t[TAG] === "bytes") return new Uint8Array(Buffer.from(t.value, "base64"));
432
+ return v;
433
+ }
422
434
  function load() {
423
435
  const file = dbFile();
424
436
  const s = /* @__PURE__ */ new Map();
425
437
  if (!file) return s;
426
438
  let raw;
427
439
  try {
428
- raw = JSON.parse(
429
- readFileSync2(file, "utf8"),
430
- (_k, v) => v && typeof v === "object" && typeof v.$date === "string" && Object.keys(v).length === 1 ? new Date(v.$date) : v
431
- );
440
+ raw = JSON.parse(readFileSync2(file, "utf8"), revive);
432
441
  } catch {
433
442
  return s;
434
443
  }
435
444
  for (const [name, docs] of Object.entries(raw)) s.set(name, new Map(Object.entries(docs)));
436
445
  return s;
437
446
  }
447
+ var warnedSave = false;
438
448
  function saveSoon() {
439
449
  const file = dbFile();
440
450
  if (!file || g.__buildersMemoryDbSave) return;
441
451
  g.__buildersMemoryDbSave = setTimeout(() => {
442
452
  g.__buildersMemoryDbSave = void 0;
443
- const out = {};
444
- for (const [name, docs] of store()) out[name] = Object.fromEntries(docs);
445
- const text = JSON.stringify(out, function(k, v) {
446
- const raw = this[k];
447
- return raw instanceof Date ? { $date: raw.toISOString() } : v;
448
- });
449
- mkdirSync(join2(file, ".."), { recursive: true });
450
- writeFileSync(`${file}.tmp`, text);
451
- renameSync(`${file}.tmp`, file);
453
+ try {
454
+ const out = {};
455
+ for (const [name, docs] of store()) out[name] = Object.fromEntries(docs);
456
+ const text = JSON.stringify(out, function(k, v) {
457
+ const raw = this[k];
458
+ if (raw instanceof Date) return { [TAG]: "date", value: Number.isNaN(raw.getTime()) ? "" : raw.toISOString() };
459
+ if (raw instanceof Uint8Array) return { [TAG]: "bytes", value: Buffer.from(raw).toString("base64") };
460
+ return typeof v === "bigint" ? v.toString() : v;
461
+ });
462
+ mkdirSync(join2(file, ".."), { recursive: true });
463
+ writeFileSync(`${file}.tmp`, text);
464
+ renameSync(`${file}.tmp`, file);
465
+ } catch (e) {
466
+ if (!warnedSave) console.warn(`[builders] The test database could not be saved to ${file}: ${e instanceof Error ? e.message : String(e)}`);
467
+ warnedSave = true;
468
+ }
452
469
  }, SAVE_DELAY_MS);
453
470
  }
454
471
  function resetMemoryDb() {
@@ -774,10 +791,13 @@ async function bucket() {
774
791
  }
775
792
  function local() {
776
793
  const dir = process.env.STORAGE_BUCKET ? void 0 : process.env[DEV_DATA_DIR_ENV];
777
- return dir ? localStorage(dir) : null;
794
+ if (!dir) return null;
795
+ const addOns = process.env.BUILDERS_DEV_ADDONS;
796
+ if (addOns !== void 0 && !addOns.split(",").includes("storage")) throw new AddOnNotEnabledError("storage");
797
+ return localStorage(dir);
778
798
  }
779
799
  function checkPath(path) {
780
- if (!path || path.startsWith("/") || path.split("/").some((p) => p === ".." || p === "")) {
800
+ if (!validDevFilePath(path)) {
781
801
  throw new BuildersError(`"${path}" is not a valid file path. Use something like "photos/cat.jpg".`, 400);
782
802
  }
783
803
  return path;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n-dream/builders-sdk",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Builders platform helpers for app code: signed-in user, database, AI, file storage, notifications.",
5
5
  "license": "UNLICENSED",
6
6
  "homepage": "https://builders.n-dream.com",