This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
/
index.js
341 lines (289 loc) · 9.45 KB
/
index.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
'use strict';
const fs = require('fs-extra');
const pathUtils = require('path');
const crypto = require('crypto');
const babel = require('babel-core');
const CoffeeScript = require('coffee-script');
const yaml = require('js-yaml');
const { promisify } = require('util')
const HASH = 'sha256';
const HASH_LENGTH = 64;
const TMP_DIR = pathUtils.join(__dirname, '../../build/tmp/');
const BABEL_OPTIONS = {
presets: ['env'],
filename: '',
babelrc: false,
ast: false
};
const COFFEE_SCRIPT_EXTS = ['.coffee', '.litcoffee'];
const YAML_EXTS = ['.yaml', '.yml'];
const REALPATH = fs.realpathSync('.');
const MTIME_DELAY = 1000;
const filesCache = Object.create(null);
const fsReadFile = promisify(fs.readFile);
fs.ensureDirSync(TMP_DIR);
process.on('exit', function () {
const files = fs.readdirSync(TMP_DIR);
const maxDate = Date.now() - 1000 * 60 * 60;
for (var i = 0; i < files.length; i++) {
const filename = pathUtils.join(TMP_DIR, files[i]);
const stat = fs.statSync(filename);
if (stat.atime < maxDate) {
fs.unlinkSync(filename);
}
}
});
function absolutePath(path) {
if (pathUtils.isAbsolute(path)) {
return path;
}
return pathUtils.join(REALPATH, path);
}
function createHash(string) {
return crypto.createHash(HASH).update(string).digest('hex');
}
async function readCacheFile(path) {
const file = await fsReadFile(path, 'utf-8');
return {
hash: file.slice(0, HASH_LENGTH),
code: file.slice(HASH_LENGTH)
}
}
function readCacheFileSync(path) {
const file = fs.readFileSync(path, 'utf-8');
return {
hash: file.slice(0, HASH_LENGTH),
code: file.slice(HASH_LENGTH)
}
}
async function writeCacheFile(path, hash, code) {
const file = hash + code;
await promisify(fs.writeFile)(path, file, 'utf-8');
}
function writeCacheFileSync(path, hash, code) {
const file = hash + code;
fs.writeFileSync(path, file, 'utf-8');
}
exports.BABEL_OPTIONS = BABEL_OPTIONS;
exports.compileFile = function (file, filename) {
const extname = pathUtils.extname(filename);
switch (extname) {
case '.coffee':
return CoffeeScript.compile(file);
case '.litcoffee':
return CoffeeScript.compile(file, {literate: true});
case '.js':
BABEL_OPTIONS.filename = filename;
return babel.transform(file, BABEL_OPTIONS).code;
case '.yaml':
case '.yml': {
const object = yaml.safeLoad(file, { filename: filename, json: true });
return JSON.stringify(object);
}
case '.nml': {
const bundle = require('src/nml-parser').bundle({ nml: file, path: filename }).bundle;
return CoffeeScript.compile(bundle);
}
default:
return file;
}
};
exports.fileHash = async function (filename) {
const code = await exports.getFile(filename);
return createHash(code)
};
exports.getFile = async function (filename) {
var path = absolutePath(filename);
// get file from memory cache
var fileCache = filesCache[path];
const fileStat = await promisify(fs.stat)(path);
if (!fileStat.isFile()) {
throw new TypeError(`Path ${path} is not a file`);
}
if (fileCache) {
if (fileStat.mtime < fileCache.mtime) {
return fileCache.code;
}
}
// get file from cache
let code;
let hash;
const pathHash = createHash(path);
const cacheFilename = pathUtils.join(TMP_DIR, pathHash);
let cacheStat;
try {
cacheStat = await promisify(fs.stat)(cacheFilename);
if (cacheStat.mtime - MTIME_DELAY >= fileStat.mtime) {
const cache = await readCacheFile(cacheFilename);
code = cache.code;
hash = cache.hash;
}
}
catch (error) {}
// get file
if (code === undefined) {
const file = await promisify(fs.readFile)(path, 'utf-8');
// use cache when both files are the same
if (cacheStat) {
const cache = await readCacheFile(cacheFilename);
if (cache.hash === createHash(file)) {
code = cache.code;
}
}
if (code === undefined) {
code = exports.compileFile(file, filename);
await writeCacheFile(cacheFilename, createHash(file), code);
}
}
// update memory cache
if (!fileCache) {
fileCache = filesCache[path] = {mtime: 0, code: ''};
}
// some of systems has 1 second precision for file modification time;
// to prevent using cache for new files, we use current time back by 1 second;
// if you're getting file multiple times just after change; it won't be optimized
fileCache.mtime = Date.now() - MTIME_DELAY;
fileCache.code = code;
return code;
};
exports.getFileSync = function (filename) {
var path = absolutePath(filename);
// get file from memory cache
var fileCache = filesCache[path];
const fileStat = fs.statSync(path);
if (!fileStat.isFile()) {
throw new TypeError(`Path ${path} is not a file`);
}
if (fileCache) {
if (fileStat.mtime < fileCache.mtime) {
return fileCache.code;
}
}
// get file from cache
let code;
let hash;
const pathHash = createHash(path);
const cacheFilename = pathUtils.join(TMP_DIR, pathHash);
let cacheStat;
try {
cacheStat = fs.statSync(cacheFilename);
if (cacheStat.mtime - MTIME_DELAY >= fileStat.mtime) {
const cache = readCacheFileSync(cacheFilename);
code = cache.code;
hash = cache.hash;
}
}
catch (error) {}
// get file
if (code === undefined) {
const file = fs.readFileSync(path, 'utf-8');
// use cache when both files are the same
if (cacheStat) {
const cache = readCacheFileSync(cacheFilename);
if (cache.hash === createHash(file)) {
code = cache.code;
}
}
if (code === undefined) {
code = exports.compileFile(file, filename);
writeCacheFileSync(cacheFilename, createHash(file), code);
}
}
// update memory cache
if (!fileCache) {
fileCache = filesCache[path] = {mtime: 0, code: ''};
}
// some of systems has 1 second precision for file modification time;
// to prevent using cache for new files, we use current time back by 1 second;
// if you're getting file multiple times just after change; it won't be optimized
fileCache.mtime = Date.now() - MTIME_DELAY;
fileCache.code = code;
return code;
};
exports.registerFilenameResolver = function () {
const Module = module.constructor;
const filenamesCache = Object.create(null);
const resolveFilename = Module._resolveFilename
Module._resolveFilename = function (request, parent, isMain) {
const cache = filenamesCache[request];
if (cache) {
return cache;
} else {
const filename = resolveFilename(request, parent, isMain);
if (request[0] !== '.') {
filenamesCache[request] = filename;
}
return filename;
}
};
exports.registerFilenameResolver = () => {};
};
exports.registerCoffeeScript = function () {
function compile (module, filename) {
const file = exports.getFileSync(filename);
return module._compile(file, filename);
}
for (var i = 0; i < COFFEE_SCRIPT_EXTS.length; i++) {
require.extensions[COFFEE_SCRIPT_EXTS[i]] = compile;
}
exports.registerCoffeeScript = () => {};
};
exports.registerBabel = function () {
const _super = require.extensions['.js'];
require.extensions['.js'] = function (module, filename) {
if (filename.indexOf('node_modules') >= 0
&& (
filename.indexOf('node_modules/neft') < 0
|| filename.indexOf('node_modules/neft/node_modules') >= 0
)) {
return _super(module, filename);
}
const file = exports.getFileSync(filename);
return module._compile(file, filename);
};
exports.registerBabel = () => {};
};
exports.registerYaml = function () {
function compile (module, filename) {
const file = exports.getFileSync(filename);
module.exports = JSON.parse(file);
}
for (var i = 0; i < YAML_EXTS.length; i++) {
require.extensions[YAML_EXTS[i]] = compile;
}
exports.registerYaml = () => {};
};
exports.registerTxt = (function () {
const registeredExtensions = Object.create(null);
return function (extnames) {
if (!Array.isArray(extnames)) {
extnames = ['.txt'];
}
function compile (module, filename) {
module.exports = exports.getFileSync(filename);
}
for (var i = 0; i < extnames.length; i++) {
const extname = extnames[i];
if (registeredExtensions[extname]) {
continue;
}
registeredExtensions[extname] = true;
require.extensions[extname] = compile;
}
};
}());
exports.registerNml = function () {
require.extensions['.nml'] = function (module, filename) {
const file = exports.getFileSync(filename);
return module._compile(file, filename);
};
exports.registerNml = () => {};
};
exports.register = function () {
exports.registerFilenameResolver();
exports.registerCoffeeScript();
exports.registerBabel();
exports.registerYaml();
exports.registerTxt();
exports.registerNml();
};