@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,13 @@
1
+ import { type CollectionSelection } from '../../models/config/collection-selection.js';
2
+ export type CollectionSelectionFlags = {
3
+ sources?: string[];
4
+ allow?: string[];
5
+ onMissing?: string;
6
+ noindex?: string;
7
+ };
8
+ export declare const COLLECTION_SELECTION_FLAGS_HELP = " --collection-selection <source> Select the collection of a request, for example for\n previews. Can be repeated; the sources are tried in order.\n host:<pattern> host:{collection}.preview.example.com\n path:<prefix>[,sticky] path:/_preview/{collection},sticky\n query:<param>[,sticky] query:collection,sticky\n cookie[:<name>][,maxAge=<seconds>][,activatePath=<path>][,resetPath=<path>]\n header:<name> header:x-publisher-collection\n 'sticky' also sets the cookie of the cookie source, so it needs a cookie source.\n --collection-allow <patterns> The collections that can be selected, for example \"pr-*,staging\".\n Default: any collection.\n --collection-on-missing <value> When the selected collection is not published or has\n expired: 'not-found' (default: a 404 response) or\n 'default' (serve the default collection).\n --collection-noindex <true|false> Add 'X-Robots-Tag: noindex' to the responses of a collection\n that is not the default collection. Default: true.";
9
+ export declare function collectionSelectionFromFlags(flags: CollectionSelectionFlags): {
10
+ collectionSelection: CollectionSelection | undefined;
11
+ errors: string[];
12
+ };
13
+ //# sourceMappingURL=collection-selection-flags.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection-selection-flags.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/collection-selection-flags.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,mBAAmB,EAIzB,MAAM,6CAA6C,CAAC;AAcrD,MAAM,MAAM,wBAAwB,GAAG;IACrC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,eAAO,MAAM,+BAA+B,y8CAe+C,CAAC;AA0E5F,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,wBAAwB,GAAG;IAC7E,mBAAmB,EAAE,mBAAmB,GAAG,SAAS,CAAC;IACrD,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB,CAqCA"}
@@ -0,0 +1,131 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ import { validateCollectionSelection, } from '../../models/config/collection-selection.js';
6
+ export const COLLECTION_SELECTION_FLAGS_HELP = `\
7
+ --collection-selection <source> Select the collection of a request, for example for
8
+ previews. Can be repeated; the sources are tried in order.
9
+ host:<pattern> host:{collection}.preview.example.com
10
+ path:<prefix>[,sticky] path:/_preview/{collection},sticky
11
+ query:<param>[,sticky] query:collection,sticky
12
+ cookie[:<name>][,maxAge=<seconds>][,activatePath=<path>][,resetPath=<path>]
13
+ header:<name> header:x-publisher-collection
14
+ 'sticky' also sets the cookie of the cookie source, so it needs a cookie source.
15
+ --collection-allow <patterns> The collections that can be selected, for example "pr-*,staging".
16
+ Default: any collection.
17
+ --collection-on-missing <value> When the selected collection is not published or has
18
+ expired: 'not-found' (default: a 404 response) or
19
+ 'default' (serve the default collection).
20
+ --collection-noindex <true|false> Add 'X-Robots-Tag: noindex' to the responses of a collection
21
+ that is not the default collection. Default: true.`;
22
+ function parseSource(spec, errors) {
23
+ const [head, ...options] = spec.split(',').map(x => x.trim());
24
+ const colon = head.indexOf(':');
25
+ const type = colon === -1 ? head : head.slice(0, colon);
26
+ const value = colon === -1 ? undefined : head.slice(colon + 1);
27
+ const needValue = (what) => {
28
+ if (value == null || value === '') {
29
+ errors.push(`--collection-selection ${type}: give the ${what}, for example '${type}:<${what}>'.`);
30
+ return false;
31
+ }
32
+ return true;
33
+ };
34
+ const sticky = () => {
35
+ let result;
36
+ for (const option of options) {
37
+ if (option === 'sticky') {
38
+ result = 'cookie';
39
+ }
40
+ else {
41
+ errors.push(`--collection-selection ${type}: unknown option '${option}'.`);
42
+ }
43
+ }
44
+ return result;
45
+ };
46
+ const noOptions = () => {
47
+ for (const option of options) {
48
+ errors.push(`--collection-selection ${type}: unknown option '${option}'.`);
49
+ }
50
+ };
51
+ switch (type) {
52
+ case 'host':
53
+ noOptions();
54
+ return needValue('pattern') ? { type, pattern: value } : null;
55
+ case 'path': {
56
+ const s = sticky();
57
+ return needValue('prefix') ? { type, prefix: value, ...(s != null ? { sticky: s } : {}) } : null;
58
+ }
59
+ case 'query': {
60
+ const s = sticky();
61
+ return needValue('param') ? { type, param: value, ...(s != null ? { sticky: s } : {}) } : null;
62
+ }
63
+ case 'header':
64
+ noOptions();
65
+ return needValue('name') ? { type, name: value } : null;
66
+ case 'cookie': {
67
+ const source = { type };
68
+ if (value != null && value !== '') {
69
+ source.cookieName = value;
70
+ }
71
+ for (const option of options) {
72
+ const eq = option.indexOf('=');
73
+ const key = eq === -1 ? option : option.slice(0, eq);
74
+ const optionValue = eq === -1 ? '' : option.slice(eq + 1);
75
+ if (key === 'maxAge' && /^\d+$/.test(optionValue)) {
76
+ source.maxAge = Number(optionValue);
77
+ }
78
+ else if (key === 'activatePath' || key === 'resetPath') {
79
+ source[key] = optionValue;
80
+ }
81
+ else {
82
+ errors.push(`--collection-selection cookie: unknown option, or a value that is not valid: '${option}'.`);
83
+ }
84
+ }
85
+ return source;
86
+ }
87
+ default:
88
+ errors.push(`--collection-selection: unknown source '${type}'. Use host, path, query, cookie, or header.`);
89
+ return null;
90
+ }
91
+ }
92
+ // Returns the collectionSelection from the flags (undefined if no flag is set),
93
+ // and the errors.
94
+ export function collectionSelectionFromFlags(flags) {
95
+ const errors = [];
96
+ const sources = flags.sources ?? [];
97
+ if (sources.length === 0) {
98
+ if (flags.allow != null || flags.onMissing != null || flags.noindex != null) {
99
+ errors.push('--collection-allow, --collection-on-missing, and --collection-noindex need --collection-selection.');
100
+ }
101
+ return { collectionSelection: undefined, errors };
102
+ }
103
+ const collectionSelection = { sources: [] };
104
+ for (const spec of sources) {
105
+ const source = parseSource(spec, errors);
106
+ if (source != null) {
107
+ collectionSelection.sources.push(source);
108
+ }
109
+ }
110
+ if (flags.allow != null) {
111
+ collectionSelection.allow = flags.allow.flatMap(x => x.split(',')).map(x => x.trim()).filter(x => x !== '');
112
+ }
113
+ if (flags.onMissing != null) {
114
+ collectionSelection.onMissing = flags.onMissing;
115
+ }
116
+ if (flags.noindex != null) {
117
+ if (flags.noindex === 'true') {
118
+ collectionSelection.noindex = true;
119
+ }
120
+ else if (flags.noindex === 'false') {
121
+ collectionSelection.noindex = false;
122
+ }
123
+ else {
124
+ errors.push(`--collection-noindex must be 'true' or 'false'.`);
125
+ }
126
+ }
127
+ if (errors.length === 0) {
128
+ errors.push(...validateCollectionSelection(collectionSelection));
129
+ }
130
+ return { collectionSelection: errors.length === 0 ? collectionSelection : undefined, errors };
131
+ }
@@ -1,12 +1,25 @@
1
1
  import { type StaticPublishRc } from '../../models/config/static-publish-rc.js';
