pax_global_header00006660000000000000000000000064135313225070014513gustar00rootroot0000000000000052 comment=b3dbaab202e043f7ecfb4cca9d594d60b0d80381 buffer-5.4.2/000077500000000000000000000000001353132250700127745ustar00rootroot00000000000000buffer-5.4.2/.github/000077500000000000000000000000001353132250700143345ustar00rootroot00000000000000buffer-5.4.2/.github/FUNDING.yml000066400000000000000000000000441353132250700161470ustar00rootroot00000000000000github: feross tidelift: npm/buffer buffer-5.4.2/.gitignore000066400000000000000000000000521353132250700147610ustar00rootroot00000000000000.DS_Store .airtap.yml *.log node_modules/ buffer-5.4.2/.npmignore000066400000000000000000000000621353132250700147710ustar00rootroot00000000000000.airtap.yml .github/ .travis.yml bin/ perf/ test/ buffer-5.4.2/.travis.yml000066400000000000000000000007541353132250700151130ustar00rootroot00000000000000language: node_js node_js: - lts/* sudo: false addons: sauce_connect: true hosts: - airtap.local env: global: - secure: AUsK+8fYSpwIMHcVt8Mu9SpG9RPHp4XDAwCQfpU3d5U65q8OVVC6C+XjvnNmEd2PoEJRHem8ZXEyRVfGM1sttKZLZP70TEKZOpOiRQnZiTQCAJ92TfGsDj/F4LoWSjUZUpfeg9b3iSp8G5dVw3+q9QZPIu6eykASK6bfcg//Cyg= - secure: eQBKJWu7XbhAN4ZvOOhMenC0IPpoYj+wZVVzzsLwUppfJqlrHV0CUW8rJdvZNiaGhYhoyHTnAcynpTE5kZfg3XjevOvF8PGY5wUYCki9BI+rp+pvVPZE/DNUAQpFR2gd2nxMJ4kYv7GVb6i/DfuqJa0h8IuY4zcMuKWwbQd3Az8= buffer-5.4.2/AUTHORS.md000066400000000000000000000042631353132250700144500ustar00rootroot00000000000000# Authors #### Ordered by first contribution. - Romain Beauxis (toots@rastageeks.org) - Tobias Koppers (tobias.koppers@googlemail.com) - Janus (ysangkok@gmail.com) - Rainer Dreyer (rdrey1@gmail.com) - Tõnis Tiigi (tonistiigi@gmail.com) - James Halliday (mail@substack.net) - Michael Williamson (mike@zwobble.org) - elliottcable (github@elliottcable.name) - rafael (rvalle@livelens.net) - Andrew Kelley (superjoe30@gmail.com) - Andreas Madsen (amwebdk@gmail.com) - Mike Brevoort (mike.brevoort@pearson.com) - Brian White (mscdex@mscdex.net) - Feross Aboukhadijeh (feross@feross.org) - Ruben Verborgh (ruben@verborgh.org) - eliang (eliang.cs@gmail.com) - Jesse Tane (jesse.tane@gmail.com) - Alfonso Boza (alfonso@cloud.com) - Mathias Buus (mathiasbuus@gmail.com) - Devon Govett (devongovett@gmail.com) - Daniel Cousens (github@dcousens.com) - Joseph Dykstra (josephdykstra@gmail.com) - Parsha Pourkhomami (parshap+git@gmail.com) - Damjan Košir (damjan.kosir@gmail.com) - daverayment (dave.rayment@gmail.com) - kawanet (u-suke@kawa.net) - Linus Unnebäck (linus@folkdatorn.se) - Nolan Lawson (nolan.lawson@gmail.com) - Calvin Metcalf (calvin.metcalf@gmail.com) - Koki Takahashi (hakatasiloving@gmail.com) - Guy Bedford (guybedford@gmail.com) - Jan Schär (jscissr@gmail.com) - RaulTsc (tomescu.raul@gmail.com) - Matthieu Monsch (monsch@alum.mit.edu) - Dan Ehrenberg (littledan@chromium.org) - Kirill Fomichev (fanatid@ya.ru) - Yusuke Kawasaki (u-suke@kawa.net) - DC (dcposch@dcpos.ch) - John-David Dalton (john.david.dalton@gmail.com) - adventure-yunfei (adventure030@gmail.com) - Emil Bay (github@tixz.dk) - Sam Sudar (sudar.sam@gmail.com) - Volker Mische (volker.mische@gmail.com) - David Walton (support@geekstocks.com) - Сковорода Никита Андреевич (chalkerx@gmail.com) - greenkeeper[bot] (greenkeeper[bot]@users.noreply.github.com) - ukstv (sergey.ukustov@machinomy.com) - Renée Kooi (renee@kooi.me) - ranbochen (ranbochen@qq.com) - Vladimir Borovik (bobahbdb@gmail.com) - greenkeeper[bot] (23040076+greenkeeper[bot]@users.noreply.github.com) - kumavis (aaron@kumavis.me) - Sergey Ukustov (sergey.ukustov@machinomy.com) - Fei Liu (liu.feiwood@gmail.com) #### Generated by bin/update-authors.sh. buffer-5.4.2/LICENSE000066400000000000000000000021221353132250700137760ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Feross Aboukhadijeh, and other contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. buffer-5.4.2/README.md000066400000000000000000000420741353132250700142620ustar00rootroot00000000000000# buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] [travis-image]: https://img.shields.io/travis/feross/buffer/master.svg [travis-url]: https://travis-ci.org/feross/buffer [npm-image]: https://img.shields.io/npm/v/buffer.svg [npm-url]: https://npmjs.org/package/buffer [downloads-image]: https://img.shields.io/npm/dm/buffer.svg [downloads-url]: https://npmjs.org/package/buffer [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg [standard-url]: https://standardjs.com #### The buffer module from [node.js](https://nodejs.org/), for the browser. [![saucelabs][saucelabs-image]][saucelabs-url] [saucelabs-image]: https://saucelabs.com/browser-matrix/buffer.svg [saucelabs-url]: https://saucelabs.com/u/buffer With [browserify](http://browserify.org), simply `require('buffer')` or use the `Buffer` global and you will get this module. The goal is to provide an API that is 100% identical to [node's Buffer API](https://nodejs.org/api/buffer.html). Read the [official docs](https://nodejs.org/api/buffer.html) for the full list of properties, instance methods, and class methods that are supported. ## features - Manipulate binary data like a boss, in all browsers! - Super fast. Backed by Typed Arrays (`Uint8Array`/`ArrayBuffer`, not `Object`) - Extremely small bundle size (**6.75KB minified + gzipped**, 51.9KB with comments) - Excellent browser support (Chrome, Firefox, Edge, Safari 9+, IE 11, iOS 9+, Android, etc.) - Preserves Node API exactly, with one minor difference (see below) - Square-bracket `buf[4]` notation works! - Does not modify any browser prototypes or put anything on `window` - Comprehensive test suite (including all buffer tests from node.js core) ## install To use this module directly (without browserify), install it: ```bash npm install buffer ``` [Get supported buffer with the Tidelift Subscription](https://tidelift.com/subscription/pkg/npm-buffer?utm_source=npm-buffer&utm_medium=referral&utm_campaign=readme) This module was previously called **native-buffer-browserify**, but please use **buffer** from now on. If you do not use a bundler, you can use the [standalone script](https://bundle.run/buffer). ## usage The module's API is identical to node's `Buffer` API. Read the [official docs](https://nodejs.org/api/buffer.html) for the full list of properties, instance methods, and class methods that are supported. As mentioned above, `require('buffer')` or use the `Buffer` global with [browserify](http://browserify.org) and this module will automatically be included in your bundle. Almost any npm module will work in the browser, even if it assumes that the node `Buffer` API will be available. To depend on this module explicitly (without browserify), require it like this: ```js var Buffer = require('buffer/').Buffer // note: the trailing slash is important! ``` To require this module explicitly, use `require('buffer/')` which tells the node.js module lookup algorithm (also used by browserify) to use the **npm module** named `buffer` instead of the **node.js core** module named `buffer`! ## how does it work? The Buffer constructor returns instances of `Uint8Array` that have their prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of `Uint8Array`, so the returned instances will have all the node `Buffer` methods and the `Uint8Array` methods. Square bracket notation works as expected -- it returns a single octet. The `Uint8Array` prototype remains unmodified. ## tracking the latest node api This module tracks the Buffer API in the latest (unstable) version of node.js. The Buffer API is considered **stable** in the [node stability index](https://nodejs.org/docs/latest/api/documentation.html#documentation_stability_index), so it is unlikely that there will ever be breaking changes. Nonetheless, when/if the Buffer API changes in node, this module's API will change accordingly. ## related packages - [`buffer-reverse`](https://www.npmjs.com/package/buffer-reverse) - Reverse a buffer - [`buffer-xor`](https://www.npmjs.com/package/buffer-xor) - Bitwise xor a buffer - [`is-buffer`](https://www.npmjs.com/package/is-buffer) - Determine if an object is a Buffer without including the whole `Buffer` package ## conversion packages ### convert typed array to buffer Use [`typedarray-to-buffer`](https://www.npmjs.com/package/typedarray-to-buffer) to convert any kind of typed array to a `Buffer`. Does not perform a copy, so it's super fast. ### convert buffer to typed array `Buffer` is a subclass of `Uint8Array` (which is a typed array). So there is no need to explicitly convert to typed array. Just use the buffer as a `Uint8Array`. ### convert blob to buffer Use [`blob-to-buffer`](https://www.npmjs.com/package/blob-to-buffer) to convert a `Blob` to a `Buffer`. ### convert buffer to blob To convert a `Buffer` to a `Blob`, use the `Blob` constructor: ```js var blob = new Blob([ buffer ]) ``` Optionally, specify a mimetype: ```js var blob = new Blob([ buffer ], { type: 'text/html' }) ``` ### convert arraybuffer to buffer To convert an `ArrayBuffer` to a `Buffer`, use the `Buffer.from` function. Does not perform a copy, so it's super fast. ```js var buffer = Buffer.from(arrayBuffer) ``` ### convert buffer to arraybuffer To convert a `Buffer` to an `ArrayBuffer`, use the `.buffer` property (which is present on all `Uint8Array` objects): ```js var arrayBuffer = buffer.buffer.slice( buffer.byteOffset, buffer.byteOffset + buffer.byteLength ) ``` Alternatively, use the [`to-arraybuffer`](https://www.npmjs.com/package/to-arraybuffer) module. ## performance See perf tests in `/perf`. `BrowserBuffer` is the browser `buffer` module (this repo). `Uint8Array` is included as a sanity check (since `BrowserBuffer` uses `Uint8Array` under the hood, `Uint8Array` will always be at least a bit faster). Finally, `NodeBuffer` is the node.js buffer module, which is included to compare against. NOTE: Performance has improved since these benchmarks were taken. PR welcome to update the README. ### Chrome 38 | Method | Operations | Accuracy | Sampled | Fastest | |:-------|:-----------|:---------|:--------|:-------:| | BrowserBuffer#bracket-notation | 11,457,464 ops/sec | ±0.86% | 66 | ✓ | | Uint8Array#bracket-notation | 10,824,332 ops/sec | ±0.74% | 65 | | | | | | | | BrowserBuffer#concat | 450,532 ops/sec | ±0.76% | 68 | | | Uint8Array#concat | 1,368,911 ops/sec | ±1.50% | 62 | ✓ | | | | | | | BrowserBuffer#copy(16000) | 903,001 ops/sec | ±0.96% | 67 | | | Uint8Array#copy(16000) | 1,422,441 ops/sec | ±1.04% | 66 | ✓ | | | | | | | BrowserBuffer#copy(16) | 11,431,358 ops/sec | ±0.46% | 69 | | | Uint8Array#copy(16) | 13,944,163 ops/sec | ±1.12% | 68 | ✓ | | | | | | | BrowserBuffer#new(16000) | 106,329 ops/sec | ±6.70% | 44 | | | Uint8Array#new(16000) | 131,001 ops/sec | ±2.85% | 31 | ✓ | | | | | | | BrowserBuffer#new(16) | 1,554,491 ops/sec | ±1.60% | 65 | | | Uint8Array#new(16) | 6,623,930 ops/sec | ±1.66% | 65 | ✓ | | | | | | | BrowserBuffer#readDoubleBE | 112,830 ops/sec | ±0.51% | 69 | ✓ | | DataView#getFloat64 | 93,500 ops/sec | ±0.57% | 68 | | | | | | | | BrowserBuffer#readFloatBE | 146,678 ops/sec | ±0.95% | 68 | ✓ | | DataView#getFloat32 | 99,311 ops/sec | ±0.41% | 67 | | | | | | | | BrowserBuffer#readUInt32LE | 843,214 ops/sec | ±0.70% | 69 | ✓ | | DataView#getUint32 | 103,024 ops/sec | ±0.64% | 67 | | | | | | | | BrowserBuffer#slice | 1,013,941 ops/sec | ±0.75% | 67 | | | Uint8Array#subarray | 1,903,928 ops/sec | ±0.53% | 67 | ✓ | | | | | | | BrowserBuffer#writeFloatBE | 61,387 ops/sec | ±0.90% | 67 | | | DataView#setFloat32 | 141,249 ops/sec | ±0.40% | 66 | ✓ | ### Firefox 33 | Method | Operations | Accuracy | Sampled | Fastest | |:-------|:-----------|:---------|:--------|:-------:| | BrowserBuffer#bracket-notation | 20,800,421 ops/sec | ±1.84% | 60 | | | Uint8Array#bracket-notation | 20,826,235 ops/sec | ±2.02% | 61 | ✓ | | | | | | | BrowserBuffer#concat | 153,076 ops/sec | ±2.32% | 61 | | | Uint8Array#concat | 1,255,674 ops/sec | ±8.65% | 52 | ✓ | | | | | | | BrowserBuffer#copy(16000) | 1,105,312 ops/sec | ±1.16% | 63 | | | Uint8Array#copy(16000) | 1,615,911 ops/sec | ±0.55% | 66 | ✓ | | | | | | | BrowserBuffer#copy(16) | 16,357,599 ops/sec | ±0.73% | 68 | | | Uint8Array#copy(16) | 31,436,281 ops/sec | ±1.05% | 68 | ✓ | | | | | | | BrowserBuffer#new(16000) | 52,995 ops/sec | ±6.01% | 35 | | | Uint8Array#new(16000) | 87,686 ops/sec | ±5.68% | 45 | ✓ | | | | | | | BrowserBuffer#new(16) | 252,031 ops/sec | ±1.61% | 66 | | | Uint8Array#new(16) | 8,477,026 ops/sec | ±0.49% | 68 | ✓ | | | | | | | BrowserBuffer#readDoubleBE | 99,871 ops/sec | ±0.41% | 69 | | | DataView#getFloat64 | 285,663 ops/sec | ±0.70% | 68 | ✓ | | | | | | | BrowserBuffer#readFloatBE | 115,540 ops/sec | ±0.42% | 69 | | | DataView#getFloat32 | 288,722 ops/sec | ±0.82% | 68 | ✓ | | | | | | | BrowserBuffer#readUInt32LE | 633,926 ops/sec | ±1.08% | 67 | ✓ | | DataView#getUint32 | 294,808 ops/sec | ±0.79% | 64 | | | | | | | | BrowserBuffer#slice | 349,425 ops/sec | ±0.46% | 69 | | | Uint8Array#subarray | 5,965,819 ops/sec | ±0.60% | 65 | ✓ | | | | | | | BrowserBuffer#writeFloatBE | 59,980 ops/sec | ±0.41% | 67 | | | DataView#setFloat32 | 317,634 ops/sec | ±0.63% | 68 | ✓ | ### Safari 8 | Method | Operations | Accuracy | Sampled | Fastest | |:-------|:-----------|:---------|:--------|:-------:| | BrowserBuffer#bracket-notation | 10,279,729 ops/sec | ±2.25% | 56 | ✓ | | Uint8Array#bracket-notation | 10,030,767 ops/sec | ±2.23% | 59 | | | | | | | | BrowserBuffer#concat | 144,138 ops/sec | ±1.38% | 65 | | | Uint8Array#concat | 4,950,764 ops/sec | ±1.70% | 63 | ✓ | | | | | | | BrowserBuffer#copy(16000) | 1,058,548 ops/sec | ±1.51% | 64 | | | Uint8Array#copy(16000) | 1,409,666 ops/sec | ±1.17% | 65 | ✓ | | | | | | | BrowserBuffer#copy(16) | 6,282,529 ops/sec | ±1.88% | 58 | | | Uint8Array#copy(16) | 11,907,128 ops/sec | ±2.87% | 58 | ✓ | | | | | | | BrowserBuffer#new(16000) | 101,663 ops/sec | ±3.89% | 57 | | | Uint8Array#new(16000) | 22,050,818 ops/sec | ±6.51% | 46 | ✓ | | | | | | | BrowserBuffer#new(16) | 176,072 ops/sec | ±2.13% | 64 | | | Uint8Array#new(16) | 24,385,731 ops/sec | ±5.01% | 51 | ✓ | | | | | | | BrowserBuffer#readDoubleBE | 41,341 ops/sec | ±1.06% | 67 | | | DataView#getFloat64 | 322,280 ops/sec | ±0.84% | 68 | ✓ | | | | | | | BrowserBuffer#readFloatBE | 46,141 ops/sec | ±1.06% | 65 | | | DataView#getFloat32 | 337,025 ops/sec | ±0.43% | 69 | ✓ | | | | | | | BrowserBuffer#readUInt32LE | 151,551 ops/sec | ±1.02% | 66 | | | DataView#getUint32 | 308,278 ops/sec | ±0.94% | 67 | ✓ | | | | | | | BrowserBuffer#slice | 197,365 ops/sec | ±0.95% | 66 | | | Uint8Array#subarray | 9,558,024 ops/sec | ±3.08% | 58 | ✓ | | | | | | | BrowserBuffer#writeFloatBE | 17,518 ops/sec | ±1.03% | 63 | | | DataView#setFloat32 | 319,751 ops/sec | ±0.48% | 68 | ✓ | ### Node 0.11.14 | Method | Operations | Accuracy | Sampled | Fastest | |:-------|:-----------|:---------|:--------|:-------:| | BrowserBuffer#bracket-notation | 10,489,828 ops/sec | ±3.25% | 90 | | | Uint8Array#bracket-notation | 10,534,884 ops/sec | ±0.81% | 92 | ✓ | | NodeBuffer#bracket-notation | 10,389,910 ops/sec | ±0.97% | 87 | | | | | | | | BrowserBuffer#concat | 487,830 ops/sec | ±2.58% | 88 | | | Uint8Array#concat | 1,814,327 ops/sec | ±1.28% | 88 | ✓ | | NodeBuffer#concat | 1,636,523 ops/sec | ±1.88% | 73 | | | | | | | | BrowserBuffer#copy(16000) | 1,073,665 ops/sec | ±0.77% | 90 | | | Uint8Array#copy(16000) | 1,348,517 ops/sec | ±0.84% | 89 | ✓ | | NodeBuffer#copy(16000) | 1,289,533 ops/sec | ±0.82% | 93 | | | | | | | | BrowserBuffer#copy(16) | 12,782,706 ops/sec | ±0.74% | 85 | | | Uint8Array#copy(16) | 14,180,427 ops/sec | ±0.93% | 92 | ✓ | | NodeBuffer#copy(16) | 11,083,134 ops/sec | ±1.06% | 89 | | | | | | | | BrowserBuffer#new(16000) | 141,678 ops/sec | ±3.30% | 67 | | | Uint8Array#new(16000) | 161,491 ops/sec | ±2.96% | 60 | | | NodeBuffer#new(16000) | 292,699 ops/sec | ±3.20% | 55 | ✓ | | | | | | | BrowserBuffer#new(16) | 1,655,466 ops/sec | ±2.41% | 82 | | | Uint8Array#new(16) | 14,399,926 ops/sec | ±0.91% | 94 | ✓ | | NodeBuffer#new(16) | 3,894,696 ops/sec | ±0.88% | 92 | | | | | | | | BrowserBuffer#readDoubleBE | 109,582 ops/sec | ±0.75% | 93 | ✓ | | DataView#getFloat64 | 91,235 ops/sec | ±0.81% | 90 | | | NodeBuffer#readDoubleBE | 88,593 ops/sec | ±0.96% | 81 | | | | | | | | BrowserBuffer#readFloatBE | 139,854 ops/sec | ±1.03% | 85 | ✓ | | DataView#getFloat32 | 98,744 ops/sec | ±0.80% | 89 | | | NodeBuffer#readFloatBE | 92,769 ops/sec | ±0.94% | 93 | | | | | | | | BrowserBuffer#readUInt32LE | 710,861 ops/sec | ±0.82% | 92 | | | DataView#getUint32 | 117,893 ops/sec | ±0.84% | 91 | | | NodeBuffer#readUInt32LE | 851,412 ops/sec | ±0.72% | 93 | ✓ | | | | | | | BrowserBuffer#slice | 1,673,877 ops/sec | ±0.73% | 94 | | | Uint8Array#subarray | 6,919,243 ops/sec | ±0.67% | 90 | ✓ | | NodeBuffer#slice | 4,617,604 ops/sec | ±0.79% | 93 | | | | | | | | BrowserBuffer#writeFloatBE | 66,011 ops/sec | ±0.75% | 93 | | | DataView#setFloat32 | 127,760 ops/sec | ±0.72% | 93 | ✓ | | NodeBuffer#writeFloatBE | 103,352 ops/sec | ±0.83% | 93 | | ### iojs 1.8.1 | Method | Operations | Accuracy | Sampled | Fastest | |:-------|:-----------|:---------|:--------|:-------:| | BrowserBuffer#bracket-notation | 10,990,488 ops/sec | ±1.11% | 91 | | | Uint8Array#bracket-notation | 11,268,757 ops/sec | ±0.65% | 97 | | | NodeBuffer#bracket-notation | 11,353,260 ops/sec | ±0.83% | 94 | ✓ | | | | | | | BrowserBuffer#concat | 378,954 ops/sec | ±0.74% | 94 | | | Uint8Array#concat | 1,358,288 ops/sec | ±0.97% | 87 | | | NodeBuffer#concat | 1,934,050 ops/sec | ±1.11% | 78 | ✓ | | | | | | | BrowserBuffer#copy(16000) | 894,538 ops/sec | ±0.56% | 84 | | | Uint8Array#copy(16000) | 1,442,656 ops/sec | ±0.71% | 96 | | | NodeBuffer#copy(16000) | 1,457,898 ops/sec | ±0.53% | 92 | ✓ | | | | | | | BrowserBuffer#copy(16) | 12,870,457 ops/sec | ±0.67% | 95 | | | Uint8Array#copy(16) | 16,643,989 ops/sec | ±0.61% | 93 | ✓ | | NodeBuffer#copy(16) | 14,885,848 ops/sec | ±0.74% | 94 | | | | | | | | BrowserBuffer#new(16000) | 109,264 ops/sec | ±4.21% | 63 | | | Uint8Array#new(16000) | 138,916 ops/sec | ±1.87% | 61 | | | NodeBuffer#new(16000) | 281,449 ops/sec | ±3.58% | 51 | ✓ | | | | | | | BrowserBuffer#new(16) | 1,362,935 ops/sec | ±0.56% | 99 | | | Uint8Array#new(16) | 6,193,090 ops/sec | ±0.64% | 95 | ✓ | | NodeBuffer#new(16) | 4,745,425 ops/sec | ±1.56% | 90 | | | | | | | | BrowserBuffer#readDoubleBE | 118,127 ops/sec | ±0.59% | 93 | ✓ | | DataView#getFloat64 | 107,332 ops/sec | ±0.65% | 91 | | | NodeBuffer#readDoubleBE | 116,274 ops/sec | ±0.94% | 95 | | | | | | | | BrowserBuffer#readFloatBE | 150,326 ops/sec | ±0.58% | 95 | ✓ | | DataView#getFloat32 | 110,541 ops/sec | ±0.57% | 98 | | | NodeBuffer#readFloatBE | 121,599 ops/sec | ±0.60% | 87 | | | | | | | | BrowserBuffer#readUInt32LE | 814,147 ops/sec | ±0.62% | 93 | | | DataView#getUint32 | 137,592 ops/sec | ±0.64% | 90 | | | NodeBuffer#readUInt32LE | 931,650 ops/sec | ±0.71% | 96 | ✓ | | | | | | | BrowserBuffer#slice | 878,590 ops/sec | ±0.68% | 93 | | | Uint8Array#subarray | 2,843,308 ops/sec | ±1.02% | 90 | | | NodeBuffer#slice | 4,998,316 ops/sec | ±0.68% | 90 | ✓ | | | | | | | BrowserBuffer#writeFloatBE | 65,927 ops/sec | ±0.74% | 93 | | | DataView#setFloat32 | 139,823 ops/sec | ±0.97% | 89 | ✓ | | NodeBuffer#writeFloatBE | 135,763 ops/sec | ±0.65% | 96 | | | | | | | ## Testing the project First, install the project: npm install Then, to run tests in Node.js, run: npm run test-node To test locally in a browser, you can run: npm run test-browser-es5-local # For ES5 browsers that don't support ES6 npm run test-browser-es6-local # For ES6 compliant browsers This will print out a URL that you can then open in a browser to run the tests, using [airtap](https://www.npmjs.com/package/airtap). To run automated browser tests using Saucelabs, ensure that your `SAUCE_USERNAME` and `SAUCE_ACCESS_KEY` environment variables are set, then run: npm test This is what's run in Travis, to check against various browsers. The list of browsers is kept in the `bin/airtap-es5.yml` and `bin/airtap-es6.yml` files. ## JavaScript Standard Style This module uses [JavaScript Standard Style](https://github.com/feross/standard). [![JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) To test that the code conforms to the style, `npm install` and run: ./node_modules/.bin/standard ## credit This was originally forked from [buffer-browserify](https://github.com/toots/buffer-browserify). ## Security Policies and Procedures The `buffer` team and community take all security bugs in `buffer` seriously. Please see our [security policies and procedures](https://github.com/feross/security) document to learn how to report issues. ## license MIT. Copyright (C) [Feross Aboukhadijeh](http://feross.org), and other contributors. Originally forked from an MIT-licensed module by Romain Beauxis. buffer-5.4.2/bin/000077500000000000000000000000001353132250700135445ustar00rootroot00000000000000buffer-5.4.2/bin/airtap-es5.yml000066400000000000000000000002301353132250700162340ustar00rootroot00000000000000sauce_connect: true loopback: airtap.local browsers: - name: ie version: latest - name: safari version: 9 - name: iphone version: 9.3 buffer-5.4.2/bin/airtap-es6.yml000066400000000000000000000004331353132250700162420ustar00rootroot00000000000000sauce_connect: true loopback: airtap.local browsers: - name: chrome version: -1..latest - name: firefox version: -1..latest - name: safari version: 10..latest - name: microsoftedge version: -1..latest - name: iphone version: - 10.3 - latest buffer-5.4.2/bin/download-node-tests.js000077500000000000000000000067361353132250700200130ustar00rootroot00000000000000#!/usr/bin/env node var concat = require('concat-stream') var cp = require('child_process') var fs = require('fs') var hyperquest = require('hyperquest') var path = require('path') var split = require('split') var through = require('through2') var url = 'https://api.github.com/repos/nodejs/node/contents' var dirs = [ '/test/parallel', '/test/pummel' ] cp.execSync('rm -rf node/test-*.js', { cwd: path.join(__dirname, '../test') }) var httpOpts = { headers: { 'User-Agent': null // auth if github rate-limits you... // 'Authorization': 'Basic ' + Buffer('username:password').toString('base64'), } } dirs.forEach(function (dir) { var req = hyperquest(url + dir, httpOpts) req.pipe(concat(function (data) { if (req.response.statusCode !== 200) { throw new Error(url + dir + ': ' + data.toString()) } downloadBufferTests(dir, JSON.parse(data)) })) }) function downloadBufferTests (dir, files) { files.forEach(function (file) { if (!/test-buffer.*/.test(file.name)) return const skipFileNames = [ // Only applies to node. Calls into C++ and needs to ensure the prototype can't // be faked, or else there will be a segfault. 'test-buffer-fakes.js', // Tests SharedArrayBuffer support, which is obscure and now temporarily // disabled in all browsers due to the Spectre/Meltdown security issue. 'test-buffer-sharedarraybuffer.js', // References Node.js internals, irrelevant to browser implementation 'test-buffer-bindingobj-no-zerofill.js', // Destructive test, modifies buffer.INSPECT_MAX_BYTES and causes later tests // to fail. 'test-buffer-inspect.js' ] // Skip test files with these names if (skipFileNames.includes(file.name)) return console.log(file.download_url) var out = path.join(__dirname, '../test/node', file.name) hyperquest(file.download_url, httpOpts) .pipe(split()) .pipe(testfixer(file.name)) .pipe(fs.createWriteStream(out)) .on('finish', function () { console.log('wrote ' + file.name) }) }) } function testfixer (filename) { var firstline = true return through(function (line, enc, cb) { line = line.toString() if (firstline) { // require buffer explicitly var preamble = 'var Buffer = require(\'../../\').Buffer;' if (/use strict/.test(line)) line += '\n' + preamble else line += preamble + '\n' + line firstline = false } // make `require('../common')` work line = line.replace(/require\('\.\.\/common'\);/g, 'require(\'./common\');') // require browser buffer line = line.replace(/(.*)require\('buffer'\)(.*)/g, '$1require(\'../../\')$2') // comment out console logs line = line.replace(/(.*console\..*)/g, '// $1') // we can't reliably test typed array max-sizes in the browser if (filename === 'test-buffer-big.js') { line = line.replace(/(.*new Int8Array.*RangeError.*)/, '// $1') line = line.replace(/(.*new ArrayBuffer.*RangeError.*)/, '// $1') line = line.replace(/(.*new Float64Array.*RangeError.*)/, '// $1') } // https://github.com/nodejs/node/blob/v0.12/test/parallel/test-buffer.js#L1138 // unfortunately we can't run this because crypto-browserify doesn't work in old // versions of ie if (filename === 'test-buffer.js') { line = line.replace(/^(\s*)(var crypto = require.*)/, '$1// $2') line = line.replace(/(crypto.createHash.*\))/, '1 /*$1*/') } cb(null, line + '\n') }) } buffer-5.4.2/bin/test.js000066400000000000000000000017411353132250700150640ustar00rootroot00000000000000#!/usr/bin/env node var cp = require('child_process') var fs = require('fs') var path = require('path') var node = cp.spawn('npm', ['run', 'test-node'], { stdio: 'inherit' }) node.on('close', function (code) { if (code !== 0) return process.exit(code) runBrowserTests() }) function runBrowserTests () { var airtapYmlPath = path.join(__dirname, '..', '.airtap.yml') writeES5AirtapYml() cp.spawn('npm', ['run', 'test-browser-es5'], { stdio: 'inherit' }) .on('close', function (code) { if (code !== 0) process.exit(code) writeES6AirtapYml() cp.spawn('npm', ['run', 'test-browser-es6'], { stdio: 'inherit' }) .on('close', function (code) { process.exit(code) }) }) function writeES5AirtapYml () { fs.writeFileSync(airtapYmlPath, fs.readFileSync(path.join(__dirname, 'airtap-es5.yml'))) } function writeES6AirtapYml () { fs.writeFileSync(airtapYmlPath, fs.readFileSync(path.join(__dirname, 'airtap-es6.yml'))) } } buffer-5.4.2/bin/update-authors.sh000077500000000000000000000010111353132250700170410ustar00rootroot00000000000000#!/bin/sh # Update AUTHORS.md based on git history. git log --reverse --format='%aN (%aE)' | perl -we ' BEGIN { %seen = (), @authors = (); } while (<>) { next if $seen{$_}; next if /(support\@greenkeeper.io)/; next if /(dcousens\@users.noreply.github.com)/; next if /(cmetcalf\@appgeo.com)/; $seen{$_} = push @authors, "- ", $_; } END { print "# Authors\n\n"; print "#### Ordered by first contribution.\n\n"; print @authors, "\n"; print "#### Generated by bin/update-authors.sh.\n"; } ' > AUTHORS.md buffer-5.4.2/index.d.ts000066400000000000000000000210621353132250700146760ustar00rootroot00000000000000export class Buffer extends Uint8Array { length: number write(string: string, offset?: number, length?: number, encoding?: string): number; toString(encoding?: string, start?: number, end?: number): string; toJSON(): { type: 'Buffer', data: any[] }; equals(otherBuffer: Buffer): boolean; compare(otherBuffer: Buffer, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number): number; copy(targetBuffer: Buffer, targetStart?: number, sourceStart?: number, sourceEnd?: number): number; slice(start?: number, end?: number): Buffer; writeUIntLE(value: number, offset: number, byteLength: number, noAssert?: boolean): number; writeUIntBE(value: number, offset: number, byteLength: number, noAssert?: boolean): number; writeIntLE(value: number, offset: number, byteLength: number, noAssert?: boolean): number; writeIntBE(value: number, offset: number, byteLength: number, noAssert?: boolean): number; readUIntLE(offset: number, byteLength: number, noAssert?: boolean): number; readUIntBE(offset: number, byteLength: number, noAssert?: boolean): number; readIntLE(offset: number, byteLength: number, noAssert?: boolean): number; readIntBE(offset: number, byteLength: number, noAssert?: boolean): number; readUInt8(offset: number, noAssert?: boolean): number; readUInt16LE(offset: number, noAssert?: boolean): number; readUInt16BE(offset: number, noAssert?: boolean): number; readUInt32LE(offset: number, noAssert?: boolean): number; readUInt32BE(offset: number, noAssert?: boolean): number; readInt8(offset: number, noAssert?: boolean): number; readInt16LE(offset: number, noAssert?: boolean): number; readInt16BE(offset: number, noAssert?: boolean): number; readInt32LE(offset: number, noAssert?: boolean): number; readInt32BE(offset: number, noAssert?: boolean): number; readFloatLE(offset: number, noAssert?: boolean): number; readFloatBE(offset: number, noAssert?: boolean): number; readDoubleLE(offset: number, noAssert?: boolean): number; readDoubleBE(offset: number, noAssert?: boolean): number; reverse(): this; swap16(): Buffer; swap32(): Buffer; swap64(): Buffer; writeUInt8(value: number, offset: number, noAssert?: boolean): number; writeUInt16LE(value: number, offset: number, noAssert?: boolean): number; writeUInt16BE(value: number, offset: number, noAssert?: boolean): number; writeUInt32LE(value: number, offset: number, noAssert?: boolean): number; writeUInt32BE(value: number, offset: number, noAssert?: boolean): number; writeInt8(value: number, offset: number, noAssert?: boolean): number; writeInt16LE(value: number, offset: number, noAssert?: boolean): number; writeInt16BE(value: number, offset: number, noAssert?: boolean): number; writeInt32LE(value: number, offset: number, noAssert?: boolean): number; writeInt32BE(value: number, offset: number, noAssert?: boolean): number; writeFloatLE(value: number, offset: number, noAssert?: boolean): number; writeFloatBE(value: number, offset: number, noAssert?: boolean): number; writeDoubleLE(value: number, offset: number, noAssert?: boolean): number; writeDoubleBE(value: number, offset: number, noAssert?: boolean): number; fill(value: any, offset?: number, end?: number): this; indexOf(value: string | number | Buffer, byteOffset?: number, encoding?: string): number; lastIndexOf(value: string | number | Buffer, byteOffset?: number, encoding?: string): number; includes(value: string | number | Buffer, byteOffset?: number, encoding?: string): boolean; /** * Allocates a new buffer containing the given {str}. * * @param str String to store in buffer. * @param encoding encoding to use, optional. Default is 'utf8' */ constructor (str: string, encoding?: string); /** * Allocates a new buffer of {size} octets. * * @param size count of octets to allocate. */ constructor (size: number); /** * Allocates a new buffer containing the given {array} of octets. * * @param array The octets to store. */ constructor (array: Uint8Array); /** * Produces a Buffer backed by the same allocated memory as * the given {ArrayBuffer}. * * * @param arrayBuffer The ArrayBuffer with which to share memory. */ constructor (arrayBuffer: ArrayBuffer); /** * Allocates a new buffer containing the given {array} of octets. * * @param array The octets to store. */ constructor (array: any[]); /** * Copies the passed {buffer} data onto a new {Buffer} instance. * * @param buffer The buffer to copy. */ constructor (buffer: Buffer); prototype: Buffer; /** * Allocates a new Buffer using an {array} of octets. * * @param array */ static from(array: any[]): Buffer; /** * When passed a reference to the .buffer property of a TypedArray instance, * the newly created Buffer will share the same allocated memory as the TypedArray. * The optional {byteOffset} and {length} arguments specify a memory range * within the {arrayBuffer} that will be shared by the Buffer. * * @param arrayBuffer The .buffer property of a TypedArray or a new ArrayBuffer() * @param byteOffset * @param length */ static from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer; /** * Copies the passed {buffer} data onto a new Buffer instance. * * @param buffer */ static from(buffer: Buffer | Uint8Array): Buffer; /** * Creates a new Buffer containing the given JavaScript string {str}. * If provided, the {encoding} parameter identifies the character encoding. * If not provided, {encoding} defaults to 'utf8'. * * @param str */ static from(str: string, encoding?: string): Buffer; /** * Returns true if {obj} is a Buffer * * @param obj object to test. */ static isBuffer(obj: any): obj is Buffer; /** * Returns true if {encoding} is a valid encoding argument. * Valid string encodings in Node 0.12: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex' * * @param encoding string to test. */ static isEncoding(encoding: string): boolean; /** * Gives the actual byte length of a string. encoding defaults to 'utf8'. * This is not the same as String.prototype.length since that returns the number of characters in a string. * * @param string string to test. * @param encoding encoding used to evaluate (defaults to 'utf8') */ static byteLength(string: string, encoding?: string): number; /** * Returns a buffer which is the result of concatenating all the buffers in the list together. * * If the list has no items, or if the totalLength is 0, then it returns a zero-length buffer. * If the list has exactly one item, then the first item of the list is returned. * If the list has more than one item, then a new Buffer is created. * * @param list An array of Buffer objects to concatenate * @param totalLength Total length of the buffers when concatenated. * If totalLength is not provided, it is read from the buffers in the list. However, this adds an additional loop to the function, so it is faster to provide the length explicitly. */ static concat(list: Buffer[], totalLength?: number): Buffer; /** * The same as buf1.compare(buf2). */ static compare(buf1: Buffer, buf2: Buffer): number; /** * Allocates a new buffer of {size} octets. * * @param size count of octets to allocate. * @param fill if specified, buffer will be initialized by calling buf.fill(fill). * If parameter is omitted, buffer will be filled with zeros. * @param encoding encoding used for call to buf.fill while initalizing */ static alloc(size: number, fill?: string | Buffer | number, encoding?: string): Buffer; /** * Allocates a new buffer of {size} octets, leaving memory not initialized, so the contents * of the newly created Buffer are unknown and may contain sensitive data. * * @param size count of octets to allocate */ static allocUnsafe(size: number): Buffer; /** * Allocates a new non-pooled buffer of {size} octets, leaving memory not initialized, so the contents * of the newly created Buffer are unknown and may contain sensitive data. * * @param size count of octets to allocate */ static allocUnsafeSlow(size: number): Buffer; } buffer-5.4.2/index.js000066400000000000000000001400511353132250700144420ustar00rootroot00000000000000/*! * The buffer module from node.js, for the browser. * * @author Feross Aboukhadijeh * @license MIT */ /* eslint-disable no-proto */ 'use strict' var base64 = require('base64-js') var ieee754 = require('ieee754') var customInspectSymbol = (typeof Symbol === 'function' && typeof Symbol.for === 'function') ? Symbol.for('nodejs.util.inspect.custom') : null exports.Buffer = Buffer exports.SlowBuffer = SlowBuffer exports.INSPECT_MAX_BYTES = 50 var K_MAX_LENGTH = 0x7fffffff exports.kMaxLength = K_MAX_LENGTH /** * If `Buffer.TYPED_ARRAY_SUPPORT`: * === true Use Uint8Array implementation (fastest) * === false Print warning and recommend using `buffer` v4.x which has an Object * implementation (most compatible, even IE6) * * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, * Opera 11.6+, iOS 4.2+. * * We report that the browser does not support typed arrays if the are not subclassable * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support * for __proto__ and has a buggy typed array implementation. */ Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport() if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && typeof console.error === 'function') { console.error( 'This browser lacks typed array (Uint8Array) support which is required by ' + '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' ) } function typedArraySupport () { // Can typed array instances can be augmented? try { var arr = new Uint8Array(1) var proto = { foo: function () { return 42 } } Object.setPrototypeOf(proto, Uint8Array.prototype) Object.setPrototypeOf(arr, proto) return arr.foo() === 42 } catch (e) { return false } } Object.defineProperty(Buffer.prototype, 'parent', { enumerable: true, get: function () { if (!Buffer.isBuffer(this)) return undefined return this.buffer } }) Object.defineProperty(Buffer.prototype, 'offset', { enumerable: true, get: function () { if (!Buffer.isBuffer(this)) return undefined return this.byteOffset } }) function createBuffer (length) { if (length > K_MAX_LENGTH) { throw new RangeError('The value "' + length + '" is invalid for option "size"') } // Return an augmented `Uint8Array` instance var buf = new Uint8Array(length) Object.setPrototypeOf(buf, Buffer.prototype) return buf } /** * The Buffer constructor returns instances of `Uint8Array` that have their * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of * `Uint8Array`, so the returned instances will have all the node `Buffer` methods * and the `Uint8Array` methods. Square bracket notation works as expected -- it * returns a single octet. * * The `Uint8Array` prototype remains unmodified. */ function Buffer (arg, encodingOrOffset, length) { // Common case. if (typeof arg === 'number') { if (typeof encodingOrOffset === 'string') { throw new TypeError( 'The "string" argument must be of type string. Received type number' ) } return allocUnsafe(arg) } return from(arg, encodingOrOffset, length) } // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 if (typeof Symbol !== 'undefined' && Symbol.species != null && Buffer[Symbol.species] === Buffer) { Object.defineProperty(Buffer, Symbol.species, { value: null, configurable: true, enumerable: false, writable: false }) } Buffer.poolSize = 8192 // not used by this implementation function from (value, encodingOrOffset, length) { if (typeof value === 'string') { return fromString(value, encodingOrOffset) } if (ArrayBuffer.isView(value)) { return fromArrayLike(value) } if (value == null) { throw new TypeError( 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + 'or Array-like Object. Received type ' + (typeof value) ) } if (isInstance(value, ArrayBuffer) || (value && isInstance(value.buffer, ArrayBuffer))) { return fromArrayBuffer(value, encodingOrOffset, length) } if (typeof value === 'number') { throw new TypeError( 'The "value" argument must not be of type number. Received type number' ) } var valueOf = value.valueOf && value.valueOf() if (valueOf != null && valueOf !== value) { return Buffer.from(valueOf, encodingOrOffset, length) } var b = fromObject(value) if (b) return b if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null && typeof value[Symbol.toPrimitive] === 'function') { return Buffer.from( value[Symbol.toPrimitive]('string'), encodingOrOffset, length ) } throw new TypeError( 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + 'or Array-like Object. Received type ' + (typeof value) ) } /** * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError * if value is a number. * Buffer.from(str[, encoding]) * Buffer.from(array) * Buffer.from(buffer) * Buffer.from(arrayBuffer[, byteOffset[, length]]) **/ Buffer.from = function (value, encodingOrOffset, length) { return from(value, encodingOrOffset, length) } // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: // https://github.com/feross/buffer/pull/148 Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype) Object.setPrototypeOf(Buffer, Uint8Array) function assertSize (size) { if (typeof size !== 'number') { throw new TypeError('"size" argument must be of type number') } else if (size < 0) { throw new RangeError('The value "' + size + '" is invalid for option "size"') } } function alloc (size, fill, encoding) { assertSize(size) if (size <= 0) { return createBuffer(size) } if (fill !== undefined) { // Only pay attention to encoding if it's a string. This // prevents accidentally sending in a number that would // be interpretted as a start offset. return typeof encoding === 'string' ? createBuffer(size).fill(fill, encoding) : createBuffer(size).fill(fill) } return createBuffer(size) } /** * Creates a new filled Buffer instance. * alloc(size[, fill[, encoding]]) **/ Buffer.alloc = function (size, fill, encoding) { return alloc(size, fill, encoding) } function allocUnsafe (size) { assertSize(size) return createBuffer(size < 0 ? 0 : checked(size) | 0) } /** * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. * */ Buffer.allocUnsafe = function (size) { return allocUnsafe(size) } /** * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. */ Buffer.allocUnsafeSlow = function (size) { return allocUnsafe(size) } function fromString (string, encoding) { if (typeof encoding !== 'string' || encoding === '') { encoding = 'utf8' } if (!Buffer.isEncoding(encoding)) { throw new TypeError('Unknown encoding: ' + encoding) } var length = byteLength(string, encoding) | 0 var buf = createBuffer(length) var actual = buf.write(string, encoding) if (actual !== length) { // Writing a hex string, for example, that contains invalid characters will // cause everything after the first invalid character to be ignored. (e.g. // 'abxxcd' will be treated as 'ab') buf = buf.slice(0, actual) } return buf } function fromArrayLike (array) { var length = array.length < 0 ? 0 : checked(array.length) | 0 var buf = createBuffer(length) for (var i = 0; i < length; i += 1) { buf[i] = array[i] & 255 } return buf } function fromArrayBuffer (array, byteOffset, length) { if (byteOffset < 0 || array.byteLength < byteOffset) { throw new RangeError('"offset" is outside of buffer bounds') } if (array.byteLength < byteOffset + (length || 0)) { throw new RangeError('"length" is outside of buffer bounds') } var buf if (byteOffset === undefined && length === undefined) { buf = new Uint8Array(array) } else if (length === undefined) { buf = new Uint8Array(array, byteOffset) } else { buf = new Uint8Array(array, byteOffset, length) } // Return an augmented `Uint8Array` instance Object.setPrototypeOf(buf, Buffer.prototype) return buf } function fromObject (obj) { if (Buffer.isBuffer(obj)) { var len = checked(obj.length) | 0 var buf = createBuffer(len) if (buf.length === 0) { return buf } obj.copy(buf, 0, 0, len) return buf } if (obj.length !== undefined) { if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { return createBuffer(0) } return fromArrayLike(obj) } if (obj.type === 'Buffer' && Array.isArray(obj.data)) { return fromArrayLike(obj.data) } } function checked (length) { // Note: cannot use `length < K_MAX_LENGTH` here because that fails when // length is NaN (which is otherwise coerced to zero.) if (length >= K_MAX_LENGTH) { throw new RangeError('Attempt to allocate Buffer larger than maximum ' + 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes') } return length | 0 } function SlowBuffer (length) { if (+length != length) { // eslint-disable-line eqeqeq length = 0 } return Buffer.alloc(+length) } Buffer.isBuffer = function isBuffer (b) { return b != null && b._isBuffer === true && b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false } Buffer.compare = function compare (a, b) { if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength) if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength) if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { throw new TypeError( 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array' ) } if (a === b) return 0 var x = a.length var y = b.length for (var i = 0, len = Math.min(x, y); i < len; ++i) { if (a[i] !== b[i]) { x = a[i] y = b[i] break } } if (x < y) return -1 if (y < x) return 1 return 0 } Buffer.isEncoding = function isEncoding (encoding) { switch (String(encoding).toLowerCase()) { case 'hex': case 'utf8': case 'utf-8': case 'ascii': case 'latin1': case 'binary': case 'base64': case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return true default: return false } } Buffer.concat = function concat (list, length) { if (!Array.isArray(list)) { throw new TypeError('"list" argument must be an Array of Buffers') } if (list.length === 0) { return Buffer.alloc(0) } var i if (length === undefined) { length = 0 for (i = 0; i < list.length; ++i) { length += list[i].length } } var buffer = Buffer.allocUnsafe(length) var pos = 0 for (i = 0; i < list.length; ++i) { var buf = list[i] if (isInstance(buf, Uint8Array)) { buf = Buffer.from(buf) } if (!Buffer.isBuffer(buf)) { throw new TypeError('"list" argument must be an Array of Buffers') } buf.copy(buffer, pos) pos += buf.length } return buffer } function byteLength (string, encoding) { if (Buffer.isBuffer(string)) { return string.length } if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) { return string.byteLength } if (typeof string !== 'string') { throw new TypeError( 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' + 'Received type ' + typeof string ) } var len = string.length var mustMatch = (arguments.length > 2 && arguments[2] === true) if (!mustMatch && len === 0) return 0 // Use a for loop to avoid recursion var loweredCase = false for (;;) { switch (encoding) { case 'ascii': case 'latin1': case 'binary': return len case 'utf8': case 'utf-8': return utf8ToBytes(string).length case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return len * 2 case 'hex': return len >>> 1 case 'base64': return base64ToBytes(string).length default: if (loweredCase) { return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8 } encoding = ('' + encoding).toLowerCase() loweredCase = true } } } Buffer.byteLength = byteLength function slowToString (encoding, start, end) { var loweredCase = false // No need to verify that "this.length <= MAX_UINT32" since it's a read-only // property of a typed array. // This behaves neither like String nor Uint8Array in that we set start/end // to their upper/lower bounds if the value passed is out of range. // undefined is handled specially as per ECMA-262 6th Edition, // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. if (start === undefined || start < 0) { start = 0 } // Return early if start > this.length. Done here to prevent potential uint32 // coercion fail below. if (start > this.length) { return '' } if (end === undefined || end > this.length) { end = this.length } if (end <= 0) { return '' } // Force coersion to uint32. This will also coerce falsey/NaN values to 0. end >>>= 0 start >>>= 0 if (end <= start) { return '' } if (!encoding) encoding = 'utf8' while (true) { switch (encoding) { case 'hex': return hexSlice(this, start, end) case 'utf8': case 'utf-8': return utf8Slice(this, start, end) case 'ascii': return asciiSlice(this, start, end) case 'latin1': case 'binary': return latin1Slice(this, start, end) case 'base64': return base64Slice(this, start, end) case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return utf16leSlice(this, start, end) default: if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) encoding = (encoding + '').toLowerCase() loweredCase = true } } } // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) // to detect a Buffer instance. It's not possible to use `instanceof Buffer` // reliably in a browserify context because there could be multiple different // copies of the 'buffer' package in use. This method works even for Buffer // instances that were created from another copy of the `buffer` package. // See: https://github.com/feross/buffer/issues/154 Buffer.prototype._isBuffer = true function swap (b, n, m) { var i = b[n] b[n] = b[m] b[m] = i } Buffer.prototype.swap16 = function swap16 () { var len = this.length if (len % 2 !== 0) { throw new RangeError('Buffer size must be a multiple of 16-bits') } for (var i = 0; i < len; i += 2) { swap(this, i, i + 1) } return this } Buffer.prototype.swap32 = function swap32 () { var len = this.length if (len % 4 !== 0) { throw new RangeError('Buffer size must be a multiple of 32-bits') } for (var i = 0; i < len; i += 4) { swap(this, i, i + 3) swap(this, i + 1, i + 2) } return this } Buffer.prototype.swap64 = function swap64 () { var len = this.length if (len % 8 !== 0) { throw new RangeError('Buffer size must be a multiple of 64-bits') } for (var i = 0; i < len; i += 8) { swap(this, i, i + 7) swap(this, i + 1, i + 6) swap(this, i + 2, i + 5) swap(this, i + 3, i + 4) } return this } Buffer.prototype.toString = function toString () { var length = this.length if (length === 0) return '' if (arguments.length === 0) return utf8Slice(this, 0, length) return slowToString.apply(this, arguments) } Buffer.prototype.toLocaleString = Buffer.prototype.toString Buffer.prototype.equals = function equals (b) { if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') if (this === b) return true return Buffer.compare(this, b) === 0 } Buffer.prototype.inspect = function inspect () { var str = '' var max = exports.INSPECT_MAX_BYTES str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim() if (this.length > max) str += ' ... ' return '' } if (customInspectSymbol) { Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect } Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { if (isInstance(target, Uint8Array)) { target = Buffer.from(target, target.offset, target.byteLength) } if (!Buffer.isBuffer(target)) { throw new TypeError( 'The "target" argument must be one of type Buffer or Uint8Array. ' + 'Received type ' + (typeof target) ) } if (start === undefined) { start = 0 } if (end === undefined) { end = target ? target.length : 0 } if (thisStart === undefined) { thisStart = 0 } if (thisEnd === undefined) { thisEnd = this.length } if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { throw new RangeError('out of range index') } if (thisStart >= thisEnd && start >= end) { return 0 } if (thisStart >= thisEnd) { return -1 } if (start >= end) { return 1 } start >>>= 0 end >>>= 0 thisStart >>>= 0 thisEnd >>>= 0 if (this === target) return 0 var x = thisEnd - thisStart var y = end - start var len = Math.min(x, y) var thisCopy = this.slice(thisStart, thisEnd) var targetCopy = target.slice(start, end) for (var i = 0; i < len; ++i) { if (thisCopy[i] !== targetCopy[i]) { x = thisCopy[i] y = targetCopy[i] break } } if (x < y) return -1 if (y < x) return 1 return 0 } // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, // OR the last index of `val` in `buffer` at offset <= `byteOffset`. // // Arguments: // - buffer - a Buffer to search // - val - a string, Buffer, or number // - byteOffset - an index into `buffer`; will be clamped to an int32 // - encoding - an optional encoding, relevant is val is a string // - dir - true for indexOf, false for lastIndexOf function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { // Empty buffer means no match if (buffer.length === 0) return -1 // Normalize byteOffset if (typeof byteOffset === 'string') { encoding = byteOffset byteOffset = 0 } else if (byteOffset > 0x7fffffff) { byteOffset = 0x7fffffff } else if (byteOffset < -0x80000000) { byteOffset = -0x80000000 } byteOffset = +byteOffset // Coerce to Number. if (numberIsNaN(byteOffset)) { // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer byteOffset = dir ? 0 : (buffer.length - 1) } // Normalize byteOffset: negative offsets start from the end of the buffer if (byteOffset < 0) byteOffset = buffer.length + byteOffset if (byteOffset >= buffer.length) { if (dir) return -1 else byteOffset = buffer.length - 1 } else if (byteOffset < 0) { if (dir) byteOffset = 0 else return -1 } // Normalize val if (typeof val === 'string') { val = Buffer.from(val, encoding) } // Finally, search either indexOf (if dir is true) or lastIndexOf if (Buffer.isBuffer(val)) { // Special case: looking for empty string/buffer always fails if (val.length === 0) { return -1 } return arrayIndexOf(buffer, val, byteOffset, encoding, dir) } else if (typeof val === 'number') { val = val & 0xFF // Search for a byte value [0-255] if (typeof Uint8Array.prototype.indexOf === 'function') { if (dir) { return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) } else { return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) } } return arrayIndexOf(buffer, [val], byteOffset, encoding, dir) } throw new TypeError('val must be string, number or Buffer') } function arrayIndexOf (arr, val, byteOffset, encoding, dir) { var indexSize = 1 var arrLength = arr.length var valLength = val.length if (encoding !== undefined) { encoding = String(encoding).toLowerCase() if (encoding === 'ucs2' || encoding === 'ucs-2' || encoding === 'utf16le' || encoding === 'utf-16le') { if (arr.length < 2 || val.length < 2) { return -1 } indexSize = 2 arrLength /= 2 valLength /= 2 byteOffset /= 2 } } function read (buf, i) { if (indexSize === 1) { return buf[i] } else { return buf.readUInt16BE(i * indexSize) } } var i if (dir) { var foundIndex = -1 for (i = byteOffset; i < arrLength; i++) { if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { if (foundIndex === -1) foundIndex = i if (i - foundIndex + 1 === valLength) return foundIndex * indexSize } else { if (foundIndex !== -1) i -= i - foundIndex foundIndex = -1 } } } else { if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength for (i = byteOffset; i >= 0; i--) { var found = true for (var j = 0; j < valLength; j++) { if (read(arr, i + j) !== read(val, j)) { found = false break } } if (found) return i } } return -1 } Buffer.prototype.includes = function includes (val, byteOffset, encoding) { return this.indexOf(val, byteOffset, encoding) !== -1 } Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { return bidirectionalIndexOf(this, val, byteOffset, encoding, true) } Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) { return bidirectionalIndexOf(this, val, byteOffset, encoding, false) } function hexWrite (buf, string, offset, length) { offset = Number(offset) || 0 var remaining = buf.length - offset if (!length) { length = remaining } else { length = Number(length) if (length > remaining) { length = remaining } } var strLen = string.length if (length > strLen / 2) { length = strLen / 2 } for (var i = 0; i < length; ++i) { var parsed = parseInt(string.substr(i * 2, 2), 16) if (numberIsNaN(parsed)) return i buf[offset + i] = parsed } return i } function utf8Write (buf, string, offset, length) { return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) } function asciiWrite (buf, string, offset, length) { return blitBuffer(asciiToBytes(string), buf, offset, length) } function latin1Write (buf, string, offset, length) { return asciiWrite(buf, string, offset, length) } function base64Write (buf, string, offset, length) { return blitBuffer(base64ToBytes(string), buf, offset, length) } function ucs2Write (buf, string, offset, length) { return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) } Buffer.prototype.write = function write (string, offset, length, encoding) { // Buffer#write(string) if (offset === undefined) { encoding = 'utf8' length = this.length offset = 0 // Buffer#write(string, encoding) } else if (length === undefined && typeof offset === 'string') { encoding = offset length = this.length offset = 0 // Buffer#write(string, offset[, length][, encoding]) } else if (isFinite(offset)) { offset = offset >>> 0 if (isFinite(length)) { length = length >>> 0 if (encoding === undefined) encoding = 'utf8' } else { encoding = length length = undefined } } else { throw new Error( 'Buffer.write(string, encoding, offset[, length]) is no longer supported' ) } var remaining = this.length - offset if (length === undefined || length > remaining) length = remaining if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { throw new RangeError('Attempt to write outside buffer bounds') } if (!encoding) encoding = 'utf8' var loweredCase = false for (;;) { switch (encoding) { case 'hex': return hexWrite(this, string, offset, length) case 'utf8': case 'utf-8': return utf8Write(this, string, offset, length) case 'ascii': return asciiWrite(this, string, offset, length) case 'latin1': case 'binary': return latin1Write(this, string, offset, length) case 'base64': // Warning: maxLength not taken into account in base64Write return base64Write(this, string, offset, length) case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return ucs2Write(this, string, offset, length) default: if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) encoding = ('' + encoding).toLowerCase() loweredCase = true } } } Buffer.prototype.toJSON = function toJSON () { return { type: 'Buffer', data: Array.prototype.slice.call(this._arr || this, 0) } } function base64Slice (buf, start, end) { if (start === 0 && end === buf.length) { return base64.fromByteArray(buf) } else { return base64.fromByteArray(buf.slice(start, end)) } } function utf8Slice (buf, start, end) { end = Math.min(buf.length, end) var res = [] var i = start while (i < end) { var firstByte = buf[i] var codePoint = null var bytesPerSequence = (firstByte > 0xEF) ? 4 : (firstByte > 0xDF) ? 3 : (firstByte > 0xBF) ? 2 : 1 if (i + bytesPerSequence <= end) { var secondByte, thirdByte, fourthByte, tempCodePoint switch (bytesPerSequence) { case 1: if (firstByte < 0x80) { codePoint = firstByte } break case 2: secondByte = buf[i + 1] if ((secondByte & 0xC0) === 0x80) { tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) if (tempCodePoint > 0x7F) { codePoint = tempCodePoint } } break case 3: secondByte = buf[i + 1] thirdByte = buf[i + 2] if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { codePoint = tempCodePoint } } break case 4: secondByte = buf[i + 1] thirdByte = buf[i + 2] fourthByte = buf[i + 3] if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { codePoint = tempCodePoint } } } } if (codePoint === null) { // we did not generate a valid codePoint so insert a // replacement char (U+FFFD) and advance only 1 byte codePoint = 0xFFFD bytesPerSequence = 1 } else if (codePoint > 0xFFFF) { // encode to utf16 (surrogate pair dance) codePoint -= 0x10000 res.push(codePoint >>> 10 & 0x3FF | 0xD800) codePoint = 0xDC00 | codePoint & 0x3FF } res.push(codePoint) i += bytesPerSequence } return decodeCodePointsArray(res) } // Based on http://stackoverflow.com/a/22747272/680742, the browser with // the lowest limit is Chrome, with 0x10000 args. // We go 1 magnitude less, for safety var MAX_ARGUMENTS_LENGTH = 0x1000 function decodeCodePointsArray (codePoints) { var len = codePoints.length if (len <= MAX_ARGUMENTS_LENGTH) { return String.fromCharCode.apply(String, codePoints) // avoid extra slice() } // Decode in chunks to avoid "call stack size exceeded". var res = '' var i = 0 while (i < len) { res += String.fromCharCode.apply( String, codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) ) } return res } function asciiSlice (buf, start, end) { var ret = '' end = Math.min(buf.length, end) for (var i = start; i < end; ++i) { ret += String.fromCharCode(buf[i] & 0x7F) } return ret } function latin1Slice (buf, start, end) { var ret = '' end = Math.min(buf.length, end) for (var i = start; i < end; ++i) { ret += String.fromCharCode(buf[i]) } return ret } function hexSlice (buf, start, end) { var len = buf.length if (!start || start < 0) start = 0 if (!end || end < 0 || end > len) end = len var out = '' for (var i = start; i < end; ++i) { out += toHex(buf[i]) } return out } function utf16leSlice (buf, start, end) { var bytes = buf.slice(start, end) var res = '' for (var i = 0; i < bytes.length; i += 2) { res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)) } return res } Buffer.prototype.slice = function slice (start, end) { var len = this.length start = ~~start end = end === undefined ? len : ~~end if (start < 0) { start += len if (start < 0) start = 0 } else if (start > len) { start = len } if (end < 0) { end += len if (end < 0) end = 0 } else if (end > len) { end = len } if (end < start) end = start var newBuf = this.subarray(start, end) // Return an augmented `Uint8Array` instance Object.setPrototypeOf(newBuf, Buffer.prototype) return newBuf } /* * Need to make sure that buffer isn't trying to write out of bounds. */ function checkOffset (offset, ext, length) { if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') } Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) var val = this[offset] var mul = 1 var i = 0 while (++i < byteLength && (mul *= 0x100)) { val += this[offset + i] * mul } return val } Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) { checkOffset(offset, byteLength, this.length) } var val = this[offset + --byteLength] var mul = 1 while (byteLength > 0 && (mul *= 0x100)) { val += this[offset + --byteLength] * mul } return val } Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 1, this.length) return this[offset] } Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 2, this.length) return this[offset] | (this[offset + 1] << 8) } Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 2, this.length) return (this[offset] << 8) | this[offset + 1] } Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 4, this.length) return ((this[offset]) | (this[offset + 1] << 8) | (this[offset + 2] << 16)) + (this[offset + 3] * 0x1000000) } Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 4, this.length) return (this[offset] * 0x1000000) + ((this[offset + 1] << 16) | (this[offset + 2] << 8) | this[offset + 3]) } Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) var val = this[offset] var mul = 1 var i = 0 while (++i < byteLength && (mul *= 0x100)) { val += this[offset + i] * mul } mul *= 0x80 if (val >= mul) val -= Math.pow(2, 8 * byteLength) return val } Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) var i = byteLength var mul = 1 var val = this[offset + --i] while (i > 0 && (mul *= 0x100)) { val += this[offset + --i] * mul } mul *= 0x80 if (val >= mul) val -= Math.pow(2, 8 * byteLength) return val } Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 1, this.length) if (!(this[offset] & 0x80)) return (this[offset]) return ((0xff - this[offset] + 1) * -1) } Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 2, this.length) var val = this[offset] | (this[offset + 1] << 8) return (val & 0x8000) ? val | 0xFFFF0000 : val } Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 2, this.length) var val = this[offset + 1] | (this[offset] << 8) return (val & 0x8000) ? val | 0xFFFF0000 : val } Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 4, this.length) return (this[offset]) | (this[offset + 1] << 8) | (this[offset + 2] << 16) | (this[offset + 3] << 24) } Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 4, this.length) return (this[offset] << 24) | (this[offset + 1] << 16) | (this[offset + 2] << 8) | (this[offset + 3]) } Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 4, this.length) return ieee754.read(this, offset, true, 23, 4) } Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 4, this.length) return ieee754.read(this, offset, false, 23, 4) } Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 8, this.length) return ieee754.read(this, offset, true, 52, 8) } Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 8, this.length) return ieee754.read(this, offset, false, 52, 8) } function checkInt (buf, value, offset, ext, max, min) { if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') if (offset + ext > buf.length) throw new RangeError('Index out of range') } Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) { var maxBytes = Math.pow(2, 8 * byteLength) - 1 checkInt(this, value, offset, byteLength, maxBytes, 0) } var mul = 1 var i = 0 this[offset] = value & 0xFF while (++i < byteLength && (mul *= 0x100)) { this[offset + i] = (value / mul) & 0xFF } return offset + byteLength } Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) { var maxBytes = Math.pow(2, 8 * byteLength) - 1 checkInt(this, value, offset, byteLength, maxBytes, 0) } var i = byteLength - 1 var mul = 1 this[offset + i] = value & 0xFF while (--i >= 0 && (mul *= 0x100)) { this[offset + i] = (value / mul) & 0xFF } return offset + byteLength } Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) this[offset] = (value & 0xff) return offset + 1 } Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) this[offset] = (value & 0xff) this[offset + 1] = (value >>> 8) return offset + 2 } Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) this[offset] = (value >>> 8) this[offset + 1] = (value & 0xff) return offset + 2 } Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) this[offset + 3] = (value >>> 24) this[offset + 2] = (value >>> 16) this[offset + 1] = (value >>> 8) this[offset] = (value & 0xff) return offset + 4 } Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) this[offset] = (value >>> 24) this[offset + 1] = (value >>> 16) this[offset + 2] = (value >>> 8) this[offset + 3] = (value & 0xff) return offset + 4 } Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) { var limit = Math.pow(2, (8 * byteLength) - 1) checkInt(this, value, offset, byteLength, limit - 1, -limit) } var i = 0 var mul = 1 var sub = 0 this[offset] = value & 0xFF while (++i < byteLength && (mul *= 0x100)) { if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { sub = 1 } this[offset + i] = ((value / mul) >> 0) - sub & 0xFF } return offset + byteLength } Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) { var limit = Math.pow(2, (8 * byteLength) - 1) checkInt(this, value, offset, byteLength, limit - 1, -limit) } var i = byteLength - 1 var mul = 1 var sub = 0 this[offset + i] = value & 0xFF while (--i >= 0 && (mul *= 0x100)) { if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { sub = 1 } this[offset + i] = ((value / mul) >> 0) - sub & 0xFF } return offset + byteLength } Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) if (value < 0) value = 0xff + value + 1 this[offset] = (value & 0xff) return offset + 1 } Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) this[offset] = (value & 0xff) this[offset + 1] = (value >>> 8) return offset + 2 } Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) this[offset] = (value >>> 8) this[offset + 1] = (value & 0xff) return offset + 2 } Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) this[offset] = (value & 0xff) this[offset + 1] = (value >>> 8) this[offset + 2] = (value >>> 16) this[offset + 3] = (value >>> 24) return offset + 4 } Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) if (value < 0) value = 0xffffffff + value + 1 this[offset] = (value >>> 24) this[offset + 1] = (value >>> 16) this[offset + 2] = (value >>> 8) this[offset + 3] = (value & 0xff) return offset + 4 } function checkIEEE754 (buf, value, offset, ext, max, min) { if (offset + ext > buf.length) throw new RangeError('Index out of range') if (offset < 0) throw new RangeError('Index out of range') } function writeFloat (buf, value, offset, littleEndian, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) { checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) } ieee754.write(buf, value, offset, littleEndian, 23, 4) return offset + 4 } Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { return writeFloat(this, value, offset, true, noAssert) } Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { return writeFloat(this, value, offset, false, noAssert) } function writeDouble (buf, value, offset, littleEndian, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) { checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) } ieee754.write(buf, value, offset, littleEndian, 52, 8) return offset + 8 } Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { return writeDouble(this, value, offset, true, noAssert) } Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { return writeDouble(this, value, offset, false, noAssert) } // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) Buffer.prototype.copy = function copy (target, targetStart, start, end) { if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer') if (!start) start = 0 if (!end && end !== 0) end = this.length if (targetStart >= target.length) targetStart = target.length if (!targetStart) targetStart = 0 if (end > 0 && end < start) end = start // Copy 0 bytes; we're done if (end === start) return 0 if (target.length === 0 || this.length === 0) return 0 // Fatal error conditions if (targetStart < 0) { throw new RangeError('targetStart out of bounds') } if (start < 0 || start >= this.length) throw new RangeError('Index out of range') if (end < 0) throw new RangeError('sourceEnd out of bounds') // Are we oob? if (end > this.length) end = this.length if (target.length - targetStart < end - start) { end = target.length - targetStart + start } var len = end - start if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') { // Use built-in when available, missing from IE11 this.copyWithin(targetStart, start, end) } else if (this === target && start < targetStart && targetStart < end) { // descending copy from end for (var i = len - 1; i >= 0; --i) { target[i + targetStart] = this[i + start] } } else { Uint8Array.prototype.set.call( target, this.subarray(start, end), targetStart ) } return len } // Usage: // buffer.fill(number[, offset[, end]]) // buffer.fill(buffer[, offset[, end]]) // buffer.fill(string[, offset[, end]][, encoding]) Buffer.prototype.fill = function fill (val, start, end, encoding) { // Handle string cases: if (typeof val === 'string') { if (typeof start === 'string') { encoding = start start = 0 end = this.length } else if (typeof end === 'string') { encoding = end end = this.length } if (encoding !== undefined && typeof encoding !== 'string') { throw new TypeError('encoding must be a string') } if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { throw new TypeError('Unknown encoding: ' + encoding) } if (val.length === 1) { var code = val.charCodeAt(0) if ((encoding === 'utf8' && code < 128) || encoding === 'latin1') { // Fast path: If `val` fits into a single byte, use that numeric value. val = code } } } else if (typeof val === 'number') { val = val & 255 } else if (typeof val === 'boolean') { val = Number(val) } // Invalid ranges are not set to a default, so can range check early. if (start < 0 || this.length < start || this.length < end) { throw new RangeError('Out of range index') } if (end <= start) { return this } start = start >>> 0 end = end === undefined ? this.length : end >>> 0 if (!val) val = 0 var i if (typeof val === 'number') { for (i = start; i < end; ++i) { this[i] = val } } else { var bytes = Buffer.isBuffer(val) ? val : Buffer.from(val, encoding) var len = bytes.length if (len === 0) { throw new TypeError('The value "' + val + '" is invalid for argument "value"') } for (i = 0; i < end - start; ++i) { this[i + start] = bytes[i % len] } } return this } // HELPER FUNCTIONS // ================ var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g function base64clean (str) { // Node takes equal signs as end of the Base64 encoding str = str.split('=')[0] // Node strips out invalid characters like \n and \t from the string, base64-js does not str = str.trim().replace(INVALID_BASE64_RE, '') // Node converts strings with length < 2 to '' if (str.length < 2) return '' // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not while (str.length % 4 !== 0) { str = str + '=' } return str } function toHex (n) { if (n < 16) return '0' + n.toString(16) return n.toString(16) } function utf8ToBytes (string, units) { units = units || Infinity var codePoint var length = string.length var leadSurrogate = null var bytes = [] for (var i = 0; i < length; ++i) { codePoint = string.charCodeAt(i) // is surrogate component if (codePoint > 0xD7FF && codePoint < 0xE000) { // last char was a lead if (!leadSurrogate) { // no lead yet if (codePoint > 0xDBFF) { // unexpected trail if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) continue } else if (i + 1 === length) { // unpaired lead if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) continue } // valid lead leadSurrogate = codePoint continue } // 2 leads in a row if (codePoint < 0xDC00) { if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) leadSurrogate = codePoint continue } // valid surrogate pair codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 } else if (leadSurrogate) { // valid bmp char, but last char was a lead if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) } leadSurrogate = null // encode utf8 if (codePoint < 0x80) { if ((units -= 1) < 0) break bytes.push(codePoint) } else if (codePoint < 0x800) { if ((units -= 2) < 0) break bytes.push( codePoint >> 0x6 | 0xC0, codePoint & 0x3F | 0x80 ) } else if (codePoint < 0x10000) { if ((units -= 3) < 0) break bytes.push( codePoint >> 0xC | 0xE0, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80 ) } else if (codePoint < 0x110000) { if ((units -= 4) < 0) break bytes.push( codePoint >> 0x12 | 0xF0, codePoint >> 0xC & 0x3F | 0x80, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80 ) } else { throw new Error('Invalid code point') } } return bytes } function asciiToBytes (str) { var byteArray = [] for (var i = 0; i < str.length; ++i) { // Node's code seems to be doing this and not & 0x7F.. byteArray.push(str.charCodeAt(i) & 0xFF) } return byteArray } function utf16leToBytes (str, units) { var c, hi, lo var byteArray = [] for (var i = 0; i < str.length; ++i) { if ((units -= 2) < 0) break c = str.charCodeAt(i) hi = c >> 8 lo = c % 256 byteArray.push(lo) byteArray.push(hi) } return byteArray } function base64ToBytes (str) { return base64.toByteArray(base64clean(str)) } function blitBuffer (src, dst, offset, length) { for (var i = 0; i < length; ++i) { if ((i + offset >= dst.length) || (i >= src.length)) break dst[i + offset] = src[i] } return i } // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass // the `instanceof` check but they should be treated as of that type. // See: https://github.com/feross/buffer/issues/166 function isInstance (obj, type) { return obj instanceof type || (obj != null && obj.constructor != null && obj.constructor.name != null && obj.constructor.name === type.name) } function numberIsNaN (obj) { // For IE11 support return obj !== obj // eslint-disable-line no-self-compare } buffer-5.4.2/package.json000066400000000000000000000043371353132250700152710ustar00rootroot00000000000000{ "name": "buffer", "description": "Node.js Buffer API, for the browser", "version": "5.4.2", "author": { "name": "Feross Aboukhadijeh", "email": "feross@feross.org", "url": "http://feross.org" }, "bugs": { "url": "https://github.com/feross/buffer/issues" }, "contributors": [ "Romain Beauxis ", "James Halliday " ], "dependencies": { "base64-js": "^1.0.2", "ieee754": "^1.1.4" }, "devDependencies": { "airtap": "^2.0.3", "benchmark": "^2.0.0", "browserify": "^16.1.0", "concat-stream": "^2.0.0", "hyperquest": "^2.0.0", "is-buffer": "^2.0.0", "is-nan": "^1.0.1", "split": "^1.0.0", "standard": "*", "tape": "^4.0.0", "through2": "^3.0.1", "uglify-js": "^3.4.5" }, "homepage": "https://github.com/feross/buffer", "jspm": { "map": { "./index.js": { "node": "@node/buffer" } } }, "keywords": [ "arraybuffer", "browser", "browserify", "buffer", "compatible", "dataview", "uint8array" ], "license": "MIT", "main": "index.js", "types": "index.d.ts", "repository": { "type": "git", "url": "git://github.com/feross/buffer.git" }, "scripts": { "perf": "browserify --debug perf/bracket-notation.js > perf/bundle.js && open perf/index.html", "perf-node": "node perf/bracket-notation.js && node perf/concat.js && node perf/copy-big.js && node perf/copy.js && node perf/new-big.js && node perf/new.js && node perf/readDoubleBE.js && node perf/readFloatBE.js && node perf/readUInt32LE.js && node perf/slice.js && node perf/writeFloatBE.js", "size": "browserify -r ./ | uglifyjs -c -m | gzip | wc -c", "test": "standard && node ./bin/test.js", "test-browser-es5": "airtap -- test/*.js", "test-browser-es5-local": "airtap --local -- test/*.js", "test-browser-es6": "airtap -- test/*.js test/node/*.js", "test-browser-es6-local": "airtap --local -- test/*.js test/node/*.js", "test-node": "tape test/*.js test/node/*.js", "update-authors": "./bin/update-authors.sh" }, "standard": { "ignore": [ "test/node/**/*.js", "test/common.js", "test/_polyfill.js", "perf/**/*.js" ] } } buffer-5.4.2/perf/000077500000000000000000000000001353132250700137305ustar00rootroot00000000000000buffer-5.4.2/perf/bracket-notation.js000066400000000000000000000012571353132250700175370ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 50 var browserBuffer = new BrowserBuffer(LENGTH) var typedarray = new Uint8Array(LENGTH) var nodeBuffer = new Buffer(LENGTH) suite .add('BrowserBuffer#bracket-notation', function () { for (var i = 0; i < LENGTH; i++) { browserBuffer[i] = i + 97 } }) .add('Uint8Array#bracket-notation', function () { for (var i = 0; i < LENGTH; i++) { typedarray[i] = i + 97 } }) if (!process.browser) suite .add('NodeBuffer#bracket-notation', function () { for (var i = 0; i < LENGTH; i++) { nodeBuffer[i] = i + 97 } }) buffer-5.4.2/perf/concat.js000066400000000000000000000017241353132250700155410ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 160 var browserBuffer = new BrowserBuffer(LENGTH) var browserBuffer2 = new BrowserBuffer(LENGTH) var typedarray = new Uint8Array(LENGTH) var typedarray2 = new Uint8Array(LENGTH) var nodeBuffer = new Buffer(LENGTH) var nodeBuffer2 = new Buffer(LENGTH) ;[browserBuffer, browserBuffer2, typedarray, typedarray2, nodeBuffer, nodeBuffer2].forEach(function (buf) { for (var i = 0; i < LENGTH; i++) { buf[i] = i + 97 } }) suite .add('BrowserBuffer#concat', function () { var x = BrowserBuffer.concat([browserBuffer, browserBuffer2], LENGTH * 2) }) .add('Uint8Array#concat', function () { var x = new Uint8Array(LENGTH * 2) x.set(typedarray, 0) x.set(typedarray2, typedarray.length) }) if (!process.browser) suite .add('NodeBuffer#concat', function () { var x = Buffer.concat([nodeBuffer, nodeBuffer2], LENGTH * 2) }) buffer-5.4.2/perf/copy-big.js000066400000000000000000000013371353132250700160030ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 16000 var browserSubject = new BrowserBuffer(LENGTH) var typedarraySubject = new Uint8Array(LENGTH) var nodeSubject = new Buffer(LENGTH) var browserTarget = new BrowserBuffer(LENGTH) var typedarrayTarget = new Uint8Array(LENGTH) var nodeTarget = new Buffer(LENGTH) suite .add('BrowserBuffer#copy(' + LENGTH + ')', function () { browserSubject.copy(browserTarget) }) .add('Uint8Array#copy(' + LENGTH + ')', function () { typedarrayTarget.set(typedarraySubject, 0) }) if (!process.browser) suite .add('NodeBuffer#copy(' + LENGTH + ')', function () { nodeSubject.copy(nodeTarget) }) buffer-5.4.2/perf/copy.js000066400000000000000000000013341353132250700152410ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 16 var browserSubject = new BrowserBuffer(LENGTH) var typedarraySubject = new Uint8Array(LENGTH) var nodeSubject = new Buffer(LENGTH) var browserTarget = new BrowserBuffer(LENGTH) var typedarrayTarget = new Uint8Array(LENGTH) var nodeTarget = new Buffer(LENGTH) suite .add('BrowserBuffer#copy(' + LENGTH + ')', function () { browserSubject.copy(browserTarget) }) .add('Uint8Array#copy(' + LENGTH + ')', function () { typedarrayTarget.set(typedarraySubject, 0) }) if (!process.browser) suite .add('NodeBuffer#copy(' + LENGTH + ')', function () { nodeSubject.copy(nodeTarget) }) buffer-5.4.2/perf/index.html000066400000000000000000000000421353132250700157210ustar00rootroot00000000000000 buffer-5.4.2/perf/new-big.js000066400000000000000000000007161353132250700156220ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 16000 suite .add('BrowserBuffer#new(' + LENGTH + ')', function () { var buf = new BrowserBuffer(LENGTH) }) .add('Uint8Array#new(' + LENGTH + ')', function () { var buf = new Uint8Array(LENGTH) }) if (!process.browser) suite .add('NodeBuffer#new(' + LENGTH + ')', function () { var buf = new Buffer(LENGTH) }) buffer-5.4.2/perf/new.js000066400000000000000000000007131353132250700150600ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 16 suite .add('BrowserBuffer#new(' + LENGTH + ')', function () { var buf = new BrowserBuffer(LENGTH) }) .add('Uint8Array#new(' + LENGTH + ')', function () { var buf = new Uint8Array(LENGTH) }) if (!process.browser) suite .add('NodeBuffer#new(' + LENGTH + ')', function () { var buf = new Buffer(LENGTH) }) buffer-5.4.2/perf/readDoubleBE.js000066400000000000000000000017451353132250700165520ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 160 var browserBuffer = new BrowserBuffer(LENGTH * 8) var typedarray = new Uint8Array(LENGTH * 8) var dataview = new DataView(typedarray.buffer) var nodeBuffer = new Buffer(LENGTH * 8) ;[browserBuffer, nodeBuffer].forEach(function (buf) { for (var i = 0; i < LENGTH; i++) { buf.writeDoubleBE(97.1919 + i, i * 8) } }) for (var i = 0; i < LENGTH; i++) { dataview.setFloat64(i * 8, 97.1919 + i) } suite .add('BrowserBuffer#readDoubleBE', function () { for (var i = 0; i < LENGTH; i++) { var x = browserBuffer.readDoubleBE(i * 8) } }) .add('DataView#getFloat64', function () { for (var i = 0; i < LENGTH; i++) { var x = dataview.getFloat64(i * 8) } }) if (!process.browser) suite .add('NodeBuffer#readDoubleBE', function () { for (var i = 0; i < LENGTH; i++) { var x = nodeBuffer.readDoubleBE(i * 8) } }) buffer-5.4.2/perf/readFloatBE.js000066400000000000000000000017401353132250700164000ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 160 var browserBuffer = new BrowserBuffer(LENGTH * 4) var typedarray = new Uint8Array(LENGTH * 4) var dataview = new DataView(typedarray.buffer) var nodeBuffer = new Buffer(LENGTH * 4) ;[browserBuffer, nodeBuffer].forEach(function (buf) { for (var i = 0; i < LENGTH; i++) { buf.writeFloatBE(97.1919 + i, i * 4) } }) for (var i = 0; i < LENGTH; i++) { dataview.setFloat32(i * 4, 97.1919 + i) } suite .add('BrowserBuffer#readFloatBE', function () { for (var i = 0; i < LENGTH; i++) { var x = browserBuffer.readFloatBE(i * 4) } }) .add('DataView#getFloat32', function () { for (var i = 0; i < LENGTH; i++) { var x = dataview.getFloat32(i * 4) } }) if (!process.browser) suite .add('NodeBuffer#readFloatBE', function () { for (var i = 0; i < LENGTH; i++) { var x = nodeBuffer.readFloatBE(i * 4) } }) buffer-5.4.2/perf/readUInt32LE.js000066400000000000000000000017421353132250700163730ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 160 var browserBuffer = new BrowserBuffer(LENGTH * 4) var typedarray = new Uint8Array(LENGTH * 4) var dataview = new DataView(typedarray.buffer) var nodeBuffer = new Buffer(LENGTH * 4) ;[browserBuffer, nodeBuffer].forEach(function (buf) { for (var i = 0; i < LENGTH; i++) { buf.writeUInt32LE(7000 + i, i * 4) } }) for (var i = 0; i < LENGTH; i++) { dataview.setUint32(i * 4, 7000 + i) } suite .add('BrowserBuffer#readUInt32LE', function () { for (var i = 0; i < LENGTH; i++) { var x = browserBuffer.readUInt32LE(i * 4) } }) .add('DataView#getUint32', function () { for (var i = 0; i < LENGTH; i++) { var x = dataview.getUint32(i * 4, true) } }) if (!process.browser) suite .add('NodeBuffer#readUInt32LE', function () { for (var i = 0; i < LENGTH; i++) { var x = nodeBuffer.readUInt32LE(i * 4) } }) buffer-5.4.2/perf/readUtf8.js000066400000000000000000000030521353132250700157500ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() // 256 random bytes var array = [ 152, 130, 206, 23, 243, 238, 197, 44, 27, 86, 208, 36, 163, 184, 164, 21, 94, 242, 178, 46, 25, 26, 253, 178, 72, 147, 207, 112, 236, 68, 179, 190, 29, 83, 239, 147, 125, 55, 143, 19, 157, 68, 157, 58, 212, 224, 150, 39, 128, 24, 94, 225, 120, 121, 75, 192, 112, 19, 184, 142, 203, 36, 43, 85, 26, 147, 227, 139, 242, 186, 57, 78, 11, 102, 136, 117, 180, 210, 241, 92, 3, 215, 54, 167, 249, 1, 44, 225, 146, 86, 2, 42, 68, 21, 47, 238, 204, 153, 216, 252, 183, 66, 222, 255, 15, 202, 16, 51, 134, 1, 17, 19, 209, 76, 238, 38, 76, 19, 7, 103, 249, 5, 107, 137, 64, 62, 170, 57, 16, 85, 179, 193, 97, 86, 166, 196, 36, 148, 138, 193, 210, 69, 187, 38, 242, 97, 195, 219, 252, 244, 38, 1, 197, 18, 31, 246, 53, 47, 134, 52, 105, 72, 43, 239, 128, 203, 73, 93, 199, 75, 222, 220, 166, 34, 63, 236, 11, 212, 76, 243, 171, 110, 78, 39, 205, 204, 6, 177, 233, 212, 243, 0, 33, 41, 122, 118, 92, 252, 0, 157, 108, 120, 70, 137, 100, 223, 243, 171, 232, 66, 126, 111, 142, 33, 3, 39, 117, 27, 107, 54, 1, 217, 227, 132, 13, 166, 3, 73, 53, 127, 225, 236, 134, 219, 98, 214, 125, 148, 24, 64, 142, 111, 231, 194, 42, 150, 185, 10, 182, 163, 244, 19, 4, 59, 135, 16 ] var browserBuffer = new BrowserBuffer(array) var nodeBuffer = new Buffer(array) suite .add('BrowserBuffer#readUtf8', function () { browserBuffer.toString() }) if (!process.browser) suite .add('NodeBuffer#readUtf8', function () { nodeBuffer.toString() }) buffer-5.4.2/perf/slice.js000066400000000000000000000010301353132250700153570ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 160 var browserBuffer = new BrowserBuffer(LENGTH) var typedarray = new Uint8Array(LENGTH) var nodeBuffer = new Buffer(LENGTH) suite .add('BrowserBuffer#slice', function () { var x = browserBuffer.slice(4) }) .add('Uint8Array#subarray', function () { var x = typedarray.subarray(4) }) if (!process.browser) suite .add('NodeBuffer#slice', function () { var x = nodeBuffer.slice(4) }) buffer-5.4.2/perf/util.js000066400000000000000000000007621353132250700152500ustar00rootroot00000000000000var benchmark = require('benchmark') exports.suite = function () { var suite = new benchmark.Suite() process.nextTick(function () { suite .on('error', function (event) { console.error(event.target.error.stack) }) .on('cycle', function (event) { console.log(String(event.target)) }) .on('complete', function () { console.log('Fastest is ' + this.filter('fastest').map('name')) }) .run({ async: true }) }) return suite } buffer-5.4.2/perf/writeFloatBE.js000066400000000000000000000014271353132250700166210ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 160 var browserBuffer = new BrowserBuffer(LENGTH * 4) var typedarray = new Uint8Array(LENGTH * 4) var dataview = new DataView(typedarray.buffer) var nodeBuffer = new Buffer(LENGTH * 4) suite .add('BrowserBuffer#writeFloatBE', function () { for (var i = 0; i < LENGTH; i++) { browserBuffer.writeFloatBE(97.1919 + i, i * 4) } }) .add('DataView#setFloat32', function () { for (var i = 0; i < LENGTH; i++) { dataview.setFloat32(i * 4, 97.1919 + i) } }) if (!process.browser) suite .add('NodeBuffer#writeFloatBE', function () { for (var i = 0; i < LENGTH; i++) { nodeBuffer.writeFloatBE(97.1919 + i, i * 4) } }) buffer-5.4.2/perf/writeUtf8.js000066400000000000000000000012421353132250700161660ustar00rootroot00000000000000var BrowserBuffer = require('../').Buffer // (this module) var util = require('./util') var suite = util.suite() var LENGTH = 9 var singleByte = 'abcdefghi' var multiByte = '\u0610' + '\u6100' + '\uD944\uDC00' var browserBuffer = new BrowserBuffer(LENGTH) var nodeBuffer = new Buffer(LENGTH) suite .add('BrowserBuffer#singleByte', function () { browserBuffer.write(singleByte) }) .add('BrowserBuffer#multiByte', function () { browserBuffer.write(multiByte) }) if (!process.browser) suite .add('NodeBuffer#singleByte', function () { nodeBuffer.write(singleByte) }) .add('NodeBuffer#multiByte', function () { nodeBuffer.write(multiByte) }) buffer-5.4.2/test/000077500000000000000000000000001353132250700137535ustar00rootroot00000000000000buffer-5.4.2/test/base64.js000066400000000000000000000032731353132250700154020ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('base64: ignore whitespace', function (t) { var text = '\n YW9ldQ== ' var buf = new B(text, 'base64') t.equal(buf.toString(), 'aoeu') t.end() }) test('base64: strings without padding', function (t) { t.equal((new B('YW9ldQ', 'base64').toString()), 'aoeu') t.end() }) test('base64: newline in utf8 -- should not be an issue', function (t) { t.equal( new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK', 'base64').toString('utf8'), '---\ntitle: Three dashes marks the spot\ntags:\n' ) t.end() }) test('base64: newline in base64 -- should get stripped', function (t) { t.equal( new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\nICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt', 'base64').toString('utf8'), '---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-' ) t.end() }) test('base64: tab characters in base64 - should get stripped', function (t) { t.equal( new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\t\t\t\tICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt', 'base64').toString('utf8'), '---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-' ) t.end() }) test('base64: invalid non-alphanumeric characters -- should be stripped', function (t) { t.equal( new B('!"#$%&\'()*,.:;<=>?@[\\]^`{|}~', 'base64').toString('utf8'), '' ) t.end() }) test('base64: high byte', function (t) { var highByte = B.from([128]) t.deepEqual( B.alloc(1, highByte.toString('base64'), 'base64'), highByte ) t.end() }) buffer-5.4.2/test/basic.js000066400000000000000000000026151353132250700153760ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('instanceof Buffer', function (t) { var buf = new B([1, 2]) t.ok(buf instanceof B) t.end() }) test('convert to Uint8Array in modern browsers', function (t) { var buf = new B([1, 2]) var uint8array = new Uint8Array(buf.buffer) t.ok(uint8array instanceof Uint8Array) t.equal(uint8array[0], 1) t.equal(uint8array[1], 2) t.end() }) test('indexes from a string', function (t) { var buf = new B('abc') t.equal(buf[0], 97) t.equal(buf[1], 98) t.equal(buf[2], 99) t.end() }) test('indexes from an array', function (t) { var buf = new B([97, 98, 99]) t.equal(buf[0], 97) t.equal(buf[1], 98) t.equal(buf[2], 99) t.end() }) test('setting index value should modify buffer contents', function (t) { var buf = new B([97, 98, 99]) t.equal(buf[2], 99) t.equal(buf.toString(), 'abc') buf[2] += 10 t.equal(buf[2], 109) t.equal(buf.toString(), 'abm') t.end() }) test('storing negative number should cast to unsigned', function (t) { var buf = new B(1) buf[0] = -3 t.equal(buf[0], 253) buf = new B(1) buf.writeInt8(-3, 0) t.equal(buf[0], 253) t.end() }) test('test that memory is copied from array-like', function (t) { var u = new Uint8Array(4) var b = new B(u) b[0] = 1 b[1] = 2 b[2] = 3 b[3] = 4 t.equal(u[0], 0) t.equal(u[1], 0) t.equal(u[2], 0) t.equal(u[3], 0) t.end() }) buffer-5.4.2/test/compare.js000066400000000000000000000021571353132250700157440ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('buffer.compare', function (t) { var b = new B(1).fill('a') var c = new B(1).fill('c') var d = new B(2).fill('aa') t.equal(b.compare(c), -1) t.equal(c.compare(d), 1) t.equal(d.compare(b), 1) t.equal(b.compare(d), -1) // static method t.equal(B.compare(b, c), -1) t.equal(B.compare(c, d), 1) t.equal(B.compare(d, b), 1) t.equal(B.compare(b, d), -1) t.end() }) test('buffer.compare argument validation', function (t) { t.throws(function () { var b = new B(1) B.compare(b, 'abc') }) t.throws(function () { var b = new B(1) B.compare('abc', b) }) t.throws(function () { var b = new B(1) b.compare('abc') }) t.end() }) test('buffer.equals', function (t) { var b = new B(5).fill('abcdf') var c = new B(5).fill('abcdf') var d = new B(5).fill('abcde') var e = new B(6).fill('abcdef') t.ok(b.equals(c)) t.ok(!c.equals(d)) t.ok(!d.equals(e)) t.end() }) test('buffer.equals argument validation', function (t) { t.throws(function () { var b = new B(1) b.equals('abc') }) t.end() }) buffer-5.4.2/test/constructor.js000066400000000000000000000102561353132250700167020ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('new buffer from array', function (t) { t.equal( new B([1, 2, 3]).toString(), '\u0001\u0002\u0003' ) t.end() }) test('new buffer from array w/ negatives', function (t) { t.equal( new B([-1, -2, -3]).toString('hex'), 'fffefd' ) t.end() }) test('new buffer from array with mixed signed input', function (t) { t.equal( new B([-255, 255, -128, 128, 512, -512, 511, -511]).toString('hex'), '01ff80800000ff01' ) t.end() }) test('new buffer from string', function (t) { t.equal( new B('hey', 'utf8').toString(), 'hey' ) t.end() }) test('new buffer from buffer', function (t) { var b1 = new B('asdf') var b2 = new B(b1) t.equal(b1.toString('hex'), b2.toString('hex')) t.end() }) test('new buffer from ArrayBuffer', function (t) { if (typeof ArrayBuffer !== 'undefined') { var arraybuffer = new Uint8Array([0, 1, 2, 3]).buffer var b = new B(arraybuffer) t.equal(b.length, 4) t.equal(b[0], 0) t.equal(b[1], 1) t.equal(b[2], 2) t.equal(b[3], 3) t.equal(b[4], undefined) } t.end() }) test('new buffer from ArrayBuffer, shares memory', function (t) { var u = new Uint8Array([0, 1, 2, 3]) var arraybuffer = u.buffer var b = new B(arraybuffer) t.equal(b.length, 4) t.equal(b[0], 0) t.equal(b[1], 1) t.equal(b[2], 2) t.equal(b[3], 3) t.equal(b[4], undefined) // changing the Uint8Array (and thus the ArrayBuffer), changes the Buffer u[0] = 10 t.equal(b[0], 10) u[1] = 11 t.equal(b[1], 11) u[2] = 12 t.equal(b[2], 12) u[3] = 13 t.equal(b[3], 13) t.end() }) test('new buffer from Uint8Array', function (t) { if (typeof Uint8Array !== 'undefined') { var b1 = new Uint8Array([0, 1, 2, 3]) var b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) t.equal(b1[2], 2) t.equal(b1[3], 3) t.equal(b1[4], undefined) } t.end() }) test('new buffer from Uint16Array', function (t) { if (typeof Uint16Array !== 'undefined') { var b1 = new Uint16Array([0, 1, 2, 3]) var b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) t.equal(b1[2], 2) t.equal(b1[3], 3) t.equal(b1[4], undefined) } t.end() }) test('new buffer from Uint32Array', function (t) { if (typeof Uint32Array !== 'undefined') { var b1 = new Uint32Array([0, 1, 2, 3]) var b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) t.equal(b1[2], 2) t.equal(b1[3], 3) t.equal(b1[4], undefined) } t.end() }) test('new buffer from Int16Array', function (t) { if (typeof Int16Array !== 'undefined') { var b1 = new Int16Array([0, 1, 2, 3]) var b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) t.equal(b1[2], 2) t.equal(b1[3], 3) t.equal(b1[4], undefined) } t.end() }) test('new buffer from Int32Array', function (t) { if (typeof Int32Array !== 'undefined') { var b1 = new Int32Array([0, 1, 2, 3]) var b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) t.equal(b1[2], 2) t.equal(b1[3], 3) t.equal(b1[4], undefined) } t.end() }) test('new buffer from Float32Array', function (t) { if (typeof Float32Array !== 'undefined') { var b1 = new Float32Array([0, 1, 2, 3]) var b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) t.equal(b1[2], 2) t.equal(b1[3], 3) t.equal(b1[4], undefined) } t.end() }) test('new buffer from Float64Array', function (t) { if (typeof Float64Array !== 'undefined') { var b1 = new Float64Array([0, 1, 2, 3]) var b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) t.equal(b1[2], 2) t.equal(b1[3], 3) t.equal(b1[4], undefined) } t.end() }) test('new buffer from buffer.toJSON() output', function (t) { if (typeof JSON === 'undefined') { // ie6, ie7 lack support t.end() return } var buf = new B('test') var json = JSON.stringify(buf) var obj = JSON.parse(json) var copy = new B(obj) t.ok(buf.equals(copy)) t.end() }) buffer-5.4.2/test/from-string.js000066400000000000000000000062321353132250700165630ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('detect utf16 surrogate pairs', function (t) { var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D' var buf = new B(text) t.equal(text, buf.toString()) t.end() }) test('detect utf16 surrogate pairs over U+20000 until U+10FFFF', function (t) { var text = '\uD842\uDFB7' + '\uD93D\uDCAD' + '\uDBFF\uDFFF' var buf = new B(text) t.equal(text, buf.toString()) t.end() }) test('replace orphaned utf16 surrogate lead code point', function (t) { var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D' var buf = new B(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d])) t.end() }) test('replace orphaned utf16 surrogate trail code point', function (t) { var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D' var buf = new B(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d])) t.end() }) test('do not write partial utf16 code units', function (t) { var f = new B([0, 0, 0, 0, 0]) t.equal(f.length, 5) var size = f.write('あいうえお', 'utf16le') t.equal(size, 4) t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00])) t.end() }) test('handle partial utf16 code points when encoding to utf8 the way node does', function (t) { var text = '\uD83D\uDE38' + '\uD83D\uDC4D' var buf = new B(8) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0xf0, 0x9f, 0x91, 0x8d])) buf = new B(7) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00, 0x00])) buf = new B(6) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00])) buf = new B(5) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0x00])) buf = new B(4) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8])) buf = new B(3) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x00, 0x00, 0x00])) buf = new B(2) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x00, 0x00])) buf = new B(1) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x00])) t.end() }) test('handle invalid utf16 code points when encoding to utf8 the way node does', function (t) { var text = 'a' + '\uDE38\uD83D' + 'b' var buf = new B(8) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x62])) buf = new B(7) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd])) buf = new B(6) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x61, 0xef, 0xbf, 0xbd, 0x00, 0x00])) buf = new B(5) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x61, 0xef, 0xbf, 0xbd, 0x00])) buf = new B(4) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x61, 0xef, 0xbf, 0xbd])) buf = new B(3) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x61, 0x00, 0x00])) buf = new B(2) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x61, 0x00])) buf = new B(1) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x61])) t.end() }) buffer-5.4.2/test/is-buffer.js000066400000000000000000000011301353132250700161660ustar00rootroot00000000000000var B = require('../').Buffer var isBuffer = require('is-buffer') var test = require('tape') test('is-buffer tests', function (t) { t.ok(isBuffer(new B(4)), 'new Buffer(4)') t.notOk(isBuffer(undefined), 'undefined') t.notOk(isBuffer(null), 'null') t.notOk(isBuffer(''), 'empty string') t.notOk(isBuffer(true), 'true') t.notOk(isBuffer(false), 'false') t.notOk(isBuffer(0), '0') t.notOk(isBuffer(1), '1') t.notOk(isBuffer(1.0), '1.0') t.notOk(isBuffer('string'), 'string') t.notOk(isBuffer({}), '{}') t.notOk(isBuffer(function foo () {}), 'function foo () {}') t.end() }) buffer-5.4.2/test/methods.js000066400000000000000000000057401353132250700157620ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('buffer.toJSON', function (t) { var data = [1, 2, 3, 4] t.deepEqual( new B(data).toJSON(), { type: 'Buffer', data: [1, 2, 3, 4] } ) t.end() }) test('buffer.copy', function (t) { // copied from nodejs.org example var buf1 = new B(26) var buf2 = new B(26) for (var i = 0; i < 26; i++) { buf1[i] = i + 97 // 97 is ASCII a buf2[i] = 33 // ASCII ! } buf1.copy(buf2, 8, 16, 20) t.equal( buf2.toString('ascii', 0, 25), '!!!!!!!!qrst!!!!!!!!!!!!!' ) t.end() }) test('test offset returns are correct', function (t) { var b = new B(16) t.equal(4, b.writeUInt32LE(0, 0)) t.equal(6, b.writeUInt16LE(0, 4)) t.equal(7, b.writeUInt8(0, 6)) t.equal(8, b.writeInt8(0, 7)) t.equal(16, b.writeDoubleLE(0, 8)) t.end() }) test('concat() a varying number of buffers', function (t) { var zero = [] var one = [new B('asdf')] var long = [] for (var i = 0; i < 10; i++) { long.push(new B('asdf')) } var flatZero = B.concat(zero) var flatOne = B.concat(one) var flatLong = B.concat(long) var flatLongLen = B.concat(long, 40) t.equal(flatZero.length, 0) t.equal(flatOne.toString(), 'asdf') t.deepEqual(flatOne, one[0]) t.equal(flatLong.toString(), (new Array(10 + 1).join('asdf'))) t.equal(flatLongLen.toString(), (new Array(10 + 1).join('asdf'))) t.end() }) test('concat() works on Uint8Array instances', function (t) { var result = B.concat([new Uint8Array([1, 2]), new Uint8Array([3, 4])]) var expected = Buffer.from([1, 2, 3, 4]) t.deepEqual(result, expected) t.end() }) test('fill', function (t) { var b = new B(10) b.fill(2) t.equal(b.toString('hex'), '02020202020202020202') t.end() }) test('fill (string)', function (t) { var b = new B(10) b.fill('abc') t.equal(b.toString(), 'abcabcabca') b.fill('է') t.equal(b.toString(), 'էէէէէ') t.end() }) test('copy() empty buffer with sourceEnd=0', function (t) { var source = new B([42]) var destination = new B([43]) source.copy(destination, 0, 0, 0) t.equal(destination.readUInt8(0), 43) t.end() }) test('copy() after slice()', function (t) { var source = new B(200) var dest = new B(200) var expected = new B(200) for (var i = 0; i < 200; i++) { source[i] = i dest[i] = 0 } source.slice(2).copy(dest) source.copy(expected, 0, 2) t.deepEqual(dest, expected) t.end() }) test('copy() ascending', function (t) { var b = new B('abcdefghij') b.copy(b, 0, 3, 10) t.equal(b.toString(), 'defghijhij') t.end() }) test('copy() descending', function (t) { var b = new B('abcdefghij') b.copy(b, 3, 0, 7) t.equal(b.toString(), 'abcabcdefg') t.end() }) test('buffer.slice sets indexes', function (t) { t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo') t.end() }) test('buffer.slice out of range', function (t) { t.plan(2) t.equal((new B('hallo')).slice(0, 10).toString(), 'hallo') t.equal((new B('hallo')).slice(10, 2).toString(), '') t.end() }) buffer-5.4.2/test/node/000077500000000000000000000000001353132250700147005ustar00rootroot00000000000000buffer-5.4.2/test/node/common.js000066400000000000000000000107561353132250700165370ustar00rootroot00000000000000// Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit // persons to whom the Software is furnished to do so, subject to the // following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. /* eslint-disable required-modules, crypto-check */ 'use strict'; const assert = require('assert'); const mustCallChecks = []; function runCallChecks(exitCode) { if (exitCode !== 0) return; const failed = mustCallChecks.filter(function(context) { if ('minimum' in context) { context.messageSegment = `at least ${context.minimum}`; return context.actual < context.minimum; } else { context.messageSegment = `exactly ${context.exact}`; return context.actual !== context.exact; } }); failed.forEach(function(context) { console.log('Mismatched %s function calls. Expected %s, actual %d.', context.name, context.messageSegment, context.actual); console.log(context.stack.split('\n').slice(2).join('\n')); }); if (failed.length) process.exit(1); } exports.mustCall = function(fn, exact) { return _mustCallInner(fn, exact, 'exact'); }; function _mustCallInner(fn, criteria = 1, field) { if (process._exiting) throw new Error('Cannot use common.mustCall*() in process exit handler'); if (typeof fn === 'number') { criteria = fn; fn = noop; } else if (fn === undefined) { fn = noop; } if (typeof criteria !== 'number') throw new TypeError(`Invalid ${field} value: ${criteria}`); const context = { [field]: criteria, actual: 0, stack: (new Error()).stack, name: fn.name || '' }; // add the exit listener only once to avoid listener leak warnings if (mustCallChecks.length === 0) process.on('exit', runCallChecks); mustCallChecks.push(context); return function() { context.actual++; return fn.apply(this, arguments); }; } exports.printSkipMessage = function(msg) {} // Useful for testing expected internal/error objects exports.expectsError = function expectsError(fn, settings, exact) { if (typeof fn !== 'function') { exact = settings; settings = fn; fn = undefined; } function innerFn(error) { if ('type' in settings) { const type = settings.type; if (type !== Error && !Error.isPrototypeOf(type)) { throw new TypeError('`settings.type` must inherit from `Error`'); } assert(error instanceof type, `${error.name} is not instance of ${type.name}`); let typeName = error.constructor.name; if (typeName === 'NodeError' && type.name !== 'NodeError') { typeName = Object.getPrototypeOf(error.constructor).name; } assert.strictEqual(typeName, type.name); } if ('message' in settings) { const message = settings.message; if (typeof message === 'string') { assert.strictEqual(error.message, message); } else { assert(message.test(error.message), `${error.message} does not match ${message}`); } } if ('name' in settings) { assert.strictEqual(error.name, settings.name); } if (error.constructor.name === 'AssertionError') { ['generatedMessage', 'actual', 'expected', 'operator'].forEach((key) => { if (key in settings) { const actual = error[key]; const expected = settings[key]; assert.strictEqual(actual, expected, `${key}: expected ${expected}, not ${actual}`); } }); } return true; } if (fn) { assert.throws(fn, innerFn); return; } return exports.mustCall(innerFn, exact); }; buffer-5.4.2/test/node/test-buffer-alloc.js000066400000000000000000001040301353132250700205520ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const assert = require('assert'); const vm = require('vm'); const SlowBuffer = require('../../').SlowBuffer; const b = Buffer.allocUnsafe(1024); assert.strictEqual(1024, b.length); b[0] = -1; assert.strictEqual(b[0], 255); for (let i = 0; i < 1024; i++) { b[i] = i % 256; } for (let i = 0; i < 1024; i++) { assert.strictEqual(i % 256, b[i]); } const c = Buffer.allocUnsafe(512); assert.strictEqual(512, c.length); const d = Buffer.from([]); assert.strictEqual(0, d.length); // Test offset properties { const b = Buffer.alloc(128); assert.strictEqual(128, b.length); assert.strictEqual(0, b.byteOffset); assert.strictEqual(0, b.offset); } // Test creating a Buffer from a Uint32Array { const ui32 = new Uint32Array(4).fill(42); const e = Buffer.from(ui32); for (const [index, value] of e.entries()) { assert.strictEqual(value, ui32[index]); } } // Test creating a Buffer from a Uint32Array (old constructor) { const ui32 = new Uint32Array(4).fill(42); const e = Buffer(ui32); for (const [key, value] of e.entries()) { assert.deepStrictEqual(value, ui32[key]); } } // Test invalid encoding for Buffer.toString assert.throws(() => b.toString('invalid'), /Unknown encoding: invalid/); // invalid encoding for Buffer.write assert.throws(() => b.write('test string', 0, 5, 'invalid'), /Unknown encoding: invalid/); // unsupported arguments for Buffer.write assert.throws(() => b.write('test', 'utf8', 0), /is no longer supported/); // try to create 0-length buffers assert.doesNotThrow(() => Buffer.from('')); assert.doesNotThrow(() => Buffer.from('', 'ascii')); assert.doesNotThrow(() => Buffer.from('', 'latin1')); assert.doesNotThrow(() => Buffer.alloc(0)); assert.doesNotThrow(() => Buffer.allocUnsafe(0)); assert.doesNotThrow(() => new Buffer('')); assert.doesNotThrow(() => new Buffer('', 'ascii')); assert.doesNotThrow(() => new Buffer('', 'latin1')); assert.doesNotThrow(() => new Buffer('', 'binary')); assert.doesNotThrow(() => Buffer(0)); assert.doesNotThrow(() => Buffer.alloc(16, !!true)); // try to write a 0-length string beyond the end of b assert.throws(() => b.write('', 2048), RangeError); // throw when writing to negative offset assert.throws(() => b.write('a', -1), RangeError); // throw when writing past bounds from the pool assert.throws(() => b.write('a', 2048), RangeError); // throw when writing to negative offset assert.throws(() => b.write('a', -1), RangeError); // try to copy 0 bytes worth of data into an empty buffer b.copy(Buffer.alloc(0), 0, 0, 0); // try to copy 0 bytes past the end of the target buffer b.copy(Buffer.alloc(0), 1, 1, 1); b.copy(Buffer.alloc(1), 1, 1, 1); // try to copy 0 bytes from past the end of the source buffer b.copy(Buffer.alloc(1), 0, 2048, 2048); // testing for smart defaults and ability to pass string values as offset { const writeTest = Buffer.from('abcdes'); writeTest.write('n', 'ascii'); writeTest.write('o', '1', 'ascii'); writeTest.write('d', '2', 'ascii'); writeTest.write('e', 3, 'ascii'); writeTest.write('j', 4, 'ascii'); assert.strictEqual(writeTest.toString(), 'nodejs'); } // Offset points to the end of the buffer // (see https://github.com/nodejs/node/issues/8127). assert.doesNotThrow(() => Buffer.alloc(1).write('', 1, 0)); // ASCII slice test { const asciiString = 'hello world'; for (let i = 0; i < asciiString.length; i++) { b[i] = asciiString.charCodeAt(i); } const asciiSlice = b.toString('ascii', 0, asciiString.length); assert.strictEqual(asciiString, asciiSlice); } { const asciiString = 'hello world'; const offset = 100; assert.strictEqual(asciiString.length, b.write(asciiString, offset, 'ascii')); const asciiSlice = b.toString('ascii', offset, offset + asciiString.length); assert.strictEqual(asciiString, asciiSlice); } { const asciiString = 'hello world'; const offset = 100; const sliceA = b.slice(offset, offset + asciiString.length); const sliceB = b.slice(offset, offset + asciiString.length); for (let i = 0; i < asciiString.length; i++) { assert.strictEqual(sliceA[i], sliceB[i]); } } // UTF-8 slice test { const utf8String = '¡hέlló wôrld!'; const offset = 100; b.write(utf8String, 0, Buffer.byteLength(utf8String), 'utf8'); let utf8Slice = b.toString('utf8', 0, Buffer.byteLength(utf8String)); assert.strictEqual(utf8String, utf8Slice); assert.strictEqual(Buffer.byteLength(utf8String), b.write(utf8String, offset, 'utf8')); utf8Slice = b.toString('utf8', offset, offset + Buffer.byteLength(utf8String)); assert.strictEqual(utf8String, utf8Slice); const sliceA = b.slice(offset, offset + Buffer.byteLength(utf8String)); const sliceB = b.slice(offset, offset + Buffer.byteLength(utf8String)); for (let i = 0; i < Buffer.byteLength(utf8String); i++) { assert.strictEqual(sliceA[i], sliceB[i]); } } { const slice = b.slice(100, 150); assert.strictEqual(50, slice.length); for (let i = 0; i < 50; i++) { assert.strictEqual(b[100 + i], slice[i]); } } { // make sure only top level parent propagates from allocPool const b = Buffer.allocUnsafe(5); const c = b.slice(0, 4); const d = c.slice(0, 2); assert.strictEqual(b.parent, c.parent); assert.strictEqual(b.parent, d.parent); } { // also from a non-pooled instance const b = Buffer.allocUnsafeSlow(5); const c = b.slice(0, 4); const d = c.slice(0, 2); assert.strictEqual(c.parent, d.parent); } { // Bug regression test const testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語 const buffer = Buffer.allocUnsafe(32); const size = buffer.write(testValue, 0, 'utf8'); const slice = buffer.toString('utf8', 0, size); assert.strictEqual(slice, testValue); } { // Test triple slice const a = Buffer.allocUnsafe(8); for (let i = 0; i < 8; i++) a[i] = i; const b = a.slice(4, 8); assert.strictEqual(4, b[0]); assert.strictEqual(5, b[1]); assert.strictEqual(6, b[2]); assert.strictEqual(7, b[3]); const c = b.slice(2, 4); assert.strictEqual(6, c[0]); assert.strictEqual(7, c[1]); } { const d = Buffer.from([23, 42, 255]); assert.strictEqual(d.length, 3); assert.strictEqual(d[0], 23); assert.strictEqual(d[1], 42); assert.strictEqual(d[2], 255); assert.deepStrictEqual(d, Buffer.from(d)); } { // Test for proper UTF-8 Encoding const e = Buffer.from('über'); assert.deepStrictEqual(e, Buffer.from([195, 188, 98, 101, 114])); } { // Test for proper ascii Encoding, length should be 4 const f = Buffer.from('über', 'ascii'); assert.deepStrictEqual(f, Buffer.from([252, 98, 101, 114])); } ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { { // Test for proper UTF16LE encoding, length should be 8 const f = Buffer.from('über', encoding); assert.deepStrictEqual(f, Buffer.from([252, 0, 98, 0, 101, 0, 114, 0])); } { // Length should be 12 const f = Buffer.from('привет', encoding); assert.deepStrictEqual( f, Buffer.from([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]) ); assert.strictEqual(f.toString(encoding), 'привет'); } { const f = Buffer.from([0, 0, 0, 0, 0]); assert.strictEqual(f.length, 5); const size = f.write('あいうえお', encoding); assert.strictEqual(size, 4); assert.deepStrictEqual(f, Buffer.from([0x42, 0x30, 0x44, 0x30, 0x00])); } }); { const f = Buffer.from('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D) assert.strictEqual(f.length, 4); assert.deepStrictEqual(f, Buffer.from('3DD84DDC', 'hex')); } // Test construction from arrayish object { const arrayIsh = { 0: 0, 1: 1, 2: 2, 3: 3, length: 4 }; let g = Buffer.from(arrayIsh); assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3])); const strArrayIsh = { 0: '0', 1: '1', 2: '2', 3: '3', length: 4 }; g = Buffer.from(strArrayIsh); assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3])); } // // Test toString('base64') // assert.strictEqual('TWFu', (Buffer.from('Man')).toString('base64')); { // test that regular and URL-safe base64 both work const expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff]; assert.deepStrictEqual(Buffer.from('//++/++/++//', 'base64'), Buffer.from(expected)); assert.deepStrictEqual(Buffer.from('__--_--_--__', 'base64'), Buffer.from(expected)); } { // big example const quote = 'Man is distinguished, not only by his reason, but by this ' + 'singular passion from other animals, which is a lust ' + 'of the mind, that by a perseverance of delight in the ' + 'continued and indefatigable generation of knowledge, ' + 'exceeds the short vehemence of any carnal pleasure.'; const expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb' + '24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci' + 'BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQ' + 'gYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu' + 'dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZ' + 'GdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm' + '5hbCBwbGVhc3VyZS4='; assert.strictEqual(expected, (Buffer.from(quote)).toString('base64')); let b = Buffer.allocUnsafe(1024); let bytesWritten = b.write(expected, 0, 'base64'); assert.strictEqual(quote.length, bytesWritten); assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); // check that the base64 decoder ignores whitespace const expectedWhite = `${expected.slice(0, 60)} \n` + `${expected.slice(60, 120)} \n` + `${expected.slice(120, 180)} \n` + `${expected.slice(180, 240)} \n` + `${expected.slice(240, 300)}\n` + `${expected.slice(300, 360)}\n`; b = Buffer.allocUnsafe(1024); bytesWritten = b.write(expectedWhite, 0, 'base64'); assert.strictEqual(quote.length, bytesWritten); assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); // check that the base64 decoder on the constructor works // even in the presence of whitespace. b = Buffer.from(expectedWhite, 'base64'); assert.strictEqual(quote.length, b.length); assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); // check that the base64 decoder ignores illegal chars const expectedIllegal = expected.slice(0, 60) + ' \x80' + expected.slice(60, 120) + ' \xff' + expected.slice(120, 180) + ' \x00' + expected.slice(180, 240) + ' \x98' + expected.slice(240, 300) + '\x03' + expected.slice(300, 360); b = Buffer.from(expectedIllegal, 'base64'); assert.strictEqual(quote.length, b.length); assert.strictEqual(quote, b.toString('ascii', 0, quote.length)); } assert.strictEqual(Buffer.from('', 'base64').toString(), ''); assert.strictEqual(Buffer.from('K', 'base64').toString(), ''); // multiple-of-4 with padding assert.strictEqual(Buffer.from('Kg==', 'base64').toString(), '*'); assert.strictEqual(Buffer.from('Kio=', 'base64').toString(), '*'.repeat(2)); assert.strictEqual(Buffer.from('Kioq', 'base64').toString(), '*'.repeat(3)); assert.strictEqual(Buffer.from('KioqKg==', 'base64').toString(), '*'.repeat(4)); assert.strictEqual(Buffer.from('KioqKio=', 'base64').toString(), '*'.repeat(5)); assert.strictEqual(Buffer.from('KioqKioq', 'base64').toString(), '*'.repeat(6)); assert.strictEqual(Buffer.from('KioqKioqKg==', 'base64').toString(), '*'.repeat(7)); assert.strictEqual(Buffer.from('KioqKioqKio=', 'base64').toString(), '*'.repeat(8)); assert.strictEqual(Buffer.from('KioqKioqKioq', 'base64').toString(), '*'.repeat(9)); assert.strictEqual(Buffer.from('KioqKioqKioqKg==', 'base64').toString(), '*'.repeat(10)); assert.strictEqual(Buffer.from('KioqKioqKioqKio=', 'base64').toString(), '*'.repeat(11)); assert.strictEqual(Buffer.from('KioqKioqKioqKioq', 'base64').toString(), '*'.repeat(12)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKg==', 'base64').toString(), '*'.repeat(13)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKio=', 'base64').toString(), '*'.repeat(14)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioq', 'base64').toString(), '*'.repeat(15)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKg==', 'base64').toString(), '*'.repeat(16)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKio=', 'base64').toString(), '*'.repeat(17)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioq', 'base64').toString(), '*'.repeat(18)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKg==', 'base64').toString(), '*'.repeat(19)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKio=', 'base64').toString(), '*'.repeat(20)); // no padding, not a multiple of 4 assert.strictEqual(Buffer.from('Kg', 'base64').toString(), '*'); assert.strictEqual(Buffer.from('Kio', 'base64').toString(), '*'.repeat(2)); assert.strictEqual(Buffer.from('KioqKg', 'base64').toString(), '*'.repeat(4)); assert.strictEqual(Buffer.from('KioqKio', 'base64').toString(), '*'.repeat(5)); assert.strictEqual(Buffer.from('KioqKioqKg', 'base64').toString(), '*'.repeat(7)); assert.strictEqual(Buffer.from('KioqKioqKio', 'base64').toString(), '*'.repeat(8)); assert.strictEqual(Buffer.from('KioqKioqKioqKg', 'base64').toString(), '*'.repeat(10)); assert.strictEqual(Buffer.from('KioqKioqKioqKio', 'base64').toString(), '*'.repeat(11)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKg', 'base64').toString(), '*'.repeat(13)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKio', 'base64').toString(), '*'.repeat(14)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKg', 'base64').toString(), '*'.repeat(16)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKio', 'base64').toString(), '*'.repeat(17)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKg', 'base64').toString(), '*'.repeat(19)); assert.strictEqual(Buffer.from('KioqKioqKioqKioqKioqKioqKio', 'base64').toString(), '*'.repeat(20)); // handle padding graciously, multiple-of-4 or not assert.strictEqual( Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw==', 'base64').length, 32 ); assert.strictEqual( Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw=', 'base64').length, 32 ); assert.strictEqual( Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw', 'base64').length, 32 ); assert.strictEqual( Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg==', 'base64').length, 31 ); assert.strictEqual( Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg=', 'base64').length, 31 ); assert.strictEqual( Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg', 'base64').length, 31 ); { // This string encodes single '.' character in UTF-16 const dot = Buffer.from('//4uAA==', 'base64'); assert.strictEqual(dot[0], 0xff); assert.strictEqual(dot[1], 0xfe); assert.strictEqual(dot[2], 0x2e); assert.strictEqual(dot[3], 0x00); assert.strictEqual(dot.toString('base64'), '//4uAA=='); } { // Writing base64 at a position > 0 should not mangle the result. // // https://github.com/joyent/node/issues/402 const segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh']; const b = Buffer.allocUnsafe(64); let pos = 0; for (let i = 0; i < segments.length; ++i) { pos += b.write(segments[i], pos, 'base64'); } assert.strictEqual(b.toString('latin1', 0, pos), 'Madness?! This is node.js!'); } // Regression test for https://github.com/nodejs/node/issues/3496. assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0); // Regression test for https://github.com/nodejs/node/issues/11987. assert.deepStrictEqual(Buffer.from('w0 ', 'base64'), Buffer.from('w0', 'base64')); // Regression test for https://github.com/nodejs/node/issues/13657. assert.deepStrictEqual(Buffer.from(' YWJvcnVtLg', 'base64'), Buffer.from('YWJvcnVtLg', 'base64')); { // Creating buffers larger than pool size. const l = Buffer.poolSize + 5; const s = 'h'.repeat(l); const b = Buffer.from(s); for (let i = 0; i < l; i++) { assert.strictEqual('h'.charCodeAt(0), b[i]); } const sb = b.toString(); assert.strictEqual(sb.length, s.length); assert.strictEqual(sb, s); } { // test hex toString const hexb = Buffer.allocUnsafe(256); for (let i = 0; i < 256; i++) { hexb[i] = i; } const hexStr = hexb.toString('hex'); assert.strictEqual(hexStr, '000102030405060708090a0b0c0d0e0f' + '101112131415161718191a1b1c1d1e1f' + '202122232425262728292a2b2c2d2e2f' + '303132333435363738393a3b3c3d3e3f' + '404142434445464748494a4b4c4d4e4f' + '505152535455565758595a5b5c5d5e5f' + '606162636465666768696a6b6c6d6e6f' + '707172737475767778797a7b7c7d7e7f' + '808182838485868788898a8b8c8d8e8f' + '909192939495969798999a9b9c9d9e9f' + 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf' + 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + 'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf' + 'e0e1e2e3e4e5e6e7e8e9eaebecedeeef' + 'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'); const hexb2 = Buffer.from(hexStr, 'hex'); for (let i = 0; i < 256; i++) { assert.strictEqual(hexb2[i], hexb[i]); } } // Test single hex character is discarded. assert.strictEqual(Buffer.from('A', 'hex').length, 0); // Test that if a trailing character is discarded, rest of string is processed. assert.deepStrictEqual(Buffer.from('Abx', 'hex'), Buffer.from('Ab', 'hex')); // Test single base64 char encodes as 0. assert.strictEqual(Buffer.from('A', 'base64').length, 0); { // test an invalid slice end. const b = Buffer.from([1, 2, 3, 4, 5]); const b2 = b.toString('hex', 1, 10000); const b3 = b.toString('hex', 1, 5); const b4 = b.toString('hex', 1); assert.strictEqual(b2, b3); assert.strictEqual(b2, b4); } function buildBuffer(data) { if (Array.isArray(data)) { const buffer = Buffer.allocUnsafe(data.length); data.forEach((v, k) => buffer[k] = v); return buffer; } return null; } const x = buildBuffer([0x81, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72]); assert.strictEqual('', x.inspect()); { const z = x.slice(4); assert.strictEqual(5, z.length); assert.strictEqual(0x6f, z[0]); assert.strictEqual(0xa3, z[1]); assert.strictEqual(0x62, z[2]); assert.strictEqual(0x61, z[3]); assert.strictEqual(0x72, z[4]); } { const z = x.slice(0); assert.strictEqual(z.length, x.length); } { const z = x.slice(0, 4); assert.strictEqual(4, z.length); assert.strictEqual(0x81, z[0]); assert.strictEqual(0xa3, z[1]); } { const z = x.slice(0, 9); assert.strictEqual(9, z.length); } { const z = x.slice(1, 4); assert.strictEqual(3, z.length); assert.strictEqual(0xa3, z[0]); } { const z = x.slice(2, 4); assert.strictEqual(2, z.length); assert.strictEqual(0x66, z[0]); assert.strictEqual(0x6f, z[1]); } ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { const b = Buffer.allocUnsafe(10); b.write('あいうえお', encoding); assert.strictEqual(b.toString(encoding), 'あいうえお'); }); ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { const b = Buffer.allocUnsafe(11); b.write('あいうえお', 1, encoding); assert.strictEqual(b.toString(encoding, 1), 'あいうえお'); }); { // latin1 encoding should write only one byte per character. const b = Buffer.from([0xde, 0xad, 0xbe, 0xef]); let s = String.fromCharCode(0xffff); b.write(s, 0, 'latin1'); assert.strictEqual(0xff, b[0]); assert.strictEqual(0xad, b[1]); assert.strictEqual(0xbe, b[2]); assert.strictEqual(0xef, b[3]); s = String.fromCharCode(0xaaee); b.write(s, 0, 'latin1'); assert.strictEqual(0xee, b[0]); assert.strictEqual(0xad, b[1]); assert.strictEqual(0xbe, b[2]); assert.strictEqual(0xef, b[3]); } { // Binary encoding should write only one byte per character. const b = Buffer.from([0xde, 0xad, 0xbe, 0xef]); let s = String.fromCharCode(0xffff); b.write(s, 0, 'latin1'); assert.strictEqual(0xff, b[0]); assert.strictEqual(0xad, b[1]); assert.strictEqual(0xbe, b[2]); assert.strictEqual(0xef, b[3]); s = String.fromCharCode(0xaaee); b.write(s, 0, 'latin1'); assert.strictEqual(0xee, b[0]); assert.strictEqual(0xad, b[1]); assert.strictEqual(0xbe, b[2]); assert.strictEqual(0xef, b[3]); } { // https://github.com/nodejs/node-v0.x-archive/pull/1210 // Test UTF-8 string includes null character let buf = Buffer.from('\0'); assert.strictEqual(buf.length, 1); buf = Buffer.from('\0\0'); assert.strictEqual(buf.length, 2); } { const buf = Buffer.allocUnsafe(2); assert.strictEqual(buf.write(''), 0); //0bytes assert.strictEqual(buf.write('\0'), 1); // 1byte (v8 adds null terminator) assert.strictEqual(buf.write('a\0'), 2); // 1byte * 2 assert.strictEqual(buf.write('あ'), 0); // 3bytes assert.strictEqual(buf.write('\0あ'), 1); // 1byte + 3bytes assert.strictEqual(buf.write('\0\0あ'), 2); // 1byte * 2 + 3bytes } { const buf = Buffer.allocUnsafe(10); assert.strictEqual(buf.write('あいう'), 9); // 3bytes * 3 (v8 adds null term.) assert.strictEqual(buf.write('あいう\0'), 10); // 3bytes * 3 + 1byte } { // https://github.com/nodejs/node-v0.x-archive/issues/243 // Test write() with maxLength const buf = Buffer.allocUnsafe(4); buf.fill(0xFF); assert.strictEqual(buf.write('abcd', 1, 2, 'utf8'), 2); assert.strictEqual(buf[0], 0xFF); assert.strictEqual(buf[1], 0x61); assert.strictEqual(buf[2], 0x62); assert.strictEqual(buf[3], 0xFF); buf.fill(0xFF); assert.strictEqual(buf.write('abcd', 1, 4), 3); assert.strictEqual(buf[0], 0xFF); assert.strictEqual(buf[1], 0x61); assert.strictEqual(buf[2], 0x62); assert.strictEqual(buf[3], 0x63); buf.fill(0xFF); assert.strictEqual(buf.write('abcd', 1, 2, 'utf8'), 2); assert.strictEqual(buf[0], 0xFF); assert.strictEqual(buf[1], 0x61); assert.strictEqual(buf[2], 0x62); assert.strictEqual(buf[3], 0xFF); buf.fill(0xFF); assert.strictEqual(buf.write('abcdef', 1, 2, 'hex'), 2); assert.strictEqual(buf[0], 0xFF); assert.strictEqual(buf[1], 0xAB); assert.strictEqual(buf[2], 0xCD); assert.strictEqual(buf[3], 0xFF); ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach((encoding) => { buf.fill(0xFF); assert.strictEqual(buf.write('abcd', 0, 2, encoding), 2); assert.strictEqual(buf[0], 0x61); assert.strictEqual(buf[1], 0x00); assert.strictEqual(buf[2], 0xFF); assert.strictEqual(buf[3], 0xFF); }); } { // test offset returns are correct const b = Buffer.allocUnsafe(16); assert.strictEqual(4, b.writeUInt32LE(0, 0)); assert.strictEqual(6, b.writeUInt16LE(0, 4)); assert.strictEqual(7, b.writeUInt8(0, 6)); assert.strictEqual(8, b.writeInt8(0, 7)); assert.strictEqual(16, b.writeDoubleLE(0, 8)); } { // test unmatched surrogates not producing invalid utf8 output // ef bf bd = utf-8 representation of unicode replacement character // see https://codereview.chromium.org/121173009/ const buf = Buffer.from('ab\ud800cd', 'utf8'); assert.strictEqual(buf[0], 0x61); assert.strictEqual(buf[1], 0x62); assert.strictEqual(buf[2], 0xef); assert.strictEqual(buf[3], 0xbf); assert.strictEqual(buf[4], 0xbd); assert.strictEqual(buf[5], 0x63); assert.strictEqual(buf[6], 0x64); } { // test for buffer overrun const buf = Buffer.from([0, 0, 0, 0, 0]); // length: 5 const sub = buf.slice(0, 4); // length: 4 assert.strictEqual(sub.write('12345', 'latin1'), 4); assert.strictEqual(buf[4], 0); assert.strictEqual(sub.write('12345', 'binary'), 4); assert.strictEqual(buf[4], 0); } { // test alloc with fill option const buf = Buffer.alloc(5, '800A', 'hex'); assert.strictEqual(buf[0], 128); assert.strictEqual(buf[1], 10); assert.strictEqual(buf[2], 128); assert.strictEqual(buf[3], 10); assert.strictEqual(buf[4], 128); } // Check for fractional length args, junk length args, etc. // https://github.com/joyent/node/issues/1758 // Call .fill() first, stops valgrind warning about uninitialized memory reads. Buffer.allocUnsafe(3.3).fill().toString(); // throws bad argument error in commit 43cb4ec Buffer.alloc(3.3).fill().toString(); assert.strictEqual(Buffer.allocUnsafe(NaN).length, 0); assert.strictEqual(Buffer.allocUnsafe(3.3).length, 3); assert.strictEqual(Buffer.from({ length: 3.3 }).length, 3); assert.strictEqual(Buffer.from({ length: 'BAM' }).length, 0); // Make sure that strings are not coerced to numbers. assert.strictEqual(Buffer.from('99').length, 2); assert.strictEqual(Buffer.from('13.37').length, 5); // Ensure that the length argument is respected. ['ascii', 'utf8', 'hex', 'base64', 'latin1', 'binary'].forEach((enc) => { assert.strictEqual(Buffer.allocUnsafe(1).write('aaaaaa', 0, 1, enc), 1); }); { // Regression test, guard against buffer overrun in the base64 decoder. const a = Buffer.allocUnsafe(3); const b = Buffer.from('xxx'); a.write('aaaaaaaa', 'base64'); assert.strictEqual(b.toString(), 'xxx'); } // issue GH-3416 Buffer.from(Buffer.allocUnsafe(0), 0, 0); // issue GH-5587 assert.throws(() => Buffer.alloc(8).writeFloatLE(0, 5), RangeError); assert.throws(() => Buffer.alloc(16).writeDoubleLE(0, 9), RangeError); // attempt to overflow buffers, similar to previous bug in array buffers assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), RangeError); assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), RangeError); // ensure negative values can't get past offset assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError); assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError); // test for common write(U)IntLE/BE { let buf = Buffer.allocUnsafe(3); buf.writeUIntLE(0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); assert.strictEqual(buf.readUIntLE(0, 3), 0x123456); buf.fill(0xFF); buf.writeUIntBE(0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); assert.strictEqual(buf.readUIntBE(0, 3), 0x123456); buf.fill(0xFF); buf.writeIntLE(0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); assert.strictEqual(buf.readIntLE(0, 3), 0x123456); buf.fill(0xFF); buf.writeIntBE(0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); assert.strictEqual(buf.readIntBE(0, 3), 0x123456); buf.fill(0xFF); buf.writeIntLE(-0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]); assert.strictEqual(buf.readIntLE(0, 3), -0x123456); buf.fill(0xFF); buf.writeIntBE(-0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]); assert.strictEqual(buf.readIntBE(0, 3), -0x123456); buf.fill(0xFF); buf.writeIntLE(-0x123400, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]); assert.strictEqual(buf.readIntLE(0, 3), -0x123400); buf.fill(0xFF); buf.writeIntBE(-0x123400, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]); assert.strictEqual(buf.readIntBE(0, 3), -0x123400); buf.fill(0xFF); buf.writeIntLE(-0x120000, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0xee]); assert.strictEqual(buf.readIntLE(0, 3), -0x120000); buf.fill(0xFF); buf.writeIntBE(-0x120000, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0xee, 0x00, 0x00]); assert.strictEqual(buf.readIntBE(0, 3), -0x120000); buf = Buffer.allocUnsafe(5); buf.writeUIntLE(0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); assert.strictEqual(buf.readUIntLE(0, 5), 0x1234567890); buf.fill(0xFF); buf.writeUIntBE(0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); assert.strictEqual(buf.readUIntBE(0, 5), 0x1234567890); buf.fill(0xFF); buf.writeIntLE(0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); assert.strictEqual(buf.readIntLE(0, 5), 0x1234567890); buf.fill(0xFF); buf.writeIntBE(0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); assert.strictEqual(buf.readIntBE(0, 5), 0x1234567890); buf.fill(0xFF); buf.writeIntLE(-0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]); assert.strictEqual(buf.readIntLE(0, 5), -0x1234567890); buf.fill(0xFF); buf.writeIntBE(-0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]); assert.strictEqual(buf.readIntBE(0, 5), -0x1234567890); buf.fill(0xFF); buf.writeIntLE(-0x0012000000, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]); assert.strictEqual(buf.readIntLE(0, 5), -0x0012000000); buf.fill(0xFF); buf.writeIntBE(-0x0012000000, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]); assert.strictEqual(buf.readIntBE(0, 5), -0x0012000000); } // Regression test for https://github.com/nodejs/node-v0.x-archive/issues/5482: // should throw but not assert in C++ land. common.expectsError( () => Buffer.from('', 'buffer'), { code: 'ERR_UNKNOWN_ENCODING', type: TypeError, message: 'Unknown encoding: buffer' } ); // Regression test for https://github.com/nodejs/node-v0.x-archive/issues/6111. // Constructing a buffer from another buffer should a) work, and b) not corrupt // the source buffer. { const a = [...Array(128).keys()]; // [0, 1, 2, 3, ... 126, 127] const b = Buffer.from(a); const c = Buffer.from(b); assert.strictEqual(b.length, a.length); assert.strictEqual(c.length, a.length); for (let i = 0, k = a.length; i < k; ++i) { assert.strictEqual(a[i], i); assert.strictEqual(b[i], i); assert.strictEqual(c[i], i); } } if (common.hasCrypto) { // eslint-disable-line crypto-check // Test truncation after decode const crypto = require('crypto'); const b1 = Buffer.from('YW55=======', 'base64'); const b2 = Buffer.from('YW55', 'base64'); assert.strictEqual( crypto.createHash('sha1').update(b1).digest('hex'), crypto.createHash('sha1').update(b2).digest('hex') ); } else { common.printSkipMessage('missing crypto'); } const ps = Buffer.poolSize; Buffer.poolSize = 0; assert(Buffer.allocUnsafe(1).parent instanceof ArrayBuffer); Buffer.poolSize = ps; // Test Buffer.copy() segfault assert.throws(() => Buffer.allocUnsafe(10).copy(), /TypeError: argument should be a Buffer/); const regErrorMsg = new RegExp('The first argument must be one of type string, Buffer, ' + 'ArrayBuffer, Array, or Array-like Object\\.'); assert.throws(() => Buffer.from(), regErrorMsg); assert.throws(() => Buffer.from(null), regErrorMsg); // Test prototype getters don't throw assert.strictEqual(Buffer.prototype.parent, undefined); assert.strictEqual(Buffer.prototype.offset, undefined); assert.strictEqual(SlowBuffer.prototype.parent, undefined); assert.strictEqual(SlowBuffer.prototype.offset, undefined); { // Test that large negative Buffer length inputs don't affect the pool offset. // Use the fromArrayLike() variant here because it's more lenient // about its input and passes the length directly to allocate(). assert.deepStrictEqual(Buffer.from({ length: -Buffer.poolSize }), Buffer.from('')); assert.deepStrictEqual(Buffer.from({ length: -100 }), Buffer.from('')); // Check pool offset after that by trying to write string into the pool. assert.doesNotThrow(() => Buffer.from('abc')); } // Test that ParseArrayIndex handles full uint32 { const errMsg = common.expectsError({ code: 'ERR_BUFFER_OUT_OF_BOUNDS', type: RangeError, message: '"offset" is outside of buffer bounds' }); assert.throws(() => Buffer.from(new ArrayBuffer(0), -1 >>> 0), errMsg); } // ParseArrayIndex() should reject values that don't fit in a 32 bits size_t. common.expectsError(() => { const a = Buffer.alloc(1); const b = Buffer.alloc(1); a.copy(b, 0, 0x100000000, 0x100000001); }, { code: undefined, type: RangeError, message: 'Index out of range' }); // Unpooled buffer (replaces SlowBuffer) { const ubuf = Buffer.allocUnsafeSlow(10); assert(ubuf); assert(ubuf.buffer); assert.strictEqual(ubuf.buffer.byteLength, 10); } // Regression test assert.doesNotThrow(() => Buffer.from(new ArrayBuffer())); // Test that ArrayBuffer from a different context is detected correctly const arrayBuf = vm.runInNewContext('new ArrayBuffer()'); assert.doesNotThrow(() => Buffer.from(arrayBuf)); assert.doesNotThrow(() => Buffer.from({ buffer: arrayBuf })); assert.throws(() => Buffer.alloc({ valueOf: () => 1 }), /"size" argument must be of type number/); assert.throws(() => Buffer.alloc({ valueOf: () => -1 }), /"size" argument must be of type number/); assert.strictEqual(Buffer.prototype.toLocaleString, Buffer.prototype.toString); { const buf = Buffer.from('test'); assert.strictEqual(buf.toLocaleString(), buf.toString()); } common.expectsError(() => { Buffer.alloc(0x1000, 'This is not correctly encoded', 'hex'); }, { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); common.expectsError(() => { Buffer.alloc(0x1000, 'c', 'hex'); }, { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); common.expectsError(() => { Buffer.alloc(1, Buffer.alloc(0)); }, { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); buffer-5.4.2/test/node/test-buffer-arraybuffer.js000066400000000000000000000101371353132250700217740ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const assert = require('assert'); const LENGTH = 16; const ab = new ArrayBuffer(LENGTH); const dv = new DataView(ab); const ui = new Uint8Array(ab); const buf = Buffer.from(ab); assert.ok(buf instanceof Buffer); assert.strictEqual(buf.parent, buf.buffer); assert.strictEqual(buf.buffer, ab); assert.strictEqual(buf.length, ab.byteLength); buf.fill(0xC); for (let i = 0; i < LENGTH; i++) { assert.strictEqual(ui[i], 0xC); ui[i] = 0xF; assert.strictEqual(buf[i], 0xF); } buf.writeUInt32LE(0xF00, 0); buf.writeUInt32BE(0xB47, 4); buf.writeDoubleLE(3.1415, 8); assert.strictEqual(dv.getUint32(0, true), 0xF00); assert.strictEqual(dv.getUint32(4), 0xB47); assert.strictEqual(dv.getFloat64(8, true), 3.1415); // Now test protecting users from doing stupid things assert.throws(function() { function AB() { } Object.setPrototypeOf(AB, ArrayBuffer); Object.setPrototypeOf(AB.prototype, ArrayBuffer.prototype); Buffer.from(new AB()); }, TypeError); // write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766 const b = Buffer.allocUnsafe(1); b.writeFloatLE(11.11, 0, true); b.writeFloatBE(11.11, 0, true); b.writeDoubleLE(11.11, 0, true); b.writeDoubleBE(11.11, 0, true); // Test the byteOffset and length arguments { const ab = new Uint8Array(5); ab[0] = 1; ab[1] = 2; ab[2] = 3; ab[3] = 4; ab[4] = 5; const buf = Buffer.from(ab.buffer, 1, 3); assert.strictEqual(buf.length, 3); assert.strictEqual(buf[0], 2); assert.strictEqual(buf[1], 3); assert.strictEqual(buf[2], 4); buf[0] = 9; assert.strictEqual(ab[1], 9); common.expectsError(() => Buffer.from(ab.buffer, 6), { code: 'ERR_BUFFER_OUT_OF_BOUNDS', type: RangeError, message: '"offset" is outside of buffer bounds' }); common.expectsError(() => Buffer.from(ab.buffer, 3, 6), { code: 'ERR_BUFFER_OUT_OF_BOUNDS', type: RangeError, message: '"length" is outside of buffer bounds' }); } // Test the deprecated Buffer() version also { const ab = new Uint8Array(5); ab[0] = 1; ab[1] = 2; ab[2] = 3; ab[3] = 4; ab[4] = 5; const buf = Buffer(ab.buffer, 1, 3); assert.strictEqual(buf.length, 3); assert.strictEqual(buf[0], 2); assert.strictEqual(buf[1], 3); assert.strictEqual(buf[2], 4); buf[0] = 9; assert.strictEqual(ab[1], 9); common.expectsError(() => Buffer(ab.buffer, 6), { code: 'ERR_BUFFER_OUT_OF_BOUNDS', type: RangeError, message: '"offset" is outside of buffer bounds' }); common.expectsError(() => Buffer(ab.buffer, 3, 6), { code: 'ERR_BUFFER_OUT_OF_BOUNDS', type: RangeError, message: '"length" is outside of buffer bounds' }); } { // If byteOffset is not numeric, it defaults to 0. const ab = new ArrayBuffer(10); const expected = Buffer.from(ab, 0); assert.deepStrictEqual(Buffer.from(ab, 'fhqwhgads'), expected); assert.deepStrictEqual(Buffer.from(ab, NaN), expected); assert.deepStrictEqual(Buffer.from(ab, {}), expected); assert.deepStrictEqual(Buffer.from(ab, []), expected); // If byteOffset can be converted to a number, it will be. assert.deepStrictEqual(Buffer.from(ab, [1]), Buffer.from(ab, 1)); // If byteOffset is Infinity, throw. common.expectsError(() => { Buffer.from(ab, Infinity); }, { code: 'ERR_BUFFER_OUT_OF_BOUNDS', type: RangeError, message: '"offset" is outside of buffer bounds' }); } { // If length is not numeric, it defaults to 0. const ab = new ArrayBuffer(10); const expected = Buffer.from(ab, 0, 0); assert.deepStrictEqual(Buffer.from(ab, 0, 'fhqwhgads'), expected); assert.deepStrictEqual(Buffer.from(ab, 0, NaN), expected); assert.deepStrictEqual(Buffer.from(ab, 0, {}), expected); assert.deepStrictEqual(Buffer.from(ab, 0, []), expected); // If length can be converted to a number, it will be. assert.deepStrictEqual(Buffer.from(ab, 0, [1]), Buffer.from(ab, 0, 1)); //If length is Infinity, throw. common.expectsError(() => { Buffer.from(ab, 0, Infinity); }, { code: 'ERR_BUFFER_OUT_OF_BOUNDS', type: RangeError, message: '"length" is outside of buffer bounds' }); } buffer-5.4.2/test/node/test-buffer-ascii.js000066400000000000000000000040751353132250700205600ustar00rootroot00000000000000// Copyright Joyent, Inc. and other Node contributors.var Buffer = require('../../').Buffer; // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit // persons to whom the Software is furnished to do so, subject to the // following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; require('./common'); const assert = require('assert'); // ASCII conversion in node.js simply masks off the high bits, // it doesn't do transliteration. assert.strictEqual(Buffer.from('hérité').toString('ascii'), 'hC)ritC)'); // 71 characters, 78 bytes. The ’ character is a triple-byte sequence. const input = 'C’est, graphiquement, la réunion d’un accent aigu ' + 'et d’un accent grave.'; const expected = 'Cb\u0000\u0019est, graphiquement, la rC)union ' + 'db\u0000\u0019un accent aigu et db\u0000\u0019un ' + 'accent grave.'; const buf = Buffer.from(input); for (let i = 0; i < expected.length; ++i) { assert.strictEqual(buf.slice(i).toString('ascii'), expected.slice(i)); // Skip remainder of multi-byte sequence. if (input.charCodeAt(i) > 65535) ++i; if (input.charCodeAt(i) > 127) ++i; } buffer-5.4.2/test/node/test-buffer-bad-overload.js000066400000000000000000000007771353132250700220340ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const assert = require('assert'); assert.doesNotThrow(function() { Buffer.allocUnsafe(10); }); const err = common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "value" argument must not be of type number. ' + 'Received type number' }); assert.throws(function() { Buffer.from(10, 'hex'); }, err); assert.doesNotThrow(function() { Buffer.from('deadbeaf', 'hex'); }); buffer-5.4.2/test/node/test-buffer-badhex.js000066400000000000000000000031761353132250700207240ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); // Test hex strings and bad hex strings { const buf = Buffer.alloc(4); assert.strictEqual(buf.length, 4); assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); assert.strictEqual(buf.write('abcdxx', 0, 'hex'), 2); assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0x00, 0x00])); assert.strictEqual(buf.toString('hex'), 'abcd0000'); assert.strictEqual(buf.write('abcdef01', 0, 'hex'), 4); assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0xef, 0x01])); assert.strictEqual(buf.toString('hex'), 'abcdef01'); const copy = Buffer.from(buf.toString('hex'), 'hex'); assert.strictEqual(buf.toString('hex'), copy.toString('hex')); } { const buf = Buffer.alloc(5); assert.strictEqual(buf.write('abcdxx', 1, 'hex'), 2); assert.strictEqual(buf.toString('hex'), '00abcd0000'); } { const buf = Buffer.alloc(4); assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); assert.strictEqual(buf.write('xxabcd', 0, 'hex'), 0); assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); assert.strictEqual(buf.write('xxab', 1, 'hex'), 0); assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); assert.strictEqual(buf.write('cdxxab', 0, 'hex'), 1); assert.deepStrictEqual(buf, new Buffer([0xcd, 0, 0, 0])); } { const buf = Buffer.alloc(256); for (let i = 0; i < 256; i++) buf[i] = i; const hex = buf.toString('hex'); assert.deepStrictEqual(Buffer.from(hex, 'hex'), buf); const badHex = `${hex.slice(0, 256)}xx${hex.slice(256, 510)}`; assert.deepStrictEqual(Buffer.from(badHex, 'hex'), buf.slice(0, 128)); } buffer-5.4.2/test/node/test-buffer-bytelength.js000066400000000000000000000106431353132250700216330ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const assert = require('assert'); const SlowBuffer = require('../../').SlowBuffer; const vm = require('vm'); [ [32, 'latin1'], [NaN, 'utf8'], [{}, 'latin1'], [] ].forEach((args) => { common.expectsError( () => Buffer.byteLength(...args), { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "string" argument must be one of type string, ' + `Buffer, or ArrayBuffer. Received type ${typeof args[0]}` } ); }); assert.strictEqual(Buffer.byteLength('', undefined, true), -1); assert(ArrayBuffer.isView(new Buffer(10))); assert(ArrayBuffer.isView(new SlowBuffer(10))); assert(ArrayBuffer.isView(Buffer.alloc(10))); assert(ArrayBuffer.isView(Buffer.allocUnsafe(10))); assert(ArrayBuffer.isView(Buffer.allocUnsafeSlow(10))); assert(ArrayBuffer.isView(Buffer.from(''))); // buffer const incomplete = Buffer.from([0xe4, 0xb8, 0xad, 0xe6, 0x96]); assert.strictEqual(Buffer.byteLength(incomplete), 5); const ascii = Buffer.from('abc'); assert.strictEqual(Buffer.byteLength(ascii), 3); // ArrayBuffer const buffer = new ArrayBuffer(8); assert.strictEqual(Buffer.byteLength(buffer), 8); // TypedArray const int8 = new Int8Array(8); assert.strictEqual(Buffer.byteLength(int8), 8); const uint8 = new Uint8Array(8); assert.strictEqual(Buffer.byteLength(uint8), 8); const uintc8 = new Uint8ClampedArray(2); assert.strictEqual(Buffer.byteLength(uintc8), 2); const int16 = new Int16Array(8); assert.strictEqual(Buffer.byteLength(int16), 16); const uint16 = new Uint16Array(8); assert.strictEqual(Buffer.byteLength(uint16), 16); const int32 = new Int32Array(8); assert.strictEqual(Buffer.byteLength(int32), 32); const uint32 = new Uint32Array(8); assert.strictEqual(Buffer.byteLength(uint32), 32); const float32 = new Float32Array(8); assert.strictEqual(Buffer.byteLength(float32), 32); const float64 = new Float64Array(8); assert.strictEqual(Buffer.byteLength(float64), 64); // DataView const dv = new DataView(new ArrayBuffer(2)); assert.strictEqual(Buffer.byteLength(dv), 2); // special case: zero length string assert.strictEqual(Buffer.byteLength('', 'ascii'), 0); assert.strictEqual(Buffer.byteLength('', 'HeX'), 0); // utf8 assert.strictEqual(Buffer.byteLength('∑éllö wørl∂!', 'utf-8'), 19); assert.strictEqual(Buffer.byteLength('κλμνξο', 'utf8'), 12); assert.strictEqual(Buffer.byteLength('挵挶挷挸挹', 'utf-8'), 15); assert.strictEqual(Buffer.byteLength('𠝹𠱓𠱸', 'UTF8'), 12); // without an encoding, utf8 should be assumed assert.strictEqual(Buffer.byteLength('hey there'), 9); assert.strictEqual(Buffer.byteLength('𠱸挶νξ#xx :)'), 17); assert.strictEqual(Buffer.byteLength('hello world', ''), 11); // it should also be assumed with unrecognized encoding assert.strictEqual(Buffer.byteLength('hello world', 'abc'), 11); assert.strictEqual(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10); // base64 assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11); assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'BASE64'), 11); assert.strictEqual(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14); assert.strictEqual(Buffer.byteLength('aGkk', 'base64'), 3); assert.strictEqual( Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw==', 'base64'), 25 ); // special padding assert.strictEqual(Buffer.byteLength('aaa=', 'base64'), 2); assert.strictEqual(Buffer.byteLength('aaaa==', 'base64'), 3); assert.strictEqual(Buffer.byteLength('Il était tué'), 14); assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14); ['ascii', 'latin1', 'binary'] .reduce((es, e) => es.concat(e, e.toUpperCase()), []) .forEach((encoding) => { assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 12); }); ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'] .reduce((es, e) => es.concat(e, e.toUpperCase()), []) .forEach((encoding) => { assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 24); }); // Test that ArrayBuffer from a different context is detected correctly const arrayBuf = vm.runInNewContext('new ArrayBuffer()'); assert.strictEqual(Buffer.byteLength(arrayBuf), 0); // Verify that invalid encodings are treated as utf8 for (let i = 1; i < 10; i++) { const encoding = String(i).repeat(i); assert.ok(!Buffer.isEncoding(encoding)); assert.strictEqual(Buffer.byteLength('foo', encoding), Buffer.byteLength('foo', 'utf8')); } buffer-5.4.2/test/node/test-buffer-compare-offset.js000066400000000000000000000047461353132250700224070ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const assert = require('assert'); const a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]); const b = Buffer.from([5, 6, 7, 8, 9, 0, 1, 2, 3, 4]); assert.strictEqual(-1, a.compare(b)); // Equivalent to a.compare(b). assert.strictEqual(-1, a.compare(b, 0)); assert.strictEqual(-1, a.compare(b, '0')); assert.strictEqual(-1, a.compare(b, undefined)); // Equivalent to a.compare(b). assert.strictEqual(-1, a.compare(b, 0, undefined, 0)); // Zero-length target, return 1 assert.strictEqual(1, a.compare(b, 0, 0, 0)); assert.strictEqual(1, a.compare(b, '0', '0', '0')); // Equivalent to Buffer.compare(a, b.slice(6, 10)) assert.strictEqual(1, a.compare(b, 6, 10)); // Zero-length source, return -1 assert.strictEqual(-1, a.compare(b, 6, 10, 0, 0)); // Zero-length source and target, return 0 assert.strictEqual(0, a.compare(b, 0, 0, 0, 0)); assert.strictEqual(0, a.compare(b, 1, 1, 2, 2)); // Equivalent to Buffer.compare(a.slice(4), b.slice(0, 5)) assert.strictEqual(1, a.compare(b, 0, 5, 4)); // Equivalent to Buffer.compare(a.slice(1), b.slice(5)) assert.strictEqual(1, a.compare(b, 5, undefined, 1)); // Equivalent to Buffer.compare(a.slice(2), b.slice(2, 4)) assert.strictEqual(-1, a.compare(b, 2, 4, 2)); // Equivalent to Buffer.compare(a.slice(4), b.slice(0, 7)) assert.strictEqual(-1, a.compare(b, 0, 7, 4)); // Equivalent to Buffer.compare(a.slice(4, 6), b.slice(0, 7)); assert.strictEqual(-1, a.compare(b, 0, 7, 4, 6)); // zero length target assert.strictEqual(1, a.compare(b, 0, null)); // coerces to targetEnd == 5 assert.strictEqual(-1, a.compare(b, 0, { valueOf: () => 5 })); // zero length target assert.strictEqual(1, a.compare(b, Infinity, -Infinity)); // zero length target because default for targetEnd <= targetSource assert.strictEqual(1, a.compare(b, '0xff')); const oor = common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }, 7); assert.throws(() => a.compare(b, 0, 100, 0), oor); assert.throws(() => a.compare(b, 0, 1, 0, 100), oor); assert.throws(() => a.compare(b, -1), oor); assert.throws(() => a.compare(b, 0, '0xff'), oor); assert.throws(() => a.compare(b, 0, Infinity), oor); assert.throws(() => a.compare(b, 0, 1, -1), oor); assert.throws(() => a.compare(b, -Infinity, Infinity), oor); common.expectsError(() => a.compare(), { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "target" argument must be one of ' + 'type Buffer or Uint8Array. Received type undefined' }); buffer-5.4.2/test/node/test-buffer-compare.js000066400000000000000000000032011353132250700211040ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const assert = require('assert'); const b = Buffer.alloc(1, 'a'); const c = Buffer.alloc(1, 'c'); const d = Buffer.alloc(2, 'aa'); const e = new Uint8Array([ 0x61, 0x61 ]); // ASCII 'aa', same as d assert.strictEqual(b.compare(c), -1); assert.strictEqual(c.compare(d), 1); assert.strictEqual(d.compare(b), 1); assert.strictEqual(d.compare(e), 0); assert.strictEqual(b.compare(d), -1); assert.strictEqual(b.compare(b), 0); assert.strictEqual(Buffer.compare(b, c), -1); assert.strictEqual(Buffer.compare(c, d), 1); assert.strictEqual(Buffer.compare(d, b), 1); assert.strictEqual(Buffer.compare(b, d), -1); assert.strictEqual(Buffer.compare(c, c), 0); assert.strictEqual(Buffer.compare(e, e), 0); assert.strictEqual(Buffer.compare(d, e), 0); assert.strictEqual(Buffer.compare(d, b), 1); assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0); assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1); assert.strictEqual(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1); const errMsg = common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "buf1", "buf2" arguments must be one of ' + 'type Buffer or Uint8Array' }, 2); assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'), errMsg); assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)), errMsg); common.expectsError(() => Buffer.alloc(1).compare('abc'), { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "target" argument must be one of ' + 'type Buffer or Uint8Array. Received type string' }); buffer-5.4.2/test/node/test-buffer-concat.js000066400000000000000000000022101353132250700207240ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; var assert = require('assert'); var zero = []; var one = [ Buffer.from('asdf') ]; var long = []; for (var i = 0; i < 10; i++) long.push(Buffer.from('asdf')); var flatZero = Buffer.concat(zero); var flatOne = Buffer.concat(one); var flatLong = Buffer.concat(long); var flatLongLen = Buffer.concat(long, 40); assert(flatZero.length === 0); assert(flatOne.toString() === 'asdf'); // A special case where concat used to return the first item, // if the length is one. This check is to make sure that we don't do that. assert(flatOne !== one[0]); assert(flatLong.toString() === (new Array(10 + 1).join('asdf'))); assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf'))); assertWrongList(); assertWrongList(null); assertWrongList(Buffer.from('hello')); assertWrongList([42]); assertWrongList(['hello', 'world']); assertWrongList(['hello', Buffer.from('world')]); function assertWrongList(value) { assert.throws(function() { Buffer.concat(value); }, function(err) { return err instanceof TypeError && err.message === '"list" argument must be an Array of Buffers'; }); } buffer-5.4.2/test/node/test-buffer-failed-alloc-typed-arrays.js000066400000000000000000000022231353132250700244170ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); const SlowBuffer = require('../../').SlowBuffer; // Test failed or zero-sized Buffer allocations not affecting typed arrays. // This test exists because of a regression that occurred. Because Buffer // instances are allocated with the same underlying allocator as TypedArrays, // but Buffer's can optional be non-zero filled, there was a regression that // occurred when a Buffer allocated failed, the internal flag specifying // whether or not to zero-fill was not being reset, causing TypedArrays to // allocate incorrectly. const zeroArray = new Uint32Array(10).fill(0); const sizes = [1e10, 0, 0.1, -1, 'a', undefined, null, NaN]; const allocators = [ Buffer, SlowBuffer, Buffer.alloc, Buffer.allocUnsafe, Buffer.allocUnsafeSlow ]; for (const allocator of allocators) { for (const size of sizes) { try { // These allocations are known to fail. If they do, // Uint32Array should still produce a zeroed out result. allocator(size); } catch (e) { assert.deepStrictEqual(new Uint32Array(10), zeroArray); } } } buffer-5.4.2/test/node/test-buffer-fill.js000066400000000000000000000206011353132250700204070ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); var assert = require('assert'); var os = require('os'); var SIZE = 28; var buf1 = Buffer.allocUnsafe(SIZE); var buf2 = Buffer.allocUnsafe(SIZE); // Default encoding testBufs('abc'); testBufs('\u0222aa'); testBufs('a\u0234b\u0235c\u0236'); testBufs('abc', 4); testBufs('abc', 5); testBufs('abc', SIZE); testBufs('\u0222aa', 2); testBufs('\u0222aa', 8); testBufs('a\u0234b\u0235c\u0236', 4); testBufs('a\u0234b\u0235c\u0236', 12); testBufs('abc', 4, -1); testBufs('abc', 4, 1); testBufs('abc', 5, 1); testBufs('\u0222aa', 2, -1); testBufs('\u0222aa', 8, 1); testBufs('a\u0234b\u0235c\u0236', 4, -1); testBufs('a\u0234b\u0235c\u0236', 4, 1); testBufs('a\u0234b\u0235c\u0236', 12, 1); // UTF8 testBufs('abc', 'utf8'); testBufs('\u0222aa', 'utf8'); testBufs('a\u0234b\u0235c\u0236', 'utf8'); testBufs('abc', 4, 'utf8'); testBufs('abc', 5, 'utf8'); testBufs('abc', SIZE, 'utf8'); testBufs('\u0222aa', 2, 'utf8'); testBufs('\u0222aa', 8, 'utf8'); testBufs('a\u0234b\u0235c\u0236', 4, 'utf8'); testBufs('a\u0234b\u0235c\u0236', 12, 'utf8'); testBufs('abc', 4, -1, 'utf8'); testBufs('abc', 4, 1, 'utf8'); testBufs('abc', 5, 1, 'utf8'); testBufs('\u0222aa', 2, -1, 'utf8'); testBufs('\u0222aa', 8, 1, 'utf8'); testBufs('a\u0234b\u0235c\u0236', 4, -1, 'utf8'); testBufs('a\u0234b\u0235c\u0236', 4, 1, 'utf8'); testBufs('a\u0234b\u0235c\u0236', 12, 1, 'utf8'); assert.equal(Buffer.allocUnsafe(1).fill(0).fill('\u0222')[0], 0xc8); // BINARY testBufs('abc', 'binary'); testBufs('\u0222aa', 'binary'); testBufs('a\u0234b\u0235c\u0236', 'binary'); testBufs('abc', 4, 'binary'); testBufs('abc', 5, 'binary'); testBufs('abc', SIZE, 'binary'); testBufs('\u0222aa', 2, 'binary'); testBufs('\u0222aa', 8, 'binary'); testBufs('a\u0234b\u0235c\u0236', 4, 'binary'); testBufs('a\u0234b\u0235c\u0236', 12, 'binary'); testBufs('abc', 4, -1, 'binary'); testBufs('abc', 4, 1, 'binary'); testBufs('abc', 5, 1, 'binary'); testBufs('\u0222aa', 2, -1, 'binary'); testBufs('\u0222aa', 8, 1, 'binary'); testBufs('a\u0234b\u0235c\u0236', 4, -1, 'binary'); testBufs('a\u0234b\u0235c\u0236', 4, 1, 'binary'); testBufs('a\u0234b\u0235c\u0236', 12, 1, 'binary'); // LATIN1 testBufs('abc', 'latin1'); testBufs('\u0222aa', 'latin1'); testBufs('a\u0234b\u0235c\u0236', 'latin1'); testBufs('abc', 4, 'latin1'); testBufs('abc', 5, 'latin1'); testBufs('abc', SIZE, 'latin1'); testBufs('\u0222aa', 2, 'latin1'); testBufs('\u0222aa', 8, 'latin1'); testBufs('a\u0234b\u0235c\u0236', 4, 'latin1'); testBufs('a\u0234b\u0235c\u0236', 12, 'latin1'); testBufs('abc', 4, -1, 'latin1'); testBufs('abc', 4, 1, 'latin1'); testBufs('abc', 5, 1, 'latin1'); testBufs('\u0222aa', 2, -1, 'latin1'); testBufs('\u0222aa', 8, 1, 'latin1'); testBufs('a\u0234b\u0235c\u0236', 4, -1, 'latin1'); testBufs('a\u0234b\u0235c\u0236', 4, 1, 'latin1'); testBufs('a\u0234b\u0235c\u0236', 12, 1, 'latin1'); // UCS2 testBufs('abc', 'ucs2'); testBufs('\u0222aa', 'ucs2'); testBufs('a\u0234b\u0235c\u0236', 'ucs2'); testBufs('abc', 4, 'ucs2'); testBufs('abc', SIZE, 'ucs2'); testBufs('\u0222aa', 2, 'ucs2'); testBufs('\u0222aa', 8, 'ucs2'); testBufs('a\u0234b\u0235c\u0236', 4, 'ucs2'); testBufs('a\u0234b\u0235c\u0236', 12, 'ucs2'); testBufs('abc', 4, -1, 'ucs2'); testBufs('abc', 4, 1, 'ucs2'); testBufs('abc', 5, 1, 'ucs2'); testBufs('\u0222aa', 2, -1, 'ucs2'); testBufs('\u0222aa', 8, 1, 'ucs2'); testBufs('a\u0234b\u0235c\u0236', 4, -1, 'ucs2'); testBufs('a\u0234b\u0235c\u0236', 4, 1, 'ucs2'); testBufs('a\u0234b\u0235c\u0236', 12, 1, 'ucs2'); assert.equal(Buffer.allocUnsafe(1).fill('\u0222', 'ucs2')[0], os.endianness() === 'LE' ? 0x22 : 0x02); // HEX testBufs('616263', 'hex'); testBufs('c8a26161', 'hex'); testBufs('61c8b462c8b563c8b6', 'hex'); testBufs('616263', 4, 'hex'); testBufs('616263', 5, 'hex'); testBufs('616263', SIZE, 'hex'); testBufs('c8a26161', 2, 'hex'); testBufs('c8a26161', 8, 'hex'); testBufs('61c8b462c8b563c8b6', 4, 'hex'); testBufs('61c8b462c8b563c8b6', 12, 'hex'); testBufs('616263', 4, -1, 'hex'); testBufs('616263', 4, 1, 'hex'); testBufs('616263', 5, 1, 'hex'); testBufs('c8a26161', 2, -1, 'hex'); testBufs('c8a26161', 8, 1, 'hex'); testBufs('61c8b462c8b563c8b6', 4, -1, 'hex'); testBufs('61c8b462c8b563c8b6', 4, 1, 'hex'); testBufs('61c8b462c8b563c8b6', 12, 1, 'hex'); common.expectsError(() => { const buf = Buffer.allocUnsafe(SIZE); buf.fill('yKJh', 'hex'); }, { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); common.expectsError(() => { const buf = Buffer.allocUnsafe(SIZE); buf.fill('\u0222', 'hex'); }, { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); // BASE64 testBufs('YWJj', 'ucs2'); testBufs('yKJhYQ==', 'ucs2'); testBufs('Yci0Ysi1Y8i2', 'ucs2'); testBufs('YWJj', 4, 'ucs2'); testBufs('YWJj', SIZE, 'ucs2'); testBufs('yKJhYQ==', 2, 'ucs2'); testBufs('yKJhYQ==', 8, 'ucs2'); testBufs('Yci0Ysi1Y8i2', 4, 'ucs2'); testBufs('Yci0Ysi1Y8i2', 12, 'ucs2'); testBufs('YWJj', 4, -1, 'ucs2'); testBufs('YWJj', 4, 1, 'ucs2'); testBufs('YWJj', 5, 1, 'ucs2'); testBufs('yKJhYQ==', 2, -1, 'ucs2'); testBufs('yKJhYQ==', 8, 1, 'ucs2'); testBufs('Yci0Ysi1Y8i2', 4, -1, 'ucs2'); testBufs('Yci0Ysi1Y8i2', 4, 1, 'ucs2'); testBufs('Yci0Ysi1Y8i2', 12, 1, 'ucs2'); // Buffer function deepStrictEqualValues(buf, arr) { for (var [index, value] of buf.entries()) { assert.deepStrictEqual(value, arr[index]); } } var buf2Fill = Buffer.allocUnsafe(1).fill(2); deepStrictEqualValues(genBuffer(4, [buf2Fill]), [2, 2, 2, 2]); deepStrictEqualValues(genBuffer(4, [buf2Fill, 1]), [0, 2, 2, 2]); deepStrictEqualValues(genBuffer(4, [buf2Fill, 1, 3]), [0, 2, 2, 0]); deepStrictEqualValues(genBuffer(4, [buf2Fill, 1, 1]), [0, 0, 0, 0]); deepStrictEqualValues(genBuffer(4, [buf2Fill, 1, -1]), [0, 0, 0, 0]); var hexBufFill = Buffer.allocUnsafe(2).fill(0).fill('0102', 'hex'); deepStrictEqualValues(genBuffer(4, [hexBufFill]), [1, 2, 1, 2]); deepStrictEqualValues(genBuffer(4, [hexBufFill, 1]), [0, 1, 2, 1]); deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, 3]), [0, 1, 2, 0]); deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, 1]), [0, 0, 0, 0]); deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]); // Check exceptions assert.throws(() => buf1.fill(0, -1)); assert.throws(() => buf1.fill(0, 0, buf1.length + 1)); assert.throws(() => buf1.fill('', -1)); assert.throws(() => buf1.fill('', 0, buf1.length + 1)); assert.throws(() => buf1.fill('a', 0, buf1.length, 'node rocks!')); assert.throws(() => buf1.fill('a', 0, 0, NaN)); assert.throws(() => buf1.fill('a', 0, 0, null)); assert.throws(() => buf1.fill('a', 0, 0, 'foo')); function genBuffer(size, args) { var b = Buffer.allocUnsafe(size); return b.fill(0).fill.apply(b, args); } function bufReset() { buf1.fill(0); buf2.fill(0); } // This is mostly accurate. Except write() won't write partial bytes to the // string while fill() blindly copies bytes into memory. To account for that an // error will be thrown if not all the data can be written, and the SIZE has // been massaged to work with the input characters. function writeToFill(string, offset, end, encoding) { if (typeof offset === 'string') { encoding = offset; offset = 0; end = buf2.length; } else if (typeof end === 'string') { encoding = end; end = buf2.length; } else if (end === undefined) { end = buf2.length; } if (offset < 0 || end > buf2.length) throw new RangeError('Out of range index'); if (end <= offset) return buf2; offset >>>= 0; end >>>= 0; assert(offset <= buf2.length); // Convert "end" to "length" (which write understands). var length = end - offset < 0 ? 0 : end - offset; var wasZero = false; do { var written = buf2.write(string, offset, length, encoding); offset += written; // Safety check in case write falls into infinite loop. if (written === 0) { if (wasZero) throw new Error('Could not write all data to Buffer'); else wasZero = true; } } while (offset < buf2.length); // Correction for UCS2 operations. if (os.endianness() === 'BE' && encoding === 'ucs2') { for (var i = 0; i < buf2.length; i += 2) { var tmp = buf2[i]; buf2[i] = buf2[i + 1]; buf2[i + 1] = tmp; } } return buf2; } function testBufs(string, offset, length, encoding) { bufReset(); buf1.fill.apply(buf1, arguments); // Swap bytes on BE archs for ucs2 encoding. assert.deepStrictEqual(buf1.fill.apply(buf1, arguments), writeToFill.apply(null, arguments)); } buffer-5.4.2/test/node/test-buffer-from.js000066400000000000000000000031731353132250700204310ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const { deepStrictEqual, throws } = require('assert'); const { runInNewContext } = require('vm'); const checkString = 'test'; const check = Buffer.from(checkString); class MyString extends String { constructor() { super(checkString); } } class MyPrimitive { [Symbol.toPrimitive]() { return checkString; } } class MyBadPrimitive { [Symbol.toPrimitive]() { return 1; } } deepStrictEqual(Buffer.from(new String(checkString)), check); deepStrictEqual(Buffer.from(new MyString()), check); deepStrictEqual(Buffer.from(new MyPrimitive()), check); deepStrictEqual( Buffer.from(runInNewContext('new String(checkString)', { checkString })), check ); [ [{}, 'object'], [new Boolean(true), 'boolean'], [{ valueOf() { return null; } }, 'object'], [{ valueOf() { return undefined; } }, 'object'], [{ valueOf: null }, 'object'], [Object.create(null), 'object'] ].forEach(([input, actualType]) => { const err = common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The first argument must be one of type string, Buffer, ' + 'ArrayBuffer, Array, or Array-like Object. Received ' + `type ${actualType}` }); throws(() => Buffer.from(input), err); }); [ new Number(true), new MyBadPrimitive() ].forEach((input) => { const errMsg = common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "value" argument must not be of type number. ' + 'Received type number' }); throws(() => Buffer.from(input), errMsg); }); buffer-5.4.2/test/node/test-buffer-includes.js000066400000000000000000000226611353132250700212770ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; var assert = require('assert'); var Buffer = require('../../').Buffer; var b = Buffer.from('abcdef'); var buf_a = Buffer.from('a'); var buf_bc = Buffer.from('bc'); var buf_f = Buffer.from('f'); var buf_z = Buffer.from('z'); var buf_empty = Buffer.from(''); assert(b.includes('a')); assert(!b.includes('a', 1)); assert(!b.includes('a', -1)); assert(!b.includes('a', -4)); assert(b.includes('a', -b.length)); assert(b.includes('a', NaN)); assert(b.includes('a', -Infinity)); assert(!b.includes('a', Infinity)); assert(b.includes('bc')); assert(!b.includes('bc', 2)); assert(!b.includes('bc', -1)); assert(!b.includes('bc', -3)); assert(b.includes('bc', -5)); assert(b.includes('bc', NaN)); assert(b.includes('bc', -Infinity)); assert(!b.includes('bc', Infinity)); assert(b.includes('f'), b.length - 1); assert(!b.includes('z')); assert(!b.includes('')); assert(!b.includes('', 1)); assert(!b.includes('', b.length + 1)); assert(!b.includes('', Infinity)); assert(b.includes(buf_a)); assert(!b.includes(buf_a, 1)); assert(!b.includes(buf_a, -1)); assert(!b.includes(buf_a, -4)); assert(b.includes(buf_a, -b.length)); assert(b.includes(buf_a, NaN)); assert(b.includes(buf_a, -Infinity)); assert(!b.includes(buf_a, Infinity)); assert(b.includes(buf_bc)); assert(!b.includes(buf_bc, 2)); assert(!b.includes(buf_bc, -1)); assert(!b.includes(buf_bc, -3)); assert(b.includes(buf_bc, -5)); assert(b.includes(buf_bc, NaN)); assert(b.includes(buf_bc, -Infinity)); assert(!b.includes(buf_bc, Infinity)); assert(b.includes(buf_f), b.length - 1); assert(!b.includes(buf_z)); assert(!b.includes(buf_empty)); assert(!b.includes(buf_empty, 1)); assert(!b.includes(buf_empty, b.length + 1)); assert(!b.includes(buf_empty, Infinity)); assert(b.includes(0x61)); assert(!b.includes(0x61, 1)); assert(!b.includes(0x61, -1)); assert(!b.includes(0x61, -4)); assert(b.includes(0x61, -b.length)); assert(b.includes(0x61, NaN)); assert(b.includes(0x61, -Infinity)); assert(!b.includes(0x61, Infinity)); assert(!b.includes(0x0)); // test offsets assert(b.includes('d', 2)); assert(b.includes('f', 5)); assert(b.includes('f', -1)); assert(!b.includes('f', 6)); assert(b.includes(Buffer.from('d'), 2)); assert(b.includes(Buffer.from('f'), 5)); assert(b.includes(Buffer.from('f'), -1)); assert(!b.includes(Buffer.from('f'), 6)); assert(!Buffer.from('ff').includes(Buffer.from('f'), 1, 'ucs2')); // test hex encoding assert.strictEqual( Buffer.from(b.toString('hex'), 'hex') .includes('64', 0, 'hex'), true ); assert.strictEqual( Buffer.from(b.toString('hex'), 'hex') .includes(Buffer.from('64', 'hex'), 0, 'hex'), true ); // test base64 encoding assert.strictEqual( Buffer.from(b.toString('base64'), 'base64') .includes('ZA==', 0, 'base64'), true ); assert.strictEqual( Buffer.from(b.toString('base64'), 'base64') .includes(Buffer.from('ZA==', 'base64'), 0, 'base64'), true ); // test ascii encoding assert.strictEqual( Buffer.from(b.toString('ascii'), 'ascii') .includes('d', 0, 'ascii'), true ); assert.strictEqual( Buffer.from(b.toString('ascii'), 'ascii') .includes(Buffer.from('d', 'ascii'), 0, 'ascii'), true ); // test latin1 encoding assert.strictEqual( Buffer.from(b.toString('latin1'), 'latin1') .includes('d', 0, 'latin1'), true ); assert.strictEqual( Buffer.from(b.toString('latin1'), 'latin1') .includes(Buffer.from('d', 'latin1'), 0, 'latin1'), true ); // test binary encoding assert.strictEqual( Buffer.from(b.toString('binary'), 'binary') .includes('d', 0, 'binary'), true ); assert.strictEqual( Buffer.from(b.toString('binary'), 'binary') .includes(Buffer.from('d', 'binary'), 0, 'binary'), true ); // test usc2 encoding var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); assert(twoByteString.includes('\u0395', 4, 'ucs2')); assert(twoByteString.includes('\u03a3', -4, 'ucs2')); assert(twoByteString.includes('\u03a3', -6, 'ucs2')); assert(twoByteString.includes( Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2')); assert(!twoByteString.includes('\u03a3', -2, 'ucs2')); var mixedByteStringUcs2 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2'); assert(mixedByteStringUcs2.includes('bc', 0, 'ucs2')); assert(mixedByteStringUcs2.includes('\u03a3', 0, 'ucs2')); assert(!mixedByteStringUcs2.includes('\u0396', 0, 'ucs2')); assert( 6, mixedByteStringUcs2.includes(Buffer.from('bc', 'ucs2'), 0, 'ucs2')); assert( 10, mixedByteStringUcs2.includes(Buffer.from('\u03a3', 'ucs2'), 0, 'ucs2')); assert( -1, mixedByteStringUcs2.includes(Buffer.from('\u0396', 'ucs2'), 0, 'ucs2')); twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); // Test single char pattern assert(twoByteString.includes('\u039a', 0, 'ucs2')); assert(twoByteString.includes('\u0391', 0, 'ucs2'), 'Alpha'); assert(twoByteString.includes('\u03a3', 0, 'ucs2'), 'First Sigma'); assert(twoByteString.includes('\u03a3', 6, 'ucs2'), 'Second Sigma'); assert(twoByteString.includes('\u0395', 0, 'ucs2'), 'Epsilon'); assert(!twoByteString.includes('\u0392', 0, 'ucs2'), 'Not beta'); // Test multi-char pattern assert(twoByteString.includes('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha'); assert(twoByteString.includes('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma'); assert(twoByteString.includes('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma'); assert(twoByteString.includes('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon'); var mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395'); assert(mixedByteStringUtf8.includes('bc')); assert(mixedByteStringUtf8.includes('bc', 5)); assert(mixedByteStringUtf8.includes('bc', -8)); assert(mixedByteStringUtf8.includes('\u03a3')); assert(!mixedByteStringUtf8.includes('\u0396')); // Test complex string includes algorithms. Only trigger for long strings. // Long string that isn't a simple repeat of a shorter string. var longString = 'A'; for (var i = 66; i < 76; i++) { // from 'B' to 'K' longString = longString + String.fromCharCode(i) + longString; } var longBufferString = Buffer.from(longString); // pattern of 15 chars, repeated every 16 chars in long var pattern = 'ABACABADABACABA'; for (var i = 0; i < longBufferString.length - pattern.length; i += 7) { var includes = longBufferString.includes(pattern, i); assert(includes, 'Long ABACABA...-string at index ' + i); } assert(longBufferString.includes('AJABACA'), 'Long AJABACA, First J'); assert(longBufferString.includes('AJABACA', 511), 'Long AJABACA, Second J'); pattern = 'JABACABADABACABA'; assert(longBufferString.includes(pattern), 'Long JABACABA..., First J'); assert(longBufferString.includes(pattern, 512), 'Long JABACABA..., Second J'); // Search for a non-ASCII string in a pure ASCII string. var asciiString = Buffer.from( 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf'); assert(!asciiString.includes('\x2061')); assert(asciiString.includes('leb', 0)); // Search in string containing many non-ASCII chars. var allCodePoints = []; for (var i = 0; i < 65536; i++) allCodePoints[i] = i; var allCharsString = String.fromCharCode.apply(String, allCodePoints); var allCharsBufferUtf8 = Buffer.from(allCharsString); var allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2'); // Search for string long enough to trigger complex search with ASCII pattern // and UC16 subject. assert(!allCharsBufferUtf8.includes('notfound')); assert(!allCharsBufferUcs2.includes('notfound')); // Find substrings in Utf8. var lengths = [1, 3, 15]; // Single char, simple and complex. var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b]; for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { for (var i = 0; i < indices.length; i++) { var index = indices[i]; var length = lengths[lengthIndex]; if (index + length > 0x7F) { length = 2 * length; } if (index + length > 0x7FF) { length = 3 * length; } if (index + length > 0xFFFF) { length = 4 * length; } var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length); assert(index, allCharsBufferUtf8.includes(patternBufferUtf8)); var patternStringUtf8 = patternBufferUtf8.toString(); assert(index, allCharsBufferUtf8.includes(patternStringUtf8)); } } // Find substrings in Usc2. lengths = [2, 4, 16]; // Single char, simple and complex. indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0]; for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { for (var i = 0; i < indices.length; i++) { var index = indices[i] * 2; var length = lengths[lengthIndex]; var patternBufferUcs2 = allCharsBufferUcs2.slice(index, index + length); assert( index, allCharsBufferUcs2.includes(patternBufferUcs2, 0, 'ucs2')); var patternStringUcs2 = patternBufferUcs2.toString('ucs2'); assert( index, allCharsBufferUcs2.includes(patternStringUcs2, 0, 'ucs2')); } } assert.throws(function() { b.includes(function() { }); }); assert.throws(function() { b.includes({}); }); assert.throws(function() { b.includes([]); }); // test truncation of Number arguments to uint8 { var buf = Buffer.from('this is a test'); assert.ok(buf.includes(0x6973)); assert.ok(buf.includes(0x697320)); assert.ok(buf.includes(0x69732069)); assert.ok(buf.includes(0x697374657374)); assert.ok(buf.includes(0x69737374)); assert.ok(buf.includes(0x69737465)); assert.ok(buf.includes(0x69737465)); assert.ok(buf.includes(-140)); assert.ok(buf.includes(-152)); assert.ok(!buf.includes(0xff)); assert.ok(!buf.includes(0xffff)); } buffer-5.4.2/test/node/test-buffer-indexof.js000066400000000000000000000466451353132250700211350ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; var assert = require('assert'); var Buffer = require('../../').Buffer; var b = Buffer.from('abcdef'); var buf_a = Buffer.from('a'); var buf_bc = Buffer.from('bc'); var buf_f = Buffer.from('f'); var buf_z = Buffer.from('z'); var buf_empty = Buffer.from(''); assert.equal(b.indexOf('a'), 0); assert.equal(b.indexOf('a', 1), -1); assert.equal(b.indexOf('a', -1), -1); assert.equal(b.indexOf('a', -4), -1); assert.equal(b.indexOf('a', -b.length), 0); assert.equal(b.indexOf('a', NaN), 0); assert.equal(b.indexOf('a', -Infinity), 0); assert.equal(b.indexOf('a', Infinity), -1); assert.equal(b.indexOf('bc'), 1); assert.equal(b.indexOf('bc', 2), -1); assert.equal(b.indexOf('bc', -1), -1); assert.equal(b.indexOf('bc', -3), -1); assert.equal(b.indexOf('bc', -5), 1); assert.equal(b.indexOf('bc', NaN), 1); assert.equal(b.indexOf('bc', -Infinity), 1); assert.equal(b.indexOf('bc', Infinity), -1); assert.equal(b.indexOf('f'), b.length - 1); assert.equal(b.indexOf('z'), -1); assert.equal(b.indexOf(''), -1); assert.equal(b.indexOf('', 1), -1); assert.equal(b.indexOf('', b.length + 1), -1); assert.equal(b.indexOf('', Infinity), -1); assert.equal(b.indexOf(buf_a), 0); assert.equal(b.indexOf(buf_a, 1), -1); assert.equal(b.indexOf(buf_a, -1), -1); assert.equal(b.indexOf(buf_a, -4), -1); assert.equal(b.indexOf(buf_a, -b.length), 0); assert.equal(b.indexOf(buf_a, NaN), 0); assert.equal(b.indexOf(buf_a, -Infinity), 0); assert.equal(b.indexOf(buf_a, Infinity), -1); assert.equal(b.indexOf(buf_bc), 1); assert.equal(b.indexOf(buf_bc, 2), -1); assert.equal(b.indexOf(buf_bc, -1), -1); assert.equal(b.indexOf(buf_bc, -3), -1); assert.equal(b.indexOf(buf_bc, -5), 1); assert.equal(b.indexOf(buf_bc, NaN), 1); assert.equal(b.indexOf(buf_bc, -Infinity), 1); assert.equal(b.indexOf(buf_bc, Infinity), -1); assert.equal(b.indexOf(buf_f), b.length - 1); assert.equal(b.indexOf(buf_z), -1); assert.equal(b.indexOf(buf_empty), -1); assert.equal(b.indexOf(buf_empty, 1), -1); assert.equal(b.indexOf(buf_empty, b.length + 1), -1); assert.equal(b.indexOf(buf_empty, Infinity), -1); assert.equal(b.indexOf(0x61), 0); assert.equal(b.indexOf(0x61, 1), -1); assert.equal(b.indexOf(0x61, -1), -1); assert.equal(b.indexOf(0x61, -4), -1); assert.equal(b.indexOf(0x61, -b.length), 0); assert.equal(b.indexOf(0x61, NaN), 0); assert.equal(b.indexOf(0x61, -Infinity), 0); assert.equal(b.indexOf(0x61, Infinity), -1); assert.equal(b.indexOf(0x0), -1); // test offsets assert.equal(b.indexOf('d', 2), 3); assert.equal(b.indexOf('f', 5), 5); assert.equal(b.indexOf('f', -1), 5); assert.equal(b.indexOf('f', 6), -1); assert.equal(b.indexOf(Buffer.from('d'), 2), 3); assert.equal(b.indexOf(Buffer.from('f'), 5), 5); assert.equal(b.indexOf(Buffer.from('f'), -1), 5); assert.equal(b.indexOf(Buffer.from('f'), 6), -1); assert.equal(Buffer.from('ff').indexOf(Buffer.from('f'), 1, 'ucs2'), -1); // test hex encoding assert.strictEqual( Buffer.from(b.toString('hex'), 'hex') .indexOf('64', 0, 'hex'), 3 ); assert.strictEqual( Buffer.from(b.toString('hex'), 'hex') .indexOf(Buffer.from('64', 'hex'), 0, 'hex'), 3 ); // test base64 encoding assert.strictEqual( Buffer.from(b.toString('base64'), 'base64') .indexOf('ZA==', 0, 'base64'), 3 ); assert.strictEqual( Buffer.from(b.toString('base64'), 'base64') .indexOf(Buffer.from('ZA==', 'base64'), 0, 'base64'), 3 ); // test ascii encoding assert.strictEqual( Buffer.from(b.toString('ascii'), 'ascii') .indexOf('d', 0, 'ascii'), 3 ); assert.strictEqual( Buffer.from(b.toString('ascii'), 'ascii') .indexOf(Buffer.from('d', 'ascii'), 0, 'ascii'), 3 ); // test latin1 encoding assert.strictEqual( Buffer.from(b.toString('latin1'), 'latin1') .indexOf('d', 0, 'latin1'), 3 ); assert.strictEqual( Buffer.from(b.toString('latin1'), 'latin1') .indexOf(Buffer.from('d', 'latin1'), 0, 'latin1'), 3 ); assert.strictEqual( Buffer.from('aa\u00e8aa', 'latin1') .indexOf('\u00e8', 'latin1'), 2 ); assert.strictEqual( Buffer.from('\u00e8', 'latin1') .indexOf('\u00e8', 'latin1'), 0 ); assert.strictEqual( Buffer.from('\u00e8', 'latin1') .indexOf(Buffer.from('\u00e8', 'latin1'), 'latin1'), 0 ); // test binary encoding assert.strictEqual( Buffer.from(b.toString('binary'), 'binary') .indexOf('d', 0, 'binary'), 3 ); assert.strictEqual( Buffer.from(b.toString('binary'), 'binary') .indexOf(Buffer.from('d', 'binary'), 0, 'binary'), 3 ); assert.strictEqual( Buffer.from('aa\u00e8aa', 'binary') .indexOf('\u00e8', 'binary'), 2 ); assert.strictEqual( Buffer.from('\u00e8', 'binary') .indexOf('\u00e8', 'binary'), 0 ); assert.strictEqual( Buffer.from('\u00e8', 'binary') .indexOf(Buffer.from('\u00e8', 'binary'), 'binary'), 0 ); // test optional offset with passed encoding assert.equal(Buffer.from('aaaa0').indexOf('30', 'hex'), 4); assert.equal(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4); { // test usc2 encoding var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); assert.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2')); assert.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2')); assert.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2')); assert.equal(4, twoByteString.indexOf( Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2')); assert.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2')); } var mixedByteStringUcs2 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2'); assert.equal(6, mixedByteStringUcs2.indexOf('bc', 0, 'ucs2')); assert.equal(10, mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2')); assert.equal(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2')); assert.equal( 6, mixedByteStringUcs2.indexOf(Buffer.from('bc', 'ucs2'), 0, 'ucs2')); assert.equal( 10, mixedByteStringUcs2.indexOf(Buffer.from('\u03a3', 'ucs2'), 0, 'ucs2')); assert.equal( -1, mixedByteStringUcs2.indexOf(Buffer.from('\u0396', 'ucs2'), 0, 'ucs2')); { var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); // Test single char pattern assert.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2')); assert.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha'); assert.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma'); assert.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma'); assert.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon'); assert.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta'); // Test multi-char pattern assert.equal( 0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha'); assert.equal( 2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma'); assert.equal( 4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma'); assert.equal( 6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon'); } var mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395'); assert.equal(5, mixedByteStringUtf8.indexOf('bc')); assert.equal(5, mixedByteStringUtf8.indexOf('bc', 5)); assert.equal(5, mixedByteStringUtf8.indexOf('bc', -8)); assert.equal(7, mixedByteStringUtf8.indexOf('\u03a3')); assert.equal(-1, mixedByteStringUtf8.indexOf('\u0396')); // Test complex string indexOf algorithms. Only trigger for long strings. // Long string that isn't a simple repeat of a shorter string. var longString = 'A'; for (var i = 66; i < 76; i++) { // from 'B' to 'K' longString = longString + String.fromCharCode(i) + longString; } var longBufferString = Buffer.from(longString); // pattern of 15 chars, repeated every 16 chars in long var pattern = 'ABACABADABACABA'; for (var i = 0; i < longBufferString.length - pattern.length; i += 7) { var index = longBufferString.indexOf(pattern, i); assert.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i); } assert.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J'); assert.equal( 1534, longBufferString.indexOf('AJABACA', 511), 'Long AJABACA, Second J'); pattern = 'JABACABADABACABA'; assert.equal( 511, longBufferString.indexOf(pattern), 'Long JABACABA..., First J'); assert.equal( 1535, longBufferString.indexOf(pattern, 512), 'Long JABACABA..., Second J'); // Search for a non-ASCII string in a pure ASCII string. var asciiString = Buffer.from( 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf'); assert.equal(-1, asciiString.indexOf('\x2061')); assert.equal(3, asciiString.indexOf('leb', 0)); // Search in string containing many non-ASCII chars. var allCodePoints = []; for (var i = 0; i < 65536; i++) allCodePoints[i] = i; var allCharsString = String.fromCharCode.apply(String, allCodePoints); var allCharsBufferUtf8 = Buffer.from(allCharsString); var allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2'); // Search for string long enough to trigger complex search with ASCII pattern // and UC16 subject. assert.equal(-1, allCharsBufferUtf8.indexOf('notfound')); assert.equal(-1, allCharsBufferUcs2.indexOf('notfound')); // Needle is longer than haystack, but only because it's encoded as UTF-16 assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1); assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'utf8'), 0); assert.strictEqual(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1); // Haystack has odd length, but the needle is UCS2. // assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1); { // Find substrings in Utf8. var lengths = [1, 3, 15]; // Single char, simple and complex. var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b]; for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { for (var i = 0; i < indices.length; i++) { var index = indices[i]; var length = lengths[lengthIndex]; if (index + length > 0x7F) { length = 2 * length; } if (index + length > 0x7FF) { length = 3 * length; } if (index + length > 0xFFFF) { length = 4 * length; } var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length); assert.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8)); var patternStringUtf8 = patternBufferUtf8.toString(); assert.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8)); } } } { // Find substrings in Usc2. var lengths = [2, 4, 16]; // Single char, simple and complex. var indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0]; for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { for (var i = 0; i < indices.length; i++) { var index = indices[i] * 2; var length = lengths[lengthIndex]; var patternBufferUcs2 = allCharsBufferUcs2.slice(index, index + length); assert.equal( index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2')); var patternStringUcs2 = patternBufferUcs2.toString('ucs2'); assert.equal( index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2')); } } } assert.throws(function() { b.indexOf(function() { }); }); assert.throws(function() { b.indexOf({}); }); assert.throws(function() { b.indexOf([]); }); // All code for handling encodings is shared between Buffer.indexOf and // Buffer.lastIndexOf, so only testing the separate lastIndexOf semantics. // Test lastIndexOf basic functionality; Buffer b contains 'abcdef'. // lastIndexOf string: assert.equal(b.lastIndexOf('a'), 0); assert.equal(b.lastIndexOf('a', 1), 0); assert.equal(b.lastIndexOf('b', 1), 1); assert.equal(b.lastIndexOf('c', 1), -1); assert.equal(b.lastIndexOf('a', -1), 0); assert.equal(b.lastIndexOf('a', -4), 0); assert.equal(b.lastIndexOf('a', -b.length), 0); assert.equal(b.lastIndexOf('a', -b.length - 1), -1); assert.equal(b.lastIndexOf('a', NaN), 0); assert.equal(b.lastIndexOf('a', -Infinity), -1); assert.equal(b.lastIndexOf('a', Infinity), 0); // lastIndexOf Buffer: assert.equal(b.lastIndexOf(buf_a), 0); assert.equal(b.lastIndexOf(buf_a, 1), 0); assert.equal(b.lastIndexOf(buf_a, -1), 0); assert.equal(b.lastIndexOf(buf_a, -4), 0); assert.equal(b.lastIndexOf(buf_a, -b.length), 0); assert.equal(b.lastIndexOf(buf_a, -b.length - 1), -1); assert.equal(b.lastIndexOf(buf_a, NaN), 0); assert.equal(b.lastIndexOf(buf_a, -Infinity), -1); assert.equal(b.lastIndexOf(buf_a, Infinity), 0); assert.equal(b.lastIndexOf(buf_bc), 1); assert.equal(b.lastIndexOf(buf_bc, 2), 1); assert.equal(b.lastIndexOf(buf_bc, -1), 1); assert.equal(b.lastIndexOf(buf_bc, -3), 1); assert.equal(b.lastIndexOf(buf_bc, -5), 1); assert.equal(b.lastIndexOf(buf_bc, -6), -1); assert.equal(b.lastIndexOf(buf_bc, NaN), 1); assert.equal(b.lastIndexOf(buf_bc, -Infinity), -1); assert.equal(b.lastIndexOf(buf_bc, Infinity), 1); assert.equal(b.lastIndexOf(buf_f), b.length - 1); assert.equal(b.lastIndexOf(buf_z), -1); assert.equal(b.lastIndexOf(buf_empty), -1); assert.equal(b.lastIndexOf(buf_empty, 1), -1); assert.equal(b.lastIndexOf(buf_empty, b.length + 1), -1); assert.equal(b.lastIndexOf(buf_empty, Infinity), -1); // lastIndexOf number: assert.equal(b.lastIndexOf(0x61), 0); assert.equal(b.lastIndexOf(0x61, 1), 0); assert.equal(b.lastIndexOf(0x61, -1), 0); assert.equal(b.lastIndexOf(0x61, -4), 0); assert.equal(b.lastIndexOf(0x61, -b.length), 0); assert.equal(b.lastIndexOf(0x61, -b.length - 1), -1); assert.equal(b.lastIndexOf(0x61, NaN), 0); assert.equal(b.lastIndexOf(0x61, -Infinity), -1); assert.equal(b.lastIndexOf(0x61, Infinity), 0); assert.equal(b.lastIndexOf(0x0), -1); // Test weird offset arguments. // Behaviour should match String.lastIndexOf: assert.equal(b.lastIndexOf('b', 0), -1); assert.equal(b.lastIndexOf('b', undefined), 1); assert.equal(b.lastIndexOf('b', null), -1); assert.equal(b.lastIndexOf('b', {}), 1); assert.equal(b.lastIndexOf('b', []), -1); assert.equal(b.lastIndexOf('b', [2]), 1); // Test needles longer than the haystack. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'ucs2'), -1); assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'utf8'), -1); assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'latin1'), -1); assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'binary'), -1); assert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa')), -1); assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 2, 'ucs2'), -1); assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 3, 'utf8'), -1); assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'latin1'), -1); assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'binary'), -1); assert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa'), 7), -1); // 你好 expands to a total of 6 bytes using UTF-8 and 4 bytes using UTF-16 assert.strictEqual(buf_bc.lastIndexOf('你好', 'ucs2'), -1); assert.strictEqual(buf_bc.lastIndexOf('你好', 'utf8'), -1); assert.strictEqual(buf_bc.lastIndexOf('你好', 'latin1'), -1); assert.strictEqual(buf_bc.lastIndexOf('你好', 'binary'), -1); assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好')), -1); assert.strictEqual(buf_bc.lastIndexOf('你好', 2, 'ucs2'), -1); assert.strictEqual(buf_bc.lastIndexOf('你好', 3, 'utf8'), -1); assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'latin1'), -1); assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'binary'), -1); assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好'), 7), -1); // Test lastIndexOf on a longer buffer: var bufferString = new Buffer('a man a plan a canal panama'); assert.equal(15, bufferString.lastIndexOf('canal')); assert.equal(21, bufferString.lastIndexOf('panama')); assert.equal(0, bufferString.lastIndexOf('a man a plan a canal panama')); assert.equal(-1, bufferString.lastIndexOf('a man a plan a canal mexico')); assert.equal(-1, bufferString.lastIndexOf('a man a plan a canal mexico city')); assert.equal(-1, bufferString.lastIndexOf(Buffer.from('a'.repeat(1000)))); assert.equal(0, bufferString.lastIndexOf('a man a plan', 4)); assert.equal(13, bufferString.lastIndexOf('a ')); assert.equal(13, bufferString.lastIndexOf('a ', 13)); assert.equal(6, bufferString.lastIndexOf('a ', 12)); assert.equal(0, bufferString.lastIndexOf('a ', 5)); assert.equal(13, bufferString.lastIndexOf('a ', -1)); assert.equal(0, bufferString.lastIndexOf('a ', -27)); assert.equal(-1, bufferString.lastIndexOf('a ', -28)); // Test lastIndexOf for the case that the first character can be found, // but in a part of the buffer that does not make search to search // due do length constraints. var abInUCS2 = Buffer.from('ab', 'ucs2'); assert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'latin1').lastIndexOf('µ')); assert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'binary').lastIndexOf('µ')); assert.strictEqual(-1, Buffer.from('bc').lastIndexOf('ab')); assert.strictEqual(-1, Buffer.from('abc').lastIndexOf('qa')); assert.strictEqual(-1, Buffer.from('abcdef').lastIndexOf('qabc')); assert.strictEqual(-1, Buffer.from('bc').lastIndexOf(Buffer.from('ab'))); assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf('ab', 'ucs2')); assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf(abInUCS2)); assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab')); assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 1)); assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 2)); assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 3)); // The above tests test the LINEAR and SINGLE-CHAR strategies. // Now, we test the BOYER-MOORE-HORSPOOL strategy. // Test lastIndexOf on a long buffer w multiple matches: pattern = 'JABACABADABACABA'; assert.equal(1535, longBufferString.lastIndexOf(pattern)); assert.equal(1535, longBufferString.lastIndexOf(pattern, 1535)); assert.equal(511, longBufferString.lastIndexOf(pattern, 1534)); // Finally, give it a really long input to trigger fallback from BMH to // regular BOYER-MOORE (which has better worst-case complexity). // Generate a really long Thue-Morse sequence of 'yolo' and 'swag', // "yolo swag swag yolo swag yolo yolo swag" ..., goes on for about 5MB. // This is hard to search because it all looks similar, but never repeats. // countBits returns the number of bits in the binary reprsentation of n. function countBits(n) { for (var count = 0; n > 0; count++) { n = n & (n - 1); // remove top bit } return count; } var parts = []; for (var i = 0; i < 1000000; i++) { parts.push((countBits(i) % 2 === 0) ? 'yolo' : 'swag'); } var reallyLong = new Buffer(parts.join(' ')); assert.equal('yolo swag swag yolo', reallyLong.slice(0, 19).toString()); // Expensive reverse searches. Stress test lastIndexOf: pattern = reallyLong.slice(0, 100000); // First 1/50th of the pattern. assert.equal(4751360, reallyLong.lastIndexOf(pattern)); assert.equal(3932160, reallyLong.lastIndexOf(pattern, 4000000)); assert.equal(2949120, reallyLong.lastIndexOf(pattern, 3000000)); pattern = reallyLong.slice(100000, 200000); // Second 1/50th. assert.equal(4728480, reallyLong.lastIndexOf(pattern)); pattern = reallyLong.slice(0, 1000000); // First 1/5th. assert.equal(3932160, reallyLong.lastIndexOf(pattern)); pattern = reallyLong.slice(0, 2000000); // first 2/5ths. assert.equal(0, reallyLong.lastIndexOf(pattern)); // test truncation of Number arguments to uint8 { var buf = Buffer.from('this is a test'); assert.strictEqual(buf.indexOf(0x6973), 3); assert.strictEqual(buf.indexOf(0x697320), 4); assert.strictEqual(buf.indexOf(0x69732069), 2); assert.strictEqual(buf.indexOf(0x697374657374), 0); assert.strictEqual(buf.indexOf(0x69737374), 0); assert.strictEqual(buf.indexOf(0x69737465), 11); assert.strictEqual(buf.indexOf(0x69737465), 11); assert.strictEqual(buf.indexOf(-140), 0); assert.strictEqual(buf.indexOf(-152), 1); assert.strictEqual(buf.indexOf(0xff), -1); assert.strictEqual(buf.indexOf(0xffff), -1); } buffer-5.4.2/test/node/test-buffer-inheritance.js000066400000000000000000000015721353132250700217600ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); function T(n) { const ui8 = new Uint8Array(n); Object.setPrototypeOf(ui8, T.prototype); return ui8; } Object.setPrototypeOf(T.prototype, Buffer.prototype); Object.setPrototypeOf(T, Buffer); T.prototype.sum = function sum() { let cntr = 0; for (let i = 0; i < this.length; i++) cntr += this[i]; return cntr; }; const vals = [new T(4), T(4)]; vals.forEach(function(t) { assert.strictEqual(t.constructor, T); assert.strictEqual(Object.getPrototypeOf(t), T.prototype); assert.strictEqual(Object.getPrototypeOf(Object.getPrototypeOf(t)), Buffer.prototype); t.fill(5); let cntr = 0; for (let i = 0; i < t.length; i++) cntr += t[i]; assert.strictEqual(t.length * 5, cntr); // Check this does not throw t.toString(); }); buffer-5.4.2/test/node/test-buffer-inspect.js000066400000000000000000000040301353132250700211240ustar00rootroot00000000000000// Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit // persons to whom the Software is furnished to do so, subject to the // following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); const util = require('util'); const buffer = require('../../'); var defaultMaxBytes = buffer.INSPECT_MAX_BYTES; buffer.INSPECT_MAX_BYTES = 2; let b = Buffer.allocUnsafe(4); b.fill('1234'); let s = buffer.SlowBuffer(4); s.fill('1234'); let expected = ''; assert.strictEqual(util.inspect(b), expected); assert.strictEqual(util.inspect(s), expected); b = Buffer.allocUnsafe(2); b.fill('12'); s = buffer.SlowBuffer(2); s.fill('12'); expected = ''; assert.strictEqual(util.inspect(b), expected); assert.strictEqual(util.inspect(s), expected); buffer.INSPECT_MAX_BYTES = Infinity; assert.strictEqual(util.inspect(b), expected); assert.strictEqual(util.inspect(s), expected); b.inspect = undefined; assert.strictEqual(util.inspect(b), expected); buffer.INSPECT_MAX_BYTES = defaultMaxBytes; buffer-5.4.2/test/node/test-buffer-isencoding.js000066400000000000000000000007711353132250700216110ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); [ 'hex', 'utf8', 'utf-8', 'ascii', 'latin1', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le' ].forEach((enc) => { assert.strictEqual(Buffer.isEncoding(enc), true); }); [ 'utf9', 'utf-7', 'Unicode-FTW', 'new gnu gun', false, NaN, {}, Infinity, [], 1, 0, -1 ].forEach((enc) => { assert.strictEqual(Buffer.isEncoding(enc), false); }); buffer-5.4.2/test/node/test-buffer-iterator.js000066400000000000000000000016641353132250700213220ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); const buffer = Buffer.from([1, 2, 3, 4, 5]); let arr; let b; // buffers should be iterable arr = []; for (b of buffer) arr.push(b); assert.deepStrictEqual(arr, [1, 2, 3, 4, 5]); // buffer iterators should be iterable arr = []; for (b of buffer[Symbol.iterator]()) arr.push(b); assert.deepStrictEqual(arr, [1, 2, 3, 4, 5]); // buffer#values() should return iterator for values arr = []; for (b of buffer.values()) arr.push(b); assert.deepStrictEqual(arr, [1, 2, 3, 4, 5]); // buffer#keys() should return iterator for keys arr = []; for (b of buffer.keys()) arr.push(b); assert.deepStrictEqual(arr, [0, 1, 2, 3, 4]); // buffer#entries() should return iterator for entries arr = []; for (b of buffer.entries()) arr.push(b); assert.deepStrictEqual(arr, [ [0, 1], [1, 2], [2, 3], [3, 4], [4, 5] ]); buffer-5.4.2/test/node/test-buffer-new.js000066400000000000000000000004271353132250700202560ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); common.expectsError(() => new Buffer(42, 'utf8'), { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "string" argument must be of type string. Received type number' }); buffer-5.4.2/test/node/test-buffer-parent-property.js000066400000000000000000000016051353132250700226370ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; /* * Fix for https://github.com/nodejs/node/issues/8266 * * Zero length Buffer objects should expose the `buffer` property of the * TypedArrays, via the `parent` property. */ require('./common'); const assert = require('assert'); // If the length of the buffer object is zero assert((new Buffer(0)).parent instanceof ArrayBuffer); // If the length of the buffer object is equal to the underlying ArrayBuffer assert((new Buffer(Buffer.poolSize)).parent instanceof ArrayBuffer); // Same as the previous test, but with user created buffer const arrayBuffer = new ArrayBuffer(0); assert.strictEqual(new Buffer(arrayBuffer).parent, arrayBuffer); assert.strictEqual(new Buffer(arrayBuffer).buffer, arrayBuffer); assert.strictEqual(Buffer.from(arrayBuffer).parent, arrayBuffer); assert.strictEqual(Buffer.from(arrayBuffer).buffer, arrayBuffer); buffer-5.4.2/test/node/test-buffer-prototype-inspect.js000066400000000000000000000011121353132250700231650ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); // lib/buffer.js defines Buffer.prototype.inspect() to override how buffers are // presented by util.inspect(). const assert = require('assert'); const util = require('util'); { const buf = Buffer.from('fhqwhgads'); assert.strictEqual(util.inspect(buf), ''); } { const buf = Buffer.from(''); assert.strictEqual(util.inspect(buf), ''); } { const buf = Buffer.from('x'.repeat(51)); assert.ok(/^$/.test(util.inspect(buf))); } buffer-5.4.2/test/node/test-buffer-safe-unsafe.js000066400000000000000000000010631353132250700216570ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); const safe = Buffer.alloc(10); function isZeroFilled(buf) { for (let n = 0; n < buf.length; n++) if (buf[n] !== 0) return false; return true; } assert(isZeroFilled(safe)); // Test that unsafe allocations doesn't affect subsequent safe allocations Buffer.allocUnsafe(10); assert(isZeroFilled(new Float64Array(10))); new Buffer(10); assert(isZeroFilled(new Float64Array(10))); Buffer.allocUnsafe(10); assert(isZeroFilled(Buffer.alloc(10))); buffer-5.4.2/test/node/test-buffer-slice.js000066400000000000000000000120721353132250700205630ustar00rootroot00000000000000// Copyright Joyent, Inc. and other Node contributors.var Buffer = require('../../').Buffer; // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit // persons to whom the Software is furnished to do so, subject to the // following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; require('./common'); const assert = require('assert'); assert.strictEqual(0, Buffer.from('hello', 'utf8').slice(0, 0).length); assert.strictEqual(0, Buffer('hello', 'utf8').slice(0, 0).length); const buf = Buffer.from('0123456789', 'utf8'); const expectedSameBufs = [ [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')], [buf.slice(-20, 10), Buffer.from('0123456789', 'utf8')], [buf.slice(-20, -10), Buffer.from('', 'utf8')], [buf.slice(), Buffer.from('0123456789', 'utf8')], [buf.slice(0), Buffer.from('0123456789', 'utf8')], [buf.slice(0, 0), Buffer.from('', 'utf8')], [buf.slice(undefined), Buffer.from('0123456789', 'utf8')], [buf.slice('foobar'), Buffer.from('0123456789', 'utf8')], [buf.slice(undefined, undefined), Buffer.from('0123456789', 'utf8')], [buf.slice(2), Buffer.from('23456789', 'utf8')], [buf.slice(5), Buffer.from('56789', 'utf8')], [buf.slice(10), Buffer.from('', 'utf8')], [buf.slice(5, 8), Buffer.from('567', 'utf8')], [buf.slice(8, -1), Buffer.from('8', 'utf8')], [buf.slice(-10), Buffer.from('0123456789', 'utf8')], [buf.slice(0, -9), Buffer.from('0', 'utf8')], [buf.slice(0, -10), Buffer.from('', 'utf8')], [buf.slice(0, -1), Buffer.from('012345678', 'utf8')], [buf.slice(2, -2), Buffer.from('234567', 'utf8')], [buf.slice(0, 65536), Buffer.from('0123456789', 'utf8')], [buf.slice(65536, 0), Buffer.from('', 'utf8')], [buf.slice(-5, -8), Buffer.from('', 'utf8')], [buf.slice(-5, -3), Buffer.from('56', 'utf8')], [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')], [buf.slice('0', '1'), Buffer.from('0', 'utf8')], [buf.slice('-5', '10'), Buffer.from('56789', 'utf8')], [buf.slice('-10', '10'), Buffer.from('0123456789', 'utf8')], [buf.slice('-10', '-5'), Buffer.from('01234', 'utf8')], [buf.slice('-10', '-0'), Buffer.from('', 'utf8')], [buf.slice('111'), Buffer.from('', 'utf8')], [buf.slice('0', '-111'), Buffer.from('', 'utf8')] ]; for (let i = 0, s = buf.toString(); i < buf.length; ++i) { expectedSameBufs.push( [buf.slice(i), Buffer.from(s.slice(i))], [buf.slice(0, i), Buffer.from(s.slice(0, i))], [buf.slice(-i), Buffer.from(s.slice(-i))], [buf.slice(0, -i), Buffer.from(s.slice(0, -i))] ); } expectedSameBufs.forEach(([buf1, buf2]) => { assert.strictEqual(0, Buffer.compare(buf1, buf2)); }); const utf16Buf = Buffer.from('0123456789', 'utf16le'); assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le')); // try to slice a zero length Buffer // see https://github.com/joyent/node/issues/5881 assert.doesNotThrow(() => Buffer.alloc(0).slice(0, 1)); assert.strictEqual(Buffer.alloc(0).slice(0, 1).length, 0); { // Single argument slice assert.strictEqual('bcde', Buffer.from('abcde', 'utf8').slice(1).toString('utf8')); } // slice(0,0).length === 0 assert.strictEqual(0, Buffer.from('hello', 'utf8').slice(0, 0).length); { // Regression tests for https://github.com/nodejs/node/issues/9096 const buf = Buffer.from('abcd', 'utf8'); assert.strictEqual(buf.slice(buf.length / 3).toString('utf8'), 'bcd'); assert.strictEqual( buf.slice(buf.length / 3, buf.length).toString(), 'bcd' ); } { const buf = Buffer.from('abcdefg', 'utf8'); assert.strictEqual(buf.slice(-(-1 >>> 0) - 1).toString('utf8'), buf.toString('utf8')); } { const buf = Buffer.from('abc', 'utf8'); assert.strictEqual(buf.slice(-0.5).toString('utf8'), buf.toString('utf8')); } { const buf = Buffer.from([ 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0, 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0 ]); const chunk1 = Buffer.from([ 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0 ]); const chunk2 = Buffer.from([ 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0 ]); const middle = buf.length / 2; assert.deepStrictEqual(buf.slice(0, middle), chunk1); assert.deepStrictEqual(buf.slice(middle), chunk2); } buffer-5.4.2/test/node/test-buffer-slow.js000066400000000000000000000037541353132250700204570ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const assert = require('assert'); const buffer = require('../../'); const SlowBuffer = buffer.SlowBuffer; const ones = [1, 1, 1, 1]; // should create a Buffer let sb = SlowBuffer(4); assert(sb instanceof Buffer); assert.strictEqual(sb.length, 4); sb.fill(1); for (const [key, value] of sb.entries()) { assert.deepStrictEqual(value, ones[key]); } // underlying ArrayBuffer should have the same length assert.strictEqual(sb.buffer.byteLength, 4); // should work without new sb = SlowBuffer(4); assert(sb instanceof Buffer); assert.strictEqual(sb.length, 4); sb.fill(1); for (const [key, value] of sb.entries()) { assert.deepStrictEqual(value, ones[key]); } // should work with edge cases assert.strictEqual(SlowBuffer(0).length, 0); try { assert.strictEqual( SlowBuffer(buffer.kMaxLength).length, buffer.kMaxLength); } catch (e) { // Don't match on message as it is from the JavaScript engine. V8 and // ChakraCore provide different messages. assert.strictEqual(e.name, 'RangeError'); } // should work with number-coercible values assert.strictEqual(SlowBuffer('6').length, 6); assert.strictEqual(SlowBuffer(true).length, 1); // should create zero-length buffer if parameter is not a number assert.strictEqual(SlowBuffer().length, 0); assert.strictEqual(SlowBuffer(NaN).length, 0); assert.strictEqual(SlowBuffer({}).length, 0); assert.strictEqual(SlowBuffer('string').length, 0); // should throw with invalid length const bufferMaxSizeMsg = common.expectsError({ code: 'ERR_INVALID_OPT_VALUE', type: RangeError, message: /^The value "[^"]*" is invalid for option "size"$/ }, 2); assert.throws(function() { SlowBuffer(Infinity); }, bufferMaxSizeMsg); common.expectsError(function() { SlowBuffer(-1); }, { code: 'ERR_INVALID_OPT_VALUE', type: RangeError, message: 'The value "-1" is invalid for option "size"' }); assert.throws(function() { SlowBuffer(buffer.kMaxLength + 1); }, bufferMaxSizeMsg); buffer-5.4.2/test/node/test-buffer-swap.js000066400000000000000000000133061353132250700204370ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); // Test buffers small enough to use the JS implementation { const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); assert.strictEqual(buf, buf.swap16()); assert.deepStrictEqual(buf, Buffer.from([0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b, 0x0e, 0x0d, 0x10, 0x0f])); buf.swap16(); // restore assert.strictEqual(buf, buf.swap32()); assert.deepStrictEqual(buf, Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07, 0x06, 0x05, 0x0c, 0x0b, 0x0a, 0x09, 0x10, 0x0f, 0x0e, 0x0d])); buf.swap32(); // restore assert.strictEqual(buf, buf.swap64()); assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09])); } // Operates in-place { const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]); buf.slice(1, 5).swap32(); assert.deepStrictEqual(buf, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7])); buf.slice(1, 5).swap16(); assert.deepStrictEqual(buf, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7])); // Length assertions const re16 = /Buffer size must be a multiple of 16-bits/; const re32 = /Buffer size must be a multiple of 32-bits/; const re64 = /Buffer size must be a multiple of 64-bits/; assert.throws(() => Buffer.from(buf).swap16(), re16); assert.throws(() => Buffer.alloc(1025).swap16(), re16); assert.throws(() => Buffer.from(buf).swap32(), re32); assert.throws(() => buf.slice(1, 3).swap32(), re32); assert.throws(() => Buffer.alloc(1025).swap32(), re32); assert.throws(() => buf.slice(1, 3).swap64(), re64); assert.throws(() => Buffer.alloc(1025).swap64(), re64); } { const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); buf.slice(2, 18).swap64(); assert.deepStrictEqual(buf, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10])); } // Force use of native code (Buffer size above threshold limit for js impl) { const bufData = new Uint32Array(256).fill(0x04030201); const buf = Buffer.from(bufData.buffer, bufData.byteOffset); const otherBufData = new Uint32Array(256).fill(0x03040102); const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); buf.swap16(); assert.deepStrictEqual(buf, otherBuf); } { const bufData = new Uint32Array(256).fill(0x04030201); const buf = Buffer.from(bufData.buffer); const otherBufData = new Uint32Array(256).fill(0x01020304); const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); buf.swap32(); assert.deepStrictEqual(buf, otherBuf); } { const bufData = new Uint8Array(256 * 8); const otherBufData = new Uint8Array(256 * 8); for (let i = 0; i < bufData.length; i++) { bufData[i] = i % 8; otherBufData[otherBufData.length - i - 1] = i % 8; } const buf = Buffer.from(bufData.buffer, bufData.byteOffset); const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); buf.swap64(); assert.deepStrictEqual(buf, otherBuf); } // Test native code with buffers that are not memory-aligned { const bufData = new Uint8Array(256 * 8); const otherBufData = new Uint8Array(256 * 8 - 2); for (let i = 0; i < bufData.length; i++) { bufData[i] = i % 2; } for (let i = 1; i < otherBufData.length; i++) { otherBufData[otherBufData.length - i] = (i + 1) % 2; } const buf = Buffer.from(bufData.buffer, bufData.byteOffset); // 0|1 0|1 0|1... const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); // 0|0 1|0 1|0... buf.slice(1, buf.length - 1).swap16(); assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf); } { const bufData = new Uint8Array(256 * 8); const otherBufData = new Uint8Array(256 * 8 - 4); for (let i = 0; i < bufData.length; i++) { bufData[i] = i % 4; } for (let i = 1; i < otherBufData.length; i++) { otherBufData[otherBufData.length - i] = (i + 1) % 4; } const buf = Buffer.from(bufData.buffer, bufData.byteOffset); // 0|1 2 3 0|1 2 3... const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); // 0|0 3 2 1|0 3 2... buf.slice(1, buf.length - 3).swap32(); assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf); } { const bufData = new Uint8Array(256 * 8); const otherBufData = new Uint8Array(256 * 8 - 8); for (let i = 0; i < bufData.length; i++) { bufData[i] = i % 8; } for (let i = 1; i < otherBufData.length; i++) { otherBufData[otherBufData.length - i] = (i + 1) % 8; } const buf = Buffer.from(bufData.buffer, bufData.byteOffset); // 0|1 2 3 4 5 6 7 0|1 2 3 4... const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); // 0|0 7 6 5 4 3 2 1|0 7 6 5... buf.slice(1, buf.length - 7).swap64(); assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf); } buffer-5.4.2/test/node/test-buffer-tojson.js000066400000000000000000000016011353132250700207740ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); { assert.strictEqual(JSON.stringify(Buffer.alloc(0)), '{"type":"Buffer","data":[]}'); assert.strictEqual(JSON.stringify(Buffer.from([1, 2, 3, 4])), '{"type":"Buffer","data":[1,2,3,4]}'); } // issue GH-7849 { const buf = Buffer.from('test'); const json = JSON.stringify(buf); const obj = JSON.parse(json); const copy = Buffer.from(obj); assert.deepStrictEqual(buf, copy); } // GH-5110 { const buffer = Buffer.from('test'); const string = JSON.stringify(buffer); assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}'); function receiver(key, value) { return value && value.type === 'Buffer' ? Buffer.from(value.data) : value; } assert.deepStrictEqual(buffer, JSON.parse(string, receiver)); } buffer-5.4.2/test/node/test-buffer-tostring.js000066400000000000000000000021341353132250700213330ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const assert = require('assert'); // utf8, ucs2, ascii, latin1, utf16le const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', 'binary', 'utf16le', 'utf-16le']; encodings .reduce((es, e) => es.concat(e, e.toUpperCase()), []) .forEach((encoding) => { assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo'); }); // base64 ['base64', 'BASE64'].forEach((encoding) => { assert.strictEqual(Buffer.from('Zm9v', encoding).toString(encoding), 'Zm9v'); }); // hex ['hex', 'HEX'].forEach((encoding) => { assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding), '666f6f'); }); // Invalid encodings for (let i = 1; i < 10; i++) { const encoding = String(i).repeat(i); const error = common.expectsError({ code: 'ERR_UNKNOWN_ENCODING', type: TypeError, message: `Unknown encoding: ${encoding}` }); assert.ok(!Buffer.isEncoding(encoding)); assert.throws(() => Buffer.from('foo').toString(encoding), error); } buffer-5.4.2/test/node/test-buffer-write.js000066400000000000000000000045551353132250700206250ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; const common = require('./common'); const assert = require('assert'); const outsideBounds = common.expectsError({ code: 'ERR_BUFFER_OUT_OF_BOUNDS', type: RangeError, message: 'Attempt to write outside buffer bounds' }, 2); assert.throws(() => Buffer.alloc(9).write('foo', -1), outsideBounds); assert.throws(() => Buffer.alloc(9).write('foo', 10), outsideBounds); const resultMap = new Map([ ['utf8', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['ucs2', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])], ['ascii', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['latin1', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['binary', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['utf16le', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])], ['base64', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], ['hex', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])] ]); // utf8, ucs2, ascii, latin1, utf16le const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', 'binary', 'utf16le', 'utf-16le']; encodings .reduce((es, e) => es.concat(e, e.toUpperCase()), []) .forEach((encoding) => { const buf = Buffer.alloc(9); const len = Buffer.byteLength('foo', encoding); assert.strictEqual(buf.write('foo', 0, len, encoding), len); if (encoding.includes('-')) encoding = encoding.replace('-', ''); assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); }); // base64 ['base64', 'BASE64'].forEach((encoding) => { const buf = Buffer.alloc(9); const len = Buffer.byteLength('Zm9v', encoding); assert.strictEqual(buf.write('Zm9v', 0, len, encoding), len); assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); }); // hex ['hex', 'HEX'].forEach((encoding) => { const buf = Buffer.alloc(9); const len = Buffer.byteLength('666f6f', encoding); assert.strictEqual(buf.write('666f6f', 0, len, encoding), len); assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); }); // Invalid encodings for (let i = 1; i < 10; i++) { const encoding = String(i).repeat(i); const error = common.expectsError({ code: 'ERR_UNKNOWN_ENCODING', type: TypeError, message: `Unknown encoding: ${encoding}` }); assert.ok(!Buffer.isEncoding(encoding)); assert.throws(() => Buffer.alloc(9).write('foo', encoding), error); } buffer-5.4.2/test/node/test-buffer-zero-fill-cli.js000066400000000000000000000014741353132250700221400ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; // Flags: --zero-fill-buffers // when using --zero-fill-buffers, every Buffer and SlowBuffer // instance must be zero filled upon creation require('./common'); const SlowBuffer = require('../../').SlowBuffer; const assert = require('assert'); function isZeroFilled(buf) { for (let n = 0; n < buf.length; n++) if (buf[n] > 0) return false; return true; } // This can be somewhat unreliable because the // allocated memory might just already happen to // contain all zeroes. The test is run multiple // times to improve the reliability. for (let i = 0; i < 50; i++) { const bufs = [ Buffer.alloc(20), Buffer.allocUnsafe(20), SlowBuffer(20), Buffer(20), new SlowBuffer(20) ]; for (const buf of bufs) { assert(isZeroFilled(buf)); } } buffer-5.4.2/test/node/test-buffer-zero-fill-reset.js000066400000000000000000000006421353132250700225070ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); function testUint8Array(ui) { const length = ui.length; for (let i = 0; i < length; i++) if (ui[i] !== 0) return false; return true; } for (let i = 0; i < 100; i++) { Buffer.alloc(0); const ui = new Uint8Array(65); assert.ok(testUint8Array(ui), `Uint8Array is not zero-filled: ${ui}`); } buffer-5.4.2/test/node/test-buffer-zero-fill.js000066400000000000000000000004711353132250700213670ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; require('./common'); const assert = require('assert'); const buf1 = Buffer(100); const buf2 = new Buffer(100); for (let n = 0; n < buf1.length; n++) assert.strictEqual(buf1[n], 0); for (let n = 0; n < buf2.length; n++) assert.strictEqual(buf2[n], 0); buffer-5.4.2/test/node/test-buffer.js000066400000000000000000001314161353132250700174720ustar00rootroot00000000000000'use strict'; var Buffer = require('../../').Buffer; var common = { skip: function () {} }; var assert = require('assert'); var Buffer = require('../../').Buffer; var SlowBuffer = require('../../').SlowBuffer; // counter to ensure unique value is always copied var cntr = 0; var b = Buffer(1024); // safe constructor // console.log('b.length == %d', b.length); assert.strictEqual(1024, b.length); b[0] = -1; assert.strictEqual(b[0], 255); for (var i = 0; i < 1024; i++) { b[i] = i % 256; } for (var i = 0; i < 1024; i++) { assert.strictEqual(i % 256, b[i]); } var c = Buffer(512); // console.log('c.length == %d', c.length); assert.strictEqual(512, c.length); var d = new Buffer([]); assert.strictEqual(0, d.length); var ui32 = new Uint32Array(4).fill(42); var e = Buffer(ui32); for (var [key, value] of e.entries()) { assert.deepStrictEqual(value, ui32[key]); } // First check Buffer#fill() works as expected. assert.throws(function() { Buffer(8).fill('a', -1); }); assert.throws(function() { Buffer(8).fill('a', 0, 9); }); // Make sure this doesn't hang indefinitely. Buffer(8).fill(''); { var buf = new Buffer(64); buf.fill(10); for (var i = 0; i < buf.length; i++) assert.equal(buf[i], 10); buf.fill(11, 0, buf.length >> 1); for (var i = 0; i < buf.length >> 1; i++) assert.equal(buf[i], 11); for (var i = (buf.length >> 1) + 1; i < buf.length; i++) assert.equal(buf[i], 10); buf.fill('h'); for (var i = 0; i < buf.length; i++) assert.equal('h'.charCodeAt(0), buf[i]); buf.fill(0); for (var i = 0; i < buf.length; i++) assert.equal(0, buf[i]); buf.fill(null); for (var i = 0; i < buf.length; i++) assert.equal(0, buf[i]); buf.fill(1, 16, 32); for (var i = 0; i < 16; i++) assert.equal(0, buf[i]); for (var i = 16; i < 32; i++) assert.equal(1, buf[i]); for (var i = 32; i < buf.length; i++) assert.equal(0, buf[i]); } { var buf = new Buffer(10); buf.fill('abc'); assert.equal(buf.toString(), 'abcabcabca'); buf.fill('է'); assert.equal(buf.toString(), 'էէէէէ'); } { // copy 512 bytes, from 0 to 512. b.fill(++cntr); c.fill(++cntr); var copied = b.copy(c, 0, 0, 512); // console.log('copied %d bytes from b into c', copied); assert.strictEqual(512, copied); for (var i = 0; i < c.length; i++) { assert.strictEqual(b[i], c[i]); } } { // copy c into b, without specifying sourceEnd b.fill(++cntr); c.fill(++cntr); var copied = c.copy(b, 0, 0); // console.log('copied %d bytes from c into b w/o sourceEnd', copied); assert.strictEqual(c.length, copied); for (var i = 0; i < c.length; i++) { assert.strictEqual(c[i], b[i]); } } { // copy c into b, without specifying sourceStart b.fill(++cntr); c.fill(++cntr); var copied = c.copy(b, 0); // console.log('copied %d bytes from c into b w/o sourceStart', copied); assert.strictEqual(c.length, copied); for (var i = 0; i < c.length; i++) { assert.strictEqual(c[i], b[i]); } } { // copy longer buffer b to shorter c without targetStart b.fill(++cntr); c.fill(++cntr); var copied = b.copy(c); // console.log('copied %d bytes from b into c w/o targetStart', copied); assert.strictEqual(c.length, copied); for (var i = 0; i < c.length; i++) { assert.strictEqual(b[i], c[i]); } } { // copy starting near end of b to c b.fill(++cntr); c.fill(++cntr); var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2)); // console.log('copied %d bytes from end of b into beginning of c', copied); assert.strictEqual(Math.floor(c.length / 2), copied); for (var i = 0; i < Math.floor(c.length / 2); i++) { assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]); } for (var i = Math.floor(c.length / 2) + 1; i < c.length; i++) { assert.strictEqual(c[c.length - 1], c[i]); } } { // try to copy 513 bytes, and check we don't overrun c b.fill(++cntr); c.fill(++cntr); var copied = b.copy(c, 0, 0, 513); // console.log('copied %d bytes from b trying to overrun c', copied); assert.strictEqual(c.length, copied); for (var i = 0; i < c.length; i++) { assert.strictEqual(b[i], c[i]); } } { // copy 768 bytes from b into b b.fill(++cntr); b.fill(++cntr, 256); var copied = b.copy(b, 0, 256, 1024); // console.log('copied %d bytes from b into b', copied); assert.strictEqual(768, copied); for (var i = 0; i < b.length; i++) { assert.strictEqual(cntr, b[i]); } } // copy string longer than buffer length (failure will segfault) var bb = Buffer(10); bb.fill('hello crazy world'); // try to copy from before the beginning of b assert.doesNotThrow(() => { b.copy(c, 0, 100, 10); }); // copy throws at negative sourceStart assert.throws(function() { Buffer(5).copy(Buffer(5), 0, -1); }, RangeError); { // check sourceEnd resets to targetEnd if former is greater than the latter b.fill(++cntr); c.fill(++cntr); var copied = b.copy(c, 0, 0, 1025); // console.log('copied %d bytes from b into c', copied); for (var i = 0; i < c.length; i++) { assert.strictEqual(b[i], c[i]); } } // throw with negative sourceEnd // console.log('test copy at negative sourceEnd'); assert.throws(function() { b.copy(c, 0, 0, -1); }, RangeError); // when sourceStart is greater than sourceEnd, zero copied assert.equal(b.copy(c, 0, 100, 10), 0); // when targetStart > targetLength, zero copied assert.equal(b.copy(c, 512, 0, 10), 0); var caught_error; // invalid encoding for Buffer.toString caught_error = null; try { b.toString('invalid'); } catch (err) { caught_error = err; } assert.strictEqual('Unknown encoding: invalid', caught_error.message); // invalid encoding for Buffer.write caught_error = null; try { b.write('test string', 0, 5, 'invalid'); } catch (err) { caught_error = err; } assert.strictEqual('Unknown encoding: invalid', caught_error.message); // try to create 0-length buffers new Buffer(''); new Buffer('', 'ascii'); new Buffer('', 'latin1'); new Buffer('', 'binary'); Buffer(0); // try to write a 0-length string beyond the end of b assert.throws(function() { b.write('', 2048); }, RangeError); // throw when writing to negative offset assert.throws(function() { b.write('a', -1); }, RangeError); // throw when writing past bounds from the pool assert.throws(function() { b.write('a', 2048); }, RangeError); // throw when writing to negative offset assert.throws(function() { b.write('a', -1); }, RangeError); // try to copy 0 bytes worth of data into an empty buffer b.copy(Buffer(0), 0, 0, 0); // try to copy 0 bytes past the end of the target buffer b.copy(Buffer(0), 1, 1, 1); b.copy(Buffer(1), 1, 1, 1); // try to copy 0 bytes from past the end of the source buffer b.copy(Buffer(1), 0, 2048, 2048); var rangeBuffer = new Buffer('abc'); // if start >= buffer's length, empty string will be returned assert.equal(rangeBuffer.toString('ascii', 3), ''); assert.equal(rangeBuffer.toString('ascii', +Infinity), ''); assert.equal(rangeBuffer.toString('ascii', 3.14, 3), ''); assert.equal(rangeBuffer.toString('ascii', 'Infinity', 3), ''); // if end <= 0, empty string will be returned assert.equal(rangeBuffer.toString('ascii', 1, 0), ''); assert.equal(rangeBuffer.toString('ascii', 1, -1.2), ''); assert.equal(rangeBuffer.toString('ascii', 1, -100), ''); assert.equal(rangeBuffer.toString('ascii', 1, -Infinity), ''); // if start < 0, start will be taken as zero assert.equal(rangeBuffer.toString('ascii', -1, 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', -1.99, 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', -Infinity, 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc'); // if start is an invalid integer, start will be taken as zero assert.equal(rangeBuffer.toString('ascii', 'node.js', 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', {}, 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', [], 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', NaN, 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', null, 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', undefined, 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', false, 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', '', 3), 'abc'); // but, if start is an integer when coerced, then it will be coerced and used. assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', '1', 3), 'bc'); assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', '3', 3), ''); assert.equal(rangeBuffer.toString('ascii', Number(3), 3), ''); assert.equal(rangeBuffer.toString('ascii', '3.14', 3), ''); assert.equal(rangeBuffer.toString('ascii', '1.99', 3), 'bc'); assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc'); assert.equal(rangeBuffer.toString('ascii', 1.99, 3), 'bc'); assert.equal(rangeBuffer.toString('ascii', true, 3), 'bc'); // if end > buffer's length, end will be taken as buffer's length assert.equal(rangeBuffer.toString('ascii', 0, 5), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0, 6.99), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0, Infinity), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0, '5'), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0, '6.99'), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0, 'Infinity'), 'abc'); // if end is an invalid integer, end will be taken as buffer's length assert.equal(rangeBuffer.toString('ascii', 0, 'node.js'), ''); assert.equal(rangeBuffer.toString('ascii', 0, {}), ''); assert.equal(rangeBuffer.toString('ascii', 0, NaN), ''); assert.equal(rangeBuffer.toString('ascii', 0, undefined), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0, null), ''); assert.equal(rangeBuffer.toString('ascii', 0, []), ''); assert.equal(rangeBuffer.toString('ascii', 0, false), ''); assert.equal(rangeBuffer.toString('ascii', 0, ''), ''); // but, if end is an integer when coerced, then it will be coerced and used. assert.equal(rangeBuffer.toString('ascii', 0, '-1'), ''); assert.equal(rangeBuffer.toString('ascii', 0, '1'), 'a'); assert.equal(rangeBuffer.toString('ascii', 0, '-Infinity'), ''); assert.equal(rangeBuffer.toString('ascii', 0, '3'), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0, Number(3)), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0, '3.14'), 'abc'); assert.equal(rangeBuffer.toString('ascii', 0, '1.99'), 'a'); assert.equal(rangeBuffer.toString('ascii', 0, '-1.99'), ''); assert.equal(rangeBuffer.toString('ascii', 0, 1.99), 'a'); assert.equal(rangeBuffer.toString('ascii', 0, true), 'a'); // try toString() with a object as a encoding assert.equal(rangeBuffer.toString({toString: function() { return 'ascii'; }}), 'abc'); // testing for smart defaults and ability to pass string values as offset var writeTest = new Buffer('abcdes'); writeTest.write('n', 'ascii'); writeTest.write('o', '1', 'ascii'); writeTest.write('d', '2', 'ascii'); writeTest.write('e', 3, 'ascii'); writeTest.write('j', 4, 'ascii'); assert.equal(writeTest.toString(), 'nodejs'); // ASCII slice test { var asciiString = 'hello world'; for (var i = 0; i < asciiString.length; i++) { b[i] = asciiString.charCodeAt(i); } var asciiSlice = b.toString('ascii', 0, asciiString.length); assert.equal(asciiString, asciiSlice); } { var asciiString = 'hello world'; var offset = 100; var written = b.write(asciiString, offset, 'ascii'); assert.equal(asciiString.length, written); var asciiSlice = b.toString('ascii', offset, offset + asciiString.length); assert.equal(asciiString, asciiSlice); } { var asciiString = 'hello world'; var offset = 100; var sliceA = b.slice(offset, offset + asciiString.length); var sliceB = b.slice(offset, offset + asciiString.length); for (var i = 0; i < asciiString.length; i++) { assert.equal(sliceA[i], sliceB[i]); } } // UTF-8 slice test var utf8String = '¡hέlló wôrld!'; var offset = 100; b.write(utf8String, 0, Buffer.byteLength(utf8String), 'utf8'); var utf8Slice = b.toString('utf8', 0, Buffer.byteLength(utf8String)); assert.equal(utf8String, utf8Slice); var written = b.write(utf8String, offset, 'utf8'); assert.equal(Buffer.byteLength(utf8String), written); utf8Slice = b.toString('utf8', offset, offset + Buffer.byteLength(utf8String)); assert.equal(utf8String, utf8Slice); var sliceA = b.slice(offset, offset + Buffer.byteLength(utf8String)); var sliceB = b.slice(offset, offset + Buffer.byteLength(utf8String)); for (var i = 0; i < Buffer.byteLength(utf8String); i++) { assert.equal(sliceA[i], sliceB[i]); } { var slice = b.slice(100, 150); assert.equal(50, slice.length); for (var i = 0; i < 50; i++) { assert.equal(b[100 + i], slice[i]); } } { // make sure only top level parent propagates from allocPool var b = new Buffer(5); var c = b.slice(0, 4); var d = c.slice(0, 2); assert.equal(b.parent, c.parent); assert.equal(b.parent, d.parent); } { // also from a non-pooled instance var b = new SlowBuffer(5); var c = b.slice(0, 4); var d = c.slice(0, 2); assert.equal(c.parent, d.parent); } { // Bug regression test var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語 var buffer = new Buffer(32); var size = buffer.write(testValue, 0, 'utf8'); // console.log('bytes written to buffer: ' + size); var slice = buffer.toString('utf8', 0, size); assert.equal(slice, testValue); } { // Test triple slice var a = new Buffer(8); for (var i = 0; i < 8; i++) a[i] = i; var b = a.slice(4, 8); assert.equal(4, b[0]); assert.equal(5, b[1]); assert.equal(6, b[2]); assert.equal(7, b[3]); var c = b.slice(2, 4); assert.equal(6, c[0]); assert.equal(7, c[1]); } { var d = new Buffer([23, 42, 255]); assert.equal(d.length, 3); assert.equal(d[0], 23); assert.equal(d[1], 42); assert.equal(d[2], 255); assert.deepStrictEqual(d, new Buffer(d)); } { var e = new Buffer('über'); // console.error('uber: \'%s\'', e.toString()); assert.deepStrictEqual(e, new Buffer([195, 188, 98, 101, 114])); } { var f = new Buffer('über', 'ascii'); // console.error('f.length: %d (should be 4)', f.length); assert.deepStrictEqual(f, new Buffer([252, 98, 101, 114])); } ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) { { var f = new Buffer('über', encoding); // console.error('f.length: %d (should be 8)', f.length); assert.deepStrictEqual(f, new Buffer([252, 0, 98, 0, 101, 0, 114, 0])); } { var f = new Buffer('привет', encoding); // console.error('f.length: %d (should be 12)', f.length); var expected = new Buffer([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]); assert.deepStrictEqual(f, expected); assert.equal(f.toString(encoding), 'привет'); } { var f = new Buffer([0, 0, 0, 0, 0]); assert.equal(f.length, 5); var size = f.write('あいうえお', encoding); // console.error('bytes written to buffer: %d (should be 4)', size); assert.equal(size, 4); assert.deepStrictEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00])); } }); { var f = new Buffer('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D) assert.equal(f.length, 4); assert.deepStrictEqual(f, new Buffer('3DD84DDC', 'hex')); } var arrayIsh = {0: 0, 1: 1, 2: 2, 3: 3, length: 4}; var g = new Buffer(arrayIsh); assert.deepStrictEqual(g, new Buffer([0, 1, 2, 3])); var strArrayIsh = {0: '0', 1: '1', 2: '2', 3: '3', length: 4}; g = new Buffer(strArrayIsh); assert.deepStrictEqual(g, new Buffer([0, 1, 2, 3])); // // Test toString('base64') // assert.equal('TWFu', (new Buffer('Man')).toString('base64')); { // test that regular and URL-safe base64 both work var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff]; assert.deepStrictEqual(Buffer('//++/++/++//', 'base64'), Buffer(expected)); assert.deepStrictEqual(Buffer('__--_--_--__', 'base64'), Buffer(expected)); } { // big example var quote = 'Man is distinguished, not only by his reason, but by this ' + 'singular passion from other animals, which is a lust ' + 'of the mind, that by a perseverance of delight in the ' + 'continued and indefatigable generation of knowledge, ' + 'exceeds the short vehemence of any carnal pleasure.'; var expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb' + '24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci' + 'BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQ' + 'gYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu' + 'dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZ' + 'GdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm' + '5hbCBwbGVhc3VyZS4='; assert.equal(expected, (new Buffer(quote)).toString('base64')); var b = new Buffer(1024); var bytesWritten = b.write(expected, 0, 'base64'); assert.equal(quote.length, bytesWritten); assert.equal(quote, b.toString('ascii', 0, quote.length)); // check that the base64 decoder ignores whitespace var expectedWhite = expected.slice(0, 60) + ' \n' + expected.slice(60, 120) + ' \n' + expected.slice(120, 180) + ' \n' + expected.slice(180, 240) + ' \n' + expected.slice(240, 300) + '\n' + expected.slice(300, 360) + '\n'; b = new Buffer(1024); bytesWritten = b.write(expectedWhite, 0, 'base64'); assert.equal(quote.length, bytesWritten); assert.equal(quote, b.toString('ascii', 0, quote.length)); // check that the base64 decoder on the constructor works // even in the presence of whitespace. b = new Buffer(expectedWhite, 'base64'); assert.equal(quote.length, b.length); assert.equal(quote, b.toString('ascii', 0, quote.length)); // check that the base64 decoder ignores illegal chars var expectedIllegal = expected.slice(0, 60) + ' \x80' + expected.slice(60, 120) + ' \xff' + expected.slice(120, 180) + ' \x00' + expected.slice(180, 240) + ' \x98' + expected.slice(240, 300) + '\x03' + expected.slice(300, 360); b = new Buffer(expectedIllegal, 'base64'); assert.equal(quote.length, b.length); assert.equal(quote, b.toString('ascii', 0, quote.length)); } assert.equal(new Buffer('', 'base64').toString(), ''); assert.equal(new Buffer('K', 'base64').toString(), ''); // multiple-of-4 with padding assert.equal(new Buffer('Kg==', 'base64').toString(), '*'); assert.equal(new Buffer('Kio=', 'base64').toString(), '**'); assert.equal(new Buffer('Kioq', 'base64').toString(), '***'); assert.equal(new Buffer('KioqKg==', 'base64').toString(), '****'); assert.equal(new Buffer('KioqKio=', 'base64').toString(), '*****'); assert.equal(new Buffer('KioqKioq', 'base64').toString(), '******'); assert.equal(new Buffer('KioqKioqKg==', 'base64').toString(), '*******'); assert.equal(new Buffer('KioqKioqKio=', 'base64').toString(), '********'); assert.equal(new Buffer('KioqKioqKioq', 'base64').toString(), '*********'); assert.equal(new Buffer('KioqKioqKioqKg==', 'base64').toString(), '**********'); assert.equal(new Buffer('KioqKioqKioqKio=', 'base64').toString(), '***********'); assert.equal(new Buffer('KioqKioqKioqKioq', 'base64').toString(), '************'); assert.equal(new Buffer('KioqKioqKioqKioqKg==', 'base64').toString(), '*************'); assert.equal(new Buffer('KioqKioqKioqKioqKio=', 'base64').toString(), '**************'); assert.equal(new Buffer('KioqKioqKioqKioqKioq', 'base64').toString(), '***************'); assert.equal(new Buffer('KioqKioqKioqKioqKioqKg==', 'base64').toString(), '****************'); assert.equal(new Buffer('KioqKioqKioqKioqKioqKio=', 'base64').toString(), '*****************'); assert.equal(new Buffer('KioqKioqKioqKioqKioqKioq', 'base64').toString(), '******************'); assert.equal(new Buffer('KioqKioqKioqKioqKioqKioqKg==', 'base64').toString(), '*******************'); assert.equal(new Buffer('KioqKioqKioqKioqKioqKioqKio=', 'base64').toString(), '********************'); // no padding, not a multiple of 4 assert.equal(new Buffer('Kg', 'base64').toString(), '*'); assert.equal(new Buffer('Kio', 'base64').toString(), '**'); assert.equal(new Buffer('KioqKg', 'base64').toString(), '****'); assert.equal(new Buffer('KioqKio', 'base64').toString(), '*****'); assert.equal(new Buffer('KioqKioqKg', 'base64').toString(), '*******'); assert.equal(new Buffer('KioqKioqKio', 'base64').toString(), '********'); assert.equal(new Buffer('KioqKioqKioqKg', 'base64').toString(), '**********'); assert.equal(new Buffer('KioqKioqKioqKio', 'base64').toString(), '***********'); assert.equal(new Buffer('KioqKioqKioqKioqKg', 'base64').toString(), '*************'); assert.equal(new Buffer('KioqKioqKioqKioqKio', 'base64').toString(), '**************'); assert.equal(new Buffer('KioqKioqKioqKioqKioqKg', 'base64').toString(), '****************'); assert.equal(new Buffer('KioqKioqKioqKioqKioqKio', 'base64').toString(), '*****************'); assert.equal(new Buffer('KioqKioqKioqKioqKioqKioqKg', 'base64').toString(), '*******************'); assert.equal(new Buffer('KioqKioqKioqKioqKioqKioqKio', 'base64').toString(), '********************'); // handle padding graciously, multiple-of-4 or not assert.equal( new Buffer('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw==', 'base64').length, 32 ); assert.equal( new Buffer('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw=', 'base64').length, 32 ); assert.equal( new Buffer('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw', 'base64').length, 32 ); assert.equal( new Buffer('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg==', 'base64').length, 31 ); assert.equal( new Buffer('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg=', 'base64').length, 31 ); assert.equal( new Buffer('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg', 'base64').length, 31 ); // This string encodes single '.' character in UTF-16 var dot = new Buffer('//4uAA==', 'base64'); assert.equal(dot[0], 0xff); assert.equal(dot[1], 0xfe); assert.equal(dot[2], 0x2e); assert.equal(dot[3], 0x00); assert.equal(dot.toString('base64'), '//4uAA=='); { // Writing base64 at a position > 0 should not mangle the result. // // https://github.com/joyent/node/issues/402 var segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh']; var b = new Buffer(64); var pos = 0; for (var i = 0; i < segments.length; ++i) { pos += b.write(segments[i], pos, 'base64'); } assert.equal(b.toString('latin1', 0, pos), 'Madness?! This is node.js!'); assert.equal(b.toString('binary', 0, pos), 'Madness?! This is node.js!'); } // Regression test for https://github.com/nodejs/node/issues/3496. // assert.equal(Buffer('=bad'.repeat(1e4), 'base64').length, 0); { // Creating buffers larger than pool size. var l = Buffer.poolSize + 5; var s = 'h'.repeat(l); var b = new Buffer(s); for (var i = 0; i < l; i++) { assert.equal('h'.charCodeAt(0), b[i]); } var sb = b.toString(); assert.equal(sb.length, s.length); assert.equal(sb, s); } { // Single argument slice var b = new Buffer('abcde'); assert.equal('bcde', b.slice(1).toString()); } // slice(0,0).length === 0 assert.equal(0, Buffer('hello').slice(0, 0).length); // test hex toString // console.log('Create hex string from buffer'); var hexb = new Buffer(256); for (var i = 0; i < 256; i++) { hexb[i] = i; } var hexStr = hexb.toString('hex'); assert.equal(hexStr, '000102030405060708090a0b0c0d0e0f' + '101112131415161718191a1b1c1d1e1f' + '202122232425262728292a2b2c2d2e2f' + '303132333435363738393a3b3c3d3e3f' + '404142434445464748494a4b4c4d4e4f' + '505152535455565758595a5b5c5d5e5f' + '606162636465666768696a6b6c6d6e6f' + '707172737475767778797a7b7c7d7e7f' + '808182838485868788898a8b8c8d8e8f' + '909192939495969798999a9b9c9d9e9f' + 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf' + 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + 'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf' + 'e0e1e2e3e4e5e6e7e8e9eaebecedeeef' + 'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'); // console.log('Create buffer from hex string'); var hexb2 = new Buffer(hexStr, 'hex'); for (var i = 0; i < 256; i++) { assert.equal(hexb2[i], hexb[i]); } // Test single base64 char encodes as 0 // assert.strictEqual(Buffer.from('A', 'base64').length, 0); { // test an invalid slice end. // console.log('Try to slice off the end of the buffer'); var b = new Buffer([1, 2, 3, 4, 5]); var b2 = b.toString('hex', 1, 10000); var b3 = b.toString('hex', 1, 5); var b4 = b.toString('hex', 1); assert.equal(b2, b3); assert.equal(b2, b4); } function buildBuffer(data) { if (Array.isArray(data)) { var buffer = Buffer(data.length); data.forEach(function(v, k) { buffer[k] = v; }); return buffer; } return null; } var x = buildBuffer([0x81, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72]); // console.log(x.inspect()); assert.equal('', x.inspect()); { var z = x.slice(4); // console.log(z.inspect()); // console.log(z.length); assert.equal(5, z.length); assert.equal(0x6f, z[0]); assert.equal(0xa3, z[1]); assert.equal(0x62, z[2]); assert.equal(0x61, z[3]); assert.equal(0x72, z[4]); } { var z = x.slice(0); // console.log(z.inspect()); // console.log(z.length); assert.equal(z.length, x.length); } { var z = x.slice(0, 4); // console.log(z.inspect()); // console.log(z.length); assert.equal(4, z.length); assert.equal(0x81, z[0]); assert.equal(0xa3, z[1]); } { var z = x.slice(0, 9); // console.log(z.inspect()); // console.log(z.length); assert.equal(9, z.length); } { var z = x.slice(1, 4); // console.log(z.inspect()); // console.log(z.length); assert.equal(3, z.length); assert.equal(0xa3, z[0]); } { var z = x.slice(2, 4); // console.log(z.inspect()); // console.log(z.length); assert.equal(2, z.length); assert.equal(0x66, z[0]); assert.equal(0x6f, z[1]); } assert.equal(0, Buffer('hello').slice(0, 0).length); ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) { var b = new Buffer(10); b.write('あいうえお', encoding); assert.equal(b.toString(encoding), 'あいうえお'); }); { // latin1 encoding should write only one byte per character. var b = Buffer([0xde, 0xad, 0xbe, 0xef]); var s = String.fromCharCode(0xffff); b.write(s, 0, 'latin1'); assert.equal(0xff, b[0]); assert.equal(0xad, b[1]); assert.equal(0xbe, b[2]); assert.equal(0xef, b[3]); s = String.fromCharCode(0xaaee); b.write(s, 0, 'latin1'); assert.equal(0xee, b[0]); assert.equal(0xad, b[1]); assert.equal(0xbe, b[2]); assert.equal(0xef, b[3]); } { // Binary encoding should write only one byte per character. var b = Buffer([0xde, 0xad, 0xbe, 0xef]); var s = String.fromCharCode(0xffff); b.write(s, 0, 'binary'); assert.equal(0xff, b[0]); assert.equal(0xad, b[1]); assert.equal(0xbe, b[2]); assert.equal(0xef, b[3]); s = String.fromCharCode(0xaaee); b.write(s, 0, 'binary'); assert.equal(0xee, b[0]); assert.equal(0xad, b[1]); assert.equal(0xbe, b[2]); assert.equal(0xef, b[3]); } { // #1210 Test UTF-8 string includes null character var buf = new Buffer('\0'); assert.equal(buf.length, 1); buf = new Buffer('\0\0'); assert.equal(buf.length, 2); } { var buf = new Buffer(2); var written = buf.write(''); // 0byte assert.equal(written, 0); written = buf.write('\0'); // 1byte (v8 adds null terminator) assert.equal(written, 1); written = buf.write('a\0'); // 1byte * 2 assert.equal(written, 2); written = buf.write('あ'); // 3bytes assert.equal(written, 0); written = buf.write('\0あ'); // 1byte + 3bytes assert.equal(written, 1); written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes assert.equal(written, 2); } { var buf = new Buffer(10); written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator) assert.equal(written, 9); written = buf.write('あいう\0'); // 3bytes * 3 + 1byte assert.equal(written, 10); } { // #243 Test write() with maxLength var buf = new Buffer(4); buf.fill(0xFF); var written = buf.write('abcd', 1, 2, 'utf8'); // console.log(buf); assert.equal(written, 2); assert.equal(buf[0], 0xFF); assert.equal(buf[1], 0x61); assert.equal(buf[2], 0x62); assert.equal(buf[3], 0xFF); buf.fill(0xFF); written = buf.write('abcd', 1, 4); // console.log(buf); assert.equal(written, 3); assert.equal(buf[0], 0xFF); assert.equal(buf[1], 0x61); assert.equal(buf[2], 0x62); assert.equal(buf[3], 0x63); buf.fill(0xFF); written = buf.write('abcd', 1, 2, 'utf8'); // console.log(buf); assert.equal(written, 2); assert.equal(buf[0], 0xFF); assert.equal(buf[1], 0x61); assert.equal(buf[2], 0x62); assert.equal(buf[3], 0xFF); buf.fill(0xFF); written = buf.write('abcdef', 1, 2, 'hex'); // console.log(buf); assert.equal(written, 2); assert.equal(buf[0], 0xFF); assert.equal(buf[1], 0xAB); assert.equal(buf[2], 0xCD); assert.equal(buf[3], 0xFF); ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) { buf.fill(0xFF); written = buf.write('abcd', 0, 2, encoding); // console.log(buf); assert.equal(written, 2); assert.equal(buf[0], 0x61); assert.equal(buf[1], 0x00); assert.equal(buf[2], 0xFF); assert.equal(buf[3], 0xFF); }); } { // test offset returns are correct var b = new Buffer(16); assert.equal(4, b.writeUInt32LE(0, 0)); assert.equal(6, b.writeUInt16LE(0, 4)); assert.equal(7, b.writeUInt8(0, 6)); assert.equal(8, b.writeInt8(0, 7)); assert.equal(16, b.writeDoubleLE(0, 8)); } { // test unmatched surrogates not producing invalid utf8 output // ef bf bd = utf-8 representation of unicode replacement character // see https://codereview.chromium.org/121173009/ var buf = new Buffer('ab\ud800cd', 'utf8'); assert.equal(buf[0], 0x61); assert.equal(buf[1], 0x62); assert.equal(buf[2], 0xef); assert.equal(buf[3], 0xbf); assert.equal(buf[4], 0xbd); assert.equal(buf[5], 0x63); assert.equal(buf[6], 0x64); } { // test for buffer overrun var buf = new Buffer([0, 0, 0, 0, 0]); // length: 5 var sub = buf.slice(0, 4); // length: 4 written = sub.write('12345', 'latin1'); assert.equal(written, 4); assert.equal(buf[4], 0); written = sub.write('12345', 'binary'); assert.equal(written, 4); assert.equal(buf[4], 0); } // Check for fractional length args, junk length args, etc. // https://github.com/joyent/node/issues/1758 // Call .fill() first, stops valgrind warning about uninitialized memory reads. Buffer(3.3).fill().toString(); // throws bad argument error in commit 43cb4ec assert.equal(Buffer(NaN).length, 0); assert.equal(Buffer(3.3).length, 3); assert.equal(Buffer({length: 3.3}).length, 3); assert.equal(Buffer({length: 'BAM'}).length, 0); // Make sure that strings are not coerced to numbers. assert.equal(Buffer('99').length, 2); assert.equal(Buffer('13.37').length, 5); // Ensure that the length argument is respected. 'ascii utf8 hex base64 latin1 binary'.split(' ').forEach(function(enc) { assert.equal(Buffer(1).write('aaaaaa', 0, 1, enc), 1); }); { // Regression test, guard against buffer overrun in the base64 decoder. var a = Buffer(3); var b = Buffer('xxx'); a.write('aaaaaaaa', 'base64'); assert.equal(b.toString(), 'xxx'); } // issue GH-3416 Buffer(Buffer(0), 0, 0); [ 'hex', 'utf8', 'utf-8', 'ascii', 'latin1', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le' ].forEach(function(enc) { assert.equal(Buffer.isEncoding(enc), true); }); [ 'utf9', 'utf-7', 'Unicode-FTW', 'new gnu gun' ].forEach(function(enc) { assert.equal(Buffer.isEncoding(enc), false); }); // GH-5110 { var buffer = new Buffer('test'); var string = JSON.stringify(buffer); assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}'); assert.deepStrictEqual(buffer, JSON.parse(string, function(key, value) { return value && value.type === 'Buffer' ? new Buffer(value.data) : value; })); } // issue GH-7849 { var buf = new Buffer('test'); var json = JSON.stringify(buf); var obj = JSON.parse(json); var copy = new Buffer(obj); assert(buf.equals(copy)); } // issue GH-4331 assert.throws(function() { Buffer(0xFFFFFFFF); }, RangeError); assert.throws(function() { Buffer(0xFFFFFFFFF); }, RangeError); // issue GH-5587 assert.throws(function() { var buf = new Buffer(8); buf.writeFloatLE(0, 5); }, RangeError); assert.throws(function() { var buf = new Buffer(16); buf.writeDoubleLE(0, 9); }, RangeError); // attempt to overflow buffers, similar to previous bug in array buffers assert.throws(function() { var buf = Buffer(8); buf.readFloatLE(0xffffffff); }, RangeError); assert.throws(function() { var buf = Buffer(8); buf.writeFloatLE(0.0, 0xffffffff); }, RangeError); assert.throws(function() { var buf = Buffer(8); buf.readFloatLE(0xffffffff); }, RangeError); assert.throws(function() { var buf = Buffer(8); buf.writeFloatLE(0.0, 0xffffffff); }, RangeError); // ensure negative values can't get past offset assert.throws(function() { var buf = Buffer(8); buf.readFloatLE(-1); }, RangeError); assert.throws(function() { var buf = Buffer(8); buf.writeFloatLE(0.0, -1); }, RangeError); assert.throws(function() { var buf = Buffer(8); buf.readFloatLE(-1); }, RangeError); assert.throws(function() { var buf = Buffer(8); buf.writeFloatLE(0.0, -1); }, RangeError); // offset checks { var buf = new Buffer(0); assert.throws(function() { buf.readUInt8(0); }, RangeError); assert.throws(function() { buf.readInt8(0); }, RangeError); } { var buf = new Buffer([0xFF]); assert.equal(buf.readUInt8(0), 255); assert.equal(buf.readInt8(0), -1); } [16, 32].forEach(function(bits) { var buf = new Buffer(bits / 8 - 1); assert.throws(function() { buf['readUInt' + bits + 'BE'](0); }, RangeError, 'readUInt' + bits + 'BE'); assert.throws(function() { buf['readUInt' + bits + 'LE'](0); }, RangeError, 'readUInt' + bits + 'LE'); assert.throws(function() { buf['readInt' + bits + 'BE'](0); }, RangeError, 'readInt' + bits + 'BE()'); assert.throws(function() { buf['readInt' + bits + 'LE'](0); }, RangeError, 'readInt' + bits + 'LE()'); }); [16, 32].forEach(function(bits) { var buf = new Buffer([0xFF, 0xFF, 0xFF, 0xFF]); assert.equal(buf['readUInt' + bits + 'BE'](0), (0xFFFFFFFF >>> (32 - bits))); assert.equal(buf['readUInt' + bits + 'LE'](0), (0xFFFFFFFF >>> (32 - bits))); assert.equal(buf['readInt' + bits + 'BE'](0), (0xFFFFFFFF >> (32 - bits))); assert.equal(buf['readInt' + bits + 'LE'](0), (0xFFFFFFFF >> (32 - bits))); }); // test for common read(U)IntLE/BE { var buf = new Buffer([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]); assert.strictEqual(buf.readUIntLE(0, 1), 0x01); assert.strictEqual(buf.readUIntBE(0, 1), 0x01); assert.strictEqual(buf.readUIntLE(0, 3), 0x030201); assert.strictEqual(buf.readUIntBE(0, 3), 0x010203); assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201); assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405); assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201); assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506); assert.strictEqual(buf.readIntLE(0, 1), 0x01); assert.strictEqual(buf.readIntBE(0, 1), 0x01); assert.strictEqual(buf.readIntLE(0, 3), 0x030201); assert.strictEqual(buf.readIntBE(0, 3), 0x010203); assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201); assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405); assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201); assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506); } // test for common write(U)IntLE/BE { var buf = Buffer(3); buf.writeUIntLE(0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); assert.equal(buf.readUIntLE(0, 3), 0x123456); buf = Buffer(3); buf.writeUIntBE(0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); assert.equal(buf.readUIntBE(0, 3), 0x123456); buf = Buffer(3); buf.writeIntLE(0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); assert.equal(buf.readIntLE(0, 3), 0x123456); buf = Buffer(3); buf.writeIntBE(0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); assert.equal(buf.readIntBE(0, 3), 0x123456); buf = Buffer(3); buf.writeIntLE(-0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]); assert.equal(buf.readIntLE(0, 3), -0x123456); buf = Buffer(3); buf.writeIntBE(-0x123456, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]); assert.equal(buf.readIntBE(0, 3), -0x123456); buf = Buffer(3); buf.writeIntLE(-0x123400, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]); assert.equal(buf.readIntLE(0, 3), -0x123400); buf = Buffer(3); buf.writeIntBE(-0x123400, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]); assert.equal(buf.readIntBE(0, 3), -0x123400); buf = Buffer(3); buf.writeIntLE(-0x120000, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0xee]); assert.equal(buf.readIntLE(0, 3), -0x120000); buf = Buffer(3); buf.writeIntBE(-0x120000, 0, 3); assert.deepStrictEqual(buf.toJSON().data, [0xee, 0x00, 0x00]); assert.equal(buf.readIntBE(0, 3), -0x120000); buf = Buffer(5); buf.writeUIntLE(0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); assert.equal(buf.readUIntLE(0, 5), 0x1234567890); buf = Buffer(5); buf.writeUIntBE(0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); assert.equal(buf.readUIntBE(0, 5), 0x1234567890); buf = Buffer(5); buf.writeIntLE(0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); assert.equal(buf.readIntLE(0, 5), 0x1234567890); buf = Buffer(5); buf.writeIntBE(0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); assert.equal(buf.readIntBE(0, 5), 0x1234567890); buf = Buffer(5); buf.writeIntLE(-0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]); assert.equal(buf.readIntLE(0, 5), -0x1234567890); buf = Buffer(5); buf.writeIntBE(-0x1234567890, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]); assert.equal(buf.readIntBE(0, 5), -0x1234567890); buf = Buffer(5); buf.writeIntLE(-0x0012000000, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]); assert.equal(buf.readIntLE(0, 5), -0x0012000000); buf = Buffer(5); buf.writeIntBE(-0x0012000000, 0, 5); assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]); assert.equal(buf.readIntBE(0, 5), -0x0012000000); } // test Buffer slice { var buf = new Buffer('0123456789'); assert.equal(buf.slice(-10, 10), '0123456789'); assert.equal(buf.slice(-20, 10), '0123456789'); assert.equal(buf.slice(-20, -10), ''); assert.equal(buf.slice(), '0123456789'); assert.equal(buf.slice(0), '0123456789'); assert.equal(buf.slice(0, 0), ''); assert.equal(buf.slice(undefined), '0123456789'); assert.equal(buf.slice('foobar'), '0123456789'); assert.equal(buf.slice(undefined, undefined), '0123456789'); assert.equal(buf.slice(2), '23456789'); assert.equal(buf.slice(5), '56789'); assert.equal(buf.slice(10), ''); assert.equal(buf.slice(5, 8), '567'); assert.equal(buf.slice(8, -1), '8'); assert.equal(buf.slice(-10), '0123456789'); assert.equal(buf.slice(0, -9), '0'); assert.equal(buf.slice(0, -10), ''); assert.equal(buf.slice(0, -1), '012345678'); assert.equal(buf.slice(2, -2), '234567'); assert.equal(buf.slice(0, 65536), '0123456789'); assert.equal(buf.slice(65536, 0), ''); assert.equal(buf.slice(-5, -8), ''); assert.equal(buf.slice(-5, -3), '56'); assert.equal(buf.slice(-10, 10), '0123456789'); for (var i = 0, s = buf.toString(); i < buf.length; ++i) { assert.equal(buf.slice(i), s.slice(i)); assert.equal(buf.slice(0, i), s.slice(0, i)); assert.equal(buf.slice(-i), s.slice(-i)); assert.equal(buf.slice(0, -i), s.slice(0, -i)); } var utf16Buf = new Buffer('0123456789', 'utf16le'); assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer('012', 'utf16le')); assert.equal(buf.slice('0', '1'), '0'); assert.equal(buf.slice('-5', '10'), '56789'); assert.equal(buf.slice('-10', '10'), '0123456789'); assert.equal(buf.slice('-10', '-5'), '01234'); assert.equal(buf.slice('-10', '-0'), ''); assert.equal(buf.slice('111'), ''); assert.equal(buf.slice('0', '-111'), ''); // try to slice a zero length Buffer // see https://github.com/joyent/node/issues/5881 SlowBuffer(0).slice(0, 1); } // Regression test for #5482: should throw but not assert in C++ land. assert.throws(function() { Buffer('', 'buffer'); }, TypeError); // Regression test for #6111. Constructing a buffer from another buffer // should a) work, and b) not corrupt the source buffer. { var a = [0]; for (var i = 0; i < 7; ++i) a = a.concat(a); a = a.map(function(_, i) { return i; }); var b = Buffer(a); var c = Buffer(b); assert.strictEqual(b.length, a.length); assert.strictEqual(c.length, a.length); for (var i = 0, k = a.length; i < k; ++i) { assert.strictEqual(a[i], i); assert.strictEqual(b[i], i); assert.strictEqual(c[i], i); } } assert.throws(function() { new Buffer((-1 >>> 0) + 1); }, RangeError); assert.throws(function() { SlowBuffer((-1 >>> 0) + 1); }, RangeError); if (common.hasCrypto) { // Test truncation after decode // var crypto = require('crypto'); var b1 = new Buffer('YW55=======', 'base64'); var b2 = new Buffer('YW55', 'base64'); assert.equal( 1 /*crypto.createHash('sha1').update(b1).digest('hex')*/, 1 /*crypto.createHash('sha1').update(b2).digest('hex')*/ ); } else { common.skip('missing crypto'); } // Test Compare { var b = new Buffer(1).fill('a'); var c = new Buffer(1).fill('c'); var d = new Buffer(2).fill('aa'); assert.equal(b.compare(c), -1); assert.equal(c.compare(d), 1); assert.equal(d.compare(b), 1); assert.equal(b.compare(d), -1); assert.equal(b.compare(b), 0); assert.equal(Buffer.compare(b, c), -1); assert.equal(Buffer.compare(c, d), 1); assert.equal(Buffer.compare(d, b), 1); assert.equal(Buffer.compare(b, d), -1); assert.equal(Buffer.compare(c, c), 0); assert.equal(Buffer.compare(Buffer(0), Buffer(0)), 0); assert.equal(Buffer.compare(Buffer(0), Buffer(1)), -1); assert.equal(Buffer.compare(Buffer(1), Buffer(0)), 1); } assert.throws(function() { var b = Buffer(1); Buffer.compare(b, 'abc'); }); assert.throws(function() { var b = Buffer(1); Buffer.compare('abc', b); }); assert.throws(function() { var b = Buffer(1); b.compare('abc'); }); // Test Equals { var b = new Buffer(5).fill('abcdf'); var c = new Buffer(5).fill('abcdf'); var d = new Buffer(5).fill('abcde'); var e = new Buffer(6).fill('abcdef'); assert.ok(b.equals(c)); assert.ok(!c.equals(d)); assert.ok(!d.equals(e)); assert.ok(d.equals(d)); } assert.throws(function() { var b = Buffer(1); b.equals('abc'); }); // Regression test for https://github.com/nodejs/node/issues/649. assert.throws(function() { Buffer(1422561062959).toString('utf8'); }); { // Test that large negative Buffer length inputs don't affect the pool offset. // Use the fromArrayLike() variant here because it's more lenient // about its input and passes the length directly to allocate(). assert.deepStrictEqual(Buffer({ length: -Buffer.poolSize }), Buffer.from('')); assert.deepStrictEqual(Buffer({ length: -100 }), Buffer.from('')); // Check pool offset after that by trying to write string into the pool. assert.doesNotThrow(() => Buffer.from('abc')); } // Test failed or zero-sized Buffer allocations not affecting typed arrays { var zeroArray = new Uint32Array(10).fill(0); var sizes = [1e10, 0, 0.1, -1, 'a', undefined, null, NaN]; var allocators = [ Buffer, SlowBuffer, Buffer.alloc, Buffer.allocUnsafe, Buffer.allocUnsafeSlow ]; for (var allocator of allocators) { for (var size of sizes) { try { allocator(size); } catch (e) { assert.deepStrictEqual(new Uint32Array(10), zeroArray); } } } } // Test that large negative Buffer length inputs throw errors. assert.throws(() => Buffer(-Buffer.poolSize), '"size" argument must not be negative'); assert.throws(() => Buffer(-100), '"size" argument must not be negative'); assert.throws(() => Buffer(-1), '"size" argument must not be negative'); buffer-5.4.2/test/slice.js000066400000000000000000000014421353132250700154110ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('modifying buffer created by .slice() modifies original memory', function (t) { var buf1 = new B(26) for (var i = 0; i < 26; i++) { buf1[i] = i + 97 // 97 is ASCII a } var buf2 = buf1.slice(0, 3) t.equal(buf2.toString('ascii', 0, buf2.length), 'abc') buf2[0] = '!'.charCodeAt(0) t.equal(buf1.toString('ascii', 0, buf2.length), '!bc') t.end() }) test('modifying parent buffer modifies .slice() buffer\'s memory', function (t) { var buf1 = new B(26) for (var i = 0; i < 26; i++) { buf1[i] = i + 97 // 97 is ASCII a } var buf2 = buf1.slice(0, 3) t.equal(buf2.toString('ascii', 0, buf2.length), 'abc') buf1[0] = '!'.charCodeAt(0) t.equal(buf2.toString('ascii', 0, buf2.length), '!bc') t.end() }) buffer-5.4.2/test/static.js000066400000000000000000000006411353132250700156010ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('Buffer.isEncoding', function (t) { t.equal(B.isEncoding('HEX'), true) t.equal(B.isEncoding('hex'), true) t.equal(B.isEncoding('bad'), false) t.end() }) test('Buffer.isBuffer', function (t) { t.equal(B.isBuffer(new B('hey', 'utf8')), true) t.equal(B.isBuffer(new B([1, 2, 3], 'utf8')), true) t.equal(B.isBuffer('hey'), false) t.end() }) buffer-5.4.2/test/to-string.js000066400000000000000000000154371353132250700162510ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('utf8 buffer to base64', function (t) { t.equal( new B('Ձאab', 'utf8').toString('base64'), '1YHXkGFi' ) t.end() }) test('utf8 buffer to hex', function (t) { t.equal( new B('Ձאab', 'utf8').toString('hex'), 'd581d7906162' ) t.end() }) test('utf8 to utf8', function (t) { t.equal( new B('öäüõÖÄÜÕ', 'utf8').toString('utf8'), 'öäüõÖÄÜÕ' ) t.end() }) test('utf16le to utf16', function (t) { t.equal( new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'), 'abcd' ) t.end() }) test('utf16le to hex', function (t) { t.equal( new B('abcd', 'utf16le').toString('hex'), '6100620063006400' ) t.end() }) test('ascii buffer to base64', function (t) { t.equal( new B('123456!@#$%^', 'ascii').toString('base64'), 'MTIzNDU2IUAjJCVe' ) t.end() }) test('ascii buffer to hex', function (t) { t.equal( new B('123456!@#$%^', 'ascii').toString('hex'), '31323334353621402324255e' ) t.end() }) test('base64 buffer to utf8', function (t) { t.equal( new B('1YHXkGFi', 'base64').toString('utf8'), 'Ձאab' ) t.end() }) test('hex buffer to utf8', function (t) { t.equal( new B('d581d7906162', 'hex').toString('utf8'), 'Ձאab' ) t.end() }) test('base64 buffer to ascii', function (t) { t.equal( new B('MTIzNDU2IUAjJCVe', 'base64').toString('ascii'), '123456!@#$%^' ) t.end() }) test('hex buffer to ascii', function (t) { t.equal( new B('31323334353621402324255e', 'hex').toString('ascii'), '123456!@#$%^' ) t.end() }) test('base64 buffer to binary', function (t) { t.equal( new B('MTIzNDU2IUAjJCVe', 'base64').toString('binary'), '123456!@#$%^' ) t.end() }) test('hex buffer to binary', function (t) { t.equal( new B('31323334353621402324255e', 'hex').toString('binary'), '123456!@#$%^' ) t.end() }) test('utf8 to binary', function (t) { /* jshint -W100 */ t.equal( new B('öäüõÖÄÜÕ', 'utf8').toString('binary'), 'öäüõÖÄÜÕ' ) /* jshint +W100 */ t.end() }) test('utf8 replacement chars (1 byte sequence)', function (t) { t.equal( new B([0x80]).toString(), '\uFFFD' ) t.equal( new B([0x7F]).toString(), '\u007F' ) t.end() }) test('utf8 replacement chars (2 byte sequences)', function (t) { t.equal( new B([0xC7]).toString(), '\uFFFD' ) t.equal( new B([0xC7, 0xB1]).toString(), '\u01F1' ) t.equal( new B([0xC0, 0xB1]).toString(), '\uFFFD\uFFFD' ) t.equal( new B([0xC1, 0xB1]).toString(), '\uFFFD\uFFFD' ) t.end() }) test('utf8 replacement chars (3 byte sequences)', function (t) { t.equal( new B([0xE0]).toString(), '\uFFFD' ) t.equal( new B([0xE0, 0xAC]).toString(), '\uFFFD\uFFFD' ) t.equal( new B([0xE0, 0xAC, 0xB9]).toString(), '\u0B39' ) t.end() }) test('utf8 replacement chars (4 byte sequences)', function (t) { t.equal( new B([0xF4]).toString(), '\uFFFD' ) t.equal( new B([0xF4, 0x8F]).toString(), '\uFFFD\uFFFD' ) t.equal( new B([0xF4, 0x8F, 0x80]).toString(), '\uFFFD\uFFFD\uFFFD' ) t.equal( new B([0xF4, 0x8F, 0x80, 0x84]).toString(), '\uDBFC\uDC04' ) t.equal( new B([0xFF]).toString(), '\uFFFD' ) t.equal( new B([0xFF, 0x8F, 0x80, 0x84]).toString(), '\uFFFD\uFFFD\uFFFD\uFFFD' ) t.end() }) test('utf8 replacement chars on 256 random bytes', function (t) { t.equal( new B([152, 130, 206, 23, 243, 238, 197, 44, 27, 86, 208, 36, 163, 184, 164, 21, 94, 242, 178, 46, 25, 26, 253, 178, 72, 147, 207, 112, 236, 68, 179, 190, 29, 83, 239, 147, 125, 55, 143, 19, 157, 68, 157, 58, 212, 224, 150, 39, 128, 24, 94, 225, 120, 121, 75, 192, 112, 19, 184, 142, 203, 36, 43, 85, 26, 147, 227, 139, 242, 186, 57, 78, 11, 102, 136, 117, 180, 210, 241, 92, 3, 215, 54, 167, 249, 1, 44, 225, 146, 86, 2, 42, 68, 21, 47, 238, 204, 153, 216, 252, 183, 66, 222, 255, 15, 202, 16, 51, 134, 1, 17, 19, 209, 76, 238, 38, 76, 19, 7, 103, 249, 5, 107, 137, 64, 62, 170, 57, 16, 85, 179, 193, 97, 86, 166, 196, 36, 148, 138, 193, 210, 69, 187, 38, 242, 97, 195, 219, 252, 244, 38, 1, 197, 18, 31, 246, 53, 47, 134, 52, 105, 72, 43, 239, 128, 203, 73, 93, 199, 75, 222, 220, 166, 34, 63, 236, 11, 212, 76, 243, 171, 110, 78, 39, 205, 204, 6, 177, 233, 212, 243, 0, 33, 41, 122, 118, 92, 252, 0, 157, 108, 120, 70, 137, 100, 223, 243, 171, 232, 66, 126, 111, 142, 33, 3, 39, 117, 27, 107, 54, 1, 217, 227, 132, 13, 166, 3, 73, 53, 127, 225, 236, 134, 219, 98, 214, 125, 148, 24, 64, 142, 111, 231, 194, 42, 150, 185, 10, 182, 163, 244, 19, 4, 59, 135, 16]).toString(), '\uFFFD\uFFFD\uFFFD\u0017\uFFFD\uFFFD\uFFFD\u002C\u001B\u0056\uFFFD\u0024\uFFFD\uFFFD\uFFFD\u0015\u005E\uFFFD\uFFFD\u002E\u0019\u001A\uFFFD\uFFFD\u0048\uFFFD\uFFFD\u0070\uFFFD\u0044\uFFFD\uFFFD\u001D\u0053\uFFFD\uFFFD\u007D\u0037\uFFFD\u0013\uFFFD\u0044\uFFFD\u003A\uFFFD\uFFFD\uFFFD\u0027\uFFFD\u0018\u005E\uFFFD\u0078\u0079\u004B\uFFFD\u0070\u0013\uFFFD\uFFFD\uFFFD\u0024\u002B\u0055\u001A\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u0039\u004E\u000B\u0066\uFFFD\u0075\uFFFD\uFFFD\uFFFD\u005C\u0003\uFFFD\u0036\uFFFD\uFFFD\u0001\u002C\uFFFD\uFFFD\u0056\u0002\u002A\u0044\u0015\u002F\uFFFD\u0319\uFFFD\uFFFD\uFFFD\u0042\uFFFD\uFFFD\u000F\uFFFD\u0010\u0033\uFFFD\u0001\u0011\u0013\uFFFD\u004C\uFFFD\u0026\u004C\u0013\u0007\u0067\uFFFD\u0005\u006B\uFFFD\u0040\u003E\uFFFD\u0039\u0010\u0055\uFFFD\uFFFD\u0061\u0056\uFFFD\uFFFD\u0024\uFFFD\uFFFD\uFFFD\uFFFD\u0045\uFFFD\u0026\uFFFD\u0061\uFFFD\uFFFD\uFFFD\uFFFD\u0026\u0001\uFFFD\u0012\u001F\uFFFD\u0035\u002F\uFFFD\u0034\u0069\u0048\u002B\uFFFD\uFFFD\uFFFD\u0049\u005D\uFFFD\u004B\uFFFD\u0726\u0022\u003F\uFFFD\u000B\uFFFD\u004C\uFFFD\uFFFD\u006E\u004E\u0027\uFFFD\uFFFD\u0006\uFFFD\uFFFD\uFFFD\uFFFD\u0000\u0021\u0029\u007A\u0076\u005C\uFFFD\u0000\uFFFD\u006C\u0078\u0046\uFFFD\u0064\uFFFD\uFFFD\uFFFD\uFFFD\u0042\u007E\u006F\uFFFD\u0021\u0003\u0027\u0075\u001B\u006B\u0036\u0001\uFFFD\uFFFD\uFFFD\u000D\uFFFD\u0003\u0049\u0035\u007F\uFFFD\uFFFD\uFFFD\uFFFD\u0062\uFFFD\u007D\uFFFD\u0018\u0040\uFFFD\u006F\uFFFD\uFFFD\u002A\uFFFD\uFFFD\u000A\uFFFD\uFFFD\uFFFD\u0013\u0004\u003B\uFFFD\u0010' ) t.end() }) test('utf8 replacement chars for anything in the surrogate pair range', function (t) { t.equal( new B([0xED, 0x9F, 0xBF]).toString(), '\uD7FF' ) t.equal( new B([0xED, 0xA0, 0x80]).toString(), '\uFFFD\uFFFD\uFFFD' ) t.equal( new B([0xED, 0xBE, 0x8B]).toString(), '\uFFFD\uFFFD\uFFFD' ) t.equal( new B([0xED, 0xBF, 0xBF]).toString(), '\uFFFD\uFFFD\uFFFD' ) t.equal( new B([0xEE, 0x80, 0x80]).toString(), '\uE000' ) t.end() }) test('utf8 don\'t replace the replacement char', function (t) { t.equal( new B('\uFFFD').toString(), '\uFFFD' ) t.end() }) buffer-5.4.2/test/write.js000066400000000000000000000063541353132250700154530ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') var isnan = require('is-nan') test('buffer.write string should get parsed as number', function (t) { var b = new B(64) b.writeUInt16LE('1003', 0) t.equal(b.readUInt16LE(0), 1003) t.end() }) test('buffer.writeUInt8 a fractional number will get Math.floored', function (t) { // Some extra work is necessary to make this test pass with the Object implementation var b = new B(1) b.writeInt8(5.5, 0) t.equal(b[0], 5) t.end() }) test('writeUint8 with a negative number throws', function (t) { var buf = new B(1) t.throws(function () { buf.writeUInt8(-3, 0) }) t.end() }) test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) { t.plan(2 * ((2 * 2 * 2) + 2)) var hex = [ '03', '0300', '0003', '03000000', '00000003', 'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd' ] var reads = [3, 3, 3, 3, 3, -3, -3, -3, -3, -3] var xs = ['UInt', 'Int'] var ys = [8, 16, 32] for (var i = 0; i < xs.length; i++) { var x = xs[i] for (var j = 0; j < ys.length; j++) { var y = ys[j] var endianesses = (y === 8) ? [''] : ['LE', 'BE'] for (var k = 0; k < endianesses.length; k++) { var z = endianesses[k] var v1 = new B(y / 8) var writefn = 'write' + x + y + z var val = (x === 'Int') ? -3 : 3 v1[writefn](val, 0) t.equal( v1.toString('hex'), hex.shift() ) var readfn = 'read' + x + y + z t.equal( v1[readfn](0), reads.shift() ) } } } t.end() }) test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { t.plan(3 * ((2 * 2 * 2) + 2)) var hex = [ '', '03', '00', '030000', '000000', '', 'fd', 'ff', 'fdffff', 'ffffff' ] var reads = [ undefined, 3, 0, NaN, 0, undefined, 253, -256, 16777213, -256 ] var xs = ['UInt', 'Int'] var ys = [8, 16, 32] for (var i = 0; i < xs.length; i++) { var x = xs[i] for (var j = 0; j < ys.length; j++) { var y = ys[j] var endianesses = (y === 8) ? [''] : ['LE', 'BE'] for (var k = 0; k < endianesses.length; k++) { var z = endianesses[k] var v1 = new B((y / 8) - 1) var next = new B(4) next.writeUInt32BE(0, 0) var writefn = 'write' + x + y + z var val = (x === 'Int') ? -3 : 3 v1[writefn](val, 0, true) t.equal( v1.toString('hex'), hex.shift() ) // check that nothing leaked to next buffer. t.equal(next.readUInt32BE(0), 0) // check that no bytes are read from next buffer. next.writeInt32BE(~0, 0) var readfn = 'read' + x + y + z var r = reads.shift() if (isnan(r)) t.pass('equal') else t.equal(v1[readfn](0, true), r) } } } t.end() }) test('large values do not improperly roll over (ref #80)', function (t) { var nums = [-25589992, -633756690, -898146932] var out = new B(12) out.fill(0) out.writeInt32BE(nums[0], 0) var newNum = out.readInt32BE(0) t.equal(nums[0], newNum) out.writeInt32BE(nums[1], 4) newNum = out.readInt32BE(4) t.equal(nums[1], newNum) out.writeInt32BE(nums[2], 8) newNum = out.readInt32BE(8) t.equal(nums[2], newNum) t.end() }) buffer-5.4.2/test/write_infinity.js000066400000000000000000000021201353132250700173470ustar00rootroot00000000000000var B = require('../').Buffer var test = require('tape') test('write/read Infinity as a float', function (t) { var buf = new B(4) t.equal(buf.writeFloatBE(Infinity, 0), 4) t.equal(buf.readFloatBE(0), Infinity) t.end() }) test('write/read -Infinity as a float', function (t) { var buf = new B(4) t.equal(buf.writeFloatBE(-Infinity, 0), 4) t.equal(buf.readFloatBE(0), -Infinity) t.end() }) test('write/read Infinity as a double', function (t) { var buf = new B(8) t.equal(buf.writeDoubleBE(Infinity, 0), 8) t.equal(buf.readDoubleBE(0), Infinity) t.end() }) test('write/read -Infinity as a double', function (t) { var buf = new B(8) t.equal(buf.writeDoubleBE(-Infinity, 0), 8) t.equal(buf.readDoubleBE(0), -Infinity) t.end() }) test('write/read float greater than max', function (t) { var buf = new B(4) t.equal(buf.writeFloatBE(4e38, 0), 4) t.equal(buf.readFloatBE(0), Infinity) t.end() }) test('write/read float less than min', function (t) { var buf = new B(4) t.equal(buf.writeFloatBE(-4e40, 0), 4) t.equal(buf.readFloatBE(0), -Infinity) t.end() })