@fastly/compute-js-static-publish 3.6.0 → 4.0.0-alpha.0
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.
- package/README.md +440 -103
- package/build/assets/asset-manager.d.ts +7 -0
- package/build/assets/asset-manager.d.ts.map +1 -0
- package/build/assets/asset-manager.js +11 -0
- package/build/assets/content-assets.d.ts +29 -0
- package/build/assets/content-assets.d.ts.map +1 -0
- package/build/assets/content-assets.js +88 -0
- package/build/assets/module-assets.d.ts +23 -0
- package/build/assets/module-assets.d.ts.map +1 -0
- package/build/assets/module-assets.js +45 -0
- package/build/cli/build-static.d.ts.map +1 -0
- package/build/cli/build-static.js +524 -0
- package/build/cli/clean-object-store.d.ts +2 -0
- package/build/cli/clean-object-store.d.ts.map +1 -0
- package/build/cli/cli.d.ts.map +1 -0
- package/build/{cli.js → cli/cli.js} +0 -0
- package/build/cli/commands/build-static.d.ts +3 -0
- package/build/cli/commands/build-static.d.ts.map +1 -0
- package/build/cli/commands/build-static.js +501 -0
- package/build/cli/commands/clean-object-store.d.ts +3 -0
- package/build/cli/commands/clean-object-store.d.ts.map +1 -0
- package/build/cli/commands/clean-object-store.js +54 -0
- package/build/cli/commands/init-app.d.ts.map +1 -0
- package/build/cli/commands/init-app.js +559 -0
- package/build/cli/compression/brotli.d.ts +3 -0
- package/build/cli/compression/brotli.d.ts.map +1 -0
- package/build/cli/compression/brotli.js +15 -0
- package/build/cli/compression/gzip.d.ts +3 -0
- package/build/cli/compression/gzip.d.ts.map +1 -0
- package/build/cli/compression/gzip.js +15 -0
- package/build/cli/compression/index.d.ts +8 -0
- package/build/cli/compression/index.d.ts.map +1 -0
- package/build/cli/compression/index.js +12 -0
- package/build/cli/index.d.ts +3 -0
- package/build/cli/index.d.ts.map +1 -0
- package/build/cli/index.js +86 -0
- package/build/cli/init-app.d.ts +3 -0
- package/build/cli/init-app.d.ts.map +1 -0
- package/build/{init-app.js → cli/init-app.js} +16 -16
- package/build/cli/load-config.d.ts +3 -0
- package/build/cli/load-config.d.ts.map +1 -0
- package/build/cli/load-config.js +357 -0
- package/build/cli/presets/implementations/create-react-app.d.ts +11 -0
- package/build/cli/presets/implementations/create-react-app.d.ts.map +1 -0
- package/build/cli/presets/implementations/create-react-app.js +34 -0
- package/build/cli/presets/implementations/docusaurus.d.ts +7 -0
- package/build/cli/presets/implementations/docusaurus.d.ts.map +1 -0
- package/build/cli/presets/implementations/docusaurus.js +13 -0
- package/build/cli/presets/implementations/gatsby.d.ts +7 -0
- package/build/cli/presets/implementations/gatsby.d.ts.map +1 -0
- package/build/cli/presets/implementations/gatsby.js +13 -0
- package/build/cli/presets/implementations/next.d.ts +7 -0
- package/build/cli/presets/implementations/next.d.ts.map +1 -0
- package/build/cli/presets/implementations/next.js +13 -0
- package/build/cli/presets/implementations/sveltekit.d.ts +7 -0
- package/build/cli/presets/implementations/sveltekit.d.ts.map +1 -0
- package/build/cli/presets/implementations/sveltekit.js +13 -0
- package/build/cli/presets/implementations/vite.d.ts +7 -0
- package/build/cli/presets/implementations/vite.d.ts.map +1 -0
- package/build/cli/presets/implementations/vite.js +13 -0
- package/build/cli/presets/index.d.ts +3 -0
- package/build/cli/presets/index.d.ts.map +1 -0
- package/build/cli/presets/index.js +16 -0
- package/build/cli/presets/preset-base.d.ts +19 -0
- package/build/cli/presets/preset-base.d.ts.map +1 -0
- package/build/cli/util/data.d.ts +7 -0
- package/build/cli/util/data.d.ts.map +1 -0
- package/build/cli/util/data.js +58 -0
- package/build/cli/util/fastly-api.d.ts +10 -0
- package/build/cli/util/fastly-api.d.ts.map +1 -0
- package/build/cli/util/fastly-api.js +47 -0
- package/build/cli/util/files.d.ts +11 -0
- package/build/cli/util/files.d.ts.map +1 -0
- package/build/cli/util/files.js +33 -0
- package/build/cli/util/hash.d.ts +2 -0
- package/build/cli/util/hash.d.ts.map +1 -0
- package/build/cli/util/hash.js +8 -0
- package/build/cli/util/id.d.ts +3 -0
- package/build/cli/util/id.d.ts.map +1 -0
- package/build/cli/util/id.js +17 -0
- package/build/cli/util/object-store.d.ts +16 -0
- package/build/cli/util/object-store.d.ts.map +1 -0
- package/build/cli/util/object-store.js +108 -0
- package/build/cli/util/publish-id.d.ts +5 -0
- package/build/cli/util/publish-id.d.ts.map +1 -0
- package/build/cli/util/publish-id.js +26 -0
- package/build/constants/compression.d.ts +3 -0
- package/build/constants/compression.d.ts.map +1 -0
- package/build/constants/compression.js +4 -0
- package/build/index.d.ts +5 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +5 -1
- package/build/presets/implementations/create-react-app.js +1 -2
- package/build/presets/preset-base.d.ts +1 -1
- package/build/presets/preset-base.d.ts.map +1 -1
- package/build/server/assets/asset-manager.d.ts +7 -0
- package/build/server/assets/asset-manager.d.ts.map +1 -0
- package/build/server/assets/asset-manager.js +11 -0
- package/build/server/assets/content-assets.d.ts +33 -0
- package/build/server/assets/content-assets.d.ts.map +1 -0
- package/build/server/assets/content-assets.js +135 -0
- package/build/server/assets/module-assets.d.ts +23 -0
- package/build/server/assets/module-assets.d.ts.map +1 -0
- package/build/server/assets/module-assets.js +45 -0
- package/build/server/object-store/include-bytes-store-entry.d.ts +19 -0
- package/build/server/object-store/include-bytes-store-entry.d.ts.map +1 -0
- package/build/server/object-store/include-bytes-store-entry.js +87 -0
- package/build/server/object-store/inline-store-entry.d.ts +23 -0
- package/build/server/object-store/inline-store-entry.d.ts.map +1 -0
- package/build/server/object-store/inline-store-entry.js +95 -0
- package/build/server/publisher-server.d.ts +23 -0
- package/build/server/publisher-server.d.ts.map +1 -0
- package/build/server/publisher-server.js +224 -0
- package/build/server/serve-preconditions/if-modified-since.d.ts +4 -0
- package/build/server/serve-preconditions/if-modified-since.d.ts.map +1 -0
- package/build/server/serve-preconditions/if-modified-since.js +26 -0
- package/build/server/serve-preconditions/if-none-match.d.ts +3 -0
- package/build/server/serve-preconditions/if-none-match.d.ts.map +1 -0
- package/build/server/serve-preconditions/if-none-match.js +24 -0
- package/build/server/util/http.d.ts +2 -0
- package/build/server/util/http.d.ts.map +1 -0
- package/build/server/util/http.js +9 -0
- package/build/types/compute.d.ts +10 -0
- package/build/types/compute.d.ts.map +1 -0
- package/build/types/compute.js +1 -0
- package/build/types/config-normalized.d.ts +33 -0
- package/build/types/config-normalized.d.ts.map +1 -0
- package/build/types/config-normalized.js +1 -0
- package/build/types/config.d.ts +36 -0
- package/build/types/config.d.ts.map +1 -0
- package/build/types/config.js +1 -0
- package/build/types/content-assets.d.ts +37 -0
- package/build/types/content-assets.d.ts.map +1 -0
- package/build/types/content-assets.js +1 -0
- package/build/types/content-types.d.ts +11 -0
- package/build/types/content-types.d.ts.map +1 -0
- package/build/types/content-types.js +1 -0
- package/build/types/index.d.ts +7 -0
- package/build/types/index.d.ts.map +1 -0
- package/build/types/index.js +1 -0
- package/build/types/module-assets.d.ts +15 -0
- package/build/types/module-assets.d.ts.map +1 -0
- package/build/types/module-assets.js +1 -0
- package/build/types/util.d.ts +7 -0
- package/build/types/util.d.ts.map +1 -0
- package/build/types/util.js +1 -0
- package/build/util/content-types.d.ts +5 -0
- package/build/util/content-types.d.ts.map +1 -0
- package/build/util/content-types.js +92 -0
- package/build/util/data.d.ts +7 -0
- package/build/util/data.d.ts.map +1 -0
- package/build/util/data.js +58 -0
- package/build/util/fastly-api.d.ts +10 -0
- package/build/util/fastly-api.d.ts.map +1 -0
- package/build/util/fastly-api.js +47 -0
- package/build/util/files.d.ts +10 -0
- package/build/util/files.d.ts.map +1 -0
- package/build/util/files.js +32 -0
- package/build/util/hash.d.ts +2 -0
- package/build/util/hash.d.ts.map +1 -0
- package/build/util/hash.js +8 -0
- package/build/util/id.d.ts +3 -0
- package/build/util/id.d.ts.map +1 -0
- package/build/util/id.js +17 -0
- package/build/util/index.d.ts +3 -0
- package/build/util/index.d.ts.map +1 -0
- package/build/util/index.js +2 -0
- package/build/util/metadata.d.ts +3 -0
- package/build/util/metadata.d.ts.map +1 -0
- package/build/util/metadata.js +12 -0
- package/build/util/object-store.d.ts +12 -0
- package/build/util/object-store.d.ts.map +1 -0
- package/build/util/object-store.js +80 -0
- package/package.json +10 -5
- package/resources/index.js +9 -114
- package/resources/statics-metadata.d.ts +10 -0
- package/resources/statics.d.ts +15 -7
- package/resources/webpack.config.js +4 -10
- package/build/assets.d.ts +0 -8
- package/build/assets.d.ts.map +0 -1
- package/build/assets.js +0 -24
- package/build/build-static.d.ts.map +0 -1
- package/build/build-static.js +0 -249
- package/build/cli.d.ts.map +0 -1
- package/build/init-app.d.ts.map +0 -1
- package/build/static-assets.d.ts +0 -11
- package/build/static-assets.d.ts.map +0 -1
- package/build/static-assets.js +0 -33
- package/build/types.d.ts +0 -50
- package/build/types.d.ts.map +0 -1
- package/resources/default-content-types.cjs +0 -106
- package/resources/default-content-types.d.ts +0 -15
- /package/build/{build-static.d.ts → cli/build-static.d.ts} +0 -0
- /package/build/{content-types.js → cli/clean-object-store.js} +0 -0
- /package/build/{cli.d.ts → cli/cli.d.ts} +0 -0
- /package/build/{init-app.d.ts → cli/commands/init-app.d.ts} +0 -0
- /package/build/{types.js → cli/presets/preset-base.js} +0 -0
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
// This program builds static resources out of the files in the
|
|
2
|
+
// public folder to be served.
|
|
3
|
+
// e.g., with create-react-app, this would be the ./build directory.
|
|
4
|
+
// Outputs:
|
|
5
|
+
// statics-metadata.js
|
|
6
|
+
// deploymentId: string - deployment ID
|
|
7
|
+
// contentAssetMetadataMap: Map<string, ContentAssetMetadata> - mapping of asset keys to their metadata
|
|
8
|
+
// - key: filename relative to public dir (e.g., /foo/index.html)
|
|
9
|
+
// - value:
|
|
10
|
+
// - size (to be implemented soon): number - File size in bytes
|
|
11
|
+
// - contentType: string - MIME type
|
|
12
|
+
// - timestamp (to be implemented soon): last modified time as unix time
|
|
13
|
+
// - etag (to be implemented soon): string - Etag
|
|
14
|
+
// - text: boolean - whether this is a text file
|
|
15
|
+
// statics.js
|
|
16
|
+
// imports statics-metadata.js and adds utilities to load the files
|
|
17
|
+
// contentAssetMap: Map<string, ContentAsset>
|
|
18
|
+
// - key: filename relative to public dir (e.g., /foo/index.html)
|
|
19
|
+
// - value:
|
|
20
|
+
// - getMetadata() - gets the metadata entry
|
|
21
|
+
// - getBody() - gets a Body object that represents the asset
|
|
22
|
+
// moduleAssetMap: Map<string, ModuleAsset>
|
|
23
|
+
// - key: filename relative to public dir (e.g., /foo/index.html)
|
|
24
|
+
// - value:
|
|
25
|
+
// - getMetadata() - gets the metadata entry
|
|
26
|
+
// - async getModule() - Returns a promise that imports the module and returns a reference.
|
|
27
|
+
// This simply returns the statically imported module if it is statically imported.
|
|
28
|
+
// - getStaticModule() - Advanced. Returns the statically imported module, or null.
|
|
29
|
+
// This also reexports values like the "spa" value in the static-publish.rc.js
|
|
30
|
+
// file so that the C@E handler knows what file to serve up if
|
|
31
|
+
// the resource doesn't map to a file.
|
|
32
|
+
import * as fs from "fs";
|
|
33
|
+
import * as path from "path";
|
|
34
|
+
import * as url from "url";
|
|
35
|
+
import { loadConfigFile } from "../load-config.js";
|
|
36
|
+
import { applyDefaults } from "../util/data.js";
|
|
37
|
+
import { calculateFileHash } from "../util/hash.js";
|
|
38
|
+
import { getFiles } from "../util/files.js";
|
|
39
|
+
import { generateOrLoadPublishId } from "../util/publish-id.js";
|
|
40
|
+
import { loadApiKey } from "../util/fastly-api.js";
|
|
41
|
+
import { objectStoreEntryExists, objectStoreSubmitFile } from "../util/object-store.js";
|
|
42
|
+
import { mergeContentTypes, testFileContentType } from "../../util/content-types.js";
|
|
43
|
+
import { algs } from "../compression/index.js";
|
|
44
|
+
async function uploadFilesToObjectStore(fastlyApiContext, objectStoreName, objectStoreItems) {
|
|
45
|
+
for (const { objectStoreKey, staticFilePath, text } of objectStoreItems) {
|
|
46
|
+
if (await objectStoreEntryExists(fastlyApiContext, objectStoreName, objectStoreKey)) {
|
|
47
|
+
// Already exists in Object Store
|
|
48
|
+
console.log(`✔️ Asset already exists in Object Store with key "${objectStoreKey}".`);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// Upload to Object Store
|
|
52
|
+
const fileData = fs.readFileSync(staticFilePath);
|
|
53
|
+
await objectStoreSubmitFile(fastlyApiContext, objectStoreName, objectStoreKey, fileData);
|
|
54
|
+
console.log(`✔️ Submitted ${text ? 'text' : 'binary'} asset "${staticFilePath}" to Object Store at key "${objectStoreKey}".`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function writeObjectStoreEntriesToFastlyToml(objectStoreName, objectStoreItems) {
|
|
59
|
+
let fastlyToml = fs.readFileSync('./fastly.toml', 'utf-8');
|
|
60
|
+
let before = '';
|
|
61
|
+
let after = '';
|
|
62
|
+
const tableMarker = `[[local_server.object_store.${objectStoreName}]]`;
|
|
63
|
+
const startPos = fastlyToml.indexOf(tableMarker);
|
|
64
|
+
if (startPos === -1) {
|
|
65
|
+
// Object store decl not in fastly.toml yet
|
|
66
|
+
if (fastlyToml.indexOf(objectStoreName) !== -1) {
|
|
67
|
+
// don't do this!
|
|
68
|
+
console.error("Don't do this!");
|
|
69
|
+
throw "No";
|
|
70
|
+
}
|
|
71
|
+
let newLines;
|
|
72
|
+
if (fastlyToml.endsWith('\n\n')) {
|
|
73
|
+
newLines = '';
|
|
74
|
+
}
|
|
75
|
+
else if (fastlyToml.endsWith('\n')) {
|
|
76
|
+
newLines = '\n';
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
newLines = '\n\n';
|
|
80
|
+
}
|
|
81
|
+
before = fastlyToml + newLines;
|
|
82
|
+
after = '';
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const lastObjStoreTablePos = fastlyToml.lastIndexOf(tableMarker);
|
|
86
|
+
const nextTablePos = fastlyToml.indexOf('[', lastObjStoreTablePos + tableMarker.length);
|
|
87
|
+
before = fastlyToml.slice(0, startPos);
|
|
88
|
+
if (nextTablePos === -1) {
|
|
89
|
+
after = '';
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
after = fastlyToml.slice(nextTablePos);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
let tablesToml = '';
|
|
96
|
+
for (const { objectStoreKey, staticFilePath } of objectStoreItems) {
|
|
97
|
+
// Probably, JSON.stringify is wrong, but it should do its job
|
|
98
|
+
tablesToml += tableMarker + '\n';
|
|
99
|
+
tablesToml += 'key = ' + JSON.stringify(objectStoreKey) + '\n';
|
|
100
|
+
tablesToml += 'path = ' + JSON.stringify(path.relative('./', staticFilePath)) + '\n';
|
|
101
|
+
tablesToml += '\n';
|
|
102
|
+
}
|
|
103
|
+
fastlyToml = before + tablesToml + after;
|
|
104
|
+
fs.writeFileSync('./fastly.toml', fastlyToml, 'utf-8');
|
|
105
|
+
}
|
|
106
|
+
export async function buildStaticLoader(commandLineValues) {
|
|
107
|
+
const { 'suppress-framework-warnings': suppressFrameworkWarnings } = commandLineValues;
|
|
108
|
+
const displayFrameworkWarnings = !suppressFrameworkWarnings;
|
|
109
|
+
const { publishId, created } = generateOrLoadPublishId();
|
|
110
|
+
if (created) {
|
|
111
|
+
console.log("✅ Created publish ID");
|
|
112
|
+
}
|
|
113
|
+
console.log(`Publish ID: ${publishId}`);
|
|
114
|
+
console.log("🚀 Building loader...");
|
|
115
|
+
const errors = [];
|
|
116
|
+
const config = await loadConfigFile(errors);
|
|
117
|
+
if (config == null) {
|
|
118
|
+
console.error("❌ Can't load static-publish.rc.js");
|
|
119
|
+
console.error("Run this from a compute-js-static-publish compute-js directory.");
|
|
120
|
+
for (const error of errors) {
|
|
121
|
+
console.error(error);
|
|
122
|
+
}
|
|
123
|
+
process.exitCode = 1;
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const outputDir = path.resolve();
|
|
127
|
+
const publicDirRoot = path.resolve(config.rootDir);
|
|
128
|
+
console.log(`✔️ Public directory '${publicDirRoot}'.`);
|
|
129
|
+
const objectStoreName = config.objectStore;
|
|
130
|
+
let fastlyApiContext = null;
|
|
131
|
+
if (objectStoreName != null) {
|
|
132
|
+
// TODO: load api key from command line
|
|
133
|
+
const apiKeyResult = loadApiKey();
|
|
134
|
+
if (apiKeyResult == null) {
|
|
135
|
+
console.error("❌ Fastly API Token not provided.");
|
|
136
|
+
console.error("Specify one on the command line, or use the FASTLY_API_TOKEN environment variable.");
|
|
137
|
+
process.exitCode = 1;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
fastlyApiContext = { apiToken: apiKeyResult.apiToken };
|
|
141
|
+
console.log(`✔️ Using Object Store mode, with object store: ${objectStoreName}`);
|
|
142
|
+
console.log(`✔️ Fastly API Token: ${fastlyApiContext.apiToken.slice(0, 4)}${'*'.repeat(fastlyApiContext.apiToken.length - 4)} from '${apiKeyResult.source}'`);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
if (displayFrameworkWarnings) {
|
|
146
|
+
console.log(`✔️ Not using Object Store mode.`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const excludeDirs = config.excludeDirs;
|
|
150
|
+
if (excludeDirs.length > 0) {
|
|
151
|
+
console.log(`✔️ Using exclude directories: ${excludeDirs.join(', ')}`);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
if (displayFrameworkWarnings) {
|
|
155
|
+
console.log(`✔️ No exclude directories defined.`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const excludeDotFiles = config.excludeDotFiles;
|
|
159
|
+
if (excludeDotFiles) {
|
|
160
|
+
console.log(`✔️ Files/Directories starting with . are excluded.`);
|
|
161
|
+
}
|
|
162
|
+
const includeWellKnown = config.includeWellKnown;
|
|
163
|
+
if (includeWellKnown) {
|
|
164
|
+
console.log(`✔️ (.well-known is exempt from exclusion.)`);
|
|
165
|
+
}
|
|
166
|
+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
|
|
167
|
+
// Load content types
|
|
168
|
+
const finalContentTypes = mergeContentTypes(config.contentTypes);
|
|
169
|
+
// getFiles() applies excludeDirs, excludeDotFiles, and includeWellKnown.
|
|
170
|
+
let files = getFiles(publicDirRoot, { ...config, publicDirRoot });
|
|
171
|
+
// If public dir is not inside the c@e dir, then
|
|
172
|
+
// exclude files that come from C@E app dir
|
|
173
|
+
if (!publicDirRoot.startsWith(outputDir)) {
|
|
174
|
+
files = files.filter(file => !file.startsWith(outputDir));
|
|
175
|
+
}
|
|
176
|
+
// And then we apply assetInclusionTest.
|
|
177
|
+
const assetInfos = files.map(file => {
|
|
178
|
+
const assetKey = file.slice(publicDirRoot.length);
|
|
179
|
+
let contentTypeTestResult = testFileContentType(finalContentTypes, assetKey);
|
|
180
|
+
if (contentTypeTestResult == null) {
|
|
181
|
+
contentTypeTestResult = {
|
|
182
|
+
text: false,
|
|
183
|
+
contentType: 'application/octet-stream',
|
|
184
|
+
};
|
|
185
|
+
if (displayFrameworkWarnings) {
|
|
186
|
+
console.log('⚠️ Notice: Unknown file type ' + assetKey + '. Treating as binary file.');
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
let contentAssetInclusionResultValue = config.contentAssetInclusionTest?.(assetKey, contentTypeTestResult.contentType);
|
|
190
|
+
if (typeof contentAssetInclusionResultValue === 'boolean') {
|
|
191
|
+
contentAssetInclusionResultValue = {
|
|
192
|
+
includeContent: contentAssetInclusionResultValue,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
else if (contentAssetInclusionResultValue === 'inline') {
|
|
196
|
+
contentAssetInclusionResultValue = {
|
|
197
|
+
includeContent: true,
|
|
198
|
+
inline: true,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
const contentAssetInclusionResult = applyDefaults(contentAssetInclusionResultValue ?? null, {
|
|
202
|
+
includeContent: true,
|
|
203
|
+
inline: false,
|
|
204
|
+
});
|
|
205
|
+
let moduleAssetInclusionResultValue = config.moduleAssetInclusionTest?.(assetKey, contentTypeTestResult.contentType);
|
|
206
|
+
if (typeof moduleAssetInclusionResultValue === 'boolean') {
|
|
207
|
+
moduleAssetInclusionResultValue = {
|
|
208
|
+
includeModule: moduleAssetInclusionResultValue,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
else if (moduleAssetInclusionResultValue === 'static-import') {
|
|
212
|
+
moduleAssetInclusionResultValue = {
|
|
213
|
+
includeModule: true,
|
|
214
|
+
useStaticImport: true,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const moduleAssetInclusionResult = applyDefaults(moduleAssetInclusionResultValue ?? null, {
|
|
218
|
+
includeModule: false,
|
|
219
|
+
useStaticImport: false,
|
|
220
|
+
});
|
|
221
|
+
const stats = fs.statSync(file);
|
|
222
|
+
const lastModifiedTime = Math.floor((stats.mtime).getTime() / 1000);
|
|
223
|
+
const hash = calculateFileHash(file);
|
|
224
|
+
return {
|
|
225
|
+
file,
|
|
226
|
+
assetKey,
|
|
227
|
+
lastModifiedTime,
|
|
228
|
+
hash,
|
|
229
|
+
...contentAssetInclusionResult,
|
|
230
|
+
...moduleAssetInclusionResult,
|
|
231
|
+
...contentTypeTestResult,
|
|
232
|
+
};
|
|
233
|
+
});
|
|
234
|
+
console.log("🚀 Preparing content assets ...");
|
|
235
|
+
// Create "static content" dir that will be used to hold a copy of static files.
|
|
236
|
+
// NOTE: this is needed because includeBytes doesn't seem to be able to traverse up to parent dir of the Compute project.
|
|
237
|
+
const staticContentDir = './src/static-content';
|
|
238
|
+
fs.rmSync(staticContentDir, { recursive: true, force: true });
|
|
239
|
+
fs.mkdirSync(staticContentDir, { recursive: true });
|
|
240
|
+
// Build content assets metadata
|
|
241
|
+
const contentAssetMetadataMap = {};
|
|
242
|
+
// Object store items to upload
|
|
243
|
+
const objectStoreItems = [];
|
|
244
|
+
let contentItems = 0;
|
|
245
|
+
const counts = {
|
|
246
|
+
inline: 0,
|
|
247
|
+
objectStore: 0,
|
|
248
|
+
excluded: 0,
|
|
249
|
+
};
|
|
250
|
+
for (const assetInfo of assetInfos) {
|
|
251
|
+
if (!assetInfo.includeContent) {
|
|
252
|
+
// Non-content asset
|
|
253
|
+
counts.excluded++;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
contentItems++;
|
|
257
|
+
console.log(`✔️ [${contentItems}] ${assetInfo.assetKey}: ${JSON.stringify(assetInfo.contentType)}`);
|
|
258
|
+
const { file, assetKey, contentType, text, hash, lastModifiedTime, } = assetInfo;
|
|
259
|
+
const entryBase = {
|
|
260
|
+
assetKey,
|
|
261
|
+
contentType,
|
|
262
|
+
lastModifiedTime,
|
|
263
|
+
hash,
|
|
264
|
+
text,
|
|
265
|
+
fileInfo: {
|
|
266
|
+
hash,
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
let metadata;
|
|
270
|
+
const isInline = objectStoreName == null || assetInfo.inline;
|
|
271
|
+
const staticContentFilePath = `${staticContentDir}/file${contentItems}.${assetInfo.text ? 'txt' : 'bin'}`;
|
|
272
|
+
async function prepareCompressedVersions(contentCompressions, func) {
|
|
273
|
+
for (const alg of contentCompression) {
|
|
274
|
+
const compressTo = algs[alg];
|
|
275
|
+
if (compressTo != null) {
|
|
276
|
+
// Even for items that are not inlined, compressed copies of the file are
|
|
277
|
+
// always created in the static content directory.
|
|
278
|
+
const staticFilePath = `${staticContentFilePath}_${alg}`;
|
|
279
|
+
if (await compressTo(file, staticFilePath, text)) {
|
|
280
|
+
console.log(`✔️ Compressed ${text ? 'text' : 'binary'} asset "${file}" to "${staticFilePath}" [${alg}].`);
|
|
281
|
+
const hash = calculateFileHash(staticFilePath);
|
|
282
|
+
func(alg, staticFilePath, hash);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const contentCompression = config.contentCompression;
|
|
288
|
+
if (isInline) {
|
|
289
|
+
// We will inline this file using includeBytes()
|
|
290
|
+
// so we copy it to the static-content directory.
|
|
291
|
+
const staticFilePath = staticContentFilePath;
|
|
292
|
+
fs.cpSync(file, staticFilePath);
|
|
293
|
+
console.log(`✔️ Copied ${text ? 'text' : 'binary'} asset "${file}" to "${staticFilePath}".`);
|
|
294
|
+
const compressedFileInfos = {};
|
|
295
|
+
await prepareCompressedVersions(contentCompression, (alg, staticFilePath, hash) => {
|
|
296
|
+
compressedFileInfos[alg] = { staticFilePath, hash };
|
|
297
|
+
});
|
|
298
|
+
metadata = {
|
|
299
|
+
...entryBase,
|
|
300
|
+
isInline,
|
|
301
|
+
fileInfo: {
|
|
302
|
+
...entryBase.fileInfo,
|
|
303
|
+
staticFilePath,
|
|
304
|
+
},
|
|
305
|
+
compressedFileInfos,
|
|
306
|
+
};
|
|
307
|
+
counts.inline++;
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
// For object store mode, we don't need to make a copy of the original file
|
|
311
|
+
const staticFilePath = file;
|
|
312
|
+
// Use the hash as part of the object store key name. This avoids having to
|
|
313
|
+
// re-upload a file if it already exists.
|
|
314
|
+
const objectStoreKey = `${publishId}:${assetKey}_${hash}`;
|
|
315
|
+
objectStoreItems.push({
|
|
316
|
+
objectStoreKey,
|
|
317
|
+
staticFilePath,
|
|
318
|
+
text,
|
|
319
|
+
});
|
|
320
|
+
const compressedFileInfos = {};
|
|
321
|
+
await prepareCompressedVersions(contentCompression, (alg, staticFilePath, hash) => {
|
|
322
|
+
const objectStoreKey = `${publishId}:${assetKey}_${alg}_${hash}`;
|
|
323
|
+
compressedFileInfos[alg] = { staticFilePath, objectStoreKey, hash };
|
|
324
|
+
objectStoreItems.push({
|
|
325
|
+
objectStoreKey,
|
|
326
|
+
staticFilePath,
|
|
327
|
+
text,
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
metadata = {
|
|
331
|
+
...entryBase,
|
|
332
|
+
isInline,
|
|
333
|
+
fileInfo: {
|
|
334
|
+
...entryBase.fileInfo,
|
|
335
|
+
staticFilePath,
|
|
336
|
+
objectStoreKey,
|
|
337
|
+
},
|
|
338
|
+
compressedFileInfos,
|
|
339
|
+
};
|
|
340
|
+
counts.objectStore++;
|
|
341
|
+
}
|
|
342
|
+
contentAssetMetadataMap[assetKey] = metadata;
|
|
343
|
+
}
|
|
344
|
+
if (objectStoreName != null) {
|
|
345
|
+
await uploadFilesToObjectStore(fastlyApiContext, objectStoreName, objectStoreItems);
|
|
346
|
+
writeObjectStoreEntriesToFastlyToml(objectStoreName, objectStoreItems);
|
|
347
|
+
}
|
|
348
|
+
console.log("✅ Prepared " + (counts.inline + counts.objectStore) + " content asset(s):");
|
|
349
|
+
if (counts.inline > 0) {
|
|
350
|
+
console.log(" " + counts.inline + " inline");
|
|
351
|
+
}
|
|
352
|
+
if (counts.objectStore > 0) {
|
|
353
|
+
console.log(" " + counts.objectStore + " object store");
|
|
354
|
+
}
|
|
355
|
+
// Build statics-metadata.js
|
|
356
|
+
let metadataFileContents = `/*
|
|
357
|
+
* Generated by @fastly/compute-js-static-publish.
|
|
358
|
+
*/
|
|
359
|
+
|
|
360
|
+
`;
|
|
361
|
+
metadataFileContents += `\nexport const objectStoreName = ${JSON.stringify(objectStoreName)};\n`;
|
|
362
|
+
metadataFileContents += `\nexport const contentAssetMetadataMap = {\n`;
|
|
363
|
+
for (const [key, value] of Object.entries(contentAssetMetadataMap)) {
|
|
364
|
+
metadataFileContents += ` ${JSON.stringify(key)}: ${JSON.stringify(value)},\n`;
|
|
365
|
+
}
|
|
366
|
+
metadataFileContents += '};\n';
|
|
367
|
+
fs.writeFileSync('./src/statics-metadata.js', metadataFileContents);
|
|
368
|
+
console.log(`✅ Wrote static file metadata for ${contentItems} file(s).`);
|
|
369
|
+
// Copy Types file for static file loader
|
|
370
|
+
try {
|
|
371
|
+
const typesFile = path.resolve(__dirname, '../../../resources/statics-metadata.d.ts');
|
|
372
|
+
fs.copyFileSync(typesFile, './src/statics-metadata.d.ts');
|
|
373
|
+
console.log("✅ Wrote content assets metadata types file statics-metadata.d.ts.");
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
console.log("⚠️ Notice: could not write content assets metadata types file statics-metadata.d.ts.");
|
|
377
|
+
}
|
|
378
|
+
// Build statics.js
|
|
379
|
+
let fileContents = `/*
|
|
380
|
+
* Generated by @fastly/compute-js-static-publish.
|
|
381
|
+
*/
|
|
382
|
+
|
|
383
|
+
`;
|
|
384
|
+
fileContents += 'import { ContentAssets, ModuleAssets, PublisherServer } from "@fastly/compute-js-static-publish";\n\n';
|
|
385
|
+
fileContents += 'import { objectStoreName, contentAssetMetadataMap } from "./statics-metadata";\n';
|
|
386
|
+
// Add import statements for assets that are modules and that need to be statically imported.
|
|
387
|
+
const staticImportModuleNumbers = {};
|
|
388
|
+
let staticImportModuleNumber = 0;
|
|
389
|
+
for (const assetInfo of assetInfos) {
|
|
390
|
+
if (assetInfo.includeModule && assetInfo.useStaticImport) {
|
|
391
|
+
staticImportModuleNumber++;
|
|
392
|
+
staticImportModuleNumbers[assetInfo.assetKey] = staticImportModuleNumber;
|
|
393
|
+
// static-content lives in the src dir, so the import must be declared as
|
|
394
|
+
// relative to that file.
|
|
395
|
+
const relativeFilePath = path.relative('./src', assetInfo.file);
|
|
396
|
+
fileContents += `import * as fileModule${staticImportModuleNumber} from "${relativeFilePath}";\n`;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
fileContents += `\nexport const moduleAssetsMap = {\n`;
|
|
400
|
+
for (const assetInfo of assetInfos) {
|
|
401
|
+
if (!assetInfo.includeModule) {
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
let module;
|
|
405
|
+
let loadModuleFunction;
|
|
406
|
+
if (assetInfo.useStaticImport) {
|
|
407
|
+
const moduleNumber = staticImportModuleNumbers[assetInfo.assetKey];
|
|
408
|
+
if (moduleNumber == null) {
|
|
409
|
+
throw new Error(`Unexpected! module asset number for "${assetInfo.assetKey}" was not found!`);
|
|
410
|
+
}
|
|
411
|
+
loadModuleFunction = `() => Promise.resolve(fileModule${moduleNumber})`;
|
|
412
|
+
module = `fileModule${moduleNumber}`;
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
// static-content lives in the src dir, so the import must be declared as
|
|
416
|
+
// relative to that file.
|
|
417
|
+
const relativeFilePath = path.relative('./src', assetInfo.file);
|
|
418
|
+
loadModuleFunction = `() => import("${relativeFilePath}")`;
|
|
419
|
+
module = 'null';
|
|
420
|
+
}
|
|
421
|
+
fileContents += ` ${JSON.stringify(assetInfo.assetKey)}: { isStaticImport: ${JSON.stringify(assetInfo.useStaticImport)}, module: ${module}, loadModule: ${loadModuleFunction} },\n`;
|
|
422
|
+
}
|
|
423
|
+
fileContents += '};\n';
|
|
424
|
+
const server = applyDefaults(config.server, {
|
|
425
|
+
publicDirPrefix: '',
|
|
426
|
+
staticItems: [],
|
|
427
|
+
compression: ['br', 'gzip'],
|
|
428
|
+
spaFile: null,
|
|
429
|
+
notFoundPageFile: null,
|
|
430
|
+
autoExt: [],
|
|
431
|
+
autoIndex: [],
|
|
432
|
+
});
|
|
433
|
+
let publicDirPrefix = server.publicDirPrefix;
|
|
434
|
+
console.log(`✔️ Server public dir prefix '${publicDirPrefix}'.`);
|
|
435
|
+
let staticItems = server.staticItems;
|
|
436
|
+
let compression = server.compression;
|
|
437
|
+
let spaFile = server.spaFile;
|
|
438
|
+
if (spaFile != null) {
|
|
439
|
+
console.log(`✔️ Application SPA file '${spaFile}'.`);
|
|
440
|
+
const spaAsset = assetInfos.find(assetInfo => assetInfo.assetKey === spaFile);
|
|
441
|
+
if (spaAsset == null || spaAsset.contentType !== 'text/html') {
|
|
442
|
+
if (displayFrameworkWarnings) {
|
|
443
|
+
console.log(`⚠️ Notice: '${spaFile}' does not exist or is not of type 'text/html'. Ignoring.`);
|
|
444
|
+
}
|
|
445
|
+
spaFile = null;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
if (displayFrameworkWarnings) {
|
|
450
|
+
console.log(`✔️ Application is not a SPA.`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
let notFoundPageFile = server.notFoundPageFile;
|
|
454
|
+
if (notFoundPageFile != null) {
|
|
455
|
+
console.log(`✔️ Application 'not found (404)' file '${notFoundPageFile}'.`);
|
|
456
|
+
const notFoundPageAsset = assetInfos.find(assetInfo => assetInfo.assetKey === notFoundPageFile);
|
|
457
|
+
if (notFoundPageAsset == null || notFoundPageAsset.contentType !== 'text/html') {
|
|
458
|
+
if (displayFrameworkWarnings) {
|
|
459
|
+
console.log(`⚠️ Notice: '${notFoundPageFile}' does not exist or is not of type 'text/html'. Ignoring.`);
|
|
460
|
+
}
|
|
461
|
+
notFoundPageFile = null;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
if (displayFrameworkWarnings) {
|
|
466
|
+
console.log(`✔️ Application specifies no 'not found (404)' page.`);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
let autoIndex = server.autoIndex;
|
|
470
|
+
let autoExt = server.autoExt;
|
|
471
|
+
const serverConfig = {
|
|
472
|
+
publicDirPrefix,
|
|
473
|
+
staticItems,
|
|
474
|
+
compression,
|
|
475
|
+
spaFile,
|
|
476
|
+
notFoundPageFile,
|
|
477
|
+
autoExt,
|
|
478
|
+
autoIndex,
|
|
479
|
+
};
|
|
480
|
+
fileContents += `\nexport const serverConfig = ${JSON.stringify(serverConfig, null, 2)};\n`;
|
|
481
|
+
fileContents += '\nexport const contentAssets = new ContentAssets(objectStoreName, contentAssetMetadataMap);';
|
|
482
|
+
fileContents += '\nexport const moduleAssets = new ModuleAssets(moduleAssetsMap);\n';
|
|
483
|
+
fileContents += '\nlet server = null;';
|
|
484
|
+
fileContents += '\nexport function getServer() {' +
|
|
485
|
+
'\n if (server == null) {' +
|
|
486
|
+
'\n server = new PublisherServer(serverConfig, contentAssets);' +
|
|
487
|
+
'\n }' +
|
|
488
|
+
'\n return server;' +
|
|
489
|
+
'\n}\n';
|
|
490
|
+
fs.writeFileSync('./src/statics.js', fileContents);
|
|
491
|
+
console.log("✅ Wrote static file loader for " + files.length + " file(s).");
|
|
492
|
+
// Copy Types file for static file loader
|
|
493
|
+
try {
|
|
494
|
+
const staticsTypeFile = path.resolve(__dirname, '../../../resources/statics.d.ts');
|
|
495
|
+
fs.copyFileSync(staticsTypeFile, './src/statics.d.ts');
|
|
496
|
+
console.log("✅ Wrote static file loader types file statics.d.ts.");
|
|
497
|
+
}
|
|
498
|
+
catch {
|
|
499
|
+
console.log("⚠️ Notice: could not write static file loader types file statics.d.ts.");
|
|
500
|
+
}
|
|
501
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clean-object-store.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/clean-object-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAS5D,wBAAsB,gBAAgB,CAAC,iBAAiB,EAAE,kBAAkB,iBAgE3E"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { generateOrLoadPublishId } from "../util/publish-id.js";
|
|
3
|
+
import { loadConfigFile } from "../load-config.js";
|
|
4
|
+
import { getObjectStoreKeys, objectStoreDeleteFile } from "../util/object-store.js";
|
|
5
|
+
import { loadApiKey } from "../util/fastly-api.js";
|
|
6
|
+
import { getObjectStoreKeysFromMetadata } from "../../util/metadata.js";
|
|
7
|
+
export async function cleanObjectStore(commandLineValues) {
|
|
8
|
+
const { publishId } = generateOrLoadPublishId();
|
|
9
|
+
const errors = [];
|
|
10
|
+
const config = await loadConfigFile(errors);
|
|
11
|
+
if (config == null) {
|
|
12
|
+
console.error("❌ Can't load static-publish.rc.js");
|
|
13
|
+
console.error("Run this from a compute-js-static-publish compute-js directory.");
|
|
14
|
+
for (const error of errors) {
|
|
15
|
+
console.error(error);
|
|
16
|
+
}
|
|
17
|
+
process.exitCode = 1;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
let fastlyApiContext = null;
|
|
21
|
+
const apiKeyResult = loadApiKey();
|
|
22
|
+
if (apiKeyResult == null) {
|
|
23
|
+
console.error("❌ Fastly API Token not provided.");
|
|
24
|
+
console.error("Specify one on the command line, or use the FASTLY_API_TOKEN environment variable.");
|
|
25
|
+
process.exitCode = 1;
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
fastlyApiContext = { apiToken: apiKeyResult.apiToken };
|
|
29
|
+
const staticsMetadata = await import(path.resolve('./src/statics-metadata.js'));
|
|
30
|
+
const { objectStoreName, contentAssetMetadataMap } = staticsMetadata;
|
|
31
|
+
if (objectStoreName == null) {
|
|
32
|
+
console.error("❌ Object store not specified.");
|
|
33
|
+
console.error("This only has meaning in object store mode.");
|
|
34
|
+
process.exitCode = 1;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
// TODO: Enable getting objectStoreName and publishId from command line
|
|
38
|
+
// These are the items that are currently in the object store and that belong to this publish ID.
|
|
39
|
+
const items = ((await getObjectStoreKeys(fastlyApiContext, objectStoreName)) ?? [])
|
|
40
|
+
.filter(x => x.startsWith(`${publishId}:`));
|
|
41
|
+
// These are the items that are currently are being used.
|
|
42
|
+
const keys = getObjectStoreKeysFromMetadata(contentAssetMetadataMap);
|
|
43
|
+
// So these are the items that we should be deleting.
|
|
44
|
+
const itemsToDelete = items.filter(x => !keys.has(x));
|
|
45
|
+
console.log("Publish ID: " + publishId);
|
|
46
|
+
console.log("Object Store contains " + items.length + " item(s) for this publish ID.");
|
|
47
|
+
console.log("Current site metadata contains " + keys.size + " item(s) (including compressed alternates).");
|
|
48
|
+
console.log("Number of items to delete: " + itemsToDelete.length);
|
|
49
|
+
for (const [index, item] of itemsToDelete.entries()) {
|
|
50
|
+
console.log("Deleting item [" + (index + 1) + "]: " + item);
|
|
51
|
+
await objectStoreDeleteFile(fastlyApiContext, objectStoreName, item);
|
|
52
|
+
}
|
|
53
|
+
console.log("✅ Completed.");
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init-app.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/init-app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AA0OvD,wBAAgB,OAAO,CAAC,iBAAiB,EAAE,kBAAkB,QAia5D"}
|