@fastly/compute-js-static-publish 6.3.0 → 7.0.0-beta.1

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 (238) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +31 -586
  3. package/README.short.md +53 -0
  4. package/build/cli/commands/clean.d.ts +2 -0
  5. package/build/cli/commands/clean.d.ts.map +1 -0
  6. package/build/cli/commands/clean.js +121 -0
  7. package/build/cli/commands/collections/delete.d.ts +2 -0
  8. package/build/cli/commands/collections/delete.d.ts.map +1 -0
  9. package/build/cli/commands/collections/delete.js +88 -0
  10. package/build/cli/commands/collections/index.d.ts +2 -0
  11. package/build/cli/commands/collections/index.d.ts.map +1 -0
  12. package/build/cli/commands/collections/index.js +52 -0
  13. package/build/cli/commands/collections/list.d.ts +2 -0
  14. package/build/cli/commands/collections/list.d.ts.map +1 -0
  15. package/build/cli/commands/collections/list.js +62 -0
  16. package/build/cli/commands/index.d.ts +2 -0
  17. package/build/cli/commands/index.d.ts.map +1 -0
  18. package/build/cli/commands/index.js +67 -0
  19. package/build/cli/commands/init-app.d.ts +18 -2
  20. package/build/cli/commands/init-app.d.ts.map +1 -1
  21. package/build/cli/commands/init-app.js +299 -266
  22. package/build/cli/commands/publish-content.d.ts +2 -0
  23. package/build/cli/commands/publish-content.d.ts.map +1 -0
  24. package/build/cli/commands/publish-content.js +472 -0
  25. package/build/cli/compression/brotli.d.ts +1 -1
  26. package/build/cli/compression/brotli.d.ts.map +1 -1
  27. package/build/cli/compression/brotli.js +8 -11
  28. package/build/cli/compression/gzip.d.ts +1 -1
  29. package/build/cli/compression/gzip.d.ts.map +1 -1
  30. package/build/cli/compression/gzip.js +8 -11
  31. package/build/cli/compression/index.d.ts +3 -6
  32. package/build/cli/compression/index.d.ts.map +1 -1
  33. package/build/cli/compression/index.js +8 -9
  34. package/build/cli/fastly-api/api-token.d.ts +17 -0
  35. package/build/cli/fastly-api/api-token.d.ts.map +1 -0
  36. package/build/cli/fastly-api/api-token.js +94 -0
  37. package/build/cli/fastly-api/kv-store.d.ts +24 -0
  38. package/build/cli/fastly-api/kv-store.d.ts.map +1 -0
  39. package/build/cli/{util → fastly-api}/kv-store.js +42 -22
  40. package/build/cli/index.js +7 -91
  41. package/build/cli/util/args.d.ts +16 -0
  42. package/build/cli/util/args.d.ts.map +1 -0
  43. package/build/cli/util/args.js +55 -0
  44. package/build/cli/util/config.d.ts +13 -0
  45. package/build/cli/util/config.d.ts.map +1 -0
  46. package/build/cli/{load-config.js → util/config.js} +234 -187
  47. package/build/{util → cli/util}/content-types.d.ts +1 -2
  48. package/build/cli/util/content-types.d.ts.map +1 -0
  49. package/build/{util → cli/util}/content-types.js +11 -6
  50. package/build/cli/util/data.d.ts +3 -3
  51. package/build/cli/util/data.d.ts.map +1 -1
  52. package/build/cli/util/data.js +6 -2
  53. package/build/cli/util/files.d.ts +10 -2
  54. package/build/cli/util/files.d.ts.map +1 -1
  55. package/build/cli/util/files.js +53 -6
  56. package/build/cli/util/kv-store-items.d.ts +12 -0
  57. package/build/cli/util/kv-store-items.d.ts.map +1 -0
  58. package/build/cli/util/kv-store-items.js +161 -0
  59. package/build/cli/util/kv-store-local-server.d.ts +3 -0
  60. package/build/cli/util/kv-store-local-server.d.ts.map +1 -0
  61. package/build/cli/util/kv-store-local-server.js +35 -0
  62. package/build/cli/util/package.d.ts +9 -0
  63. package/build/cli/util/package.d.ts.map +1 -0
  64. package/build/cli/util/package.js +44 -0
  65. package/build/cli/util/retryable.d.ts +12 -0
  66. package/build/cli/util/retryable.d.ts.map +1 -0
  67. package/build/cli/util/retryable.js +43 -0
  68. package/build/cli/util/variants.d.ts +4 -0
  69. package/build/cli/util/variants.d.ts.map +1 -0
  70. package/build/cli/util/variants.js +27 -0
  71. package/build/index.d.ts +5 -5
  72. package/build/index.d.ts.map +1 -1
  73. package/build/index.js +7 -6
  74. package/build/models/assets/kvstore-assets.d.ts +17 -0
  75. package/build/models/assets/kvstore-assets.d.ts.map +1 -0
  76. package/build/models/assets/kvstore-assets.js +28 -0
  77. package/build/{constants/compression.d.ts → models/compression/index.d.ts} +2 -1
  78. package/build/models/compression/index.d.ts.map +1 -0
  79. package/build/models/compression/index.js +11 -0
  80. package/build/models/config/publish-content-config.d.ts +41 -0
  81. package/build/models/config/publish-content-config.d.ts.map +1 -0
  82. package/build/models/config/publish-content-config.js +5 -0
  83. package/build/models/config/publisher-server-config.d.ts +20 -0
  84. package/build/models/config/publisher-server-config.d.ts.map +1 -0
  85. package/build/models/config/publisher-server-config.js +5 -0
  86. package/build/models/config/static-publish-rc.d.ts +6 -0
  87. package/build/models/config/static-publish-rc.d.ts.map +1 -0
  88. package/build/models/config/static-publish-rc.js +5 -0
  89. package/build/server/collection-selector/from-config-store.d.ts +2 -0
  90. package/build/server/collection-selector/from-config-store.d.ts.map +1 -0
  91. package/build/server/collection-selector/from-config-store.js +15 -0
  92. package/build/server/collection-selector/from-cookie.d.ts +23 -0
  93. package/build/server/collection-selector/from-cookie.d.ts.map +1 -0
  94. package/build/server/collection-selector/from-cookie.js +98 -0
  95. package/build/server/collection-selector/from-request.d.ts +13 -0
  96. package/build/server/collection-selector/from-request.d.ts.map +1 -0
  97. package/build/server/collection-selector/from-request.js +22 -0
  98. package/build/server/collection-selector/index.d.ts +5 -0
  99. package/build/server/collection-selector/index.d.ts.map +1 -0
  100. package/build/server/collection-selector/index.js +7 -0
  101. package/build/server/collection-selector/request-collection-selector.d.ts +5 -0
  102. package/build/server/collection-selector/request-collection-selector.d.ts.map +1 -0
  103. package/build/server/collection-selector/request-collection-selector.js +5 -0
  104. package/build/server/publisher-server/index.d.ts +38 -0
  105. package/build/server/publisher-server/index.d.ts.map +1 -0
  106. package/build/server/publisher-server/index.js +418 -0
  107. package/build/server/publisher-server/serve-preconditions/if-modified-since.d.ts +3 -0
  108. package/build/server/publisher-server/serve-preconditions/if-modified-since.d.ts.map +1 -0
  109. package/build/server/{serve-preconditions → publisher-server/serve-preconditions}/if-modified-since.js +6 -2
  110. package/build/server/publisher-server/serve-preconditions/if-none-match.d.ts.map +1 -0
  111. package/build/server/{serve-preconditions → publisher-server/serve-preconditions}/if-none-match.js +4 -0
  112. package/build/server/util/cookies.d.ts +2 -0
  113. package/build/server/util/cookies.d.ts.map +1 -0
  114. package/build/server/util/cookies.js +14 -0
  115. package/build/server/util/kv-store.d.ts +3 -0
  116. package/build/server/util/kv-store.d.ts.map +1 -0
  117. package/build/server/util/kv-store.js +95 -0
  118. package/package.json +18 -10
  119. package/build/cli/commands/build-static.d.ts +0 -3
  120. package/build/cli/commands/build-static.d.ts.map +0 -1
  121. package/build/cli/commands/build-static.js +0 -541
  122. package/build/cli/commands/clean-kv-store.d.ts +0 -3
  123. package/build/cli/commands/clean-kv-store.d.ts.map +0 -1
  124. package/build/cli/commands/clean-kv-store.js +0 -55
  125. package/build/cli/load-config.d.ts +0 -6
  126. package/build/cli/load-config.d.ts.map +0 -1
  127. package/build/cli/presets/implementations/astro.d.ts +0 -7
  128. package/build/cli/presets/implementations/astro.d.ts.map +0 -1
  129. package/build/cli/presets/implementations/astro.js +0 -23
  130. package/build/cli/presets/implementations/create-react-app.d.ts +0 -11
  131. package/build/cli/presets/implementations/create-react-app.d.ts.map +0 -1
  132. package/build/cli/presets/implementations/create-react-app.js +0 -34
  133. package/build/cli/presets/implementations/docusaurus.d.ts +0 -7
  134. package/build/cli/presets/implementations/docusaurus.d.ts.map +0 -1
  135. package/build/cli/presets/implementations/docusaurus.js +0 -13
  136. package/build/cli/presets/implementations/gatsby.d.ts +0 -7
  137. package/build/cli/presets/implementations/gatsby.d.ts.map +0 -1
  138. package/build/cli/presets/implementations/gatsby.js +0 -13
  139. package/build/cli/presets/implementations/next.d.ts +0 -7
  140. package/build/cli/presets/implementations/next.d.ts.map +0 -1
  141. package/build/cli/presets/implementations/next.js +0 -13
  142. package/build/cli/presets/implementations/sveltekit.d.ts +0 -7
  143. package/build/cli/presets/implementations/sveltekit.d.ts.map +0 -1
  144. package/build/cli/presets/implementations/sveltekit.js +0 -13
  145. package/build/cli/presets/implementations/vite.d.ts +0 -7
  146. package/build/cli/presets/implementations/vite.d.ts.map +0 -1
  147. package/build/cli/presets/implementations/vite.js +0 -13
  148. package/build/cli/presets/implementations/vue.d.ts +0 -7
  149. package/build/cli/presets/implementations/vue.d.ts.map +0 -1
  150. package/build/cli/presets/implementations/vue.js +0 -24
  151. package/build/cli/presets/index.d.ts +0 -3
  152. package/build/cli/presets/index.d.ts.map +0 -1
  153. package/build/cli/presets/index.js +0 -20
  154. package/build/cli/presets/preset-base.d.ts +0 -21
  155. package/build/cli/presets/preset-base.d.ts.map +0 -1
  156. package/build/cli/presets/preset-base.js +0 -1
  157. package/build/cli/util/fastly-api.d.ts +0 -10
  158. package/build/cli/util/fastly-api.d.ts.map +0 -1
  159. package/build/cli/util/fastly-api.js +0 -47
  160. package/build/cli/util/hash.d.ts +0 -5
  161. package/build/cli/util/hash.d.ts.map +0 -1
  162. package/build/cli/util/hash.js +0 -9
  163. package/build/cli/util/id.d.ts +0 -3
  164. package/build/cli/util/id.d.ts.map +0 -1
  165. package/build/cli/util/id.js +0 -17
  166. package/build/cli/util/kv-store.d.ts +0 -16
  167. package/build/cli/util/kv-store.d.ts.map +0 -1
  168. package/build/cli/util/publish-id.d.ts +0 -5
  169. package/build/cli/util/publish-id.d.ts.map +0 -1
  170. package/build/cli/util/publish-id.js +0 -26
  171. package/build/compute-js.d.ts +0 -3
  172. package/build/compute-js.d.ts.map +0 -1
  173. package/build/compute-js.js +0 -2
  174. package/build/constants/compression.d.ts.map +0 -1
  175. package/build/constants/compression.js +0 -4
  176. package/build/server/assets/asset-manager.d.ts +0 -8
  177. package/build/server/assets/asset-manager.d.ts.map +0 -1
  178. package/build/server/assets/asset-manager.js +0 -14
  179. package/build/server/assets/content-asset-kv-store.d.ts +0 -19
  180. package/build/server/assets/content-asset-kv-store.d.ts.map +0 -1
  181. package/build/server/assets/content-asset-kv-store.js +0 -70
  182. package/build/server/assets/content-asset-wasm-inline.d.ts +0 -18
  183. package/build/server/assets/content-asset-wasm-inline.d.ts.map +0 -1
  184. package/build/server/assets/content-asset-wasm-inline.js +0 -52
  185. package/build/server/assets/content-assets.d.ts +0 -46
  186. package/build/server/assets/content-assets.d.ts.map +0 -1
  187. package/build/server/assets/content-assets.js +0 -100
  188. package/build/server/assets/module-assets.d.ts +0 -23
  189. package/build/server/assets/module-assets.d.ts.map +0 -1
  190. package/build/server/assets/module-assets.js +0 -45
  191. package/build/server/kv-store/inline-store-entry.d.ts +0 -25
  192. package/build/server/kv-store/inline-store-entry.d.ts.map +0 -1
  193. package/build/server/kv-store/inline-store-entry.js +0 -100
  194. package/build/server/publisher-server.d.ts +0 -23
  195. package/build/server/publisher-server.d.ts.map +0 -1
  196. package/build/server/publisher-server.js +0 -257
  197. package/build/server/serve-preconditions/if-modified-since.d.ts +0 -4
  198. package/build/server/serve-preconditions/if-modified-since.d.ts.map +0 -1
  199. package/build/server/serve-preconditions/if-none-match.d.ts.map +0 -1
  200. package/build/server/util/http.d.ts +0 -2
  201. package/build/server/util/http.d.ts.map +0 -1
  202. package/build/server/util/http.js +0 -9
  203. package/build/types/compute.d.ts +0 -11
  204. package/build/types/compute.d.ts.map +0 -1
  205. package/build/types/compute.js +0 -1
  206. package/build/types/config-normalized.d.ts +0 -34
  207. package/build/types/config-normalized.d.ts.map +0 -1
  208. package/build/types/config-normalized.js +0 -1
  209. package/build/types/config.d.ts +0 -37
  210. package/build/types/config.d.ts.map +0 -1
  211. package/build/types/config.js +0 -1
  212. package/build/types/content-assets.d.ts +0 -55
  213. package/build/types/content-assets.d.ts.map +0 -1
  214. package/build/types/content-assets.js +0 -1
  215. package/build/types/content-types.d.ts +0 -11
  216. package/build/types/content-types.d.ts.map +0 -1
  217. package/build/types/content-types.js +0 -1
  218. package/build/types/index.d.ts +0 -7
  219. package/build/types/index.d.ts.map +0 -1
  220. package/build/types/index.js +0 -1
  221. package/build/types/module-assets.d.ts +0 -15
  222. package/build/types/module-assets.d.ts.map +0 -1
  223. package/build/types/module-assets.js +0 -1
  224. package/build/types/util.d.ts +0 -7
  225. package/build/types/util.d.ts.map +0 -1
  226. package/build/types/util.js +0 -1
  227. package/build/util/content-types.d.ts.map +0 -1
  228. package/build/util/index.d.ts +0 -3
  229. package/build/util/index.d.ts.map +0 -1
  230. package/build/util/index.js +0 -2
  231. package/build/util/metadata.d.ts +0 -3
  232. package/build/util/metadata.d.ts.map +0 -1
  233. package/build/util/metadata.js +0 -12
  234. package/resources/README.md +0 -3
  235. package/resources/statics-metadata.d.ts +0 -10
  236. package/resources/statics.d.ts +0 -19
  237. package/resources/webpack.config.js +0 -41
  238. /package/build/server/{serve-preconditions → publisher-server/serve-preconditions}/if-none-match.d.ts +0 -0
