@fastly/compute-js-static-publish 2.3.0-alpha.2 → 2.3.0-alpha.5

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,iBAwN5F"}
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,iBAuN5F"}
@@ -21,7 +21,7 @@ function getFiles(results, dir) {
21
21
  }
22
22
  }
23
23
  export async function buildStaticLoader(commandLineValues) {
24
- var _a, _b, _c, _d, _e, _f, _g, _h;
24
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
25
25
  const { 'suppress-framework-warnings': suppressFrameworkWarnings } = commandLineValues;
26
26
  const displayFrameworkWarnings = !suppressFrameworkWarnings;
27
27
  console.log("🚀 Building loader...");
@@ -37,12 +37,12 @@ export async function buildStaticLoader(commandLineValues) {
37
37
  process.exitCode = 1;
38
38
  return;
39
39
  }
40
- const results = [];
41
- getFiles(results, config.publicDir);
42
40
  const outputDir = path.resolve();
43
- const publicDirRoot = path.resolve(config.publicDir);
41
+ const publicDirRoot = path.resolve((_a = config.publicDir) !== null && _a !== void 0 ? _a : './');
42
+ const results = [];
43
+ getFiles(results, publicDirRoot);
44
44
  console.log(`✔️ Public directory '${publicDirRoot}'.`);
45
- const staticDirs = (_a = config.staticDirs) !== null && _a !== void 0 ? _a : [];
45
+ const staticDirs = (_b = config.staticDirs) !== null && _b !== void 0 ? _b : [];
46
46
  if (staticDirs.length > 0) {
47
47
  console.log(`✔️ Using static directories: ${staticDirs.join(', ')}`);
48
48
  }
@@ -51,11 +51,11 @@ export async function buildStaticLoader(commandLineValues) {
51
51
  console.log(`✔️ No static directories defined.`);
52
52
  }
53
53
  }
54
- const staticRoots = staticDirs.map(dir => path.resolve(config.publicDir, dir));
54
+ const staticRoots = staticDirs.map(dir => path.resolve(publicDirRoot, dir));
55
55
  const DEFAULT_EXCLUDE_DIRS = [
56
56
  './node_modules'
57
57
  ];
58
- const excludeDirs = (_b = config.excludeDirs) !== null && _b !== void 0 ? _b : DEFAULT_EXCLUDE_DIRS;
58
+ const excludeDirs = (_c = config.excludeDirs) !== null && _c !== void 0 ? _c : DEFAULT_EXCLUDE_DIRS;
59
59
  if (excludeDirs.length > 0) {
60
60
  console.log(`✔️ Using exclude directories: ${excludeDirs.join(', ')}`);
61
61
  }
@@ -64,17 +64,17 @@ export async function buildStaticLoader(commandLineValues) {
64
64
  console.log(`✔️ No exclude directories defined.`);
65
65
  }
66
66
  }
67
- const excludeRoots = excludeDirs.map(dir => path.resolve(config.publicDir, dir));
67
+ const excludeRoots = excludeDirs.map(dir => path.resolve(publicDirRoot, dir));
68
68
  // Load defaultContentTypes module
69
69
  const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
70
70
  const defaultContentTypesJsSrcPath = path.resolve(__dirname, '../resources/default-content-types.cjs');
71
71
  const defaultContentTypes = await import(defaultContentTypesJsSrcPath);
72
72
  // Load content types
73
- const finalContentTypes = defaultContentTypes.mergeContentTypes((_c = config.contentTypes) !== null && _c !== void 0 ? _c : []);
73
+ const finalContentTypes = defaultContentTypes.mergeContentTypes((_d = config.contentTypes) !== null && _d !== void 0 ? _d : []);
74
74
  const DEFAULT_INCLUDE_DIRS = [
75
75
  './.well-known'
76
76
  ];
77
- const includeDirs = (_d = config.includeDirs) !== null && _d !== void 0 ? _d : DEFAULT_INCLUDE_DIRS;
77
+ const includeDirs = (_e = config.includeDirs) !== null && _e !== void 0 ? _e : DEFAULT_INCLUDE_DIRS;
78
78
  if (includeDirs.length > 0) {
79
79
  console.log(`✔️ Using include directories: ${includeDirs.join(', ')}`);
80
80
  }
@@ -83,12 +83,12 @@ export async function buildStaticLoader(commandLineValues) {
83
83
  console.log(`✔️ No include directories defined.`);
84
84
  }
85
85
  }
86
- const includeRoots = includeDirs.map(dir => path.resolve(config.publicDir, dir));
86
+ const includeRoots = includeDirs.map(dir => path.resolve(publicDirRoot, dir));
87
87
  const excludeTest = config.excludeTest;
88
88
  const files = results
89
89
  .filter(file => {
90
90
  // Exclude files that come from C@E app dir
91
- if (file.startsWith(outputDir)) {
91
+ if (outputDir != publicDirRoot && file.startsWith(outputDir)) {
92
92
  return false;
93
93
  }
94
94
  // Include files that come from "included roots" dir
@@ -163,7 +163,7 @@ export async function buildStaticLoader(commandLineValues) {
163
163
  fileContents += ` ${JSON.stringify(filePath)}: { contentType: ${JSON.stringify(type)}, content: ${content}, module: ${module}, isStatic: ${JSON.stringify(isStatic)} },\n`;
164
164
  }
165
165
  fileContents += '};\n';
166
- let spaFile = (_e = config.spa) !== null && _e !== void 0 ? _e : false;
166
+ let spaFile = (_f = config.spa) !== null && _f !== void 0 ? _f : false;
167
167
  if (spaFile) {
168
168
  console.log(`✔️ Application SPA file '${spaFile}'.`);
169
169
  if (!knownAssets[spaFile] || knownAssets[spaFile].contentType !== 'text/html') {
@@ -179,7 +179,7 @@ export async function buildStaticLoader(commandLineValues) {
179
179
  }
180
180
  }
181
181
  fileContents += `\nexport const spaFile = ${JSON.stringify(spaFile)};\n`;
182
- let notFoundPageFile = (_f = config.notFoundPage) !== null && _f !== void 0 ? _f : false;
182
+ let notFoundPageFile = (_g = config.notFoundPage) !== null && _g !== void 0 ? _g : false;
183
183
  if (notFoundPageFile) {
184
184
  console.log(`✔️ Application 'not found (404)' file '${notFoundPageFile}'.`);
185
185
  if (!knownAssets[notFoundPageFile] || knownAssets[notFoundPageFile].contentType !== 'text/html') {
@@ -195,9 +195,9 @@ export async function buildStaticLoader(commandLineValues) {
195
195
  }
196
196
  }
197
197
  fileContents += `\nexport const notFoundPageFile = ${JSON.stringify(notFoundPageFile)};\n`;
198
- let autoIndex = (_g = config.autoIndex) !== null && _g !== void 0 ? _g : null;
198
+ let autoIndex = (_h = config.autoIndex) !== null && _h !== void 0 ? _h : null;
199
199
  fileContents += `\nexport const autoIndex = ${JSON.stringify(autoIndex)};\n`;
200
- let autoExt = (_h = config.autoExt) !== null && _h !== void 0 ? _h : null;
200
+ let autoExt = (_j = config.autoExt) !== null && _j !== void 0 ? _j : null;
201
201
  fileContents += `\nexport const autoExt = ${JSON.stringify(autoExt)};\n`;
202
202
  fileContents += '\nexport const staticAssets = new StaticAssets(assets);\n';
203
203
  fs.writeFileSync('./src/statics.js', fileContents);
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.2",
4
+ "version": "2.3.0-alpha.5",
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", {