├── .gitignore
├── README.md
├── lib
└── ts-bytearray.js
├── node.d.ts
├── package.json
├── src
└── ts-bytearray.ts
├── ts-bytearray.d.ts
└── uvrun2.d.ts
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | npm-debug.log
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | TypeScript(JavaScript) ByteArray
2 | ================================
3 | ### Status : [](https://travis-ci.org/nidin/TS-ByteArray)
4 | ActionScript3 ByteArray API for JavaScript
5 |
6 | Developed by [Nidin Vinayakan]
7 |
8 | License
9 | ----
10 |
11 | MIT
12 |
13 |
14 | [Nidin Vinayakan]:https://github.com/nidin
--------------------------------------------------------------------------------
/lib/ts-bytearray.js:
--------------------------------------------------------------------------------
1 | ///
2 | ///
3 | var uvrun2_1 = require('uvrun2');
4 | var zlib = require('zlib');
5 | var ByteArrayBase = (function () {
6 | function ByteArrayBase(buffer, offset, length) {
7 | if (offset === void 0) { offset = 0; }
8 | if (length === void 0) { length = 0; }
9 | this.bitsPending = 0;
10 | this.BUFFER_EXT_SIZE = 1024; //Buffer expansion size
11 | this.array = null;
12 | this.EOF_byte = -1;
13 | this.EOF_code_point = -1;
14 | if (buffer == undefined) {
15 | buffer = new ArrayBuffer(this.BUFFER_EXT_SIZE);
16 | this.write_position = 0;
17 | }
18 | else if (buffer == null) {
19 | this.write_position = 0;
20 | }
21 | else {
22 | this.write_position = length > 0 ? length : buffer.byteLength;
23 | }
24 | if (buffer) {
25 | this.data = new DataView(buffer, offset, length > 0 ? length : buffer.byteLength);
26 | }
27 | this._position = 0;
28 | this.endian = ByteArrayBase.BIG_ENDIAN;
29 | }
30 | ByteArrayBase.prototype.readBits = function (bits, bitBuffer) {
31 | if (bitBuffer === void 0) { bitBuffer = 0; }
32 | if (bits == 0) {
33 | return bitBuffer;
34 | }
35 | var partial;
36 | var bitsConsumed;
37 | if (this.bitsPending > 0) {
38 | var _byte = this[this.position - 1] & (0xff >> (8 - this.bitsPending));
39 | bitsConsumed = Math.min(this.bitsPending, bits);
40 | this.bitsPending -= bitsConsumed;
41 | partial = _byte >> this.bitsPending;
42 | }
43 | else {
44 | bitsConsumed = Math.min(8, bits);
45 | this.bitsPending = 8 - bitsConsumed;
46 | partial = this.readUnsignedByte() >> this.bitsPending;
47 | }
48 | bits -= bitsConsumed;
49 | bitBuffer = (bitBuffer << bitsConsumed) | partial;
50 | return (bits > 0) ? this.readBits(bits, bitBuffer) : bitBuffer;
51 | };
52 | ByteArrayBase.prototype.writeBits = function (bits, value) {
53 | if (bits == 0) {
54 | return;
55 | }
56 | value &= (0xffffffff >>> (32 - bits));
57 | var bitsConsumed;
58 | if (this.bitsPending > 0) {
59 | if (this.bitsPending > bits) {
60 | this[this.position - 1] |= value << (this.bitsPending - bits);
61 | bitsConsumed = bits;
62 | this.bitsPending -= bits;
63 | }
64 | else if (this.bitsPending == bits) {
65 | this[this.position - 1] |= value;
66 | bitsConsumed = bits;
67 | this.bitsPending = 0;
68 | }
69 | else {
70 | this[this.position - 1] |= value >> (bits - this.bitsPending);
71 | bitsConsumed = this.bitsPending;
72 | this.bitsPending = 0;
73 | }
74 | }
75 | else {
76 | bitsConsumed = Math.min(8, bits);
77 | this.bitsPending = 8 - bitsConsumed;
78 | this.writeByte((value >> (bits - bitsConsumed)) << this.bitsPending);
79 | }
80 | bits -= bitsConsumed;
81 | if (bits > 0) {
82 | this.writeBits(bits, value);
83 | }
84 | };
85 | ByteArrayBase.prototype.resetBitsPending = function () {
86 | this.bitsPending = 0;
87 | };
88 | ByteArrayBase.prototype.calculateMaxBits = function (signed, values) {
89 | var b = 0;
90 | var vmax = -2147483648; //int.MIN_VALUE;
91 | if (!signed) {
92 | for (var usvalue in values) {
93 | b |= usvalue;
94 | }
95 | }
96 | else {
97 | for (var svalue in values) {
98 | if (svalue >= 0) {
99 | b |= svalue;
100 | }
101 | else {
102 | b |= ~svalue << 1;
103 | }
104 | if (vmax < svalue) {
105 | vmax = svalue;
106 | }
107 | }
108 | }
109 | var bits = 0;
110 | if (b > 0) {
111 | bits = b.toString(2).length;
112 | if (signed && vmax > 0 && vmax.toString(2).length >= bits) {
113 | bits++;
114 | }
115 | }
116 | return bits;
117 | };
118 | Object.defineProperty(ByteArrayBase.prototype, "buffer", {
119 | // getter setter
120 | get: function () {
121 | return this.data.buffer;
122 | },
123 | set: function (value) {
124 | this.data = new DataView(value);
125 | },
126 | enumerable: true,
127 | configurable: true
128 | });
129 | Object.defineProperty(ByteArrayBase.prototype, "dataView", {
130 | get: function () {
131 | return this.data;
132 | },
133 | set: function (value) {
134 | this.data = value;
135 | this.write_position = value.byteLength;
136 | },
137 | enumerable: true,
138 | configurable: true
139 | });
140 | Object.defineProperty(ByteArrayBase.prototype, "phyPosition", {
141 | get: function () {
142 | return this._position + this.data.byteOffset;
143 | },
144 | enumerable: true,
145 | configurable: true
146 | });
147 | Object.defineProperty(ByteArrayBase.prototype, "bufferOffset", {
148 | get: function () {
149 | return this.data.byteOffset;
150 | },
151 | enumerable: true,
152 | configurable: true
153 | });
154 | Object.defineProperty(ByteArrayBase.prototype, "position", {
155 | get: function () {
156 | return this._position;
157 | },
158 | set: function (value) {
159 | if (this._position < value) {
160 | if (!this.validate(this._position - value)) {
161 | return;
162 | }
163 | }
164 | this._position = value;
165 | this.write_position = value > this.write_position ? value : this.write_position;
166 | },
167 | enumerable: true,
168 | configurable: true
169 | });
170 | Object.defineProperty(ByteArrayBase.prototype, "length", {
171 | get: function () {
172 | return this.write_position;
173 | },
174 | set: function (value) {
175 | this.validateBuffer(value);
176 | },
177 | enumerable: true,
178 | configurable: true
179 | });
180 | Object.defineProperty(ByteArrayBase.prototype, "bytesAvailable", {
181 | get: function () {
182 | return this.data.byteLength - this._position;
183 | },
184 | enumerable: true,
185 | configurable: true
186 | });
187 | //end
188 | ByteArrayBase.prototype.clear = function () {
189 | this._position = 0;
190 | };
191 | ByteArrayBase.prototype.getArray = function () {
192 | if (this.array == null) {
193 | this.array = new Uint8Array(this.data.buffer, this.data.byteOffset, this.data.byteLength);
194 | }
195 | return this.array;
196 | };
197 | ByteArrayBase.prototype.setArray = function (array) {
198 | this.array = array;
199 | this.setBuffer(array.buffer, array.byteOffset, array.byteLength);
200 | };
201 | ByteArrayBase.prototype.setBuffer = function (buffer, offset, length) {
202 | if (offset === void 0) { offset = 0; }
203 | if (length === void 0) { length = 0; }
204 | if (buffer) {
205 | this.data = new DataView(buffer, offset, length > 0 ? length : buffer.byteLength);
206 | this.write_position = length > 0 ? length : buffer.byteLength;
207 | }
208 | else {
209 | this.write_position = 0;
210 | }
211 | this._position = 0;
212 | };
213 | /**
214 | * Reads a Boolean value from the byte stream. A single byte is read,
215 | * and true is returned if the byte is nonzero,
216 | * false otherwise.
217 | * @return Returns true if the byte is nonzero, false otherwise.
218 | */
219 | ByteArrayBase.prototype.readBoolean = function () {
220 | if (!this.validate(ByteArrayBase.SIZE_OF_BOOLEAN))
221 | return null;
222 | return this.data.getUint8(this.position++) != 0;
223 | };
224 | /**
225 | * Reads a signed byte from the byte stream.
226 | * The returned value is in the range -128 to 127.
227 | * @return An integer between -128 and 127.
228 | */
229 | ByteArrayBase.prototype.readByte = function () {
230 | if (!this.validate(ByteArrayBase.SIZE_OF_INT8))
231 | return null;
232 | return this.data.getInt8(this.position++);
233 | };
234 | /**
235 | * Reads the number of data bytes, specified by the length parameter, from the byte stream.
236 | * The bytes are read into the ByteArrayBase object specified by the bytes parameter,
237 | * and the bytes are written into the destination ByteArrayBase starting at the _position specified by offset.
238 | * @param bytes The ByteArrayBase object to read data into.
239 | * @param offset The offset (_position) in bytes at which the read data should be written.
240 | * @param length The number of bytes to read. The default value of 0 causes all available data to be read.
241 | */
242 | ByteArrayBase.prototype.readBytes = function (_bytes, offset, length, createNewBuffer) {
243 | if (_bytes === void 0) { _bytes = null; }
244 | if (offset === void 0) { offset = 0; }
245 | if (length === void 0) { length = 0; }
246 | if (createNewBuffer === void 0) { createNewBuffer = false; }
247 | if (length == 0) {
248 | length = this.bytesAvailable;
249 | }
250 | else if (!this.validate(length))
251 | return null;
252 | if (createNewBuffer) {
253 | _bytes = _bytes == null ? new ByteArrayBase(new ArrayBuffer(length)) : _bytes;
254 | //This method is expensive
255 | for (var i = 0; i < length; i++) {
256 | _bytes.data.setUint8(i + offset, this.data.getUint8(this.position++));
257 | }
258 | }
259 | else {
260 | //Offset argument ignored
261 | _bytes = _bytes == null ? new ByteArrayBase(null) : _bytes;
262 | _bytes.dataView = new DataView(this.data.buffer, this.bufferOffset + this.position, length);
263 | this.position += length;
264 | }
265 | return _bytes;
266 | };
267 | /**
268 | * Reads an IEEE 754 double-precision (64-bit) floating-point number from the byte stream.
269 | * @return A double-precision (64-bit) floating-point number.
270 | */
271 | ByteArrayBase.prototype.readDouble = function () {
272 | if (!this.validate(ByteArrayBase.SIZE_OF_FLOAT64))
273 | return null;
274 | var value = this.data.getFloat64(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
275 | this.position += ByteArrayBase.SIZE_OF_FLOAT64;
276 | return value;
277 | };
278 | /**
279 | * Reads an IEEE 754 single-precision (32-bit) floating-point number from the byte stream.
280 | * @return A single-precision (32-bit) floating-point number.
281 | */
282 | ByteArrayBase.prototype.readFloat = function () {
283 | if (!this.validate(ByteArrayBase.SIZE_OF_FLOAT32))
284 | return null;
285 | var value = this.data.getFloat32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
286 | this.position += ByteArrayBase.SIZE_OF_FLOAT32;
287 | return value;
288 | };
289 | /**
290 | * Reads a signed 32-bit integer from the byte stream.
291 | *
292 | * The returned value is in the range -2147483648 to 2147483647.
293 | * @return A 32-bit signed integer between -2147483648 and 2147483647.
294 | */
295 | ByteArrayBase.prototype.readInt = function () {
296 | if (!this.validate(ByteArrayBase.SIZE_OF_INT32))
297 | return null;
298 | var value = this.data.getInt32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
299 | this.position += ByteArrayBase.SIZE_OF_INT32;
300 | return value;
301 | };
302 | /**
303 | * Reads a signed 64-bit integer from the byte stream.
304 | *
305 | * The returned value is in the range −(2^63) to 2^63 − 1
306 | * @return A 64-bit signed integer between −(2^63) to 2^63 − 1
307 | */
308 | ByteArrayBase.prototype.readInt64 = function () {
309 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT32))
310 | return null;
311 | var low = this.data.getInt32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
312 | this.position += ByteArrayBase.SIZE_OF_INT32;
313 | var high = this.data.getInt32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
314 | this.position += ByteArrayBase.SIZE_OF_INT32;
315 | return new ByteArrayBase.Int64(low, high);
316 | };
317 | /**
318 | * Reads a multibyte string of specified length from the byte stream using the
319 | * specified character set.
320 | * @param length The number of bytes from the byte stream to read.
321 | * @param charSet The string denoting the character set to use to interpret the bytes.
322 | * Possible character set strings include "shift-jis", "cn-gb",
323 | * "iso-8859-1", and others.
324 | * For a complete list, see Supported Character Sets.
325 | * Note: If the value for the charSet parameter
326 | * is not recognized by the current system, the application uses the system's default
327 | * code page as the character set. For example, a value for the charSet parameter,
328 | * as in myTest.readMultiByte(22, "iso-8859-01") that uses 01 instead of
329 | * 1 might work on your development system, but not on another system.
330 | * On the other system, the application will use the system's default code page.
331 | * @return UTF-8 encoded string.
332 | */
333 | ByteArrayBase.prototype.readMultiByte = function (length, charSet) {
334 | if (!this.validate(length))
335 | return null;
336 | return "";
337 | };
338 | /**
339 | * Reads a signed 16-bit integer from the byte stream.
340 | *
341 | * The returned value is in the range -32768 to 32767.
342 | * @return A 16-bit signed integer between -32768 and 32767.
343 | */
344 | ByteArrayBase.prototype.readShort = function () {
345 | if (!this.validate(ByteArrayBase.SIZE_OF_INT16))
346 | return null;
347 | var value = this.data.getInt16(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
348 | this.position += ByteArrayBase.SIZE_OF_INT16;
349 | return value;
350 | };
351 | /**
352 | * Reads an unsigned byte from the byte stream.
353 | *
354 | * The returned value is in the range 0 to 255.
355 | * @return A 32-bit unsigned integer between 0 and 255.
356 | */
357 | ByteArrayBase.prototype.readUnsignedByte = function () {
358 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT8))
359 | return null;
360 | return this.data.getUint8(this.position++);
361 | };
362 | /**
363 | * Reads an unsigned 32-bit integer from the byte stream.
364 | *
365 | * The returned value is in the range 0 to 4294967295.
366 | * @return A 32-bit unsigned integer between 0 and 4294967295.
367 | */
368 | ByteArrayBase.prototype.readUnsignedInt = function () {
369 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT32))
370 | return null;
371 | var value = this.data.getUint32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
372 | this.position += ByteArrayBase.SIZE_OF_UINT32;
373 | return value;
374 | };
375 | /**
376 | * Reads a variable sized unsigned integer (VX -> 16-bit or 32-bit) from the byte stream.
377 | *
378 | * A VX is written as a variable length 2- or 4-byte element. If the index value is less than 65,280 (0xFF00),
379 | * then the index is written as an unsigned two-byte integer. Otherwise the index is written as an unsigned
380 | * four byte integer with bits 24-31 set. When reading an index, if the first byte encountered is 255 (0xFF),
381 | * then the four-byte form is being used and the first byte should be discarded or masked out.
382 | *
383 | * The returned value is in the range 0 to 65279 or 0 to 2147483647.
384 | * @return A VX 16-bit or 32-bit unsigned integer between 0 to 65279 or 0 and 2147483647.
385 | */
386 | ByteArrayBase.prototype.readVariableSizedUnsignedInt = function () {
387 | var value;
388 | var c = this.readUnsignedByte();
389 | if (c != 0xFF) {
390 | value = c << 8;
391 | c = this.readUnsignedByte();
392 | value |= c;
393 | }
394 | else {
395 | c = this.readUnsignedByte();
396 | value = c << 16;
397 | c = this.readUnsignedByte();
398 | value |= c << 8;
399 | c = this.readUnsignedByte();
400 | value |= c;
401 | }
402 | return value;
403 | };
404 | /**
405 | * Fast read for WebGL since only Uint16 numbers are expected
406 | */
407 | ByteArrayBase.prototype.readU16VX = function () {
408 | return (this.readUnsignedByte() << 8) | this.readUnsignedByte();
409 | };
410 | /**
411 | * Reads an unsigned 64-bit integer from the byte stream.
412 | *
413 | * The returned value is in the range 0 to 2^64 − 1.
414 | * @return A 64-bit unsigned integer between 0 and 2^64 − 1
415 | */
416 | ByteArrayBase.prototype.readUnsignedInt64 = function () {
417 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT32))
418 | return null;
419 | var low = this.data.getUint32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
420 | this.position += ByteArrayBase.SIZE_OF_UINT32;
421 | var high = this.data.getUint32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
422 | this.position += ByteArrayBase.SIZE_OF_UINT32;
423 | return new ByteArrayBase.UInt64(low, high);
424 | };
425 | /**
426 | * Reads an unsigned 16-bit integer from the byte stream.
427 | *
428 | * The returned value is in the range 0 to 65535.
429 | * @return A 16-bit unsigned integer between 0 and 65535.
430 | */
431 | ByteArrayBase.prototype.readUnsignedShort = function () {
432 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT16))
433 | return null;
434 | var value = this.data.getUint16(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
435 | this.position += ByteArrayBase.SIZE_OF_UINT16;
436 | return value;
437 | };
438 | /**
439 | * Reads a UTF-8 string from the byte stream. The string
440 | * is assumed to be prefixed with an unsigned short indicating
441 | * the length in bytes.
442 | * @return UTF-8 encoded string.
443 | */
444 | ByteArrayBase.prototype.readUTF = function () {
445 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT16))
446 | return null;
447 | var length = this.data.getUint16(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
448 | this.position += ByteArrayBase.SIZE_OF_UINT16;
449 | if (length > 0) {
450 | return this.readUTFBytes(length);
451 | }
452 | else {
453 | return "";
454 | }
455 | };
456 | /**
457 | * Reads a sequence of UTF-8 bytes specified by the length
458 | * parameter from the byte stream and returns a string.
459 | * @param length An unsigned short indicating the length of the UTF-8 bytes.
460 | * @return A string composed of the UTF-8 bytes of the specified length.
461 | */
462 | ByteArrayBase.prototype.readUTFBytes = function (length) {
463 | if (!this.validate(length))
464 | return null;
465 | var _bytes = new Uint8Array(this.buffer, this.bufferOffset + this.position, length);
466 | this.position += length;
467 | /*var _bytes: Uint8Array = new Uint8Array(new ArrayBuffer(length));
468 | for (var i = 0; i < length; i++) {
469 | _bytes[i] = this.data.getUint8(this.position++);
470 | }*/
471 | return this.decodeUTF8(_bytes);
472 | };
473 | ByteArrayBase.prototype.readStandardString = function (length) {
474 | if (!this.validate(length))
475 | return null;
476 | var str = "";
477 | for (var i = 0; i < length; i++) {
478 | str += String.fromCharCode(this.data.getUint8(this.position++));
479 | }
480 | return str;
481 | };
482 | ByteArrayBase.prototype.readStringTillNull = function (keepEvenByte) {
483 | if (keepEvenByte === void 0) { keepEvenByte = true; }
484 | var str = "";
485 | var num = 0;
486 | while (this.bytesAvailable > 0) {
487 | var _byte = this.data.getUint8(this.position++);
488 | num++;
489 | if (_byte != 0) {
490 | str += String.fromCharCode(_byte);
491 | }
492 | else {
493 | if (keepEvenByte && num % 2 != 0) {
494 | this.position++;
495 | }
496 | break;
497 | }
498 | }
499 | return str;
500 | };
501 | /**
502 | * Writes a Boolean value. A single byte is written according to the value parameter,
503 | * either 1 if true or 0 if false.
504 | * @param value A Boolean value determining which byte is written. If the parameter is true,
505 | * the method writes a 1; if false, the method writes a 0.
506 | */
507 | ByteArrayBase.prototype.writeBoolean = function (value) {
508 | this.validateBuffer(ByteArrayBase.SIZE_OF_BOOLEAN);
509 | this.data.setUint8(this.position++, value ? 1 : 0);
510 | };
511 | /**
512 | * Writes a byte to the byte stream.
513 | * The low 8 bits of the
514 | * parameter are used. The high 24 bits are ignored.
515 | * @param value A 32-bit integer. The low 8 bits are written to the byte stream.
516 | */
517 | ByteArrayBase.prototype.writeByte = function (value) {
518 | this.validateBuffer(ByteArrayBase.SIZE_OF_INT8);
519 | this.data.setInt8(this.position++, value);
520 | };
521 | ByteArrayBase.prototype.writeUnsignedByte = function (value) {
522 | this.validateBuffer(ByteArrayBase.SIZE_OF_UINT8);
523 | this.data.setUint8(this.position++, value);
524 | };
525 | /**
526 | * Writes a sequence of length bytes from the
527 | * specified byte array, bytes,
528 | * starting offset(zero-based index) bytes
529 | * into the byte stream.
530 | *
531 | * If the length parameter is omitted, the default
532 | * length of 0 is used; the method writes the entire buffer starting at
533 | * offset.
534 | * If the offset parameter is also omitted, the entire buffer is
535 | * written. If offset or length
536 | * is out of range, they are clamped to the beginning and end
537 | * of the bytes array.
538 | * @param bytes The ByteArrayBase object.
539 | * @param offset A zero-based index indicating the _position into the array to begin writing.
540 | * @param length An unsigned integer indicating how far into the buffer to write.
541 | */
542 | ByteArrayBase.prototype.writeBytes = function (_bytes, offset, length) {
543 | if (offset === void 0) { offset = 0; }
544 | if (length === void 0) { length = 0; }
545 | this.validateBuffer(length);
546 | var tmp_data = new DataView(_bytes.buffer);
547 | for (var i = 0; i < _bytes.length; i++) {
548 | this.data.setUint8(this.position++, tmp_data.getUint8(i));
549 | }
550 | };
551 | /**
552 | * Writes an IEEE 754 double-precision (64-bit) floating-point number to the byte stream.
553 | * @param value A double-precision (64-bit) floating-point number.
554 | */
555 | ByteArrayBase.prototype.writeDouble = function (value) {
556 | this.validateBuffer(ByteArrayBase.SIZE_OF_FLOAT64);
557 | this.data.setFloat64(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
558 | this.position += ByteArrayBase.SIZE_OF_FLOAT64;
559 | };
560 | /**
561 | * Writes an IEEE 754 single-precision (32-bit) floating-point number to the byte stream.
562 | * @param value A single-precision (32-bit) floating-point number.
563 | */
564 | ByteArrayBase.prototype.writeFloat = function (value) {
565 | this.validateBuffer(ByteArrayBase.SIZE_OF_FLOAT32);
566 | this.data.setFloat32(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
567 | this.position += ByteArrayBase.SIZE_OF_FLOAT32;
568 | };
569 | /**
570 | * Writes a 32-bit signed integer to the byte stream.
571 | * @param value An integer to write to the byte stream.
572 | */
573 | ByteArrayBase.prototype.writeInt = function (value) {
574 | this.validateBuffer(ByteArrayBase.SIZE_OF_INT32);
575 | this.data.setInt32(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
576 | this.position += ByteArrayBase.SIZE_OF_INT32;
577 | };
578 | /**
579 | * Writes a multibyte string to the byte stream using the specified character set.
580 | * @param value The string value to be written.
581 | * @param charSet The string denoting the character set to use. Possible character set strings
582 | * include "shift-jis", "cn-gb", "iso-8859-1", and others.
583 | * For a complete list, see Supported Character Sets.
584 | */
585 | ByteArrayBase.prototype.writeMultiByte = function (value, charSet) {
586 | };
587 | /**
588 | * Writes a 16-bit integer to the byte stream. The low 16 bits of the parameter are used.
589 | * The high 16 bits are ignored.
590 | * @param value 32-bit integer, whose low 16 bits are written to the byte stream.
591 | */
592 | ByteArrayBase.prototype.writeShort = function (value) {
593 | this.validateBuffer(ByteArrayBase.SIZE_OF_INT16);
594 | this.data.setInt16(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
595 | this.position += ByteArrayBase.SIZE_OF_INT16;
596 | };
597 | ByteArrayBase.prototype.writeUnsignedShort = function (value) {
598 | this.validateBuffer(ByteArrayBase.SIZE_OF_UINT16);
599 | this.data.setUint16(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
600 | this.position += ByteArrayBase.SIZE_OF_UINT16;
601 | };
602 | /**
603 | * Writes a 32-bit unsigned integer to the byte stream.
604 | * @param value An unsigned integer to write to the byte stream.
605 | */
606 | ByteArrayBase.prototype.writeUnsignedInt = function (value) {
607 | this.validateBuffer(ByteArrayBase.SIZE_OF_UINT32);
608 | this.data.setUint32(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
609 | this.position += ByteArrayBase.SIZE_OF_UINT32;
610 | };
611 | /**
612 | * Writes a UTF-8 string to the byte stream. The length of the UTF-8 string in bytes
613 | * is written first, as a 16-bit integer, followed by the bytes representing the
614 | * characters of the string.
615 | * @param value The string value to be written.
616 | */
617 | ByteArrayBase.prototype.writeUTF = function (value) {
618 | var utf8bytes = this.encodeUTF8(value);
619 | var length = utf8bytes.length;
620 | this.validateBuffer(ByteArrayBase.SIZE_OF_UINT16 + length);
621 | this.data.setUint16(this.position, length, this.endian === ByteArrayBase.LITTLE_ENDIAN);
622 | this.position += ByteArrayBase.SIZE_OF_UINT16;
623 | this.writeUint8Array(utf8bytes);
624 | };
625 | /**
626 | * Writes a UTF-8 string to the byte stream. Similar to the writeUTF() method,
627 | * but writeUTFBytes() does not prefix the string with a 16-bit length word.
628 | * @param value The string value to be written.
629 | */
630 | ByteArrayBase.prototype.writeUTFBytes = function (value) {
631 | this.writeUint8Array(this.encodeUTF8(value));
632 | };
633 | ByteArrayBase.prototype.toString = function () {
634 | return "[ByteArrayBase] length:" + this.length + ", bytesAvailable:" + this.bytesAvailable;
635 | };
636 | /****************************/
637 | /* EXTRA JAVASCRIPT APIs */
638 | /****************************/
639 | /**
640 | * Writes a Uint8Array to the byte stream.
641 | * @param value The Uint8Array to be written.
642 | */
643 | ByteArrayBase.prototype.writeUint8Array = function (_bytes) {
644 | this.validateBuffer(this.position + _bytes.length);
645 | for (var i = 0; i < _bytes.length; i++) {
646 | this.data.setUint8(this.position++, _bytes[i]);
647 | }
648 | };
649 | /**
650 | * Writes a Uint16Array to the byte stream.
651 | * @param value The Uint16Array to be written.
652 | */
653 | ByteArrayBase.prototype.writeUint16Array = function (_bytes) {
654 | this.validateBuffer(this.position + _bytes.length);
655 | for (var i = 0; i < _bytes.length; i++) {
656 | this.data.setUint16(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
657 | this.position += ByteArrayBase.SIZE_OF_UINT16;
658 | }
659 | };
660 | /**
661 | * Writes a Uint32Array to the byte stream.
662 | * @param value The Uint32Array to be written.
663 | */
664 | ByteArrayBase.prototype.writeUint32Array = function (_bytes) {
665 | this.validateBuffer(this.position + _bytes.length);
666 | for (var i = 0; i < _bytes.length; i++) {
667 | this.data.setUint32(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
668 | this.position += ByteArrayBase.SIZE_OF_UINT32;
669 | }
670 | };
671 | /**
672 | * Writes a Int8Array to the byte stream.
673 | * @param value The Int8Array to be written.
674 | */
675 | ByteArrayBase.prototype.writeInt8Array = function (_bytes) {
676 | this.validateBuffer(this.position + _bytes.length);
677 | for (var i = 0; i < _bytes.length; i++) {
678 | this.data.setInt8(this.position++, _bytes[i]);
679 | }
680 | };
681 | /**
682 | * Writes a Int16Array to the byte stream.
683 | * @param value The Int16Array to be written.
684 | */
685 | ByteArrayBase.prototype.writeInt16Array = function (_bytes) {
686 | this.validateBuffer(this.position + _bytes.length);
687 | for (var i = 0; i < _bytes.length; i++) {
688 | this.data.setInt16(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
689 | this.position += ByteArrayBase.SIZE_OF_INT16;
690 | }
691 | };
692 | /**
693 | * Writes a Int32Array to the byte stream.
694 | * @param value The Int32Array to be written.
695 | */
696 | ByteArrayBase.prototype.writeInt32Array = function (_bytes) {
697 | this.validateBuffer(this.position + _bytes.length);
698 | for (var i = 0; i < _bytes.length; i++) {
699 | this.data.setInt32(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
700 | this.position += ByteArrayBase.SIZE_OF_INT32;
701 | }
702 | };
703 | /**
704 | * Writes a Float32Array to the byte stream.
705 | * @param value The Float32Array to be written.
706 | */
707 | ByteArrayBase.prototype.writeFloat32Array = function (_bytes) {
708 | this.validateBuffer(this.position + _bytes.length);
709 | for (var i = 0; i < _bytes.length; i++) {
710 | this.data.setFloat32(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
711 | this.position += ByteArrayBase.SIZE_OF_FLOAT32;
712 | }
713 | };
714 | /**
715 | * Writes a Float64Array to the byte stream.
716 | * @param value The Float64Array to be written.
717 | */
718 | ByteArrayBase.prototype.writeFloat64Array = function (_bytes) {
719 | this.validateBuffer(this.position + _bytes.length);
720 | for (var i = 0; i < _bytes.length; i++) {
721 | this.data.setFloat64(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
722 | this.position += ByteArrayBase.SIZE_OF_FLOAT64;
723 | }
724 | };
725 | /**
726 | * Read a Uint8Array from the byte stream.
727 | * @param length An unsigned short indicating the length of the Uint8Array.
728 | */
729 | ByteArrayBase.prototype.readUint8Array = function (length, createNewBuffer) {
730 | if (createNewBuffer === void 0) { createNewBuffer = true; }
731 | if (!this.validate(length))
732 | return null;
733 | if (!createNewBuffer) {
734 | var result = new Uint8Array(this.buffer, this.bufferOffset + this.position, length);
735 | this.position += length;
736 | }
737 | else {
738 | result = new Uint8Array(new ArrayBuffer(length));
739 | for (var i = 0; i < length; i++) {
740 | result[i] = this.data.getUint8(this.position);
741 | this.position += ByteArrayBase.SIZE_OF_UINT8;
742 | }
743 | }
744 | return result;
745 | };
746 | /**
747 | * Read a Uint16Array from the byte stream.
748 | * @param length An unsigned short indicating the length of the Uint16Array.
749 | */
750 | ByteArrayBase.prototype.readUint16Array = function (length, createNewBuffer) {
751 | if (createNewBuffer === void 0) { createNewBuffer = true; }
752 | var size = length * ByteArrayBase.SIZE_OF_UINT16;
753 | if (!this.validate(size))
754 | return null;
755 | if (!createNewBuffer) {
756 | var result = new Uint16Array(this.buffer, this.bufferOffset + this.position, length);
757 | this.position += size;
758 | }
759 | else {
760 | result = new Uint16Array(new ArrayBuffer(size));
761 | for (var i = 0; i < length; i++) {
762 | result[i] = this.data.getUint16(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
763 | this.position += ByteArrayBase.SIZE_OF_UINT16;
764 | }
765 | }
766 | return result;
767 | };
768 | /**
769 | * Read a Uint32Array from the byte stream.
770 | * @param length An unsigned short indicating the length of the Uint32Array.
771 | */
772 | ByteArrayBase.prototype.readUint32Array = function (length, createNewBuffer) {
773 | if (createNewBuffer === void 0) { createNewBuffer = true; }
774 | var size = length * ByteArrayBase.SIZE_OF_UINT32;
775 | if (!this.validate(size))
776 | return null;
777 | if (!createNewBuffer) {
778 | var result = new Uint32Array(this.buffer, this.bufferOffset + this.position, length);
779 | this.position += size;
780 | }
781 | else {
782 | result = new Uint32Array(new ArrayBuffer(size));
783 | for (var i = 0; i < length; i++) {
784 | result[i] = this.data.getUint32(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
785 | this.position += ByteArrayBase.SIZE_OF_UINT32;
786 | }
787 | }
788 | return result;
789 | };
790 | /**
791 | * Read a Int8Array from the byte stream.
792 | * @param length An unsigned short indicating the length of the Int8Array.
793 | */
794 | ByteArrayBase.prototype.readInt8Array = function (length, createNewBuffer) {
795 | if (createNewBuffer === void 0) { createNewBuffer = true; }
796 | if (!this.validate(length))
797 | return null;
798 | if (!createNewBuffer) {
799 | var result = new Int8Array(this.buffer, this.bufferOffset + this.position, length);
800 | this.position += length;
801 | }
802 | else {
803 | result = new Int8Array(new ArrayBuffer(length));
804 | for (var i = 0; i < length; i++) {
805 | result[i] = this.data.getInt8(this.position);
806 | this.position += ByteArrayBase.SIZE_OF_INT8;
807 | }
808 | }
809 | return result;
810 | };
811 | /**
812 | * Read a Int16Array from the byte stream.
813 | * @param length An unsigned short indicating the length of the Int16Array.
814 | */
815 | ByteArrayBase.prototype.readInt16Array = function (length, createNewBuffer) {
816 | if (createNewBuffer === void 0) { createNewBuffer = true; }
817 | var size = length * ByteArrayBase.SIZE_OF_INT16;
818 | if (!this.validate(size))
819 | return null;
820 | if (!createNewBuffer) {
821 | var result = new Int16Array(this.buffer, this.bufferOffset + this.position, length);
822 | this.position += size;
823 | }
824 | else {
825 | result = new Int16Array(new ArrayBuffer(size));
826 | for (var i = 0; i < length; i++) {
827 | result[i] = this.data.getInt16(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
828 | this.position += ByteArrayBase.SIZE_OF_INT16;
829 | }
830 | }
831 | return result;
832 | };
833 | /**
834 | * Read a Int32Array from the byte stream.
835 | * @param length An unsigned short indicating the length of the Int32Array.
836 | */
837 | ByteArrayBase.prototype.readInt32Array = function (length, createNewBuffer) {
838 | if (createNewBuffer === void 0) { createNewBuffer = true; }
839 | var size = length * ByteArrayBase.SIZE_OF_INT32;
840 | if (!this.validate(size))
841 | return null;
842 | if (!createNewBuffer) {
843 | if ((this.bufferOffset + this.position) % 4 == 0) {
844 | var result = new Int32Array(this.buffer, this.bufferOffset + this.position, length);
845 | this.position += size;
846 | }
847 | else {
848 | var tmp = new Uint8Array(new ArrayBuffer(size));
849 | for (var i = 0; i < size; i++) {
850 | tmp[i] = this.data.getUint8(this.position);
851 | this.position += ByteArrayBase.SIZE_OF_UINT8;
852 | }
853 | result = new Int32Array(tmp.buffer);
854 | }
855 | }
856 | else {
857 | result = new Int32Array(new ArrayBuffer(size));
858 | for (var i = 0; i < length; i++) {
859 | result[i] = this.data.getInt32(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
860 | this.position += ByteArrayBase.SIZE_OF_INT32;
861 | }
862 | }
863 | return result;
864 | };
865 | /**
866 | * Read a Float32Array from the byte stream.
867 | * @param length An unsigned short indicating the length of the Float32Array.
868 | */
869 | ByteArrayBase.prototype.readFloat32Array = function (length, createNewBuffer) {
870 | if (createNewBuffer === void 0) { createNewBuffer = true; }
871 | var size = length * ByteArrayBase.SIZE_OF_FLOAT32;
872 | if (!this.validate(size))
873 | return null;
874 | if (!createNewBuffer) {
875 | if ((this.bufferOffset + this.position) % 4 == 0) {
876 | var result = new Float32Array(this.buffer, this.bufferOffset + this.position, length);
877 | this.position += size;
878 | }
879 | else {
880 | var tmp = new Uint8Array(new ArrayBuffer(size));
881 | for (var i = 0; i < size; i++) {
882 | tmp[i] = this.data.getUint8(this.position);
883 | this.position += ByteArrayBase.SIZE_OF_UINT8;
884 | }
885 | result = new Float32Array(tmp.buffer);
886 | }
887 | }
888 | else {
889 | result = new Float32Array(new ArrayBuffer(size));
890 | for (var i = 0; i < length; i++) {
891 | result[i] = this.data.getFloat32(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
892 | this.position += ByteArrayBase.SIZE_OF_FLOAT32;
893 | }
894 | }
895 | return result;
896 | };
897 | /**
898 | * Read a Float64Array from the byte stream.
899 | * @param length An unsigned short indicating the length of the Float64Array.
900 | */
901 | ByteArrayBase.prototype.readFloat64Array = function (length, createNewBuffer) {
902 | if (createNewBuffer === void 0) { createNewBuffer = true; }
903 | var size = length * ByteArrayBase.SIZE_OF_FLOAT64;
904 | if (!this.validate(size))
905 | return null;
906 | if (!createNewBuffer) {
907 | var result = new Float64Array(this.buffer, this.position, length);
908 | this.position += size;
909 | }
910 | else {
911 | result = new Float64Array(new ArrayBuffer(size));
912 | for (var i = 0; i < length; i++) {
913 | result[i] = this.data.getFloat64(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
914 | this.position += ByteArrayBase.SIZE_OF_FLOAT64;
915 | }
916 | }
917 | return result;
918 | };
919 | ByteArrayBase.prototype.validate = function (len) {
920 | //len += this.data.byteOffset;
921 | if (this.data.byteLength > 0 && this._position + len <= this.data.byteLength) {
922 | return true;
923 | }
924 | else {
925 | throw 'Error #2030: End of file was encountered.';
926 | }
927 | };
928 | ByteArrayBase.prototype.compress = function () {
929 | var _this = this;
930 | var finished = false;
931 | zlib.deflate(this.toBuffer(), function (err, result) {
932 | finished = true;
933 | _this.fromBuffer(result);
934 | });
935 | while (!finished) {
936 | uvrun2_1.runOnce();
937 | }
938 | };
939 | ByteArrayBase.prototype.uncompress = function () {
940 | var _this = this;
941 | var finished = false;
942 | zlib.inflate(this.toBuffer(), function (err, result) {
943 | finished = true;
944 | _this.fromBuffer(result);
945 | });
946 | while (!finished) {
947 | uvrun2_1.runOnce();
948 | }
949 | };
950 | ByteArrayBase.prototype.toBuffer = function () {
951 | var buffer = new Buffer(this.buffer.byteLength);
952 | var view = new Uint8Array(this.buffer);
953 | for (var i = 0; i < buffer.length; ++i) {
954 | buffer[i] = view[i];
955 | }
956 | return buffer;
957 | };
958 | ByteArrayBase.prototype.fromBuffer = function (buff) {
959 | var ab = new ArrayBuffer(buff.length);
960 | var view = new Uint8Array(ab);
961 | for (var i = 0; i < buff.length; ++i) {
962 | view[i] = buff[i];
963 | }
964 | this.setBuffer(ab);
965 | };
966 | /**********************/
967 | /* PRIVATE METHODS */
968 | /**********************/
969 | ByteArrayBase.prototype.validateBuffer = function (len) {
970 | this.write_position = len > this.write_position ? len : this.write_position;
971 | if (this.data.byteLength < len) {
972 | var tmp = new Uint8Array(new ArrayBuffer(len + this.BUFFER_EXT_SIZE));
973 | tmp.set(new Uint8Array(this.data.buffer));
974 | this.data.buffer = tmp.buffer;
975 | }
976 | };
977 | /**
978 | * UTF-8 Encoding/Decoding
979 | */
980 | ByteArrayBase.prototype.encodeUTF8 = function (str) {
981 | var pos = 0;
982 | var codePoints = this.stringToCodePoints(str);
983 | var outputBytes = [];
984 | while (codePoints.length > pos) {
985 | var code_point = codePoints[pos++];
986 | if (this.inRange(code_point, 0xD800, 0xDFFF)) {
987 | this.encoderError(code_point);
988 | }
989 | else if (this.inRange(code_point, 0x0000, 0x007f)) {
990 | outputBytes.push(code_point);
991 | }
992 | else {
993 | var count, offset;
994 | if (this.inRange(code_point, 0x0080, 0x07FF)) {
995 | count = 1;
996 | offset = 0xC0;
997 | }
998 | else if (this.inRange(code_point, 0x0800, 0xFFFF)) {
999 | count = 2;
1000 | offset = 0xE0;
1001 | }
1002 | else if (this.inRange(code_point, 0x10000, 0x10FFFF)) {
1003 | count = 3;
1004 | offset = 0xF0;
1005 | }
1006 | outputBytes.push(this.div(code_point, Math.pow(64, count)) + offset);
1007 | while (count > 0) {
1008 | var temp = this.div(code_point, Math.pow(64, count - 1));
1009 | outputBytes.push(0x80 + (temp % 64));
1010 | count -= 1;
1011 | }
1012 | }
1013 | }
1014 | return new Uint8Array(outputBytes);
1015 | };
1016 | ByteArrayBase.prototype.decodeUTF8 = function (data) {
1017 | var fatal = false;
1018 | var pos = 0;
1019 | var result = "";
1020 | var code_point;
1021 | var utf8_code_point = 0;
1022 | var utf8_bytes_needed = 0;
1023 | var utf8_bytes_seen = 0;
1024 | var utf8_lower_boundary = 0;
1025 | while (data.length > pos) {
1026 | var _byte = data[pos++];
1027 | if (_byte === this.EOF_byte) {
1028 | if (utf8_bytes_needed !== 0) {
1029 | code_point = this.decoderError(fatal);
1030 | }
1031 | else {
1032 | code_point = this.EOF_code_point;
1033 | }
1034 | }
1035 | else {
1036 | if (utf8_bytes_needed === 0) {
1037 | if (this.inRange(_byte, 0x00, 0x7F)) {
1038 | code_point = _byte;
1039 | }
1040 | else {
1041 | if (this.inRange(_byte, 0xC2, 0xDF)) {
1042 | utf8_bytes_needed = 1;
1043 | utf8_lower_boundary = 0x80;
1044 | utf8_code_point = _byte - 0xC0;
1045 | }
1046 | else if (this.inRange(_byte, 0xE0, 0xEF)) {
1047 | utf8_bytes_needed = 2;
1048 | utf8_lower_boundary = 0x800;
1049 | utf8_code_point = _byte - 0xE0;
1050 | }
1051 | else if (this.inRange(_byte, 0xF0, 0xF4)) {
1052 | utf8_bytes_needed = 3;
1053 | utf8_lower_boundary = 0x10000;
1054 | utf8_code_point = _byte - 0xF0;
1055 | }
1056 | else {
1057 | this.decoderError(fatal);
1058 | }
1059 | utf8_code_point = utf8_code_point * Math.pow(64, utf8_bytes_needed);
1060 | code_point = null;
1061 | }
1062 | }
1063 | else if (!this.inRange(_byte, 0x80, 0xBF)) {
1064 | utf8_code_point = 0;
1065 | utf8_bytes_needed = 0;
1066 | utf8_bytes_seen = 0;
1067 | utf8_lower_boundary = 0;
1068 | pos--;
1069 | code_point = this.decoderError(fatal, _byte);
1070 | }
1071 | else {
1072 | utf8_bytes_seen += 1;
1073 | utf8_code_point = utf8_code_point + (_byte - 0x80) * Math.pow(64, utf8_bytes_needed - utf8_bytes_seen);
1074 | if (utf8_bytes_seen !== utf8_bytes_needed) {
1075 | code_point = null;
1076 | }
1077 | else {
1078 | var cp = utf8_code_point;
1079 | var lower_boundary = utf8_lower_boundary;
1080 | utf8_code_point = 0;
1081 | utf8_bytes_needed = 0;
1082 | utf8_bytes_seen = 0;
1083 | utf8_lower_boundary = 0;
1084 | if (this.inRange(cp, lower_boundary, 0x10FFFF) && !this.inRange(cp, 0xD800, 0xDFFF)) {
1085 | code_point = cp;
1086 | }
1087 | else {
1088 | code_point = this.decoderError(fatal, _byte);
1089 | }
1090 | }
1091 | }
1092 | }
1093 | //Decode string
1094 | if (code_point !== null && code_point !== this.EOF_code_point) {
1095 | if (code_point <= 0xFFFF) {
1096 | if (code_point > 0)
1097 | result += String.fromCharCode(code_point);
1098 | }
1099 | else {
1100 | code_point -= 0x10000;
1101 | result += String.fromCharCode(0xD800 + ((code_point >> 10) & 0x3ff));
1102 | result += String.fromCharCode(0xDC00 + (code_point & 0x3ff));
1103 | }
1104 | }
1105 | }
1106 | return result;
1107 | };
1108 | ByteArrayBase.prototype.encoderError = function (code_point) {
1109 | throw 'EncodingError! The code point ' + code_point + ' could not be encoded.';
1110 | };
1111 | ByteArrayBase.prototype.decoderError = function (fatal, opt_code_point) {
1112 | if (fatal) {
1113 | throw 'DecodingError';
1114 | }
1115 | return opt_code_point || 0xFFFD;
1116 | };
1117 | ByteArrayBase.prototype.inRange = function (a, min, max) {
1118 | return min <= a && a <= max;
1119 | };
1120 | ByteArrayBase.prototype.div = function (n, d) {
1121 | return Math.floor(n / d);
1122 | };
1123 | ByteArrayBase.prototype.stringToCodePoints = function (string) {
1124 | /** @type {Array.} */
1125 | var cps = [];
1126 | // Based on http://www.w3.org/TR/WebIDL/#idl-DOMString
1127 | var i = 0, n = string.length;
1128 | while (i < string.length) {
1129 | var c = string.charCodeAt(i);
1130 | if (!this.inRange(c, 0xD800, 0xDFFF)) {
1131 | cps.push(c);
1132 | }
1133 | else if (this.inRange(c, 0xDC00, 0xDFFF)) {
1134 | cps.push(0xFFFD);
1135 | }
1136 | else {
1137 | if (i === n - 1) {
1138 | cps.push(0xFFFD);
1139 | }
1140 | else {
1141 | var d = string.charCodeAt(i + 1);
1142 | if (this.inRange(d, 0xDC00, 0xDFFF)) {
1143 | var a = c & 0x3FF;
1144 | var b = d & 0x3FF;
1145 | i += 1;
1146 | cps.push(0x10000 + (a << 10) + b);
1147 | }
1148 | else {
1149 | cps.push(0xFFFD);
1150 | }
1151 | }
1152 | }
1153 | i += 1;
1154 | }
1155 | return cps;
1156 | };
1157 | ByteArrayBase.BIG_ENDIAN = "bigEndian";
1158 | ByteArrayBase.LITTLE_ENDIAN = "littleEndian";
1159 | ByteArrayBase.SIZE_OF_BOOLEAN = 1;
1160 | ByteArrayBase.SIZE_OF_INT8 = 1;
1161 | ByteArrayBase.SIZE_OF_INT16 = 2;
1162 | ByteArrayBase.SIZE_OF_INT32 = 4;
1163 | ByteArrayBase.SIZE_OF_INT64 = 8;
1164 | ByteArrayBase.SIZE_OF_UINT8 = 1;
1165 | ByteArrayBase.SIZE_OF_UINT16 = 2;
1166 | ByteArrayBase.SIZE_OF_UINT32 = 4;
1167 | ByteArrayBase.SIZE_OF_UINT64 = 8;
1168 | ByteArrayBase.SIZE_OF_FLOAT32 = 4;
1169 | ByteArrayBase.SIZE_OF_FLOAT64 = 8;
1170 | return ByteArrayBase;
1171 | })();
1172 | var ByteArrayBase;
1173 | (function (ByteArrayBase) {
1174 | var Int64 = (function () {
1175 | function Int64(low, high) {
1176 | if (low === void 0) { low = 0; }
1177 | if (high === void 0) { high = 0; }
1178 | this.low = low;
1179 | this.high = high;
1180 | }
1181 | Int64.prototype.value = function () {
1182 | //this._value = (this.low | (this.high << 32));
1183 | var _h = this.high.toString(16);
1184 | var _hd = 8 - _h.length;
1185 | if (_hd > 0) {
1186 | for (var i = 0; i < _hd; i++) {
1187 | _h = '0' + _h;
1188 | }
1189 | }
1190 | var _l = this.low.toString(16);
1191 | var _ld = 8 - _l.length;
1192 | if (_ld > 0) {
1193 | for (i = 0; i < _ld; i++) {
1194 | _l = '0' + _l;
1195 | }
1196 | }
1197 | this._value = Number('0x' + _h + _l);
1198 | return this._value;
1199 | };
1200 | return Int64;
1201 | })();
1202 | ByteArrayBase.Int64 = Int64;
1203 | var UInt64 = (function () {
1204 | function UInt64(low, high) {
1205 | if (low === void 0) { low = 0; }
1206 | if (high === void 0) { high = 0; }
1207 | this.low = low;
1208 | this.high = high;
1209 | }
1210 | UInt64.prototype.value = function () {
1211 | //this._value = (this.low | (this.high << 32));
1212 | var _h = this.high.toString(16);
1213 | var _hd = 8 - _h.length;
1214 | if (_hd > 0) {
1215 | for (var i = 0; i < _hd; i++) {
1216 | _h = '0' + _h;
1217 | }
1218 | }
1219 | var _l = this.low.toString(16);
1220 | var _ld = 8 - _l.length;
1221 | if (_ld > 0) {
1222 | for (i = 0; i < _ld; i++) {
1223 | _l = '0' + _l;
1224 | }
1225 | }
1226 | this._value = Number('0x' + _h + _l);
1227 | return this._value;
1228 | };
1229 | return UInt64;
1230 | })();
1231 | ByteArrayBase.UInt64 = UInt64;
1232 | })(ByteArrayBase || (ByteArrayBase = {}));
1233 | module.exports = ByteArrayBase;
1234 |
--------------------------------------------------------------------------------
/node.d.ts:
--------------------------------------------------------------------------------
1 | // Type definitions for Node.js v0.12.0
2 | // Project: http://nodejs.org/
3 | // Definitions by: Microsoft TypeScript , DefinitelyTyped
4 | // Definitions: https://github.com/borisyankov/DefinitelyTyped
5 |
6 | /************************************************
7 | * *
8 | * Node.js v0.12.0 API *
9 | * *
10 | ************************************************/
11 |
12 | interface Error {
13 | stack?: string;
14 | }
15 |
16 |
17 | // compat for TypeScript 1.5.3
18 | // if you use with --target es3 or --target es5 and use below definitions,
19 | // use the lib.es6.d.ts that is bundled with TypeScript 1.5.3.
20 | interface MapConstructor {}
21 | interface WeakMapConstructor {}
22 | interface SetConstructor {}
23 | interface WeakSetConstructor {}
24 |
25 | /************************************************
26 | * *
27 | * GLOBAL *
28 | * *
29 | ************************************************/
30 | declare var process: NodeJS.Process;
31 | declare var global: NodeJS.Global;
32 |
33 | declare var __filename: string;
34 | declare var __dirname: string;
35 |
36 | declare function setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer;
37 | declare function clearTimeout(timeoutId: NodeJS.Timer): void;
38 | declare function setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer;
39 | declare function clearInterval(intervalId: NodeJS.Timer): void;
40 | declare function setImmediate(callback: (...args: any[]) => void, ...args: any[]): any;
41 | declare function clearImmediate(immediateId: any): void;
42 |
43 | interface NodeRequireFunction {
44 | (id: string): any;
45 | }
46 |
47 | interface NodeRequire extends NodeRequireFunction {
48 | resolve(id:string): string;
49 | cache: any;
50 | extensions: any;
51 | main: any;
52 | }
53 |
54 | declare var require: NodeRequire;
55 |
56 | interface NodeModule {
57 | exports: any;
58 | require: NodeRequireFunction;
59 | id: string;
60 | filename: string;
61 | loaded: boolean;
62 | parent: any;
63 | children: any[];
64 | }
65 |
66 | declare var module: NodeModule;
67 |
68 | // Same as module.exports
69 | declare var exports: any;
70 | declare var SlowBuffer: {
71 | new (str: string, encoding?: string): Buffer;
72 | new (size: number): Buffer;
73 | new (size: Uint8Array): Buffer;
74 | new (array: any[]): Buffer;
75 | prototype: Buffer;
76 | isBuffer(obj: any): boolean;
77 | byteLength(string: string, encoding?: string): number;
78 | concat(list: Buffer[], totalLength?: number): Buffer;
79 | };
80 |
81 |
82 | // Buffer class
83 | interface Buffer extends NodeBuffer {}
84 |
85 | /**
86 | * Raw data is stored in instances of the Buffer class.
87 | * A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.
88 | * Valid string encodings: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex'
89 | */
90 | declare var Buffer: {
91 | /**
92 | * Allocates a new buffer containing the given {str}.
93 | *
94 | * @param str String to store in buffer.
95 | * @param encoding encoding to use, optional. Default is 'utf8'
96 | */
97 | new (str: string, encoding?: string): Buffer;
98 | /**
99 | * Allocates a new buffer of {size} octets.
100 | *
101 | * @param size count of octets to allocate.
102 | */
103 | new (size: number): Buffer;
104 | /**
105 | * Allocates a new buffer containing the given {array} of octets.
106 | *
107 | * @param array The octets to store.
108 | */
109 | new (array: Uint8Array): Buffer;
110 | /**
111 | * Allocates a new buffer containing the given {array} of octets.
112 | *
113 | * @param array The octets to store.
114 | */
115 | new (array: any[]): Buffer;
116 | prototype: Buffer;
117 | /**
118 | * Returns true if {obj} is a Buffer
119 | *
120 | * @param obj object to test.
121 | */
122 | isBuffer(obj: any): boolean;
123 | /**
124 | * Returns true if {encoding} is a valid encoding argument.
125 | * Valid string encodings in Node 0.12: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex'
126 | *
127 | * @param encoding string to test.
128 | */
129 | isEncoding(encoding: string): boolean;
130 | /**
131 | * Gives the actual byte length of a string. encoding defaults to 'utf8'.
132 | * This is not the same as String.prototype.length since that returns the number of characters in a string.
133 | *
134 | * @param string string to test.
135 | * @param encoding encoding used to evaluate (defaults to 'utf8')
136 | */
137 | byteLength(string: string, encoding?: string): number;
138 | /**
139 | * Returns a buffer which is the result of concatenating all the buffers in the list together.
140 | *
141 | * If the list has no items, or if the totalLength is 0, then it returns a zero-length buffer.
142 | * If the list has exactly one item, then the first item of the list is returned.
143 | * If the list has more than one item, then a new Buffer is created.
144 | *
145 | * @param list An array of Buffer objects to concatenate
146 | * @param totalLength Total length of the buffers when concatenated.
147 | * If totalLength is not provided, it is read from the buffers in the list. However, this adds an additional loop to the function, so it is faster to provide the length explicitly.
148 | */
149 | concat(list: Buffer[], totalLength?: number): Buffer;
150 | /**
151 | * The same as buf1.compare(buf2).
152 | */
153 | compare(buf1: Buffer, buf2: Buffer): number;
154 | };
155 |
156 | /************************************************
157 | * *
158 | * GLOBAL INTERFACES *
159 | * *
160 | ************************************************/
161 | declare module NodeJS {
162 | export interface ErrnoException extends Error {
163 | errno?: number;
164 | code?: string;
165 | path?: string;
166 | syscall?: string;
167 | stack?: string;
168 | }
169 |
170 | export interface EventEmitter {
171 | addListener(event: string, listener: Function): EventEmitter;
172 | on(event: string, listener: Function): EventEmitter;
173 | once(event: string, listener: Function): EventEmitter;
174 | removeListener(event: string, listener: Function): EventEmitter;
175 | removeAllListeners(event?: string): EventEmitter;
176 | setMaxListeners(n: number): void;
177 | listeners(event: string): Function[];
178 | emit(event: string, ...args: any[]): boolean;
179 | }
180 |
181 | export interface ReadableStream extends EventEmitter {
182 | readable: boolean;
183 | read(size?: number): string|Buffer;
184 | setEncoding(encoding: string): void;
185 | pause(): void;
186 | resume(): void;
187 | pipe(destination: T, options?: { end?: boolean; }): T;
188 | unpipe(destination?: T): void;
189 | unshift(chunk: string): void;
190 | unshift(chunk: Buffer): void;
191 | wrap(oldStream: ReadableStream): ReadableStream;
192 | }
193 |
194 | export interface WritableStream extends EventEmitter {
195 | writable: boolean;
196 | write(buffer: Buffer|string, cb?: Function): boolean;
197 | write(str: string, encoding?: string, cb?: Function): boolean;
198 | end(): void;
199 | end(buffer: Buffer, cb?: Function): void;
200 | end(str: string, cb?: Function): void;
201 | end(str: string, encoding?: string, cb?: Function): void;
202 | }
203 |
204 | export interface ReadWriteStream extends ReadableStream, WritableStream {}
205 |
206 | export interface Process extends EventEmitter {
207 | stdout: WritableStream;
208 | stderr: WritableStream;
209 | stdin: ReadableStream;
210 | argv: string[];
211 | execPath: string;
212 | abort(): void;
213 | chdir(directory: string): void;
214 | cwd(): string;
215 | env: any;
216 | exit(code?: number): void;
217 | getgid(): number;
218 | setgid(id: number): void;
219 | setgid(id: string): void;
220 | getuid(): number;
221 | setuid(id: number): void;
222 | setuid(id: string): void;
223 | version: string;
224 | versions: {
225 | http_parser: string;
226 | node: string;
227 | v8: string;
228 | ares: string;
229 | uv: string;
230 | zlib: string;
231 | openssl: string;
232 | };
233 | config: {
234 | target_defaults: {
235 | cflags: any[];
236 | default_configuration: string;
237 | defines: string[];
238 | include_dirs: string[];
239 | libraries: string[];
240 | };
241 | variables: {
242 | clang: number;
243 | host_arch: string;
244 | node_install_npm: boolean;
245 | node_install_waf: boolean;
246 | node_prefix: string;
247 | node_shared_openssl: boolean;
248 | node_shared_v8: boolean;
249 | node_shared_zlib: boolean;
250 | node_use_dtrace: boolean;
251 | node_use_etw: boolean;
252 | node_use_openssl: boolean;
253 | target_arch: string;
254 | v8_no_strict_aliasing: number;
255 | v8_use_snapshot: boolean;
256 | visibility: string;
257 | };
258 | };
259 | kill(pid: number, signal?: string): void;
260 | pid: number;
261 | title: string;
262 | arch: string;
263 | platform: string;
264 | memoryUsage(): { rss: number; heapTotal: number; heapUsed: number; };
265 | nextTick(callback: Function): void;
266 | umask(mask?: number): number;
267 | uptime(): number;
268 | hrtime(time?:number[]): number[];
269 |
270 | // Worker
271 | send?(message: any, sendHandle?: any): void;
272 | }
273 |
274 | export interface Global {
275 | Array: typeof Array;
276 | ArrayBuffer: typeof ArrayBuffer;
277 | Boolean: typeof Boolean;
278 | Buffer: typeof Buffer;
279 | DataView: typeof DataView;
280 | Date: typeof Date;
281 | Error: typeof Error;
282 | EvalError: typeof EvalError;
283 | Float32Array: typeof Float32Array;
284 | Float64Array: typeof Float64Array;
285 | Function: typeof Function;
286 | GLOBAL: Global;
287 | Infinity: typeof Infinity;
288 | Int16Array: typeof Int16Array;
289 | Int32Array: typeof Int32Array;
290 | Int8Array: typeof Int8Array;
291 | Intl: typeof Intl;
292 | JSON: typeof JSON;
293 | Map: MapConstructor;
294 | Math: typeof Math;
295 | NaN: typeof NaN;
296 | Number: typeof Number;
297 | Object: typeof Object;
298 | Promise: Function;
299 | RangeError: typeof RangeError;
300 | ReferenceError: typeof ReferenceError;
301 | RegExp: typeof RegExp;
302 | Set: SetConstructor;
303 | String: typeof String;
304 | Symbol: Function;
305 | SyntaxError: typeof SyntaxError;
306 | TypeError: typeof TypeError;
307 | URIError: typeof URIError;
308 | Uint16Array: typeof Uint16Array;
309 | Uint32Array: typeof Uint32Array;
310 | Uint8Array: typeof Uint8Array;
311 | Uint8ClampedArray: Function;
312 | WeakMap: WeakMapConstructor;
313 | WeakSet: WeakSetConstructor;
314 | clearImmediate: (immediateId: any) => void;
315 | clearInterval: (intervalId: NodeJS.Timer) => void;
316 | clearTimeout: (timeoutId: NodeJS.Timer) => void;
317 | console: typeof console;
318 | decodeURI: typeof decodeURI;
319 | decodeURIComponent: typeof decodeURIComponent;
320 | encodeURI: typeof encodeURI;
321 | encodeURIComponent: typeof encodeURIComponent;
322 | escape: (str: string) => string;
323 | eval: typeof eval;
324 | global: Global;
325 | isFinite: typeof isFinite;
326 | isNaN: typeof isNaN;
327 | parseFloat: typeof parseFloat;
328 | parseInt: typeof parseInt;
329 | process: Process;
330 | root: Global;
331 | setImmediate: (callback: (...args: any[]) => void, ...args: any[]) => any;
332 | setInterval: (callback: (...args: any[]) => void, ms: number, ...args: any[]) => NodeJS.Timer;
333 | setTimeout: (callback: (...args: any[]) => void, ms: number, ...args: any[]) => NodeJS.Timer;
334 | undefined: typeof undefined;
335 | unescape: (str: string) => string;
336 | gc: () => void;
337 | v8debug?: any;
338 | }
339 |
340 | export interface Timer {
341 | ref() : void;
342 | unref() : void;
343 | }
344 | }
345 |
346 | /**
347 | * @deprecated
348 | */
349 | interface NodeBuffer {
350 | [index: number]: number;
351 | write(string: string, offset?: number, length?: number, encoding?: string): number;
352 | toString(encoding?: string, start?: number, end?: number): string;
353 | toJSON(): any;
354 | length: number;
355 | equals(otherBuffer: Buffer): boolean;
356 | compare(otherBuffer: Buffer): number;
357 | copy(targetBuffer: Buffer, targetStart?: number, sourceStart?: number, sourceEnd?: number): number;
358 | slice(start?: number, end?: number): Buffer;
359 | writeUIntLE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
360 | writeUIntBE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
361 | writeIntLE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
362 | writeIntBE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
363 | readUIntLE(offset: number, byteLength: number, noAssert?: boolean): number;
364 | readUIntBE(offset: number, byteLength: number, noAssert?: boolean): number;
365 | readIntLE(offset: number, byteLength: number, noAssert?: boolean): number;
366 | readIntBE(offset: number, byteLength: number, noAssert?: boolean): number;
367 | readUInt8(offset: number, noAsset?: boolean): number;
368 | readUInt16LE(offset: number, noAssert?: boolean): number;
369 | readUInt16BE(offset: number, noAssert?: boolean): number;
370 | readUInt32LE(offset: number, noAssert?: boolean): number;
371 | readUInt32BE(offset: number, noAssert?: boolean): number;
372 | readInt8(offset: number, noAssert?: boolean): number;
373 | readInt16LE(offset: number, noAssert?: boolean): number;
374 | readInt16BE(offset: number, noAssert?: boolean): number;
375 | readInt32LE(offset: number, noAssert?: boolean): number;
376 | readInt32BE(offset: number, noAssert?: boolean): number;
377 | readFloatLE(offset: number, noAssert?: boolean): number;
378 | readFloatBE(offset: number, noAssert?: boolean): number;
379 | readDoubleLE(offset: number, noAssert?: boolean): number;
380 | readDoubleBE(offset: number, noAssert?: boolean): number;
381 | writeUInt8(value: number, offset: number, noAssert?: boolean): number;
382 | writeUInt16LE(value: number, offset: number, noAssert?: boolean): number;
383 | writeUInt16BE(value: number, offset: number, noAssert?: boolean): number;
384 | writeUInt32LE(value: number, offset: number, noAssert?: boolean): number;
385 | writeUInt32BE(value: number, offset: number, noAssert?: boolean): number;
386 | writeInt8(value: number, offset: number, noAssert?: boolean): number;
387 | writeInt16LE(value: number, offset: number, noAssert?: boolean): number;
388 | writeInt16BE(value: number, offset: number, noAssert?: boolean): number;
389 | writeInt32LE(value: number, offset: number, noAssert?: boolean): number;
390 | writeInt32BE(value: number, offset: number, noAssert?: boolean): number;
391 | writeFloatLE(value: number, offset: number, noAssert?: boolean): number;
392 | writeFloatBE(value: number, offset: number, noAssert?: boolean): number;
393 | writeDoubleLE(value: number, offset: number, noAssert?: boolean): number;
394 | writeDoubleBE(value: number, offset: number, noAssert?: boolean): number;
395 | fill(value: any, offset?: number, end?: number): Buffer;
396 | }
397 |
398 | /************************************************
399 | * *
400 | * MODULES *
401 | * *
402 | ************************************************/
403 | declare module "buffer" {
404 | export var INSPECT_MAX_BYTES: number;
405 | }
406 |
407 | declare module "querystring" {
408 | export function stringify(obj: any, sep?: string, eq?: string): string;
409 | export function parse(str: string, sep?: string, eq?: string, options?: { maxKeys?: number; }): any;
410 | export function escape(str: string): string;
411 | export function unescape(str: string): string;
412 | }
413 |
414 | declare module "events" {
415 | export class EventEmitter implements NodeJS.EventEmitter {
416 | static listenerCount(emitter: EventEmitter, event: string): number;
417 |
418 | addListener(event: string, listener: Function): EventEmitter;
419 | on(event: string, listener: Function): EventEmitter;
420 | once(event: string, listener: Function): EventEmitter;
421 | removeListener(event: string, listener: Function): EventEmitter;
422 | removeAllListeners(event?: string): EventEmitter;
423 | setMaxListeners(n: number): void;
424 | listeners(event: string): Function[];
425 | emit(event: string, ...args: any[]): boolean;
426 | }
427 | }
428 |
429 | declare module "http" {
430 | import * as events from "events";
431 | import * as net from "net";
432 | import * as stream from "stream";
433 |
434 | export interface Server extends events.EventEmitter {
435 | listen(port: number, hostname?: string, backlog?: number, callback?: Function): Server;
436 | listen(port: number, hostname?: string, callback?: Function): Server;
437 | listen(path: string, callback?: Function): Server;
438 | listen(handle: any, listeningListener?: Function): Server;
439 | close(cb?: any): Server;
440 | address(): { port: number; family: string; address: string; };
441 | maxHeadersCount: number;
442 | }
443 | /**
444 | * @deprecated Use IncomingMessage
445 | */
446 | export interface ServerRequest extends IncomingMessage {
447 | connection: net.Socket;
448 | }
449 | export interface ServerResponse extends events.EventEmitter, stream.Writable {
450 | // Extended base methods
451 | write(buffer: Buffer): boolean;
452 | write(buffer: Buffer, cb?: Function): boolean;
453 | write(str: string, cb?: Function): boolean;
454 | write(str: string, encoding?: string, cb?: Function): boolean;
455 | write(str: string, encoding?: string, fd?: string): boolean;
456 |
457 | writeContinue(): void;
458 | writeHead(statusCode: number, reasonPhrase?: string, headers?: any): void;
459 | writeHead(statusCode: number, headers?: any): void;
460 | statusCode: number;
461 | statusMessage: string;
462 | setHeader(name: string, value: string): void;
463 | sendDate: boolean;
464 | getHeader(name: string): string;
465 | removeHeader(name: string): void;
466 | write(chunk: any, encoding?: string): any;
467 | addTrailers(headers: any): void;
468 |
469 | // Extended base methods
470 | end(): void;
471 | end(buffer: Buffer, cb?: Function): void;
472 | end(str: string, cb?: Function): void;
473 | end(str: string, encoding?: string, cb?: Function): void;
474 | end(data?: any, encoding?: string): void;
475 | }
476 | export interface ClientRequest extends events.EventEmitter, stream.Writable {
477 | // Extended base methods
478 | write(buffer: Buffer): boolean;
479 | write(buffer: Buffer, cb?: Function): boolean;
480 | write(str: string, cb?: Function): boolean;
481 | write(str: string, encoding?: string, cb?: Function): boolean;
482 | write(str: string, encoding?: string, fd?: string): boolean;
483 |
484 | write(chunk: any, encoding?: string): void;
485 | abort(): void;
486 | setTimeout(timeout: number, callback?: Function): void;
487 | setNoDelay(noDelay?: boolean): void;
488 | setSocketKeepAlive(enable?: boolean, initialDelay?: number): void;
489 |
490 | // Extended base methods
491 | end(): void;
492 | end(buffer: Buffer, cb?: Function): void;
493 | end(str: string, cb?: Function): void;
494 | end(str: string, encoding?: string, cb?: Function): void;
495 | end(data?: any, encoding?: string): void;
496 | }
497 | export interface IncomingMessage extends events.EventEmitter, stream.Readable {
498 | httpVersion: string;
499 | headers: any;
500 | rawHeaders: string[];
501 | trailers: any;
502 | rawTrailers: any;
503 | setTimeout(msecs: number, callback: Function): NodeJS.Timer;
504 | /**
505 | * Only valid for request obtained from http.Server.
506 | */
507 | method?: string;
508 | /**
509 | * Only valid for request obtained from http.Server.
510 | */
511 | url?: string;
512 | /**
513 | * Only valid for response obtained from http.ClientRequest.
514 | */
515 | statusCode?: number;
516 | /**
517 | * Only valid for response obtained from http.ClientRequest.
518 | */
519 | statusMessage?: string;
520 | socket: net.Socket;
521 | }
522 | /**
523 | * @deprecated Use IncomingMessage
524 | */
525 | export interface ClientResponse extends IncomingMessage { }
526 |
527 | export interface AgentOptions {
528 | /**
529 | * Keep sockets around in a pool to be used by other requests in the future. Default = false
530 | */
531 | keepAlive?: boolean;
532 | /**
533 | * When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets being kept alive. Default = 1000.
534 | * Only relevant if keepAlive is set to true.
535 | */
536 | keepAliveMsecs?: number;
537 | /**
538 | * Maximum number of sockets to allow per host. Default for Node 0.10 is 5, default for Node 0.12 is Infinity
539 | */
540 | maxSockets?: number;
541 | /**
542 | * Maximum number of sockets to leave open in a free state. Only relevant if keepAlive is set to true. Default = 256.
543 | */
544 | maxFreeSockets?: number;
545 | }
546 |
547 | export class Agent {
548 | maxSockets: number;
549 | sockets: any;
550 | requests: any;
551 |
552 | constructor(opts?: AgentOptions);
553 |
554 | /**
555 | * Destroy any sockets that are currently in use by the agent.
556 | * It is usually not necessary to do this. However, if you are using an agent with KeepAlive enabled,
557 | * then it is best to explicitly shut down the agent when you know that it will no longer be used. Otherwise,
558 | * sockets may hang open for quite a long time before the server terminates them.
559 | */
560 | destroy(): void;
561 | }
562 |
563 | export var METHODS: string[];
564 |
565 | export var STATUS_CODES: {
566 | [errorCode: number]: string;
567 | [errorCode: string]: string;
568 | };
569 | export function createServer(requestListener?: (request: IncomingMessage, response: ServerResponse) =>void ): Server;
570 | export function createClient(port?: number, host?: string): any;
571 | export function request(options: any, callback?: (res: IncomingMessage) => void): ClientRequest;
572 | export function get(options: any, callback?: (res: IncomingMessage) => void): ClientRequest;
573 | export var globalAgent: Agent;
574 | }
575 |
576 | declare module "cluster" {
577 | import * as child from "child_process";
578 | import * as events from "events";
579 |
580 | export interface ClusterSettings {
581 | exec?: string;
582 | args?: string[];
583 | silent?: boolean;
584 | }
585 |
586 | export class Worker extends events.EventEmitter {
587 | id: string;
588 | process: child.ChildProcess;
589 | suicide: boolean;
590 | send(message: any, sendHandle?: any): void;
591 | kill(signal?: string): void;
592 | destroy(signal?: string): void;
593 | disconnect(): void;
594 | }
595 |
596 | export var settings: ClusterSettings;
597 | export var isMaster: boolean;
598 | export var isWorker: boolean;
599 | export function setupMaster(settings?: ClusterSettings): void;
600 | export function fork(env?: any): Worker;
601 | export function disconnect(callback?: Function): void;
602 | export var worker: Worker;
603 | export var workers: Worker[];
604 |
605 | // Event emitter
606 | export function addListener(event: string, listener: Function): void;
607 | export function on(event: string, listener: Function): any;
608 | export function once(event: string, listener: Function): void;
609 | export function removeListener(event: string, listener: Function): void;
610 | export function removeAllListeners(event?: string): void;
611 | export function setMaxListeners(n: number): void;
612 | export function listeners(event: string): Function[];
613 | export function emit(event: string, ...args: any[]): boolean;
614 | }
615 |
616 | declare module "zlib" {
617 | import * as stream from "stream";
618 | export interface ZlibOptions { chunkSize?: number; windowBits?: number; level?: number; memLevel?: number; strategy?: number; dictionary?: any; }
619 |
620 | export interface Gzip extends stream.Transform { }
621 | export interface Gunzip extends stream.Transform { }
622 | export interface Deflate extends stream.Transform { }
623 | export interface Inflate extends stream.Transform { }
624 | export interface DeflateRaw extends stream.Transform { }
625 | export interface InflateRaw extends stream.Transform { }
626 | export interface Unzip extends stream.Transform { }
627 |
628 | export function createGzip(options?: ZlibOptions): Gzip;
629 | export function createGunzip(options?: ZlibOptions): Gunzip;
630 | export function createDeflate(options?: ZlibOptions): Deflate;
631 | export function createInflate(options?: ZlibOptions): Inflate;
632 | export function createDeflateRaw(options?: ZlibOptions): DeflateRaw;
633 | export function createInflateRaw(options?: ZlibOptions): InflateRaw;
634 | export function createUnzip(options?: ZlibOptions): Unzip;
635 |
636 | export function deflate(buf: Buffer, callback: (error: Error, result: any) =>void ): void;
637 | export function deflateSync(buf: Buffer, options?: ZlibOptions): any;
638 | export function deflateRaw(buf: Buffer, callback: (error: Error, result: any) =>void ): void;
639 | export function deflateRawSync(buf: Buffer, options?: ZlibOptions): any;
640 | export function gzip(buf: Buffer, callback: (error: Error, result: any) =>void ): void;
641 | export function gzipSync(buf: Buffer, options?: ZlibOptions): any;
642 | export function gunzip(buf: Buffer, callback: (error: Error, result: any) =>void ): void;
643 | export function gunzipSync(buf: Buffer, options?: ZlibOptions): any;
644 | export function inflate(buf: Buffer, callback: (error: Error, result: any) =>void ): void;
645 | export function inflateSync(buf: Buffer, options?: ZlibOptions): any;
646 | export function inflateRaw(buf: Buffer, callback: (error: Error, result: any) =>void ): void;
647 | export function inflateRawSync(buf: Buffer, options?: ZlibOptions): any;
648 | export function unzip(buf: Buffer, callback: (error: Error, result: any) =>void ): void;
649 | export function unzipSync(buf: Buffer, options?: ZlibOptions): any;
650 |
651 | // Constants
652 | export var Z_NO_FLUSH: number;
653 | export var Z_PARTIAL_FLUSH: number;
654 | export var Z_SYNC_FLUSH: number;
655 | export var Z_FULL_FLUSH: number;
656 | export var Z_FINISH: number;
657 | export var Z_BLOCK: number;
658 | export var Z_TREES: number;
659 | export var Z_OK: number;
660 | export var Z_STREAM_END: number;
661 | export var Z_NEED_DICT: number;
662 | export var Z_ERRNO: number;
663 | export var Z_STREAM_ERROR: number;
664 | export var Z_DATA_ERROR: number;
665 | export var Z_MEM_ERROR: number;
666 | export var Z_BUF_ERROR: number;
667 | export var Z_VERSION_ERROR: number;
668 | export var Z_NO_COMPRESSION: number;
669 | export var Z_BEST_SPEED: number;
670 | export var Z_BEST_COMPRESSION: number;
671 | export var Z_DEFAULT_COMPRESSION: number;
672 | export var Z_FILTERED: number;
673 | export var Z_HUFFMAN_ONLY: number;
674 | export var Z_RLE: number;
675 | export var Z_FIXED: number;
676 | export var Z_DEFAULT_STRATEGY: number;
677 | export var Z_BINARY: number;
678 | export var Z_TEXT: number;
679 | export var Z_ASCII: number;
680 | export var Z_UNKNOWN: number;
681 | export var Z_DEFLATED: number;
682 | export var Z_NULL: number;
683 | }
684 |
685 | declare module "os" {
686 | export function tmpdir(): string;
687 | export function hostname(): string;
688 | export function type(): string;
689 | export function platform(): string;
690 | export function arch(): string;
691 | export function release(): string;
692 | export function uptime(): number;
693 | export function loadavg(): number[];
694 | export function totalmem(): number;
695 | export function freemem(): number;
696 | export function cpus(): { model: string; speed: number; times: { user: number; nice: number; sys: number; idle: number; irq: number; }; }[];
697 | export function networkInterfaces(): any;
698 | export var EOL: string;
699 | }
700 |
701 | declare module "https" {
702 | import * as tls from "tls";
703 | import * as events from "events";
704 | import * as http from "http";
705 |
706 | export interface ServerOptions {
707 | pfx?: any;
708 | key?: any;
709 | passphrase?: string;
710 | cert?: any;
711 | ca?: any;
712 | crl?: any;
713 | ciphers?: string;
714 | honorCipherOrder?: boolean;
715 | requestCert?: boolean;
716 | rejectUnauthorized?: boolean;
717 | NPNProtocols?: any;
718 | SNICallback?: (servername: string) => any;
719 | }
720 |
721 | export interface RequestOptions {
722 | host?: string;
723 | hostname?: string;
724 | port?: number;
725 | path?: string;
726 | method?: string;
727 | headers?: any;
728 | auth?: string;
729 | agent?: any;
730 | pfx?: any;
731 | key?: any;
732 | passphrase?: string;
733 | cert?: any;
734 | ca?: any;
735 | ciphers?: string;
736 | rejectUnauthorized?: boolean;
737 | }
738 |
739 | export interface Agent {
740 | maxSockets: number;
741 | sockets: any;
742 | requests: any;
743 | }
744 | export var Agent: {
745 | new (options?: RequestOptions): Agent;
746 | };
747 | export interface Server extends tls.Server { }
748 | export function createServer(options: ServerOptions, requestListener?: Function): Server;
749 | export function request(options: RequestOptions, callback?: (res: http.IncomingMessage) =>void ): http.ClientRequest;
750 | export function get(options: RequestOptions, callback?: (res: http.IncomingMessage) =>void ): http.ClientRequest;
751 | export var globalAgent: Agent;
752 | }
753 |
754 | declare module "punycode" {
755 | export function decode(string: string): string;
756 | export function encode(string: string): string;
757 | export function toUnicode(domain: string): string;
758 | export function toASCII(domain: string): string;
759 | export var ucs2: ucs2;
760 | interface ucs2 {
761 | decode(string: string): number[];
762 | encode(codePoints: number[]): string;
763 | }
764 | export var version: any;
765 | }
766 |
767 | declare module "repl" {
768 | import * as stream from "stream";
769 | import * as events from "events";
770 |
771 | export interface ReplOptions {
772 | prompt?: string;
773 | input?: NodeJS.ReadableStream;
774 | output?: NodeJS.WritableStream;
775 | terminal?: boolean;
776 | eval?: Function;
777 | useColors?: boolean;
778 | useGlobal?: boolean;
779 | ignoreUndefined?: boolean;
780 | writer?: Function;
781 | }
782 | export function start(options: ReplOptions): events.EventEmitter;
783 | }
784 |
785 | declare module "readline" {
786 | import * as events from "events";
787 | import * as stream from "stream";
788 |
789 | export interface ReadLine extends events.EventEmitter {
790 | setPrompt(prompt: string): void;
791 | prompt(preserveCursor?: boolean): void;
792 | question(query: string, callback: Function): void;
793 | pause(): void;
794 | resume(): void;
795 | close(): void;
796 | write(data: any, key?: any): void;
797 | }
798 | export interface ReadLineOptions {
799 | input: NodeJS.ReadableStream;
800 | output: NodeJS.WritableStream;
801 | completer?: Function;
802 | terminal?: boolean;
803 | }
804 | export function createInterface(options: ReadLineOptions): ReadLine;
805 | }
806 |
807 | declare module "vm" {
808 | export interface Context { }
809 | export interface Script {
810 | runInThisContext(): void;
811 | runInNewContext(sandbox?: Context): void;
812 | }
813 | export function runInThisContext(code: string, filename?: string): void;
814 | export function runInNewContext(code: string, sandbox?: Context, filename?: string): void;
815 | export function runInContext(code: string, context: Context, filename?: string): void;
816 | export function createContext(initSandbox?: Context): Context;
817 | export function createScript(code: string, filename?: string): Script;
818 | }
819 |
820 | declare module "child_process" {
821 | import * as events from "events";
822 | import * as stream from "stream";
823 |
824 | export interface ChildProcess extends events.EventEmitter {
825 | stdin: stream.Writable;
826 | stdout: stream.Readable;
827 | stderr: stream.Readable;
828 | pid: number;
829 | kill(signal?: string): void;
830 | send(message: any, sendHandle?: any): void;
831 | disconnect(): void;
832 | unref(): void;
833 | }
834 |
835 | export function spawn(command: string, args?: string[], options?: {
836 | cwd?: string;
837 | stdio?: any;
838 | custom?: any;
839 | env?: any;
840 | detached?: boolean;
841 | }): ChildProcess;
842 | export function exec(command: string, options: {
843 | cwd?: string;
844 | stdio?: any;
845 | customFds?: any;
846 | env?: any;
847 | encoding?: string;
848 | timeout?: number;
849 | maxBuffer?: number;
850 | killSignal?: string;
851 | }, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
852 | export function exec(command: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
853 | export function execFile(file: string,
854 | callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
855 | export function execFile(file: string, args?: string[],
856 | callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
857 | export function execFile(file: string, args?: string[], options?: {
858 | cwd?: string;
859 | stdio?: any;
860 | customFds?: any;
861 | env?: any;
862 | encoding?: string;
863 | timeout?: number;
864 | maxBuffer?: number;
865 | killSignal?: string;
866 | }, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
867 | export function fork(modulePath: string, args?: string[], options?: {
868 | cwd?: string;
869 | env?: any;
870 | encoding?: string;
871 | }): ChildProcess;
872 | export function spawnSync(command: string, args?: string[], options?: {
873 | cwd?: string;
874 | input?: string | Buffer;
875 | stdio?: any;
876 | env?: any;
877 | uid?: number;
878 | gid?: number;
879 | timeout?: number;
880 | maxBuffer?: number;
881 | killSignal?: string;
882 | encoding?: string;
883 | }): {
884 | pid: number;
885 | output: string[];
886 | stdout: string | Buffer;
887 | stderr: string | Buffer;
888 | status: number;
889 | signal: string;
890 | error: Error;
891 | };
892 | export function execSync(command: string, options?: {
893 | cwd?: string;
894 | input?: string|Buffer;
895 | stdio?: any;
896 | env?: any;
897 | uid?: number;
898 | gid?: number;
899 | timeout?: number;
900 | maxBuffer?: number;
901 | killSignal?: string;
902 | encoding?: string;
903 | }): string | Buffer;
904 | export function execFileSync(command: string, args?: string[], options?: {
905 | cwd?: string;
906 | input?: string|Buffer;
907 | stdio?: any;
908 | env?: any;
909 | uid?: number;
910 | gid?: number;
911 | timeout?: number;
912 | maxBuffer?: number;
913 | killSignal?: string;
914 | encoding?: string;
915 | }): string | Buffer;
916 | }
917 |
918 | declare module "url" {
919 | export interface Url {
920 | href?: string;
921 | protocol?: string;
922 | auth?: string;
923 | hostname?: string;
924 | port?: string;
925 | host?: string;
926 | pathname?: string;
927 | search?: string;
928 | query?: any; // string | Object
929 | slashes?: boolean;
930 | hash?: string;
931 | path?: string;
932 | }
933 |
934 | export function parse(urlStr: string, parseQueryString?: boolean , slashesDenoteHost?: boolean ): Url;
935 | export function format(url: Url): string;
936 | export function resolve(from: string, to: string): string;
937 | }
938 |
939 | declare module "dns" {
940 | export function lookup(domain: string, family: number, callback: (err: Error, address: string, family: number) =>void ): string;
941 | export function lookup(domain: string, callback: (err: Error, address: string, family: number) =>void ): string;
942 | export function resolve(domain: string, rrtype: string, callback: (err: Error, addresses: string[]) =>void ): string[];
943 | export function resolve(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[];
944 | export function resolve4(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[];
945 | export function resolve6(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[];
946 | export function resolveMx(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[];
947 | export function resolveTxt(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[];
948 | export function resolveSrv(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[];
949 | export function resolveNs(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[];
950 | export function resolveCname(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[];
951 | export function reverse(ip: string, callback: (err: Error, domains: string[]) =>void ): string[];
952 | }
953 |
954 | declare module "net" {
955 | import * as stream from "stream";
956 |
957 | export interface Socket extends stream.Duplex {
958 | // Extended base methods
959 | write(buffer: Buffer): boolean;
960 | write(buffer: Buffer, cb?: Function): boolean;
961 | write(str: string, cb?: Function): boolean;
962 | write(str: string, encoding?: string, cb?: Function): boolean;
963 | write(str: string, encoding?: string, fd?: string): boolean;
964 |
965 | connect(port: number, host?: string, connectionListener?: Function): void;
966 | connect(path: string, connectionListener?: Function): void;
967 | bufferSize: number;
968 | setEncoding(encoding?: string): void;
969 | write(data: any, encoding?: string, callback?: Function): void;
970 | destroy(): void;
971 | pause(): void;
972 | resume(): void;
973 | setTimeout(timeout: number, callback?: Function): void;
974 | setNoDelay(noDelay?: boolean): void;
975 | setKeepAlive(enable?: boolean, initialDelay?: number): void;
976 | address(): { port: number; family: string; address: string; };
977 | unref(): void;
978 | ref(): void;
979 |
980 | remoteAddress: string;
981 | remoteFamily: string;
982 | remotePort: number;
983 | localAddress: string;
984 | localPort: number;
985 | bytesRead: number;
986 | bytesWritten: number;
987 |
988 | // Extended base methods
989 | end(): void;
990 | end(buffer: Buffer, cb?: Function): void;
991 | end(str: string, cb?: Function): void;
992 | end(str: string, encoding?: string, cb?: Function): void;
993 | end(data?: any, encoding?: string): void;
994 | }
995 |
996 | export var Socket: {
997 | new (options?: { fd?: string; type?: string; allowHalfOpen?: boolean; }): Socket;
998 | };
999 |
1000 | export interface Server extends Socket {
1001 | listen(port: number, host?: string, backlog?: number, listeningListener?: Function): Server;
1002 | listen(path: string, listeningListener?: Function): Server;
1003 | listen(handle: any, listeningListener?: Function): Server;
1004 | close(callback?: Function): Server;
1005 | address(): { port: number; family: string; address: string; };
1006 | maxConnections: number;
1007 | connections: number;
1008 | }
1009 | export function createServer(connectionListener?: (socket: Socket) =>void ): Server;
1010 | export function createServer(options?: { allowHalfOpen?: boolean; }, connectionListener?: (socket: Socket) =>void ): Server;
1011 | export function connect(options: { port: number, host?: string, localAddress? : string, localPort? : string, family? : number, allowHalfOpen?: boolean; }, connectionListener?: Function): Socket;
1012 | export function connect(port: number, host?: string, connectionListener?: Function): Socket;
1013 | export function connect(path: string, connectionListener?: Function): Socket;
1014 | export function createConnection(options: { port: number, host?: string, localAddress? : string, localPort? : string, family? : number, allowHalfOpen?: boolean; }, connectionListener?: Function): Socket;
1015 | export function createConnection(port: number, host?: string, connectionListener?: Function): Socket;
1016 | export function createConnection(path: string, connectionListener?: Function): Socket;
1017 | export function isIP(input: string): number;
1018 | export function isIPv4(input: string): boolean;
1019 | export function isIPv6(input: string): boolean;
1020 | }
1021 |
1022 | declare module "dgram" {
1023 | import * as events from "events";
1024 |
1025 | interface RemoteInfo {
1026 | address: string;
1027 | port: number;
1028 | size: number;
1029 | }
1030 |
1031 | interface AddressInfo {
1032 | address: string;
1033 | family: string;
1034 | port: number;
1035 | }
1036 |
1037 | export function createSocket(type: string, callback?: (msg: Buffer, rinfo: RemoteInfo) => void): Socket;
1038 |
1039 | interface Socket extends events.EventEmitter {
1040 | send(buf: Buffer, offset: number, length: number, port: number, address: string, callback?: (error: Error, bytes: number) => void): void;
1041 | bind(port: number, address?: string, callback?: () => void): void;
1042 | close(): void;
1043 | address(): AddressInfo;
1044 | setBroadcast(flag: boolean): void;
1045 | setMulticastTTL(ttl: number): void;
1046 | setMulticastLoopback(flag: boolean): void;
1047 | addMembership(multicastAddress: string, multicastInterface?: string): void;
1048 | dropMembership(multicastAddress: string, multicastInterface?: string): void;
1049 | }
1050 | }
1051 |
1052 | declare module "fs" {
1053 | import * as stream from "stream";
1054 | import * as events from "events";
1055 |
1056 | interface Stats {
1057 | isFile(): boolean;
1058 | isDirectory(): boolean;
1059 | isBlockDevice(): boolean;
1060 | isCharacterDevice(): boolean;
1061 | isSymbolicLink(): boolean;
1062 | isFIFO(): boolean;
1063 | isSocket(): boolean;
1064 | dev: number;
1065 | ino: number;
1066 | mode: number;
1067 | nlink: number;
1068 | uid: number;
1069 | gid: number;
1070 | rdev: number;
1071 | size: number;
1072 | blksize: number;
1073 | blocks: number;
1074 | atime: Date;
1075 | mtime: Date;
1076 | ctime: Date;
1077 | birthtime: Date;
1078 | }
1079 |
1080 | interface FSWatcher extends events.EventEmitter {
1081 | close(): void;
1082 | }
1083 |
1084 | export interface ReadStream extends stream.Readable {
1085 | close(): void;
1086 | }
1087 | export interface WriteStream extends stream.Writable {
1088 | close(): void;
1089 | bytesWritten: number;
1090 | }
1091 |
1092 | /**
1093 | * Asynchronous rename.
1094 | * @param oldPath
1095 | * @param newPath
1096 | * @param callback No arguments other than a possible exception are given to the completion callback.
1097 | */
1098 | export function rename(oldPath: string, newPath: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1099 | /**
1100 | * Synchronous rename
1101 | * @param oldPath
1102 | * @param newPath
1103 | */
1104 | export function renameSync(oldPath: string, newPath: string): void;
1105 | export function truncate(path: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1106 | export function truncate(path: string, len: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1107 | export function truncateSync(path: string, len?: number): void;
1108 | export function ftruncate(fd: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1109 | export function ftruncate(fd: number, len: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1110 | export function ftruncateSync(fd: number, len?: number): void;
1111 | export function chown(path: string, uid: number, gid: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1112 | export function chownSync(path: string, uid: number, gid: number): void;
1113 | export function fchown(fd: number, uid: number, gid: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1114 | export function fchownSync(fd: number, uid: number, gid: number): void;
1115 | export function lchown(path: string, uid: number, gid: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1116 | export function lchownSync(path: string, uid: number, gid: number): void;
1117 | export function chmod(path: string, mode: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1118 | export function chmod(path: string, mode: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1119 | export function chmodSync(path: string, mode: number): void;
1120 | export function chmodSync(path: string, mode: string): void;
1121 | export function fchmod(fd: number, mode: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1122 | export function fchmod(fd: number, mode: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1123 | export function fchmodSync(fd: number, mode: number): void;
1124 | export function fchmodSync(fd: number, mode: string): void;
1125 | export function lchmod(path: string, mode: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1126 | export function lchmod(path: string, mode: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1127 | export function lchmodSync(path: string, mode: number): void;
1128 | export function lchmodSync(path: string, mode: string): void;
1129 | export function stat(path: string, callback?: (err: NodeJS.ErrnoException, stats: Stats) => any): void;
1130 | export function lstat(path: string, callback?: (err: NodeJS.ErrnoException, stats: Stats) => any): void;
1131 | export function fstat(fd: number, callback?: (err: NodeJS.ErrnoException, stats: Stats) => any): void;
1132 | export function statSync(path: string): Stats;
1133 | export function lstatSync(path: string): Stats;
1134 | export function fstatSync(fd: number): Stats;
1135 | export function link(srcpath: string, dstpath: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1136 | export function linkSync(srcpath: string, dstpath: string): void;
1137 | export function symlink(srcpath: string, dstpath: string, type?: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1138 | export function symlinkSync(srcpath: string, dstpath: string, type?: string): void;
1139 | export function readlink(path: string, callback?: (err: NodeJS.ErrnoException, linkString: string) => any): void;
1140 | export function readlinkSync(path: string): string;
1141 | export function realpath(path: string, callback?: (err: NodeJS.ErrnoException, resolvedPath: string) => any): void;
1142 | export function realpath(path: string, cache: {[path: string]: string}, callback: (err: NodeJS.ErrnoException, resolvedPath: string) =>any): void;
1143 | export function realpathSync(path: string, cache?: { [path: string]: string }): string;
1144 | /*
1145 | * Asynchronous unlink - deletes the file specified in {path}
1146 | *
1147 | * @param path
1148 | * @param callback No arguments other than a possible exception are given to the completion callback.
1149 | */
1150 | export function unlink(path: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1151 | /*
1152 | * Synchronous unlink - deletes the file specified in {path}
1153 | *
1154 | * @param path
1155 | */
1156 | export function unlinkSync(path: string): void;
1157 | /*
1158 | * Asynchronous rmdir - removes the directory specified in {path}
1159 | *
1160 | * @param path
1161 | * @param callback No arguments other than a possible exception are given to the completion callback.
1162 | */
1163 | export function rmdir(path: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1164 | /*
1165 | * Synchronous rmdir - removes the directory specified in {path}
1166 | *
1167 | * @param path
1168 | */
1169 | export function rmdirSync(path: string): void;
1170 | /*
1171 | * Asynchronous mkdir - creates the directory specified in {path}. Parameter {mode} defaults to 0777.
1172 | *
1173 | * @param path
1174 | * @param callback No arguments other than a possible exception are given to the completion callback.
1175 | */
1176 | export function mkdir(path: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1177 | /*
1178 | * Asynchronous mkdir - creates the directory specified in {path}. Parameter {mode} defaults to 0777.
1179 | *
1180 | * @param path
1181 | * @param mode
1182 | * @param callback No arguments other than a possible exception are given to the completion callback.
1183 | */
1184 | export function mkdir(path: string, mode: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1185 | /*
1186 | * Asynchronous mkdir - creates the directory specified in {path}. Parameter {mode} defaults to 0777.
1187 | *
1188 | * @param path
1189 | * @param mode
1190 | * @param callback No arguments other than a possible exception are given to the completion callback.
1191 | */
1192 | export function mkdir(path: string, mode: string, callback?: (err?: NodeJS.ErrnoException) => void): void;
1193 | /*
1194 | * Synchronous mkdir - creates the directory specified in {path}. Parameter {mode} defaults to 0777.
1195 | *
1196 | * @param path
1197 | * @param mode
1198 | * @param callback No arguments other than a possible exception are given to the completion callback.
1199 | */
1200 | export function mkdirSync(path: string, mode?: number): void;
1201 | /*
1202 | * Synchronous mkdir - creates the directory specified in {path}. Parameter {mode} defaults to 0777.
1203 | *
1204 | * @param path
1205 | * @param mode
1206 | * @param callback No arguments other than a possible exception are given to the completion callback.
1207 | */
1208 | export function mkdirSync(path: string, mode?: string): void;
1209 | export function readdir(path: string, callback?: (err: NodeJS.ErrnoException, files: string[]) => void): void;
1210 | export function readdirSync(path: string): string[];
1211 | export function close(fd: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1212 | export function closeSync(fd: number): void;
1213 | export function open(path: string, flags: string, callback?: (err: NodeJS.ErrnoException, fd: number) => any): void;
1214 | export function open(path: string, flags: string, mode: number, callback?: (err: NodeJS.ErrnoException, fd: number) => any): void;
1215 | export function open(path: string, flags: string, mode: string, callback?: (err: NodeJS.ErrnoException, fd: number) => any): void;
1216 | export function openSync(path: string, flags: string, mode?: number): number;
1217 | export function openSync(path: string, flags: string, mode?: string): number;
1218 | export function utimes(path: string, atime: number, mtime: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1219 | export function utimes(path: string, atime: Date, mtime: Date, callback?: (err?: NodeJS.ErrnoException) => void): void;
1220 | export function utimesSync(path: string, atime: number, mtime: number): void;
1221 | export function utimesSync(path: string, atime: Date, mtime: Date): void;
1222 | export function futimes(fd: number, atime: number, mtime: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1223 | export function futimes(fd: number, atime: Date, mtime: Date, callback?: (err?: NodeJS.ErrnoException) => void): void;
1224 | export function futimesSync(fd: number, atime: number, mtime: number): void;
1225 | export function futimesSync(fd: number, atime: Date, mtime: Date): void;
1226 | export function fsync(fd: number, callback?: (err?: NodeJS.ErrnoException) => void): void;
1227 | export function fsyncSync(fd: number): void;
1228 | export function write(fd: number, buffer: Buffer, offset: number, length: number, position: number, callback?: (err: NodeJS.ErrnoException, written: number, buffer: Buffer) => void): void;
1229 | export function write(fd: number, buffer: Buffer, offset: number, length: number, callback?: (err: NodeJS.ErrnoException, written: number, buffer: Buffer) => void): void;
1230 | export function write(fd: number, data: any, callback?: (err: NodeJS.ErrnoException, written: number, str: string) => void): void;
1231 | export function write(fd: number, data: any, offset: number, callback?: (err: NodeJS.ErrnoException, written: number, str: string) => void): void;
1232 | export function write(fd: number, data: any, offset: number, encoding: string, callback?: (err: NodeJS.ErrnoException, written: number, str: string) => void): void;
1233 | export function writeSync(fd: number, buffer: Buffer, offset: number, length: number, position: number): number;
1234 | export function read(fd: number, buffer: Buffer, offset: number, length: number, position: number, callback?: (err: NodeJS.ErrnoException, bytesRead: number, buffer: Buffer) => void): void;
1235 | export function readSync(fd: number, buffer: Buffer, offset: number, length: number, position: number): number;
1236 | /*
1237 | * Asynchronous readFile - Asynchronously reads the entire contents of a file.
1238 | *
1239 | * @param fileName
1240 | * @param encoding
1241 | * @param callback - The callback is passed two arguments (err, data), where data is the contents of the file.
1242 | */
1243 | export function readFile(filename: string, encoding: string, callback: (err: NodeJS.ErrnoException, data: string) => void): void;
1244 | /*
1245 | * Asynchronous readFile - Asynchronously reads the entire contents of a file.
1246 | *
1247 | * @param fileName
1248 | * @param options An object with optional {encoding} and {flag} properties. If {encoding} is specified, readFile returns a string; otherwise it returns a Buffer.
1249 | * @param callback - The callback is passed two arguments (err, data), where data is the contents of the file.
1250 | */
1251 | export function readFile(filename: string, options: { encoding: string; flag?: string; }, callback: (err: NodeJS.ErrnoException, data: string) => void): void;
1252 | /*
1253 | * Asynchronous readFile - Asynchronously reads the entire contents of a file.
1254 | *
1255 | * @param fileName
1256 | * @param options An object with optional {encoding} and {flag} properties. If {encoding} is specified, readFile returns a string; otherwise it returns a Buffer.
1257 | * @param callback - The callback is passed two arguments (err, data), where data is the contents of the file.
1258 | */
1259 | export function readFile(filename: string, options: { flag?: string; }, callback: (err: NodeJS.ErrnoException, data: Buffer) => void): void;
1260 | /*
1261 | * Asynchronous readFile - Asynchronously reads the entire contents of a file.
1262 | *
1263 | * @param fileName
1264 | * @param callback - The callback is passed two arguments (err, data), where data is the contents of the file.
1265 | */
1266 | export function readFile(filename: string, callback: (err: NodeJS.ErrnoException, data: Buffer) => void): void;
1267 | /*
1268 | * Synchronous readFile - Synchronously reads the entire contents of a file.
1269 | *
1270 | * @param fileName
1271 | * @param encoding
1272 | */
1273 | export function readFileSync(filename: string, encoding: string): string;
1274 | /*
1275 | * Synchronous readFile - Synchronously reads the entire contents of a file.
1276 | *
1277 | * @param fileName
1278 | * @param options An object with optional {encoding} and {flag} properties. If {encoding} is specified, readFileSync returns a string; otherwise it returns a Buffer.
1279 | */
1280 | export function readFileSync(filename: string, options: { encoding: string; flag?: string; }): string;
1281 | /*
1282 | * Synchronous readFile - Synchronously reads the entire contents of a file.
1283 | *
1284 | * @param fileName
1285 | * @param options An object with optional {encoding} and {flag} properties. If {encoding} is specified, readFileSync returns a string; otherwise it returns a Buffer.
1286 | */
1287 | export function readFileSync(filename: string, options?: { flag?: string; }): Buffer;
1288 | export function writeFile(filename: string, data: any, callback?: (err: NodeJS.ErrnoException) => void): void;
1289 | export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: number; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
1290 | export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: string; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
1291 | export function writeFileSync(filename: string, data: any, options?: { encoding?: string; mode?: number; flag?: string; }): void;
1292 | export function writeFileSync(filename: string, data: any, options?: { encoding?: string; mode?: string; flag?: string; }): void;
1293 | export function appendFile(filename: string, data: any, options: { encoding?: string; mode?: number; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
1294 | export function appendFile(filename: string, data: any, options: { encoding?: string; mode?: string; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
1295 | export function appendFile(filename: string, data: any, callback?: (err: NodeJS.ErrnoException) => void): void;
1296 | export function appendFileSync(filename: string, data: any, options?: { encoding?: string; mode?: number; flag?: string; }): void;
1297 | export function appendFileSync(filename: string, data: any, options?: { encoding?: string; mode?: string; flag?: string; }): void;
1298 | export function watchFile(filename: string, listener: (curr: Stats, prev: Stats) => void): void;
1299 | export function watchFile(filename: string, options: { persistent?: boolean; interval?: number; }, listener: (curr: Stats, prev: Stats) => void): void;
1300 | export function unwatchFile(filename: string, listener?: (curr: Stats, prev: Stats) => void): void;
1301 | export function watch(filename: string, listener?: (event: string, filename: string) => any): FSWatcher;
1302 | export function watch(filename: string, options: { persistent?: boolean; }, listener?: (event: string, filename: string) => any): FSWatcher;
1303 | export function exists(path: string, callback?: (exists: boolean) => void): void;
1304 | export function existsSync(path: string): boolean;
1305 | /** Constant for fs.access(). File is visible to the calling process. */
1306 | export var F_OK: number;
1307 | /** Constant for fs.access(). File can be read by the calling process. */
1308 | export var R_OK: number;
1309 | /** Constant for fs.access(). File can be written by the calling process. */
1310 | export var W_OK: number;
1311 | /** Constant for fs.access(). File can be executed by the calling process. */
1312 | export var X_OK: number;
1313 | /** Tests a user's permissions for the file specified by path. */
1314 | export function access(path: string, callback: (err: NodeJS.ErrnoException) => void): void;
1315 | export function access(path: string, mode: number, callback: (err: NodeJS.ErrnoException) => void): void;
1316 | /** Synchronous version of fs.access. This throws if any accessibility checks fail, and does nothing otherwise. */
1317 | export function accessSync(path: string, mode ?: number): void;
1318 | export function createReadStream(path: string, options?: {
1319 | flags?: string;
1320 | encoding?: string;
1321 | fd?: number;
1322 | mode?: number;
1323 | autoClose?: boolean;
1324 | }): ReadStream;
1325 | export function createWriteStream(path: string, options?: {
1326 | flags?: string;
1327 | encoding?: string;
1328 | fd?: number;
1329 | mode?: number;
1330 | }): WriteStream;
1331 | }
1332 |
1333 | declare module "path" {
1334 |
1335 | /**
1336 | * A parsed path object generated by path.parse() or consumed by path.format().
1337 | */
1338 | export interface ParsedPath {
1339 | /**
1340 | * The root of the path such as '/' or 'c:\'
1341 | */
1342 | root: string;
1343 | /**
1344 | * The full directory path such as '/home/user/dir' or 'c:\path\dir'
1345 | */
1346 | dir: string;
1347 | /**
1348 | * The file name including extension (if any) such as 'index.html'
1349 | */
1350 | base: string;
1351 | /**
1352 | * The file extension (if any) such as '.html'
1353 | */
1354 | ext: string;
1355 | /**
1356 | * The file name without extension (if any) such as 'index'
1357 | */
1358 | name: string;
1359 | }
1360 |
1361 | /**
1362 | * Normalize a string path, reducing '..' and '.' parts.
1363 | * When multiple slashes are found, they're replaced by a single one; when the path contains a trailing slash, it is preserved. On Windows backslashes are used.
1364 | *
1365 | * @param p string path to normalize.
1366 | */
1367 | export function normalize(p: string): string;
1368 | /**
1369 | * Join all arguments together and normalize the resulting path.
1370 | * Arguments must be strings. In v0.8, non-string arguments were silently ignored. In v0.10 and up, an exception is thrown.
1371 | *
1372 | * @param paths string paths to join.
1373 | */
1374 | export function join(...paths: any[]): string;
1375 | /**
1376 | * Join all arguments together and normalize the resulting path.
1377 | * Arguments must be strings. In v0.8, non-string arguments were silently ignored. In v0.10 and up, an exception is thrown.
1378 | *
1379 | * @param paths string paths to join.
1380 | */
1381 | export function join(...paths: string[]): string;
1382 | /**
1383 | * The right-most parameter is considered {to}. Other parameters are considered an array of {from}.
1384 | *
1385 | * Starting from leftmost {from} paramter, resolves {to} to an absolute path.
1386 | *
1387 | * If {to} isn't already absolute, {from} arguments are prepended in right to left order, until an absolute path is found. If after using all {from} paths still no absolute path is found, the current working directory is used as well. The resulting path is normalized, and trailing slashes are removed unless the path gets resolved to the root directory.
1388 | *
1389 | * @param pathSegments string paths to join. Non-string arguments are ignored.
1390 | */
1391 | export function resolve(...pathSegments: any[]): string;
1392 | /**
1393 | * Determines whether {path} is an absolute path. An absolute path will always resolve to the same location, regardless of the working directory.
1394 | *
1395 | * @param path path to test.
1396 | */
1397 | export function isAbsolute(path: string): boolean;
1398 | /**
1399 | * Solve the relative path from {from} to {to}.
1400 | * At times we have two absolute paths, and we need to derive the relative path from one to the other. This is actually the reverse transform of path.resolve.
1401 | *
1402 | * @param from
1403 | * @param to
1404 | */
1405 | export function relative(from: string, to: string): string;
1406 | /**
1407 | * Return the directory name of a path. Similar to the Unix dirname command.
1408 | *
1409 | * @param p the path to evaluate.
1410 | */
1411 | export function dirname(p: string): string;
1412 | /**
1413 | * Return the last portion of a path. Similar to the Unix basename command.
1414 | * Often used to extract the file name from a fully qualified path.
1415 | *
1416 | * @param p the path to evaluate.
1417 | * @param ext optionally, an extension to remove from the result.
1418 | */
1419 | export function basename(p: string, ext?: string): string;
1420 | /**
1421 | * Return the extension of the path, from the last '.' to end of string in the last portion of the path.
1422 | * If there is no '.' in the last portion of the path or the first character of it is '.', then it returns an empty string
1423 | *
1424 | * @param p the path to evaluate.
1425 | */
1426 | export function extname(p: string): string;
1427 | /**
1428 | * The platform-specific file separator. '\\' or '/'.
1429 | */
1430 | export var sep: string;
1431 | /**
1432 | * The platform-specific file delimiter. ';' or ':'.
1433 | */
1434 | export var delimiter: string;
1435 | /**
1436 | * Returns an object from a path string - the opposite of format().
1437 | *
1438 | * @param pathString path to evaluate.
1439 | */
1440 | export function parse(pathString: string): ParsedPath;
1441 | /**
1442 | * Returns a path string from an object - the opposite of parse().
1443 | *
1444 | * @param pathString path to evaluate.
1445 | */
1446 | export function format(pathObject: ParsedPath): string;
1447 |
1448 | export module posix {
1449 | export function normalize(p: string): string;
1450 | export function join(...paths: any[]): string;
1451 | export function resolve(...pathSegments: any[]): string;
1452 | export function isAbsolute(p: string): boolean;
1453 | export function relative(from: string, to: string): string;
1454 | export function dirname(p: string): string;
1455 | export function basename(p: string, ext?: string): string;
1456 | export function extname(p: string): string;
1457 | export var sep: string;
1458 | export var delimiter: string;
1459 | export function parse(p: string): ParsedPath;
1460 | export function format(pP: ParsedPath): string;
1461 | }
1462 |
1463 | export module win32 {
1464 | export function normalize(p: string): string;
1465 | export function join(...paths: any[]): string;
1466 | export function resolve(...pathSegments: any[]): string;
1467 | export function isAbsolute(p: string): boolean;
1468 | export function relative(from: string, to: string): string;
1469 | export function dirname(p: string): string;
1470 | export function basename(p: string, ext?: string): string;
1471 | export function extname(p: string): string;
1472 | export var sep: string;
1473 | export var delimiter: string;
1474 | export function parse(p: string): ParsedPath;
1475 | export function format(pP: ParsedPath): string;
1476 | }
1477 | }
1478 |
1479 | declare module "string_decoder" {
1480 | export interface NodeStringDecoder {
1481 | write(buffer: Buffer): string;
1482 | detectIncompleteChar(buffer: Buffer): number;
1483 | }
1484 | export var StringDecoder: {
1485 | new (encoding: string): NodeStringDecoder;
1486 | };
1487 | }
1488 |
1489 | declare module "tls" {
1490 | import * as crypto from "crypto";
1491 | import * as net from "net";
1492 | import * as stream from "stream";
1493 |
1494 | var CLIENT_RENEG_LIMIT: number;
1495 | var CLIENT_RENEG_WINDOW: number;
1496 |
1497 | export interface TlsOptions {
1498 | pfx?: any; //string or buffer
1499 | key?: any; //string or buffer
1500 | passphrase?: string;
1501 | cert?: any;
1502 | ca?: any; //string or buffer
1503 | crl?: any; //string or string array
1504 | ciphers?: string;
1505 | honorCipherOrder?: any;
1506 | requestCert?: boolean;
1507 | rejectUnauthorized?: boolean;
1508 | NPNProtocols?: any; //array or Buffer;
1509 | SNICallback?: (servername: string) => any;
1510 | }
1511 |
1512 | export interface ConnectionOptions {
1513 | host?: string;
1514 | port?: number;
1515 | socket?: net.Socket;
1516 | pfx?: any; //string | Buffer
1517 | key?: any; //string | Buffer
1518 | passphrase?: string;
1519 | cert?: any; //string | Buffer
1520 | ca?: any; //Array of string | Buffer
1521 | rejectUnauthorized?: boolean;
1522 | NPNProtocols?: any; //Array of string | Buffer
1523 | servername?: string;
1524 | }
1525 |
1526 | export interface Server extends net.Server {
1527 | // Extended base methods
1528 | listen(port: number, host?: string, backlog?: number, listeningListener?: Function): Server;
1529 | listen(path: string, listeningListener?: Function): Server;
1530 | listen(handle: any, listeningListener?: Function): Server;
1531 |
1532 | listen(port: number, host?: string, callback?: Function): Server;
1533 | close(): Server;
1534 | address(): { port: number; family: string; address: string; };
1535 | addContext(hostName: string, credentials: {
1536 | key: string;
1537 | cert: string;
1538 | ca: string;
1539 | }): void;
1540 | maxConnections: number;
1541 | connections: number;
1542 | }
1543 |
1544 | export interface ClearTextStream extends stream.Duplex {
1545 | authorized: boolean;
1546 | authorizationError: Error;
1547 | getPeerCertificate(): any;
1548 | getCipher: {
1549 | name: string;
1550 | version: string;
1551 | };
1552 | address: {
1553 | port: number;
1554 | family: string;
1555 | address: string;
1556 | };
1557 | remoteAddress: string;
1558 | remotePort: number;
1559 | }
1560 |
1561 | export interface SecurePair {
1562 | encrypted: any;
1563 | cleartext: any;
1564 | }
1565 |
1566 | export interface SecureContextOptions {
1567 | pfx?: any; //string | buffer
1568 | key?: any; //string | buffer
1569 | passphrase?: string;
1570 | cert?: any; // string | buffer
1571 | ca?: any; // string | buffer
1572 | crl?: any; // string | string[]
1573 | ciphers?: string;
1574 | honorCipherOrder?: boolean;
1575 | }
1576 |
1577 | export interface SecureContext {
1578 | context: any;
1579 | }
1580 |
1581 | export function createServer(options: TlsOptions, secureConnectionListener?: (cleartextStream: ClearTextStream) =>void ): Server;
1582 | export function connect(options: TlsOptions, secureConnectionListener?: () =>void ): ClearTextStream;
1583 | export function connect(port: number, host?: string, options?: ConnectionOptions, secureConnectListener?: () =>void ): ClearTextStream;
1584 | export function connect(port: number, options?: ConnectionOptions, secureConnectListener?: () =>void ): ClearTextStream;
1585 | export function createSecurePair(credentials?: crypto.Credentials, isServer?: boolean, requestCert?: boolean, rejectUnauthorized?: boolean): SecurePair;
1586 | export function createSecureContext(details: SecureContextOptions): SecureContext;
1587 | }
1588 |
1589 | declare module "crypto" {
1590 | export interface CredentialDetails {
1591 | pfx: string;
1592 | key: string;
1593 | passphrase: string;
1594 | cert: string;
1595 | ca: any; //string | string array
1596 | crl: any; //string | string array
1597 | ciphers: string;
1598 | }
1599 | export interface Credentials { context?: any; }
1600 | export function createCredentials(details: CredentialDetails): Credentials;
1601 | export function createHash(algorithm: string): Hash;
1602 | export function createHmac(algorithm: string, key: string): Hmac;
1603 | export function createHmac(algorithm: string, key: Buffer): Hmac;
1604 | interface Hash {
1605 | update(data: any, input_encoding?: string): Hash;
1606 | digest(encoding: 'buffer'): Buffer;
1607 | digest(encoding: string): any;
1608 | digest(): Buffer;
1609 | }
1610 | interface Hmac {
1611 | update(data: any, input_encoding?: string): Hmac;
1612 | digest(encoding: 'buffer'): Buffer;
1613 | digest(encoding: string): any;
1614 | digest(): Buffer;
1615 | }
1616 | export function createCipher(algorithm: string, password: any): Cipher;
1617 | export function createCipheriv(algorithm: string, key: any, iv: any): Cipher;
1618 | interface Cipher {
1619 | update(data: Buffer): Buffer;
1620 | update(data: string, input_encoding?: string, output_encoding?: string): string;
1621 | final(): Buffer;
1622 | final(output_encoding: string): string;
1623 | setAutoPadding(auto_padding: boolean): void;
1624 | }
1625 | export function createDecipher(algorithm: string, password: any): Decipher;
1626 | export function createDecipheriv(algorithm: string, key: any, iv: any): Decipher;
1627 | interface Decipher {
1628 | update(data: Buffer): Buffer;
1629 | update(data: string, input_encoding?: string, output_encoding?: string): string;
1630 | final(): Buffer;
1631 | final(output_encoding: string): string;
1632 | setAutoPadding(auto_padding: boolean): void;
1633 | }
1634 | export function createSign(algorithm: string): Signer;
1635 | interface Signer extends NodeJS.WritableStream {
1636 | update(data: any): void;
1637 | sign(private_key: string, output_format: string): string;
1638 | }
1639 | export function createVerify(algorith: string): Verify;
1640 | interface Verify extends NodeJS.WritableStream {
1641 | update(data: any): void;
1642 | verify(object: string, signature: string, signature_format?: string): boolean;
1643 | }
1644 | export function createDiffieHellman(prime_length: number): DiffieHellman;
1645 | export function createDiffieHellman(prime: number, encoding?: string): DiffieHellman;
1646 | interface DiffieHellman {
1647 | generateKeys(encoding?: string): string;
1648 | computeSecret(other_public_key: string, input_encoding?: string, output_encoding?: string): string;
1649 | getPrime(encoding?: string): string;
1650 | getGenerator(encoding: string): string;
1651 | getPublicKey(encoding?: string): string;
1652 | getPrivateKey(encoding?: string): string;
1653 | setPublicKey(public_key: string, encoding?: string): void;
1654 | setPrivateKey(public_key: string, encoding?: string): void;
1655 | }
1656 | export function getDiffieHellman(group_name: string): DiffieHellman;
1657 | export function pbkdf2(password: string, salt: string, iterations: number, keylen: number, callback: (err: Error, derivedKey: Buffer) => any): void;
1658 | export function pbkdf2(password: string, salt: string, iterations: number, keylen: number, digest: string, callback: (err: Error, derivedKey: Buffer) => any): void;
1659 | export function pbkdf2Sync(password: string, salt: string, iterations: number, keylen: number) : Buffer;
1660 | export function pbkdf2Sync(password: string, salt: string, iterations: number, keylen: number, digest: string) : Buffer;
1661 | export function randomBytes(size: number): Buffer;
1662 | export function randomBytes(size: number, callback: (err: Error, buf: Buffer) =>void ): void;
1663 | export function pseudoRandomBytes(size: number): Buffer;
1664 | export function pseudoRandomBytes(size: number, callback: (err: Error, buf: Buffer) =>void ): void;
1665 | }
1666 |
1667 | declare module "stream" {
1668 | import * as events from "events";
1669 |
1670 | export interface Stream extends events.EventEmitter {
1671 | pipe(destination: T, options?: { end?: boolean; }): T;
1672 | }
1673 |
1674 | export interface ReadableOptions {
1675 | highWaterMark?: number;
1676 | encoding?: string;
1677 | objectMode?: boolean;
1678 | }
1679 |
1680 | export class Readable extends events.EventEmitter implements NodeJS.ReadableStream {
1681 | readable: boolean;
1682 | constructor(opts?: ReadableOptions);
1683 | _read(size: number): void;
1684 | read(size?: number): any;
1685 | setEncoding(encoding: string): void;
1686 | pause(): void;
1687 | resume(): void;
1688 | pipe(destination: T, options?: { end?: boolean; }): T;
1689 | unpipe(destination?: T): void;
1690 | unshift(chunk: any): void;
1691 | wrap(oldStream: NodeJS.ReadableStream): NodeJS.ReadableStream;
1692 | push(chunk: any, encoding?: string): boolean;
1693 | }
1694 |
1695 | export interface WritableOptions {
1696 | highWaterMark?: number;
1697 | decodeStrings?: boolean;
1698 | objectMode?: boolean;
1699 | }
1700 |
1701 | export class Writable extends events.EventEmitter implements NodeJS.WritableStream {
1702 | writable: boolean;
1703 | constructor(opts?: WritableOptions);
1704 | _write(chunk: any, encoding: string, callback: Function): void;
1705 | write(chunk: any, cb?: Function): boolean;
1706 | write(chunk: any, encoding?: string, cb?: Function): boolean;
1707 | end(): void;
1708 | end(chunk: any, cb?: Function): void;
1709 | end(chunk: any, encoding?: string, cb?: Function): void;
1710 | }
1711 |
1712 | export interface DuplexOptions extends ReadableOptions, WritableOptions {
1713 | allowHalfOpen?: boolean;
1714 | }
1715 |
1716 | // Note: Duplex extends both Readable and Writable.
1717 | export class Duplex extends Readable implements NodeJS.ReadWriteStream {
1718 | writable: boolean;
1719 | constructor(opts?: DuplexOptions);
1720 | _write(chunk: any, encoding: string, callback: Function): void;
1721 | write(chunk: any, cb?: Function): boolean;
1722 | write(chunk: any, encoding?: string, cb?: Function): boolean;
1723 | end(): void;
1724 | end(chunk: any, cb?: Function): void;
1725 | end(chunk: any, encoding?: string, cb?: Function): void;
1726 | }
1727 |
1728 | export interface TransformOptions extends ReadableOptions, WritableOptions {}
1729 |
1730 | // Note: Transform lacks the _read and _write methods of Readable/Writable.
1731 | export class Transform extends events.EventEmitter implements NodeJS.ReadWriteStream {
1732 | readable: boolean;
1733 | writable: boolean;
1734 | constructor(opts?: TransformOptions);
1735 | _transform(chunk: any, encoding: string, callback: Function): void;
1736 | _flush(callback: Function): void;
1737 | read(size?: number): any;
1738 | setEncoding(encoding: string): void;
1739 | pause(): void;
1740 | resume(): void;
1741 | pipe(destination: T, options?: { end?: boolean; }): T;
1742 | unpipe(destination?: T): void;
1743 | unshift(chunk: any): void;
1744 | wrap(oldStream: NodeJS.ReadableStream): NodeJS.ReadableStream;
1745 | push(chunk: any, encoding?: string): boolean;
1746 | write(chunk: any, cb?: Function): boolean;
1747 | write(chunk: any, encoding?: string, cb?: Function): boolean;
1748 | end(): void;
1749 | end(chunk: any, cb?: Function): void;
1750 | end(chunk: any, encoding?: string, cb?: Function): void;
1751 | }
1752 |
1753 | export class PassThrough extends Transform {}
1754 | }
1755 |
1756 | declare module "util" {
1757 | export interface InspectOptions {
1758 | showHidden?: boolean;
1759 | depth?: number;
1760 | colors?: boolean;
1761 | customInspect?: boolean;
1762 | }
1763 |
1764 | export function format(format: any, ...param: any[]): string;
1765 | export function debug(string: string): void;
1766 | export function error(...param: any[]): void;
1767 | export function puts(...param: any[]): void;
1768 | export function print(...param: any[]): void;
1769 | export function log(string: string): void;
1770 | export function inspect(object: any, showHidden?: boolean, depth?: number, color?: boolean): string;
1771 | export function inspect(object: any, options: InspectOptions): string;
1772 | export function isArray(object: any): boolean;
1773 | export function isRegExp(object: any): boolean;
1774 | export function isDate(object: any): boolean;
1775 | export function isError(object: any): boolean;
1776 | export function inherits(constructor: any, superConstructor: any): void;
1777 | export function debuglog(key:string): (msg:string,...param: any[])=>void;
1778 | }
1779 |
1780 | declare module "assert" {
1781 | function internal (value: any, message?: string): void;
1782 | module internal {
1783 | export class AssertionError implements Error {
1784 | name: string;
1785 | message: string;
1786 | actual: any;
1787 | expected: any;
1788 | operator: string;
1789 | generatedMessage: boolean;
1790 |
1791 | constructor(options?: {message?: string; actual?: any; expected?: any;
1792 | operator?: string; stackStartFunction?: Function});
1793 | }
1794 |
1795 | export function fail(actual?: any, expected?: any, message?: string, operator?: string): void;
1796 | export function ok(value: any, message?: string): void;
1797 | export function equal(actual: any, expected: any, message?: string): void;
1798 | export function notEqual(actual: any, expected: any, message?: string): void;
1799 | export function deepEqual(actual: any, expected: any, message?: string): void;
1800 | export function notDeepEqual(acutal: any, expected: any, message?: string): void;
1801 | export function strictEqual(actual: any, expected: any, message?: string): void;
1802 | export function notStrictEqual(actual: any, expected: any, message?: string): void;
1803 | export var throws: {
1804 | (block: Function, message?: string): void;
1805 | (block: Function, error: Function, message?: string): void;
1806 | (block: Function, error: RegExp, message?: string): void;
1807 | (block: Function, error: (err: any) => boolean, message?: string): void;
1808 | };
1809 |
1810 | export var doesNotThrow: {
1811 | (block: Function, message?: string): void;
1812 | (block: Function, error: Function, message?: string): void;
1813 | (block: Function, error: RegExp, message?: string): void;
1814 | (block: Function, error: (err: any) => boolean, message?: string): void;
1815 | };
1816 |
1817 | export function ifError(value: any): void;
1818 | }
1819 |
1820 | export = internal;
1821 | }
1822 |
1823 | declare module "tty" {
1824 | import * as net from "net";
1825 |
1826 | export function isatty(fd: number): boolean;
1827 | export interface ReadStream extends net.Socket {
1828 | isRaw: boolean;
1829 | setRawMode(mode: boolean): void;
1830 | }
1831 | export interface WriteStream extends net.Socket {
1832 | columns: number;
1833 | rows: number;
1834 | }
1835 | }
1836 |
1837 | declare module "domain" {
1838 | import * as events from "events";
1839 |
1840 | export class Domain extends events.EventEmitter {
1841 | run(fn: Function): void;
1842 | add(emitter: events.EventEmitter): void;
1843 | remove(emitter: events.EventEmitter): void;
1844 | bind(cb: (err: Error, data: any) => any): any;
1845 | intercept(cb: (data: any) => any): any;
1846 | dispose(): void;
1847 |
1848 | addListener(event: string, listener: Function): Domain;
1849 | on(event: string, listener: Function): Domain;
1850 | once(event: string, listener: Function): Domain;
1851 | removeListener(event: string, listener: Function): Domain;
1852 | removeAllListeners(event?: string): Domain;
1853 | }
1854 |
1855 | export function create(): Domain;
1856 | }
1857 |
1858 | declare module "constants" {
1859 | export var E2BIG: number;
1860 | export var EACCES: number;
1861 | export var EADDRINUSE: number;
1862 | export var EADDRNOTAVAIL: number;
1863 | export var EAFNOSUPPORT: number;
1864 | export var EAGAIN: number;
1865 | export var EALREADY: number;
1866 | export var EBADF: number;
1867 | export var EBADMSG: number;
1868 | export var EBUSY: number;
1869 | export var ECANCELED: number;
1870 | export var ECHILD: number;
1871 | export var ECONNABORTED: number;
1872 | export var ECONNREFUSED: number;
1873 | export var ECONNRESET: number;
1874 | export var EDEADLK: number;
1875 | export var EDESTADDRREQ: number;
1876 | export var EDOM: number;
1877 | export var EEXIST: number;
1878 | export var EFAULT: number;
1879 | export var EFBIG: number;
1880 | export var EHOSTUNREACH: number;
1881 | export var EIDRM: number;
1882 | export var EILSEQ: number;
1883 | export var EINPROGRESS: number;
1884 | export var EINTR: number;
1885 | export var EINVAL: number;
1886 | export var EIO: number;
1887 | export var EISCONN: number;
1888 | export var EISDIR: number;
1889 | export var ELOOP: number;
1890 | export var EMFILE: number;
1891 | export var EMLINK: number;
1892 | export var EMSGSIZE: number;
1893 | export var ENAMETOOLONG: number;
1894 | export var ENETDOWN: number;
1895 | export var ENETRESET: number;
1896 | export var ENETUNREACH: number;
1897 | export var ENFILE: number;
1898 | export var ENOBUFS: number;
1899 | export var ENODATA: number;
1900 | export var ENODEV: number;
1901 | export var ENOENT: number;
1902 | export var ENOEXEC: number;
1903 | export var ENOLCK: number;
1904 | export var ENOLINK: number;
1905 | export var ENOMEM: number;
1906 | export var ENOMSG: number;
1907 | export var ENOPROTOOPT: number;
1908 | export var ENOSPC: number;
1909 | export var ENOSR: number;
1910 | export var ENOSTR: number;
1911 | export var ENOSYS: number;
1912 | export var ENOTCONN: number;
1913 | export var ENOTDIR: number;
1914 | export var ENOTEMPTY: number;
1915 | export var ENOTSOCK: number;
1916 | export var ENOTSUP: number;
1917 | export var ENOTTY: number;
1918 | export var ENXIO: number;
1919 | export var EOPNOTSUPP: number;
1920 | export var EOVERFLOW: number;
1921 | export var EPERM: number;
1922 | export var EPIPE: number;
1923 | export var EPROTO: number;
1924 | export var EPROTONOSUPPORT: number;
1925 | export var EPROTOTYPE: number;
1926 | export var ERANGE: number;
1927 | export var EROFS: number;
1928 | export var ESPIPE: number;
1929 | export var ESRCH: number;
1930 | export var ETIME: number;
1931 | export var ETIMEDOUT: number;
1932 | export var ETXTBSY: number;
1933 | export var EWOULDBLOCK: number;
1934 | export var EXDEV: number;
1935 | export var WSAEINTR: number;
1936 | export var WSAEBADF: number;
1937 | export var WSAEACCES: number;
1938 | export var WSAEFAULT: number;
1939 | export var WSAEINVAL: number;
1940 | export var WSAEMFILE: number;
1941 | export var WSAEWOULDBLOCK: number;
1942 | export var WSAEINPROGRESS: number;
1943 | export var WSAEALREADY: number;
1944 | export var WSAENOTSOCK: number;
1945 | export var WSAEDESTADDRREQ: number;
1946 | export var WSAEMSGSIZE: number;
1947 | export var WSAEPROTOTYPE: number;
1948 | export var WSAENOPROTOOPT: number;
1949 | export var WSAEPROTONOSUPPORT: number;
1950 | export var WSAESOCKTNOSUPPORT: number;
1951 | export var WSAEOPNOTSUPP: number;
1952 | export var WSAEPFNOSUPPORT: number;
1953 | export var WSAEAFNOSUPPORT: number;
1954 | export var WSAEADDRINUSE: number;
1955 | export var WSAEADDRNOTAVAIL: number;
1956 | export var WSAENETDOWN: number;
1957 | export var WSAENETUNREACH: number;
1958 | export var WSAENETRESET: number;
1959 | export var WSAECONNABORTED: number;
1960 | export var WSAECONNRESET: number;
1961 | export var WSAENOBUFS: number;
1962 | export var WSAEISCONN: number;
1963 | export var WSAENOTCONN: number;
1964 | export var WSAESHUTDOWN: number;
1965 | export var WSAETOOMANYREFS: number;
1966 | export var WSAETIMEDOUT: number;
1967 | export var WSAECONNREFUSED: number;
1968 | export var WSAELOOP: number;
1969 | export var WSAENAMETOOLONG: number;
1970 | export var WSAEHOSTDOWN: number;
1971 | export var WSAEHOSTUNREACH: number;
1972 | export var WSAENOTEMPTY: number;
1973 | export var WSAEPROCLIM: number;
1974 | export var WSAEUSERS: number;
1975 | export var WSAEDQUOT: number;
1976 | export var WSAESTALE: number;
1977 | export var WSAEREMOTE: number;
1978 | export var WSASYSNOTREADY: number;
1979 | export var WSAVERNOTSUPPORTED: number;
1980 | export var WSANOTINITIALISED: number;
1981 | export var WSAEDISCON: number;
1982 | export var WSAENOMORE: number;
1983 | export var WSAECANCELLED: number;
1984 | export var WSAEINVALIDPROCTABLE: number;
1985 | export var WSAEINVALIDPROVIDER: number;
1986 | export var WSAEPROVIDERFAILEDINIT: number;
1987 | export var WSASYSCALLFAILURE: number;
1988 | export var WSASERVICE_NOT_FOUND: number;
1989 | export var WSATYPE_NOT_FOUND: number;
1990 | export var WSA_E_NO_MORE: number;
1991 | export var WSA_E_CANCELLED: number;
1992 | export var WSAEREFUSED: number;
1993 | export var SIGHUP: number;
1994 | export var SIGINT: number;
1995 | export var SIGILL: number;
1996 | export var SIGABRT: number;
1997 | export var SIGFPE: number;
1998 | export var SIGKILL: number;
1999 | export var SIGSEGV: number;
2000 | export var SIGTERM: number;
2001 | export var SIGBREAK: number;
2002 | export var SIGWINCH: number;
2003 | export var SSL_OP_ALL: number;
2004 | export var SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION: number;
2005 | export var SSL_OP_CIPHER_SERVER_PREFERENCE: number;
2006 | export var SSL_OP_CISCO_ANYCONNECT: number;
2007 | export var SSL_OP_COOKIE_EXCHANGE: number;
2008 | export var SSL_OP_CRYPTOPRO_TLSEXT_BUG: number;
2009 | export var SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: number;
2010 | export var SSL_OP_EPHEMERAL_RSA: number;
2011 | export var SSL_OP_LEGACY_SERVER_CONNECT: number;
2012 | export var SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER: number;
2013 | export var SSL_OP_MICROSOFT_SESS_ID_BUG: number;
2014 | export var SSL_OP_MSIE_SSLV2_RSA_PADDING: number;
2015 | export var SSL_OP_NETSCAPE_CA_DN_BUG: number;
2016 | export var SSL_OP_NETSCAPE_CHALLENGE_BUG: number;
2017 | export var SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG: number;
2018 | export var SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG: number;
2019 | export var SSL_OP_NO_COMPRESSION: number;
2020 | export var SSL_OP_NO_QUERY_MTU: number;
2021 | export var SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION: number;
2022 | export var SSL_OP_NO_SSLv2: number;
2023 | export var SSL_OP_NO_SSLv3: number;
2024 | export var SSL_OP_NO_TICKET: number;
2025 | export var SSL_OP_NO_TLSv1: number;
2026 | export var SSL_OP_NO_TLSv1_1: number;
2027 | export var SSL_OP_NO_TLSv1_2: number;
2028 | export var SSL_OP_PKCS1_CHECK_1: number;
2029 | export var SSL_OP_PKCS1_CHECK_2: number;
2030 | export var SSL_OP_SINGLE_DH_USE: number;
2031 | export var SSL_OP_SINGLE_ECDH_USE: number;
2032 | export var SSL_OP_SSLEAY_080_CLIENT_DH_BUG: number;
2033 | export var SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG: number;
2034 | export var SSL_OP_TLS_BLOCK_PADDING_BUG: number;
2035 | export var SSL_OP_TLS_D5_BUG: number;
2036 | export var SSL_OP_TLS_ROLLBACK_BUG: number;
2037 | export var ENGINE_METHOD_DSA: number;
2038 | export var ENGINE_METHOD_DH: number;
2039 | export var ENGINE_METHOD_RAND: number;
2040 | export var ENGINE_METHOD_ECDH: number;
2041 | export var ENGINE_METHOD_ECDSA: number;
2042 | export var ENGINE_METHOD_CIPHERS: number;
2043 | export var ENGINE_METHOD_DIGESTS: number;
2044 | export var ENGINE_METHOD_STORE: number;
2045 | export var ENGINE_METHOD_PKEY_METHS: number;
2046 | export var ENGINE_METHOD_PKEY_ASN1_METHS: number;
2047 | export var ENGINE_METHOD_ALL: number;
2048 | export var ENGINE_METHOD_NONE: number;
2049 | export var DH_CHECK_P_NOT_SAFE_PRIME: number;
2050 | export var DH_CHECK_P_NOT_PRIME: number;
2051 | export var DH_UNABLE_TO_CHECK_GENERATOR: number;
2052 | export var DH_NOT_SUITABLE_GENERATOR: number;
2053 | export var NPN_ENABLED: number;
2054 | export var RSA_PKCS1_PADDING: number;
2055 | export var RSA_SSLV23_PADDING: number;
2056 | export var RSA_NO_PADDING: number;
2057 | export var RSA_PKCS1_OAEP_PADDING: number;
2058 | export var RSA_X931_PADDING: number;
2059 | export var RSA_PKCS1_PSS_PADDING: number;
2060 | export var POINT_CONVERSION_COMPRESSED: number;
2061 | export var POINT_CONVERSION_UNCOMPRESSED: number;
2062 | export var POINT_CONVERSION_HYBRID: number;
2063 | export var O_RDONLY: number;
2064 | export var O_WRONLY: number;
2065 | export var O_RDWR: number;
2066 | export var S_IFMT: number;
2067 | export var S_IFREG: number;
2068 | export var S_IFDIR: number;
2069 | export var S_IFCHR: number;
2070 | export var S_IFLNK: number;
2071 | export var O_CREAT: number;
2072 | export var O_EXCL: number;
2073 | export var O_TRUNC: number;
2074 | export var O_APPEND: number;
2075 | export var F_OK: number;
2076 | export var R_OK: number;
2077 | export var W_OK: number;
2078 | export var X_OK: number;
2079 | export var UV_UDP_REUSEADDR: number;
2080 | }
2081 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ts-bytearray",
3 | "version": "1.2.4",
4 | "description": "JavaScript ByteArray an implementation of ActionScript ByteArray",
5 | "main": "lib/ts-bytearray.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/Snapizz/ts-bytearray.git"
12 | },
13 | "keywords": [
14 | "js",
15 | "javascript",
16 | "bytearray",
17 | "as3",
18 | "as",
19 | "actionscript",
20 | "flash"
21 | ],
22 | "author": "Nidin Vinayakan | nidinthb@gmail.com",
23 | "license": "MIT",
24 | "bugs": {
25 | "url": "https://github.com/Snapizz/ts-bytearray/issues"
26 | },
27 | "homepage": "https://github.com/Snapizz/ts-bytearray#readme",
28 | "dependencies": {
29 | "uvrun2": "^0.3.0"
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/ts-bytearray.ts:
--------------------------------------------------------------------------------
1 | ///
2 | ///
3 |
4 | import {runOnce} from 'uvrun2';
5 | import zlib = require('zlib');
6 |
7 | class ByteArrayBase {
8 | private bitsPending: number = 0;
9 | public readBits(bits: number, bitBuffer: number = 0): number {
10 | if (bits == 0) { return bitBuffer; }
11 | var partial: number;
12 | var bitsConsumed: number;
13 | if (this.bitsPending > 0) {
14 | var _byte: number = this[this.position - 1] & (0xff >> (8 - this.bitsPending));
15 | bitsConsumed = Math.min(this.bitsPending, bits);
16 | this.bitsPending -= bitsConsumed;
17 | partial = _byte >> this.bitsPending;
18 | } else {
19 | bitsConsumed = Math.min(8, bits);
20 | this.bitsPending = 8 - bitsConsumed;
21 | partial = this.readUnsignedByte() >> this.bitsPending;
22 | }
23 | bits -= bitsConsumed;
24 | bitBuffer = (bitBuffer << bitsConsumed) | partial;
25 | return (bits > 0) ? this.readBits(bits, bitBuffer) : bitBuffer;
26 | }
27 |
28 | public writeBits(bits: number, value: number) {
29 | if (bits == 0) { return; }
30 | value &= (0xffffffff >>> (32 - bits));
31 | var bitsConsumed: number;
32 | if (this.bitsPending > 0) {
33 | if (this.bitsPending > bits) {
34 | this[this.position - 1] |= value << (this.bitsPending - bits);
35 | bitsConsumed = bits;
36 | this.bitsPending -= bits;
37 | } else if (this.bitsPending == bits) {
38 | this[this.position - 1] |= value;
39 | bitsConsumed = bits;
40 | this.bitsPending = 0;
41 | } else {
42 | this[this.position - 1] |= value >> (bits - this.bitsPending);
43 | bitsConsumed = this.bitsPending;
44 | this.bitsPending = 0;
45 | }
46 | } else {
47 | bitsConsumed = Math.min(8, bits);
48 | this.bitsPending = 8 - bitsConsumed;
49 | this.writeByte((value >> (bits - bitsConsumed)) << this.bitsPending);
50 | }
51 | bits -= bitsConsumed;
52 | if (bits > 0) {
53 | this.writeBits(bits, value);
54 | }
55 | }
56 |
57 | public resetBitsPending() {
58 | this.bitsPending = 0;
59 | }
60 |
61 | public calculateMaxBits(signed: boolean, values: Array): number {
62 | var b: number = 0;
63 | var vmax: number = -2147483648//int.MIN_VALUE;
64 | if (!signed) {
65 | for (var usvalue in values) {
66 | b |= usvalue;
67 | }
68 | } else {
69 | for (var svalue in values) {
70 | if (svalue >= 0) {
71 | b |= svalue;
72 | } else {
73 | b |= ~svalue << 1;
74 | }
75 | if (vmax < svalue) {
76 | vmax = svalue;
77 | }
78 | }
79 | }
80 | var bits: number = 0;
81 | if (b > 0) {
82 | bits = b.toString(2).length;
83 | if (signed && vmax > 0 && vmax.toString(2).length >= bits) {
84 | bits++;
85 | }
86 | }
87 | return bits;
88 | }
89 | static BIG_ENDIAN: string = "bigEndian";
90 | static LITTLE_ENDIAN: string = "littleEndian";
91 |
92 | static SIZE_OF_BOOLEAN: number = 1;
93 | static SIZE_OF_INT8: number = 1;
94 | static SIZE_OF_INT16: number = 2;
95 | static SIZE_OF_INT32: number = 4;
96 | static SIZE_OF_INT64: number = 8;
97 | static SIZE_OF_UINT8: number = 1;
98 | static SIZE_OF_UINT16: number = 2;
99 | static SIZE_OF_UINT32: number = 4;
100 | static SIZE_OF_UINT64: number = 8;
101 | static SIZE_OF_FLOAT32: number = 4;
102 | static SIZE_OF_FLOAT64: number = 8;
103 |
104 | private BUFFER_EXT_SIZE: number = 1024;//Buffer expansion size
105 |
106 | public array: Uint8Array = null;
107 | public data: DataView;
108 | private _position: number;
109 | public write_position: number;
110 | public endian: string;
111 |
112 | constructor(buffer?: ArrayBuffer, offset: number = 0, length: number = 0) {
113 |
114 | if (buffer == undefined) {
115 | buffer = new ArrayBuffer(this.BUFFER_EXT_SIZE);
116 | this.write_position = 0;
117 | }
118 | else if (buffer == null) {
119 | this.write_position = 0;
120 | } else {
121 | this.write_position = length > 0 ? length : buffer.byteLength;
122 | }
123 | if (buffer) {
124 | this.data = new DataView(buffer, offset, length > 0 ? length : buffer.byteLength);
125 | }
126 | this._position = 0;
127 | this.endian = ByteArrayBase.BIG_ENDIAN;
128 | }
129 |
130 | // getter setter
131 | get buffer(): ArrayBuffer {
132 | return this.data.buffer;
133 | }
134 | set buffer(value: ArrayBuffer) {
135 | this.data = new DataView(value);
136 | }
137 | get dataView(): DataView {
138 | return this.data;
139 | }
140 | set dataView(value: DataView) {
141 | this.data = value;
142 | this.write_position = value.byteLength;
143 | }
144 | get phyPosition(): number {
145 | return this._position + this.data.byteOffset;
146 | }
147 | get bufferOffset(): number {
148 | return this.data.byteOffset;
149 | }
150 | get position(): number {
151 | return this._position;
152 | }
153 | set position(value: number) {
154 | if (this._position < value) {
155 | if (!this.validate(this._position - value)) {
156 | return;
157 | }
158 | }
159 | this._position = value;
160 | this.write_position = value > this.write_position ? value : this.write_position;
161 | }
162 | get length(): number {
163 | return this.write_position;
164 | }
165 | set length(value: number) {
166 | this.validateBuffer(value);
167 | }
168 |
169 | get bytesAvailable(): number {
170 | return this.data.byteLength - this._position;
171 | }
172 | //end
173 | public clear(): void {
174 | this._position = 0;
175 | }
176 | public getArray(): Uint8Array {
177 | if (this.array == null) {
178 | this.array = new Uint8Array(this.data.buffer, this.data.byteOffset, this.data.byteLength);
179 | }
180 | return this.array;
181 | }
182 | public setArray(array: Uint8Array): void {
183 | this.array = array;
184 | this.setBuffer(array.buffer, array.byteOffset, array.byteLength);
185 | }
186 | public setBuffer(buffer: ArrayBuffer, offset: number = 0, length: number = 0) {
187 | if (buffer) {
188 | this.data = new DataView(buffer, offset, length > 0 ? length : buffer.byteLength);
189 | this.write_position = length > 0 ? length : buffer.byteLength;
190 | } else {
191 | this.write_position = 0;
192 | }
193 | this._position = 0;
194 | }
195 | /**
196 | * Reads a Boolean value from the byte stream. A single byte is read,
197 | * and true is returned if the byte is nonzero,
198 | * false otherwise.
199 | * @return Returns true if the byte is nonzero, false otherwise.
200 | */
201 | public readBoolean(): boolean {
202 | if (!this.validate(ByteArrayBase.SIZE_OF_BOOLEAN)) return null;
203 |
204 | return this.data.getUint8(this.position++) != 0;
205 | }
206 |
207 | /**
208 | * Reads a signed byte from the byte stream.
209 | * The returned value is in the range -128 to 127.
210 | * @return An integer between -128 and 127.
211 | */
212 | public readByte(): number {
213 | if (!this.validate(ByteArrayBase.SIZE_OF_INT8)) return null;
214 |
215 | return this.data.getInt8(this.position++);
216 | }
217 |
218 | /**
219 | * Reads the number of data bytes, specified by the length parameter, from the byte stream.
220 | * The bytes are read into the ByteArrayBase object specified by the bytes parameter,
221 | * and the bytes are written into the destination ByteArrayBase starting at the _position specified by offset.
222 | * @param bytes The ByteArrayBase object to read data into.
223 | * @param offset The offset (_position) in bytes at which the read data should be written.
224 | * @param length The number of bytes to read. The default value of 0 causes all available data to be read.
225 | */
226 | public readBytes(_bytes: ByteArrayBase = null, offset: number = 0, length: number = 0, createNewBuffer: boolean = false): ByteArrayBase {
227 | if (length == 0) {
228 | length = this.bytesAvailable;
229 | }
230 | else if (!this.validate(length)) return null;
231 |
232 | if (createNewBuffer) {
233 | _bytes = _bytes == null ? new ByteArrayBase(new ArrayBuffer(length)) : _bytes;
234 | //This method is expensive
235 | for (var i = 0; i < length; i++) {
236 | _bytes.data.setUint8(i + offset, this.data.getUint8(this.position++));
237 | }
238 | } else {
239 | //Offset argument ignored
240 | _bytes = _bytes == null ? new ByteArrayBase(null) : _bytes;
241 | _bytes.dataView = new DataView(this.data.buffer, this.bufferOffset + this.position, length);
242 | this.position += length;
243 | }
244 |
245 | return _bytes;
246 | }
247 |
248 | /**
249 | * Reads an IEEE 754 double-precision (64-bit) floating-point number from the byte stream.
250 | * @return A double-precision (64-bit) floating-point number.
251 | */
252 | public readDouble(): number {
253 | if (!this.validate(ByteArrayBase.SIZE_OF_FLOAT64)) return null;
254 |
255 | var value: number = this.data.getFloat64(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
256 | this.position += ByteArrayBase.SIZE_OF_FLOAT64;
257 | return value;
258 | }
259 |
260 | /**
261 | * Reads an IEEE 754 single-precision (32-bit) floating-point number from the byte stream.
262 | * @return A single-precision (32-bit) floating-point number.
263 | */
264 | public readFloat(): number {
265 | if (!this.validate(ByteArrayBase.SIZE_OF_FLOAT32)) return null;
266 |
267 | var value: number = this.data.getFloat32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
268 | this.position += ByteArrayBase.SIZE_OF_FLOAT32;
269 | return value;
270 | }
271 |
272 | /**
273 | * Reads a signed 32-bit integer from the byte stream.
274 | *
275 | * The returned value is in the range -2147483648 to 2147483647.
276 | * @return A 32-bit signed integer between -2147483648 and 2147483647.
277 | */
278 | public readInt(): number {
279 | if (!this.validate(ByteArrayBase.SIZE_OF_INT32)) return null;
280 |
281 | var value = this.data.getInt32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
282 | this.position += ByteArrayBase.SIZE_OF_INT32;
283 | return value;
284 | }
285 | /**
286 | * Reads a signed 64-bit integer from the byte stream.
287 | *
288 | * The returned value is in the range −(2^63) to 2^63 − 1
289 | * @return A 64-bit signed integer between −(2^63) to 2^63 − 1
290 | */
291 | public readInt64(): ByteArrayBase.Int64 {
292 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT32)) return null;
293 |
294 | var low = this.data.getInt32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
295 | this.position += ByteArrayBase.SIZE_OF_INT32;
296 | var high = this.data.getInt32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
297 | this.position += ByteArrayBase.SIZE_OF_INT32;
298 | return new ByteArrayBase.Int64(low, high);
299 | }
300 |
301 | /**
302 | * Reads a multibyte string of specified length from the byte stream using the
303 | * specified character set.
304 | * @param length The number of bytes from the byte stream to read.
305 | * @param charSet The string denoting the character set to use to interpret the bytes.
306 | * Possible character set strings include "shift-jis", "cn-gb",
307 | * "iso-8859-1", and others.
308 | * For a complete list, see Supported Character Sets.
309 | * Note: If the value for the charSet parameter
310 | * is not recognized by the current system, the application uses the system's default
311 | * code page as the character set. For example, a value for the charSet parameter,
312 | * as in myTest.readMultiByte(22, "iso-8859-01") that uses 01 instead of
313 | * 1 might work on your development system, but not on another system.
314 | * On the other system, the application will use the system's default code page.
315 | * @return UTF-8 encoded string.
316 | */
317 | public readMultiByte(length: number, charSet?: string): string {
318 | if (!this.validate(length)) return null;
319 |
320 | return "";
321 | }
322 |
323 | /**
324 | * Reads a signed 16-bit integer from the byte stream.
325 | *
326 | * The returned value is in the range -32768 to 32767.
327 | * @return A 16-bit signed integer between -32768 and 32767.
328 | */
329 | public readShort(): number {
330 | if (!this.validate(ByteArrayBase.SIZE_OF_INT16)) return null;
331 |
332 | var value = this.data.getInt16(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
333 | this.position += ByteArrayBase.SIZE_OF_INT16;
334 | return value;
335 | }
336 |
337 | /**
338 | * Reads an unsigned byte from the byte stream.
339 | *
340 | * The returned value is in the range 0 to 255.
341 | * @return A 32-bit unsigned integer between 0 and 255.
342 | */
343 | public readUnsignedByte(): number {
344 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT8)) return null;
345 |
346 | return this.data.getUint8(this.position++);
347 | }
348 |
349 | /**
350 | * Reads an unsigned 32-bit integer from the byte stream.
351 | *
352 | * The returned value is in the range 0 to 4294967295.
353 | * @return A 32-bit unsigned integer between 0 and 4294967295.
354 | */
355 | public readUnsignedInt(): number {
356 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT32)) return null;
357 |
358 | var value = this.data.getUint32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
359 | this.position += ByteArrayBase.SIZE_OF_UINT32;
360 | return value;
361 | }
362 |
363 | /**
364 | * Reads a variable sized unsigned integer (VX -> 16-bit or 32-bit) from the byte stream.
365 | *
366 | * A VX is written as a variable length 2- or 4-byte element. If the index value is less than 65,280 (0xFF00),
367 | * then the index is written as an unsigned two-byte integer. Otherwise the index is written as an unsigned
368 | * four byte integer with bits 24-31 set. When reading an index, if the first byte encountered is 255 (0xFF),
369 | * then the four-byte form is being used and the first byte should be discarded or masked out.
370 | *
371 | * The returned value is in the range 0 to 65279 or 0 to 2147483647.
372 | * @return A VX 16-bit or 32-bit unsigned integer between 0 to 65279 or 0 and 2147483647.
373 | */
374 | public readVariableSizedUnsignedInt(): number {
375 |
376 | var value: number;
377 | var c = this.readUnsignedByte();
378 | if (c != 0xFF) {
379 | value = c << 8;
380 | c = this.readUnsignedByte();
381 | value |= c;
382 | }
383 | else {
384 | c = this.readUnsignedByte();
385 | value = c << 16;
386 | c = this.readUnsignedByte();
387 | value |= c << 8;
388 | c = this.readUnsignedByte();
389 | value |= c;
390 | }
391 | return value;
392 | }
393 |
394 | /**
395 | * Fast read for WebGL since only Uint16 numbers are expected
396 | */
397 | public readU16VX(): number {
398 | return (this.readUnsignedByte() << 8) | this.readUnsignedByte();
399 | }
400 | /**
401 | * Reads an unsigned 64-bit integer from the byte stream.
402 | *
403 | * The returned value is in the range 0 to 2^64 − 1.
404 | * @return A 64-bit unsigned integer between 0 and 2^64 − 1
405 | */
406 | public readUnsignedInt64(): ByteArrayBase.UInt64 {
407 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT32)) return null;
408 |
409 | var low = this.data.getUint32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
410 | this.position += ByteArrayBase.SIZE_OF_UINT32;
411 | var high = this.data.getUint32(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
412 | this.position += ByteArrayBase.SIZE_OF_UINT32;
413 | return new ByteArrayBase.UInt64(low, high);
414 | }
415 |
416 | /**
417 | * Reads an unsigned 16-bit integer from the byte stream.
418 | *
419 | * The returned value is in the range 0 to 65535.
420 | * @return A 16-bit unsigned integer between 0 and 65535.
421 | */
422 | public readUnsignedShort(): number {
423 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT16)) return null;
424 |
425 | var value = this.data.getUint16(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
426 | this.position += ByteArrayBase.SIZE_OF_UINT16;
427 | return value;
428 | }
429 |
430 | /**
431 | * Reads a UTF-8 string from the byte stream. The string
432 | * is assumed to be prefixed with an unsigned short indicating
433 | * the length in bytes.
434 | * @return UTF-8 encoded string.
435 | */
436 | public readUTF(): string {
437 | if (!this.validate(ByteArrayBase.SIZE_OF_UINT16)) return null;
438 |
439 | var length: number = this.data.getUint16(this.position, this.endian == ByteArrayBase.LITTLE_ENDIAN);
440 | this.position += ByteArrayBase.SIZE_OF_UINT16;
441 |
442 | if (length > 0) {
443 | return this.readUTFBytes(length);
444 | } else {
445 | return "";
446 | }
447 | }
448 |
449 | /**
450 | * Reads a sequence of UTF-8 bytes specified by the length
451 | * parameter from the byte stream and returns a string.
452 | * @param length An unsigned short indicating the length of the UTF-8 bytes.
453 | * @return A string composed of the UTF-8 bytes of the specified length.
454 | */
455 | public readUTFBytes(length: number): string {
456 | if (!this.validate(length)) return null;
457 |
458 | var _bytes: Uint8Array = new Uint8Array(this.buffer, this.bufferOffset + this.position, length);
459 | this.position += length;
460 | /*var _bytes: Uint8Array = new Uint8Array(new ArrayBuffer(length));
461 | for (var i = 0; i < length; i++) {
462 | _bytes[i] = this.data.getUint8(this.position++);
463 | }*/
464 | return this.decodeUTF8(_bytes);
465 | }
466 | public readStandardString(length: number): string {
467 | if (!this.validate(length)) return null;
468 |
469 | var str: string = "";
470 |
471 | for (var i = 0; i < length; i++) {
472 | str += String.fromCharCode(this.data.getUint8(this.position++));
473 | }
474 | return str;
475 | }
476 | public readStringTillNull(keepEvenByte: boolean = true): string {
477 |
478 | var str: string = "";
479 | var num: number = 0;
480 | while (this.bytesAvailable > 0) {
481 | var _byte: number = this.data.getUint8(this.position++);
482 | num++;
483 | if (_byte != 0) {
484 | str += String.fromCharCode(_byte);
485 | } else {
486 | if (keepEvenByte && num % 2 != 0) {
487 | this.position++;
488 | }
489 | break;
490 | }
491 | }
492 | return str;
493 | }
494 |
495 | /**
496 | * Writes a Boolean value. A single byte is written according to the value parameter,
497 | * either 1 if true or 0 if false.
498 | * @param value A Boolean value determining which byte is written. If the parameter is true,
499 | * the method writes a 1; if false, the method writes a 0.
500 | */
501 | public writeBoolean(value: boolean): void {
502 | this.validateBuffer(ByteArrayBase.SIZE_OF_BOOLEAN);
503 |
504 | this.data.setUint8(this.position++, value ? 1 : 0);
505 | }
506 |
507 | /**
508 | * Writes a byte to the byte stream.
509 | * The low 8 bits of the
510 | * parameter are used. The high 24 bits are ignored.
511 | * @param value A 32-bit integer. The low 8 bits are written to the byte stream.
512 | */
513 | public writeByte(value: number): void {
514 | this.validateBuffer(ByteArrayBase.SIZE_OF_INT8);
515 |
516 | this.data.setInt8(this.position++, value);
517 | }
518 | public writeUnsignedByte(value: number): void {
519 | this.validateBuffer(ByteArrayBase.SIZE_OF_UINT8);
520 |
521 | this.data.setUint8(this.position++, value);
522 | }
523 | /**
524 | * Writes a sequence of length bytes from the
525 | * specified byte array, bytes,
526 | * starting offset(zero-based index) bytes
527 | * into the byte stream.
528 | *
529 | * If the length parameter is omitted, the default
530 | * length of 0 is used; the method writes the entire buffer starting at
531 | * offset.
532 | * If the offset parameter is also omitted, the entire buffer is
533 | * written. If offset or length
534 | * is out of range, they are clamped to the beginning and end
535 | * of the bytes array.
536 | * @param bytes The ByteArrayBase object.
537 | * @param offset A zero-based index indicating the _position into the array to begin writing.
538 | * @param length An unsigned integer indicating how far into the buffer to write.
539 | */
540 | public writeBytes(_bytes: ByteArrayBase, offset: number = 0, length: number = 0): void {
541 | this.validateBuffer(length);
542 |
543 | var tmp_data = new DataView(_bytes.buffer);
544 | for (var i = 0; i < _bytes.length; i++) {
545 | this.data.setUint8(this.position++, tmp_data.getUint8(i));
546 | }
547 | }
548 |
549 | /**
550 | * Writes an IEEE 754 double-precision (64-bit) floating-point number to the byte stream.
551 | * @param value A double-precision (64-bit) floating-point number.
552 | */
553 | public writeDouble(value: number): void {
554 | this.validateBuffer(ByteArrayBase.SIZE_OF_FLOAT64);
555 |
556 | this.data.setFloat64(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
557 | this.position += ByteArrayBase.SIZE_OF_FLOAT64;
558 | }
559 |
560 | /**
561 | * Writes an IEEE 754 single-precision (32-bit) floating-point number to the byte stream.
562 | * @param value A single-precision (32-bit) floating-point number.
563 | */
564 | public writeFloat(value: number): void {
565 | this.validateBuffer(ByteArrayBase.SIZE_OF_FLOAT32);
566 |
567 | this.data.setFloat32(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
568 | this.position += ByteArrayBase.SIZE_OF_FLOAT32;
569 | }
570 |
571 | /**
572 | * Writes a 32-bit signed integer to the byte stream.
573 | * @param value An integer to write to the byte stream.
574 | */
575 | public writeInt(value: number): void {
576 | this.validateBuffer(ByteArrayBase.SIZE_OF_INT32);
577 |
578 | this.data.setInt32(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
579 | this.position += ByteArrayBase.SIZE_OF_INT32;
580 | }
581 |
582 | /**
583 | * Writes a multibyte string to the byte stream using the specified character set.
584 | * @param value The string value to be written.
585 | * @param charSet The string denoting the character set to use. Possible character set strings
586 | * include "shift-jis", "cn-gb", "iso-8859-1", and others.
587 | * For a complete list, see Supported Character Sets.
588 | */
589 | public writeMultiByte(value: string, charSet: string): void {
590 |
591 | }
592 |
593 | /**
594 | * Writes a 16-bit integer to the byte stream. The low 16 bits of the parameter are used.
595 | * The high 16 bits are ignored.
596 | * @param value 32-bit integer, whose low 16 bits are written to the byte stream.
597 | */
598 | public writeShort(value: number): void {
599 | this.validateBuffer(ByteArrayBase.SIZE_OF_INT16);
600 |
601 | this.data.setInt16(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
602 | this.position += ByteArrayBase.SIZE_OF_INT16;
603 | }
604 | public writeUnsignedShort(value: number): void {
605 | this.validateBuffer(ByteArrayBase.SIZE_OF_UINT16);
606 |
607 | this.data.setUint16(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
608 | this.position += ByteArrayBase.SIZE_OF_UINT16;
609 | }
610 |
611 | /**
612 | * Writes a 32-bit unsigned integer to the byte stream.
613 | * @param value An unsigned integer to write to the byte stream.
614 | */
615 | public writeUnsignedInt(value: number): void {
616 | this.validateBuffer(ByteArrayBase.SIZE_OF_UINT32);
617 |
618 | this.data.setUint32(this.position, value, this.endian == ByteArrayBase.LITTLE_ENDIAN);
619 | this.position += ByteArrayBase.SIZE_OF_UINT32;
620 | }
621 |
622 | /**
623 | * Writes a UTF-8 string to the byte stream. The length of the UTF-8 string in bytes
624 | * is written first, as a 16-bit integer, followed by the bytes representing the
625 | * characters of the string.
626 | * @param value The string value to be written.
627 | */
628 | public writeUTF(value: string): void {
629 | var utf8bytes: Uint8Array = this.encodeUTF8(value);
630 | var length: number = utf8bytes.length;
631 |
632 | this.validateBuffer(ByteArrayBase.SIZE_OF_UINT16 + length);
633 |
634 | this.data.setUint16(this.position, length, this.endian === ByteArrayBase.LITTLE_ENDIAN);
635 | this.position += ByteArrayBase.SIZE_OF_UINT16;
636 | this.writeUint8Array(utf8bytes);
637 | }
638 |
639 | /**
640 | * Writes a UTF-8 string to the byte stream. Similar to the writeUTF() method,
641 | * but writeUTFBytes() does not prefix the string with a 16-bit length word.
642 | * @param value The string value to be written.
643 | */
644 | public writeUTFBytes(value: string): void {
645 | this.writeUint8Array(this.encodeUTF8(value));
646 | }
647 |
648 |
649 | public toString(): string {
650 | return "[ByteArrayBase] length:" + this.length + ", bytesAvailable:" + this.bytesAvailable;
651 | }
652 |
653 | /****************************/
654 | /* EXTRA JAVASCRIPT APIs */
655 | /****************************/
656 |
657 | /**
658 | * Writes a Uint8Array to the byte stream.
659 | * @param value The Uint8Array to be written.
660 | */
661 | public writeUint8Array(_bytes: Uint8Array): void {
662 | this.validateBuffer(this.position + _bytes.length);
663 |
664 | for (var i = 0; i < _bytes.length; i++) {
665 | this.data.setUint8(this.position++, _bytes[i]);
666 | }
667 | }
668 |
669 | /**
670 | * Writes a Uint16Array to the byte stream.
671 | * @param value The Uint16Array to be written.
672 | */
673 | public writeUint16Array(_bytes: Uint16Array): void {
674 | this.validateBuffer(this.position + _bytes.length);
675 |
676 | for (var i = 0; i < _bytes.length; i++) {
677 | this.data.setUint16(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
678 | this.position += ByteArrayBase.SIZE_OF_UINT16;
679 | }
680 | }
681 |
682 | /**
683 | * Writes a Uint32Array to the byte stream.
684 | * @param value The Uint32Array to be written.
685 | */
686 | public writeUint32Array(_bytes: Uint32Array): void {
687 | this.validateBuffer(this.position + _bytes.length);
688 |
689 | for (var i = 0; i < _bytes.length; i++) {
690 | this.data.setUint32(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
691 | this.position += ByteArrayBase.SIZE_OF_UINT32;
692 | }
693 | }
694 |
695 | /**
696 | * Writes a Int8Array to the byte stream.
697 | * @param value The Int8Array to be written.
698 | */
699 | public writeInt8Array(_bytes: Int8Array): void {
700 | this.validateBuffer(this.position + _bytes.length);
701 |
702 | for (var i = 0; i < _bytes.length; i++) {
703 | this.data.setInt8(this.position++, _bytes[i]);
704 | }
705 | }
706 |
707 | /**
708 | * Writes a Int16Array to the byte stream.
709 | * @param value The Int16Array to be written.
710 | */
711 | public writeInt16Array(_bytes: Int16Array): void {
712 | this.validateBuffer(this.position + _bytes.length);
713 |
714 | for (var i = 0; i < _bytes.length; i++) {
715 | this.data.setInt16(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
716 | this.position += ByteArrayBase.SIZE_OF_INT16;
717 | }
718 | }
719 |
720 | /**
721 | * Writes a Int32Array to the byte stream.
722 | * @param value The Int32Array to be written.
723 | */
724 | public writeInt32Array(_bytes: Int32Array): void {
725 | this.validateBuffer(this.position + _bytes.length);
726 |
727 | for (var i = 0; i < _bytes.length; i++) {
728 | this.data.setInt32(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
729 | this.position += ByteArrayBase.SIZE_OF_INT32;
730 | }
731 | }
732 |
733 | /**
734 | * Writes a Float32Array to the byte stream.
735 | * @param value The Float32Array to be written.
736 | */
737 | public writeFloat32Array(_bytes: Float32Array): void {
738 | this.validateBuffer(this.position + _bytes.length);
739 |
740 | for (var i = 0; i < _bytes.length; i++) {
741 | this.data.setFloat32(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
742 | this.position += ByteArrayBase.SIZE_OF_FLOAT32;
743 | }
744 | }
745 |
746 | /**
747 | * Writes a Float64Array to the byte stream.
748 | * @param value The Float64Array to be written.
749 | */
750 | public writeFloat64Array(_bytes: Float64Array): void {
751 | this.validateBuffer(this.position + _bytes.length);
752 |
753 | for (var i = 0; i < _bytes.length; i++) {
754 | this.data.setFloat64(this.position, _bytes[i], this.endian === ByteArrayBase.LITTLE_ENDIAN);
755 | this.position += ByteArrayBase.SIZE_OF_FLOAT64;
756 | }
757 | }
758 |
759 | /**
760 | * Read a Uint8Array from the byte stream.
761 | * @param length An unsigned short indicating the length of the Uint8Array.
762 | */
763 | public readUint8Array(length: number, createNewBuffer: boolean = true): Uint8Array {
764 | if (!this.validate(length)) return null;
765 | if (!createNewBuffer) {
766 | var result: Uint8Array = new Uint8Array(this.buffer, this.bufferOffset + this.position, length);
767 | this.position += length;
768 | } else {
769 | result = new Uint8Array(new ArrayBuffer(length));
770 | for (var i = 0; i < length; i++) {
771 | result[i] = this.data.getUint8(this.position);
772 | this.position += ByteArrayBase.SIZE_OF_UINT8;
773 | }
774 | }
775 | return result;
776 | }
777 |
778 | /**
779 | * Read a Uint16Array from the byte stream.
780 | * @param length An unsigned short indicating the length of the Uint16Array.
781 | */
782 | public readUint16Array(length: number, createNewBuffer: boolean = true): Uint16Array {
783 | var size: number = length * ByteArrayBase.SIZE_OF_UINT16;
784 | if (!this.validate(size)) return null;
785 | if (!createNewBuffer) {
786 | var result: Uint16Array = new Uint16Array(this.buffer, this.bufferOffset + this.position, length);
787 | this.position += size;
788 | }
789 | else {
790 | result = new Uint16Array(new ArrayBuffer(size));
791 | for (var i = 0; i < length; i++) {
792 | result[i] = this.data.getUint16(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
793 | this.position += ByteArrayBase.SIZE_OF_UINT16;
794 | }
795 | }
796 | return result;
797 | }
798 |
799 | /**
800 | * Read a Uint32Array from the byte stream.
801 | * @param length An unsigned short indicating the length of the Uint32Array.
802 | */
803 | public readUint32Array(length: number, createNewBuffer: boolean = true): Uint32Array {
804 | var size: number = length * ByteArrayBase.SIZE_OF_UINT32;
805 | if (!this.validate(size)) return null;
806 | if (!createNewBuffer) {
807 | var result: Uint32Array = new Uint32Array(this.buffer, this.bufferOffset + this.position, length);
808 | this.position += size;
809 | }
810 | else {
811 | result = new Uint32Array(new ArrayBuffer(size));
812 | for (var i = 0; i < length; i++) {
813 | result[i] = this.data.getUint32(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
814 | this.position += ByteArrayBase.SIZE_OF_UINT32;
815 | }
816 | }
817 | return result;
818 | }
819 |
820 | /**
821 | * Read a Int8Array from the byte stream.
822 | * @param length An unsigned short indicating the length of the Int8Array.
823 | */
824 | public readInt8Array(length: number, createNewBuffer: boolean = true): Int8Array {
825 | if (!this.validate(length)) return null;
826 | if (!createNewBuffer) {
827 | var result: Int8Array = new Int8Array(this.buffer, this.bufferOffset + this.position, length);
828 | this.position += length;
829 | }
830 | else {
831 | result = new Int8Array(new ArrayBuffer(length));
832 | for (var i = 0; i < length; i++) {
833 | result[i] = this.data.getInt8(this.position);
834 | this.position += ByteArrayBase.SIZE_OF_INT8;
835 | }
836 | }
837 | return result;
838 | }
839 |
840 | /**
841 | * Read a Int16Array from the byte stream.
842 | * @param length An unsigned short indicating the length of the Int16Array.
843 | */
844 | public readInt16Array(length: number, createNewBuffer: boolean = true): Int16Array {
845 | var size: number = length * ByteArrayBase.SIZE_OF_INT16
846 | if (!this.validate(size)) return null;
847 | if (!createNewBuffer) {
848 | var result: Int16Array = new Int16Array(this.buffer, this.bufferOffset + this.position, length);
849 | this.position += size;
850 | }
851 | else {
852 | result = new Int16Array(new ArrayBuffer(size));
853 | for (var i = 0; i < length; i++) {
854 | result[i] = this.data.getInt16(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
855 | this.position += ByteArrayBase.SIZE_OF_INT16;
856 | }
857 | }
858 | return result;
859 | }
860 |
861 | /**
862 | * Read a Int32Array from the byte stream.
863 | * @param length An unsigned short indicating the length of the Int32Array.
864 | */
865 | public readInt32Array(length: number, createNewBuffer: boolean = true): Int32Array {
866 | var size: number = length * ByteArrayBase.SIZE_OF_INT32
867 | if (!this.validate(size)) return null;
868 | if (!createNewBuffer) {
869 |
870 | if ((this.bufferOffset + this.position) % 4 == 0) {
871 | var result: Float32Array = new Int32Array(this.buffer, this.bufferOffset + this.position, length);
872 | this.position += size;
873 | } else {
874 | var tmp: Uint8Array = new Uint8Array(new ArrayBuffer(size));
875 | for (var i = 0; i < size; i++) {
876 | tmp[i] = this.data.getUint8(this.position);
877 | this.position += ByteArrayBase.SIZE_OF_UINT8;
878 | }
879 | result = new Int32Array(tmp.buffer);
880 | }
881 | }
882 | else {
883 | result = new Int32Array(new ArrayBuffer(size));
884 | for (var i = 0; i < length; i++) {
885 | result[i] = this.data.getInt32(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
886 | this.position += ByteArrayBase.SIZE_OF_INT32;
887 | }
888 | }
889 | return result;
890 | }
891 |
892 | /**
893 | * Read a Float32Array from the byte stream.
894 | * @param length An unsigned short indicating the length of the Float32Array.
895 | */
896 | public readFloat32Array(length: number, createNewBuffer: boolean = true): Float32Array {
897 | var size: number = length * ByteArrayBase.SIZE_OF_FLOAT32;
898 | if (!this.validate(size)) return null;
899 | if (!createNewBuffer) {
900 | if ((this.bufferOffset + this.position) % 4 == 0) {
901 | var result: Float32Array = new Float32Array(this.buffer, this.bufferOffset + this.position, length);
902 | this.position += size;
903 | } else {
904 | var tmp: Uint8Array = new Uint8Array(new ArrayBuffer(size));
905 | for (var i = 0; i < size; i++) {
906 | tmp[i] = this.data.getUint8(this.position);
907 | this.position += ByteArrayBase.SIZE_OF_UINT8;
908 | }
909 | result = new Float32Array(tmp.buffer);
910 | }
911 | }
912 | else {
913 | result = new Float32Array(new ArrayBuffer(size));
914 |
915 | for (var i = 0; i < length; i++) {
916 | result[i] = this.data.getFloat32(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
917 | this.position += ByteArrayBase.SIZE_OF_FLOAT32;
918 | }
919 | }
920 | return result;
921 | }
922 |
923 | /**
924 | * Read a Float64Array from the byte stream.
925 | * @param length An unsigned short indicating the length of the Float64Array.
926 | */
927 | public readFloat64Array(length: number, createNewBuffer: boolean = true): Float64Array {
928 | var size: number = length * ByteArrayBase.SIZE_OF_FLOAT64
929 | if (!this.validate(size)) return null;
930 | if (!createNewBuffer) {
931 | var result: Float64Array = new Float64Array(this.buffer, this.position, length);
932 | this.position += size;
933 | } else {
934 | result = new Float64Array(new ArrayBuffer(size));
935 | for (var i = 0; i < length; i++) {
936 | result[i] = this.data.getFloat64(this.position, this.endian === ByteArrayBase.LITTLE_ENDIAN);
937 | this.position += ByteArrayBase.SIZE_OF_FLOAT64;
938 | }
939 | }
940 | return result;
941 | }
942 | public validate(len: number): boolean {
943 | //len += this.data.byteOffset;
944 | if (this.data.byteLength > 0 && this._position + len <= this.data.byteLength) {
945 | return true;
946 | } else {
947 | throw 'Error #2030: End of file was encountered.';
948 | }
949 | }
950 | public compress(): void {
951 | let finished = false;
952 | zlib.deflate(this.toBuffer(), (err, result) => {
953 | finished = true;
954 | this.fromBuffer(result);
955 | });
956 | while (!finished) {
957 | runOnce();
958 | }
959 | }
960 | public uncompress(): void {
961 | let finished = false;
962 | zlib.inflate(this.toBuffer(), (err, result) => {
963 | finished = true;
964 | this.fromBuffer(result);
965 | });
966 | while (!finished) {
967 | runOnce();
968 | }
969 | }
970 | public toBuffer(): Buffer {
971 | var buffer = new Buffer(this.buffer.byteLength);
972 | var view = new Uint8Array(this.buffer);
973 | for (var i = 0; i < buffer.length; ++i) {
974 | buffer[i] = view[i];
975 | }
976 | return buffer;
977 | }
978 | public fromBuffer(buff: Buffer): void {
979 | var ab = new ArrayBuffer(buff.length);
980 | var view = new Uint8Array(ab);
981 | for (var i = 0; i < buff.length; ++i) {
982 | view[i] = buff[i];
983 | }
984 | this.setBuffer(ab);
985 | }
986 | /**********************/
987 | /* PRIVATE METHODS */
988 | /**********************/
989 | private validateBuffer(len: number): void {
990 | this.write_position = len > this.write_position ? len : this.write_position;
991 | if (this.data.byteLength < len) {
992 | var tmp: Uint8Array = new Uint8Array(new ArrayBuffer(len + this.BUFFER_EXT_SIZE));
993 | tmp.set(new Uint8Array(this.data.buffer));
994 | this.data.buffer = tmp.buffer;
995 | }
996 | }
997 |
998 | /**
999 | * UTF-8 Encoding/Decoding
1000 | */
1001 | private encodeUTF8(str: string): Uint8Array {
1002 | var pos: number = 0;
1003 | var codePoints = this.stringToCodePoints(str);
1004 | var outputBytes = [];
1005 |
1006 | while (codePoints.length > pos) {
1007 | var code_point: number = codePoints[pos++];
1008 |
1009 | if (this.inRange(code_point, 0xD800, 0xDFFF)) {
1010 | this.encoderError(code_point);
1011 | }
1012 | else if (this.inRange(code_point, 0x0000, 0x007f)) {
1013 | outputBytes.push(code_point);
1014 | } else {
1015 | var count, offset;
1016 | if (this.inRange(code_point, 0x0080, 0x07FF)) {
1017 | count = 1;
1018 | offset = 0xC0;
1019 | } else if (this.inRange(code_point, 0x0800, 0xFFFF)) {
1020 | count = 2;
1021 | offset = 0xE0;
1022 | } else if (this.inRange(code_point, 0x10000, 0x10FFFF)) {
1023 | count = 3;
1024 | offset = 0xF0;
1025 | }
1026 |
1027 | outputBytes.push(this.div(code_point, Math.pow(64, count)) + offset);
1028 |
1029 | while (count > 0) {
1030 | var temp = this.div(code_point, Math.pow(64, count - 1));
1031 | outputBytes.push(0x80 + (temp % 64));
1032 | count -= 1;
1033 | }
1034 | }
1035 | }
1036 | return new Uint8Array(outputBytes);
1037 | }
1038 | private decodeUTF8(data: Uint8Array): string {
1039 | var fatal: boolean = false;
1040 | var pos: number = 0;
1041 | var result: string = "";
1042 | var code_point: number;
1043 | var utf8_code_point = 0;
1044 | var utf8_bytes_needed = 0;
1045 | var utf8_bytes_seen = 0;
1046 | var utf8_lower_boundary = 0;
1047 |
1048 | while (data.length > pos) {
1049 |
1050 | var _byte = data[pos++];
1051 |
1052 | if (_byte === this.EOF_byte) {
1053 | if (utf8_bytes_needed !== 0) {
1054 | code_point = this.decoderError(fatal);
1055 | } else {
1056 | code_point = this.EOF_code_point;
1057 | }
1058 | } else {
1059 |
1060 | if (utf8_bytes_needed === 0) {
1061 | if (this.inRange(_byte, 0x00, 0x7F)) {
1062 | code_point = _byte;
1063 | } else {
1064 | if (this.inRange(_byte, 0xC2, 0xDF)) {
1065 | utf8_bytes_needed = 1;
1066 | utf8_lower_boundary = 0x80;
1067 | utf8_code_point = _byte - 0xC0;
1068 | } else if (this.inRange(_byte, 0xE0, 0xEF)) {
1069 | utf8_bytes_needed = 2;
1070 | utf8_lower_boundary = 0x800;
1071 | utf8_code_point = _byte - 0xE0;
1072 | } else if (this.inRange(_byte, 0xF0, 0xF4)) {
1073 | utf8_bytes_needed = 3;
1074 | utf8_lower_boundary = 0x10000;
1075 | utf8_code_point = _byte - 0xF0;
1076 | } else {
1077 | this.decoderError(fatal);
1078 | }
1079 | utf8_code_point = utf8_code_point * Math.pow(64, utf8_bytes_needed);
1080 | code_point = null;
1081 | }
1082 | } else if (!this.inRange(_byte, 0x80, 0xBF)) {
1083 | utf8_code_point = 0;
1084 | utf8_bytes_needed = 0;
1085 | utf8_bytes_seen = 0;
1086 | utf8_lower_boundary = 0;
1087 | pos--;
1088 | code_point = this.decoderError(fatal, _byte);
1089 | } else {
1090 |
1091 | utf8_bytes_seen += 1;
1092 | utf8_code_point = utf8_code_point + (_byte - 0x80) * Math.pow(64, utf8_bytes_needed - utf8_bytes_seen);
1093 |
1094 | if (utf8_bytes_seen !== utf8_bytes_needed) {
1095 | code_point = null;
1096 | } else {
1097 |
1098 | var cp = utf8_code_point;
1099 | var lower_boundary = utf8_lower_boundary;
1100 | utf8_code_point = 0;
1101 | utf8_bytes_needed = 0;
1102 | utf8_bytes_seen = 0;
1103 | utf8_lower_boundary = 0;
1104 | if (this.inRange(cp, lower_boundary, 0x10FFFF) && !this.inRange(cp, 0xD800, 0xDFFF)) {
1105 | code_point = cp;
1106 | } else {
1107 | code_point = this.decoderError(fatal, _byte);
1108 | }
1109 | }
1110 |
1111 | }
1112 | }
1113 | //Decode string
1114 | if (code_point !== null && code_point !== this.EOF_code_point) {
1115 | if (code_point <= 0xFFFF) {
1116 | if (code_point > 0) result += String.fromCharCode(code_point);
1117 | } else {
1118 | code_point -= 0x10000;
1119 | result += String.fromCharCode(0xD800 + ((code_point >> 10) & 0x3ff));
1120 | result += String.fromCharCode(0xDC00 + (code_point & 0x3ff));
1121 | }
1122 | }
1123 | }
1124 | return result;
1125 | }
1126 | private encoderError(code_point) {
1127 | throw 'EncodingError! The code point ' + code_point + ' could not be encoded.';
1128 | }
1129 | private decoderError(fatal, opt_code_point?): number {
1130 | if (fatal) {
1131 | throw 'DecodingError';
1132 | }
1133 | return opt_code_point || 0xFFFD;
1134 | }
1135 | private EOF_byte: number = -1;
1136 | private EOF_code_point: number = -1;
1137 |
1138 | private inRange(a, min, max) {
1139 | return min <= a && a <= max;
1140 | }
1141 | private div(n, d) {
1142 | return Math.floor(n / d);
1143 | }
1144 | private stringToCodePoints(string) {
1145 | /** @type {Array.} */
1146 | var cps = [];
1147 | // Based on http://www.w3.org/TR/WebIDL/#idl-DOMString
1148 | var i = 0, n = string.length;
1149 | while (i < string.length) {
1150 | var c = string.charCodeAt(i);
1151 | if (!this.inRange(c, 0xD800, 0xDFFF)) {
1152 | cps.push(c);
1153 | } else if (this.inRange(c, 0xDC00, 0xDFFF)) {
1154 | cps.push(0xFFFD);
1155 | } else { // (inRange(c, 0xD800, 0xDBFF))
1156 | if (i === n - 1) {
1157 | cps.push(0xFFFD);
1158 | } else {
1159 | var d = string.charCodeAt(i + 1);
1160 | if (this.inRange(d, 0xDC00, 0xDFFF)) {
1161 | var a = c & 0x3FF;
1162 | var b = d & 0x3FF;
1163 | i += 1;
1164 | cps.push(0x10000 + (a << 10) + b);
1165 | } else {
1166 | cps.push(0xFFFD);
1167 | }
1168 | }
1169 | }
1170 | i += 1;
1171 | }
1172 | return cps;
1173 | }
1174 | }
1175 | module ByteArrayBase {
1176 | export class Int64 {
1177 | public low: number;
1178 | public high: number;
1179 | public _value: number;
1180 |
1181 | constructor(low: number = 0, high: number = 0) {
1182 | this.low = low;
1183 | this.high = high;
1184 | }
1185 | public value(): number {
1186 | //this._value = (this.low | (this.high << 32));
1187 | var _h: string = this.high.toString(16);
1188 | var _hd: number = 8 - _h.length;
1189 | if (_hd > 0) {
1190 | for (var i = 0; i < _hd; i++) {
1191 | _h = '0' + _h;
1192 | }
1193 | }
1194 | var _l: string = this.low.toString(16);
1195 | var _ld: number = 8 - _l.length;
1196 | if (_ld > 0) {
1197 | for (i = 0; i < _ld; i++) {
1198 | _l = '0' + _l;
1199 | }
1200 | }
1201 | this._value = Number('0x' + _h + _l);
1202 | return this._value;
1203 | }
1204 |
1205 | }
1206 | export class UInt64 {
1207 | public low: number;
1208 | public high: number;
1209 | public _value: number;
1210 |
1211 | constructor(low: number = 0, high: number = 0) {
1212 | this.low = low;
1213 | this.high = high;
1214 | }
1215 | public value(): number {
1216 | //this._value = (this.low | (this.high << 32));
1217 | var _h: string = this.high.toString(16);
1218 | var _hd: number = 8 - _h.length;
1219 | if (_hd > 0) {
1220 | for (var i = 0; i < _hd; i++) {
1221 | _h = '0' + _h;
1222 | }
1223 | }
1224 | var _l: string = this.low.toString(16);
1225 | var _ld: number = 8 - _l.length;
1226 | if (_ld > 0) {
1227 | for (i = 0; i < _ld; i++) {
1228 | _l = '0' + _l;
1229 | }
1230 | }
1231 | this._value = Number('0x' + _h + _l);
1232 | return this._value;
1233 | }
1234 |
1235 | }
1236 | }
1237 |
1238 | export = ByteArrayBase;
1239 |
--------------------------------------------------------------------------------
/ts-bytearray.d.ts:
--------------------------------------------------------------------------------
1 | ///
2 | declare module 'ts-bytearray' {
3 | class ByteArrayBase {
4 | private bitsPending;
5 | readBits(bits: number, bitBuffer?: number): number;
6 | writeBits(bits: number, value: number): void;
7 | resetBitsPending(): void;
8 | calculateMaxBits(signed: boolean, values: Array): number;
9 | static BIG_ENDIAN: string;
10 | static LITTLE_ENDIAN: string;
11 | static SIZE_OF_BOOLEAN: number;
12 | static SIZE_OF_INT8: number;
13 | static SIZE_OF_INT16: number;
14 | static SIZE_OF_INT32: number;
15 | static SIZE_OF_INT64: number;
16 | static SIZE_OF_UINT8: number;
17 | static SIZE_OF_UINT16: number;
18 | static SIZE_OF_UINT32: number;
19 | static SIZE_OF_UINT64: number;
20 | static SIZE_OF_FLOAT32: number;
21 | static SIZE_OF_FLOAT64: number;
22 | private BUFFER_EXT_SIZE;
23 | array: Uint8Array;
24 | data: DataView;
25 | private _position;
26 | write_position: number;
27 | endian: string;
28 | constructor(buffer?: ArrayBuffer, offset?: number, length?: number);
29 | buffer: ArrayBuffer;
30 | dataView: DataView;
31 | phyPosition: number;
32 | bufferOffset: number;
33 | position: number;
34 | length: number;
35 | bytesAvailable: number;
36 | clear(): void;
37 | getArray(): Uint8Array;
38 | setArray(array: Uint8Array): void;
39 | setBuffer(buffer: ArrayBuffer, offset?: number, length?: number): void;
40 | /**
41 | * Reads a Boolean value from the byte stream. A single byte is read,
42 | * and true is returned if the byte is nonzero,
43 | * false otherwise.
44 | * @return Returns true if the byte is nonzero, false otherwise.
45 | */
46 | readBoolean(): boolean;
47 | /**
48 | * Reads a signed byte from the byte stream.
49 | * The returned value is in the range -128 to 127.
50 | * @return An integer between -128 and 127.
51 | */
52 | readByte(): number;
53 | /**
54 | * Reads the number of data bytes, specified by the length parameter, from the byte stream.
55 | * The bytes are read into the ByteArrayBase object specified by the bytes parameter,
56 | * and the bytes are written into the destination ByteArrayBase starting at the _position specified by offset.
57 | * @param bytes The ByteArrayBase object to read data into.
58 | * @param offset The offset (_position) in bytes at which the read data should be written.
59 | * @param length The number of bytes to read. The default value of 0 causes all available data to be read.
60 | */
61 | readBytes(_bytes?: ByteArrayBase, offset?: number, length?: number, createNewBuffer?: boolean): ByteArrayBase;
62 | /**
63 | * Reads an IEEE 754 double-precision (64-bit) floating-point number from the byte stream.
64 | * @return A double-precision (64-bit) floating-point number.
65 | */
66 | readDouble(): number;
67 | /**
68 | * Reads an IEEE 754 single-precision (32-bit) floating-point number from the byte stream.
69 | * @return A single-precision (32-bit) floating-point number.
70 | */
71 | readFloat(): number;
72 | /**
73 | * Reads a signed 32-bit integer from the byte stream.
74 | *
75 | * The returned value is in the range -2147483648 to 2147483647.
76 | * @return A 32-bit signed integer between -2147483648 and 2147483647.
77 | */
78 | readInt(): number;
79 | /**
80 | * Reads a signed 64-bit integer from the byte stream.
81 | *
82 | * The returned value is in the range −(2^63) to 2^63 − 1
83 | * @return A 64-bit signed integer between −(2^63) to 2^63 − 1
84 | */
85 | readInt64(): ByteArrayBase.Int64;
86 | /**
87 | * Reads a multibyte string of specified length from the byte stream using the
88 | * specified character set.
89 | * @param length The number of bytes from the byte stream to read.
90 | * @param charSet The string denoting the character set to use to interpret the bytes.
91 | * Possible character set strings include "shift-jis", "cn-gb",
92 | * "iso-8859-1", and others.
93 | * For a complete list, see Supported Character Sets.
94 | * Note: If the value for the charSet parameter
95 | * is not recognized by the current system, the application uses the system's default
96 | * code page as the character set. For example, a value for the charSet parameter,
97 | * as in myTest.readMultiByte(22, "iso-8859-01") that uses 01 instead of
98 | * 1 might work on your development system, but not on another system.
99 | * On the other system, the application will use the system's default code page.
100 | * @return UTF-8 encoded string.
101 | */
102 | readMultiByte(length: number, charSet?: string): string;
103 | /**
104 | * Reads a signed 16-bit integer from the byte stream.
105 | *
106 | * The returned value is in the range -32768 to 32767.
107 | * @return A 16-bit signed integer between -32768 and 32767.
108 | */
109 | readShort(): number;
110 | /**
111 | * Reads an unsigned byte from the byte stream.
112 | *
113 | * The returned value is in the range 0 to 255.
114 | * @return A 32-bit unsigned integer between 0 and 255.
115 | */
116 | readUnsignedByte(): number;
117 | /**
118 | * Reads an unsigned 32-bit integer from the byte stream.
119 | *
120 | * The returned value is in the range 0 to 4294967295.
121 | * @return A 32-bit unsigned integer between 0 and 4294967295.
122 | */
123 | readUnsignedInt(): number;
124 | /**
125 | * Reads a variable sized unsigned integer (VX -> 16-bit or 32-bit) from the byte stream.
126 | *
127 | * A VX is written as a variable length 2- or 4-byte element. If the index value is less than 65,280 (0xFF00),
128 | * then the index is written as an unsigned two-byte integer. Otherwise the index is written as an unsigned
129 | * four byte integer with bits 24-31 set. When reading an index, if the first byte encountered is 255 (0xFF),
130 | * then the four-byte form is being used and the first byte should be discarded or masked out.
131 | *
132 | * The returned value is in the range 0 to 65279 or 0 to 2147483647.
133 | * @return A VX 16-bit or 32-bit unsigned integer between 0 to 65279 or 0 and 2147483647.
134 | */
135 | readVariableSizedUnsignedInt(): number;
136 | /**
137 | * Fast read for WebGL since only Uint16 numbers are expected
138 | */
139 | readU16VX(): number;
140 | /**
141 | * Reads an unsigned 64-bit integer from the byte stream.
142 | *
143 | * The returned value is in the range 0 to 2^64 − 1.
144 | * @return A 64-bit unsigned integer between 0 and 2^64 − 1
145 | */
146 | readUnsignedInt64(): ByteArrayBase.UInt64;
147 | /**
148 | * Reads an unsigned 16-bit integer from the byte stream.
149 | *
150 | * The returned value is in the range 0 to 65535.
151 | * @return A 16-bit unsigned integer between 0 and 65535.
152 | */
153 | readUnsignedShort(): number;
154 | /**
155 | * Reads a UTF-8 string from the byte stream. The string
156 | * is assumed to be prefixed with an unsigned short indicating
157 | * the length in bytes.
158 | * @return UTF-8 encoded string.
159 | */
160 | readUTF(): string;
161 | /**
162 | * Reads a sequence of UTF-8 bytes specified by the length
163 | * parameter from the byte stream and returns a string.
164 | * @param length An unsigned short indicating the length of the UTF-8 bytes.
165 | * @return A string composed of the UTF-8 bytes of the specified length.
166 | */
167 | readUTFBytes(length: number): string;
168 | readStandardString(length: number): string;
169 | readStringTillNull(keepEvenByte?: boolean): string;
170 | /**
171 | * Writes a Boolean value. A single byte is written according to the value parameter,
172 | * either 1 if true or 0 if false.
173 | * @param value A Boolean value determining which byte is written. If the parameter is true,
174 | * the method writes a 1; if false, the method writes a 0.
175 | */
176 | writeBoolean(value: boolean): void;
177 | /**
178 | * Writes a byte to the byte stream.
179 | * The low 8 bits of the
180 | * parameter are used. The high 24 bits are ignored.
181 | * @param value A 32-bit integer. The low 8 bits are written to the byte stream.
182 | */
183 | writeByte(value: number): void;
184 | writeUnsignedByte(value: number): void;
185 | /**
186 | * Writes a sequence of length bytes from the
187 | * specified byte array, bytes,
188 | * starting offset(zero-based index) bytes
189 | * into the byte stream.
190 | *
191 | * If the length parameter is omitted, the default
192 | * length of 0 is used; the method writes the entire buffer starting at
193 | * offset.
194 | * If the offset parameter is also omitted, the entire buffer is
195 | * written. If offset or length
196 | * is out of range, they are clamped to the beginning and end
197 | * of the bytes array.
198 | * @param bytes The ByteArrayBase object.
199 | * @param offset A zero-based index indicating the _position into the array to begin writing.
200 | * @param length An unsigned integer indicating how far into the buffer to write.
201 | */
202 | writeBytes(_bytes: ByteArrayBase, offset?: number, length?: number): void;
203 | /**
204 | * Writes an IEEE 754 double-precision (64-bit) floating-point number to the byte stream.
205 | * @param value A double-precision (64-bit) floating-point number.
206 | */
207 | writeDouble(value: number): void;
208 | /**
209 | * Writes an IEEE 754 single-precision (32-bit) floating-point number to the byte stream.
210 | * @param value A single-precision (32-bit) floating-point number.
211 | */
212 | writeFloat(value: number): void;
213 | /**
214 | * Writes a 32-bit signed integer to the byte stream.
215 | * @param value An integer to write to the byte stream.
216 | */
217 | writeInt(value: number): void;
218 | /**
219 | * Writes a multibyte string to the byte stream using the specified character set.
220 | * @param value The string value to be written.
221 | * @param charSet The string denoting the character set to use. Possible character set strings
222 | * include "shift-jis", "cn-gb", "iso-8859-1", and others.
223 | * For a complete list, see Supported Character Sets.
224 | */
225 | writeMultiByte(value: string, charSet: string): void;
226 | /**
227 | * Writes a 16-bit integer to the byte stream. The low 16 bits of the parameter are used.
228 | * The high 16 bits are ignored.
229 | * @param value 32-bit integer, whose low 16 bits are written to the byte stream.
230 | */
231 | writeShort(value: number): void;
232 | writeUnsignedShort(value: number): void;
233 | /**
234 | * Writes a 32-bit unsigned integer to the byte stream.
235 | * @param value An unsigned integer to write to the byte stream.
236 | */
237 | writeUnsignedInt(value: number): void;
238 | /**
239 | * Writes a UTF-8 string to the byte stream. The length of the UTF-8 string in bytes
240 | * is written first, as a 16-bit integer, followed by the bytes representing the
241 | * characters of the string.
242 | * @param value The string value to be written.
243 | */
244 | writeUTF(value: string): void;
245 | /**
246 | * Writes a UTF-8 string to the byte stream. Similar to the writeUTF() method,
247 | * but writeUTFBytes() does not prefix the string with a 16-bit length word.
248 | * @param value The string value to be written.
249 | */
250 | writeUTFBytes(value: string): void;
251 | toString(): string;
252 | /****************************/
253 | /****************************/
254 | /**
255 | * Writes a Uint8Array to the byte stream.
256 | * @param value The Uint8Array to be written.
257 | */
258 | writeUint8Array(_bytes: Uint8Array): void;
259 | /**
260 | * Writes a Uint16Array to the byte stream.
261 | * @param value The Uint16Array to be written.
262 | */
263 | writeUint16Array(_bytes: Uint16Array): void;
264 | /**
265 | * Writes a Uint32Array to the byte stream.
266 | * @param value The Uint32Array to be written.
267 | */
268 | writeUint32Array(_bytes: Uint32Array): void;
269 | /**
270 | * Writes a Int8Array to the byte stream.
271 | * @param value The Int8Array to be written.
272 | */
273 | writeInt8Array(_bytes: Int8Array): void;
274 | /**
275 | * Writes a Int16Array to the byte stream.
276 | * @param value The Int16Array to be written.
277 | */
278 | writeInt16Array(_bytes: Int16Array): void;
279 | /**
280 | * Writes a Int32Array to the byte stream.
281 | * @param value The Int32Array to be written.
282 | */
283 | writeInt32Array(_bytes: Int32Array): void;
284 | /**
285 | * Writes a Float32Array to the byte stream.
286 | * @param value The Float32Array to be written.
287 | */
288 | writeFloat32Array(_bytes: Float32Array): void;
289 | /**
290 | * Writes a Float64Array to the byte stream.
291 | * @param value The Float64Array to be written.
292 | */
293 | writeFloat64Array(_bytes: Float64Array): void;
294 | /**
295 | * Read a Uint8Array from the byte stream.
296 | * @param length An unsigned short indicating the length of the Uint8Array.
297 | */
298 | readUint8Array(length: number, createNewBuffer?: boolean): Uint8Array;
299 | /**
300 | * Read a Uint16Array from the byte stream.
301 | * @param length An unsigned short indicating the length of the Uint16Array.
302 | */
303 | readUint16Array(length: number, createNewBuffer?: boolean): Uint16Array;
304 | /**
305 | * Read a Uint32Array from the byte stream.
306 | * @param length An unsigned short indicating the length of the Uint32Array.
307 | */
308 | readUint32Array(length: number, createNewBuffer?: boolean): Uint32Array;
309 | /**
310 | * Read a Int8Array from the byte stream.
311 | * @param length An unsigned short indicating the length of the Int8Array.
312 | */
313 | readInt8Array(length: number, createNewBuffer?: boolean): Int8Array;
314 | /**
315 | * Read a Int16Array from the byte stream.
316 | * @param length An unsigned short indicating the length of the Int16Array.
317 | */
318 | readInt16Array(length: number, createNewBuffer?: boolean): Int16Array;
319 | /**
320 | * Read a Int32Array from the byte stream.
321 | * @param length An unsigned short indicating the length of the Int32Array.
322 | */
323 | readInt32Array(length: number, createNewBuffer?: boolean): Int32Array;
324 | /**
325 | * Read a Float32Array from the byte stream.
326 | * @param length An unsigned short indicating the length of the Float32Array.
327 | */
328 | readFloat32Array(length: number, createNewBuffer?: boolean): Float32Array;
329 | /**
330 | * Read a Float64Array from the byte stream.
331 | * @param length An unsigned short indicating the length of the Float64Array.
332 | */
333 | readFloat64Array(length: number, createNewBuffer?: boolean): Float64Array;
334 | validate(len: number): boolean;
335 | compress(): void;
336 | uncompress(): void;
337 | toBuffer(): Buffer;
338 | fromBuffer(buff: Buffer): void;
339 | /**********************/
340 | /**********************/
341 | private validateBuffer(len);
342 | /**
343 | * UTF-8 Encoding/Decoding
344 | */
345 | private encodeUTF8(str);
346 | private decodeUTF8(data);
347 | private encoderError(code_point);
348 | private decoderError(fatal, opt_code_point?);
349 | private EOF_byte;
350 | private EOF_code_point;
351 | private inRange(a, min, max);
352 | private div(n, d);
353 | private stringToCodePoints(string);
354 | }
355 | module ByteArrayBase {
356 | class Int64 {
357 | low: number;
358 | high: number;
359 | _value: number;
360 | constructor(low?: number, high?: number);
361 | value(): number;
362 | }
363 | class UInt64 {
364 | low: number;
365 | high: number;
366 | _value: number;
367 | constructor(low?: number, high?: number);
368 | value(): number;
369 | }
370 | }
371 | export = ByteArrayBase;
372 | }
373 |
--------------------------------------------------------------------------------
/uvrun2.d.ts:
--------------------------------------------------------------------------------
1 | declare module 'uvrun2' {
2 | export function runOnce(): void;
3 | }
--------------------------------------------------------------------------------