mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Remove all manual uses of v8 serialization (#5548)
* Remove all manual uses of v8 serialization - Seems that sometimes the is a versioning mismatch within the binary format causes cluster screen the go grey Signed-off-by: Sebastian Malton <sebastian@malton.name> * handle ping Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
327978dbc8
commit
fae6bff6fb
31
src/common/terminal/channels.ts
Normal file
31
src/common/terminal/channels.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
export enum TerminalChannels {
|
||||||
|
STDIN = "stdin",
|
||||||
|
STDOUT = "stdout",
|
||||||
|
CONNECTED = "connected",
|
||||||
|
RESIZE = "resize",
|
||||||
|
PING = "ping",
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TerminalMessage = {
|
||||||
|
type: TerminalChannels.STDIN;
|
||||||
|
data: string;
|
||||||
|
} | {
|
||||||
|
type: TerminalChannels.STDOUT;
|
||||||
|
data: string;
|
||||||
|
} | {
|
||||||
|
type: TerminalChannels.CONNECTED;
|
||||||
|
} | {
|
||||||
|
type: TerminalChannels.RESIZE;
|
||||||
|
data: {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
};
|
||||||
|
} | {
|
||||||
|
type: TerminalChannels.PING;
|
||||||
|
};
|
||||||
@ -4,7 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import v8 from "v8";
|
|
||||||
import * as yaml from "js-yaml";
|
import * as yaml from "js-yaml";
|
||||||
import type { HelmRepo } from "./helm-repo-manager";
|
import type { HelmRepo } from "./helm-repo-manager";
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
@ -15,7 +14,7 @@ import type { SetRequired } from "type-fest";
|
|||||||
import { assert } from "console";
|
import { assert } from "console";
|
||||||
|
|
||||||
interface ChartCacheEntry {
|
interface ChartCacheEntry {
|
||||||
data: Buffer;
|
data: string; // serialized JSON
|
||||||
mtimeMs: number;
|
mtimeMs: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +87,7 @@ export class HelmChartManager {
|
|||||||
HelmChartManager.#cache,
|
HelmChartManager.#cache,
|
||||||
this.repo.name,
|
this.repo.name,
|
||||||
{
|
{
|
||||||
data: v8.serialize(normalized),
|
data: JSON.stringify(normalized),
|
||||||
mtimeMs: cacheFileStats.mtimeMs,
|
mtimeMs: cacheFileStats.mtimeMs,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -107,7 +106,7 @@ export class HelmChartManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return v8.deserialize(cacheEntry.data);
|
return JSON.parse(cacheEntry.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,8 +13,8 @@ import { get, once } from "lodash";
|
|||||||
import { Node, NodeApi } from "../../../common/k8s-api/endpoints";
|
import { Node, NodeApi } from "../../../common/k8s-api/endpoints";
|
||||||
import { KubeJsonApi } from "../../../common/k8s-api/kube-json-api";
|
import { KubeJsonApi } from "../../../common/k8s-api/kube-json-api";
|
||||||
import logger from "../../logger";
|
import logger from "../../logger";
|
||||||
import { TerminalChannels } from "../../../renderer/api/terminal-api";
|
|
||||||
import type { Kubectl } from "../../kubectl/kubectl";
|
import type { Kubectl } from "../../kubectl/kubectl";
|
||||||
|
import { TerminalChannels } from "../../../common/terminal/channels";
|
||||||
|
|
||||||
export class NodeShellSession extends ShellSession {
|
export class NodeShellSession extends ShellSession {
|
||||||
ShellType = "node-shell";
|
ShellType = "node-shell";
|
||||||
|
|||||||
@ -16,11 +16,9 @@ import { UserStore } from "../../common/user-store";
|
|||||||
import * as pty from "node-pty";
|
import * as pty from "node-pty";
|
||||||
import { appEventBus } from "../../common/app-event-bus/event-bus";
|
import { appEventBus } from "../../common/app-event-bus/event-bus";
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
import type { TerminalMessage } from "../../renderer/api/terminal-api";
|
|
||||||
import { TerminalChannels } from "../../renderer/api/terminal-api";
|
|
||||||
import { deserialize, serialize } from "v8";
|
|
||||||
import { stat } from "fs/promises";
|
import { stat } from "fs/promises";
|
||||||
import { getOrInsertWith } from "../../common/utils";
|
import { getOrInsertWith } from "../../common/utils";
|
||||||
|
import { type TerminalMessage, TerminalChannels } from "../../common/terminal/channels";
|
||||||
|
|
||||||
export class ShellOpenError extends Error {
|
export class ShellOpenError extends Error {
|
||||||
constructor(message: string, options?: ErrorOptions) {
|
constructor(message: string, options?: ErrorOptions) {
|
||||||
@ -161,7 +159,7 @@ export abstract class ShellSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected send(message: TerminalMessage): void {
|
protected send(message: TerminalMessage): void {
|
||||||
this.websocket.send(serialize(message));
|
this.websocket.send(JSON.stringify(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async getCwd(env: Record<string, string | undefined>): Promise<string> {
|
protected async getCwd(env: Record<string, string | undefined>): Promise<string> {
|
||||||
@ -232,17 +230,19 @@ export abstract class ShellSession {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.websocket
|
this.websocket
|
||||||
.on("message", (data: string | Uint8Array): void => {
|
.on("message", (rawData: unknown): void => {
|
||||||
if (!this.running) {
|
if (!this.running) {
|
||||||
return void logger.debug(`[SHELL-SESSION]: received message from ${this.terminalId}, but shellProcess isn't running`);
|
return void logger.debug(`[SHELL-SESSION]: received message from ${this.terminalId}, but shellProcess isn't running`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof data === "string") {
|
if (!(rawData instanceof Buffer)) {
|
||||||
return void logger.silly(`[SHELL-SESSION]: Received message from ${this.terminalId}`, { data });
|
return void logger.error(`[SHELL-SESSION]: Received message non-buffer message.`, { rawData });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const data = rawData.toString();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const message: TerminalMessage = deserialize(data);
|
const message: TerminalMessage = JSON.parse(data);
|
||||||
|
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case TerminalChannels.STDIN:
|
case TerminalChannels.STDIN:
|
||||||
@ -251,6 +251,9 @@ export abstract class ShellSession {
|
|||||||
case TerminalChannels.RESIZE:
|
case TerminalChannels.RESIZE:
|
||||||
shellProcess.resize(message.data.width, message.data.height);
|
shellProcess.resize(message.data.width, message.data.height);
|
||||||
break;
|
break;
|
||||||
|
case TerminalChannels.PING:
|
||||||
|
logger.silly(`[SHELL-SESSION]: ${this.terminalId} ping!`);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
logger.warn(`[SHELL-SESSION]: unknown or unhandleable message type for ${this.terminalId}`, message);
|
logger.warn(`[SHELL-SESSION]: unknown or unhandleable message type for ${this.terminalId}`, message);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -10,31 +10,8 @@ import url from "url";
|
|||||||
import { makeObservable, observable } from "mobx";
|
import { makeObservable, observable } from "mobx";
|
||||||
import { ipcRenderer } from "electron";
|
import { ipcRenderer } from "electron";
|
||||||
import logger from "../../common/logger";
|
import logger from "../../common/logger";
|
||||||
import { deserialize, serialize } from "v8";
|
|
||||||
import { once } from "lodash";
|
import { once } from "lodash";
|
||||||
|
import { type TerminalMessage, TerminalChannels } from "../../common/terminal/channels";
|
||||||
export enum TerminalChannels {
|
|
||||||
STDIN = "stdin",
|
|
||||||
STDOUT = "stdout",
|
|
||||||
CONNECTED = "connected",
|
|
||||||
RESIZE = "resize",
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TerminalMessage = {
|
|
||||||
type: TerminalChannels.STDIN;
|
|
||||||
data: string;
|
|
||||||
} | {
|
|
||||||
type: TerminalChannels.STDOUT;
|
|
||||||
data: string;
|
|
||||||
} | {
|
|
||||||
type: TerminalChannels.CONNECTED;
|
|
||||||
} | {
|
|
||||||
type: TerminalChannels.RESIZE;
|
|
||||||
data: {
|
|
||||||
width: number;
|
|
||||||
height: number;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
enum TerminalColor {
|
enum TerminalColor {
|
||||||
RED = "\u001b[31m",
|
RED = "\u001b[31m",
|
||||||
@ -133,13 +110,10 @@ export class TerminalApi extends WebSocketApi<TerminalEvents> {
|
|||||||
this.prependListener("connected", onReady);
|
this.prependListener("connected", onReady);
|
||||||
|
|
||||||
super.connect(socketUrl);
|
super.connect(socketUrl);
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
||||||
this.socket!.binaryType = "arraybuffer";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(message: TerminalMessage) {
|
sendMessage(message: TerminalMessage) {
|
||||||
return this.send(serialize(message));
|
return this.send(JSON.stringify(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
sendTerminalSize(cols: number, rows: number) {
|
sendTerminalSize(cols: number, rows: number) {
|
||||||
@ -154,9 +128,9 @@ export class TerminalApi extends WebSocketApi<TerminalEvents> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _onMessage({ data, ...evt }: MessageEvent<ArrayBuffer>): void {
|
protected _onMessage({ data, ...evt }: MessageEvent<string>): void {
|
||||||
try {
|
try {
|
||||||
const message: TerminalMessage = deserialize(new Uint8Array(data));
|
const message = JSON.parse(data) as TerminalMessage;
|
||||||
|
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case TerminalChannels.STDOUT:
|
case TerminalChannels.STDOUT:
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import type TypedEventEmitter from "typed-emitter";
|
|||||||
import type { Arguments } from "typed-emitter";
|
import type { Arguments } from "typed-emitter";
|
||||||
import { isDevelopment } from "../../common/vars";
|
import { isDevelopment } from "../../common/vars";
|
||||||
import type { Defaulted } from "../utils";
|
import type { Defaulted } from "../utils";
|
||||||
|
import { TerminalChannels, type TerminalMessage } from "../../common/terminal/channels";
|
||||||
|
|
||||||
interface WebsocketApiParams {
|
interface WebsocketApiParams {
|
||||||
/**
|
/**
|
||||||
@ -30,9 +31,9 @@ interface WebsocketApiParams {
|
|||||||
/**
|
/**
|
||||||
* The message for pinging the websocket
|
* The message for pinging the websocket
|
||||||
*
|
*
|
||||||
* @default "PING"
|
* @default "{type: \"ping\"}"
|
||||||
*/
|
*/
|
||||||
pingMessage?: string | ArrayBufferLike | Blob | ArrayBufferView;
|
pingMessage?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If set to a number > 0, then the API will ping the socket on that interval.
|
* If set to a number > 0, then the API will ping the socket on that interval.
|
||||||
@ -65,7 +66,7 @@ export interface WebSocketEvents {
|
|||||||
|
|
||||||
export class WebSocketApi<Events extends WebSocketEvents> extends (EventEmitter as { new<T>(): TypedEventEmitter<T> })<Events> {
|
export class WebSocketApi<Events extends WebSocketEvents> extends (EventEmitter as { new<T>(): TypedEventEmitter<T> })<Events> {
|
||||||
protected socket: WebSocket | null = null;
|
protected socket: WebSocket | null = null;
|
||||||
protected pendingCommands: (string | ArrayBufferLike | Blob | ArrayBufferView)[] = [];
|
protected pendingCommands: string[] = [];
|
||||||
protected reconnectTimer?: number;
|
protected reconnectTimer?: number;
|
||||||
protected pingTimer?: number;
|
protected pingTimer?: number;
|
||||||
protected params: Defaulted<WebsocketApiParams, keyof typeof WebSocketApi["defaultParams"]>;
|
protected params: Defaulted<WebsocketApiParams, keyof typeof WebSocketApi["defaultParams"]>;
|
||||||
@ -76,7 +77,7 @@ export class WebSocketApi<Events extends WebSocketEvents> extends (EventEmitter
|
|||||||
logging: isDevelopment,
|
logging: isDevelopment,
|
||||||
reconnectDelay: 10,
|
reconnectDelay: 10,
|
||||||
flushOnOpen: true,
|
flushOnOpen: true,
|
||||||
pingMessage: "PING",
|
pingMessage: JSON.stringify({ type: TerminalChannels.PING } as TerminalMessage),
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(params: WebsocketApiParams) {
|
constructor(params: WebsocketApiParams) {
|
||||||
@ -149,7 +150,7 @@ export class WebSocketApi<Events extends WebSocketEvents> extends (EventEmitter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
send(command: string | ArrayBufferLike | Blob | ArrayBufferView) {
|
send(command: string) {
|
||||||
if (this.getIsConnected()) {
|
if (this.getIsConnected()) {
|
||||||
this.socket.send(command);
|
this.socket.send(command);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -4,9 +4,9 @@
|
|||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { when } from "mobx";
|
import { when } from "mobx";
|
||||||
|
import { TerminalChannels } from "../../../../common/terminal/channels";
|
||||||
import { waitUntilDefined } from "../../../../common/utils/wait";
|
import { waitUntilDefined } from "../../../../common/utils/wait";
|
||||||
import type { TerminalApi } from "../../../api/terminal-api";
|
import type { TerminalApi } from "../../../api/terminal-api";
|
||||||
import { TerminalChannels } from "../../../api/terminal-api";
|
|
||||||
import { noop } from "../../../utils";
|
import { noop } from "../../../utils";
|
||||||
import { Notifications } from "../../notifications";
|
import { Notifications } from "../../notifications";
|
||||||
import selectDockTabInjectable from "../dock/select-dock-tab.injectable";
|
import selectDockTabInjectable from "../dock/select-dock-tab.injectable";
|
||||||
|
|||||||
@ -10,7 +10,6 @@ import { Terminal as XTerm } from "xterm";
|
|||||||
import { FitAddon } from "xterm-addon-fit";
|
import { FitAddon } from "xterm-addon-fit";
|
||||||
import type { TabId } from "../dock/store";
|
import type { TabId } from "../dock/store";
|
||||||
import type { TerminalApi } from "../../../api/terminal-api";
|
import type { TerminalApi } from "../../../api/terminal-api";
|
||||||
import { TerminalChannels } from "../../../api/terminal-api";
|
|
||||||
import type { ThemeStore } from "../../../themes/store";
|
import type { ThemeStore } from "../../../themes/store";
|
||||||
import { disposer } from "../../../utils";
|
import { disposer } from "../../../utils";
|
||||||
import { isMac } from "../../../../common/vars";
|
import { isMac } from "../../../../common/vars";
|
||||||
@ -19,6 +18,7 @@ import { clipboard } from "electron";
|
|||||||
import logger from "../../../../common/logger";
|
import logger from "../../../../common/logger";
|
||||||
import type { TerminalConfig } from "../../../../common/user-store/preferences-helpers";
|
import type { TerminalConfig } from "../../../../common/user-store/preferences-helpers";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
|
import { TerminalChannels } from "../../../../common/terminal/channels";
|
||||||
|
|
||||||
export interface TerminalDependencies {
|
export interface TerminalDependencies {
|
||||||
readonly spawningPool: HTMLElement;
|
readonly spawningPool: HTMLElement;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user