2
+ import { type StaticPublishProjectManaged } from '../../models/config/static-publish-project.js';
2
3
  import { type PublishContentConfigNormalized } from '../../models/config/publish-content-config.js';
3
4
  import { type PublisherServerConfigNormalized } from '../../models/config/publisher-server-config.js';
4
5
  export declare class LoadConfigError extends Error {
5
6
  errors: string[];
6
7
  constructor(configFilePath: string, errors: string[]);
7
8
  }
9
+ export type ProjectMode = 'managed' | 'ejected' | 'scaffold' | 'conflict';
10
+ export declare function detectProjectMode(dir?: string): ProjectMode;
11
+ export declare function getComputeAppDir(): string;
12
+ export declare function configFileName(): string;
13
+ export type StaticPublishProjectNormalized = {
14
+ rc: StaticPublishRc;
15
+ publishContent: unknown;
16
+ managed: StaticPublishProjectManaged;
17
+ };
18
+ export declare const normalizeProjectManaged: (obj: unknown, errCtx?: string[]) => StaticPublishProjectManaged | null;
19
+ export declare function loadProjectFile(): Promise<StaticPublishProjectNormalized>;
8
20
  export declare function loadStaticPublisherRcFile(): Promise<StaticPublishRc>;
9
21
  export declare const normalizeStaticPublisherRc: (obj: unknown, errCtx?: string[]) => StaticPublishRc | null;
22
+ export declare function loadPublishContentConfig(configFile: string | undefined): Promise<PublishContentConfigNormalized>;
10
23
  export declare function loadPublishContentConfigFile(configFile: string): Promise<PublishContentConfigNormalized>;
11
24
  export declare const normalizePublishContentConfig: (obj: unknown, errCtx?: string[]) => PublishContentConfigNormalized | null;
