@spotlightjs/sidecar
Advanced tools
Comparing version 1.3.4 to 1.3.5
@@ -8,3 +8,3 @@ var __defProp = Object.defineProperty; | ||
import { createWriteStream, readFile } from "fs"; | ||
import { createServer } from "http"; | ||
import { createServer, get } from "http"; | ||
import { extname, join } from "path"; | ||
@@ -103,2 +103,3 @@ import { createGunzip, createInflate } from "zlib"; | ||
const DEFAULT_PORT = 8969; | ||
const SERVER_IDENTIFIER = "spotlight-by-sentry"; | ||
function getCorsHeader() { | ||
@@ -111,2 +112,7 @@ return { | ||
} | ||
function getSpotlightHeader() { | ||
return { | ||
"X-Powered-by": SERVER_IDENTIFIER | ||
}; | ||
} | ||
function handleStreamRequest(req, res, buffer2, incomingPayload) { | ||
@@ -119,2 +125,3 @@ if (req.headers.accept && req.headers.accept == "text/event-stream") { | ||
...getCorsHeader(), | ||
...getSpotlightHeader(), | ||
Connection: "keep-alive" | ||
@@ -147,3 +154,4 @@ }); | ||
"Cache-Control": "no-cache", | ||
...getCorsHeader() | ||
...getCorsHeader(), | ||
...getSpotlightHeader() | ||
}); | ||
@@ -183,2 +191,3 @@ res.end(); | ||
...getCorsHeader(), | ||
...getSpotlightHeader(), | ||
Connection: "keep-alive" | ||
@@ -224,10 +233,19 @@ }); | ||
const server = createServer((req, res) => { | ||
const handled = handleStreamRequest(req, res, buffer2, incomingPayload); | ||
if (!handled && basePath) { | ||
serveFile(req, res, basePath); | ||
if (req.url === "/health") { | ||
res.writeHead(200, { | ||
"Content-Type": "text/plain", | ||
...getCorsHeader(), | ||
...getSpotlightHeader() | ||
}); | ||
res.end("OK"); | ||
} else { | ||
const handled = handleStreamRequest(req, res, buffer2, incomingPayload); | ||
if (!handled && basePath) { | ||
serveFile(req, res, basePath); | ||
} | ||
if (!handled && !basePath) { | ||
res.writeHead(404); | ||
res.end(); | ||
} | ||
} | ||
if (!handled && !basePath) { | ||
res.writeHead(404); | ||
res.end(); | ||
} | ||
}); | ||
@@ -261,2 +279,24 @@ server.on("error", (e) => { | ||
}; | ||
function isSidecarRunning(port) { | ||
return new Promise((resolve) => { | ||
const options = { | ||
hostname: "localhost", | ||
port, | ||
path: "/health", | ||
method: "GET", | ||
timeout: 5e3 | ||
}; | ||
const healthReq = get(options, (res) => { | ||
const serverIdentifier = res.headers["x-powered-by"]; | ||
if (serverIdentifier === "spotlight-by-sentry") { | ||
resolve(true); | ||
} else { | ||
resolve(false); | ||
} | ||
}); | ||
healthReq.on("error", () => { | ||
resolve(false); | ||
}); | ||
}); | ||
} | ||
function setupSidecar({ | ||
@@ -282,5 +322,11 @@ port, | ||
} | ||
if (!serverInstance) { | ||
serverInstance = startServer(buffer, sidecarPort, basePath, incomingPayload); | ||
} | ||
isSidecarRunning(sidecarPort).then((isRunning) => { | ||
if (isRunning) { | ||
logger.info(`Sidecar is already running on port ${sidecarPort}`); | ||
} else { | ||
if (!serverInstance) { | ||
serverInstance = startServer(buffer, sidecarPort, basePath, incomingPayload); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -287,0 +333,0 @@ function clearBuffer() { |
{ | ||
"name": "@spotlightjs/sidecar", | ||
"description": "A small proxy server to capture and forward data from backend services to Spotlight.", | ||
"version": "1.3.4", | ||
"version": "1.3.5", | ||
"license": "Apache-2.0", | ||
@@ -6,0 +6,0 @@ "type": "module", |
import { createWriteStream, readFile } from 'fs'; | ||
import { IncomingMessage, Server, ServerResponse, createServer } from 'http'; | ||
import { IncomingMessage, Server, ServerResponse, createServer, get } from 'http'; | ||
import { extname, join } from 'path'; | ||
@@ -9,2 +9,3 @@ import { createGunzip, createInflate } from 'zlib'; | ||
const DEFAULT_PORT = 8969; | ||
const SERVER_IDENTIFIER = 'spotlight-by-sentry'; | ||
@@ -55,2 +56,7 @@ type Payload = [string, string]; | ||
function getSpotlightHeader() { | ||
return { | ||
'X-Powered-by': SERVER_IDENTIFIER, | ||
}; | ||
} | ||
/** | ||
@@ -71,2 +77,3 @@ * Returns true of the request was handled, false otherwise. | ||
...getCorsHeader(), | ||
...getSpotlightHeader(), | ||
Connection: 'keep-alive', | ||
@@ -101,2 +108,3 @@ }); | ||
...getCorsHeader(), | ||
...getSpotlightHeader(), | ||
}); | ||
@@ -147,2 +155,3 @@ res.end(); | ||
...getCorsHeader(), | ||
...getSpotlightHeader(), | ||
Connection: 'keep-alive', | ||
@@ -196,10 +205,19 @@ }); | ||
const server = createServer((req, res) => { | ||
const handled = handleStreamRequest(req, res, buffer, incomingPayload); | ||
if (!handled && basePath) { | ||
serveFile(req, res, basePath); | ||
if (req.url === '/health') { | ||
res.writeHead(200, { | ||
'Content-Type': 'text/plain', | ||
...getCorsHeader(), | ||
...getSpotlightHeader(), | ||
}); | ||
res.end('OK'); | ||
} else { | ||
const handled = handleStreamRequest(req, res, buffer, incomingPayload); | ||
if (!handled && basePath) { | ||
serveFile(req, res, basePath); | ||
} | ||
if (!handled && !basePath) { | ||
res.writeHead(404); | ||
res.end(); | ||
} | ||
} | ||
if (!handled && !basePath) { | ||
res.writeHead(404); | ||
res.end(); | ||
} | ||
}); | ||
@@ -239,2 +257,26 @@ | ||
function isSidecarRunning(port: string | number | undefined) { | ||
return new Promise(resolve => { | ||
const options = { | ||
hostname: 'localhost', | ||
port: port, | ||
path: '/health', | ||
method: 'GET', | ||
timeout: 5000, | ||
}; | ||
const healthReq = get(options, res => { | ||
const serverIdentifier = res.headers['x-powered-by']; | ||
if (serverIdentifier === 'spotlight-by-sentry') { | ||
resolve(true); | ||
} else { | ||
resolve(false); | ||
} | ||
}); | ||
healthReq.on('error', () => { | ||
resolve(false); | ||
}); | ||
}); | ||
} | ||
export function setupSidecar({ | ||
@@ -263,6 +305,11 @@ port, | ||
} | ||
if (!serverInstance) { | ||
serverInstance = startServer(buffer, sidecarPort, basePath, incomingPayload); | ||
} | ||
isSidecarRunning(sidecarPort).then(isRunning => { | ||
if (isRunning) { | ||
logger.info(`Sidecar is already running on port ${sidecarPort}`); | ||
} else { | ||
if (!serverInstance) { | ||
serverInstance = startServer(buffer, sidecarPort, basePath, incomingPayload); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -269,0 +316,0 @@ |
Sorry, the diff of this file is not supported yet
44582
797