@lwrjs/server 0.11.0-alpha.8 → 0.11.0

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;
@@ -43,7 +55,15 @@ var AbstractMiddlewareRequest = class {
43
55
  return !targetEnvironment || targetEnvironment === defaultEnvironment || supportedEnvironments?.includes(targetEnvironment);
44
56
  }
45
57
  getRuntimeContext(defaultRuntimeEnvironment) {
46
- const {format, apiVersion, compat, locale, environment, bundleSpecifier} = this.params;
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,25 @@ 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 defaultLocale = runtimeEnvironment.i18n.defaultLocale;
84
+ const locale = localeParam || this.locale || defaultLocale;
85
+ const uiBasePath = runtimeEnvironment.i18n.uriPattern === "path-prefix" && defaultLocale !== locale ? `${basePath}/${locale}` : `${basePath}`;
86
+ const assetBasePath = `${runtimeEnvironment.basePath}${runtimeEnvironment.defaultAssetPath}`;
57
87
  const runtimeParams = {
58
- locale: locale || this.locale || defaultRuntimeEnvironment.defaultLocale,
59
- environment
88
+ basePath,
89
+ locale,
90
+ assetBasePath,
91
+ uiBasePath,
92
+ environment,
93
+ host
60
94
  };
61
95
  return {
62
96
  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
  }
@@ -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
  }
@@ -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;
@@ -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;
@@ -36,7 +52,7 @@ export class AbstractMiddlewareRequest {
36
52
  supportedEnvironments?.includes(targetEnvironment));
37
53
  }
38
54
  getRuntimeContext(defaultRuntimeEnvironment) {
39
- const { format, apiVersion, compat, locale, environment, bundleSpecifier } = this.params;
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,29 @@ 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 defaultLocale = runtimeEnvironment.i18n.defaultLocale;
74
+ const locale = localeParam || this.locale || defaultLocale;
75
+ // Base path to be used in routing ${basePath}/locale
76
+ const uiBasePath = runtimeEnvironment.i18n.uriPattern === 'path-prefix' && defaultLocale !== locale
77
+ ? `${basePath}/${locale}`
78
+ : `${basePath}`;
79
+ // Path to static artifacts on MRT the is ${basePath}/mobify/build/${bundleId}
80
+ // On express this is ${basePath}
81
+ const assetBasePath = `${runtimeEnvironment.basePath}${runtimeEnvironment.defaultAssetPath}`;
51
82
  const runtimeParams = {
52
- locale: locale || this.locale || defaultRuntimeEnvironment.defaultLocale,
83
+ basePath,
84
+ locale,
85
+ assetBasePath,
86
+ uiBasePath,
53
87
  environment,
88
+ host,
54
89
  };
55
90
  return {
56
91
  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
  }
@@ -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
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.11.0-alpha.8",
7
+ "version": "0.11.0",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -34,14 +34,14 @@
34
34
  "build/**/*.d.ts"
35
35
  ],
36
36
  "devDependencies": {
37
- "@lwrjs/types": "0.11.0-alpha.8",
37
+ "@lwrjs/types": "0.11.0",
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.8",
44
+ "@lwrjs/shared-utils": "0.11.0",
45
45
  "@types/express": "^4.17.17",
46
46
  "@types/koa": "^2.11.7",
47
47
  "express": "^4.18.2",
@@ -51,5 +51,5 @@
51
51
  "engines": {
52
52
  "node": ">=16.0.0"
53
53
  },
54
- "gitHead": "bba3cfa996e84ee702f287e7e7b6361b807434db"
54
+ "gitHead": "fbc883ea90a12672ce6f1adc2201144fda8762bd"
55
55
  }