Skip to content

Commit 2accb1a

Browse files
authored
Merge 9af2112 into 825a40e
2 parents 825a40e + 9af2112 commit 2accb1a

File tree

5 files changed

+184
-23
lines changed

5 files changed

+184
-23
lines changed

.github/PULL_REQUEST_TEMPLATE/pull_request_template.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Please link to the issue this PR solves.
33
If there is no existing issue, please first create one unless the fix is minor.
44
5-
Please make sure the base of your PR is the master branch!
5+
Please make sure the base of your PR is the default branch!
66
-->
77

88
## Checklist

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ VS Code v1.56
7070

7171
### Development
7272

73-
- item
73+
- fix(socket): use xdgBasedir.runtime instead of tmp #3304 @jsjoeio
7474

7575
## 3.10.0
7676

src/node/socket.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import * as path from "path"
44
import * as tls from "tls"
55
import { Emitter } from "../common/emitter"
66
import { generateUuid } from "../common/util"
7-
import { tmpdir } from "./constants"
8-
import { canConnect } from "./util"
7+
import { canConnect, paths } from "./util"
98

109
/**
1110
* Provides a way to proxy a TLS socket. Can be used when you need to pass a
1211
* socket to a child process since you can't pass the TLS socket.
1312
*/
1413
export class SocketProxyProvider {
1514
private readonly onProxyConnect = new Emitter<net.Socket>()
16-
private proxyPipe = path.join(tmpdir, "tls-proxy")
15+
private proxyPipe = path.join(paths.runtime, "tls-proxy")
1716
private _proxyServer?: Promise<net.Server>
1817
private readonly proxyTimeout = 5000
1918

@@ -76,7 +75,10 @@ export class SocketProxyProvider {
7675
this._proxyServer = this.findFreeSocketPath(this.proxyPipe)
7776
.then((pipe) => {
7877
this.proxyPipe = pipe
79-
return Promise.all([fs.mkdir(tmpdir, { recursive: true }), fs.rmdir(this.proxyPipe, { recursive: true })])
78+
return Promise.all([
79+
fs.mkdir(path.dirname(this.proxyPipe), { recursive: true }),
80+
fs.rmdir(this.proxyPipe, { recursive: true }),
81+
])
8082
})
8183
.then(() => {
8284
return new Promise((resolve) => {

src/node/util.ts

+29-17
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import * as path from "path"
88
import * as util from "util"
99
import xdgBasedir from "xdg-basedir"
1010

11-
interface Paths {
11+
export interface Paths {
1212
data: string
1313
config: string
14+
runtime: string
1415
}
1516

1617
export const paths = getEnvPaths()
@@ -20,23 +21,34 @@ export const paths = getEnvPaths()
2021
* On MacOS this function gets the standard XDG directories instead of using the native macOS
2122
* ones. Most CLIs do this as in practice only GUI apps use the standard macOS directories.
2223
*/
23-
function getEnvPaths(): Paths {
24-
let paths: Paths
25-
if (process.platform === "win32") {
26-
paths = envPaths("code-server", {
27-
suffix: "",
28-
})
29-
} else {
30-
if (xdgBasedir.data === undefined || xdgBasedir.config === undefined) {
31-
throw new Error("No home folder?")
32-
}
33-
paths = {
34-
data: path.join(xdgBasedir.data, "code-server"),
35-
config: path.join(xdgBasedir.config, "code-server"),
36-
}
24+
export function getEnvPaths(): Paths {
25+
const paths = envPaths("code-server", { suffix: "" })
26+
const append = (p: string): string => path.join(p, "code-server")
27+
switch (process.platform) {
28+
case "darwin":
29+
return {
30+
// envPaths uses native directories so force Darwin to use the XDG spec
31+
// to align with other CLI tools.
32+
data: xdgBasedir.data ? append(xdgBasedir.data) : paths.data,
33+
config: xdgBasedir.config ? append(xdgBasedir.config) : paths.config,
34+
// Fall back to temp if there is no runtime dir.
35+
runtime: xdgBasedir.runtime ? append(xdgBasedir.runtime) : paths.temp,
36+
}
37+
case "win32":
38+
return {
39+
data: paths.data,
40+
config: paths.config,
41+
// Windows doesn't have a runtime dir.
42+
runtime: paths.temp,
43+
}
44+
default:
45+
return {
46+
data: paths.data,
47+
config: paths.config,
48+
// Fall back to temp if there is no runtime dir.
49+
runtime: xdgBasedir.runtime ? append(xdgBasedir.runtime) : paths.temp,
50+
}
3751
}
38-
39-
return paths
4052
}
4153

4254
/**

test/unit/node/util.test.ts

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
describe("getEnvPaths", () => {
2+
describe("on darwin", () => {
3+
let ORIGINAL_PLATFORM = ""
4+
5+
beforeAll(() => {
6+
ORIGINAL_PLATFORM = process.platform
7+
8+
Object.defineProperty(process, "platform", {
9+
value: "darwin",
10+
})
11+
})
12+
13+
beforeEach(() => {
14+
jest.resetModules()
15+
jest.mock("env-paths", () => {
16+
return () => ({
17+
data: "/home/envPath/.local/share",
18+
config: "/home/envPath/.config",
19+
temp: "/tmp/envPath/runtime",
20+
})
21+
})
22+
})
23+
24+
afterAll(() => {
25+
// Restore old platform
26+
27+
Object.defineProperty(process, "platform", {
28+
value: ORIGINAL_PLATFORM,
29+
})
30+
})
31+
32+
it("should return the env paths using xdgBasedir", () => {
33+
jest.mock("xdg-basedir", () => ({
34+
data: "/home/usr/.local/share",
35+
config: "/home/usr/.config",
36+
runtime: "/tmp/runtime",
37+
}))
38+
const getEnvPaths = require("../../../src/node/util").getEnvPaths
39+
const envPaths = getEnvPaths()
40+
41+
expect(envPaths.data).toEqual("/home/usr/.local/share/code-server")
42+
expect(envPaths.config).toEqual("/home/usr/.config/code-server")
43+
expect(envPaths.runtime).toEqual("/tmp/runtime/code-server")
44+
})
45+
46+
it("should return the env paths using envPaths when xdgBasedir is undefined", () => {
47+
jest.mock("xdg-basedir", () => ({}))
48+
const getEnvPaths = require("../../../src/node/util").getEnvPaths
49+
const envPaths = getEnvPaths()
50+
51+
expect(envPaths.data).toEqual("/home/envPath/.local/share")
52+
expect(envPaths.config).toEqual("/home/envPath/.config")
53+
expect(envPaths.runtime).toEqual("/tmp/envPath/runtime")
54+
})
55+
})
56+
describe("on win32", () => {
57+
let ORIGINAL_PLATFORM = ""
58+
59+
beforeAll(() => {
60+
ORIGINAL_PLATFORM = process.platform
61+
62+
Object.defineProperty(process, "platform", {
63+
value: "win32",
64+
})
65+
})
66+
67+
beforeEach(() => {
68+
jest.resetModules()
69+
jest.mock("env-paths", () => {
70+
return () => ({
71+
data: "/windows/envPath/.local/share",
72+
config: "/windows/envPath/.config",
73+
temp: "/tmp/envPath/runtime",
74+
})
75+
})
76+
})
77+
78+
afterAll(() => {
79+
// Restore old platform
80+
81+
Object.defineProperty(process, "platform", {
82+
value: ORIGINAL_PLATFORM,
83+
})
84+
})
85+
86+
it("should return the env paths using envPaths", () => {
87+
const getEnvPaths = require("../../../src/node/util").getEnvPaths
88+
const envPaths = getEnvPaths()
89+
90+
expect(envPaths.data).toEqual("/windows/envPath/.local/share")
91+
expect(envPaths.config).toEqual("/windows/envPath/.config")
92+
expect(envPaths.runtime).toEqual("/tmp/envPath/runtime")
93+
})
94+
})
95+
describe("on other platforms", () => {
96+
let ORIGINAL_PLATFORM = ""
97+
98+
beforeAll(() => {
99+
ORIGINAL_PLATFORM = process.platform
100+
101+
Object.defineProperty(process, "platform", {
102+
value: "linux",
103+
})
104+
})
105+
106+
beforeEach(() => {
107+
jest.resetModules()
108+
jest.mock("env-paths", () => {
109+
return () => ({
110+
data: "/linux/envPath/.local/share",
111+
config: "/linux/envPath/.config",
112+
temp: "/tmp/envPath/runtime",
113+
})
114+
})
115+
})
116+
117+
afterAll(() => {
118+
// Restore old platform
119+
120+
Object.defineProperty(process, "platform", {
121+
value: ORIGINAL_PLATFORM,
122+
})
123+
})
124+
125+
it("should return the runtime using xdgBasedir if it exists", () => {
126+
jest.mock("xdg-basedir", () => ({
127+
runtime: "/tmp/runtime",
128+
}))
129+
const getEnvPaths = require("../../../src/node/util").getEnvPaths
130+
const envPaths = getEnvPaths()
131+
132+
expect(envPaths.data).toEqual("/linux/envPath/.local/share")
133+
expect(envPaths.config).toEqual("/linux/envPath/.config")
134+
expect(envPaths.runtime).toEqual("/tmp/runtime/code-server")
135+
})
136+
137+
it("should return the env paths using envPaths when xdgBasedir is undefined", () => {
138+
jest.mock("xdg-basedir", () => ({}))
139+
const getEnvPaths = require("../../../src/node/util").getEnvPaths
140+
const envPaths = getEnvPaths()
141+
142+
expect(envPaths.data).toEqual("/linux/envPath/.local/share")
143+
expect(envPaths.config).toEqual("/linux/envPath/.config")
144+
expect(envPaths.runtime).toEqual("/tmp/envPath/runtime")
145+
})
146+
})
147+
})

0 commit comments

Comments
 (0)