@spotlightjs/sidecar
Advanced tools
Comparing version 1.8.0 to 1.9.0
@@ -1,3 +0,2 @@ | ||
/// <reference types="node" resolution-mode="require"/> | ||
import { IncomingMessage, ServerResponse } from 'node:http'; | ||
export declare function contextLinesHandler(req: IncomingMessage, res: ServerResponse): void; |
@@ -20,2 +20,3 @@ import { type SidecarLogger } from './logger.js'; | ||
basePath?: string; | ||
filesToServe?: Record<string, Buffer>; | ||
/** | ||
@@ -31,5 +32,5 @@ * More verbose logging. | ||
}; | ||
export declare function setupSidecar({ port, logger: customLogger, basePath, debug, incomingPayload, }?: SideCarOptions): void; | ||
export declare function setupSidecar({ port, logger: customLogger, basePath, filesToServe, debug, incomingPayload, }?: SideCarOptions): void; | ||
export declare function clearBuffer(): void; | ||
export declare function shutdown(): void; | ||
export {}; |
var __defProp = Object.defineProperty; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
import launchEditor from "launch-editor"; | ||
import { readFileSync, createWriteStream, readFile } from "node:fs"; | ||
import { readFileSync, createWriteStream } from "node:fs"; | ||
import { createServer, get } from "node:http"; | ||
import * as path from "node:path"; | ||
import { resolve, extname, join } from "node:path"; | ||
import { join, resolve, extname } from "node:path"; | ||
import { createGunzip, createInflate } from "node:zlib"; | ||
@@ -182,6 +179,4 @@ import * as os from "node:os"; | ||
atItem = this.items[this.head % this.size]; | ||
if (atItem === void 0) | ||
break; | ||
if (atItem[0] > minTime) | ||
break; | ||
if (atItem === void 0) break; | ||
if (atItem[0] > minTime) break; | ||
this.head += 1; | ||
@@ -201,4 +196,3 @@ } | ||
const cb = this.readers.get(readerId); | ||
if (!cb) | ||
return; | ||
if (!cb) return; | ||
let atReadPos = readPos; | ||
@@ -266,3 +260,3 @@ let item; | ||
return function handleStreamRequest(req, res, pathname, searchParams) { | ||
if (req.method === "GET" && req.headers.accept && req.headers.accept == "text/event-stream" && pathname === "/stream") { | ||
if (req.method === "GET" && req.headers.accept && req.headers.accept === "text/event-stream" && pathname === "/stream") { | ||
res.writeHead(200, { | ||
@@ -276,9 +270,9 @@ "Content-Type": "text/event-stream", | ||
const sub = buffer2.subscribe(([payloadType, data]) => { | ||
logger.debug(`🕊️ sending to Spotlight`); | ||
logger.debug("🕊️ sending to Spotlight"); | ||
res.write(`event:${payloadType} | ||
`); | ||
data.split("\n").forEach((line) => { | ||
for (const line of data.split("\n")) { | ||
res.write(`data:${line} | ||
`); | ||
}); | ||
} | ||
res.write("\n"); | ||
@@ -339,8 +333,9 @@ }); | ||
} | ||
function fileServer(basePath) { | ||
function fileServer(filesToServe) { | ||
return function serveFile(req, res, pathname) { | ||
let filePath = `.${pathname || req.url}`; | ||
if (filePath === "./") { | ||
filePath = "./src/index.html"; | ||
let filePath = `${pathname || req.url}`; | ||
if (filePath === "/") { | ||
filePath = "/src/index.html"; | ||
} | ||
filePath = filePath.slice(1); | ||
const extName = extname(filePath); | ||
@@ -359,9 +354,8 @@ let contentType = "text/html"; | ||
} | ||
readFile(join(basePath, filePath), (error, content) => { | ||
if (error) { | ||
return error404(req, res); | ||
} | ||
if (!Object.prototype.hasOwnProperty.call(filesToServe, filePath)) { | ||
error404(req, res); | ||
} else { | ||
res.writeHead(200, { "Content-Type": contentType }); | ||
res.end(content, "utf-8"); | ||
}); | ||
res.end(filesToServe[filePath]); | ||
} | ||
}; | ||
@@ -401,2 +395,3 @@ } | ||
const targetPath = resolve(basePath, requestBody); | ||
logger.debug(`Launching editor for ${targetPath}`); | ||
launchEditor( | ||
@@ -424,3 +419,9 @@ // filename:line:column | ||
const error405 = errorResponse(405); | ||
function startServer(buffer2, port, basePath, incomingPayload) { | ||
function startServer(buffer2, port, basePath, filesToServe, incomingPayload) { | ||
if (basePath && !filesToServe) { | ||
filesToServe = { | ||
"/src/index.html": readFileSync(join(basePath, "src/index.html")), | ||
"/assets/main.js": readFileSync(join(basePath, "assets/main.js")) | ||
}; | ||
} | ||
const ROUTES = [ | ||
@@ -432,3 +433,3 @@ [/^\/health$/, handleHealthRequest], | ||
[RegExp(`^${CONTEXT_LINES_ENDPOINT}$`), enableCORS(contextLinesHandler)], | ||
[/^.+$/, basePath != null ? enableCORS(fileServer(basePath)) : error404] | ||
[/^.+$/, filesToServe != null ? enableCORS(fileServer(filesToServe)) : error404] | ||
]; | ||
@@ -485,3 +486,4 @@ const server = createServer((req, res) => { | ||
method: "GET", | ||
timeout: 5e3 | ||
timeout: 2e3, | ||
headers: { Connection: "close" } | ||
}; | ||
@@ -499,2 +501,3 @@ const healthReq = get(options, (res) => { | ||
}); | ||
healthReq.end(); | ||
}); | ||
@@ -506,2 +509,3 @@ } | ||
basePath, | ||
filesToServe, | ||
debug, | ||
@@ -528,3 +532,3 @@ incomingPayload | ||
if (!serverInstance) { | ||
serverInstance = startServer(buffer, sidecarPort, basePath, incomingPayload); | ||
serverInstance = startServer(buffer, sidecarPort, basePath, filesToServe, incomingPayload); | ||
} | ||
@@ -539,9 +543,8 @@ } | ||
if (serverInstance) { | ||
logger.info("Shutting down Server"); | ||
logger.info("Shutting down server..."); | ||
serverInstance.close(); | ||
} | ||
} | ||
process.on("SIGTERM", () => { | ||
shutdown(); | ||
}); | ||
process.on("SIGINT", shutdown); | ||
process.on("SIGTERM", shutdown); | ||
export { | ||
@@ -548,0 +551,0 @@ clearBuffer, |
{ | ||
"name": "@spotlightjs/sidecar", | ||
"description": "A small proxy server to capture and forward data from backend services to Spotlight.", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"license": "Apache-2.0", | ||
@@ -34,10 +34,10 @@ "type": "module", | ||
"dependencies": { | ||
"launch-editor": "^2.8.0", | ||
"source-map": "^0.7.4", | ||
"kleur": "^4.1.5" | ||
"kleur": "^4.1.5", | ||
"launch-editor": "^2.9.1", | ||
"source-map": "^0.7.4" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18", | ||
"typescript": "^5.0.2", | ||
"vite": "^4.5.3", | ||
"@types/node": "^18.19.55", | ||
"typescript": "^5.6.2", | ||
"vite": "^5.4.11", | ||
"@spotlightjs/tsconfig": "1.0.0" | ||
@@ -44,0 +44,0 @@ }, |
import launchEditor from 'launch-editor'; | ||
import { createWriteStream, readFile } from 'node:fs'; | ||
import { createWriteStream, readFileSync } from 'node:fs'; | ||
import { createServer, get, type IncomingMessage, type Server, type ServerResponse } from 'node:http'; | ||
@@ -35,2 +35,4 @@ import { extname, join, resolve } from 'node:path'; | ||
filesToServe?: Record<string, Buffer>; | ||
/** | ||
@@ -105,3 +107,3 @@ * More verbose logging. | ||
req.headers.accept && | ||
req.headers.accept == 'text/event-stream' && | ||
req.headers.accept === 'text/event-stream' && | ||
pathname === '/stream' | ||
@@ -120,8 +122,8 @@ ) { | ||
const sub = buffer.subscribe(([payloadType, data]) => { | ||
logger.debug(`🕊️ sending to Spotlight`); | ||
logger.debug('🕊️ sending to Spotlight'); | ||
res.write(`event:${payloadType}\n`); | ||
// This is very important - SSE events are delimited by two newlines | ||
data.split('\n').forEach(line => { | ||
for (const line of data.split('\n')) { | ||
res.write(`data:${line}\n`); | ||
}); | ||
} | ||
res.write('\n'); | ||
@@ -195,8 +197,9 @@ }); | ||
function fileServer(basePath: string) { | ||
function fileServer(filesToServe: Record<string, Buffer>) { | ||
return function serveFile(req: IncomingMessage, res: ServerResponse, pathname?: string): void { | ||
let filePath = `.${pathname || req.url}`; | ||
if (filePath === './') { | ||
filePath = './src/index.html'; | ||
let filePath = `${pathname || req.url}`; | ||
if (filePath === '/') { | ||
filePath = '/src/index.html'; | ||
} | ||
filePath = filePath.slice(1); | ||
@@ -217,10 +220,8 @@ const extName = extname(filePath); | ||
readFile(join(basePath, filePath), (error, content) => { | ||
if (error) { | ||
return error404(req, res); | ||
} | ||
if (!Object.prototype.hasOwnProperty.call(filesToServe, filePath)) { | ||
error404(req, res); | ||
} else { | ||
res.writeHead(200, { 'Content-Type': contentType }); | ||
res.end(content, 'utf-8'); | ||
}); | ||
res.end(filesToServe[filePath]); | ||
} | ||
}; | ||
@@ -266,2 +267,3 @@ } | ||
const targetPath = resolve(basePath, requestBody); | ||
logger.debug(`Launching editor for ${targetPath}`); | ||
launchEditor( | ||
@@ -296,4 +298,11 @@ // filename:line:column | ||
basePath?: string, | ||
filesToServe?: Record<string, Buffer>, | ||
incomingPayload?: IncomingPayloadCallback, | ||
): Server { | ||
if (basePath && !filesToServe) { | ||
filesToServe = { | ||
'/src/index.html': readFileSync(join(basePath, 'src/index.html')), | ||
'/assets/main.js': readFileSync(join(basePath, 'assets/main.js')), | ||
}; | ||
} | ||
const ROUTES: [RegExp, RequestHandler][] = [ | ||
@@ -305,3 +314,3 @@ [/^\/health$/, handleHealthRequest], | ||
[RegExp(`^${CONTEXT_LINES_ENDPOINT}$`), enableCORS(contextLinesHandler)], | ||
[/^.+$/, basePath != null ? enableCORS(fileServer(basePath)) : error404], | ||
[/^.+$/, filesToServe != null ? enableCORS(fileServer(filesToServe)) : error404], | ||
]; | ||
@@ -368,3 +377,4 @@ | ||
method: 'GET', | ||
timeout: 5000, | ||
timeout: 2000, | ||
headers: { Connection: 'close' }, | ||
}; | ||
@@ -383,2 +393,3 @@ | ||
}); | ||
healthReq.end(); | ||
}); | ||
@@ -391,2 +402,3 @@ } | ||
basePath, | ||
filesToServe, | ||
debug, | ||
@@ -416,3 +428,3 @@ incomingPayload, | ||
if (!serverInstance) { | ||
serverInstance = startServer(buffer, sidecarPort, basePath, incomingPayload); | ||
serverInstance = startServer(buffer, sidecarPort, basePath, filesToServe, incomingPayload); | ||
} | ||
@@ -429,3 +441,3 @@ } | ||
if (serverInstance) { | ||
logger.info('Shutting down Server'); | ||
logger.info('Shutting down server...'); | ||
serverInstance.close(); | ||
@@ -435,4 +447,3 @@ } | ||
process.on('SIGTERM', () => { | ||
shutdown(); | ||
}); | ||
process.on('SIGINT', shutdown); | ||
process.on('SIGTERM', shutdown); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76907
1265
Updatedlaunch-editor@^2.9.1