Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hono/context): add buffer returns #3813

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading