Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: browser database name should contain a valid DevWorkspace identifier #274

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**********************************************************************
* Copyright (c) 2023 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/
/* eslint-disable header/header */

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// NOTE: DO NOT CHANGE. Launcher updates this constant on workspace startup
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const DEVWORKSPACE = "https://{{che-cluster}}.{{host}}/{{namespace}}/{{workspace-name}}/{{port}}/";

export function getDevWorkspaceId(): string | undefined {
// checking for "https://" guarantees we apply here the ID set by che-code launcher
if (DEVWORKSPACE && !DEVWORKSPACE.startsWith("https://")) {
return DEVWORKSPACE;
}

return undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier } from 'vs/platform/workspace/common/workspace';
import { URI } from 'vs/base/common/uri';
import { hash } from 'vs/base/common/hash';
import { getDevWorkspaceId } from 'vs/workbench/services/workspaces/browser/che/devWorkspaceId'

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// NOTE: DO NOT CHANGE. IDENTIFIERS HAVE TO REMAIN STABLE
Expand All @@ -30,5 +31,5 @@ export function getSingleFolderWorkspaceIdentifier(folderUri: URI): ISingleFolde
}

function getWorkspaceId(uri: URI): string {
return hash(window.location.href + uri.toString()).toString(16);
return hash(getDevWorkspaceId() + uri.toString()).toString(16);
}
45 changes: 45 additions & 0 deletions launcher/src/devworkspace-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**********************************************************************
* Copyright (c) 2023 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import { env } from "process";
import { FILE_WORKBENCH_WEB_MAIN } from "./files";
import * as fs from "./fs-extra";

const DEVWORKSPACE_ID_MASK =
"https://{{che-cluster}}.{{host}}/{{namespace}}/{{workspace-name}}/{{port}}/";

export class DevWorkspaceId {
async configure(): Promise<void> {
console.log("# Setting curent DevWorkspace ID to che-code...");

if (!env.DEVWORKSPACE_ID) {
console.log(" > env.DEVWORKSPACE_ID is not set, skip this step");
return;
}

console.log(` > apply DevWorkspace ID [${env.DEVWORKSPACE_ID}]`);

try {
await this.update(
FILE_WORKBENCH_WEB_MAIN,
DEVWORKSPACE_ID_MASK,
env.DEVWORKSPACE_ID
);
} catch (err) {
console.error(`${err.message} Webviews will not work if CDN disabled.`);
}
}

async update(file: string, text: string, newText: string): Promise<void> {
const content = await fs.readFile(file);
const newContent = content.replace(text, newText);
await fs.writeFile(file, newContent);
}
}
14 changes: 14 additions & 0 deletions launcher/src/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**********************************************************************
* Copyright (c) 2023 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

export const FILE_WORKBENCH_WEB_MAIN = "out/vs/workbench/workbench.web.main.js";

export const FILE_EXTENSION_HOST_PROCESS =
"out/vs/workbench/api/node/extensionHostProcess.js";
2 changes: 1 addition & 1 deletion launcher/src/fs-extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Why do we need to add the wrapper:
* - it allows to get rid of using `fs-extra` package
* - it allows to use common `node_modules` directory for launcher and VS Code
* - it simplifies writing tests and allows to easily mock of this module
* - it simplifies writing tests and allows to easily mock this module
*/

import * as fs from "fs";
Expand Down
2 changes: 2 additions & 0 deletions launcher/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
***********************************************************************/

