|
18 | 18 | * @packageDocumentation |
19 | 19 | */ |
20 | 20 |
|
| 21 | +import * as path from 'path'; |
| 22 | +import * as semver from 'semver'; |
| 23 | +import * as readPkgUp from 'read-pkg-up'; |
| 24 | +import {pathToFileURL} from 'url'; |
21 | 25 | /** |
22 | 26 | * Import function signature type's definition. |
23 | 27 | */ |
24 | 28 | import {HandlerFunction} from './functions'; |
25 | 29 |
|
| 30 | +// Dynamic import function required to load user code packaged as an |
| 31 | +// ES module is only available on Node.js v13.2.0 and up. |
| 32 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility |
| 33 | +// Exported for testing. |
| 34 | +export const MIN_NODE_VERSION_ESMODULES = '13.2.0'; |
| 35 | + |
| 36 | +/** |
| 37 | + * Determines whether the given module is an ES module. |
| 38 | + * |
| 39 | + * Implements "algorithm" described at: |
| 40 | + * https://nodejs.org/api/packages.html#packages_type |
| 41 | + * |
| 42 | + * In words: |
| 43 | + * 1. A module with .mjs extension is an ES module. |
| 44 | + * 2. A module with .clj extension is not an ES module. |
| 45 | + * 3. A module with .js extensions where the nearest package.json's |
| 46 | + * with "type": "module" is an ES module. |
| 47 | + * 4. Otherwise, it is not an ES module. |
| 48 | + * |
| 49 | + * @returns {Promise<boolean>} True if module is an ES module. |
| 50 | + */ |
| 51 | +async function isEsModule(modulePath: string): Promise<boolean> { |
| 52 | + const ext = path.extname(modulePath); |
| 53 | + if (ext === '.mjs') { |
| 54 | + return true; |
| 55 | + } |
| 56 | + if (ext === '.cjs') { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + const pkg = await readPkgUp({ |
| 61 | + cwd: path.dirname(modulePath), |
| 62 | + normalize: false, |
| 63 | + }); |
| 64 | + |
| 65 | + // If package.json specifies type as 'module', it's an ES module. |
| 66 | + return pkg?.packageJson.type === 'module'; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Dynamically load import function to prevent TypeScript from |
| 71 | + * transpiling into a require. |
| 72 | + * |
| 73 | + * See https://github.com/microsoft/TypeScript/issues/43329. |
| 74 | + */ |
| 75 | +const dynamicImport = new Function( |
| 76 | + 'modulePath', |
| 77 | + 'return import(modulePath)' |
| 78 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 79 | +) as (modulePath: string) => Promise<any>; |
| 80 | + |
26 | 81 | /** |
27 | 82 | * Returns user's function from function file. |
28 | 83 | * Returns null if function can't be retrieved. |
29 | 84 | * @return User's function or null. |
30 | 85 | */ |
31 | | -export function getUserFunction( |
| 86 | +export async function getUserFunction( |
32 | 87 | codeLocation: string, |
33 | 88 | functionTarget: string |
34 | | -): HandlerFunction | null { |
| 89 | +): Promise<HandlerFunction | null> { |
35 | 90 | try { |
36 | 91 | const functionModulePath = getFunctionModulePath(codeLocation); |
37 | 92 | if (functionModulePath === null) { |
38 | 93 | console.error('Provided code is not a loadable module.'); |
39 | 94 | return null; |
40 | 95 | } |
41 | 96 |
|
42 | | - // eslint-disable-next-line @typescript-eslint/no-var-requires |
43 | | - const functionModule = require(functionModulePath); |
| 97 | + let functionModule; |
| 98 | + const esModule = await isEsModule(functionModulePath); |
| 99 | + if (esModule) { |
| 100 | + if (semver.lt(process.version, MIN_NODE_VERSION_ESMODULES)) { |
| 101 | + console.error( |
| 102 | + `Cannot load ES Module on Node.js ${process.version}. ` + |
| 103 | + `Please upgrade to Node.js v${MIN_NODE_VERSION_ESMODULES} and up.` |
| 104 | + ); |
| 105 | + return null; |
| 106 | + } |
| 107 | + // Resolve module path to file:// URL. Required for windows support. |
| 108 | + const fpath = pathToFileURL(functionModulePath); |
| 109 | + functionModule = await dynamicImport(fpath.href); |
| 110 | + } else { |
| 111 | + functionModule = require(functionModulePath); |
| 112 | + } |
| 113 | + |
44 | 114 | let userFunction = functionTarget |
45 | 115 | .split('.') |
46 | 116 | .reduce((code, functionTargetPart) => { |
|
0 commit comments