Skip to content

Commit

Permalink
Added more versatile slowBufferCreate.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Feb 24, 2016
1 parent 427b0ed commit 591e52e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 18 deletions.
50 changes: 32 additions & 18 deletions index.js
@@ -1,33 +1,45 @@
"use strict";


(function() {
var root = this;
var previous_mymodule = root.mymodule;

var createBuffer = null, convertBytesToString, convertStringToBytes = null;

if (typeof Buffer === 'undefined') {
createBuffer = function(arg) {
var slowCreateBuffer = function(arg) {

// Passed in a single number, the length to pre-allocate
if (typeof arg === 'number') {
var result = [];
for (var i = 0; i < arg; i++) {
result.push(0);
}
return result;
// Passed in a single number, the length to pre-allocate
if (typeof arg === 'number') {
var result = [];
for (var i = 0; i < arg; i++) {
result.push(0);
}
return result;

} else {
// Make sure they are passing sensible data
for (var i = 0; i < arg.length; i++) {
if (arg[i] < 0 || arg[i] >= 256 || typeof arg[i] !== 'number') {
throw new Error('invalid byte at index ' + i + '(' + arg[i] + ')');
}
} else {
// Make sure they are passing sensible data
for (var i = 0; i < arg.length; i++) {
if (arg[i] < 0 || arg[i] >= 256 || typeof arg[i] !== 'number') {
throw new Error('invalid byte at index ' + i + '(' + arg[i] + ')');
}
}

// Most array-like things should support this
if (arg.slice) {
return arg.slice(0);
}

// Something *weird*; copy it into an array (see PR#2)
var result = [];
for (var i = 0; i < arg.length; i++) {
result.push(arg[i]);
}
return result;
}
}

if (typeof(Buffer) === 'undefined') {
createBuffer = slowCreateBuffer;

Array.prototype.copy = function(targetArray, targetStart, sourceStart, sourceEnd) {
if (targetStart == null) { targetStart = 0; }
Expand Down Expand Up @@ -621,7 +633,8 @@
ModeOfOperation: ModeOfOperation,
util: {
convertBytesToString: convertBytesToString,
convertStringToBytes: convertStringToBytes
convertStringToBytes: convertStringToBytes,
_slowCreateBuffer: slowCreateBuffer
}
};

Expand All @@ -633,7 +646,8 @@
exports.ModeOfOperation = ModeOfOperation;
exports.util = {
convertBytesToString: convertBytesToString,
convertStringToBytes: convertStringToBytes
convertStringToBytes: convertStringToBytes,
_slowCreateBuffer: slowCreateBuffer
}
/*
if(typeof module !== 'undefined' && module.exports) {
Expand Down
46 changes: 46 additions & 0 deletions test/test-buffer.js
@@ -0,0 +1,46 @@
'use strict';

var nodeunit = require('nodeunit');

var slowCreateBuffer = require('../index').util._slowCreateBuffer;

var testArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var testBuffer = new Buffer(testArray);

// We mimic some weird non-array-but-sortof-like-an-array object that people on
// obscure browsers seem to have problems with, for the purpose of testing our
// slowCreateBuffer.
function WeirdBuffer(data) {
this.length = data.length;
for (var i = 0; i < data.length; i++) {
this[i] = data[i];
}
}

function buffersEqual(a, b) {
if (a.length !== b.length) { return false; }
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}

nodeunit.reporters.default.run({
"test-buffer": {
"slowCreate": function(test) {
//var result = new AES(testArray).key;
var result = slowCreateBuffer(testArray);
test.ok(buffersEqual(testArray, result), 'bufferCreate failed to match input array');

result = slowCreateBuffer(testBuffer);
test.ok(buffersEqual(testBuffer, result), 'bufferCreate failed to match input array');

result = slowCreateBuffer(new WeirdBuffer(testArray));
test.ok(buffersEqual(testBuffer, result), 'bufferCreate failed to match input array');

test.done();
},
},
});

0 comments on commit 591e52e

Please sign in to comment.