Skip to content

Commit ffe4d08

Browse files
authored
Modernize and run eslint on tools/lz4-compress.js. NFC (#15846)
Followup to #15836
1 parent 30e3c87 commit ffe4d08

File tree

2 files changed

+32
-129
lines changed

2 files changed

+32
-129
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"wasm2c": "1.0.0"
1515
},
1616
"scripts": {
17-
"lint": "eslint src/parseTools.js tools/acorn-optimizer.js"
17+
"lint": "eslint src/parseTools.js tools/acorn-optimizer.js tools/lz4-compress.js"
1818
}
1919
}

tools/lz4-compress.js

100644100755
Lines changed: 31 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,60 @@
1+
#!/usr/bin/env node
12
// Copyright 2015 The Emscripten Authors. All rights reserved.
23
// Emscripten is available under two separate licenses, the MIT license and the
34
// University of Illinois/NCSA Open Source License. Both these licenses can be
45
// found in the LICENSE file.
56

6-
// *** Environment setup code ***
7-
var arguments_ = [];
8-
var debug = false;
7+
const fs = require('fs');
8+
const path = require('path');
99

10-
var ENVIRONMENT_IS_NODE = typeof process === 'object';
11-
var ENVIRONMENT_IS_WEB = typeof window === 'object';
12-
var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
13-
var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
10+
const arguments_ = process['argv'].slice(2);
11+
const debug = false;
1412

15-
if (ENVIRONMENT_IS_NODE) {
16-
// Expose functionality in the same simple way that the shells work
17-
// Note that we pollute the global namespace here, otherwise we break in node
18-
print = function(x) {
19-
process['stdout'].write(x + '\n');
20-
};
21-
printErr = function(x) {
22-
process['stderr'].write(x + '\n');
23-
};
24-
25-
var nodeFS = require('fs');
26-
var nodePath = require('path');
27-
28-
if (!nodeFS.existsSync) {
29-
nodeFS.existsSync = function(path) {
30-
try {
31-
return !!nodeFS.readFileSync(path);
32-
} catch(e) {
33-
return false;
34-
}
35-
}
36-
}
37-
38-
function find(filename) {
39-
var prefixes = [nodePath.join(__dirname, '..', 'src'), process.cwd()];
40-
for (var i = 0; i < prefixes.length; ++i) {
41-
var combined = nodePath.join(prefixes[i], filename);
42-
if (nodeFS.existsSync(combined)) {
43-
return combined;
44-
}
45-
}
46-
return filename;
47-
}
48-
49-
read = function(filename, binary) {
50-
filename = nodePath['normalize'](filename);
51-
var ret = nodeFS['readFileSync'](filename);
52-
// The path is absolute if the normalized version is the same as the resolved.
53-
if (!ret && filename != nodePath['resolve'](filename)) {
54-
filename = path.join(__dirname, '..', 'src', filename);
55-
ret = nodeFS['readFileSync'](filename);
56-
}
57-
if (ret && !binary) ret = ret.toString();
58-
return ret;
59-
};
60-
61-
readBinary = function(filename) { return read(filename, true) };
62-
63-
load = function(f) {
64-
globalEval(read(f));
65-
};
66-
67-
arguments_ = process['argv'].slice(2);
68-
69-
} else if (ENVIRONMENT_IS_SHELL) {
70-
// Polyfill over SpiderMonkey/V8 differences
71-
if (!this['read']) {
72-
this['read'] = function(f) { snarf(f) };
73-
}
74-
75-
if (typeof scriptArgs != 'undefined') {
76-
arguments_ = scriptArgs;
77-
} else if (typeof arguments != 'undefined') {
78-
arguments_ = arguments;
79-
}
80-
81-
} else if (ENVIRONMENT_IS_WEB) {
82-
this['print'] = printErr = function(x) {
83-
console.log(x);
84-
};
85-
86-
this['read'] = function(url) {
87-
var xhr = new XMLHttpRequest();
88-
xhr.open('GET', url, false);
89-
xhr.send(null);
90-
return xhr.responseText;
91-
};
92-
93-
if (this['arguments']) {
94-
arguments_ = arguments;
95-
}
96-
} else if (ENVIRONMENT_IS_WORKER) {
97-
// We can do very little here...
98-
99-
this['load'] = importScripts;
100-
101-
} else {
102-
throw 'Unknown runtime environment. Where are we?';
13+
function print(x) {
14+
process['stdout'].write(x + '\n');
10315
}
10416

105-
function globalEval(x) {
106-
eval.call(null, x);
17+
function printErr(x) {
18+
process['stderr'].write(x + '\n');
10719
}
10820

109-
if (typeof load === 'undefined' && typeof read != 'undefined') {
110-
this['load'] = function(f) {
111-
globalEval(read(f));
112-
};
21+
function read(filename, binary) {
22+
filename = path['normalize'](filename);
23+
let ret = fs['readFileSync'](filename);
24+
if (ret && !binary) ret = ret.toString();
25+
return ret;
11326
}
11427

115-
if (typeof printErr === 'undefined') {
116-
this['printErr'] = function(){};
28+
function readBinary(filename) {
29+
return read(filename, true);
11730
}
11831

119-
if (typeof print === 'undefined') {
120-
this['print'] = printErr;
32+
function globalEval(x) {
33+
eval.call(null, x);
12134
}
12235

123-
assert = function(x, message) {
124-
if (!x) throw 'assertion failed: ' + message + ' : ' + new Error().stack;
36+
function load(f) {
37+
globalEval(read(f));
12538
}
12639

127-
if (!Math['imul'] || Math['imul'](0xffffffff, 5) !== -5) Math['imul'] = function imul(a, b) {
128-
var ah = a >>> 16;
129-
var al = a & 0xffff;
130-
var bh = b >>> 16;
131-
var bl = b & 0xffff;
132-
return (al*bl + ((ah*bl + al*bh) << 16))|0;
40+
assert = function(x, message) {
41+
if (!x) throw new Errror(message);
13342
};
13443

44+
// Redirect console.log message from MiniLZ4 to stderr since stdout is
45+
// where we return the decompressed data.
13546
console.log = printErr;
13647

137-
// *** Environment setup code ***
138-
139-
var lz4 = arguments_[0];
140-
var input = arguments_[1];
141-
var output = arguments_[2];
48+
const lz4 = arguments_[0];
49+
const input = arguments_[1];
50+
const output = arguments_[2];
14251

14352
load(lz4);
14453

145-
var data = readBinary(input);
146-
if (!(data instanceof ArrayBuffer)) {
147-
printErr('converting to ArrayBuffer');
148-
data = new Uint8Array(data).buffer;
149-
}
150-
151-
var start = Date.now();
152-
var compressedData = MiniLZ4.compressPackage(data);
153-
nodeFS['writeFileSync'](output, Buffer.from(compressedData['data']));
54+
const data = new Uint8Array(readBinary(input)).buffer;
55+
const start = Date.now();
56+
const compressedData = MiniLZ4.compressPackage(data);
57+
fs.writeFileSync(output, Buffer.from(compressedData['data']));
15458
compressedData['data'] = null;
15559
printErr('compressed in ' + (Date.now() - start) + ' ms');
15660
print(JSON.stringify(compressedData));
157-

0 commit comments

Comments
 (0)