12
25
  export declare const normalizePublisherServerConfig: (obj: unknown, errCtx?: string[]) => PublisherServerConfigNormalized | null;
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/config.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,KAAK,eAAe,EAIrB,MAAM,0CAA0C,CAAC;AAOlD,OAAO,EACL,KAAK,8BAA8B,EAEpC,MAAM,+CAA+C,CAAC;AAOvD,OAAO,EAAE,KAAK,+BAA+B,EAAE,MAAM,gDAAgD,CAAC;AAEtG,qBAAa,eAAgB,SAAQ,KAAK;IACxC,MAAM,EAAE,MAAM,EAAE,CAAC;gBAEL,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE;CAIrD;AAED,wBAAsB,yBAAyB,IAAI,OAAO,CAAC,eAAe,CAAC,CA+B1E;AAGD,eAAO,MAAM,0BAA0B,6DAmErC,CAAC;AAEH,wBAAsB,4BAA4B,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,8BAA8B,CAAC,CA6B9G;AA4CD,eAAO,MAAM,6BAA6B,4EAsKxC,CAAC;AAEH,eAAO,MAAM,8BAA8B,6EAkIzC,CAAC"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/config.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,KAAK,eAAe,EAKrB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EACL,KAAK,2BAA2B,EAIjC,MAAM,+CAA+C,CAAC;AAWvD,OAAO,EACL,KAAK,8BAA8B,EAEpC,MAAM,+CAA+C,CAAC;AAOvD,OAAO,EAAE,KAAK,+BAA+B,EAAE,MAAM,gDAAgD,CAAC;AAEtG,qBAAa,eAAgB,SAAQ,KAAK;IACxC,MAAM,EAAE,MAAM,EAAE,CAAC;IAEjB,YAAY,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,EAGnD;CACF;AAGD,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,SAAS,GAAG,UAAU,GAAG,UAAU,CAAC;AAE1E,wBAAgB,iBAAiB,CAAC,GAAG,GAAE,MAAY,GAAG,WAAW,CAOhE;AAKD,wBAAgB,gBAAgB,IAAI,MAAM,CAEzC;AAGD,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAED,MAAM,MAAM,8BAA8B,GAAG;IAC3C,EAAE,EAAE,eAAe,CAAC;IAEpB,cAAc,EAAE,OAAO,CAAC;IACxB,OAAO,EAAE,2BAA2B,CAAC;CACtC,CAAC;AAsBF,eAAO,MAAM,uBAAuB,yEAoBlC,CAAC;AAGH,wBAAsB,eAAe,IAAI,OAAO,CAAC,8BAA8B,CAAC,CAiB/E;AAID,wBAAsB,yBAAyB,IAAI,OAAO,CAAC,eAAe,CAAC,CAmC1E;AAGD,eAAO,MAAM,0BAA0B,6DAgGrC,CAAC;AAKH,wBAAsB,wBAAwB,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,8BAA8B,CAAC,CAWtH;AAED,wBAAsB,4BAA4B,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,8BAA8B,CAAC,CA6B9G;AAwED,eAAO,MAAM,6BAA6B,4EA8KxC,CAAC;AAEH,eAAO,MAAM,8BAA8B,6EA2GzC,CAAC"}
@@ -2,10 +2,16 @@
2
2
  * Copyright Fastly, Inc.
3
3
  * Licensed under the MIT license. See LICENSE file for details.
4
4
  */
5
+ import fs from 'node:fs';
5
6
  import path from 'node:path';
6
7
  import { pathToFileURL } from 'node:url';
7
8
  import globToRegExp from 'glob-to-regexp';
8
- import { isKvStoreConfigRc, isS3StorageConfigRc, } from '../../models/config/static-publish-rc.js';
9
+ import { checkStaticPublisherWorkingDir, isKvStoreConfigRc, isS3StorageConfigRc, } from '../../models/config/static-publish-rc.js';
10
+ import { MANAGED_APP_DIR, PROJECT_FILE_NAME, RC_FILE_NAME, } from '../../models/config/static-publish-project.js';
11
+ import { validateCollectionSelection } from '../../models/config/collection-selection.js';
12
+ import { checkTemplateSettings } from '../../models/config/template-settings.js';
13
+ import { APP_TEMPLATE_NAMES, isAppTemplateName } from '../commands/scaffold/app-template.js';
14
+ import { checkFastlyTomlSections } from '../commands/scaffold/fastly-toml-sections.js';
9
15
  import { getKvStoreConfigFromRc, } from '../../models/config/kv-store-config.js';
10
16
  import { getS3StorageConfigFromRc, } from '../../models/config/s3-storage-config.js';
11
17
  import { buildNormalizeFunctionForArray, buildNormalizeFunctionForObject, isSpecified, isStringAndNotEmpty, } from './data.js';
@@ -15,9 +21,91 @@ export class LoadConfigError extends Error {
15
21
  this.errors = errors;
16
22
  }
17
23
  }
