@fastly/compute-js-static-publish 8.0.0-rc.2 → 8.0.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/build/cli/cli/commands/manage/clean.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/clean.js +1 -0
- package/build/cli/cli/commands/manage/collections/delete.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/delete.js +39 -2
- package/build/cli/cli/commands/manage/collections/index.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/index.js +1 -0
- package/build/cli/cli/commands/manage/collections/list.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/list.js +10 -1
- package/build/cli/cli/commands/manage/collections/promote.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/promote.js +1 -0
- package/build/cli/cli/commands/manage/collections/update-expiration.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/update-expiration.js +1 -0
- package/build/cli/cli/commands/manage/index.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/index.js +1 -0
- package/build/cli/cli/commands/manage/publish-content.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/publish-content.js +64 -73
- package/build/cli/cli/storage/s3-storage-provider.d.ts +1 -0
- package/build/cli/cli/storage/s3-storage-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/s3-storage-provider.js +8 -4
- package/build/cli/cli/util/purge.d.ts +1 -0
- package/build/cli/cli/util/purge.d.ts.map +1 -1
- package/build/cli/cli/util/purge.js +21 -6
- package/build/cli/cli/util/service-id.js +1 -1
- package/build/cli/cli/util/variants.d.ts +7 -0
- package/build/cli/cli/util/variants.d.ts.map +1 -1
- package/build/cli/cli/util/variants.js +17 -0
- package/build/cli/models/server/index.d.ts +4 -0
- package/build/cli/models/server/index.d.ts.map +1 -1
- package/build/cli/models/server/index.js +34 -1
- package/build/server/models/server/index.d.ts +4 -0
- package/build/server/models/server/index.d.ts.map +1 -1
- package/build/server/models/server/index.js +34 -1
- package/build/server/server/publisher-server/index.d.ts +0 -1
- package/build/server/server/publisher-server/index.d.ts.map +1 -1
- package/build/server/server/publisher-server/index.js +62 -52
- package/build/server/server/publisher-server/response-cache.d.ts +13 -0
- package/build/server/server/publisher-server/response-cache.d.ts.map +1 -1
- package/build/server/server/publisher-server/response-cache.js +54 -0
- package/build/server/server/util/body-length.d.ts +5 -0
- package/build/server/server/util/body-length.d.ts.map +1 -0
- package/build/server/server/util/body-length.js +63 -0
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clean.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/clean.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"clean.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/clean.ts"],"names":[],"mappings":"AAoDA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA0NhD"}
|
|
@@ -27,6 +27,7 @@ KV Store Options:
|
|
|
27
27
|
--local Instead of working with the Fastly KV Store, operate on
|
|
28
28
|
local files that will be used to simulate the KV Store
|
|
29
29
|
with the local development environment.
|
|
30
|
+
KV Store mode only. In S3 mode, --local is an error.
|
|
30
31
|
|
|
31
32
|
--fastly-api-token=<token> Fastly API token for KV Store access.
|
|
32
33
|
If not set, the tool uses the FASTLY_API_TOKEN
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delete.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/delete.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"delete.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/delete.ts"],"names":[],"mappings":"AAiEA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA+JhD"}
|
|
@@ -6,6 +6,7 @@ import path from 'node:path';
|
|
|
6
6
|
import { parseCommandLine } from '../../../util/args.js';
|
|
7
7
|
import { LoadConfigError, loadStaticPublisherRcFile } from '../../../util/config.js';
|
|
8
8
|
import { loadStorageProviderFromStaticPublishRc } from '../../../storage/storage-provider.js';
|
|
9
|
+
import { loadPurgeTarget, purgeCollection } from '../../../util/purge.js';
|
|
9
10
|
function help() {
|
|
10
11
|
console.log(`\
|
|
11
12
|
|
|
@@ -19,6 +20,9 @@ Description:
|
|
|
19
20
|
Use the 'npx @fastly/compute-js-static-publish clean' command afterward to remove content
|
|
20
21
|
files that are no longer referenced by any collection.
|
|
21
22
|
|
|
23
|
+
If a Service ID is found, the tool then purges the cached responses of the collection
|
|
24
|
+
from that service.
|
|
25
|
+
|
|
22
26
|
Required:
|
|
23
27
|
--collection-name=<name> The name of the collection to delete
|
|
24
28
|
|
|
@@ -26,11 +30,23 @@ KV Store Options:
|
|
|
26
30
|
--local Instead of working with the Fastly KV Store, operate on
|
|
27
31
|
local files that will be used to simulate the KV Store
|
|
28
32
|
with the local development environment.
|
|
33
|
+
KV Store mode only. In S3 mode, --local is an error.
|
|
29
34
|
|
|
30
|
-
--fastly-api-token=<token> Fastly API token for KV Store access.
|
|
35
|
+
--fastly-api-token=<token> Fastly API token for KV Store or cache access.
|
|
31
36
|
If not set, the tool uses the FASTLY_API_TOKEN
|
|
32
37
|
environment variable.
|
|
33
38
|
|
|
39
|
+
Purge Options:
|
|
40
|
+
--fastly-service-id=<id> Fastly Service ID to purge after deleting (not with --local).
|
|
41
|
+
If not set, the tool will check:
|
|
42
|
+
1. FASTLY_SERVICE_ID environment variable
|
|
43
|
+
2. service_id in fastly.toml
|
|
44
|
+
If none is found, the purge is skipped.
|
|
45
|
+
|
|
46
|
+
--purge-environment=<env> Environment to purge: production, staging, or a
|
|
47
|
+
comma-separated list of both. Can be repeated.
|
|
48
|
+
Default: production
|
|
49
|
+
|
|
34
50
|
S3 Storage Options (BETA):
|
|
35
51
|
--s3-access-key-id=<id> Access Key ID and Secret Access Key used to
|
|
36
52
|
--s3-secret-access-key=<key> interface with S3 or compatible storage.
|
|
@@ -47,6 +63,8 @@ export async function action(actionArgs) {
|
|
|
47
63
|
{ name: 'collection-name', type: String },
|
|
48
64
|
{ name: 'local', type: Boolean },
|
|
49
65
|
{ name: 'fastly-api-token', type: String, },
|
|
66
|
+
{ name: 'fastly-service-id', type: String, },
|
|
67
|
+
{ name: 'purge-environment', type: String, multiple: true, },
|
|
50
68
|
{ name: 's3-access-key-id', type: String, },
|
|
51
69
|
{ name: 's3-secret-access-key', type: String, },
|
|
52
70
|
];
|
|
@@ -60,7 +78,7 @@ export async function action(actionArgs) {
|
|
|
60
78
|
help();
|
|
61
79
|
return;
|
|
62
80
|
}
|
|
63
|
-
const { verbose, ['collection-name']: collectionNameValue, ['fastly-api-token']: fastlyApiToken, local: localMode, ['s3-access-key-id']: s3AccessKeyId, ['s3-secret-access-key']: s3SecretAccessKey, } = parsed.commandLineOptions;
|
|
81
|
+
const { verbose, ['collection-name']: collectionNameValue, ['fastly-api-token']: fastlyApiToken, ['fastly-service-id']: fastlyServiceId, ['purge-environment']: purgeEnvironmentValues, local: localMode, ['s3-access-key-id']: s3AccessKeyId, ['s3-secret-access-key']: s3SecretAccessKey, } = parsed.commandLineOptions;
|
|
64
82
|
// compute-js-static-publisher cli is always run from the Compute application directory
|
|
65
83
|
// in other words, the directory that contains `fastly.toml`.
|
|
66
84
|
const computeAppDir = path.resolve();
|
|
@@ -116,6 +134,22 @@ export async function action(actionArgs) {
|
|
|
116
134
|
return;
|
|
117
135
|
}
|
|
118
136
|
console.log(`✔️ Collection to delete: ${collectionName}`);
|
|
137
|
+
// Purge target: checked now, so that a missing API token fails before anything is deleted.
|
|
138
|
+
let purgeTarget;
|
|
139
|
+
try {
|
|
140
|
+
purgeTarget = loadPurgeTarget({
|
|
141
|
+
localMode: localMode ?? false,
|
|
142
|
+
computeAppDir,
|
|
143
|
+
fastlyServiceId,
|
|
144
|
+
fastlyApiToken,
|
|
145
|
+
purgeEnvironments: purgeEnvironmentValues,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
console.error(String(err));
|
|
150
|
+
process.exitCode = 1;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
119
153
|
// ### Asset Keys to delete
|
|
120
154
|
const assetKeysToDelete = new Set();
|
|
121
155
|
// ### List all indexes ###
|
|
@@ -144,5 +178,8 @@ export async function action(actionArgs) {
|
|
|
144
178
|
console.log(`Deleting key from storage: ${key}`);
|
|
145
179
|
await storageProvider.deleteStorageEntry(key);
|
|
146
180
|
}, undefined, true);
|
|
181
|
+
if (purgeTarget != null) {
|
|
182
|
+
await purgeCollection(purgeTarget, publishId, collectionName);
|
|
183
|
+
}
|
|
147
184
|
console.log("✅ Completed.");
|
|
148
185
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/index.ts"],"names":[],"mappings":"AAoDA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA0BhD"}
|
|
@@ -27,6 +27,7 @@ KV Store Options:
|
|
|
27
27
|
--local Instead of working with the Fastly KV Store, operate on
|
|
28
28
|
local files that will be used to simulate the KV Store
|
|
29
29
|
with the local development environment.
|
|
30
|
+
KV Store mode only. In S3 mode, --local is an error.
|
|
30
31
|
|
|
31
32
|
--fastly-api-token=<token> Fastly API token for KV Store access.
|
|
32
33
|
If not set, the tool uses the FASTLY_API_TOKEN
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/list.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/list.ts"],"names":[],"mappings":"AA6CA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA4IhD"}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
4
|
*/
|
|
5
5
|
import path from 'node:path';
|
|
6
|
-
import { decodeIndexMetadata } from '../../../../models/server/index.js';
|
|
6
|
+
import { decodeIndexMetadata, getUnsupportedIndexFeatures } from '../../../../models/server/index.js';
|
|
7
7
|
import { isExpired } from '../../../../models/time/index.js';
|
|
8
8
|
import { parseCommandLine } from '../../../util/args.js';
|
|
9
9
|
import { LoadConfigError, loadStaticPublisherRcFile } from '../../../util/config.js';
|
|
@@ -21,6 +21,7 @@ KV Store Options:
|
|
|
21
21
|
--local Instead of working with the Fastly KV Store, operate on
|
|
22
22
|
local files that will be used to simulate the KV Store
|
|
23
23
|
with the local development environment.
|
|
24
|
+
KV Store mode only. In S3 mode, --local is an error.
|
|
24
25
|
|
|
25
26
|
--fastly-api-token=<token> Fastly API token for KV Store access.
|
|
26
27
|
If not set, the tool uses the FASTLY_API_TOKEN
|
|
@@ -134,6 +135,14 @@ export async function action(actionArgs) {
|
|
|
134
135
|
else {
|
|
135
136
|
console.log(` Published : ${new Date(indexMetadata.publishedTime * 1000)}`);
|
|
136
137
|
}
|
|
138
|
+
if (indexMetadata.requires != null) {
|
|
139
|
+
console.log(` Requires : ${indexMetadata.requires.join(', ')}`);
|
|
140
|
+
const unsupportedFeatures = getUnsupportedIndexFeatures(indexMetadata);
|
|
141
|
+
if (unsupportedFeatures.length > 0) {
|
|
142
|
+
console.log(` ⚠️ This version does not support ${unsupportedFeatures.join(', ')}. An application`);
|
|
143
|
+
console.log(` that is built with this version will not serve this collection.`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
137
146
|
if (indexMetadata.expirationTime == null) {
|
|
138
147
|
console.log(' Expiration : not set');
|
|
139
148
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promote.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/promote.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"promote.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/promote.ts"],"names":[],"mappings":"AAmEA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBAuLhD"}
|
|
@@ -43,6 +43,7 @@ KV Store Options:
|
|
|
43
43
|
--local Instead of working with the Fastly KV Store, operate on
|
|
44
44
|
local files that will be used to simulate the KV Store
|
|
45
45
|
with the local development environment.
|
|
46
|
+
KV Store mode only. In S3 mode, --local is an error.
|
|
46
47
|
|
|
47
48
|
--fastly-api-token=<token> Fastly API token for KV Store access.
|
|
48
49
|
If not set, the tool uses the FASTLY_API_TOKEN
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update-expiration.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/update-expiration.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"update-expiration.d.ts","sourceRoot":"","sources":["../../../../../../src/cli/commands/manage/collections/update-expiration.ts"],"names":[],"mappings":"AA+DA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA0JhD"}
|
|
@@ -39,6 +39,7 @@ KV Store Options:
|
|
|
39
39
|
--local Instead of working with the Fastly KV Store, operate on
|
|
40
40
|
local files that will be used to simulate the KV Store
|
|
41
41
|
with the local development environment.
|
|
42
|
+
KV Store mode only. In S3 mode, --local is an error.
|
|
42
43
|
|
|
43
44
|
--fastly-api-token=<token> Fastly API token for KV Store access.
|
|
44
45
|
If not set, the tool uses the FASTLY_API_TOKEN
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/index.ts"],"names":[],"mappings":"AA2DA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBAyBhD"}
|
|
@@ -29,6 +29,7 @@ KV Store Options:
|
|
|
29
29
|
--local Instead of working with the Fastly KV Store, operate on
|
|
30
30
|
local files that will be used to simulate the KV Store
|
|
31
31
|
with the local development environment.
|
|
32
|
+
KV Store mode only. In S3 mode, --local is an error.
|
|
32
33
|
|
|
33
34
|
--fastly-api-token=<token> Fastly API token for KV Store access.
|
|
34
35
|
If not set, the tool uses the FASTLY_API_TOKEN
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"publish-content.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/publish-content.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"publish-content.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/publish-content.ts"],"names":[],"mappings":"AA0HA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA2jBhD"}
|
|
@@ -11,9 +11,9 @@ import { mergeContentTypes, testFileContentType } from '../../util/content-types
|
|
|
11
11
|
import { LoadConfigError, loadPublishContentConfigFile, loadStaticPublisherRcFile } from '../../util/config.js';
|
|
12
12
|
import { applyDefaults } from '../../util/data.js';
|
|
13
13
|
import { calculateFileSizeAndHash, enumerateFiles, rootRelative } from '../../util/files.js';
|
|
14
|
-
import { ensureVariantFileExists } from '../../util/variants.js';
|
|
14
|
+
import { ensureVariantFileExists, OncePerKey } from '../../util/variants.js';
|
|
15
15
|
import { concurrentMap } from '../../util/retryable.js';
|
|
16
|
-
import { loadPurgeTarget,
|
|
16
|
+
import { loadPurgeTarget, purgeCollection } from '../../util/purge.js';
|
|
17
17
|
import { getStorageKeysByHexPrefix, loadStorageProviderFromStaticPublishRc, StorageProviderBatch, } from '../../storage/storage-provider.js';
|
|
18
18
|
// Storage key format:
|
|
19
19
|
// <publishId>_index_<preview_id>.json
|
|
@@ -80,6 +80,7 @@ KV Store Options:
|
|
|
80
80
|
--local Instead of working with the Fastly KV Store, operate on
|
|
81
81
|
local files that will be used to simulate the KV Store
|
|
82
82
|
with the local development environment.
|
|
83
|
+
KV Store mode only. In S3 mode, --local is an error.
|
|
83
84
|
|
|
84
85
|
--kv-overwrite Alias for --overwrite-existing.
|
|
85
86
|
|
|
@@ -306,7 +307,11 @@ export async function action(actionArgs) {
|
|
|
306
307
|
const batch = new StorageProviderBatch();
|
|
307
308
|
// Assets included in the publishing, keyed by asset key
|
|
308
309
|
const assetsIndex = {};
|
|
309
|
-
|
|
310
|
+
// For each variant that we know about during this publishing, true if the
|
|
311
|
+
// index keeps the variant. Keyed on the variant's storage key. Files with the
|
|
312
|
+
// same content have the same keys, so the first of them prepares each variant
|
|
313
|
+
// one time, and the other files await its result.
|
|
314
|
+
const variantKeeps = new OncePerKey();
|
|
310
315
|
// #### Keys that are already in storage
|
|
311
316
|
// The key of a file is the hash of its content. If the key exists, storage
|
|
312
317
|
// already has the same content. Thus we do not compress, hash, or upload it again.
|
|
@@ -369,12 +374,6 @@ export async function action(actionArgs) {
|
|
|
369
374
|
console.log(`📄 File '${rootRelative(file)}' - ${baseSize} bytes, sha256: ${baseHash}`);
|
|
370
375
|
const stats = fs.statSync(file);
|
|
371
376
|
const lastModifiedTime = Math.floor((stats.mtime).getTime() / 1000);
|
|
372
|
-
// #### Keep flag per variant
|
|
373
|
-
let variantKeeps = baseHashToVariantKeepMap.get(baseHash);
|
|
374
|
-
if (variantKeeps == null) {
|
|
375
|
-
variantKeeps = new Map();
|
|
376
|
-
baseHashToVariantKeepMap.set(baseHash, variantKeeps);
|
|
377
|
-
}
|
|
378
377
|
// We know the number of chunks of the original before we read it. For a
|
|
379
378
|
// compressed variant, we know it only if the original fits in one chunk.
|
|
380
379
|
const baseNumChunks = storageProvider.calculateNumChunks(baseSize);
|
|
@@ -384,21 +383,10 @@ export async function action(actionArgs) {
|
|
|
384
383
|
...contentCompression,
|
|
385
384
|
];
|
|
386
385
|
const batchItems = [];
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
if (variant
|
|
391
|
-
variantKey = `${variantKey}_${variant}`;
|
|
392
|
-
variantFilename = `${variantFilename}_${variant}`;
|
|
393
|
-
}
|
|
394
|
-
const variantFilePath = path.resolve(storageContentDir, variantFilename);
|
|
395
|
-
let keep = variantKeeps.get(variant);
|
|
396
|
-
if (keep != null) {
|
|
397
|
-
if (verbose) {
|
|
398
|
-
console.log(` 🏃♂️ Asset "${variantKey}" is identical to an item we already know about, reusing existing copy.`);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
else if ((variant === 'original' || baseNumChunks === 1) &&
|
|
386
|
+
// Finds the variant in storage, or prepares it and adds it to batchItems.
|
|
387
|
+
// Returns true if the index keeps the variant.
|
|
388
|
+
async function prepareVariant(variant, variantKey, variantFilePath) {
|
|
389
|
+
if ((variant === 'original' || baseNumChunks === 1) &&
|
|
402
390
|
isVariantInStorage(variantKey, variant === 'original' ? baseNumChunks : 1)) {
|
|
403
391
|
if (verbose) {
|
|
404
392
|
console.log(` ・ Asset found in storage with key "${variantKey}".`);
|
|
@@ -406,48 +394,59 @@ export async function action(actionArgs) {
|
|
|
406
394
|
existingVariantCount++;
|
|
407
395
|
// We upload a compressed variant only if it is smaller than the
|
|
408
396
|
// original. Thus if it is in storage, the index keeps it.
|
|
409
|
-
|
|
410
|
-
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
await ensureVariantFileExists(variantFilePath, variant, file, verbose, { brotliQuality: publishContentConfig.brotliQuality });
|
|
400
|
+
let contentEncoding, hash, size;
|
|
401
|
+
if (variant === 'original') {
|
|
402
|
+
contentEncoding = undefined;
|
|
403
|
+
hash = baseHash;
|
|
404
|
+
size = baseSize;
|
|
411
405
|
}
|
|
412
406
|
else {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
variantKeeps.set(variant, keep);
|
|
428
|
-
if (keep) {
|
|
429
|
-
const numChunks = storageProvider.calculateNumChunks(size);
|
|
430
|
-
const metadataJson = {
|
|
431
|
-
size: String(size),
|
|
432
|
-
hash,
|
|
433
|
-
};
|
|
434
|
-
if (contentEncoding != null) {
|
|
435
|
-
metadataJson.contentEncoding = contentEncoding;
|
|
436
|
-
}
|
|
437
|
-
if (numChunks > 1) {
|
|
438
|
-
metadataJson.numChunks = String(numChunks);
|
|
439
|
-
}
|
|
440
|
-
batchItems.push({
|
|
441
|
-
size,
|
|
442
|
-
key: variantKey,
|
|
443
|
-
filePath: variantFilePath,
|
|
444
|
-
metadataJson,
|
|
445
|
-
});
|
|
407
|
+
contentEncoding = variant;
|
|
408
|
+
({ hash, size } = await calculateFileSizeAndHash(variantFilePath));
|
|
409
|
+
}
|
|
410
|
+
// Only keep variants whose file size actually ends up smaller than
|
|
411
|
+
// what we started with. Do not upload the other variants.
|
|
412
|
+
const keep = variant === 'original' || size < baseSize;
|
|
413
|
+
if (keep) {
|
|
414
|
+
const numChunks = storageProvider.calculateNumChunks(size);
|
|
415
|
+
const metadataJson = {
|
|
416
|
+
size: String(size),
|
|
417
|
+
hash,
|
|
418
|
+
};
|
|
419
|
+
if (contentEncoding != null) {
|
|
420
|
+
metadataJson.contentEncoding = contentEncoding;
|
|
446
421
|
}
|
|
447
|
-
|
|
448
|
-
|
|
422
|
+
if (numChunks > 1) {
|
|
423
|
+
metadataJson.numChunks = String(numChunks);
|
|
449
424
|
}
|
|
425
|
+
batchItems.push({
|
|
426
|
+
size,
|
|
427
|
+
key: variantKey,
|
|
428
|
+
filePath: variantFilePath,
|
|
429
|
+
metadataJson,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
else if (verbose) {
|
|
433
|
+
console.log(` ・ Variant "${variantKey}" is not smaller than the original, not uploading.`);
|
|
450
434
|
}
|
|
435
|
+
return keep;
|
|
436
|
+
}
|
|
437
|
+
for (const variant of variants) {
|
|
438
|
+
let variantKey = `${publishId}_files_sha256_${baseHash}`;
|
|
439
|
+
let variantFilename = `${baseHash}`;
|
|
440
|
+
if (variant !== 'original') {
|
|
441
|
+
variantKey = `${variantKey}_${variant}`;
|
|
442
|
+
variantFilename = `${variantFilename}_${variant}`;
|
|
443
|
+
}
|
|
444
|
+
const variantFilePath = path.resolve(storageContentDir, variantFilename);
|
|
445
|
+
const { promise, first } = variantKeeps.run(variantKey, () => prepareVariant(variant, variantKey, variantFilePath));
|
|
446
|
+
if (!first && verbose) {
|
|
447
|
+
console.log(` 🏃♂️ Asset "${variantKey}" is identical to an item we already know about, reusing existing copy.`);
|
|
448
|
+
}
|
|
449
|
+
const keep = await promise;
|
|
451
450
|
if (variant !== 'original' && keep) {
|
|
452
451
|
variantsToKeep.push(variant);
|
|
453
452
|
}
|
|
@@ -477,6 +476,9 @@ export async function action(actionArgs) {
|
|
|
477
476
|
if (existingKeys != null) {
|
|
478
477
|
console.log(` | ${existingVariantCount} variant(s) already in storage, ${batch.storageProviderBatchEntries.length} to upload.`);
|
|
479
478
|
}
|
|
479
|
+
else {
|
|
480
|
+
console.log(` | ${batch.storageProviderBatchEntries.length} variant(s) to upload.`);
|
|
481
|
+
}
|
|
480
482
|
await storageProvider.applyBatch(batch, { existingKeys });
|
|
481
483
|
// #### INDEX FILE
|
|
482
484
|
console.log(`🗂️ Saving Index...`);
|
|
@@ -558,18 +560,7 @@ export async function action(actionArgs) {
|
|
|
558
560
|
await storageProvider.submitStorageEntry(settingsFileKey, settingsFilePath, JSON.stringify(serverSettings));
|
|
559
561
|
console.log(`✅ Settings have been saved.`);
|
|
560
562
|
if (purgeTarget != null) {
|
|
561
|
-
|
|
562
|
-
for (const environment of purgeTarget.environments) {
|
|
563
|
-
console.log(`Purging surrogate key [${surrogateKey}] on service [${purgeTarget.serviceId}] (${environment})...`);
|
|
564
|
-
const purged = await purgeSurrogateKey(purgeTarget.fastlyApiContext, purgeTarget.serviceId, surrogateKey, true, environment);
|
|
565
|
-
if (purged) {
|
|
566
|
-
console.log('Purged');
|
|
567
|
-
}
|
|
568
|
-
else {
|
|
569
|
-
// The content is published. Only the cached copies may be stale until they expire.
|
|
570
|
-
console.warn(`⚠️ Warning: Failed purging (${environment}). Cached copies may be served until they expire.`);
|
|
571
|
-
}
|
|
572
|
-
}
|
|
563
|
+
await purgeCollection(purgeTarget, publishId, collectionName);
|
|
573
564
|
}
|
|
574
565
|
console.log(`🎉 Completed.`);
|
|
575
566
|
}
|
|
@@ -3,6 +3,7 @@ import { type Command, type HttpHandlerOptions } from '@aws-sdk/types';
|
|
|
3
3
|
import { type StorageEntry, type StorageProvider, type StorageProviderBuilder, type StorageProviderBatch, type ApplyBatchOptions } from './storage-provider.js';
|
|
4
4
|
type CommandOutput<C> = C extends Command<any, any, any, infer O, any> ? O : never;
|
|
5
5
|
export declare const DEFAULT_S3_UPLOAD_CONCURRENCY = 64;
|
|
6
|
+
export declare const S3_LOCAL_MODE_ERROR = "--local is available only in KV Store mode. In S3 mode, the commands always use the bucket in static-publish.rc.js.";
|
|
6
7
|
export declare const buildStoreProvider: StorageProviderBuilder;
|
|
7
8
|
export declare class S3StorageProvider implements StorageProvider {
|
|
8
9
|
constructor(s3Region: string, accessKeyId: string, secretAccessKey: string, s3Bucket: string, s3Endpoint?: string, uploadConcurrency?: number);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"s3-storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/cli/storage/s3-storage-provider.ts"],"names":[],"mappings":"AAQA,OAAO,EAUL,QAAQ,EAIT,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,kBAAkB,EACxB,MAAM,gBAAgB,CAAC;AASxB,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAE3B,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACvB,MAAM,uBAAuB,CAAC;AAa/B,KAAK,aAAa,CAAC,CAAC,IAAI,CAAC,SAA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GAAE,MAAW,EAC1B,YAAY,GAAE,OAAe,GAC5B,OAAO,CAAC,IAAI,CAAC;IAmBhB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;CAI1C"}
|
|
@@ -16,13 +16,17 @@ import { rootRelative, } from '../util/files.js';
|
|
|
16
16
|
// requests, and each request mostly waits for the network. In a test with small
|
|
17
17
|
// objects, 64 was approximately 4 times faster than 12.
|
|
18
18
|
export const DEFAULT_S3_UPLOAD_CONCURRENCY = 64;
|
|
19
|
+
export const S3_LOCAL_MODE_ERROR = '--local is available only in KV Store mode. In S3 mode, the commands always use the bucket in static-publish.rc.js.';
|
|
19
20
|
export const buildStoreProvider = async (config, context) => {
|
|
20
|
-
if (isS3StorageConfigRc(config)) {
|
|
21
|
-
console.log(` Working on S3 (or compatible) storage (BETA)...`);
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
21
|
+
if (!isS3StorageConfigRc(config)) {
|
|
24
22
|
return null;
|
|
25
23
|
}
|
|
24
|
+
// There is no local simulation of S3 storage. Stop before the command
|
|
25
|
+
// connects to the bucket.
|
|
26
|
+
if (context.localMode) {
|
|
27
|
+
throw new Error(S3_LOCAL_MODE_ERROR);
|
|
28
|
+
}
|
|
29
|
+
console.log(` Working on S3 (or compatible) storage (BETA)...`);
|
|
26
30
|
const { region, bucket, endpoint, } = getS3StorageConfigFromRc(config);
|
|
27
31
|
console.log(` | Using S3 storage (BETA)`);
|
|
28
32
|
console.log(` Region : ${region}`);
|
|
@@ -16,4 +16,5 @@ export type LoadPurgeTargetParams = {
|
|
|
16
16
|
};
|
|
17
17
|
export declare function loadPurgeTarget(params: LoadPurgeTargetParams): PurgeTarget | null;
|
|
18
18
|
export declare function purgeSurrogateKey(fastlyApiContext: FastlyApiContext, fastlyServiceId: string, surrogateKey: string, softPurge?: boolean, environment?: PurgeEnvironment): Promise<boolean>;
|
|
19
|
+
export declare function purgeCollection(purgeTarget: PurgeTarget, publishId: string, collectionName: string): Promise<void>;
|
|
19
20
|
//# sourceMappingURL=purge.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"purge.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/purge.ts"],"names":[],"mappings":"AAOA,OAAO,EAAiB,KAAK,gBAAgB,EAA4B,MAAM,gBAAgB,CAAC;AAGhG,eAAO,MAAM,iBAAiB,oCAAuC,CAAC;AACtE,MAAM,MAAM,gBAAgB,GAAG,OAAO,iBAAiB,CAAC,MAAM,CAAC,CAAC;AAIhE,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,OAAO,GAAG,gBAAgB,EAAE,CAkB1E;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,YAAY,EAAE,gBAAgB,EAAE,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,SAAS,EAAE,OAAO,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,OAAO,CAAC;IACzB,cAAc,EAAE,OAAO,CAAC;IACxB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAMF,wBAAgB,eAAe,CAAC,MAAM,EAAE,qBAAqB,GAAG,WAAW,GAAG,IAAI,CAgCjF;AAED,wBAAsB,iBAAiB,CACrC,gBAAgB,EAAE,gBAAgB,EAClC,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,MAAM,EACpB,SAAS,GAAE,OAAe,EAC1B,WAAW,GAAE,gBAA+B,oBA4B7C"}
|
|
1
|
+
{"version":3,"file":"purge.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/purge.ts"],"names":[],"mappings":"AAOA,OAAO,EAAiB,KAAK,gBAAgB,EAA4B,MAAM,gBAAgB,CAAC;AAGhG,eAAO,MAAM,iBAAiB,oCAAuC,CAAC;AACtE,MAAM,MAAM,gBAAgB,GAAG,OAAO,iBAAiB,CAAC,MAAM,CAAC,CAAC;AAIhE,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,OAAO,GAAG,gBAAgB,EAAE,CAkB1E;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,YAAY,EAAE,gBAAgB,EAAE,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,SAAS,EAAE,OAAO,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,OAAO,CAAC;IACzB,cAAc,EAAE,OAAO,CAAC;IACxB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAMF,wBAAgB,eAAe,CAAC,MAAM,EAAE,qBAAqB,GAAG,WAAW,GAAG,IAAI,CAgCjF;AAED,wBAAsB,iBAAiB,CACrC,gBAAgB,EAAE,gBAAgB,EAClC,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,MAAM,EACpB,SAAS,GAAE,OAAe,EAC1B,WAAW,GAAE,gBAA+B,oBA4B7C;AAID,wBAAsB,eAAe,CAAC,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,iBAWxG"}
|
|
@@ -27,15 +27,15 @@ export function parsePurgeEnvironments(values) {
|
|
|
27
27
|
}
|
|
28
28
|
return result.length > 0 ? result : ['production'];
|
|
29
29
|
}
|
|
30
|
-
// Finds the service to purge after
|
|
31
|
-
// copies of the collection. Returns null, and logs why, if the
|
|
32
|
-
// Throws if a Service ID is found but an API token is not, so
|
|
33
|
-
// before
|
|
30
|
+
// Finds the service to purge after a change to a collection, so that it stops
|
|
31
|
+
// serving cached copies of the collection. Returns null, and logs why, if the
|
|
32
|
+
// purge is skipped. Throws if a Service ID is found but an API token is not, so
|
|
33
|
+
// that a command can fail before it changes storage.
|
|
34
34
|
export function loadPurgeTarget(params) {
|
|
35
35
|
// Check the option even when the purge is skipped, so that a typo is reported.
|
|
36
36
|
const environments = parsePurgeEnvironments(params.purgeEnvironments);
|
|
37
37
|
if (params.localMode) {
|
|
38
|
-
console.log(`- Local mode: will skip purge
|
|
38
|
+
console.log(`- Local mode: will skip the purge.`);
|
|
39
39
|
return null;
|
|
40
40
|
}
|
|
41
41
|
const serviceIdResult = loadServiceId({
|
|
@@ -43,7 +43,7 @@ export function loadPurgeTarget(params) {
|
|
|
43
43
|
fastlyTomlPath: path.resolve(params.computeAppDir, 'fastly.toml'),
|
|
44
44
|
});
|
|
45
45
|
if (serviceIdResult == null) {
|
|
46
|
-
console.log(`- Service ID not found (--fastly-service-id, FASTLY_SERVICE_ID, or fastly.toml). Will skip purge
|
|
46
|
+
console.log(`- Service ID not found (--fastly-service-id, FASTLY_SERVICE_ID, or fastly.toml). Will skip the purge.`);
|
|
47
47
|
return null;
|
|
48
48
|
}
|
|
49
49
|
console.log(`✔️ Service ID from ${serviceIdResult.source}: ${serviceIdResult.serviceId}`);
|
|
@@ -81,3 +81,18 @@ export async function purgeSurrogateKey(fastlyApiContext, fastlyServiceId, surro
|
|
|
81
81
|
}
|
|
82
82
|
return true;
|
|
83
83
|
}
|
|
84
|
+
// Purges the cached responses of a collection in each environment of the purge
|
|
85
|
+
// target. A failed purge is a warning, because the change to storage is done.
|
|
86
|
+
export async function purgeCollection(purgeTarget, publishId, collectionName) {
|
|
87
|
+
const surrogateKey = `${publishId}-${collectionName}`;
|
|
88
|
+
for (const environment of purgeTarget.environments) {
|
|
89
|
+
console.log(`Purging surrogate key [${surrogateKey}] on service [${purgeTarget.serviceId}] (${environment})...`);
|
|
90
|
+
const purged = await purgeSurrogateKey(purgeTarget.fastlyApiContext, purgeTarget.serviceId, surrogateKey, true, environment);
|
|
91
|
+
if (purged) {
|
|
92
|
+
console.log('Purged');
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
console.warn(`⚠️ Warning: Failed purging (${environment}). Cached copies may be served until they expire.`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import fs from 'node:fs';
|
|
6
6
|
import { readServiceId } from './fastly-toml.js';
|
|
7
|
-
// Finds the Fastly Service ID to purge after
|
|
7
|
+
// Finds the Fastly Service ID to purge after a change to a collection. Checks, in order:
|
|
8
8
|
// 1. The command line (--fastly-service-id)
|
|
9
9
|
// 2. The FASTLY_SERVICE_ID environment variable
|
|
10
10
|
// 3. service_id in fastly.toml
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { type ContentCompressionTypes } from '../../models/compression/index.js';
|
|
2
2
|
import { type CompressOptions } from '../compression/index.js';
|
|
3
3
|
export type Variants = 'original' | ContentCompressionTypes;
|
|
4
|
+
export declare class OncePerKey<T> {
|
|
5
|
+
private readonly promises;
|
|
6
|
+
run(key: string, fn: () => Promise<T>): {
|
|
7
|
+
promise: Promise<T>;
|
|
8
|
+
first: boolean;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
4
11
|
export declare function ensureVariantFileExists(variantFilePath: string, variant: Variants, file: string, verbose: boolean, compressOptions?: CompressOptions): Promise<void>;
|
|
5
12
|
//# sourceMappingURL=variants.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"variants.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/variants.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAQ,KAAK,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAGrE,MAAM,MAAM,QAAQ,GAAG,UAAU,GAAG,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"variants.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/variants.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAQ,KAAK,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAGrE,MAAM,MAAM,QAAQ,GAAG,UAAU,GAAG,uBAAuB,CAAC;AAK5D,qBAAa,UAAU,CAAC,CAAC;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAiC;IAE1D,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG;QAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE;CAShF;AAED,wBAAsB,uBAAuB,CAC3C,eAAe,EAAE,MAAM,EACvB,OAAO,EAAE,QAAQ,EACjB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,EAChB,eAAe,GAAE,eAAoB,iBA6BtC"}
|
|
@@ -5,6 +5,23 @@
|
|
|
5
5
|
import fs from 'node:fs';
|
|
6
6
|
import { algs } from '../compression/index.js';
|
|
7
7
|
import { rootRelative } from './files.js';
|
|
8
|
+
// Runs an async function one time for each key. The first call for a key
|
|
9
|
+
// starts the function and stores its promise before it returns. Later calls
|
|
10
|
+
// for the same key, also concurrent calls, get the same promise.
|
|
11
|
+
export class OncePerKey {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.promises = new Map();
|
|
14
|
+
}
|
|
15
|
+
run(key, fn) {
|
|
16
|
+
let promise = this.promises.get(key);
|
|
17
|
+
if (promise != null) {
|
|
18
|
+
return { promise, first: false };
|
|
19
|
+
}
|
|
20
|
+
promise = fn();
|
|
21
|
+
this.promises.set(key, promise);
|
|
22
|
+
return { promise, first: true };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
8
25
|
export async function ensureVariantFileExists(variantFilePath, variant, file, verbose, compressOptions = {}) {
|
|
9
26
|
// Compress/prepare the asset if it doesn't already exist
|
|
10
27
|
// We disregard chunked copies of the file and only check for the existence
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export type IndexMetadata = {
|
|
2
2
|
publishedTime?: number;
|
|
3
3
|
expirationTime?: number;
|
|
4
|
+
requires?: string[];
|
|
5
|
+
otherFields?: Record<string, string>;
|
|
4
6
|
};
|
|
7
|
+
export declare const SUPPORTED_INDEX_FEATURES: readonly string[];
|
|
5
8
|
export declare function encodeIndexMetadata(indexMetadata: IndexMetadata): Record<string, string>;
|
|
6
9
|
export declare function decodeIndexMetadata(obj: Record<string, string> | undefined): IndexMetadata | null;
|
|
10
|
+
export declare function getUnsupportedIndexFeatures(indexMetadata: IndexMetadata, supportedFeatures?: readonly string[]): string[];
|
|
7
11
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/models/server/index.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,aAAa,GAAG;IAC1B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/models/server/index.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,aAAa,GAAG;IAC1B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IAIxB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IAGpB,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC,CAAC;AAMF,eAAO,MAAM,wBAAwB,EAAE,SAAS,MAAM,EAAO,CAAC;AAI9D,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAqBxF;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,GAAG,aAAa,GAAG,IAAI,CA2BjG;AAGD,wBAAgB,2BAA2B,CACzC,aAAa,EAAE,aAAa,EAC5B,iBAAiB,GAAE,SAAS,MAAM,EAA6B,GAC9D,MAAM,EAAE,CAEV"}
|
|
@@ -3,14 +3,28 @@
|
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
4
|
*/
|
|
5
5
|
import { getMetadataFieldValue } from '../metadata/index.js';
|
|
6
|
+
// The features that this version of PublisherServer supports. A server does
|
|
7
|
+
// not serve a collection that requires a feature that is not in this list.
|
|
8
|
+
// A feature name must not contain a comma, because the metadata uses commas to
|
|
9
|
+
// separate the names. Use short kebab-case names, for example 'my-feature'.
|
|
10
|
+
export const SUPPORTED_INDEX_FEATURES = [];
|
|
11
|
+
const KNOWN_FIELDS = ['publishedTime', 'expirationTime', 'requires'];
|
|
6
12
|
export function encodeIndexMetadata(indexMetadata) {
|
|
7
|
-
const result = {};
|
|
13
|
+
const result = { ...indexMetadata.otherFields };
|
|
8
14
|
if (indexMetadata.publishedTime != null && !isNaN(indexMetadata.publishedTime)) {
|
|
9
15
|
result['publishedTime'] = String(indexMetadata.publishedTime);
|
|
10
16
|
}
|
|
11
17
|
if (indexMetadata.expirationTime != null && !isNaN(indexMetadata.expirationTime)) {
|
|
12
18
|
result['expirationTime'] = String(indexMetadata.expirationTime);
|
|
13
19
|
}
|
|
20
|
+
if (indexMetadata.requires != null && indexMetadata.requires.length > 0) {
|
|
21
|
+
for (const feature of indexMetadata.requires) {
|
|
22
|
+
if (feature.includes(',')) {
|
|
23
|
+
throw new Error(`Feature name '${feature}' must not contain a comma.`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
result['requires'] = indexMetadata.requires.join(',');
|
|
27
|
+
}
|
|
14
28
|
return result;
|
|
15
29
|
}
|
|
16
30
|
export function decodeIndexMetadata(obj) {
|
|
@@ -19,8 +33,27 @@ export function decodeIndexMetadata(obj) {
|
|
|
19
33
|
}
|
|
20
34
|
const publishedTime = parseFloat(getMetadataFieldValue(obj, 'publishedTime'));
|
|
21
35
|
const expirationTime = parseFloat(getMetadataFieldValue(obj, 'expirationTime'));
|
|
36
|
+
const requiresValue = getMetadataFieldValue(obj, 'requires');
|
|
37
|
+
const requires = requiresValue != null
|
|
38
|
+
? String(requiresValue).split(',').map(x => x.trim()).filter(x => x !== '')
|
|
39
|
+
: [];
|
|
40
|
+
// S3 makes the metadata keys lowercase, so compare the keys without case.
|
|
41
|
+
const knownKeys = KNOWN_FIELDS.map(x => x.toLowerCase());
|
|
42
|
+
let otherFields;
|
|
43
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
44
|
+
if (!knownKeys.includes(key.toLowerCase())) {
|
|
45
|
+
otherFields ??= {};
|
|
46
|
+
otherFields[key] = String(value);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
22
49
|
return {
|
|
23
50
|
publishedTime: !isNaN(publishedTime) ? publishedTime : undefined,
|
|
24
51
|
expirationTime: !isNaN(expirationTime) ? expirationTime : undefined,
|
|
52
|
+
requires: requires.length > 0 ? requires : undefined,
|
|
53
|
+
otherFields,
|
|
25
54
|
};
|
|
26
55
|
}
|
|
56
|
+
// Returns the features in requires that are not in supportedFeatures.
|
|
57
|
+
export function getUnsupportedIndexFeatures(indexMetadata, supportedFeatures = SUPPORTED_INDEX_FEATURES) {
|
|
58
|
+
return (indexMetadata.requires ?? []).filter(x => !supportedFeatures.includes(x));
|
|
59
|
+
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export type IndexMetadata = {
|
|
2
2
|
publishedTime?: number;
|
|
3
3
|
expirationTime?: number;
|
|
4
|
+
requires?: string[];
|
|
5
|
+
otherFields?: Record<string, string>;
|
|
4
6
|
};
|
|
7
|
+
export declare const SUPPORTED_INDEX_FEATURES: readonly string[];
|
|
5
8
|
export declare function encodeIndexMetadata(indexMetadata: IndexMetadata): Record<string, string>;
|
|
6
9
|
export declare function decodeIndexMetadata(obj: Record<string, string> | undefined): IndexMetadata | null;
|
|
10
|
+
export declare function getUnsupportedIndexFeatures(indexMetadata: IndexMetadata, supportedFeatures?: readonly string[]): string[];
|
|
7
11
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/models/server/index.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,aAAa,GAAG;IAC1B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/models/server/index.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,aAAa,GAAG;IAC1B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IAIxB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IAGpB,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC,CAAC;AAMF,eAAO,MAAM,wBAAwB,EAAE,SAAS,MAAM,EAAO,CAAC;AAI9D,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAqBxF;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,GAAG,aAAa,GAAG,IAAI,CA2BjG;AAGD,wBAAgB,2BAA2B,CACzC,aAAa,EAAE,aAAa,EAC5B,iBAAiB,GAAE,SAAS,MAAM,EAA6B,GAC9D,MAAM,EAAE,CAEV"}
|
|
@@ -3,14 +3,28 @@
|
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
4
|
*/
|
|
5
5
|
import { getMetadataFieldValue } from '../metadata/index.js';
|
|
6
|
+
// The features that this version of PublisherServer supports. A server does
|
|
7
|
+
// not serve a collection that requires a feature that is not in this list.
|
|
8
|
+
// A feature name must not contain a comma, because the metadata uses commas to
|
|
9
|
+
// separate the names. Use short kebab-case names, for example 'my-feature'.
|
|
10
|
+
export const SUPPORTED_INDEX_FEATURES = [];
|
|
11
|
+
const KNOWN_FIELDS = ['publishedTime', 'expirationTime', 'requires'];
|
|
6
12
|
export function encodeIndexMetadata(indexMetadata) {
|
|
7
|
-
const result = {};
|
|
13
|
+
const result = { ...indexMetadata.otherFields };
|
|
8
14
|
if (indexMetadata.publishedTime != null && !isNaN(indexMetadata.publishedTime)) {
|
|
9
15
|
result['publishedTime'] = String(indexMetadata.publishedTime);
|
|
10
16
|
}
|
|
11
17
|
if (indexMetadata.expirationTime != null && !isNaN(indexMetadata.expirationTime)) {
|
|
12
18
|
result['expirationTime'] = String(indexMetadata.expirationTime);
|
|
13
19
|
}
|
|
20
|
+
if (indexMetadata.requires != null && indexMetadata.requires.length > 0) {
|
|
21
|
+
for (const feature of indexMetadata.requires) {
|
|
22
|
+
if (feature.includes(',')) {
|
|
23
|
+
throw new Error(`Feature name '${feature}' must not contain a comma.`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
result['requires'] = indexMetadata.requires.join(',');
|
|
27
|
+
}
|
|
14
28
|
return result;
|
|
15
29
|
}
|
|
16
30
|
export function decodeIndexMetadata(obj) {
|
|
@@ -19,8 +33,27 @@ export function decodeIndexMetadata(obj) {
|
|
|
19
33
|
}
|
|
20
34
|
const publishedTime = parseFloat(getMetadataFieldValue(obj, 'publishedTime'));
|
|
21
35
|
const expirationTime = parseFloat(getMetadataFieldValue(obj, 'expirationTime'));
|
|
36
|
+
const requiresValue = getMetadataFieldValue(obj, 'requires');
|
|
37
|
+
const requires = requiresValue != null
|
|
38
|
+
? String(requiresValue).split(',').map(x => x.trim()).filter(x => x !== '')
|
|
39
|
+
: [];
|
|
40
|
+
// S3 makes the metadata keys lowercase, so compare the keys without case.
|
|
41
|
+
const knownKeys = KNOWN_FIELDS.map(x => x.toLowerCase());
|
|
42
|
+
let otherFields;
|
|
43
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
44
|
+
if (!knownKeys.includes(key.toLowerCase())) {
|
|
45
|
+
otherFields ??= {};
|
|
46
|
+
otherFields[key] = String(value);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
22
49
|
return {
|
|
23
50
|
publishedTime: !isNaN(publishedTime) ? publishedTime : undefined,
|
|
24
51
|
expirationTime: !isNaN(expirationTime) ? expirationTime : undefined,
|
|
52
|
+
requires: requires.length > 0 ? requires : undefined,
|
|
53
|
+
otherFields,
|
|
25
54
|
};
|
|
26
55
|
}
|
|
56
|
+
// Returns the features in requires that are not in supportedFeatures.
|
|
57
|
+
export function getUnsupportedIndexFeatures(indexMetadata, supportedFeatures = SUPPORTED_INDEX_FEATURES) {
|
|
58
|
+
return (indexMetadata.requires ?? []).filter(x => !supportedFeatures.includes(x));
|
|
59
|
+
}
|
|
@@ -45,7 +45,6 @@ export declare class PublisherServer {
|
|
|
45
45
|
private findAssetVariantForAcceptEncodingsGroups;
|
|
46
46
|
serveAsset(request: Request, asset: AssetEntry, init?: AssetInit): Promise<Response>;
|
|
47
47
|
serveCached(request: Request, keyPath: string, produce: (request: Request) => Promise<Response | null>): Promise<Response | null>;
|
|
48
|
-
private copyBodyToCache;
|
|
49
48
|
private responseFromCache;
|
|
50
49
|
serveRequest(request: Request): Promise<Response | null>;
|
|
51
50
|
private serveRequestFromStorage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/server/publisher-server/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/server/publisher-server/index.ts"],"names":[],"mappings":"AASA,OAAO,EACL,KAAK,eAAe,EACrB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EACL,KAAK,+BAA+B,EACrC,MAAM,gDAAgD,CAAC;AACxD,OAAO,EACL,KAAK,uBAAuB,EAC7B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EACL,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,oBAAoB,EAE1B,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,eAAe,EA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
|
4
4
|
*/
|
|
5
|
+
/// <reference types="@fastly/js-compute" />
|
|
5
6
|
import { CoreCache } from 'fastly:cache';
|
|
6
7
|
import { decodeAssetVariantMetadata, } from '../../models/assets/index.js';
|
|
7
|
-
import { decodeIndexMetadata, } from '../../models/server/index.js';
|
|
8
|
+
import { decodeIndexMetadata, getUnsupportedIndexFeatures } from '../../models/server/index.js';
|
|
8
9
|
import { isExpired } from '../../models/time/index.js';
|
|
9
10
|
import { loadStorageProviderFromStaticPublishRc, } from '../storage/storage-provider.js';
|
|
10
11
|
import { checkIfModifiedSince, getIfModifiedSinceHeader } from './serve-preconditions/if-modified-since.js';
|
|
11
12
|
import { checkIfNoneMatch, getIfNoneMatchHeader } from './serve-preconditions/if-none-match.js';
|
|
12
13
|
import { ServerTiming } from '../util/server-timing.js';
|
|
13
|
-
import { CACHEABLE_STATUSES, buildCacheFillRequest, buildResponseCacheKey, decodeCachedResponseMetadata, encodeCachedResponseMetadata, parseAcceptEncodingGroups, } from './response-cache.js';
|
|
14
|
+
import { CACHEABLE_STATUSES, DEFAULT_MAX_BODY_SIZE, buildCacheFillRequest, buildResponseCacheKey, decodeCachedResponseMetadata, encodeCachedResponseMetadata, parseAcceptEncodingGroups, readBodyUpTo, } from './response-cache.js';
|
|
15
|
+
import { checkBodyLength } from '../util/body-length.js';
|
|
14
16
|
export function buildHeadersSubset(responseHeaders, keys) {
|
|
15
17
|
const resultHeaders = new Headers();
|
|
16
18
|
for (const value of keys) {
|
|
@@ -140,21 +142,28 @@ export class PublisherServer {
|
|
|
140
142
|
this.assetEntryMapCache = null;
|
|
141
143
|
return null;
|
|
142
144
|
}
|
|
143
|
-
let
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
metadata = JSON.parse(metadataText);
|
|
150
|
-
}
|
|
151
|
-
catch {
|
|
152
|
-
}
|
|
153
|
-
metadata = decodeIndexMetadata(metadata);
|
|
145
|
+
let metadata;
|
|
146
|
+
const metadataText = indexFile.metadataText();
|
|
147
|
+
if (metadataText != null) {
|
|
148
|
+
try {
|
|
149
|
+
metadata = JSON.parse(metadataText);
|
|
154
150
|
}
|
|
155
|
-
|
|
156
|
-
collectionIsExpired = metadata.expirationTime != null && isExpired(metadata.expirationTime);
|
|
151
|
+
catch {
|
|
157
152
|
}
|
|
153
|
+
metadata = decodeIndexMetadata(metadata);
|
|
154
|
+
}
|
|
155
|
+
// Do not serve a collection that needs a feature that this version does not
|
|
156
|
+
// support. This check applies to all collections, also the default collection.
|
|
157
|
+
const unsupportedFeatures = metadata != null ? getUnsupportedIndexFeatures(metadata) : [];
|
|
158
|
+
if (unsupportedFeatures.length > 0) {
|
|
159
|
+
console.error(`Collection at ${indexFileKey} requires features that this version of the server does not support: ${unsupportedFeatures.join(', ')}.`);
|
|
160
|
+
console.error(`Build and deploy the application with a newer version of @fastly/compute-js-static-publish.`);
|
|
161
|
+
this.assetEntryMapCache = null;
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
let collectionIsExpired = false;
|
|
165
|
+
if (this.activeCollectionName !== this.defaultCollectionName && metadata != null) {
|
|
166
|
+
collectionIsExpired = metadata.expirationTime != null && isExpired(metadata.expirationTime);
|
|
158
167
|
}
|
|
159
168
|
if (collectionIsExpired) {
|
|
160
169
|
console.error(`Requested collection expired at ${indexFileKey}.`);
|
|
@@ -392,8 +401,11 @@ export class PublisherServer {
|
|
|
392
401
|
if (preconditionResponse != null) {
|
|
393
402
|
return preconditionResponse;
|
|
394
403
|
}
|
|
395
|
-
|
|
396
|
-
|
|
404
|
+
// If the body does not have the size in its metadata, the response must not
|
|
405
|
+
// look complete. An empty body throws here, and any other mismatch causes an
|
|
406
|
+
// error in the body stream, so that the client gets an incomplete response.
|
|
407
|
+
const body = await checkBodyLength(assetVariant.storageEntry.body, assetVariant.size, `File ${asset.key} (${assetVariant.contentEncoding ?? 'identity'})`);
|
|
408
|
+
return new Response(body, {
|
|
397
409
|
status: init?.status ?? 200,
|
|
398
410
|
headers,
|
|
399
411
|
});
|
|
@@ -415,6 +427,10 @@ export class PublisherServer {
|
|
|
415
427
|
const state = entry.state();
|
|
416
428
|
if (!state.mustInsertOrUpdate()) {
|
|
417
429
|
const metadata = state.found() && state.usable() ? decodeCachedResponseMetadata(entry.userMetadata()) : null;
|
|
430
|
+
if (metadata?.tooLarge) {
|
|
431
|
+
this.serverTiming?.add('cache', performance.now() - lookupStart, 'bypass');
|
|
432
|
+
return produce(request);
|
|
433
|
+
}
|
|
418
434
|
if (metadata != null) {
|
|
419
435
|
// age() is in milliseconds. A hit younger than the time since a purge proves the entry was refilled.
|
|
420
436
|
this.serverTiming?.add('cache', performance.now() - lookupStart, `hit age=${Math.round(entry.age() / 1000)}s`);
|
|
@@ -457,45 +473,39 @@ export class PublisherServer {
|
|
|
457
473
|
// Server-Timing describes one request, so it is not stored.
|
|
458
474
|
headers.delete('Server-Timing');
|
|
459
475
|
const metadata = { status: response.status, headers: [...headers] };
|
|
460
|
-
|
|
461
|
-
//
|
|
462
|
-
//
|
|
463
|
-
//
|
|
464
|
-
//
|
|
465
|
-
|
|
466
|
-
...insertOptions,
|
|
467
|
-
userMetadata: encodeCachedResponseMetadata(metadata),
|
|
468
|
-
length: Number.isInteger(contentLength) && headers.has('Content-Length') ? contentLength : undefined,
|
|
469
|
-
});
|
|
470
|
-
const copied = this.copyBodyToCache(response, writer);
|
|
471
|
-
const cachedResponse = this.responseFromCache(request, metadata, streamedEntry.body());
|
|
472
|
-
// A response without a body (HEAD, 304) does not keep the request running
|
|
473
|
-
// until the copy finishes. If the copy is cut short, the entry can be stored
|
|
474
|
-
// with a partial body, so wait for it.
|
|
475
|
-
if (cachedResponse?.body == null) {
|
|
476
|
-
await copied;
|
|
477
|
-
}
|
|
478
|
-
return cachedResponse;
|
|
479
|
-
}
|
|
480
|
-
async copyBodyToCache(response, writer) {
|
|
476
|
+
// Read the whole body before the insertion starts. After it starts, a body
|
|
477
|
+
// that is not complete is not safe: the requests that read the entry get
|
|
478
|
+
// a short body that ends as if it is complete. If the read fails (for example,
|
|
479
|
+
// serveAsset() finds a body that does not have the size in its metadata),
|
|
480
|
+
// cancel() stores nothing, and the next request fills the entry again.
|
|
481
|
+
let readResult;
|
|
481
482
|
try {
|
|
482
|
-
|
|
483
|
-
const reader = response.body.getReader();
|
|
484
|
-
for (;;) {
|
|
485
|
-
const { done, value } = await reader.read();
|
|
486
|
-
if (done) {
|
|
487
|
-
break;
|
|
488
|
-
}
|
|
489
|
-
writer.append(value);
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
writer.close();
|
|
483
|
+
readResult = await readBodyUpTo(response.body, this.responseCache.maxBodySize ?? DEFAULT_MAX_BODY_SIZE);
|
|
493
484
|
}
|
|
494
485
|
catch (err) {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
486
|
+
entry.cancel();
|
|
487
|
+
throw err;
|
|
488
|
+
}
|
|
489
|
+
if ('body' in readResult) {
|
|
490
|
+
// Too large to cache. Cache a marker, so that later requests do not read
|
|
491
|
+
// the body to find this again, and serve the body from storage.
|
|
492
|
+
const tooLargeMetadata = { tooLarge: true };
|
|
493
|
+
const writer = entry.insert({
|
|
494
|
+
...insertOptions,
|
|
495
|
+
userMetadata: encodeCachedResponseMetadata(tooLargeMetadata),
|
|
496
|
+
length: 0,
|
|
497
|
+
});
|
|
498
|
+
writer.close();
|
|
499
|
+
return this.responseFromCache(request, metadata, readResult.body);
|
|
498
500
|
}
|
|
501
|
+
const writer = entry.insert({
|
|
502
|
+
...insertOptions,
|
|
503
|
+
userMetadata: encodeCachedResponseMetadata(metadata),
|
|
504
|
+
length: readResult.bytes.byteLength,
|
|
505
|
+
});
|
|
506
|
+
writer.append(readResult.bytes);
|
|
507
|
+
writer.close();
|
|
508
|
+
return this.responseFromCache(request, metadata, readResult.bytes);
|
|
499
509
|
}
|
|
500
510
|
responseFromCache(request, metadata, body) {
|
|
501
511
|
if (metadata.notFound) {
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { type ContentCompressionTypes } from '../../models/compression/index.js';
|
|
2
2
|
export type ResponseCacheOptions = {
|
|
3
3
|
maxAge: number;
|
|
4
|
+
maxBodySize?: number;
|
|
4
5
|
};
|
|
6
|
+
export declare const DEFAULT_MAX_BODY_SIZE: number;
|
|
5
7
|
export type CachedResponseMetadata = {
|
|
6
8
|
notFound: true;
|
|
9
|
+
tooLarge?: false;
|
|
7
10
|
} | {
|
|
8
11
|
notFound?: false;
|
|
12
|
+
tooLarge: true;
|
|
13
|
+
} | {
|
|
14
|
+
notFound?: false;
|
|
15
|
+
tooLarge?: false;
|
|
9
16
|
status: number;
|
|
10
17
|
headers: [string, string][];
|
|
11
18
|
};
|
|
@@ -14,6 +21,12 @@ export declare function parseAcceptEncodingGroups(headerValue: string, allowedEn
|
|
|
14
21
|
export declare function acceptEncodingCacheKeyPart(request: Request): string;
|
|
15
22
|
export declare function buildResponseCacheKey(publishId: string, collectionName: string, keyPath: string, request: Request): string;
|
|
16
23
|
export declare function buildCacheFillRequest(request: Request): Request;
|
|
24
|
+
export type ReadBodyResult = {
|
|
25
|
+
bytes: Uint8Array<ArrayBuffer>;
|
|
26
|
+
} | {
|
|
27
|
+
body: ReadableStream<Uint8Array>;
|
|
28
|
+
};
|
|
29
|
+
export declare function readBodyUpTo(body: ReadableStream<Uint8Array> | null, maxLength: number): Promise<ReadBodyResult>;
|
|
17
30
|
export declare function encodeCachedResponseMetadata(metadata: CachedResponseMetadata): string;
|
|
18
31
|
export declare function decodeCachedResponseMetadata(buffer: ArrayBuffer): CachedResponseMetadata | null;
|
|
19
32
|
//# sourceMappingURL=response-cache.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response-cache.d.ts","sourceRoot":"","sources":["../../../../src/server/publisher-server/response-cache.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,uBAAuB,EAE7B,MAAM,mCAAmC,CAAC;AAE3C,MAAM,MAAM,oBAAo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AAU,CAAC,WAAW,CAAC,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,CAAA;CAAE,CAAC;AAMzC,wBAAsB,YAAY,CAChC,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,IAAI,EACvC,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,cAAc,CAAC,CA8CzB;AAED,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,sBAAsB,GAAG,MAAM,CAErF;AAED,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,WAAW,GAAG,sBAAsB,GAAG,IAAI,CAgB/F"}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
4
|
*/
|
|
5
5
|
import { compressionTypes, } from '../../models/compression/index.js';
|
|
6
|
+
export const DEFAULT_MAX_BODY_SIZE = 8 * 1024 * 1024;
|
|
6
7
|
// Statuses that serveAsset() produces for a found file or a not-found page.
|
|
7
8
|
// Anything else (for example, a 500 when the settings are missing) is not cached.
|
|
8
9
|
export const CACHEABLE_STATUSES = [200, 404];
|
|
@@ -86,6 +87,56 @@ export function buildCacheFillRequest(request) {
|
|
|
86
87
|
}
|
|
87
88
|
return new Request(request.url, { method: 'GET', headers });
|
|
88
89
|
}
|
|
90
|
+
// Reads body completely if it has maxLength bytes or fewer, and returns the bytes.
|
|
91
|
+
// Otherwise it stops reading, and returns a stream with all the bytes of body,
|
|
92
|
+
// including the bytes that it read. An error from body rejects the promise, or,
|
|
93
|
+
// after the limit, errors the stream.
|
|
94
|
+
export async function readBodyUpTo(body, maxLength) {
|
|
95
|
+
if (body == null) {
|
|
96
|
+
return { bytes: new Uint8Array(0) };
|
|
97
|
+
}
|
|
98
|
+
const reader = body.getReader();
|
|
99
|
+
const chunks = [];
|
|
100
|
+
let length = 0;
|
|
101
|
+
for (;;) {
|
|
102
|
+
const { done, value } = await reader.read();
|
|
103
|
+
if (done) {
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
chunks.push(value);
|
|
107
|
+
length += value.byteLength;
|
|
108
|
+
if (length > maxLength) {
|
|
109
|
+
return {
|
|
110
|
+
body: new ReadableStream({
|
|
111
|
+
async pull(controller) {
|
|
112
|
+
const chunk = chunks.shift();
|
|
113
|
+
if (chunk != null) {
|
|
114
|
+
controller.enqueue(chunk);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const result = await reader.read();
|
|
118
|
+
if (result.done) {
|
|
119
|
+
controller.close();
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
controller.enqueue(result.value);
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
async cancel(reason) {
|
|
126
|
+
await reader.cancel(reason);
|
|
127
|
+
},
|
|
128
|
+
}),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const bytes = new Uint8Array(length);
|
|
133
|
+
let offset = 0;
|
|
134
|
+
for (const chunk of chunks) {
|
|
135
|
+
bytes.set(chunk, offset);
|
|
136
|
+
offset += chunk.byteLength;
|
|
137
|
+
}
|
|
138
|
+
return { bytes };
|
|
139
|
+
}
|
|
89
140
|
export function encodeCachedResponseMetadata(metadata) {
|
|
90
141
|
return JSON.stringify(metadata);
|
|
91
142
|
}
|
|
@@ -95,6 +146,9 @@ export function decodeCachedResponseMetadata(buffer) {
|
|
|
95
146
|
if (value?.notFound === true) {
|
|
96
147
|
return { notFound: true };
|
|
97
148
|
}
|
|
149
|
+
if (value?.tooLarge === true) {
|
|
150
|
+
return { tooLarge: true };
|
|
151
|
+
}
|
|
98
152
|
if (typeof value?.status === 'number' && Array.isArray(value.headers)) {
|
|
99
153
|
return { status: value.status, headers: value.headers };
|
|
100
154
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare class BodyLengthError extends Error {
|
|
2
|
+
constructor(description: string, expectedLength: number, actualLength: number, complete: boolean);
|
|
3
|
+
}
|
|
4
|
+
export declare function checkBodyLength(body: ReadableStream<Uint8Array>, expectedLength: number, description: string): Promise<ReadableStream<Uint8Array>>;
|
|
5
|
+
//# sourceMappingURL=body-length.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-length.d.ts","sourceRoot":"","sources":["../../../../src/server/util/body-length.ts"],"names":[],"mappings":"AAQA,qBAAa,eAAgB,SAAQ,KAAK;gBAC5B,WAAW,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO;CAIjG;AAaD,wBAAsB,eAAe,CACnC,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,EAChC,cAAc,EAAE,MAAM,EACtB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC,CAyCrC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fastly, Inc.
|
|
3
|
+
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
|
+
*/
|
|
5
|
+
// A stored body that does not have the size that its metadata gives. Storage can
|
|
6
|
+
// return such a body for a short time, for example a KV Store entry whose
|
|
7
|
+
// metadata reached the POP before its body.
|
|
8
|
+
export class BodyLengthError extends Error {
|
|
9
|
+
constructor(description, expectedLength, actualLength, complete) {
|
|
10
|
+
super(`${description}: expected ${expectedLength} bytes, got ${complete ? '' : 'at least '}${actualLength}`);
|
|
11
|
+
this.name = 'BodyLengthError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
// The runtime does not log the message of an error from the request handler or
|
|
15
|
+
// from a response body stream. Thus log the error here.
|
|
16
|
+
function logged(err) {
|
|
17
|
+
console.error(err.message);
|
|
18
|
+
return err;
|
|
19
|
+
}
|
|
20
|
+
// Returns a stream with the bytes of body. The stream errors with a
|
|
21
|
+
// BodyLengthError if body has a different number of bytes than expectedLength.
|
|
22
|
+
// This function reads the first chunk before it returns, so it throws for an
|
|
23
|
+
// empty body before a response is made from the stream.
|
|
24
|
+
export async function checkBodyLength(body, expectedLength, description) {
|
|
25
|
+
const reader = body.getReader();
|
|
26
|
+
const first = await reader.read();
|
|
27
|
+
let length = first.done ? 0 : first.value.byteLength;
|
|
28
|
+
if ((first.done && expectedLength !== 0) || length > expectedLength) {
|
|
29
|
+
await reader.cancel();
|
|
30
|
+
throw logged(new BodyLengthError(description, expectedLength, length, first.done));
|
|
31
|
+
}
|
|
32
|
+
let firstChunk = first.done ? null : first.value;
|
|
33
|
+
let done = first.done;
|
|
34
|
+
return new ReadableStream({
|
|
35
|
+
async pull(controller) {
|
|
36
|
+
if (firstChunk != null) {
|
|
37
|
+
controller.enqueue(firstChunk);
|
|
38
|
+
firstChunk = null;
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const result = done ? { done: true } : await reader.read();
|
|
42
|
+
if (!result.done) {
|
|
43
|
+
length += result.value.byteLength;
|
|
44
|
+
if (length > expectedLength) {
|
|
45
|
+
await reader.cancel();
|
|
46
|
+
controller.error(logged(new BodyLengthError(description, expectedLength, length, false)));
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
controller.enqueue(result.value);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
done = true;
|
|
53
|
+
if (length !== expectedLength) {
|
|
54
|
+
controller.error(logged(new BodyLengthError(description, expectedLength, length, true)));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
controller.close();
|
|
58
|
+
},
|
|
59
|
+
async cancel(reason) {
|
|
60
|
+
await reader.cancel(reason);
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastly/compute-js-static-publish",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "8.0.0
|
|
4
|
+
"version": "8.0.0",
|
|
5
5
|
"description": "Static Publisher for Fastly Compute JavaScript",
|
|
6
|
-
"main": "build/index.js",
|
|
6
|
+
"main": "build/server/server/index.js",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
9
|
"types": "./build/server/server/index.d.ts",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@smithy/signature-v4": "^5.7.4",
|
|
47
47
|
"command-line-args": "^5.2.1",
|
|
48
48
|
"glob-to-regexp": "^0.4.1",
|
|
49
|
-
"toml": "^5.0.
|
|
49
|
+
"toml": "^5.0.1"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"@fastly/js-compute": "^3.33.2 || ^4.0.0"
|