1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

perf(serve): hoist repeated condition (#19449)

This commit is contained in:
Marvin Hagemeister 2023-06-09 23:21:26 +02:00 committed by GitHub
parent 2b2eebd583
commit ed76456059
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -503,16 +503,19 @@ async function asyncResponse(responseBodies, req, status, stream) {
function mapToCallback(context, callback, onError) { function mapToCallback(context, callback, onError) {
const responseBodies = context.responseBodies; const responseBodies = context.responseBodies;
const signal = context.abortController.signal; const signal = context.abortController.signal;
const hasCallback = callback.length > 0;
const hasOneCallback = callback.length === 1;
return async function (req) { return async function (req) {
// Get the response from the user-provided callback. If that fails, use onError. If that fails, return a fallback // Get the response from the user-provided callback. If that fails, use onError. If that fails, return a fallback
// 500 error. // 500 error.
let innerRequest; let innerRequest;
let response; let response;
try { try {
if (callback.length > 0) { if (hasCallback) {
innerRequest = new InnerRequest(req, context); innerRequest = new InnerRequest(req, context);
const request = fromInnerRequest(innerRequest, signal, "immutable"); const request = fromInnerRequest(innerRequest, signal, "immutable");
if (callback.length === 1) { if (hasOneCallback) {
response = await callback(request); response = await callback(request);
} else { } else {
response = await callback(request, { response = await callback(request, {