@lwrjs/core 0.15.0-alpha.35 → 0.15.0-alpha.36

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.
@@ -0,0 +1,16 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, {get: all[name], enumerable: true});
6
+ };
7
+
8
+ // packages/@lwrjs/core/src/middleware/utils/compression.ts
9
+ __markAsModule(exports);
10
+ __export(exports, {
11
+ getMrtCompressionThreshold: () => getMrtCompressionThreshold
12
+ });
13
+ var MAX_LAMBDA_RESPONSE_SIZE = 6 * 1024 * 1024;
14
+ function getMrtCompressionThreshold() {
15
+ return MAX_LAMBDA_RESPONSE_SIZE - 5 * 1024 * 1024;
16
+ }
@@ -35,10 +35,13 @@ var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
35
35
  var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
36
36
  var import_view_registry = __toModule(require("@lwrjs/view-registry"));
37
37
  var import_instrumentation2 = __toModule(require("@lwrjs/instrumentation"));
38
+ var import_zlib = __toModule(require("zlib"));
39
+ var import_compression = __toModule(require("./utils/compression.cjs"));
38
40
  function createViewMiddleware(route, errorRoutes, context, viewHandler) {
39
41
  const errorRoute = errorRoutes.find((route2) => route2.status === 500);
40
42
  const appConfig = context.appConfig;
41
43
  const {environment: environmentConfig} = appConfig;
44
+ const mrtCompressionThreshold = (0, import_compression.getMrtCompressionThreshold)();
42
45
  return async (req, res) => {
43
46
  if (!req.validateEnvironmentRequest(appConfig)) {
44
47
  res.status(400);
@@ -144,7 +147,32 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
144
147
  }
145
148
  }
146
149
  res.status(status);
147
- res.send(viewResponse.body);
150
+ const viewResponseBody = viewResponse.body;
151
+ let shouldCompress, viewResponseSize;
152
+ if ((0, import_shared_utils.isLambdaEnv)() && typeof viewResponseBody === "string") {
153
+ viewResponseSize = Buffer.byteLength(viewResponseBody, "utf-8");
154
+ shouldCompress = Buffer.byteLength(viewResponseBody, "utf-8") >= mrtCompressionThreshold;
155
+ }
156
+ if (shouldCompress) {
157
+ const start = performance.now();
158
+ let compressionMethod;
159
+ if (req.headers["accept-encoding"]?.includes("br")) {
160
+ res.setHeader("Content-Encoding", "br");
161
+ compressionMethod = import_zlib.brotliCompressSync;
162
+ } else if (req.headers["accept-encoding"]?.includes("gzip")) {
163
+ compressionMethod = import_zlib.gzipSync;
164
+ res.setHeader("Content-Encoding", "gzip");
165
+ } else {
166
+ import_diagnostics.logger.warn(`View size (${viewResponseSize}) should be compressed, but failed due to missing content-encoding headers.`);
167
+ return res.send(viewResponse.body);
168
+ }
169
+ const compressedView = compressionMethod(viewResponseBody);
170
+ const end = performance.now();
171
+ import_diagnostics.logger.warn(`View size (${viewResponseSize}) compressed due to Lambda response limit. Compression took ${Math.round(end - start)} ms`);
172
+ res.send(compressedView);
173
+ } else {
174
+ res.send(viewResponse.body);
175
+ }
148
176
  };
149
177
  }
