1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00
metamask-extension/development/lib/create-segment-server.js
Mark Stacey 4cdf251ea5
Add mock Segment server (#9783)
This mock Segment server can be used to test our extension metrics. It
will respond to all request with HTTP 200, and will print the requests
to the console. It also has parsing built-in for Segment request
payloads.

Right now only the event name is printed, but we can enhance this in
the future to print more event information. We can also enhance the
mock to be a more realistic representation of the API.

The extension has been modified to allow the Segment host to be
overwritten with the `SEGMENT_HOST` environment variable. This will
ensure that all Segment events are redirected to that host.

So for example, to create a dev build that uses this server, you could
set the `SEGMENT_WRITE_KEY` and `SEGMENT_LEGACY_WRITE_KEY` values to
any non-empty string, and set `SEGMENT_HOST` to
`http://localhost:9090`.

This was created originally to test PR #9768
2020-11-09 18:15:23 -03:30

94 lines
2.5 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 }