@lwrjs/tools 0.15.0-alpha.1 → 0.15.0-alpha.11

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.
@@ -24,6 +24,7 @@ var __toModule = (module2) => {
24
24
  // packages/@lwrjs/tools/src/index.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
+ REQUEST_SCRIPT_NAME: () => import_server_build.REQUEST_SCRIPT_NAME,
27
28
  buildServer: () => import_server_build.buildServer,
28
29
  dedupeBundles: () => import_static.dedupeBundles
29
30
  });
@@ -24,6 +24,7 @@ var __toModule = (module2) => {
24
24
  // packages/@lwrjs/tools/src/server-build.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
+ REQUEST_SCRIPT_NAME: () => REQUEST_SCRIPT_NAME,
27
28
  buildServer: () => buildServer
28
29
  });
29
30
  var import_path = __toModule(require("path"));
@@ -33,8 +34,10 @@ var import_perf_hooks = __toModule(require("perf_hooks"));
33
34
  var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
34
35
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
35
36
  var import_config = __toModule(require("@lwrjs/config"));
37
+ var import_request_script = __toModule(require("./util/request-script.cjs"));
36
38
  var import_generate_entry_plugin = __toModule(require("./plugins/generate-entry-plugin.cjs"));
37
39
  var import_build_server_plugin = __toModule(require("./plugins/build-server-plugin.cjs"));
