test: correct usages of `Proxy` to support Node.js 24 (#4260)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 5149aa4 · Browse files · 3 files·+41 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+41 added−39 removed
- tests:+40 added−38 removed
- other:+1 added−1 removed
Provenance
- Commit
108e35b- Tree
5f8e430- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
5149aa4
3 files changed+41 added−39 removed
modified:.github/workflows/ci.yml+1 added-1 removed
43 unmodified lines44 steps:45 - uses: actions/checkout@v446 - uses: actions/setup-node@v447 with:48- node-version: '20.x'48+ node-version: '24.x'49 - uses: oven-sh/setup-bun@v250 with:51 bun-version: '1.1.39'52 - run: bun install211 unmodified linesmodified:src/context.test.ts+15 added-2 removed
8 unmodified lines9 throw new TypeError('Cannot modify headers: Headers are immutable')10 }11 return Reflect.set(target, prop, value)12 },13- get(target, prop) {13+ get(target, prop, receiver) {14 if (prop === 'set') {15 return function () {16 throw new TypeError('Cannot modify headers: Headers are immutable')17 }18 }19- return Reflect.get(target, prop)19+ const value = Reflect.get(target, prop)20+ if (typeof value === 'function') {21+ return Object.defineProperties(22+ function (...args: unknown[]) {23+ // @ts-expect-error: `this` context is intentionally dynamic for proxy method binding24+ return Reflect.apply(value, this === receiver ? target : this, args)25+ },26+ {27+ name: { value: value.name },28+ length: { value: value.length },29+ }30+ )31+ }32+ return value33 },34 }),35 writable: false,36 })540 unmodified linesmodified:src/hono.test.ts+25 added-36 removed
23 unmodified lines24 _: string25 }26 }27 28+const createResponseProxy = (response: Response) => {29+ return new Proxy(response, {30+ get(target, prop, receiver) {31+ const value = target[prop as keyof Response]32+ if (typeof value === 'function') {33+ return Object.defineProperties(34+ function (...args: unknown[]) {35+ // @ts-expect-error: `this` context is intentionally dynamic for proxy method binding36+ return Reflect.apply(value, this === receiver ? target : this, args)37+ },38+ {39+ name: { value: value.name },40+ length: { value: value.length },41+ }42+ )43+ }44+ return value45+ },46+ })47+}48+49 describe('GET Request', () => {50 describe('without middleware', () => {51 // In other words, this is a test for cases that do not use `compose()`52 15 unmodified lines68 app.get('/hello-env', (c) => {69 return c.json(c.env)70 })71 51- app.get(52- '/proxy-object',53- () =>54- new Proxy(new Response('proxy'), {55- get(target, prop: keyof Response) {56- return target[prop]57- },58- })59- )72+ app.get('/proxy-object', () => createResponseProxy(new Response('proxy')))73 61- app.get(62- '/async-proxy-object',63- async () =>64- new Proxy(new Response('proxy'), {65- get(target, prop: keyof Response) {66- return target[prop]67- },68- })69- )74+ app.get('/async-proxy-object', async () => createResponseProxy(new Response('proxy')))75 76 it('GET http://localhost/hello is ok', async () => {77 const res = await app.request('http://localhost/hello')78 expect(res).not.toBeNull()84 unmodified lines163 app.get('/hello-env', (c) => {164 return c.json(c.env)165 })166 162- app.get(163- '/proxy-object',164- () =>165- new Proxy(new Response('proxy'), {166- get(target, prop: keyof Response) {167- return target[prop]168- },169- })170- )167+ app.get('/proxy-object', () => createResponseProxy(new Response('proxy')))168 172- app.get(173- '/async-proxy-object',174- async () =>175- new Proxy(new Response('proxy'), {176- get(target, prop: keyof Response) {177- return target[prop]178- },179- })180- )169+ app.get('/async-proxy-object', async () => createResponseProxy(new Response('proxy')))170 171 it('GET http://localhost/hello is ok', async () => {172 const res = await app.request('http://localhost/hello')173 expect(res).not.toBeNull()3487 unmodified lines