ValueError: DELIM not in msg_list using Jupyter Kernel in local and NodeJS server

Hello,

I would like to connect a Jupyter Kernel launched from my console with a NodeJS server in localhost.

I think my NodeJS server can reach the kernel because this one is emetting an error : ValueError: DELIM not in msg_list, which means, if I understand correctly, that the “<IDS|MSG>” part is not in my message. But as you can see on my not so good code (sorry for that I am pretty new to Node and JS) this part is included in my message.

I looked up in the notebook sourcecode and I saw that this part was not in a standard message (notebook/static/services/kernels/kernel.js, line 79) and I couldn’t find why.

I also thought that the mistake was in my way to encode the message but I also tried the serialize function from the source code and it didn’t change anything (notebook/static/services/kernels/serialize.js, line 70)

Did I miss something or didn’t understand something else ?

Thanks in advance for your answers =D

code

const fs = require(“fs”);

const zmq = require(“zeromq”);

const utf8 = require(‘utf8’);

var kernel = JSON.parse(fs.readFileSync(“kernel.json”));

var shell_socket = zmq.socket(“req”);

var shell_addr = kernel.transport + “://” + kernel.ip + “:” + kernel.shell_port;

/* var msg = [

“”,

“<IDS|MSG>”,

“”,

{

header: {

  msg_id: "0",

  msg_type: "execute_request",

},

parent_header: "",

metatadata: "",

content: "a = 1",

buffers: [],

},

]; */

let id = {};

let del = “<IDS|MSG>”;

let sign = {};

let header = { msg_id: “0”, msg_type: “execute_request” };

let phead = {};

let metadata = {};

let content = “a = 1”;

let buff = [];

JSON.stringify(id);

JSON.stringify(del);

JSON.stringify(sign);

JSON.stringify(header);

JSON.stringify(phead);

JSON.stringify(metadata);

JSON.stringify(content);

JSON.stringify(buff);

/* utf8.encode(id);

utf8.encode(del);

utf8.encode(sign);

utf8.encode(header);

utf8.encode(phead);

utf8.encode(metadata);

utf8.encode(content);

utf8.encode(buff); */

let msg = id + del + sign + header + phead + metadata + content + buff;

console.log("\nConnecting shell socket at " + shell_addr + “…”);

shell_socket.connect(shell_addr);

shell_socket.on(“message”, function (msg) {

console.log("\nShell socket says:\n");

console.log(toString(msg));

});

utf8.encode(msg);

shell_socket.send([msg]);

/*

var _serialize_binary = function (msg) {

msg = _.clone(msg);

var offsets = [];

var buffers = [];

var i;

for (i = 0; i < msg.buffers.length; i++) {

  // msg.buffers elements could be either views or ArrayBuffers

  // buffers elements are ArrayBuffers

  var b = msg.buffers[i];

  buffers.push(b.buffer instanceof ArrayBuffer ? b.buffer : b);

}

delete msg.buffers;

var json_utf8 = (new TextEncoder(‘utf8’)).encode(JSON.stringify(msg));

buffers.unshift(json_utf8);

var nbufs = buffers.length;

offsets.push(4 * (nbufs + 1));

for (i = 0; i + 1 < buffers.length; i++) {

  offsets.push(offsets[offsets.length-1] + buffers[i].byteLength);

}

var msg_buf = new Uint8Array(

  offsets[offsets.length-1] + buffers[buffers.length-1].byteLength

);

// use DataView.setUint32 for network byte-order

var view = new DataView(msg_buf.buffer);

// write nbufs to first 4 bytes

view.setUint32(0, nbufs);

// write offsets to next 4 * nbufs bytes

for (i = 0; i < offsets.length; i++) {

  view.setUint32(4 * (i+1), offsets[i]);

}

// write all the buffers at their respective offsets

for (i = 0; i < buffers.length; i++) {

  msg_buf.set(new Uint8Array(buffers[i]), offsets[i]);

}

// return raw ArrayBuffer

return msg_buf.buffer;

};

var serialize = function (msg) {

if (msg.buffers && msg.buffers.length) {

  return _serialize_binary(msg);

} else {

  return JSON.stringify(msg);

}

};*/

I managed to go through this error but here comes a new challenger : ValueError: Invalid Signature !

I understand what the signature is and how it should be built but I can’t get it done, I think it is something regarding the way I want to create the Hmac hex but I tried many ways and nothing good came out.

Here is my updated code if somehow someone wants to look at it, thanks in advance ! :slight_smile:

New Code

const fs = require(“fs”);

const zmq = require(“zeromq”);

const { v4: uuidv4 } = require(“uuid”);

const crypto = require(“crypto”);

var kernel = JSON.parse(fs.readFileSync(“kernel.json”));

var shell_socket = zmq.socket(“req”);

var shell_addr = kernel.transport + “://” + kernel.ip + “:” + kernel.shell_port;

let key = kernel.key;

let id = “”;

let del = “<IDS|MSG>”;

let header = { msg_id: uuidv4(), msg_type: “execute_request” };

let parent = “”;

let metadata = “”;

let content = “1 + 1”;

let header_cpy = header;

let parent_cpy = parent;

let metadata_cpy = metadata;

let content_cpy = content;

console.log("\nConnecting shell socket at " + shell_addr + “…”);

shell_socket.connect(shell_addr);

shell_socket.on(“message”, function (msg) {

console.log("\nShell socket says:\n");

console.log(toString(msg));

});

JSON.stringify(header);

JSON.stringify(parent);

JSON.stringify(metadata);

JSON.stringify(content);

let sign = key + header + parent + metadata + content;

let hmac = crypto.createHmac(“sha256”, key).update(sign);

hmac.digest(‘hex’);

//let sign = key + header_cpy + parent_cpy + metadata_cpy + content_cpy;

msg = [id, del, sign, header, parent, metadata, content];

console.log(msg);

shell_socket.send(msg);