forked from MetaMask/metamask-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock-segment.js
executable file
·68 lines (58 loc) · 1.99 KB
/
mock-segment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
const { createSegmentServer } = require('./lib/create-segment-server');
const { parsePort } = require('./lib/parse-port');
const DEFAULT_PORT = 9090;
const prefix = '[mock-segment]';
function onRequest(request, response, events) {
console.log(`${prefix}: ${request.method} ${request.url}`);
const eventDescriptions = events.map((event) => {
if (event.type === 'track') {
return event.event;
} else if (event.type === 'page') {
return event.name;
}
return `[Unrecognized event type: ${event.type}]`;
});
console.log(`${prefix}: Events received: ${eventDescriptions.join(', ')}`);
response.statusCode = 200;
response.end();
}
function onError(error) {
console.error(error);
process.exit(1);
}
/**
* 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 () => {
const args = process.argv.slice(2);
let port = process.env.port || DEFAULT_PORT;
while (args.length) {
if (/^(--port|-p)$/u.test(args[0])) {
if (args[1] === undefined) {
throw new Error('Missing port argument');
}
port = parsePort(args[1]);
args.splice(0, 2);
}
}
const server = createSegmentServer(onRequest, onError);
await server.start(port);
console.log(`${prefix}: Listening on port ${port}`);
};
main().catch(onError);