@palbase/backend 18.0.0 → 20.0.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.
@@ -1330,7 +1330,7 @@ function renderPath(template, tokens) {
1330
1330
  return template.replaceAll("{userId}", sanitizeSegment(tokens.userId ?? "anonymous")).replaceAll("{uploadId}", sanitizeSegment(tokens.uploadId)).replaceAll("{filename}", sanitizeSegment(tokens.filename ?? "file"));
1331
1331
  }
1332
1332
  function sanitizeSegment(raw) {
1333
- const cleaned = raw.replace(/[-]/g, "").replace(/[/\\]/g, "-").replace(/\.{2,}/g, ".").replace(/^\.+/, "").trim();
1333
+ const cleaned = raw.replace(/[\x00-\x1F\x7F]/g, "").replace(/[/\\]/g, "-").replace(/\.{2,}/g, ".").replace(/^\.+/, "").trim();
1334
1334
  return cleaned === "" ? "file" : cleaned.slice(0, 200);
1335
1335
  }
1336
1336
  function grantFor(entry, ctx, bucketLimits) {
@@ -1428,6 +1428,39 @@ async function createApp(opts) {
1428
1428
  const requestALS = opts.runtimeHooks?.__requestALS ?? __requestALS;
1429
1429
  const uploadSecret = config.uploadSecret ?? "";
1430
1430
  const completions = new CompletionLedger();
1431
+ function buildServices(db) {
1432
+ return {
1433
+ Database: db.client,
1434
+ Cache: cache,
1435
+ Log: log,
1436
+ Documents: modules.Documents ?? stubModule("Documents"),
1437
+ Storage: modules.Storage ?? stubModule("Storage"),
1438
+ Notifications: modules.Notifications ?? stubModule("Notifications"),
1439
+ Flags: modules.Flags ?? stubModule("Flags"),
1440
+ Realtime: modules.Realtime ?? stubModule("Realtime"),
1441
+ Purchases: modules.Purchases ?? stubModule("Purchases"),
1442
+ // Named, never undefined. A backend started without a secrets client
1443
+ // that returned `undefined` here would fail inside the handler as
1444
+ // "Cannot read properties of undefined", which says nothing about what
1445
+ // to configure — the stub says the name and the variable.
1446
+ Secrets: modules.Secrets ?? stubModule("Secrets")
1447
+ };
1448
+ }
1449
+ async function runInServiceScope(fn) {
1450
+ const db = createRequestDatabase(sql, {
1451
+ role: config.dbRole,
1452
+ serviceRole: config.dbServiceRole,
1453
+ claimsJson: "{}"
1454
+ });
1455
+ try {
1456
+ const out = await runWithRuntime(buildServices(db), fn);
1457
+ await db.commit();
1458
+ return out;
1459
+ } catch (err) {
1460
+ await db.rollback(err);
1461
+ throw err;
1462
+ }
1463
+ }
1431
1464
  async function handleAuthorize(req, requestId) {
1432
1465
  if (uploadSecret === "" || !verifySignature(req.headers.get(SIGNATURE_HEADER) ?? "", uploadSecret)) {
1433
1466
  return envelope("unauthorized", "This endpoint is not callable directly", 401, requestId);
@@ -1594,22 +1627,7 @@ async function createApp(opts) {
1594
1627
  claimsJson: JSON.stringify(claims ?? {})
1595
1628
  });
1596
1629
  try {
1597
- const services = {
1598
- Database: db.client,
1599
- Cache: cache,
1600
- Log: log,
1601
- Documents: modules.Documents ?? stubModule("Documents"),
1602
- Storage: modules.Storage ?? stubModule("Storage"),
1603
- Notifications: modules.Notifications ?? stubModule("Notifications"),
1604
- Flags: modules.Flags ?? stubModule("Flags"),
1605
- Realtime: modules.Realtime ?? stubModule("Realtime"),
1606
- Purchases: modules.Purchases ?? stubModule("Purchases"),
1607
- // Named, never undefined. A backend started without a secrets client
1608
- // that returned `undefined` here would fail inside the handler as
1609
- // "Cannot read properties of undefined", which says nothing about what
1610
- // to configure — the stub says the name and the variable.
1611
- Secrets: modules.Secrets ?? stubModule("Secrets")
1612
- };
1630
+ const services = buildServices(db);
1613
1631
  const result = await runWithRuntime(services, () => {
1614
1632
  const box = requestALS.getStore();
1615
1633
  if (box) {
@@ -1672,6 +1690,7 @@ async function createApp(opts) {
1672
1690
  handle,
1673
1691
  routes,
1674
1692
  config,
1693
+ runInServiceScope,
1675
1694
  async shutdown() {
1676
1695
  const closable = sql;
1677
1696
  await closable.close?.();