securenow 8.11.2 → 8.12.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.
package/cli/run.js CHANGED
@@ -1,8 +1,8 @@
1
- 'use strict';
2
-
3
- const { spawn } = require('child_process');
4
- const path = require('path');
5
- const fs = require('fs');
1
+ 'use strict';
2
+
3
+ const { spawn } = require('child_process');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
6
  const ui = require('./ui');
7
7
 
8
8
  const NODE_FLAGS_WITH_VALUE = new Set([
@@ -21,94 +21,94 @@ const NODE_FLAGS_WITH_VALUE = new Set([
21
21
  '--title',
22
22
  ]);
23
23
 
24
- function isESM(scriptPath) {
25
- if (scriptPath.endsWith('.mjs')) return true;
26
- if (scriptPath.endsWith('.cjs')) return false;
27
-
28
- let dir = path.resolve(path.dirname(scriptPath));
29
- while (true) {
30
- const pkgPath = path.join(dir, 'package.json');
31
- if (fs.existsSync(pkgPath)) {
32
- try {
33
- const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
34
- return pkg.type === 'module';
35
- } catch { return false; }
36
- }
37
- const parent = path.dirname(dir);
38
- if (parent === dir) break;
39
- dir = parent;
40
- }
41
- return false;
42
- }
43
-
44
- function resolveSecurenowRegister() {
45
- // When running from the published package, securenow/register resolves to our own register.js.
46
- // When running from the monorepo, resolve relative to this file.
47
- try {
48
- return require.resolve('securenow/register');
49
- } catch {
50
- return path.resolve(__dirname, '..', 'register.js');
51
- }
52
- }
53
-
54
- function resolveFirewallOnly() {
55
- try {
56
- return require.resolve('securenow/firewall-only');
57
- } catch {
58
- return path.resolve(__dirname, '..', 'firewall-only.js');
59
- }
60
- }
61
-
62
- function resolveESMHook() {
63
- // --import uses ESM resolution which requires file:// URLs for absolute paths
64
- // on Windows. Using the bare specifier lets Node's own resolver handle it.
65
- try {
66
- const resolved = require.resolve('@opentelemetry/instrumentation/hook.mjs');
67
- const { pathToFileURL } = require('url');
68
- return pathToFileURL(resolved).href;
69
- } catch {
70
- return '@opentelemetry/instrumentation/hook.mjs';
71
- }
72
- }
73
-
74
- /**
75
- * securenow run [node-flags...] <script> [app-args...]
76
- *
77
- * Spawns node with the correct OTel preload flags.
78
- * Passes through Node flags (--watch, --inspect, etc.) and app arguments.
79
- *
80
- * We re-parse from process.argv directly so that flags like --watch and
81
- * --inspect are forwarded to node verbatim (the CLI's own arg parser
82
- * would otherwise consume them).
83
- */
84
- function run(rawArgs) {
85
- // rawArgs comes directly from process.argv, everything after `run` (or the file path)
86
- if (!rawArgs || rawArgs.length === 0) {
87
- ui.error('Missing script path.');
88
- console.log('');
89
- console.log(` ${ui.c.bold('Usage:')} securenow run [node-flags] <script> [app-args]`);
90
- console.log('');
91
- console.log(` ${ui.c.bold('Examples:')}`);
92
- console.log(` securenow run src/index.js`);
93
- console.log(` securenow run --watch src/index.js`);
94
- console.log(` securenow run --inspect src/server.js --port 3000`);
95
- console.log('');
96
- process.exit(1);
97
- }
98
-
99
- const args = rawArgs;
100
-
101
- // Split into: node flags (before the script), script path, and app args (after)
102
- // --firewall-only is a securenow-specific flag, not a Node flag — consume it.
103
- const nodeFlags = [];
104
- let firewallOnly = false;
105
- let scriptIdx = -1;
106
-
107
- for (let i = 0; i < args.length; i++) {
108
- if (args[i] === '--firewall-only') {
109
- firewallOnly = true;
110
- continue;
111
- }
24
+ function isESM(scriptPath) {
25
+ if (scriptPath.endsWith('.mjs')) return true;
26
+ if (scriptPath.endsWith('.cjs')) return false;
27
+
28
+ let dir = path.resolve(path.dirname(scriptPath));
29
+ while (true) {
30
+ const pkgPath = path.join(dir, 'package.json');
31
+ if (fs.existsSync(pkgPath)) {
32
+ try {
33
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
34
+ return pkg.type === 'module';
35
+ } catch { return false; }
36
+ }
37
+ const parent = path.dirname(dir);
38
+ if (parent === dir) break;
39
+ dir = parent;
40
+ }
41
+ return false;
42
+ }
43
+
44
+ function resolveSecurenowRegister() {
45
+ // When running from the published package, securenow/register resolves to our own register.js.
46
+ // When running from the monorepo, resolve relative to this file.
47
+ try {
48
+ return require.resolve('securenow/register');
49
+ } catch {
50
+ return path.resolve(__dirname, '..', 'register.js');
51
+ }
52
+ }
53
+
54
+ function resolveFirewallOnly() {
55
+ try {
56
+ return require.resolve('securenow/firewall-only');
57
+ } catch {
58
+ return path.resolve(__dirname, '..', 'firewall-only.js');
59
+ }
60
+ }
61
+
62
+ function resolveESMHook() {
63
+ // --import uses ESM resolution which requires file:// URLs for absolute paths
64
+ // on Windows. Using the bare specifier lets Node's own resolver handle it.
65
+ try {
66
+ const resolved = require.resolve('@opentelemetry/instrumentation/hook.mjs');
67
+ const { pathToFileURL } = require('url');
68
+ return pathToFileURL(resolved).href;
69
+ } catch {
70
+ return '@opentelemetry/instrumentation/hook.mjs';
71
+ }
72
+ }
73
+
74
+ /**
75
+ * securenow run [node-flags...] <script> [app-args...]
76
+ *
77
+ * Spawns node with the correct OTel preload flags.
78
+ * Passes through Node flags (--watch, --inspect, etc.) and app arguments.
79
+ *
80
+ * We re-parse from process.argv directly so that flags like --watch and
81
+ * --inspect are forwarded to node verbatim (the CLI's own arg parser
82
+ * would otherwise consume them).
83
+ */
84
+ function run(rawArgs) {
85
+ // rawArgs comes directly from process.argv, everything after `run` (or the file path)
86
+ if (!rawArgs || rawArgs.length === 0) {
87
+ ui.error('Missing script path.');
88
+ console.log('');
89
+ console.log(` ${ui.c.bold('Usage:')} securenow run [node-flags] <script> [app-args]`);
90
+ console.log('');
91
+ console.log(` ${ui.c.bold('Examples:')}`);
92
+ console.log(` securenow run src/index.js`);
93
+ console.log(` securenow run --watch src/index.js`);
94
+ console.log(` securenow run --inspect src/server.js --port 3000`);
95
+ console.log('');
96
+ process.exit(1);
97
+ }
98
+
99
+ const args = rawArgs;
100
+
101
+ // Split into: node flags (before the script), script path, and app args (after)
102
+ // --firewall-only is a securenow-specific flag, not a Node flag — consume it.
103
+ const nodeFlags = [];
104
+ let firewallOnly = false;
105
+ let scriptIdx = -1;
106
+
107
+ for (let i = 0; i < args.length; i++) {
108
+ if (args[i] === '--firewall-only') {
109
+ firewallOnly = true;
110
+ continue;
111
+ }
112
112
  if (args[i] === '--') {
113
113
  scriptIdx = i + 1;
114
114
  break;
@@ -127,47 +127,47 @@ function run(rawArgs) {
127
127
  } else {
128
128
  scriptIdx = i;
129
129
  break;
130
- }
131
- }
132
-
130
+ }
131
+ }
132
+
133
133
  if (scriptIdx === -1 || scriptIdx >= args.length) {
134
134
  ui.error('No script path found. Provide a .js/.mjs/.ts file to run.');
135
135
  process.exit(1);
136
136
  }
137
-
138
- const scriptPath = args[scriptIdx];
139
- const appArgs = args.slice(scriptIdx + 1);
140
-
141
- const esm = isESM(scriptPath);
142
- const preloadPath = firewallOnly ? resolveFirewallOnly() : resolveSecurenowRegister();
143
-
144
- const otelFlags = [];
145
- // ESM hook is only needed for full tracing; firewall-only skips OTel entirely.
146
- if (esm && !firewallOnly) {
147
- otelFlags.push('--import', resolveESMHook());
148
- }
149
- otelFlags.push('--require', preloadPath);
150
-
151
- const finalArgs = [...otelFlags, ...nodeFlags, scriptPath, ...appArgs];
152
-
153
- const nodeExe = process.execPath;
154
-
137
+
138
+ const scriptPath = args[scriptIdx];
139
+ const appArgs = args.slice(scriptIdx + 1);
140
+
141
+ const esm = isESM(scriptPath);
142
+ const preloadPath = firewallOnly ? resolveFirewallOnly() : resolveSecurenowRegister();
143
+
144
+ const otelFlags = [];
145
+ // ESM hook is only needed for full tracing; firewall-only skips OTel entirely.
146
+ if (esm && !firewallOnly) {
147
+ otelFlags.push('--import', resolveESMHook());
148
+ }
149
+ otelFlags.push('--require', preloadPath);
150
+
151
+ const finalArgs = [...otelFlags, ...nodeFlags, scriptPath, ...appArgs];
152
+
153
+ const nodeExe = process.execPath;
154
+
155
155
  const child = spawn(nodeExe, finalArgs, {
156
- stdio: 'inherit',
157
- env: process.env,
158
- cwd: process.cwd(),
159
- });
160
-
161
- child.on('close', (code) => process.exit(code ?? 1));
162
- child.on('error', (err) => {
163
- ui.error(`Failed to start: ${err.message}`);
164
- process.exit(1);
165
- });
166
-
167
- // Forward termination signals to the child
168
- for (const sig of ['SIGINT', 'SIGTERM', 'SIGHUP']) {
169
- process.on(sig, () => child.kill(sig));
170
- }
171
- }
172
-
173
- module.exports = { run };
156
+ stdio: 'inherit',
157
+ env: process.env,
158
+ cwd: process.cwd(),
159
+ });
160
+
161
+ child.on('close', (code) => process.exit(code ?? 1));
162
+ child.on('error', (err) => {
163
+ ui.error(`Failed to start: ${err.message}`);
164
+ process.exit(1);
165
+ });
166
+
167
+ // Forward termination signals to the child
168
+ for (const sig of ['SIGINT', 'SIGTERM', 'SIGHUP']) {
169
+ process.on(sig, () => child.kill(sig));
170
+ }
171
+ }
172
+
173
+ module.exports = { run };