@spotlightjs/sidecar
Advanced tools
Comparing version 1.3.5 to 1.4.0
@@ -107,3 +107,4 @@ var __defProp = Object.defineProperty; | ||
"Access-Control-Allow-Credentials": "true", | ||
"Access-Control-Allow-Headers": "*" | ||
"Access-Control-Allow-Headers": "*", | ||
"Access-Control-Allow-Methods": "GET,POST,PUT,OPTIONS,DELETE,PATCH" | ||
}; | ||
@@ -227,4 +228,34 @@ } | ||
function startServer(buffer2, port, basePath, incomingPayload) { | ||
const server = createServer((req, res) => { | ||
const server = createServer(handleRequest); | ||
server.on("error", handleServerError); | ||
server.listen(port, () => { | ||
handleServerListen(port, basePath); | ||
}); | ||
return server; | ||
function handleRequest(req, res) { | ||
if (req.url === "/health") { | ||
handleHealthRequest(res); | ||
} else if (req.url === "/clear") { | ||
handleClearRequest(req, res); | ||
} else { | ||
handleOtherRequest(req, res); | ||
} | ||
} | ||
function handleHealthRequest(res) { | ||
res.writeHead(200, { | ||
"Content-Type": "text/plain", | ||
...getCorsHeader(), | ||
...getSpotlightHeader() | ||
}); | ||
res.end("OK"); | ||
} | ||
function handleClearRequest(req, res) { | ||
if (req.method === "OPTIONS") { | ||
res.writeHead(204, { | ||
"Cache-Control": "no-cache", | ||
...getCorsHeader(), | ||
...getSpotlightHeader() | ||
}); | ||
res.end(); | ||
} else if (req.method === "DELETE") { | ||
res.writeHead(200, { | ||
@@ -235,15 +266,17 @@ "Content-Type": "text/plain", | ||
}); | ||
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(); | ||
} | ||
clearBuffer(); | ||
res.end("Cleared"); | ||
} | ||
}); | ||
server.on("error", (e) => { | ||
} | ||
function handleOtherRequest(req, res) { | ||
const handled = handleStreamRequest(req, res, buffer2, incomingPayload); | ||
if (!handled && basePath) { | ||
serveFile(req, res, basePath); | ||
} | ||
if (!handled && !basePath) { | ||
res.writeHead(404); | ||
res.end(); | ||
} | ||
} | ||
function handleServerError(e) { | ||
if ("code" in e && e.code === "EADDRINUSE") { | ||
@@ -257,10 +290,9 @@ logger.info(`Port ${port} in use, retrying...`); | ||
} | ||
}); | ||
server.listen(port, () => { | ||
logger.info(`Sidecar listening on ${port}`); | ||
if (basePath) { | ||
logger.info(`You can open: http://localhost:${port} to see the Spotlight overlay directly`); | ||
} | ||
function handleServerListen(port2, basePath2) { | ||
logger.info(`Sidecar listening on ${port2}`); | ||
if (basePath2) { | ||
logger.info(`You can open: http://localhost:${port2} to see the Spotlight overlay directly`); | ||
} | ||
}); | ||
return server; | ||
} | ||
} | ||
@@ -267,0 +299,0 @@ let serverInstance; |
{ | ||
"name": "@spotlightjs/sidecar", | ||
"description": "A small proxy server to capture and forward data from backend services to Spotlight.", | ||
"version": "1.3.5", | ||
"version": "1.4.0", | ||
"license": "Apache-2.0", | ||
@@ -34,3 +34,3 @@ "type": "module", | ||
"typescript": "^5.0.2", | ||
"vite": "^4.5.1", | ||
"vite": "^4.5.2", | ||
"@spotlightjs/tsconfig": "1.0.0" | ||
@@ -37,0 +37,0 @@ }, |
@@ -52,2 +52,3 @@ import { createWriteStream, readFile } from 'fs'; | ||
'Access-Control-Allow-Headers': '*', | ||
'Access-Control-Allow-Methods': 'GET,POST,PUT,OPTIONS,DELETE,PATCH', | ||
}; | ||
@@ -200,4 +201,40 @@ } | ||
): Server { | ||
const server = createServer((req, res) => { | ||
const server = createServer(handleRequest); | ||
server.on('error', handleServerError); | ||
server.listen(port, () => { | ||
handleServerListen(port, basePath); | ||
}); | ||
return server; | ||
function handleRequest(req: IncomingMessage, res: ServerResponse): void { | ||
if (req.url === '/health') { | ||
handleHealthRequest(res); | ||
} else if (req.url === '/clear') { | ||
handleClearRequest(req, res); | ||
} else { | ||
handleOtherRequest(req, res); | ||
} | ||
} | ||
function handleHealthRequest(res: ServerResponse): void { | ||
res.writeHead(200, { | ||
'Content-Type': 'text/plain', | ||
...getCorsHeader(), | ||
...getSpotlightHeader(), | ||
}); | ||
res.end('OK'); | ||
} | ||
function handleClearRequest(req: IncomingMessage, res: ServerResponse): void { | ||
if (req.method === 'OPTIONS') { | ||
res.writeHead(204, { | ||
'Cache-Control': 'no-cache', | ||
...getCorsHeader(), | ||
...getSpotlightHeader(), | ||
}); | ||
res.end(); | ||
} else if (req.method === 'DELETE') { | ||
res.writeHead(200, { | ||
@@ -208,16 +245,21 @@ 'Content-Type': 'text/plain', | ||
}); | ||
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(); | ||
} | ||
clearBuffer(); | ||
res.end('Cleared'); | ||
} | ||
}); | ||
} | ||
server.on('error', e => { | ||
function handleOtherRequest(req: IncomingMessage, res: ServerResponse): void { | ||
const handled = handleStreamRequest(req, res, buffer, incomingPayload); | ||
if (!handled && basePath) { | ||
serveFile(req, res, basePath); | ||
} | ||
if (!handled && !basePath) { | ||
res.writeHead(404); | ||
res.end(); | ||
} | ||
} | ||
function handleServerError(e: { code?: string }): void { | ||
if ('code' in e && e.code === 'EADDRINUSE') { | ||
@@ -231,5 +273,5 @@ logger.info(`Port ${port} in use, retrying...`); | ||
} | ||
}); | ||
} | ||
server.listen(port, () => { | ||
function handleServerListen(port: number, basePath?: string): void { | ||
logger.info(`Sidecar listening on ${port}`); | ||
@@ -239,5 +281,3 @@ if (basePath) { | ||
} | ||
}); | ||
return server; | ||
} | ||
} | ||
@@ -244,0 +284,0 @@ |
Sorry, the diff of this file is not supported yet
48396
861