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
  • Loading branch information
usualoma committed Apr 8, 2024
1 parent e76c687 commit 9ee301e
Show file tree
Hide file tree
Showing 7 changed files with 35 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
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

0 comments on commit 9ee301e

Please sign in to comment.