Skip to content

Commit 451ae7a

Browse files
committed
Support fallback of jupyter message serialization
1 parent bb17a03 commit 451ae7a

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/kernels/jupyter/jupyterUtils.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import type { ServerConnection } from '@jupyterlab/services';
4+
import type { KernelMessage, ServerConnection } from '@jupyterlab/services';
55
import * as path from '../../platform/vscode-path/path';
66
import { ConfigurationTarget, Uri, window } from 'vscode';
77
import { IJupyterConnection } from '../types';
@@ -137,7 +137,36 @@ export function createJupyterConnectionInfo(
137137
requestInit = { ...requestInit, agent: requestAgent };
138138
}
139139

140-
const { ServerConnection } = require('@jupyterlab/services');
140+
const { ServerConnection } = require('@jupyterlab/services') as typeof import('@jupyterlab/services');
141+
const { deserialize, serialize } =
142+
require('@jupyterlab/services/lib/kernel/serialize') as typeof import('@jupyterlab/services/lib/kernel/serialize');
143+
const { supportedKernelWebSocketProtocols } =
144+
require('@jupyterlab/services/lib/kernel/messages') as typeof import('@jupyterlab/services/lib/kernel/messages');
145+
146+
const serializer: import('@jupyterlab/services').ServerConnection.ISettings['serializer'] = {
147+
deserialize: (data: ArrayBuffer, protocol?: string) => {
148+
try {
149+
return deserialize(data, protocol);
150+
} catch (ex) {
151+
if (protocol) {
152+
return deserialize(data, '');
153+
} else {
154+
return deserialize(data, supportedKernelWebSocketProtocols.v1KernelWebsocketJupyterOrg);
155+
}
156+
}
157+
},
158+
serialize: (msg: KernelMessage.IMessage, protocol?: string) => {
159+
try {
160+
return serialize(msg, protocol);
161+
} catch (ex) {
162+
if (protocol) {
163+
return serialize(msg, '');
164+
} else {
165+
return serialize(msg, supportedKernelWebSocketProtocols.v1KernelWebsocketJupyterOrg);
166+
}
167+
}
168+
}
169+
};
141170
// This replaces the WebSocket constructor in jupyter lab services with our own implementation
142171
// See _createSocket here:
143172
// https://github.com/jupyterlab/jupyterlab/blob/cfc8ebda95e882b4ed2eefd54863bb8cdb0ab763/packages/services/src/kernel/default.ts
@@ -155,7 +184,8 @@ export function createJupyterConnectionInfo(
155184
) as any),
156185
fetch: serverUri.fetch || requestCreator.getFetchMethod(),
157186
Request: requestCreator.getRequestCtor(undefined, allowUnauthorized, getAuthHeader),
158-
Headers: requestCreator.getHeadersCtor()
187+
Headers: requestCreator.getHeadersCtor(),
188+
serializer
159189
};
160190

161191
const connection: IJupyterConnection = {

0 commit comments

Comments
 (0)