* Class: Buffer * ============= * * The Buffer constructor returns instances of `Uint8Array` that are augmented * with function properties for all the node `Buffer` API functions. We use * `Uint8Array` so that square bracket notation works as expected -- it returns * a single octet. * * By a
(subject, encoding, noZero)
| 979 | * prototype. |
| 980 | */ |
| 981 | function Buffer (subject, encoding, noZero) { |
| 982 | if (!(this instanceof Buffer)) |
| 983 | return new Buffer(subject, encoding, noZero) |
| 984 | |
| 985 | var type = typeof subject |
| 986 | |
| 987 | // Workaround: node's base64 implementation allows for non-padded strings |
| 988 | // while base64-js does not. |
| 989 | if (encoding === 'base64' && type === 'string') { |
| 990 | subject = stringtrim(subject) |
| 991 | while (subject.length % 4 !== 0) { |
| 992 | subject = subject + '=' |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | // Find the length |
| 997 | var length |
| 998 | if (type === 'number') |
| 999 | length = coerce(subject) |
| 1000 | else if (type === 'string') |
| 1001 | length = Buffer.byteLength(subject, encoding) |
| 1002 | else if (type === 'object') |
| 1003 | length = coerce(subject.length) // assume that object is array-like |
| 1004 | else |
| 1005 | throw new Error('First argument needs to be a number, array or string.') |
| 1006 | |
| 1007 | var buf |
| 1008 | if (Buffer._useTypedArrays) { |
| 1009 | // Preferred: Return an augmented `Uint8Array` instance for best performance |
| 1010 | buf = Buffer._augment(new Uint8Array(length)) |
| 1011 | } else { |
| 1012 | // Fallback: Return THIS instance of Buffer (created by `new`) |
| 1013 | buf = this |
| 1014 | buf.length = length |
| 1015 | buf._isBuffer = true |
| 1016 | } |
| 1017 | |
| 1018 | var i |
| 1019 | if (Buffer._useTypedArrays && typeof subject.byteLength === 'number') { |
| 1020 | // Speed optimization -- use set if we're copying from a typed array |
| 1021 | buf._set(subject) |
| 1022 | } else if (isArrayish(subject)) { |
| 1023 | // Treat array-ish objects as a byte array |
| 1024 | for (i = 0; i < length; i++) { |
| 1025 | if (Buffer.isBuffer(subject)) |
| 1026 | buf[i] = subject.readUInt8(i) |
| 1027 | else |
| 1028 | buf[i] = subject[i] |
| 1029 | } |
| 1030 | } else if (type === 'string') { |
| 1031 | buf.write(subject, 0, encoding) |
| 1032 | } else if (type === 'number' && !Buffer._useTypedArrays && !noZero) { |
| 1033 | for (i = 0; i < length; i++) { |
| 1034 | buf[i] = 0 |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | return buf |
nothing calls this directly
no test coverage detected