typescript-require
Advanced tools
Comparing version 0.1.7 to 0.2.0
138
index.js
@@ -0,109 +1,53 @@ | ||
var os = require('os'); | ||
var vm = require('vm'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var io = require("./io"); | ||
var tsc = require.resolve("typescript").replace(/typescript\.js$/, "tsc.js"); | ||
var tscScript = vm.createScript('(function() { ' + fs.readFileSync(tsc, "utf8") + '})();', tsc); | ||
var tsTempFile = null; | ||
['TMPDIR', 'TMP', 'TEMP'].forEach(function(td) { | ||
if(!tsTempFile && process.env[td]) tsTempFile = process.env[td]; | ||
}); | ||
tsTempFile = path.join((tsTempFile || "/tmp"), "typescript-require-" + Date.now() + ".js"); | ||
var contents = ["(function() {", fs.readFileSync(require.resolve("typescript"), "utf8"), "module.exports = TypeScript;", "}).call({});"].join(""); | ||
fs.writeFileSync(tsTempFile, contents, "utf8"); | ||
var TypeScript = module.exports.TypeScript = require(tsTempFile); | ||
TypeScript.moduleGenTarget = TypeScript.ModuleGenTarget.Synchronous; | ||
fs.unlinkSync(tsTempFile); | ||
//setting up the compiler | ||
var settings = new TypeScript.CompilationSettings(); | ||
settings.codeGenTarget = TypeScript.CodeGenTarget.ES5; | ||
settings.moduleGenTarget = TypeScript.ModuleGenTarget.Synchronous; | ||
settings.resolve = true; | ||
var env = new TypeScript.CompilationEnvironment(settings, io); | ||
require.extensions['.ts'] = function(module) { | ||
var js = ''; | ||
var output = { | ||
Write: function(value) { | ||
js += value; | ||
}, | ||
WriteLine: function(value) { | ||
js += value + "\n"; | ||
}, | ||
Close: function() {} | ||
}; | ||
var exitCode = 0; | ||
var tmpDir = path.join(os.tmpDir(), "tsreq_" + Math.floor(Math.random() * 0xFFFFFFFF)); | ||
var nulloutput = { | ||
Write: function(value) {}, | ||
WriteLine: function(value) {}, | ||
Close: function() {} | ||
}; | ||
var compiler = new TypeScript.TypeScriptCompiler(null, new TypeScript.NullLogger(), settings); | ||
compiler.parser.errorRecovery = true; | ||
var units = [{ | ||
fileName: path.join(__dirname, "./typings/lib.d.ts") | ||
}]; | ||
var proc = merge(merge({}, process), { | ||
argv: [ | ||
"node", | ||
"tsc.js", | ||
"--nolib", | ||
"--target", | ||
"ES5", | ||
"--out", | ||
tmpDir, | ||
path.resolve(__dirname, "typings/lib.d.ts"), | ||
path.resolve(__dirname, "typings/node.d.ts"), | ||
module.filename | ||
], | ||
exit: function(code) { | ||
exitCode = code; | ||
} | ||
}); | ||
var moduleFilename = TypeScript.switchToForwardSlashes(module.filename); | ||
var sandbox = { | ||
process: proc, | ||
require: require | ||
}; | ||
var resolver = new TypeScript.CodeResolver(env); | ||
tscScript.runInNewContext(sandbox); | ||
resolver.resolveCode(moduleFilename, "", false, { | ||
postResolution: function(file, code) { | ||
if(!units.some(function(u) { | ||
return u.fileName == code.path | ||
})) units.push({ | ||
fileName: code.path, | ||
code: code.content | ||
}); | ||
}, | ||
postResolutionError: function(file, message) { | ||
throw new Error('TypeScript Error: ' + message + '\n File: ' + file); | ||
} | ||
}); | ||
var isErrorPrinted = false; | ||
if (exitCode != 0) { | ||
return null; | ||
} | ||
compiler.setErrorCallback(function(start, len, message, block) { | ||
var jsname = path.join(tmpDir, path.basename(module.filename, ".ts") + ".js"); | ||
module._compile(fs.readFileSync(jsname, 'utf8'), jsname); | ||
}; | ||
if(isErrorPrinted == false && units[block].fileName == moduleFilename) { | ||
var code = units[block].code; | ||
var cursor = code.substr(0, start).split('\n'); | ||
var line = [ | ||
cursor.slice(-1)[0].replace(/^\s+/, ""), code.substr(start, len), code.substr(start + len).split('\n').slice(0, 1)[0].replace(/\s+$/, "")]; | ||
var underline = [ | ||
line[0].replace(/./g, '-'), line[1].replace(/./g, '^'), line[2].replace(/./g, '-'), ]; | ||
var error = new Error('TypeScript Error: ' + message); | ||
error.stack = ['TypeScript Error: ' + message, 'File: ' + units[block].fileName, '', 'Line '+cursor.length +': '+ line.join(""), '------' + underline.join("")].join('\n'); | ||
console.error('\n' + error.stack); | ||
isErrorPrinted = true; | ||
function merge(a, b) { | ||
if (a && b) { | ||
for (var key in b) { | ||
a[key] = b[key]; | ||
} | ||
}); | ||
units.forEach(function(u) { | ||
if(!u.code) u.code = fs.readFileSync(u.fileName, "utf8"); | ||
compiler.addUnit(u.code, u.fileName, false); | ||
}); | ||
compiler.typeCheck(); | ||
compiler.emit(function(fn) { | ||
count = 0; | ||
if(fn == moduleFilename.replace(/\.ts$/, ".js")) return output; | ||
else return nulloutput; | ||
}); | ||
module._compile(js, moduleFilename); | ||
}; | ||
} | ||
return a; | ||
}; |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"main": "./index.js", | ||
@@ -17,3 +17,3 @@ "repository": { | ||
"dependencies": { | ||
"typescript": ">= 0.8.1-1" | ||
"typescript": ">= 0.8.2" | ||
}, | ||
@@ -20,0 +20,0 @@ "keywords": ["typescript", "require"], |
@@ -1,2 +0,1 @@ | ||
///<reference path='node.d.ts'/> | ||
import tsmodule = module("tsmodule"); | ||
@@ -3,0 +2,0 @@ var jsmodule = require("./jsmodule.js"); |
Sorry, the diff of this file is too big to display
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
304335
8576
3
9
Updatedtypescript@>= 0.8.2