Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bytesish

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bytesish - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

8

browser.js

@@ -42,8 +42,8 @@ /* globals atob, btoa */

bytes.native = arg => {
if (arg instanceof Uint8Array) return arg
arg = bytes.from(arg)
return new Uint8Array(arg.buffer, arg.byteOffset, arg.byteLength)
bytes.native = (_from, encoding) => {
if (_from instanceof Uint8Array) return _from
_from = bytes.from(_from, encoding)
return new Uint8Array(_from.buffer, _from.byteOffset, _from.byteLength)
}
module.exports = bytes

@@ -11,3 +11,3 @@ 'use strict'

bytes.sort = (a, b) => {
bytes.sorter = (a, b) => {
a = bytes(a)

@@ -28,3 +28,3 @@ b = bytes(b)

bytes.compare = (a, b) => !bytes.sort(a, b)
bytes.compare = (a, b) => !bytes.sorter(a, b)
bytes.memcopy = (_from, encoding) => {

@@ -34,3 +34,19 @@ const b = bytes(_from, encoding)

}
bytes.arrayBuffer = (_from, encoding) => {
_from = bytes(_from, encoding)
if (_from.buffer.byteLength === _from.byteLength) return _from.buffer
return _from.buffer.slice(_from.byteOffset, _from.byteOffset + _from.byteLength)
}
const sliceOptions = (_from, start = 0, end = null) => {
_from = bytes(_from)
end = (end === null ? _from.byteLength : end) - start
return [_from.buffer, _from.byteOffset + start, end]
}
bytes.slice = (_from, start, end) => new DataView(...sliceOptions(_from, start, end))
bytes.memcopySlice = (_from, start, end) => {
const [buffer, offset, length] = sliceOptions(_from, start, end)
return buffer.slice(offset, length + offset)
}
module.exports = bytes

@@ -21,8 +21,8 @@ 'use strict'

bytes.native = arg => {
if (Buffer.isBuffer(arg)) return arg
arg = bytes(arg)
return Buffer.from(arg.buffer, arg.byteOffset, arg.byteLength)
bytes.native = (_from, encoding) => {
if (Buffer.isBuffer(_from)) return _from
_from = bytes(_from, encoding)
return Buffer.from(_from.buffer, _from.byteOffset, _from.byteLength)
}
module.exports = bytes
{
"name": "bytesish",
"version": "0.1.0",
"version": "0.2.0",
"main": "node.js",

@@ -5,0 +5,0 @@ "browser": "browser.js",

@@ -60,1 +60,22 @@ # `bytesish`

# API
## Zero Copy
### `bytes(from[, encoding])`
### `bytes.sort(a, b)`
### `bytes.compare(a, b)`
### `bytes.native(from[, encoding])
## Optimized (memcopy only when necessary)
### `bytes.arrayBuffer(_from[, encoding])`
## Memory Copy
All binary API's that **must** do a memcopy are prefaced with `"memcopy"`.
## String Conversions

@@ -38,3 +38,3 @@ 'use strict'

test('array buffer', done => {
test('from array buffer', done => {
const a = bytes('hello world')

@@ -46,2 +46,11 @@ const b = bytes(bytes.memcopy(a))

test('to array buffer', done => {
const a = bytes.arrayBuffer('hello world')
const b = bytes('hello world')
assert(a instanceof ArrayBuffer)
assert(bytes.compare(a, b))
assert(a, bytes.arrayBuffer(a))
done()
})
test('Uint8Array', done => {

@@ -67,1 +76,20 @@ const a = bytes('hello world')

})
test('slice', done => {
const a = bytes.slice('hello world', 2, 7)
assert(a instanceof DataView)
const b = bytes.arrayBuffer('hello world').slice(2, 7)
same(b, bytes.arrayBuffer(a))
done()
})
test('slice memcopy', done => {
const a = bytes.memcopySlice('hello world', 2, 7)
assert(a instanceof ArrayBuffer)
const b = bytes.arrayBuffer('hello world').slice(2, 7)
same(b, a)
const c = bytes.memcopySlice(a)
assert(a !== c)
same(a, c)
done()
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc