27 lines
1.3 KiB
JavaScript
27 lines
1.3 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.connectionResetGuard = exports.isConnectionResetError = void 0;
|
||
|
const domain_1 = __importDefault(require("domain"));
|
||
|
const os_family_1 = __importDefault(require("os-family"));
|
||
|
const connectionResetDomain = domain_1.default.create();
|
||
|
function isConnectionResetError(err) {
|
||
|
return err.code === 'ECONNRESET' || !os_family_1.default.win && err.code === 'EPIPE' || os_family_1.default.win && err.code === 'ECONNABORTED';
|
||
|
}
|
||
|
exports.isConnectionResetError = isConnectionResetError;
|
||
|
connectionResetDomain.on('error', (err) => {
|
||
|
// NOTE: The nodejs throw the EPIPE error instead of the ECONNRESET error
|
||
|
// when the connection is broken in some cases on MacOS and Linux
|
||
|
// https://github.com/nodejs/node/blob/8b4af64f50c5e41ce0155716f294c24ccdecad03/test/parallel/test-http-destroyed-socket-write2.js
|
||
|
if (isConnectionResetError(err))
|
||
|
return;
|
||
|
connectionResetDomain.removeAllListeners('error');
|
||
|
throw err;
|
||
|
});
|
||
|
function connectionResetGuard(fn) {
|
||
|
connectionResetDomain.run(fn);
|
||
|
}
|
||
|
exports.connectionResetGuard = connectionResetGuard;
|