@fastly/compute-js-static-publish 8.0.1 → 8.1.0-beta.2

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 (247) hide show
  1. package/DOCUMENTATION.md +792 -35
  2. package/README.md +13 -3
  3. package/README.short.md +13 -3
  4. package/build/cli/cli/commands/manage/clean.d.ts.map +1 -1
  5. package/build/cli/cli/commands/manage/clean.js +14 -8
  6. package/build/cli/cli/commands/manage/collections/delete.d.ts.map +1 -1
  7. package/build/cli/cli/commands/manage/collections/delete.js +32 -15
  8. package/build/cli/cli/commands/manage/collections/index.d.ts.map +1 -1
  9. package/build/cli/cli/commands/manage/collections/index.js +10 -4
  10. package/build/cli/cli/commands/manage/collections/list.d.ts.map +1 -1
  11. package/build/cli/cli/commands/manage/collections/list.js +49 -8
  12. package/build/cli/cli/commands/manage/collections/promote.d.ts.map +1 -1
  13. package/build/cli/cli/commands/manage/collections/promote.js +69 -21
  14. package/build/cli/cli/commands/manage/collections/update-expiration.d.ts.map +1 -1
  15. package/build/cli/cli/commands/manage/collections/update-expiration.js +12 -12
  16. package/build/cli/cli/commands/manage/deploy.d.ts +2 -0
  17. package/build/cli/cli/commands/manage/deploy.d.ts.map +1 -0
  18. package/build/cli/cli/commands/manage/deploy.js +194 -0
  19. package/build/cli/cli/commands/manage/dev.d.ts +2 -0
  20. package/build/cli/cli/commands/manage/dev.d.ts.map +1 -0
  21. package/build/cli/cli/commands/manage/dev.js +78 -0
  22. package/build/cli/cli/commands/manage/eject.d.ts +2 -0
  23. package/build/cli/cli/commands/manage/eject.d.ts.map +1 -0
  24. package/build/cli/cli/commands/manage/eject.js +155 -0
  25. package/build/cli/cli/commands/manage/index.d.ts.map +1 -1
  26. package/build/cli/cli/commands/manage/index.js +42 -5
  27. package/build/cli/cli/commands/manage/publish-content.d.ts.map +1 -1
  28. package/build/cli/cli/commands/manage/publish-content.js +83 -37
  29. package/build/cli/cli/commands/manage/status.d.ts +2 -0
  30. package/build/cli/cli/commands/manage/status.d.ts.map +1 -0
  31. package/build/cli/cli/commands/manage/status.js +162 -0
  32. package/build/cli/cli/commands/manage/template/index.d.ts +2 -0
  33. package/build/cli/cli/commands/manage/template/index.d.ts.map +1 -0
  34. package/build/cli/cli/commands/manage/template/index.js +43 -0
  35. package/build/cli/cli/commands/manage/template/init.d.ts +13 -0
  36. package/build/cli/cli/commands/manage/template/init.d.ts.map +1 -0
  37. package/build/cli/cli/commands/manage/template/init.js +172 -0
  38. package/build/cli/cli/commands/manage/upgrade.d.ts +2 -0
  39. package/build/cli/cli/commands/manage/upgrade.d.ts.map +1 -0
  40. package/build/cli/cli/commands/manage/upgrade.js +59 -0
  41. package/build/cli/cli/commands/run-dev-watch.d.ts +8 -0
  42. package/build/cli/cli/commands/run-dev-watch.d.ts.map +1 -0
  43. package/build/cli/cli/commands/run-dev-watch.js +41 -0
  44. package/build/cli/cli/commands/run-template-hook.d.ts +2 -0
  45. package/build/cli/cli/commands/run-template-hook.d.ts.map +1 -0
  46. package/build/cli/cli/commands/run-template-hook.js +28 -0
  47. package/build/cli/cli/commands/scaffold/app-template.d.ts +17 -0
  48. package/build/cli/cli/commands/scaffold/app-template.d.ts.map +1 -0
  49. package/build/cli/cli/commands/scaffold/app-template.js +54 -0
  50. package/build/cli/cli/commands/scaffold/fastly-toml-sections.d.ts +5 -0
  51. package/build/cli/cli/commands/scaffold/fastly-toml-sections.d.ts.map +1 -0
  52. package/build/cli/cli/commands/scaffold/fastly-toml-sections.js +154 -0
  53. package/build/cli/cli/commands/scaffold/generate-app.d.ts +47 -0
  54. package/build/cli/cli/commands/scaffold/generate-app.d.ts.map +1 -0
  55. package/build/cli/cli/commands/scaffold/generate-app.js +164 -0
  56. package/build/cli/cli/commands/scaffold/index.d.ts +6 -0
  57. package/build/cli/cli/commands/scaffold/index.d.ts.map +1 -1
  58. package/build/cli/cli/commands/scaffold/index.js +208 -290
  59. package/build/cli/cli/commands/scaffold/render.d.ts +7 -0
  60. package/build/cli/cli/commands/scaffold/render.d.ts.map +1 -0
  61. package/build/cli/cli/commands/scaffold/render.js +72 -0
  62. package/build/cli/cli/hono/build.d.ts +43 -0
  63. package/build/cli/cli/hono/build.d.ts.map +1 -0
  64. package/build/cli/cli/hono/build.js +298 -0
  65. package/build/cli/cli/index.js +98 -5
  66. package/build/cli/cli/managed/app.d.ts +21 -0
  67. package/build/cli/cli/managed/app.d.ts.map +1 -0
  68. package/build/cli/cli/managed/app.js +162 -0
  69. package/build/cli/cli/managed/command-helpers.d.ts +6 -0
  70. package/build/cli/cli/managed/command-helpers.d.ts.map +1 -0
  71. package/build/cli/cli/managed/command-helpers.js +81 -0
  72. package/build/cli/cli/managed/eject.d.ts +6 -0
  73. package/build/cli/cli/managed/eject.d.ts.map +1 -0
  74. package/build/cli/cli/managed/eject.js +68 -0
  75. package/build/cli/cli/managed/fastly-service.d.ts +8 -0
  76. package/build/cli/cli/managed/fastly-service.d.ts.map +1 -0
  77. package/build/cli/cli/managed/fastly-service.js +21 -0
  78. package/build/cli/cli/managed/fingerprint.d.ts +13 -0
  79. package/build/cli/cli/managed/fingerprint.d.ts.map +1 -0
  80. package/build/cli/cli/managed/fingerprint.js +60 -0
  81. package/build/cli/cli/managed/lockfile.d.ts +31 -0
  82. package/build/cli/cli/managed/lockfile.d.ts.map +1 -0
  83. package/build/cli/cli/managed/lockfile.js +62 -0
  84. package/build/cli/cli/managed/project-edit.d.ts +3 -0
  85. package/build/cli/cli/managed/project-edit.d.ts.map +1 -0
  86. package/build/cli/cli/managed/project-edit.js +35 -0
  87. package/build/cli/cli/managed/project-file.d.ts +16 -0
  88. package/build/cli/cli/managed/project-file.d.ts.map +1 -0
  89. package/build/cli/cli/managed/project-file.js +119 -0
  90. package/build/cli/cli/managed/template-init.d.ts +12 -0
  91. package/build/cli/cli/managed/template-init.d.ts.map +1 -0
  92. package/build/cli/cli/managed/template-init.js +120 -0
  93. package/build/cli/cli/managed/version-check.d.ts +25 -0
  94. package/build/cli/cli/managed/version-check.d.ts.map +1 -0
  95. package/build/cli/cli/managed/version-check.js +84 -0
  96. package/build/cli/cli/storage/kv-store-local-provider.d.ts +1 -0
  97. package/build/cli/cli/storage/kv-store-local-provider.d.ts.map +1 -1
  98. package/build/cli/cli/storage/kv-store-local-provider.js +6 -3
  99. package/build/cli/cli/storage/kv-store-provider.d.ts +1 -0
  100. package/build/cli/cli/storage/kv-store-provider.d.ts.map +1 -1
  101. package/build/cli/cli/storage/kv-store-provider.js +9 -1
  102. package/build/cli/cli/storage/s3-storage-provider.d.ts +1 -0
  103. package/build/cli/cli/storage/s3-storage-provider.d.ts.map +1 -1
  104. package/build/cli/cli/storage/s3-storage-provider.js +4 -0
  105. package/build/cli/cli/storage/storage-provider.d.ts +3 -0
  106. package/build/cli/cli/storage/storage-provider.d.ts.map +1 -1
  107. package/build/cli/cli/storage/storage-provider.js +32 -0
  108. package/build/cli/cli/util/api-token.d.ts.map +1 -1
  109. package/build/cli/cli/util/collection-name.d.ts +3 -0
  110. package/build/cli/cli/util/collection-name.d.ts.map +1 -0
  111. package/build/cli/cli/util/collection-name.js +24 -0
  112. package/build/cli/cli/util/collection-selection-flags.d.ts +13 -0
  113. package/build/cli/cli/util/collection-selection-flags.d.ts.map +1 -0
  114. package/build/cli/cli/util/collection-selection-flags.js +131 -0
  115. package/build/cli/cli/util/config.d.ts +13 -0
  116. package/build/cli/cli/util/config.d.ts.map +1 -1
  117. package/build/cli/cli/util/config.js +172 -38
  118. package/build/cli/cli/util/data.d.ts.map +1 -1
  119. package/build/cli/cli/util/global-options.d.ts +13 -0
  120. package/build/cli/cli/util/global-options.d.ts.map +1 -0
  121. package/build/cli/cli/util/global-options.js +53 -0
  122. package/build/cli/cli/util/kv-store-items.d.ts +1 -0
  123. package/build/cli/cli/util/kv-store-items.d.ts.map +1 -1
  124. package/build/cli/cli/util/kv-store-items.js +16 -40
  125. package/build/cli/cli/util/kv-store-local-server.d.ts +1 -1
  126. package/build/cli/cli/util/kv-store-local-server.d.ts.map +1 -1
  127. package/build/cli/cli/util/kv-store-local-server.js +4 -2
  128. package/build/cli/cli/util/output.d.ts +31 -0
  129. package/build/cli/cli/util/output.d.ts.map +1 -0
  130. package/build/cli/cli/util/output.js +139 -0
  131. package/build/cli/cli/util/package.d.ts +6 -0
  132. package/build/cli/cli/util/package.d.ts.map +1 -1
  133. package/build/cli/cli/util/package.js +23 -0
  134. package/build/cli/cli/util/progress.d.ts.map +1 -1
  135. package/build/cli/cli/util/purge.d.ts +6 -2
  136. package/build/cli/cli/util/purge.d.ts.map +1 -1
  137. package/build/cli/cli/util/purge.js +7 -2
  138. package/build/cli/cli/util/resources.d.ts +2 -0
  139. package/build/cli/cli/util/resources.d.ts.map +1 -0
  140. package/build/cli/cli/util/resources.js +8 -0
  141. package/build/cli/cli/util/service-id.d.ts +1 -0
  142. package/build/cli/cli/util/service-id.d.ts.map +1 -1
  143. package/build/cli/cli/util/service-id.js +5 -1
  144. package/build/cli/cli/util/variants.d.ts.map +1 -1
  145. package/build/cli/models/assets/index.d.ts +6 -0
  146. package/build/cli/models/assets/index.d.ts.map +1 -1
  147. package/build/cli/models/assets/index.js +24 -0
  148. package/build/cli/models/collections/index.d.ts +3 -0
  149. package/build/cli/models/collections/index.d.ts.map +1 -0
  150. package/build/cli/models/collections/index.js +15 -0
  151. package/build/cli/models/compression/index.d.ts +1 -1
  152. package/build/cli/models/compression/index.d.ts.map +1 -1
  153. package/build/cli/models/config/collection-selection.d.ts +55 -0
  154. package/build/cli/models/config/collection-selection.d.ts.map +1 -0
  155. package/build/cli/models/config/collection-selection.js +238 -0
  156. package/build/cli/models/config/item-patterns.d.ts +4 -0
  157. package/build/cli/models/config/item-patterns.d.ts.map +1 -0
  158. package/build/cli/models/config/item-patterns.js +43 -0
  159. package/build/cli/models/config/publish-content-config.d.ts +6 -1
  160. package/build/cli/models/config/publish-content-config.d.ts.map +1 -1
  161. package/build/cli/models/config/static-publish-project.d.ts +23 -0
  162. package/build/cli/models/config/static-publish-project.d.ts.map +1 -0
  163. package/build/cli/models/config/static-publish-project.js +11 -0
  164. package/build/cli/models/config/static-publish-rc.d.ts +25 -0
  165. package/build/cli/models/config/static-publish-rc.d.ts.map +1 -1
  166. package/build/cli/models/config/static-publish-rc.js +31 -0
  167. package/build/cli/models/config/template-settings.d.ts +10 -0
  168. package/build/cli/models/config/template-settings.d.ts.map +1 -0
  169. package/build/cli/models/config/template-settings.js +72 -0
  170. package/build/cli/models/server/index.d.ts.map +1 -1
  171. package/build/cli/models/server/index.js +6 -2
  172. package/build/server/hono/extensions.d.ts +48 -0
  173. package/build/server/hono/extensions.d.ts.map +1 -0
  174. package/build/server/hono/extensions.js +65 -0
  175. package/build/server/hono/index.d.ts +30 -0
  176. package/build/server/hono/index.d.ts.map +1 -0
  177. package/build/server/hono/index.js +72 -0
  178. package/build/server/hono/path.d.ts +7 -0
  179. package/build/server/hono/path.d.ts.map +1 -0
  180. package/build/server/hono/path.js +32 -0
  181. package/build/server/models/assets/index.d.ts +6 -0
  182. package/build/server/models/assets/index.d.ts.map +1 -1
  183. package/build/server/models/assets/index.js +24 -0
  184. package/build/server/models/collections/index.d.ts +3 -0
  185. package/build/server/models/collections/index.d.ts.map +1 -0
  186. package/build/server/models/collections/index.js +15 -0
  187. package/build/server/models/compression/index.d.ts +1 -1
  188. package/build/server/models/compression/index.d.ts.map +1 -1
  189. package/build/server/models/config/collection-selection.d.ts +55 -0
  190. package/build/server/models/config/collection-selection.d.ts.map +1 -0
  191. package/build/server/models/config/collection-selection.js +238 -0
  192. package/build/server/models/config/item-patterns.d.ts +4 -0
  193. package/build/server/models/config/item-patterns.d.ts.map +1 -0
  194. package/build/server/models/config/item-patterns.js +43 -0
  195. package/build/server/models/config/publish-content-config.d.ts +6 -1
  196. package/build/server/models/config/publish-content-config.d.ts.map +1 -1
  197. package/build/server/models/config/static-publish-project.d.ts +23 -0
  198. package/build/server/models/config/static-publish-project.d.ts.map +1 -0
  199. package/build/server/models/config/static-publish-project.js +11 -0
  200. package/build/server/models/config/static-publish-rc.d.ts +25 -0
  201. package/build/server/models/config/static-publish-rc.d.ts.map +1 -1
  202. package/build/server/models/config/static-publish-rc.js +31 -0
  203. package/build/server/models/server/index.d.ts.map +1 -1
  204. package/build/server/models/server/index.js +6 -2
  205. package/build/server/server/collection-selection/index.d.ts +11 -0
  206. package/build/server/server/collection-selection/index.d.ts.map +1 -0
  207. package/build/server/server/collection-selection/index.js +192 -0
  208. package/build/server/server/collection-selector/from-cookie.d.ts.map +1 -1
  209. package/build/server/server/collection-selector/from-cookie.js +6 -8
  210. package/build/server/server/collection-selector/from-request.d.ts.map +1 -1
  211. package/build/server/server/index.d.ts +4 -1
  212. package/build/server/server/index.d.ts.map +1 -1
  213. package/build/server/server/index.js +3 -1
  214. package/build/server/server/publisher-server/accept-html.d.ts +2 -0
  215. package/build/server/server/publisher-server/accept-html.d.ts.map +1 -0
  216. package/build/server/server/publisher-server/accept-html.js +50 -0
  217. package/build/server/server/publisher-server/index.d.ts +59 -7
  218. package/build/server/server/publisher-server/index.d.ts.map +1 -1
  219. package/build/server/server/publisher-server/index.js +362 -145
  220. package/build/server/server/publisher-server/response-cache.d.ts +3 -0
  221. package/build/server/server/publisher-server/response-cache.d.ts.map +1 -1
  222. package/build/server/server/publisher-server/response-cache.js +24 -1
  223. package/build/server/server/storage/kv-store-provider.d.ts.map +1 -1
  224. package/build/server/server/storage/s3-storage-provider.d.ts.map +1 -1
  225. package/build/server/server/storage/storage-provider.d.ts.map +1 -1
  226. package/build/server/server/util/body-length.d.ts.map +1 -1
  227. package/build/server/server/util/server-timing.d.ts.map +1 -1
  228. package/package.json +21 -3
  229. package/resources/app/fastly.toml.njk +58 -0
  230. package/resources/app/gitignore.njk +9 -0
  231. package/resources/app/publish-content.config.from-project.js.njk +9 -0
  232. package/resources/app/publish-content.config.js.njk +35 -0
  233. package/resources/app/static-publish.project.js.njk +83 -0
  234. package/resources/app/static-publish.rc.js.njk +43 -0
  235. package/resources/template-init/hono/_example.ts +22 -0
  236. package/resources/template-init/hono/tsconfig.json +17 -0
  237. package/resources/templates/hono/package-lock.json +4123 -0
  238. package/resources/templates/hono/package.json +15 -0
  239. package/resources/templates/hono/scripts/template_postinstall.js +10 -0
  240. package/resources/templates/hono/scripts/template_prebuild.js +8 -0
  241. package/resources/templates/hono/scripts/template_watch.js +15 -0
  242. package/resources/templates/hono/src/hono-extensions.generated.d.ts +5 -0
  243. package/resources/templates/hono/src/index.ts +53 -0
  244. package/resources/templates/hono/tsconfig.json +17 -0
  245. package/resources/templates/plain/package-lock.json +4088 -0
  246. package/resources/templates/plain/package.json +10 -0
  247. package/resources/templates/plain/src/index.js +34 -0
