Skip to content

Commit

Permalink
feat: enable to skip overriding global Request and Response classes (#…
Browse files Browse the repository at this point in the history
…156)

* feat: enable to skip overriding global Request and Response classes

* test: make global.fetch writable in setup.ts

In node.js 20.12, supertest (or its dependencies) makes global.fetch unwritable and the test fails.
This problem occurs only in the test code and should be handled in test/setup.ts.
  • Loading branch information
usualoma committed Apr 11, 2024
1 parent e76c687 commit 5fbe0b3
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 13 deletions.
18 changes: 15 additions & 3 deletions src/listener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node:http'
import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2'
import { getAbortController, newRequest } from './request'
import { cacheKey, getInternalBody } from './response'
import { getAbortController, newRequest, Request as LightweightRequest } from './request'
import { cacheKey, getInternalBody, Response as LightweightResponse } from './response'
import type { CustomErrorHandler, FetchCallback, HttpBindings } from './types'
import { writeFromReadableStream, buildOutgoingHttpHeaders } from './utils'
import { X_ALREADY_SENT } from './utils/response/constants'
Expand Down Expand Up @@ -139,8 +139,20 @@ const responseViaResponseObject = async (

export const getRequestListener = (
fetchCallback: FetchCallback,
options: { errorHandler?: CustomErrorHandler } = {}
options: {
errorHandler?: CustomErrorHandler
overrideGlobalObjects?: boolean
} = {}
) => {
if (options.overrideGlobalObjects !== false && global.Request !== LightweightRequest) {
Object.defineProperty(global, 'Request', {
value: LightweightRequest,
})
Object.defineProperty(global, 'Response', {
value: LightweightResponse,
})
}

return async (
incoming: IncomingMessage | Http2ServerRequest,
outgoing: ServerResponse | Http2ServerResponse
Expand Down
3 changes: 0 additions & 3 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export class Request extends GlobalRequest {
super(input, options)
}
}
Object.defineProperty(global, 'Request', {
value: Request,
})

const newRequestFromIncoming = (
method: string,
Expand Down
3 changes: 0 additions & 3 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ export class Response {
})
Object.setPrototypeOf(Response, GlobalResponse)
Object.setPrototypeOf(Response.prototype, GlobalResponse.prototype)
Object.defineProperty(global, 'Response', {
value: Response,
})

const stateKey = Reflect.ownKeys(new GlobalResponse()).find(
(k) => typeof k === 'symbol' && k.toString() === 'Symbol(state)'
Expand Down
4 changes: 3 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import type { Options, ServerType } from './types'

export const createAdaptorServer = (options: Options): ServerType => {
const fetchCallback = options.fetch
const requestListener = getRequestListener(fetchCallback)
const requestListener = getRequestListener(fetchCallback, {
overrideGlobalObjects: options.overrideGlobalObjects,
})
// ts will complain about createServerHTTP and createServerHTTP2 not being callable, which works just fine
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const createServer: any = options.createServer || createServerHTTP
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type ServerOptions =

export type Options = {
fetch: FetchCallback
overrideGlobalObjects?: boolean
port?: number
hostname?: string
} & ServerOptions
Expand Down
45 changes: 45 additions & 0 deletions test/listener.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createServer } from 'node:http'
import request from 'supertest'
import { getRequestListener } from '../src/listener'
import { GlobalRequest, Request as LightweightRequest } from '../src/request'
import { GlobalResponse, Response as LightweightResponse } from '../src/response'

describe('Error handling - sync fetchCallback', () => {
const fetchCallback = jest.fn(() => {
Expand Down Expand Up @@ -205,3 +207,46 @@ describe('Abort request', () => {
}
})
})

describe('overrideGlobalObjects', () => {
const fetchCallback = jest.fn()

beforeEach(() => {
Object.defineProperty(global, 'Request', {
value: GlobalRequest,
writable: true,
})
Object.defineProperty(global, 'Response', {
value: GlobalResponse,
writable: true,
})
})

describe('default', () => {
it('Should be overridden', () => {
getRequestListener(fetchCallback)
expect(global.Request).toBe(LightweightRequest)
expect(global.Response).toBe(LightweightResponse)
})
})

describe('overrideGlobalObjects: true', () => {
it('Should be overridden', () => {
getRequestListener(fetchCallback, {
overrideGlobalObjects: true,
})
expect(global.Request).toBe(LightweightRequest)
expect(global.Response).toBe(LightweightResponse)
})
})

describe('overrideGlobalObjects: false', () => {
it('Should not be overridden', () => {
getRequestListener(fetchCallback, {
overrideGlobalObjects: false,
})
expect(global.Request).toBe(GlobalRequest)
expect(global.Response).toBe(GlobalResponse)
})
})
})
11 changes: 10 additions & 1 deletion test/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import type { IncomingMessage } from 'node:http'
import { newRequest, Request, GlobalRequest, getAbortController } from '../src/request'
import {
newRequest,
Request as LightweightRequest,
GlobalRequest,
getAbortController,
} from '../src/request'

Object.defineProperty(global, 'Request', {
value: LightweightRequest,
})

describe('Request', () => {
describe('newRequest', () => {
Expand Down
8 changes: 6 additions & 2 deletions test/response.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { createServer, type Server } from 'node:http'
import type { AddressInfo } from 'node:net'
import { GlobalResponse } from '../src/response'
import { GlobalResponse, Response as LightweightResponse } from '../src/response'

class NextResponse extends Response {}
Object.defineProperty(global, 'Response', {
value: LightweightResponse,
})

class NextResponse extends LightweightResponse {}

class UpperCaseStream extends TransformStream {
constructor() {
Expand Down
41 changes: 41 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { compress } from 'hono/compress'
import { poweredBy } from 'hono/powered-by'
import { stream } from 'hono/streaming'
import request from 'supertest'
import { GlobalRequest, Request as LightweightRequest } from '../src/request'
import { GlobalResponse, Response as LightweightResponse } from '../src/response'
import { createAdaptorServer } from '../src/server'
import type { HttpBindings } from '../src/types'

Expand Down Expand Up @@ -755,3 +757,42 @@ describe('forwarding IncomingMessage and ServerResponse in env', () => {
expect(res.body.status).toBe(200)
})
})

describe('overrideGlobalObjects', () => {
const app = new Hono()

beforeEach(() => {
Object.defineProperty(global, 'Request', {
value: GlobalRequest,
writable: true,
})
Object.defineProperty(global, 'Response', {
value: GlobalResponse,
writable: true,
})
})

describe('default', () => {
it('Should be overridden', () => {
createAdaptorServer(app)
expect(global.Request).toBe(LightweightRequest)
expect(global.Response).toBe(LightweightResponse)
})
})

describe('overrideGlobalObjects: true', () => {
it('Should be overridden', () => {
createAdaptorServer({ overrideGlobalObjects: true, fetch: app.fetch })
expect(global.Request).toBe(LightweightRequest)
expect(global.Response).toBe(LightweightResponse)
})
})

describe('overrideGlobalObjects: false', () => {
it('Should not be overridden', () => {
createAdaptorServer({ overrideGlobalObjects: false, fetch: app.fetch })
expect(global.Request).toBe(GlobalRequest)
expect(global.Response).toBe(GlobalResponse)
})
})
})
4 changes: 4 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Object.defineProperty(global, 'fetch', {
value: global.fetch,
writable: true,
})
Object.defineProperty(global, 'Response', {
value: global.Response,
writable: true,
Expand Down

0 comments on commit 5fbe0b3

Please sign in to comment.