From 4ce76f99a6c18a3d3f4996533997892737f1449e 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] feat(hono/context): add buffer returns --- runtime-tests/node/index.test.ts | 17 +++++++++++++++++ src/context.ts | 6 ++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/runtime-tests/node/index.test.ts b/runtime-tests/node/index.test.ts index d14bb9618..0e4e8c0a6 100644 --- a/runtime-tests/node/index.test.ts +++ b/runtime-tests/node/index.test.ts @@ -247,3 +247,20 @@ describe('compress', async () => { expect(res.text).toBe(cssContent) }) }) + +describe('Buffers', () => { + const buffer = Buffer.from('hello') + buffer.write('Hello') + + const app = new Hono().get('/', async (c) => { + return c.body(buffer) + }) + + 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.