@@ -0,0 +1,65 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ // The phases of the app, in the order that the app runs them:
6
+ // - middleware: before all other phases, for example a login check.
7
+ // - beforeFiles: before the published files.
8
+ // - afterFiles: after the published files, only for the requests that no
9
+ // published file matches.
10
+ export const EXTENSION_PHASES = ['middleware', 'beforeFiles', 'afterFiles'];
11
+ export function defineExtension(optionsOrPhases, phases) {
12
+ const [options, value] = phases === undefined ?
13
+ [{}, optionsOrPhases] :
14
+ [optionsOrPhases, phases];
15
+ return {
16
+ kind: 'extension',
17
+ bindings: { ...options.bindings },
18
+ phases: typeof value === 'function' ? { middleware: value } : value,
19
+ };
20
+ }
21
+ const isPlainObject = (value) => typeof value === 'object' && value != null && !Array.isArray(value);
22
+ // Checks the modules of the manifest, and merges their bindings with the
23
+ // bindings of the app (templateOptions.hono.bindings), for buildFire(). The
24
+ // same name with the same resource is allowed. Throws for a module that does not
25
+ // default-export defineExtension(…), for phases that are not correct, or for
26
+ // one name with two different resources; the message names the file. Call it at
27
+ // the top level of the app, so that an error stops the build.
28
+ export function mergeBindings(appBindings, entries) {
29
+ const merged = {};
30
+ const sourceOf = {};
31
+ const add = (bindings, source) => {
32
+ for (const [name, resource] of Object.entries(bindings)) {
33
+ if (name in merged && merged[name] !== resource) {
34
+ throw new Error(`The binding '${name}' is '${merged[name]}' in ${sourceOf[name]}, ` +
35
+ `but '${resource}' in ${source}. Use the same resource, or another name.`);
36
+ }
37
+ merged[name] = resource;
38
+ sourceOf[name] ??= source;
39
+ }
40
+ };
41
+ add(appBindings ?? {}, 'templateOptions.hono.bindings');
42
+ for (const entry of entries) {
43
+ const source = `hono-extensions/${entry.file}`;
44
+ const module = entry.module;
45
+ if (module == null || module.kind !== 'extension') {
46
+ throw new Error(`${source}: the default export must be defineExtension(…).`);
47
+ }
48
+ const phases = module.phases;
49
+ const keys = isPlainObject(phases) ? Object.keys(phases) : [];
50
+ const unknown = keys.filter(key => !EXTENSION_PHASES.includes(key));
51
+ if (!isPlainObject(phases) || keys.length === 0 || unknown.length > 0) {
52
+ throw new Error(`${source}: defineExtension() needs a function, or an object with a function ` +
53
+ `for one or more of the phases ${EXTENSION_PHASES.join(', ')}.` +
54
+ (unknown.length === 1 ? ` '${unknown[0]}' is not a phase.` : '') +
55
+ (unknown.length > 1 ? ` ${unknown.map(x => `'${x}'`).join(', ')} are not phases.` : ''));
56
+ }
57
+ for (const key of keys) {
58
+ if (typeof phases[key] !== 'function') {
59
+ throw new Error(`${source}: the phase ${key} must be a function.`);
60
+ }
61
+ }
62
+ add(module.bindings ?? {}, source);
63
+ }
64
+ return merged;
65
+ }
@@ -0,0 +1,30 @@
1
+ import type { Context, MiddlewareHandler, NotFoundHandler } from 'hono';
2
+ import { PublisherServer, type StaticPublishRc } from '../server/index.js';
3
+ import { type ServeStaticPathOptions } from './path.js';
4
+ export { type BindingResource, type DefineExtensionOptions, EXTENSION_PHASES, type Extension, type ExtensionBindings, type ExtensionEntry, type ExtensionEnv, type ExtensionPhase, type ExtensionPhaseFunction, type ExtensionPhases, defineExtension, mergeBindings, } from './extensions.js';
5
+ export type StaticPublisherVariables = {
6
+ staticPublisher: {
7
+ publisherServer: PublisherServer;
8
+ collectionName: string | null;
9
+ pathname?: string;
10
+ };
11
+ };
12
+ export type CollectionNameOption = string | null | ((c: Context) => string | null | undefined);
13
+ export type ServeStaticOptions = ServeStaticPathOptions<Context> & {
14
+ collectionName?: CollectionNameOption;
15
+ };
16
+ export type ServeFallbackOptions = {
17
+ collectionName?: CollectionNameOption;
18
+ };
19
+ export type PublisherServerMiddlewareDefaults = {
20
+ collectionName?: CollectionNameOption;
21
+ };
22
+ export type PublisherServerMiddleware = {
23
+ publisherServer: PublisherServer;
24
+ serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
25
+ serveFallback: (options?: ServeFallbackOptions) => NotFoundHandler;
26
+ selectCollection: () => MiddlewareHandler;
27
+ };
28
+ export declare function fromPublisherServer(server: PublisherServer, defaults?: PublisherServerMiddlewareDefaults): PublisherServerMiddleware;
29
+ export declare function fromStaticPublishRc(rc: StaticPublishRc, defaults?: PublisherServerMiddlewareDefaults): PublisherServerMiddleware;
30
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hono/index.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAGxE,OAAO,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,KAAK,sBAAsB,EAA8B,MAAM,WAAW,CAAC;AAEpF,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC3B,gBAAgB,EAChB,KAAK,SAAS,EACd,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,sBAAsB,EAC3B,KAAK,eAAe,EACpB,eAAe,EACf,aAAa,GACd,MAAM,iBAAiB,CAAC;AAIzB,MAAM,MAAM,wBAAwB,GAAG;IACrC,eAAe,EAAE;QACf,eAAe,EAAE,eAAe,CAAC;QAGjC,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;QAE9B,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,CAAC;CACH,CAAC;AAMF,MAAM,MAAM,oBAAoB,GAAG,MAAM,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,EAAE,OAAO,KAAK,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;AAE/F,MAAM,MAAM,kBAAkB,GAAG,sBAAsB,CAAC,OAAO,CAAC,GAAG;IACjE,cAAc,CAAC,EAAE,oBAAoB,CAAC;CACvC,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,cAAc,CAAC,EAAE,oBAAoB,CAAC;CACvC,CAAC;AAIF,MAAM,MAAM,iCAAiC,GAAG;IAC9C,cAAc,CAAC,EAAE,oBAAoB,CAAC;CACvC,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACtC,eAAe,EAAE,eAAe,CAAC;IAIjC,WAAW,EAAE,CAAC,OAAO,CAAC,EAAE,kBAAkB,KAAK,iBAAiB,CAAC;IAIjE,aAAa,EAAE,CAAC,OAAO,CAAC,EAAE,oBAAoB,KAAK,eAAe,CAAC;IAKnE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;CAC3C,CAAC;AAsBF,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,eAAe,EAAE,QAAQ,GAAE,iCAAsC,GAAG,yBAAyB,CAkDxI;AAED,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,eAAe,EAAE,QAAQ,GAAE,iCAAsC,GAAG,yBAAyB,CAEpI"}
@@ -0,0 +1,72 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ // Import from the package entry point, which registers the storage providers.
6
+ import { PublisherServer } from '../server/index.js';
7
+ import { resolveServeStaticPathname } from './path.js';
8
+ export { EXTENSION_PHASES, defineExtension, mergeBindings, } from './extensions.js';
9
+ // The selection that selectCollection() of this server made for the request.
10
+ function getSelection(c, server) {
11
+ const selection = c.var.staticPublisher;
12
+ return selection?.publisherServer === server ? selection : undefined;
13
+ }
14
+ function resolveCollectionName(c, option, selection) {
15
+ const value = typeof option === 'function' ? option(c) : option;
16
+ if (value !== undefined) {
17
+ return value;
18
+ }
19
+ // The collection that selectCollection() chose. Without a selection,
20
+ // undefined selects the active collection of the PublisherServer.
21
+ return selection != null ? selection.collectionName : undefined;
22
+ }
23
+ export function fromPublisherServer(server, defaults = {}) {
24
+ const serveStatic = (callOptions = {}) => {
25
+ const options = { ...defaults, ...callOptions };
26
+ return async (c, next) => {
27
+ const selection = getSelection(c, server);
28
+ // For a path source, the path without its prefix. A path option
29
+ // replaces the request path (see resolveServeStaticPathname()).
30
+ const requestPath = selection?.pathname ?? c.req.path;
31
+ const pathname = resolveServeStaticPathname(requestPath, c, options);
32
+ const response = pathname != null ? await server.serveRequest(c.req.raw, {
33
+ collectionName: resolveCollectionName(c, options.collectionName, selection),
34
+ pathname,
35
+ fallback: false,
36
+ healthCheck: false,
37
+ }) : null;
38
+ if (response == null) {
39
+ await next();
40
+ return;
41
+ }
42
+ return response;
43
+ };
44
+ };
45
+ const serveFallback = (callOptions = {}) => {
46
+ const options = { ...defaults, ...callOptions };
47
+ return async (c) => {
48
+ const response = await server.serveFallback(c.req.raw, {
49
+ collectionName: resolveCollectionName(c, options.collectionName, getSelection(c, server)),
50
+ });
51
+ return response ?? c.text('404 Not Found', 404);
52
+ };
53
+ };
54
+ const selectCollection = () => {
55
+ return async (c, next) => {
56
+ const selection = server.resolveCollection(c.req.raw);
57
+ if (selection.response != null) {
58
+ return selection.response;
59
+ }
60
+ c.set('staticPublisher', {
61
+ publisherServer: server,
62
+ collectionName: selection.collectionName,
63
+ pathname: selection.pathname,
64
+ });
65
+ await next();
66
+ };
67
+ };
68
+ return { publisherServer: server, serveStatic, serveFallback, selectCollection };
69
+ }
70
+ export function fromStaticPublishRc(rc, defaults = {}) {
71
+ return fromPublisherServer(PublisherServer.fromStaticPublishRc(rc), defaults);
72
+ }
@@ -0,0 +1,7 @@
1
+ export type ServeStaticPathOptions<C> = {
2
+ path?: string;
3
+ root?: string;
4
+ rewriteRequestPath?: (path: string, c: C) => string;
5
+ };
6
+ export declare function resolveServeStaticPathname<C>(requestPath: string, c: C, options: ServeStaticPathOptions<C>): string | null;
7
+ //# sourceMappingURL=path.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../../../src/hono/path.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,sBAAsB,CAAC,CAAC,IAAI;IAEtC,IAAI,CAAC,EAAE,MAAM,CAAC;IAGd,IAAI,CAAC,EAAE,MAAM,CAAC;IAGd,kBAAkB,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC;CACrD,CAAC;AAIF,wBAAgB,0BAA0B,CAAC,CAAC,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,OAAO,EAAE,sBAAsB,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,IAAI,CAmB1H"}
@@ -0,0 +1,32 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ // The pathname to give to PublisherServer, or null if the path is outside the
6
+ // root. requestPath is decoded, as Hono's c.req.path is.
7
+ export function resolveServeStaticPathname(requestPath, c, options) {
8
+ let root = options.root ?? './';
9
+ if (!root.endsWith('/')) {
10
+ root += '/';
11
+ }
12
+ const base = new URL(encodePath(root), 'https://www.example.com/');
13
+ let reqPath = options.path ??
14
+ (options.rewriteRequestPath != null ? options.rewriteRequestPath(requestPath, c) : requestPath);
15
+ if (reqPath.startsWith('/')) {
16
+ reqPath = '.' + reqPath;
17
+ }
18
+ // A decoded path can have '..'. Do not serve a path outside the root.
19
+ const pathname = new URL(encodePath(reqPath), base).pathname;
20
+ if (!pathname.startsWith(base.pathname)) {
21
+ return null;
22
+ }
23
+ return decodePath(pathname);
24
+ }
25
+ // Encode each segment, so that URL resolves only the slashes and the '.' and
26
+ // '..' segments of a decoded path. A '%', '?', or '#' in a segment stays in it.
27
+ function encodePath(path) {
28
+ return path.split('/').map(encodeURIComponent).join('/');
29
+ }
30
+ function decodePath(path) {
31
+ return path.split('/').map(decodeURIComponent).join('/');
32
+ }
@@ -7,6 +7,12 @@ export type AssetEntry = {
7
7
  variants: ContentCompressionTypes[];
8
8
  };
