Skip to content

Commit 15de5d1

Browse files
authored
Use lowercase for workspace folder state connection keys (#1055)
1 parent f27fcbf commit 15de5d1

File tree

6 files changed

+48
-52
lines changed

6 files changed

+48
-52
lines changed

src/api/index.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const { default: fetch } = require("node-fetch-cjs");
33

44
import * as httpModule from "http";
55
import * as httpsModule from "https";
6-
import * as url from "url";
76
import * as vscode from "vscode";
87
import * as Cache from "vscode-cache";
98
import {
@@ -57,16 +56,13 @@ export class AtelierAPI {
5756
public get config(): ConnectionSettings {
5857
const { serverName, active = false, https = false, pathPrefix = "", username } = this._config;
5958
const ns = this.namespace || this._config.ns;
60-
const host = this.externalServer
61-
? this._config.host
62-
: workspaceState.get(this.configName + ":host", this._config.host);
63-
const port = this.externalServer
64-
? this._config.port
65-
: workspaceState.get(this.configName + ":port", this._config.port);
66-
const password = workspaceState.get(this.configName + ":password", this._config.password);
67-
const apiVersion = workspaceState.get(this.configName + ":apiVersion", DEFAULT_API_VERSION);
68-
const docker = workspaceState.get(this.configName + ":docker", false);
69-
const dockerService = workspaceState.get<string>(this.configName + ":dockerService");
59+
const wsKey = this.configName.toLowerCase();
60+
const host = this.externalServer ? this._config.host : workspaceState.get(wsKey + ":host", this._config.host);
61+
const port = this.externalServer ? this._config.port : workspaceState.get(wsKey + ":port", this._config.port);
62+
const password = workspaceState.get(wsKey + ":password", this._config.password);
63+
const apiVersion = workspaceState.get(wsKey + ":apiVersion", DEFAULT_API_VERSION);
64+
const docker = workspaceState.get(wsKey + ":docker", false);
65+
const dockerService = workspaceState.get<string>(wsKey + ":dockerService");
7066
return {
7167
serverName,
7268
active,
@@ -108,11 +104,9 @@ export class AtelierAPI {
108104
workspaceFolderName = parts[0];
109105
namespace = parts[1];
110106
} else {
111-
const { query } = url.parse(wsOrFile.toString(true), true);
112-
if (query) {
113-
if (query.ns && query.ns !== "") {
114-
namespace = query.ns.toString();
115-
}
107+
const params = new URLSearchParams(wsOrFile.query);
108+
if (params.has("ns") && params.get("ns") != "") {
109+
namespace = params.get("ns");
116110
}
117111
}
118112
} else {
@@ -198,7 +192,7 @@ export class AtelierAPI {
198192
this._config = {
199193
serverName,
200194
active: this.externalServer || conn.active,
201-
apiVersion: workspaceState.get(this.configName + ":apiVersion", DEFAULT_API_VERSION),
195+
apiVersion: workspaceState.get(this.configName.toLowerCase() + ":apiVersion", DEFAULT_API_VERSION),
202196
https: scheme === "https",
203197
ns,
204198
host,
@@ -432,8 +426,8 @@ export class AtelierAPI {
432426
authRequestMap.delete(target);
433427
panel.text = `${this.connInfo} $(debug-disconnect)`;
434428
panel.tooltip = "Disconnected";
435-
workspaceState.update(this.configName + ":host", undefined);
436-
workspaceState.update(this.configName + ":port", undefined);
429+
workspaceState.update(this.configName.toLowerCase() + ":host", undefined);
430+
workspaceState.update(this.configName.toLowerCase() + ":port", undefined);
437431
if (!checkingConnection) {
438432
setTimeout(() => checkConnection(false, undefined, true), 30000);
439433
}
@@ -458,8 +452,8 @@ export class AtelierAPI {
458452
};
459453
}
460454
return Promise.all([
461-
workspaceState.update(this.configName + ":apiVersion", apiVersion),
462-
workspaceState.update(this.configName + ":iris", data.version.startsWith("IRIS")),
455+
workspaceState.update(this.configName.toLowerCase() + ":apiVersion", apiVersion),
456+
workspaceState.update(this.configName.toLowerCase() + ":iris", data.version.startsWith("IRIS")),
463457
]).then(() => info);
464458
}
465459
});

src/commands/serverActions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ export async function serverActions(): Promise<void> {
100100
rawLink,
101101
});
102102
}
103-
if (workspaceState.get(workspaceFolder + ":docker", false)) {
103+
if (workspaceState.get(workspaceFolder.toLowerCase() + ":docker", false)) {
104104
actions.push({
105105
id: "openDockerTerminal",
106106
label: "Open Terminal in Docker",
107107
detail: "Use docker-compose to start session inside configured service",
108108
});
109109
}
110-
if (workspaceState.get(workspaceFolder + ":docker", false)) {
110+
if (workspaceState.get(workspaceFolder.toLowerCase() + ":docker", false)) {
111111
actions.push({
112112
id: "openDockerShell",
113113
label: "Open Shell in Docker",

src/explorer/explorer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ export function registerExplorerOpen(): vscode.Disposable {
9595
projectsExplorerProvider.refresh();
9696
}
9797
} else {
98-
throw error;
98+
outputChannel.appendLine(
99+
typeof error == "string" ? error : error instanceof Error ? error.message : JSON.stringify(error)
100+
);
101+
outputChannel.show();
99102
}
100103
}
101104
}
@@ -117,6 +120,7 @@ function wasDoubleClick(uri: vscode.Uri): boolean {
117120
};
118121
return result;
119122
}
123+
120124
export class ObjectScriptExplorerProvider implements vscode.TreeDataProvider<NodeBase> {
121125
public onDidChange?: vscode.Event<vscode.Uri>;
122126
public onDidChangeTreeData: vscode.Event<NodeBase>;

src/extension.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,14 @@ export async function checkConnection(
247247
}
248248

249249
const { apiTarget, configName } = connectionTarget(uri);
250+
const wsKey = configName.toLowerCase();
250251
if (clearCookies) {
251252
/// clean-up cached values
252-
await workspaceState.update(configName + ":host", undefined);
253-
await workspaceState.update(configName + ":port", undefined);
254-
await workspaceState.update(configName + ":password", undefined);
255-
await workspaceState.update(configName + ":apiVersion", undefined);
256-
await workspaceState.update(configName + ":docker", undefined);
253+
await workspaceState.update(wsKey + ":host", undefined);
254+
await workspaceState.update(wsKey + ":port", undefined);
255+
await workspaceState.update(wsKey + ":password", undefined);
256+
await workspaceState.update(wsKey + ":apiVersion", undefined);
257+
await workspaceState.update(wsKey + ":docker", undefined);
257258
_onDidChangeConnection.fire();
258259
}
259260
let api = new AtelierAPI(apiTarget, false);
@@ -278,11 +279,11 @@ export async function checkConnection(
278279
return;
279280
}
280281

281-
if (!workspaceState.get(configName + ":port") && !api.externalServer) {
282+
if (!workspaceState.get(wsKey + ":port") && !api.externalServer) {
282283
try {
283284
const { port: dockerPort, docker: withDocker, service } = await portFromDockerCompose();
284-
workspaceState.update(configName + ":docker", withDocker);
285-
workspaceState.update(configName + ":dockerService", service);
285+
workspaceState.update(wsKey + ":docker", withDocker);
286+
workspaceState.update(wsKey + ":dockerService", service);
286287
if (withDocker) {
287288
if (!dockerPort) {
288289
const errorMessage = `Something is wrong with your docker-compose connection settings, or your service is not running.`;
@@ -294,15 +295,15 @@ export async function checkConnection(
294295
const { autoShowTerminal } = config();
295296
autoShowTerminal && terminalWithDocker();
296297
if (dockerPort !== port) {
297-
workspaceState.update(configName + ":host", "localhost");
298-
workspaceState.update(configName + ":port", dockerPort);
298+
workspaceState.update(wsKey + ":host", "localhost");
299+
workspaceState.update(wsKey + ":port", dockerPort);
299300
}
300301
connInfo = `localhost:${dockerPort}[${ns}]`;
301302
_onDidChangeConnection.fire();
302303
}
303304
} catch (error) {
304305
outputChannel.appendError(error);
305-
workspaceState.update(configName + ":docker", true);
306+
workspaceState.update(wsKey + ":docker", true);
306307
panel.text = `${PANEL_LABEL} $(error)`;
307308
panel.tooltip = error;
308309
return;
@@ -392,7 +393,7 @@ export async function checkConnection(
392393
.then(
393394
async (password) => {
394395
if (password) {
395-
await workspaceState.update(configName + ":password", password);
396+
await workspaceState.update(wsKey + ":password", password);
396397
resolve(
397398
api
398399
.serverInfo()
@@ -404,7 +405,7 @@ export async function checkConnection(
404405
.catch(async (error) => {
405406
console.log(`Second connect failed: ${error}`);
406407
await setConnectionState(configName, false);
407-
await workspaceState.update(configName + ":password", undefined);
408+
await workspaceState.update(wsKey + ":password", undefined);
408409
return false;
409410
})
410411
.finally(() => {

src/providers/DocumentContentProvider.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as fs from "fs";
22
import * as path from "path";
3-
import * as url from "url";
43
import * as vscode from "vscode";
54
import { AtelierAPI } from "../api";
65

@@ -189,17 +188,15 @@ export class DocumentContentProvider implements vscode.TextDocumentContentProvid
189188

190189
public provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): vscode.ProviderResult<string> {
191190
const api = new AtelierAPI(uri);
192-
const query = url.parse(uri.toString(true), true).query;
193-
const fileName = query && query.csp ? uri.path.substring(1) : uri.path.split("/").slice(1).join(".");
194-
if (query) {
195-
if (query.ns && query.ns !== "") {
196-
const namespace = query.ns.toString();
197-
api.setNamespace(namespace);
198-
}
191+
const params = new URLSearchParams(uri.query);
192+
const fileName =
193+
params.has("csp") && ["", "1"].includes(params.get("csp"))
194+
? uri.path.slice(1)
195+
: uri.path.split("/").slice(1).join(".");
196+
if (params.has("ns") && params.get("ns") != "") {
197+
api.setNamespace(params.get("ns"));
199198
}
200-
return api.getDoc(fileName).then((data) => {
201-
return data.result.content.join("\n");
202-
});
199+
return api.getDoc(fileName).then((data) => data.result.content.join("\n"));
203200
}
204201

205202
public update(uri: vscode.Uri, message?: string): void {

src/providers/FileSystemProvider/FileSystemProvider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
361361
)
362362
.catch((error) => {
363363
// Throw all failures
364-
if (error.errorText && error.errorText !== "") {
364+
if (error && error.errorText && error.errorText !== "") {
365365
throw vscode.FileSystemError.Unavailable(error.errorText);
366366
}
367367
throw vscode.FileSystemError.Unavailable(uri);
@@ -566,7 +566,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
566566
)
567567
.catch((error) => {
568568
// Throw all failures
569-
if (error.errorText && error.errorText !== "") {
569+
if (error && error.errorText && error.errorText !== "") {
570570
throw vscode.FileSystemError.Unavailable(error.errorText);
571571
}
572572
throw vscode.FileSystemError.Unavailable(error.message);
@@ -605,7 +605,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
605605
.serverInfo()
606606
.then()
607607
.catch((error) => {
608-
if (error.errorText && error.errorText !== "") {
608+
if (error && error.errorText && error.errorText !== "") {
609609
throw vscode.FileSystemError.Unavailable(error.errorText);
610610
}
611611
throw vscode.FileSystemError.Unavailable(uri);
@@ -719,7 +719,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
719719
if (error?.statusCode === 304 && cachedFile) {
720720
return cachedFile;
721721
}
722-
if (error.errorText && error.errorText !== "") {
722+
if (error && error.errorText && error.errorText !== "") {
723723
throw vscode.FileSystemError.FileNotFound(error.errorText);
724724
}
725725
throw vscode.FileSystemError.FileNotFound(uri);

0 commit comments

Comments
 (0)