2022-08-24 19:25:27 +02:00
|
|
|
#!/usr/bin/env node
|
2021-02-04 19:15:23 +01:00
|
|
|
const { createSegmentServer } = require('./lib/create-segment-server');
|
|
|
|
const { parsePort } = require('./lib/parse-port');
|
2020-11-09 22:45:23 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const DEFAULT_PORT = 9090;
|
|
|
|
const prefix = '[mock-segment]';
|
2020-11-09 22:45:23 +01:00
|
|
|
|
|
|
|
function onRequest(request, response, events) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log(`${prefix}: ${request.method} ${request.url}`);
|
2020-11-09 22:45:23 +01:00
|
|
|
const eventDescriptions = events.map((event) => {
|
|
|
|
if (event.type === 'track') {
|
2021-02-04 19:15:23 +01:00
|
|
|
return event.event;
|
2020-11-09 22:45:23 +01:00
|
|
|
} else if (event.type === 'page') {
|
2021-02-04 19:15:23 +01:00
|
|
|
return event.name;
|
2020-11-09 22:45:23 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return `[Unrecognized event type: ${event.type}]`;
|
|
|
|
});
|
|
|
|
console.log(`${prefix}: Events received: ${eventDescriptions.join(', ')}`);
|
2020-11-09 22:45:23 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.end();
|
2020-11-09 22:45:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function onError(error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
2020-11-09 22:45:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a mock Segment API meant to be run from the command line. It will start a server
|
|
|
|
* with the port specified, and respond with HTTP 200 to all requests. Any requests will be
|
|
|
|
* logged to the console, along with the parsed Segment events included in the request (if
|
|
|
|
* any)
|
|
|
|
*
|
|
|
|
* This can be used with the MetaMask extension by setting the `SEGMENT_HOST` environment
|
|
|
|
* variable or config entry when building MetaMask.
|
|
|
|
*
|
|
|
|
* For example, to build MetaMask for use with this mock Segment server, you could set the
|
|
|
|
* following values in `.metamaskrc` before building:
|
|
|
|
*
|
|
|
|
* SEGMENT_HOST='http://localhost:9090'
|
|
|
|
* SEGMENT_WRITE_KEY=FAKE
|
|
|
|
*
|
|
|
|
* Note that the Segment keys must also be set - otherwise the extension will not send any
|
|
|
|
* metric events.
|
|
|
|
*/
|
|
|
|
const main = async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const args = process.argv.slice(2);
|
2020-11-09 22:45:23 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let port = process.env.port || DEFAULT_PORT;
|
2020-11-09 22:45:23 +01:00
|
|
|
|
|
|
|
while (args.length) {
|
|
|
|
if (/^(--port|-p)$/u.test(args[0])) {
|
|
|
|
if (args[1] === undefined) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('Missing port argument');
|
2020-11-09 22:45:23 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
port = parsePort(args[1]);
|
|
|
|
args.splice(0, 2);
|
2020-11-09 22:45:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const server = createSegmentServer(onRequest, onError);
|
2020-11-09 22:45:23 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await server.start(port);
|
|
|
|
console.log(`${prefix}: Listening on port ${port}`);
|
|
|
|
};
|
2020-11-09 22:45:23 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
main().catch(onError);
|