Skip to content

Commit f657cae

Browse files
committed
feat: remove getInternalBody function
1 parent 860de9f commit f657cae

File tree

3 files changed

+4
-55
lines changed

3 files changed

+4
-55
lines changed

src/listener.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Request as LightweightRequest,
77
toRequestError,
88
} from './request'
9-
import { cacheKey, getInternalBody, Response as LightweightResponse } from './response'
9+
import { cacheKey, Response as LightweightResponse } from './response'
1010
import type { InternalCache } from './response'
1111
import type { CustomErrorHandler, FetchCallback, HttpBindings } from './types'
1212
import { writeFromReadableStream, buildOutgoingHttpHeaders } from './utils'
@@ -93,28 +93,6 @@ const responseViaResponseObject = async (
9393

9494
const resHeaderRecord: OutgoingHttpHeaders = buildOutgoingHttpHeaders(res.headers)
9595

96-
const internalBody = getInternalBody(res as Response)
97-
if (internalBody) {
98-
const { length, source, stream } = internalBody
99-
if (source instanceof Uint8Array && source.byteLength !== length) {
100-
// maybe `source` is detached, so we should send via res.body
101-
} else {
102-
// send via internal raw data
103-
if (length) {
104-
resHeaderRecord['content-length'] = length
105-
}
106-
outgoing.writeHead(res.status, resHeaderRecord)
107-
if (typeof source === 'string' || source instanceof Uint8Array) {
108-
outgoing.end(source)
109-
} else if (source instanceof Blob) {
110-
outgoing.end(new Uint8Array(await source.arrayBuffer()))
111-
} else {
112-
await writeFromReadableStream(stream, outgoing)
113-
}
114-
return
115-
}
116-
}
117-
11896
if (res.body) {
11997
/**
12098
* If content-encoding is set, we assume that the response should be not decoded.

src/response.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33

44
import type { OutgoingHttpHeaders } from 'node:http'
55

6-
interface InternalBody {
7-
source: string | Uint8Array | FormData | Blob | null
8-
stream: ReadableStream
9-
length: number | null
10-
}
11-
126
const responseCache = Symbol('responseCache')
137
const getResponseCache = Symbol('getResponseCache')
148
export const cacheKey = Symbol('cache')
@@ -97,26 +91,3 @@ export class Response {
9791
})
9892
Object.setPrototypeOf(Response, GlobalResponse)
9993
Object.setPrototypeOf(Response.prototype, GlobalResponse.prototype)
100-
101-
const stateKey = Reflect.ownKeys(new GlobalResponse()).find(
102-
(k) => typeof k === 'symbol' && k.toString() === 'Symbol(state)'
103-
) as symbol | undefined
104-
if (!stateKey) {
105-
console.warn('Failed to find Response internal state key')
106-
}
107-
108-
export function getInternalBody(
109-
response: Response | globalThis.Response
110-
): InternalBody | undefined {
111-
if (!stateKey) {
112-
return
113-
}
114-
115-
if (response instanceof Response) {
116-
response = (response as any)[getResponseCache]()
117-
}
118-
119-
const state = (response as any)[stateKey] as { body?: InternalBody } | undefined
120-
121-
return (state && state.body) || undefined
122-
}

test/server.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('Basic', () => {
107107
})
108108
})
109109

110-
describe('via internal body', () => {
110+
describe('via response body', () => {
111111
const app = new Hono()
112112
app.use('*', async (c, next) => {
113113
await next()
@@ -173,15 +173,15 @@ describe('via internal body', () => {
173173
const res = await request(server).get('/uint8array')
174174
expect(res.status).toBe(200)
175175
expect(res.headers['content-type']).toMatch('application/octet-stream')
176-
expect(res.headers['content-length']).toMatch('3')
176+
expect(res.headers['content-length']).toBeUndefined()
177177
expect(res.body).toEqual(Buffer.from([1, 2, 3]))
178178
})
179179

180180
it('Should return 200 response - GET /blob', async () => {
181181
const res = await request(server).get('/blob')
182182
expect(res.status).toBe(200)
183183
expect(res.headers['content-type']).toMatch('application/octet-stream')
184-
expect(res.headers['content-length']).toMatch('3')
184+
expect(res.headers['content-length']).toBeUndefined()
185185
expect(res.body).toEqual(Buffer.from([1, 2, 3]))
186186
})
187187

0 commit comments

Comments
 (0)