mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 18:00:18 +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
78 lines
2.0 KiB
JavaScript
78 lines
2.0 KiB
JavaScript
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
const chalk = require('chalk')
|
|
const pify = require('pify')
|
|
|
|
const createStaticServer = require('./create-static-server')
|
|
const { parsePort } = require('./lib/parse-port')
|
|
|
|
const fsStat = pify(fs.stat)
|
|
const DEFAULT_PORT = 9080
|
|
|
|
const onResponse = (request, response) => {
|
|
if (response.statusCode >= 400) {
|
|
console.log(chalk`{gray '-->'} {red ${response.statusCode}} ${request.url}`)
|
|
} else if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
console.log(
|
|
chalk`{gray '-->'} {green ${response.statusCode}} ${request.url}`,
|
|
)
|
|
} else {
|
|
console.log(
|
|
chalk`{gray '-->'} {green.dim ${response.statusCode}} ${request.url}`,
|
|
)
|
|
}
|
|
}
|
|
const onRequest = (request, response) => {
|
|
console.log(chalk`{gray '<--'} {blue [${request.method}]} ${request.url}`)
|
|
response.on('finish', () => onResponse(request, response))
|
|
}
|
|
|
|
const startServer = ({ port, rootDirectory }) => {
|
|
const server = createStaticServer(rootDirectory)
|
|
|
|
server.on('request', onRequest)
|
|
|
|
server.listen(port, () => {
|
|
console.log(`Running at http://localhost:${port}`)
|
|
})
|
|
}
|
|
|
|
const parseDirectoryArgument = async (pathString) => {
|
|
const resolvedPath = path.resolve(pathString)
|
|
const directoryStats = await fsStat(resolvedPath)
|
|
if (!directoryStats.isDirectory()) {
|
|
throw new Error(`Invalid path '${pathString}'; must be a directory`)
|
|
}
|
|
return resolvedPath
|
|
}
|
|
|
|
const main = async () => {
|
|
const args = process.argv.slice(2)
|
|
|
|
const options = {
|
|
port: process.env.port || DEFAULT_PORT,
|
|
rootDirectory: path.resolve('.'),
|
|
}
|
|
|
|
while (args.length) {
|
|
if (/^(--port|-p)$/u.test(args[0])) {
|
|
if (args[1] === undefined) {
|
|
throw new Error('Missing port argument')
|
|
}
|
|
options.port = parsePort(args[1])
|
|
args.splice(0, 2)
|
|
} else {
|
|
options.rootDirectory = await parseDirectoryArgument(args[0])
|
|
args.splice(0, 1)
|
|
}
|
|
}
|
|
|
|
startServer(options)
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error)
|
|
process.exit(1)
|
|
})
|