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

serverless-heaven / serverless-webpack / 22627166981

03 Mar 2026 02:19PM UTC coverage: 92.728%. First build
22627166981

Pull #2356

github

web-flow
Merge c21a8e86d into 10eb641b5
Pull Request #2356: Drop Node 18 support

1001 of 1118 branches covered (89.53%)

Branch coverage included in aggregate %.

38 of 41 new or added lines in 16 files covered. (92.68%)

2544 of 2705 relevant lines covered (94.05%)

71.81 hits per line

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

86.06
/lib/utils.js
1
const _ = require('lodash');
2✔
2
const BbPromise = require('bluebird');
2✔
3
const childProcess = require('node:child_process');
2✔
4

2✔
5
function guid() {
20✔
6
  function s4() {
20✔
7
    return Math.floor((1 + Math.random()) * 0x10000)
160✔
8
      .toString(16)
160✔
9
      .substring(1);
160✔
10
  }
160✔
11
  return `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
20✔
12
}
20✔
13

2✔
14
/**
2✔
15
 * Remove the specified module from the require cache.
2✔
16
 * @param {string} moduleName
2✔
17
 */
2✔
18
function purgeCache(moduleName) {
×
NEW
19
  return searchAndProcessCache(moduleName, mod => {
×
20
    delete require.cache[mod.id];
×
21
  }).then(() => {
×
NEW
22
    _.forEach(_.keys(module.constructor._pathCache), cacheKey => {
×
23
      if (cacheKey.indexOf(moduleName) > 0) {
×
24
        delete module.constructor._pathCache[cacheKey];
×
25
      }
×
26
    });
×
27
    return BbPromise.resolve();
×
28
  });
×
29
}
×
30

2✔
31
function searchAndProcessCache(moduleName, processor) {
×
32
  let mod_src = require.resolve(moduleName);
×
33
  const visitedModules = [];
×
34
  if (mod_src && (mod_src = require.cache[mod_src]) !== undefined) {
×
35
    const modStack = [mod_src];
×
36

×
37
    while (!_.isEmpty(modStack)) {
×
38
      const mod = modStack.pop();
×
39
      if (!_.includes(visitedModules, mod)) {
×
40
        visitedModules.push(mod);
×
41
        Array.prototype.push.apply(modStack, mod.children);
×
42
        processor(mod);
×
43
      }
×
44
    }
×
45
  }
×
46
  return BbPromise.resolve();
×
47
}
×
48

2✔
49
class SpawnError extends Error {
2✔
50
  constructor(message, stdout, stderr) {
2✔
51
    super(message);
16✔
52
    this.stdout = stdout;
16✔
53
    this.stderr = stderr;
16✔
54
  }
16✔
55

2✔
56
  toString() {
2✔
57
    return `${this.message}\n${this.stderr}`;
2✔
58
  }
2✔
59
}
2✔
60

2✔
61
/**
2✔
62
 * Executes a child process without limitations on stdout and stderr.
2✔
63
 * On error (exit code is not 0), it rejects with a SpawnProcessError that contains the stdout and stderr streams,
2✔
64
 * on success it returns the streams in an object.
2✔
65
 * @param {string} command - Command
2✔
66
 * @param {string[]} [args] - Arguments
2✔
67
 * @param {Object} [options] - Options for child_process.spawn
2✔
68
 */
2✔
69
function spawnProcess(command, args, options) {
44✔
70
  return new BbPromise((resolve, reject) => {
44✔
71
    const child = childProcess.spawn(command, args, {
44✔
72
      ...options,
44✔
73
      // nodejs 20 on windows doesn't allow `.cmd` command to run without `shell: true`
44✔
74
      // https://github.com/serverless-heaven/serverless-webpack/issues/1791
44✔
75
      shell: /^win/.test(process.platform)
44✔
76
    });
44✔
77
    let stdout = '';
44✔
78
    let stderr = '';
44✔
79
    // Configure stream encodings
44✔
80
    child.stdout.setEncoding('utf8');
44✔
81
    child.stderr.setEncoding('utf8');
44✔
82
    // Listen to stream events
44✔
83
    child.stdout.on('data', data => {
44✔
84
      stdout += data;
188✔
85
    });
44✔
86
    child.stderr.on('data', data => {
44✔
87
      stderr += data;
34✔
88
    });
44✔
89
    child.on('error', err => {
44✔
90
      if (process.env.NODE_ENV === 'test') {
2✔
91
        console.error(err);
2✔
92
      }
2✔
93

2✔
94
      reject(err);
2✔
95
    });
44✔
96
    child.on('close', exitCode => {
44✔
97
      if (exitCode !== 0) {
42✔
98
        reject(new SpawnError(`${command} ${_.join(args, ' ')} failed with code ${exitCode}`, stdout, stderr));
2✔
99
      } else {
42✔
100
        resolve({ stdout, stderr });
40✔
101
      }
40✔
102
    });
44✔
103
  });
44✔
104
}
44✔
105

2✔
106
function safeJsonParse(str) {
48✔
107
  try {
48✔
108
    return JSON.parse(str);
48✔
109
  } catch (_e) {
48✔
110
    return null;
8✔
111
  }
8✔
112
}
48✔
113

2✔
114
function splitLines(str) {
10✔
115
  return _.split(str, /\r?\n/);
10✔
116
}
10✔
117

2✔
118
function isNodeRuntime(runtime) {
330✔
119
  return runtime.match(/node/);
330✔
120
}
330✔
121

2✔
122
function getAllNodeFunctions() {
124✔
123
  const functions = this.serverless.service.getAllFunctions();
124✔
124

124✔
125
  return _.filter(functions, funcName => {
124✔
126
    const func = this.serverless.service.getFunction(funcName);
334✔
127

334✔
128
    // if `uri` or `name` is provided or simple remote image path, it means the
334✔
129
    // image isn't built by Serverless so we shouldn't take care of it
334✔
130
    if ((func.image && (func.image.uri || func.image.name)) || (func.image && typeof func.image === 'string')) {
334✔
131
      return false;
66✔
132
    }
66✔
133

268✔
134
    return isNodeRuntime(func.runtime || this.serverless.service.provider.runtime || 'nodejs');
334✔
135
  });
124✔
136
}
124✔
137

2✔
138
/**
2✔
139
 * Given a serverless instance, will return whether the provider is google (as opposed to another
2✔
140
 * provider like 'aws')
2✔
141
 * @param {*} serverless the serverless instance that holds the configuration
2✔
142
 * @returns true if the provider is google, otherwise false
2✔
143
 */
2✔
144
function isProviderGoogle(serverless) {
76✔
145
  return _.get(serverless, 'service.provider.name') === 'google';
76✔
146
}
76✔
147

2✔
148
module.exports = {
2✔
149
  guid,
2✔
150
  purgeCache,
2✔
151
  searchAndProcessCache,
2✔
152
  SpawnError,
2✔
153
  spawnProcess,
2✔
154
  safeJsonParse,
2✔
155
  splitLines,
2✔
156
  getAllNodeFunctions,
2✔
157
  isNodeRuntime,
2✔
158
  isProviderGoogle
2✔
159
};
2✔
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