@fastly/compute-js-static-publish 2.3.0-alpha.1 → 2.3.0-alpha.4

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.
@@ -1 +1 @@
1
- {"version":3,"file":"build-static.d.ts","sourceRoot":"","sources":["../src/build-static.ts"],"names":[],"mappings":"AAcA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAchD,wBAAsB,iBAAiB,CAAC,iBAAiB,EAAE,eAAe,CAAC,kBAAkB,iBAsN5F"}
1
+ {"version":3,"file":"build-static.d.ts","sourceRoot":"","sources":["../src/build-static.ts"],"names":[],"mappings":"AAcA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAchD,wBAAsB,iBAAiB,CAAC,iBAAiB,EAAE,eAAe,CAAC,kBAAkB,iBAwN5F"}
@@ -132,6 +132,7 @@ export async function buildStaticLoader(commandLineValues) {
132
132
  }
133
133
  knownAssets[filePath] = { contentType: type, isStatic, loadModule, };
134
134
  }
135
+ fileContents += 'import { StaticAssets } from "@fastly/compute-js-static-publish";\n';
135
136
  fileContents += `\nexport const assets = {\n`;
136
137
  for (const [index, file] of files.entries()) {
137
138
  const contentDef = defaultContentTypes.testFileContentType(finalContentTypes, file);
package/build/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { Asset, AssetsMap } from './types.js';
1
+ export type { Asset, AssetsMap } from './types.js';
2
2
  export { StaticAssets } from './static-assets.js';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
@@ -1,9 +1,10 @@
1
1
  /// <reference types="@fastly/js-compute" />
2
- import { AssetsMap } from "./types.js";
2
+ import { Asset, AssetsMap } from "./types.js";
3
3
  export declare class StaticAssets {
4
4
  readonly assetsMap: AssetsMap;
5
5
  constructor(assetsMap: AssetsMap);
6
- getAsset(key: string): import("./types.js").Asset;
7
- serveAsset(event: FetchEvent, pathPrefix?: string): Response | null;
6
+ getAsset(key: string): Asset;
7
+ serveAsset(asset: Asset): Response;
8
+ serveAssetForEvent(event: FetchEvent, pathPrefix?: string): Response | null;
8
9
  }
9
10
  //# sourceMappingURL=static-assets.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"static-assets.d.ts","sourceRoot":"","sources":["../src/static-assets.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,qBAAa,YAAY;IACvB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;gBAElB,SAAS,EAAE,SAAS;IAIzB,QAAQ,CAAC,GAAG,EAAE,MAAM;IAIpB,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,UAAU,GAAE,MAAW;CAqB7D"}
1
+ {"version":3,"file":"static-assets.d.ts","sourceRoot":"","sources":["../src/static-assets.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE9C,qBAAa,YAAY;IACvB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;gBAElB,SAAS,EAAE,SAAS;IAIzB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK;IAI5B,UAAU,CAAC,KAAK,EAAE,KAAK;IAcvB,kBAAkB,CAAC,KAAK,EAAE,UAAU,EAAE,UAAU,GAAE,MAAW;CAWrE"}
@@ -5,13 +5,7 @@ export class StaticAssets {
5
5
  getAsset(key) {
6
6
  return this.assetsMap[key];
7
7
  }
8
- serveAsset(event, pathPrefix = '') {
9
- const { request } = event;
10
- const { pathname } = new URL(request.url);
11
- const asset = this.getAsset(`${pathPrefix}${pathname}`);
12
- if (!asset) {
13
- return null;
14
- }
8
+ serveAsset(asset) {
15
9
  // Aggressive caching for static files, and no caching for everything else.
16
10
  const headers = {
17
11
  'Cache-Control': asset.isStatic ? 'max-age=31536000' : 'no-cache',
@@ -24,4 +18,13 @@ export class StaticAssets {
24
18
  headers,
25
19
  });
26
20
  }
21
+ serveAssetForEvent(event, pathPrefix = '') {
22
+ const { request } = event;
23
+ const { pathname } = new URL(request.url);
24
+ const asset = this.getAsset(`${pathPrefix}${pathname}`);
25
+ if (!asset) {
26
+ return null;
27
+ }
28
+ return this.serveAsset(asset);
29
+ }
27
30
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fastly/compute-js-static-publish",
3
3
  "type": "module",
4
- "version": "2.3.0-alpha.1",
4
+ "version": "2.3.0-alpha.4",
5
5
  "description": "Static Publisher for Compute@Edge JavaScript",
6
6
  "main": "./build/index.js",
7
7
  "types": "./build/index.d.ts",
@@ -1,7 +1,7 @@
1
1
  /// <reference types="@fastly/js-compute" />
2
2
 
3
3
  import { Router } from '@fastly/expressly';
4
- import { assets, spaFile, notFoundPageFile, autoIndex, autoExt } from './statics';
4
+ import { staticAssets, spaFile, notFoundPageFile, autoIndex, autoExt } from './statics';
5
5
 
6
6
  const router = new Router();
7
7
 
@@ -10,7 +10,7 @@ function getMatchingRequestPath(path) {
10
10
  // match exactly one of the assets
11
11
  if(!path.endsWith('/')) {
12
12
 
13
- if(path in assets) {
13
+ if (staticAssets.getAsset(path) != null) {
14
14
  return path;
15
15
  }
16
16
 
@@ -18,7 +18,7 @@ function getMatchingRequestPath(path) {
18
18
  if(autoExt != null) {
19
19
  for (const extEntry of autoExt) {
20
20
  let pathWithExt = path + extEntry;
21
- if(pathWithExt in assets) {
21
+ if (staticAssets.getAsset(pathWithExt) != null) {
22
22
  return pathWithExt;
23
23
  }
24
24
  }
@@ -32,7 +32,7 @@ function getMatchingRequestPath(path) {
32
32
  if(autoIndex != null) {
33
33
  for (const indexEntry of autoIndex) {
34
34
  let indexPath = path + indexEntry;
35
- if(indexPath in assets) {
35
+ if (staticAssets.getAsset(indexPath) != null) {
36
36
  return indexPath;
37
37
  }
38
38
  }
@@ -53,23 +53,13 @@ function requestAcceptsTextHtml(req) {
53
53
 
54
54
  router.get("*", (req, res) => {
55
55
  const assetPath = getMatchingRequestPath(req.urlObj.pathname);
56
- if(assetPath == null) {
56
+ const asset = staticAssets.getAsset(assetPath);
57
+ if(asset == null) {
57
58
  return;
58
59
  }
59
- const staticFile = assets[assetPath];
60
-
61
- // Aggressive caching for static files, and no caching for everything else.
62
- // https://create-react-app.dev/docs/production-build/#static-file-caching
63
- const headers = {
64
- 'Cache-Control': staticFile.isStatic ? 'max-age=31536000' : 'no-cache',
65
- };
66
- if(staticFile.contentType != null) {
67
- headers['Content-Type'] = staticFile.contentType;
68
- }
69
- res.send(new Response(staticFile.content, {
70
- status: 200,
71
- headers,
72
- }));
60
+
61
+ const response = staticAssets.serveAsset(asset);
62
+ res.send(response);
73
63
  });
74
64
 
75
65
  // TODO: If you need to handle any API routes, add them here.
@@ -85,28 +75,35 @@ router.get("*", (req, res) => {
85
75
  if(!requestAcceptsTextHtml(req)) {
86
76
  return;
87
77
  }
78
+ const asset = staticAssets.getAsset(spaFile);
79
+ if(asset == null) {
80
+ return;
81
+ }
88
82
 
89
- const staticFile = assets[spaFile];
90
- res.send(new Response(staticFile.content, {
83
+ const response = new Response(asset.content, {
91
84
  status: 200,
92
85
  headers: {
93
86
  'Cache-Control': 'no-cache',
94
87
  'Content-Type': 'text/html',
95
88
  }
96
- }));
89
+ });
90
+ res.send(response);
97
91
  });
98
92
 
99
93
  router.all("*", (req, res) => {
100
94
  if(notFoundPageFile && requestAcceptsTextHtml(req)) {
101
- const staticFile = assets[notFoundPageFile];
102
- res.send(new Response(staticFile.content, {
103
- status: 404,
104
- headers: {
105
- 'Cache-Control': 'no-cache',
106
- 'Content-Type': 'text/html',
107
- }
108
- }));
109
- return;
95
+ const asset = staticAssets.getAsset(notFoundPageFile);
96
+ if(asset != null) {
97
+ const response = new Response(asset.content, {
98
+ status: 404,
99
+ headers: {
100
+ 'Cache-Control': 'no-cache',
101
+ 'Content-Type': 'text/html',
102
+ }
103
+ });
104
+ res.send(response);
105
+ return;
106
+ }
110
107
  }
111
108
 
112
109
  res.send(new Response("404 Not Found", {