Skip to content

Commit

Permalink
[remark-images-download] Fix tests for Node 20
Browse files Browse the repository at this point in the history
  • Loading branch information
StaloneLab committed Oct 18, 2023
1 parent baf1bdf commit 993493c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
18 changes: 16 additions & 2 deletions packages/remark-images-download/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<p><img src="http://${blackholeAddr}:32764/bad"></p>`

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 = `<p><img src="http://example.com:32764/bad"></p>`

Expand All @@ -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)`
Expand Down
17 changes: 14 additions & 3 deletions packages/remark-images-download/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand Down Expand Up @@ -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);
});
});
};
Expand Down
14 changes: 13 additions & 1 deletion packages/remark-images-download/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 993493c

Please sign in to comment.