| 79 | * @return {string} a binary string i.e. one byte per character |
| 80 | */ |
| 81 | export function buffer2string(buf, offset = 0, length = 1e99) { |
| 82 | // The max number of arguments varies between JS engines but it's >32k so we're safe |
| 83 | const sliceSize = 8192; |
| 84 | const slices = []; |
| 85 | const arrayLen = buf.length; // present on Uint8Array/Array |
| 86 | const end = Math.min(arrayLen || buf.byteLength, offset + length); |
| 87 | const needsSlicing = arrayLen == null || offset || end > sliceSize; |
| 88 | for (; offset < end; offset += sliceSize) { |
| 89 | slices.push(String.fromCharCode.apply(null, |
| 90 | needsSlicing |
| 91 | ? new Uint8Array(buf, offset, Math.min(sliceSize, end - offset)) |
| 92 | : buf)); |
| 93 | } |
| 94 | return slices.join(''); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Faster than buffer2string+btoa: 2x in Chrome, 10x in FF |