@fastly/compute-js-static-publish 8.0.0 → 8.1.0-beta.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 +1984 -0
- package/README.md +11 -4
- package/README.short.md +11 -4
- package/build/cli/cli/commands/manage/clean.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/clean.js +12 -7
- package/build/cli/cli/commands/manage/collections/delete.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/delete.js +29 -13
- package/build/cli/cli/commands/manage/collections/index.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/index.js +6 -1
- package/build/cli/cli/commands/manage/collections/list.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/list.js +47 -7
- package/build/cli/cli/commands/manage/collections/promote.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/promote.js +67 -20
- package/build/cli/cli/commands/manage/collections/update-expiration.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/update-expiration.js +10 -11
- package/build/cli/cli/commands/manage/deploy.d.ts +2 -0
- package/build/cli/cli/commands/manage/deploy.d.ts.map +1 -0
- package/build/cli/cli/commands/manage/deploy.js +194 -0
- package/build/cli/cli/commands/manage/dev.d.ts +2 -0
- package/build/cli/cli/commands/manage/dev.d.ts.map +1 -0
- package/build/cli/cli/commands/manage/dev.js +77 -0
- package/build/cli/cli/commands/manage/eject.d.ts +2 -0
- package/build/cli/cli/commands/manage/eject.d.ts.map +1 -0
- package/build/cli/cli/commands/manage/eject.js +155 -0
- package/build/cli/cli/commands/manage/index.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/index.js +37 -1
- package/build/cli/cli/commands/manage/publish-content.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/publish-content.js +85 -35
- package/build/cli/cli/commands/manage/status.d.ts +2 -0
- package/build/cli/cli/commands/manage/status.d.ts.map +1 -0
- package/build/cli/cli/commands/manage/status.js +162 -0
- package/build/cli/cli/commands/manage/template/index.d.ts +2 -0
- package/build/cli/cli/commands/manage/template/index.d.ts.map +1 -0
- package/build/cli/cli/commands/manage/template/index.js +42 -0
- package/build/cli/cli/commands/manage/template/init.d.ts +13 -0
- package/build/cli/cli/commands/manage/template/init.d.ts.map +1 -0
- package/build/cli/cli/commands/manage/template/init.js +172 -0
- package/build/cli/cli/commands/manage/upgrade.d.ts +2 -0
- package/build/cli/cli/commands/manage/upgrade.d.ts.map +1 -0
- package/build/cli/cli/commands/manage/upgrade.js +59 -0
- package/build/cli/cli/commands/run-dev-watch.d.ts +8 -0
- package/build/cli/cli/commands/run-dev-watch.d.ts.map +1 -0
- package/build/cli/cli/commands/run-dev-watch.js +41 -0
- package/build/cli/cli/commands/run-template-hook.d.ts +2 -0
- package/build/cli/cli/commands/run-template-hook.d.ts.map +1 -0
- package/build/cli/cli/commands/run-template-hook.js +28 -0
- package/build/cli/cli/commands/scaffold/app-template.d.ts +17 -0
- package/build/cli/cli/commands/scaffold/app-template.d.ts.map +1 -0
- package/build/cli/cli/commands/scaffold/app-template.js +54 -0
- package/build/cli/cli/commands/scaffold/fastly-toml-sections.d.ts +5 -0
- package/build/cli/cli/commands/scaffold/fastly-toml-sections.d.ts.map +1 -0
- package/build/cli/cli/commands/scaffold/fastly-toml-sections.js +154 -0
- package/build/cli/cli/commands/scaffold/generate-app.d.ts +47 -0
- package/build/cli/cli/commands/scaffold/generate-app.d.ts.map +1 -0
- package/build/cli/cli/commands/scaffold/generate-app.js +164 -0
- package/build/cli/cli/commands/scaffold/index.d.ts +6 -0
- package/build/cli/cli/commands/scaffold/index.d.ts.map +1 -1
- package/build/cli/cli/commands/scaffold/index.js +239 -300
- package/build/cli/cli/commands/scaffold/render.d.ts +7 -0
- package/build/cli/cli/commands/scaffold/render.d.ts.map +1 -0
- package/build/cli/cli/commands/scaffold/render.js +72 -0
- package/build/cli/cli/hono/build.d.ts +43 -0
- package/build/cli/cli/hono/build.d.ts.map +1 -0
- package/build/cli/cli/hono/build.js +297 -0
- package/build/cli/cli/index.js +98 -5
- package/build/cli/cli/managed/app.d.ts +21 -0
- package/build/cli/cli/managed/app.d.ts.map +1 -0
- package/build/cli/cli/managed/app.js +162 -0
- package/build/cli/cli/managed/command-helpers.d.ts +6 -0
- package/build/cli/cli/managed/command-helpers.d.ts.map +1 -0
- package/build/cli/cli/managed/command-helpers.js +81 -0
- package/build/cli/cli/managed/eject.d.ts +6 -0
- package/build/cli/cli/managed/eject.d.ts.map +1 -0
- package/build/cli/cli/managed/eject.js +68 -0
- package/build/cli/cli/managed/fastly-service.d.ts +8 -0
- package/build/cli/cli/managed/fastly-service.d.ts.map +1 -0
- package/build/cli/cli/managed/fastly-service.js +21 -0
- package/build/cli/cli/managed/fingerprint.d.ts +13 -0
- package/build/cli/cli/managed/fingerprint.d.ts.map +1 -0
- package/build/cli/cli/managed/fingerprint.js +60 -0
- package/build/cli/cli/managed/lockfile.d.ts +31 -0
- package/build/cli/cli/managed/lockfile.d.ts.map +1 -0
- package/build/cli/cli/managed/lockfile.js +62 -0
- package/build/cli/cli/managed/project-edit.d.ts +3 -0
- package/build/cli/cli/managed/project-edit.d.ts.map +1 -0
- package/build/cli/cli/managed/project-edit.js +35 -0
- package/build/cli/cli/managed/project-file.d.ts +16 -0
- package/build/cli/cli/managed/project-file.d.ts.map +1 -0
- package/build/cli/cli/managed/project-file.js +119 -0
- package/build/cli/cli/managed/template-init.d.ts +12 -0
- package/build/cli/cli/managed/template-init.d.ts.map +1 -0
- package/build/cli/cli/managed/template-init.js +120 -0
- package/build/cli/cli/managed/version-check.d.ts +25 -0
- package/build/cli/cli/managed/version-check.d.ts.map +1 -0
- package/build/cli/cli/managed/version-check.js +84 -0
- 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 +6 -3
- package/build/cli/cli/storage/kv-store-provider.d.ts +2 -1
- package/build/cli/cli/storage/kv-store-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/kv-store-provider.js +17 -6
- 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 -2
- package/build/cli/cli/storage/storage-provider.d.ts +4 -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/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/collection-name.d.ts +3 -0
- package/build/cli/cli/util/collection-name.d.ts.map +1 -0
- package/build/cli/cli/util/collection-name.js +24 -0
- package/build/cli/cli/util/collection-selection-flags.d.ts +13 -0
- package/build/cli/cli/util/collection-selection-flags.d.ts.map +1 -0
- package/build/cli/cli/util/collection-selection-flags.js +131 -0
- package/build/cli/cli/util/config.d.ts +13 -0
- package/build/cli/cli/util/config.d.ts.map +1 -1
- package/build/cli/cli/util/config.js +172 -38
- package/build/cli/cli/util/data.d.ts.map +1 -1
- package/build/cli/cli/util/global-options.d.ts +13 -0
- package/build/cli/cli/util/global-options.d.ts.map +1 -0
- package/build/cli/cli/util/global-options.js +53 -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-local-server.d.ts +1 -1
- package/build/cli/cli/util/kv-store-local-server.d.ts.map +1 -1
- package/build/cli/cli/util/kv-store-local-server.js +4 -2
- package/build/cli/cli/util/output.d.ts +31 -0
- package/build/cli/cli/util/output.d.ts.map +1 -0
- package/build/cli/cli/util/output.js +139 -0
- package/build/cli/cli/util/package.d.ts +3 -0
- package/build/cli/cli/util/package.d.ts.map +1 -1
- package/build/cli/cli/util/package.js +12 -0
- 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 +15 -3
- package/build/cli/cli/util/purge.d.ts.map +1 -1
- package/build/cli/cli/util/purge.js +23 -10
- package/build/cli/cli/util/resources.d.ts +2 -0
- package/build/cli/cli/util/resources.d.ts.map +1 -0
- package/build/cli/cli/util/resources.js +8 -0
- package/build/cli/cli/util/service-id.d.ts +1 -0
- package/build/cli/cli/util/service-id.d.ts.map +1 -1
- package/build/cli/cli/util/service-id.js +5 -1
- package/build/cli/cli/util/variants.d.ts.map +1 -1
- package/build/cli/models/assets/index.d.ts +6 -0
- package/build/cli/models/assets/index.d.ts.map +1 -1
- package/build/cli/models/assets/index.js +24 -0
- package/build/cli/models/collections/index.d.ts +3 -0
- package/build/cli/models/collections/index.d.ts.map +1 -0
- package/build/cli/models/collections/index.js +15 -0
- package/build/cli/models/compression/index.d.ts +1 -1
- package/build/cli/models/compression/index.d.ts.map +1 -1
- package/build/cli/models/config/collection-selection.d.ts +55 -0
- package/build/cli/models/config/collection-selection.d.ts.map +1 -0
- package/build/cli/models/config/collection-selection.js +238 -0
- package/build/cli/models/config/item-patterns.d.ts +4 -0
- package/build/cli/models/config/item-patterns.d.ts.map +1 -0
- package/build/cli/models/config/item-patterns.js +43 -0
- package/build/cli/models/config/publish-content-config.d.ts +6 -1
- package/build/cli/models/config/publish-content-config.d.ts.map +1 -1
- package/build/cli/models/config/static-publish-project.d.ts +23 -0
- package/build/cli/models/config/static-publish-project.d.ts.map +1 -0
- package/build/cli/models/config/static-publish-project.js +11 -0
- package/build/cli/models/config/static-publish-rc.d.ts +25 -0
- package/build/cli/models/config/static-publish-rc.d.ts.map +1 -1
- package/build/cli/models/config/static-publish-rc.js +31 -0
- package/build/cli/models/config/template-settings.d.ts +10 -0
- package/build/cli/models/config/template-settings.d.ts.map +1 -0
- package/build/cli/models/config/template-settings.js +72 -0
- package/build/cli/models/server/index.d.ts.map +1 -1
- package/build/cli/models/server/index.js +6 -2
- package/build/server/hono/extensions.d.ts +48 -0
- package/build/server/hono/extensions.d.ts.map +1 -0
- package/build/server/hono/extensions.js +65 -0
- package/build/server/hono/index.d.ts +30 -0
- package/build/server/hono/index.d.ts.map +1 -0
- package/build/server/hono/index.js +72 -0
- package/build/server/hono/path.d.ts +7 -0
- package/build/server/hono/path.d.ts.map +1 -0
- package/build/server/hono/path.js +32 -0
- package/build/server/models/assets/index.d.ts +6 -0
- package/build/server/models/assets/index.d.ts.map +1 -1
- package/build/server/models/assets/index.js +24 -0
- package/build/server/models/collections/index.d.ts +3 -0
- package/build/server/models/collections/index.d.ts.map +1 -0
- package/build/server/models/collections/index.js +15 -0
- package/build/server/models/compression/index.d.ts +1 -1
- package/build/server/models/compression/index.d.ts.map +1 -1
- package/build/server/models/config/collection-selection.d.ts +55 -0
- package/build/server/models/config/collection-selection.d.ts.map +1 -0
- package/build/server/models/config/collection-selection.js +238 -0
- package/build/server/models/config/item-patterns.d.ts +4 -0
- package/build/server/models/config/item-patterns.d.ts.map +1 -0
- package/build/server/models/config/item-patterns.js +43 -0
- package/build/server/models/config/publish-content-config.d.ts +6 -1
- package/build/server/models/config/publish-content-config.d.ts.map +1 -1
- package/build/server/models/config/static-publish-project.d.ts +23 -0
- package/build/server/models/config/static-publish-project.d.ts.map +1 -0
- package/build/server/models/config/static-publish-project.js +11 -0
- package/build/server/models/config/static-publish-rc.d.ts +25 -0
- package/build/server/models/config/static-publish-rc.d.ts.map +1 -1
- package/build/server/models/config/static-publish-rc.js +31 -0
- package/build/server/models/server/index.d.ts.map +1 -1
- package/build/server/models/server/index.js +6 -2
- package/build/server/server/collection-selection/index.d.ts +11 -0
- package/build/server/server/collection-selection/index.d.ts.map +1 -0
- package/build/server/server/collection-selection/index.js +192 -0
- package/build/server/server/collection-selector/from-cookie.d.ts.map +1 -1
- package/build/server/server/collection-selector/from-cookie.js +6 -8
- package/build/server/server/collection-selector/from-request.d.ts.map +1 -1
- package/build/server/server/index.d.ts +4 -1
- package/build/server/server/index.d.ts.map +1 -1
- package/build/server/server/index.js +3 -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 +59 -6
- package/build/server/server/publisher-server/index.d.ts.map +1 -1
- package/build/server/server/publisher-server/index.js +370 -131
- 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/s3-storage-provider.d.ts.map +1 -1
- package/build/server/server/storage/storage-provider.d.ts.map +1 -1
- package/build/server/server/util/body-length.d.ts.map +1 -1
- package/build/server/server/util/server-timing.d.ts.map +1 -1
- package/package.json +23 -4
- package/resources/app/fastly.toml.njk +58 -0
- package/resources/app/gitignore.njk +9 -0
- package/resources/app/publish-content.config.from-project.js.njk +9 -0
- package/resources/app/publish-content.config.js.njk +35 -0
- package/resources/app/static-publish.project.js.njk +83 -0
- package/resources/app/static-publish.rc.js.njk +43 -0
- package/resources/template-init/hono/_example.ts +22 -0
- package/resources/template-init/hono/tsconfig.json +17 -0
- package/resources/templates/hono/package-lock.json +4123 -0
- package/resources/templates/hono/package.json +15 -0
- package/resources/templates/hono/scripts/template_postinstall.js +10 -0
- package/resources/templates/hono/scripts/template_prebuild.js +8 -0
- package/resources/templates/hono/scripts/template_watch.js +15 -0
- package/resources/templates/hono/src/hono-extensions.generated.d.ts +5 -0
- package/resources/templates/hono/src/index.ts +53 -0
- package/resources/templates/hono/tsconfig.json +17 -0
- package/resources/templates/plain/package-lock.json +4088 -0
- package/resources/templates/plain/package.json +10 -0
- package/resources/templates/plain/src/index.js +34 -0
|
@@ -4,14 +4,18 @@
|
|
|
4
4
|
*/
|
|
5
5
|
/// <reference types="@fastly/js-compute" />
|
|
6
6
|
import { CoreCache } from 'fastly:cache';
|
|
7
|
-
import { decodeAssetVariantMetadata, } from '../../models/assets/index.js';
|
|
7
|
+
import { decodeAssetVariantMetadata, findAssetEntry, isHiddenIndexKey, } from '../../models/assets/index.js';
|
|
8
|
+
import { parseItemPatterns, testItemPatterns } from '../../models/config/item-patterns.js';
|
|
9
|
+
import { validateCollectionSelection, } from '../../models/config/collection-selection.js';
|
|
10
|
+
import { isSelectedByUrl, resolveCollection } from '../collection-selection/index.js';
|
|
8
11
|
import { decodeIndexMetadata, getUnsupportedIndexFeatures } from '../../models/server/index.js';
|
|
9
12
|
import { isExpired } from '../../models/time/index.js';
|
|
10
13
|
import { loadStorageProviderFromStaticPublishRc, } from '../storage/storage-provider.js';
|
|
11
14
|
import { checkIfModifiedSince, getIfModifiedSinceHeader } from './serve-preconditions/if-modified-since.js';
|
|
12
15
|
import { checkIfNoneMatch, getIfNoneMatchHeader } from './serve-preconditions/if-none-match.js';
|
|
13
16
|
import { ServerTiming } from '../util/server-timing.js';
|
|
14
|
-
import {
|
|
17
|
+
import { requestAcceptsTextHtml } from './accept-html.js';
|
|
18
|
+
import { CACHEABLE_STATUSES, DEFAULT_MAX_BODY_SIZE, buildCacheFillRequest, buildResponseCacheKey, bytesInPieces, decodeCachedResponseMetadata, encodeCachedResponseMetadata, parseAcceptEncodingGroups, readBodyUpTo, streamBytesInPieces, } from './response-cache.js';
|
|
15
19
|
import { checkBodyLength } from '../util/body-length.js';
|
|
16
20
|
export function buildHeadersSubset(responseHeaders, keys) {
|
|
17
21
|
const resultHeaders = new Headers();
|
|
@@ -31,37 +35,68 @@ export function buildHeadersSubset(responseHeaders, keys) {
|
|
|
31
35
|
// * Content-Location, Date, ETag, and Vary
|
|
32
36
|
// * Cache-Control and Expires
|
|
33
37
|
const headersToPreserveForUnmodified = ['Content-Location', 'ETag', 'Vary', 'Cache-Control', 'Expires'];
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
// The response for a collection other than the default collection that is not
|
|
39
|
+
// published, or that expired. It can be published at any time, so no cache
|
|
40
|
+
// stores this response.
|
|
41
|
+
function collectionNotAvailableResponse(collectionName) {
|
|
42
|
+
return new Response(`Collection '${collectionName}' is not available. It is not published, or it expired.`, {
|
|
43
|
+
status: 404,
|
|
44
|
+
headers: {
|
|
45
|
+
'content-type': 'text/plain',
|
|
46
|
+
'x-content-type-options': 'nosniff',
|
|
47
|
+
'cache-control': 'no-store',
|
|
48
|
+
},
|
|
49
|
+
});
|
|
42
50
|
}
|
|
43
51
|
export class PublisherServer {
|
|
44
52
|
constructor(publishId, storageProvider, defaultCollectionName) {
|
|
53
|
+
// See setCollectionSelection().
|
|
54
|
+
this.collectionSelection = null;
|
|
45
55
|
this.publishId = publishId;
|
|
46
56
|
this.storageProvider = storageProvider;
|
|
47
57
|
this.defaultCollectionName = defaultCollectionName;
|
|
48
58
|
this.activeCollectionName = this.defaultCollectionName;
|
|
49
59
|
this.collectionNameHeader = 'X-Publisher-Server-Collection';
|
|
50
60
|
this.serverTimingRequestHeader = null;
|
|
51
|
-
this.serverTiming = null;
|
|
52
61
|
this.responseCache = null;
|
|
62
|
+
this.currentScope = this.newScope(this.activeCollectionName, null);
|
|
63
|
+
this.requestScopes = new WeakMap();
|
|
53
64
|
}
|
|
54
65
|
static fromStaticPublishRc(config) {
|
|
55
66
|
const storeProvider = loadStorageProviderFromStaticPublishRc(config);
|
|
56
|
-
|
|
67
|
+
const server = new PublisherServer(config.publishId, storeProvider, config.defaultCollectionName);
|
|
68
|
+
server.setCollectionSelection(config.collectionSelection ?? null);
|
|
69
|
+
return server;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @deprecated Read-only. This returns the Server-Timing of the current request.
|
|
73
|
+
* It will be removed in v9.
|
|
74
|
+
*/
|
|
75
|
+
get serverTiming() {
|
|
76
|
+
return this.currentScope.serverTiming;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @deprecated Read-only. This returns the settings that the current request
|
|
80
|
+
* read. Use getServerConfig().
|
|
81
|
+
* It will be removed in v9.
|
|
82
|
+
*/
|
|
83
|
+
get settingsCached() {
|
|
84
|
+
return this.currentScope.settings;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @deprecated Read-only. This returns the index that the current request read.
|
|
88
|
+
* Use getAssetEntryMap().
|
|
89
|
+
* It will be removed in v9.
|
|
90
|
+
*/
|
|
91
|
+
get assetEntryMapCache() {
|
|
92
|
+
return this.currentScope.assetEntryMap;
|
|
57
93
|
}
|
|
58
94
|
// null selects the default collection. A sandbox can be reused for more than
|
|
59
95
|
// one request, so call this for each request, also when no collection is
|
|
60
96
|
// selected. Otherwise, the collection of the previous request stays active.
|
|
61
97
|
setActiveCollectionName(collectionName) {
|
|
62
98
|
this.activeCollectionName = collectionName ?? this.defaultCollectionName;
|
|
63
|
-
this.
|
|
64
|
-
this.assetEntryMapCache = undefined;
|
|
99
|
+
this.currentScope = this.newScope(this.activeCollectionName, this.currentScope.serverTiming);
|
|
65
100
|
}
|
|
66
101
|
setCollectionNameHeader(collectionHeader) {
|
|
67
102
|
this.collectionNameHeader = collectionHeader;
|
|
@@ -79,29 +114,84 @@ export class PublisherServer {
|
|
|
79
114
|
setResponseCache(options) {
|
|
80
115
|
this.responseCache = options;
|
|
81
116
|
}
|
|
82
|
-
//
|
|
117
|
+
// The collectionSelection of static-publish.rc.js. fromStaticPublishRc() sets
|
|
118
|
+
// it. null (the default) turns collection selection off. Throws a TypeError if
|
|
119
|
+
// the config is not valid.
|
|
120
|
+
// With a config, a collection that is not the default collection, selected
|
|
121
|
+
// with the collectionName option of serveRequest() or serveFallback(), gets:
|
|
122
|
+
// - if it does not exist or has expired: onMissing ('not-found': a 404
|
|
123
|
+
// response; 'default': the default collection).
|
|
124
|
+
// - X-Robots-Tag: noindex (unless noindex is false).
|
|
125
|
+
// - Cache-Control: private, unless the collection comes from the URL (a host,
|
|
126
|
+
// path, or query source). See markPreviewResponse().
|
|
127
|
+
setCollectionSelection(config) {
|
|
128
|
+
if (config != null) {
|
|
129
|
+
const errors = validateCollectionSelection(config);
|
|
130
|
+
if (errors.length > 0) {
|
|
131
|
+
throw new TypeError(`Invalid collectionSelection:\n${errors.join('\n')}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
this.collectionSelection = config;
|
|
135
|
+
}
|
|
136
|
+
// Choose the collection for a request with the collectionSelection config.
|
|
137
|
+
// If the result has a response, return it. Otherwise, pass its collectionName
|
|
138
|
+
// and pathname to serveRequest().
|
|
139
|
+
resolveCollection(request) {
|
|
140
|
+
return resolveCollection(request, this.collectionSelection);
|
|
141
|
+
}
|
|
142
|
+
// Start handling a new request. serveRequest() and serveFallback() call this. If you call
|
|
83
143
|
// getMatchingAsset() and serveAsset() directly, call this first.
|
|
84
|
-
// A sandbox can be reused for more than one request, so this
|
|
85
|
-
// settings and the index that a previous request read
|
|
86
|
-
// sandbox serves an old index after a publish, and
|
|
144
|
+
// A sandbox can be reused for more than one request, so this starts a new
|
|
145
|
+
// scope: the settings and the index that a previous request read are not used.
|
|
146
|
+
// Without this, a reused sandbox serves an old index after a publish, and
|
|
147
|
+
// serves an expired collection.
|
|
87
148
|
beginRequest(request) {
|
|
88
|
-
this.
|
|
89
|
-
|
|
149
|
+
this.beginRequestFor(request, undefined);
|
|
150
|
+
}
|
|
151
|
+
newServerTiming(request) {
|
|
90
152
|
const header = this.serverTimingRequestHeader;
|
|
91
|
-
|
|
153
|
+
return header != null && request.headers.has(header) ? new ServerTiming() : null;
|
|
154
|
+
}
|
|
155
|
+
newScope(collectionName, serverTiming) {
|
|
156
|
+
return {
|
|
157
|
+
collectionName,
|
|
158
|
+
serverTiming,
|
|
159
|
+
settings: undefined,
|
|
160
|
+
assetEntryMap: undefined,
|
|
161
|
+
doNotStoreResponse: false,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
// The scope of a request for a collection. A new request gets a new scope.
|
|
165
|
+
scopeForRequest(request, name) {
|
|
166
|
+
let scopes = this.requestScopes.get(request);
|
|
167
|
+
if (scopes == null) {
|
|
168
|
+
scopes = new Map();
|
|
169
|
+
this.requestScopes.set(request, scopes);
|
|
170
|
+
}
|
|
171
|
+
let scope = scopes.get(name);
|
|
172
|
+
if (scope == null) {
|
|
173
|
+
// All the collections of one request share one Server-Timing.
|
|
174
|
+
const otherScope = scopes.values().next().value;
|
|
175
|
+
scope = this.newScope(name, otherScope != null ? otherScope.serverTiming : this.newServerTiming(request));
|
|
176
|
+
scopes.set(name, scope);
|
|
177
|
+
}
|
|
178
|
+
return scope;
|
|
92
179
|
}
|
|
93
|
-
timed(name, fn) {
|
|
94
|
-
return
|
|
180
|
+
timed(scope, name, fn) {
|
|
181
|
+
return scope.serverTiming != null ? scope.serverTiming.measure(name, fn) : fn();
|
|
95
182
|
}
|
|
96
183
|
// Server config is obtained from storage, and cached until the next beginRequest()
|
|
97
184
|
// or setActiveCollectionName().
|
|
98
185
|
async getServerConfig() {
|
|
99
|
-
|
|
100
|
-
|
|
186
|
+
return this.getServerConfigFor(this.currentScope);
|
|
187
|
+
}
|
|
188
|
+
async getServerConfigFor(scope) {
|
|
189
|
+
if (scope.settings !== undefined) {
|
|
190
|
+
return scope.settings;
|
|
101
191
|
}
|
|
102
|
-
const settingsFileKey = `${this.publishId}_settings_${
|
|
103
|
-
|
|
104
|
-
const settingsFile = await this.storageProvider.getEntry(settingsFileKey, [`${this.publishId}-${
|
|
192
|
+
const settingsFileKey = `${this.publishId}_settings_${scope.collectionName}`;
|
|
193
|
+
scope.settings = await this.timed(scope, 'settings', async () => {
|
|
194
|
+
const settingsFile = await this.storageProvider.getEntry(settingsFileKey, [`${this.publishId}-${scope.collectionName}`, 'settings']);
|
|
105
195
|
if (settingsFile == null) {
|
|
106
196
|
console.error(`Settings File not found at ${settingsFileKey}.`);
|
|
107
197
|
console.error(`You may need to publish your application.`);
|
|
@@ -109,37 +199,33 @@ export class PublisherServer {
|
|
|
109
199
|
}
|
|
110
200
|
return (await settingsFile.json());
|
|
111
201
|
});
|
|
112
|
-
return
|
|
202
|
+
return scope.settings;
|
|
113
203
|
}
|
|
114
204
|
async getStaticItems() {
|
|
115
|
-
|
|
205
|
+
return this.getStaticItemsFor(this.currentScope);
|
|
206
|
+
}
|
|
207
|
+
async getStaticItemsFor(scope) {
|
|
208
|
+
const serverConfig = await this.getServerConfigFor(scope);
|
|
116
209
|
if (serverConfig == null) {
|
|
117
210
|
return [];
|
|
118
211
|
}
|
|
119
|
-
return serverConfig.staticItems
|
|
120
|
-
.
|
|
121
|
-
|
|
122
|
-
const fragments = x.slice(3).match(/\/(.*?)\/([a-z]*)?$/i);
|
|
123
|
-
if (fragments == null) {
|
|
124
|
-
console.warn(`Cannot parse staticItems item index ${i}: '${x}', skipping...`);
|
|
125
|
-
return '';
|
|
126
|
-
}
|
|
127
|
-
return new RegExp(fragments[1], fragments[2] || '');
|
|
128
|
-
}
|
|
129
|
-
return x;
|
|
130
|
-
})
|
|
131
|
-
.filter(x => Boolean(x));
|
|
212
|
+
return parseItemPatterns(serverConfig.staticItems, (i, x) => {
|
|
213
|
+
console.warn(`Cannot parse staticItems item index ${i}: '${x}', skipping...`);
|
|
214
|
+
});
|
|
132
215
|
}
|
|
133
216
|
async getAssetEntryMap() {
|
|
134
|
-
|
|
135
|
-
|
|
217
|
+
return this.getAssetEntryMapFor(this.currentScope);
|
|
218
|
+
}
|
|
219
|
+
async getAssetEntryMapFor(scope) {
|
|
220
|
+
if (scope.assetEntryMap !== undefined) {
|
|
221
|
+
return scope.assetEntryMap;
|
|
136
222
|
}
|
|
137
|
-
const indexFileKey = `${this.publishId}_index_${
|
|
138
|
-
const indexFile = await this.timed('index', () => this.storageProvider.getEntry(indexFileKey, [`${this.publishId}-${
|
|
223
|
+
const indexFileKey = `${this.publishId}_index_${scope.collectionName}`;
|
|
224
|
+
const indexFile = await this.timed(scope, 'index', () => this.storageProvider.getEntry(indexFileKey, [`${this.publishId}-${scope.collectionName}`, 'index']));
|
|
139
225
|
if (indexFile == null) {
|
|
140
226
|
console.error(`Index File not found at ${indexFileKey}.`);
|
|
141
227
|
console.error(`You may need to publish your application.`);
|
|
142
|
-
|
|
228
|
+
scope.assetEntryMap = null;
|
|
143
229
|
return null;
|
|
144
230
|
}
|
|
145
231
|
let metadata;
|
|
@@ -158,35 +244,48 @@ export class PublisherServer {
|
|
|
158
244
|
if (unsupportedFeatures.length > 0) {
|
|
159
245
|
console.error(`Collection at ${indexFileKey} requires features that this version of the server does not support: ${unsupportedFeatures.join(', ')}.`);
|
|
160
246
|
console.error(`Build and deploy the application with a newer version of @fastly/compute-js-static-publish.`);
|
|
161
|
-
|
|
247
|
+
scope.assetEntryMap = null;
|
|
162
248
|
return null;
|
|
163
249
|
}
|
|
164
250
|
let collectionIsExpired = false;
|
|
165
|
-
if (
|
|
251
|
+
if (scope.collectionName !== this.defaultCollectionName && metadata != null) {
|
|
166
252
|
collectionIsExpired = metadata.expirationTime != null && isExpired(metadata.expirationTime);
|
|
167
253
|
}
|
|
168
254
|
if (collectionIsExpired) {
|
|
169
255
|
console.error(`Requested collection expired at ${indexFileKey}.`);
|
|
170
|
-
|
|
256
|
+
scope.assetEntryMap = null;
|
|
171
257
|
return null;
|
|
172
258
|
}
|
|
173
259
|
// Read and parse in two steps, so that Server-Timing can report each one.
|
|
174
|
-
const indexText = await this.timed('index-body', () => indexFile.text());
|
|
175
|
-
|
|
176
|
-
return
|
|
260
|
+
const indexText = await this.timed(scope, 'index-body', () => indexFile.text());
|
|
261
|
+
scope.assetEntryMap = await this.timed(scope, 'index-parse', async () => JSON.parse(indexText));
|
|
262
|
+
return scope.assetEntryMap;
|
|
177
263
|
}
|
|
178
|
-
|
|
179
|
-
|
|
264
|
+
// Find the file for an asset key. The second argument can also be a boolean,
|
|
265
|
+
// the value of applyAuto.
|
|
266
|
+
async getMatchingAsset(assetKey, options = false) {
|
|
267
|
+
const { applyAuto = false, includeHidden = false } = typeof options === 'boolean' ? { applyAuto: options } : options;
|
|
268
|
+
return this.getMatchingAssetFor(this.currentScope, assetKey, applyAuto, includeHidden);
|
|
269
|
+
}
|
|
270
|
+
async getMatchingAssetFor(scope, assetKey, applyAuto, includeHidden) {
|
|
271
|
+
const serverConfig = await this.getServerConfigFor(scope);
|
|
180
272
|
if (serverConfig == null) {
|
|
181
273
|
return null;
|
|
182
274
|
}
|
|
183
|
-
const assetEntryMap = await this.
|
|
275
|
+
const assetEntryMap = await this.getAssetEntryMapFor(scope);
|
|
184
276
|
if (assetEntryMap == null) {
|
|
185
277
|
return null;
|
|
186
278
|
}
|
|
279
|
+
// A key that starts with 'hidden:' is never looked up, so that a request path
|
|
280
|
+
// cannot name a hidden file. (A real asset key always starts with a slash.)
|
|
281
|
+
// With includeHidden, a normal key also finds the hidden file of that key.
|
|
282
|
+
if (isHiddenIndexKey(assetKey)) {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
const lookup = (key) => findAssetEntry(assetEntryMap, key, includeHidden);
|
|
187
286
|
if (!assetKey.endsWith('/')) {
|
|
188
287
|
// A path that does not end in a slash can match an asset directly
|
|
189
|
-
const asset =
|
|
288
|
+
const asset = lookup(assetKey);
|
|
190
289
|
if (asset != null) {
|
|
191
290
|
return asset;
|
|
192
291
|
}
|
|
@@ -195,7 +294,7 @@ export class PublisherServer {
|
|
|
195
294
|
// looks for an asset that has the specified suffix (usually extension, such as .html)
|
|
196
295
|
for (const extEntry of serverConfig.autoExt) {
|
|
197
296
|
let assetKeyWithExt = assetKey + extEntry;
|
|
198
|
-
const asset =
|
|
297
|
+
const asset = lookup(assetKeyWithExt);
|
|
199
298
|
if (asset != null) {
|
|
200
299
|
return asset;
|
|
201
300
|
}
|
|
@@ -215,7 +314,7 @@ export class PublisherServer {
|
|
|
215
314
|
assetNameAsDir = assetNameAsDir + '/';
|
|
216
315
|
for (const indexEntry of serverConfig.autoIndex) {
|
|
217
316
|
let assetKeyIndex = assetNameAsDir + indexEntry;
|
|
218
|
-
const asset =
|
|
317
|
+
const asset = lookup(assetKeyIndex);
|
|
219
318
|
if (asset != null) {
|
|
220
319
|
return asset;
|
|
221
320
|
}
|
|
@@ -229,24 +328,21 @@ export class PublisherServer {
|
|
|
229
328
|
// For example, if accept-encoding had br;q=1,gzip;q=0.5, and the server accepts both br and gzip,
|
|
230
329
|
// the result would be [['br'], ['gzip']]
|
|
231
330
|
async findAcceptEncodingsGroups(request) {
|
|
232
|
-
|
|
331
|
+
return this.findAcceptEncodingsGroupsFor(this.currentScope, request);
|
|
332
|
+
}
|
|
333
|
+
async findAcceptEncodingsGroupsFor(scope, request) {
|
|
334
|
+
const serverConfig = await this.getServerConfigFor(scope);
|
|
233
335
|
if (serverConfig == null || serverConfig.allowedEncodings.length === 0) {
|
|
234
336
|
return [];
|
|
235
337
|
}
|
|
236
338
|
return parseAcceptEncodingGroups(request.headers.get('accept-encoding') ?? '', serverConfig.allowedEncodings);
|
|
237
339
|
}
|
|
238
340
|
async testExtendedCache(pathname) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
if (x.endsWith('/')) {
|
|
246
|
-
return pathname.startsWith(x);
|
|
247
|
-
}
|
|
248
|
-
return x === pathname;
|
|
249
|
-
});
|
|
341
|
+
return this.testExtendedCacheFor(this.currentScope, pathname);
|
|
342
|
+
}
|
|
343
|
+
async testExtendedCacheFor(scope, pathname) {
|
|
344
|
+
const staticItems = await this.getStaticItemsFor(scope);
|
|
345
|
+
return testItemPatterns(staticItems, pathname);
|
|
250
346
|
}
|
|
251
347
|
handlePreconditions(request, asset, responseHeaders) {
|
|
252
348
|
return this.handlePreconditionsForLastModified(request, asset.lastModifiedTime, responseHeaders);
|
|
@@ -307,10 +403,13 @@ export class PublisherServer {
|
|
|
307
403
|
return null;
|
|
308
404
|
}
|
|
309
405
|
async loadAssetVariant(entry, variant) {
|
|
406
|
+
return this.loadAssetVariantFor(this.currentScope, entry, variant);
|
|
407
|
+
}
|
|
408
|
+
async loadAssetVariantFor(scope, entry, variant) {
|
|
310
409
|
const baseHash = entry.key.slice(7);
|
|
311
410
|
const baseKey = `${this.publishId}_files_sha256_${baseHash}`;
|
|
312
411
|
const variantKey = variant != null ? `${baseKey}_${variant}` : baseKey;
|
|
313
|
-
const storageEntry = await this.timed('asset', () => this.storageProvider.getEntry(variantKey));
|
|
412
|
+
const storageEntry = await this.timed(scope, 'asset', () => this.storageProvider.getEntry(variantKey));
|
|
314
413
|
if (storageEntry == null) {
|
|
315
414
|
return null;
|
|
316
415
|
}
|
|
@@ -334,7 +433,7 @@ export class PublisherServer {
|
|
|
334
433
|
...metadata,
|
|
335
434
|
};
|
|
336
435
|
}
|
|
337
|
-
async findAssetVariantForAcceptEncodingsGroups(entry, acceptEncodingsGroups = []) {
|
|
436
|
+
async findAssetVariantForAcceptEncodingsGroups(scope, entry, acceptEncodingsGroups = []) {
|
|
338
437
|
if (!entry.key.startsWith('sha256:')) {
|
|
339
438
|
throw new TypeError(`Key must start with 'sha256:': ${entry.key}`);
|
|
340
439
|
}
|
|
@@ -347,7 +446,7 @@ export class PublisherServer {
|
|
|
347
446
|
if (!entry.variants.includes(encoding)) {
|
|
348
447
|
continue;
|
|
349
448
|
}
|
|
350
|
-
const assetVariant = await this.
|
|
449
|
+
const assetVariant = await this.loadAssetVariantFor(scope, entry, encoding);
|
|
351
450
|
if (assetVariant == null) {
|
|
352
451
|
continue;
|
|
353
452
|
}
|
|
@@ -360,17 +459,20 @@ export class PublisherServer {
|
|
|
360
459
|
return smallestVariant;
|
|
361
460
|
}
|
|
362
461
|
}
|
|
363
|
-
const baseAssetVariant = await this.
|
|
462
|
+
const baseAssetVariant = await this.loadAssetVariantFor(scope, entry, null);
|
|
364
463
|
if (baseAssetVariant == null) {
|
|
365
464
|
throw new TypeError('Key not found: ' + entry.key);
|
|
366
465
|
}
|
|
367
466
|
return baseAssetVariant;
|
|
368
467
|
}
|
|
369
468
|
async serveAsset(request, asset, init) {
|
|
469
|
+
return this.serveAssetFor(this.currentScope, request, asset, init);
|
|
470
|
+
}
|
|
471
|
+
async serveAssetFor(scope, request, asset, init) {
|
|
370
472
|
const headers = new Headers(init?.headers);
|
|
371
473
|
headers.set('Content-Type', asset.contentType);
|
|
372
474
|
if (this.collectionNameHeader) {
|
|
373
|
-
headers.set(this.collectionNameHeader,
|
|
475
|
+
headers.set(this.collectionNameHeader, scope.collectionName);
|
|
374
476
|
headers.append('Vary', this.collectionNameHeader);
|
|
375
477
|
}
|
|
376
478
|
if (init?.cache != null) {
|
|
@@ -385,8 +487,8 @@ export class PublisherServer {
|
|
|
385
487
|
}
|
|
386
488
|
headers.append('Cache-Control', cacheControlValue);
|
|
387
489
|
}
|
|
388
|
-
const acceptEncodings = await this.
|
|
389
|
-
const assetVariant = await this.findAssetVariantForAcceptEncodingsGroups(asset, acceptEncodings);
|
|
490
|
+
const acceptEncodings = await this.findAcceptEncodingsGroupsFor(scope, request);
|
|
491
|
+
const assetVariant = await this.findAssetVariantForAcceptEncodingsGroups(scope, asset, acceptEncodings);
|
|
390
492
|
if (assetVariant.contentEncoding != null) {
|
|
391
493
|
headers.append('Content-Encoding', assetVariant.contentEncoding);
|
|
392
494
|
}
|
|
@@ -394,8 +496,8 @@ export class PublisherServer {
|
|
|
394
496
|
if (asset.lastModifiedTime !== 0) {
|
|
395
497
|
headers.set('Last-Modified', (new Date(asset.lastModifiedTime * 1000)).toUTCString());
|
|
396
498
|
}
|
|
397
|
-
if (
|
|
398
|
-
headers.append('Server-Timing',
|
|
499
|
+
if (scope.serverTiming != null) {
|
|
500
|
+
headers.append('Server-Timing', scope.serverTiming.toHeaderValue());
|
|
399
501
|
}
|
|
400
502
|
const preconditionResponse = this.handlePreconditions(request, asset, headers);
|
|
401
503
|
if (preconditionResponse != null) {
|
|
@@ -418,30 +520,33 @@ export class PublisherServer {
|
|
|
418
520
|
// null (not found). Conditional requests and HEAD are then answered from the
|
|
419
521
|
// cached response. If the cache is off, produce() gets the original request.
|
|
420
522
|
async serveCached(request, keyPath, produce) {
|
|
523
|
+
return this.serveCachedFor(this.currentScope, request, keyPath, produce);
|
|
524
|
+
}
|
|
525
|
+
async serveCachedFor(scope, request, keyPath, produce) {
|
|
421
526
|
if (this.responseCache == null) {
|
|
422
527
|
return produce(request);
|
|
423
528
|
}
|
|
424
|
-
const key = buildResponseCacheKey(this.publishId,
|
|
529
|
+
const key = buildResponseCacheKey(this.publishId, scope.collectionName, keyPath, request);
|
|
425
530
|
const lookupStart = performance.now();
|
|
426
531
|
const entry = CoreCache.transactionLookup(key);
|
|
427
532
|
const state = entry.state();
|
|
428
533
|
if (!state.mustInsertOrUpdate()) {
|
|
429
534
|
const metadata = state.found() && state.usable() ? decodeCachedResponseMetadata(entry.userMetadata()) : null;
|
|
430
535
|
if (metadata?.tooLarge) {
|
|
431
|
-
|
|
536
|
+
scope.serverTiming?.add('cache', performance.now() - lookupStart, 'bypass');
|
|
432
537
|
return produce(request);
|
|
433
538
|
}
|
|
434
539
|
if (metadata != null) {
|
|
435
540
|
// age() is in milliseconds. A hit younger than the time since a purge proves the entry was refilled.
|
|
436
|
-
|
|
437
|
-
return this.responseFromCache(request, metadata, entry.body());
|
|
541
|
+
scope.serverTiming?.add('cache', performance.now() - lookupStart, `hit age=${Math.round(entry.age() / 1000)}s`);
|
|
542
|
+
return this.responseFromCache(scope, request, metadata, entry.body());
|
|
438
543
|
}
|
|
439
544
|
// Not usable, and another request is not filling it for us: serve without the cache.
|
|
440
|
-
|
|
545
|
+
scope.serverTiming?.add('cache', performance.now() - lookupStart, 'bypass');
|
|
441
546
|
return produce(request);
|
|
442
547
|
}
|
|
443
548
|
// This request must fill the entry (a miss, or a stale entry after a soft purge).
|
|
444
|
-
|
|
549
|
+
scope.serverTiming?.add('cache', performance.now() - lookupStart, 'miss');
|
|
445
550
|
let response;
|
|
446
551
|
try {
|
|
447
552
|
response = await produce(buildCacheFillRequest(request));
|
|
@@ -450,10 +555,14 @@ export class PublisherServer {
|
|
|
450
555
|
entry.cancel();
|
|
451
556
|
throw err;
|
|
452
557
|
}
|
|
558
|
+
if (scope.doNotStoreResponse) {
|
|
559
|
+
entry.cancel();
|
|
560
|
+
return response;
|
|
561
|
+
}
|
|
453
562
|
const insertOptions = {
|
|
454
563
|
// The Core Cache takes maxAge in milliseconds. ResponseCacheOptions.maxAge is in seconds.
|
|
455
564
|
maxAge: this.responseCache.maxAge * 1000,
|
|
456
|
-
surrogateKeys: [`${this.publishId}-${
|
|
565
|
+
surrogateKeys: [`${this.publishId}-${scope.collectionName}`],
|
|
457
566
|
};
|
|
458
567
|
if (response == null) {
|
|
459
568
|
const metadata = { notFound: true };
|
|
@@ -496,24 +605,28 @@ export class PublisherServer {
|
|
|
496
605
|
length: 0,
|
|
497
606
|
});
|
|
498
607
|
writer.close();
|
|
499
|
-
return this.responseFromCache(request, metadata, readResult.body);
|
|
608
|
+
return this.responseFromCache(scope, request, metadata, readResult.body);
|
|
500
609
|
}
|
|
501
610
|
const writer = entry.insert({
|
|
502
611
|
...insertOptions,
|
|
503
612
|
userMetadata: encodeCachedResponseMetadata(metadata),
|
|
504
613
|
length: readResult.bytes.byteLength,
|
|
505
614
|
});
|
|
506
|
-
|
|
615
|
+
// The body goes to the entry and to the response in pieces. A call with the
|
|
616
|
+
// whole body makes a full copy of it, which needs memory for one more body.
|
|
617
|
+
for (const piece of bytesInPieces(readResult.bytes)) {
|
|
618
|
+
writer.append(piece);
|
|
619
|
+
}
|
|
507
620
|
writer.close();
|
|
508
|
-
return this.responseFromCache(request, metadata, readResult.bytes);
|
|
621
|
+
return this.responseFromCache(scope, request, metadata, streamBytesInPieces(readResult.bytes));
|
|
509
622
|
}
|
|
510
|
-
responseFromCache(request, metadata, body) {
|
|
623
|
+
responseFromCache(scope, request, metadata, body) {
|
|
511
624
|
if (metadata.notFound) {
|
|
512
625
|
return null;
|
|
513
626
|
}
|
|
514
627
|
const headers = new Headers(metadata.headers);
|
|
515
|
-
if (
|
|
516
|
-
headers.append('Server-Timing',
|
|
628
|
+
if (scope.serverTiming != null) {
|
|
629
|
+
headers.append('Server-Timing', scope.serverTiming.toHeaderValue());
|
|
517
630
|
}
|
|
518
631
|
const lastModified = headers.get('Last-Modified');
|
|
519
632
|
const lastModifiedMs = lastModified != null ? Date.parse(lastModified) : NaN;
|
|
@@ -527,25 +640,138 @@ export class PublisherServer {
|
|
|
527
640
|
headers,
|
|
528
641
|
});
|
|
529
642
|
}
|
|
530
|
-
|
|
531
|
-
|
|
643
|
+
// Serve the file that matches the request. If no file matches, serve the SPA
|
|
644
|
+
// file or the 404 page (see options.fallback). Returns null if there is no
|
|
645
|
+
// response for the request, for example for a method other than GET and HEAD.
|
|
646
|
+
async serveRequest(request, options = {}) {
|
|
647
|
+
let scope = this.beginRequestFor(request, options.collectionName);
|
|
532
648
|
// Only handle GET and HEAD
|
|
533
649
|
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
534
650
|
return null;
|
|
535
651
|
}
|
|
536
|
-
|
|
537
|
-
|
|
652
|
+
let pathname;
|
|
653
|
+
if (options.pathname != null) {
|
|
654
|
+
if (!options.pathname.startsWith('/')) {
|
|
655
|
+
throw new TypeError(`pathname must start with '/': ${options.pathname}`);
|
|
656
|
+
}
|
|
657
|
+
pathname = options.pathname;
|
|
658
|
+
}
|
|
659
|
+
else {
|
|
660
|
+
pathname = decodeURI(new URL(request.url).pathname);
|
|
661
|
+
}
|
|
538
662
|
// Custom health check route
|
|
539
|
-
if (pathname === '/healthz') {
|
|
663
|
+
if (options.healthCheck !== false && pathname === '/healthz') {
|
|
540
664
|
return new Response("OK", { status: 200 });
|
|
541
665
|
}
|
|
666
|
+
const selected = await this.checkSelectedCollection(request, scope);
|
|
667
|
+
if (selected instanceof Response) {
|
|
668
|
+
return selected;
|
|
669
|
+
}
|
|
670
|
+
scope = selected.scope;
|
|
542
671
|
// The fallback pages (SPA, not found) depend on whether the client accepts HTML.
|
|
543
|
-
|
|
544
|
-
|
|
672
|
+
// With fallback: false, the response does not depend on HTML, so it has its own cache key.
|
|
673
|
+
const fallback = options.fallback !== false && requestAcceptsTextHtml(request);
|
|
674
|
+
const keyPath = fallback ? pathname : `${pathname}|no-html`;
|
|
675
|
+
const response = await this.serveCachedFor(scope, request, keyPath, (fillRequest) => this.serveRequestFromStorage(scope, fillRequest, pathname, fallback));
|
|
676
|
+
return selected.preview ? this.markPreviewResponse(request, selected.scope, response) : response;
|
|
545
677
|
}
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
678
|
+
// Serve the SPA file or the 404 page, as serveRequest() does when no file
|
|
679
|
+
// matches. Use it after serveRequest() with fallback: false. Returns null for
|
|
680
|
+
// a method other than GET and HEAD, if the client does not accept HTML, or if
|
|
681
|
+
// the collection has no fallback page.
|
|
682
|
+
async serveFallback(request, options = {}) {
|
|
683
|
+
let scope = this.beginRequestFor(request, options.collectionName);
|
|
684
|
+
// Only handle GET and HEAD
|
|
685
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
if (!requestAcceptsTextHtml(request)) {
|
|
689
|
+
return null;
|
|
690
|
+
}
|
|
691
|
+
const selected = await this.checkSelectedCollection(request, scope);
|
|
692
|
+
if (selected instanceof Response) {
|
|
693
|
+
return selected;
|
|
694
|
+
}
|
|
695
|
+
scope = selected.scope;
|
|
696
|
+
// The fallback response is the same for all paths, so it has one cache key.
|
|
697
|
+
// A path always starts with a slash, so this key is not the key of a path.
|
|
698
|
+
const response = await this.serveCachedFor(scope, request, '|fallback', async (fillRequest) => {
|
|
699
|
+
const serverConfig = await this.getServerConfigFor(scope);
|
|
700
|
+
if (serverConfig == null) {
|
|
701
|
+
return null;
|
|
702
|
+
}
|
|
703
|
+
return this.serveFallbackFromStorage(scope, fillRequest, serverConfig);
|
|
704
|
+
});
|
|
705
|
+
return selected.preview ? this.markPreviewResponse(request, selected.scope, response) : response;
|
|
706
|
+
}
|
|
707
|
+
// Returns true if the collection is published, has not expired, and can
|
|
708
|
+
// be served by this version. It reads the settings and the index into the scope.
|
|
709
|
+
async isCollectionAvailable(request, collectionName) {
|
|
710
|
+
return this.isCollectionAvailableFor(this.scopeForRequest(request, collectionName ?? this.defaultCollectionName));
|
|
711
|
+
}
|
|
712
|
+
async isCollectionAvailableFor(scope) {
|
|
713
|
+
return (await this.getServerConfigFor(scope)) != null && (await this.getAssetEntryMapFor(scope)) != null;
|
|
714
|
+
}
|
|
715
|
+
// With collection selection, check a collection that is not the default
|
|
716
|
+
// collection (see setCollectionSelection()). Returns the scope to serve, and
|
|
717
|
+
// whether to mark the response as a preview, or a 404 response for onMissing:
|
|
718
|
+
// 'not-found'. The check reads the index, also when the response cache has the
|
|
719
|
+
// response.
|
|
720
|
+
async checkSelectedCollection(request, scope) {
|
|
721
|
+
const config = this.collectionSelection;
|
|
722
|
+
if (config == null || scope.collectionName === this.defaultCollectionName) {
|
|
723
|
+
return { scope, preview: false };
|
|
724
|
+
}
|
|
725
|
+
if (await this.isCollectionAvailableFor(scope)) {
|
|
726
|
+
return { scope, preview: true };
|
|
727
|
+
}
|
|
728
|
+
if (config.onMissing === 'default') {
|
|
729
|
+
this.currentScope = this.scopeForRequest(request, this.defaultCollectionName);
|
|
730
|
+
return { scope: this.currentScope, preview: false };
|
|
731
|
+
}
|
|
732
|
+
const response = collectionNotAvailableResponse(scope.collectionName);
|
|
733
|
+
if (config.noindex !== false) {
|
|
734
|
+
response.headers.set('X-Robots-Tag', 'noindex');
|
|
735
|
+
}
|
|
736
|
+
return response;
|
|
737
|
+
}
|
|
738
|
+
// A response from a collection that is not the default collection. If a host,
|
|
739
|
+
// path, or query source of the request selects this collection, the URL
|
|
740
|
+
// names the collection, so a shared cache can store the response. Otherwise
|
|
741
|
+
// (a cookie or header source, or a collectionName that the app chose) the same
|
|
742
|
+
// URL can serve another collection, so the response gets Cache-Control:
|
|
743
|
+
// private, and shared caches do not store it.
|
|
744
|
+
markPreviewResponse(request, scope, response) {
|
|
745
|
+
if (response == null || this.collectionSelection == null) {
|
|
746
|
+
return response;
|
|
747
|
+
}
|
|
748
|
+
const headers = new Headers(response.headers);
|
|
749
|
+
if (!isSelectedByUrl(resolveCollection(request, this.collectionSelection), scope.collectionName)) {
|
|
750
|
+
const cacheControl = headers.get('Cache-Control');
|
|
751
|
+
if (cacheControl == null || cacheControl === '') {
|
|
752
|
+
headers.set('Cache-Control', 'private');
|
|
753
|
+
}
|
|
754
|
+
else if (!/\b(private|no-store)\b/i.test(cacheControl)) {
|
|
755
|
+
headers.set('Cache-Control', `private, ${cacheControl}`);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
if (this.collectionSelection.noindex !== false) {
|
|
759
|
+
headers.set('X-Robots-Tag', 'noindex');
|
|
760
|
+
}
|
|
761
|
+
return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
|
|
762
|
+
}
|
|
763
|
+
// Make the scope of the request for the collection the current scope.
|
|
764
|
+
// collectionName is as in ServeRequestOptions.
|
|
765
|
+
beginRequestFor(request, collectionName) {
|
|
766
|
+
const name = collectionName === undefined ?
|
|
767
|
+
this.activeCollectionName :
|
|
768
|
+
collectionName ?? this.defaultCollectionName;
|
|
769
|
+
this.currentScope = this.scopeForRequest(request, name);
|
|
770
|
+
return this.currentScope;
|
|
771
|
+
}
|
|
772
|
+
async serveRequestFromStorage(scope, request, pathname, fallback) {
|
|
773
|
+
const serverConfig = await this.getServerConfigFor(scope);
|
|
774
|
+
if (serverConfig == null && scope.collectionName === this.defaultCollectionName) {
|
|
549
775
|
return new Response(`Settings not found. You may need to publish your application.`, {
|
|
550
776
|
status: 500,
|
|
551
777
|
headers: {
|
|
@@ -553,37 +779,50 @@ export class PublisherServer {
|
|
|
553
779
|
},
|
|
554
780
|
});
|
|
555
781
|
}
|
|
556
|
-
|
|
782
|
+
// A collection other than the default collection that is not published, or
|
|
783
|
+
// that expired, gets a 404. It can be published at any time, so the
|
|
784
|
+
// response cache does not store this response.
|
|
785
|
+
if (serverConfig == null ||
|
|
786
|
+
(scope.collectionName !== this.defaultCollectionName && await this.getAssetEntryMapFor(scope) == null)) {
|
|
787
|
+
scope.doNotStoreResponse = true;
|
|
788
|
+
return collectionNotAvailableResponse(scope.collectionName);
|
|
789
|
+
}
|
|
790
|
+
const asset = await this.getMatchingAssetFor(scope, serverConfig.publicDirPrefix + pathname, true, false);
|
|
557
791
|
if (asset != null) {
|
|
558
|
-
return this.
|
|
559
|
-
cache: await this.
|
|
792
|
+
return this.serveAssetFor(scope, request, asset, {
|
|
793
|
+
cache: await this.testExtendedCacheFor(scope, pathname) ? 'extended' : null,
|
|
560
794
|
});
|
|
561
795
|
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
796
|
+
if (fallback) {
|
|
797
|
+
return this.serveFallbackFromStorage(scope, request, serverConfig);
|
|
798
|
+
}
|
|
799
|
+
return null;
|
|
800
|
+
}
|
|
801
|
+
// fallback HTML responses, like SPA and "not found" pages
|
|
802
|
+
async serveFallbackFromStorage(scope, request, serverConfig) {
|
|
803
|
+
const assetEntryMap = await this.getAssetEntryMapFor(scope);
|
|
804
|
+
if (assetEntryMap == null) {
|
|
805
|
+
return null;
|
|
806
|
+
}
|
|
807
|
+
// These are raw asset paths, not relative to public path. The SPA file and
|
|
808
|
+
// the 404 page can be hidden files.
|
|
809
|
+
const { spaFile } = serverConfig;
|
|
810
|
+
if (spaFile != null) {
|
|
811
|
+
const asset = findAssetEntry(assetEntryMap, spaFile, true);
|
|
812
|
+
if (asset != null) {
|
|
813
|
+
return this.serveAssetFor(scope, request, asset, {
|
|
814
|
+
cache: 'never',
|
|
815
|
+
});
|
|
577
816
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
}
|
|
817
|
+
}
|
|
818
|
+
const { notFoundPageFile } = serverConfig;
|
|
819
|
+
if (notFoundPageFile != null) {
|
|
820
|
+
const asset = findAssetEntry(assetEntryMap, notFoundPageFile, true);
|
|
821
|
+
if (asset != null) {
|
|
822
|
+
return this.serveAssetFor(scope, request, asset, {
|
|
823
|
+
status: 404,
|
|
824
|
+
cache: 'never',
|
|
825
|
+
});
|
|
587
826
|
}
|
|
588
827
|
}
|
|
589
828
|
return null;
|