-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Activate ESM loader hook of the "import-in-the-middle" library for ES…
… (EcmaScript) based user handlers (#1574) * Activate ESM loader hook of the "import-in-the-middle" library for ES (EcmaScript) based user handlers * Add tests
- Loading branch information
1 parent
990e881
commit 8d8e317
Showing
15 changed files
with
506 additions
and
168 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { register } from 'module'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
function _hasFolderPackageJsonTypeModule(folder) { | ||
if (folder.endsWith('/node_modules')) { | ||
return false; | ||
} | ||
let pj = path.join(folder, '/package.json'); | ||
if (fs.existsSync(pj)) { | ||
try { | ||
let pkg = JSON.parse(fs.readFileSync(pj).toString()); | ||
if (pkg) { | ||
if (pkg.type === 'module') { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} catch (e) { | ||
console.warn(`${pj} cannot be read, it will be ignored for ES module detection purposes.`, e); | ||
return false; | ||
} | ||
} | ||
if (folder === '/') { | ||
return false; | ||
} | ||
return _hasFolderPackageJsonTypeModule(path.resolve(folder, '..')); | ||
} | ||
|
||
function _hasPackageJsonTypeModule(file) { | ||
let jsPath = file + '.js'; | ||
if (fs.existsSync(jsPath)) { | ||
return _hasFolderPackageJsonTypeModule(path.resolve(path.dirname(jsPath))); | ||
} | ||
return false; | ||
} | ||
|
||
function _resolveHandlerFileName() { | ||
const taskRoot = process.env.LAMBDA_TASK_ROOT; | ||
const handlerDef = process.env._HANDLER; | ||
if (!taskRoot || !handlerDef) { | ||
return null; | ||
} | ||
const handler = path.basename(handlerDef); | ||
const moduleRoot = handlerDef.substr(0, handlerDef.length - handler.length); | ||
const [module, _] = handler.split('.', 2); | ||
return path.resolve(taskRoot, moduleRoot, module); | ||
} | ||
|
||
function _isHandlerAnESModule() { | ||
try { | ||
const handlerFileName = _resolveHandlerFileName(); | ||
if (!handlerFileName) { | ||
return false; | ||
} | ||
if (fs.existsSync(handlerFileName + '.mjs')) { | ||
return true; | ||
} else if (fs.existsSync(handlerFileName + '.cjs')) { | ||
return false; | ||
} else { | ||
return _hasPackageJsonTypeModule(handlerFileName); | ||
} | ||
} catch (e) { | ||
console.error('Unknown error occurred while checking whether handler is an ES module', e); | ||
return false; | ||
} | ||
} | ||
|
||
let registered = false; | ||
|
||
export function registerLoader() { | ||
if (!registered) { | ||
register('import-in-the-middle/hook.mjs', import.meta.url); | ||
registered = true; | ||
} | ||
} | ||
|
||
if (_isHandlerAnESModule()) { | ||
/* | ||
We could activate ESM loader hook of the "import-in-the-middle" library, | ||
- by "--loader=import-in-the-middle/hook.mjs" Node CLI option, but "--loader" option has been deprecated | ||
- or by "--import=import-in-the-middle/hook.mjs" Node CLI option, but in this case, | ||
there will always be "import-in-the-middle" hook initialization overhead even for non-ESM (CommonJS) modules | ||
Hence, instead, we initialize "import-in-the-middle" hook only for ES (EcmaScript) based user handlers | ||
to prevent redundant "import-in-the-middle" hook initialization overhead during coldstart | ||
of the CommonJS based user handlers. | ||
*/ | ||
registerLoader(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.