diff --git a/packages/remark-images-download/__tests__/index.js b/packages/remark-images-download/__tests__/index.js index b2f5bdb5..6db4af44 100644 --- a/packages/remark-images-download/__tests__/index.js +++ b/packages/remark-images-download/__tests__/index.js @@ -529,7 +529,21 @@ describe('mock server tests', () => { }) }) - test('report connection errors', () => { + test('report timeout errors', () => { + const blackholeAddr = '192.0.2.1' + const file = `![](http://${blackholeAddr}:32764/bad)` + const html = `

` + + const render = renderFactory({httpRequestTimeout: 500}) + + return render(file).then(vfile => { + expect(firstMsg(vfile)).toBe( + `Request for http://${blackholeAddr}:32764/bad timed out`) + expect(vfile.contents).toBe(html) + }) + }) + + test('report timeout errors - IPv6 unreachable', () => { const file = `![](http://example.com:32764/bad)` const html = `

` @@ -540,7 +554,7 @@ describe('mock server tests', () => { 'Request for http://example.com:32764/bad timed out') expect(vfile.contents).toBe(html) }) - }, 600) + }) test('does not download large chunked streams', async () => { const file = `![](http://localhost:27273/stream-bomb)` diff --git a/packages/remark-images-download/dist/index.js b/packages/remark-images-download/dist/index.js index 30886c26..9ef0300d 100644 --- a/packages/remark-images-download/dist/index.js +++ b/packages/remark-images-download/dist/index.js @@ -4,11 +4,11 @@ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArra function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } -function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } +function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } -function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } +function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } @@ -307,7 +307,18 @@ function plugin() { reject(new Error("Request for ".concat(url, " timed out"))); }); req.on('error', function (err) { - return reject(err); + if (err.errors) { + var errorCodes = err.errors.map(function (e) { + return e.code; + }); // Node > 18 issues two requests: IPv4 and IPv6, timeout is + // not handled by default if one of them throws unreachable + + if (errorCodes.length === 2 && errorCodes.includes('ETIMEDOUT') && errorCodes.includes('ENETUNREACH')) { + req.emit('timeout'); + } + } + + reject(err); }); }); }; diff --git a/packages/remark-images-download/src/index.js b/packages/remark-images-download/src/index.js index 4bc135cb..00b2b563 100644 --- a/packages/remark-images-download/src/index.js +++ b/packages/remark-images-download/src/index.js @@ -210,7 +210,19 @@ function plugin ({ reject(new Error(`Request for ${url} timed out`)) }) - req.on('error', err => reject(err)) + req.on('error', err => { + if (err.errors) { + const errorCodes = err.errors.map(e => e.code) + // Node > 18 issues two requests: IPv4 and IPv6, timeout is + // not handled by default if one of them throws unreachable + if (errorCodes.length === 2 && + errorCodes.includes('ETIMEDOUT') && + errorCodes.includes('ENETUNREACH')) { + req.emit('timeout') + } + } + reject(err) + }) }) const checkAndCopy = async (from, to) => {