40
+ var REQUEST_SCRIPT_NAME = "index.js";
38
41
  async function build(buildOptions, config) {
39
42
  const {outputDir, normalizedOutputDir, minify} = buildOptions;
40
43
  await import_esbuild.default.build({
@@ -61,10 +64,11 @@ async function build(buildOptions, config) {
61
64
  plugins: [(0, import_generate_entry_plugin.default)(), (0, import_build_server_plugin.default)(config, outputDir)],
62
65
  outfile: import_path.default.join(normalizedOutputDir, "ssr.js")
63
66
  });
67
+ await import_fs_extra.default.writeFile(import_path.default.join(normalizedOutputDir, REQUEST_SCRIPT_NAME), (0, import_request_script.getMainScriptContent)());
64
68
  }
65
69
  async function buildServer(configArg, options) {
66
70
  const startTime = import_perf_hooks.performance.now();
67
- const outputDir = options?.outputDir || "/build";
71
+ const outputDir = options?.outputDir ?? "/build";
68
72
  const normalizedOutputDir = import_path.default.join(configArg?.rootDir || process.cwd(), outputDir);
69
73
  await import_fs_extra.default.ensureDir(normalizedOutputDir);
70
74
  await build({outputDir, normalizedOutputDir, minify: !!options?.minify}, configArg);
@@ -0,0 +1,209 @@
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/tools/src/util/request-script.ts
9
+ __markAsModule(exports);
10
+ __export(exports, {
11
+ getMainScriptContent: () => getMainScriptContent
12
+ });
13
+ function getMainScriptContent() {
14
+ return getEnvironmentSetup() + getHandlerFunction() + getDefaultInput() + getDefaultContext() + getParseArguments() + getPrintHelpMessage() + getUpdateInput() + getMainExecution();
15
+ }
16
+ function getEnvironmentSetup() {
17
+ return `
18
+ // Manually Invoke a SSR request
19
+
20
+ // Overwrite Envs from SSR.js
21
+ process.env.ASSETS_ON_LAMBDA = 'true';
22
+ process.env.LWR_TRACING = 'off';
23
+
24
+ // Lambda Envs (required by pwa-kit runtime)
25
+ process.env.AWS_LAMBDA_FUNCTION_NAME = 'get';
26
+ process.env.BUNDLE_ID = '1';
27
+ process.env.DEPLOY_TARGET = 'set';
28
+ process.env.EXTERNAL_DOMAIN_NAME = 'mock-domain.example.com';
29
+ process.env.MOBIFY_PROPERTY_ID = 'set';
30
+ `;
31
+ }
32
+ function getHandlerFunction() {
33
+ return `
34
+ /**
35
+ * Main handler function to be invoked by Java client.
36
+ * @param {Object} input - The input object containing JavaScript code.
37
+ * @returns {Promise<Object>} - The result object with linting results.
38
+ */
39
+ async function handler(input) {
40
+ if (!input) {
41
+ input = DEFAULT_INPUT;
42
+ }
43
+
44
+ // Import the SSR module
45
+ const ssr = require('./ssr.js');
46
+ return new Promise((resolve, reject) => {
47
+ ssr.get(input, DEFAULT_CONTEXT, (error, result) => {
48
+ if (error) {
49
+ return reject(error);
50
+ } else {
51
+ resolve(result);
52
+ }
53
+ });
54
+ });
55
+ }
56
+ `;
57
+ }
58
+ function getDefaultInput() {
59
+ return `
60
+ // Example Input
61
+ const DEFAULT_INPUT = {
62
+ path: '/',
63
+ httpMethod: 'GET',
64
+ headers: {
65
+ host: 'mock-domain.example.com',
66
+ 'user-agent': 'curl/7.64.1',
67
+ }
68
+ };
69
+ `;
70
+ }
71
+ function getDefaultContext() {
72
+ return `
73
+ // Define the context object (with basic properties, you can add more as needed)
74
+ const DEFAULT_CONTEXT = {
75
+ awsRequestId: 'test-request-id',
76
+ logGroupName: '/aws/lambda/test',
77
+ logStreamName: 'test-log-stream',
78
+ functionName: 'get',
79
+ functionVersion: '1',
80
+ invokedFunctionArn: 'arn:aws:lambda:test-region:123456789012:function:test-function',
81
+ memoryLimitInMB: '128',
82
+ };
83
+ `;
84
+ }
85
+ function getParseArguments() {
86
+ return `
87
+ // Parse command-line arguments
88
+ function parseArguments() {
89
+ const args = process.argv.slice(2);
90
+ const options = {
91
+ host: null,
92
+ proto: null,
93
+ basePath: null,
94
+ path: DEFAULT_INPUT.path,
95
+ output: null,
96
+ };
97
+
98
+ if (args.includes('--help') || args.includes('-h')) {
99
+ printHelpMessage();
100
+ process.exit(0); // Exit after printing help message
101
+ }
102
+
103
+ args.forEach((arg) => {
104
+ const [key, value] = arg.split('=');
105
+ switch (key) {
106
+ case '--host':
107
+ case '-H':
108
+ options.host = value;
109
+ break;
110
+ case '--proto':
111
+ case '-p':
112
+ options.proto = value;
113
+ break;
114
+ case '--basePath':
115
+ case '-b':
116
+ options.basePath = value;
117
+ break;
118
+ case '--path':
119
+ case '-P':
120
+ options.path = value;
121
+ break;
122
+ case '--output':
123
+ case '-o':
124
+ options.output = value;
125
+ break;
126
+ }
127
+ });
128
+
129
+ return options;
130
+ }
131
+ `;
132
+ }
133
+ function getPrintHelpMessage() {
134
+ return `
135
+ function printHelpMessage() {
136
+ console.log(
137
+ 'Usage: node your-script.js [options]\\n' +
138
+ '\\n' +
139
+ 'Options:\\n' +
140
+ ' -H, --host=<host> Specify the host\\n' +
141
+ ' -p, --proto=<protocol> Specify the protocol (e.g., http, https)\\n' +
142
+ ' -b, --basePath=<basePath> Specify the base path\\n' +
143
+ ' -P, --path=<path> Specify the input path (default: ' + DEFAULT_INPUT.path + ')\\n' +
144
+ ' -o, --output=<output> Specify the output file path\\n' +
145
+ ' -h, --help Show this help message\\n' +
146
+ '\\n' +
147
+ 'Example:\\n' +
148
+ ' node your-script.js --host=localhost --proto=http --basePath=/api --path=/data --output=result.json\\n' +
149
+ ' node your-script.js -H=localhost -p=http -b=/api -P=/data -o=result.json\\n'
150
+ );
151
+ }
152
+ `;
153
+ }
154
+ function getUpdateInput() {
155
+ return `
156
+ // Update the DEFAULT_INPUT object based on arguments
157
+ function updateInput(input, args) {
158
+ if (args.host) {
159
+ const proto = args.proto || 'https'; // Default to 'https' if proto is not provided
160
+ input.headers.forwarded = 'host=' + args.host + ';proto=' + proto;
161
+ }
162
+
163
+ if (args.basePath !== null) {
164
+ // Ensure basePath starts with a '/'
165
+ const normalizedBasePath = args.basePath.startsWith('/') ? args.basePath : '/' + args.basePath;
166
+ input.headers['x-mobify-request-class'] = 'basePath=' + normalizedBasePath;
167
+ }
168
+
169
+ if (args.path) {
170
+ input.path = args.path;
171
+ }
172
+
173
+ return input;
174
+ }
175
+ `;
176
+ }
177
+ function getMainExecution() {
178
+ return `
179
+ // Automatically perform a request when this script is executed directly
180
+ if (require.main === module) {
181
+ const args = parseArguments();
182
+ const updatedInput = updateInput(DEFAULT_INPUT, args);
183
+ handler(updatedInput).then((result) => {
184
+ const fs = require('fs');
185
+ const path = require('path');
186
+
187
+ // Write to a file
188
+ if (args.output) {
189
+ fs.writeFileSync(args.output, JSON.stringify(result, null, 2), 'utf-8');
190
+
191
+ const outputPath = args.output;
192
+ const isAbsolutePath = path.isAbsolute(outputPath);
193
+ const filePath = isAbsolutePath ? outputPath : path.resolve(process.cwd(), outputPath);
194
+
195
+ console.log('\\nRequest Saved: \\x1b[34mfile://' + filePath + '\\x1b[0m');
196
+ } else {
197
+ // Print to console
198
+ console.log(result);
199
+ }
200
+ process.exit(0);
201
+ }).catch(err => {
202
+ console.error(err);
203
+ process.exit(1);
204
+ });
205
+ }
206
+
207
+ module.exports = handler;
208
+ `;
209
+ }
@@ -1,3 +1,3 @@
1
- export { buildServer } from './server-build.js';
1
+ export { buildServer, REQUEST_SCRIPT_NAME } from './server-build.js';
2
2
  export { dedupeBundles } from '@lwrjs/static';
3
3
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { buildServer } from './server-build.js';
1
+ export { buildServer, REQUEST_SCRIPT_NAME } from './server-build.js';
2
2
  export { dedupeBundles } from '@lwrjs/static';
3
3
  //# sourceMappingURL=index.js.map
@@ -4,6 +4,7 @@ export interface BuildOptions {
4
4
  normalizedOutputDir?: string;
5
5
  minify?: boolean;
6
6
  }
7
+ export declare const REQUEST_SCRIPT_NAME = "index.js";
7
8
  /**
8
9
  * Resolve configurations, generate a server build module, and bundle
9
10
  * the LWR server. The bundled server will be written to the file system.
@@ -5,8 +5,10 @@ import { performance } from 'perf_hooks';
5
5
  import { logger } from '@lwrjs/diagnostics';
6
6
  import { getFeatureFlags } from '@lwrjs/shared-utils';
7
7
  import { LWR_VERSION, LWC_VERSION, PWA_KIT_RUNTIME_VERSION } from '@lwrjs/config';
8
+ import { getMainScriptContent } from './util/request-script.js';
8
9
  import generateLwrEntry from './plugins/generate-entry-plugin.js';
9
10
  import buildLwrServer from './plugins/build-server-plugin.js';
11
+ export const REQUEST_SCRIPT_NAME = 'index.js';
10
12
  async function build(buildOptions, config) {
11
13
  const { outputDir, normalizedOutputDir, minify } = buildOptions;
12
14
  // building specifically for MRT/Lambda
@@ -41,6 +43,8 @@ async function build(buildOptions, config) {
41
43
  // MRT expects an entry point named `ssr.js`
42
44
  outfile: path.join(normalizedOutputDir, 'ssr.js'),
43
45
  });
46
+ // Save the request script to the app folder
47
+ await fs.writeFile(path.join(normalizedOutputDir, REQUEST_SCRIPT_NAME), getMainScriptContent());
44
48
  }
45
49
  /**
46
50
  * Resolve configurations, generate a server build module, and bundle
@@ -51,7 +55,7 @@ async function build(buildOptions, config) {
51
55
  */
52
56
  export async function buildServer(configArg, options) {
53
57
  const startTime = performance.now();
54
- const outputDir = options?.outputDir || '/build';
58
+ const outputDir = options?.outputDir ?? '/build';
55
59
  const normalizedOutputDir = path.join(configArg?.rootDir || process.cwd(), outputDir);
56
60
  await fs.ensureDir(normalizedOutputDir);
57
61
  await build({ outputDir, normalizedOutputDir, minify: !!options?.minify }, configArg);
@@ -0,0 +1,2 @@
1
+ export declare function getMainScriptContent(): string;
2
+ //# sourceMappingURL=request-script.d.ts.map
@@ -0,0 +1,205 @@
1
+ export function getMainScriptContent() {
2
+ return (getEnvironmentSetup() +
3
+ getHandlerFunction() +
4
+ getDefaultInput() +
5
+ getDefaultContext() +
6
+ getParseArguments() +
7
+ getPrintHelpMessage() +
8
+ getUpdateInput() +
9
+ getMainExecution());
10
+ }
11
+ function getEnvironmentSetup() {
12
+ return `
13
+ // Manually Invoke a SSR request
14
+
15
+ // Overwrite Envs from SSR.js
16
+ process.env.ASSETS_ON_LAMBDA = 'true';
17
+ process.env.LWR_TRACING = 'off';
18
+
19
+ // Lambda Envs (required by pwa-kit runtime)
20
+ process.env.AWS_LAMBDA_FUNCTION_NAME = 'get';
21
+ process.env.BUNDLE_ID = '1';
22
+ process.env.DEPLOY_TARGET = 'set';
23
+ process.env.EXTERNAL_DOMAIN_NAME = 'mock-domain.example.com';
24
+ process.env.MOBIFY_PROPERTY_ID = 'set';
25
+ `;
26
+ }
27
+ function getHandlerFunction() {
28
+ return `
29
+ /**
30
+ * Main handler function to be invoked by Java client.
31
+ * @param {Object} input - The input object containing JavaScript code.
32
+ * @returns {Promise<Object>} - The result object with linting results.
33
+ */
34
+ async function handler(input) {
35
+ if (!input) {
36
+ input = DEFAULT_INPUT;
37
+ }
38
+
39
+ // Import the SSR module
40
+ const ssr = require('./ssr.js');
41
+ return new Promise((resolve, reject) => {
42
+ ssr.get(input, DEFAULT_CONTEXT, (error, result) => {
43
+ if (error) {
44
+ return reject(error);
45
+ } else {
46
+ resolve(result);
47
+ }
48
+ });
49
+ });
50
+ }
51
+ `;
52
+ }
53
+ function getDefaultInput() {
54
+ return `
55
+ // Example Input
56
+ const DEFAULT_INPUT = {
57
+ path: '/',
58
+ httpMethod: 'GET',
59
+ headers: {
60
+ host: 'mock-domain.example.com',
61
+ 'user-agent': 'curl/7.64.1',
62
+ }
63
+ };
64
+ `;
65
+ }
66
+ function getDefaultContext() {
67
+ return `
68
+ // Define the context object (with basic properties, you can add more as needed)
69
+ const DEFAULT_CONTEXT = {
70
+ awsRequestId: 'test-request-id',
71
+ logGroupName: '/aws/lambda/test',
72
+ logStreamName: 'test-log-stream',
73
+ functionName: 'get',
74
+ functionVersion: '1',
75
+ invokedFunctionArn: 'arn:aws:lambda:test-region:123456789012:function:test-function',
76
+ memoryLimitInMB: '128',
77
+ };
78
+ `;
79
+ }
80
+ function getParseArguments() {
81
+ return `
82
+ // Parse command-line arguments
83
+ function parseArguments() {
84
+ const args = process.argv.slice(2);
85
+ const options = {
86
+ host: null,
87
+ proto: null,
88
+ basePath: null,
89
+ path: DEFAULT_INPUT.path,
90
+ output: null,
91
+ };
92
+
93
+ if (args.includes('--help') || args.includes('-h')) {
94
+ printHelpMessage();
95
+ process.exit(0); // Exit after printing help message
96
+ }
97
+
98
+ args.forEach((arg) => {
99
+ const [key, value] = arg.split('=');
100
+ switch (key) {
101
+ case '--host':
102
+ case '-H':
103
+ options.host = value;
104
+ break;
105
+ case '--proto':
106
+ case '-p':
107
+ options.proto = value;
108
+ break;
109
+ case '--basePath':
110
+ case '-b':
111
+ options.basePath = value;
112
+ break;
113
+ case '--path':
114
+ case '-P':
115
+ options.path = value;
116
+ break;
117
+ case '--output':
118
+ case '-o':
119
+ options.output = value;
120
+ break;
121
+ }
122
+ });
123
+
124
+ return options;
125
+ }
126
+ `;
127
+ }
128
+ function getPrintHelpMessage() {
129
+ return `
130
+ function printHelpMessage() {
131
+ console.log(
132
+ 'Usage: node your-script.js [options]\\n' +
133
+ '\\n' +
134
+ 'Options:\\n' +
135
+ ' -H, --host=<host> Specify the host\\n' +
136
+ ' -p, --proto=<protocol> Specify the protocol (e.g., http, https)\\n' +
137
+ ' -b, --basePath=<basePath> Specify the base path\\n' +
138
+ ' -P, --path=<path> Specify the input path (default: ' + DEFAULT_INPUT.path + ')\\n' +
139
+ ' -o, --output=<output> Specify the output file path\\n' +
140
+ ' -h, --help Show this help message\\n' +
141
+ '\\n' +
142
+ 'Example:\\n' +
143
+ ' node your-script.js --host=localhost --proto=http --basePath=/api --path=/data --output=result.json\\n' +
144
+ ' node your-script.js -H=localhost -p=http -b=/api -P=/data -o=result.json\\n'
145
+ );
146
+ }
147
+ `;
148
+ }
149
+ function getUpdateInput() {
150
+ return `
151
+ // Update the DEFAULT_INPUT object based on arguments
152
+ function updateInput(input, args) {
153
+ if (args.host) {
154
+ const proto = args.proto || 'https'; // Default to 'https' if proto is not provided
155
+ input.headers.forwarded = 'host=' + args.host + ';proto=' + proto;
156
+ }
157
+
158
+ if (args.basePath !== null) {
159
+ // Ensure basePath starts with a '/'
160
+ const normalizedBasePath = args.basePath.startsWith('/') ? args.basePath : '/' + args.basePath;
161
+ input.headers['x-mobify-request-class'] = 'basePath=' + normalizedBasePath;
162
+ }
163
+
164
+ if (args.path) {
165
+ input.path = args.path;
166
+ }
167
+
168
+ return input;
169
+ }
170
+ `;
171
+ }
172
+ function getMainExecution() {
173
+ return `
174
+ // Automatically perform a request when this script is executed directly
175
+ if (require.main === module) {
176
+ const args = parseArguments();
177
+ const updatedInput = updateInput(DEFAULT_INPUT, args);
178
+ handler(updatedInput).then((result) => {
179
+ const fs = require('fs');
180
+ const path = require('path');
181
+
182
+ // Write to a file
183
+ if (args.output) {
184
+ fs.writeFileSync(args.output, JSON.stringify(result, null, 2), 'utf-8');
185
+
186
+ const outputPath = args.output;
187
+ const isAbsolutePath = path.isAbsolute(outputPath);
188
+ const filePath = isAbsolutePath ? outputPath : path.resolve(process.cwd(), outputPath);
189
+
190
+ console.log('\\nRequest Saved: \\x1b[34mfile://' + filePath + '\\x1b[0m');
191
+ } else {
192
+ // Print to console
193
+ console.log(result);
194
+ }
195
+ process.exit(0);
196
+ }).catch(err => {
197
+ console.error(err);
198
+ process.exit(1);
199
+ });
200
+ }
201
+
202
+ module.exports = handler;
203
+ `;
204
+ }
205
+ //# sourceMappingURL=request-script.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.15.0-alpha.1",
7
+ "version": "0.15.0-alpha.11",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -34,16 +34,16 @@
34
34
  "package.cjs"
35
35
  ],
36
36
  "dependencies": {
37
- "@lwrjs/config": "0.15.0-alpha.1",
38
- "@lwrjs/core": "0.15.0-alpha.1",
39
- "@lwrjs/diagnostics": "0.15.0-alpha.1",
40
- "@lwrjs/shared-utils": "0.15.0-alpha.1",
41
- "@lwrjs/static": "0.15.0-alpha.1",
37
+ "@lwrjs/config": "0.15.0-alpha.11",
38
+ "@lwrjs/core": "0.15.0-alpha.11",
39
+ "@lwrjs/diagnostics": "0.15.0-alpha.11",
40
+ "@lwrjs/shared-utils": "0.15.0-alpha.11",
41
+ "@lwrjs/static": "0.15.0-alpha.11",
42
42
  "esbuild": "^0.17.4",
43
43
  "fs-extra": "^11.2.0"
44
44
  },
45
45
  "devDependencies": {
46
- "@lwrjs/types": "0.15.0-alpha.1",
46
+ "@lwrjs/types": "0.15.0-alpha.11",
47
47
  "jest": "^26.6.3",
48
48
  "ts-jest": "^26.5.6"
49
49
  },
@@ -56,5 +56,5 @@
56
56
  "volta": {
57
57
  "extends": "../../../package.json"
58
58
  },
59
- "gitHead": "4547b6c6a7c9e9f33f829d4fdf2082caf4c9c44b"
59
+ "gitHead": "86f6f83ff30c750aab3b29448fa484d58918fb05"
60
60
  }