isbuffer.js
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var test = require('tape');
var mdeps = require('module-deps');
var bpack = require('browser-pack');
var insert = require('../');
var concat = require('concat-stream');
var vm = require('vm');
// use is-buffer instead of builtin Buffer.isBuffer. The builtin
// does `instanceof` which does not work on the browserified version
var isBuffer = require('is-buffer');
test('isbuffer', function (t) {
t.plan(5);
var deps = mdeps()
var pack = bpack({ raw: true, hasExports: true });
deps.pipe(pack).pipe(concat(function (src) {
var c = { global: {} };
vm.runInNewContext(src, c);
t.equal(c.require('main')(Buffer('wow')), true, 'is a buffer');
t.equal(c.require('main')('wow'), false, 'not a buffer (string)');
t.equal(c.require('main')({}), false, 'not a buffer (object)');
t.notOk(/require\("buffer"\)/.test(src), 'buffer not required in source')
t.notOk(/require\("\//.test(src), 'absolute path not required in source')
}));
deps.write({ transform: inserter, global: true });
deps.end({ id: 'main', file: __dirname + '/isbuffer/main.js' });
});
test('isbuffer (and Buffer.from)', function (t) {
t.plan(5);
var deps = mdeps()
var pack = bpack({ raw: true, hasExports: true });
deps.pipe(pack).pipe(concat(function (src) {
var c = { global: {} };
vm.runInNewContext(src, c);
t.equal(c.require('main')(c.require('main').a()), true, 'is a buffer');
t.equal(c.require('main')('wow'), false, 'is not a buffer');
t.equal(isBuffer(c.require('main').a()), true, 'is a buffer');
t.ok(/require\("buffer"\)/.test(src), 'buffer required in source')
t.equal(c.require('main').a().toString('hex'), 'abcd', 'is a buffer');
}));
deps.write({ transform: inserter, global: true });
deps.end({ id: 'main', file: __dirname + '/isbuffer/both.js' });
});
test('isbuffer (and new Buffer)', function (t) {
t.plan(5);
var deps = mdeps()
var pack = bpack({ raw: true, hasExports: true });
deps.pipe(pack).pipe(concat(function (src) {
var c = { global: {} };
vm.runInNewContext(src, c);
t.equal(c.require('main')(c.require('main').a()), true, 'is a buffer');
t.equal(c.require('main')('wow'), false, 'is not a buffer');
t.equal(isBuffer(c.require('main').a()), true, 'is a buffer');
t.ok(/require\("buffer"\)/.test(src), 'buffer required in source')
t.equal(c.require('main').a().toString('utf8'), 'abcd', 'is a buffer');
}));
deps.write({ transform: inserter, global: true });
deps.end({ id: 'main', file: __dirname + '/isbuffer/new.js' });
});
function inserter (file) {
return insert(file, { basedir: __dirname + '/isbuffer' });
}