michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: module.metadata = { michael@0: 'stability': 'experimental' michael@0: }; michael@0: michael@0: /* michael@0: * Encodings supported by TextEncoder/Decoder: michael@0: * utf-8, utf-16le, utf-16be michael@0: * http://encoding.spec.whatwg.org/#interface-textencoder michael@0: * michael@0: * Node however supports the following encodings: michael@0: * ascii, utf-8, utf-16le, usc2, base64, hex michael@0: */ michael@0: michael@0: const { Cu } = require('chrome'); michael@0: const { isNumber } = require('sdk/lang/type'); michael@0: const { TextEncoder, TextDecoder } = Cu.import('resource://gre/modules/commonjs/toolkit/loader.js', {}); michael@0: michael@0: exports.TextEncoder = TextEncoder; michael@0: exports.TextDecoder = TextDecoder; michael@0: michael@0: /** michael@0: * Use WeakMaps to work around Bug 929146, which prevents us from adding michael@0: * getters or values to typed arrays michael@0: * https://bugzilla.mozilla.org/show_bug.cgi?id=929146 michael@0: */ michael@0: const parents = new WeakMap(); michael@0: const views = new WeakMap(); michael@0: michael@0: function Buffer(subject, encoding /*, bufferLength */) { michael@0: michael@0: // Allow invocation without `new` constructor michael@0: if (!(this instanceof Buffer)) michael@0: return new Buffer(subject, encoding, arguments[2]); michael@0: michael@0: var type = typeof(subject); michael@0: michael@0: switch (type) { michael@0: case 'number': michael@0: // Create typed array of the given size if number. michael@0: try { michael@0: let buffer = new Uint8Array(subject > 0 ? Math.floor(subject) : 0); michael@0: return buffer; michael@0: } catch (e) { michael@0: if (/size and count too large/.test(e.message) || michael@0: /invalid arguments/.test(e.message)) michael@0: throw new RangeError('Could not instantiate buffer: size of buffer may be too large'); michael@0: else michael@0: throw new Error('Could not instantiate buffer'); michael@0: } michael@0: break; michael@0: case 'string': michael@0: // If string encode it and use buffer for the returned Uint8Array michael@0: // to create a local patched version that acts like node buffer. michael@0: encoding = encoding || 'utf8'; michael@0: return new Uint8Array(new TextEncoder(encoding).encode(subject).buffer); michael@0: case 'object': michael@0: // This form of the constructor uses the form of michael@0: // new Uint8Array(buffer, offset, length); michael@0: // So we can instantiate a typed array within the constructor michael@0: // to inherit the appropriate properties, where both the michael@0: // `subject` and newly instantiated buffer share the same underlying michael@0: // data structure. michael@0: if (arguments.length === 3) michael@0: return new Uint8Array(subject, encoding, arguments[2]); michael@0: // If array or alike just make a copy with a local patched prototype. michael@0: else michael@0: return new Uint8Array(subject); michael@0: default: michael@0: throw new TypeError('must start with number, buffer, array or string'); michael@0: } michael@0: } michael@0: exports.Buffer = Buffer; michael@0: michael@0: // Tests if `value` is a Buffer. michael@0: Buffer.isBuffer = value => value instanceof Buffer michael@0: michael@0: // Returns true if the encoding is a valid encoding argument & false otherwise michael@0: Buffer.isEncoding = function (encoding) { michael@0: if (!encoding) return false; michael@0: try { michael@0: new TextDecoder(encoding); michael@0: } catch(e) { michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // Gives the actual byte length of a string. encoding defaults to 'utf8'. michael@0: // This is not the same as String.prototype.length since that returns the michael@0: // number of characters in a string. michael@0: Buffer.byteLength = (value, encoding = 'utf8') => michael@0: new TextEncoder(encoding).encode(value).byteLength michael@0: michael@0: // Direct copy of the nodejs's buffer implementation: michael@0: // https://github.com/joyent/node/blob/b255f4c10a80343f9ce1cee56d0288361429e214/lib/buffer.js#L146-L177 michael@0: Buffer.concat = function(list, length) { michael@0: if (!Array.isArray(list)) michael@0: throw new TypeError('Usage: Buffer.concat(list[, length])'); michael@0: michael@0: if (typeof length === 'undefined') { michael@0: length = 0; michael@0: for (var i = 0; i < list.length; i++) michael@0: length += list[i].length; michael@0: } else { michael@0: length = ~~length; michael@0: } michael@0: michael@0: if (length < 0) michael@0: length = 0; michael@0: michael@0: if (list.length === 0) michael@0: return new Buffer(0); michael@0: else if (list.length === 1) michael@0: return list[0]; michael@0: michael@0: if (length < 0) michael@0: throw new RangeError('length is not a positive number'); michael@0: michael@0: var buffer = new Buffer(length); michael@0: var pos = 0; michael@0: for (var i = 0; i < list.length; i++) { michael@0: var buf = list[i]; michael@0: buf.copy(buffer, pos); michael@0: pos += buf.length; michael@0: } michael@0: michael@0: return buffer; michael@0: }; michael@0: michael@0: // Node buffer is very much like Uint8Array although it has bunch of methods michael@0: // that typically can be used in combination with `DataView` while preserving michael@0: // access by index. Since in SDK each module has it's own set of bult-ins it michael@0: // ok to patch ours to make it nodejs Buffer compatible. michael@0: Buffer.prototype = Uint8Array.prototype; michael@0: Object.defineProperties(Buffer.prototype, { michael@0: parent: { michael@0: get: function() { return parents.get(this, undefined); } michael@0: }, michael@0: view: { michael@0: get: function () { michael@0: let view = views.get(this, undefined); michael@0: if (view) return view; michael@0: view = new DataView(this.buffer); michael@0: views.set(this, view); michael@0: return view; michael@0: } michael@0: }, michael@0: toString: { michael@0: value: function(encoding, start, end) { michael@0: encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8'; michael@0: start = Math.max(0, ~~start); michael@0: end = Math.min(this.length, end === void(0) ? this.length : ~~end); michael@0: return new TextDecoder(encoding).decode(this.subarray(start, end)); michael@0: } michael@0: }, michael@0: toJSON: { michael@0: value: function() { michael@0: return { type: 'Buffer', data: Array.slice(this, 0) }; michael@0: } michael@0: }, michael@0: get: { michael@0: value: function(offset) { michael@0: return this[offset]; michael@0: } michael@0: }, michael@0: set: { michael@0: value: function(offset, value) { this[offset] = value; } michael@0: }, michael@0: copy: { michael@0: value: function(target, offset, start, end) { michael@0: let length = this.length; michael@0: let targetLength = target.length; michael@0: offset = isNumber(offset) ? offset : 0; michael@0: start = isNumber(start) ? start : 0; michael@0: michael@0: if (start < 0) michael@0: throw new RangeError('sourceStart is outside of valid range'); michael@0: if (end < 0) michael@0: throw new RangeError('sourceEnd is outside of valid range'); michael@0: michael@0: // If sourceStart > sourceEnd, or targetStart > targetLength, michael@0: // zero bytes copied michael@0: if (start > end || michael@0: offset > targetLength michael@0: ) michael@0: return 0; michael@0: michael@0: // If `end` is not defined, or if it is defined michael@0: // but would overflow `target`, redefine `end` michael@0: // so we can copy as much as we can michael@0: if (end - start > targetLength - offset || michael@0: end == null) { michael@0: let remainingTarget = targetLength - offset; michael@0: let remainingSource = length - start; michael@0: if (remainingSource <= remainingTarget) michael@0: end = length; michael@0: else michael@0: end = start + remainingTarget; michael@0: } michael@0: michael@0: Uint8Array.set(target, this.subarray(start, end), offset); michael@0: return end - start; michael@0: } michael@0: }, michael@0: slice: { michael@0: value: function(start, end) { michael@0: let length = this.length; michael@0: start = ~~start; michael@0: end = end != null ? end : length; michael@0: michael@0: if (start < 0) { michael@0: start += length; michael@0: if (start < 0) start = 0; michael@0: } else if (start > length) michael@0: start = length; michael@0: michael@0: if (end < 0) { michael@0: end += length; michael@0: if (end < 0) end = 0; michael@0: } else if (end > length) michael@0: end = length; michael@0: michael@0: if (end < start) michael@0: end = start; michael@0: michael@0: // This instantiation uses the new Uint8Array(buffer, offset, length) version michael@0: // of construction to share the same underling data structure michael@0: let buffer = new Buffer(this.buffer, start, end - start); michael@0: michael@0: // If buffer has a value, assign its parent value to the michael@0: // buffer it shares its underlying structure with. If a slice of michael@0: // a slice, then use the root structure michael@0: if (buffer.length > 0) michael@0: parents.set(buffer, this.parent || this); michael@0: michael@0: return buffer; michael@0: } michael@0: }, michael@0: write: { michael@0: value: function(string, offset, length, encoding = 'utf8') { michael@0: // write(string, encoding); michael@0: if (typeof(offset) === 'string' && Number.isNaN(parseInt(offset))) { michael@0: ([offset, length, encoding]) = [0, null, offset]; michael@0: } michael@0: // write(string, offset, encoding); michael@0: else if (typeof(length) === 'string') michael@0: ([length, encoding]) = [null, length]; michael@0: michael@0: if (offset < 0 || offset > this.length) michael@0: throw new RangeError('offset is outside of valid range'); michael@0: michael@0: offset = ~~offset; michael@0: michael@0: // Clamp length if it would overflow buffer, or if its michael@0: // undefined michael@0: if (length == null || length + offset > this.length) michael@0: length = this.length - offset; michael@0: michael@0: let buffer = new TextEncoder(encoding).encode(string); michael@0: let result = Math.min(buffer.length, length); michael@0: if (buffer.length !== length) michael@0: buffer = buffer.subarray(0, length); michael@0: michael@0: Uint8Array.set(this, buffer, offset); michael@0: return result; michael@0: } michael@0: }, michael@0: fill: { michael@0: value: function fill(value, start, end) { michael@0: let length = this.length; michael@0: value = value || 0; michael@0: start = start || 0; michael@0: end = end || length; michael@0: michael@0: if (typeof(value) === 'string') michael@0: value = value.charCodeAt(0); michael@0: if (typeof(value) !== 'number' || isNaN(value)) michael@0: throw TypeError('value is not a number'); michael@0: if (end < start) michael@0: throw new RangeError('end < start'); michael@0: michael@0: // Fill 0 bytes; we're done michael@0: if (end === start) michael@0: return 0; michael@0: if (length == 0) michael@0: return 0; michael@0: michael@0: if (start < 0 || start >= length) michael@0: throw RangeError('start out of bounds'); michael@0: michael@0: if (end < 0 || end > length) michael@0: throw RangeError('end out of bounds'); michael@0: michael@0: let index = start; michael@0: while (index < end) this[index++] = value; michael@0: } michael@0: } michael@0: }); michael@0: michael@0: // Define nodejs Buffer's getter and setter functions that just proxy michael@0: // to internal DataView's equivalent methods. michael@0: michael@0: // TODO do we need to check architecture to see if it's default big/little endian? michael@0: [['readUInt16LE', 'getUint16', true], michael@0: ['readUInt16BE', 'getUint16', false], michael@0: ['readInt16LE', 'getInt16', true], michael@0: ['readInt16BE', 'getInt16', false], michael@0: ['readUInt32LE', 'getUint32', true], michael@0: ['readUInt32BE', 'getUint32', false], michael@0: ['readInt32LE', 'getInt32', true], michael@0: ['readInt32BE', 'getInt32', false], michael@0: ['readFloatLE', 'getFloat32', true], michael@0: ['readFloatBE', 'getFloat32', false], michael@0: ['readDoubleLE', 'getFloat64', true], michael@0: ['readDoubleBE', 'getFloat64', false], michael@0: ['readUInt8', 'getUint8'], michael@0: ['readInt8', 'getInt8']].forEach(([alias, name, littleEndian]) => { michael@0: Object.defineProperty(Buffer.prototype, alias, { michael@0: value: function(offset) this.view[name](offset, littleEndian) michael@0: }); michael@0: }); michael@0: michael@0: [['writeUInt16LE', 'setUint16', true], michael@0: ['writeUInt16BE', 'setUint16', false], michael@0: ['writeInt16LE', 'setInt16', true], michael@0: ['writeInt16BE', 'setInt16', false], michael@0: ['writeUInt32LE', 'setUint32', true], michael@0: ['writeUInt32BE', 'setUint32', false], michael@0: ['writeInt32LE', 'setInt32', true], michael@0: ['writeInt32BE', 'setInt32', false], michael@0: ['writeFloatLE', 'setFloat32', true], michael@0: ['writeFloatBE', 'setFloat32', false], michael@0: ['writeDoubleLE', 'setFloat64', true], michael@0: ['writeDoubleBE', 'setFloat64', false], michael@0: ['writeUInt8', 'setUint8'], michael@0: ['writeInt8', 'setInt8']].forEach(([alias, name, littleEndian]) => { michael@0: Object.defineProperty(Buffer.prototype, alias, { michael@0: value: function(value, offset) this.view[name](offset, value, littleEndian) michael@0: }); michael@0: });