@fastly/compute-js-static-publish 8.0.1 → 8.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +544 -0
- package/DOCUMENTATION.md +31 -15
- package/MIGRATING.md +128 -0
- package/build/cli/cli/commands/manage/collections/promote.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/promote.js +43 -4
- package/build/cli/cli/commands/manage/publish-content.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/publish-content.js +5 -13
- package/build/cli/cli/commands/scaffold/index.js +12 -13
- package/build/cli/cli/storage/kv-store-local-provider.d.ts +1 -0
- package/build/cli/cli/storage/kv-store-local-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/kv-store-local-provider.js +4 -1
- package/build/cli/cli/storage/kv-store-provider.d.ts +1 -0
- package/build/cli/cli/storage/kv-store-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/kv-store-provider.js +9 -1
- 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 +4 -0
- package/build/cli/cli/storage/storage-provider.d.ts +3 -0
- package/build/cli/cli/storage/storage-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/storage-provider.js +32 -0
- package/build/cli/cli/util/kv-store-items.d.ts +1 -0
- package/build/cli/cli/util/kv-store-items.d.ts.map +1 -1
- package/build/cli/cli/util/kv-store-items.js +16 -40
- package/build/cli/cli/util/s3.d.ts +10 -4
- package/build/cli/cli/util/s3.d.ts.map +1 -1
- package/build/cli/cli/util/s3.js +13 -35
- package/build/server/server/publisher-server/accept-html.d.ts +2 -0
- package/build/server/server/publisher-server/accept-html.d.ts.map +1 -0
- package/build/server/server/publisher-server/accept-html.js +50 -0
- package/build/server/server/publisher-server/index.d.ts.map +1 -1
- package/build/server/server/publisher-server/index.js +8 -12
- package/build/server/server/publisher-server/response-cache.d.ts +3 -0
- package/build/server/server/publisher-server/response-cache.d.ts.map +1 -1
- package/build/server/server/publisher-server/response-cache.js +24 -1
- package/package.json +4 -2
|
@@ -37,3 +37,35 @@ export async function loadStorageProviderFromStaticPublishRc(config, context) {
|
|
|
37
37
|
}
|
|
38
38
|
throw new Error('Static Publisher Error: Invalid static-publish.rc.js, storage mode not recognized, or could not instantiate store provider.');
|
|
39
39
|
}
|
|
40
|
+
// Returns true if storage has the variant with the key `variantKey`, which has
|
|
41
|
+
// `size` bytes in `numChunks` chunks. For a variant in more than one chunk, it
|
|
42
|
+
// also compares the stored size of each chunk with its expected size. It removes
|
|
43
|
+
// a chunk that has a different size, or that is not in storage now, from
|
|
44
|
+
// existingKeys, so that applyBatch() uploads the chunk again.
|
|
45
|
+
export async function isVariantInStorage(storageProvider, existingKeys, variantKey, size, numChunks) {
|
|
46
|
+
const chunkKeys = Array.from({ length: numChunks }, (_, chunkIndex) => chunkIndex === 0 ? variantKey : `${variantKey}_${chunkIndex}`);
|
|
47
|
+
if (!chunkKeys.every(key => existingKeys.has(key))) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (numChunks === 1) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
const expectedSizes = storageProvider.calculateChunkSizes(size);
|
|
54
|
+
const entries = await Promise.all(chunkKeys.map(key => storageProvider.getStorageEntryInfo(key)));
|
|
55
|
+
let found = true;
|
|
56
|
+
for (const [chunkIndex, key] of chunkKeys.entries()) {
|
|
57
|
+
const entry = entries[chunkIndex];
|
|
58
|
+
if (entry == null) {
|
|
59
|
+
existingKeys.delete(key);
|
|
60
|
+
found = false;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const storedSize = entry.size;
|
|
64
|
+
if (storedSize != null && storedSize !== expectedSizes[chunkIndex]) {
|
|
65
|
+
console.log(` ‼️ Chunk "${key}" in storage has ${storedSize} bytes, but it must have ${expectedSizes[chunkIndex]} bytes. It will be uploaded again.`);
|
|
66
|
+
existingKeys.delete(key);
|
|
67
|
+
found = false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return found;
|
|
71
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { StorageProviderBatchEntry } from '../storage/storage-provider.js';
|
|
2
2
|
export declare const KV_STORE_CHUNK_SIZE: number;
|
|
3
|
+
export declare function chunkSizes(size: number, chunkSize: number): number[];
|
|
3
4
|
export declare function shouldRecreateChunks(chunksDir: string, numChunks: number, item: StorageProviderBatchEntry, chunkSize: number): boolean;
|
|
4
5
|
export declare function applyKVStoreEntriesChunks(kvStoreItemDescriptions: StorageProviderBatchEntry[], chunkSize: number): Promise<void>;
|
|
5
6
|
export declare const KV_BATCH_MAX_ITEMS = 256;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kv-store-items.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/kv-store-items.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"kv-store-items.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/kv-store-items.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAI3E,eAAO,MAAM,mBAAmB,QAAqB,CAAC;AAItD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAGpE;AAED,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,yBAAyB,EAAE,SAAS,EAAE,MAAM,WAkC5H;AAED,wBAAsB,yBAAyB,CAAC,uBAAuB,EAAE,yBAAyB,EAAE,EAAE,SAAS,EAAE,MAAM,iBAiEtH;AAED,eAAO,MAAM,kBAAkB,MAAM,CAAC;AACtC,eAAO,MAAM,kBAAkB,QAAoB,CAAC;AAEpD,MAAM,MAAM,WAAW,GAAG;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,yBAAyB,EAAE,CAAC;CACtC,CAAC;AAEF,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,yBAAyB,EAAE,EACpC,QAAQ,GAAE,MAA2B,EACrC,QAAQ,GAAE,MAA2B,GACpC;IAAE,OAAO,EAAE,WAAW,EAAE,CAAC;IAAC,SAAS,EAAE,yBAAyB,EAAE,CAAA;CAAE,CAuCpE;AAED,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,yBAAyB,GAAG,MAAM,CAOhF;AAED,wBAAgB,eAAe,CAC7B,OAAO,EAAE,yBAAyB,EAAE,EACpC,YAAY,EAAE,GAAG,CAAC,MAAM,CAAC,GACxB,yBAAyB,EAAE,CAE7B"}
|
|
@@ -3,9 +3,16 @@
|
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
4
|
*/
|
|
5
5
|
import fs from 'node:fs';
|
|
6
|
+
import { pipeline } from 'node:stream/promises';
|
|
6
7
|
import { directoryExists, getFileSize } from './files.js';
|
|
7
8
|
// split large files into 20MiB chunks
|
|
8
9
|
export const KV_STORE_CHUNK_SIZE = 1_024 * 1_024 * 20;
|
|
10
|
+
// The size of each chunk of a file of `size` bytes. All chunks have chunkSize
|
|
11
|
+
// bytes, except the last, which has the bytes that are left.
|
|
12
|
+
export function chunkSizes(size, chunkSize) {
|
|
13
|
+
const numChunks = Math.ceil(size / chunkSize);
|
|
14
|
+
return Array.from({ length: numChunks }, (_, chunk) => Math.min(chunkSize, size - chunk * chunkSize));
|
|
15
|
+
}
|
|
9
16
|
export function shouldRecreateChunks(chunksDir, numChunks, item, chunkSize) {
|
|
10
17
|
console.log(` 📄 '${item.key}' - ${item.size} bytes → ${numChunks} chunks`);
|
|
11
18
|
if (!directoryExists(chunksDir)) {
|
|
@@ -17,7 +24,7 @@ export function shouldRecreateChunks(chunksDir, numChunks, item, chunkSize) {
|
|
|
17
24
|
console.log(` ・ Recreating chunks for '${item.key}' - found ${existingChunks} existing chunk(s), expected ${numChunks}`);
|
|
18
25
|
return true;
|
|
19
26
|
}
|
|
20
|
-
const
|
|
27
|
+
const expectedFileSizes = chunkSizes(item.size, chunkSize);
|
|
21
28
|
for (let chunk = 0; chunk < numChunks; chunk++) {
|
|
22
29
|
const chunkFileName = `${chunksDir}/${chunk}`;
|
|
23
30
|
// If the chunk does not exist
|
|
@@ -26,7 +33,7 @@ export function shouldRecreateChunks(chunksDir, numChunks, item, chunkSize) {
|
|
|
26
33
|
return true;
|
|
27
34
|
}
|
|
28
35
|
// or if the chunk isn't the expected size
|
|
29
|
-
const expectedFileSize = chunk
|
|
36
|
+
const expectedFileSize = expectedFileSizes[chunk];
|
|
30
37
|
const existingFileSize = getFileSize(chunkFileName);
|
|
31
38
|
if (existingFileSize !== expectedFileSize) {
|
|
32
39
|
console.log(` ・ Recreating chunks for '${item.key}' - chunk ${chunk} existing file size ${existingFileSize} does not match expected size ${expectedFileSize}`);
|
|
@@ -52,44 +59,13 @@ export async function applyKVStoreEntriesChunks(kvStoreItemDescriptions, chunkSi
|
|
|
52
59
|
// Recreate chunks
|
|
53
60
|
fs.rmSync(chunksDir, { recursive: true, force: true });
|
|
54
61
|
fs.mkdirSync(chunksDir, { recursive: true });
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
inputStream.on('data', (chunk) => {
|
|
63
|
-
let offset = 0;
|
|
64
|
-
while (offset < chunk.length) {
|
|
65
|
-
if (!currentStream || bytesWritten >= chunkSize) {
|
|
66
|
-
if (currentStream != null) {
|
|
67
|
-
currentStream.end();
|
|
68
|
-
}
|
|
69
|
-
const chunkFileName = `${chunksDir}/${chunkIndex}`;
|
|
70
|
-
currentStream = fs.createWriteStream(chunkFileName);
|
|
71
|
-
chunkPaths.push(chunkFileName);
|
|
72
|
-
chunkIndex++;
|
|
73
|
-
bytesWritten = 0;
|
|
74
|
-
}
|
|
75
|
-
const bytesLeftInChunk = chunkSize - bytesWritten;
|
|
76
|
-
const bytesLeftInBuffer = chunk.length - offset;
|
|
77
|
-
const bytesToWrite = Math.min(bytesLeftInChunk, bytesLeftInBuffer);
|
|
78
|
-
const slice = chunk.slice(offset, offset + bytesToWrite);
|
|
79
|
-
currentStream.write(slice);
|
|
80
|
-
bytesWritten += bytesToWrite;
|
|
81
|
-
offset += bytesToWrite;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
inputStream.on('end', () => {
|
|
85
|
-
if (currentStream != null) {
|
|
86
|
-
currentStream.end();
|
|
87
|
-
}
|
|
88
|
-
resolve(chunkPaths.length);
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
if (numWrittenChunks !== numChunks) {
|
|
92
|
-
throw new Error(`numWrittenChunks (${numWrittenChunks}) does not equal numChunks (${numChunks})`);
|
|
62
|
+
// Each chunk is a copy of a range of the file. pipeline() resolves
|
|
63
|
+
// after the chunk file is completely written, so the upload reads the
|
|
64
|
+
// full chunk.
|
|
65
|
+
let start = 0;
|
|
66
|
+
for (const [chunkIndex, size] of chunkSizes(item.size, chunkSize).entries()) {
|
|
67
|
+
await pipeline(fs.createReadStream(item.filePath, { start, end: start + size - 1 }), fs.createWriteStream(`${chunksDir}/${chunkIndex}`));
|
|
68
|
+
start += size;
|
|
93
69
|
}
|
|
94
70
|
}
|
|
95
71
|
for (let chunkIndex = 0; chunkIndex < numChunks; chunkIndex++) {
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* The AWS S3 host name of a bucket, for the backend of the app:
|
|
3
|
+
* <bucket>.s3.<region>.amazonaws.com. The server uses the same host name for a
|
|
4
|
+
* bucket without an endpoint (see objectUrl() in
|
|
5
|
+
* src/server/storage/s3-storage-provider.ts). It needs no credentials and no
|
|
6
|
+
* network.
|
|
7
|
+
*
|
|
8
|
+
* Returns null for a bucket name with a dot: under HTTPS, such a name does not
|
|
9
|
+
* match the certificate of *.s3.<region>.amazonaws.com. Such a bucket needs
|
|
10
|
+
* an endpoint, so that the bucket is in the path of the URL.
|
|
5
11
|
*/
|
|
6
|
-
export declare function findHostnameForAwsS3RegionAndBucket(region: string, bucket: string):
|
|
12
|
+
export declare function findHostnameForAwsS3RegionAndBucket(region: string, bucket: string): string | null;
|
|
7
13
|
//# sourceMappingURL=s3.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"s3.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/s3.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"s3.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/s3.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;GAUG;AACH,wBAAgB,mCAAmC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAKjG"}
|
package/build/cli/cli/util/s3.js
CHANGED
|
@@ -2,42 +2,20 @@
|
|
|
2
2
|
* Copyright Fastly, Inc.
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
4
|
*/
|
|
5
|
-
import { S3Client, HeadObjectCommand } from '@aws-sdk/client-s3';
|
|
6
|
-
const DUMMY_KEY = 'dummy.dat'; // placeholder, never sent over the network
|
|
7
5
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* The AWS S3 host name of a bucket, for the backend of the app:
|
|
7
|
+
* <bucket>.s3.<region>.amazonaws.com. The server uses the same host name for a
|
|
8
|
+
* bucket without an endpoint (see objectUrl() in
|
|
9
|
+
* src/server/storage/s3-storage-provider.ts). It needs no credentials and no
|
|
10
|
+
* network.
|
|
11
|
+
*
|
|
12
|
+
* Returns null for a bucket name with a dot: under HTTPS, such a name does not
|
|
13
|
+
* match the certificate of *.s3.<region>.amazonaws.com. Such a bucket needs
|
|
14
|
+
* an endpoint, so that the bucket is in the path of the URL.
|
|
11
15
|
*/
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const captureHostname = (_next) => async (args) => {
|
|
16
|
-
if (typeof args.request === 'object' &&
|
|
17
|
-
args.request != null &&
|
|
18
|
-
'hostname' in args.request &&
|
|
19
|
-
typeof args.request['hostname'] === 'string') {
|
|
20
|
-
hostname = args.request['hostname'];
|
|
21
|
-
}
|
|
22
|
-
const err = new Error('SHORT_CIRCUIT');
|
|
23
|
-
err.name = 'SHORT_CIRCUIT';
|
|
24
|
-
throw err; // short-circuit before auth/send
|
|
25
|
-
};
|
|
26
|
-
client.middlewareStack.add(captureHostname, {
|
|
27
|
-
name: 'captureS3Hostname',
|
|
28
|
-
step: 'build',
|
|
29
|
-
priority: 'high', // run early in build
|
|
30
|
-
});
|
|
31
|
-
try {
|
|
32
|
-
await client.send(new HeadObjectCommand({ Bucket: bucket, Key: DUMMY_KEY }));
|
|
16
|
+
export function findHostnameForAwsS3RegionAndBucket(region, bucket) {
|
|
17
|
+
if (bucket.includes('.')) {
|
|
18
|
+
return null;
|
|
33
19
|
}
|
|
34
|
-
|
|
35
|
-
if (typeof e !== 'object' ||
|
|
36
|
-
e == null ||
|
|
37
|
-
!('name' in e) ||
|
|
38
|
-
e.name !== 'SHORT_CIRCUIT') {
|
|
39
|
-
throw e;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return hostname;
|
|
20
|
+
return `${bucket}.s3.${region}.amazonaws.com`;
|
|
43
21
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accept-html.d.ts","sourceRoot":"","sources":["../../../../src/server/publisher-server/accept-html.ts"],"names":[],"mappings":"AAYA,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAyChE"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fastly, Inc.
|
|
3
|
+
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
|
+
*/
|
|
5
|
+
// Returns true if the request accepts a text/html response.
|
|
6
|
+
// https://httpwg.org/specs/rfc9110.html#field.accept
|
|
7
|
+
// * A missing or empty Accept header accepts all media types.
|
|
8
|
+
// * The most specific media range that matches text/html gives the q-value:
|
|
9
|
+
// text/html, then text/*, then */*. A q-value of 0 means "not acceptable".
|
|
10
|
+
// * The media type parameters other than q are ignored.
|
|
11
|
+
// * A bare '*' is the same as '*/*'.
|
|
12
|
+
export function requestAcceptsTextHtml(request) {
|
|
13
|
+
const header = (request.headers.get('Accept') ?? '').trim();
|
|
14
|
+
if (header === '') {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
// The highest q-value for each media range that matches text/html
|
|
18
|
+
let htmlQValue = null;
|
|
19
|
+
let textQValue = null;
|
|
20
|
+
let anyQValue = null;
|
|
21
|
+
for (const item of header.split(',')) {
|
|
22
|
+
const [rangeStr, ...paramStrs] = item.split(';');
|
|
23
|
+
const range = rangeStr.trim().toLowerCase();
|
|
24
|
+
if (range === '') {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
let qValue = 1;
|
|
28
|
+
for (const paramStr of paramStrs) {
|
|
29
|
+
const [name, value] = paramStr.split('=');
|
|
30
|
+
if (name.trim().toLowerCase() !== 'q' || value == null) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const parsed = parseFloat(value.trim());
|
|
34
|
+
if (!Number.isNaN(parsed)) {
|
|
35
|
+
qValue = Math.min(Math.max(parsed, 0), 1);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (range === 'text/html') {
|
|
39
|
+
htmlQValue = Math.max(htmlQValue ?? 0, qValue);
|
|
40
|
+
}
|
|
41
|
+
else if (range === 'text/*') {
|
|
42
|
+
textQValue = Math.max(textQValue ?? 0, qValue);
|
|
43
|
+
}
|
|
44
|
+
else if (range === '*/*' || range === '*') {
|
|
45
|
+
anyQValue = Math.max(anyQValue ?? 0, qValue);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const qValue = htmlQValue ?? textQValue ?? anyQValue;
|
|
49
|
+
return qValue != null && qValue > 0;
|
|
50
|
+
}
|
|
@@ -1 +1 @@
|
|
|
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,EAErB,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;
|
|
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,EAErB,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD,OAAO,EAEL,KAAK,oBAAoB,EAW1B,MAAM,qBAAqB,CAAC;AAG7B,KAAK,YAAY,GAAG;IAClB,YAAY,EAAE,YAAY,CAAC;CAC5B,GAAG,oBAAoB,CAAC;AAEzB,wBAAgB,kBAAkB,CA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,EAAE,SAAS,GAAE,OAAe,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IA2D1F,yBAAyB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,uBAAuB,EAAE,EAAE,CAAC;IASjF,iBAAiB,CAAC,QAAQ,EAAE,MAAM;IAcxC,mBAAmB,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,OAAO,GAAG,QAAQ,GAAG,IAAI;IAMnG,OAAO,CAAC,kCAAkC;IA6D7B,gBAAgB,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,uBAAuB,GAAG,IAAI,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;YA8BzG,wCAAwC;IAyChD,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,QAAQ,CAAC;IAmEpF,WAAW,CACf,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,GACtD,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IA4G3B,OAAO,CAAC,iBAAiB;IA4BnB,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;YAsBhD,uBAAuB;CA6EtC"}
|
|
@@ -11,7 +11,8 @@ import { loadStorageProviderFromStaticPublishRc, } from '../storage/storage-prov
|
|
|
11
11
|
import { checkIfModifiedSince, getIfModifiedSinceHeader } from './serve-preconditions/if-modified-since.js';
|
|
12
12
|
import { checkIfNoneMatch, getIfNoneMatchHeader } from './serve-preconditions/if-none-match.js';
|
|
13
13
|
import { ServerTiming } from '../util/server-timing.js';
|
|
14
|
-
import {
|
|
14
|
+
import { requestAcceptsTextHtml } from './accept-html.js';
|
|
15
|
+
import { CACHEABLE_STATUSES, DEFAULT_MAX_BODY_SIZE, buildCacheFillRequest, buildResponseCacheKey, bytesInPieces, decodeCachedResponseMetadata, encodeCachedResponseMetadata, parseAcceptEncodingGroups, readBodyUpTo, streamBytesInPieces, } from './response-cache.js';
|
|
15
16
|
import { checkBodyLength } from '../util/body-length.js';
|
|
16
17
|
export function buildHeadersSubset(responseHeaders, keys) {
|
|
17
18
|
const resultHeaders = new Headers();
|
|
@@ -31,15 +32,6 @@ export function buildHeadersSubset(responseHeaders, keys) {
|
|
|
31
32
|
// * Content-Location, Date, ETag, and Vary
|
|
32
33
|
// * Cache-Control and Expires
|
|
33
34
|
const headersToPreserveForUnmodified = ['Content-Location', 'ETag', 'Vary', 'Cache-Control', 'Expires'];
|
|
34
|
-
function requestAcceptsTextHtml(req) {
|
|
35
|
-
const accept = (req.headers.get('Accept') ?? '')
|
|
36
|
-
.split(',')
|
|
37
|
-
.map(x => x.split(';')[0]);
|
|
38
|
-
if (!accept.includes('text/html') && !accept.includes('*/*') && accept.includes('*')) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
35
|
export class PublisherServer {
|
|
44
36
|
constructor(publishId, storageProvider, defaultCollectionName) {
|
|
45
37
|
// When true, serveCached() does not store the response of the current request.
|
|
@@ -510,9 +502,13 @@ export class PublisherServer {
|
|
|
510
502
|
userMetadata: encodeCachedResponseMetadata(metadata),
|
|
511
503
|
length: readResult.bytes.byteLength,
|
|
512
504
|
});
|
|
513
|
-
|
|
505
|
+
// The body goes to the entry and to the response in pieces. A call with the
|
|
506
|
+
// whole body makes a full copy of it, which needs memory for one more body.
|
|
507
|
+
for (const piece of bytesInPieces(readResult.bytes)) {
|
|
508
|
+
writer.append(piece);
|
|
509
|
+
}
|
|
514
510
|
writer.close();
|
|
515
|
-
return this.responseFromCache(request, metadata, readResult.bytes);
|
|
511
|
+
return this.responseFromCache(request, metadata, streamBytesInPieces(readResult.bytes));
|
|
516
512
|
}
|
|
517
513
|
responseFromCache(request, metadata, body) {
|
|
518
514
|
if (metadata.notFound) {
|
|
@@ -27,6 +27,9 @@ export type ReadBodyResult = {
|
|
|
27
27
|
body: ReadableStream<Uint8Array>;
|
|
28
28
|
};
|
|
29
29
|
export declare function readBodyUpTo(body: ReadableStream<Uint8Array> | null, maxLength: number): Promise<ReadBodyResult>;
|
|
30
|
+
export declare const BODY_PIECE_SIZE: number;
|
|
31
|
+
export declare function bytesInPieces(bytes: Uint8Array<ArrayBuffer>): Generator<Uint8Array<ArrayBuffer>>;
|
|
32
|
+
export declare function streamBytesInPieces(bytes: Uint8Array<ArrayBuffer>): ReadableStream<Uint8Array>;
|
|
30
33
|
export declare function encodeCachedResponseMetadata(metadata: CachedResponseMetadata): string;
|
|
31
34
|
export declare function decodeCachedResponseMetadata(buffer: ArrayBuffer): CachedResponseMetadata | null;
|
|
32
35
|
//# 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,oBAAoB,GAAG;IAIjC,MAAM,EAAE,MAAM,CAAC;IAIf,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,eAAO,MAAM,qBAAqB,
|
|
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,oBAAoB,GAAG;IAIjC,MAAM,EAAE,MAAM,CAAC;IAIf,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,eAAO,MAAM,qBAAqB,QAAmB,CAAC;AAKtD,MAAM,MAAM,sBAAsB,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,CAAC,CAAC,CAIjG;AAGD,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,CAAC,WAAW,CAAC,GAAG,cAAc,CAAC,UAAU,CAAC,CAY9F;AAED,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,sBAAsB,GAAG,MAAM,CAErF;AAED,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,WAAW,GAAG,sBAAsB,GAAG,IAAI,CAgB/F"}
|
|
@@ -3,7 +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 =
|
|
6
|
+
export const DEFAULT_MAX_BODY_SIZE = 16 * 1024 * 1024;
|
|
7
7
|
// Statuses that serveAsset() produces for a found file or a not-found page.
|
|
8
8
|
// Anything else (for example, a 500 when the settings are missing) is not cached.
|
|
9
9
|
export const CACHEABLE_STATUSES = [200, 404];
|
|
@@ -137,6 +137,29 @@ export async function readBodyUpTo(body, maxLength) {
|
|
|
137
137
|
}
|
|
138
138
|
return { bytes };
|
|
139
139
|
}
|
|
140
|
+
export const BODY_PIECE_SIZE = 1024 * 1024;
|
|
141
|
+
// Gives bytes in pieces of BODY_PIECE_SIZE bytes. The pieces are views of
|
|
142
|
+
// bytes, not copies.
|
|
143
|
+
export function* bytesInPieces(bytes) {
|
|
144
|
+
for (let offset = 0; offset < bytes.byteLength; offset += BODY_PIECE_SIZE) {
|
|
145
|
+
yield bytes.subarray(offset, offset + BODY_PIECE_SIZE);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// A stream of bytes in pieces of BODY_PIECE_SIZE bytes, as bytesInPieces() gives them.
|
|
149
|
+
export function streamBytesInPieces(bytes) {
|
|
150
|
+
const pieces = bytesInPieces(bytes);
|
|
151
|
+
return new ReadableStream({
|
|
152
|
+
pull(controller) {
|
|
153
|
+
const { done, value } = pieces.next();
|
|
154
|
+
if (done) {
|
|
155
|
+
controller.close();
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
controller.enqueue(value);
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
}
|
|
140
163
|
export function encodeCachedResponseMetadata(metadata) {
|
|
141
164
|
return JSON.stringify(metadata);
|
|
142
165
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastly/compute-js-static-publish",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "8.0.
|
|
4
|
+
"version": "8.0.2",
|
|
5
5
|
"description": "Static Publisher for Fastly Compute JavaScript",
|
|
6
6
|
"main": "build/server/server/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"@types/command-line-args": "^5.2.0",
|
|
57
57
|
"@types/glob-to-regexp": "^0.4.1",
|
|
58
58
|
"@types/node": "^20.11.0",
|
|
59
|
-
"rimraf": "^
|
|
59
|
+
"rimraf": "^6.1.4",
|
|
60
60
|
"typescript": "^5.8.0"
|
|
61
61
|
},
|
|
62
62
|
"engines": {
|
|
@@ -69,6 +69,8 @@
|
|
|
69
69
|
"package.json",
|
|
70
70
|
"README.md",
|
|
71
71
|
"DOCUMENTATION.md",
|
|
72
|
+
"CHANGELOG.md",
|
|
73
|
+
"MIGRATING.md",
|
|
72
74
|
"SECURITY.md"
|
|
73
75
|
]
|
|
74
76
|
}
|