dineway 0.1.3

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 (96) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +89 -0
  3. package/dist/adapters-BlzWJG82.d.mts +106 -0
  4. package/dist/apply-CAPvMfoU.mjs +1339 -0
  5. package/dist/astro/index.d.mts +50 -0
  6. package/dist/astro/index.mjs +1326 -0
  7. package/dist/astro/middleware/auth.d.mts +30 -0
  8. package/dist/astro/middleware/auth.mjs +708 -0
  9. package/dist/astro/middleware/redirect.d.mts +21 -0
  10. package/dist/astro/middleware/redirect.mjs +62 -0
  11. package/dist/astro/middleware/request-context.d.mts +17 -0
  12. package/dist/astro/middleware/request-context.mjs +1371 -0
  13. package/dist/astro/middleware/setup.d.mts +19 -0
  14. package/dist/astro/middleware/setup.mjs +46 -0
  15. package/dist/astro/middleware.d.mts +12 -0
  16. package/dist/astro/middleware.mjs +1716 -0
  17. package/dist/astro/types.d.mts +269 -0
  18. package/dist/astro/types.mjs +1 -0
  19. package/dist/base64-F8-DUraK.mjs +58 -0
  20. package/dist/byline-DeWCMU_i.mjs +234 -0
  21. package/dist/bylines-DyqBV9EQ.mjs +137 -0
  22. package/dist/chunk-ClPoSABd.mjs +21 -0
  23. package/dist/cli/index.d.mts +1 -0
  24. package/dist/cli/index.mjs +3987 -0
  25. package/dist/client/external-auth-headers.d.mts +38 -0
  26. package/dist/client/external-auth-headers.mjs +101 -0
  27. package/dist/client/index.d.mts +397 -0
  28. package/dist/client/index.mjs +345 -0
  29. package/dist/config-Cq8H0SfX.mjs +46 -0
  30. package/dist/connection-C9pxzuag.mjs +52 -0
  31. package/dist/content-zSgdNmnt.mjs +836 -0
  32. package/dist/db/index.d.mts +4 -0
  33. package/dist/db/index.mjs +62 -0
  34. package/dist/db/libsql.d.mts +10 -0
  35. package/dist/db/libsql.mjs +21 -0
  36. package/dist/db/postgres.d.mts +10 -0
  37. package/dist/db/postgres.mjs +29 -0
  38. package/dist/db/sqlite.d.mts +10 -0
  39. package/dist/db/sqlite.mjs +15 -0
  40. package/dist/default-WYlzADZL.mjs +80 -0
  41. package/dist/dialect-helpers-B9uSp2GJ.mjs +89 -0
  42. package/dist/error-DrxtnGPg.mjs +26 -0
  43. package/dist/index-C-jx21qs.d.mts +4771 -0
  44. package/dist/index.d.mts +16 -0
  45. package/dist/index.mjs +30 -0
  46. package/dist/load-C6FCD1FU.mjs +27 -0
  47. package/dist/loader-qKmo0wAY.mjs +446 -0
  48. package/dist/manifest-schema-CTSEyIJ3.mjs +186 -0
  49. package/dist/media/index.d.mts +25 -0
  50. package/dist/media/index.mjs +54 -0
  51. package/dist/media/local-runtime.d.mts +38 -0
  52. package/dist/media/local-runtime.mjs +132 -0
  53. package/dist/media-DMTr80Gv.mjs +199 -0
  54. package/dist/mode-BlyYtIFO.mjs +22 -0
  55. package/dist/page/index.d.mts +148 -0
  56. package/dist/page/index.mjs +419 -0
  57. package/dist/placeholder-B3knXwNc.mjs +267 -0
  58. package/dist/placeholder-bOx1xCTY.d.mts +283 -0
  59. package/dist/plugin-utils.d.mts +57 -0
  60. package/dist/plugin-utils.mjs +77 -0
  61. package/dist/plugins/adapt-sandbox-entry.d.mts +21 -0
  62. package/dist/plugins/adapt-sandbox-entry.mjs +112 -0
  63. package/dist/query-BiaPl_g2.mjs +459 -0
  64. package/dist/redirect-JPqLAbxa.mjs +328 -0
  65. package/dist/registry-DSd1GWB8.mjs +851 -0
  66. package/dist/request-context.d.mts +49 -0
  67. package/dist/request-context.mjs +42 -0
  68. package/dist/runner-B5l1JfOj.d.mts +26 -0
  69. package/dist/runner-BGUGywgG.mjs +1529 -0
  70. package/dist/runtime.d.mts +25 -0
  71. package/dist/runtime.mjs +41 -0
  72. package/dist/search-BNruJHDL.mjs +11054 -0
  73. package/dist/seed/index.d.mts +3 -0
  74. package/dist/seed/index.mjs +15 -0
  75. package/dist/seo/index.d.mts +69 -0
  76. package/dist/seo/index.mjs +69 -0
  77. package/dist/storage/local.d.mts +38 -0
  78. package/dist/storage/local.mjs +165 -0
  79. package/dist/storage/s3.d.mts +31 -0
  80. package/dist/storage/s3.mjs +174 -0
  81. package/dist/tokens-4vgYuXsZ.mjs +170 -0
  82. package/dist/transport-C5FYnid7.mjs +417 -0
  83. package/dist/transport-gIL-e43D.d.mts +41 -0
  84. package/dist/types-BawVha09.mjs +30 -0
  85. package/dist/types-BgQeVaPj.d.mts +192 -0
  86. package/dist/types-CLLdsG3g.d.mts +103 -0
  87. package/dist/types-D38djUXv.d.mts +1196 -0
  88. package/dist/types-DShnjzb6.mjs +15 -0
  89. package/dist/types-DkvMXalq.d.mts +425 -0
  90. package/dist/types-DuNbGKjF.mjs +74 -0
  91. package/dist/types-ju-_ORz7.d.mts +182 -0
  92. package/dist/validate-CXnRKfJK.mjs +327 -0
  93. package/dist/validate-CqRJb_xU.mjs +96 -0
  94. package/dist/validate-DVKJJ-M_.d.mts +377 -0
  95. package/locals.d.ts +47 -0
  96. package/package.json +313 -0
