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

stop global pollution and clean up methods/exports #12

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
49 changes: 11 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* globals fetch, Headers */
/* istanbul ignore next */
if (!process.browser) {
global.fetch = require('node-fetch')
global.Headers = global.fetch.Headers
var fetch = require('node-fetch')
Copy link

Choose a reason for hiding this comment

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

This will shadow the global fetch in browsers with undefined.

Copy link
Author

Choose a reason for hiding this comment

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

No it won't?

Copy link

Choose a reason for hiding this comment

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

the var fetch will in fact be hoisted, unconditionally, to the top of the scope, which since this is a module, won't be the global scope - it will be a function wrapper.

Copy link
Author

@devsnek devsnek Sep 4, 2017

Choose a reason for hiding this comment

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

...That code won't run in browsers, and in node it will hoist to the bootstrap wrapper function in module compile

Copy link

@ljharb ljharb Sep 5, 2017

Choose a reason for hiding this comment

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

var fetch runs unconditionally, even if it's in the if block - it's hoisted.

Which means that it's the same as:

var fetch;
if (!process.browser) {
  fetch = require('node-fetch');
}

var Headers = fetch.Headers
}

const caseless = require('caseless')
Expand Down Expand Up @@ -89,36 +89,6 @@ class R2 {
if (opts.headers) this.setHeaders(opts.headers)
this.opts = opts
}
put (...args) {
this.opts.method = 'PUT'
this._args(...args)
return this
}
get (...args) {
this.opts.method = 'GET'
this._args(...args)
return this
}
post (...args) {
this.opts.method = 'POST'
this._args(...args)
return this
}
head (...args) {
this.opts.method = 'HEAD'
this._args(...args)
return this
}
patch (...args) {
this.opts.method = 'PATCH'
this._args(...args)
return this
}
delete (...args) {
this.opts.method = 'DELETE'
this._args(...args)
return this
}
_request () {
let url = this.opts.url
delete this.opts.url
Expand Down Expand Up @@ -155,9 +125,12 @@ class R2 {
}

module.exports = (...args) => new R2(...args)
module.exports.put = (...args) => new R2().put(...args)
module.exports.get = (...args) => new R2().get(...args)
module.exports.post = (...args) => new R2().post(...args)
module.exports.head = (...args) => new R2().head(...args)
module.exports.patch = (...args) => new R2().patch(...args)
module.exports.delete = (...args) => new R2().delete(...args)
const methods = ['get', 'put', 'post', 'head', 'patch', 'delete']
for (const method of methods) {
R2.prototype[method] = function(...args) {
Copy link

Choose a reason for hiding this comment

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

This is actually much slower, and also not equivalent (enumerability, for one) - these should stay as explicit prototype methods.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure that enumerability matters in this context, and the speed of attaching things to exports isn't really an issue in any situation.

Copy link

Choose a reason for hiding this comment

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

I'm referring to the speed of using the class in the first place, at runtime.

this.opts.method = method.toUpperCase();
this._args(...args)
return this
}
module.exports[method] = (...args) => new R2()[method](...args)
}