Skip to content

Commit

Permalink
feat: parseFromUrl does not resolve relative references (asyncapi#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Jul 19, 2022
1 parent c962a7e commit 3288ba9
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,20 @@ function parseFromUrl(url, fetchOptions, options) {
//To not break the API by changing argument position and to silet the linter it is just better to move adding
if (!fetchOptions) fetchOptions = {};

// When executed in multiple async calls, 'checkErrorWrapper' passes URL
// string as an object by reference. In this case 'String.replace()' method
// mutates original URL string causing test with multiple async calls to fail.
// Thus completely independent object containing URL string is created for
// future manipulation.
const urlCopy = new String(url);

const indexLastSlashPosition = urlCopy.lastIndexOf('/');
const baseUrl = urlCopy.substring(0, indexLastSlashPosition + 1);

return new Promise((resolve, reject) => {
fetch(url, fetchOptions)
.then(res => res.text())
.then(doc => parse(doc, options))
.then(doc => parse(doc, { path: baseUrl }))
.then(result => resolve(result))
.catch(e => {
if (e instanceof ParserError) return reject(e);
Expand Down

0 comments on commit 3288ba9

Please sign in to comment.