npm install feross
TODO
MIT. Copyright (c) Feross Aboukhadijeh.
The buffer module from node.js, for the browser.
License: Other
npm install feross
TODO
MIT. Copyright (c) Feross Aboukhadijeh.
As discussed in #24, add tests for if Safari version > X, then assert that we're getting the Uint8Array implementation, etc. for all browsers.
Firefox does not allow augmenting "native" objects (like Uint8Array
instances) with new properties for some unknown (probably silly)
reason.
My chrome updated during the night and now my stuff doesn't work anymore, fun times, I get to debug!
The first thing was actually already fixed by 5f5478a, I just had to update to the latest version. @feross Was that commit intentionally fixing a Chrome bug?
The next problem is that .subarray(start, end)
seems broken when the __proto__
is tampered with.
The following code fixes the problem but I'm not sure that it's the right approach:
diff --git a/index.js b/index.js
index 15d7c50..cbb1516 100644
--- a/index.js
+++ b/index.js
@@ -782,7 +782,9 @@ Buffer.prototype.slice = function slice (start, end) {
var newBuf
if (Buffer.TYPED_ARRAY_SUPPORT) {
+ this.__proto__ = Uint8Array.prototype
newBuf = this.subarray(start, end)
+ this.__proto__ = Buffer.prototype
newBuf.__proto__ = Buffer.prototype
} else {
var sliceLen = end - start
When using https://github.com/crypto-browserify/randombytes on Safari iOS 7 it throws a TypeError.
This works:
var a = new Uint8Array(128)
crypto.getRandomValues(a)
This throws a TypeError with message "Type error"
:
var b = Buffer(128)
crypto.getRandomValues(b)
The second case is how randombytes
implements it, see https://github.com/crypto-browserify/randombytes/blob/master/browser.js#L17-21.
The current implementation works fine on Safari iOS 8.x, so this seems to be a bug in Safari iOS 7.
Just for fun I forked randombytes
and changed it to using Uint8Array and then converting to a Buffer:
mpfluger/randombytes@711ccbc
I'm not sure if that's a workable solution or if there is something in the buffer
project you can do to handle that correctly.
There's a native html5 api for string encoding and decoding. Benchmark it and see if it's faster to use it, when possible.
Hi,
in node doing Buffer.concat(...)
of array of Buffers works but in the browser I get Buffer.concat undefined is not a function
.
The code is like this:
...
var bodyArr = []
bodyArr.push(new Buffer('bla bla'));
bodyArr.push(new Buffer('bla2 bla2'));
var body = Buffer.concat(bodyArr);
...
req.write(body);
req.end();
I use this to create a POST body for http.
What could be the reason of this error?
I did checked that Buffer constructor is present after browserify build. But it looks that .concat
method is not?
Hi,
I tried using specifically the function utf8slice on a byte array but I get a slightly different result than calling new Buffer(arr).toString() from node. I tried finding the utf8slice function in the node source code but didn't have much luck.
Here is the function I used from your source code:
var res = ''
var tmp = ''
var end = arr.length
for (var i = 0; i < end; i++) {
if (arr[i] <= 0x7F) {
res += decodeUtf8Char(tmp) + String.fromCharCode(arr[i])
tmp = ''
} else {
tmp += '%' + arr[i].toString(16)
}
}
var str = res + decodeUtf8Char(tmp)
function decodeUtf8Char(str) {
try {
return decodeURIComponent(str)
} catch (err) {
return String.fromCharCode(0xFFFD) // UTF 8 invalid char
}
}
Here is the array:
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 96, 0, 96, 0, 0, 255, 219, 0, 67, 0, 8, 6, 6, 7, 6, 5, 8, 7, 7, 7, 9, 9, 8, 10, 12, 20, 13, 12, 11, 11, 12, 25, 18, 19, 15, 20, 29, 26, 31, 30, 29, 26, 28, 28, 32, 36, 46, 39, 32, 34, 44, 35, 28, 28, 40, 55, 41, 44, 48, 49, 52, 52, 52, 31, 39, 57, 61, 56, 50, 60, 46, 51, 52, 50, 255, 219, 0, 67, 1, 9, 9, 9, 12, 11, 12, 24, 13, 13, 24, 50, 33, 28, 33, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 255, 192, 0, 17, 8, 0, 102, 0, 242, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, 255, 196, 0, 31, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0, 181, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 255, 196, 0, 31, 1, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0, 181, 17, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119, 0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34, 50, 129, 8, 20, 66, 145, 161, 177, 193, 9, 35, 51, 82, 240, 21, 98, 114, 209, 10, 22, 36, 52, 225, 37, 241, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 130, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242, 243, 244, 245, 246, 247, 248, 249, 250, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, 247, 250, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 42, 148, 58, 149, 141, 197, 228, 246, 112, 94, 219, 203, 117, 6, 60, 232, 18, 85, 103, 143, 61, 55, 40, 57, 31, 141, 93, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 243, 207, 0, 248, 187, 83, 241, 31, 139, 124, 105, 167, 94, 180, 70, 223, 73, 191, 16, 91, 4, 76, 16, 187, 229, 94, 79, 127, 245, 96, 254, 38, 189, 14, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 170, 247, 55, 48, 89, 219, 73, 115, 117, 52, 112, 195, 18, 238, 121, 36, 96, 170, 163, 212, 147, 210, 128, 62, 87, 211, 117, 75, 173, 51, 246, 135, 158, 91, 89, 74, 180, 254, 32, 150, 214, 64, 122, 52, 114, 78, 81, 129, 31, 67, 199, 161, 0, 246, 175, 172, 43, 231, 79, 132, 246, 86, 126, 37, 248, 193, 226, 79, 16, 45, 186, 220, 216, 67, 44, 247, 22, 210, 200, 188, 163, 203, 54, 99, 108, 30, 251, 3, 245, 233, 245, 175, 160, 231, 18, 52, 18, 8, 164, 242, 228, 101, 33, 28, 174, 237, 167, 28, 28, 119, 250, 80, 5, 138, 42, 165, 162, 92, 69, 105, 10, 93, 76, 179, 220, 42, 42, 203, 40, 143, 96, 145, 128, 229, 130, 228, 237, 4, 243, 140, 156, 85, 186, 0, 40, 162, 138, 0, 40, 162, 138, 0, 40, 170, 211, 220, 67, 105, 109, 45, 205, 196, 139, 20, 16, 161, 146, 71, 99, 128, 170, 6, 73, 62, 192, 87, 155, 234, 95, 30, 252, 17, 98, 232, 182, 243, 223, 106, 27, 179, 184, 218, 219, 16, 19, 235, 230, 20, 253, 51, 64, 25, 63, 5, 174, 225, 212, 60, 105, 241, 18, 246, 217, 183, 193, 113, 168, 164, 177, 183, 170, 180, 151, 4, 31, 200, 215, 179, 215, 201, 255, 0, 9, 117, 207, 23, 105, 82, 234, 182, 190, 17, 208, 161, 212, 238, 46, 140, 38, 103, 152, 31, 46, 16, 130, 76, 2, 119, 40, 5, 183, 28, 101, 191, 132, 224, 26, 246, 175, 10, 107, 31, 19, 46, 245, 200, 225, 241, 63, 134, 180, 219, 61, 49, 149, 183, 207, 4, 203, 189, 8, 31, 46, 0, 149, 243, 147, 129, 208, 122, 230, 128, 61, 26, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 43, 230, 31, 11, 199, 227, 239, 24, 105, 250, 198, 165, 166, 120, 195, 84, 26, 222, 157, 114, 187, 244, 183, 153, 227, 70, 13, 146, 48, 75, 132, 7, 42, 227, 97, 92, 124, 189, 121, 160, 15, 166, 93, 214, 36, 46, 236, 21, 84, 101, 153, 142, 0, 30, 166, 188, 35, 197, 26, 237, 199, 197, 255, 0, 22, 218, 248, 71, 195, 151, 51, 199, 161, 66, 75, 223, 222, 170, 157, 146, 129, 200, 36, 119, 80, 70, 23, 61, 88, 231, 176, 53, 207, 106, 126, 60, 248, 145, 227, 86, 159, 194, 54, 182, 150, 223, 104, 230, 59, 175, 236, 193, 146, 235, 247, 72, 121, 119, 178, 5, 57, 228, 130, 7, 108, 246, 174, 131, 64, 180, 248, 191, 225, 143, 13, 38, 135, 163, 248, 79, 76, 182, 137, 3, 98, 224, 205, 19, 76, 89, 142, 75, 146, 102, 218, 91, 234, 184, 192, 3, 28, 80, 5, 255, 0, 131, 145, 89, 120, 127, 196, 191, 17, 45, 214, 68, 182, 176, 176, 189, 72, 149, 166, 147, 11, 28, 105, 37, 194, 140, 177, 246, 3, 147, 93, 126, 139, 241, 107, 195, 58, 255, 0, 139, 7, 135, 244, 215, 187, 150, 225, 247, 136, 167, 242, 128, 138, 82, 170, 88, 237, 57, 207, 69, 60, 144, 7, 21, 226, 250, 63, 129, 188, 73, 175, 252, 66, 215, 124, 39, 170, 235, 107, 103, 53, 202, 46, 163, 170, 180, 11, 189, 102, 108, 171, 129, 180, 109, 4, 134, 159, 253, 208, 115, 140, 224, 87, 75, 174, 71, 225, 191, 135, 63, 27, 124, 46, 193, 87, 78, 210, 109, 52, 182, 50, 56, 70, 115, 185, 190, 208, 187, 155, 0, 179, 18, 72, 25, 231, 242, 20, 1, 239, 146, 203, 29, 188, 77, 44, 210, 44, 113, 160, 203, 59, 176, 1, 71, 169, 38, 184, 125, 47, 226, 199, 134, 117, 159, 25, 31, 13, 89, 75, 59, 221, 23, 100, 142, 227, 98, 249, 18, 178, 130, 72, 86, 221, 147, 208, 224, 227, 7, 177, 228, 87, 207, 223, 20, 252, 108, 254, 44, 241, 101, 217, 177, 212, 174, 46, 52, 56, 202, 125, 146, 54, 220, 137, 194, 0, 205, 176, 227, 146, 219, 185, 35, 56, 165, 208, 124, 37, 170, 120, 123, 197, 30, 6, 212, 181, 40, 146, 8, 245, 93, 78, 7, 182, 140, 182, 95, 98, 203, 17, 220, 195, 176, 33, 198, 57, 207, 92, 129, 64, 30, 219, 241, 223, 88, 187, 210, 190, 29, 108, 180, 144, 198, 111, 175, 35, 182, 145, 215, 33, 130, 109, 119, 56, 35, 212, 160, 31, 66, 106, 166, 187, 227, 185, 60, 51, 240, 159, 195, 113, 218, 59, 220, 107, 250, 174, 153, 109, 21, 178, 110, 221, 32, 102, 137, 65, 148, 247, 56, 39, 143, 86, 35, 222, 184, 15, 139, 58, 246, 177, 227, 91, 205, 98, 43, 91, 53, 139, 69, 240, 173, 201, 138, 225, 252, 223, 153, 228, 119, 242, 131, 21, 207, 63, 50, 182, 48, 56, 4, 228, 243, 93, 23, 193, 159, 11, 220, 107, 215, 22, 254, 52, 215, 85, 38, 75, 104, 18, 199, 75, 137, 211, 133, 16, 170, 160, 144, 118, 227, 105, 3, 253, 173, 199, 131, 138, 0, 185, 240, 33, 181, 65, 171, 248, 198, 203, 88, 187, 158, 123, 203, 91, 152, 99, 155, 205, 152, 201, 137, 1, 153, 95, 146, 78, 121, 81, 207, 176, 175, 109, 175, 31, 248, 65, 255, 0, 37, 15, 226, 111, 253, 133, 71, 254, 141, 184, 172, 143, 137, 190, 55, 190, 241, 70, 177, 23, 129, 188, 31, 114, 175, 36, 187, 197, 236, 203, 42, 162, 182, 209, 184, 160, 114, 70, 2, 133, 98, 199, 191, 79, 80, 64, 58, 143, 132, 94, 57, 186, 241, 222, 139, 170, 38, 174, 99, 146, 242, 218, 227, 14, 137, 16, 84, 242, 100, 31, 32, 247, 229, 100, 31, 64, 51, 93, 99, 88, 120, 119, 193, 186, 109, 254, 179, 107, 164, 233, 246, 9, 4, 13, 36, 207, 107, 109, 28, 76, 202, 163, 59, 114, 160, 103, 167, 79, 90, 241, 159, 128, 23, 31, 217, 222, 55, 241, 38, 129, 20, 144, 221, 66, 208, 249, 130, 234, 34, 118, 191, 147, 38, 192, 87, 253, 150, 18, 147, 248, 10, 191, 241, 175, 197, 55, 186, 174, 160, 254, 12, 209, 188, 169, 97, 134, 213, 175, 181, 7, 71, 228, 121, 74, 242, 24, 219, 176, 1, 84, 55, 185, 42, 61, 136, 5, 255, 0, 217, 199, 76, 72, 60, 47, 171, 234, 185, 97, 37, 213, 226, 193, 130, 56, 219, 26, 2, 8, 252, 101, 97, 248, 87, 171, 77, 175, 105, 118, 250, 237, 190, 139, 45, 236, 73, 169, 92, 198, 210, 195, 108, 79, 204, 234, 51, 146, 63, 35, 249, 31, 67, 92, 159, 193, 63, 249, 36, 58, 31, 253, 188, 127, 232, 249, 43, 135, 109, 126, 223, 95, 253, 168, 116, 150, 181, 120, 228, 130, 198, 222, 75, 69, 150, 54, 220, 28, 136, 38, 118, 63, 131, 59, 47, 252, 6, 128, 61, 151, 88, 215, 116, 191, 15, 217, 139, 189, 86, 246, 43, 75, 118, 145, 98, 18, 74, 120, 44, 122, 15, 208, 254, 70, 181, 43, 193, 63, 104, 191, 17, 219, 181, 166, 155, 225, 168, 128, 121, 196, 191, 109, 159, 158, 99, 1, 89, 16, 126, 59, 152, 254, 3, 214, 189, 191, 79, 190, 135, 83, 211, 45, 53, 11, 99, 186, 11, 168, 82, 104, 207, 170, 176, 4, 126, 134, 128, 41, 107, 190, 33, 210, 252, 55, 167, 127, 104, 107, 23, 169, 107, 108, 28, 70, 29, 129, 57, 99, 208, 0, 1, 36, 240, 122, 14, 198, 175, 217, 94, 218, 234, 86, 80, 222, 89, 207, 28, 246, 211, 32, 120, 229, 141, 178, 172, 15, 112, 107, 196, 117, 155, 91, 255, 0, 140, 95, 16, 245, 29, 21, 223, 236, 90, 15, 135, 100, 146, 25, 74, 182, 93, 230, 59, 144, 54, 59, 146, 200, 113, 232, 160, 247, 53, 119, 225, 86, 175, 168, 120, 91, 197, 55, 95, 14, 117, 195, 33, 16, 25, 31, 74, 145, 161, 216, 36, 64, 206, 204, 71, 114, 24, 101, 135, 92, 97, 134, 123, 80, 7, 181, 209, 69, 120, 86, 187, 123, 226, 79, 31, 252, 64, 214, 116, 237, 15, 196, 23, 58, 6, 143, 160, 70, 209, 220, 92, 71, 51, 160, 105, 65, 108, 150, 218, 71, 86, 86, 28, 156, 0, 132, 245, 224, 128, 123, 173, 21, 228, 191, 3, 181, 239, 18, 120, 139, 64, 191, 155, 90, 187, 251, 85, 173, 188, 169, 5, 164, 239, 131, 35, 16, 9, 112, 199, 171, 1, 148, 193, 60, 242, 121, 175, 90, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 14, 103, 197, 158, 51, 209, 188, 23, 167, 165, 222, 179, 59, 168, 148, 149, 134, 40, 163, 220, 242, 17, 201, 0, 116, 252, 73, 2, 188, 1, 252, 61, 227, 111, 26, 93, 235, 158, 37, 240, 182, 145, 45, 150, 151, 174, 74, 209, 201, 110, 46, 82, 51, 50, 100, 18, 199, 121, 25, 5, 134, 73, 29, 216, 129, 198, 107, 233, 107, 189, 47, 79, 212, 140, 38, 250, 194, 214, 232, 192, 251, 226, 51, 194, 175, 229, 183, 170, 228, 112, 125, 197, 95, 160, 15, 2, 240, 174, 157, 241, 91, 193, 186, 88, 177, 210, 60, 15, 162, 162, 177, 13, 44, 210, 76, 141, 36, 173, 234, 199, 237, 31, 160, 0, 14, 194, 183, 127, 225, 33, 248, 229, 255, 0, 66, 110, 135, 255, 0, 127, 151, 255, 0, 146, 43, 216, 40, 160, 15, 33, 248, 125, 225, 255, 0, 25, 127, 194, 205, 213, 252, 91, 226, 157, 38, 218, 192, 222, 216, 8, 49, 4, 200, 202, 92, 24, 128, 192, 14, 196, 124, 177, 243, 147, 214, 189, 39, 82, 240, 246, 137, 172, 60, 111, 169, 104, 250, 125, 243, 198, 54, 163, 93, 91, 36, 165, 71, 160, 44, 14, 43, 86, 138, 0, 228, 225, 248, 119, 224, 251, 125, 100, 106, 144, 248, 114, 198, 59, 160, 0, 93, 177, 98, 53, 199, 66, 35, 251, 128, 251, 129, 154, 243, 111, 218, 15, 80, 155, 73, 213, 188, 23, 169, 91, 237, 243, 237, 39, 158, 120, 247, 12, 141, 200, 208, 48, 207, 226, 43, 221, 107, 231, 47, 137, 190, 33, 211, 60, 105, 241, 83, 195, 62, 30, 136, 173, 198, 159, 105, 123, 29, 189, 196, 136, 120, 118, 150, 84, 89, 20, 31, 64, 20, 12, 250, 230, 128, 52, 124, 23, 240, 250, 109, 123, 224, 198, 172, 240, 222, 183, 246, 167, 137, 93, 101, 121, 46, 179, 180, 121, 51, 146, 185, 192, 39, 230, 195, 18, 220, 253, 225, 233, 207, 173, 248, 47, 195, 241, 248, 91, 193, 250, 102, 140, 175, 230, 27, 88, 177, 35, 231, 33, 164, 98, 89, 200, 246, 44, 205, 143, 108, 86, 204, 16, 197, 109, 4, 112, 195, 26, 199, 28, 106, 21, 17, 87, 1, 64, 224, 0, 59, 10, 177, 64, 30, 39, 47, 194, 175, 26, 69, 226, 205, 122, 235, 73, 241, 76, 26, 94, 155, 172, 94, 181, 204, 207, 110, 95, 237, 0, 110, 118, 81, 128, 7, 77, 236, 56, 113, 154, 196, 147, 224, 15, 136, 236, 239, 230, 143, 75, 215, 172, 26, 206, 226, 17, 12, 243, 79, 27, 44, 219, 73, 5, 192, 80, 172, 6, 72, 236, 192, 144, 72, 39, 4, 231, 232, 122, 40, 3, 196, 46, 190, 14, 107, 122, 6, 179, 166, 106, 126, 3, 212, 236, 180, 249, 224, 179, 16, 79, 45, 211, 57, 51, 75, 200, 105, 54, 149, 117, 195, 2, 62, 94, 128, 129, 138, 191, 164, 124, 62, 190, 240, 183, 195, 223, 25, 92, 106, 207, 5, 246, 189, 169, 218, 93, 180, 151, 22, 251, 156, 178, 152, 155, 10, 9, 80, 114, 88, 146, 112, 57, 200, 244, 175, 96, 162, 128, 62, 102, 240, 54, 161, 226, 239, 22, 248, 82, 195, 193, 126, 27, 63, 217, 214, 22, 145, 202, 53, 13, 65, 135, 93, 242, 59, 109, 7, 168, 225, 128, 192, 228, 243, 200, 21, 216, 207, 240, 22, 11, 89, 116, 251, 157, 3, 196, 151, 122, 85, 229, 172, 6, 57, 46, 99, 136, 179, 202, 231, 118, 92, 21, 117, 41, 144, 197, 112, 51, 192, 3, 215, 62, 209, 69, 0, 120, 31, 140, 126, 24, 105, 94, 11, 248, 93, 175, 95, 188, 243, 234, 122, 196, 203, 8, 146, 250, 224, 114, 51, 113, 30, 118, 47, 59, 115, 220, 146, 79, 191, 56, 173, 93, 43, 227, 39, 133, 52, 79, 135, 90, 117, 188, 55, 198, 231, 86, 179, 210, 225, 136, 90, 136, 37, 25, 153, 99, 85, 218, 88, 174, 220, 110, 28, 156, 244, 233, 154, 245, 219, 171, 75, 107, 235, 73, 45, 174, 173, 226, 184, 183, 148, 109, 146, 41, 144, 58, 56, 244, 32, 240, 69, 100, 197, 224, 207, 11, 65, 34, 75, 15, 134, 180, 104, 228, 67, 149, 116, 176, 136, 21, 62, 160, 133, 226, 128, 60, 255, 0, 225, 189, 180, 158, 18, 240, 182, 189, 227, 191, 19, 220, 172, 45, 172, 50, 223, 205, 26, 166, 54, 46, 231, 43, 199, 247, 152, 203, 192, 247, 90, 206, 240, 197, 197, 215, 138, 252, 119, 31, 196, 221, 126, 75, 125, 27, 69, 178, 141, 173, 172, 5, 204, 138, 158, 106, 144, 234, 50, 204, 64, 255, 0, 150, 140, 115, 220, 240, 58, 26, 245, 111, 18, 120, 126, 211, 197, 58, 5, 214, 139, 126, 210, 139, 91, 165, 80, 230, 22, 10, 227, 107, 6, 4, 18, 8, 200, 42, 59, 87, 1, 109, 251, 62, 120, 58, 11, 152, 228, 150, 231, 87, 184, 68, 108, 152, 101, 157, 2, 184, 244, 59, 80, 54, 62, 132, 80, 6, 175, 138, 62, 47, 248, 115, 67, 138, 40, 244, 201, 211, 92, 191, 159, 136, 109, 180, 249, 86, 65, 158, 219, 153, 115, 143, 160, 201, 246, 175, 21, 240, 238, 149, 107, 226, 223, 21, 120, 145, 188, 77, 226, 56, 188, 51, 109, 45, 247, 159, 123, 97, 44, 226, 38, 158, 67, 36, 132, 160, 222, 192, 101, 14, 225, 146, 14, 55, 14, 57, 175, 160, 180, 31, 135, 62, 19, 240, 198, 161, 246, 237, 31, 70, 75, 123, 173, 165, 4, 173, 44, 146, 21, 7, 174, 55, 177, 199, 212, 85, 61, 71, 225, 63, 130, 117, 109, 74, 227, 80, 189, 208, 196, 151, 23, 50, 25, 37, 117, 185, 153, 55, 49, 228, 156, 43, 129, 201, 244, 20, 1, 147, 63, 143, 124, 7, 240, 243, 195, 86, 246, 58, 45, 229, 189, 236, 113, 130, 176, 90, 105, 243, 172, 238, 204, 78, 73, 102, 4, 129, 146, 122, 147, 244, 7, 165, 114, 90, 134, 129, 227, 175, 140, 115, 91, 75, 170, 91, 71, 225, 237, 14, 38, 47, 12, 114, 134, 105, 27, 56, 249, 138, 100, 22, 62, 132, 237, 24, 39, 21, 233, 154, 55, 195, 63, 7, 120, 119, 81, 91, 253, 47, 67, 138, 43, 165, 31, 36, 143, 44, 146, 148, 247, 93, 236, 112, 125, 199, 53, 216, 80, 7, 15, 240, 231, 225, 250, 120, 3, 78, 187, 182, 93, 78, 107, 249, 46, 164, 14, 238, 201, 229, 162, 227, 56, 10, 153, 56, 60, 156, 156, 243, 199, 165, 119, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 30, 65, 241, 63, 226, 59, 219, 221, 79, 224, 141, 2, 9, 229, 215, 239, 10, 91, 52, 129, 112, 145, 9, 87, 248, 78, 115, 191, 12, 189, 176, 51, 156, 228, 87, 39, 174, 120, 50, 219, 193, 26, 223, 194, 253, 57, 22, 41, 47, 31, 84, 47, 119, 114, 137, 131, 43, 249, 182, 255, 0, 142, 20, 28, 15, 166, 122, 147, 93, 63, 195, 40, 142, 163, 241, 115, 226, 6, 177, 117, 35, 73, 117, 107, 117, 246, 40, 201, 60, 121, 70, 71, 0, 99, 216, 65, 24, 31, 67, 79, 248, 247, 157, 59, 77, 240, 207, 137, 96, 63, 233, 90, 102, 168, 190, 82, 158, 135, 112, 243, 57, 252, 97, 95, 206, 128, 61, 134, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 15, 28, 240, 156, 98, 195, 246, 142, 241, 133, 141, 169, 49, 218, 203, 100, 183, 50, 70, 15, 13, 35, 121, 12, 91, 235, 153, 31, 254, 250, 52, 159, 180, 76, 139, 255, 0, 8, 54, 155, 6, 225, 231, 62, 168, 140, 137, 252, 76, 4, 82, 2, 64, 250, 176, 252, 197, 63, 198, 126, 26, 241, 141, 135, 197, 11, 127, 23, 120, 51, 75, 183, 187, 105, 44, 188, 171, 165, 154, 117, 85, 119, 229, 126, 96, 93, 73, 27, 124, 178, 48, 122, 167, 231, 14, 135, 224, 207, 23, 248, 171, 199, 176, 248, 151, 199, 214, 240, 90, 199, 96, 1, 181, 176, 138, 69, 120, 203, 129, 193, 1, 93, 176, 1, 249, 142, 78, 73, 192, 233, 64, 30, 207, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 7, 255, 217]
Can anyone help? Thanks
L86 throws for me with TypeError: buf.set is not a function
in [email protected] and nightly
as far as I can see the array doesnt get augmented since L24-L31 throws, so Buffer._useTypedArrays
is false
. in L84 it checks against typeof Uint8Array === 'function' && subject instanceof Uint8Array
although buf
is actually not a uint8.
so, i think we should at least check against Buffer._useTypedArrays
at L84 and not against typeof && instanceof
. but using the object-fallback sucks anyway.
Follow-up to #69 and #70; looks like chunking up the array is actually faster: http://jsperf.com/string-fromcharcode-apply-vs-string-fromcharcode-using-/3
I've noticed that partial bytes of UCS-2 symbols are written, while they shouldn't be according to the Node.js documentation.
Here's the code taken from Node.js test:
var f = new Buffer([0, 0, 0, 0, 0]);
assert.equal(f.length, 5);
var size = f.write('あいうえお', 'ucs2');
console.error('bytes written to buffer: %d (should be 4)', size);
assert.equal(size, 4);
assert.deepEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00]));
Why is Buffer.isBuffer solved this way?
Buffer.isBuffer = function (b) {
return !!(b !== null && b !== undefined && b._isBuffer)
}
Why not just return b instanceof Buffer?
Why do you need to dispatch the isBuffer property to the UInt8Array anyways?
I ran into an issue, documented in nathanpeck/s3-upload-stream#20, where a Buffer that had been sliced and then passed onto the browser's native XHR object wasn't having its sliced view respected.
It looks like you might have already addressed and documented this in 500be9e? If so, it might be worth rephrasing with less low-level terminology or something? I think I understand what "parent tracking" means, and that this is the issue I'm seeing, but I'm not sure.
> var buf = new Buffer([1,2,3])
> buf.toJSON()
[ 1, 2, 3 ]
With buffer
in the browser I get {type: 'Buffer', data: [...]}
. Obviously you're doing this intentionally, since the code does exactly this. However, I wonder why you've chosen to do it this way? At the very least, documenting the difference will save users problems when it works differently than they expect.
Waiting for this PR to published on npm: beatgammit/base64-js#3
In this place:
347: var byte = parseInt(string.substr(i * 2, 2), 16)
348: if (isNaN(byte)) throw new Error('Invalid hex string')
349: buf[offset + i] = byte
Lines 347 to 349 in 3a22a49
used variable with name "byte", but such names are only allowed in ecmascript 5. I'm use RhinoScriptEngine in java and Rhino not complete support ecmascript 5 now.
Please change the code for this example:
347: var byte2 = parseInt(string.substr(i * 2, 2), 16)
348: if (isNaN(byte2)) throw new Error('Invalid hex string')
349: buf[offset + i] = byte2
Thank you.
When a base64 string contains new lines the node buffer module will behave correctly,
however the browserified version will garble the data.
Reproduce:
var Buffer = require('./node_modules/buffer').Buffer;
var data = [
"LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK",
"LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\nICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt",
"LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoKICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt"
];
data.forEach(function(dat) {
var d = new Buffer(dat, 'base64');
console.log(d.toString('utf8'));
});
Output will be:
---
title: Three dashes marks the spot
tags:
---
title: Three dashes marks the spot
tags:
�(�(�)�
---
title: Three dashes marks the spot
tags:
- yaml
- front-matter
- dashes
expaned-
Github seems to transfer it's content
data including new lines, e.g.:
and is why I encounter this.
Node removed the buffer.toArrayBuffer() API sometime during node 0.11. Users who rely on it will have code that doesn't work in node.js, which is unfortunate.
Let's remove buffer.toArrayBuffer() so we have parity with node.js.
Merged @jessetane's PR (beatgammit/base64-js#10) so we can remove the special handling code in buffer for this.
The following will work:
var number = "1003";
var nodeBuf = new Buffer(64);
// Succeeds
nodeBuf.writeUInt16LE(number, 0);
console.log("Written to buffer: " + nodeBuf.readUInt16LE(0));
The following will not:
var NEWBuffer = require('buffer/').Buffer;
var newBuffer = new NEWBuffer(64);
// Fails
newBuffer.writeUInt16LE(number, 0);
Node's internal buffer handles this case and parses the integer, but this buffer implementation fails in Node and throws this error:
$ node bufferDebug.js
d:\********\node_modules\buffer\index.js:1168
if (!test) throw new Error(message || 'Failed assertion')
^
Error: cannot write a non-number as a number
at assert (d:\********\node_modules\buffer\index.js:1168:20)
at verifuint (d:\********\node_modules\buffer\index.js:1148:3)
at writeUInt16 (d:\********\node_modules\buffer\index.js:744:5)
at Uint8Array.Buffer.writeUInt16LE (d:\********\node_modules\buffer\index.js:760:10)
at Object.<anonymous> (d:\********\server\utilities\bufferDebug.js:19:11
)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
my code is like this :
var fs = require('fs');
var B = require('buffer/').Buffer;
var buffin = fs.readFileSync('excel.xls');
var b64 = buffin.toString('base64');
var bf = new B(b64,'base64');
fs.writeFile('excelo.xls', bf, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
the excelo.xls file is cannot be open, because corrupted file.
the filesize is also different with the original file.
the original file is 1,182 kb but the result file is 1,416 kb.
kindly help, thanks.
TextEncoder and TextDecoder are already supported in Chrome and Firefox (not sure about MSIE and Safari).
var buf = new TextEncoder('utf-16le').encode("I ½ ♥ 💩");
var str = new TextDecoder('utf-16le').decode(buf);
If you use them you could drop a fair amount of code and only provide your UTF-8, UTF-16, etc conversions as a separate Polyfill module when the native TextEncoder and TextDecoder don't exist.
I haven't actually tested your UTF-8 converter, but if you get the same values as you get in node, then it's probably the best that there is. The full TextEncoder Polyfill seems to have much more code than I would intuitively expect and Mozilla's UTF-8 converter is actually incorrect (well it provides a correct encoding, but it's not the encoding that node and the W3C utilities use).
Anyway, then you could drop a bit of code.
I'm not sure how this compares in terms of performance, but if you wanted the polyfill code to be even smaller you can actually do this:
function utf8ToBinaryString() {
var escstr = encodeURIComponent(str);
// replaces any uri escape sequence, such as %0A, with binary escape, such as 0x0A
var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
});
return binstr;
}
function utf8ToBuffer(str) {
var binstr = utf8ToBinaryString(str);
var buf = new Uint8Array(binstr.length);
Array.prototype.forEach.call(binstr, function (ch, i) {
buf[i] = ch.charCodeAt(0);
});
return buf;
}
function utf8ToBase64(str) {
var binstr = utf8ToBinaryString(str);
return btoa(binstr);
}
I don't know if that still works for utf-16, but I have a hunch that it would.
I know I could have just used your module here, but I've been enjoying the headache of learning the hacky, non-hacky, and best-possible ways of doing this stuff in the browser.
node-util
(will fix this for most people)core-util-is
to use Buffer.isBuffer
instead of instanceof Buffer
(also used quite a bit)I recently tried to use telegram.link library with browsefiry and noticed that all the requests fail with Uncaught TypeError: item.copy is not a function (index.js:328)
.
I've tried a few workarounds and found out that if I add:
if (list.length === 1) {
return list[0];
}
In Buffer.concat
function, around line 324
, then all the requests pass fine.
I'm not (yet) pro in binary conversion yet so I'd like to ask a question:
What might be wrong with Buffer object passed to Buffer.concat
so item.copy fails? You might assume that it doesn't pass Buffer object but actually it does, I've checked.
And will my workaround break something in the future?
I can provide a sample repo as a playground if needed ;-)
I've made the change in my fork, feel free to check it
Buffer 3.5.0 breaks in Chrome (45.0.2454.85 (Official Build) (64-bit)
)
What I'm getting is
Uncaught TypeError: Cannot set property length of [object Object] which has only a getter
It happens when buffer tries to set .prototype.length
to undefined
, here: https://github.com/feross/buffer/blob/master/index.js#L372
I discovered this while I was rolling out our app's new version. Later I'm gonna try to put together a small demo.
(I'm using browserify 11.11.0 which uses [email protected]
)
Firefox is fixing their Uint8Array
implementation (see bug in https://bugzilla.mozilla.org/show_bug.cgi?id=695438). Once that lands, let's confirm that buffer is using it and run the benchmarks.
Not sure if this is a bug, but the get
method of Buffer
seems to be missing in native-buffer-browserify:
b = new Buffer(1)
b.get(0) // Node v0.10.21 returns 0, browserify throws: TypeError: Object [object Uint8Array] has no method 'get'
applies to both data view and object-backed buffers.
http://nodejs.org/api/buffer.html does not document get
, which is why I am unsure whether this is a legitimate issue. But I hit this problem while attempting to browserify kriskowal/zip#12 - fixed by changing to readUInt8
In PouchDB we use large binary strings when we store binary data in localstorage-down
and level-js
, and our tests started failing recently due to buffer 3.4.1 and 3.4.2 (pouchdb/pouchdb#4164, see discussion over there). In short, 3.4.1 had a perf regression which appears fixed in 3.4.2, but 3.4.2 now has a "call stack size exceeded" error when I test in Chrome 44 (on an admittedly slow laptop, so it may be hard to reproduce on a faster one).
Glancing at the code, I think a reasonable fix would be to break up the array into decent-size chunks and then concat the string together, but I admit I'm not super familiar with the codebase.
Advantages:
Still broken in IE6-8.
typedarray
uses Object.defineProperty
which is not supported in IE6-8.
Quoting from the source (https://github.com/feross/buffer/blob/master/index.js#L1032), this is the given implementation:
// slice(start, end)
function clamp (index, len, defaultValue) {
if (typeof index !== 'number') return defaultValue
index = ~~index; // Coerce to integer.
if (index >= len) return len
if (index >= 0) return index
index += len
if (index >= 0) return index
return 0
}
This whole method can be realized in a more readable way, I haven't tested performance, but it should be better as multiple if conditions are saved:
Buffer.prototype.slice = function(start, end) {
var length = this.length;
start = typeof start === 'number' ? (start | 0) : 0;
end = typeof end === 'number' ? (end | 0) : length;
start = Math.min(start, length);
end = Math.min(end, length);
// ... etc. etc.
};
If the Math.min calls are too heavy (which I don't expect, because they are faster than the previous implementation anyways), we can switch to a a ^ ((a ^ b) & -(a < b)) or a turnery, but for now I think that would be overkill as embedded Math.min calls get optimized anyways.
Should we expose the augment
function somehow to allow for converting an Uint8Array to a Buffer without having to do a copy (which I believe new Buffer(uint8array)
does now) ?
I saw a potential way to get instanceof Buffer
working in a module by @dcodeIO: https://github.com/dcodeIO/node-BufferView/blob/master/BufferView.js
// Extend DataView and clear all instance methods to be sure
BufferView.prototype = Object.create(DataView.prototype);
(function() { for (var i in BufferView.prototype) delete BufferView.prototype[i]; })();
See for description of the problem: toots/buffer-browserify#19
From the node docs:
buf.slice([start], [end])#
start Number, Optional, Default: 0
end Number, Optional, Default: buffer.length
Returns a new buffer which references the same memory as the old, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.
Modifying the new buffer slice will modify memory in the original buffer!
I'm working on a cordova project which is client-side javascript. I want to use buffer to decompress my string like this:
var s = Buffer('737AB86B719E82CBC3DDDDC9190ABE0F77CFCE54303230300200', 'hex').toString('binary');
Please help me to use your library in client javascript. Thanks you!
Would it be reasonable to support toArrayBuffer
on browsers where Buffer
is Object
-backed, but typed arrays are available? (= Firefox)
Currently toArrayBuffer
is only available where Buffer
is an augmented Uint8Array
:
buf = new Buffer(1)
buf.toArrayBuffer // undefined on Firefox, new Buffer(this).buffer on Chrome
added in augment():
arr.toArrayBuffer = BufferToArrayBuffer
ArrayBuffer
s are required by some browser APIs (at least the W3C File API), but toArrayBuffer
cannot be relied on for Firefox compatibility. Workaround:
arrayBuffer = new Uint8Array(buf.length);
for (var i = 0; i < buf.length; i += 1) arrayBuffer[i] = buf[i]
(though in my specific case, I ended up porting to use bops
instead so the data would be backed by Uint8Array
even on Firefox)
references:
chrisdickinson/bops#7 (comment)
kriskowal/zip#13
Tracking changes in node master: nodejs/node-v0.x-archive@2cae44f
Currently, we use Uint8Array in Chrome, IE10, Opera, and Safari. We use a Proxy + Uint8Array in Firefox. And we use fake Uint8Array from typedarray
in IE6-9 and Firefox 3.
There are some problems that have become clear with this approach.
Proxy
performance is terribleProxy
performance is terribleOnce Uint8Array
instances are augmentable then performance should be awesome in Firefox. See bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=695438
Using an ES6 Proxy (supported since Firefox 18) to wrap the Uint8Array instance (to simulate augmenting the instance) causes an unacceptable performance hit. Just using the typedarray
implementation is also quite slow.
But the old implementation of buffer-browserify
had reasonable performance in Firefox, so let's use their Object approach as the fallback behavior for all Firefoxes, and IE6-9.
On the bracket-notation perf test:
Proxy and typedarray implementations get "56,784 ops/sec ±1.68% (65 runs sampled)"
OldBuffer gets "11,787,803 ops/sec ±0.95% (66 runs sampled)"
typedarray
uses Object.defineProperty which is not exist in IE6-8. It does exist in IE9 though. So this was a good stop-gap solution for the time, but it needs to be replaced because it can't work.
typedarray
was really big and had a lot of code dedicated to Uint8Array/ArrayBuffer details which aren't important and just bloat the bundle size. The Proxy
implementation also added some cruft.
$ browserify -r native-buffer-browserify | wc -c
57387
$ browserify -r buffer-browserify | wc -c
64427
Eliminate the Proxy and typedarray code. Fallback to using an Object when Uint8Array is not available.
Tests are failing because Uint8Arrays lack the subarray
method in Chrome 9-10. Odd.
Presently, this module does require('assert')
which results in a rather large bundle because of downstream dependencies from assert:
$ browserify -r native-buffer-browserify | wc -c
66737
commenting out the assert, this module has a much smaller footprint:
$ browserify -r native-buffer-browserify | wc -c
35575
which is only 3k more than the index.js itself.
It seems like the asserts could be replaced with throw new Error()
for a huge savings in bytes?
It would be handy if the release download included a build version of the code that could be used directly in the browser for the occasional proof of concept html page put together on a tablet without a node build environment
I'm looking into a bug (browserify/createHash#8) effecting creating hashes in Android 4.3.1 and earlier. Some preliminary sauce lab's testing suggests it might effect ciphers too (though could have the same root cause in the hashes if it doesn't effect createCipheriv), that being said it could be a bug in buffer and I noticed that this repo doesn't test against android older than 5.0 and since I couldn't find any mention of it in the issues I thought I'd check to make sure there arn't any known issues.
slicing buffers fails in Edge 25.10586.0.0
it boils down to this:
var buf = new Buffer(100);
var arr = new Uint8Array(100);
buf instanceof Uint8Array; // true
Uint8Array.prototype.subarray.call(arr, 0, 0).length; // 0
Uint8Array.prototype.subarray.call(buf, 0, 0).length; // 100 !
Would you say this is an issue with Edge or with this lib?
Otherwise, it's unclear what will break those packages. This is extra important now that is-buffer
is also used in browserify core.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.