Skip to content

Fix https://github.com/github/fetch/issues/657 #794

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

Merged
merged 3 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
var global = (function(self) {
return self
// eslint-disable-next-line no-invalid-this
})(typeof self !== 'undefined' ? self : this)
Copy link

Choose a reason for hiding this comment

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

Can you please add some fallback such as an empty object?
I know this whatwg-fetch is intended to be used only in browser, however, it gives many conveniences by just "importing it" and not using it.

Copy link

Choose a reason for hiding this comment

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

To illustrate more, I'm suggesting to make importing 'whatwg-fetch' in nodejs environment works, while blocking the usage.

Copy link
Owner Author

@JakeChampion JakeChampion Jul 10, 2020

Choose a reason for hiding this comment

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

This works in NodeJS correctly because the global this works in NodeJS.

Here the output of running it in NodeJS

❯ node -v
v14.4.0
❯ node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> var global = (function(self) {
...   return self
...   // eslint-disable-next-line no-invalid-this
... })(typeof self !== 'undefined' ? self : this)
undefined
> global
<ref *1> Object [global] {
  global: [Circular *1],
  clearInterval: [Function: clearInterval],
  clearTimeout: [Function: clearTimeout],
  setInterval: [Function: setInterval],
  setTimeout: [Function: setTimeout] {
    [Symbol(nodejs.util.promisify.custom)]: [Function (anonymous)]
  },
  queueMicrotask: [Function: queueMicrotask],
  clearImmediate: [Function: clearImmediate],
  setImmediate: [Function: setImmediate] {
    [Symbol(nodejs.util.promisify.custom)]: [Function (anonymous)]
  }
}
> 
(To exit, press ^C again or ^D or type .exit)
> 

var support = {
searchParams: 'URLSearchParams' in self,
iterable: 'Symbol' in self && 'iterator' in Symbol,
searchParams: 'URLSearchParams' in global,
iterable: 'Symbol' in global && 'iterator' in Symbol,
blob:
'FileReader' in self &&
'Blob' in self &&
'FileReader' in global &&
'Blob' in global &&
(function() {
try {
new Blob()
Expand All @@ -12,8 +16,8 @@ var support = {
return false
}
})(),
formData: 'FormData' in self,
arrayBuffer: 'ArrayBuffer' in self
formData: 'FormData' in global,
arrayBuffer: 'ArrayBuffer' in global
}

function isDataView(obj) {
Expand Down Expand Up @@ -435,7 +439,7 @@ Response.redirect = function(url, status) {
return new Response(null, {status: status, headers: {location: url}})
}

export var DOMException = self.DOMException
export var DOMException = global.DOMException
try {
new DOMException()
} catch (err) {
Expand Down Expand Up @@ -496,7 +500,7 @@ export function fetch(input, init) {

function fixUrl(url) {
try {
return url === '' && self.location.href ? self.location.href : url
return url === '' && global.location.href ? global.location.href : url
} catch (e) {
return url
}
Expand Down Expand Up @@ -543,9 +547,9 @@ export function fetch(input, init) {

fetch.polyfill = true

if (!self.fetch) {
self.fetch = fetch
self.Headers = Headers
self.Request = Request
self.Response = Response
if (!global.fetch) {
global.fetch = fetch
global.Headers = Headers
global.Request = Request
global.Response = Response
}
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default {
output: {
file: 'dist/fetch.umd.js',
format: 'umd',
name: 'WHATWGFetch'
name: 'WHATWGFetch',
strict: false
}
}