From 27c8cb6ffa5b9e38eefe27c53711d17015293180 Mon Sep 17 00:00:00 2001 From: askorupskyy <50280805+rcbxd@users.noreply.github.com> Date: Wed, 8 Jan 2025 17:29:39 -0600 Subject: [PATCH 1/3] feat(hono/context): add buffer returns --- runtime-tests/node/index.test.ts | 14 ++++++++++++++ src/context.ts | 6 ++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/runtime-tests/node/index.test.ts b/runtime-tests/node/index.test.ts index d14bb9618..8395ff625 100644 --- a/runtime-tests/node/index.test.ts +++ b/runtime-tests/node/index.test.ts @@ -247,3 +247,17 @@ describe('compress', async () => { expect(res.text).toBe(cssContent) }) }) + +describe('Buffers', () => { + const app = new Hono().get('/', async (c) => { + return c.body(Buffer.from('hello')) + }) + + const server = createAdaptorServer(app) + + it('should allow returning buffers', async () => { + const res = await request(server).get('/') + expect(res.status).toBe(200) + expect(res.text).toBe('hello') + }) +}) diff --git a/src/context.ts b/src/context.ts index a5f6b4c6a..3b6d54047 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,3 +1,5 @@ +import type { Buffer } from 'node:buffer' + import { HonoRequest } from './request' import type { Result } from './router' import type { @@ -27,9 +29,9 @@ type HeaderRecord = | Record /** - * Data type can be a string, ArrayBuffer, or ReadableStream. + * Data type can be a string, ArrayBuffer, Buffer, or ReadableStream. */ -export type Data = string | ArrayBuffer | ReadableStream +export type Data = string | ArrayBuffer | ReadableStream | Buffer /** * Interface for the execution context in a web worker or similar environment. From 22a2251904a9f4bd2440ab45d1b7734b8ee874ea Mon Sep 17 00:00:00 2001 From: askorupskyy <50280805+rcbxd@users.noreply.github.com> Date: Wed, 8 Jan 2025 17:39:25 -0600 Subject: [PATCH 2/3] test: add bun & deno tests for buffers --- runtime-tests/bun/index.test.tsx | 12 ++++++++++++ runtime-tests/deno/hono.test.ts | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/runtime-tests/bun/index.test.tsx b/runtime-tests/bun/index.test.tsx index 6dda78aba..838ade322 100644 --- a/runtime-tests/bun/index.test.tsx +++ b/runtime-tests/bun/index.test.tsx @@ -438,3 +438,15 @@ describe('streaming', () => { }) }) }) + +describe('Buffers', () => { + const app = new Hono().get('/', async (c) => { + return c.body(Buffer.from('hello')) + }) + + it('should allow returning buffers', async () => { + const res = await app.request(new Request('http://localhost/')) + expect(res.status).toBe(200) + expect(await res.text()).toBe('hello') + }) +}) diff --git a/runtime-tests/deno/hono.test.ts b/runtime-tests/deno/hono.test.ts index 8808c1633..fd5b544ff 100644 --- a/runtime-tests/deno/hono.test.ts +++ b/runtime-tests/deno/hono.test.ts @@ -1,4 +1,6 @@ import { assertEquals } from '@std/assert' +import { Buffer } from 'node:buffer' + import { Context } from '../../src/context.ts' import { env, getRuntimeKey } from '../../src/helper/adapter/index.ts' import { Hono } from '../../src/hono.ts' @@ -32,3 +34,13 @@ Deno.test('environment variables', () => { const { NAME } = env<{ NAME: string }>(c) assertEquals(NAME, 'Deno') }) + +Deno.test('Buffers', async () => { + const app = new Hono().get('/', async (c) => { + return c.body(Buffer.from('hello')) + }) + + const res = await app.request('/') + assertEquals(res.status, 200) + assertEquals(await res.text(), 'hello') +}) From 228c4992937c21b0448fce5d63b7b2558d4927c6 Mon Sep 17 00:00:00 2001 From: askorupskyy <50280805+rcbxd@users.noreply.github.com> Date: Thu, 9 Jan 2025 21:54:18 -0600 Subject: [PATCH 3/3] fix(hono/context): buffer -> uint8array for web standards --- runtime-tests/deno/deno.lock | 56 -------------------------------- runtime-tests/node/index.test.ts | 16 +++++++-- src/context.ts | 6 ++-- 3 files changed, 15 insertions(+), 63 deletions(-) delete mode 100644 runtime-tests/deno/deno.lock diff --git a/runtime-tests/deno/deno.lock b/runtime-tests/deno/deno.lock deleted file mode 100644 index 75d93277c..000000000 --- a/runtime-tests/deno/deno.lock +++ /dev/null @@ -1,56 +0,0 @@ -{ - "version": "3", - "packages": { - "specifiers": { - "jsr:@std/assert@^1.0.3": "jsr:@std/assert@1.0.5", - "jsr:@std/assert@^1.0.4": "jsr:@std/assert@1.0.5", - "jsr:@std/internal@^1.0.2": "jsr:@std/internal@1.0.2", - "jsr:@std/internal@^1.0.3": "jsr:@std/internal@1.0.3", - "jsr:@std/path@^1.0.3": "jsr:@std/path@1.0.6", - "jsr:@std/testing@^1.0.1": "jsr:@std/testing@1.0.2" - }, - "jsr": { - "@std/assert@1.0.3": { - "integrity": "b0d03ce1ced880df67132eea140623010d415848df66f6aa5df76507ca7c26d8", - "dependencies": [ - "jsr:@std/internal@^1.0.2" - ] - }, - "@std/assert@1.0.5": { - "integrity": "e37da8e4033490ce613eec4ac1d78dba1faf5b02a3f6c573a28f15365b9b440f", - "dependencies": [ - "jsr:@std/internal@^1.0.3" - ] - }, - "@std/internal@1.0.2": { - "integrity": "f4cabe2021352e8bfc24e6569700df87bf070914fc38d4b23eddd20108ac4495" - }, - "@std/internal@1.0.3": { - "integrity": "208e9b94a3d5649bd880e9ca38b885ab7651ab5b5303a56ed25de4755fb7b11e" - }, - "@std/path@1.0.6": { - "integrity": "ab2c55f902b380cf28e0eec501b4906e4c1960d13f00e11cfbcd21de15f18fed" - }, - "@std/testing@1.0.1": { - "integrity": "9c25841137ee818933e1722091bb9ed5fdc251c35e84c97979a52196bdb6c5c3", - "dependencies": [ - "jsr:@std/assert@^1.0.3" - ] - }, - "@std/testing@1.0.2": { - "integrity": "9e8a7f4e26c219addabe7942d09c3450fa0a74e9662341961bc0ef502274dec3", - "dependencies": [ - "jsr:@std/assert@^1.0.4" - ] - } - } - }, - "remote": {}, - "workspace": { - "dependencies": [ - "jsr:@std/assert@^1.0.3", - "jsr:@std/path@^1.0.3", - "jsr:@std/testing@^1.0.1" - ] - } -} diff --git a/runtime-tests/node/index.test.ts b/runtime-tests/node/index.test.ts index 8395ff625..42ba4e263 100644 --- a/runtime-tests/node/index.test.ts +++ b/runtime-tests/node/index.test.ts @@ -249,9 +249,13 @@ describe('compress', async () => { }) describe('Buffers', () => { - const app = new Hono().get('/', async (c) => { - return c.body(Buffer.from('hello')) - }) + const app = new Hono() + .get('/', async (c) => { + return c.body(Buffer.from('hello')) + }) + .get('/uint8array', async (c) => { + return c.body(Uint8Array.from('hello'.split(''), (c) => c.charCodeAt(0))) + }) const server = createAdaptorServer(app) @@ -260,4 +264,10 @@ describe('Buffers', () => { expect(res.status).toBe(200) expect(res.text).toBe('hello') }) + + it('should allow returning uint8array as well', async () => { + const res = await request(server).get('/uint8array') + expect(res.status).toBe(200) + expect(res.text).toBe('hello') + }) }) diff --git a/src/context.ts b/src/context.ts index 3b6d54047..9c19eff31 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,5 +1,3 @@ -import type { Buffer } from 'node:buffer' - import { HonoRequest } from './request' import type { Result } from './router' import type { @@ -29,9 +27,9 @@ type HeaderRecord = | Record /** - * Data type can be a string, ArrayBuffer, Buffer, or ReadableStream. + * Data type can be a string, ArrayBuffer, Uint8Array (buffer), or ReadableStream. */ -export type Data = string | ArrayBuffer | ReadableStream | Buffer +export type Data = string | ArrayBuffer | ReadableStream | Uint8Array /** * Interface for the execution context in a web worker or similar environment.