Skip to content

Commit

Permalink
Support fallback of jupyter message serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Dec 18, 2024
1 parent bb17a03 commit 451ae7a
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 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,36 @@ 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 {
return deserialize(data, protocol);
} catch (ex) {
if (protocol) {
return deserialize(data, '');
} else {
return deserialize(data, supportedKernelWebSocketProtocols.v1KernelWebsocketJupyterOrg);
}
}
},
serialize: (msg: KernelMessage.IMessage, protocol?: string) => {
try {
return serialize(msg, protocol);
} catch (ex) {
if (protocol) {
return serialize(msg, '');
} else {
return serialize(msg, supportedKernelWebSocketProtocols.v1KernelWebsocketJupyterOrg);
}
}
}
};
// 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 +184,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

0 comments on commit 451ae7a

Please sign in to comment.