git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
99941cf
Branches
Tags
develop-client_server
master
typescript
fs-words.git
server
lib
plankton
comm-server
logic-impl.js
[upd] server:lib:plankton
Christian Fraß
commited
99941cf
at 2021-03-12 00:46:54
logic-impl.js
Blame
History
Raw
/* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ lib_trait.define("comm-server", { "state": null, "message_in": null, "message_out": null }, { "setup": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "state" } }, "shape_output": { "name": "function", "parameters": { "shape_input": { "name": "any" }, "shape_output": { "name": "promise", "parameters": { "shape_result": { "name": "variable", "parameters": { "name": "state" } }, "shape_reason": { "name": "any" } } } } } } } }, "run": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "state" } }, "shape_output": { "name": "void" } } } } }); })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ lib_comm.default_parameters_server_http = { "protocol": "http", "port": 80, "headers": { "Access-Control-Allow-Methods": "OPTIONS, POST", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "origin, content-type", "Access-Control-Allow-Credentials": null, "Content-Type": "plain/text; charset=utf-8", } }; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ function server_http_construct(parameters = {}) { return { "parameters": lib_object.patched(lib_comm.default_parameters_server_http, parameters), "answering": null, "serverinstance": null, }; } lib_comm.server_http_construct = server_http_construct; /** * @author fenris */ function server_http_setup(subject, answering) { subject.answering = answering; let factory; switch (subject.parameters.protocol) { case undefined: case "http": { factory = ((options, listener) => require("http").createServer(listener)); break; } case "https": { factory = ((options, listener) => require("https").createServer(options, listener)); break; } default: { return lib_call.promise_reject(new Error(`no module for protocol '${subject.parameters.protocol}'`)); break; } } const listener = (request, response) => { // headers { Object.keys(subject.parameters.headers) .forEach((name) => { const value = subject.parameters.headers[name]; if (value != null) { response.setHeader(name, value); } }); } let message = null; request.on("data", (buffer) => { message = buffer.toString(); }); request.on("end", () => { switch (request.method) { case "OPTIONS": { response.writeHead(200); response.end("options-echo"); break; } case "POST": { lib_call.promise_then_close(subject.answering(message), (answer) => { response.writeHead(answer.code); response.end(answer.text); }, (error) => { console.error(error); response.writeHead(500); response.end("internal error"); }); break; } default: { response.writeHead(500); response.end(`unhandled method ${request.method}`); break; } } }); }; const _fs = require("fs"); let options = {}; if (subject.parameters.ssl_key !== undefined) { options["key"] = _fs.readFileSync(subject.parameters.ssl_key); } if (subject.parameters.ssl_cert !== undefined) { options["cert"] = _fs.readFileSync(subject.parameters.ssl_cert); } if (subject.parameters.ssl_ca !== undefined) { options["ca"] = _fs.readFileSync(subject.parameters.ssl_ca); } // setup server instance subject.serverinstance = factory(options, listener); subject.serverinstance.on("clientError", (error, socket) => { socket.end("HTTP/1.1 400 Bad Request\r\n\r\n"); }); return lib_call.promise_resolve(undefined); } lib_comm.server_http_setup = server_http_setup; /** * @author fenris */ function server_http_run(subject) { subject.serverinstance.listen(subject.parameters.port); } lib_comm.server_http_run = server_http_run; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ lib_trait.attend("comm-server", "http", { "state": { "name": "any" }, "message_in": { "name": "string" }, "message_out": { "name": "any" // type_response_http } }, { "setup": () => (state) => (answering) => { return lib_comm.server_http_setup(state, answering).then(() => lib_call.promise_resolve(state)); }, "run": () => (state) => { return lib_comm.server_http_run(state); } }); })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ class class_server_http { /** * @author fenris */ constructor(parameters = lib_comm.default_parameters_server_http) { this.subject = lib_comm.server_http_construct(parameters); } /** * @author fenris * @implementation */ setup(answering) { return lib_comm.server_http_setup(this.subject, answering); } /** * @author fenris * @implementation */ run() { return lib_comm.server_http_run(this.subject); } } lib_comm.class_server_http = class_server_http; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ class class_server_mhttp { /** * @author fenris */ constructor(parameters = lib_comm.default_parameters_server_http) { this.core = new lib_comm.class_server_http(parameters); } /** * @author fenris * @implementation */ setup(answering) { return (this.core.setup((message) => (answering(message) .then((text) => { let response = { "code": 200, "text": text }; return lib_call.promise_resolve(response); })))); } /** * @author fenris * @implementation */ run() { this.core.run(); } } lib_comm.class_server_mhttp = class_server_mhttp; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @desc wrapper for string-based servers * @author fenris */ class class_server_complex { /** * @author fenris */ constructor(core, code = new lib_code.class_code_pair(new lib_code.class_code_json(), new lib_code.class_code_uri())) { this.core = core; this.code = code; } /** * @author fenris * @implementation */ setup(answering) { return (this.core.setup((message_in_raw) => { const message_in = ((message_in_raw == null) ? null : this.code.decode(message_in_raw)); return (answering(message_in) .then((message_out) => { const message_out_raw = this.code.encode(message_out); return lib_call.promise_resolve(message_out_raw); })); })); } /** * @author fenris * @implementation */ run() { this.core.run(); } } lib_comm.class_server_complex = class_server_complex; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** */ function server_common_make(port, handle, verbosity = 1) { return { "port": port, "handle": handle, "verbosity": verbosity, "server": undefined, }; } lib_comm.server_common_make = server_common_make; /** */ function log(subject, level, message) { if (subject.verbosity >= level) { console.log("[server]", message); } else { // do nothing } } /** */ function server_common_start(subject, finish = undefined) { const net = require("net"); subject.server = net.createServer((socket) => { log(subject, 2, "client connected"); socket.on("readable", () => { let chunk; while (!((chunk = socket.read()) === null)) { const input = chunk.toString(); log(subject, 3, "reading: " + input); subject.handle(input) .then((output) => { log(subject, 3, "writing: " + output); socket.write(output); socket.end(); }) .catch((error) => { log(subject, 1, "handle failed: " + error.toString()); }); } }); socket.on("end", () => { log(subject, 2, "client disconnected"); }); }); subject.server.on("error", (error) => { throw error; }); subject.server.listen(subject.port, () => { log(subject, 1, "listening on port " + subject.port.toFixed(0)); if (!(finish === undefined)) { finish(); } }); } lib_comm.server_common_start = server_common_start; /** */ function server_common_kill(subject) { subject.server.close(); } lib_comm.server_common_kill = server_common_kill; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:comm-server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:comm-server«. If not, see <common://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ class class_server_common { /** * @author fenris */ constructor(port, handle, verbosity = undefined) { this.subject = lib_comm.server_common_make(port, handle, verbosity); } /** * @author fenris */ start(finish = undefined) { return lib_comm.server_common_start(this.subject, finish); } /** * @author fenris */ kill() { return lib_comm.server_common_kill(this.subject); } } lib_comm.class_server_common = class_server_common; })(lib_comm || (lib_comm = {}));