Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions packages/engine.io/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,17 +397,45 @@ export abstract class BaseServer extends EventEmitter {

const apply = (i) => {
debug("applying middleware n°%d", i + 1);
this.middlewares[i](req, res, (err?: any) => {
if (err) {
return callback(err);

// next(), a throw, or a rejection may each reach callback. Only the first
// one wins: a middleware that calls next() and then throws must not fire
// callback a second time. Errors thrown by next()'s own continuation are
// not middleware failures and are rethrown.
let settled = false;
let continuationError: any;

const done = (err?: any) => {
if (settled) {
debug("middleware already settled, ignoring extra callback");
return;
}
settled = true;

try {
if (err) {
callback(err);
} else if (i + 1 < this.middlewares.length) {
apply(i + 1);
} else {
callback();
}
} catch (e) {
continuationError = e;
throw e;
}
};

if (i + 1 < this.middlewares.length) {
apply(i + 1);
} else {
callback();
try {
Promise.resolve(this.middlewares[i](req, res, done)).catch((err) => {
done(err);
});
} catch (e) {
if (continuationError) {
throw continuationError;
}
});
done(e);
}
};

apply(0);
Expand Down Expand Up @@ -1042,6 +1070,16 @@ function abortRequest(
? errorContext.message
: Server.errorMessages[errorCode];

// A middleware may have already committed the response before failing.
// Writing again throws "Cannot write headers after they are sent".
if (res.headersSent) {
debug("response headers already sent, skipping error response");
if (!res.writableEnded && !res.finished) {
res.end();
}
return;
}

res.writeHead(statusCode, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
Expand Down Expand Up @@ -1116,7 +1154,7 @@ const validHdrChars = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // ... 255
]
];

function checkInvalidHeaderChar(val?: string) {
val += "";
Expand Down
Loading