@@ -0,0 +1,1339 @@
1
+ import { t as __exportAll } from "./chunk-ClPoSABd.mjs";
2
+ import { t as ContentRepository } from "./content-zSgdNmnt.mjs";
3
+ import { t as MediaRepository } from "./media-DMTr80Gv.mjs";
4
+ import { i as FTSManager, n as SchemaRegistry } from "./registry-DSd1GWB8.mjs";
5
+ import { t as RedirectRepository } from "./redirect-JPqLAbxa.mjs";
6
+ import { t as BylineRepository } from "./byline-DeWCMU_i.mjs";
7
+ import { n as getDb } from "./loader-qKmo0wAY.mjs";
8
+ import { t as validateSeed } from "./validate-CXnRKfJK.mjs";
9
+ import { sql } from "kysely";
10
+ import { ulid } from "ulidx";
11
+ import { imageSize } from "image-size";
12
+ import mime from "mime/lite";
13
+
14
+ //#region src/database/repositories/taxonomy.ts
15
+ /**
16
+ * Taxonomy repository for categories, tags, and other classification
17
+ *
18
+ * Taxonomies are hierarchical (via parentId) and can be attached to content entries.
19
+ */
20
+ var TaxonomyRepository = class {
21
+ constructor(db) {
22
+ this.db = db;
23
+ }
24
+ /**
25
+ * Create a new taxonomy term
26
+ */
27
+ async create(input) {
28
+ const id = ulid();
29
+ const row = {
30
+ id,
31
+ name: input.name,
32
+ slug: input.slug,
33
+ label: input.label,
34
+ parent_id: input.parentId ?? null,
35
+ data: input.data ? JSON.stringify(input.data) : null
36
+ };
37
+ await this.db.insertInto("taxonomies").values(row).execute();
38
+ const taxonomy = await this.findById(id);
39
+ if (!taxonomy) throw new Error("Failed to create taxonomy");
40
+ return taxonomy;
41
+ }
42
+ /**
43
+ * Find taxonomy by ID
44
+ */
45
+ async findById(id) {
46
+ const row = await this.db.selectFrom("taxonomies").selectAll().where("id", "=", id).executeTakeFirst();
47
+ return row ? this.rowToTaxonomy(row) : null;
48
+ }
49
+ /**
50
+ * Find taxonomy by name and slug (unique constraint)
51
+ */
52
+ async findBySlug(name, slug) {
53
+ const row = await this.db.selectFrom("taxonomies").selectAll().where("name", "=", name).where("slug", "=", slug).executeTakeFirst();
54
+ return row ? this.rowToTaxonomy(row) : null;
55
+ }
56
+ /**
57
+ * Get all terms for a taxonomy (e.g., all categories)
58
+ */
59
+ async findByName(name, options = {}) {
60
+ let query = this.db.selectFrom("taxonomies").selectAll().where("name", "=", name).orderBy("label", "asc");
61
+ if (options.parentId !== void 0) if (options.parentId === null) query = query.where("parent_id", "is", null);
62
+ else query = query.where("parent_id", "=", options.parentId);
63
+ return (await query.execute()).map((row) => this.rowToTaxonomy(row));
64
+ }
65
+ /**
66
+ * Get children of a taxonomy term
67
+ */
68
+ async findChildren(parentId) {
69
+ return (await this.db.selectFrom("taxonomies").selectAll().where("parent_id", "=", parentId).orderBy("label", "asc").execute()).map((row) => this.rowToTaxonomy(row));
70
+ }
71
+ /**
72
+ * Update a taxonomy term
73
+ */
74
+ async update(id, input) {
75
+ if (!await this.findById(id)) return null;
76
+ const updates = {};
77
+ if (input.slug !== void 0) updates.slug = input.slug;
78
+ if (input.label !== void 0) updates.label = input.label;
79
+ if (input.parentId !== void 0) updates.parent_id = input.parentId;
80
+ if (input.data !== void 0) updates.data = JSON.stringify(input.data);
81
+ if (Object.keys(updates).length > 0) await this.db.updateTable("taxonomies").set(updates).where("id", "=", id).execute();
82
+ return this.findById(id);
83
+ }
84
+ /**
85
+ * Delete a taxonomy term
86
+ */
87
+ async delete(id) {
88
+ await this.db.deleteFrom("content_taxonomies").where("taxonomy_id", "=", id).execute();
89
+ return ((await this.db.deleteFrom("taxonomies").where("id", "=", id).executeTakeFirst()).numDeletedRows ?? 0) > 0;
90
+ }
91
+ /**
92
+ * Attach a taxonomy term to a content entry
93
+ */
94
+ async attachToEntry(collection, entryId, taxonomyId) {
95
+ const row = {
96
+ collection,
97
+ entry_id: entryId,
98
+ taxonomy_id: taxonomyId
99
+ };
100
+ await this.db.insertInto("content_taxonomies").values(row).onConflict((oc) => oc.doNothing()).execute();
101
+ }
102
+ /**
103
+ * Detach a taxonomy term from a content entry
104
+ */
105
+ async detachFromEntry(collection, entryId, taxonomyId) {
106
+ await this.db.deleteFrom("content_taxonomies").where("collection", "=", collection).where("entry_id", "=", entryId).where("taxonomy_id", "=", taxonomyId).execute();
107
+ }
108
+ /**
109
+ * Get all taxonomy terms for a content entry
110
+ */
111
+ async getTermsForEntry(collection, entryId, taxonomyName) {
112
+ let query = this.db.selectFrom("content_taxonomies").innerJoin("taxonomies", "taxonomies.id", "content_taxonomies.taxonomy_id").selectAll("taxonomies").where("content_taxonomies.collection", "=", collection).where("content_taxonomies.entry_id", "=", entryId);
113
+ if (taxonomyName) query = query.where("taxonomies.name", "=", taxonomyName);
114
+ return (await query.execute()).map((row) => this.rowToTaxonomy(row));
115
+ }
116
+ /**
117
+ * Set all taxonomy terms for a content entry (replaces existing)
118
+ * Uses batch operations to avoid N+1 queries.
119
+ */
120
+ async setTermsForEntry(collection, entryId, taxonomyName, taxonomyIds) {
121
+ const current = await this.getTermsForEntry(collection, entryId, taxonomyName);
122
+ const currentIds = new Set(current.map((t) => t.id));
123
+ const newIds = new Set(taxonomyIds);
124
+ const toRemove = current.filter((t) => !newIds.has(t.id)).map((t) => t.id);
125
+ if (toRemove.length > 0) await this.db.deleteFrom("content_taxonomies").where("collection", "=", collection).where("entry_id", "=", entryId).where("taxonomy_id", "in", toRemove).execute();
126
+ const toAdd = taxonomyIds.filter((id) => !currentIds.has(id));
127
+ if (toAdd.length > 0) await this.db.insertInto("content_taxonomies").values(toAdd.map((taxonomy_id) => ({
128
+ collection,
129
+ entry_id: entryId,
130
+ taxonomy_id
131
+ }))).onConflict((oc) => oc.doNothing()).execute();
132
+ }
133
+ /**
134
+ * Remove all taxonomy associations for an entry (use when entry is deleted)
135
+ */
136
+ async clearEntryTerms(collection, entryId) {
137
+ const result = await this.db.deleteFrom("content_taxonomies").where("collection", "=", collection).where("entry_id", "=", entryId).executeTakeFirst();
138
+ return Number(result.numDeletedRows ?? 0);
139
+ }
140
+ /**
141
+ * Count entries that have a specific taxonomy term
142
+ */
143
+ async countEntriesWithTerm(taxonomyId) {
144
+ const result = await this.db.selectFrom("content_taxonomies").select((eb) => eb.fn.count("entry_id").as("count")).where("taxonomy_id", "=", taxonomyId).executeTakeFirst();
145
+ return Number(result?.count || 0);
146
+ }
147
+ /**
148
+ * Convert database row to Taxonomy object
149
+ */
150
+ rowToTaxonomy(row) {
151
+ return {
152
+ id: row.id,
153
+ name: row.name,
154
+ slug: row.slug,
155
+ label: row.label,
156
+ parentId: row.parent_id,
157
+ data: row.data ? JSON.parse(row.data) : null
158
+ };
159
+ }
160
+ };
161
+
162
+ //#endregion
163
+ //#region src/database/repositories/options.ts
164
+ function escapeLike(value) {
165
+ return value.replaceAll("\\", "\\\\").replaceAll("%", "\\%").replaceAll("_", "\\_");
166
+ }
167
+ /**
168
+ * Options repository for key-value settings storage
169
+ *
170
+ * Used for site settings, plugin configuration, and other arbitrary key-value data.
171
+ * Values are stored as JSON for flexibility.
172
+ */
173
+ var OptionsRepository = class {
174
+ constructor(db) {
175
+ this.db = db;
176
+ }
177
+ /**
178
+ * Get an option value
179
+ */
180
+ async get(name) {
181
+ const row = await this.db.selectFrom("options").select("value").where("name", "=", name).executeTakeFirst();
182
+ if (!row) return null;
183
+ return JSON.parse(row.value);
184
+ }
185
+ /**
186
+ * Get an option value with a default
187
+ */
188
+ async getOrDefault(name, defaultValue) {
189
+ return await this.get(name) ?? defaultValue;
190
+ }
191
+ /**
192
+ * Set an option value (creates or updates)
193
+ */
194
+ async set(name, value) {
195
+ const row = {
196
+ name,
197
+ value: JSON.stringify(value)
198
+ };
199
+ await this.db.insertInto("options").values(row).onConflict((oc) => oc.column("name").doUpdateSet({ value: row.value })).execute();
200
+ }
201
+ /**
202
+ * Delete an option
203
+ */
204
+ async delete(name) {
205
+ return ((await this.db.deleteFrom("options").where("name", "=", name).executeTakeFirst()).numDeletedRows ?? 0) > 0;
206
+ }
207
+ /**
208
+ * Check if an option exists
209
+ */
210
+ async exists(name) {
211
+ return !!await this.db.selectFrom("options").select("name").where("name", "=", name).executeTakeFirst();
212
+ }
213
+ /**
214
+ * Get multiple options at once
215
+ */
216
+ async getMany(names) {
217
+ if (names.length === 0) return /* @__PURE__ */ new Map();
218
+ const rows = await this.db.selectFrom("options").select(["name", "value"]).where("name", "in", names).execute();
219
+ const result = /* @__PURE__ */ new Map();
220
+ for (const row of rows) result.set(row.name, JSON.parse(row.value));
221
+ return result;
222
+ }
223
+ /**
224
+ * Set multiple options at once
225
+ */
226
+ async setMany(options) {
227
+ const entries = Object.entries(options);
228
+ if (entries.length === 0) return;
229
+ for (const [name, value] of entries) await this.set(name, value);
230
+ }
231
+ /**
232
+ * Get all options (use sparingly)
233
+ */
234
+ async getAll() {
235
+ const rows = await this.db.selectFrom("options").select(["name", "value"]).execute();
236
+ const result = /* @__PURE__ */ new Map();
237
+ for (const row of rows) result.set(row.name, JSON.parse(row.value));
238
+ return result;
239
+ }
240
+ /**
241
+ * Get all options matching a prefix
242
+ */
243
+ async getByPrefix(prefix) {
244
+ const pattern = `${escapeLike(prefix)}%`;
245
+ const rows = await this.db.selectFrom("options").select(["name", "value"]).where(sql`name LIKE ${pattern} ESCAPE '\\'`).execute();
246
+ const result = /* @__PURE__ */ new Map();
247
+ for (const row of rows) result.set(row.name, JSON.parse(row.value));
248
+ return result;
249
+ }
250
+ /**
251
+ * Delete all options matching a prefix
252
+ */
253
+ async deleteByPrefix(prefix) {
254
+ const pattern = `${escapeLike(prefix)}%`;
255
+ const result = await this.db.deleteFrom("options").where(sql`name LIKE ${pattern} ESCAPE '\\'`).executeTakeFirst();
256
+ return Number(result.numDeletedRows ?? 0);
257
+ }
258
+ };
259
+
260
+ //#endregion
261
+ //#region src/settings/index.ts
262
+ /** Prefix for site settings in the options table */
263
+ const SETTINGS_PREFIX = "site:";
264
+ /**
265
+ * Type guard for MediaReference values
266
+ */
267
+ function isMediaReference(value) {
268
+ return typeof value === "object" && value !== null && "mediaId" in value;
269
+ }
270
+ /**
271
+ * Resolve a media reference to include the full URL
272
+ */
273
+ async function resolveMediaReference(mediaRef, db, _storage) {
274
+ if (!mediaRef?.mediaId) return mediaRef;
275
+ try {
276
+ const media = await new MediaRepository(db).findById(mediaRef.mediaId);
277
+ if (media) return {
278
+ ...mediaRef,
279
+ url: `/_dineway/api/media/file/${media.storageKey}`
280
+ };
281
+ } catch {}
282
+ return mediaRef;
283
+ }
284
+ /**
285
+ * Get a single site setting by key
286
+ *
287
+ * Returns `undefined` if the setting has not been configured.
288
+ * For media settings (logo, favicon), the URL is resolved automatically.
289
+ *
290
+ * @param key - The setting key (e.g., "title", "logo", "social")
291
+ * @returns The setting value, or undefined if not set
292
+ *
293
+ * @example
294
+ * ```ts
295
+ * import { getSiteSetting } from "dineway";
296
+ *
297
+ * const title = await getSiteSetting("title");
298
+ * const logo = await getSiteSetting("logo");
299
+ * console.log(logo?.url); // Resolved URL
300
+ * ```
301
+ */
302
+ async function getSiteSetting(key) {
303
+ return getSiteSettingWithDb(key, await getDb());
304
+ }
305
+ /**
306
+ * Get a single site setting by key (with explicit db)
307
+ *
308
+ * @internal Use `getSiteSetting()` in templates. This variant is for admin routes
309
+ * that already have a database handle.
310
+ */
311
+ async function getSiteSettingWithDb(key, db, storage = null) {
312
+ const value = await new OptionsRepository(db).get(`${SETTINGS_PREFIX}${key}`);
313
+ if (!value) return;
314
+ if ((key === "logo" || key === "favicon") && isMediaReference(value)) return await resolveMediaReference(value, db, storage);
315
+ return value;
316
+ }
317
+ /**
318
+ * Get all site settings
319
+ *
320
+ * Returns all configured settings. Unset values are undefined.
321
+ * Media references (logo/favicon) are resolved to include URLs.
322
+ *
323
+ * @example
324
+ * ```ts
325
+ * import { getSiteSettings } from "dineway";
326
+ *
327
+ * const settings = await getSiteSettings();
328
+ * console.log(settings.title); // "My Site"
329
+ * console.log(settings.logo?.url); // "/_dineway/api/media/file/abc123"
330
+ * ```
331
+ */
332
+ async function getSiteSettings() {
333
+ return getSiteSettingsWithDb(await getDb());
334
+ }
335
+ /**
336
+ * Get all site settings (with explicit db)
337
+ *
338
+ * @internal Use `getSiteSettings()` in templates. This variant is for admin routes
339
+ * that already have a database handle.
340
+ */
341
+ async function getSiteSettingsWithDb(db, storage = null) {
342
+ const allOptions = await new OptionsRepository(db).getByPrefix(SETTINGS_PREFIX);
343
+ const settings = {};
344
+ for (const [key, value] of allOptions) {
345
+ const settingKey = key.replace(SETTINGS_PREFIX, "");
346
+ settings[settingKey] = value;
347
+ }
348
+ const typedSettings = settings;
349
+ if (typedSettings.logo) typedSettings.logo = await resolveMediaReference(typedSettings.logo, db, storage);
350
+ if (typedSettings.favicon) typedSettings.favicon = await resolveMediaReference(typedSettings.favicon, db, storage);
351
+ return typedSettings;
352
+ }
353
+ /**
354
+ * Set site settings (internal function used by admin API)
355
+ *
356
+ * Merges provided settings with existing ones. Only provided fields are updated.
357
+ * Media references should include just the mediaId; URLs are resolved on read.
358
+ *
359
+ * @param settings - Partial settings object with values to update
360
+ * @param db - Kysely database instance
361
+ * @returns Promise that resolves when settings are saved
362
+ *
363
+ * @internal
364
+ *
365
+ * @example
366
+ * ```ts
367
+ * // Update multiple settings at once
368
+ * await setSiteSettings({
369
+ * title: "My Site",
370
+ * tagline: "Welcome",
371
+ * logo: { mediaId: "med_123", alt: "Logo" }
372
+ * }, db);
373
+ * ```
374
+ */
375
+ async function setSiteSettings(settings, db) {
376
+ const options = new OptionsRepository(db);
377
+ const updates = {};
378
+ for (const [key, value] of Object.entries(settings)) if (value !== void 0) updates[`${SETTINGS_PREFIX}${key}`] = value;
379
+ await options.setMany(updates);
380
+ }
381
+ /**
382
+ * Get a single plugin setting by key.
383
+ *
384
+ * Plugin settings are stored in the options table under
385
+ * `plugin:<pluginId>:settings:<key>`.
386
+ */
387
+ async function getPluginSetting(pluginId, key) {
388
+ return getPluginSettingWithDb(pluginId, key, await getDb());
389
+ }
390
+ /**
391
+ * Get a single plugin setting by key (with explicit db).
392
+ *
393
+ * @internal Use `getPluginSetting()` in templates and plugin rendering code.
394
+ */
395
+ async function getPluginSettingWithDb(pluginId, key, db) {
396
+ return await new OptionsRepository(db).get(`plugin:${pluginId}:settings:${key}`) ?? void 0;
397
+ }
398
+ /**
399
+ * Get all persisted plugin settings for a plugin.
400
+ *
401
+ * Defaults declared in `admin.settingsSchema` are not materialized
402
+ * automatically; callers should apply their own fallback defaults.
403
+ */
404
+ async function getPluginSettings(pluginId) {
405
+ return getPluginSettingsWithDb(pluginId, await getDb());
406
+ }
407
+ /**
408
+ * Get all persisted plugin settings for a plugin (with explicit db).
409
+ *
410
+ * @internal Use `getPluginSettings()` in templates and plugin rendering code.
411
+ */
412
+ async function getPluginSettingsWithDb(pluginId, db) {
413
+ const prefix = `plugin:${pluginId}:settings:`;
414
+ const allOptions = await new OptionsRepository(db).getByPrefix(prefix);
415
+ const settings = {};
416
+ for (const [key, value] of allOptions) {
417
+ if (!key.startsWith(prefix)) continue;
418
+ settings[key.slice(prefix.length)] = value;
419
+ }
420
+ return settings;
421
+ }
422
+
423
+ //#endregion
424
+ //#region src/import/ssrf.ts
425
+ /**
426
+ * SSRF protection for import URLs.
427
+ *
428
+ * Validates that URLs don't target internal/private network addresses.
429
+ * Applied before any fetch() call in the import pipeline.
430
+ */
431
+ const IPV4_MAPPED_IPV6_DOTTED_PATTERN = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/i;
432
+ const IPV4_MAPPED_IPV6_HEX_PATTERN = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i;
433
+ const IPV4_TRANSLATED_HEX_PATTERN = /^::ffff:0:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i;
434
+ const IPV6_EXPANDED_MAPPED_PATTERN = /^0{0,4}:0{0,4}:0{0,4}:0{0,4}:0{0,4}:ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i;
435
+ /**
436
+ * IPv4-compatible (deprecated) addresses: ::XXXX:XXXX
437
+ *
438
+ * The WHATWG URL parser normalizes [::127.0.0.1] to [::7f00:1] (no ffff prefix).
439
+ * These are deprecated but still parsed, and bypass the ffff-based checks.
440
+ */
441
+ const IPV4_COMPATIBLE_HEX_PATTERN = /^::([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i;
442
+ /**
443
+ * NAT64 prefix (RFC 6052): 64:ff9b::XXXX:XXXX
444
+ *
445
+ * Used by NAT64 gateways to embed IPv4 addresses in IPv6.
446
+ * [64:ff9b::127.0.0.1] normalizes to [64:ff9b::7f00:1].
447
+ */
448
+ const NAT64_HEX_PATTERN = /^64:ff9b::([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i;
449
+ const IPV6_BRACKET_PATTERN = /^\[|\]$/g;
450
+ /**
451
+ * Private and reserved IP ranges that should never be fetched.
452
+ *
453
+ * Includes:
454
+ * - Loopback (127.0.0.0/8)
455
+ * - Private (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16)
456
+ * - Link-local (169.254.0.0/16)
457
+ * - Cloud metadata (169.254.169.254 — AWS/GCP/Azure)
458
+ * - IPv6 loopback and link-local
459
+ */
460
+ const BLOCKED_PATTERNS = [
461
+ {
462
+ start: ip4ToNum(127, 0, 0, 0),
463
+ end: ip4ToNum(127, 255, 255, 255)
464
+ },
465
+ {
466
+ start: ip4ToNum(10, 0, 0, 0),
467
+ end: ip4ToNum(10, 255, 255, 255)
468
+ },
469
+ {
470
+ start: ip4ToNum(172, 16, 0, 0),
471
+ end: ip4ToNum(172, 31, 255, 255)
472
+ },
473
+ {
474
+ start: ip4ToNum(192, 168, 0, 0),
475
+ end: ip4ToNum(192, 168, 255, 255)
476
+ },
477
+ {
478
+ start: ip4ToNum(169, 254, 0, 0),
479
+ end: ip4ToNum(169, 254, 255, 255)
480
+ },
481
+ {
482
+ start: ip4ToNum(0, 0, 0, 0),
483
+ end: ip4ToNum(0, 255, 255, 255)
484
+ }
485
+ ];
486
+ const BLOCKED_HOSTNAMES = new Set([
487
+ "localhost",
488
+ "metadata.google.internal",
489
+ "metadata.google",
490
+ "[::1]"
491
+ ]);
492
+ /** Blocked URL schemes */
493
+ const ALLOWED_SCHEMES = new Set(["http:", "https:"]);
494
+ function ip4ToNum(a, b, c, d) {
495
+ return (a << 24 | b << 16 | c << 8 | d) >>> 0;
496
+ }
497
+ function parseIpv4(ip) {
498
+ const parts = ip.split(".");
499
+ if (parts.length !== 4) return null;
500
+ const nums = parts.map(Number);
501
+ if (nums.some((n) => isNaN(n) || n < 0 || n > 255)) return null;
502
+ return ip4ToNum(nums[0], nums[1], nums[2], nums[3]);
503
+ }
504
+ /**
505
+ * Convert IPv4-mapped/translated IPv6 addresses from hex form back to IPv4.
506
+ *
507
+ * The WHATWG URL parser normalizes dotted-decimal to hex:
508
+ * [::ffff:127.0.0.1] -> [::ffff:7f00:1]
509
+ * [::ffff:169.254.169.254] -> [::ffff:a9fe:a9fe]
510
+ *
511
+ * Without this conversion, the hex forms bypass isPrivateIp() regex checks.
512
+ */
513
+ function normalizeIPv6MappedToIPv4(ip) {
514
+ let match = ip.match(IPV4_MAPPED_IPV6_HEX_PATTERN);
515
+ if (!match) match = ip.match(IPV4_TRANSLATED_HEX_PATTERN);
516
+ if (!match) match = ip.match(IPV6_EXPANDED_MAPPED_PATTERN);
517
+ if (!match) match = ip.match(IPV4_COMPATIBLE_HEX_PATTERN);
518
+ if (!match) match = ip.match(NAT64_HEX_PATTERN);
519
+ if (match) {
520
+ const high = parseInt(match[1] ?? "", 16);
521
+ const low = parseInt(match[2] ?? "", 16);
522
+ return `${high >> 8 & 255}.${high & 255}.${low >> 8 & 255}.${low & 255}`;
523
+ }
524
+ return null;
525
+ }
526
+ function isPrivateIp(ip) {
527
+ if (ip === "::1" || ip === "::ffff:127.0.0.1") return true;
528
+ const hexIpv4 = normalizeIPv6MappedToIPv4(ip);
529
+ if (hexIpv4) return isPrivateIp(hexIpv4);
530
+ const v4Match = ip.match(IPV4_MAPPED_IPV6_DOTTED_PATTERN);
531
+ const num = parseIpv4(v4Match ? v4Match[1] : ip);
532
+ if (num === null) return ip.startsWith("fe80:") || ip.startsWith("fc") || ip.startsWith("fd");
533
+ return BLOCKED_PATTERNS.some((range) => num >= range.start && num <= range.end);
534
+ }
535
+ /**
536
+ * Error thrown when SSRF protection blocks a URL.
537
+ */
538
+ var SsrfError = class extends Error {
539
+ code = "SSRF_BLOCKED";
540
+ constructor(message) {
541
+ super(message);
542
+ this.name = "SsrfError";
543
+ }
544
+ };
545
+ /**
546
+ * Validate that a URL is safe to fetch (not targeting internal networks).
547
+ *
548
+ * Checks:
549
+ * 1. URL is well-formed with http/https scheme
550
+ * 2. Hostname is not a known internal name (localhost, metadata endpoints)
551
+ * 3. If hostname is an IP literal, it's not in a private range
552
+ *
553
+ * Note: DNS rebinding attacks are not fully mitigated (hostname could resolve
554
+ * to a private IP). Full protection requires resolving DNS and checking the IP
555
+ * before connecting, which needs a custom fetch implementation. This covers
556
+ * the most common SSRF vectors.
557
+ *
558
+ * @throws SsrfError if the URL targets an internal address
559
+ */
560
+ /** Maximum number of redirects to follow in ssrfSafeFetch */
561
+ const MAX_REDIRECTS = 5;
562
+ function validateExternalUrl(url) {
563
+ let parsed;
564
+ try {
565
+ parsed = new URL(url);
566
+ } catch {
567
+ throw new SsrfError("Invalid URL");
568
+ }
569
+ if (!ALLOWED_SCHEMES.has(parsed.protocol)) throw new SsrfError(`Scheme '${parsed.protocol}' is not allowed`);
570
+ const hostname = parsed.hostname.replace(IPV6_BRACKET_PATTERN, "");
571
+ if (BLOCKED_HOSTNAMES.has(hostname.toLowerCase())) throw new SsrfError("URLs targeting internal hosts are not allowed");
572
+ if (isPrivateIp(hostname)) throw new SsrfError("URLs targeting private IP addresses are not allowed");
573
+ return parsed;
574
+ }
575
+ /**
576
+ * Fetch a URL with SSRF protection on redirects.
577
+ *
578
+ * Uses `redirect: "manual"` to intercept redirects and re-validate each
579
+ * redirect target against SSRF rules before following it. This prevents
580
+ * an attacker from setting up an allowed external URL that redirects to
581
+ * an internal IP (e.g. 169.254.169.254 for cloud metadata).
582
+ *
583
+ * @throws SsrfError if the initial URL or any redirect target is internal
584
+ */
585
+ /** Headers that must be stripped when a redirect crosses origins */
586
+ const CREDENTIAL_HEADERS = [
587
+ "authorization",
588
+ "cookie",
589
+ "proxy-authorization"
590
+ ];
591
+ async function ssrfSafeFetch(url, init) {
592
+ let currentUrl = url;
593
+ let currentInit = init;
594
+ for (let i = 0; i <= MAX_REDIRECTS; i++) {
595
+ validateExternalUrl(currentUrl);
596
+ const response = await globalThis.fetch(currentUrl, {
597
+ ...currentInit,
598
+ redirect: "manual"
599
+ });
600
+ if (response.status < 300 || response.status >= 400) return response;
601
+ const location = response.headers.get("Location");
602
+ if (!location) return response;
603
+ const previousOrigin = new URL(currentUrl).origin;
604
+ currentUrl = new URL(location, currentUrl).href;
605
+ if (previousOrigin !== new URL(currentUrl).origin && currentInit) currentInit = stripCredentialHeaders(currentInit);
606
+ }
607
+ throw new SsrfError(`Too many redirects (max ${MAX_REDIRECTS})`);
608
+ }
609
+ /**
610
+ * Return a copy of init with credential headers removed.
611
+ */
612
+ function stripCredentialHeaders(init) {
613
+ if (!init.headers) return init;
614
+ const headers = new Headers(init.headers);
615
+ for (const name of CREDENTIAL_HEADERS) headers.delete(name);
616
+ return {
617
+ ...init,
618
+ headers
619
+ };
620
+ }
621
+
622
+ //#endregion
623
+ //#region src/seed/apply.ts
624
+ /**
625
+ * Seed engine - applies seed files to database
626
+ *
627
+ * This is the core implementation that bootstraps an Dineway site from a seed file.
628
+ * Apply order is critical for foreign keys and references.
629
+ */
630
+ var apply_exports = /* @__PURE__ */ __exportAll({ applySeed: () => applySeed });
631
+ const FILE_EXTENSION_PATTERN = /\.([a-z0-9]+)(?:\?|$)/i;
632
+ /** Pattern to remove file extensions */
633
+ const EXTENSION_PATTERN = /\.[^.]+$/;
634
+ /** Pattern to remove query parameters */
635
+ const QUERY_PARAM_PATTERN = /\?.*$/;
636
+ /** Pattern to remove non-alphanumeric characters (except dash and underscore) */
637
+ const SANITIZE_PATTERN = /[^a-zA-Z0-9_-]/g;
638
+ /** Pattern to collapse multiple hyphens */
639
+ const MULTIPLE_HYPHENS_PATTERN = /-+/g;
640
+ /**
641
+ * Apply a seed file to the database
642
+ *
643
+ * This function is idempotent - safe to run multiple times.
644
+ *
645
+ * @param db - Kysely database instance
646
+ * @param seed - Seed file to apply
647
+ * @param options - Application options
648
+ * @returns Result summary
649
+ */
650
+ async function applySeed(db, seed, options = {}) {
651
+ const validation = validateSeed(seed);
652
+ if (!validation.valid) throw new Error(`Invalid seed file:\n${validation.errors.join("\n")}`);
653
+ const { includeContent = false, storage, skipMediaDownload = false, onConflict = "skip" } = options;
654
+ const result = {
655
+ collections: {
656
+ created: 0,
657
+ skipped: 0,
658
+ updated: 0
659
+ },
660
+ fields: {
661
+ created: 0,
662
+ skipped: 0,
663
+ updated: 0
664
+ },
665
+ taxonomies: {
666
+ created: 0,
667
+ terms: 0
668
+ },
669
+ bylines: {
670
+ created: 0,
671
+ skipped: 0,
672
+ updated: 0
673
+ },
674
+ menus: {
675
+ created: 0,
676
+ items: 0
677
+ },
678
+ redirects: {
679
+ created: 0,
680
+ skipped: 0,
681
+ updated: 0
682
+ },
683
+ widgetAreas: {
684
+ created: 0,
685
+ widgets: 0
686
+ },
687
+ sections: {
688
+ created: 0,
689
+ skipped: 0,
690
+ updated: 0
691
+ },
692
+ settings: { applied: 0 },
693
+ content: {
694
+ created: 0,
695
+ skipped: 0,
696
+ updated: 0
697
+ },
698
+ media: {
699
+ created: 0,
700
+ skipped: 0
701
+ }
702
+ };
703
+ const mediaContext = {
704
+ db,
705
+ storage: storage ?? null,
706
+ skipMediaDownload,
707
+ mediaCache: /* @__PURE__ */ new Map()
708
+ };
709
+ const seedIdMap = /* @__PURE__ */ new Map();
710
+ const seedBylineIdMap = /* @__PURE__ */ new Map();
711
+ if (seed.settings) {
712
+ await setSiteSettings(seed.settings, db);
713
+ result.settings.applied = Object.keys(seed.settings).length;
714
+ }
715
+ if (seed.collections) {
716
+ const registry = new SchemaRegistry(db);
717
+ for (const collection of seed.collections) {
718
+ if (await registry.getCollection(collection.slug)) {
719
+ if (onConflict === "error") throw new Error(`Conflict: collection "${collection.slug}" already exists`);
720
+ if (onConflict === "update") {
721
+ await registry.updateCollection(collection.slug, {
722
+ label: collection.label,
723
+ labelSingular: collection.labelSingular,
724
+ description: collection.description,
725
+ icon: collection.icon,
726
+ supports: collection.supports || [],
727
+ urlPattern: collection.urlPattern,
728
+ commentsEnabled: collection.commentsEnabled
729
+ });
730
+ result.collections.updated++;
731
+ for (const field of collection.fields) if (await registry.getField(collection.slug, field.slug)) {
732
+ await registry.updateField(collection.slug, field.slug, {
733
+ label: field.label,
734
+ required: field.required || false,
735
+ unique: field.unique || false,
736
+ searchable: field.searchable || false,
737
+ defaultValue: field.defaultValue,
738
+ validation: field.validation,
739
+ widget: field.widget,
740
+ options: field.options
741
+ });
742
+ result.fields.updated++;
743
+ } else {
744
+ await registry.createField(collection.slug, {
745
+ slug: field.slug,
746
+ label: field.label,
747
+ type: field.type,
748
+ required: field.required || false,
749
+ unique: field.unique || false,
750
+ searchable: field.searchable || false,
751
+ defaultValue: field.defaultValue,
752
+ validation: field.validation,
753
+ widget: field.widget,
754
+ options: field.options
755
+ });
756
+ result.fields.created++;
757
+ }
758
+ continue;
759
+ }
760
+ result.collections.skipped++;
761
+ result.fields.skipped += collection.fields.length;
762
+ continue;
763
+ }
764
+ await registry.createCollection({
765
+ slug: collection.slug,
766
+ label: collection.label,
767
+ labelSingular: collection.labelSingular,
768
+ description: collection.description,
769
+ icon: collection.icon,
770
+ supports: collection.supports || [],
771
+ source: "seed",
772
+ urlPattern: collection.urlPattern,
773
+ commentsEnabled: collection.commentsEnabled
774
+ });
775
+ result.collections.created++;
776
+ for (const field of collection.fields) {
777
+ await registry.createField(collection.slug, {
778
+ slug: field.slug,
779
+ label: field.label,
780
+ type: field.type,
781
+ required: field.required || false,
782
+ unique: field.unique || false,
783
+ searchable: field.searchable || false,
784
+ defaultValue: field.defaultValue,
785
+ validation: field.validation,
786
+ widget: field.widget,
787
+ options: field.options
788
+ });
789
+ result.fields.created++;
790
+ }
791
+ }
792
+ }
793
+ if (seed.taxonomies) for (const taxonomy of seed.taxonomies) {
794
+ const existingDef = await db.selectFrom("_dineway_taxonomy_defs").selectAll().where("name", "=", taxonomy.name).executeTakeFirst();
795
+ if (existingDef) {
796
+ if (onConflict === "error") throw new Error(`Conflict: taxonomy "${taxonomy.name}" already exists`);
797
+ if (onConflict === "update") await db.updateTable("_dineway_taxonomy_defs").set({
798
+ label: taxonomy.label,
799
+ label_singular: taxonomy.labelSingular ?? null,
800
+ hierarchical: taxonomy.hierarchical ? 1 : 0,
801
+ collections: JSON.stringify(taxonomy.collections)
802
+ }).where("id", "=", existingDef.id).execute();
803
+ } else {
804
+ await db.insertInto("_dineway_taxonomy_defs").values({
805
+ id: ulid(),
806
+ name: taxonomy.name,
807
+ label: taxonomy.label,
808
+ label_singular: taxonomy.labelSingular ?? null,
809
+ hierarchical: taxonomy.hierarchical ? 1 : 0,
810
+ collections: JSON.stringify(taxonomy.collections)
811
+ }).execute();
812
+ result.taxonomies.created++;
813
+ }
814
+ if (taxonomy.terms && taxonomy.terms.length > 0) {
815
+ const termRepo = new TaxonomyRepository(db);
816
+ if (taxonomy.hierarchical) await applyHierarchicalTerms(termRepo, taxonomy.name, taxonomy.terms, result, onConflict);
817
+ else for (const term of taxonomy.terms) {
818
+ const existing = await termRepo.findBySlug(taxonomy.name, term.slug);
819
+ if (existing) {
820
+ if (onConflict === "error") throw new Error(`Conflict: taxonomy term "${term.slug}" in "${taxonomy.name}" already exists`);
821
+ if (onConflict === "update") {
822
+ await termRepo.update(existing.id, {
823
+ label: term.label,
824
+ data: term.description ? { description: term.description } : {}
825
+ });
826
+ result.taxonomies.terms++;
827
+ }
828
+ } else {
829
+ await termRepo.create({
830
+ name: taxonomy.name,
831
+ slug: term.slug,
832
+ label: term.label,
833
+ data: term.description ? { description: term.description } : void 0
834
+ });
835
+ result.taxonomies.terms++;
836
+ }
837
+ }
838
+ }
839
+ }
840
+ if (seed.bylines) {
841
+ const bylineRepo = new BylineRepository(db);
842
+ for (const byline of seed.bylines) {
843
+ const existing = await bylineRepo.findBySlug(byline.slug);
844
+ if (existing) {
845
+ if (onConflict === "error") throw new Error(`Conflict: byline "${byline.slug}" already exists`);
846
+ if (onConflict === "update") {
847
+ await bylineRepo.update(existing.id, {
848
+ displayName: byline.displayName,
849
+ bio: byline.bio ?? null,
850
+ websiteUrl: byline.websiteUrl ?? null,
851
+ isGuest: byline.isGuest
852
+ });
853
+ seedBylineIdMap.set(byline.id, existing.id);
854
+ result.bylines.updated++;
855
+ continue;
856
+ }
857
+ seedBylineIdMap.set(byline.id, existing.id);
858
+ result.bylines.skipped++;
859
+ continue;
860
+ }
861
+ const created = await bylineRepo.create({
862
+ slug: byline.slug,
863
+ displayName: byline.displayName,
864
+ bio: byline.bio ?? null,
865
+ websiteUrl: byline.websiteUrl ?? null,
866
+ isGuest: byline.isGuest
867
+ });
868
+ seedBylineIdMap.set(byline.id, created.id);
869
+ result.bylines.created++;
870
+ }
871
+ }
872
+ if (includeContent && seed.content) {
873
+ const contentRepo = new ContentRepository(db);
874
+ const bylineRepo = new BylineRepository(db);
875
+ for (const [collectionSlug, entries] of Object.entries(seed.content)) for (const entry of entries) {
876
+ const existing = await contentRepo.findBySlug(collectionSlug, entry.slug, entry.locale);
877
+ if (existing) {
878
+ if (onConflict === "error") throw new Error(`Conflict: content "${entry.slug}" in "${collectionSlug}" already exists`);
879
+ if (onConflict === "update") {
880
+ const resolvedData = await resolveReferences(entry.data, seedIdMap, mediaContext, result);
881
+ const status = entry.status || "published";
882
+ await contentRepo.update(collectionSlug, existing.id, {
883
+ status,
884
+ data: resolvedData
885
+ });
886
+ seedIdMap.set(entry.id, existing.id);
887
+ result.content.updated++;
888
+ await applyContentBylines(bylineRepo, collectionSlug, existing.id, entry, seedBylineIdMap, true);
889
+ await applyContentTaxonomies(db, collectionSlug, existing.id, entry, true);
890
+ continue;
891
+ }
892
+ result.content.skipped++;
893
+ seedIdMap.set(entry.id, existing.id);
894
+ continue;
895
+ }
896
+ const resolvedData = await resolveReferences(entry.data, seedIdMap, mediaContext, result);
897
+ let translationOf;
898
+ if (entry.translationOf) {
899
+ const sourceId = seedIdMap.get(entry.translationOf);
900
+ if (!sourceId) console.warn(`content.${collectionSlug}: translationOf "${entry.translationOf}" not found (not yet created or missing). Skipping translation link.`);
901
+ else translationOf = sourceId;
902
+ }
903
+ const status = entry.status || "published";
904
+ const created = await contentRepo.create({
905
+ type: collectionSlug,
906
+ slug: entry.slug,
907
+ status,
908
+ data: resolvedData,
909
+ locale: entry.locale,
910
+ translationOf,
911
+ publishedAt: status === "published" ? (/* @__PURE__ */ new Date()).toISOString() : null
912
+ });
913
+ seedIdMap.set(entry.id, created.id);
914
+ result.content.created++;
915
+ await applyContentBylines(bylineRepo, collectionSlug, created.id, entry, seedBylineIdMap);
916
+ await applyContentTaxonomies(db, collectionSlug, created.id, entry, false);
917
+ }
918
+ }
919
+ if (seed.menus) for (const menu of seed.menus) {
920
+ const existingMenu = await db.selectFrom("_dineway_menus").selectAll().where("name", "=", menu.name).executeTakeFirst();
921
+ let menuId;
922
+ if (existingMenu) {
923
+ menuId = existingMenu.id;
924
+ await db.deleteFrom("_dineway_menu_items").where("menu_id", "=", menuId).execute();
925
+ } else {
926
+ menuId = ulid();
927
+ await db.insertInto("_dineway_menus").values({
928
+ id: menuId,
929
+ name: menu.name,
930
+ label: menu.label,
931
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
932
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
933
+ }).execute();
934
+ result.menus.created++;
935
+ }
936
+ const itemCount = await applyMenuItems(db, menuId, menu.items, null, 0, seedIdMap);
937
+ result.menus.items += itemCount;
938
+ }
939
+ if (seed.redirects) {
940
+ const redirectRepo = new RedirectRepository(db);
941
+ for (const redirect of seed.redirects) {
942
+ const existing = await redirectRepo.findBySource(redirect.source);
943
+ if (existing) {
944
+ if (onConflict === "error") throw new Error(`Conflict: redirect "${redirect.source}" already exists`);
945
+ if (onConflict === "update") {
946
+ await redirectRepo.update(existing.id, {
947
+ destination: redirect.destination,
948
+ type: redirect.type,
949
+ enabled: redirect.enabled,
950
+ groupName: redirect.groupName
951
+ });
952
+ result.redirects.updated++;
953
+ continue;
954
+ }
955
+ result.redirects.skipped++;
956
+ continue;
957
+ }
958
+ await redirectRepo.create({
959
+ source: redirect.source,
960
+ destination: redirect.destination,
961
+ type: redirect.type,
962
+ enabled: redirect.enabled,
963
+ groupName: redirect.groupName
964
+ });
965
+ result.redirects.created++;
966
+ }
967
+ }
968
+ if (seed.widgetAreas) for (const area of seed.widgetAreas) {
969
+ const existingArea = await db.selectFrom("_dineway_widget_areas").selectAll().where("name", "=", area.name).executeTakeFirst();
970
+ let areaId;
971
+ if (existingArea) {
972
+ areaId = existingArea.id;
973
+ await db.deleteFrom("_dineway_widgets").where("area_id", "=", areaId).execute();
974
+ } else {
975
+ areaId = ulid();
976
+ await db.insertInto("_dineway_widget_areas").values({
977
+ id: areaId,
978
+ name: area.name,
979
+ label: area.label,
980
+ description: area.description ?? null
981
+ }).execute();
982
+ result.widgetAreas.created++;
983
+ }
984
+ for (let i = 0; i < area.widgets.length; i++) {
985
+ const widget = area.widgets[i];
986
+ await applyWidget(db, areaId, widget, i);
987
+ result.widgetAreas.widgets++;
988
+ }
989
+ }
990
+ if (seed.sections) for (const section of seed.sections) {
991
+ const existing = await db.selectFrom("_dineway_sections").select("id").where("slug", "=", section.slug).executeTakeFirst();
992
+ if (existing) {
993
+ if (onConflict === "error") throw new Error(`Conflict: section "${section.slug}" already exists`);
994
+ if (onConflict === "update") {
995
+ await db.updateTable("_dineway_sections").set({
996
+ title: section.title,
997
+ description: section.description ?? null,
998
+ keywords: section.keywords ? JSON.stringify(section.keywords) : null,
999
+ content: JSON.stringify(section.content),
1000
+ source: section.source || "theme",
1001
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
1002
+ }).where("id", "=", existing.id).execute();
1003
+ result.sections.updated++;
1004
+ continue;
1005
+ }
1006
+ result.sections.skipped++;
1007
+ continue;
1008
+ }
1009
+ const id = ulid();
1010
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1011
+ await db.insertInto("_dineway_sections").values({
1012
+ id,
1013
+ slug: section.slug,
1014
+ title: section.title,
1015
+ description: section.description ?? null,
1016
+ keywords: section.keywords ? JSON.stringify(section.keywords) : null,
1017
+ content: JSON.stringify(section.content),
1018
+ preview_media_id: null,
1019
+ source: section.source || "theme",
1020
+ theme_id: section.source === "theme" ? section.slug : null,
1021
+ created_at: now,
1022
+ updated_at: now
1023
+ }).execute();
1024
+ result.sections.created++;
1025
+ }
1026
+ if (seed.collections) {
1027
+ const ftsManager = new FTSManager(db);
1028
+ for (const collection of seed.collections) if (collection.supports?.includes("search")) {
1029
+ if ((await ftsManager.getSearchableFields(collection.slug)).length > 0) try {
1030
+ await ftsManager.enableSearch(collection.slug);
1031
+ } catch (err) {
1032
+ console.warn(`Failed to enable search for ${collection.slug}:`, err);
1033
+ }
1034
+ }
1035
+ }
1036
+ return result;
1037
+ }
1038
+ /**
1039
+ * Apply hierarchical taxonomy terms (parents before children)
1040
+ */
1041
+ async function applyHierarchicalTerms(termRepo, taxonomyName, terms, result, onConflict = "skip") {
1042
+ const slugToId = /* @__PURE__ */ new Map();
1043
+ let remaining = [...terms];
1044
+ let maxPasses = 10;
1045
+ while (remaining.length > 0 && maxPasses > 0) {
1046
+ const processedThisPass = [];
1047
+ for (const term of remaining) if (!term.parent || slugToId.has(term.parent)) {
1048
+ const parentId = term.parent ? slugToId.get(term.parent) : void 0;
1049
+ const existing = await termRepo.findBySlug(taxonomyName, term.slug);
1050
+ if (existing) {
1051
+ if (onConflict === "error") throw new Error(`Conflict: taxonomy term "${term.slug}" in "${taxonomyName}" already exists`);
1052
+ if (onConflict === "update") {
1053
+ await termRepo.update(existing.id, {
1054
+ label: term.label,
1055
+ parentId,
1056
+ data: term.description ? { description: term.description } : {}
1057
+ });
1058
+ result.taxonomies.terms++;
1059
+ }
1060
+ slugToId.set(term.slug, existing.id);
1061
+ } else {
1062
+ const created = await termRepo.create({
1063
+ name: taxonomyName,
1064
+ slug: term.slug,
1065
+ label: term.label,
1066
+ parentId,
1067
+ data: term.description ? { description: term.description } : void 0
1068
+ });
1069
+ slugToId.set(term.slug, created.id);
1070
+ result.taxonomies.terms++;
1071
+ }
1072
+ processedThisPass.push(term.slug);
1073
+ }
1074
+ remaining = remaining.filter((t) => !processedThisPass.includes(t.slug));
1075
+ maxPasses--;
1076
+ }
1077
+ if (remaining.length > 0) console.warn(`Could not process ${remaining.length} terms due to missing parents`);
1078
+ }
1079
+ /**
1080
+ * Apply byline credits to a content entry.
1081
+ * In update mode, clears existing credits even if the seed has none.
1082
+ */
1083
+ async function applyContentBylines(bylineRepo, collectionSlug, contentId, entry, seedBylineIdMap, isUpdate = false) {
1084
+ if (!entry.bylines || entry.bylines.length === 0) {
1085
+ if (isUpdate) await bylineRepo.setContentBylines(collectionSlug, contentId, []);
1086
+ return;
1087
+ }
1088
+ const credits = entry.bylines.map((credit) => {
1089
+ const bylineId = seedBylineIdMap.get(credit.byline);
1090
+ if (!bylineId) return null;
1091
+ return {
1092
+ bylineId,
1093
+ roleLabel: credit.roleLabel ?? null
1094
+ };
1095
+ }).filter((credit) => Boolean(credit));
1096
+ if (credits.length !== entry.bylines.length) console.warn(`content.${collectionSlug}.${entry.slug}: one or more byline refs could not be resolved`);
1097
+ if (credits.length > 0 || isUpdate) await bylineRepo.setContentBylines(collectionSlug, contentId, credits);
1098
+ }
1099
+ /**
1100
+ * Apply taxonomy term assignments to a content entry.
1101
+ * In update mode, clears existing assignments before re-attaching.
1102
+ */
1103
+ async function applyContentTaxonomies(db, collectionSlug, contentId, entry, isUpdate) {
1104
+ if (isUpdate) await db.deleteFrom("content_taxonomies").where("collection", "=", collectionSlug).where("entry_id", "=", contentId).execute();
1105
+ if (!entry.taxonomies) return;
1106
+ for (const [taxonomyName, termSlugs] of Object.entries(entry.taxonomies)) {
1107
+ const termRepo = new TaxonomyRepository(db);
1108
+ for (const termSlug of termSlugs) {
1109
+ const term = await termRepo.findBySlug(taxonomyName, termSlug);
1110
+ if (term) await termRepo.attachToEntry(collectionSlug, contentId, term.id);
1111
+ }
1112
+ }
1113
+ }
1114
+ /**
1115
+ * Apply menu items recursively
1116
+ */
1117
+ async function applyMenuItems(db, menuId, items, parentId, startOrder, seedIdMap) {
1118
+ let count = 0;
1119
+ let order = startOrder;
1120
+ for (const item of items) {
1121
+ const itemId = ulid();
1122
+ let referenceId = null;
1123
+ let referenceCollection = null;
1124
+ if (item.type === "page" || item.type === "post") {
1125
+ if (item.ref && seedIdMap.has(item.ref)) {
1126
+ referenceId = seedIdMap.get(item.ref);
1127
+ referenceCollection = item.collection || `${item.type}s`;
1128
+ }
1129
+ }
1130
+ await db.insertInto("_dineway_menu_items").values({
1131
+ id: itemId,
1132
+ menu_id: menuId,
1133
+ parent_id: parentId,
1134
+ sort_order: order,
1135
+ type: item.type,
1136
+ reference_collection: referenceCollection,
1137
+ reference_id: referenceId,
1138
+ custom_url: item.url ?? null,
1139
+ label: item.label || "",
1140
+ title_attr: item.titleAttr ?? null,
1141
+ target: item.target ?? null,
1142
+ css_classes: item.cssClasses ?? null,
1143
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
1144
+ }).execute();
1145
+ count++;
1146
+ order++;
1147
+ if (item.children && item.children.length > 0) {
1148
+ const childCount = await applyMenuItems(db, menuId, item.children, itemId, 0, seedIdMap);
1149
+ count += childCount;
1150
+ }
1151
+ }
1152
+ return count;
1153
+ }
1154
+ /**
1155
+ * Apply a widget
1156
+ */
1157
+ async function applyWidget(db, areaId, widget, sortOrder) {
1158
+ await db.insertInto("_dineway_widgets").values({
1159
+ id: ulid(),
1160
+ area_id: areaId,
1161
+ sort_order: sortOrder,
1162
+ type: widget.type,
1163
+ title: widget.title ?? null,
1164
+ content: widget.content ? JSON.stringify(widget.content) : null,
1165
+ menu_name: widget.menuName ?? null,
1166
+ component_id: widget.componentId ?? null,
1167
+ component_props: widget.props ? JSON.stringify(widget.props) : null
1168
+ }).execute();
1169
+ }
1170
+ /**
1171
+ * Type guard for $media reference
1172
+ */
1173
+ function isSeedMediaReference(value) {
1174
+ if (typeof value !== "object" || value === null || !("$media" in value)) return false;
1175
+ const media = value.$media;
1176
+ return typeof media === "object" && media !== null && "url" in media && typeof media.url === "string";
1177
+ }
1178
+ /**
1179
+ * Resolve $ref: and $media references in content data
1180
+ */
1181
+ async function resolveReferences(data, seedIdMap, mediaContext, result) {
1182
+ const resolved = {};
1183
+ for (const [key, value] of Object.entries(data)) resolved[key] = await resolveValue(value, seedIdMap, mediaContext, result);
1184
+ return resolved;
1185
+ }
1186
+ /**
1187
+ * Resolve a single value recursively
1188
+ */
1189
+ async function resolveValue(value, seedIdMap, mediaContext, result) {
1190
+ if (typeof value === "string" && value.startsWith("$ref:")) {
1191
+ const seedId = value.slice(5);
1192
+ return seedIdMap.get(seedId) ?? value;
1193
+ }
1194
+ if (isSeedMediaReference(value)) return resolveMedia(value, mediaContext, result);
1195
+ if (Array.isArray(value)) return Promise.all(value.map((item) => resolveValue(item, seedIdMap, mediaContext, result)));
1196
+ if (typeof value === "object" && value !== null) {
1197
+ const resolved = {};
1198
+ for (const [k, v] of Object.entries(value)) resolved[k] = await resolveValue(v, seedIdMap, mediaContext, result);
1199
+ return resolved;
1200
+ }
1201
+ return value;
1202
+ }
1203
+ /**
1204
+ * Resolve a $media reference by downloading and uploading the media
1205
+ */
1206
+ async function resolveMedia(ref, ctx, result) {
1207
+ const { url, alt, filename, caption } = ref.$media;
1208
+ const cached = ctx.mediaCache.get(url);
1209
+ if (cached) {
1210
+ result.media.skipped++;
1211
+ return {
1212
+ ...cached,
1213
+ alt: alt ?? cached.alt
1214
+ };
1215
+ }
1216
+ if (ctx.skipMediaDownload) {
1217
+ const mediaValue = {
1218
+ provider: "external",
1219
+ id: ulid(),
1220
+ src: url,
1221
+ alt: alt ?? void 0,
1222
+ filename: filename ?? void 0
1223
+ };
1224
+ ctx.mediaCache.set(url, mediaValue);
1225
+ result.media.created++;
1226
+ return mediaValue;
1227
+ }
1228
+ if (!ctx.storage) {
1229
+ console.warn(`Skipping $media reference (no storage configured): ${url}`);
1230
+ result.media.skipped++;
1231
+ return null;
1232
+ }
1233
+ try {
1234
+ validateExternalUrl(url);
1235
+ console.log(` 📥 Downloading: ${url}`);
1236
+ const response = await ssrfSafeFetch(url, { headers: { "User-Agent": "Dineway-AI/1.0" } });
1237
+ if (!response.ok) {
1238
+ console.warn(` ⚠️ Failed to download ${url}: ${response.status}`);
1239
+ result.media.skipped++;
1240
+ return null;
1241
+ }
1242
+ const contentType = response.headers.get("content-type") || "application/octet-stream";
1243
+ const ext = getExtensionFromContentType(contentType) || getExtensionFromUrl(url) || ".bin";
1244
+ const id = ulid();
1245
+ const finalFilename = filename || generateFilename(url, ext);
1246
+ const storageKey = `${id}${ext}`;
1247
+ const arrayBuffer = await response.arrayBuffer();
1248
+ const body = new Uint8Array(arrayBuffer);
1249
+ let width;
1250
+ let height;
1251
+ if (contentType.startsWith("image/")) {
1252
+ const dimensions = getImageDimensions(body);
1253
+ width = dimensions?.width;
1254
+ height = dimensions?.height;
1255
+ }
1256
+ await ctx.storage.upload({
1257
+ key: storageKey,
1258
+ body,
1259
+ contentType
1260
+ });
1261
+ await new MediaRepository(ctx.db).create({
1262
+ filename: finalFilename,
1263
+ mimeType: contentType,
1264
+ size: body.length,
1265
+ width,
1266
+ height,
1267
+ alt,
1268
+ caption,
1269
+ storageKey,
1270
+ status: "ready"
1271
+ });
1272
+ const mediaValue = {
1273
+ provider: "local",
1274
+ id,
1275
+ alt: alt ?? void 0,
1276
+ width,
1277
+ height,
1278
+ mimeType: contentType,
1279
+ filename: finalFilename,
1280
+ meta: { storageKey }
1281
+ };
1282
+ ctx.mediaCache.set(url, mediaValue);
1283
+ result.media.created++;
1284
+ console.log(` ✅ Uploaded: ${finalFilename}`);
1285
+ return mediaValue;
1286
+ } catch (error) {
1287
+ console.warn(` ⚠️ Error processing $media ${url}:`, error instanceof Error ? error.message : error);
1288
+ result.media.skipped++;
1289
+ return null;
1290
+ }
1291
+ }
1292
+ /**
1293
+ * Get file extension from content type
1294
+ */
1295
+ function getExtensionFromContentType(contentType) {
1296
+ const baseMime = contentType.split(";")[0].trim();
1297
+ const ext = mime.getExtension(baseMime);
1298
+ return ext ? `.${ext}` : null;
1299
+ }
1300
+ /**
1301
+ * Get file extension from URL
1302
+ */
1303
+ function getExtensionFromUrl(url) {
1304
+ try {
1305
+ const match = new URL(url).pathname.match(FILE_EXTENSION_PATTERN);
1306
+ return match ? `.${match[1]}` : null;
1307
+ } catch {
1308
+ return null;
1309
+ }
1310
+ }
1311
+ /**
1312
+ * Generate a filename from URL
1313
+ */
1314
+ function generateFilename(url, ext) {
1315
+ try {
1316
+ return `${(new URL(url).pathname.split("/").pop() || "media").replace(EXTENSION_PATTERN, "").replace(QUERY_PARAM_PATTERN, "").replace(SANITIZE_PATTERN, "-").replace(MULTIPLE_HYPHENS_PATTERN, "-") || "media"}${ext}`;
1317
+ } catch {
1318
+ return `media${ext}`;
1319
+ }
1320
+ }
1321
+ /**
1322
+ * Get image dimensions from buffer using image-size.
1323
+ * Supports PNG, JPEG, GIF, WebP, AVIF, SVG, TIFF, and more.
1324
+ */
1325
+ function getImageDimensions(buffer) {
1326
+ try {
1327
+ const result = imageSize(buffer);
1328
+ if (result.width != null && result.height != null) return {
1329
+ width: result.width,
1330
+ height: result.height
1331
+ };
1332
+ return null;
1333
+ } catch {
1334
+ return null;
1335
+ }
1336
+ }
1337
+
1338
+ //#endregion
1339
+ export { stripCredentialHeaders as a, getPluginSettings as c, setSiteSettings as d, OptionsRepository as f, ssrfSafeFetch as i, getSiteSetting as l, apply_exports as n, validateExternalUrl as o, TaxonomyRepository as p, SsrfError as r, getPluginSetting as s, applySeed as t, getSiteSettings as u };