150
178
  function createConfigMiddleware(routes, context, viewHandler) {
@@ -0,0 +1,2 @@
1
+ export declare function getMrtCompressionThreshold(): number;
2
+ //# sourceMappingURL=compression.d.ts.map
@@ -0,0 +1,6 @@
1
+ // currently documented AWS Lambda limit, 6MB
2
+ const MAX_LAMBDA_RESPONSE_SIZE = 6 * 1024 * 1024;
3
+ export function getMrtCompressionThreshold() {
4
+ return MAX_LAMBDA_RESPONSE_SIZE - 5 * 1024 * 1024; // Trigger compression within 1MB of limit
5
+ }
6
+ //# sourceMappingURL=compression.js.map
@@ -2,15 +2,18 @@ import { TextEncoder } from 'util';
2
2
  import { URLSearchParams } from 'url';
3
3
  import { descriptions, logger } from '@lwrjs/diagnostics';
4
4
  import { getClientRoutes } from '@lwrjs/router';
5
- import { decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRoutes, isURL, parseRequestDepth, REQUEST_DEPTH_KEY, shortestTtl, } from '@lwrjs/shared-utils';
5
+ import { decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRoutes, isURL, parseRequestDepth, REQUEST_DEPTH_KEY, shortestTtl, isLambdaEnv, } from '@lwrjs/shared-utils';
6
6
  import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
7
7
  import { handleErrors } from './utils/error-handling.js';
8
8
  import { LwrViewHandler } from '@lwrjs/view-registry';
9
9
  import { getTraceCollector } from '@lwrjs/instrumentation';
10
+ import { brotliCompressSync, gzipSync } from 'zlib';
11
+ import { getMrtCompressionThreshold } from './utils/compression.js';
10
12
  function createViewMiddleware(route, errorRoutes, context, viewHandler) {
11
13
  const errorRoute = errorRoutes.find((route) => route.status === 500);
12
14
  const appConfig = context.appConfig;
13
15
  const { environment: environmentConfig } = appConfig;
16
+ const mrtCompressionThreshold = getMrtCompressionThreshold();
14
17
  return async (req, res) => {
15
18
  if (!req.validateEnvironmentRequest(appConfig)) {
16
19
  res.status(400);
@@ -128,7 +131,37 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
128
131
  }
129
132
  }
130
133
  res.status(status);
131
- res.send(viewResponse.body);
134
+ // LWR@MRT 254 temporary safeguard for "dupe styles" issue causing huge base docs
135
+ const viewResponseBody = viewResponse.body;
136
+ let shouldCompress, viewResponseSize;
137
+ if (isLambdaEnv() && typeof viewResponseBody === 'string') {
138
+ viewResponseSize = Buffer.byteLength(viewResponseBody, 'utf-8');
139
+ shouldCompress = Buffer.byteLength(viewResponseBody, 'utf-8') >= mrtCompressionThreshold;
140
+ }
141
+ // Conditionally apply compression if size is near the threshold
142
+ if (shouldCompress) {
143
+ const start = performance.now();
144
+ let compressionMethod;
145
+ if (req.headers['accept-encoding']?.includes('br')) {
146
+ res.setHeader('Content-Encoding', 'br');
147
+ compressionMethod = brotliCompressSync;
148
+ }
149
+ else if (req.headers['accept-encoding']?.includes('gzip')) {
150
+ compressionMethod = gzipSync;
151
+ res.setHeader('Content-Encoding', 'gzip');
152
+ }
153
+ else {
154
+ logger.warn(`View size (${viewResponseSize}) should be compressed, but failed due to missing content-encoding headers.`);
155
+ return res.send(viewResponse.body);
156
+ }
157
+ const compressedView = compressionMethod(viewResponseBody);
158
+ const end = performance.now();
159
+ logger.warn(`View size (${viewResponseSize}) compressed due to Lambda response limit. Compression took ${Math.round(end - start)} ms`);
160
+ res.send(compressedView);
161
+ }
162
+ else {
163
+ res.send(viewResponse.body);
164
+ }
132
165
  };
133
166
  }
134
167
  function createConfigMiddleware(routes, context, viewHandler) {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.15.0-alpha.35",
7
+ "version": "0.15.0-alpha.36",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -43,34 +43,34 @@
43
43
  "build": "tsc -b"
44
44
  },
