@fastly/compute-js-static-publish 8.1.0-beta.4 → 8.1.0-beta.6

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.
Files changed (33) hide show
  1. package/CHANGELOG.md +9 -6
  2. package/DOCUMENTATION.md +105 -5
  3. package/MIGRATING.md +289 -0
  4. package/build/cli/cli/commands/manage/deploy.d.ts.map +1 -1
  5. package/build/cli/cli/commands/manage/deploy.js +5 -4
  6. package/build/cli/cli/commands/manage/index.d.ts +1 -0
  7. package/build/cli/cli/commands/manage/index.d.ts.map +1 -1
  8. package/build/cli/cli/commands/manage/index.js +13 -11
  9. package/build/cli/cli/commands/manage/status.d.ts.map +1 -1
  10. package/build/cli/cli/commands/manage/status.js +7 -5
  11. package/build/cli/cli/index.js +15 -1
  12. package/build/cli/cli/managed/fastly-service.d.ts +7 -0
  13. package/build/cli/cli/managed/fastly-service.d.ts.map +1 -1
  14. package/build/cli/cli/managed/fastly-service.js +31 -1
  15. package/build/cli/cli/managed/fingerprint.d.ts +12 -0
  16. package/build/cli/cli/managed/fingerprint.d.ts.map +1 -1
  17. package/build/cli/cli/managed/fingerprint.js +25 -0
  18. package/build/cli/cli/util/config.d.ts +4 -0
  19. package/build/cli/cli/util/config.d.ts.map +1 -1
  20. package/build/cli/cli/util/config.js +33 -0
  21. package/build/cli/cli/util/output.js +1 -1
  22. package/build/server/hono/extensions.d.ts +3 -1
  23. package/build/server/hono/extensions.d.ts.map +1 -1
  24. package/build/server/hono/index.d.ts +11 -4
  25. package/build/server/hono/index.d.ts.map +1 -1
  26. package/build/server/hono/index.js +45 -18
  27. package/build/server/server/index.d.ts +1 -1
  28. package/build/server/server/index.d.ts.map +1 -1
  29. package/build/server/server/publisher-server/index.d.ts +7 -1
  30. package/build/server/server/publisher-server/index.d.ts.map +1 -1
  31. package/build/server/server/publisher-server/index.js +56 -14
  32. package/package.json +3 -2
  33. package/resources/templates/hono/src/index.ts +14 -3
