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

serverless-heaven / serverless-webpack / 24782393027

22 Apr 2026 01:56PM UTC coverage: 92.964% (+0.2%) from 92.749%
24782393027

push

github

web-flow
Merge pull request #2409 from serverless-heaven/fix/remove-bluebird

Remove bluebird and use native Promise

1060 of 1186 branches covered (89.38%)

Branch coverage included in aggregate %.

607 of 636 new or added lines in 13 files covered. (95.44%)

2600 of 2751 relevant lines covered (94.51%)

74.93 hits per line

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

86.43
/lib/utils.js
1
const childProcess = require('node:child_process');
2✔
2

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

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

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

×
NEW
34
    while (modStack.length > 0) {
×
35
      const mod = modStack.pop();
×
NEW
36
      if (!visitedModules.includes(mod)) {
×
37
        visitedModules.push(mod);
×
38
        Array.prototype.push.apply(modStack, mod.children);
×
39
        processor(mod);
×
40
      }
×
41
    }
×
42
  }
×
NEW
43
  return Promise.resolve();
×
44
}
×
45

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

2✔
53
  toString() {
2✔
54
    return `${this.message}\n${this.stderr}`;
2✔
55
  }
2✔
56
}
2✔
57

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

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

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

2✔
112
function splitLines(str) {
10✔
113
  return str.split(/\r?\n/);
10✔
114
}
10✔
115

2✔
116
function isNodeRuntime(runtime) {
334✔
117
  return runtime.match(/node/);
334✔
118
}
334✔
119

2✔
120
function getAllNodeFunctions() {
126✔
121
  const functions = this.serverless.service.getAllFunctions();
126✔
122
  const ecrImages = Object.keys(this.serverless.service.provider?.ecr?.images || []);
126✔
123

126✔
124
  return functions.filter(funcName => {
126✔
125
    const func = this.serverless.service.getFunction(funcName);
338✔
126
    const imageName = func.image?.name;
338✔
127
    const isEcrImage = imageName && ecrImages.includes(imageName);
338✔
128

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

272✔
135
    return isNodeRuntime(func.runtime || this.serverless.service.provider.runtime || 'nodejs');
338✔
136
  });
126✔
137
}
126✔
138

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

2✔
149
module.exports = {
2✔
150
  guid,
2✔
151
  purgeCache,
2✔
152
  searchAndProcessCache,
2✔
153
  SpawnError,
2✔
154
  spawnProcess,
2✔
155
  safeJsonParse,
2✔
156
  splitLines,
2✔
157
  getAllNodeFunctions,
2✔
158
  isNodeRuntime,
2✔
159
  isProviderGoogle
2✔
160
};
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc