From 28e513c495a5a8214c6fefb8a0bcdcd225221d85 Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 02:02:49 +0000 Subject: [PATCH 01/12] feat(utils/color): support cloudflare workers --- src/middleware/logger/index.ts | 12 +++++++----- src/utils/color.ts | 16 +++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/middleware/logger/index.ts b/src/middleware/logger/index.ts index abcfbec7d..9db5749dc 100644 --- a/src/middleware/logger/index.ts +++ b/src/middleware/logger/index.ts @@ -6,6 +6,7 @@ import type { MiddlewareHandler } from '../../types' import { getColorEnabled } from '../../utils/color' import { getPath } from '../../utils/url' +import type { Context } from '../../context' enum LogPrefix { Outgoing = '-->', @@ -26,8 +27,8 @@ const time = (start: number) => { return humanize([delta < 1000 ? delta + 'ms' : Math.round(delta / 1000) + 's']) } -const colorStatus = (status: number) => { - const colorEnabled = getColorEnabled() +const colorStatus = (status: number, c: Context) => { + const colorEnabled = getColorEnabled(c) if (colorEnabled) { switch ((status / 100) | 0) { case 5: // red = error @@ -53,13 +54,14 @@ function log( prefix: string, method: string, path: string, + c: Context, status: number = 0, elapsed?: string ) { const out = prefix === LogPrefix.Incoming ? `${prefix} ${method} ${path}` - : `${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}` + : `${prefix} ${method} ${path} ${colorStatus(status, c)} ${elapsed}` fn(out) } @@ -85,12 +87,12 @@ export const logger = (fn: PrintFunc = console.log): MiddlewareHandler => { const path = getPath(c.req.raw) - log(fn, LogPrefix.Incoming, method, path) + log(fn, LogPrefix.Incoming, method, path, c) const start = Date.now() await next() - log(fn, LogPrefix.Outgoing, method, path, c.res.status, time(start)) + log(fn, LogPrefix.Outgoing, method, path, c, c.res.status, time(start)) } } diff --git a/src/utils/color.ts b/src/utils/color.ts index 522d3460a..1baf0c2fa 100644 --- a/src/utils/color.ts +++ b/src/utils/color.ts @@ -2,25 +2,23 @@ * @module * Color utility. */ +import { getRuntimeKey, env } from '../helper/adapter' +import type { Context } from '../context' /** * Get whether color change on terminal is enabled or disabled. * If `NO_COLOR` environment variable is set, this function returns `false`. * @see {@link https://no-color.org/} * + * @param {Context} c - the context of request + * * @returns {boolean} */ -export function getColorEnabled(): boolean { +export function getColorEnabled(c: Context): boolean { // eslint-disable-next-line @typescript-eslint/no-explicit-any - const { process, Deno } = globalThis as any + const { Deno } = globalThis as any - const isNoColor = - typeof Deno?.noColor === 'boolean' - ? (Deno.noColor as boolean) - : process !== undefined - ? // eslint-disable-next-line no-unsafe-optional-chaining - 'NO_COLOR' in process?.env - : false + const isNoColor = getRuntimeKey() === 'deno' ? Deno?.noColor : env(c).NO_COLOR return !isNoColor } From a2b51749bb68e3a9ade01823400bd863570096d5 Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 02:09:09 +0000 Subject: [PATCH 02/12] add tests --- src/utils/color.test.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/utils/color.test.ts b/src/utils/color.test.ts index 30d7a6213..97139337b 100644 --- a/src/utils/color.test.ts +++ b/src/utils/color.test.ts @@ -1,12 +1,29 @@ import { getColorEnabled } from './color' +import { Context } from '../context' describe('getColorEnabled() - With colors enabled', () => { + const mockContext = new Context(new Request('Hono is cool')) + it('should return true', async () => { - expect(getColorEnabled()).toBe(true) + expect(getColorEnabled(mockContext)).toBe(true) + }) +}) + +describe('getColorEnabled() - With colors disabled in Edge', () => { + const edgeContext = new Context(new Request('Hono is cool'), { + env: { + NO_COLOR: false, + }, + }) + + it('should return false', async () => { + expect(getColorEnabled(edgeContext)).toBe(false) }) }) describe('getColorEnabled() - With NO_COLOR environment variable set', () => { + const mockContext = new Context(new Request('Hono is cool')) + beforeAll(() => { vi.stubEnv('NO_COLOR', '1') }) @@ -16,6 +33,6 @@ describe('getColorEnabled() - With NO_COLOR environment variable set', () => { }) it('should return false', async () => { - expect(getColorEnabled()).toBe(false) + expect(getColorEnabled(mockContext)).toBe(false) }) }) From d5f882c10bdcc07e1299246011a757ba28b7875d Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 02:11:16 +0000 Subject: [PATCH 03/12] lint --- src/middleware/logger/index.ts | 2 +- src/utils/color.test.ts | 2 +- src/utils/color.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/middleware/logger/index.ts b/src/middleware/logger/index.ts index 9db5749dc..5502a5396 100644 --- a/src/middleware/logger/index.ts +++ b/src/middleware/logger/index.ts @@ -3,10 +3,10 @@ * Logger Middleware for Hono. */ +import type { Context } from '../../context' import type { MiddlewareHandler } from '../../types' import { getColorEnabled } from '../../utils/color' import { getPath } from '../../utils/url' -import type { Context } from '../../context' enum LogPrefix { Outgoing = '-->', diff --git a/src/utils/color.test.ts b/src/utils/color.test.ts index 97139337b..cc669ed85 100644 --- a/src/utils/color.test.ts +++ b/src/utils/color.test.ts @@ -1,5 +1,5 @@ -import { getColorEnabled } from './color' import { Context } from '../context' +import { getColorEnabled } from './color' describe('getColorEnabled() - With colors enabled', () => { const mockContext = new Context(new Request('Hono is cool')) diff --git a/src/utils/color.ts b/src/utils/color.ts index 1baf0c2fa..fb1ac10c1 100644 --- a/src/utils/color.ts +++ b/src/utils/color.ts @@ -2,8 +2,8 @@ * @module * Color utility. */ -import { getRuntimeKey, env } from '../helper/adapter' import type { Context } from '../context' +import { getRuntimeKey, env } from '../helper/adapter' /** * Get whether color change on terminal is enabled or disabled. From 34c0f4313e2b216f4d54fcbf8adfbea4d1dd7bcf Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 02:16:46 +0000 Subject: [PATCH 04/12] fix --- src/utils/color.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils/color.ts b/src/utils/color.ts index fb1ac10c1..6e0e2e321 100644 --- a/src/utils/color.ts +++ b/src/utils/color.ts @@ -14,11 +14,12 @@ import { getRuntimeKey, env } from '../helper/adapter' * * @returns {boolean} */ -export function getColorEnabled(c: Context): boolean { +export function getColorEnabled(c?: Context): boolean { // eslint-disable-next-line @typescript-eslint/no-explicit-any - const { Deno } = globalThis as any + const { Deno, process } = globalThis as any - const isNoColor = getRuntimeKey() === 'deno' ? Deno?.noColor : env(c).NO_COLOR + const isNoColor = + getRuntimeKey() === 'deno' ? Deno?.noColor : c ? env(c).NO_COLOR : 'NO_COLOR' in process?.env return !isNoColor } From 1a9c928892f287b2be568746d79915632eff0534 Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 02:18:27 +0000 Subject: [PATCH 05/12] fix --- src/utils/color.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils/color.ts b/src/utils/color.ts index 6e0e2e321..e11f942f2 100644 --- a/src/utils/color.ts +++ b/src/utils/color.ts @@ -19,7 +19,11 @@ export function getColorEnabled(c?: Context): boolean { const { Deno, process } = globalThis as any const isNoColor = - getRuntimeKey() === 'deno' ? Deno?.noColor : c ? env(c).NO_COLOR : 'NO_COLOR' in process?.env + getRuntimeKey() === 'deno' + ? Deno?.noColor + : c + ? env(c).NO_COLOR + : process?.env && 'NO_COLOR' in process.env return !isNoColor } From 1a40e8697839880f17c059d3ea5b98eb3c20b4f7 Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 02:21:44 +0000 Subject: [PATCH 06/12] fix --- src/utils/color.test.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/utils/color.test.ts b/src/utils/color.test.ts index cc669ed85..d638db314 100644 --- a/src/utils/color.test.ts +++ b/src/utils/color.test.ts @@ -2,15 +2,13 @@ import { Context } from '../context' import { getColorEnabled } from './color' describe('getColorEnabled() - With colors enabled', () => { - const mockContext = new Context(new Request('Hono is cool')) - it('should return true', async () => { - expect(getColorEnabled(mockContext)).toBe(true) + expect(getColorEnabled()).toBe(true) }) }) describe('getColorEnabled() - With colors disabled in Edge', () => { - const edgeContext = new Context(new Request('Hono is cool'), { + const edgeContext = new Context(new Request('/'), { env: { NO_COLOR: false, }, @@ -22,7 +20,7 @@ describe('getColorEnabled() - With colors disabled in Edge', () => { }) describe('getColorEnabled() - With NO_COLOR environment variable set', () => { - const mockContext = new Context(new Request('Hono is cool')) + const mockContext = new Context(new Request('/')) beforeAll(() => { vi.stubEnv('NO_COLOR', '1') From bd394f275c195f12643c901b90caa58b46e50fef Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 11:55:45 +0900 Subject: [PATCH 07/12] Update color.test.ts --- src/utils/color.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/color.test.ts b/src/utils/color.test.ts index d638db314..032d3a119 100644 --- a/src/utils/color.test.ts +++ b/src/utils/color.test.ts @@ -8,7 +8,7 @@ describe('getColorEnabled() - With colors enabled', () => { }) describe('getColorEnabled() - With colors disabled in Edge', () => { - const edgeContext = new Context(new Request('/'), { + const edgeContext = new Context(new Request('http://localhost/'), { env: { NO_COLOR: false, }, @@ -20,7 +20,7 @@ describe('getColorEnabled() - With colors disabled in Edge', () => { }) describe('getColorEnabled() - With NO_COLOR environment variable set', () => { - const mockContext = new Context(new Request('/')) + const mockContext = new Context(new Request('http://localhost/')) beforeAll(() => { vi.stubEnv('NO_COLOR', '1') From a39552b0aae0a3c14d100e34075b14af88715bae Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 11:59:25 +0900 Subject: [PATCH 08/12] Update color.test.ts --- src/utils/color.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/color.test.ts b/src/utils/color.test.ts index 032d3a119..48c263406 100644 --- a/src/utils/color.test.ts +++ b/src/utils/color.test.ts @@ -10,7 +10,7 @@ describe('getColorEnabled() - With colors enabled', () => { describe('getColorEnabled() - With colors disabled in Edge', () => { const edgeContext = new Context(new Request('http://localhost/'), { env: { - NO_COLOR: false, + NO_COLOR: true, }, }) From ab4c5f5dfdbdce372160407f1d089abb1711cb90 Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 03:20:33 +0000 Subject: [PATCH 09/12] wip --- runtime-tests/workerd/index.test.ts | 25 +++++++++++++++++++++++++ runtime-tests/workerd/index.ts | 19 +++++++++++++++++-- src/utils/color.test.ts | 12 ------------ 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/runtime-tests/workerd/index.test.ts b/runtime-tests/workerd/index.test.ts index 2173ce6ae..935bf9eec 100644 --- a/runtime-tests/workerd/index.test.ts +++ b/runtime-tests/workerd/index.test.ts @@ -67,3 +67,28 @@ describe('workerd with WebSocket', () => { expect(closeHandler).toHaveBeenCalled() }) }) + +describe('worked with getColorEnabled()', () => { + let worker: UnstableDevWorker + + beforeAll(async () => { + worker = await unstable_dev('./runtime-tests/workerd/index.ts', { + vars: { + NAME: 'Hono', + }, + experimental: { disableExperimentalWarning: true }, + }) + }) + + afterAll(async () => { + await worker.stop() + }) + + it('Should return 200 response with the colorEnabled', async () => { + const res = await worker.fetch('/color') + expect(res.status).toBe(200) + expect(await res.json()).toEqual({ + colorEnabled: false, + }) + }) +}) diff --git a/runtime-tests/workerd/index.ts b/runtime-tests/workerd/index.ts index 3c94cec85..aec480c0f 100644 --- a/runtime-tests/workerd/index.ts +++ b/runtime-tests/workerd/index.ts @@ -1,13 +1,21 @@ import { upgradeWebSocket } from '../../src/adapter/cloudflare-workers' import { env, getRuntimeKey } from '../../src/helper/adapter' import { Hono } from '../../src/hono' +import { getColorEnabled } from '../../src/utils/color' -const app = new Hono() +interface Env { + NO_COLOR: boolean + NAME: string +} + +const app = new Hono<{ + Bindings: Env +}>() app.get('/', (c) => c.text(`Hello from ${getRuntimeKey()}`)) app.get('/env', (c) => { - const { NAME } = env<{ NAME: string }>(c) + const { NAME } = env(c) return c.text(NAME) }) @@ -22,4 +30,11 @@ app.get( }) ) +app.get('/color', (c) => { + c.env.NO_COLOR = true + return c.json({ + colorEnabled: getColorEnabled(c), + }) +}) + export default app diff --git a/src/utils/color.test.ts b/src/utils/color.test.ts index 48c263406..8c3ba9d7a 100644 --- a/src/utils/color.test.ts +++ b/src/utils/color.test.ts @@ -7,18 +7,6 @@ describe('getColorEnabled() - With colors enabled', () => { }) }) -describe('getColorEnabled() - With colors disabled in Edge', () => { - const edgeContext = new Context(new Request('http://localhost/'), { - env: { - NO_COLOR: true, - }, - }) - - it('should return false', async () => { - expect(getColorEnabled(edgeContext)).toBe(false) - }) -}) - describe('getColorEnabled() - With NO_COLOR environment variable set', () => { const mockContext = new Context(new Request('http://localhost/')) From 2d75f75a9a4fcb761fe7956c7d67bac8a3c1160d Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 03:21:23 +0000 Subject: [PATCH 10/12] fix --- runtime-tests/workerd/index.test.ts | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/runtime-tests/workerd/index.test.ts b/runtime-tests/workerd/index.test.ts index 935bf9eec..56bd1c482 100644 --- a/runtime-tests/workerd/index.test.ts +++ b/runtime-tests/workerd/index.test.ts @@ -68,20 +68,9 @@ describe('workerd with WebSocket', () => { }) }) -describe('worked with getColorEnabled()', () => { - let worker: UnstableDevWorker - - beforeAll(async () => { - worker = await unstable_dev('./runtime-tests/workerd/index.ts', { - vars: { - NAME: 'Hono', - }, - experimental: { disableExperimentalWarning: true }, - }) - }) - - afterAll(async () => { - await worker.stop() +describe('worked with getColorEnabled()', async () => { + const worker = await unstable_dev('./runtime-tests/workerd/index.ts', { + experimental: { disableExperimentalWarning: true }, }) it('Should return 200 response with the colorEnabled', async () => { From 21715cedc1a76a517ef23b98b1b2066e52f0defa Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 03:22:31 +0000 Subject: [PATCH 11/12] fix --- runtime-tests/workerd/index.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime-tests/workerd/index.test.ts b/runtime-tests/workerd/index.test.ts index 56bd1c482..b7b916eeb 100644 --- a/runtime-tests/workerd/index.test.ts +++ b/runtime-tests/workerd/index.test.ts @@ -75,6 +75,7 @@ describe('worked with getColorEnabled()', async () => { it('Should return 200 response with the colorEnabled', async () => { const res = await worker.fetch('/color') + await worker.stop() expect(res.status).toBe(200) expect(await res.json()).toEqual({ colorEnabled: false, From dc73ca22be23e4b7a661e235ddfe10d56083aacb Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 16 Dec 2024 04:50:47 +0000 Subject: [PATCH 12/12] add tests for deno --- runtime-tests/deno/utils.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 runtime-tests/deno/utils.test.ts diff --git a/runtime-tests/deno/utils.test.ts b/runtime-tests/deno/utils.test.ts new file mode 100644 index 000000000..8a610429d --- /dev/null +++ b/runtime-tests/deno/utils.test.ts @@ -0,0 +1,13 @@ +import { assertEquals } from '@std/assert' +import { Context } from '../../src/context' +import { getColorEnabled } from '../../src/utils/color' + +Deno.test('getColorEnabled() - With colors enabled', () => { + assertEquals(getColorEnabled(), true) +}) + +Deno.test('getColorEnabled() - With Deno.noColor set', () => { + const mockContext = new Context(new Request('http://localhost/')) + + assertEquals(getColorEnabled(mockContext), true) +})