Skip to content

Commit db5f86f

Browse files
committed
Move splitOnFirstEquals to util
I will be making use of this to parse the forwarded header.
1 parent a47cd81 commit db5f86f

File tree

4 files changed

+39
-40
lines changed

4 files changed

+39
-40
lines changed

src/node/cli.ts

+1-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { promises as fs } from "fs"
33
import { load } from "js-yaml"
44
import * as os from "os"
55
import * as path from "path"
6-
import { canConnect, generateCertificate, generatePassword, humanPath, paths, isNodeJSErrnoException } from "./util"
6+
import { canConnect, generateCertificate, generatePassword, humanPath, paths, isNodeJSErrnoException, splitOnFirstEquals } from "./util"
77

88
const DEFAULT_SOCKET_PATH = path.join(os.tmpdir(), "vscode-ipc")
99

@@ -292,19 +292,6 @@ export const optionDescriptions = (opts: Partial<Options<Required<UserProvidedAr
292292
})
293293
}
294294

295-
export function splitOnFirstEquals(str: string): string[] {
296-
// we use regex instead of "=" to ensure we split at the first
297-
// "=" and return the following substring with it
298-
// important for the hashed-password which looks like this
299-
// $argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY
300-
// 2 means return two items
301-
// Source: https://stackoverflow.com/a/4607799/3015595
302-
// We use the ? to say the the substr after the = is optional
303-
const split = str.split(/=(.+)?/, 2)
304-
305-
return split
306-
}
307-
308295
/**
309296
* Parse arguments into UserProvidedArgs. This should not go beyond checking
310297
* that arguments are valid types and have values when required.

src/node/util.ts

+13
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,16 @@ export const loadAMDModule = async <T>(amdPath: string, exportName: string): Pro
541541

542542
return module[exportName] as T
543543
}
544+
545+
export function splitOnFirstEquals(str: string): string[] {
546+
// we use regex instead of "=" to ensure we split at the first
547+
// "=" and return the following substring with it
548+
// important for the hashed-password which looks like this
549+
// $argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY
550+
// 2 means return two items
551+
// Source: https://stackoverflow.com/a/4607799/3015595
552+
// We use the ? to say the the substr after the = is optional
553+
const split = str.split(/=(.+)?/, 2)
554+
555+
return split
556+
}

test/unit/node/cli.test.ts

-26
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
readSocketPath,
1212
setDefaults,
1313
shouldOpenInExistingInstance,
14-
splitOnFirstEquals,
1514
toCodeArgs,
1615
optionDescriptions,
1716
options,
@@ -535,31 +534,6 @@ describe("cli", () => {
535534
})
536535
})
537536

538-
describe("splitOnFirstEquals", () => {
539-
it("should split on the first equals", () => {
540-
const testStr = "enabled-proposed-api=test=value"
541-
const actual = splitOnFirstEquals(testStr)
542-
const expected = ["enabled-proposed-api", "test=value"]
543-
expect(actual).toEqual(expect.arrayContaining(expected))
544-
})
545-
it("should split on first equals regardless of multiple equals signs", () => {
546-
const testStr =
547-
"hashed-password=$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY"
548-
const actual = splitOnFirstEquals(testStr)
549-
const expected = [
550-
"hashed-password",
551-
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
552-
]
553-
expect(actual).toEqual(expect.arrayContaining(expected))
554-
})
555-
it("should always return the first element before an equals", () => {
556-
const testStr = "auth="
557-
const actual = splitOnFirstEquals(testStr)
558-
const expected = ["auth"]
559-
expect(actual).toEqual(expect.arrayContaining(expected))
560-
})
561-
})
562-
563537
describe("shouldSpawnCliProcess", () => {
564538
it("should return false if no 'extension' related args passed in", async () => {
565539
const args = {}

test/unit/node/util.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,28 @@ describe("constructOpenOptions", () => {
601601
expect(urlSearch).toBe("?q=^&test")
602602
})
603603
})
604+
605+
describe("splitOnFirstEquals", () => {
606+
it("should split on the first equals", () => {
607+
const testStr = "enabled-proposed-api=test=value"
608+
const actual = util.splitOnFirstEquals(testStr)
609+
const expected = ["enabled-proposed-api", "test=value"]
610+
expect(actual).toEqual(expect.arrayContaining(expected))
611+
})
612+
it("should split on first equals regardless of multiple equals signs", () => {
613+
const testStr =
614+
"hashed-password=$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY"
615+
const actual = util.splitOnFirstEquals(testStr)
616+
const expected = [
617+
"hashed-password",
618+
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
619+
]
620+
expect(actual).toEqual(expect.arrayContaining(expected))
621+
})
622+
it("should always return the first element before an equals", () => {
623+
const testStr = "auth="
624+
const actual = util.splitOnFirstEquals(testStr)
625+
const expected = ["auth"]
626+
expect(actual).toEqual(expect.arrayContaining(expected))
627+
})
628+
})

0 commit comments

Comments
 (0)