@lwrjs/server 0.11.0-alpha.1 → 0.11.0-alpha.10

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.
@@ -10,6 +10,18 @@ __markAsModule(exports);
10
10
  __export(exports, {
11
11
  AbstractMiddlewareRequest: () => AbstractMiddlewareRequest
12
12
  });
13
+ function parseForwardedHeader(forwarded) {
14
+ if (!forwarded)
15
+ return void 0;
16
+ const couples = forwarded.toLocaleLowerCase().split(";");
17
+ const map = couples.reduce((m, c) => {
18
+ const [key, value] = c.trim().split("=");
19
+ m[key] = value;
20
+ return m;
21
+ }, {});
22
+ const proto = map.proto ? `${map.proto}://` : "";
23
+ return map.host ? `${proto}${map.host}` : void 0;
24
+ }
13
25
  var AbstractMiddlewareRequest = class {
14
26
  constructor(isSiteGenerationRequest) {
15
27
  this.isSiteGenerationRequest = isSiteGenerationRequest || false;
@@ -42,8 +54,16 @@ var AbstractMiddlewareRequest = class {
42
54
  const supportedEnvironments = config.environment.supported || [];
43
55
  return !targetEnvironment || targetEnvironment === defaultEnvironment || supportedEnvironments?.includes(targetEnvironment);
44
56
  }
45
- getRuntimeContext(defaultRuntimeEnvironment) {
46
- const {format, apiVersion, compat, locale, environment, bundleSpecifier} = this.params;
57
+ getRuntimeContext(defaultRuntimeEnvironment, i18n) {
58
+ const {
59
+ format,
60
+ apiVersion,
61
+ basePath: basePathParam,
62
+ compat,
63
+ locale: localeParam,
64
+ environment,
65
+ bundleSpecifier
66
+ } = this.params;
47
67
  const debug = this.query.debug !== void 0;
48
68
  const bundle = !!bundleSpecifier;
49
69
  const runtimeEnvironment = {
@@ -52,11 +72,24 @@ var AbstractMiddlewareRequest = class {
52
72
  compat: compat || defaultRuntimeEnvironment.compat,
53
73
  debug: debug || defaultRuntimeEnvironment.debug,
54
74
  apiVersion: apiVersion || defaultRuntimeEnvironment.apiVersion,
55
- bundle: bundle || defaultRuntimeEnvironment.bundle
75
+ bundle: bundle || defaultRuntimeEnvironment.bundle,
76
+ basePath: this.basePath || basePathParam || defaultRuntimeEnvironment.basePath
56
77
  };
78
+ let host = parseForwardedHeader(this.headers?.forwarded) || this.headers?.host || void 0;
79
+ if (host && !host.startsWith("http")) {
80
+ host = `${this.protocol}://${host}`;
81
+ }
82
+ const basePath = runtimeEnvironment.basePath;
83
+ const locale = localeParam || this.locale || defaultRuntimeEnvironment.defaultLocale;
84
+ const uiBasePath = i18n.uriPattern === "path-prefix" && i18n.defaultLocale !== locale ? `${basePath}/${locale}` : `${basePath}`;
85
+ const assetBasePath = `${runtimeEnvironment.basePath}${runtimeEnvironment.defaultAssetPath}`;
57
86
  const runtimeParams = {
58
- locale: locale || this.locale || defaultRuntimeEnvironment.defaultLocale,
59
- environment
87
+ basePath,
88
+ locale,
89
+ assetBasePath,
90
+ uiBasePath,
91
+ environment,
92
+ host
60
93
  };
61
94
  return {
62
95
  runtimeEnvironment,
@@ -56,6 +56,12 @@ var ExpressRequest = class extends import_abstract_request.AbstractMiddlewareReq
56
56
  get protocol() {
57
57
  return this.req.protocol;
58
58
  }
59
+ get basePath() {
60
+ return this.req.basePath || "";
61
+ }
62
+ set basePath(bp) {
63
+ this.req.basePath = bp;
64
+ }
59
65
  get locale() {
60
66
  return this.req.locale;
61
67
  }
@@ -28,7 +28,6 @@ __export(exports, {
28
28
  });
29
29
  var import_express = __toModule(require("express"));
30
30
  var import_http = __toModule(require("http"));
31
- var import_compression = __toModule(require("../common/compression.cjs"));
32
31
  var import_abstract_internal_app_server = __toModule(require("../common/abstract-internal-app-server.cjs"));
33
32
  var import_express_request = __toModule(require("./express-request.cjs"));
34
33
  var import_express_response = __toModule(require("./express-response.cjs"));
@@ -85,9 +84,6 @@ var ExpressInternalServer = class extends import_abstract_internal_app_server.de
85
84
  await middleware(lwrReq, lwrRes, next);
86
85
  });
87
86
  }
88
- useCompression() {
89
- this.expressApp.use((0, import_compression.default)());
90
- }
91
87
  getRegexWildcard() {
92
88
  return "*";
93
89
  }
@@ -97,8 +97,6 @@ var FsInternalServer = class extends import_abstract_internal_app_server.default
97
97
  this.middlewares.push(this.koaRouter.routes());
98
98
  this.lwrFsKoaApp.setMiddlewares(this.middlewares);
99
99
  }
100
- useCompression() {
101
- }
102
100
  getImpl() {
103
101
  return this.lwrFsKoaApp;
104
102
  }
@@ -56,6 +56,12 @@ var KoaRequest = class extends import_abstract_request.AbstractMiddlewareRequest
56
56
  get headers() {
57
57
  return this.ctx.headers;
58
58
  }
59
+ get basePath() {
60
+ return this.ctx.basePath || "";
61
+ }
62
+ set basePath(bp) {
63
+ this.ctx.basePath = bp;
64
+ }
59
65
  get locale() {
60
66
  return this.ctx.locale;
61
67
  }
@@ -29,7 +29,6 @@ __export(exports, {
29
29
  var import_koa = __toModule(require("koa"));
30
30
  var import_router = __toModule(require("@koa/router"));
31
31
  var import_http = __toModule(require("http"));
32
- var import_koa_compress = __toModule(require("koa-compress"));
33
32
  var import_abstract_internal_app_server = __toModule(require("../common/abstract-internal-app-server.cjs"));
34
33
  var import_koa_request = __toModule(require("./koa-request.cjs"));
35
34
  var import_koa_response = __toModule(require("./koa-response.cjs"));
@@ -78,9 +77,6 @@ var KoaInternalServer = class extends import_abstract_internal_app_server.defaul
78
77
  await middleware(lwrReq, lwrRes, next);
79
78
  });
80
79
  }
81
- useCompression() {
82
- this.koaApp.use((0, import_koa_compress.default)());
83
- }
84
80
  getRegexWildcard() {
85
81
  return "(.*)";
86
82
  }
@@ -1,4 +1,4 @@
1
- import type { RuntimeContext, RuntimeEnvironment, Headers, MiddlewareRequest, NormalizedLwrGlobalConfig } from '@lwrjs/types';
1
+ import type { RuntimeContext, RuntimeEnvironment, Headers, MiddlewareRequest, NormalizedLwrGlobalConfig, I18NConfig } from '@lwrjs/types';
2
2
  export declare abstract class AbstractMiddlewareRequest implements MiddlewareRequest {
3
3
  isSiteGenerationRequest: boolean;
4
4
  constructor(isSiteGenerationRequest?: boolean);
@@ -10,6 +10,7 @@ export declare abstract class AbstractMiddlewareRequest implements MiddlewareReq
10
10
  abstract params: any;
11
11
  abstract query: any;
12
12
  abstract locale: string;
13
+ abstract basePath: string;
13
14
  abstract headers: Headers;
14
15
  abstract acceptsLanguages(): string[];
15
16
  abstract get(field: string): string | undefined;
@@ -19,6 +20,6 @@ export declare abstract class AbstractMiddlewareRequest implements MiddlewareReq
19
20
  isJsonRequest(): boolean;
20
21
  validateJsonRequest(): boolean;
21
22
  validateEnvironmentRequest(config: NormalizedLwrGlobalConfig): boolean;
22
- getRuntimeContext(defaultRuntimeEnvironment: RuntimeEnvironment): RuntimeContext;
23
+ getRuntimeContext(defaultRuntimeEnvironment: RuntimeEnvironment, i18n: I18NConfig): RuntimeContext;
23
24
  }
24
25
  //# sourceMappingURL=abstract-request.d.ts.map
@@ -1,3 +1,19 @@
1
+ // See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
2
+ function parseForwardedHeader(forwarded) {
3
+ if (!forwarded)
4
+ return undefined;
5
+ // parse the Forwarded header to find the "host" and "proto"
6
+ // case-insensitive and can contain empty space around the key=value pairs
7
+ const couples = forwarded.toLocaleLowerCase().split(';');
8
+ const map = couples.reduce((m, c) => {
9
+ const [key, value] = c.trim().split('=');
10
+ m[key] = value;
11
+ return m;
12
+ }, {});
13
+ // build the protocol:host string
14
+ const proto = map.proto ? `${map.proto}://` : '';
15
+ return map.host ? `${proto}${map.host}` : undefined;
16
+ }
1
17
  export class AbstractMiddlewareRequest {
2
18
  constructor(isSiteGenerationRequest) {
3
19
  this.isSiteGenerationRequest = isSiteGenerationRequest || false;
@@ -35,8 +51,8 @@ export class AbstractMiddlewareRequest {
35
51
  targetEnvironment === defaultEnvironment ||
36
52
  supportedEnvironments?.includes(targetEnvironment));
37
53
  }
38
- getRuntimeContext(defaultRuntimeEnvironment) {
39
- const { format, apiVersion, compat, locale, environment, bundleSpecifier } = this.params;
54
+ getRuntimeContext(defaultRuntimeEnvironment, i18n) {
55
+ const { format, apiVersion, basePath: basePathParam, compat, locale: localeParam, environment, bundleSpecifier, } = this.params;
40
56
  const debug = this.query.debug !== undefined;
41
57
  const bundle = !!bundleSpecifier;
42
58
  const runtimeEnvironment = {
@@ -47,10 +63,28 @@ export class AbstractMiddlewareRequest {
47
63
  debug: debug || defaultRuntimeEnvironment.debug,
48
64
  apiVersion: apiVersion || defaultRuntimeEnvironment.apiVersion,
49
65
  bundle: bundle || defaultRuntimeEnvironment.bundle,
66
+ basePath: this.basePath || basePathParam || defaultRuntimeEnvironment.basePath,
50
67
  };
68
+ let host = parseForwardedHeader(this.headers?.forwarded) || this.headers?.host || undefined;
69
+ if (host && !host.startsWith('http')) {
70
+ host = `${this.protocol}://${host}`;
71
+ }
72
+ const basePath = runtimeEnvironment.basePath;
73
+ const locale = localeParam || this.locale || defaultRuntimeEnvironment.defaultLocale;
74
+ // Base path to be used in routing ${basePath}/locale
75
+ const uiBasePath = i18n.uriPattern === 'path-prefix' && i18n.defaultLocale !== locale
76
+ ? `${basePath}/${locale}`
77
+ : `${basePath}`;
78
+ // Path to static artifacts on MRT the is ${basePath}/mobify/build/${bundleId}
79
+ // On express this is ${basePath}
80
+ const assetBasePath = `${runtimeEnvironment.basePath}${runtimeEnvironment.defaultAssetPath}`;
51
81
  const runtimeParams = {
52
- locale: locale || this.locale || defaultRuntimeEnvironment.defaultLocale,
82
+ basePath,
83
+ locale,
84
+ assetBasePath,
85
+ uiBasePath,
53
86
  environment,
87
+ host,
54
88
  };
55
89
  return {
56
90
  runtimeEnvironment,
@@ -12,6 +12,8 @@ export default class ExpressRequest extends AbstractMiddlewareRequest {
12
12
  get method(): string;
13
13
  get headers(): Headers;
14
14
  get protocol(): any;
15
+ get basePath(): string;
16
+ set basePath(bp: string);
15
17
  get locale(): string;
16
18
  set locale(l: string);
17
19
  acceptsLanguages(): string[];
@@ -29,6 +29,12 @@ export default class ExpressRequest extends AbstractMiddlewareRequest {
29
29
  get protocol() {
30
30
  return this.req.protocol;
31
31
  }
32
+ get basePath() {
33
+ return this.req.basePath || '';
34
+ }
35
+ set basePath(bp) {
36
+ this.req.basePath = bp;
37
+ }
32
38
  get locale() {
33
39
  return this.req.locale;
34
40
  }
@@ -17,7 +17,6 @@ export default class ExpressInternalServer extends AbstractInternalAppServer imp
17
17
  all(path: string | Array<string>, middleware: MiddlewareFunction): void;
18
18
  get(path: string | Array<string>, middleware: MiddlewareFunction): void;
19
19
  post(path: string | Array<string>, middleware: MiddlewareFunction): void;
20
- useCompression(): void;
21
20
  getRegexWildcard(): string;
22
21
  initRoutes(): void;
23
22
  }
@@ -1,6 +1,5 @@
1
1
  import express from 'express';
2
2
  import http from 'http';
3
- import compression from '../common/compression.js';
4
3
  import AbstractInternalAppServer from '../common/abstract-internal-app-server.js';
5
4
  import ExpressRequest from './express-request.js';
6
5
  import ExpressResponse from './express-response.js';
@@ -61,9 +60,6 @@ export default class ExpressInternalServer extends AbstractInternalAppServer {
61
60
  await middleware(lwrReq, lwrRes, next);
62
61
  });
63
62
  }
64
- useCompression() {
65
- this.expressApp.use(compression());
66
- }
67
63
  getRegexWildcard() {
68
64
  return '*';
69
65
  }
@@ -26,7 +26,6 @@ export declare class FsInternalServer extends AbstractInternalAppServer implemen
26
26
  get(path: string | string[], middleware: MiddlewareFunction): void;
27
27
  post(path: string | string[], middleware: MiddlewareFunction): void;
28
28
  initRoutes(): void;
29
- useCompression(): void;
30
29
  getImpl(): LwrFsKoa;
31
30
  getRegexWildcard(): string;
32
31
  }
@@ -72,9 +72,6 @@ export class FsInternalServer extends AbstractInternalAppServer {
72
72
  this.middlewares.push(this.koaRouter.routes());
73
73
  this.lwrFsKoaApp.setMiddlewares(this.middlewares);
74
74
  }
75
- useCompression() {
76
- /* noop */
77
- }
78
75
  getImpl() {
79
76
  return this.lwrFsKoaApp;
80
77
  }
@@ -12,6 +12,8 @@ export default class KoaRequest extends AbstractMiddlewareRequest {
12
12
  get body(): any;
13
13
  get method(): string;
14
14
  get headers(): Headers;
15
+ get basePath(): string;
16
+ set basePath(bp: string);
15
17
  get locale(): string;
16
18
  set locale(l: string);
17
19
  acceptsLanguages(): string[];
@@ -28,6 +28,12 @@ export default class KoaRequest extends AbstractMiddlewareRequest {
28
28
  get headers() {
29
29
  return this.ctx.headers;
30
30
  }
31
+ get basePath() {
32
+ return this.ctx.basePath || '';
33
+ }
34
+ set basePath(bp) {
35
+ this.ctx.basePath = bp;
36
+ }
31
37
  get locale() {
32
38
  return this.ctx.locale;
33
39
  }
@@ -17,7 +17,6 @@ export default class KoaInternalServer extends AbstractInternalAppServer impleme
17
17
  all(path: string | string[], middleware: MiddlewareFunction): void;
18
18
  get(path: string | string[], middleware: MiddlewareFunction): void;
19
19
  post(path: string | string[], middleware: MiddlewareFunction): void;
20
- useCompression(): void;
21
20
  getRegexWildcard(): string;
22
21
  initRoutes(): void;
23
22
  }
@@ -1,7 +1,6 @@
1
1
  import Koa from 'koa';
2
2
  import KoaRouter from '@koa/router';
3
3
  import http from 'http';
4
- import compress from 'koa-compress';
5
4
  import AbstractInternalAppServer from '../common/abstract-internal-app-server.js';
6
5
  import KoaRequest from './koa-request.js';
7
6
  import KoaResponse from './koa-response.js';
@@ -53,9 +52,6 @@ export default class KoaInternalServer extends AbstractInternalAppServer {
53
52
  await middleware(lwrReq, lwrRes, next);
54
53
  });
55
54
  }
56
- useCompression() {
57
- this.koaApp.use(compress());
58
- }
59
55
  getRegexWildcard() {
60
56
  return '(.*)';
61
57
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.11.0-alpha.1",
7
+ "version": "0.11.0-alpha.10",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -34,30 +34,22 @@
34
34
  "build/**/*.d.ts"
35
35
  ],
36
36
  "devDependencies": {
37
- "@lwrjs/types": "0.11.0-alpha.1",
37
+ "@lwrjs/types": "0.11.0-alpha.10",
38
38
  "@types/koa-compress": "^4.0.1",
39
39
  "@types/koa__router": "^8.0.4",
40
40
  "jest-express": "^1.12.0"
41
41
  },
42
42
  "dependencies": {
43
43
  "@koa/router": "^10.0.0",
44
- "@lwrjs/shared-utils": "0.11.0-alpha.1",
45
- "@types/compression": "^1.7.2",
44
+ "@lwrjs/shared-utils": "0.11.0-alpha.10",
46
45
  "@types/express": "^4.17.17",
47
46
  "@types/koa": "^2.11.7",
48
- "compression": "^1.7.4",
49
47
  "express": "^4.18.2",
50
48
  "koa": "^2.13.1",
51
- "koa-compose": "^4.1.0",
52
- "koa-compress": "^5.0.1"
53
- },
54
- "optionalDependencies": {
55
- "iltorb": "^2.4.5",
56
- "node-zopfli-es": "^2.0.1",
57
- "shrink-ray-current": "^4.1.3"
49
+ "koa-compose": "^4.1.0"
58
50
  },
59
51
  "engines": {
60
52
  "node": ">=16.0.0"
61
53
  },
62
- "gitHead": "6a840d4694fac9f0be5c3a21707015379cbadad7"
54
+ "gitHead": "34b1e289e8de12531f5624b64512e870157195d4"
63
55
  }
@@ -1,45 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getProtoOf = Object.getPrototypeOf;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, {get: all[name], enumerable: true});
11
- };
12
- var __exportStar = (target, module2, desc) => {
13
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
- for (let key of __getOwnPropNames(module2))
15
- if (!__hasOwnProp.call(target, key) && key !== "default")
16
- __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
- }
18
- return target;
19
- };
20
- var __toModule = (module2) => {
21
- return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
- };
23
-
24
- // packages/@lwrjs/server/src/common/compression.ts
25
- __markAsModule(exports);
26
- __export(exports, {
27
- default: () => compression_default
28
- });
29
- var import_compression = __toModule(require("compression"));
30
- var compressionLibToUse;
31
- var compression = async (req, res, next) => {
32
- if (compressionLibToUse === void 0) {
33
- try {
34
- let shrinkRay = await Promise.resolve().then(() => __toModule(require("shrink-ray-current")));
35
- shrinkRay = shrinkRay.default || shrinkRay;
36
- compressionLibToUse = shrinkRay({useZopfliForGzip: false});
37
- } catch (err) {
38
- compressionLibToUse = (0, import_compression.default)();
39
- }
40
- }
41
- compressionLibToUse(req, res, next);
42
- };
43
- function compression_default() {
44
- return compression;
45
- }
@@ -1,3 +0,0 @@
1
- import type { RequestHandler } from 'express';
2
- export default function (): RequestHandler;
3
- //# sourceMappingURL=compression.d.ts.map
@@ -1,25 +0,0 @@
1
- import expressCompression from 'compression';
2
- let compressionLibToUse;
3
- const compression = async (req, res, next) => {
4
- if (compressionLibToUse === undefined) {
5
- // TODO: Unfortunately the compression story for express is a mess.
6
- // shrink-ray-current is a fork of compression with brotlli support, we will use it until the
7
- // "official" one accepts a PR to support it: https://github.com/expressjs/compression/issues/71
8
- // Since it has some native dependencies (node-zopfli-es, iltorb) that may fail to compile in
9
- // certain environments, we make it an optional dependency and fall back to vanilla compression.
10
- try {
11
- let shrinkRay = await import('shrink-ray-current');
12
- shrinkRay = shrinkRay.default || shrinkRay;
13
- compressionLibToUse = shrinkRay({ useZopfliForGzip: false });
14
- }
15
- catch (err) {
16
- compressionLibToUse = expressCompression();
17
- }
18
- }
19
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
20
- compressionLibToUse(req, res, next);
21
- };
22
- export default function () {
23
- return compression;
24
- }
25
- //# sourceMappingURL=compression.js.map