import { CodeWorkspace } from "./code-workspace";
import { DevWorkspaceId } from "./devworkspace-id";
import { NodeExtraCertificate } from "./node-extra-certificate";
import { OpenVSIXRegistry } from "./openvsix-registry";
import { VSCodeLauncher } from "./vscode-launcher";
Expand All @@ -22,6 +23,7 @@ import { WebviewResources } from "./webview-resources";
*/
export class Main {
async start(): Promise<void> {
await new DevWorkspaceId().configure();
await new OpenVSIXRegistry().configure();
await new WebviewResources().configure();
await new NodeExtraCertificate().configure();
Expand Down
3 changes: 2 additions & 1 deletion launcher/src/openvsix-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
***********************************************************************/

import { env } from "process";
import { FILE_WORKBENCH_WEB_MAIN } from "./files";
import * as fs from "./fs-extra";
import { ProductJSON } from "./product-json";

Expand Down Expand Up @@ -74,7 +75,7 @@ export class OpenVSIXRegistry {
await productJSON.save();

await this.update(
"out/vs/workbench/workbench.web.main.js",
FILE_WORKBENCH_WEB_MAIN,
serviceURL,
newServiceURL,
itemURL,
Expand Down
2 changes: 0 additions & 2 deletions launcher/src/vscode-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import * as child_process from "child_process";
import { NODE_EXTRA_CERTIFICATE } from "./node-extra-certificate";
import { workspaceFilePath } from "./code-workspace";

export const DEFAULT_PROJECTS_DIRECTORY = "/projects";

export class VSCodeLauncher {
async launch(): Promise<void> {
console.log("# Launching VS Code...");
Expand Down
4 changes: 1 addition & 3 deletions launcher/src/webview-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import * as fs from "./fs-extra";
import { FlattenedDevfile } from "./flattened-devfile";
import { ProductJSON } from "./product-json";

const FILE_WORKBENCH_WEB_MAIN = "out/vs/workbench/workbench.web.main.js";
const FILE_EXTENSION_HOST_PROCESS =
"out/vs/workbench/api/node/extensionHostProcess.js";
import { FILE_EXTENSION_HOST_PROCESS, FILE_WORKBENCH_WEB_MAIN } from "./files";

export class WebviewResources {
/*****************************************************************************************************************
Expand Down
63 changes: 63 additions & 0 deletions launcher/tests/devworkspace-id.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**********************************************************************
* Copyright (c) 2023 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import * as fs from "../src/fs-extra";
import { env } from "process";

import { DevWorkspaceId } from "../src/devworkspace-id";

const ORIGIN_WORKBENCH_WEB_MAIN = `
some code, some code, a mask to be replaced https://{{che-cluster}}.{{host}}/{{namespace}}/{{workspace-name}}/{{port}}/, some code
`;

const NEW_WORKBENCH_WEB_MAIN = `
some code, some code, a mask to be replaced test-workspace-1234567890, some code
`;

describe("Test setting DevWorkspace ID to VS Code", () => {
beforeEach(() => {
delete env.DEVWORKSPACE_ID;
});

test("should return if env.DEVWORKSPACE_ID is not set", async () => {
const readFileMock = jest.fn();
Object.assign(fs, {
readFile: readFileMock,
});

const devWorkspaceId = new DevWorkspaceId();
await devWorkspaceId.configure();

expect(readFileMock).toBeCalledTimes(0);
});

test("should apply env.DEVWORKSPACE_ID", async () => {
env.DEVWORKSPACE_ID = "test-workspace-1234567890";

const readFileMock = jest.fn();
const writeFileMock = jest.fn();

Object.assign(fs, {
readFile: readFileMock,
writeFile: writeFileMock,
});

readFileMock.mockImplementation(() => ORIGIN_WORKBENCH_WEB_MAIN);

const devWorkspaceId = new DevWorkspaceId();
await devWorkspaceId.configure();

expect(readFileMock).toBeCalledTimes(1);
expect(writeFileMock).toBeCalledWith(
"out/vs/workbench/workbench.web.main.js",
NEW_WORKBENCH_WEB_MAIN
);
});
});
8 changes: 8 additions & 0 deletions launcher/tests/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

import { Main } from "../src/main";

const setDevWorkspaceIdMock = jest.fn();
jest.mock("../src/devworkspace-id", () => ({
DevWorkspaceId: function () {
return { configure: setDevWorkspaceIdMock };
},
}));

const configureOpenVSIXRegistryMock = jest.fn();
jest.mock("../src/openvsix-registry", () => ({
OpenVSIXRegistry: function () {
Expand Down Expand Up @@ -49,6 +56,7 @@ describe("Test main flow:", () => {
test("should configure all the stuff", async () => {
await new Main().start();

expect(setDevWorkspaceIdMock).toBeCalled();
expect(configureOpenVSIXRegistryMock).toBeCalled();
expect(configureWebviewResourcesMock).toBeCalled();
expect(configureNodeExtraCertificate).toBeCalled();
Expand Down
Loading