@fastly/compute-js-static-publish 8.0.0-beta.13 → 8.0.0-beta.14

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.
@@ -3,6 +3,7 @@ import { type PublisherServerConfigNormalized } from '../../models/config/publis
3
3
  import { type ContentCompressionTypes } from '../../models/compression/index.js';
4
4
  import { type AssetEntry, type AssetEntryMap, type AssetVariantMetadata } from '../../models/assets/index.js';
5
5
  import { type StorageEntry, type StorageProvider } from '../storage/storage-provider.js';
6
+ import { ServerTiming } from '../util/server-timing.js';
6
7
  type AssetVariant = {
7
8
  storageEntry: StorageEntry;
8
9
  } & AssetVariantMetadata;
@@ -20,10 +21,15 @@ export declare class PublisherServer {
20
21
  defaultCollectionName: string;
21
22
  activeCollectionName: string;
22
23
  collectionNameHeader: string | null;
24
+ serverTimingRequestHeader: string | null;
25
+ serverTiming: ServerTiming | null;
23
26
  settingsCached: PublisherServerConfigNormalized | null | undefined;
24
27
  assetEntryMapCache: AssetEntryMap | null | undefined;
25
28
  setActiveCollectionName(collectionName: string): void;
26
29
  setCollectionNameHeader(collectionHeader: string | null): void;
30
+ setServerTimingRequestHeader(requestHeader: string | null): void;
31
+ beginRequest(request: Request): void;
32
+ private timed;
27
33
  getServerConfig(): Promise<PublisherServerConfigNormalized | null>;
28
34
  getStaticItems(): Promise<(string | RegExp)[]>;
29
35
  getAssetEntryMap(): Promise<AssetEntryMap | null>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/server/publisher-server/index.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,KAAK,eAAe,EACrB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EACL,KAAK,+BAA+B,EACrC,MAAM,gDAAgD,CAAC;AACxD,OAAO,EACL,KAAK,uBAAuB,EAC7B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EACL,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,oBAAoB,EA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EAAE,OAAO,GAAG,QAAQ,GAAG,IAAI;IA6DtF,gBAAgB,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,uBAAuB,GAAG,IAAI,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;YA8BzG,wCAAwC;IAyChD,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,QAAQ,CAAC;IAqDpF,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;CAuE/D"}
@@ -8,6 +8,7 @@ import { isExpired } from '../../models/time/index.js';
8
8
  import { loadStorageProviderFromStaticPublishRc, } from '../storage/storage-provider.js';
9
9
  import { checkIfModifiedSince, getIfModifiedSinceHeader } from './serve-preconditions/if-modified-since.js';
10
10
  import { checkIfNoneMatch, getIfNoneMatchHeader } from './serve-preconditions/if-none-match.js';
11
+ import { ServerTiming } from '../util/server-timing.js';
11
12
  export function buildHeadersSubset(responseHeaders, keys) {
12
13
  const resultHeaders = new Headers();
13
14
  for (const value of keys) {
@@ -42,6 +43,8 @@ export class PublisherServer {
42
43
  this.defaultCollectionName = defaultCollectionName;
43
44
  this.activeCollectionName = this.defaultCollectionName;
44
45
  this.collectionNameHeader = 'X-Publisher-Server-Collection';
46
+ this.serverTimingRequestHeader = null;
47
+ this.serverTiming = null;
45
48
  }
46
49
  static fromStaticPublishRc(config) {
47
50
  const storeProvider = loadStorageProviderFromStaticPublishRc(config);
@@ -55,21 +58,35 @@ export class PublisherServer {
55
58
  setCollectionNameHeader(collectionHeader) {
56
59
  this.collectionNameHeader = collectionHeader;
57
60
  }
61
+ // Set the request header that turns on the Server-Timing response header.
62
+ // null (the default) turns timing off.
63
+ setServerTimingRequestHeader(requestHeader) {
64
+ this.serverTimingRequestHeader = requestHeader;
65
+ }
66
+ // Start handling a new request. serveRequest() calls this. If you call
67
+ // getMatchingAsset() and serveAsset() directly, call this first.
68
+ beginRequest(request) {
69
+ const header = this.serverTimingRequestHeader;
70
+ this.serverTiming = header != null && request.headers.has(header) ? new ServerTiming() : null;
71
+ }
72
+ timed(name, fn) {
73
+ return this.serverTiming != null ? this.serverTiming.measure(name, fn) : fn();
74
+ }
58
75
  // Server config is obtained from storage, and cached for the duration of this object.
59
76
  async getServerConfig() {
60
77
  if (this.settingsCached !== undefined) {
61
78
  return this.settingsCached;
62
79
  }
63
80
  const settingsFileKey = `${this.publishId}_settings_${this.activeCollectionName}`;
64
- const settingsFile = await this.storageProvider.getEntry(settingsFileKey, [`${this.publishId}-${this.activeCollectionName}`, 'settings']);
65
- if (settingsFile == null) {
66
- console.error(`Settings File not found at ${settingsFileKey}.`);
67
- console.error(`You may need to publish your application.`);
68
- this.settingsCached = null;
69
- }
70
- else {
71
- this.settingsCached = (await settingsFile.json());
72
- }
81
+ this.settingsCached = await this.timed('settings', async () => {
82
+ const settingsFile = await this.storageProvider.getEntry(settingsFileKey, [`${this.publishId}-${this.activeCollectionName}`, 'settings']);
83
+ if (settingsFile == null) {
84
+ console.error(`Settings File not found at ${settingsFileKey}.`);
85
+ console.error(`You may need to publish your application.`);
86
+ return null;
87
+ }
88
+ return (await settingsFile.json());
89
+ });
73
90
  return this.settingsCached;
74
91
  }
75
92
  async getStaticItems() {
@@ -96,7 +113,7 @@ export class PublisherServer {
96
113
  return this.assetEntryMapCache;
97
114
  }
98
115
  const indexFileKey = `${this.publishId}_index_${this.activeCollectionName}`;
99
- const indexFile = await this.storageProvider.getEntry(indexFileKey, [`${this.publishId}-${this.activeCollectionName}`, 'index']);
116
+ const indexFile = await this.timed('index', () => this.storageProvider.getEntry(indexFileKey, [`${this.publishId}-${this.activeCollectionName}`, 'index']));
100
117
  if (indexFile == null) {
101
118
  console.error(`Index File not found at ${indexFileKey}.`);
102
119
  console.error(`You may need to publish your application.`);
@@ -124,7 +141,9 @@ export class PublisherServer {
124
141
  this.assetEntryMapCache = null;
125
142
  return null;
126
143
  }
127
- this.assetEntryMapCache = (await indexFile.json());
144
+ // Read and parse in two steps, so that Server-Timing can report each one.
145
+ const indexText = await this.timed('index-body', () => indexFile.text());
146
+ this.assetEntryMapCache = await this.timed('index-parse', async () => JSON.parse(indexText));
128
147
  return this.assetEntryMapCache;
129
148
  }
130
149
  async getMatchingAsset(assetKey, applyAuto = false) {
@@ -296,7 +315,7 @@ export class PublisherServer {
296
315
  const baseHash = entry.key.slice(7);
297
316
  const baseKey = `${this.publishId}_files_sha256_${baseHash}`;
298
317
  const variantKey = variant != null ? `${baseKey}_${variant}` : baseKey;
299
- const storageEntry = await this.storageProvider.getEntry(variantKey);
318
+ const storageEntry = await this.timed('asset', () => this.storageProvider.getEntry(variantKey));
300
319
  if (storageEntry == null) {
301
320
  return null;
302
321
  }
@@ -380,6 +399,9 @@ export class PublisherServer {
380
399
  if (asset.lastModifiedTime !== 0) {
381
400
  headers.set('Last-Modified', (new Date(asset.lastModifiedTime * 1000)).toUTCString());
382
401
  }
402
+ if (this.serverTiming != null) {
403
+ headers.append('Server-Timing', this.serverTiming.toHeaderValue());
404
+ }
383
405
  const preconditionResponse = this.handlePreconditions(request, asset, headers);
384
406
  if (preconditionResponse != null) {
385
407
  return preconditionResponse;
@@ -391,6 +413,7 @@ export class PublisherServer {
391
413
  });
392
414
  }
393
415
  async serveRequest(request) {
416
+ this.beginRequest(request);
394
417
  // Only handle GET and HEAD
395
418
  if (request.method !== 'GET' && request.method !== 'HEAD') {
396
419
  return null;
@@ -0,0 +1,7 @@
1
+ export declare class ServerTiming {
2
+ private entries;
3
+ add(name: string, dur: number, desc?: string): void;
4
+ measure<T>(name: string, fn: () => Promise<T>, desc?: string): Promise<T>;
5
+ toHeaderValue(): string;
6
+ }
7
+ //# sourceMappingURL=server-timing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-timing.d.ts","sourceRoot":"","sources":["../../../../src/server/util/server-timing.ts"],"names":[],"mappings":"AAaA,qBAAa,YAAY;IACvB,OAAO,CAAC,OAAO,CAA2B;IAE1C,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM;IAItC,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;IAS/E,aAAa,IAAI,MAAM;CAWxB"}
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ // Collects durations for one request and formats them as a Server-Timing
6
+ // header value (https://www.w3.org/TR/server-timing/).
7
+ export class ServerTiming {
8
+ constructor() {
9
+ this.entries = [];
10
+ }
11
+ add(name, dur, desc) {
12
+ this.entries.push({ name, dur, desc });
13
+ }
14
+ async measure(name, fn, desc) {
15
+ const start = performance.now();
16
+ try {
17
+ return await fn();
18
+ }
19
+ finally {
20
+ this.add(name, performance.now() - start, desc);
21
+ }
22
+ }
23
+ toHeaderValue() {
24
+ return this.entries
25
+ .map(({ name, dur, desc }) => {
26
+ let value = `${name};dur=${dur.toFixed(1)}`;
27
+ if (desc != null) {
28
+ value += `;desc=${JSON.stringify(desc)}`;
29
+ }
30
+ return value;
31
+ })
32
+ .join(', ');
33
+ }
34
+ }
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.0-beta.13",
4
+ "version": "8.0.0-beta.14",
5
5
  "description": "Static Publisher for Fastly Compute JavaScript",
6
6
  "main": "build/index.js",
7
7
  "exports": {