@hasna/shortlinks 0.1.12 → 0.1.14

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.
@@ -32,6 +32,7 @@ export declare function writeWorkerFiles(options?: {
32
32
  outDir?: string;
33
33
  workerName?: string;
34
34
  origin?: string;
35
+ attachmentsOrigin?: string;
35
36
  }): {
36
37
  workerPath: string;
37
38
  wranglerPath: string;
@@ -56,17 +56,33 @@ function generateWorkerScript() {
56
56
  }
57
57
 
58
58
  const incoming = new URL(request.url);
59
- const upstream = new URL(incoming.pathname + incoming.search, origin);
60
- const headers = new Headers(request.headers);
61
- headers.set("x-forwarded-host", incoming.host);
62
- headers.set("x-shortlinks-worker", "cloudflare");
59
+ const proxyTo = (targetOrigin, marker) => {
60
+ const upstream = new URL(incoming.pathname + incoming.search, targetOrigin);
61
+ const headers = new Headers(request.headers);
62
+ headers.set("x-forwarded-host", incoming.host);
63
+ headers.set("x-shortlinks-worker", marker);
63
64
 
64
- return fetch(upstream.toString(), {
65
- method: request.method,
66
- headers,
67
- body: request.method === "GET" || request.method === "HEAD" ? undefined : request.body,
68
- redirect: "manual"
69
- });
65
+ return fetch(upstream.toString(), {
66
+ method: request.method,
67
+ headers,
68
+ body: request.method === "GET" || request.method === "HEAD" ? undefined : request.body,
69
+ redirect: "manual"
70
+ });
71
+ };
72
+
73
+ const reserved = (env.SHORTLINKS_RESERVED_PATH_PREFIXES || "a")
74
+ .split(",")
75
+ .map((value) => value.trim().toLowerCase())
76
+ .filter(Boolean);
77
+ const firstSegment = decodeURIComponent(incoming.pathname.replace(/^\\/+/, "").split("/")[0] || "").toLowerCase();
78
+ if (firstSegment && reserved.includes(firstSegment)) {
79
+ if (env.ATTACHMENTS_ORIGIN) {
80
+ return proxyTo(env.ATTACHMENTS_ORIGIN, "attachments");
81
+ }
82
+ return new Response("Reserved path prefix", { status: 404 });
83
+ }
84
+
85
+ return proxyTo(origin, "cloudflare");
70
86
  }
71
87
  };
72
88
  `;
@@ -84,6 +100,8 @@ compatibility_date = "2026-05-01"
84
100
 
85
101
  [vars]
86
102
  SHORTLINKS_ORIGIN = "${options.origin || "https://shortlinks.example.com"}"
103
+ ATTACHMENTS_ORIGIN = "${options.attachmentsOrigin || ""}"
104
+ SHORTLINKS_RESERVED_PATH_PREFIXES = "a"
87
105
  `);
88
106
  return { workerPath, wranglerPath };
89
107
  }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  export { ShortlinksDatabase, SQLITE_MIGRATIONS, makeId, now } from "./database.js";
2
2
  export { ShortlinksStore } from "./store.js";
3
3
  export { PgShortlinksStore } from "./pg-store.js";
4
+ export { CANONICAL_SHORTLINKS_RDS_CLUSTER, CANONICAL_SHORTLINKS_RDS_DATABASE, CANONICAL_SHORTLINKS_RDS_SECRET_PATH, SHORTLINKS_STORAGE_ENV, SHORTLINKS_STORAGE_FALLBACK_ENV, SHORTLINKS_STORAGE_MODE_ENV, SHORTLINKS_STORAGE_MODE_FALLBACK_ENV, STORAGE_DATABASE_ENV, STORAGE_MODE_ENV, getConnectionString, getCanonicalShortlinksRdsConfig, getStorageDatabaseEnv, getStorageDatabaseEnvName, getStorageConfig, getStorageConnectionString, getStorageDatabaseUrl, } from "./storage-config.js";
5
+ export type { CanonicalShortlinksRdsConfig, StorageConfig, StorageEnv, StorageMode } from "./storage-config.js";
6
+ export { PgAdapterAsync } from "./remote-storage.js";
7
+ export { applyPgMigrations } from "./pg-migrate.js";
8
+ export { SHORTLINKS_STORAGE_TABLES, STORAGE_TABLES, getStoragePg, getStorageStatus, parseStorageTables, pullStorageChanges, pushStorageChanges, runStorageMigrations, syncStorageChanges, } from "./storage-sync.js";
9
+ export type { StorageStatus, StorageSyncResult, SyncResult } from "./storage-sync.js";
4
10
  export { createShortlinksHandler, serveShortlinks } from "./server.js";
5
11
  export { createCloudflarePlan, generateWorkerScript, writeWorkerFiles, upsertCloudflareDnsRecord } from "./cloudflare.js";
6
12
  export { createLocalSetupPlan, registerMachinesDns } from "./local.js";