@@ -1,17 +1,23 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
1
5
  // This program creates a Fastly Compute JavaScript application
2
6
  // in a subfolder named compute-js.
3
7
  // This project can be served using fastly compute serve
4
8
  // or deployed to a Compute service using fastly compute publish.
5
- import * as child_process from "child_process";
6
- import * as path from "path";
7
- import * as fs from "fs";
8
- import * as url from 'url';
9
- import { presets } from '../presets/index.js';
9
+ import * as child_process from 'node:child_process';
10
+ import * as path from 'node:path';
11
+ import * as fs from 'node:fs';
12
+ import commandLineArgs from 'command-line-args';
13
+ import { dotRelative, rootRelative } from '../util/files.js';
14
+ import { findComputeJsStaticPublisherVersion } from '../util/package.js';
10
15
  const defaultOptions = {
16
+ outputDir: undefined,
11
17
  rootDir: undefined,
12
18
  publicDir: undefined,
13
19
  staticDirs: [],
14
- staticContentRootDir: undefined,
20
+ staticPublisherWorkingDir: undefined,
15
21
  spa: undefined,
16
22
  notFoundPage: '[public-dir]/404.html',
17
23
  autoIndex: ['index.html', 'index.htm'],
@@ -20,66 +26,77 @@ const defaultOptions = {
20
26
  name: 'compute-js-static-site',
21
27
  description: 'Fastly Compute static site',
22
28
  serviceId: undefined,
29
+ publishId: undefined,
23
30
  kvStoreName: undefined,
24
31
  };
25
- // Current directory of this program that's running.
26
- const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
27
- function processCommandLineArgs(commandLineValues) {
28
- // All paths are relative to CWD.
29
- let preset;
32
+ function buildOptions(packageJson, commandLineValues) {
33
+ // Applied in this order for proper overriding
34
+ // 1. defaults
35
+ // 2. package.json
36
+ // 3. command-line args
37
+ const options = structuredClone(defaultOptions);
38
+ if (packageJson?.name !== undefined) {
39
+ options.name = packageJson.name;
40
+ }
41
+ if (packageJson?.author !== undefined) {
42
+ options.author = packageJson.author;
43
+ }
44
+ if (packageJson?.description !== undefined) {
45
+ options.description = packageJson.description;
46
+ }
30
47
  {
31
- const presetValue = commandLineValues['preset'];
32
- if (presetValue == null || typeof presetValue === 'string') {
33
- preset = presetValue;
48
+ let outputDir;
49
+ const outputDirValue = commandLineValues['output'];
50
+ if (outputDirValue == null || typeof outputDirValue === 'string') {
51
+ outputDir = outputDirValue;
52
+ }
53
+ if (outputDir !== undefined) {
54
+ options.outputDir = outputDir;
34
55
  }
35
56
  }
36
- let rootDir;
37
- let publicDir;
38
57
  {
58
+ let rootDir;
39
59
  const rootDirValue = commandLineValues['root-dir'];
40
60
  if (rootDirValue == null || typeof rootDirValue === 'string') {
41
61
  rootDir = rootDirValue;
42
62
  }
43
- if (rootDir != null) {
44
- rootDir = path.resolve(rootDir);
63
+ if (rootDir !== undefined) {
64
+ options.rootDir = rootDir;
45
65
  }
66
+ }
67
+ {
68
+ let publicDir;
46
69
  const publicDirValue = commandLineValues['public-dir'];
47
70
  if (publicDirValue == null || typeof publicDirValue === 'string') {
48
71
  publicDir = publicDirValue;
49
72
  }
50
- if (publicDir != null) {
51
- publicDir = path.resolve(publicDir);
52
- }
53
- // If we don't have a preset, then for backwards compatibility
54
- // we check if we have public-dir but no root-dir. If that is
55
- // the case then we use the value public-dir as root-dir.
56
- if (preset == null && rootDir == null && publicDir != null) {
57
- rootDir = publicDir;
58
- publicDir = undefined;
73
+ if (publicDir !== undefined) {
74
+ options.publicDir = publicDir;
59
75
  }
60
76
  }
61
- // Filepaths provided on the command line are always given relative to CWD,
62
- // so we need to resolve them.
63
- let staticDirs;
64
77
  {
78
+ let staticDirs;
65
79
  const staticDirsValue = commandLineValues['static-dir'];
66
80
  const asArray = Array.isArray(staticDirsValue) ? staticDirsValue : [staticDirsValue];
67
81
  if (asArray.every((x) => typeof x === 'string')) {
68
- staticDirs = asArray.map(x => path.resolve(x));
82
+ staticDirs = asArray;
83
+ }
84
+ if (staticDirs !== undefined) {
85
+ options.staticDirs = staticDirs;
69
86
  }
70
87
  }
71
- let staticContentRootDir;
72
88
  {
73
- const staticContentRootDirValue = commandLineValues['static-content-root-dir'];
74
- if (staticContentRootDirValue == null || typeof staticContentRootDirValue === 'string') {
75
- staticContentRootDir = staticContentRootDirValue;
89
+ let staticPublisherWorkingDir;
90
+ const staticPublisherWorkingDirValue = commandLineValues['static-publisher-working-dir'];
91
+ if (staticPublisherWorkingDirValue == null || typeof staticPublisherWorkingDirValue === 'string') {
92
+ staticPublisherWorkingDir = staticPublisherWorkingDirValue;
76
93
  }
77
- if (staticContentRootDir != null) {
78
- staticContentRootDir = path.resolve(staticContentRootDir);
94
+ if (staticPublisherWorkingDir !== undefined) {
95
+ options.staticPublisherWorkingDir = staticPublisherWorkingDir;
79
96
  }
80
97
  }
81
- let spa;
82
98
  {
99
+ let spa;
83
100
  const spaValue = commandLineValues['spa'];
84
101
  if (spaValue === null) {
85
102
  // If 'spa' is provided with a null value, then the flag was provided
@@ -90,9 +107,12 @@ function processCommandLineArgs(commandLineValues) {
90
107
  else if (spaValue == null || typeof spaValue === 'string') {
91
108
  spa = spaValue;
92
109
  }
110
+ if (spa !== undefined) {
111
+ options.spa = spa;
112
+ }
93
113
  }
94
- let notFoundPage;
95
114
  {
115
+ let notFoundPage;
96
116
  const notFoundPageValue = commandLineValues['not-found-page'];
97
117
  if (notFoundPageValue === null) {
98
118
  // If 'spa' is provided with a null value, then the flag was provided
@@ -103,9 +123,12 @@ function processCommandLineArgs(commandLineValues) {
103
123
  else if (notFoundPageValue == null || typeof notFoundPageValue === 'string') {
104
124
  notFoundPage = notFoundPageValue;
105
125
  }
126
+ if (notFoundPage !== undefined) {
127
+ options.notFoundPage = notFoundPage;
128
+ }
106
129
  }
107
- let autoIndex;
108
130
  {
131
+ let autoIndex;
109
132
  const autoIndexValue = commandLineValues['auto-index'];
110
133
  const asArray = Array.isArray(autoIndexValue) ? autoIndexValue : [autoIndexValue];
111
134
  if (asArray.every((x) => typeof x === 'string')) {
@@ -120,9 +143,12 @@ function processCommandLineArgs(commandLineValues) {
120
143
  return acc;
121
144
  }, []);
122
145
  }
146
+ if (autoIndex !== undefined) {
147
+ options.autoIndex = autoIndex;
148
+ }
123
149
  }
124
- let autoExt = [];
125
150
  {
151
+ let autoExt = [];
126
152
  const autoExtValue = commandLineValues['auto-ext'];
127
153
  const asArray = Array.isArray(autoExtValue) ? autoExtValue : [autoExtValue];
128
154
  if (asArray.every((x) => typeof x === 'string')) {
@@ -138,90 +164,138 @@ function processCommandLineArgs(commandLineValues) {
138
164
  return acc;
139
165
  }, []);
140
166
  }
167
+ if (autoExt !== undefined) {
168
+ options.autoExt = autoExt;
169
+ }
141
170
  }
142
- let name;
143
171
  {
172
+ let name;
144
173
  const nameValue = commandLineValues['name'];
145
174
  if (nameValue == null || typeof nameValue === 'string') {
146
175
  name = nameValue;
147
176
  }
177
+ if (name !== undefined) {
178
+ options.name = name;
179
+ }
148
180
  }
149
- let author;
150
181
  {
182
+ let author;
151
183
  const authorValue = commandLineValues['author'];
152
184
  if (authorValue == null || typeof authorValue === 'string') {
153
185
  author = authorValue;
154
186
  }
187
+ if (author !== undefined) {
188
+ options.author = author;
189
+ }
155
190
  }
156
- let description;
157
191
  {
192
+ let description;
158
193
  const descriptionValue = commandLineValues['description'];
159
194
  if (descriptionValue == null || typeof descriptionValue === 'string') {
160
195
  description = descriptionValue;
161
196
  }
197
+ if (description !== undefined) {
198
+ options.description = description;
199
+ }
162
200
  }
163
- let serviceId;
164
201
  {
202
+ let serviceId;
165
203
  const serviceIdValue = commandLineValues['service-id'];
166
204
  if (serviceIdValue == null || typeof serviceIdValue === 'string') {
167
205
  serviceId = serviceIdValue;
168
206
  }
207
+ if (serviceId !== undefined) {
208
+ options.serviceId = serviceId;
209
+ }
210
+ }
211
+ {
212
+ let publishId;
213
+ const publishIdValue = commandLineValues['publish-id'];
214
+ if (publishIdValue == null || typeof publishIdValue === 'string') {
215
+ publishId = publishIdValue;
216
+ }
217
+ if (publishId !== undefined) {
218
+ options.publishId = publishId;
219
+ }
169
220
  }
170
- let kvStoreName;
171
221
  {
222
+ let kvStoreName;
172
223
  const kvStoreNameValue = commandLineValues['kv-store-name'];
173
224
  if (kvStoreNameValue == null || typeof kvStoreNameValue === 'string') {
174
225
  kvStoreName = kvStoreNameValue;
175
226
  }
176
- }
177
- return {
178
- rootDir,
179
- publicDir,
180
- staticDirs,
181
- staticContentRootDir,
182
- spa,
183
- notFoundPage,
184
- autoIndex,
185
- autoExt,
186
- name,
187
- author,
188
- description,
189
- serviceId,
190
- kvStoreName,
191
- };
192
- }
193
- function pickKeys(keys, object) {
194
- const result = {};
195
- for (const key of keys) {
196
- if (object[key] !== undefined) {
197
- result[key] = object[key];
227
+ if (kvStoreName !== undefined) {
228
+ options.kvStoreName = kvStoreName;
198
229
  }
199
230
  }
200
- return result;
231
+ return options;
201
232
  }
202
233
  const PUBLIC_DIR_TOKEN = '[public-dir]';
203
234
  function processPublicDirToken(filepath, publicDir) {
204
235
  if (!filepath.startsWith(PUBLIC_DIR_TOKEN)) {
205
- return filepath;
236
+ return path.resolve(filepath);
206
237
  }
207
238
  const processedPath = '.' + filepath.slice(PUBLIC_DIR_TOKEN.length);
208
- const resolvedPath = path.resolve(publicDir, processedPath);
209
- return path.relative(path.resolve(), resolvedPath);
239
+ return path.resolve(publicDir, processedPath);
210
240
  }
211
- export function initApp(commandLineValues) {
212
- let options = defaultOptions;
213
- let preset = null;
214
- const presetName = commandLineValues['preset'] ?? 'none';
215
- if (presetName !== 'none') {
216
- const presetClass = presets[presetName];
217
- if (presetClass == null) {
218
- console.error('Unknown preset name.');
219
- console.error("--preset must be one of: none, " + (Object.keys(presets).join(', ')));
220
- process.exitCode = 1;
221
- return;
222
- }
223
- preset = new presetClass();
224
- }
241
+ export async function action(argv) {
242
+ const optionDefinitions = [
243
+ { name: 'verbose', type: Boolean },
244
+ // Required. The name of a Fastly KV Store to hold the content assets.
245
+ // It is also added to the fastly.toml that is generated.
246
+ { name: 'kv-store-name', type: String, },
247
+ // Output directory. "Required" (if not specified, then defaultValue is used).
248
+ { name: 'output', alias: 'o', type: String, defaultValue: './compute-js', },
249
+ // Name of the application, to be inserted into fastly.toml
250
+ { name: 'name', type: String, },
251
+ // Description of the application, to be inserted into fastly.toml
252
+ { name: 'description', type: String, },
253
+ // Name of the author, to be inserted into fastly.toml
254
+ { name: 'author', type: String, },
255
+ // Fastly Service ID to be added to the fastly.toml that is generated.
256
+ { name: 'service-id', type: String },
257
+ // Required. The 'root' directory for the publishing.
258
+ // All assets are expected to exist under this root. Required.
259
+ // For backwards compatibility, if this value is not provided,
260
+ // then the value of 'public-dir' is used.
261
+ { name: 'root-dir', type: String, },
262
+ // Publish ID to be used as a prefix for all KV Store entries.
263
+ // If not provided, the default value of 'default' is used.
264
+ { name: 'publish-id', type: String, },
265
+ // The 'static publisher working directory' is the directory under the Compute
266
+ // application where asset files are written in preparation for upload to the
267
+ // KV Store and for serving for local mode.
268
+ { name: 'static-publisher-working-dir', type: String, },
269
+ // The 'public' directory. The Publisher Server will
270
+ // resolve requests relative to this directory. If not specified,
271
+ // defaults to the same value as 'root-dir'. See README for
272
+ // details.
273
+ { name: 'public-dir', type: String, },
274
+ // Directories to specify as containing 'static' files. The
275
+ // Publisher Server will serve files from these directories
276
+ // with a long TTL.
277
+ { name: 'static-dir', type: String, multiple: true, },
278
+ // Path to a file to be used to serve in a SPA application.
279
+ // The Publisher Server will serve this file with a 200 status code
280
+ // when the request doesn't match a known file, and the accept
281
+ // header includes text/html. You may use the '[public-dir]' token
282
+ // if you wish to specify this as a relative path from the 'public-dir'.
283
+ { name: 'spa', type: String, },
284
+ // Path to a file to be used to serve as a 404 not found page.
285
+ // The Publisher Server will serve this file with a 404 status code
286
+ // when the request doesn't match a known file, and the accept
287
+ // header includes text/html. You may use the '[public-dir]' token
288
+ // if you wish to specify this as a relative path from the 'public-dir'.
289
+ { name: 'not-found-page', type: String, },
290
+ // List of files to automatically use as index, for example, index.html,index.htm
291
+ // If a request comes in but the route does not exist, we check the route
292
+ // plus a slash plus the items in this array.
293
+ { name: 'auto-index', type: String, multiple: true, },
294
+ // List of extensions to apply to a path name, for example, if
295
+ // http://example.com/about is requested, we can respond with http://example.com/about.html
296
+ { name: 'auto-ext', type: String, multiple: true, },
297
+ ];
298
+ const commandLineValues = commandLineArgs(optionDefinitions, { argv });
225
299
  let packageJson;
226
300
  try {
227
301
  const packageJsonText = fs.readFileSync("./package.json", "utf-8");
@@ -231,53 +305,13 @@ export function initApp(commandLineValues) {
231
305
  console.log("Can't read/parse package.json in current directory, making no assumptions!");
232
306
  packageJson = null;
233
307
  }
234
- // Get the current compute js static publisher version.
235
- let computeJsStaticPublisherVersion = null;
236
- if (packageJson != null) {
237
- // First try current project's package.json
238
- computeJsStaticPublisherVersion =
239
- packageJson.dependencies?.["@fastly/compute-js-static-publish"] ??
240
- packageJson.devDependencies?.["@fastly/compute-js-static-publish"];
241
- // This may be a file url if during development
242
- if (computeJsStaticPublisherVersion != null) {
243
- if (computeJsStaticPublisherVersion.startsWith('file:')) {
244
- // this is a relative path from the current directory.
245
- // we replace it with an absolute path
246
- const relPath = computeJsStaticPublisherVersion.slice('file:'.length);
247
- const absPath = path.resolve(relPath);
248
- computeJsStaticPublisherVersion = 'file:' + absPath;
249
- }
250
- }
251
- }
252
- if (computeJsStaticPublisherVersion == null) {
253
- // Also try package.json of the package that contains the currently running program
254
- // This is used when the program doesn't actually install the package (running via npx).
255
- const computeJsStaticPublishPackageJsonPath = path.resolve(__dirname, '../../../package.json');
256
- const computeJsStaticPublishPackageJsonText = fs.readFileSync(computeJsStaticPublishPackageJsonPath, 'utf-8');
257
- const computeJsStaticPublishPackageJson = JSON.parse(computeJsStaticPublishPackageJsonText);
258
- computeJsStaticPublisherVersion = computeJsStaticPublishPackageJson?.version;
259
- }
260
- if (computeJsStaticPublisherVersion == null) {
261
- // Unexpected, but if it's still null then we go to a literal
262
- computeJsStaticPublisherVersion = '^4.0.0';
263
- }
264
- const commandLineAppOptions = processCommandLineArgs(commandLineValues);
265
- options = {
266
- ...options,
267
- ...(preset != null ? preset.defaultOptions : {}),
268
- ...pickKeys(['author', 'name', 'description'], (packageJson ?? {})),
269
- ...pickKeys(['rootDir', 'publicDir', 'staticDirs', 'staticContentRootDir', 'spa', 'notFoundPage', 'autoIndex', 'autoExt', 'author', 'name', 'description', 'serviceId', 'kvStoreName'], commandLineAppOptions),
270
- };
271
- if (preset != null) {
272
- if (!preset.check(packageJson, options)) {
273
- console.log("Failed preset check.");
274
- process.exitCode = 1;
275
- return;
276
- }
277
- }
278
- // Webpack now optional as of v4
279
- const useWebpack = commandLineValues['webpack'];
280
- const COMPUTE_JS_DIR = commandLineValues.output;
308
+ const options = buildOptions(packageJson, commandLineValues);
309
+ const COMPUTE_JS_DIR = options.outputDir;
310
+ if (COMPUTE_JS_DIR == null) {
311
+ console.error("❌ required parameter --output not provided.");
312
+ process.exitCode = 1;
313
+ return;
314
+ }
281
315
  const computeJsDir = path.resolve(COMPUTE_JS_DIR);
282
316
  // Resolve the root dir, relative to current directory, and make sure it exists.
283
317
  const ROOT_DIR = options.rootDir;
@@ -289,7 +323,7 @@ export function initApp(commandLineValues) {
289
323
  const rootDir = path.resolve(ROOT_DIR);
290
324
  if (!fs.existsSync(rootDir) || !fs.statSync(rootDir).isDirectory()) {
291
325
  console.error(`❌ Specified root directory '${ROOT_DIR}' does not exist.`);
292
- console.error(` * ${rootDir} must exist and be a directory.`);
326
+ console.error(` * ${rootRelative(rootDir)} must exist and be a directory.`);
293
327
  process.exitCode = 1;
294
328
  return;
295
329
  }
@@ -298,14 +332,14 @@ export function initApp(commandLineValues) {
298
332
  const publicDir = path.resolve(PUBLIC_DIR);
299
333
  if (!fs.existsSync(publicDir) || !fs.statSync(publicDir).isDirectory()) {
300
334
  console.error(`❌ Specified public directory '${PUBLIC_DIR}' does not exist.`);
301
- console.error(` * ${publicDir} must exist and be a directory.`);
335
+ console.error(` * ${rootRelative(publicDir)} must exist and be a directory.`);
302
336
  process.exitCode = 1;
303
337
  return;
304
338
  }
305
339
  // Public dir must be inside the root dir.
306
340
  if (!publicDir.startsWith(rootDir)) {
307
341
  console.error(`❌ Specified public directory '${PUBLIC_DIR}' is not under the asset root directory.`);
308
- console.error(` * ${publicDir} must be under ${rootDir}`);
342
+ console.error(` * ${rootRelative(publicDir)} must be under ${rootRelative(rootDir)}`);
309
343
  process.exitCode = 1;
310
344
  return;
311
345
  }
@@ -314,68 +348,66 @@ export function initApp(commandLineValues) {
314
348
  const staticDirs = [];
315
349
  for (const STATIC_DIR of STATIC_DIRS) {
316
350
  // For backwards compatibility, these values can start with [public-dir]
317
- const staticDir = path.resolve(processPublicDirToken(STATIC_DIR, publicDir));
351
+ const staticDir = processPublicDirToken(STATIC_DIR, publicDir);
318
352
  if (!staticDir.startsWith(publicDir)) {
319
353
  console.log(`⚠️ Ignoring static directory '${STATIC_DIR}'`);
320
- console.log(` * ${staticDir} is not under ${publicDir}`);
354
+ console.log(` * ${rootRelative(staticDir)} is not under ${rootRelative(publicDir)}`);
321
355
  continue;
322
356
  }
323
357
  if (!fs.existsSync(staticDir) || !fs.statSync(staticDir).isDirectory()) {
324
358
  console.log(`⚠️ Ignoring static directory '${STATIC_DIR}'`);
325
- console.log(` * ${staticDir} does not exist or is not a directory.`);
359
+ console.log(` * ${rootRelative(staticDir)} does not exist or is not a directory.`);
326
360
  continue;
327
361
  }
328
362
  staticDirs.push(staticDir);
329
363
  }
330
- // Static Content Root Dir must be under the current dir
331
- let staticContentRootDir = options.staticContentRootDir;
332
- if (staticContentRootDir == null) {
333
- staticContentRootDir = path.resolve(computeJsDir, './static-publisher');
334
- }
335
- if (!staticContentRootDir.startsWith(computeJsDir) ||
336
- staticContentRootDir === computeJsDir ||
337
- staticContentRootDir === path.resolve(computeJsDir, './bin') ||
338
- staticContentRootDir === path.resolve(computeJsDir, './pkg') ||
339
- staticContentRootDir === path.resolve(computeJsDir, './node_modules')) {
340
- console.error(`❌ Specified static content root directory '${staticContentRootDir}' must be under ${computeJsDir}.`);
341
- console.error(` It also must not be bin, pkg, or node_modules.`);
364
+ // Static Publisher Working Root Dir must be under the current dir
365
+ // This comes in relative to cwd.
366
+ const STATIC_PUBLISHER_WORKING_DIR = options.staticPublisherWorkingDir ??
367
+ path.resolve(computeJsDir, './static-publisher');
368
+ const staticPublisherWorkingDir = path.resolve(STATIC_PUBLISHER_WORKING_DIR);
369
+ if (!staticPublisherWorkingDir.startsWith(computeJsDir) ||
370
+ staticPublisherWorkingDir === computeJsDir ||
371
+ staticPublisherWorkingDir === path.resolve(computeJsDir, './bin') ||
372
+ staticPublisherWorkingDir === path.resolve(computeJsDir, './pkg') ||
373
+ staticPublisherWorkingDir === path.resolve(computeJsDir, './src') ||
374
+ staticPublisherWorkingDir === path.resolve(computeJsDir, './node_modules')) {
375
+ console.error(`❌ Specified static publisher working directory '${rootRelative(staticPublisherWorkingDir)}' must be under ${rootRelative(computeJsDir)}.`);
376
+ console.error(` It also must not be bin, pkg, src, or node_modules.`);
342
377
  process.exitCode = 1;
343
378
  return;
344
379
  }
345
- staticContentRootDir = './' + path.relative(computeJsDir, staticContentRootDir);
346
380
  // SPA and Not Found are relative to the asset root dir.
347
381
  const SPA = options.spa;
348
382
  let spaFilename;
349
383
  if (SPA != null) {
350
384
  // If it starts with [public-dir], then resolve it relative to public directory.
351
- spaFilename = path.resolve(processPublicDirToken(SPA, publicDir));
385
+ spaFilename = processPublicDirToken(SPA, publicDir);
352
386
  // At any rate it must exist under the root directory
353
387
  if (!spaFilename.startsWith(rootDir)) {
354
388
  console.log(`⚠️ Ignoring specified SPA file '${SPA}' as is not under the asset root directory.`);
355
- console.log(` * ${spaFilename} is not under ${rootDir}`);
389
+ console.log(` * ${rootRelative(spaFilename)} is not under ${rootRelative(rootDir)}`);
356
390
  spaFilename = undefined;
357
391
  }
358
392
  else if (!fs.existsSync(spaFilename)) {
359
- console.log(`⚠️ Ignoring specified SPA file '${SPA}' as it does not exist.`);
360
- console.log(` * ${spaFilename} does not exist.`);
361
- spaFilename = undefined;
393
+ console.log(`⚠️ Warning: Ignoring specified SPA file '${SPA}' does not exist.`);
394
+ console.log(` * ${rootRelative(spaFilename)} does not exist.`);
362
395
  }
363
396
  }
364
397
  const NOT_FOUND_PAGE = options.notFoundPage;
365
398
  let notFoundPageFilename;
366
399
  if (NOT_FOUND_PAGE != null) {
367
400
  // If it starts with [public-dir], then resolve it relative to public directory.
368
- notFoundPageFilename = path.resolve(processPublicDirToken(NOT_FOUND_PAGE, publicDir));
401
+ notFoundPageFilename = processPublicDirToken(NOT_FOUND_PAGE, publicDir);
369
402
  // At any rate it must exist under the root directory
370
403
  if (!notFoundPageFilename.startsWith(rootDir)) {
371
404
  console.log(`⚠️ Ignoring specified Not Found file '${NOT_FOUND_PAGE}' as is not under the asset root directory.`);
372
- console.log(` * ${notFoundPageFilename} is not under ${rootDir}`);
405
+ console.log(` * ${rootRelative(notFoundPageFilename)} is not under ${rootRelative(rootDir)}`);
373
406
  notFoundPageFilename = undefined;
374
407
  }
375
408
  else if (!fs.existsSync(notFoundPageFilename)) {
376
- console.log(`⚠️ Ignoring specified Not Found file '${NOT_FOUND_PAGE}' as it does not exist.`);
377
- console.log(` * ${notFoundPageFilename} does not exist.`);
378
- notFoundPageFilename = undefined;
409
+ console.log(`⚠️ Warning: Ignoring specified Not Found file '${NOT_FOUND_PAGE}' as it does not exist.`);
410
+ console.log(` * ${rootRelative(notFoundPageFilename)} does not exist.`);
379
411
  }
380
412
  }
381
413
  const autoIndex = options.autoIndex;
@@ -384,8 +416,9 @@ export function initApp(commandLineValues) {
384
416
  if (exists) {
385
417
  console.error(`❌ '${COMPUTE_JS_DIR}' directory already exists!`);
386
418
  console.error(` You should not run this command if this directory exists.`);
387
- console.error(` If you need to re-scaffold the static publisher, delete the following directory and then try again:`);
388
- console.error(` ${computeJsDir}`);
419
+ console.error(` If you need to re-scaffold the static publisher Compute App,`);
420
+ console.error(` delete the following directory and then try again:`);
421
+ console.error(` ${rootRelative(computeJsDir)}`);
389
422
  process.exitCode = 1;
390
423
  return;
391
424
  }
@@ -394,116 +427,118 @@ export function initApp(commandLineValues) {
394
427
  const description = options.description;
395
428
  const fastlyServiceId = options.serviceId;
396
429
  const kvStoreName = options.kvStoreName;
397
- function rootRelative(itemPath) {
398
- if (itemPath == null) {
399
- return null;
400
- }
401
- const v = path.relative(path.resolve(), itemPath);
402
- return v.startsWith('..') ? v : './' + v;
430
+ if (kvStoreName == null) {
431
+ console.error(`❌ required parameter --kv-store-name not provided.`);
432
+ process.exitCode = 1;
433
+ return;
434
+ }
435
+ let publishId = options.publishId;
436
+ if (publishId == null) {
437
+ publishId = 'default';
403
438
  }
439
+ const defaultCollectionName = 'live';
404
440
  console.log('');
405
- console.log('Asset Root Dir :', rootRelative(rootDir));
406
- console.log('Public Dir :', rootRelative(publicDir));
407
- console.log('Static Dir :', staticDirs.length > 0 ? staticDirs.map(rootRelative) : '(None)');
408
- console.log('Static Content Root Dir :', staticContentRootDir);
409
- console.log('SPA :', rootRelative(spaFilename) ?? '(None)');
410
- console.log('404 Page :', rootRelative(notFoundPageFilename) ?? '(None)');
411
- console.log('Auto-Index :', autoIndex.length > 0 ? autoIndex.map(rootRelative) : '(None)');
412
- console.log('Auto-Ext :', autoExt.length > 0 ? autoExt.map(rootRelative) : '(None)');
413
- console.log('name :', name);
414
- console.log('author :', author);
415
- console.log('description :', description);
416
- console.log('Service ID :', fastlyServiceId ?? '(None)');
417
- console.log('KV Store Name :', kvStoreName ?? '(None)');
441
+ console.log('Compute Application Settings');
442
+ console.log('----------------------------');
443
+ console.log('Compute Application Output Dir :', rootRelative(computeJsDir));
444
+ console.log('name :', name);
445
+ console.log('author :', author);
446
+ console.log('description :', description);
447
+ console.log('Service ID :', fastlyServiceId ?? '(None)');
448
+ console.log('KV Store Name :', kvStoreName);
449
+ console.log('Default Collection Name :', defaultCollectionName);
450
+ console.log('Publish ID :', publishId);
451
+ console.log('');
452
+ console.log('Publish Settings');
453
+ console.log('----------------');
454
+ console.log('Publish Root Dir :', rootRelative(rootDir));
455
+ console.log('Publisher Working Dir :', rootRelative(staticPublisherWorkingDir));
456
+ console.log('');
457
+ console.log('Publisher Server Settings');
458
+ console.log('-------------------------');
459
+ console.log('Server Public Dir :', rootRelative(publicDir));
460
+ console.log('SPA :', spaFilename != null ? rootRelative(spaFilename) : '(None)');
461
+ console.log('404 Page :', notFoundPageFilename != null ? rootRelative(notFoundPageFilename) : '(None)');
462
+ console.log('Auto-Index :', autoIndex.length > 0 ? autoIndex : '(None)');
463
+ console.log('Auto-Ext :', autoExt.length > 0 ? autoExt : '(None)');
464
+ console.log('Static Files Dir :', staticDirs.length > 0 ? staticDirs.map(rootRelative) : '(None)');
418
465
  console.log('');
419
- if (useWebpack) {
420
- console.log('Creating project with webpack.');
421
- console.log('');
422
- }
423
466
  console.log("Initializing Compute Application in " + computeJsDir + "...");
424
467
  fs.mkdirSync(computeJsDir);
425
468
  fs.mkdirSync(path.resolve(computeJsDir, './src'));
426
- const staticContentRootDirFromRoot = staticContentRootDir.slice(1);
427
- let staticFiles = staticContentRootDirFromRoot;
428
- if (staticContentRootDirFromRoot === '/src') {
429
- staticFiles = `\
430
- ${staticContentRootDirFromRoot}/statics.js
431
- ${staticContentRootDirFromRoot}/statics.d.ts
432
- ${staticContentRootDirFromRoot}/statics-metadata.js
433
- ${staticContentRootDirFromRoot}/statics-metadata.d.ts
434
- ${staticContentRootDirFromRoot}/static-content`;
435
- }
469
+ const resourceFiles = {};
436
470
  // .gitignore
437
- const gitIgnoreContent = `\
471
+ resourceFiles['.gitignore'] = `\
438
472
  /node_modules
439
473
  /bin
440
474
  /pkg
441
- ${staticFiles}
475
+ /${path.relative(computeJsDir, staticPublisherWorkingDir)}
442
476
  `;
443
- const gitIgnorePath = path.resolve(computeJsDir, '.gitignore');
444
- fs.writeFileSync(gitIgnorePath, gitIgnoreContent, "utf-8");
445
477
  // package.json
446
- const packageJsonContent = {
478
+ const computeJsStaticPublisherVersion = findComputeJsStaticPublisherVersion(packageJson);
479
+ resourceFiles['package.json'] = JSON.stringify({
447
480
  name,
448
481
  version: '0.1.0',
449
482
  description,
450
483
  author,
451
484
  type: 'module',
452
485
  devDependencies: {
453
- "@fastly/cli": "^10.14.0",
486
+ "@fastly/cli": "^11.2.0",
454
487
  '@fastly/compute-js-static-publish': computeJsStaticPublisherVersion,
455
488
  },
456
489
  dependencies: {
457
- '@fastly/js-compute': '^3.0.0',
490
+ '@fastly/js-compute': '^3.26.0',
458
491
  },
459
492
  engines: {
460
- node: '>=18.0.0',
493
+ node: '>=20.11.0',
461
494
  },
462
- license: 'UNLICENSED',
463
495
  private: true,
464
496
  scripts: {
497
+ prestart: "npx @fastly/compute-js-static-publish publish-content --local-only",
465
498
  start: "fastly compute serve",
466
- deploy: "fastly compute publish",
467
- prebuild: 'npx @fastly/compute-js-static-publish --build-static',
499
+ "publish-service": "fastly compute publish",
500
+ "publish-content": 'npx @fastly/compute-js-static-publish publish-content',
468
501
  build: 'js-compute-runtime ./src/index.js ./bin/main.wasm'
469
502
  },
470
- };
471
- if (useWebpack) {
472
- delete packageJsonContent.type;
473
- packageJsonContent.devDependencies = {
474
- ...packageJsonContent.devDependencies,
475
- 'webpack': '^5.75.0',
476
- 'webpack-cli': '^5.0.0',
477
- };
478
- packageJsonContent.scripts = {
479
- ...packageJsonContent.scripts,
480
- prebuild: 'npx @fastly/compute-js-static-publish --build-static && webpack',
481
- build: 'js-compute-runtime ./bin/index.js ./bin/main.wasm'
482
- };
483
- }
484
- const packageJsonContentJson = JSON.stringify(packageJsonContent, undefined, 2);
485
- const packageJsonPath = path.resolve(computeJsDir, 'package.json');
486
- fs.writeFileSync(packageJsonPath, packageJsonContentJson, "utf-8");
503
+ }, undefined, 2);
487
504
  // fastly.toml
488
- // language=toml
489
- const fastlyTomlContent = `\
505
+ const localServerKvStorePath = dotRelative(computeJsDir, path.resolve(staticPublisherWorkingDir, 'kvstore.json'));
506
+ resourceFiles['fastly.toml'] = /* language=text */ `\
490
507
  # This file describes a Fastly Compute package. To learn more visit:
491
508
  # https://developer.fastly.com/reference/fastly-toml/
492
509
 
493
510
  authors = [ "${author}" ]
494
511
  description = "${description}"
495
512
  language = "javascript"
496
- manifest_version = 2
513
+ manifest_version = 3
497
514
  name = "${name}"
498
- ${fastlyServiceId != null ? `service_id = "${fastlyServiceId}"
499
- ` : ''}
515
+ ${fastlyServiceId != null ? `service_id = "${fastlyServiceId}"\n` : ''}
500
516
  [scripts]
501
- build = "npm run build"
502
- `;
503
- const fastlyTomlPath = path.resolve(computeJsDir, 'fastly.toml');
504
- fs.writeFileSync(fastlyTomlPath, fastlyTomlContent, "utf-8");
517
+ build = "npm run build"
518
+
519
+ [local_server.kv_stores]
520
+ ${kvStoreName} = { file = "${localServerKvStorePath}", format = "json" }
521
+
522
+ [setup.kv_stores.${kvStoreName}]
523
+ `;
524
+ // kvstore.json
525
+ resourceFiles[localServerKvStorePath] = '{}';
505
526
  // static-publish.rc.js
506
- const rootDirRel = path.relative(computeJsDir, rootDir);
527
+ resourceFiles['static-publish.rc.js'] = `\
528
+ /*
529
+ * Generated by @fastly/compute-js-static-publish.
530
+ */
531
+
532
+ /** @type {import('@fastly/compute-js-static-publish').StaticPublishRc} */
533
+ const rc = {
534
+ kvStoreName: ${JSON.stringify(kvStoreName)},
535
+ publishId: ${JSON.stringify(publishId)},
536
+ defaultCollectionName: ${JSON.stringify(defaultCollectionName)},
537
+ };
538
+
539
+ export default rc;
540
+ `;
541
+ // publish-content.config.js
507
542
  // publicDirPrefix -- if public dir is deeper than the root dir, then
508
543
  // public dir is used as a prefix to drill into asset names.
509
544
  // e.g.,
@@ -539,33 +574,32 @@ ${fastlyServiceId != null ? `service_id = "${fastlyServiceId}"
539
574
  // and that it exists
540
575
  notFoundPageFile = notFoundPageFilename.slice(rootDir.length);
541
576
  }
542
- const staticPublishJsContent = `\
577
+ resourceFiles['publish-content.config.js'] = `\
543
578
  /*
544
- * Copyright Fastly, Inc.
545
- * Licensed under the MIT license. See LICENSE file for details.
579
+ * Generated by @fastly/compute-js-static-publish.
546
580
  */
547
581
 
548
582
  // Commented items are defaults, feel free to modify and experiment!
549
583
  // See README for a detailed explanation of the configuration options.
550
584
 
551
- /** @type {import('@fastly/compute-js-static-publish').StaticPublisherConfig} */
585
+ /** @type {import('@fastly/compute-js-static-publish').PublishContentConfig} */
552
586
  const config = {
553
- rootDir: ${JSON.stringify(rootDirRel)},
554
- staticContentRootDir: ${JSON.stringify(staticContentRootDir)},
555
- ${(kvStoreName != null ? 'kvStoreName: ' + JSON.stringify(kvStoreName) : '// kvStoreName: false')},
587
+ rootDir: ${JSON.stringify(dotRelative(computeJsDir, rootDir))},
588
+ staticPublisherWorkingDir: ${JSON.stringify(dotRelative(computeJsDir, staticPublisherWorkingDir))},
556
589
  // excludeDirs: [ './node_modules' ],
557
590
  // excludeDotFiles: true,
558
591
  // includeWellKnown: true,
559
- // contentAssetInclusionTest: (filename) => true,
560
- // contentCompression: [ 'br', 'gzip' ], // For this config value, default is [] if kvStoreName is null.
561
- // moduleAssetInclusionTest: (filename) => false,
592
+ // kvStoreAssetInclusionTest: (assetKey) => true,
593
+ // contentCompression: [ 'br', 'gzip' ],
562
594
  // contentTypes: [
563
595
  // { test: /.custom$/, contentType: 'application/x-custom', text: false },
564
596
  // ],
597
+
598
+ // Server settings are saved to the KV Store per collection
565
599
  server: {
566
600
  publicDirPrefix: ${JSON.stringify(publicDirPrefix)},
567
601
  staticItems: ${JSON.stringify(staticItems)},
568
- // compression: [ 'br', 'gzip' ],
602
+ allowedEncodings: [ 'br', 'gzip' ],
569
603
  spaFile: ${JSON.stringify(spaFile)},
570
604
  notFoundPageFile: ${JSON.stringify(notFoundPageFile)},
571
605
  autoExt: ${JSON.stringify(autoExt)},
@@ -573,31 +607,25 @@ const config = {
573
607
  },
574
608
  };
575
609
 
576
- ${useWebpack ? 'module.exports =' : 'export default'} config;
610
+ export default config;
577
611
  `;
578
- const staticPublishJsonPath = path.resolve(computeJsDir, 'static-publish.rc.js');
579
- fs.writeFileSync(staticPublishJsonPath, staticPublishJsContent, "utf-8");
580
- // Copy resource files
581
- if (useWebpack) {
582
- // webpack.config.js
583
- const webpackConfigJsSrcPath = path.resolve(__dirname, '../../../resources/webpack.config.js');
584
- const webpackConfigJsPath = path.resolve(computeJsDir, 'webpack.config.js');
585
- fs.copyFileSync(webpackConfigJsSrcPath, webpackConfigJsPath);
586
- }
587
612
  // src/index.js
588
- const staticsRelativePath = staticContentRootDir !== './src' ?
589
- path.relative('./src', staticContentRootDir) :
590
- '.';
591
- const indexJsContent = /* language=JavaScript */ `\
613
+ resourceFiles['./src/index.js'] = /* language=text */ `\
592
614
  /// <reference types="@fastly/js-compute" />
593
- import { getServer } from '${staticsRelativePath}/statics.js';
594
- const staticContentServer = getServer();
615
+ import { env } from 'fastly:env';
616
+ import { PublisherServer } from '@fastly/compute-js-static-publish';
617
+ import rc from '../static-publish.rc.js';
618
+ const publisherServer = PublisherServer.fromStaticPublishRc(rc);
595
619
 
596
620
  // eslint-disable-next-line no-restricted-globals
597
621
  addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
598
622
  async function handleRequest(event) {
599
623
 
600
- const response = await staticContentServer.serveRequest(event.request);
624
+ console.log('FASTLY_SERVICE_VERSION', env('FASTLY_SERVICE_VERSION'));
625
+
626
+ const request = event.request;
627
+
628
+ const response = await publisherServer.serveRequest(request);
601
629
  if (response != null) {
602
630
  return response;
603
631
  }
@@ -608,8 +636,12 @@ async function handleRequest(event) {
608
636
  return new Response('Not found', { status: 404 });
609
637
  }
610
638
  `;
611
- const indexJsPath = path.resolve(computeJsDir, './src/index.js');
612
- fs.writeFileSync(indexJsPath, indexJsContent, 'utf-8');
639
+ // Write out the files
640
+ for (const [filename, content] of Object.entries(resourceFiles)) {
641
+ const filePath = path.resolve(computeJsDir, filename);
642
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
643
+ fs.writeFileSync(filePath, content, 'utf-8');
644
+ }
613
645
  console.log("🚀 Compute application created!");
614
646
  console.log('Installing dependencies...');
615
647
  console.log(`npm --prefix ${COMPUTE_JS_DIR} install`);
@@ -624,6 +656,7 @@ async function handleRequest(event) {
624
656
  console.log('To build and deploy to your Compute service:');
625
657
  console.log('');
626
658
  console.log(' cd ' + COMPUTE_JS_DIR);
627
- console.log(' npm run deploy');
659
+ console.log(' npm run publish-service');
660
+ console.log(' npm run publish-content');
628
661
  console.log('');
629
662
  }