git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
6ae1cb8
Branches
Tags
develop-client_server
master
typescript
fs-words.git
client
lib
plankton
comm-client
logic-impl.js
[add] client
Christian Fraß
commited
6ae1cb8
at 2021-03-08 23:52:24
logic-impl.js
Blame
History
Raw
/* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« 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-client« 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-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ lib_trait.define("comm-client", { "state": null, "message_in": null, "message_out": null }, { "setup": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "state" } }, "shape_output": { "name": "promise", "parameters": { "shape_result": { "name": "variable", "parameters": { "name": "state" } }, "shape_reason": { "name": "any" } } } } } }, "send": { "shape": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "state" } }, "shape_output": { "name": "function", "parameters": { "shape_input": { "name": "variable", "parameters": { "name": "message_out" } }, "shape_output": { "name": "promise", "parameters": { "shape_result": { "name": "variable", "parameters": { "name": "message_in" } }, "shape_reason": { "name": "any" } } } } } } } } }); })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« 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-client« 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-client«. If not, see <http://www.gnu.org/licenses/>. */ /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« 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-client« 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-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ lib_comm.default_parameters_client_http = { "protocol": "http", "host": null, "port": null, "path": null, "omit_protocol": false, "content_type": "plain/text; charset=utf-8", "with_credentials": false, }; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« 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-client« 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-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ function client_http_construct(parameters = {}, method = "POST", headers = {}) { return { "parameters": lib_object.patched(lib_comm.default_parameters_client_http, parameters), "method": method.toLowerCase(), "headers": headers, }; } lib_comm.client_http_construct = client_http_construct; /** * @author fenris */ function client_http_setup(subject) { return (lib_call.promise_resolve(undefined)); } lib_comm.client_http_setup = client_http_setup; /** * @author fenris */ function client_http_send(subject, message) { return (lib_call.promise_make((resolve, reject) => { if ((subject.method === "get") && (message !== null)) { let message_ = "can not send data via GET"; reject(new Error(message_)); } else { let request = new XMLHttpRequest(); let url = lib_string.make_url({ "protocol": (subject.parameters.omit_protocol ? null : subject.parameters.protocol), "host": subject.parameters.host, "port": subject.parameters.port, "path": subject.parameters.path, }); request.open(subject.method.toUpperCase(), url, true); lib_object.to_array(subject.headers).forEach(({ "key": key, "value": value }) => { request.setRequestHeader(key, value); }); request.setRequestHeader("Content-Type", subject.parameters.content_type); if (subject.parameters.with_credentials) { request.withCredentials = true; } /* request.onerror = () => { debugger; reject(new Error("unhandled error")); }; */ request.onreadystatechange = () => { switch (request.readyState) { case XMLHttpRequest.DONE: { switch (request.status) { case 0: { reject(new Error("XMLHttpRequest failed")); break; } default: { let answer = { "code": request.status, "text": request.responseText }; resolve(answer); break; } } break; } default: { // console.warn(`unhandled readyState ${request.readyState}`); break; } } }; request.send(message); } })); } lib_comm.client_http_send = client_http_send; /** * @author Martin Springwald <springwald@greenscale.de> * @author Christian Neubauer <neubauer@greenscale.de> * @author Christian Fraß <frass@greenscale.de> */ function http_get_blob({ "url": url, "mime": type }) { let request = new XMLHttpRequest(); return new Promise((resolve, reject) => { request.open("GET", url, true); request.responseType = "arraybuffer"; request.onload = function () { if (request.status === 200) { resolve(new Blob([request.response], { "type": type })); } else { reject(new Error("error on blob-load")); } }; request.onerror = function () { reject(undefined); }; request.send(); }); } lib_comm.http_get_blob = http_get_blob; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« 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-client« 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-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ lib_trait.attend("comm-client", "http", { "state": { "name": "any" }, "message_in": { "name": "string" }, "message_out": { "name": "any" // type_response_http } }, { "setup": () => (state) => { return lib_comm.client_http_setup(state).then(_ => lib_call.promise_resolve(state)); }, "send": () => (state) => (message) => { return lib_comm.client_http_send(state, message); } }); })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« 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-client« 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-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ class class_client_http { /** * @author fenris */ constructor(parameters = lib_comm.default_parameters_client_http, method = "POST", headers = {}) { this.subject = lib_comm.client_http_construct(parameters, method, headers); } /** * @author fenris * @implementation */ setup() { return lib_comm.client_http_setup(this.subject); } /** * @author fenris * @implementation */ send(message) { return lib_comm.client_http_send(this.subject, message); } } lib_comm.class_client_http = class_client_http; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« 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-client« 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-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @author fenris */ class class_client_mhttp { /** * @author fenris */ constructor(parameters = lib_comm.default_parameters_client_http) { this.core = new lib_comm.class_client_http(parameters); } /** * @author fenris * @implementation */ setup() { return this.core.setup(); } /** * @author fenris * @implementation */ send(message) { return (this.core.send(message) .then((answer) => { switch (answer.code) { case 200: { return lib_call.promise_resolve(answer.text /*.toString()*/); break; } default: { return lib_call.promise_reject(new Error(`XMLHttpRequest failed; status was ${answer.code.toFixed(0)}`)); break; } } })); } } lib_comm.class_client_mhttp = class_client_mhttp; })(lib_comm || (lib_comm = {})); /* This file is part of »bacterio-plankton:comm-client«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:comm-client« 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-client« 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-client«. If not, see <http://www.gnu.org/licenses/>. */ var lib_comm; (function (lib_comm) { /** * @desc wrapper for string-based clients * @author fenris */ class class_client_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() { return this.core.setup(); } /** * @author fenris * @implementation */ send(message) { const message_out_raw = this.code.encode(message); return (this.core.send(message_out_raw) .then((message_in_raw) => { const message_in = this.code.decode(message_in_raw); return lib_call.promise_resolve(message_in); })); } } lib_comm.class_client_complex = class_client_complex; })(lib_comm || (lib_comm = {}));