find-duplicate-js 1.6.0 → 1.6.1

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.
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { exec as open } from "child_process";
4
3
  import http from "http";
5
4
  import fs from "fs";
6
5
  import path from "path";
@@ -138,34 +137,83 @@ const server = http.createServer((req, res) => {
138
137
  try {
139
138
  const params = new URLSearchParams(req.url.split("?")[1]);
140
139
  const filePath = params.get("path");
141
- const line = params.get("line") || "1";
140
+ const lineParam = params.get("line") || "1";
142
141
 
143
- if (filePath) {
144
- // Open file in VSCode using 'code' command
145
- // The filePath from findDuplicates is already absolute
146
- const absolutePath = path.resolve(filePath);
147
- const command = `code --goto "${absolutePath}:${line}"`;
142
+ if (!filePath) {
143
+ res.writeHead(400, { "Content-Type": "text/plain" });
144
+ res.end("Missing file path");
145
+ return;
146
+ }
147
+
148
+ // Security: Validate line number is actually a number
149
+ const line = parseInt(lineParam, 10);
150
+ if (isNaN(line) || line < 1) {
151
+ res.writeHead(400, { "Content-Type": "text/plain" });
152
+ res.end("Invalid line number");
153
+ return;
154
+ }
155
+
156
+ // Security: Resolve and normalize the path
157
+ const absolutePath = path.resolve(filePath);
158
+
159
+ // Security: Verify the file exists and is within allowed directory
160
+ if (!fs.existsSync(absolutePath)) {
161
+ res.writeHead(404, { "Content-Type": "text/plain" });
162
+ res.end("File not found");
163
+ return;
164
+ }
165
+
166
+ // Security: Check if it's actually a file (not a directory)
167
+ const stats = fs.statSync(absolutePath);
168
+ if (!stats.isFile()) {
169
+ res.writeHead(400, { "Content-Type": "text/plain" });
170
+ res.end("Path is not a file");
171
+ return;
172
+ }
173
+
174
+ // Security: Use array syntax to prevent command injection
175
+ console.log(`📂 Opening: ${absolutePath}:${line}`);
176
+
177
+ // Use spawn instead of exec for better security (prevents command injection)
178
+ import('child_process').then(({ spawn, exec }) => {
179
+ // Try using spawn first (more secure)
180
+ const child = spawn('code', ['--goto', `${absolutePath}:${line}`], {
181
+ detached: true,
182
+ stdio: 'ignore',
183
+ shell: false
184
+ });
148
185
 
149
- console.log(`📂 Opening: ${absolutePath}:${line}`);
186
+ child.unref();
150
187
 
151
- open(command, (error) => {
152
- if (error) {
153
- console.error(" Error opening file:", error);
154
- } else {
155
- console.log("✅ File opened successfully");
156
- }
188
+ child.on('error', (spawnError) => {
189
+ // Fallback to exec if spawn fails (e.g., code not in PATH on Windows)
190
+ console.log("Trying alternative method to open VSCode...");
191
+ const command = process.platform === 'win32'
192
+ ? `code --goto "${absolutePath}:${line}"`
193
+ : `code --goto '${absolutePath}:${line}'`;
194
+
195
+ exec(command, (execError) => {
196
+ if (execError) {
197
+ console.error("❌ Error opening file:", execError.message);
198
+ console.log("💡 Make sure VSCode is installed and 'code' command is available in PATH");
199
+ } else {
200
+ console.log("✅ File opened successfully");
201
+ }
202
+ });
157
203
  });
158
204
 
159
- res.writeHead(200, { "Content-Type": "text/plain" });
160
- res.end("File opened in VSCode");
161
- } else {
162
- res.writeHead(400, { "Content-Type": "text/plain" });
163
- res.end("Missing file path");
164
- }
205
+ // If no error, spawn succeeded
206
+ setTimeout(() => console.log("File opened successfully"), 100);
207
+ }).catch(error => {
208
+ console.error("❌ Error loading child_process:", error);
209
+ });
210
+
211
+ res.writeHead(200, { "Content-Type": "text/plain" });
212
+ res.end("File opened in VSCode");
165
213
  } catch (error) {
166
214
  console.error("❌ Error opening file:", error);
167
215
  res.writeHead(500, { "Content-Type": "text/plain" });
168
- res.end(`Error: ${error.message}`);
216
+ res.end("Internal server error");
169
217
  }
170
218
  } else {
171
219
  res.writeHead(404);
@@ -180,15 +228,24 @@ server.listen(PORT, () => {
180
228
 
181
229
  // Try to open browser automatically
182
230
  const url = `http://localhost:${PORT}`;
183
-
184
- switch (process.platform) {
185
- case "win32":
186
- open(`start ${url}`);
187
- break;
188
- case "darwin":
189
- open(`open ${url}`);
190
- break;
191
- default:
192
- open(`xdg-open ${url}`);
193
- }
231
+
232
+ import('child_process').then(({ exec }) => {
233
+ let command;
234
+ switch (process.platform) {
235
+ case "win32":
236
+ command = `start ${url}`;
237
+ break;
238
+ case "darwin":
239
+ command = `open ${url}`;
240
+ break;
241
+ default:
242
+ command = `xdg-open ${url}`;
243
+ }
244
+
245
+ exec(command, (error) => {
246
+ if (error) {
247
+ console.log("Note: Could not open browser automatically. Please open manually.");
248
+ }
249
+ });
250
+ });
194
251
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "find-duplicate-js",
3
- "version": "1.6.0",
3
+ "version": "1.6.1",
4
4
  "description": "A tool to find duplicate code in JavaScript and TypeScript functions with smart JSX/TSX component detection",
5
5
  "main": "find-duplicates.js",
6
6
  "type": "module",