-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Correctly quote NODE_OPTIONS when spaces occur in paths
This had a workaround but it turns out that it is in fact our bug. Fixes #122
- Loading branch information
1 parent
c864d36
commit 419c5bb
Showing
4 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export default class NodeOptions extends Array { | ||
constructor(value?: string) { | ||
super(); | ||
if (!value) return; | ||
if (typeof value !== "string") throw new Error("NODE_OPTIONS must be a string"); | ||
|
||
this.push(...parseNodeOptionsEnvVar(value)); | ||
} | ||
|
||
toString(): string { | ||
return this.map(maybeQuote).join(" "); | ||
} | ||
|
||
static get [Symbol.species]() { | ||
return Array; | ||
} | ||
} | ||
|
||
// based on ParseNodeOptionsEnvVar() | ||
// https://github.com/nodejs/node/blob/39f1b899cd536de4d4c9bbf56f24927d8d06999a/src/node_options.cc#L1397 | ||
function parseNodeOptionsEnvVar(nodeOptions: string): string[] { | ||
const result: string[] = []; | ||
let buffer = ""; | ||
let inString = false; | ||
for (let i = 0; i < nodeOptions.length; i++) { | ||
let c = nodeOptions[i]; | ||
if (c === "\\" && inString) { | ||
if (i + 1 === nodeOptions.length) break; | ||
c = nodeOptions[++i]; | ||
} else if (c === " " && !inString) { | ||
result.push(buffer); | ||
buffer = ""; | ||
continue; | ||
} else if (c === '"') { | ||
inString = !inString; | ||
continue; | ||
} | ||
|
||
buffer += c; | ||
} | ||
|
||
result.push(buffer); | ||
return result; | ||
} | ||
|
||
function maybeQuote(value: string): string { | ||
if (value.includes(" ")) return `"${value.replace("\\", "\\\\").replace('"', '\\"')}"`; | ||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import NodeOptions from "../NodeOptions"; | ||
|
||
describe(NodeOptions, () => { | ||
it("splits the options correctly", () => { | ||
const options = new NodeOptions('--option "spaced argument"'); | ||
expect(Array.from(options)).toEqual(["--option", "spaced argument"]); | ||
}); | ||
|
||
it("stringifies spaced options properly", () => { | ||
const opts = new NodeOptions(""); | ||
opts.push("--option", 'spaced \\argu"ment'); | ||
expect(opts.toString()).toBe('--option "spaced \\\\argu\\"ment"'); | ||
expect(Array.from(new NodeOptions(opts.toString()))).toEqual(Array.from(opts)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters