@spotlightjs/sidecar
Advanced tools
Comparing version 1.3.2 to 1.3.3
import { SidecarLogger } from './logger.js'; | ||
type IncomingPayloadCallback = (body: string) => void; | ||
type SideCarOptions = { | ||
@@ -23,6 +24,11 @@ /** | ||
debug?: boolean; | ||
/** | ||
* A callback that will be called with the incoming message. | ||
* Helpful for debugging. | ||
*/ | ||
incomingPayload?: IncomingPayloadCallback; | ||
}; | ||
export declare function setupSidecar({ port, logger: customLogger, basePath, debug }?: SideCarOptions): void; | ||
export declare function setupSidecar({ port, logger: customLogger, basePath, debug, incomingPayload, }?: SideCarOptions): void; | ||
export declare function clearBuffer(): void; | ||
export declare function shutdown(): void; | ||
export {}; |
@@ -109,3 +109,3 @@ var __defProp = Object.defineProperty; | ||
} | ||
function handleStreamRequest(req, res, buffer2) { | ||
function handleStreamRequest(req, res, buffer2, incomingPayload) { | ||
if (req.headers.accept && req.headers.accept == "text/event-stream") { | ||
@@ -165,8 +165,12 @@ if (req.url == "/stream") { | ||
buffer2.put([`${req.headers["content-type"]}`, body]); | ||
if (process.env.SPOTLIGHT_CAPTURE) { | ||
if (process.env.SPOTLIGHT_CAPTURE || incomingPayload) { | ||
const timestamp = (/* @__PURE__ */ new Date()).getTime(); | ||
const contentType = `${req.headers["content-type"]}`; | ||
const filename = `${contentType.replace(/[^a-z0-9]/gi, "_").toLowerCase()}-${timestamp}.txt`; | ||
createWriteStream(filename).write(body); | ||
logger.info(`🗃️ Saved data to ${filename}`); | ||
if (incomingPayload) { | ||
incomingPayload(body); | ||
} else { | ||
createWriteStream(filename).write(body); | ||
logger.info(`🗃️ Saved data to ${filename}`); | ||
} | ||
} | ||
@@ -214,5 +218,5 @@ res.writeHead(200, { | ||
} | ||
function startServer(buffer2, port, basePath) { | ||
function startServer(buffer2, port, basePath, incomingPayload) { | ||
const server = createServer((req, res) => { | ||
const handled = handleStreamRequest(req, res, buffer2); | ||
const handled = handleStreamRequest(req, res, buffer2, incomingPayload); | ||
if (!handled && basePath) { | ||
@@ -253,3 +257,9 @@ serveFile(req, res, basePath); | ||
}; | ||
function setupSidecar({ port, logger: customLogger, basePath, debug } = {}) { | ||
function setupSidecar({ | ||
port, | ||
logger: customLogger, | ||
basePath, | ||
debug, | ||
incomingPayload | ||
} = {}) { | ||
let sidecarPort = DEFAULT_PORT; | ||
@@ -269,3 +279,3 @@ if (customLogger) { | ||
if (!serverInstance) { | ||
serverInstance = startServer(buffer, sidecarPort, basePath); | ||
serverInstance = startServer(buffer, sidecarPort, basePath, incomingPayload); | ||
} | ||
@@ -290,1 +300,2 @@ } | ||
}; | ||
//# sourceMappingURL=main.js.map |
{ | ||
"name": "@spotlightjs/sidecar", | ||
"description": "A small proxy server to capture and forward data from backend services to Spotlight.", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"license": "Apache-2.0", | ||
@@ -6,0 +6,0 @@ "type": "module", |
@@ -12,2 +12,4 @@ import { createWriteStream, readFile } from 'fs'; | ||
type IncomingPayloadCallback = (body: string) => void; | ||
type SideCarOptions = { | ||
@@ -37,2 +39,8 @@ /** | ||
debug?: boolean; | ||
/** | ||
* A callback that will be called with the incoming message. | ||
* Helpful for debugging. | ||
*/ | ||
incomingPayload?: IncomingPayloadCallback; | ||
}; | ||
@@ -51,3 +59,8 @@ | ||
*/ | ||
function handleStreamRequest(req: IncomingMessage, res: ServerResponse, buffer: MessageBuffer<Payload>): boolean { | ||
function handleStreamRequest( | ||
req: IncomingMessage, | ||
res: ServerResponse, | ||
buffer: MessageBuffer<Payload>, | ||
incomingPayload?: IncomingPayloadCallback, | ||
): boolean { | ||
if (req.headers.accept && req.headers.accept == 'text/event-stream') { | ||
@@ -116,3 +129,3 @@ if (req.url == '/stream') { | ||
if (process.env.SPOTLIGHT_CAPTURE) { | ||
if (process.env.SPOTLIGHT_CAPTURE || incomingPayload) { | ||
const timestamp = new Date().getTime(); | ||
@@ -122,4 +135,8 @@ const contentType = `${req.headers['content-type']}`; | ||
createWriteStream(filename).write(body); | ||
logger.info(`🗃️ Saved data to ${filename}`); | ||
if (incomingPayload) { | ||
incomingPayload(body); | ||
} else { | ||
createWriteStream(filename).write(body); | ||
logger.info(`🗃️ Saved data to ${filename}`); | ||
} | ||
} | ||
@@ -172,5 +189,10 @@ | ||
function startServer(buffer: MessageBuffer<Payload>, port: number, basePath?: string): Server { | ||
function startServer( | ||
buffer: MessageBuffer<Payload>, | ||
port: number, | ||
basePath?: string, | ||
incomingPayload?: IncomingPayloadCallback, | ||
): Server { | ||
const server = createServer((req, res) => { | ||
const handled = handleStreamRequest(req, res, buffer); | ||
const handled = handleStreamRequest(req, res, buffer, incomingPayload); | ||
if (!handled && basePath) { | ||
@@ -217,3 +239,9 @@ serveFile(req, res, basePath); | ||
export function setupSidecar({ port, logger: customLogger, basePath, debug }: SideCarOptions = {}): void { | ||
export function setupSidecar({ | ||
port, | ||
logger: customLogger, | ||
basePath, | ||
debug, | ||
incomingPayload, | ||
}: SideCarOptions = {}): void { | ||
let sidecarPort = DEFAULT_PORT; | ||
@@ -237,3 +265,3 @@ | ||
if (!serverInstance) { | ||
serverInstance = startServer(buffer, sidecarPort, basePath); | ||
serverInstance = startServer(buffer, sidecarPort, basePath, incomingPayload); | ||
} | ||
@@ -240,0 +268,0 @@ } |
40121
13
705