@fastly/compute-js-static-publish 8.0.1 → 8.0.3-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +557 -0
- package/DOCUMENTATION.md +59 -38
- package/MIGRATING.md +128 -0
- package/README.md +3 -3
- package/README.short.md +3 -3
- package/build/cli/cli/commands/manage/clean.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/clean.js +27 -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 +43 -14
- package/build/cli/cli/commands/scaffold/index.js +12 -13
- package/build/cli/cli/storage/kv-store-local-provider.d.ts +3 -2
- package/build/cli/cli/storage/kv-store-local-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/kv-store-local-provider.js +6 -15
- package/build/cli/cli/storage/kv-store-provider.d.ts +3 -2
- package/build/cli/cli/storage/kv-store-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/kv-store-provider.js +11 -2
- package/build/cli/cli/storage/s3-storage-provider.d.ts +3 -2
- package/build/cli/cli/storage/s3-storage-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/s3-storage-provider.js +6 -1
- package/build/cli/cli/storage/storage-provider.d.ts +6 -1
- package/build/cli/cli/storage/storage-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/storage-provider.js +48 -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/kv-store.d.ts +1 -1
- package/build/cli/cli/util/kv-store.d.ts.map +1 -1
- 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/cli/models/index-parts/index.d.ts +20 -0
- package/build/cli/models/index-parts/index.d.ts.map +1 -0
- package/build/cli/models/index-parts/index.js +76 -0
- package/build/cli/models/server/index.d.ts +2 -0
- package/build/cli/models/server/index.d.ts.map +1 -1
- package/build/cli/models/server/index.js +7 -1
- package/build/server/models/index-parts/index.d.ts +20 -0
- package/build/server/models/index-parts/index.d.ts.map +1 -0
- package/build/server/models/index-parts/index.js +76 -0
- package/build/server/models/server/index.d.ts +2 -0
- package/build/server/models/server/index.d.ts.map +1 -1
- package/build/server/models/server/index.js +7 -1
- package/build/server/server/index.d.ts +1 -1
- package/build/server/server/index.d.ts.map +1 -1
- 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 +5 -0
- package/build/server/server/publisher-server/index.d.ts.map +1 -1
- package/build/server/server/publisher-server/index.js +103 -33
- 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/build/server/server/storage/kv-store-provider.d.ts.map +1 -1
- package/build/server/server/storage/kv-store-provider.js +1 -0
- package/build/server/server/storage/s3-storage-provider.d.ts +2 -2
- package/build/server/server/storage/s3-storage-provider.d.ts.map +1 -1
- package/build/server/server/storage/s3-storage-provider.js +5 -2
- package/build/server/server/storage/storage-provider.d.ts +12 -1
- package/build/server/server/storage/storage-provider.d.ts.map +1 -1
- package/package.json +4 -2
|
@@ -5,13 +5,15 @@
|
|
|
5
5
|
/// <reference types="@fastly/js-compute" />
|
|
6
6
|
import { CoreCache } from 'fastly:cache';
|
|
7
7
|
import { decodeAssetVariantMetadata, } from '../../models/assets/index.js';
|
|
8
|
-
import { decodeIndexMetadata, getUnsupportedIndexFeatures } from '../../models/server/index.js';
|
|
8
|
+
import { decodeIndexMetadata, getUnsupportedIndexFeatures, } from '../../models/server/index.js';
|
|
9
|
+
import { getIndexPartKey, getIndexPartNumber, } from '../../models/index-parts/index.js';
|
|
9
10
|
import { isExpired } from '../../models/time/index.js';
|
|
10
11
|
import { loadStorageProviderFromStaticPublishRc, } from '../storage/storage-provider.js';
|
|
11
12
|
import { checkIfModifiedSince, getIfModifiedSinceHeader } from './serve-preconditions/if-modified-since.js';
|
|
12
13
|
import { checkIfNoneMatch, getIfNoneMatchHeader } from './serve-preconditions/if-none-match.js';
|
|
13
14
|
import { ServerTiming } from '../util/server-timing.js';
|
|
14
|
-
import {
|
|
15
|
+
import { requestAcceptsTextHtml } from './accept-html.js';
|
|
16
|
+
import { CACHEABLE_STATUSES, DEFAULT_MAX_BODY_SIZE, buildCacheFillRequest, buildResponseCacheKey, bytesInPieces, decodeCachedResponseMetadata, encodeCachedResponseMetadata, parseAcceptEncodingGroups, readBodyUpTo, streamBytesInPieces, } from './response-cache.js';
|
|
15
17
|
import { checkBodyLength } from '../util/body-length.js';
|
|
16
18
|
export function buildHeadersSubset(responseHeaders, keys) {
|
|
17
19
|
const resultHeaders = new Headers();
|
|
@@ -31,17 +33,11 @@ export function buildHeadersSubset(responseHeaders, keys) {
|
|
|
31
33
|
// * Content-Location, Date, ETag, and Vary
|
|
32
34
|
// * Cache-Control and Expires
|
|
33
35
|
const headersToPreserveForUnmodified = ['Content-Location', 'ETag', 'Vary', 'Cache-Control', 'Expires'];
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
}
|
|
36
|
+
// The time, in seconds, that a storage provider with a cache keeps a file (one year).
|
|
37
|
+
const FILE_CACHE_TTL = 31536000;
|
|
43
38
|
export class PublisherServer {
|
|
44
39
|
constructor(publishId, storageProvider, defaultCollectionName) {
|
|
40
|
+
this.indexPartsCache = new Map();
|
|
45
41
|
// When true, serveCached() does not store the response of the current request.
|
|
46
42
|
this.doNotStoreResponse = false;
|
|
47
43
|
this.publishId = publishId;
|
|
@@ -64,6 +60,8 @@ export class PublisherServer {
|
|
|
64
60
|
this.activeCollectionName = collectionName ?? this.defaultCollectionName;
|
|
65
61
|
this.settingsCached = undefined;
|
|
66
62
|
this.assetEntryMapCache = undefined;
|
|
63
|
+
this.indexCache = undefined;
|
|
64
|
+
this.indexPartsCache.clear();
|
|
67
65
|
}
|
|
68
66
|
setCollectionNameHeader(collectionHeader) {
|
|
69
67
|
this.collectionNameHeader = collectionHeader;
|
|
@@ -89,6 +87,8 @@ export class PublisherServer {
|
|
|
89
87
|
beginRequest(request) {
|
|
90
88
|
this.settingsCached = undefined;
|
|
91
89
|
this.assetEntryMapCache = undefined;
|
|
90
|
+
this.indexCache = undefined;
|
|
91
|
+
this.indexPartsCache.clear();
|
|
92
92
|
this.doNotStoreResponse = false;
|
|
93
93
|
const header = this.serverTimingRequestHeader;
|
|
94
94
|
this.serverTiming = header != null && request.headers.has(header) ? new ServerTiming() : null;
|
|
@@ -133,27 +133,31 @@ export class PublisherServer {
|
|
|
133
133
|
})
|
|
134
134
|
.filter(x => Boolean(x));
|
|
135
135
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
136
|
+
// Reads the index entry of the active collection, but not its body. Returns
|
|
137
|
+
// null if there is no usable index: the index is not found, it requires a
|
|
138
|
+
// feature that this version does not support, or the collection expired.
|
|
139
|
+
async getIndex() {
|
|
140
|
+
if (this.indexCache !== undefined) {
|
|
141
|
+
return this.indexCache;
|
|
139
142
|
}
|
|
140
143
|
const indexFileKey = `${this.publishId}_index_${this.activeCollectionName}`;
|
|
141
144
|
const indexFile = await this.timed('index', () => this.storageProvider.getEntry(indexFileKey, [`${this.publishId}-${this.activeCollectionName}`, 'index']));
|
|
142
145
|
if (indexFile == null) {
|
|
143
146
|
console.error(`Index File not found at ${indexFileKey}.`);
|
|
144
147
|
console.error(`You may need to publish your application.`);
|
|
145
|
-
this.
|
|
148
|
+
this.indexCache = null;
|
|
146
149
|
return null;
|
|
147
150
|
}
|
|
148
|
-
let metadata;
|
|
151
|
+
let metadata = null;
|
|
149
152
|
const metadataText = indexFile.metadataText();
|
|
150
153
|
if (metadataText != null) {
|
|
154
|
+
let metadataObj;
|
|
151
155
|
try {
|
|
152
|
-
|
|
156
|
+
metadataObj = JSON.parse(metadataText);
|
|
153
157
|
}
|
|
154
158
|
catch {
|
|
155
159
|
}
|
|
156
|
-
metadata = decodeIndexMetadata(
|
|
160
|
+
metadata = decodeIndexMetadata(metadataObj);
|
|
157
161
|
}
|
|
158
162
|
// Do not serve a collection that needs a feature that this version does not
|
|
159
163
|
// support. This check applies to all collections, also the default collection.
|
|
@@ -161,7 +165,7 @@ export class PublisherServer {
|
|
|
161
165
|
if (unsupportedFeatures.length > 0) {
|
|
162
166
|
console.error(`Collection at ${indexFileKey} requires features that this version of the server does not support: ${unsupportedFeatures.join(', ')}.`);
|
|
163
167
|
console.error(`Build and deploy the application with a newer version of @fastly/compute-js-static-publish.`);
|
|
164
|
-
this.
|
|
168
|
+
this.indexCache = null;
|
|
165
169
|
return null;
|
|
166
170
|
}
|
|
167
171
|
let collectionIsExpired = false;
|
|
@@ -170,26 +174,86 @@ export class PublisherServer {
|
|
|
170
174
|
}
|
|
171
175
|
if (collectionIsExpired) {
|
|
172
176
|
console.error(`Requested collection expired at ${indexFileKey}.`);
|
|
177
|
+
this.indexCache = null;
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
this.indexCache = { key: indexFileKey, entry: indexFile, metadata };
|
|
181
|
+
return this.indexCache;
|
|
182
|
+
}
|
|
183
|
+
// Returns the whole index of the active collection. This reads and parses
|
|
184
|
+
// the full index. To find one asset, getMatchingAsset() is faster, because
|
|
185
|
+
// it reads only one index part if the index has parts.
|
|
186
|
+
async getAssetEntryMap() {
|
|
187
|
+
if (this.assetEntryMapCache !== undefined) {
|
|
188
|
+
return this.assetEntryMapCache;
|
|
189
|
+
}
|
|
190
|
+
const index = await this.getIndex();
|
|
191
|
+
if (index == null) {
|
|
173
192
|
this.assetEntryMapCache = null;
|
|
174
193
|
return null;
|
|
175
194
|
}
|
|
176
195
|
// Read and parse in two steps, so that Server-Timing can report each one.
|
|
177
|
-
const indexText = await this.timed('index-body', () =>
|
|
196
|
+
const indexText = await this.timed('index-body', () => index.entry.text());
|
|
178
197
|
this.assetEntryMapCache = await this.timed('index-parse', async () => JSON.parse(indexText));
|
|
179
198
|
return this.assetEntryMapCache;
|
|
180
199
|
}
|
|
200
|
+
// Reads one index part. Returns null if the part cannot be read, so that the
|
|
201
|
+
// caller uses the full index.
|
|
202
|
+
async getIndexPart(indexParts, partNumber) {
|
|
203
|
+
const cached = this.indexPartsCache.get(partNumber);
|
|
204
|
+
if (cached !== undefined) {
|
|
205
|
+
return cached;
|
|
206
|
+
}
|
|
207
|
+
const partKey = getIndexPartKey(this.publishId, indexParts, partNumber);
|
|
208
|
+
let part = null;
|
|
209
|
+
try {
|
|
210
|
+
part = await this.timed('index-part', async () => {
|
|
211
|
+
// The key of a part has the hash of the full index, so a cached copy is
|
|
212
|
+
// never out of date. It has the same cache TTL and tag as a file.
|
|
213
|
+
const entry = await this.storageProvider.getEntry(partKey, [`${this.publishId}-files`], { ttl: FILE_CACHE_TTL });
|
|
214
|
+
return entry != null ? (await entry.json()) : null;
|
|
215
|
+
});
|
|
216
|
+
if (part == null) {
|
|
217
|
+
console.warn(`Index part not found at ${partKey}. Using the full index.`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
catch (err) {
|
|
221
|
+
console.warn(`Could not read the index part at ${partKey}. Using the full index.`, err);
|
|
222
|
+
}
|
|
223
|
+
this.indexPartsCache.set(partNumber, part);
|
|
224
|
+
return part;
|
|
225
|
+
}
|
|
226
|
+
// Finds the entry of one asset key in the index of the active collection.
|
|
227
|
+
// If the index has parts, this reads only the part that has the key.
|
|
228
|
+
async findAssetEntry(assetKey) {
|
|
229
|
+
if (this.assetEntryMapCache != null) {
|
|
230
|
+
return this.assetEntryMapCache[assetKey] ?? null;
|
|
231
|
+
}
|
|
232
|
+
const index = await this.getIndex();
|
|
233
|
+
if (index == null) {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
const indexParts = index.metadata?.indexParts;
|
|
237
|
+
if (indexParts != null) {
|
|
238
|
+
const part = await this.getIndexPart(indexParts, getIndexPartNumber(assetKey, indexParts.count));
|
|
239
|
+
if (part != null) {
|
|
240
|
+
return part[assetKey] ?? null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const assetEntryMap = await this.getAssetEntryMap();
|
|
244
|
+
return assetEntryMap?.[assetKey] ?? null;
|
|
245
|
+
}
|
|
181
246
|
async getMatchingAsset(assetKey, applyAuto = false) {
|
|
182
247
|
const serverConfig = await this.getServerConfig();
|
|
183
248
|
if (serverConfig == null) {
|
|
184
249
|
return null;
|
|
185
250
|
}
|
|
186
|
-
|
|
187
|
-
if (assetEntryMap == null) {
|
|
251
|
+
if (await this.getIndex() == null) {
|
|
188
252
|
return null;
|
|
189
253
|
}
|
|
190
254
|
if (!assetKey.endsWith('/')) {
|
|
191
255
|
// A path that does not end in a slash can match an asset directly
|
|
192
|
-
const asset =
|
|
256
|
+
const asset = await this.findAssetEntry(assetKey);
|
|
193
257
|
if (asset != null) {
|
|
194
258
|
return asset;
|
|
195
259
|
}
|
|
@@ -198,7 +262,7 @@ export class PublisherServer {
|
|
|
198
262
|
// looks for an asset that has the specified suffix (usually extension, such as .html)
|
|
199
263
|
for (const extEntry of serverConfig.autoExt) {
|
|
200
264
|
let assetKeyWithExt = assetKey + extEntry;
|
|
201
|
-
const asset =
|
|
265
|
+
const asset = await this.findAssetEntry(assetKeyWithExt);
|
|
202
266
|
if (asset != null) {
|
|
203
267
|
return asset;
|
|
204
268
|
}
|
|
@@ -218,7 +282,7 @@ export class PublisherServer {
|
|
|
218
282
|
assetNameAsDir = assetNameAsDir + '/';
|
|
219
283
|
for (const indexEntry of serverConfig.autoIndex) {
|
|
220
284
|
let assetKeyIndex = assetNameAsDir + indexEntry;
|
|
221
|
-
const asset =
|
|
285
|
+
const asset = await this.findAssetEntry(assetKeyIndex);
|
|
222
286
|
if (asset != null) {
|
|
223
287
|
return asset;
|
|
224
288
|
}
|
|
@@ -313,7 +377,10 @@ export class PublisherServer {
|
|
|
313
377
|
const baseHash = entry.key.slice(7);
|
|
314
378
|
const baseKey = `${this.publishId}_files_sha256_${baseHash}`;
|
|
315
379
|
const variantKey = variant != null ? `${baseKey}_${variant}` : baseKey;
|
|
316
|
-
|
|
380
|
+
// The key of a file has the hash of its content, so a cached copy is never
|
|
381
|
+
// out of date. A publish does not purge the tag; purge it to remove the
|
|
382
|
+
// cached copies of all files, for example after a bad upload.
|
|
383
|
+
const storageEntry = await this.timed('asset', () => this.storageProvider.getEntry(variantKey, [`${this.publishId}-files`], { ttl: FILE_CACHE_TTL }));
|
|
317
384
|
if (storageEntry == null) {
|
|
318
385
|
return null;
|
|
319
386
|
}
|
|
@@ -510,9 +577,13 @@ export class PublisherServer {
|
|
|
510
577
|
userMetadata: encodeCachedResponseMetadata(metadata),
|
|
511
578
|
length: readResult.bytes.byteLength,
|
|
512
579
|
});
|
|
513
|
-
|
|
580
|
+
// The body goes to the entry and to the response in pieces. A call with the
|
|
581
|
+
// whole body makes a full copy of it, which needs memory for one more body.
|
|
582
|
+
for (const piece of bytesInPieces(readResult.bytes)) {
|
|
583
|
+
writer.append(piece);
|
|
584
|
+
}
|
|
514
585
|
writer.close();
|
|
515
|
-
return this.responseFromCache(request, metadata, readResult.bytes);
|
|
586
|
+
return this.responseFromCache(request, metadata, streamBytesInPieces(readResult.bytes));
|
|
516
587
|
}
|
|
517
588
|
responseFromCache(request, metadata, body) {
|
|
518
589
|
if (metadata.notFound) {
|
|
@@ -564,7 +635,7 @@ export class PublisherServer {
|
|
|
564
635
|
// that expired, gets a 404. It can be published at any time, so the
|
|
565
636
|
// response cache does not store this response.
|
|
566
637
|
if (serverConfig == null ||
|
|
567
|
-
(this.activeCollectionName !== this.defaultCollectionName && await this.
|
|
638
|
+
(this.activeCollectionName !== this.defaultCollectionName && await this.getIndex() == null)) {
|
|
568
639
|
this.doNotStoreResponse = true;
|
|
569
640
|
return new Response(`Collection '${this.activeCollectionName}' is not available. It is not published, or it expired.`, {
|
|
570
641
|
status: 404,
|
|
@@ -583,14 +654,13 @@ export class PublisherServer {
|
|
|
583
654
|
}
|
|
584
655
|
// fallback HTML responses, like SPA and "not found" pages
|
|
585
656
|
if (requestAcceptsTextHtml(request)) {
|
|
586
|
-
|
|
587
|
-
if (assetEntryMap == null) {
|
|
657
|
+
if (await this.getIndex() == null) {
|
|
588
658
|
return null;
|
|
589
659
|
}
|
|
590
660
|
// These are raw asset paths, not relative to public path
|
|
591
661
|
const { spaFile } = serverConfig;
|
|
592
662
|
if (spaFile != null) {
|
|
593
|
-
const asset =
|
|
663
|
+
const asset = await this.findAssetEntry(spaFile);
|
|
594
664
|
if (asset != null) {
|
|
595
665
|
return this.serveAsset(request, asset, {
|
|
596
666
|
cache: 'never',
|
|
@@ -599,7 +669,7 @@ export class PublisherServer {
|
|
|
599
669
|
}
|
|
600
670
|
const { notFoundPageFile } = serverConfig;
|
|
601
671
|
if (notFoundPageFile != null) {
|
|
602
|
-
const asset =
|
|
672
|
+
const asset = await this.findAssetEntry(notFoundPageFile);
|
|
603
673
|
if (asset != null) {
|
|
604
674
|
return this.serveAsset(request, asset, {
|
|
605
675
|
status: 404,
|
|
@@ -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,GAC9B;IAAE,QAAQ,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,EAAE,KAAK,CAAA;CAAE,GACpC;IAAE,QAAQ,CAAC,EAAE,KAAK,CAAC;IAAC,QAAQ,EAAE,IAAI,CAAA;CAAE,GACpC;IAAE,QAAQ,CAAC,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAA;CAAE,CAAC;AAIxF,e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
|
|
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
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kv-store-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/kv-store-provider.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC5B,MAAM,uBAAuB,CAAC;AAK/B,eAAO,MAAM,kBAAkB,EAAE,sBAMhC,CAAC;AAEF,qBAAa,eAAgB,YAAW,eAAe;gBACzC,WAAW,EAAE,MAAM;IAI/B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"kv-store-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/kv-store-provider.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC5B,MAAM,uBAAuB,CAAC;AAK/B,eAAO,MAAM,kBAAkB,EAAE,sBAMhC,CAAC;AAEF,qBAAa,eAAgB,YAAW,eAAe;gBACzC,WAAW,EAAE,MAAM;IAI/B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAGvB,QAAQ,CAAC,GAAG,EAAE,MAAM;CAI3B"}
|
|
@@ -17,6 +17,7 @@ export class KvStoreProvider {
|
|
|
17
17
|
constructor(kvStoreName) {
|
|
18
18
|
this.kvStoreName = kvStoreName;
|
|
19
19
|
}
|
|
20
|
+
// The KV Store has its own cache, so the tags and the TTL are not used.
|
|
20
21
|
async getEntry(key) {
|
|
21
22
|
const kvStore = new KVStore(this.kvStoreName);
|
|
22
23
|
return await getKVStoreEntry(kvStore, key);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type StorageEntry, type StorageProvider, StorageProviderBuilder } from './storage-provider.js';
|
|
1
|
+
import { type GetEntryOptions, type StorageEntry, type StorageProvider, StorageProviderBuilder } from './storage-provider.js';
|
|
2
2
|
export type S3Credentials = {
|
|
3
3
|
accessKeyId: string;
|
|
4
4
|
secretAccessKey: string;
|
|
@@ -27,6 +27,6 @@ export declare class S3StorageProvider implements StorageProvider {
|
|
|
27
27
|
private readonly s3FastlyBackendName?;
|
|
28
28
|
objectUrl(key: string): URL;
|
|
29
29
|
fetchObject(key: string, requestInit: RequestInit): Promise<Response>;
|
|
30
|
-
getEntry(key: string, tags?: string[]): Promise<StorageEntry | null>;
|
|
30
|
+
getEntry(key: string, tags?: string[], options?: GetEntryOptions): Promise<StorageEntry | null>;
|
|
31
31
|
}
|
|
32
32
|
//# sourceMappingURL=s3-storage-provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"s3-storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/s3-storage-provider.ts"],"names":[],"mappings":"AAiBA,OAAO,EAEL,KAAK,YAAY,EAEjB,KAAK,eAAe,EACpB,sBAAsB,EACvB,MAAM,uBAAuB,CAAC;AAE/B,MAAM,MAAM,aAAa,GAAG;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC;AAElF,eAAO,MAAM,kBAAkB,EAAE,sBAahC,CAAC;AAMF;;;;GAIG;AACH,wBAAgB,8BAA8B,CAAC,gBAAgB,EAAE,MAAM,QAEtE;AAED,wBAAgB,iCAAiC,CAAC,cAAc,EAAE,MAAM,QAEvE;AAED,wBAAgB,qCAAqC,CAAC,cAAc,EAAE,MAAM,QAE3E;AAGD,wBAAsB,iCAAiC,2BA2BtD;AAGD,wBAAgB,uBAAuB,CAAC,oBAAoB,EAAE,oBAAoB,QAEjF;AAED,MAAM,MAAM,uBAAuB,GAAG;IACpC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B,CAAC;
|
|
1
|
+
{"version":3,"file":"s3-storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/s3-storage-provider.ts"],"names":[],"mappings":"AAiBA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,YAAY,EAEjB,KAAK,eAAe,EACpB,sBAAsB,EACvB,MAAM,uBAAuB,CAAC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CAkCtG"}
|
|
@@ -68,6 +68,9 @@ let _s3CredentialsBuilder = buildS3CredentialsFromSecretStore;
|
|
|
68
68
|
export function setS3CredentialsBuilder(s3CredentialsBuilder) {
|
|
69
69
|
_s3CredentialsBuilder = s3CredentialsBuilder;
|
|
70
70
|
}
|
|
71
|
+
// The time, in seconds, that the readthrough cache keeps an object when
|
|
72
|
+
// getEntry() does not give a TTL.
|
|
73
|
+
const DEFAULT_CACHE_TTL = 3600;
|
|
71
74
|
// Attempts for a request that fails with a retryable status or a network error.
|
|
72
75
|
const MAX_ATTEMPTS = 5;
|
|
73
76
|
const RETRY_BASE_DELAY_MS = 100;
|
|
@@ -153,11 +156,11 @@ export class S3StorageProvider {
|
|
|
153
156
|
await new Promise(resolve => setTimeout(resolve, delay));
|
|
154
157
|
}
|
|
155
158
|
}
|
|
156
|
-
async getEntry(key, tags) {
|
|
159
|
+
async getEntry(key, tags, options) {
|
|
157
160
|
const response = await this.fetchObject(key, {
|
|
158
161
|
backend: this.s3FastlyBackendName ?? "s3_storage",
|
|
159
162
|
cacheOverride: new CacheOverride({
|
|
160
|
-
ttl:
|
|
163
|
+
ttl: options?.ttl ?? DEFAULT_CACHE_TTL,
|
|
161
164
|
surrogateKey: (tags ?? []).join(' ') || undefined,
|
|
162
165
|
}),
|
|
163
166
|
});
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { type StaticPublishRc } from '../../models/config/static-publish-rc.js';
|
|
2
|
+
export type GetEntryOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* How long, in seconds, a provider that caches what it reads (such as the
|
|
5
|
+
* S3 provider) keeps the entry. A provider that does not cache ignores it.
|
|
6
|
+
*/
|
|
7
|
+
ttl?: number;
|
|
8
|
+
};
|
|
2
9
|
export interface StorageProvider {
|
|
3
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Reads an entry. `tags` are the surrogate keys of the cached copy, if the
|
|
12
|
+
* provider caches what it reads.
|
|
13
|
+
*/
|
|
14
|
+
getEntry(key: string, tags?: string[], options?: GetEntryOptions): Promise<StorageEntry | null>;
|
|
4
15
|
}
|
|
5
16
|
export type StorageProviderBuilder = (config: StaticPublishRc) => (StorageProvider | null);
|
|
6
17
|
export interface StorageEntry {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/storage-provider.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,eAAe,EACrB,MAAM,0CAA0C,CAAC;AAElD,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/storage-provider.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,eAAe,EACrB,MAAM,0CAA0C,CAAC;AAElD,MAAM,MAAM,eAAe,GAAG;IAC5B;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC;CACjG;AAED,MAAM,MAAM,sBAAsB,GAAG,CAAC,MAAM,EAAE,eAAe,KAAK,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC;AAE3F,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,IAAI,IAAI,IAAI,cAAc,CAAC;IAE3B;;OAEG;IACH,IAAI,QAAQ,IAAI,OAAO,CAAC;IAExB;;;OAGG;IACH,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAExB;;OAEG;IACH,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAExB;;OAEG;IACH,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;IAEpC;;OAEG;IACH,QAAQ,IAAI,WAAW,GAAG,IAAI,CAAC;IAE/B;;;OAGG;IACH,YAAY,IAAI,MAAM,GAAG,IAAI,CAAC;CAC/B;AAKD,qBAAa,gBAAiB,SAAQ,QAAS,YAAW,YAAY;gBACxD,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM;IAKhD,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;IAE3C,IAAI,IAAI,4CAEP;IAED,QAAQ,IAAI,WAAW,GAAG,IAAI;IAG9B,YAAY,IAAI,MAAM,GAAG,IAAI;CAG9B;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,cAAc,CAAC,UAAU,CAAC,EAAE,GAAG,cAAc,CAAC,UAAU,CAAC,CAuCvG;AAGD,wBAAgB,8BAA8B,CAAC,OAAO,EAAE,sBAAsB,QAE7E;AAED,wBAAgB,sCAAsC,CAAC,MAAM,EAAE,eAAe,mBAS7E"}
|
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.1",
|
|
4
|
+
"version": "8.0.3-rc.1",
|
|
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
|
}
|