• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nodeshift / faas-js-runtime / 16341850588

17 Jul 2025 09:48AM UTC coverage: 55.699% (-31.8%) from 87.527%
16341850588

Pull #421

github

web-flow
Merge c3bb8e82a into eb6047b08
Pull Request #421: [Snyk] Upgrade fastify from 4.21.0 to 5.4.0

64 of 146 branches covered (43.84%)

Branch coverage included in aggregate %.

195 of 319 relevant lines covered (61.13%)

13.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

11.76
/lib/function-loader.js
1
const fs = require('fs');
6✔
2
const path = require('path');
6✔
3

4
// This is the way to import es modules inside a CJS module
5
const dynamicImport = new Function('modulePath', 'return import(modulePath)');
6✔
6

7
async function loadFunction(filePath) {
8
  if (isESM(filePath)) {
×
9
    if (process.platform === 'win32') {
×
10
      // Windows requires the file path to be a URL
11
      filePath = `file:///${filePath}`;
×
12
    }
13
    code = await dynamicImport(filePath);
×
14
  } else {
15
    code = require(filePath);
×
16
  }
17

18
  return code;
×
19
}
20

21
// https://nodejs.org/dist/latest-v18.x/docs/api/packages.html#determining-module-system
22
// An ESM module can be determined 2 ways
23
// 1. has the mjs file extention
24
// 2. type=module in the package.json
25
function isESM(filePath) {
26
  const pathParsed = path.parse(filePath);
×
27

28
  if (pathParsed.ext === '.mjs') {
×
29
    return true;
×
30
  }
31

32
  // find the functions package.json and see if it has a type field
33
  // look in the parent directory if the function is in a subdirectory
34
  let dir = pathParsed.dir;
×
35
  const rootDir = path.parse(process.cwd()).root;
×
36
  while (!hasPackageJson(dir)) {
×
37
    if (dir === rootDir) return false;
×
38
    dir = path.dirname(dir);
×
39
  }
40

41
  // Load the package.json and check the type field
42
  // Put this in a try/catch in case the package.json is invalid
43
  let pkgPath = path.join(dir, 'package.json');
×
44
  try {
×
45
    const functionPkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
×
46
    return functionPkg.type === 'module';
×
47
  } catch(e) {
48
    console.warn(e);
×
49
    return false;
×
50
  }
51
}
52

53
function hasPackageJson(dir) {
54
  const pkgPath = path.join(dir, 'package.json');
×
55
  return fs.existsSync(pkgPath);
×
56
}
57

58
module.exports = exports = { loadFunction };
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc