mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-29 07:16:36 +01:00
4cdf251ea5
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
69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
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
|
|
* SEGMENT_LEGACY_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)
|