24
+ export function detectProjectMode(dir = '.') {
25
+ const hasProject = fs.existsSync(path.resolve(dir, PROJECT_FILE_NAME));
26
+ const hasRc = fs.existsSync(path.resolve(dir, RC_FILE_NAME));
27
+ if (hasProject && hasRc) {
28
+ return 'conflict';
29
+ }
30
+ return hasProject ? 'managed' : hasRc ? 'ejected' : 'scaffold';
31
+ }
32
+ // The directory of the Compute app: the current directory in an ejected app,
33
+ // or .static-publish/app in a managed project. The local KV Store file has
34
+ // paths relative to it, because `fastly compute serve` runs there.
35
+ export function getComputeAppDir() {
36
+ return detectProjectMode() === 'managed' ? path.resolve(MANAGED_APP_DIR) : path.resolve();
37
+ }
38
+ // The name of the config file of the current directory, for messages.
39
+ export function configFileName() {
40
+ return detectProjectMode() === 'managed' ? PROJECT_FILE_NAME : RC_FILE_NAME;
41
+ }
42
+ async function importDefault(configFile) {
43
+ const configFilePath = path.resolve(configFile);
44
+ let configRaw;
45
+ try {
46
+ configRaw = (await import(pathToFileURL(configFilePath).href)).default;
47
+ }
48
+ catch (ex) {
49
+ throw new LoadConfigError(configFile, [
50
+ `Unable to load ${configFilePath}`,
51
+ String(ex),
52
+ ]);
53
+ }
54
+ if (configRaw == null) {
55
+ throw new LoadConfigError(configFile, [
56
+ `Unable to load ${configFilePath}`,
57
+ `default export does not exist or is null.`
58
+ ]);
59
+ }
60
+ return configRaw;
61
+ }
62
+ export const normalizeProjectManaged = buildNormalizeFunctionForObject((config, errors) => {
63
+ const { name, description, authors, template, serviceId, staticPublishVersion, fastlyToml } = config;
64
+ for (const [key, value] of Object.entries({ name, description, template, serviceId, staticPublishVersion })) {
65
+ if (value !== undefined && typeof value !== 'string') {
66
+ errors.push(`managed.${key}, if specified, must be a string.`);
67
+ }
68
+ }
69
+ if (authors !== undefined && !(Array.isArray(authors) && authors.every(x => typeof x === 'string'))) {
70
+ errors.push('managed.authors, if specified, must be an array of strings.');
71
+ }
72
+ if (typeof template === 'string' && !isAppTemplateName(template)) {
73
+ errors.push(`managed.template must be one of: ${APP_TEMPLATE_NAMES.join(', ')}.`);
74
+ }
75
+ if (fastlyToml !== undefined) {
76
+ checkFastlyTomlSections(fastlyToml, errors);
77
+ }
78
+ if (errors.length > 0) {
79
+ return null;
80
+ }
81
+ return { name, description, authors, template, serviceId, staticPublishVersion, fastlyToml };
82
+ });
83
+ // Loads static-publish.project.js from the current directory (the project dir).
84
+ export async function loadProjectFile() {
85
+ const configFile = `./${PROJECT_FILE_NAME}`;
86
+ const configRaw = await importDefault(configFile);
87
+ const errors = [];
88
+ const rc = normalizeStaticPublisherRc(configRaw, errors);
89
+ if (!('publishContent' in configRaw) || configRaw.publishContent == null || typeof configRaw.publishContent !== 'object') {
90
+ errors.push('publishContent must be specified.');
91
+ }
92
+ let managed = {};
93
+ if ('managed' in configRaw && configRaw.managed != null) {
94
+ managed = normalizeProjectManaged(configRaw.managed, errors);
95
+ }
96
+ if (rc == null || managed == null || errors.length > 0) {
97
+ throw new LoadConfigError(configFile, errors);
98
+ }
99
+ return { rc, publishContent: configRaw.publishContent, managed };
100
+ }
101
+ // Loads the settings of the app: static-publish.rc.js in an ejected app, or the
102
+ // top level of static-publish.project.js in a managed project.
18
103
  export async function loadStaticPublisherRcFile() {
104
+ if (detectProjectMode() === 'managed') {
105
+ return (await loadProjectFile()).rc;
106
+ }
19
107
  let configRaw;
20
- const configFile = './static-publish.rc.js';
108
+ const configFile = `./${RC_FILE_NAME}`;
21
109
  const configFilePath = path.resolve(configFile);
22
110
  try {
23
111
  configRaw = (await import(pathToFileURL(configFilePath).href)).default;
@@ -42,7 +130,7 @@ export async function loadStaticPublisherRcFile() {
42
130
  return config;
43
131
  }
44
132
  export const normalizeStaticPublisherRc = buildNormalizeFunctionForObject((config, errors) => {
45
- let { publishId, defaultCollectionName, staticPublisherWorkingDir, } = config;
133
+ let { publishId, defaultCollectionName, staticPublisherWorkingDir, generatedFrom, collectionSelection, build, templateOptions, } = config;
46
134
  let storage = null;
47
135
  if (isKvStoreConfigRc(config)) {
48
136
  storage = {
@@ -82,24 +170,60 @@ export const normalizeStaticPublisherRc = buildNormalizeFunctionForObject((confi
82
170
  errors.push('staticPublisherWorkingDir must be specified.');
83
171
  }
84
172
  else {
85
- if (staticPublisherWorkingDir.startsWith('./') &&
86
- staticPublisherWorkingDir !== './' &&
87
- !staticPublisherWorkingDir.includes('//')) {
88
- // ok
89
- }
90
- else {
91
- errors.push('staticPublisherWorkingDir must be a relative subdirectory.');
173
+ const workingDirError = checkStaticPublisherWorkingDir(staticPublisherWorkingDir);
174
+ if (workingDirError != null) {
175
+ // The caller uses the result even if there are errors. Thus return null,
176
+ // so that publish-content never writes into a directory that breaks the rule.
177
+ errors.push(workingDirError);
178
+ return null;
92
179
  }
93
180
  while (staticPublisherWorkingDir.endsWith('/')) {
94
181
  staticPublisherWorkingDir = staticPublisherWorkingDir.slice(0, -1);
95
182
  }
96
183
  }
184
+ if (isSpecified(config, 'collectionSelection') && collectionSelection != null) {
185
+ // The caller uses the result even if there are errors. Thus return null, so
186
+ // that a collectionSelection that is not valid stops the command.
187
+ const collectionSelectionErrors = validateCollectionSelection(collectionSelection);
188
+ if (collectionSelectionErrors.length > 0) {
189
+ errors.push(...collectionSelectionErrors);
190
+ return null;
191
+ }
192
+ }
193
+ else {
194
+ collectionSelection = undefined;
195
+ }
196
+ // The settings of the templates. A setting that no template declares is only
197
+ // a warning.
198
+ const templateSettings = checkTemplateSettings(build, templateOptions);
199
+ for (const warning of templateSettings.warnings) {
200
+ console.warn(`⚠️ ${warning}`);
201
+ }
202
+ if (templateSettings.errors.length > 0) {
203
+ errors.push(...templateSettings.errors);
204
+ return null;
205
+ }
97
206
  return Object.assign({}, storage, {
98
207
  publishId,
99
208
  defaultCollectionName,
100
209
  staticPublisherWorkingDir,
101
- });
210
+ }, generatedFrom != null ? { generatedFrom } : {}, collectionSelection != null ? { collectionSelection } : {}, build != null ? { build } : {}, templateOptions != null ? { templateOptions } : {});
102
211
  });
212
+ // Loads the publish settings: the file of --config if given, else the
213
+ // publishContent section of static-publish.project.js in a managed project,
214
+ // else ./publish-content.config.js.
215
+ export async function loadPublishContentConfig(configFile) {
216
+ if (configFile == null && detectProjectMode() === 'managed') {
217
+ const project = await loadProjectFile();
218
+ const errors = [];
219
+ const config = normalizePublishContentConfig(project.publishContent, errors);
220
+ if (config == null || errors.length > 0) {
221
+ throw new LoadConfigError(`./${PROJECT_FILE_NAME} (publishContent)`, errors);
222
+ }
223
+ return config;
224
+ }
225
+ return loadPublishContentConfigFile(configFile ?? './publish-content.config.js');
226
+ }
103
227
  export async function loadPublishContentConfigFile(configFile) {
104
228
  let configRaw;
105
229
  const configFilePath = path.resolve(configFile);
@@ -125,6 +249,34 @@ export async function loadPublishContentConfigFile(configFile) {
125
249
  }
126
250
  return config;
127
251
  }
252
+ // staticItems and hiddenItems: a string, an array of strings, false, or null.
253
+ // Glob patterns become 're:' items. See models/config/item-patterns.ts.
254
+ function normalizeItemList(name, value, errors) {
255
+ if (value === null || value === false) {
256
+ value = [];
257
+ }
258
+ if (!Array.isArray(value)) {
259
+ value = [value];
260
+ }
261
+ const items = value;
262
+ if (!items.every((x) => typeof x === 'string')) {
263
+ errors.push(`${name}, if specified, must be a string value, an array of string values, false, or null`);
264
+ return [];
265
+ }
266
+ return items.map((x, index) => {
267
+ if (x.includes('*')) {
268
+ try {
269
+ const regexp = globToRegExp(x, { globstar: true });
270
+ return 're:' + String(regexp);
271
+ }
272
+ catch {
273
+ errors.push(`${name} item at index ${index}, '${x}', cannot be parsed as a glob pattern.`);
274
+ return '';
275
+ }
276
+ }
277
+ return x;
278
+ });
279
+ }
128
280
  const normalizeContentTypeDef = buildNormalizeFunctionForObject((config, errors) => {
129
281
  let { test, contentType, text } = config;
130
282
  if (typeof test === 'function' || test instanceof RegExp) {
@@ -163,7 +315,7 @@ const normalizeContentTypeDefs = buildNormalizeFunctionForArray((config, errors)
163
315
  return normalizeContentTypeDef(config, errors);
164
316
  });
165
317
  export const normalizePublishContentConfig = buildNormalizeFunctionForObject((config, errors) => {
166
- let { rootDir, excludeDirs, excludeDotFiles, includeWellKnown, assetInclusionTest, kvStoreAssetInclusionTest, contentCompression, brotliQuality, contentTypes, server, } = config;
318
+ let { rootDir, excludeDirs, excludeDotFiles, includeWellKnown, assetInclusionTest, kvStoreAssetInclusionTest, hiddenItems, contentCompression, brotliQuality, contentTypes, server, } = config;
167
319
  if (!isSpecified(config, 'rootDir')) {
168
320
  errors.push('rootDir must be specified.');
169
321
  }
@@ -255,6 +407,12 @@ export const normalizePublishContentConfig = buildNormalizeFunctionForObject((co
255
407
  else {
256
408
  errors.push('assetInclusionTest, if specified, must be null or a function.');
257
409
  }
410
+ if (!isSpecified(config, 'hiddenItems')) {
411
+ hiddenItems = [];
412
+ }
413
+ else {
414
+ hiddenItems = normalizeItemList('hiddenItems', hiddenItems, errors);
415
+ }
258
416
  if (!isSpecified(config, 'contentCompression')) {
259
417
  contentCompression = ['br', 'gzip'];
260
418
  }
@@ -320,6 +478,7 @@ export const normalizePublishContentConfig = buildNormalizeFunctionForObject((co
320
478
  excludeDotFiles,
321
479
  includeWellKnown,
322
480
  assetInclusionTest,
481
+ hiddenItems,
323
482
  contentCompression,
324
483
  brotliQuality,
325
484
  contentTypes,
@@ -343,32 +502,7 @@ export const normalizePublisherServerConfig = buildNormalizeFunctionForObject((c
343
502
  staticItems = [];
344
503
  }
345
504
  else {
346
- if (staticItems === null || staticItems === false) {
347
- staticItems = [];
348
- }
349
- if (!Array.isArray(staticItems)) {
350
- staticItems = [staticItems];
351
- }
352
- if (staticItems.every((x) => typeof x === 'string')) {
353
- staticItems = staticItems.map((x, index) => {
354
- if (x.includes('*')) {
355
- let re;
356
- try {
357
- const regexp = globToRegExp(x, { globstar: true });
358
- re = 're:' + String(regexp);
359
- }
360
- catch {
361
- errors.push(`staticItems item at index ${index}, '${x}', cannot be parsed as glob pattern.`);
362
- re = null;
363
- }
364
- return re;
365
- }
366
- return x;
367
- });
368
- }
369
- else {
370
- errors.push('staticItems, if specified, must be a string value, an array of string values, false, or null');
371
- }
505
+ staticItems = normalizeItemList('staticItems', staticItems, errors);
372
506
  }
373
507
  if (!isSpecified(config, 'allowedEncodings')) {
374
508
  allowedEncodings = ['br', 'gzip'];
@@ -1 +1 @@
1
- {"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/data.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC;AAE5F,wBAAgB,+BAA+B,CAAC,OAAO,EAAE,MAAM,EAAE,eAAe,CAAC,OAAO,CAAC,IAE/E,KAAK,OAAO,EAAE,SAAQ,MAAM,EAAO,KAAG,OAAO,GAAG,IAAI,CAmB7D;AAED,wBAAgB,8BAA8B,CAAC,MAAM,EAAE,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC,IAE5E,KAAK,OAAO,EAAE,SAAQ,MAAM,EAAO,KAAG,MAAM,EAAE,GAAG,IAAI,CAgC9D;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM,CAE/D;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAC,GAAG,EAAE,GAAG,EAAE,MAAM,WAE/C;AAED,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,QAAQ,EAAE,CAAC,kBAYpG"}
1
+ {"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/data.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC;AAE5F,wBAAgB,+BAA+B,CAAC,OAAO,EAAE,MAAM,EAAE,eAAe,CAAC,OAAO,CAAC,SAE1E,OAAO,WAAU,MAAM,EAAE,KAAQ,OAAO,GAAG,IAAI,CAmB7D;AAED,wBAAgB,8BAA8B,CAAC,MAAM,EAAE,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC,SAEvE,OAAO,WAAU,MAAM,EAAE,KAAQ,MAAM,EAAE,GAAG,IAAI,CAgC9D;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM,CAE/D;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAC,GAAG,EAAE,GAAG,EAAE,MAAM,WAE/C;AAED,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,QAAQ,EAAE,CAAC,kBAYpG"}
@@ -0,0 +1,13 @@
1
+ export type GlobalOptions = {
2
+ projectDir: string | undefined;
3
+ version: boolean;
4
+ nonInteractive: boolean;
5
+ json: boolean;
6
+ secrets: string[];
7
+ argv: string[];
8
+ };
9
+ export declare function isCiEnvironment(env: Record<string, string | undefined>): boolean;
10
+ export declare function parseGlobalOptions(args: string[], env?: Record<string, string | undefined>): GlobalOptions | {
11
+ error: string;
12
+ };
13
+ //# sourceMappingURL=global-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"global-options.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/global-options.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,aAAa,GAAG;IAG1B,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAE/B,OAAO,EAAE,OAAO,CAAC;IAEjB,cAAc,EAAE,OAAO,CAAC;IAExB,IAAI,EAAE,OAAO,CAAC;IAGd,OAAO,EAAE,MAAM,EAAE,CAAC;IAElB,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC;AAOF,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,WAEtE;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAM,GAAG,aAAa,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAsClI"}
@@ -0,0 +1,53 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ // The options of the commands that take a secret.
6
+ const SECRET_OPTIONS = ['--fastly-api-token', '--s3-secret-access-key'];
7
+ // CI is set (to a value that is not empty, 'false', or '0') by CI systems such
8
+ // as GitHub Actions.
9
+ export function isCiEnvironment(env) {
10
+ return env.CI != null && env.CI !== '' && env.CI !== 'false' && env.CI !== '0';
11
+ }
12
+ export function parseGlobalOptions(args, env = {}) {
13
+ let projectDir;
14
+ let version = false;
15
+ let nonInteractive = isCiEnvironment(env);
16
+ let json = false;
17
+ const secrets = [];
18
+ const argv = [];
19
+ for (let i = 0; i < args.length; i++) {
20
+ const arg = args[i];
21
+ for (const option of SECRET_OPTIONS) {
22
+ if (arg.startsWith(`${option}=`)) {
23
+ secrets.push(arg.slice(option.length + 1));
24
+ }
25
+ else if (arg === option && args[i + 1] != null) {
26
+ secrets.push(args[i + 1]);
27
+ }
28
+ }
29
+ if (arg === '--version') {
30
+ version = true;
31
+ }
32
+ else if (arg === '--non-interactive') {
33
+ nonInteractive = true;
34
+ }
35
+ else if (arg === '--json') {
36
+ json = true;
37
+ }
38
+ else if (arg === '--project-dir' || arg.startsWith('--project-dir=')) {
39
+ const value = arg === '--project-dir' ? args[++i] : arg.slice('--project-dir='.length);
40
+ if (value == null || value === '' || value.startsWith('--')) {
41
+ return { error: '--project-dir needs a directory.' };
42
+ }
43
+ if (projectDir != null) {
44
+ return { error: '--project-dir can be given only one time.' };
45
+ }
46
+ projectDir = value;
47
+ }
48
+ else {
49
+ argv.push(arg);
50
+ }
51
+ }
52
+ return { projectDir, version, nonInteractive, json, secrets, argv };
53
+ }
@@ -1,5 +1,6 @@
1
1
  import { StorageProviderBatchEntry } from '../storage/storage-provider.js';
2
2
  export declare const KV_STORE_CHUNK_SIZE: number;
3
+ export declare function chunkSizes(size: number, chunkSize: number): number[];
3
4
  export declare function shouldRecreateChunks(chunksDir: string, numChunks: number, item: StorageProviderBatchEntry, chunkSize: number): boolean;
4
5
  export declare function applyKVStoreEntriesChunks(kvStoreItemDescriptions: StorageProviderBatchEntry[], chunkSize: number): Promise<void>;
5
6
  export declare const KV_BATCH_MAX_ITEMS = 256;
@@ -1 +1 @@
1
- {"version":3,"file":"kv-store-items.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/kv-store-items.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAI3E,eAAO,MAAM,mBAAmB,QAAqB,CAAC;AAEtD,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,yBAAyB,EAAE,SAAS,EAAE,MAAM,WAkC5H;AAED,wBAAsB,yBAAyB,CAAC,uBAAuB,EAAE,yBAAyB,EAAE,EAAE,SAAS,EAAE,MAAM,iBAoGtH;AAED,eAAO,MAAM,kBAAkB,MAAM,CAAC;AACtC,eAAO,MAAM,kBAAkB,QAAoB,CAAC;AAEpD,MAAM,MAAM,WAAW,GAAG;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,yBAAyB,EAAE,CAAC;CACtC,CAAC;AAEF,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,yBAAyB,EAAE,EACpC,QAAQ,GAAE,MAA2B,EACrC,QAAQ,GAAE,MAA2B,GACpC;IAAE,OAAO,EAAE,WAAW,EAAE,CAAC;IAAC,SAAS,EAAE,yBAAyB,EAAE,CAAA;CAAE,CAuCpE;AAED,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,yBAAyB,GAAG,MAAM,CAOhF;AAED,wBAAgB,eAAe,CAC7B,OAAO,EAAE,yBAAyB,EAAE,EACpC,YAAY,EAAE,GAAG,CAAC,MAAM,CAAC,GACxB,yBAAyB,EAAE,CAE7B"}
1
+ {"version":3,"file":"kv-store-items.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/kv-store-items.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAI3E,eAAO,MAAM,mBAAmB,QAAqB,CAAC;AAItD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAGpE;AAED,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,yBAAyB,EAAE,SAAS,EAAE,MAAM,WAkC5H;AAED,wBAAsB,yBAAyB,CAAC,uBAAuB,EAAE,yBAAyB,EAAE,EAAE,SAAS,EAAE,MAAM,iBAiEtH;AAED,eAAO,MAAM,kBAAkB,MAAM,CAAC;AACtC,eAAO,MAAM,kBAAkB,QAAoB,CAAC;AAEpD,MAAM,MAAM,WAAW,GAAG;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,yBAAyB,EAAE,CAAC;CACtC,CAAC;AAEF,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,yBAAyB,EAAE,EACpC,QAAQ,GAAE,MAA2B,EACrC,QAAQ,GAAE,MAA2B,GACpC;IAAE,OAAO,EAAE,WAAW,EAAE,CAAC;IAAC,SAAS,EAAE,yBAAyB,EAAE,CAAA;CAAE,CAuCpE;AAED,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,yBAAyB,GAAG,MAAM,CAOhF;AAED,wBAAgB,eAAe,CAC7B,OAAO,EAAE,yBAAyB,EAAE,EACpC,YAAY,EAAE,GAAG,CAAC,MAAM,CAAC,GACxB,yBAAyB,EAAE,CAE7B"}
@@ -3,9 +3,16 @@
3
3
  * Licensed under the MIT license. See LICENSE file for details.
4
4
  */
5
5
  import fs from 'node:fs';
6
+ import { pipeline } from 'node:stream/promises';
6
7
  import { directoryExists, getFileSize } from './files.js';
7
8
  // split large files into 20MiB chunks
8
9
  export const KV_STORE_CHUNK_SIZE = 1_024 * 1_024 * 20;
10
+ // The size of each chunk of a file of `size` bytes. All chunks have chunkSize
11
+ // bytes, except the last, which has the bytes that are left.
12
+ export function chunkSizes(size, chunkSize) {
13
+ const numChunks = Math.ceil(size / chunkSize);
14
+ return Array.from({ length: numChunks }, (_, chunk) => Math.min(chunkSize, size - chunk * chunkSize));
15
+ }
9
16
  export function shouldRecreateChunks(chunksDir, numChunks, item, chunkSize) {
10
17
  console.log(` 📄 '${item.key}' - ${item.size} bytes → ${numChunks} chunks`);
11
18
  if (!directoryExists(chunksDir)) {
@@ -17,7 +24,7 @@ export function shouldRecreateChunks(chunksDir, numChunks, item, chunkSize) {
17
24
  console.log(` ・ Recreating chunks for '${item.key}' - found ${existingChunks} existing chunk(s), expected ${numChunks}`);
18
25
  return true;
19
26
  }
20
- const finalChunkSize = item.size % chunkSize;
27
+ const expectedFileSizes = chunkSizes(item.size, chunkSize);
21
28
  for (let chunk = 0; chunk < numChunks; chunk++) {
22
29
  const chunkFileName = `${chunksDir}/${chunk}`;
23
30
  // If the chunk does not exist
@@ -26,7 +33,7 @@ export function shouldRecreateChunks(chunksDir, numChunks, item, chunkSize) {
26
33
  return true;
27
34
  }
28
35
  // or if the chunk isn't the expected size
29
- const expectedFileSize = chunk === numChunks - 1 ? finalChunkSize : chunkSize;
36
+ const expectedFileSize = expectedFileSizes[chunk];
30
37
  const existingFileSize = getFileSize(chunkFileName);
31
38
  if (existingFileSize !== expectedFileSize) {
32
39
  console.log(` ・ Recreating chunks for '${item.key}' - chunk ${chunk} existing file size ${existingFileSize} does not match expected size ${expectedFileSize}`);
@@ -52,44 +59,13 @@ export async function applyKVStoreEntriesChunks(kvStoreItemDescriptions, chunkSi
52
59
  // Recreate chunks
53
60
  fs.rmSync(chunksDir, { recursive: true, force: true });
54
61
  fs.mkdirSync(chunksDir, { recursive: true });
55
- const numWrittenChunks = await new Promise((resolve, reject) => {
56
- const chunkPaths = [];
57
- let chunkIndex = 0;
58
- let bytesWritten = 0;
59
- let currentStream = null;
60
- const inputStream = fs.createReadStream(item.filePath);
61
- inputStream.on('error', reject);
62
- inputStream.on('data', (chunk) => {
63
- let offset = 0;
64
- while (offset < chunk.length) {
65
- if (!currentStream || bytesWritten >= chunkSize) {
66
- if (currentStream != null) {
67
- currentStream.end();
68
- }
69
- const chunkFileName = `${chunksDir}/${chunkIndex}`;
70
- currentStream = fs.createWriteStream(chunkFileName);
71
- chunkPaths.push(chunkFileName);
72
- chunkIndex++;
73
- bytesWritten = 0;
74
- }
75
- const bytesLeftInChunk = chunkSize - bytesWritten;
76
- const bytesLeftInBuffer = chunk.length - offset;
77
- const bytesToWrite = Math.min(bytesLeftInChunk, bytesLeftInBuffer);
78
- const slice = chunk.slice(offset, offset + bytesToWrite);
79
- currentStream.write(slice);
80
- bytesWritten += bytesToWrite;
81
- offset += bytesToWrite;
82
- }
83
- });
84
- inputStream.on('end', () => {
85
- if (currentStream != null) {
86
- currentStream.end();
87
- }
88
- resolve(chunkPaths.length);
89
- });
90
- });
91
- if (numWrittenChunks !== numChunks) {
92
- throw new Error(`numWrittenChunks (${numWrittenChunks}) does not equal numChunks (${numChunks})`);
62
+ // Each chunk is a copy of a range of the file. pipeline() resolves
63
+ // after the chunk file is completely written, so the upload reads the
64
+ // full chunk.
65
+ let start = 0;
66
+ for (const [chunkIndex, size] of chunkSizes(item.size, chunkSize).entries()) {
67
+ await pipeline(fs.createReadStream(item.filePath, { start, end: start + size - 1 }), fs.createWriteStream(`${chunksDir}/${chunkIndex}`));
68
+ start += size;
93
69
  }
94
70
  }
95
71
  for (let chunkIndex = 0; chunkIndex < numChunks; chunkIndex++) {