Skip to content

Commit

Permalink
Replace use of deprecated Buffer constructor (which could be used to …
Browse files Browse the repository at this point in the history
…create an uninitialized Buffer of any size) with Buffer.from()
  • Loading branch information
Florian Reiterer committed Apr 27, 2018
1 parent 716bab7 commit d0b9823
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
@@ -1,9 +1,10 @@
sudo: false
language: node_js
node_js:
- iojs
- '0.12'
- '0.10'
- "10"
- "9"
- "8"
- "6"
after_script:
- npm run coveralls
branches:
Expand Down
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -13,9 +13,9 @@ function Concat(generateSourceMap, fileName, separator) {
this.contentParts = [];

if (separator === undefined) {
this.separator = new Buffer(0);
this.separator = Buffer.from('');
} else {
this.separator = new Buffer(separator);
this.separator = Buffer.from(separator);
}

if (this.sourceMapping) {
Expand All @@ -37,7 +37,7 @@ Concat.prototype.add = function(filePath, content, sourceMap) {
filePath = filePath && unixStylePath(filePath);

if (!Buffer.isBuffer(content)) {
content = new Buffer(content);
content = Buffer.from(content);
}

if (this.contentParts.length !== 0) {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -21,11 +21,11 @@
"source-map": "^0.6.1"
},
"devDependencies": {
"jshint": "^2.8.0",
"tape": "^4.2.0",
"istanbul": "^0.3.21",
"coveralls": "^3.0.0",
"faucet": "0.0.1",
"coveralls": "^2.11.4"
"istanbul": "^0.4.5",
"jshint": "^2.9.5",
"tape": "^4.9.0"
},
"files": [
"index.js",
Expand Down
9 changes: 8 additions & 1 deletion test/index.js
Expand Up @@ -8,7 +8,7 @@ function testCase(description, options) {
// content as Buffer
var concat = new Concat(options.sourceMapping, options.outFile, options.separator);
options.input.forEach(function(input, i) {
concat.add((input.fileName !== undefined ? input.fileName : 'test'+(i+1)), new Buffer(input.content), input.sourceMap);
concat.add((input.fileName !== undefined ? input.fileName : 'test'+(i+1)), Buffer.from(input.content), input.sourceMap);
});
t.equal(concat.content.toString(), options.output.content, 'should produce the right output');
if (options.output.sourceMap)
Expand Down Expand Up @@ -344,3 +344,10 @@ testCase('should allow content without filename and produce no mapping for it',
sourceMap: '{"version":3,"file":"out.js","sources":["test2","test3","test5"],"names":[],"mappings":";AAAA;AACA;ACDA;;ACAA;AACA"}'
}
});

test('should not allocate an uninitialized buffer when passing a number', function(t) {
t.throws(function() {
new Concat(true, 'all.js', 234);
}, "passing a number as separator should throw");
t.end();
});

0 comments on commit d0b9823

Please sign in to comment.