|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | | - |
4 | 3 | (function() { |
5 | 4 | var root = this; |
6 | 5 | var previous_mymodule = root.mymodule; |
7 | 6 |
|
8 | 7 | var createBuffer = null, convertBytesToString, convertStringToBytes = null; |
9 | 8 |
|
10 | | - if (typeof Buffer === 'undefined') { |
11 | | - createBuffer = function(arg) { |
| 9 | + var slowCreateBuffer = function(arg) { |
12 | 10 |
|
13 | | - // Passed in a single number, the length to pre-allocate |
14 | | - if (typeof arg === 'number') { |
15 | | - var result = []; |
16 | | - for (var i = 0; i < arg; i++) { |
17 | | - result.push(0); |
18 | | - } |
19 | | - return result; |
| 11 | + // Passed in a single number, the length to pre-allocate |
| 12 | + if (typeof arg === 'number') { |
| 13 | + var result = []; |
| 14 | + for (var i = 0; i < arg; i++) { |
| 15 | + result.push(0); |
| 16 | + } |
| 17 | + return result; |
20 | 18 |
|
21 | | - } else { |
22 | | - // Make sure they are passing sensible data |
23 | | - for (var i = 0; i < arg.length; i++) { |
24 | | - if (arg[i] < 0 || arg[i] >= 256 || typeof arg[i] !== 'number') { |
25 | | - throw new Error('invalid byte at index ' + i + '(' + arg[i] + ')'); |
26 | | - } |
| 19 | + } else { |
| 20 | + // Make sure they are passing sensible data |
| 21 | + for (var i = 0; i < arg.length; i++) { |
| 22 | + if (arg[i] < 0 || arg[i] >= 256 || typeof arg[i] !== 'number') { |
| 23 | + throw new Error('invalid byte at index ' + i + '(' + arg[i] + ')'); |
27 | 24 | } |
| 25 | + } |
| 26 | + |
| 27 | + // Most array-like things should support this |
| 28 | + if (arg.slice) { |
28 | 29 | return arg.slice(0); |
29 | 30 | } |
| 31 | + |
| 32 | + // Something *weird*; copy it into an array (see PR#2) |
| 33 | + var result = []; |
| 34 | + for (var i = 0; i < arg.length; i++) { |
| 35 | + result.push(arg[i]); |
| 36 | + } |
| 37 | + return result; |
30 | 38 | } |
| 39 | + } |
| 40 | + |
| 41 | + if (typeof(Buffer) === 'undefined') { |
| 42 | + createBuffer = slowCreateBuffer; |
31 | 43 |
|
32 | 44 | Array.prototype.copy = function(targetArray, targetStart, sourceStart, sourceEnd) { |
33 | 45 | if (targetStart == null) { targetStart = 0; } |
|
621 | 633 | ModeOfOperation: ModeOfOperation, |
622 | 634 | util: { |
623 | 635 | convertBytesToString: convertBytesToString, |
624 | | - convertStringToBytes: convertStringToBytes |
| 636 | + convertStringToBytes: convertStringToBytes, |
| 637 | + _slowCreateBuffer: slowCreateBuffer |
625 | 638 | } |
626 | 639 | }; |
627 | 640 |
|
|
633 | 646 | exports.ModeOfOperation = ModeOfOperation; |
634 | 647 | exports.util = { |
635 | 648 | convertBytesToString: convertBytesToString, |
636 | | - convertStringToBytes: convertStringToBytes |
| 649 | + convertStringToBytes: convertStringToBytes, |
| 650 | + _slowCreateBuffer: slowCreateBuffer |
637 | 651 | } |
638 | 652 | /* |
639 | 653 | if(typeof module !== 'undefined' && module.exports) { |
|
0 commit comments