Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Data URI handling and drop all URL analysis RegExps #853

Merged
merged 3 commits into from May 31, 2020
Merged

Fix Data URI handling and drop all URL analysis RegExps #853

merged 3 commits into from May 31, 2020

Conversation

tinovyatkin
Copy link
Member

@tinovyatkin tinovyatkin commented May 29, 2020

This PR adds a breaking test for existing dataUri implementation and reimplements it without using RegExp and also drops them in Request, following browsers logic on this case.

Only changes to tests are breaking case (from Wikipedia) for current implementation and adjusting of error messages as they are coming from native URL parser now (unsupported protocol message is copied from Chrome fetch)

@tinovyatkin tinovyatkin marked this pull request as draft May 29, 2020 19:14
@tinovyatkin tinovyatkin changed the title fix-data-url fix dataUri handling and drop all URL analysing RegExps May 29, 2020
@tinovyatkin tinovyatkin marked this pull request as ready for review May 29, 2020 19:40
@tinovyatkin tinovyatkin mentioned this pull request May 29, 2020
35 tasks
@tinovyatkin tinovyatkin added bug enhancement dependencies Pull requests that update a dependency file labels May 29, 2020
Copy link
Member

@xxczaki xxczaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jimmywarting

This comment has been minimized.

@tinovyatkin

This comment has been minimized.

@jimmywarting

This comment has been minimized.

@tinovyatkin

This comment has been minimized.

@jimmywarting
Copy link
Collaborator

#311 is an old issue worth bringing up, @TimothyGu first recommended that we used data-uri-to-buffer but later suggested that we also would use data-urls but the pr got closed by @Richienb who used native node functions (#659).

@tinovyatkin
Copy link
Member Author

tinovyatkin commented May 29, 2020

yeah, my first intention was to use native Buffer.from, but data URI can be pretty complex...

@jimmywarting
Copy link
Collaborator

well, #659 should never have been accepted. but that is gone now

@tinovyatkin tinovyatkin removed the dependencies Pull requests that update a dependency file label May 30, 2020
return new Promise((resolve, reject) => {
// Build request object
const request = new Request(url, options_);
const options = getNodeRequestOptions(request);
if (!supportedSchemas.has(options.protocol)) {
throw new TypeError(`node-fetch cannot load ${url}. URL scheme "${options.protocol.replace(/:$/, '')}" is not supported.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a FetchError?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All URL checking and unsupported protocol errors earlier were TypeErrors (like in browsers). Should we change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a limitation of node-fetch then I think it should be a FetchError. Otherwise, a TypeError will do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it’s not node-fetch specific - original error is from Chrome in this case and says “Fetch API cannot load...” - I’ve just replaced fetch API with node-fetch

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
@Richienb Richienb changed the title fix dataUri handling and drop all URL analysing RegExps Fix Data URI handling and drop all URL analysis RegExps May 30, 2020
test/main.js Outdated
@@ -2133,7 +2133,6 @@ describe('node-fetch', () => {
});

it('should accept data uri 2', async () => {
// this is from wikipedia: https://en.wikipedia.org/wiki/Data_URI_scheme
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Richienb why do you think this documentation comment is irrelevant? It's the source reference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A link to Wikipedia about data uris is not necessary since it might as well go in the readme.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not only about data URIs, this specific URI for the test was copied from the article, so, any future maintainer will have no doubts that the actual URI is correct.

@tinovyatkin tinovyatkin merged commit 69d25b9 into node-fetch:master May 31, 2020
@tinovyatkin tinovyatkin deleted the fix-data-url branch May 31, 2020 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants