Outlook_Addin_LLM/node_modules/office-addin-node-debugger/lib/debugger.js

78 lines
2.9 KiB
JavaScript

#!/usr/bin/env node
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// Copyright (c) 2015-present, Facebook, Inc.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
const child_process_1 = require("child_process");
const WebSocket = require("ws");
/* global console, __dirname, setTimeout */
function run(host = "localhost", port = "8081", role = "debugger", debuggerName = "OfficeAddinDebugger") {
const debuggerWorkerRelativePath = "\\debuggerWorker.js";
const debuggerWorkerFullPath = `${__dirname}${debuggerWorkerRelativePath}`;
const websocketRetryTimeout = 500;
function connectToDebuggerProxy() {
var ws = new WebSocket(`ws://${host}:${port}/debugger-proxy?role=${role}&name=${debuggerName}`);
var worker;
function createJSRuntime() {
// This worker will run the application javascript code.
worker = (0, child_process_1.fork)(`${debuggerWorkerFullPath}`, [], {
stdio: ["pipe", "pipe", "pipe", "ipc"],
execArgv: ["--inspect"],
});
worker.on("message", (message) => {
ws.send(JSON.stringify(message));
});
}
function shutdownJSRuntime() {
if (worker) {
worker.kill();
worker.unref();
}
}
ws.onopen = () => {
console.log("Web socket opened...");
};
ws.onmessage = (message) => {
if (!message.data) {
return;
}
var object = JSON.parse(message.data.toString());
if (object.$event === "client-disconnected") {
shutdownJSRuntime();
return;
}
if (!object.method) {
return;
}
// Special message that asks for a new JS runtime
if (object.method === "prepareJSRuntime") {
shutdownJSRuntime();
//console.clear();
createJSRuntime();
ws.send(JSON.stringify({ replyID: object.id }));
}
else if (object.method === "$disconnected") {
shutdownJSRuntime();
}
else {
worker.send(object);
}
};
ws.onclose = (e) => {
shutdownJSRuntime();
if (e.reason) {
console.log(`Web socket closed because the following reason: ${e.reason}`);
}
setTimeout(connectToDebuggerProxy, websocketRetryTimeout);
};
ws.onerror = (event) => {
console.log(`${event.error}`);
};
}
connectToDebuggerProxy();
}
exports.run = run;
//# sourceMappingURL=debugger.js.map