Skip to content

Commit efb08bc

Browse files
committed
wip
1 parent 3ea046a commit efb08bc

File tree

5 files changed

+57
-69
lines changed

5 files changed

+57
-69
lines changed

.github/workflows/node-aught.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

.github/workflows/node-pretest.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

.github/workflows/node-tens.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

.github/workflows/node-twenties.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

index.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ var isView = ArrayBuffer.isView || function isView(obj) {
1616
var useUint8Array = typeof Uint8Array !== 'undefined';
1717
var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
1818
&& typeof Uint8Array !== 'undefined';
19-
var useFromArrayBuffer = useArrayBuffer && (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);
19+
// Check if we're on a big-endian system
20+
var isBigEndian = (function() {
21+
var buffer = new ArrayBuffer(2);
22+
new DataView(buffer).setInt16(0, 256, true); // little-endian
23+
return new Int16Array(buffer)[0] !== 256;
24+
})();
25+
26+
var useFromArrayBuffer = useArrayBuffer && (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT) && !isBigEndian;
2027

2128
module.exports = function toBuffer(data, encoding) {
2229
if (Buffer.isBuffer(data)) {
@@ -54,7 +61,55 @@ module.exports = function toBuffer(data, encoding) {
5461
}
5562

5663
// Convert to Uint8Array bytes and then to Buffer
57-
var uint8 = data instanceof Uint8Array ? data : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
64+
var uint8;
65+
if (data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
66+
// These are already byte arrays, no endianness issues
67+
uint8 = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
68+
} else {
69+
// For multi-byte TypedArrays, ensure consistent little-endian byte order
70+
// Create a new buffer and use DataView to read elements in little-endian format
71+
var elemSize = data.BYTES_PER_ELEMENT;
72+
var elemCount = data.length;
73+
uint8 = new Uint8Array(data.byteLength);
74+
var outputView = new DataView(uint8.buffer);
75+
var inputView = new DataView(data.buffer, data.byteOffset, data.byteLength);
76+
77+
// Copy each element, reading in native order and writing in little-endian
78+
for (var j = 0; j < elemCount; j++) {
79+
var offset = j * elemSize;
80+
var value;
81+
82+
// Read the value in platform's native endianness, write in little-endian
83+
if (elemSize === 1) {
84+
// 8-bit values have no endianness
85+
value = inputView.getUint8(offset);
86+
outputView.setUint8(offset, value);
87+
} else if (elemSize === 2) {
88+
// 16-bit values: read native, write little-endian
89+
value = inputView.getUint16(offset); // native endianness
90+
outputView.setUint16(offset, value, true); // true = little-endian
91+
} else if (elemSize === 4) {
92+
// 32-bit values - check if it's float or int by examining the original array
93+
if (data instanceof Float32Array) {
94+
value = inputView.getFloat32(offset); // native endianness
95+
outputView.setFloat32(offset, value, true);
96+
} else {
97+
value = inputView.getUint32(offset); // native endianness
98+
outputView.setUint32(offset, value, true);
99+
}
100+
} else if (elemSize === 8) {
101+
// 64-bit values
102+
if (data instanceof Float64Array) {
103+
value = inputView.getFloat64(offset); // native endianness
104+
outputView.setFloat64(offset, value, true);
105+
} else {
106+
value = inputView.getBigUint64(offset); // native endianness
107+
outputView.setBigUint64(offset, value, true);
108+
}
109+
}
110+
}
111+
}
112+
58113
var result = Buffer.from(uint8);
59114

60115
/*

0 commit comments

Comments
 (0)