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

serverless-heaven / serverless-webpack / 13759990655

10 Mar 2025 08:21AM UTC coverage: 92.399% (-0.09%) from 92.492%
13759990655

push

github

web-flow
Merge pull request #2062 from serverless-heaven/dependabot/npm_and_yarn/examples/typescript/axios-1.8.2

build(deps): bump axios from 1.7.7 to 1.8.2 in /examples/typescript

984 of 1106 branches covered (88.97%)

Branch coverage included in aggregate %.

2 of 4 new or added lines in 1 file covered. (50.0%)

2578 of 2749 relevant lines covered (93.78%)

70.72 hits per line

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

84.29
/lib/utils.js
1
'use strict';
2✔
2

2✔
3
const _ = require('lodash');
2✔
4
const BbPromise = require('bluebird');
2✔
5
const childProcess = require('child_process');
2✔
6

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

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

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

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

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

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

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

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

2✔
108
function safeJsonParse(str) {
34✔
109
  try {
34✔
110
    return JSON.parse(str);
34✔
111
  } catch (e) {
34✔
112
    return null;
2✔
113
  }
2✔
114
}
34✔
115

2✔
116
function splitLines(str) {
8✔
117
  return _.split(str, /\r?\n/);
8✔
118
}
8✔
119

2✔
120
function isNodeRuntime(runtime) {
360✔
121
  return runtime.match(/node/);
360✔
122
}
360✔
123

2✔
124
function getAllNodeFunctions() {
118✔
125
  const functions = this.serverless.service.getAllFunctions();
118✔
126

118✔
127
  return _.filter(functions, funcName => {
118✔
128
    const func = this.serverless.service.getFunction(funcName);
320✔
129

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

288✔
136
    return isNodeRuntime(func.runtime || this.serverless.service.provider.runtime || 'nodejs');
320✔
137
  });
118✔
138
}
118✔
139

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

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