Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d4e848a

Browse files
Share common util code with VS Code
This lets us re-use the normalized base path so when we expire/clear the cookie we use the same base path.
1 parent 6d4c81f commit d4e848a

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

‎lib/vscode/.eslintignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
# These are code-server code symlinks.
2020
src/vs/base/node/proxy_agent.ts
2121
src/vs/ipc.d.ts
22+
src/vs/server/common/util.ts

‎lib/vscode/src/vs/server/browser/client.ts‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ import { Registry } from 'vs/platform/registry/common/platform';
1313
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
1414
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
1515
import { TelemetryChannelClient } from 'vs/server/common/telemetry';
16+
import { getOptions } from 'vs/server/common/util';
1617
import 'vs/workbench/contrib/localizations/browser/localizations.contribution';
1718
import 'vs/workbench/services/localizations/browser/localizationsService';
1819
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
1920

21+
/**
22+
* All client-side customization to VS Code should live in this file when
23+
* possible.
24+
*/
25+
26+
const options = getOptions<Options>();
27+
2028
class TelemetryService extends TelemetryChannelClient {
2129
public constructor(
2230
@IRemoteAgentService remoteAgentService: IRemoteAgentService,
@@ -25,26 +33,6 @@ class TelemetryService extends TelemetryChannelClient {
2533
}
2634
}
2735

28-
/**
29-
* Remove extra slashes in a URL.
30-
*/
31-
export const normalize = (url: string, keepTrailing = false): string => {
32-
return url.replace(/\/\/+/g, '/').replace(/\/+$/, keepTrailing ? '/' : '');
33-
};
34-
35-
/**
36-
* Get options embedded in the HTML.
37-
*/
38-
export const getOptions = <T extends Options>(): T => {
39-
try {
40-
return JSON.parse(document.getElementById('coder-options')!.getAttribute('data-settings')!);
41-
} catch (error) {
42-
return {} as T;
43-
}
44-
};
45-
46-
const options = getOptions();
47-
4836
const TELEMETRY_SECTION_ID = 'telemetry';
4937
Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerConfiguration({
5038
'id': TELEMETRY_SECTION_ID,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../../src/common/util.ts

‎src/browser/register.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { logger } from "@coder/logger"
12
import { getOptions, normalize, logError } from "../common/util"
23

34
import "./pages/error.css"
@@ -6,19 +7,21 @@ import "./pages/login.css"
67

78
export async function registerServiceWorker(): Promise<void> {
89
const options = getOptions()
10+
logger.level = options.logLevel
11+
912
const path = normalize(`${options.csStaticBase}/dist/serviceWorker.js`)
1013
try {
1114
await navigator.serviceWorker.register(path, {
1215
scope: options.base + "/",
1316
})
1417
console.log("[Service Worker] registered")
1518
} catch (error) {
16-
logError(`[Service Worker] registration`, error)
19+
logError(logger,`[Service Worker] registration`, error)
1720
}
1821
}
1922

2023
if (typeof navigator !== "undefined" && "serviceWorker" in navigator) {
2124
registerServiceWorker()
2225
} else {
23-
console.error(`[Service Worker] navigator is undefined`)
26+
logger.error(`[Service Worker] navigator is undefined`)
2427
}

‎src/common/util.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { logger, field } from "@coder/logger"
1+
/*
2+
* This file exists in two locations:
3+
* - src/common/util.ts
4+
* - lib/vscode/src/vs/server/common/util.ts
5+
* The second is a symlink to the first.
6+
*/
27

8+
/**
9+
* Base options included on every page.
10+
*/
311
export interface Options {
412
base: string
513
csStaticBase: string
@@ -78,13 +86,9 @@ export const getOptions = <T extends Options>(): T => {
7886
}
7987
}
8088

81-
logger.level = options.logLevel
82-
8389
options.base = resolveBase(options.base)
8490
options.csStaticBase = resolveBase(options.csStaticBase)
8591

86-
logger.debug("got options", field("options", options))
87-
8892
return options
8993
}
9094

@@ -113,7 +117,8 @@ export const getFirstString = (value: string | string[] | object | undefined): s
113117
return typeof value === "string" ? value : undefined
114118
}
115119

116-
export function logError(prefix: string, err: any): void {
120+
// TODO: Might make sense to add Error handling to the logger itself.
121+
export function logError(logger: { error: (msg: string) => void }, prefix: string, err: Error | string): void {
117122
if (err instanceof Error) {
118123
logger.error(`${prefix}: ${err.message} ${err.stack}`)
119124
} else {

‎src/node/app.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const createApp = async (args: DefaultedArgs): Promise<[Express, Express,
3737
reject(err)
3838
} else {
3939
// Promise resolved earlier so this is an unrelated error.
40-
util.logError("http server error", err)
40+
util.logError(logger,"http server error", err)
4141
}
4242
})
4343

‎typings/ipc.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
export interface Options {
99
authed: boolean
1010
base: string
11+
csStaticBase: string
1112
disableTelemetry: boolean
1213
disableUpdateCheck: boolean
14+
logLevel: number
1315
}
1416

1517
export interface InitMessage {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /