Skip to content

Commit c5ec591

Browse files
committed
Fix warnings about new Buffer()
1 parent 9523ad6 commit c5ec591

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ script: "npm run travis"
66
node_js:
77
- "0.10"
88
- "0.12"
9-
- "4"
9+
- "8"
10+
- "10"
1011
- "node"

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ function unixPathname(pathname) {
88
return pathname.replace(/\\/g, '/');
99
}
1010

11+
function bufferFrom(data, encoding) {
12+
return typeof Buffer.from === 'function'
13+
? Buffer.from(data, encoding)
14+
: new Buffer(data, encoding);
15+
}
16+
1117
function readFromStream(stream, minify) {
1218
var buffer = [];
1319

@@ -141,7 +147,7 @@ function resolveSourceMap(source, inputMap, outputMap, inputFile, outputFile) {
141147

142148
if (inputMapComment.substr(0, 5) === 'data:') {
143149
// decode source map content from comment
144-
inputMapContent = new Buffer(inputMapComment.substr(inputMapComment.indexOf('base64,') + 7), 'base64').toString();
150+
inputMapContent = bufferFrom(inputMapComment.substr(inputMapComment.indexOf('base64,') + 7), 'base64').toString();
145151
} else {
146152
// value is filename – resolve it as absolute path
147153
if (inputFile) {
@@ -321,7 +327,7 @@ function minifyStream(options) {
321327
// inline source map
322328
sourceMapAnnotation = '\n' +
323329
'/*# sourceMappingURL=data:application/json;base64,' +
324-
new Buffer(result.map.toString()).toString('base64') +
330+
bufferFrom(result.map.toString()).toString('base64') +
325331
' */';
326332
}
327333

test/basic.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ function fixtureContent(filepath) {
1313
return fs.readFileSync(fixturePath(filepath), 'utf-8').trim();
1414
}
1515

16+
function bufferFrom(data, encoding) {
17+
return typeof Buffer.from === 'function'
18+
? Buffer.from(data, encoding)
19+
: new Buffer(data, encoding);
20+
}
21+
1622
function run() {
1723
var args = [path.join(__dirname, '../bin/csso')].concat(Array.prototype.slice.call(arguments));
1824
var proc = child.spawn(cmd, args, { stdio: 'pipe' });
@@ -59,9 +65,8 @@ function run() {
5965
}
6066

6167
it('should output version', function() {
62-
return run('-v').output(
63-
require('csso/package.json').version
64-
);
68+
return run('-v')
69+
.output(require('csso/package.json').version);
6570
});
6671

6772
it('should read content from stdin if no file specified', function() {
@@ -79,7 +84,7 @@ it('--source-map inline', function() {
7984
return run(fixturePath('1.css'), '--source-map', 'inline')
8085
.output(function(res) {
8186
var expected = fixtureContent('1.min.css.map');
82-
var actual = new Buffer(String(res).match(/data:application\/json;base64,(.+)/)[1], 'base64').toString('utf-8');
87+
var actual = bufferFrom(String(res).match(/data:application\/json;base64,(.+)/)[1], 'base64').toString('utf-8');
8388

8489
assert.equal(actual, expected);
8590
});

0 commit comments

Comments
 (0)