Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node.js Native Messaging Host Memory Usage
/*
port = chrome.runtime.connectNative('node_native_messaging_host_memory_usage');
port.onMessage.addListener((message)=>{
console.log(message);
});
port.onDisconnect.addListener(()=>{
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message)
}
console.log('Disconnected')
});
port.postMessage('test');
*/
#!node
// https://github.com/simov/native-messaging/blob/master/protocol.js
// https://github.com/simov/native-messaging/blob/master/nodejs/example.js
// Might be good to use an explicit path to node on the shebang line
// in case it isn't in PATH when launched by Chrome
// let sendMessage = require('./protocol')(handleMessage);
process.stdin.on('readable', () => {
let input = [];
let chunk;
while ((chunk = process.stdin.read())) {
input.push(chunk);
}
input = Buffer.concat(input);
let msgLen = input.readUInt32LE(0);
let dataLen = msgLen + 4;
if (input.length >= dataLen) {
let content = input.slice(4, dataLen);
let json = JSON.parse(content.toString());
handleMessage(json);
}
});
function sendMessage(msg) {
let buffer = Buffer.from(JSON.stringify(msg));
let header = Buffer.alloc(4);
header.writeUInt32LE(buffer.length, 0);
let data = Buffer.concat([header, buffer]);
process.stdout.write(data);
}
process.on('uncaughtException', (err) => {
sendMessage({ error: err.toString() });
});
function handleMessage(input) {
while (true) {
sendMessage([...new Uint8Array(1764).fill(255)]);
}
}
{
"name": "node-native-messaging-host-memory-usage",
"version": "1.0",
"manifest_version": 3,
"permissions": [
"nativeMessaging"
],
"background": {
"service_worker": "background.js"
},
"action": {}
}
{
"name": "node_native_messaging_host_memory_usage",
"description": "Node.js Native Messaging Host Memory Usage",
"path": "/path/to/node-native-messaging-host-memory-usage/example.js",
"type": "stdio",
"allowed_origins": [
"chrome-extension://<id>/"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment