Skip to content

Commit 45b19f6

Browse files
committed
fixes to avoid relying on buffer and to work in older browsers
1 parent e502eca commit 45b19f6

File tree

4 files changed

+94
-49
lines changed

4 files changed

+94
-49
lines changed

.zuul.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ui: mocha-qunit
2+
tunnel: ngrok
23
browsers:
34
- name: chrome
45
version: latest
@@ -8,3 +9,5 @@ browsers:
89
version: latest
910
- name: ie
1011
version: 9..latest
12+
- name: microsoftedge
13+
version: latest

assert.js

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
'use strict';
2+
3+
// compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js
4+
// original notice:
5+
6+
/*!
7+
* The buffer module from node.js, for the browser.
8+
*
9+
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
10+
* @license MIT
11+
*/
12+
function compare(a, b) {
13+
if (a === b) {
14+
return 0;
15+
}
16+
17+
var x = a.length;
18+
var y = b.length;
19+
20+
for (var i = 0, len = Math.min(x, y); i < len; ++i) {
21+
if (a[i] !== b[i]) {
22+
x = a[i];
23+
y = b[i];
24+
break;
25+
}
26+
}
27+
28+
if (x < y) {
29+
return -1;
30+
}
31+
if (y < x) {
32+
return 1;
33+
}
34+
return 0;
35+
}
36+
function isBuffer(b) {
37+
if (global.Buffer && typeof global.Buffer.isBuffer === 'function') {
38+
return global.Buffer.isBuffer(b);
39+
}
40+
return !!(b != null && b._isBuffer);
41+
}
42+
43+
// based on node assert, original notice:
44+
145
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
246
//
347
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
@@ -22,30 +66,7 @@
2266
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2367
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2468

25-
'use strict';
26-
27-
// UTILITY
28-
function compare(bufa, bufb) {
29-
var cmpLen = Math.min(bufa, bufb);
30-
if (cmpLen <= 0) {
31-
return 0;
32-
}
33-
var i = -1;
34-
var a,b;
35-
while (++i < cmpLen) {
36-
a = bufa[i];
37-
b = bufb[i];
38-
if (a < b) {
39-
return -1;
40-
} else if (a > b) {
41-
return 1;
42-
}
43-
}
44-
return 0;
45-
}
4669
var util = require('util/');
47-
var Buffer = require('buffer').Buffer;
48-
var BufferShim = require('buffer-shims');
4970
var hasOwn = Object.prototype.hasOwnProperty;
5071
var pSlice = Array.prototype.slice;
5172
var functionsHaveNames = (function () {
@@ -55,6 +76,9 @@ function pToString (obj) {
5576
return Object.prototype.toString.call(obj);
5677
}
5778
function isView(arrbuf) {
79+
if (isBuffer(arrbuf)) {
80+
return false;
81+
}
5882
if (typeof global.ArrayBuffer !== 'function') {
5983
return false;
6084
}
@@ -110,25 +134,25 @@ assert.AssertionError = function AssertionError(options) {
110134
}
111135
var stackStartFunction = options.stackStartFunction || fail;
112136
if (Error.captureStackTrace) {
113-
Error.captureStackTrace(this, stackStartFunction);
114-
} else {
115-
// non v8 browsers so we can have a stacktrace
116-
var err = new Error();
117-
if (err.stack) {
118-
var out = err.stack;
119-
120-
// try to strip useless frames
121-
var fn_name = getName(stackStartFunction);
122-
var idx = out.indexOf('\n' + fn_name);
123-
if (idx >= 0) {
124-
// once we have located the function frame
125-
// we need to strip out everything before it (and its line)
126-
var next_line = out.indexOf('\n', idx + 1);
127-
out = out.substring(next_line + 1);
128-
}
129-
130-
this.stack = out;
131-
}
137+
Error.captureStackTrace(this, stackStartFunction);
138+
} else {
139+
// non v8 browsers so we can have a stacktrace
140+
var err = new Error();
141+
if (err.stack) {
142+
var out = err.stack;
143+
144+
// try to strip useless frames
145+
var fn_name = getName(stackStartFunction);
146+
var idx = out.indexOf('\n' + fn_name);
147+
if (idx >= 0) {
148+
// once we have located the function frame
149+
// we need to strip out everything before it (and its line)
150+
var next_line = out.indexOf('\n', idx + 1);
151+
out = out.substring(next_line + 1);
152+
}
153+
154+
this.stack = out;
155+
}
132156
}
133157
};
134158

@@ -228,7 +252,7 @@ function _deepEqual(actual, expected, strict, memos) {
228252
// 7.1. All identical values are equivalent, as determined by ===.
229253
if (actual === expected) {
230254
return true;
231-
} else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
255+
} else if (isBuffer(actual) && isBuffer(expected)) {
232256
return compare(actual, expected) === 0;
233257

234258
// 7.2. If the expected value is a Date object, the actual value is
@@ -262,15 +286,17 @@ function _deepEqual(actual, expected, strict, memos) {
262286
pToString(actual) === pToString(expected) &&
263287
!(actual instanceof Float32Array ||
264288
actual instanceof Float64Array)) {
265-
return compare(BufferShim.from(actual.buffer),
266-
BufferShim.from(expected.buffer)) === 0;
289+
return compare(new Uint8Array(actual.buffer),
290+
new Uint8Array(expected.buffer)) === 0;
267291

268292
// 7.5 For all other Object pairs, including Array objects, equivalence is
269293
// determined by having the same number of owned properties (as verified
270294
// with Object.prototype.hasOwnProperty.call), the same set of keys
271295
// (although not necessarily the same order), equivalent values for every
272296
// corresponding key, and an identical 'prototype' property. Note: this
273297
// accounts for both named and indexed properties on Arrays.
298+
} else if (isBuffer(actual) !== isBuffer(expected)) {
299+
return false;
274300
} else {
275301
memos = memos || {actual: [], expected: []};
276302

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@
1212
},
1313
"main": "./assert.js",
1414
"dependencies": {
15-
"buffer-shims": "1.0.0",
1615
"util": "0.10.3"
1716
},
1817
"devDependencies": {
19-
"zuul": "~3.9.0",
20-
"mocha": "~1.21.4"
18+
"mocha": "~1.21.4",
19+
"zuul": "~3.10.0",
20+
"zuul-ngrok": "^4.0.0"
2121
},
2222
"license": "MIT",
2323
"scripts": {
2424
"test-node": "mocha --ui qunit test.js",
2525
"test-browser": "zuul -- test.js",
2626
"test": "npm run test-node && npm run test-browser",
27-
"test-native": "TEST_NATIVE=true mocha --ui qunit test.js"
27+
"test-native": "TEST_NATIVE=true mocha --ui qunit test.js",
28+
"browser-local": "zuul --no-coverage --local 8000 -- test.js"
2829
}
2930
}

test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ function tests (assert, what) {
165165
assert.doesNotThrow(makeBlock(assert.deepEqual, new Boolean(true), {}));
166166
});
167167

168+
test('assert.deepEqual - Buffers', function () {
169+
assert.doesNotThrow(makeBlock(assert.deepEqual, new Buffer([1, 2, 3]), new Buffer([1, 2, 3])));
170+
if (typeof global.Uint8Array === 'function') {
171+
assert.throws(makeBlock(assert.deepEqual, new Buffer([1, 2, 3]), new Uint8Array([1, 2, 3])));
172+
}
173+
if (typeof global.Uint16Array === 'function') {
174+
assert.doesNotThrow(makeBlock(assert.deepEqual, new Uint16Array([1, 2, 3]), new Uint16Array([1, 2, 3])));
175+
}
176+
});
177+
168178
function thrower(errorConstructor) {
169179
throw new errorConstructor('test');
170180
}
@@ -294,6 +304,11 @@ function tests (assert, what) {
294304
testAssertionMessage('foo', '\'foo\'');
295305
testAssertionMessage([], '[]');
296306
testAssertionMessage([1, 2, 3], '[ 1, 2, 3 ]');
307+
testAssertionMessage(new Buffer([1, 2, 3]), '<Buffer 01 02 03>');
308+
if (typeof global.Uint8Array === 'function' && Object.getOwnPropertyNames( new Uint8Array([])).length === 0) {
309+
// todo fix util.inspect
310+
testAssertionMessage(new Uint8Array([1, 2, 3]), '{ \'0\': 1, \'1\': 2, \'2\': 3 }');
311+
}
297312
testAssertionMessage(/a/, '/a/');
298313
testAssertionMessage(function f() {}, '[Function: f]');
299314
testAssertionMessage({}, '{}');

0 commit comments

Comments
 (0)