pax_global_header 0000666 0000000 0000000 00000000064 15065075427 0014525 g ustar 00root root 0000000 0000000 52 comment=47c574df5d2f45acbc762ce2eaa760b1263a3ae9
browserify-to-buffer-ef56d61/ 0000775 0000000 0000000 00000000000 15065075427 0016231 5 ustar 00root root 0000000 0000000 browserify-to-buffer-ef56d61/.eslintrc 0000664 0000000 0000000 00000000340 15065075427 0020052 0 ustar 00root root 0000000 0000000 {
"root": true,
"extends": "@ljharb",
"globals": {
"Float64Array": "readonly",
"Uint8Array": "readonly",
"Uint16Array": "readonly",
},
"rules": {
"complexity": "off",
"max-lines-per-function": "off",
},
}
browserify-to-buffer-ef56d61/.github/ 0000775 0000000 0000000 00000000000 15065075427 0017571 5 ustar 00root root 0000000 0000000 browserify-to-buffer-ef56d61/.github/FUNDING.yml 0000664 0000000 0000000 00000001117 15065075427 0021406 0 ustar 00root root 0000000 0000000 # These are supported funding model platforms
github: [ljharb, mafintosh]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: npm/to-buffer
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
browserify-to-buffer-ef56d61/.github/workflows/ 0000775 0000000 0000000 00000000000 15065075427 0021626 5 ustar 00root root 0000000 0000000 browserify-to-buffer-ef56d61/.github/workflows/node-aught.yml 0000664 0000000 0000000 00000000474 15065075427 0024411 0 ustar 00root root 0000000 0000000 name: 'Tests: node.js < 10'
on: [pull_request, push]
jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '< 10'
type: minors
command: npm run tests-only
node:
name: 'node < 10'
needs: [tests]
runs-on: ubuntu-latest
steps:
- run: true
browserify-to-buffer-ef56d61/.github/workflows/node-pretest.yml 0000664 0000000 0000000 00000000206 15065075427 0024760 0 ustar 00root root 0000000 0000000 name: 'Tests: pretest/posttest'
on: [pull_request, push]
jobs:
tests:
uses: ljharb/actions/.github/workflows/pretest.yml@main
browserify-to-buffer-ef56d61/.github/workflows/node-tens.yml 0000664 0000000 0000000 00000000547 15065075427 0024253 0 ustar 00root root 0000000 0000000 name: 'Tests: node.js 10 - 20'
on: [pull_request, push]
permissions:
contents: read
jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '>= 10 < 20'
type: minors
command: npm run tests-only
node:
name: 'node 10 - 20'
needs: [tests]
runs-on: ubuntu-latest
steps:
- run: true
browserify-to-buffer-ef56d61/.github/workflows/node-twenties.yml 0000664 0000000 0000000 00000000536 15065075427 0025142 0 ustar 00root root 0000000 0000000 name: 'Tests: node.js >= 20'
on: [pull_request, push]
permissions:
contents: read
jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '>= 20'
type: minors
command: npm run tests-only
node:
name: 'node >= 20'
needs: [tests]
runs-on: ubuntu-latest
steps:
- run: true
browserify-to-buffer-ef56d61/.github/workflows/rebase.yml 0000664 0000000 0000000 00000000257 15065075427 0023616 0 ustar 00root root 0000000 0000000 name: Automatic Rebase
on: [pull_request_target]
jobs:
_:
uses: ljharb/actions/.github/workflows/rebase.yml@main
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
browserify-to-buffer-ef56d61/.github/workflows/require-allow-edits.yml 0000664 0000000 0000000 00000000303 15065075427 0026243 0 ustar 00root root 0000000 0000000 name: Require “Allow Edits”
on: [pull_request_target]
jobs:
_:
name: "Require “Allow Edits”"
runs-on: ubuntu-latest
steps:
- uses: ljharb/require-allow-edits@main
browserify-to-buffer-ef56d61/.gitignore 0000664 0000000 0000000 00000000204 15065075427 0020215 0 ustar 00root root 0000000 0000000 node_modules
coverage/
.nyc_output/
# Only apps should have lockfiles
npm-shrinkwrap.json
package-lock.json
yarn.lock
.npmignore
browserify-to-buffer-ef56d61/.npmrc 0000664 0000000 0000000 00000000067 15065075427 0017354 0 ustar 00root root 0000000 0000000 package-lock=false
allow-same-version=true
message=v%s
browserify-to-buffer-ef56d61/.nycrc 0000664 0000000 0000000 00000000213 15065075427 0017344 0 ustar 00root root 0000000 0000000 {
"all": true,
"check-coverage": false,
"reporter": ["text-summary", "text", "html", "json"],
"exclude": [
"coverage",
"test"
]
}
browserify-to-buffer-ef56d61/CHANGELOG.md 0000664 0000000 0000000 00000013553 15065075427 0020051 0 ustar 00root root 0000000 0000000 # Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v1.2.2](https://github.com/browserify/to-buffer/compare/v1.2.1...v1.2.2) - 2025-09-24
### Commits
- [Fix] handle SlowBuffers in node 0.10 [`ca20eaa`](https://github.com/browserify/to-buffer/commit/ca20eaad8c9cbd6e3e6e99880b2b1c95abe62566)
- [Refactor] use `SafeBuffer.isBuffer` instead of `instanceof` [`81283c1`](https://github.com/browserify/to-buffer/commit/81283c14b585c0e921e04f327381e975cc8561bb)
- [Dev Deps] update `@ljharb/eslint-config` [`c7bc986`](https://github.com/browserify/to-buffer/commit/c7bc986d378ce4bdf2dac75612b45b0f618f26d6)
- [meta] since tests are npmignored, also npmignore test config files [`866639c`](https://github.com/browserify/to-buffer/commit/866639cf294799f8c0397153e5876ae1e6992a57)
## [v1.2.1](https://github.com/browserify/to-buffer/compare/v1.2.0...v1.2.1) - 2025-06-19
### Commits
- [Fix] handle non-Uint8Arrays in node < 3 [`7f8a881`](https://github.com/browserify/to-buffer/commit/7f8a881929133935f8e15ffd60d6dbbc513b2c5f)
- [Tests] add coverage [`286c96a`](https://github.com/browserify/to-buffer/commit/286c96a52cfeee14a2ba974d78071bdd667e9360)
- [Fix] provide a fallback for engines without `ArrayBuffer.isView` [`e336166`](https://github.com/browserify/to-buffer/commit/e336166b8f4bf13860bafa191ee1ec53fca2e331)
- [Fix] correct error message [`b45247e`](https://github.com/browserify/to-buffer/commit/b45247ed337fb44b2c8d74a14e8f86d985119fb9)
## [v1.2.0](https://github.com/browserify/to-buffer/compare/v1.1.1...v1.2.0) - 2025-06-17
### Commits
- [New] replace with implementation from cipher-base [`970adb5`](https://github.com/browserify/to-buffer/commit/970adb5523efdaa13f5ecb82967fc9f617865549)
- [Tests] migrate from travis to GHA [`8084393`](https://github.com/browserify/to-buffer/commit/808439337ca9ac3dbb8399079aaa2a4cb738627c)
- [eslint] fix whitespace [`a62e651`](https://github.com/browserify/to-buffer/commit/a62e651b661adf98c17e9e486b55bb8ff0ffb8c9)
- [eslint] fix semicolon usage [`4d85c63`](https://github.com/browserify/to-buffer/commit/4d85c6318c72feae8d19037937154f9b99ba266f)
- [meta] add `auto-changelog` [`aa0279c`](https://github.com/browserify/to-buffer/commit/aa0279c5199ca7fe39acfb950a4c824e97f06232)
- [readme] update URLs, add badges [`ff77d90`](https://github.com/browserify/to-buffer/commit/ff77d90b89de7b02538ecb2d6a89da086093bed8)
- [lint] switch to eslint [`e45f467`](https://github.com/browserify/to-buffer/commit/e45f467c7229e632cd3c10fc02895ba4d3204bbe)
- [Fix] validate that arrays contain valid byte values [`c2fb75e`](https://github.com/browserify/to-buffer/commit/c2fb75edf2fb113d58599e990f86574b4dfa62d8)
- [Fix] restore previous implementation Array behavior [`cb93b75`](https://github.com/browserify/to-buffer/commit/cb93b75a79caa9897f6c29ecde91f4ae35f704fe)
- [Tests] add nyc for coverage [`ab7026e`](https://github.com/browserify/to-buffer/commit/ab7026e36e3716c8101229f426e7f4571e55794b)
- [Refactor] use `safe-buffer`.from instead of `buffer-from` [`8e01307`](https://github.com/browserify/to-buffer/commit/8e01307191245044469e47695c5c2675b85e84e9)
- [Fix] Replace Buffer.from with `buffer-from` [`d652e54`](https://github.com/browserify/to-buffer/commit/d652e54e2396a47358a553c447e0f338b4c2dc67)
- [Tests] use `deepEqual` over `same` alias [`66a5548`](https://github.com/browserify/to-buffer/commit/66a55480258011bb5d81c5aad1360468f418d0b4)
- [meta] add `npmignore` [`90ce602`](https://github.com/browserify/to-buffer/commit/90ce6023737d50521aff44d87063db1e3f7e352a)
- [Tests] move into a test dir, update tape [`08aea81`](https://github.com/browserify/to-buffer/commit/08aea81b61b90d1fcb7e9275b5b0ba718531d9a8)
- Only apps should have lockfiles [`16ccceb`](https://github.com/browserify/to-buffer/commit/16ccceb23f350be16e80111188c90a3492916f5d)
- [Tests] add coverage [`d2cba2e`](https://github.com/browserify/to-buffer/commit/d2cba2ec76ed43c83e2a7a91f58fa7640aeb61e9)
- [meta] update description [`2cf2a20`](https://github.com/browserify/to-buffer/commit/2cf2a200a31f9543d2e8a24b5ea0e8bd843166c3)
- [Fix] add `safe-buffer`, missing from 970adb5 [`d9a0dea`](https://github.com/browserify/to-buffer/commit/d9a0dead7c638d188f8b48cdf2b5fd6cfa886071)
- [meta] temporarily limit support to node v0.10 [`8dca458`](https://github.com/browserify/to-buffer/commit/8dca458bd9a2c6b84d6e3a1996e41b242fe1c49a)
- [meta] add missing `engines.node` [`35bdfcb`](https://github.com/browserify/to-buffer/commit/35bdfcb3a71dfbd5b35d35250c9dac19c99f4197)
- [Dev Deps] add missing peer dep [`220143f`](https://github.com/browserify/to-buffer/commit/220143f1f6e47154380c27a2d88ce300104007fa)
- [meta] add `sideEffects` flag [`cd37473`](https://github.com/browserify/to-buffer/commit/cd374738d24b22b029862b3c27b9e247d4e62daf)
## [v1.1.1](https://github.com/browserify/to-buffer/compare/v1.1.0...v1.1.1) - 2018-04-26
### Commits
- use Buffer.from when avail [`eebe20e`](https://github.com/browserify/to-buffer/commit/eebe20e0603e2c6a542b00316f1661741fdf1124)
## [v1.1.0](https://github.com/browserify/to-buffer/compare/v1.0.1...v1.1.0) - 2017-04-12
### Merged
- Fix typo [`#2`](https://github.com/browserify/to-buffer/pull/2)
### Commits
- support arrays as well [`ef98c82`](https://github.com/browserify/to-buffer/commit/ef98c82791d71601077577e84c4614ec2d05f086)
## [v1.0.1](https://github.com/browserify/to-buffer/compare/v1.0.0...v1.0.1) - 2016-02-15
### Commits
- fix desc [`7d50a7c`](https://github.com/browserify/to-buffer/commit/7d50a7c69c3eef77448893744ada16601c44af6a)
## v1.0.0 - 2016-02-15
### Commits
- first commit [`8361941`](https://github.com/browserify/to-buffer/commit/8361941d7acb3b82c732ecd10bdb047da5af2028)
- add travis [`de911b5`](https://github.com/browserify/to-buffer/commit/de911b5364558561d84b4ec9e43c6a0fe1c8e904)
browserify-to-buffer-ef56d61/LICENSE 0000664 0000000 0000000 00000002067 15065075427 0017243 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2016 Mathias Buus
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.
browserify-to-buffer-ef56d61/README.md 0000664 0000000 0000000 00000003133 15065075427 0017510 0 ustar 00root root 0000000 0000000 # to-buffer [![Version Badge][2]][1]
Pass in a string, array, Buffer, Data View, or Uint8Array, and get a Buffer back.
[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![dependency status][5]][6]
[![dev dependency status][7]][8]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]
[![npm badge][11]][1]
```
npm install to-buffer
```
## Usage
``` js
var toBuffer = require('to-buffer');
console.log(toBuffer('hi')); //
console.log(toBuffer(Buffer('hi'))); //
console.log(toBuffer('6869', 'hex')); //
console.log(toBuffer(43)); // throws
```
[1]: https://npmjs.org/package/to-buffer
[2]: https://versionbadg.es/browserify/to-buffer.svg
[5]: https://david-dm.org/browserify/to-buffer.svg
[6]: https://david-dm.org/browserify/to-buffer
[7]: https://david-dm.org/browserify/to-buffer/dev-status.svg
[8]: https://david-dm.org/browserify/to-buffer#info=devDependencies
[11]: https://nodei.co/npm/to-buffer.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/to-buffer.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/to-buffer.svg
[downloads-url]: https://npm-stat.com/charts.html?package=to-buffer
[codecov-image]: https://codecov.io/gh/browserify/to-buffer/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/browserify/to-buffer/
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/browserify/to-buffer
[actions-url]: https://github.com/browserify/to-buffer/actions
browserify-to-buffer-ef56d61/index.js 0000664 0000000 0000000 00000005751 15065075427 0017706 0 ustar 00root root 0000000 0000000 'use strict';
var Buffer = require('safe-buffer').Buffer;
var isArray = require('isarray');
var typedArrayBuffer = require('typed-array-buffer');
var isView = ArrayBuffer.isView || function isView(obj) {
try {
typedArrayBuffer(obj);
return true;
} catch (e) {
return false;
}
};
var useUint8Array = typeof Uint8Array !== 'undefined';
var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
&& typeof Uint8Array !== 'undefined';
var useFromArrayBuffer = useArrayBuffer && (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);
module.exports = function toBuffer(data, encoding) {
if (Buffer.isBuffer(data)) {
if (data.constructor && !('isBuffer' in data)) {
// probably a SlowBuffer
return Buffer.from(data);
}
return data;
}
if (typeof data === 'string') {
return Buffer.from(data, encoding);
}
/*
* Wrap any TypedArray instances and DataViews
* Makes sense only on engines with full TypedArray support -- let Buffer detect that
*/
if (useArrayBuffer && isView(data)) {
// Bug in Node.js <6.3.1, which treats this as out-of-bounds
if (data.byteLength === 0) {
return Buffer.alloc(0);
}
// When Buffer is based on Uint8Array, we can just construct it from ArrayBuffer
if (useFromArrayBuffer) {
var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
/*
* Recheck result size, as offset/length doesn't work on Node.js <5.10
* We just go to Uint8Array case if this fails
*/
if (res.byteLength === data.byteLength) {
return res;
}
}
// Convert to Uint8Array bytes and then to Buffer
var uint8 = data instanceof Uint8Array ? data : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
var result = Buffer.from(uint8);
/*
* Let's recheck that conversion succeeded
* We have .length but not .byteLength when useFromArrayBuffer is false
*/
if (result.length === data.byteLength) {
return result;
}
}
/*
* Uint8Array in engines where Buffer.from might not work with ArrayBuffer, just copy over
* Doesn't make sense with other TypedArray instances
*/
if (useUint8Array && data instanceof Uint8Array) {
return Buffer.from(data);
}
var isArr = isArray(data);
if (isArr) {
for (var i = 0; i < data.length; i += 1) {
var x = data[i];
if (
typeof x !== 'number'
|| x < 0
|| x > 255
|| ~~x !== x // NaN and integer check
) {
throw new RangeError('Array items must be numbers in the range 0-255.');
}
}
}
/*
* Old Buffer polyfill on an engine that doesn't have TypedArray support
* Also, this is from a different Buffer polyfill implementation then we have, as instanceof check failed
* Convert to our current Buffer implementation
*/
if (
isArr || (
Buffer.isBuffer(data)
&& data.constructor
&& typeof data.constructor.isBuffer === 'function'
&& data.constructor.isBuffer(data)
)
) {
return Buffer.from(data);
}
throw new TypeError('The "data" argument must be a string, an Array, a Buffer, a Uint8Array, or a DataView.');
};
browserify-to-buffer-ef56d61/package.json 0000664 0000000 0000000 00000003200 15065075427 0020512 0 ustar 00root root 0000000 0000000 {
"name": "to-buffer",
"version": "1.2.2",
"description": "Pass in a string, array, Buffer, Data View, or Uint8Array, and get a Buffer back.",
"main": "index.js",
"sideEffects": false,
"scripts": {
"prepack": "npmignore --auto --commentLines=autogenerated",
"lint": "eslint --ext=js,mjs .",
"pretest": "npm run lint",
"tests-only": "nyc tape 'test/**/*'",
"test": "npm run tests-only",
"posttest": "npx npm@\">= 10.2\" audit --production",
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
},
"repository": {
"type": "git",
"url": "https://github.com/browserify/to-buffer.git"
},
"author": "Mathias Buus (@mafintosh)",
"license": "MIT",
"bugs": {
"url": "https://github.com/browserify/to-buffer/issues"
},
"homepage": "https://github.com/browserify/to-buffer",
"dependencies": {
"isarray": "^2.0.5",
"safe-buffer": "^5.2.1",
"typed-array-buffer": "^1.0.3"
},
"devDependencies": {
"@ljharb/eslint-config": "^21.2.0",
"auto-changelog": "^2.5.0",
"available-typed-arrays": "^1.0.7",
"encoding": "^0.1.13",
"eslint": "=8.8.0",
"for-each": "^0.3.5",
"npmignore": "^0.3.1",
"nyc": "^10.3.2",
"tape": "^5.9.0"
},
"engines": {
"node": ">= 0.4"
},
"publishConfig": {
"ignore": [
".github/workflows",
".eslintrc",
".nycrc",
"test"
]
},
"auto-changelog": {
"output": "CHANGELOG.md",
"template": "keepachangelog",
"unreleased": false,
"commitLimit": false,
"backfillLimit": false,
"hideCredit": true
}
}
browserify-to-buffer-ef56d61/test/ 0000775 0000000 0000000 00000000000 15065075427 0017210 5 ustar 00root root 0000000 0000000 browserify-to-buffer-ef56d61/test/fixtures.json 0000664 0000000 0000000 00000002556 15065075427 0021764 0 ustar 00root root 0000000 0000000 {
"Float16Array": {
"input": [1234.56, 45.67],
"output": [211, 100, 181, 81]
},
"Float32Array": {
"input": [1234.56, 45.67],
"output": [236, 81, 154, 68, 20, 174, 54, 66]
},
"Float64Array": {
"input": [1234.56, 45.67],
"output": [10, 215, 163, 112, 61, 74, 147, 64, 246, 40, 92, 143, 194, 213, 70, 64]
},
"Int8Array": {
"input": [1234, 45],
"output": [210, 45]
},
"Int16Array": {
"input": [1234, 512],
"output": [210, 4, 0, 2]
},
"Int32Array": {
"input": [1234, 512],
"output": [210, 4, 0, 0, 0, 2, 0, 0]
},
"Uint8Array": {
"input": [1234, 45],
"output": [210, 45]
},
"Uint8ClampedArray": {
"input": [1234, 45],
"output": [255, 45]
},
"Uint16Array": {
"input": [1234, 512],
"output": [210, 4, 0, 2]
},
"Uint32Array": {
"input": [1234, 512],
"output": [210, 4, 0, 0, 0, 2, 0, 0]
},
"BigInt64Array": {
"input": ["123456789012345678", "987654321098765432"],
"output": [78, 243, 48, 166, 75, 155, 182, 1, 120, 180, 248, 73, 95, 218, 180, 13]
},
"BigUint64Array": {
"input": ["123456789012345678", "987654321098765432"],
"output": [78, 243, 48, 166, 75, 155, 182, 1, 120, 180, 248, 73, 95, 218, 180, 13]
}
} browserify-to-buffer-ef56d61/test/index.js 0000664 0000000 0000000 00000007022 15065075427 0020656 0 ustar 00root root 0000000 0000000 'use strict';
var test = require('tape');
var availableTypedArrays = require('available-typed-arrays')();
var forEach = require('for-each');
var typedArrayBuffer = require('typed-array-buffer');
var SafeBuffer = require('safe-buffer').Buffer;
var SlowBuffer = require('buffer').SlowBuffer;
var toBuffer = require('../');
var fixtures = require('./fixtures.json');
test('buffer returns buffer', function (t) {
var result = toBuffer(new Buffer('hi'));
t.deepEqual(result, new Buffer('hi'));
t.ok(result instanceof Buffer, 'is a Buffer');
t.notOk(result instanceof SlowBuffer, 'not a SlowBuffer');
t.end();
});
test('SlowBuffer returns Buffer', { skip: SlowBuffer.name === 'deprecated' }, function (t) {
var slow = new SlowBuffer(2);
slow[0] = 7;
slow[1] = 8;
var result = toBuffer(slow);
t.deepEqual(result, new Buffer([7, 8]));
t.ok(result instanceof Buffer, 'is a Buffer');
t.notOk(result instanceof SlowBuffer, 'not a SlowBuffer');
t.end();
});
test('string returns buffer', function (t) {
t.deepEqual(toBuffer('hi'), new Buffer('hi'));
t.end();
});
test('string + enc returns buffer', function (t) {
t.deepEqual(toBuffer('6869', 'hex'), new Buffer('hi'));
t.end();
});
test('array returns buffer', function (t) {
t.deepEqual(toBuffer([104, 105]), new Buffer('hi'));
forEach([-1, 256, NaN, 4.2, Infinity], function (nonByte) {
t['throws'](
function () { toBuffer([0, 42, nonByte]); },
RangeError,
nonByte + ': arrays with out-of-bounds byte values throw'
);
});
t.end();
});
test('other input throws', function (t) {
try {
toBuffer(42);
} catch (err) {
t.deepEqual(err.message, 'The "data" argument must be a string, an Array, a Buffer, a Uint8Array, or a DataView.');
t.end();
}
});
test('handle all TA types', function (t) {
forEach(availableTypedArrays, function (type) {
var TA = global[type];
if (!(type in fixtures)) {
t.fail('No fixtures for ' + type);
return;
}
var input = fixtures[type].input;
if (typeof input[0] === 'string') {
for (var i = 0; i < input.length; i++) {
input[i] = BigInt(input[i]);
}
}
t.deepEqual(
toBuffer(new TA(input)),
new Buffer(fixtures[type].output),
type + ' should be converted to Buffer correctly'
);
});
t.test('TA subset view on another one', { skip: typeof Float64Array === 'undefined' || typeof Uint8Array === 'undefined' }, function (st) {
var arr = new Uint8Array(100);
for (var i = 0; i < arr.length; i += 1) {
arr[i] = i;
}
var buffer = typedArrayBuffer(arr);
var expectedHex = '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f60616263';
forEach(availableTypedArrays, function (type) {
var TA = global[type];
var offset = 8;
var len = 4;
var arr2 = new TA(buffer, offset, len);
st.ok(arr2.BYTES_PER_ELEMENT >= 1, 'BYTES_PER_ELEMENT: sanity check');
var expectedSlice = expectedHex.slice(offset * 2, (offset + (len * arr2.BYTES_PER_ELEMENT)) * 2);
var expected = SafeBuffer.from(expectedSlice, 'hex');
st.equal(expected.length, len * arr2.BYTES_PER_ELEMENT, 'expected length: sanity check');
var result = toBuffer(arr2);
st.deepEqual(
result,
expected,
'Uint8Array subset view on ' + type + ' should be converted to Buffer correctly'
);
st.equal(
result.toString('hex'),
expectedSlice,
'Uint8Array subset view on ' + type + ' should be converted to Buffer that toStrings correctly'
);
});
st.end();
});
t.end();
});