Skip to content

Commit

Permalink
feat(hono/context): add buffer returns (#3813)
Browse files Browse the repository at this point in the history
* feat(hono/context): add buffer returns

* test: add bun & deno tests for buffers

* fix(hono/context): buffer -> uint8array for web standards

---------

Co-authored-by: askorupskyy <[email protected]>
  • Loading branch information
askorupskyy and askorupskyy authored Feb 6, 2025
1 parent fa92596 commit 426cad6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 58 deletions.
12 changes: 12 additions & 0 deletions runtime-tests/bun/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
56 changes: 0 additions & 56 deletions runtime-tests/deno/deno.lock

This file was deleted.

12 changes: 12 additions & 0 deletions runtime-tests/deno/hono.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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')
})
24 changes: 24 additions & 0 deletions runtime-tests/node/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,27 @@ describe('compress', async () => {
expect(res.text).toBe(cssContent)
})
})

describe('Buffers', () => {
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)

it('should allow returning buffers', async () => {
const res = await request(server).get('/')
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')
})
})
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type HeaderRecord =
| Record<string, string | string[]>

/**
* Data type can be a string, ArrayBuffer, or ReadableStream.
* Data type can be a string, ArrayBuffer, Uint8Array (buffer), or ReadableStream.
*/
export type Data = string | ArrayBuffer | ReadableStream
export type Data = string | ArrayBuffer | ReadableStream | Uint8Array

/**
* Interface for the execution context in a web worker or similar environment.
Expand Down

0 comments on commit 426cad6

Please sign in to comment.