9
9
  export type AssetEntryMap = Record<string, AssetEntry>;
10
+ export declare const HIDDEN_INDEX_KEY_PREFIX = "hidden:";
11
+ export declare const HIDDEN_INDEX_FEATURE = "hidden";
12
+ export declare function hiddenIndexKey(assetKey: string): string;
13
+ export declare function isHiddenIndexKey(indexKey: string): boolean;
14
+ export declare function findAssetEntry(assetEntryMap: AssetEntryMap, assetKey: string, includeHidden: boolean): AssetEntry | null;
15
+ export declare function countHiddenEntries(assetEntryMap: AssetEntryMap): number;
10
16
  export type AssetVariantMetadata = {
11
17
  contentEncoding?: ContentCompressionTypes;
12
18
  size: number;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/models/assets/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,uBAAuB,EAAqB,MAAM,yBAAyB,CAAC;AAE1F,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,gBAAgB,EAAE,MAAM,CAAC;IACzB,QAAQ,EAAE,uBAAuB,EAAE,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAEvD,MAAM,MAAM,oBAAoB,GAAG;IACjC,eAAe,CAAC,EAAE,uBAAuB,CAAC;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,GAAG,oBAAoB,GAAG,IAAI,CAiC/G"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/models/assets/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,uBAAuB,EAAqB,MAAM,yBAAyB,CAAC;AAE1F,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,gBAAgB,EAAE,MAAM,CAAC;IACzB,QAAQ,EAAE,uBAAuB,EAAE,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAOvD,eAAO,MAAM,uBAAuB,YAAY,CAAC;AACjD,eAAO,MAAM,oBAAoB,WAAW,CAAC;AAE7C,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEvD;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAE1D;AAGD,wBAAgB,cAAc,CAAC,aAAa,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,OAAO,GAAG,UAAU,GAAG,IAAI,CAKxH;AAGD,wBAAgB,kBAAkB,CAAC,aAAa,EAAE,aAAa,GAAG,MAAM,CAEvE;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC,eAAe,CAAC,EAAE,uBAAuB,CAAC;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,GAAG,oBAAoB,GAAG,IAAI,CAiC/G"}
