diff --git a/src/kernels/jupyter/jupyterUtils.ts b/src/kernels/jupyter/jupyterUtils.ts index 3576e4be92b..2c6840adcfe 100644 --- a/src/kernels/jupyter/jupyterUtils.ts +++ b/src/kernels/jupyter/jupyterUtils.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -import type { ServerConnection } from '@jupyterlab/services'; +import type { KernelMessage, ServerConnection } from '@jupyterlab/services'; import * as path from '../../platform/vscode-path/path'; import { ConfigurationTarget, Uri, window } from 'vscode'; import { IJupyterConnection } from '../types'; @@ -137,7 +137,41 @@ export function createJupyterConnectionInfo( requestInit = { ...requestInit, agent: requestAgent }; } - const { ServerConnection } = require('@jupyterlab/services'); + const { ServerConnection } = require('@jupyterlab/services') as typeof import('@jupyterlab/services'); + const { deserialize, serialize } = + require('@jupyterlab/services/lib/kernel/serialize') as typeof import('@jupyterlab/services/lib/kernel/serialize'); + const { supportedKernelWebSocketProtocols } = + require('@jupyterlab/services/lib/kernel/messages') as typeof import('@jupyterlab/services/lib/kernel/messages'); + + const serializer: import('@jupyterlab/services').ServerConnection.ISettings['serializer'] = { + deserialize: (data: ArrayBuffer, protocol?: string) => { + try { + if (typeof data === 'string') { + return deserialize(data, ''); + } + return deserialize(data, protocol); + } catch (ex) { + logger.warn(`Failed to deserialize message protocol = ${protocol}`, ex); + if (protocol) { + return deserialize(data, supportedKernelWebSocketProtocols.v1KernelWebsocketJupyterOrg); + } else { + return deserialize(data, ''); + } + } + }, + serialize: (msg: KernelMessage.IMessage, protocol?: string) => { + try { + return serialize(msg, protocol); + } catch (ex) { + logger.warn(`Failed to serialize message protocol = ${protocol}`, ex); + if (protocol) { + return serialize(msg, supportedKernelWebSocketProtocols.v1KernelWebsocketJupyterOrg); + } else { + return serialize(msg, ''); + } + } + } + }; // This replaces the WebSocket constructor in jupyter lab services with our own implementation // See _createSocket here: // https://github.com/jupyterlab/jupyterlab/blob/cfc8ebda95e882b4ed2eefd54863bb8cdb0ab763/packages/services/src/kernel/default.ts @@ -155,7 +189,8 @@ export function createJupyterConnectionInfo( ) as any), fetch: serverUri.fetch || requestCreator.getFetchMethod(), Request: requestCreator.getRequestCtor(undefined, allowUnauthorized, getAuthHeader), - Headers: requestCreator.getHeadersCtor() + Headers: requestCreator.getHeadersCtor(), + serializer }; const connection: IJupyterConnection = {