|
| 1 | +#!/usr/bin/env node |
1 | 2 | // Copyright 2015 The Emscripten Authors. All rights reserved. |
2 | 3 | // Emscripten is available under two separate licenses, the MIT license and the |
3 | 4 | // University of Illinois/NCSA Open Source License. Both these licenses can be |
4 | 5 | // found in the LICENSE file. |
5 | 6 |
|
6 | | -// *** Environment setup code *** |
7 | | -var arguments_ = []; |
8 | | -var debug = false; |
| 7 | +const fs = require('fs'); |
| 8 | +const path = require('path'); |
9 | 9 |
|
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; |
14 | 12 |
|
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'); |
103 | 15 | } |
104 | 16 |
|
105 | | -function globalEval(x) { |
106 | | - eval.call(null, x); |
| 17 | +function printErr(x) { |
| 18 | + process['stderr'].write(x + '\n'); |
107 | 19 | } |
108 | 20 |
|
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; |
113 | 26 | } |
114 | 27 |
|
115 | | -if (typeof printErr === 'undefined') { |
116 | | - this['printErr'] = function(){}; |
| 28 | +function readBinary(filename) { |
| 29 | + return read(filename, true); |
117 | 30 | } |
118 | 31 |
|
119 | | -if (typeof print === 'undefined') { |
120 | | - this['print'] = printErr; |
| 32 | +function globalEval(x) { |
| 33 | + eval.call(null, x); |
121 | 34 | } |
122 | 35 |
|
123 | | -assert = function(x, message) { |
124 | | - if (!x) throw 'assertion failed: ' + message + ' : ' + new Error().stack; |
| 36 | +function load(f) { |
| 37 | + globalEval(read(f)); |
125 | 38 | } |
126 | 39 |
|
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); |
133 | 42 | }; |
134 | 43 |
|
| 44 | +// Redirect console.log message from MiniLZ4 to stderr since stdout is |
| 45 | +// where we return the decompressed data. |
135 | 46 | console.log = printErr; |
136 | 47 |
|
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]; |
142 | 51 |
|
143 | 52 | load(lz4); |
144 | 53 |
|
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'])); |
154 | 58 | compressedData['data'] = null; |
155 | 59 | printErr('compressed in ' + (Date.now() - start) + ' ms'); |
156 | 60 | print(JSON.stringify(compressedData)); |
157 | | - |
0 commit comments