mirror of
https://github.com/gnh1201/welsonjs.git
synced 2026-04-19 02:28:41 +00:00
Introduce a StdioServer module and mcploader entry script, and extend jsonrpc2 with an extract() helper. stdio-server.js provides an EventTarget-based wrapper for reading/writing via WScript stdio. mcploader.js uses the server and JsonRpc2.extract to parse incoming JSON-RPC messages and respond. Also change DEFAULT_JSONRPC2_URL to http://localhost:5555, export the new extract function, and bump jsonrpc2 version to 0.1.6.
59 lines
1.3 KiB
JavaScript
59 lines
1.3 KiB
JavaScript
// stdio-server.js
|
|
// Copyright 2019-2026, Namhyeon Go <gnh1201@catswords.re.kr> and the WelsonJS contributors.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// https://github.com/gnh1201/welsonjs
|
|
//
|
|
var STD = require("lib/std");
|
|
|
|
function StdioServer() {
|
|
// Set event-attachable object
|
|
STD.EventTarget.apply(this, arguments);
|
|
|
|
this.messages = [];
|
|
|
|
this.receive = function () {
|
|
return this.messages.shift();
|
|
};
|
|
|
|
this.send = function (message) {
|
|
WScript.StdOut.WriteLine(message);
|
|
};
|
|
|
|
this.listen = function () {
|
|
while (!WScript.StdIn.AtEndOfStream) {
|
|
this.messages.push(WScript.StdIn.ReadLine());
|
|
this.dispatchEvent(new STD.Event("message"));
|
|
}
|
|
};
|
|
}
|
|
StdioServer.prototype = new STD.EventTarget();
|
|
StdioServer.prototype.constructor = StdioServer;
|
|
|
|
/*
|
|
|
|
// For example
|
|
var server = new StdioServer();
|
|
|
|
server.addEventListener("message", function(e) {
|
|
// receive message
|
|
var message = e.target.receive();
|
|
|
|
console.log(message);
|
|
|
|
// send message
|
|
e.target.send("Hello world");
|
|
});
|
|
|
|
server.listen();
|
|
|
|
*/
|
|
|
|
exports.create = function () {
|
|
return new StdioServer();
|
|
};
|
|
|
|
exports.VERSIONINFO = "Stdio Server (stdio-server.js) version 0.1";
|
|
exports.AUTHOR = "gnh1201@catswords.re.kr";
|
|
exports.global = global;
|
|
exports.require = global.require;
|