45
45
  "dependencies": {
46
- "@lwrjs/app-service": "0.15.0-alpha.35",
47
- "@lwrjs/asset-registry": "0.15.0-alpha.35",
48
- "@lwrjs/asset-transformer": "0.15.0-alpha.35",
49
- "@lwrjs/base-view-provider": "0.15.0-alpha.35",
50
- "@lwrjs/base-view-transformer": "0.15.0-alpha.35",
51
- "@lwrjs/client-modules": "0.15.0-alpha.35",
52
- "@lwrjs/config": "0.15.0-alpha.35",
53
- "@lwrjs/diagnostics": "0.15.0-alpha.35",
54
- "@lwrjs/esbuild": "0.15.0-alpha.35",
55
- "@lwrjs/fs-asset-provider": "0.15.0-alpha.35",
56
- "@lwrjs/fs-watch": "0.15.0-alpha.35",
57
- "@lwrjs/html-view-provider": "0.15.0-alpha.35",
58
- "@lwrjs/instrumentation": "0.15.0-alpha.35",
59
- "@lwrjs/loader": "0.15.0-alpha.35",
60
- "@lwrjs/lwc-module-provider": "0.15.0-alpha.35",
61
- "@lwrjs/lwc-ssr": "0.15.0-alpha.35",
62
- "@lwrjs/markdown-view-provider": "0.15.0-alpha.35",
63
- "@lwrjs/module-bundler": "0.15.0-alpha.35",
64
- "@lwrjs/module-registry": "0.15.0-alpha.35",
65
- "@lwrjs/npm-module-provider": "0.15.0-alpha.35",
66
- "@lwrjs/nunjucks-view-provider": "0.15.0-alpha.35",
67
- "@lwrjs/o11y": "0.15.0-alpha.35",
68
- "@lwrjs/resource-registry": "0.15.0-alpha.35",
69
- "@lwrjs/router": "0.15.0-alpha.35",
70
- "@lwrjs/server": "0.15.0-alpha.35",
71
- "@lwrjs/shared-utils": "0.15.0-alpha.35",
72
- "@lwrjs/static": "0.15.0-alpha.35",
73
- "@lwrjs/view-registry": "0.15.0-alpha.35",
46
+ "@lwrjs/app-service": "0.15.0-alpha.36",
47
+ "@lwrjs/asset-registry": "0.15.0-alpha.36",
48
+ "@lwrjs/asset-transformer": "0.15.0-alpha.36",
49
+ "@lwrjs/base-view-provider": "0.15.0-alpha.36",
50
+ "@lwrjs/base-view-transformer": "0.15.0-alpha.36",
51
+ "@lwrjs/client-modules": "0.15.0-alpha.36",
52
+ "@lwrjs/config": "0.15.0-alpha.36",
53
+ "@lwrjs/diagnostics": "0.15.0-alpha.36",
54
+ "@lwrjs/esbuild": "0.15.0-alpha.36",
55
+ "@lwrjs/fs-asset-provider": "0.15.0-alpha.36",
56
+ "@lwrjs/fs-watch": "0.15.0-alpha.36",
57
+ "@lwrjs/html-view-provider": "0.15.0-alpha.36",
58
+ "@lwrjs/instrumentation": "0.15.0-alpha.36",
59
+ "@lwrjs/loader": "0.15.0-alpha.36",
60
+ "@lwrjs/lwc-module-provider": "0.15.0-alpha.36",
61
+ "@lwrjs/lwc-ssr": "0.15.0-alpha.36",
62
+ "@lwrjs/markdown-view-provider": "0.15.0-alpha.36",
63
+ "@lwrjs/module-bundler": "0.15.0-alpha.36",
64
+ "@lwrjs/module-registry": "0.15.0-alpha.36",
65
+ "@lwrjs/npm-module-provider": "0.15.0-alpha.36",
66
+ "@lwrjs/nunjucks-view-provider": "0.15.0-alpha.36",
67
+ "@lwrjs/o11y": "0.15.0-alpha.36",
68
+ "@lwrjs/resource-registry": "0.15.0-alpha.36",
69
+ "@lwrjs/router": "0.15.0-alpha.36",
70
+ "@lwrjs/server": "0.15.0-alpha.36",
71
+ "@lwrjs/shared-utils": "0.15.0-alpha.36",
72
+ "@lwrjs/static": "0.15.0-alpha.36",
73
+ "@lwrjs/view-registry": "0.15.0-alpha.36",
74
74
  "chokidar": "^3.6.0",
75
75
  "esbuild": "^0.9.7",
76
76
  "fs-extra": "^11.2.0",
@@ -80,7 +80,7 @@
80
80
  "ws": "^8.18.0"
81
81
  },
82
82
  "devDependencies": {
83
- "@lwrjs/types": "0.15.0-alpha.35",
83
+ "@lwrjs/types": "0.15.0-alpha.36",
84
84
  "@types/ws": "^8.5.12",
85
85
  "memfs": "^4.13.0"
86
86
  },
@@ -93,5 +93,5 @@
93
93
  "volta": {
94
94
  "extends": "../../../package.json"
95
95
  },
96
- "gitHead": "42f04ad68558ec00f543a04e363671ceb294c88b"
96
+ "gitHead": "325f5a8d06e1af07795f108e32c8300c06e62aa3"
97
97
  }