Skip to content

Commit 6cb2280

Browse files
committed
Add base path to update endpoint from VS Code
This will make it work regardless of what the current URL happens to be. Also move the telemetry setting into the options since we might as well make use of it seeing as how we have to parse it for the base path anyway.
1 parent a00fa85 commit 6cb2280

File tree

4 files changed

+74
-24
lines changed

4 files changed

+74
-24
lines changed

ci/vscode.patch

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,10 @@ index eab8591492..26668701f7 100644
486486
options.logService.error(`${logPrefix} socketFactory.connect() failed. Error:`);
487487
diff --git a/src/vs/server/browser/client.ts b/src/vs/server/browser/client.ts
488488
new file mode 100644
489-
index 0000000000..3f53907de0
489+
index 0000000000..4042e32f74
490490
--- /dev/null
491491
+++ b/src/vs/server/browser/client.ts
492-
@@ -0,0 +1,227 @@
492+
@@ -0,0 +1,263 @@
493493
+import { Emitter } from 'vs/base/common/event';
494494
+import { URI } from 'vs/base/common/uri';
495495
+import { localize } from 'vs/nls';
@@ -507,6 +507,7 @@ index 0000000000..3f53907de0
507507
+import 'vs/workbench/contrib/localizations/browser/localizations.contribution';
508508
+import { LocalizationsService } from 'vs/workbench/services/localizations/electron-browser/localizationsService';
509509
+import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
510+
+import { Options } from 'vs/server/ipc.d';
510511
+
511512
+class TelemetryService extends TelemetryChannelClient {
512513
+ public constructor(
@@ -516,11 +517,46 @@ index 0000000000..3f53907de0
516517
+ }
517518
+}
518519
+
519-
+const TELEMETRY_SECTION_ID = 'telemetry';
520+
+/**
521+
+ * Remove extra slashes in a URL.
522+
+ */
523+
+export const normalize = (url: string, keepTrailing = false): string => {
524+
+ return url.replace(/\/\/+/g, "/").replace(/\/+$/, keepTrailing ? "/" : "");
525+
+};
526+
+
527+
+/**
528+
+ * Get options embedded in the HTML from the server.
529+
+ */
530+
+export const getOptions = <T extends Options>(): T => {
531+
+ if (typeof document === "undefined") {
532+
+ return {} as T;
533+
+ }
534+
+ const el = document.getElementById("coder-options");
535+
+ try {
536+
+ if (!el) {
537+
+ throw new Error("no options element");
538+
+ }
539+
+ const value = el.getAttribute("data-settings");
540+
+ if (!value) {
541+
+ throw new Error("no options value");
542+
+ }
543+
+ const options = JSON.parse(value);
544+
+ const parts = window.location.pathname.replace(/^\//g, "").split("/");
545+
+ parts[parts.length - 1] = options.base;
546+
+ const url = new URL(window.location.origin + "/" + parts.join("/"));
547+
+ return {
548+
+ ...options,
549+
+ base: normalize(url.pathname, true),
550+
+ };
551+
+ } catch (error) {
552+
+ console.warn(error);
553+
+ return {} as T;
554+
+ }
555+
+};
520556
+
521-
+const el = document.getElementById("vscode-disable-telemetry");
522-
+const disableTelemetry = el && el.getAttribute("data-value") === "true" || false;
557+
+const options = getOptions();
523558
+
559+
+const TELEMETRY_SECTION_ID = 'telemetry';
524560
+Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerConfiguration({
525561
+ 'id': TELEMETRY_SECTION_ID,
526562
+ 'order': 110,
@@ -530,7 +566,7 @@ index 0000000000..3f53907de0
530566
+ 'telemetry.enableTelemetry': {
531567
+ 'type': 'boolean',
532568
+ 'description': localize('telemetry.enableTelemetry', 'Enable usage data and errors to be sent to a Microsoft online service.'),
533-
+ 'default': !disableTelemetry,
569+
+ 'default': !options.disableTelemetry,
534570
+ 'tags': ['usesOnlineServices']
535571
+ }
536572
+ }
@@ -625,7 +661,7 @@ index 0000000000..3f53907de0
625661
+ const applyUpdate = async (): Promise<void> => {
626662
+ (services.get(ILogService) as ILogService).debug("Applying update...");
627663
+
628-
+ const response = await fetch("./update/apply", {
664+
+ const response = await fetch(normalize(`${options.base}/update/apply`), {
629665
+ headers: { "content-type": "application/json" },
630666
+ });
631667
+ if (response.status !== 200) {
@@ -643,7 +679,7 @@ index 0000000000..3f53907de0
643679
+ const getUpdate = async (): Promise<void> => {
644680
+ (services.get(ILogService) as ILogService).debug("Checking for update...");
645681
+
646-
+ const response = await fetch("./update", {
682+
+ const response = await fetch(normalize(`${options.base}/update`), {
647683
+ headers: { "content-type": "application/json" },
648684
+ });
649685
+ if (response.status !== 200) {
@@ -1076,14 +1112,20 @@ index 0000000000..56331ff1fc
10761112
+require('../../bootstrap-amd').load('vs/server/entry');
10771113
diff --git a/src/vs/server/ipc.d.ts b/src/vs/server/ipc.d.ts
10781114
new file mode 100644
1079-
index 0000000000..a0d1d0df54
1115+
index 0000000000..15d2ba55d1
10801116
--- /dev/null
10811117
+++ b/src/vs/server/ipc.d.ts
1082-
@@ -0,0 +1,108 @@
1118+
@@ -0,0 +1,114 @@
10831119
+/**
10841120
+ * External interfaces for integration into code-server over IPC. No vs imports
10851121
+ * should be made in this file.
10861122
+ */
1123+
+export interface Options {
1124+
+ base: string
1125+
+ commit: string
1126+
+ disableTelemetry: boolean
1127+
+ nlsConfiguration: NLSConfiguration
1128+
+}
10871129
+
10881130
+export interface InitMessage {
10891131
+ type: 'init';

src/browser/pages/vscode.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020

2121
<!-- Workarounds/Hacks (remote user data uri) -->
2222
<meta id="vscode-remote-user-data-uri" data-settings="{{REMOTE_USER_DATA_URI}}" />
23-
<meta id="vscode-remote-commit" data-settings="{{COMMIT}}" />
2423
<meta id="vscode-remote-product-configuration" data-settings="{{PRODUCT_CONFIGURATION}}" />
2524
<meta id="vscode-remote-nls-configuration" data-settings="{{NLS_CONFIGURATION}}" />
26-
<meta id="vscode-disable-telemetry" data-value="{{DISABLE_TELEMETRY}}" />
2725

2826
<!-- Workbench Icon/Manifest/CSS -->
2927
<link rel="icon" href="{{BASE}}/static/{{COMMIT}}/src/browser/media/favicon.ico" type="image/x-icon" />
@@ -53,9 +51,7 @@
5351
const parts = window.location.pathname.replace(/^\//g, "").split("/")
5452
parts[parts.length - 1] = "{{BASE}}"
5553
const url = new URL(window.location.origin + "/" + parts.join("/"))
56-
const el = document.getElementById("vscode-remote-commit")
57-
const commit = el ? el.getAttribute("data-settings") : ""
58-
const staticBase = url.href.replace(/\/+$/, "") + "/static/" + commit + "/lib/vscode"
54+
const staticBase = url.href.replace(/\/+$/, "") + "/static/{{COMMIT}}/lib/vscode"
5955
let nlsConfig
6056
try {
6157
nlsConfig = JSON.parse(document.getElementById("vscode-remote-nls-configuration").getAttribute("data-settings"))

src/node/app/vscode.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as path from "path"
88
import * as url from "url"
99
import {
1010
CodeServerMessage,
11+
Options,
1112
StartPath,
1213
VscodeMessage,
1314
VscodeOptions,
@@ -205,8 +206,11 @@ export class VscodeHttpProvider extends HttpProvider {
205206
.replace(`"{{PRODUCT_CONFIGURATION}}"`, `'${JSON.stringify(options.productConfiguration)}'`)
206207
.replace(`"{{WORKBENCH_WEB_CONFIGURATION}}"`, `'${JSON.stringify(options.workbenchWebConfiguration)}'`)
207208
.replace(`"{{NLS_CONFIGURATION}}"`, `'${JSON.stringify(options.nlsConfiguration)}'`)
208-
.replace("{{DISABLE_TELEMETRY}}", this.args["disable-telemetry"] ? "true" : "false")
209-
return this.replaceTemplates(route, response)
209+
return this.replaceTemplates<Options>(route, response, {
210+
base: this.base(route),
211+
commit: this.options.commit,
212+
disableTelemetry: !!this.args["disable-telemetry"],
213+
})
210214
}
211215

212216
/**

src/node/http.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,30 @@ export abstract class HttpProvider {
185185
/**
186186
* Replace common templates strings.
187187
*/
188+
protected replaceTemplates(route: Route, response: HttpStringFileResponse, sessionId?: string): HttpStringFileResponse
189+
protected replaceTemplates<T extends object>(
190+
route: Route,
191+
response: HttpStringFileResponse,
192+
options: T,
193+
): HttpStringFileResponse
188194
protected replaceTemplates(
189195
route: Route,
190196
response: HttpStringFileResponse,
191-
sessionId?: string,
197+
sessionIdOrOptions?: string | object,
192198
): HttpStringFileResponse {
193-
const options: Options = {
194-
base: this.base(route),
195-
commit: this.options.commit,
196-
logLevel: logger.level,
197-
sessionId,
199+
if (typeof sessionIdOrOptions === "undefined" || typeof sessionIdOrOptions === "string") {
200+
sessionIdOrOptions = {
201+
base: this.base(route),
202+
commit: this.options.commit,
203+
logLevel: logger.level,
204+
sessionID: sessionIdOrOptions,
205+
} as Options
198206
}
199207
response.content = response.content
200208
.replace(/{{COMMIT}}/g, this.options.commit)
201209
.replace(/{{TO}}/g, Array.isArray(route.query.to) ? route.query.to[0] : route.query.to || "/dashboard")
202210
.replace(/{{BASE}}/g, this.base(route))
203-
.replace(/"{{OPTIONS}}"/, `'${JSON.stringify(options)}'`)
211+
.replace(/"{{OPTIONS}}"/, `'${JSON.stringify(sessionIdOrOptions)}'`)
204212
return response
205213
}
206214

0 commit comments

Comments
 (0)