Skip to content

Commit e43f1f4

Browse files
ukstvachingbrain
andauthored
fix: Use subpath imports to choose Node or vanilla JS environment (#88)
Import buffer from `node:buffer` instead of using `globalThis.Buffer` to prevent accidentally using janky polyfills and allow bundlers to cleanly supply valid versions. Fixes: #87 --------- Co-authored-by: achingbrain <[email protected]>
1 parent dc81e75 commit e43f1f4

27 files changed

+192
-59
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
66
# About
77

8+
<!--
9+
10+
!IMPORTANT!
11+
12+
Everything in this README between "# About" and "# Install" is automatically
13+
generated and will be overwritten the next time the doc generator is run.
14+
15+
To make changes to this section, please update the @packageDocumentation section
16+
of src/index.js or src/index.ts
17+
18+
To experiment with formatting, please run "npm run docs" from the root of this
19+
repo and examine the changes made.
20+
21+
-->
22+
823
`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.
924

1025
This module exports a number of function that let you do common operations - joining Uint8Arrays together, seeing if they have the same contents etc.
@@ -13,7 +28,7 @@ Since Node.js `Buffer`s are also `Uint8Array`s, it falls back to `Buffer` intern
1328

1429
## alloc(size)
1530

16-
Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`.
31+
Create a new `Uint8Array`. When running under Node.js, `Buffer` will be used in preference to `Uint8Array`.
1732

1833
### Example
1934

@@ -25,7 +40,7 @@ const buf = alloc(100)
2540

2641
## allocUnsafe(size)
2742

28-
Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`.
43+
Create a new `Uint8Array`. When running under Node.js, `Buffer` will be used in preference to `Uint8Array`.
2944

3045
On platforms that support it, memory referenced by the returned `Uint8Array` will not be initialized.
3146

benchmarks/alloc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ $ npx playwright-test benchmarks/alloc.js --runner benchmark
66
*/
77

88
import Benchmark from 'benchmark'
9-
import { alloc, allocUnsafe } from '../src/dist/alloc.js'
9+
import { alloc, allocUnsafe } from '#alloc'
1010

1111
const LENGTH = 1024
1212

benchmarks/concat.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ $ npx playwright-test benchmarks/concat.js --runner benchmark
66
*/
77

88
import Benchmark from 'benchmark'
9-
import { allocUnsafe } from '../dist/src/alloc.js'
10-
import { concat } from '../dist/src/concat.js'
11-
import { fromString } from '../dist/src/from-string.js'
9+
import { allocUnsafe } from '#alloc'
10+
import { concat } from '#concat'
11+
import { fromString } from '#from-string'
1212

1313
const string = 'Hello world, this is a Uint8Array created from a string'
1414
const DATA1 = fromString(string)

benchmarks/from-string.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ $ npx playwright-test benchmarks/from-string.js --runner benchmark
66
*/
77

88
import Benchmark from 'benchmark'
9-
import { fromString } from '../dist/src/from-string.js'
9+
import { fromString } from '#from-string'
1010

1111
const string = 'Hello world, this is a Uint8Array created from a string'
1212
const DATA = fromString(string)

benchmarks/to-string.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ $ npx playwright-test benchmarks/to-string.js --runner benchmark
66
*/
77

88
import Benchmark from 'benchmark'
9-
import { fromString } from '../dist/src/from-string.js'
10-
import { toString } from '../dist/src/to-string.js'
9+
import { fromString } from '#from-string'
10+
import { toString } from '#to-string'
1111

1212
const string = 'Hello world, this is a Uint8Array created from a string'
1313
const DATA = fromString(string)

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@
4747
},
4848
"./alloc": {
4949
"types": "./dist/src/alloc.d.ts",
50+
"node": "./dist/src/alloc.node.js",
5051
"import": "./dist/src/alloc.js"
5152
},
5253
"./compare": {
5354
"types": "./dist/src/compare.d.ts",
55+
"node": "./dist/src/compare.node.js",
5456
"import": "./dist/src/compare.js"
5557
},
5658
"./concat": {
5759
"types": "./dist/src/concat.d.ts",
60+
"node": "./dist/src/concat.node.js",
5861
"import": "./dist/src/concat.js"
5962
},
6063
"./equals": {
@@ -63,17 +66,51 @@
6366
},
6467
"./from-string": {
6568
"types": "./dist/src/from-string.d.ts",
69+
"node": "./dist/src/from-string.node.js",
6670
"import": "./dist/src/from-string.js"
6771
},
6872
"./to-string": {
6973
"types": "./dist/src/to-string.d.ts",
74+
"node": "./dist/src/to-string.node.js",
7075
"import": "./dist/src/to-string.js"
7176
},
7277
"./xor": {
7378
"types": "./dist/src/xor.d.ts",
7479
"import": "./dist/src/xor.js"
7580
}
7681
},
82+
"imports": {
83+
"#util/as-uint8array": {
84+
"types": "./dist/src/util/as-uint8array.d.ts",
85+
"node": "./dist/src/util/as-uint8array.node.js",
86+
"import": "./dist/src/util/as-uint8array.js"
87+
},
88+
"#alloc": {
89+
"types": "./dist/src/alloc.d.ts",
90+
"node": "./dist/src/alloc.node.js",
91+
"import": "./dist/src/alloc.js"
92+
},
93+
"#compare": {
94+
"types": "./dist/src/compare.d.ts",
95+
"node": "./dist/src/compare.node.js",
96+
"import": "./dist/src/compare.js"
97+
},
98+
"#concat": {
99+
"types": "./dist/src/concat.d.ts",
100+
"node": "./dist/src/concat.node.js",
101+
"import": "./dist/src/concat.js"
102+
},
103+
"#from-string": {
104+
"types": "./dist/src/from-string.d.ts",
105+
"node": "./dist/src/from-string.node.js",
106+
"import": "./dist/src/from-string.js"
107+
},
108+
"#to-string": {
109+
"types": "./dist/src/to-string.d.ts",
110+
"node": "./dist/src/to-string.node.js",
111+
"import": "./dist/src/to-string.js"
112+
}
113+
},
77114
"eslintConfig": {
78115
"extends": "ipfs",
79116
"parserOptions": {
@@ -187,5 +224,13 @@
187224
"@types/benchmark": "^2.1.1",
188225
"aegir": "^42.2.3",
189226
"benchmark": "^2.1.4"
227+
},
228+
"react-native": {
229+
"#util/as-uint8array": "./dist/src/util/as-uint8array.js",
230+
"#alloc": "./dist/src/alloc.js",
231+
"#compare": "./dist/src/compare.js",
232+
"#concat": "./dist/src/concat.js",
233+
"#from-string": "./dist/src/from-string.js",
234+
"#to-string": "./dist/src/to-string.js"
190235
}
191236
}

src/alloc.node.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Buffer } from 'node:buffer'
2+
import { asUint8Array } from '#util/as-uint8array'
3+
4+
/**
5+
* Returns a `Uint8Array` of the requested size. Referenced memory will
6+
* be initialized to 0.
7+
*/
8+
export function alloc (size: number = 0): Uint8Array {
9+
return asUint8Array(Buffer.alloc(size))
10+
}
11+
12+
/**
13+
* Where possible returns a Uint8Array of the requested size that references
14+
* uninitialized memory. Only use if you are certain you will immediately
15+
* overwrite every value in the returned `Uint8Array`.
16+
*/
17+
export function allocUnsafe (size: number = 0): Uint8Array {
18+
return asUint8Array(Buffer.allocUnsafe(size))
19+
}

src/alloc.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import { asUint8Array } from './util/as-uint8array.js'
2-
31
/**
42
* Returns a `Uint8Array` of the requested size. Referenced memory will
53
* be initialized to 0.
64
*/
75
export function alloc (size: number = 0): Uint8Array {
8-
if (globalThis.Buffer?.alloc != null) {
9-
return asUint8Array(globalThis.Buffer.alloc(size))
10-
}
11-
126
return new Uint8Array(size)
137
}
148

@@ -18,9 +12,5 @@ export function alloc (size: number = 0): Uint8Array {
1812
* overwrite every value in the returned `Uint8Array`.
1913
*/
2014
export function allocUnsafe (size: number = 0): Uint8Array {
21-
if (globalThis.Buffer?.allocUnsafe != null) {
22-
return asUint8Array(globalThis.Buffer.allocUnsafe(size))
23-
}
24-
2515
return new Uint8Array(size)
2616
}

src/compare.node.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Buffer } from 'node:buffer'
2+
3+
/**
4+
* Can be used with Array.sort to sort and array with Uint8Array entries
5+
*/
6+
export function compare (a: Uint8Array, b: Uint8Array): number {
7+
return Buffer.compare(a, b)
8+
}

src/compare.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
* Can be used with Array.sort to sort and array with Uint8Array entries
33
*/
44
export function compare (a: Uint8Array, b: Uint8Array): number {
5-
if (globalThis.Buffer != null) {
6-
return globalThis.Buffer.compare(a, b)
7-
}
8-
95
for (let i = 0; i < a.byteLength; i++) {
106
if (a[i] < b[i]) {
117
return -1

0 commit comments

Comments
 (0)