@@ -4,6 +4,30 @@
4
4
  */
5
5
  import { getMetadataFieldValue } from '../metadata/index.js';
6
6
  import { isCompressionType } from '../compression/index.js';
7
+ // A hidden file is published, but PublisherServer does not serve it for a
8
+ // request. Its key in the index is this prefix and then the asset key, for
9
+ // example 'hidden:/404.html'. A real asset key always starts with '/', so no
10
+ // request path can match such a key. A collection that has a hidden file
11
+ // requires the 'hidden' feature.
12
+ export const HIDDEN_INDEX_KEY_PREFIX = 'hidden:';
13
+ export const HIDDEN_INDEX_FEATURE = 'hidden';
14
+ export function hiddenIndexKey(assetKey) {
15
+ return HIDDEN_INDEX_KEY_PREFIX + assetKey;
16
+ }
17
+ export function isHiddenIndexKey(indexKey) {
18
+ return indexKey.startsWith(HIDDEN_INDEX_KEY_PREFIX);
19
+ }
20
+ // Find the entry for an asset key. With includeHidden, also find a hidden file.
21
+ export function findAssetEntry(assetEntryMap, assetKey, includeHidden) {
22
+ if (isHiddenIndexKey(assetKey)) {
23
+ return null;
24
+ }
25
+ return assetEntryMap[assetKey] ?? (includeHidden ? assetEntryMap[hiddenIndexKey(assetKey)] : undefined) ?? null;
26
+ }
27
+ // The number of hidden files in an index.
28
+ export function countHiddenEntries(assetEntryMap) {
29
+ return Object.keys(assetEntryMap).filter(isHiddenIndexKey).length;
30
+ }
7
31
  export function decodeAssetVariantMetadata(obj) {
8
32
  if (obj == null) {
9
33
  return null;
@@ -0,0 +1,3 @@
1
+ export declare const COLLECTION_NAME_RULE = "Use lowercase a-z, 0-9, and '-', from 1 to 63 characters, with no '-' at the start or the end.";
2
+ export declare function isValidCollectionName(name: string): boolean;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/models/collections/index.ts"],"names":[],"mappings":"AAaA,eAAO,MAAM,oBAAoB,mGACiE,CAAC;AAEnG,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE3D"}
@@ -0,0 +1,15 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ // A collection name must be a DNS label: lowercase a-z, 0-9, and '-', from
6
+ // 1 to 63 characters, with no '-' at the start or the end. A collection name
7
+ // is used in storage keys, in the surrogate key of the purge, in a cookie,
8
+ // and in hostnames (preview URLs), and a DNS label is safe in all of them.
9
+ // It also cannot have a character that breaks a cookie (';', ',', '=',
10
+ // spaces, quotes, '\', or control characters).
11
+ const COLLECTION_NAME_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
12
+ export const COLLECTION_NAME_RULE = "Use lowercase a-z, 0-9, and '-', from 1 to 63 characters, with no '-' at the start or the end.";
13
+ export function isValidCollectionName(name) {
14
+ return COLLECTION_NAME_PATTERN.test(name);
15
+ }
@@ -1,4 +1,4 @@
1
- export declare const compressionTypes: readonly ["br", "gzip"];
1
+ export declare const compressionTypes: readonly ['br', 'gzip'];
2
2
  export type ContentCompressionTypes = typeof compressionTypes[number];
3
3
  export declare function isCompressionType(value: string): value is ContentCompressionTypes;
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/models/compression/index.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,gBAAgB,yBAGnB,CAAC;AAEX,MAAM,MAAM,uBAAuB,GAAG,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC;AAEtE,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,uBAAuB,CAEjF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/models/compression/index.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,gBAAgB,YAC3B,IAAI,EACJ,MAAM,CACE,CAAC;AAEX,MAAM,MAAM,uBAAuB,GAAG,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC;AAEtE,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,uBAAuB,CAEjF"}
@@ -0,0 +1,55 @@
1
+ export declare const COLLECTION_PLACEHOLDER = "{collection}";
2
+ export type CollectionSelectionHostSource = {
3
+ type: 'host';
4
+ pattern: string;
5
+ };
6
+ export type CollectionSelectionPathSource = {
7
+ type: 'path';
8
+ prefix: string;
9
+ sticky?: 'cookie';
10
+ };
11
+ export type CollectionSelectionQuerySource = {
12
+ type: 'query';
13
+ param: string;
14
+ sticky?: 'cookie';
15
+ };
16
+ export type CollectionSelectionCookieSource = {
17
+ type: 'cookie';
18
+ cookieName?: string;
19
+ activatePath?: string;
20
+ resetPath?: string;
21
+ maxAge?: number;
22
+ };
23
+ export type CollectionSelectionHeaderSource = {
24
+ type: 'header';
25
+ name: string;
26
+ };
27
+ export type CollectionSelectionSource = CollectionSelectionHostSource | CollectionSelectionPathSource | CollectionSelectionQuerySource | CollectionSelectionCookieSource | CollectionSelectionHeaderSource;
28
+ export type CollectionSelectionSourceType = CollectionSelectionSource['type'];
29
+ export type CollectionSelection = {
30
+ sources: CollectionSelectionSource[];
31
+ allow?: string[];
32
+ onMissing?: 'not-found' | 'default';
33
+ noindex?: boolean;
34
+ };
35
+ export declare const COOKIE_SOURCE_DEFAULTS: {
36
+ readonly cookieName: 'publisher-collection';
37
+ readonly activatePath: '/activate';
38
+ readonly resetPath: '/reset';
39
+ };
40
+ export type PatternParts = {
41
+ before: string;
42
+ after: string;
43
+ };
44
+ export declare function splitPattern(pattern: string): PatternParts | null;
45
+ export declare function hostLabelFixedLength(parts: PatternParts): number;
46
+ export declare function maxCollectionNameLengthForHost(pattern: string): number;
47
+ export declare function allowPatternToRegExp(pattern: string): RegExp;
48
+ export declare function isCollectionNameAllowed(collectionName: string, allow: readonly string[] | undefined): boolean;
49
+ export declare function validateCollectionSelection(config: unknown): string[];
50
+ export declare function previewUrl(collectionName: string, baseUrl: string, config: CollectionSelection): string | null;
51
+ export declare function previewUrlTemplates(baseUrl: string, config: CollectionSelection): {
52
+ type: CollectionSelectionSourceType;
53
+ url: string;
54
+ }[];
55
+ //# sourceMappingURL=collection-selection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection-selection.d.ts","sourceRoot":"","sources":["../../../../src/models/config/collection-selection.ts"],"names":[],"mappings":"AAYA,eAAO,MAAM,sBAAsB,iBAAiB,CAAC;AAErD,MAAM,MAAM,6BAA6B,GAAG;IAC1C,IAAI,EAAE,MAAM,CAAC;IAGb,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG;IAC1C,IAAI,EAAE,MAAM,CAAC;IAGb,MAAM,EAAE,MAAM,CAAC;IAEf,MAAM,CAAC,EAAE,QAAQ,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG;IAC3C,IAAI,EAAE,OAAO,CAAC;IAEd,KAAK,EAAE,MAAM,CAAC;IAEd,MAAM,CAAC,EAAE,QAAQ,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,+BAA+B,GAAG;IAC5C,IAAI,EAAE,QAAQ,CAAC;IAEf,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,+BAA+B,GAAG;IAC5C,IAAI,EAAE,QAAQ,CAAC;IAEf,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,yBAAyB,GACjC,6BAA6B,GAC7B,6BAA6B,GAC7B,8BAA8B,GAC9B,+BAA+B,GAC/B,+BAA+B,CAAC;AAEpC,MAAM,MAAM,6BAA6B,GAAG,yBAAyB,CAAC,MAAM,CAAC,CAAC;AAE9E,MAAM,MAAM,mBAAmB,GAAG;IAGhC,OAAO,EAAE,yBAAyB,EAAE,CAAC;IAGrC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IAGjB,SAAS,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC;IAGpC,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,eAAO,MAAM,sBAAsB;aACjC,UAAU,EAAE,sBAAsB;aAClC,YAAY,EAAE,WAAW;aACzB,SAAS,EAAE,QAAQ;CACX,CAAC;AAaX,MAAM,MAAM,YAAY,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAE7D,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CASjE;AAGD,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAKhE;AAGD,wBAAgB,8BAA8B,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAMtE;AAMD,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE5D;AAGD,wBAAgB,uBAAuB,CAAC,cAAc,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,GAAG,OAAO,CAQ7G;AAGD,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,EAAE,CA0GrE;AAOD,wBAAgB,UAAU,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,GAAG,MAAM,GAAG,IAAI,CAiC9G;AAMD,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,GAAG;IAAE,IAAI,EAAE,6BAA6B,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EAAE,CAqBxI"}
@@ -0,0 +1,238 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ import { COLLECTION_NAME_RULE, isValidCollectionName } from '../collections/index.js';
6
+ // Collection selection: the options that choose which collection serves a
7
+ // request, for example for previews. The config is data only (strings, numbers,
8
+ // and booleans), so that each field can also be a scaffold flag.
9
+ // The resolver is in server/collection-selection/.
10
+ export const COLLECTION_PLACEHOLDER = '{collection}';
11
+ export const COOKIE_SOURCE_DEFAULTS = {
12
+ cookieName: 'publisher-collection',
13
+ activatePath: '/activate',
14
+ resetPath: '/reset',
15
+ };
16
+ const SOURCE_TYPES = ['host', 'path', 'query', 'cookie', 'header'];
17
+ // RFC 7230 token: the characters of a header name or a cookie name.
18
+ const TOKEN_PATTERN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
19
+ // A host label has at most 63 characters.
20
+ const MAX_LABEL_LENGTH = 63;
21
+ const ALLOW_PATTERN = /^[a-z0-9*-]+$/;
22
+ export function splitPattern(pattern) {
23
+ const index = pattern.indexOf(COLLECTION_PLACEHOLDER);
24
+ if (index === -1 || pattern.indexOf(COLLECTION_PLACEHOLDER, index + 1) !== -1) {
25
+ return null;
26
+ }
27
+ return {
28
+ before: pattern.slice(0, index),
29
+ after: pattern.slice(index + COLLECTION_PLACEHOLDER.length),
30
+ };
31
+ }
32
+ // The fixed text in the host label that has the placeholder.
33
+ export function hostLabelFixedLength(parts) {
34
+ const before = parts.before.slice(parts.before.lastIndexOf('.') + 1);
35
+ const afterDot = parts.after.indexOf('.');
36
+ const after = afterDot === -1 ? parts.after : parts.after.slice(0, afterDot);
37
+ return before.length + after.length;
38
+ }
39
+ // The longest collection name that fits in the host label of a host pattern.
40
+ export function maxCollectionNameLengthForHost(pattern) {
41
+ const parts = splitPattern(pattern);
42
+ if (parts == null) {
43
+ return 0;
44
+ }
45
+ return MAX_LABEL_LENGTH - hostLabelFixedLength(parts);
46
+ }
47
+ function escapeRegExp(value) {
48
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
49
+ }
50
+ export function allowPatternToRegExp(pattern) {
51
+ return new RegExp('^' + pattern.split('*').map(escapeRegExp).join('[a-z0-9-]*') + '$');
52
+ }
53
+ // Returns true if the name is valid, and allow (if set) has a pattern that matches it.
54
+ export function isCollectionNameAllowed(collectionName, allow) {
55
+ if (!isValidCollectionName(collectionName)) {
56
+ return false;
57
+ }
58
+ if (allow == null) {
59
+ return true;
60
+ }
61
+ return allow.some(pattern => allowPatternToRegExp(pattern).test(collectionName));
62
+ }
63
+ // Returns the errors in a collectionSelection config. An empty list means that it is valid.
64
+ export function validateCollectionSelection(config) {
65
+ const errors = [];
66
+ if (config == null || typeof config !== 'object' || Array.isArray(config)) {
67
+ return ['collectionSelection must be an object.'];
68
+ }
69
+ const { sources, allow, onMissing, noindex } = config;
70
+ if (!Array.isArray(sources)) {
71
+ errors.push('collectionSelection.sources must be an array.');
72
+ }
73
+ else {
74
+ let cookieSources = 0;
75
+ let stickySources = 0;
76
+ sources.forEach((source, index) => {
77
+ const name = `collectionSelection.sources[${index}]`;
78
+ if (source == null || typeof source !== 'object') {
79
+ errors.push(`${name} must be an object.`);
80
+ return;
81
+ }
82
+ const s = source;
83
+ if (typeof s.type !== 'string' || !SOURCE_TYPES.includes(s.type)) {
84
+ errors.push(`${name}.type must be one of ${SOURCE_TYPES.map(x => `'${x}'`).join(', ')}.`);
85
+ return;
86
+ }
87
+ switch (s.type) {
88
+ case 'host': {
89
+ const parts = typeof s.pattern === 'string' ? splitPattern(s.pattern) : null;
90
+ if (parts == null) {
91
+ errors.push(`${name}.pattern must be a string with exactly one ${COLLECTION_PLACEHOLDER}.`);
92
+ }
93
+ else if (!/^[a-z0-9.-]*$/i.test(parts.before + parts.after)) {
94
+ errors.push(`${name}.pattern must be a hostname: use only a-z, 0-9, '-', and '.' around ${COLLECTION_PLACEHOLDER}.`);
95
+ }
96
+ else if (MAX_LABEL_LENGTH - hostLabelFixedLength(parts) < 1) {
97
+ errors.push(`${name}.pattern: the host label with ${COLLECTION_PLACEHOLDER} has no room for a name (at most ${MAX_LABEL_LENGTH} characters).`);
98
+ }
99
+ break;
100
+ }
101
+ case 'path': {
102
+ const parts = typeof s.prefix === 'string' ? splitPattern(s.prefix) : null;
103
+ if (parts == null) {
104
+ errors.push(`${name}.prefix must be a string with exactly one ${COLLECTION_PLACEHOLDER}.`);
105
+ }
106
+ else if (!parts.before.startsWith('/')) {
107
+ errors.push(`${name}.prefix must start with '/'.`);
108
+ }
109
+ else if (parts.after.endsWith('/')) {
110
+ errors.push(`${name}.prefix must not end with '/'.`);
111
+ }
112
+ else if (/[?#]/.test(parts.before + parts.after)) {
113
+ errors.push(`${name}.prefix must not contain '?' or '#'.`);
114
+ }
115
+ break;
116
+ }
117
+ case 'query':
118
+ if (typeof s.param !== 'string' || s.param === '') {
119
+ errors.push(`${name}.param must be a non-empty string.`);
120
+ }
121
+ break;
122
+ case 'cookie':
123
+ cookieSources++;
124
+ if (s.cookieName !== undefined && (typeof s.cookieName !== 'string' || !TOKEN_PATTERN.test(s.cookieName))) {
125
+ errors.push(`${name}.cookieName must be a valid cookie name.`);
126
+ }
127
+ for (const field of ['activatePath', 'resetPath']) {
128
+ if (s[field] !== undefined && (typeof s[field] !== 'string' || !s[field].startsWith('/'))) {
129
+ errors.push(`${name}.${field} must be a string that starts with '/'.`);
130
+ }
131
+ }
132
+ if (s.maxAge !== undefined && (!Number.isInteger(s.maxAge) || s.maxAge < 0)) {
133
+ errors.push(`${name}.maxAge must be an integer that is 0 or more.`);
134
+ }
135
+ break;
136
+ case 'header':
137
+ if (typeof s.name !== 'string' || !TOKEN_PATTERN.test(s.name)) {
138
+ errors.push(`${name}.name must be a valid header name.`);
139
+ }
140
+ break;
141
+ }
142
+ if (s.sticky !== undefined) {
143
+ if (s.type !== 'path' && s.type !== 'query') {
144
+ errors.push(`${name}.sticky is only for the 'path' and 'query' sources.`);
145
+ }
146
+ else if (s.sticky !== 'cookie') {
147
+ errors.push(`${name}.sticky must be 'cookie'.`);
148
+ }
149
+ else {
150
+ stickySources++;
151
+ }
152
+ }
153
+ });
154
+ if (cookieSources > 1) {
155
+ errors.push(`collectionSelection.sources must have at most one 'cookie' source.`);
156
+ }
157
+ if (stickySources > 0 && cookieSources === 0) {
158
+ errors.push(`sticky: 'cookie' needs a 'cookie' source in collectionSelection.sources, for the settings of the cookie.`);
159
+ }
160
+ }
161
+ if (allow !== undefined) {
162
+ if (!Array.isArray(allow) || !allow.every(x => typeof x === 'string' && ALLOW_PATTERN.test(x))) {
163
+ errors.push(`collectionSelection.allow must be an array of patterns. Use a-z, 0-9, '-', and '*'.`);
164
+ }
165
+ }
166
+ if (onMissing !== undefined && onMissing !== 'not-found' && onMissing !== 'default') {
167
+ errors.push(`collectionSelection.onMissing must be 'not-found' or 'default'.`);
168
+ }
169
+ if (noindex !== undefined && typeof noindex !== 'boolean') {
170
+ errors.push(`collectionSelection.noindex must be a boolean.`);
171
+ }
172
+ return errors;
173
+ }
174
+ // The URL that shows a collection, from the first source that can make one.
175
+ // baseUrl is the URL of the service, for example 'https://example.edgecompute.app'.
176
+ // The host source uses only its protocol. Returns null if no source can make a
177
+ // URL (for example, only a header source). Throws for a name that cannot be
178
+ // selected, or that does not fit in the host label.
179
+ export function previewUrl(collectionName, baseUrl, config) {
180
+ if (!isValidCollectionName(collectionName)) {
181
+ throw new Error(`'${collectionName}' is not a valid collection name. ${COLLECTION_NAME_RULE}`);
182
+ }
183
+ if (!isCollectionNameAllowed(collectionName, config.allow)) {
184
+ throw new Error(`'${collectionName}' is not in collectionSelection.allow.`);
185
+ }
186
+ const base = new URL(baseUrl);
187
+ for (const source of config.sources) {
188
+ switch (source.type) {
189
+ case 'host': {
190
+ const maxLength = maxCollectionNameLengthForHost(source.pattern);
191
+ if (collectionName.length > maxLength) {
192
+ throw new Error(`'${collectionName}' is too long for the host pattern '${source.pattern}': a host label has at most ${MAX_LABEL_LENGTH} characters, so the name can have at most ${maxLength}.`);
193
+ }
194
+ return `${base.protocol}//${source.pattern.replace(COLLECTION_PLACEHOLDER, collectionName).toLowerCase()}/`;
195
+ }
196
+ case 'path':
197
+ return `${base.origin}${source.prefix.replace(COLLECTION_PLACEHOLDER, collectionName)}/`;
198
+ case 'query': {
199
+ const url = new URL('/', base);
200
+ url.searchParams.set(source.param, collectionName);
201
+ return url.href;
202
+ }
203
+ case 'cookie': {
204
+ const url = new URL(source.activatePath ?? COOKIE_SOURCE_DEFAULTS.activatePath, base);
205
+ url.searchParams.set('collection', collectionName);
206
+ url.searchParams.set('redirectTo', '/');
207
+ return url.href;
208
+ }
209
+ }
210
+ }
211
+ return null;
212
+ }
213
+ // The preview URL of each source that can make one, with the {collection}
214
+ // placeholder in it. For example, for CI tools that show the link of a pull
215
+ // request. baseUrl is like the one of previewUrl(), and can be a placeholder
216
+ // such as 'https://<service domain>'.
217
+ export function previewUrlTemplates(baseUrl, config) {
218
+ const base = baseUrl.replace(/\/+$/, '');
219
+ const protocol = /^([a-z]+:)\/\//i.exec(base)?.[1] ?? 'https:';
220
+ const result = [];
221
+ for (const source of config.sources) {
222
+ switch (source.type) {
223
+ case 'host':
224
+ result.push({ type: 'host', url: `${protocol}//${source.pattern}/` });
225
+ break;
226
+ case 'path':
227
+ result.push({ type: 'path', url: `${base}${source.prefix}/` });
228
+ break;
229
+ case 'query':
230
+ result.push({ type: 'query', url: `${base}/?${encodeURIComponent(source.param)}=${COLLECTION_PLACEHOLDER}` });
231
+ break;
232
+ case 'cookie':
233
+ result.push({ type: 'cookie', url: `${base}${source.activatePath ?? COOKIE_SOURCE_DEFAULTS.activatePath}?collection=${COLLECTION_PLACEHOLDER}&redirectTo=%2F` });
234
+ break;
235
+ }
236
+ }
237
+ return result;
238
+ }
@@ -0,0 +1,4 @@
1
+ export type ItemPattern = string | RegExp;
2
+ export declare function parseItemPatterns(items: readonly string[], onError?: (index: number, item: string) => void): ItemPattern[];
3
+ export declare function testItemPatterns(patterns: readonly ItemPattern[], path: string): boolean;
4
+ //# sourceMappingURL=item-patterns.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"item-patterns.d.ts","sourceRoot":"","sources":["../../../../src/models/config/item-patterns.ts"],"names":[],"mappings":"AAUA,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,CAAC;AAI1C,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,GAC9C,WAAW,EAAE,CAwBf;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,SAAS,WAAW,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAUxF"}