Skip to content

Commit 4b73e78

Browse files
Merge pull request #40 from atlowChemi/fix-Uint8Array-is-undefined
patch `buffer` to enforce global `Uint8Array` is used
2 parents bb7ab7c + 5884870 commit 4b73e78

File tree

3 files changed

+186
-3
lines changed

3 files changed

+186
-3
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,10 @@
8585
},
8686
"workspaces": [
8787
"."
88-
]
88+
],
89+
"pnpm": {
90+
"patchedDependencies": {
91+
92+
}
93+
}
8994
}

patches/[email protected]

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
diff --git a/index.js b/index.js
2+
index 7a0e9c2a123bc9d26c20bb3de4a3c4e49b24ee40..c9af1ef3ebdab802bd32609bfb87a2545f4d54ec 100644
3+
--- a/index.js
4+
+++ b/index.js
5+
@@ -21,6 +21,7 @@ exports.INSPECT_MAX_BYTES = 50
6+
7+
const K_MAX_LENGTH = 0x7fffffff
8+
exports.kMaxLength = K_MAX_LENGTH
9+
+const { Uint8Array: GlobalUint8Array, ArrayBuffer: GlobalArrayBuffer, SharedArrayBuffer: GlobalSharedArrayBuffer } = globalThis
10+
11+
/**
12+
* If `Buffer.TYPED_ARRAY_SUPPORT`:
13+
@@ -49,9 +50,9 @@ if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
14+
function typedArraySupport () {
15+
// Can typed array instances can be augmented?
16+
try {
17+
- const arr = new Uint8Array(1)
18+
+ const arr = new GlobalUint8Array(1)
19+
const proto = { foo: function () { return 42 } }
20+
- Object.setPrototypeOf(proto, Uint8Array.prototype)
21+
+ Object.setPrototypeOf(proto, GlobalUint8Array.prototype)
22+
Object.setPrototypeOf(arr, proto)
23+
return arr.foo() === 42
24+
} catch (e) {
25+
@@ -80,7 +81,7 @@ function createBuffer (length) {
26+
throw new RangeError('The value "' + length + '" is invalid for option "size"')
27+
}
28+
// Return an augmented `Uint8Array` instance
29+
- const buf = new Uint8Array(length)
30+
+ const buf = new GlobalUint8Array(length)
31+
Object.setPrototypeOf(buf, Buffer.prototype)
32+
return buf
33+
}
34+
@@ -115,7 +116,7 @@ function from (value, encodingOrOffset, length) {
35+
return fromString(value, encodingOrOffset)
36+
}
37+
38+
- if (ArrayBuffer.isView(value)) {
39+
+ if (GlobalArrayBuffer.isView(value)) {
40+
return fromArrayView(value)
41+
}
42+
43+
@@ -126,14 +127,14 @@ function from (value, encodingOrOffset, length) {
44+
)
45+
}
46+
47+
- if (isInstance(value, ArrayBuffer) ||
48+
- (value && isInstance(value.buffer, ArrayBuffer))) {
49+
+ if (isInstance(value, GlobalArrayBuffer) ||
50+
+ (value && isInstance(value.buffer, GlobalArrayBuffer))) {
51+
return fromArrayBuffer(value, encodingOrOffset, length)
52+
}
53+
54+
- if (typeof SharedArrayBuffer !== 'undefined' &&
55+
- (isInstance(value, SharedArrayBuffer) ||
56+
- (value && isInstance(value.buffer, SharedArrayBuffer)))) {
57+
+ if (typeof GlobalSharedArrayBuffer !== 'undefined' &&
58+
+ (isInstance(value, GlobalSharedArrayBuffer) ||
59+
+ (value && isInstance(value.buffer, GlobalSharedArrayBuffer)))) {
60+
return fromArrayBuffer(value, encodingOrOffset, length)
61+
}
62+
63+
@@ -176,8 +177,8 @@ Buffer.from = function (value, encodingOrOffset, length) {
64+
65+
// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
66+
// https://github.com/feross/buffer/pull/148
67+
-Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype)
68+
-Object.setPrototypeOf(Buffer, Uint8Array)
69+
+Object.setPrototypeOf(Buffer.prototype, GlobalUint8Array.prototype)
70+
+Object.setPrototypeOf(Buffer, GlobalUint8Array)
71+
72+
function assertSize (size) {
73+
if (typeof size !== 'number') {
74+
@@ -263,8 +264,8 @@ function fromArrayLike (array) {
75+
}
76+
77+
function fromArrayView (arrayView) {
78+
- if (isInstance(arrayView, Uint8Array)) {
79+
- const copy = new Uint8Array(arrayView)
80+
+ if (isInstance(arrayView, GlobalUint8Array)) {
81+
+ const copy = new GlobalUint8Array(arrayView)
82+
return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength)
83+
}
84+
return fromArrayLike(arrayView)
85+
@@ -281,11 +282,11 @@ function fromArrayBuffer (array, byteOffset, length) {
86+
87+
let buf
88+
if (byteOffset === undefined && length === undefined) {
89+
- buf = new Uint8Array(array)
90+
+ buf = new GlobalUint8Array(array)
91+
} else if (length === undefined) {
92+
- buf = new Uint8Array(array, byteOffset)
93+
+ buf = new GlobalUint8Array(array, byteOffset)
94+
} else {
95+
- buf = new Uint8Array(array, byteOffset, length)
96+
+ buf = new GlobalUint8Array(array, byteOffset, length)
97+
}
98+
99+
// Return an augmented `Uint8Array` instance
100+
@@ -342,8 +343,8 @@ Buffer.isBuffer = function isBuffer (b) {
101+
}
102+
103+
Buffer.compare = function compare (a, b) {
104+
- if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength)
105+
- if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength)
106+
+ if (isInstance(a, GlobalUint8Array)) a = Buffer.from(a, a.offset, a.byteLength)
107+
+ if (isInstance(b, GlobalUint8Array)) b = Buffer.from(b, b.offset, b.byteLength)
108+
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
109+
throw new TypeError(
110+
'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
111+
@@ -408,12 +409,12 @@ Buffer.concat = function concat (list, length) {
112+
let pos = 0
113+
for (i = 0; i < list.length; ++i) {
114+
let buf = list[i]
115+
- if (isInstance(buf, Uint8Array)) {
116+
+ if (isInstance(buf, GlobalUint8Array)) {
117+
if (pos + buf.length > buffer.length) {
118+
if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf)
119+
buf.copy(buffer, pos)
120+
} else {
121+
- Uint8Array.prototype.set.call(
122+
+ GlobalUint8Array.prototype.set.call(
123+
buffer,
124+
buf,
125+
pos
126+
@@ -433,7 +434,7 @@ function byteLength (string, encoding) {
127+
if (Buffer.isBuffer(string)) {
128+
return string.length
129+
}
130+
- if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
131+
+ if (GlobalArrayBuffer.isView(string) || isInstance(string, GlobalArrayBuffer)) {
132+
return string.byteLength
133+
}
134+
if (typeof string !== 'string') {
135+
@@ -626,7 +627,7 @@ if (customInspectSymbol) {
136+
}
137+
138+
Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
139+
- if (isInstance(target, Uint8Array)) {
140+
+ if (isInstance(target, GlobalUint8Array)) {
141+
target = Buffer.from(target, target.offset, target.byteLength)
142+
}
143+
if (!Buffer.isBuffer(target)) {
144+
@@ -742,11 +743,11 @@ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
145+
return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
146+
} else if (typeof val === 'number') {
147+
val = val & 0xFF // Search for a byte value [0-255]
148+
- if (typeof Uint8Array.prototype.indexOf === 'function') {
149+
+ if (typeof GlobalUint8Array.prototype.indexOf === 'function') {
150+
if (dir) {
151+
- return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
152+
+ return GlobalUint8Array.prototype.indexOf.call(buffer, val, byteOffset)
153+
} else {
154+
- return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
155+
+ return GlobalUint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
156+
}
157+
}
158+
return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
159+
@@ -1714,11 +1715,11 @@ Buffer.prototype.copy = function copy (target, targetStart, start, end) {
160+
161+
const len = end - start
162+
163+
- if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
164+
+ if (this === target && typeof GlobalUint8Array.prototype.copyWithin === 'function') {
165+
// Use built-in when available, missing from IE11
166+
this.copyWithin(targetStart, start, end)
167+
} else {
168+
- Uint8Array.prototype.set.call(
169+
+ GlobalUint8Array.prototype.set.call(
170+
target,
171+
this.subarray(start, end),
172+
targetStart

pnpm-lock.yaml

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)