@@ -35,6 +35,43 @@ export function buildHeadersSubset(responseHeaders, keys) {
35
35
  // * Content-Location, Date, ETag, and Vary
36
36
  // * Cache-Control and Expires
37
37
  const headersToPreserveForUnmodified = ['Content-Location', 'ETag', 'Vary', 'Cache-Control', 'Expires'];
38
+ // RFC 9110 evaluates the preconditions only for a response that would be 2xx.
39
+ function isSuccessStatus(status) {
40
+ return status >= 200 && status < 300;
41
+ }
42
+ const PRECONDITION_HEADERS = ['If-Match', 'If-None-Match', 'If-Modified-Since', 'If-Unmodified-Since', 'If-Range'];
43
+ // The request without its precondition headers, for a response with a status
44
+ // other than 2xx.
45
+ function requestForStatus(request, status) {
46
+ if (status == null || isSuccessStatus(status)) {
47
+ return request;
48
+ }
49
+ const headers = new Headers(request.headers);
50
+ for (const name of PRECONDITION_HEADERS) {
51
+ headers.delete(name);
52
+ }
53
+ return new Request(request.url, { method: request.method, headers });
54
+ }
55
+ // Apply the overrides to the response of a file. The response of a file always
56
+ // has an ETag.
57
+ function applyResponseOverrides(response, overrides) {
58
+ if (response == null ||
59
+ (overrides.status == null && overrides.headers == null) ||
60
+ response.status === 304 ||
61
+ !response.headers.has('ETag')) {
62
+ return response;
63
+ }
64
+ const headers = new Headers(response.headers);
65
+ new Headers(overrides.headers).forEach((value, name) => {
66
+ headers.set(name, value);
67
+ });
68
+ const status = overrides.status ?? response.status;
69
+ return new Response(response.body, {
70
+ status,
71
+ statusText: status === response.status ? response.statusText : '',
72
+ headers,
73
+ });
74
+ }
38
75
  // The response for a collection other than the default collection that is not
39
76
  // published, or that expired. It can be published at any time, so no cache
40
77
  // stores this response.
@@ -499,16 +536,19 @@ export class PublisherServer {
499
536
  if (scope.serverTiming != null) {
500
537
  headers.append('Server-Timing', scope.serverTiming.toHeaderValue());
501
538
  }
502
- const preconditionResponse = this.handlePreconditions(request, asset, headers);
503
- if (preconditionResponse != null) {
504
- return preconditionResponse;
539
+ const status = init?.status ?? 200;
540
+ if (isSuccessStatus(status)) {
541
+ const preconditionResponse = this.handlePreconditions(request, asset, headers);
542
+ if (preconditionResponse != null) {
543
+ return preconditionResponse;
544
+ }
505
545
  }
506
546
  // If the body does not have the size in its metadata, the response must not
507
547
  // look complete. An empty body throws here, and any other mismatch causes an
508
548
  // error in the body stream, so that the client gets an incomplete response.
509
549
  const body = await checkBodyLength(assetVariant.storageEntry.body, assetVariant.size, `File ${asset.key} (${assetVariant.contentEncoding ?? 'identity'})`);
510
550
  return new Response(body, {
511
- status: init?.status ?? 200,
551
+ status,
512
552
  headers,
513
553
  });
514
554
  }
@@ -631,9 +671,11 @@ export class PublisherServer {
631
671
  const lastModified = headers.get('Last-Modified');
632
672
  const lastModifiedMs = lastModified != null ? Date.parse(lastModified) : NaN;
633
673
  const lastModifiedTime = Number.isNaN(lastModifiedMs) ? 0 : Math.floor(lastModifiedMs / 1000);
634
- const preconditionResponse = this.handlePreconditionsForLastModified(request, lastModifiedTime, headers);
635
- if (preconditionResponse != null) {
636
- return preconditionResponse;
674
+ if (isSuccessStatus(metadata.status)) {
675
+ const preconditionResponse = this.handlePreconditionsForLastModified(request, lastModifiedTime, headers);
676
+ if (preconditionResponse != null) {
677
+ return preconditionResponse;
678
+ }
637
679
  }
638
680
  return new Response(request.method === 'HEAD' ? null : body, {
639
681
  status: metadata.status,
@@ -663,7 +705,7 @@ export class PublisherServer {
663
705
  if (options.healthCheck !== false && pathname === '/healthz') {
664
706
  return new Response("OK", { status: 200 });
665
707
  }
666
- const selected = await this.checkSelectedCollection(request, scope);
708
+ const selected = await this.checkSelectedCollection(request, scope, options.onMissing);
667
709
  if (selected instanceof Response) {
668
710
  return selected;
669
711
  }
@@ -672,7 +714,7 @@ export class PublisherServer {
672
714
  // With fallback: false, the response does not depend on HTML, so it has its own cache key.
673
715
  const fallback = options.fallback !== false && requestAcceptsTextHtml(request);
674
716
  const keyPath = fallback ? pathname : `${pathname}|no-html`;
675
- const response = await this.serveCachedFor(scope, request, keyPath, (fillRequest) => this.serveRequestFromStorage(scope, fillRequest, pathname, fallback));
717
+ const response = applyResponseOverrides(await this.serveCachedFor(scope, requestForStatus(request, options.status), keyPath, (fillRequest) => this.serveRequestFromStorage(scope, fillRequest, pathname, fallback)), options);
676
718
  return selected.preview ? this.markPreviewResponse(request, selected.scope, response) : response;
677
719
  }
678
720
  // Serve the SPA file or the 404 page, as serveRequest() does when no file
@@ -688,20 +730,20 @@ export class PublisherServer {
688
730
  if (!requestAcceptsTextHtml(request)) {
689
731
  return null;
690
732
  }
691
- const selected = await this.checkSelectedCollection(request, scope);
733
+ const selected = await this.checkSelectedCollection(request, scope, options.onMissing);
692
734
  if (selected instanceof Response) {
693
735
  return selected;
694
736
  }
695
737
  scope = selected.scope;
696
738
  // The fallback response is the same for all paths, so it has one cache key.
697
739
  // 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) => {
740
+ const response = applyResponseOverrides(await this.serveCachedFor(scope, requestForStatus(request, options.status), '|fallback', async (fillRequest) => {
699
741
  const serverConfig = await this.getServerConfigFor(scope);
700
742
  if (serverConfig == null) {
701
743
  return null;
702
744
  }
703
745
  return this.serveFallbackFromStorage(scope, fillRequest, serverConfig);
704
- });
746
+ }), options);
705
747
  return selected.preview ? this.markPreviewResponse(request, selected.scope, response) : response;
706
748
  }
707
749
  // Returns true if the collection is published, has not expired, and can
@@ -717,7 +759,7 @@ export class PublisherServer {
717
759
  // whether to mark the response as a preview, or a 404 response for onMissing:
718
760
  // 'not-found'. The check reads the index, also when the response cache has the
719
761
  // response.
720
- async checkSelectedCollection(request, scope) {
762
+ async checkSelectedCollection(request, scope, onMissing) {
721
763
  const config = this.collectionSelection;
722
764
  if (config == null || scope.collectionName === this.defaultCollectionName) {
723
765
  return { scope, preview: false };
@@ -725,7 +767,7 @@ export class PublisherServer {
725
767
  if (await this.isCollectionAvailableFor(scope)) {
726
768
  return { scope, preview: true };
727
769
  }
728
- if (config.onMissing === 'default') {
770
+ if ((onMissing ?? config.onMissing) === 'default') {
729
771
  this.currentScope = this.scopeForRequest(request, this.defaultCollectionName);
730
772
  return { scope: this.currentScope, preview: false };
731
773
  }
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.1.0-beta.4",
4
+ "version": "8.1.0-beta.6",
5
5
  "description": "Static Publisher for Fastly Compute JavaScript",
6
6
  "main": "build/server/server/index.js",
7
7
  "exports": {
@@ -74,7 +74,7 @@
74
74
  "@types/nunjucks": "^3.2.6",
75
75
  "esbuild": "^0.28.2",
76
76
  "hono": "^4.13.14",
77
- "rimraf": "^4.3.0",
77
+ "rimraf": "^6.1.4",
78
78
  "typescript": "^7.0.2"
79
79
  },
80
80
  "engines": {
@@ -88,6 +88,7 @@
88
88
  "README.md",
89
89
  "DOCUMENTATION.md",
90
90
  "CHANGELOG.md",
91
+ "MIGRATING.md",
91
92
  "SECURITY.md"
92
93
  ]
93
94
  }
@@ -16,7 +16,18 @@ const honoOptions = rc.templateOptions?.hono;
16
16
  // with two different resources stops the build.
17
17
  const fire = buildFire(mergeBindings(honoOptions?.bindings, extensions) as BindingsDefs);
18
18
 
19
- const { serveStatic, serveFallback, selectCollection } = fromStaticPublishRc(rc);
19
+ const {
20
+ serveStatic,
21
+ staticResponse,
22
+ serveFallback,
23
+ fallbackResponse,
24
+ isCollectionAvailable,
25
+ selectCollection,
26
+ publisherServer,
27
+ } = fromStaticPublishRc(rc);
28
+
29
+ // The helpers of the app that each phase of the extensions gets.
30
+ const helpers = { serveStatic, staticResponse, serveFallback, fallbackResponse, isCollectionAvailable, publisherServer };
20
31
 
21
32
  const app = new Hono<{ Bindings: typeof fire.Bindings, Variables: StaticPublisherVariables }>();
22
33
 
@@ -30,7 +41,7 @@ app.use('*', selectCollection());
30
41
  // them, then the beforeFiles phase.
31
42
  for (const phase of [ 'middleware', 'beforeFiles' ] as const) {
32
43
  for (const e of extensions) {
33
- e.module.phases[phase]?.(app, honoOptions?.extensions?.[e.name]);
44
+ e.module.phases[phase]?.(app, honoOptions?.extensions?.[e.name], helpers);
34
45
  }
35
46
  }
36
47
 
@@ -43,7 +54,7 @@ app.use('*', serveStatic());
43
54
  // The afterFiles phase of the extensions: only for the requests that no
44
55
  // published file matches.
45
56
  for (const e of extensions) {
46
- e.module.phases.afterFiles?.(app, honoOptions?.extensions?.[e.name]);
57
+ e.module.phases.afterFiles?.(app, honoOptions?.extensions?.[e.name], helpers);
47
58
  }
48
59
 
49
60
  // When nothing matches, serve the SPA file or the 404 page.