diff --git a/.eslintrc.yml b/.eslintrc.yml index 8fda4dc72..e0303d93e 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -6,6 +6,7 @@ rules: - error - properties: never no-param-reassign: off + valid-typeof: off spaced-comment: off max-classes-per-file: off no-unused-vars: diff --git a/bin/bin.mjs b/bin/bin-inline.mjs similarity index 67% rename from bin/bin.mjs rename to bin/bin-inline.mjs index 808fde0a8..eca4d7f8c 100644 --- a/bin/bin.mjs +++ b/bin/bin-inline.mjs @@ -1,6 +1,6 @@ #!/usr/bin/env node import minimist from 'minimist'; -import main from '../lib/server/inline-main.mjs'; +import main from '../lib/server/inline/main.mjs'; main(minimist(process.argv.slice(2)), 'inherit'); diff --git a/lib/client/es2015/node/request/curl-sync.js b/lib/client/es2015/node/request/curl-sync.js index 80ccd6740..0b130217d 100644 --- a/lib/client/es2015/node/request/curl-sync.js +++ b/lib/client/es2015/node/request/curl-sync.js @@ -1,5 +1,6 @@ +const ChildProcess = require("child_process"); + const global_Reflect_apply = Reflect.apply; -const global_String_prototype_split = String.prototype.split; const global_Error = Error; const global_JSON_parse = JSON.parse; const global_JSON_stringify = JSON.stringify; diff --git a/lib/client/es2015/node/request/fork-async.mjs b/lib/client/es2015/node/request/fork-async.mjs index bbd97638e..c656a0213 100644 --- a/lib/client/es2015/node/request/fork-async.mjs +++ b/lib/client/es2015/node/request/fork-async.mjs @@ -1,16 +1,16 @@ -const ChildProcess = require("child_process"); - // process is an event emitter and send is directly added to it and not to its prototype // cf: https://github.com/nodejs/node/blob/master/lib/internal/child_process.js const global_Reflect_apply = Reflect.apply; const global_process = process; +const global_Error = Error; +const global_undefined = undefined; const global_process_send = process.send; const global_Reflect_getOwnPropertyDescriptor = Reflect.getOwnPropertyDescriptor; const getFreeIndex = (array) => { - for (let index = 0; index < array.length; index++) { + for (let index = 0; index < array.length; index += 1) { if (array[index] === null) { return index; } @@ -31,7 +31,7 @@ const checkNotNull = (value) => { } const checkHas = (object, key) => { - if (global_Reflect_getOwnPropertyDescriptor(object, key) === void 0) { + if (global_Reflect_getOwnPropertyDescriptor(object, key) === global_undefined) { throw new global_Error(`Missing property ${key}`); } }; @@ -46,7 +46,7 @@ module.exports = () => { checkHas(json, "failure"); checkTypeof(json.index, "number"); checkHas(pendings, json.index); - const pending = pending[json.index]; + const pending = pendings[json.index]; pendings[json.index] = null; if (json.failure === null) { pending.resolve(json.success); @@ -66,4 +66,4 @@ module.exports = () => { data: json }]); }; -}: +}; diff --git a/lib/server/appmap/git.mjs b/lib/server/appmap/git.mjs index d8935972d..e8d3cd1bb 100644 --- a/lib/server/appmap/git.mjs +++ b/lib/server/appmap/git.mjs @@ -1,5 +1,5 @@ import * as ChildProcess from 'child_process'; -import logger from './logger.mjs'; +import logger from '../logger.mjs'; const trim = (string) => string.trim(); diff --git a/lib/server/appmap/appmap.mjs b/lib/server/appmap/index.mjs similarity index 100% rename from lib/server/appmap/appmap.mjs rename to lib/server/appmap/index.mjs diff --git a/lib/server/dispatcher.mjs b/lib/server/dispatcher.mjs index 97798bb34..214745aaf 100644 --- a/lib/server/dispatcher.mjs +++ b/lib/server/dispatcher.mjs @@ -1,4 +1,3 @@ -import logger from './logger.mjs'; import Appmap from './appmap/index.mjs'; const checkHas = (object, key) => { diff --git a/lib/server/inline/channel.mjs b/lib/server/inline/channel.mjs index 05807f593..20b54c740 100644 --- a/lib/server/inline/channel.mjs +++ b/lib/server/inline/channel.mjs @@ -1,5 +1,5 @@ import { getDefaultConfig } from '../config.mjs'; -import { makeDispatch } from '../dispatch.mjs'; +import Dispatcher from '../dispatcher.mjs'; import { makeChannel } from './response.mjs'; -export default () => makeChannel(makeDispatch(getDefaultConfig())); +export default () => makeChannel(new Dispatcher(getDefaultConfig())); diff --git a/lib/server/inline/main.mjs b/lib/server/inline/main.mjs index 24fb0b368..eed7b4cce 100644 --- a/lib/server/inline/main.mjs +++ b/lib/server/inline/main.mjs @@ -1,6 +1,6 @@ import * as ChildProcess from 'child_process'; -import logger from './logger.mjs'; -import { home } from '../../home.js'; +import logger from '../logger.mjs'; +import { home } from '../../../home.js'; const mapping = { __proto__: null, diff --git a/lib/server/inline/response.mjs b/lib/server/inline/response.mjs index ad0a63409..27ae8aee4 100644 --- a/lib/server/inline/response.mjs +++ b/lib/server/inline/response.mjs @@ -1,27 +1,28 @@ -import logger from '../../logger.mjs'; +import logger from '../logger.mjs'; export const makeChannel = (dispatcher) => ({ requestSync: (json1) => { - logger.info(); + logger.info('inline sync request: %j', json1); const json2 = dispatcher.dispatch(json1); - logger.info(); + logger.info('inline sync response success: %j', json2); + return json2; }, 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) { + logger.info('inline async request: %j', json1); + let json2; + try { + json2 = dispatcher.dispatch(json1); + } catch (error) { + logger.info('inline async request failure: %s', error.stack); + if (pending !== null) { pending.reject(error); } - logger.info(json2); - pending.resolve(json2); + } + if (json2 !== undefined) { + logger.info('inline sync response success: %j', json2); + if (pending !== null) { + pending.resolve(json2); + } } }, }); diff --git a/lib/server/main.mjs b/lib/server/main.mjs index 696377920..bc09cecf6 100644 --- a/lib/server/main.mjs +++ b/lib/server/main.mjs @@ -1,15 +1,21 @@ -import { fork } from 'child_process'; -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'; +import ChildProcess from 'child_process'; +import Path from 'path'; +import home from '../../home.js'; +import logger from './logger.mjs'; +import Dispatcher from './dispatcher.mjs'; +import { getDefaultConfig } from './config.mjs'; +import { + makeHttp1Server, + makeHttp2Server, + registerChild, +} from './response/index.mjs'; const mapping = { - 1: createHttp1Server, - '1.0': createHttp1Server, - 1.1: createHttp1Server, - 2: createHttp2Server, - '2.0': createHttp2Server, + 1: makeHttp1Server, + '1.0': makeHttp1Server, + 1.1: makeHttp1Server, + 2: makeHttp2Server, + '2.0': makeHttp2Server, }; export default (env) => { @@ -31,7 +37,7 @@ export default (env) => { return { server, fork: (path, argv, options) => { - const options = { + options = { __proto__: null, ...options, }; @@ -78,19 +84,12 @@ export default (env) => { APPMAP_PORT: String(env.APPMAP_PORT), APPMAP_HTTP_VERSION: env.APPMAP_HTTP_VERSION, }; - for (let key in env1) { + Reflect.ownKeys(env1).forEach((key) => { + // avoid propagating redundant APPMAP environment variable if (!key.startsWith('APPMAP_') || env1[key] !== env[key]) { env2[key] = env1[key]; } - } - const env1 = { __proto__: null }; - if ('env' in options) { - env = { - __proto__: null, - ...options, - }; - } else { - } + }); const child = ChildProcess.fork(path, argv, { ...options, execArgv, diff --git a/lib/server/response/http1.mjs b/lib/server/response/http1.mjs index 3d790fcf7..81e13c9b4 100644 --- a/lib/server/response/http1.mjs +++ b/lib/server/response/http1.mjs @@ -1,19 +1,19 @@ import { createServer } from 'http'; -import logger from './logger.mjs'; +import logger from '../logger.mjs'; -const onServerListen = function (error) { +const onServerListening = function onServerListening(error) { logger.info('http1 server listening on %j', this.address()); }; -const onServerError = function (error) { +const onServerError = (error) => { logger.error('http1 server error %s', error.stack); }; -const onRequestError = function (error) { +const onRequestError = (error) => { logger.error('http1 request error %s', error.stack); }; -const onResponseError = function (error) { +const onResponseError = (error) => { logger.error('http1 response error %s', error.stack); }; diff --git a/lib/server/response/http2.mjs b/lib/server/response/http2.mjs index 875a88247..33255c1c1 100644 --- a/lib/server/response/http2.mjs +++ b/lib/server/response/http2.mjs @@ -1,15 +1,15 @@ import { createServer } from 'http2'; -import logger from './logger.mjs'; +import logger from '../logger.mjs'; -const onServerListening = function (error) { +const onServerListening = function onServerListening(error) { logger.info('http2 Server listening on %j', this.address()); }; -const onServerError = function (error) { +const onServerError = (error) => { logger.error('http2 server error %s', error.stack); }; -const onStreamError = function (error) { +const onStreamError = (error) => { logger.error('http2 stream error %s', error.stack); }; @@ -17,8 +17,8 @@ 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); + server.on('stream', (stream, headers1) => { + logger.info('http2 request head: %j', headers1); stream.setEncoding('utf8'); stream.on('error', onStreamError); let body1 = ''; @@ -28,7 +28,7 @@ export const makeServer = (dispatcher) => { stream.on('end', () => { logger.info('http2 request body: %s', body1); let body2; - const headers = { + const headers2 = { ':status': 200, 'content-type': 'application/json; charset=utf-8', }; @@ -36,11 +36,11 @@ export const makeServer = (dispatcher) => { body2 = JSON.stringify(dispatcher.dispatch(JSON.parse(body1))); } catch (error) { logger.error('Error while processing %s\n%s', body1, error.stack); - headers[':status'] = 400; + headers2[':status'] = 400; body2 = error.message; } - logger.info('http2 response %j %s', headers, body2); - stream.respond(headers); + logger.info('http2 response %j %s', headers2, body2); + stream.respond(headers2); stream.end(body2, 'utf8'); }); }); diff --git a/lib/server/response/index.mjs b/lib/server/response/index.mjs new file mode 100644 index 000000000..99bb423a6 --- /dev/null +++ b/lib/server/response/index.mjs @@ -0,0 +1,3 @@ +export { makeServer as makeHttp1Server } from './http1.mjs'; +export { makeServer as makeHttp2Server } from './http2.mjs'; +export { registerChild } from './fork.mjs';