Skip to content

Commit

Permalink
logAccessInTerminal : with-mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel.benychou committed Jul 15, 2024
1 parent f82771e commit 3d86c63
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ All boolean settings default to false when unspecified.
- `dontTranslateLocationHeader`: (`boolean`) when getting a response location header, in case `replaceResponseBodyUrls` does not change the URL, change the origin to the proxy anyway
- `dontUseHttp2Downstream`: (`boolean`) force calling downstream services in http1.1 only (to save some time)
- `simpleLogs`: (`boolean`) disable colored logs for text terminals
- `logAccessInTerminal`: (`boolean`) write an access log in the terminal on each call (defaults to false)
- `logAccessInTerminal`: (`boolean` | 'with-mapping') write an access log in the terminal on each call (>= 0.1.2 : 'with-mapping' will log the key used to find the target)
- `websocket`: (`boolean`) true to activate websocket connections proxying via sockets. Required for logs UI.
- `disableWebSecurity`: (`boolean`) true for easygoing values in cross origin requests or content security policy headers
- `connectTimeout`: (`number`) max time before aborting the connection (defaults to 3000ms)
Expand Down
42 changes: 28 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ interface LocalConfiguration {
replaceResponseBodyUrls?: boolean;
dontUseHttp2Downstream?: boolean;
dontTranslateLocationHeader?: boolean;
logAccessInTerminal?: boolean;
logAccessInTerminal?: boolean | "with-mapping";
simpleLogs?: boolean;
websocket?: boolean;
disableWebSecurity?: boolean;
Expand Down Expand Up @@ -248,9 +248,10 @@ const log = async function (
);
else {
for (let element of logs) {
const logTexts = element.map(e => `\u001b[48;5;${e.color}m${e.text}`);
const renderedColors = element.filter(e => e?.text?.length);
const logTexts = renderedColors.map(e => `\u001b[48;5;${e.color}m${e.text}`);
stdout.write(
`${getCurrentTime(state?.config?.simpleLogs)}${element
`${getCurrentTime(state?.config?.simpleLogs)}${renderedColors
.map(e => `\u001b[48;5;${e.color}m${"".padEnd((e.length ?? 64) + 1)}`)
.join("▐")}\u001b[0m\n`,
);
Expand Down Expand Up @@ -840,17 +841,19 @@ const configPage = (
${Object.entries({ ...defaultConfig, ssl: { cert: "", key: "" } })
.map(
([property, exampleValue]) =>
`${property}: {type: ${
`${property}:${
property === "unwantedHeaderNamesInMocks"
? '"array","items": {"type":"string"}'
? '{type:"array","items":{"type":"string"}}'
: property === "logAccessInTerminal"
? '{"oneOf":[{type:"boolean"},{enum:["with-mapping"]}]}'
: typeof exampleValue === "number"
? '"integer"'
? '{type:"integer"}'
: typeof exampleValue === "string"
? '"string"'
? '{type:"string"}'
: typeof exampleValue === "boolean"
? '"boolean"'
: '"object"'
}}`,
? '{type:"boolean"}'
: '{type:"object"}'
}`,
)
.join(",\n ")}
},
Expand Down Expand Up @@ -2006,7 +2009,11 @@ const onWatch = async function (state: State): Promise<Partial<State>> {
}
if (config.logAccessInTerminal !== previousConfig.logAccessInTerminal) {
logElements.push({
text: `${EMOJIS.LOGS} access terminal logging ${!config.logAccessInTerminal ? "off" : "on"}`,
text: `${EMOJIS.LOGS} access terminal logging ${
config.logAccessInTerminal === true ? "on" :
config.logAccessInTerminal === "with-mapping" ? ": show both path and mapping" :
"off"
}`,
color: LogLevel.INFO,
});
}
Expand Down Expand Up @@ -2895,6 +2902,8 @@ const serve = async function (
state.config.logAccessInTerminal &&
!targetUrl.pathname.startsWith("/:/")
) {
const keyToDisplay = state.config.logAccessInTerminal === "with-mapping" ? (key ?? "") : "";
const keyLength = (keyToDisplay.length ? keyToDisplay.length + 2 : 0);
await state.log([
[
{
Expand All @@ -2921,13 +2930,18 @@ const serve = async function (
text: (inboundRequest.method ?? "GET").toString(),
length: inboundRequest.method?.length,
},
{
color: 32,
text: keyToDisplay,
length: keyToDisplay.length,
},
{
color: 8,
text: targetUrl.pathname
.toString()
.padStart(62 - (inboundRequest.method?.length ?? 3))
.substring(0, 62 - (inboundRequest.method?.length ?? 3)),
length: 62 - (inboundRequest.method?.length ?? 3),
.padStart(62 - (inboundRequest.method?.length ?? 3) - keyLength)
.substring(0, 62 - (inboundRequest.method?.length ?? 3) - keyLength),
length: 62 - (inboundRequest.method?.length ?? 3) - keyLength,
},
],
]);
Expand Down
2 changes: 1 addition & 1 deletion zipify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import('node:fs/promises')
.then(({ readFile }) => readFile(process.argv[2]))
.then(async code => await import('node:zlib').then(({ gzip }) =>
new Promise(resolve => gzip(Buffer.from(code),
(_, data) => resolve(`#!/usr/bin/env node\neval(require('zlib').gunzipSync(Buffer.from("${data.toString("base64")}","base64")).toString("utf8"))`)))))
(_, data) => resolve(`#!/usr/bin/env node\neval(require('zlib').gunzipSync(Buffer.from('${data.toString('base64')}','base64')).toString('utf8'))`)))))
.then(console.log)

0 comments on commit 3d86c63

Please sign in to comment.