Clean up the oopsie. Rebuild the blank exercises.

This commit is contained in:
Michael Frank
2021-05-09 00:39:21 +12:00
parent b984cb4c6f
commit d28d80c46f
1565 changed files with 27186 additions and 199003 deletions
+16 -21
View File
@@ -1,37 +1,32 @@
'use strict';
var fs = require('fs');
var LRU = require('lru-cache');
var shebangCommand = require('shebang-command');
var shebangCache = new LRU({ max: 50, maxAge: 30 * 1000 }); // Cache just for 30sec
const fs = require('fs');
const shebangCommand = require('shebang-command');
function readShebang(command) {
var buffer;
var fd;
var shebang;
// Read the first 150 bytes from the file
const size = 150;
let buffer;
// Check if it is in the cache first
if (shebangCache.has(command)) {
return shebangCache.get(command);
if (Buffer.alloc) {
// Node.js v4.5+ / v5.10+
buffer = Buffer.alloc(size);
} else {
// Old Node.js API
buffer = new Buffer(size);
buffer.fill(0); // zero-fill
}
// Read the first 150 bytes from the file
buffer = new Buffer(150);
let fd;
try {
fd = fs.openSync(command, 'r');
fs.readSync(fd, buffer, 0, 150, 0);
fs.readSync(fd, buffer, 0, size, 0);
fs.closeSync(fd);
} catch (e) { /* empty */ }
} catch (e) { /* Empty */ }
// Attempt to extract shebang (null is returned if not a shebang)
shebang = shebangCommand(buffer.toString());
// Store the shebang in the cache
shebangCache.set(command, shebang);
return shebang;
return shebangCommand(buffer.toString());
}
module.exports = readShebang;