Skip to content

Commit

Permalink
refactor(util): pass in process.platform
Browse files Browse the repository at this point in the history
  • Loading branch information
jsjoeio committed Jan 13, 2022
1 parent 4ee0a77 commit 02944b3
Show file tree
Hide file tree
Showing 8 changed files with 1,204 additions and 2,052 deletions.
5 changes: 0 additions & 5 deletions ci/build/build-standalone-release.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/usr/bin/env bash
set -euo pipefail

# This is due to an upstream issue with RHEL7/CentOS 7 comptability with node-argon2
# TODO@jsjoeio see if we can remove
# See: https://github.com/cdr/code-server/pull/3422#pullrequestreview-677765057
export npm_config_build_from_source=true

main() {
cd "$(dirname "${0}")/../.."

Expand Down
4 changes: 0 additions & 4 deletions ci/build/npm-postinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ detect_arch() {
}

ARCH="${NPM_CONFIG_ARCH:-$(detect_arch)}"
# This is due to an upstream issue with RHEL7/CentOS 7 comptability with node-argon2
# TODO@jsjoeio see if we can remove
# See: https://github.com/cdr/code-server/pull/3422#pullrequestreview-677765057
export npm_config_build_from_source=true

main() {
# Grabs the major version of node from $npm_config_user_agent which looks like
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
},
"dependencies": {
"@coder/logger": "1.1.16",
"@node-rs/argon2": "^1.0.3",
"@node-rs/argon2": "^1.0.5",
"compression": "^1.7.4",
"cookie-parser": "^1.4.5",
"env-paths": "^2.2.0",
Expand Down
10 changes: 5 additions & 5 deletions src/node/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { logger } from "@coder/logger"
import { hash as _hash, verify } from "@node-rs/argon2"
import * as argon2 from "@node-rs/argon2"
import * as cp from "child_process"
import * as crypto from "crypto"
import envPaths from "env-paths"
Expand Down Expand Up @@ -58,10 +58,10 @@ export const paths = getEnvPaths()
* On MacOS this function gets the standard XDG directories instead of using the native macOS
* ones. Most CLIs do this as in practice only GUI apps use the standard macOS directories.
*/
export function getEnvPaths(): Paths {
export function getEnvPaths(platform = process.platform): Paths {
const paths = envPaths("code-server", { suffix: "" })
const append = (p: string): string => path.join(p, "code-server")
switch (process.platform) {
switch (platform) {
case "darwin":
return {
// envPaths uses native directories so force Darwin to use the XDG spec
Expand Down Expand Up @@ -158,7 +158,7 @@ export const generatePassword = async (length = 24): Promise<string> => {
*/
export const hash = async (password: string): Promise<string> => {
try {
return await _hash(password)
return await argon2.hash(password)
} catch (error: any) {
logger.error(error)
return ""
Expand All @@ -173,7 +173,7 @@ export const isHashMatch = async (password: string, hash: string) => {
return false
}
try {
return await verify(hash, password)
return await argon2.verify(hash, password)
} catch (error: any) {
logger.error(error)
return false
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"license": "MIT",
"#": "We must put jest in a sub-directory otherwise VS Code somehow picks up the types and generates conflicts with mocha.",
"devDependencies": {
"@node-rs/argon2": "^1.0.3",
"@node-rs/argon2": "^1.0.5",
"@playwright/test": "^1.16.3",
"@types/jest": "^27.0.2",
"@types/jsdom": "^16.2.13",
Expand Down
70 changes: 7 additions & 63 deletions test/unit/node/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,8 @@ import { generateUuid } from "../../../src/common/util"
import { tmpdir } from "../../../src/node/constants"
import * as util from "../../../src/node/util"

describe.skip("getEnvPaths", () => {
describe("getEnvPaths", () => {
describe("on darwin", () => {
let ORIGINAL_PLATFORM = ""

beforeAll(() => {
ORIGINAL_PLATFORM = process.platform

Object.defineProperty(process, "platform", {
value: "darwin",
})
})

beforeEach(() => {
jest.resetModules()
jest.mock("env-paths", () => {
Expand All @@ -27,23 +17,14 @@ describe.skip("getEnvPaths", () => {
})
})
})

afterAll(() => {
// Restore old platform

Object.defineProperty(process, "platform", {
value: ORIGINAL_PLATFORM,
})
})

it("should return the env paths using xdgBasedir", () => {
jest.mock("xdg-basedir", () => ({
data: "/home/usr/.local/share",
config: "/home/usr/.config",
runtime: "/tmp/runtime",
}))
const getEnvPaths = require("../../../src/node/util").getEnvPaths
const envPaths = getEnvPaths()
const envPaths = getEnvPaths("darwin")

expect(envPaths.data).toEqual("/home/usr/.local/share/code-server")
expect(envPaths.config).toEqual("/home/usr/.config/code-server")
Expand All @@ -53,24 +34,14 @@ describe.skip("getEnvPaths", () => {
it("should return the env paths using envPaths when xdgBasedir is undefined", () => {
jest.mock("xdg-basedir", () => ({}))
const getEnvPaths = require("../../../src/node/util").getEnvPaths
const envPaths = getEnvPaths()
const envPaths = getEnvPaths("darwin")

expect(envPaths.data).toEqual("/home/envPath/.local/share")
expect(envPaths.config).toEqual("/home/envPath/.config")
expect(envPaths.runtime).toEqual("/tmp/envPath/runtime")
})
})
describe("on win32", () => {
let ORIGINAL_PLATFORM = ""

beforeAll(() => {
ORIGINAL_PLATFORM = process.platform

Object.defineProperty(process, "platform", {
value: "win32",
})
})

beforeEach(() => {
jest.resetModules()
jest.mock("env-paths", () => {
Expand All @@ -82,34 +53,16 @@ describe.skip("getEnvPaths", () => {
})
})

afterAll(() => {
// Restore old platform

Object.defineProperty(process, "platform", {
value: ORIGINAL_PLATFORM,
})
})

it("should return the env paths using envPaths", () => {
const getEnvPaths = require("../../../src/node/util").getEnvPaths
const envPaths = getEnvPaths()
const envPaths = getEnvPaths("win32")

expect(envPaths.data).toEqual("/windows/envPath/.local/share")
expect(envPaths.config).toEqual("/windows/envPath/.config")
expect(envPaths.runtime).toEqual("/tmp/envPath/runtime")
})
})
describe("on other platforms", () => {
let ORIGINAL_PLATFORM = ""

beforeAll(() => {
ORIGINAL_PLATFORM = process.platform

Object.defineProperty(process, "platform", {
value: "linux",
})
})

beforeEach(() => {
jest.resetModules()
jest.mock("env-paths", () => {
Expand All @@ -121,20 +74,12 @@ describe.skip("getEnvPaths", () => {
})
})

afterAll(() => {
// Restore old platform

Object.defineProperty(process, "platform", {
value: ORIGINAL_PLATFORM,
})
})

it("should return the runtime using xdgBasedir if it exists", () => {
jest.mock("xdg-basedir", () => ({
runtime: "/tmp/runtime",
}))
const getEnvPaths = require("../../../src/node/util").getEnvPaths
const envPaths = getEnvPaths()
const envPaths = getEnvPaths("linux")

expect(envPaths.data).toEqual("/linux/envPath/.local/share")
expect(envPaths.config).toEqual("/linux/envPath/.config")
Expand All @@ -144,7 +89,7 @@ describe.skip("getEnvPaths", () => {
it("should return the env paths using envPaths when xdgBasedir is undefined", () => {
jest.mock("xdg-basedir", () => ({}))
const getEnvPaths = require("../../../src/node/util").getEnvPaths
const envPaths = getEnvPaths()
const envPaths = getEnvPaths("linux")

expect(envPaths.data).toEqual("/linux/envPath/.local/share")
expect(envPaths.config).toEqual("/linux/envPath/.config")
Expand Down Expand Up @@ -192,10 +137,9 @@ describe("isHashMatch", () => {
const actual = await util.isHashMatch(password, _hash)
expect(actual).toBe(false)
})
it("should return false and not throw an error if the hash doesn't start with a $", async () => {
it("should return false if the hash doesn't start with a $", async () => {
const password = "hellowpasssword"
const _hash = "n2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"
expect(async () => await util.isHashMatch(password, _hash)).not.toThrow()
expect(await util.isHashMatch(password, _hash)).toBe(false)
})
it("should return false if the password and hash don't match", async () => {
Expand Down
Loading

0 comments on commit 02944b3

Please sign in to comment.