Skip to content

Commit edfe3d4

Browse files
committed
fix: Properly detect Response short-circuit
1 parent 4bd258f commit edfe3d4

4 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/internal/__tests__/compile-fetch-function.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe("compileFetchFunction", () => {
7373
try {
7474
const onGlobalRequestRes0 = utils.hooks.onGlobalRequest[0].callback(ctx);
7575
if (onGlobalRequestRes0)
76-
if (typeof onGlobalRequestRes0.body === utils.FUNCTION)
76+
if (typeof onGlobalRequestRes0.body?.bytes === utils.FUNCTION)
7777
return onGlobalRequestRes0;
7878
else
7979
for (const key of Object.keys(onGlobalRequestRes0))

src/internal/__tests__/compile-route-handler.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("compileRouteHandler", () => {
4141
ctx.set.status = ctx.response.status;
4242
ctx.response = ctx.response.body;
4343
}
44-
if (typeof ctx.response?.body === utils.FUNCTION) return ctx.response;
44+
if (typeof ctx.response?.body?.bytes === utils.FUNCTION) return ctx.response;
4545
}
4646
4747
if (ctx.response == null) {
@@ -84,7 +84,7 @@ describe("compileRouteHandler", () => {
8484
ctx.set.status = ctx.response.status;
8585
ctx.response = ctx.response.body;
8686
}
87-
if (typeof ctx.response?.body === utils.FUNCTION) return ctx.response;
87+
if (typeof ctx.response?.body?.bytes === utils.FUNCTION) return ctx.response;
8888
}
8989
9090
if (ctx.response == null) {
@@ -121,7 +121,7 @@ describe("compileRouteHandler", () => {
121121
"async (request, ctx) => {
122122
const onTransformRes0 = await ctx.matchedRoute.data.hooks.onTransform[0].callback(ctx);
123123
if (onTransformRes0)
124-
if (typeof onTransformRes0.body === utils.FUNCTION)
124+
if (typeof onTransformRes0.body?.bytes === utils.FUNCTION)
125125
return onTransformRes0;
126126
else
127127
for (const key of Object.keys(onTransformRes0))
@@ -133,7 +133,7 @@ describe("compileRouteHandler", () => {
133133
ctx.set.status = ctx.response.status;
134134
ctx.response = ctx.response.body;
135135
}
136-
if (typeof ctx.response?.body === utils.FUNCTION) return ctx.response;
136+
if (typeof ctx.response?.body?.bytes === utils.FUNCTION) return ctx.response;
137137
}
138138
139139
if (ctx.response == null) {
@@ -169,7 +169,7 @@ describe("compileRouteHandler", () => {
169169
"async (request, ctx) => {
170170
const onBeforeHandleRes0 = await ctx.matchedRoute.data.hooks.onBeforeHandle[0].callback(ctx);
171171
if (onBeforeHandleRes0)
172-
if (typeof onBeforeHandleRes0.body === utils.FUNCTION)
172+
if (typeof onBeforeHandleRes0.body?.bytes === utils.FUNCTION)
173173
return onBeforeHandleRes0;
174174
else
175175
for (const key of Object.keys(onBeforeHandleRes0))
@@ -181,7 +181,7 @@ describe("compileRouteHandler", () => {
181181
ctx.set.status = ctx.response.status;
182182
ctx.response = ctx.response.body;
183183
}
184-
if (typeof ctx.response?.body === utils.FUNCTION) return ctx.response;
184+
if (typeof ctx.response?.body?.bytes === utils.FUNCTION) return ctx.response;
185185
}
186186
187187
if (ctx.response == null) {
@@ -221,12 +221,12 @@ describe("compileRouteHandler", () => {
221221
ctx.set.status = ctx.response.status;
222222
ctx.response = ctx.response.body;
223223
}
224-
if (typeof ctx.response?.body === utils.FUNCTION) return ctx.response;
224+
if (typeof ctx.response?.body?.bytes === utils.FUNCTION) return ctx.response;
225225
}
226226
227227
const onAfterHandleRes0 = await ctx.matchedRoute.data.hooks.onAfterHandle[0].callback(ctx);
228228
if (onAfterHandleRes0) ctx.response = onAfterHandleRes0;
229-
if (typeof onAfterHandleRes0.body === utils.FUNCTION)
229+
if (typeof onAfterHandleRes0.body?.bytes === utils.FUNCTION)
230230
return onAfterHandleRes0;
231231
232232
if (ctx.response == null) {
@@ -266,12 +266,12 @@ describe("compileRouteHandler", () => {
266266
ctx.set.status = ctx.response.status;
267267
ctx.response = ctx.response.body;
268268
}
269-
if (typeof ctx.response?.body === utils.FUNCTION) return ctx.response;
269+
if (typeof ctx.response?.body?.bytes === utils.FUNCTION) return ctx.response;
270270
}
271271
272272
const onMapResponseRes0 = await ctx.matchedRoute.data.hooks.onMapResponse[0].callback(ctx);
273273
if (onMapResponseRes0) ctx.response = onMapResponseRes0;
274-
if (typeof onMapResponseRes0.body === utils.FUNCTION)
274+
if (typeof onMapResponseRes0.body?.bytes === utils.FUNCTION)
275275
return onMapResponseRes0;
276276
277277
if (ctx.response == null) {

src/internal/compile-fetch-function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function compileOnGlobalRequestHook(hookCount: number): string {
7777
]
7878
: []),
7979
` if (${resultVar})`,
80-
` if (typeof ${resultVar}.body === utils.FUNCTION)`,
80+
` if (typeof ${resultVar}.body?.bytes === utils.FUNCTION)`,
8181
` return ${resultVar};`,
8282
` else`,
8383
` for (const key of Object.keys(${resultVar}))`,

src/internal/compile-route-handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ${options.hooks.onBeforeHandle?.length ? compileCtxModifierHookCall("onBeforeHan
4747
ctx.set.status = ctx.response.status;
4848
ctx.response = ctx.response.body;
4949
}
50-
if (typeof ctx.response?.body === utils.FUNCTION) return ctx.response;
50+
if (typeof ctx.response?.body?.bytes === utils.FUNCTION) return ctx.response;
5151
}
5252
5353
${compileValidateResponse(options)}
@@ -119,7 +119,7 @@ function compileCtxModifierHookCall(
119119
lines.push(
120120
` const ${resultVar} = await ctx.matchedRoute.data.hooks.${hook}[${i}].callback(ctx);`,
121121
` if (${resultVar})`,
122-
` if (typeof ${resultVar}.body === utils.FUNCTION)`,
122+
` if (typeof ${resultVar}.body?.bytes === utils.FUNCTION)`,
123123
` return ${resultVar};`,
124124
` else`,
125125
` for (const key of Object.keys(${resultVar}))`,
@@ -141,7 +141,7 @@ function compileResponseModifierHookCall(
141141
lines.push(
142142
` const ${resultVar} = await ctx.matchedRoute.data.hooks.${hook}[${i}].callback(ctx);`,
143143
` if (${resultVar}) ctx.response = ${resultVar};`,
144-
` if (typeof ${resultVar}.body === utils.FUNCTION)`,
144+
` if (typeof ${resultVar}.body?.bytes === utils.FUNCTION)`,
145145
` return ${resultVar};`,
146146
);
147147
}

0 commit comments

Comments
 (0)