Skip to content

Commit ea51546

Browse files
authored
feat!: use buffer.concat in node (#76)
Reinstates the performance changes from #73 BREAKING CHANGE: concat now expects an array of Uint8Arrays
1 parent 5123219 commit ea51546

File tree

5 files changed

+155
-18
lines changed

5 files changed

+155
-18
lines changed

.github/workflows/js-test-and-release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ on:
99

1010
permissions:
1111
contents: write
12+
id-token: write
1213
packages: write
14+
pull-requests: write
1315

1416
concurrency:
1517
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name == 'push' && github.sha || github.ref }}

README.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,149 @@
33

44
> Utility functions to make dealing with Uint8Arrays easier
55
6+
# About
7+
8+
`Uint8Array`s bring memory-efficient(ish) byte handling to browsers - they are similar to Node.js `Buffer`s but lack a lot of the utility methods present on that class.
9+
10+
This module exports a number of function that let you do common operations - joining Uint8Arrays together, seeing if they have the same contents etc.
11+
12+
Since Node.js `Buffer`s are also `Uint8Array`s, it falls back to `Buffer` internally where it makes sense for performance reasons.
13+
14+
## alloc(size)
15+
16+
Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`.
17+
18+
### Example
19+
20+
```js
21+
import { alloc } from 'uint8arrays/alloc'
22+
23+
const buf = alloc(100)
24+
```
25+
26+
## allocUnsafe(size)
27+
28+
Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`.
29+
30+
On platforms that support it, memory referenced by the returned `Uint8Array` will not be initialized.
31+
32+
### Example
33+
34+
```js
35+
import { allocUnsafe } from 'uint8arrays/alloc'
36+
37+
const buf = allocUnsafe(100)
38+
```
39+
40+
## compare(a, b)
41+
42+
Compare two `Uint8Arrays`
43+
44+
### Example
45+
46+
```js
47+
import { compare } from 'uint8arrays/compare'
48+
49+
const arrays = [
50+
Uint8Array.from([3, 4, 5]),
51+
Uint8Array.from([0, 1, 2])
52+
]
53+
54+
const sorted = arrays.sort(compare)
55+
56+
console.info(sorted)
57+
// [
58+
// Uint8Array[0, 1, 2]
59+
// Uint8Array[3, 4, 5]
60+
// ]
61+
```
62+
63+
## concat(arrays, \[length])
64+
65+
Concatenate one or more `Uint8Array`s and return a `Uint8Array` with their contents.
66+
67+
If you know the length of the arrays, pass it as a second parameter, otherwise it will be calculated by traversing the list of arrays.
68+
69+
### Example
70+
71+
```js
72+
import { concat } from 'uint8arrays/concat'
73+
74+
const arrays = [
75+
Uint8Array.from([0, 1, 2]),
76+
Uint8Array.from([3, 4, 5])
77+
]
78+
79+
const all = concat(arrays, 6)
80+
81+
console.info(all)
82+
// Uint8Array[0, 1, 2, 3, 4, 5]
83+
```
84+
85+
## equals(a, b)
86+
87+
Returns true if the two arrays are the same array or if they have the same length and contents.
88+
89+
### Example
90+
91+
```js
92+
import { equals } from 'uint8arrays/equals'
93+
94+
const a = Uint8Array.from([0, 1, 2])
95+
const b = Uint8Array.from([3, 4, 5])
96+
const c = Uint8Array.from([0, 1, 2])
97+
98+
console.info(equals(a, b)) // false
99+
console.info(equals(a, c)) // true
100+
console.info(equals(a, a)) // true
101+
```
102+
103+
## fromString(string, encoding = 'utf8')
104+
105+
Returns a new `Uint8Array` created from the passed string and interpreted as the passed encoding.
106+
107+
Supports `utf8` and any of the [multibase encodings](https://github.com/multiformats/multibase/blob/master/multibase.csv) as implemented by the [multiformats module](https://www.npmjs.com/package/multiformats).
108+
109+
### Example
110+
111+
```js
112+
import { fromString } from 'uint8arrays/from-string'
113+
114+
console.info(fromString('hello world')) // Uint8Array[104, 101 ...
115+
console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ...
116+
console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ...
117+
console.info(fromString('01234', 'ascii')) // Uint8Array[48, 49 ...
118+
```
119+
120+
## toString(array, encoding = 'utf8')
121+
122+
Returns a string created from the passed `Uint8Array` in the passed encoding.
123+
124+
Supports `utf8` and any of the [multibase encodings](https://github.com/multiformats/multibase/blob/master/multibase.csv) as implemented by the [multiformats module](https://www.npmjs.com/package/multiformats).
125+
126+
### Example
127+
128+
```js
129+
import { toString } from 'uint8arrays/to-string'
130+
131+
console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world'
132+
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc'
133+
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA'
134+
console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234'
135+
```
136+
137+
## xor(a, b)
138+
139+
Returns a `Uint8Array` containing `a` and `b` xored together.
140+
141+
### Example
142+
143+
```js
144+
import { xor } from 'uint8arrays/xor'
145+
146+
console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]
147+
```
148+
6149
# Install
7150

8151
```console

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"bugs": {
1313
"url": "https://github.com/achingbrain/uint8arrays/issues"
1414
},
15+
"publishConfig": {
16+
"access": "public",
17+
"provenance": true
18+
},
1519
"type": "module",
1620
"types": "./dist/src/index.d.ts",
1721
"typesVersions": {

src/concat.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { allocUnsafe } from './alloc.js'
22
import { asUint8Array } from './util/as-uint8array.js'
33

44
/**
5-
* Returns a new Uint8Array created by concatenating the passed ArrayLikes
5+
* Returns a new Uint8Array created by concatenating the passed Uint8Arrays
66
*/
7-
export function concat (arrays: Array<ArrayLike<number>>, length?: number): Uint8Array {
7+
export function concat (arrays: Uint8Array[], length?: number): Uint8Array {
8+
if (globalThis.Buffer != null) {
9+
return asUint8Array(globalThis.Buffer.concat(arrays, length))
10+
}
11+
812
if (length == null) {
913
length = arrays.reduce((acc, curr) => acc + curr.length, 0)
1014
}

test/concat.spec.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ describe('Uint8Array concat', () => {
2121
expect(concat([a, b], 8)).to.deep.equal(c)
2222
})
2323

24-
it('concats mixed Uint8Arrays and Arrays', () => {
25-
const a = Uint8Array.from([0, 1, 2, 3])
26-
const b = [4, 5, 6, 7]
27-
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7])
28-
29-
expect(concat([a, b])).to.deep.equal(c)
30-
})
31-
32-
it('concats mixed Uint8Arrays and Arrays with a length', () => {
33-
const a = Uint8Array.from([0, 1, 2, 3])
34-
const b = [4, 5, 6, 7]
35-
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7])
36-
37-
expect(concat([a, b], 8)).to.deep.equal(c)
38-
})
39-
4024
it('concat returns Uint8Array', () => {
4125
const a = Uint8Array.from([0, 1, 2, 3])
4226
const b = alloc(10).fill(1)

0 commit comments

Comments
 (0)