Skip to content

Commit 8ff9647

Browse files
committed
Add http server.
1 parent b680e3b commit 8ff9647

File tree

6 files changed

+217
-116
lines changed

6 files changed

+217
-116
lines changed

lib/vscode

Submodule vscode updated 1307 files

patches/store-socket.diff

+40-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Index: code-server/lib/vscode/src/vs/workbench/api/node/extHostExtensionService.
1818
* Licensed under the MIT License. See License.txt in the project root for license information.
1919
*--------------------------------------------------------------------------------------------*/
2020
-
21-
+import { promises as fs } from 'fs';
22-
+import * as os from 'os'
21+
+import * as os from 'os';
22+
+import * as _http from 'http';
2323
+import * as path from 'vs/base/common/path';
2424
import * as performance from 'vs/base/common/performance';
2525
import { createApiFactoryAndRegisterActors } from 'vs/workbench/api/common/extHost.api.impl';
@@ -32,12 +32,12 @@ Index: code-server/lib/vscode/src/vs/workbench/api/node/extHostExtensionService.
3232

3333
class NodeModuleRequireInterceptor extends RequireInterceptor {
3434

35-
@@ -79,6 +82,24 @@ export class ExtHostExtensionService ext
35+
@@ -79,6 +82,59 @@ export class ExtHostExtensionService ext
3636
await interceptor.install();
3737
performance.mark('code/extHost/didInitAPI');
3838

3939
+ (async () => {
40-
+ let socketPath = process.env['VSCODE_IPC_HOOK_CLI'];
40+
+ const socketPath = process.env['VSCODE_IPC_HOOK_CLI'];
4141
+ if (!socketPath) {
4242
+ return;
4343
+ }
@@ -49,7 +49,42 @@ Index: code-server/lib/vscode/src/vs/workbench/api/node/extHostExtensionService.
4949
+ workspace,
5050
+ socketPath
5151
+ };
52-
+ fs.appendFile(path.join(os.tmpdir(), 'vscode-ipc'), '\n' + JSON.stringify(entry), 'utf-8');
52+
+ const message = JSON.stringify({entry});
53+
+ const codeServerSocketPath = path.join(os.tmpdir(), 'code-server-ipc.sock');
54+
+ await new Promise<void>((resolve, reject) => {
55+
+ const opts: _http.RequestOptions = {
56+
+ path: '/session',
57+
+ socketPath: codeServerSocketPath,
58+
+ method: 'POST',
59+
+ headers: {
60+
+ 'content-type': 'application/json',
61+
+ 'accept': 'application/json'
62+
+ }
63+
+ };
64+
+ const req = _http.request(opts, (res) => {
65+
+ if (res.headers['content-type'] !== 'application/json') {
66+
+ reject('Error in response: Invalid content type: Expected \'application/json\', is: ' + res.headers['content-type']);
67+
+ return;
68+
+ }
69+
+
70+
+ res.setEncoding('utf8');
71+
+ res.on('error', reject);
72+
+ res.on('end', () => {
73+
+ try {
74+
+ if (res.statusCode === 200) {
75+
+ resolve();
76+
+ } else {
77+
+ reject(new Error('Unexpected status code: ' + res.statusCode));
78+
+ }
79+
+ } catch (e: unknown) {
80+
+ reject(e);
81+
+ }
82+
+ });
83+
+ });
84+
+ req.on('error', reject);
85+
+ req.write(message);
86+
+ req.end();
87+
+ });
5388
+ })().catch(error => {
5489
+ this._logService.error(error);
5590
+ });

src/node/app.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as util from "../common/util"
99
import { DefaultedArgs } from "./cli"
1010
import { disposer } from "./http"
1111
import { isNodeJSErrnoException } from "./util"
12+
import { DEFAULT_SOCKET_PATH, EditorSessionManager, makeEditorSessionManagerServer } from "./vscodeSocket"
1213
import { handleUpgrade } from "./wsRouter"
1314

1415
type ListenOptions = Pick<DefaultedArgs, "socket-mode" | "socket" | "port" | "host">
@@ -20,6 +21,8 @@ export interface App extends Disposable {
2021
wsRouter: Express
2122
/** The underlying HTTP server. */
2223
server: http.Server
24+
/** Handles requests to the editor session management API. */
25+
editorSessionManagerServer: http.Server
2326
}
2427

2528
export const listen = async (server: http.Server, { host, port, socket, "socket-mode": mode }: ListenOptions) => {
@@ -77,7 +80,10 @@ export const createApp = async (args: DefaultedArgs): Promise<App> => {
7780
const wsRouter = express()
7881
handleUpgrade(wsRouter, server)
7982

80-
return { router, wsRouter, server, dispose }
83+
const editorSessionManager = new EditorSessionManager()
84+
const editorSessionManagerServer = await makeEditorSessionManagerServer(DEFAULT_SOCKET_PATH, editorSessionManager)
85+
86+
return { router, wsRouter, server, dispose, editorSessionManagerServer }
8187
}
8288

8389
/**

src/node/cli.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ 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 { generateCertificate, generatePassword, humanPath, paths, splitOnFirstEquals } from "./util"
7-
import { DEFAULT_SOCKET_PATH, VscodeSocketResolver } from "./vscodeSocket"
86
import { getResolvedPathsFromArgs } from "./main"
7+
import { generateCertificate, generatePassword, humanPath, paths, splitOnFirstEquals } from "./util"
8+
import { DEFAULT_SOCKET_PATH, EditorSessionManagerClient } from "./vscodeSocket"
99

1010
export enum Feature {
1111
// No current experimental features!
@@ -734,7 +734,12 @@ export const shouldOpenInExistingInstance = async (args: UserProvidedArgs): Prom
734734
}
735735

736736
const paths = getResolvedPathsFromArgs(args)
737-
const resolver = new VscodeSocketResolver(DEFAULT_SOCKET_PATH)
737+
const resolver = new EditorSessionManagerClient(DEFAULT_SOCKET_PATH)
738+
739+
// If we can't connect to the socket then there's no existing instance.
740+
if (!(await resolver.canConnect())) {
741+
return undefined
742+
}
738743

739744
// If these flags are set then assume the user is trying to open in an
740745
// existing instance since these flags have no effect otherwise.
@@ -743,8 +748,7 @@ export const shouldOpenInExistingInstance = async (args: UserProvidedArgs): Prom
743748
}, 0)
744749
if (openInFlagCount > 0) {
745750
logger.debug("Found --reuse-window or --new-window")
746-
await resolver.load()
747-
return await resolver.getConnectedSocketPath(paths)
751+
return await resolver.getConnectedSocketPath(paths[0])
748752
}
749753

750754
// It's possible the user is trying to spawn another instance of code-server.
@@ -753,8 +757,7 @@ export const shouldOpenInExistingInstance = async (args: UserProvidedArgs): Prom
753757
// 2. That a file or directory was passed.
754758
// 3. That the socket is active.
755759
if (Object.keys(args).length === 1 && typeof args._ !== "undefined" && args._.length > 0) {
756-
await resolver.load()
757-
const socketPath = await resolver.getConnectedSocketPath(paths)
760+
const socketPath = await resolver.getConnectedSocketPath(paths[0])
758761
if (socketPath) {
759762
logger.debug("Found existing code-server socket")
760763
return socketPath

0 commit comments

Comments
 (0)