@fastly/compute-js-static-publish 8.0.0-rc.2 → 8.0.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/DOCUMENTATION.md +1227 -0
- package/README.md +9 -2
- package/README.short.md +9 -2
- 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 +72 -76
- package/build/cli/cli/commands/scaffold/index.d.ts.map +1 -1
- package/build/cli/cli/commands/scaffold/index.js +39 -18
- package/build/cli/cli/storage/kv-store-provider.d.ts +1 -1
- package/build/cli/cli/storage/kv-store-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/kv-store-provider.js +8 -5
- 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 +12 -6
- package/build/cli/cli/storage/storage-provider.d.ts +1 -0
- package/build/cli/cli/storage/storage-provider.d.ts.map +1 -1
- package/build/cli/cli/util/api-token.d.ts +2 -1
- package/build/cli/cli/util/api-token.d.ts.map +1 -1
- package/build/cli/cli/util/api-token.js +38 -3
- package/build/cli/cli/util/progress.d.ts +11 -0
- package/build/cli/cli/util/progress.d.ts.map +1 -0
- package/build/cli/cli/util/progress.js +32 -0
- package/build/cli/cli/util/purge.d.ts +10 -1
- package/build/cli/cli/util/purge.d.ts.map +1 -1
- package/build/cli/cli/util/purge.js +31 -8
- 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 +1 -1
- package/build/server/server/publisher-server/index.d.ts.map +1 -1
- package/build/server/server/publisher-server/index.js +85 -53
- 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 +5 -4
|
@@ -2,15 +2,17 @@
|
|
|
2
2
|
* Copyright Fastly, Inc.
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
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) {
|
|
@@ -40,6 +42,8 @@ function requestAcceptsTextHtml(req) {
|
|
|
40
42
|
}
|
|
41
43
|
export class PublisherServer {
|
|
42
44
|
constructor(publishId, storageProvider, defaultCollectionName) {
|
|
45
|
+
// When true, serveCached() does not store the response of the current request.
|
|
46
|
+
this.doNotStoreResponse = false;
|
|
43
47
|
this.publishId = publishId;
|
|
44
48
|
this.storageProvider = storageProvider;
|
|
45
49
|
this.defaultCollectionName = defaultCollectionName;
|
|
@@ -85,6 +89,7 @@ export class PublisherServer {
|
|
|
85
89
|
beginRequest(request) {
|
|
86
90
|
this.settingsCached = undefined;
|
|
87
91
|
this.assetEntryMapCache = undefined;
|
|
92
|
+
this.doNotStoreResponse = false;
|
|
88
93
|
const header = this.serverTimingRequestHeader;
|
|
89
94
|
this.serverTiming = header != null && request.headers.has(header) ? new ServerTiming() : null;
|
|
90
95
|
}
|
|
@@ -140,21 +145,28 @@ export class PublisherServer {
|
|
|
140
145
|
this.assetEntryMapCache = null;
|
|
141
146
|
return null;
|
|
142
147
|
}
|
|
143
|
-
let
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
metadata = JSON.parse(metadataText);
|
|
150
|
-
}
|
|
151
|
-
catch {
|
|
152
|
-
}
|
|
153
|
-
metadata = decodeIndexMetadata(metadata);
|
|
148
|
+
let metadata;
|
|
149
|
+
const metadataText = indexFile.metadataText();
|
|
150
|
+
if (metadataText != null) {
|
|
151
|
+
try {
|
|
152
|
+
metadata = JSON.parse(metadataText);
|
|
154
153
|
}
|
|
155
|
-
|
|
156
|
-
collectionIsExpired = metadata.expirationTime != null && isExpired(metadata.expirationTime);
|
|
154
|
+
catch {
|
|
157
155
|
}
|
|
156
|
+
metadata = decodeIndexMetadata(metadata);
|
|
157
|
+
}
|
|
158
|
+
// Do not serve a collection that needs a feature that this version does not
|
|
159
|
+
// support. This check applies to all collections, also the default collection.
|
|
160
|
+
const unsupportedFeatures = metadata != null ? getUnsupportedIndexFeatures(metadata) : [];
|
|
161
|
+
if (unsupportedFeatures.length > 0) {
|
|
162
|
+
console.error(`Collection at ${indexFileKey} requires features that this version of the server does not support: ${unsupportedFeatures.join(', ')}.`);
|
|
163
|
+
console.error(`Build and deploy the application with a newer version of @fastly/compute-js-static-publish.`);
|
|
164
|
+
this.assetEntryMapCache = null;
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
let collectionIsExpired = false;
|
|
168
|
+
if (this.activeCollectionName !== this.defaultCollectionName && metadata != null) {
|
|
169
|
+
collectionIsExpired = metadata.expirationTime != null && isExpired(metadata.expirationTime);
|
|
158
170
|
}
|
|
159
171
|
if (collectionIsExpired) {
|
|
160
172
|
console.error(`Requested collection expired at ${indexFileKey}.`);
|
|
@@ -392,8 +404,11 @@ export class PublisherServer {
|
|
|
392
404
|
if (preconditionResponse != null) {
|
|
393
405
|
return preconditionResponse;
|
|
394
406
|
}
|
|
395
|
-
|
|
396
|
-
|
|
407
|
+
// If the body does not have the size in its metadata, the response must not
|
|
408
|
+
// look complete. An empty body throws here, and any other mismatch causes an
|
|
409
|
+
// error in the body stream, so that the client gets an incomplete response.
|
|
410
|
+
const body = await checkBodyLength(assetVariant.storageEntry.body, assetVariant.size, `File ${asset.key} (${assetVariant.contentEncoding ?? 'identity'})`);
|
|
411
|
+
return new Response(body, {
|
|
397
412
|
status: init?.status ?? 200,
|
|
398
413
|
headers,
|
|
399
414
|
});
|
|
@@ -415,6 +430,10 @@ export class PublisherServer {
|
|
|
415
430
|
const state = entry.state();
|
|
416
431
|
if (!state.mustInsertOrUpdate()) {
|
|
417
432
|
const metadata = state.found() && state.usable() ? decodeCachedResponseMetadata(entry.userMetadata()) : null;
|
|
433
|
+
if (metadata?.tooLarge) {
|
|
434
|
+
this.serverTiming?.add('cache', performance.now() - lookupStart, 'bypass');
|
|
435
|
+
return produce(request);
|
|
436
|
+
}
|
|
418
437
|
if (metadata != null) {
|
|
419
438
|
// age() is in milliseconds. A hit younger than the time since a purge proves the entry was refilled.
|
|
420
439
|
this.serverTiming?.add('cache', performance.now() - lookupStart, `hit age=${Math.round(entry.age() / 1000)}s`);
|
|
@@ -434,6 +453,10 @@ export class PublisherServer {
|
|
|
434
453
|
entry.cancel();
|
|
435
454
|
throw err;
|
|
436
455
|
}
|
|
456
|
+
if (this.doNotStoreResponse) {
|
|
457
|
+
entry.cancel();
|
|
458
|
+
return response;
|
|
459
|
+
}
|
|
437
460
|
const insertOptions = {
|
|
438
461
|
// The Core Cache takes maxAge in milliseconds. ResponseCacheOptions.maxAge is in seconds.
|
|
439
462
|
maxAge: this.responseCache.maxAge * 1000,
|
|
@@ -457,45 +480,39 @@ export class PublisherServer {
|
|
|
457
480
|
// Server-Timing describes one request, so it is not stored.
|
|
458
481
|
headers.delete('Server-Timing');
|
|
459
482
|
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) {
|
|
483
|
+
// Read the whole body before the insertion starts. After it starts, a body
|
|
484
|
+
// that is not complete is not safe: the requests that read the entry get
|
|
485
|
+
// a short body that ends as if it is complete. If the read fails (for example,
|
|
486
|
+
// serveAsset() finds a body that does not have the size in its metadata),
|
|
487
|
+
// cancel() stores nothing, and the next request fills the entry again.
|
|
488
|
+
let readResult;
|
|
481
489
|
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();
|
|
490
|
+
readResult = await readBodyUpTo(response.body, this.responseCache.maxBodySize ?? DEFAULT_MAX_BODY_SIZE);
|
|
493
491
|
}
|
|
494
492
|
catch (err) {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
493
|
+
entry.cancel();
|
|
494
|
+
throw err;
|
|
495
|
+
}
|
|
496
|
+
if ('body' in readResult) {
|
|
497
|
+
// Too large to cache. Cache a marker, so that later requests do not read
|
|
498
|
+
// the body to find this again, and serve the body from storage.
|
|
499
|
+
const tooLargeMetadata = { tooLarge: true };
|
|
500
|
+
const writer = entry.insert({
|
|
501
|
+
...insertOptions,
|
|
502
|
+
userMetadata: encodeCachedResponseMetadata(tooLargeMetadata),
|
|
503
|
+
length: 0,
|
|
504
|
+
});
|
|
505
|
+
writer.close();
|
|
506
|
+
return this.responseFromCache(request, metadata, readResult.body);
|
|
498
507
|
}
|
|
508
|
+
const writer = entry.insert({
|
|
509
|
+
...insertOptions,
|
|
510
|
+
userMetadata: encodeCachedResponseMetadata(metadata),
|
|
511
|
+
length: readResult.bytes.byteLength,
|
|
512
|
+
});
|
|
513
|
+
writer.append(readResult.bytes);
|
|
514
|
+
writer.close();
|
|
515
|
+
return this.responseFromCache(request, metadata, readResult.bytes);
|
|
499
516
|
}
|
|
500
517
|
responseFromCache(request, metadata, body) {
|
|
501
518
|
if (metadata.notFound) {
|
|
@@ -535,7 +552,7 @@ export class PublisherServer {
|
|
|
535
552
|
}
|
|
536
553
|
async serveRequestFromStorage(request, pathname) {
|
|
537
554
|
const serverConfig = await this.getServerConfig();
|
|
538
|
-
if (serverConfig == null) {
|
|
555
|
+
if (serverConfig == null && this.activeCollectionName === this.defaultCollectionName) {
|
|
539
556
|
return new Response(`Settings not found. You may need to publish your application.`, {
|
|
540
557
|
status: 500,
|
|
541
558
|
headers: {
|
|
@@ -543,6 +560,21 @@ export class PublisherServer {
|
|
|
543
560
|
},
|
|
544
561
|
});
|
|
545
562
|
}
|
|
563
|
+
// A collection other than the default collection that is not published, or
|
|
564
|
+
// that expired, gets a 404. It can be published at any time, so the
|
|
565
|
+
// response cache does not store this response.
|
|
566
|
+
if (serverConfig == null ||
|
|
567
|
+
(this.activeCollectionName !== this.defaultCollectionName && await this.getAssetEntryMap() == null)) {
|
|
568
|
+
this.doNotStoreResponse = true;
|
|
569
|
+
return new Response(`Collection '${this.activeCollectionName}' is not available. It is not published, or it expired.`, {
|
|
570
|
+
status: 404,
|
|
571
|
+
headers: {
|
|
572
|
+
'content-type': 'text/plain',
|
|
573
|
+
'x-content-type-options': 'nosniff',
|
|
574
|
+
'cache-control': 'no-store',
|
|
575
|
+
},
|
|
576
|
+
});
|
|
577
|
+
}
|
|
546
578
|
const asset = await this.getMatchingAsset(serverConfig.publicDirPrefix + pathname, true);
|
|
547
579
|
if (asset != null) {
|
|
548
580
|
return this.serveAsset(request, asset, {
|
|
@@ -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,oBAAoB,GAAG;IAIjC,MAAM,EAAE,MAAM,CAAC;
|
|
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,QAAkB,CAAC;AAKrD,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,eAAO,MAAM,kBAAkB,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAgB,CAAC;AAgBnE,wBAAgB,yBAAyB,CACvC,WAAW,EAAE,MAAM,EACnB,gBAAgB,EAAE,QAAQ,CAAC,uBAAuB,EAAE,CAAC,GACpD,uBAAuB,EAAE,EAAE,CA2C7B;AAOD,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAQnE;AAED,wBAAgB,qBAAqB,CACnC,SAAS,EAAE,MAAM,EACjB,cAAc,EAAE,MAAM,EACtB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,UAGjB;AAID,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAM/D;AAED,MAAM,MAAM,cAAc,GACtB;IAAE,KAAK,EAAE,UAAU,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.
|
|
4
|
+
"version": "8.0.1",
|
|
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",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"compile:server": "tsc -p tsconfig.server.json",
|
|
20
20
|
"test": "npm run build && node --test \"test/unit/*.test.js\"",
|
|
21
21
|
"test:e2e": "npm run build && node --test --test-concurrency=1 \"test/e2e/*.test.js\"",
|
|
22
|
-
"prepublishOnly": "cp README.short.md README.md"
|
|
22
|
+
"prepublishOnly": "cp README.md DOCUMENTATION.md && cp README.short.md README.md"
|
|
23
23
|
},
|
|
24
24
|
"bin": {
|
|
25
25
|
"compute-js-static-publish": "build/cli/cli/index.js"
|
|
@@ -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"
|
|
@@ -68,6 +68,7 @@
|
|
|
68
68
|
"LICENSE",
|
|
69
69
|
"package.json",
|
|
70
70
|
"README.md",
|
|
71
|
+
"DOCUMENTATION.md",
|
|
71
72
|
"SECURITY.md"
|
|
72
73
|
]
|
|
73
74
|
}
|