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

No buffer in browser #887

Merged
merged 2 commits into from May 31, 2017
Merged

No buffer in browser #887

merged 2 commits into from May 31, 2017

Conversation

fgnass
Copy link
Contributor

@fgnass fgnass commented May 4, 2017

This PR fixes #846 by using Feross' is-buffer module instead Buffer.isBuffer().

fgnass added 2 commits May 4, 2017 23:41
The is-buffer module checks if an object is a Buffer without causing
Webpack or Browserify to include the whole Buffer module in the bundle.
Since the http adapter is never used in the browser it's safe to use
the Buffer global and its isBuffer() method directly.
@coveralls
Copy link

Coverage Status

Coverage increased (+1.5%) to 93.719% when pulling c849467 on fgnass:no-buffer-in-browser into f31317a on mzabriskie:master.

@albertogasparin
Copy link

albertogasparin commented May 5, 2017

An alternative to the is-buffer module is just changing the isBuffer util function to:

function isBuffer (obj) {
  return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
}

Copy link

@atri-dastidar-snkeos atri-dastidar-snkeos left a comment

Choose a reason for hiding this comment

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

@fgnass: In lib/adapters/http.js you have removed Buffer, but you are still using it in the same file. Since Buffer is a global anyway, it is still being used.

Why not move the import of is-buffer from utils.js to http.js itself, and change this line to
if (isBuffer(data)) {

Or if you want to retain the function isBuffer in utils, you can do what @albertogasparin suggested.

BTW @albertogasparin: what you suggested is exactly what the is-buffer module does 😄

@fgnass
Copy link
Contributor Author

fgnass commented May 11, 2017

I removed the require call because http.js is only used in Node.js and Buffer is a global there so there's no need for importing it. I'm fine with inlining the is-buffer code in utils.js as we don't really need the extra checks for node 0.10 and Safari 4/5 that are provided by Feross' module.

@fgnass
Copy link
Contributor Author

fgnass commented May 11, 2017

... in http.js it's fine to use the native Buffer.isBuffer() check directly which, as a positive side effect, also covers the SlowBuffer case.

@albertogasparin
Copy link

@csvbox I know, as it's a one liner it looks silly to me importing a 3rd party dependency
@fgnass Are you sure that by using Buffer.isBuffer() in http.js Webpack is not including Buffer anyway? Is it not the reason why is-buffer was created (and you imported it) in first place?

@atri-dastidar-snkeos
Copy link

I removed the require call because http.js is only used in Node.js

Does it matter for the webpack build? In webpack.config.js I don't see separate targets defined for node or web, so even if you reference Buffer only in http.js, it would still be included, right? If yes, all other changes are rendered pointless.

@fgnass
Copy link
Contributor Author

fgnass commented May 11, 2017

@csvbox Webpack respects the "browser" field in package.json (https://github.com/mzabriskie/axios/blob/master/package.json#L71) and therefore replaces http.js with xhr.js whenever it is required.

@albertogasparin
Copy link

@csvbox I've tested @fgnass code and it works
@fgnass May I ask you what it the reason to include is-buffer now? It looks like it's not used anywhere. I think Buffer.isBuffer in http.js is all we need (and it's supported by node since ever, so checking if it exists seems pointless).

@albertogasparin
Copy link

My bad, utils.isBuffer is actually used in lib/defaults.js (missed it when I re-checked the project...).
Only question left for me is whenever include is-buffer module or write the one liner.

@atri-dastidar-snkeos
Copy link

Webpack respects the "browser" field in package.json (https://github.com/mzabriskie/axios/blob/master/package.json#L71) and therefore replaces http.js with xhr.js whenever it is required.

@fgnass sorry I didn't see that before, thanks

@fgnass
Copy link
Contributor Author

fgnass commented May 11, 2017

I would opt for keeping is-buffer as it already comes with good test coverage. We would otherwise have to copy the tests too. Small modules FTW.

@rubennorte
Copy link
Member

We're usually avoiding adding new dependencies to the project but since the module is very small I think it's OK. @mzabriskie, @nickuraltsev WDYT?

@nickuraltsev
Copy link
Member

I'm ok with this dependency. @fgnass Thank you for the PR!

@nickuraltsev nickuraltsev merged commit d1278df into axios:master May 31, 2017
jimthedev pushed a commit to commitizen/cz-cli that referenced this pull request May 24, 2018
This Pull Request updates dependency [axios](https://github.com/axios/axios) from `v0.15.2` to `v0.18.0`



<details>
<summary>Release Notes</summary>

### [`v0.18.0`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0180-Feb-19-2018)
[Compare Source](axios/axios@v0.17.1...v0.18.0)
- Adding support for UNIX Sockets when running with Node.js ([#&#8203;1070](`axios/axios#1070))
- Fixing typings ([#&#8203;1177](`axios/axios#1177)):
    - AxiosRequestConfig.proxy: allows type false
    - AxiosProxyConfig: added auth field
- Adding function signature in AxiosInstance interface so AxiosInstance can be invoked ([#&#8203;1192](`axios/axios#1192), [#&#8203;1254](`axios/axios#1254))
- Allowing maxContentLength to pass through to redirected calls as maxBodyLength in follow-redirects config ([#&#8203;1287](`axios/axios#1287))
- Fixing configuration when using an instance - method can now be set ([#&#8203;1342](`axios/axios#1342))

---

### [`v0.17.1`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0171-Nov-11-2017)
[Compare Source](axios/axios@v0.17.0...v0.17.1)
- Fixing issue with web workers ([#&#8203;1160](`axios/axios#1160))
- Allowing overriding transport ([#&#8203;1080](`axios/axios#1080))
- Updating TypeScript typings ([#&#8203;1165](`axios/axios#1165), [#&#8203;1125](`axios/axios#1125), [#&#8203;1131](`axios/axios#1131))

---

### [`v0.17.0`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0170-Oct-21-2017)
[Compare Source](axios/axios@v0.16.2...v0.17.0)
- **BREAKING** Fixing issue with `baseURL` and interceptors ([#&#8203;950](`axios/axios#950))
- **BREAKING** Improving handing of duplicate headers ([#&#8203;874](`axios/axios#874))
- Adding support for disabling proxies ([#&#8203;691](`axios/axios#691))
- Updating TypeScript typings with generic type parameters ([#&#8203;1061](`axios/axios#1061))

---

### [`v0.16.2`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0162-Jun-3-2017)
[Compare Source](axios/axios@v0.16.1...v0.16.2)
- Fixing issue with including `buffer` in bundle ([#&#8203;887](`axios/axios#887))
- Including underlying request in errors ([#&#8203;830](`axios/axios#830))
- Convert `method` to lowercase ([#&#8203;930](`axios/axios#930))

---

### [`v0.16.1`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0161-Apr-8-2017)
[Compare Source](axios/axios@v0.16.0...v0.16.1)
- Improving HTTP adapter to return last request in case of redirects ([#&#8203;828](`axios/axios#828))
- Updating `follow-redirects` dependency ([#&#8203;829](`axios/axios#829))
- Adding support for passing `Buffer` in node ([#&#8203;773](`axios/axios#773))

---

### [`v0.16.0`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0160-Mar-31-2017)
[Compare Source](axios/axios@v0.15.3...v0.16.0)
- **BREAKING** Removing `Promise` from axios typings in favor of built-in type declarations ([#&#8203;480](`axios/axios#480))
- Adding `options` shortcut method ([#&#8203;461](`axios/axios#461))
- Fixing issue with using `responseType: 'json'` in browsers incompatible with XHR Level 2 ([#&#8203;654](`axios/axios#654))
- Improving React Native detection ([#&#8203;731](`axios/axios#731))
- Fixing `combineURLs` to support empty `relativeURL` ([#&#8203;581](`axios/axios#581))
- Removing `PROTECTION_PREFIX` support ([#&#8203;561](`axios/axios#561))

---

### [`v0.15.3`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0153-Nov-27-2016)
[Compare Source](axios/axios@v0.15.2...v0.15.3)
- Fixing issue with custom instances and global defaults ([#&#8203;443](`axios/axios#443))
- Renaming `axios.d.ts` to `index.d.ts` ([#&#8203;519](`axios/axios#519))
- Adding `get`, `head`, and `delete` to `defaults.headers` ([#&#8203;509](`axios/axios#509))
- Fixing issue with `btoa` and IE ([#&#8203;507](`axios/axios#507))
- Adding support for proxy authentication ([#&#8203;483](`axios/axios#483))
- Improving HTTP adapter to use `http` protocol by default ([#&#8203;493](`axios/axios#493))
- Fixing proxy issues ([#&#8203;491](`axios/axios#491))

---

</details>




---

This PR has been generated by [Renovate Bot](https://renovatebot.com).
@axios axios locked and limited conversation to collaborators May 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Axios 0.16.1 adds buffer.js to the browser bundle, tripling its size
6 participants