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

Support fallback of jupyter message serialization #16318

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
41 changes: 38 additions & 3 deletions src/kernels/jupyter/jupyterUtils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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 = {
Expand Down
Loading