From 82ed52cff184b2103bc81191d6abdff057650de1 Mon Sep 17 00:00:00 2001 From: Laurent Christophe Date: Fri, 9 Apr 2021 11:57:20 +0200 Subject: [PATCH] refactor: rename some server files --- lib/client/es2015/node/request/curl-sync.js | 51 ++++++++++++---- lib/client/es2015/node/request/http1-async.js | 23 ++++--- lib/client/es2015/node/request/http2-async.js | 15 +++-- lib/client/es2015/node/request/http3-sync.js | 1 - lib/server/{ => appmap}/appmap.mjs | 22 ++++--- lib/server/{ => appmap}/file.mjs | 0 lib/server/{ => appmap}/git.mjs | 0 lib/server/{ => appmap}/namespace.mjs | 5 +- lib/server/channel/fork.mjs | 0 lib/server/channel/inline.mjs | 5 -- lib/server/channel/util/fork.mjs | 39 ------------ lib/server/channel/util/http-1-1.mjs | 56 ----------------- lib/server/channel/util/http-2-0.mjs | 49 --------------- lib/server/channel/util/inline.mjs | 16 ----- lib/server/dispatcher.mjs | 1 + lib/server/inline/channel.mjs | 5 ++ .../{inline-main.mjs => inline/main.mjs} | 2 +- lib/server/inline/response.mjs | 28 +++++++++ lib/server/instrument/location.mjs | 9 ++- lib/server/instrument/visit.mjs | 1 - lib/server/{channel/server.mjs => main.mjs} | 29 ++++----- lib/server/response/fork.mjs | 43 +++++++++++++ lib/server/response/http1.mjs | 60 +++++++++++++++++++ lib/server/response/http2.mjs | 48 +++++++++++++++ 24 files changed, 286 insertions(+), 222 deletions(-) rename lib/server/{ => appmap}/appmap.mjs (83%) rename lib/server/{ => appmap}/file.mjs (100%) rename lib/server/{ => appmap}/git.mjs (100%) rename lib/server/{ => appmap}/namespace.mjs (88%) delete mode 100644 lib/server/channel/fork.mjs delete mode 100644 lib/server/channel/inline.mjs delete mode 100644 lib/server/channel/util/fork.mjs delete mode 100644 lib/server/channel/util/http-1-1.mjs delete mode 100644 lib/server/channel/util/http-2-0.mjs delete mode 100644 lib/server/channel/util/inline.mjs create mode 100644 lib/server/inline/channel.mjs rename lib/server/{inline-main.mjs => inline/main.mjs} (98%) create mode 100644 lib/server/inline/response.mjs rename lib/server/{channel/server.mjs => main.mjs} (76%) create mode 100644 lib/server/response/fork.mjs create mode 100644 lib/server/response/http1.mjs create mode 100644 lib/server/response/http2.mjs diff --git a/lib/client/es2015/node/request/curl-sync.js b/lib/client/es2015/node/request/curl-sync.js index 0351eac24..80ccd6740 100644 --- a/lib/client/es2015/node/request/curl-sync.js +++ b/lib/client/es2015/node/request/curl-sync.js @@ -1,4 +1,3 @@ - const global_Reflect_apply = Reflect.apply; const global_String_prototype_split = String.prototype.split; const global_Error = Error; @@ -8,10 +7,13 @@ const global_JSON_stringify = JSON.stringify; module.exports = (version, host, port) => { const args = [ `--${version}`, - "--request", "PUT", - "--data", "@-", - "--header", "content-type: application/json; charset=utf-8", - `http://${host}:${port}` + "--request", + "PUT", + "--data", + "@-", + "--header", + "content-type: application/json; charset=utf-8", + `http://${host}:${port}`, ]; return (data) => { const result = ChildProcess.spawnSync("curl", args, { @@ -25,19 +27,44 @@ module.exports = (version, host, port) => { throw new global_Error(`Unexpected kill signal: ${result.signal}`); } if (result.status !== 0) { - throw new global_Error(`curl request failed with ${result.status} >> ${result.stderr}`); + throw new global_Error( + `curl request failed with ${result.status} >> ${result.stderr}` + ); } - if (!global_Reflect_apply(global_String_prototype_startsWith, result.stdout, ["HTTP/2 200 "])) { - const index = global_Reflect_apply(global_String_prototype_indexOf, result.stdout, ["\r\n"]); + if ( + !global_Reflect_apply(global_String_prototype_startsWith, result.stdout, [ + "HTTP/2 200 ", + ]) + ) { + const index = global_Reflect_apply( + global_String_prototype_indexOf, + result.stdout, + ["\r\n"] + ); if (index === -1) { - throw new global_Error("Cannot extract status line from curl http/2 response"); + throw new global_Error( + "Cannot extract status line from curl http/2 response" + ); } - throw new global_Error(global_Reflect_apply(global_String_prototype_substring, result.stdout, [0, index])); + throw new global_Error( + global_Reflect_apply(global_String_prototype_substring, result.stdout, [ + 0, + index, + ]) + ); } - const index = global_Reflect_apply(global_String_prototype_indexOf, result.stdout, ["\r\n\r\n"]); + const index = global_Reflect_apply( + global_String_prototype_indexOf, + result.stdout, + ["\r\n\r\n"] + ); if (index === -1) { throw new global_Error("Cannot extract body from curl http/2 response"); } - return global_JSON_parse(global_Reflect_apply(global_String_prototype_substring, result.stdout, [index + 4])); + return global_JSON_parse( + global_Reflect_apply(global_String_prototype_substring, result.stdout, [ + index + 4, + ]) + ); }; }; diff --git a/lib/client/es2015/node/request/http1-async.js b/lib/client/es2015/node/request/http1-async.js index c66047e0e..080c96465 100644 --- a/lib/client/es2015/node/request/http1-async.js +++ b/lib/client/es2015/node/request/http1-async.js @@ -1,5 +1,4 @@ - -const { request : connect } = require("http"); +const { request: connect } = require("http"); const global_Error = Error; const global_JSON_stringify = JSON.stringify; const global_JSON_parse = JSON.parse; @@ -11,11 +10,13 @@ const discard = (response) => { if (response.statusCode !== 200) { let body = ""; response.setEncoding("utf8"); - response.on('data', (data) => { + response.on("data", (data) => { body += data; }); response.on("end", () => { - throw new global_Error(`http status ${global_String(response.statusCode)}: ${body}`); + throw new global_Error( + `http status ${global_String(response.statusCode)}: ${body}` + ); }); } else { response.on("data", noop); @@ -24,15 +25,15 @@ const discard = (response) => { }; module.exports = (host, port) => { - const cache = {__proto__:null}; + const cache = { __proto__: null }; const options = { host, port, method: "PUT", path: `/${name}`, headers: { - "content-type": "application/json; charset=utf-8" - } + "content-type": "application/json; charset=utf-8", + }, }; return (data, pending) => { const request = connect(options); @@ -47,9 +48,13 @@ module.exports = (host, port) => { result += data; }); response.on("error", pending.reject); - response.on('end', () => { + response.on("end", () => { if (response.statusCode !== 200) { - return pending.reject(new global_Error(`http status ${global_String(response.statusCode)}: ${body}`)); + return pending.reject( + new global_Error( + `http status ${global_String(response.statusCode)}: ${body}` + ) + ); } try { pending.resolve(global_JSON_parse(result)); diff --git a/lib/client/es2015/node/request/http2-async.js b/lib/client/es2015/node/request/http2-async.js index 56f26f6f9..0ddf6681a 100644 --- a/lib/client/es2015/node/request/http2-async.js +++ b/lib/client/es2015/node/request/http2-async.js @@ -1,5 +1,6 @@ +const { connect } = require("http2"); -const {connect} = require("http2"); +const global_String = String; const global_Error = Error; const global_JSON_stringify = JSON.stringify; const global_JSON_parse = JSON.parse; @@ -13,9 +14,13 @@ const checkStatus = function (headers) { } else { let body = ""; this.setEncoding("utf8"); - this.on("data", (data) => { body += data; }); + this.on("data", (data) => { + body += data; + }); this.on("end", () => { - throw new global_Error(`http status ${global_String(headers[":status"])}: ${body}`); + throw new global_Error( + `http status ${global_String(headers[":status"])}: ${body}` + ); }); } }; @@ -25,7 +30,7 @@ module.exports = (host, port) => { const headers = { ":method": "PUT", ":path": `/`, - "content-type": "application/json; charset=utf-8" + "content-type": "application/json; charset=utf-8", }; return (data, pending) => { const request = session.request(headers); @@ -39,7 +44,7 @@ module.exports = (host, port) => { request.on("response", (headers) => { status = headers[":status"]; }); - request.on("data", (data) => body += data); + request.on("data", (data) => (body += data)); request.on("end", () => { if (status !== 200) { reject(new Error(`http status ${global_String(status)} >> ${body}`)); diff --git a/lib/client/es2015/node/request/http3-sync.js b/lib/client/es2015/node/request/http3-sync.js index 7e966e4a4..f8890dc99 100644 --- a/lib/client/es2015/node/request/http3-sync.js +++ b/lib/client/es2015/node/request/http3-sync.js @@ -1,4 +1,3 @@ - const global_Error = Error; module.exports = () => { diff --git a/lib/server/appmap.mjs b/lib/server/appmap/appmap.mjs similarity index 83% rename from lib/server/appmap.mjs rename to lib/server/appmap/appmap.mjs index 534249d46..2c34432f4 100644 --- a/lib/server/appmap.mjs +++ b/lib/server/appmap/appmap.mjs @@ -1,10 +1,10 @@ import * as Path from 'path'; import * as FileSystem from 'fs'; -import logger from './logger.mjs'; +import logger from '../logger.mjs'; import Namespace from './namespace.mjs'; import git from './git.mjs'; import File from './file.mjs'; -import instrument from './instrument/index.mjs'; +import instrument from '../instrument/index.mjs'; const VERSION = '1.4'; @@ -35,7 +35,7 @@ export default (class Appmap { recorder: null, recording: null, engine: null, - ...json + ...json, }; this.config = config.extendWithEnv(init.env); this.namespace = new Namespace(config.getEscapePrefix()); @@ -84,18 +84,22 @@ export default (class Appmap { } emit(event) { if (this.terminated) { - throw new Error("Terminated appmap can no longer receive events"); + throw new Error('Terminated appmap can no longer receive events'); } this.state.appmap.events.push(event); } terminate(reason) { if (this.terminated) { - throw new Error("Terminated appmap can no longer be terminated"); + throw new Error('Terminated appmap can no longer be terminated'); } this.terminated = true; - FileSystem.writeFileSync(Path.join( - this.state.config.getOutputDir(), - `${this.state.config.getMapName()}.appmap.json`, - ), JSON.stringify(this.state.appmap), 'utf8'); + FileSystem.writeFileSync( + Path.join( + this.state.config.getOutputDir(), + `${this.state.config.getMapName()}.appmap.json`, + ), + JSON.stringify(this.state.appmap), + 'utf8', + ); } }); diff --git a/lib/server/file.mjs b/lib/server/appmap/file.mjs similarity index 100% rename from lib/server/file.mjs rename to lib/server/appmap/file.mjs diff --git a/lib/server/git.mjs b/lib/server/appmap/git.mjs similarity index 100% rename from lib/server/git.mjs rename to lib/server/appmap/git.mjs diff --git a/lib/server/namespace.mjs b/lib/server/appmap/namespace.mjs similarity index 88% rename from lib/server/namespace.mjs rename to lib/server/appmap/namespace.mjs index ac6ba50dd..b7064d6e3 100644 --- a/lib/server/namespace.mjs +++ b/lib/server/appmap/namespace.mjs @@ -1,4 +1,3 @@ - const globals = { __proto__: null, UNDEFINED: null, @@ -33,7 +32,9 @@ export default (class Namespace { } checkCollision(identifier) { if (identifier.startsWith(this.prefix)) { - throw new Error(`Base-level identifier should never start with the escape prefix ${this.prefix}, got: ${identifier}`); + throw new Error( + `Base-level identifier should never start with the escape prefix ${this.prefix}, got: ${identifier}`, + ); } } getGlobal(name) { diff --git a/lib/server/channel/fork.mjs b/lib/server/channel/fork.mjs deleted file mode 100644 index e69de29bb..000000000 diff --git a/lib/server/channel/inline.mjs b/lib/server/channel/inline.mjs deleted file mode 100644 index ca263aced..000000000 --- a/lib/server/channel/inline.mjs +++ /dev/null @@ -1,5 +0,0 @@ -import { getDefaultConfig } from './config.mjs'; -import { makeDispatch } from "./dispatch.mjs"; -import { makeChannel } from "./util/inline.mjs"; - -export default (env) => makeChannel(makeDispatch(getDefaultConfig())); diff --git a/lib/server/channel/util/fork.mjs b/lib/server/channel/util/fork.mjs deleted file mode 100644 index 25b0c6f82..000000000 --- a/lib/server/channel/util/fork.mjs +++ /dev/null @@ -1,39 +0,0 @@ - -import logger from "../logger.mjs"; - -const onChildError = (error) => { - logger.error("Child error: %s", error.stack); -}; - -export const registerChild = (child, dispatcher) => { - child.on("error", onChildError); - child.on("message", (json) => { - logger.info("fork request %j", json); - if (Reflect.getOwnPropertyDescriptor(json, "index") === undefined) { - logger.error("Missing index field on %j", json); - } else if (Reflect.getOwnPropertyDescriptor(json, "query") === undefined) { - logger.error("Missing query field on %j", json); - } else { - let success = null; - let failure = null; - try { - success = dispatcher.dispatch(json.query); - } catch (error) { - logger.error(`Error while handling %j\n%s`, json, error.stack); - failure = error.message - } - if (json.index === null) { - if (success !== null) { - logger.error("Expected dispatcher to return null, got: %j", success); - } - } else { - logger.info("fork response: {index:%j, success:%j, failure: %j}", json.index, success, failure); - child.send({ - index: json.index, - success, - failure - }); - } - } - }); -}; diff --git a/lib/server/channel/util/http-1-1.mjs b/lib/server/channel/util/http-1-1.mjs deleted file mode 100644 index 23af4ecd8..000000000 --- a/lib/server/channel/util/http-1-1.mjs +++ /dev/null @@ -1,56 +0,0 @@ - -import {createServer} from "http"; -import logger from "./logger.mjs"; - -const onServerListen = function (error) { - logger.info("http1 server listening on %j", this.address()); -}; - -const onServerError = function (error) { - logger.error("http1 server error %s", error.stack); -} - -const onRequestError = function (error) { - logger.error("http1 request error %s", error.stack); -}; - -const onResponseError = function (error) { - logger.error("http1 response error %s", error.stack); -}; - -export const makeServer = (dispatcher) => { - const server = createServer(); - server.on("error", onServerError); - server.on("listening", onServerListening); - server.on("request", (request, response) => { - logger.info("http1 request head: %s %s %j", request.method, request.path, request.headers); - request.setEncoding("utf8"); - response.setEncoding("utf8"); - request.on("error", onRequestError); - response.on("error", onResponseError); - let body1 = ""; - request.on("data", (data) => { - body1 += data; - }); - request.on("end", () => { - logger.info("http1 request body: %s", body1); - let status = 200; - const headers = { - "content-type": "application/json; charset=utf-8" - }; - let body2; - try { - body2 = JSON.stringify(dispatcher.dispatch(JSON.parse(body1))); - } catch (error) { - logger.error("Error during handling of: %s \n%s", body1, error.stack); - status = 400; - body2 = error.message; - headers["content-type"] = "text/plain; charset=utf-8"; - } - logger.info("http response: %i %s", status, body2); - response.writeHead(status, headers); - response.end(body2); - }); - }); - return server; -}; diff --git a/lib/server/channel/util/http-2-0.mjs b/lib/server/channel/util/http-2-0.mjs deleted file mode 100644 index 63ef66f01..000000000 --- a/lib/server/channel/util/http-2-0.mjs +++ /dev/null @@ -1,49 +0,0 @@ - -import {createServer} from "http2"; -import logger from "./logger.mjs"; - -const onServerListening = function (error) { - logger.info("http2 Server listening on %j", this.address()); -}; - -const onServerError = function (error) { - logger.error("http2 server error %s", error.stack); -} - -const onStreamError = function (error) { - logger.error("http2 stream error %s", error.stack); -}; - -export const makeServer = (dispatcher) => { - const server = createServer(); - server.on("error", onServerError); - server.on("listening", onServerListening); - server.on("stream", (stream, headers) => { - logger.info("http2 request head: %j", headers); - stream.setEncoding("utf8"); - stream.on("error", onStreamError); - let body1 = ""; - stream.on("data", (data) => { - body1 += data; - }); - stream.on("end", () => { - logger.info("http2 request body: %s", body1); - let body2; - const headers = { - ":status": 200, - "content-type": "application/json; charset=utf-8" - }; - try { - body2 = JSON.stringify(dispatcher.dispatch(JSON.parse(body1))); - } catch (error) { - logger.error("Error while processing %s\n%s", body1, error.stack); - headers[":status"] = 400; - body2 = error.message; - } - logger.info("http2 response %j %s", headers, body2); - stream.respond(headers); - stream.end(body2, "utf8"); - }); - }); - return server; -}; diff --git a/lib/server/channel/util/inline.mjs b/lib/server/channel/util/inline.mjs deleted file mode 100644 index 2e948e17a..000000000 --- a/lib/server/channel/util/inline.mjs +++ /dev/null @@ -1,16 +0,0 @@ - -import logger from "../../logger.mjs"; - -export const makeChannel = (dispatcher) => ({ - requestSync: (data) => { - logger.info(); - const result = dispatch() - }; - requestAsync: (data, pending) => { - if (pending === null) { - if (dispatch(data) !== null) { - - } - } - } -}); diff --git a/lib/server/dispatcher.mjs b/lib/server/dispatcher.mjs index be00dbe42..4f8ecda7e 100644 --- a/lib/server/dispatcher.mjs +++ b/lib/server/dispatcher.mjs @@ -1,5 +1,6 @@ import logger from "./logger.mjs"; +import Appmap from "./appmap/index.mjs"; const checkHas = (object, key) => { if (Reflect.getOwnPropertyDescriptor(object, key) === undefined) { diff --git a/lib/server/inline/channel.mjs b/lib/server/inline/channel.mjs new file mode 100644 index 000000000..e05fd3eb7 --- /dev/null +++ b/lib/server/inline/channel.mjs @@ -0,0 +1,5 @@ +import { getDefaultConfig } from './config.mjs'; +import { makeDispatch } from './dispatch.mjs'; +import { makeChannel } from './response/inline.mjs'; + +export default () => makeChannel(makeDispatch(getDefaultConfig())); diff --git a/lib/server/inline-main.mjs b/lib/server/inline/main.mjs similarity index 98% rename from lib/server/inline-main.mjs rename to lib/server/inline/main.mjs index 710e9d8ab..73b929569 100644 --- a/lib/server/inline-main.mjs +++ b/lib/server/inline/main.mjs @@ -30,8 +30,8 @@ export default (argv, stdio) => { }, { __proto__: null }, ), + APPMAP_CHANNEL: "inline" }; - env.APPMAP_CHANNEL = 'inline'; if (Reflect.getOwnPropertyDescriptor(argv, 'esm') && argv.esm) { let name; if (Reflect.getOwnPropertyDescriptor(argv, 'cjs') && argv.cjs) { diff --git a/lib/server/inline/response.mjs b/lib/server/inline/response.mjs new file mode 100644 index 000000000..65c570be3 --- /dev/null +++ b/lib/server/inline/response.mjs @@ -0,0 +1,28 @@ + +import logger from "../../logger.mjs"; + +export const makeChannel = (dispatcher) => ({ + requestSync: (json1) => { + logger.info(); + const json2 = dispatcher.dispatch(json1); + logger.info(); + }; + requestAsync: (json1, pending) => { + logger.info(); + if (pending === null) { + const json2 = dispatcher.dispatch(json1); + if (json1 !== null) { + throw new Error(``); + } + } else { + let json2; + try { + json2 = dispatcher.dispatch(json1); + } catch (error) { + pending.reject(error); + } + logger.info(json2); + pending.resolve(json2); + } + } +}); diff --git a/lib/server/instrument/location.mjs b/lib/server/instrument/location.mjs index 51c13760d..4ed204ea3 100644 --- a/lib/server/instrument/location.mjs +++ b/lib/server/instrument/location.mjs @@ -1,4 +1,3 @@ - const getPatternName = (node) => { if (node === null) { return '@#default'; @@ -32,7 +31,9 @@ class Location { } getKind() { if (this.node.type !== 'VariableDeclaration') { - throw new Error(`Expected node to be of type VariableDeclaration and got: ${this.node.type}`) + throw new Error( + `Expected node to be of type VariableDeclaration and got: ${this.node.type}`, + ); } return this.node.kind; } @@ -107,7 +108,9 @@ class Location { } isChildNonScopingIdentifier(node) { if (node.type !== 'Identifier') { - throw new Error(`isChildScopingIdentifier should only be called on Identifier node, got: ${node.type}`); + throw new Error( + `isChildScopingIdentifier should only be called on Identifier node, got: ${node.type}`, + ); } if ( this.node.type === 'BreakStatement' || diff --git a/lib/server/instrument/visit.mjs b/lib/server/instrument/visit.mjs index eaa9521d4..a8fb8e17c 100644 --- a/lib/server/instrument/visit.mjs +++ b/lib/server/instrument/visit.mjs @@ -1,4 +1,3 @@ - const visitors = { __proto__: null }; export const setVisitor = (type, split, join) => { diff --git a/lib/server/channel/server.mjs b/lib/server/main.mjs similarity index 76% rename from lib/server/channel/server.mjs rename to lib/server/main.mjs index f9bd085a1..9f2b0e139 100644 --- a/lib/server/channel/server.mjs +++ b/lib/server/main.mjs @@ -1,10 +1,18 @@ import { fork } from "child_process"; -import { makeServer as createHttp11Server } from "./util/http-1-1.mjs"; -import { makeServer as createHttp20Server } from "./util/http-2-0.mjs"; -import { registerChild } from "./util/fork.mjs"; +import { makeServer as createHttp1Server } from "./response/http1.mjs"; +import { makeServer as createHttp2Server } from "./response/http2.mjs"; +import { registerChild } from "./response/fork.mjs"; import home from "../../../home.js"; +const mapping = { + "1": createHttp1Server, + "1.0": createHttp1Server, + "1.1": createHttp1Server, + "2": createHttp2Server, + "2.0": createHttp2Server +}; + export default = (env) => { env = { APPMAP_PORT: "8080", @@ -12,18 +20,11 @@ export default = (env) => { ...env }; const dispatcher = new Dispatcher(getDefaultConfig().extendWithEnv(env)); - let server; - if (options.APPMAP_HTTP_VERSION === "1.1") { - logger.info("Creating http/1.1 server"); - server = createHttp11Server(dispatcher); - } else if (options.APPMAP_HTTP_VERSION === "2.0" || options.APPMAP_HTTP_VERSION === "2") { - logger.info("Creating http/2.0 server"); - server = createHttp20Server(dispatcher); - } else { - logger.warning("Unrecognized APPMAP_HTTP_VERSION, defaulting to 1.1; expected either '1.1', '2.0', or '2' and got: %s", env.APPMAP_HTTP_VERSION); - options.APPMAP_HTTP_VERSION = "1.1"; - server = createHttp11Server(dispatcher); + if (!(env.APPMAP_HTTP_VERSION in mapping)) { + logger.warning("Unrecognized APPMAP_HTTP_VERSION, defaulting to 1.1 and got: %s", env.APPMAP_HTTP_VERSION); + env.APPMAP_HTTP_VERSION = "1.1"; } + const server = mapping[env.APPMAP_HTTP_VERSION](dispatcher); server.listen(env.APPMAP_PORT); return { server, diff --git a/lib/server/response/fork.mjs b/lib/server/response/fork.mjs new file mode 100644 index 000000000..7bc627a48 --- /dev/null +++ b/lib/server/response/fork.mjs @@ -0,0 +1,43 @@ +import logger from '../logger.mjs'; + +const onChildError = (error) => { + logger.error('Child error: %s', error.stack); +}; + +export const registerChild = (child, dispatcher) => { + child.on('error', onChildError); + child.on('message', (json) => { + logger.info('fork request %j', json); + if (Reflect.getOwnPropertyDescriptor(json, 'index') === undefined) { + logger.error('Missing index field on %j', json); + } else if (Reflect.getOwnPropertyDescriptor(json, 'query') === undefined) { + logger.error('Missing query field on %j', json); + } else { + let success = null; + let failure = null; + try { + success = dispatcher.dispatch(json.query); + } catch (error) { + logger.error(`Error while handling %j\n%s`, json, error.stack); + failure = error.message; + } + if (json.index === null) { + if (success !== null) { + logger.error('Expected dispatcher to return null, got: %j', success); + } + } else { + logger.info( + 'fork response: {index:%j, success:%j, failure: %j}', + json.index, + success, + failure, + ); + child.send({ + index: json.index, + success, + failure, + }); + } + } + }); +}; diff --git a/lib/server/response/http1.mjs b/lib/server/response/http1.mjs new file mode 100644 index 000000000..3d790fcf7 --- /dev/null +++ b/lib/server/response/http1.mjs @@ -0,0 +1,60 @@ +import { createServer } from 'http'; +import logger from './logger.mjs'; + +const onServerListen = function (error) { + logger.info('http1 server listening on %j', this.address()); +}; + +const onServerError = function (error) { + logger.error('http1 server error %s', error.stack); +}; + +const onRequestError = function (error) { + logger.error('http1 request error %s', error.stack); +}; + +const onResponseError = function (error) { + logger.error('http1 response error %s', error.stack); +}; + +export const makeServer = (dispatcher) => { + const server = createServer(); + server.on('error', onServerError); + server.on('listening', onServerListening); + server.on('request', (request, response) => { + logger.info( + 'http1 request head: %s %s %j', + request.method, + request.path, + request.headers, + ); + request.setEncoding('utf8'); + response.setEncoding('utf8'); + request.on('error', onRequestError); + response.on('error', onResponseError); + let body1 = ''; + request.on('data', (data) => { + body1 += data; + }); + request.on('end', () => { + logger.info('http1 request body: %s', body1); + let status = 200; + const headers = { + 'content-type': 'application/json; charset=utf-8', + }; + let body2; + try { + body2 = JSON.stringify(dispatcher.dispatch(JSON.parse(body1))); + } catch (error) { + logger.error('Error during handling of: %s \n%s', body1, error.stack); + status = 400; + body2 = error.message; + headers['content-type'] = 'text/plain; charset=utf-8'; + } + logger.info('http response: %i %s', status, body2); + response.writeHead(status, headers); + response.end(body2); + }); + }); + return server; +}; diff --git a/lib/server/response/http2.mjs b/lib/server/response/http2.mjs new file mode 100644 index 000000000..875a88247 --- /dev/null +++ b/lib/server/response/http2.mjs @@ -0,0 +1,48 @@ +import { createServer } from 'http2'; +import logger from './logger.mjs'; + +const onServerListening = function (error) { + logger.info('http2 Server listening on %j', this.address()); +}; + +const onServerError = function (error) { + logger.error('http2 server error %s', error.stack); +}; + +const onStreamError = function (error) { + logger.error('http2 stream error %s', error.stack); +}; + +export const makeServer = (dispatcher) => { + const server = createServer(); + server.on('error', onServerError); + server.on('listening', onServerListening); + server.on('stream', (stream, headers) => { + logger.info('http2 request head: %j', headers); + stream.setEncoding('utf8'); + stream.on('error', onStreamError); + let body1 = ''; + stream.on('data', (data) => { + body1 += data; + }); + stream.on('end', () => { + logger.info('http2 request body: %s', body1); + let body2; + const headers = { + ':status': 200, + 'content-type': 'application/json; charset=utf-8', + }; + try { + body2 = JSON.stringify(dispatcher.dispatch(JSON.parse(body1))); + } catch (error) { + logger.error('Error while processing %s\n%s', body1, error.stack); + headers[':status'] = 400; + body2 = error.message; + } + logger.info('http2 response %j %s', headers, body2); + stream.respond(headers); + stream.end(body2, 'utf8'); + }); + }); + return server; +};