1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00
metamask-extension/development/lib/create-segment-server.js
Mark Stacey 3732c5f71e
Add JSDoc ESLint rules (#12112)
ESLint rules have been added to enforce our JSDoc conventions. These
rules were introduced by updating `@metamask/eslint-config` to v9.

Some of the rules have been disabled because the effort to fix all lint
errors was too high. It might be easiest to enable these rules one
directory at a time, or one rule at a time.

Most of the changes in this PR were a result of running
`yarn lint:fix`. There were a handful of manual changes that seemed
obvious and simple to make. Anything beyond that and the rule was left
disabled.
2022-01-07 12:27:33 -03:30

95 lines
2.6 KiB
JavaScript

const http = require('http');
/**
* This is the default error handler to be used by this mock segment server.
* It will print the error to the console and exit the process.
*
* @param {Error} error - The server error
*/
function defaultOnError(error) {
console.log(error);
process.exit(1);
}
/**
* @typedef {import('http').IncomingMessage} IncomingMessage
* @typedef {import('http').ServerResponse} ServerResponse
*/
/**
* This function handles requests for the mock Segment server
*
* @typedef {(request: IncomingMessage, response: ServerResponse, metricEvents: Array<Object>) => void} MockSegmentRequestHandler
*/
/**
* Creates a HTTP server that acts as a fake version of the Segment API.
* The bevahiour is rudimentary at the moment - it returns HTTP 200 in response
* to every request. The only function this serves is to spy on requests sent to
* this server, and to parse the request payloads as Segment batch events.
*
* @param {MockSegmentRequestHandler} onRequest - A callback for each request the server receives.
* @param {(error: Error) => void} [onError] - A callback for server error events
*/
function createSegmentServer(onRequest, onError = defaultOnError) {
const server = http.createServer(async (request, response) => {
const chunks = [];
request.on('data', (chunk) => {
chunks.push(chunk);
});
await new Promise((resolve) => {
request.on('end', () => {
resolve();
});
});
// respond to preflight request
if (request.method === 'OPTIONS') {
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', '*');
response.setHeader('Access-Control-Allow-Headers', '*');
response.statusCode = 200;
response.end();
return;
}
let metricEvents = [];
if (chunks.length) {
const body = Buffer.concat(chunks).toString();
const segmentPayload = JSON.parse(body);
metricEvents = segmentPayload.batch;
}
onRequest(request, response, metricEvents);
});
server.on('error', onError);
return {
start: async (port) => {
await new Promise((resolve, reject) => {
server.listen(port, (error) => {
if (error) {
return reject(error);
}
return resolve();
});
});
},
stop: async () => {
await new Promise((resolve, reject) => {
server.close((error) => {
if (error) {
return reject(error);
}
return resolve();
});
});
},
};
}
module.exports = { createSegmentServer };