serverless-spy 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsii +3 -3
- package/README.md +15 -12
- package/cli/cli.ts +8 -1
- package/dist/releasetag.txt +1 -1
- package/extension/aws/Errors.js +124 -0
- package/extension/aws/HttpResponseStream.js +36 -0
- package/extension/aws/UserFunction.js +324 -0
- package/extension/aws/VerboseLog.js +53 -0
- package/extension/interceptor.ts +8 -7
- package/lib/cli/cli.js +7 -2
- package/lib/cli/cli.mjs +7 -2
- package/lib/cli/cli.ts +8 -1
- package/lib/extension/dist/layer/nodejs/node_modules/interceptor.js +531 -274
- package/lib/extension/dist/layer/nodejs/node_modules/interceptor.js.map +4 -4
- package/lib/src/ServerlessSpy.d.ts +1 -0
- package/lib/src/ServerlessSpy.js +20 -8
- package/lib/src/ServerlessSpy.mjs +19 -7
- package/package.json +1 -1
- package/doc/Contributors.md +0 -4
- package/extension/aws/Common.ts +0 -88
- package/extension/aws/Errors.ts +0 -140
- package/extension/aws/UserFunction.ts +0 -169
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
/**
|
|
4
|
-
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
|
-
*
|
|
6
|
-
* This code was copied from:
|
|
7
|
-
* https://raw.githubusercontent.com/aws/aws-lambda-nodejs-runtime-interface-client/main/src/utils/UserFunction.ts
|
|
8
|
-
*
|
|
9
|
-
* This module defines the functions for loading the user's code as specified
|
|
10
|
-
* in a handler string.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
'use strict';
|
|
14
|
-
|
|
15
|
-
import fs from 'fs';
|
|
16
|
-
import path from 'path';
|
|
17
|
-
import { HandlerFunction } from './Common';
|
|
18
|
-
import {
|
|
19
|
-
HandlerNotFound,
|
|
20
|
-
MalformedHandlerName,
|
|
21
|
-
ImportModuleError,
|
|
22
|
-
UserCodeSyntaxError,
|
|
23
|
-
} from './Errors';
|
|
24
|
-
|
|
25
|
-
const FUNCTION_EXPR = /^([^.]*)\.(.*)$/;
|
|
26
|
-
const RELATIVE_PATH_SUBSTRING = '..';
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Break the full handler string into two pieces, the module root and the actual
|
|
30
|
-
* handler string.
|
|
31
|
-
* Given './somepath/something/module.nestedobj.handler' this returns
|
|
32
|
-
* ['./somepath/something', 'module.nestedobj.handler']
|
|
33
|
-
*/
|
|
34
|
-
function _moduleRootAndHandler(fullHandlerString: string): [string, string] {
|
|
35
|
-
const handlerString = path.basename(fullHandlerString);
|
|
36
|
-
const moduleRoot = fullHandlerString.substring(
|
|
37
|
-
0,
|
|
38
|
-
fullHandlerString.indexOf(handlerString)
|
|
39
|
-
);
|
|
40
|
-
return [moduleRoot, handlerString];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Split the handler string into two pieces: the module name and the path to
|
|
45
|
-
* the handler function.
|
|
46
|
-
*/
|
|
47
|
-
function _splitHandlerString(handler: string): [string, string] {
|
|
48
|
-
const match = handler.match(FUNCTION_EXPR);
|
|
49
|
-
// eslint-disable-next-line eqeqeq
|
|
50
|
-
if (!match || match.length != 3) {
|
|
51
|
-
throw new MalformedHandlerName('Bad handler');
|
|
52
|
-
}
|
|
53
|
-
return [match[1], match[2]]; // [module, function-path]
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Resolve the user's handler function from the module.
|
|
58
|
-
*/
|
|
59
|
-
function _resolveHandler(object: any, nestedProperty: string): any {
|
|
60
|
-
return nestedProperty.split('.').reduce((nested, key) => {
|
|
61
|
-
return nested && nested[key];
|
|
62
|
-
}, object);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Verify that the provided path can be loaded as a file per:
|
|
67
|
-
* https://nodejs.org/dist/latest-v10.x/docs/api/modules.html#modules_all_together
|
|
68
|
-
* @param string - the fully resolved file path to the module
|
|
69
|
-
* @return bool
|
|
70
|
-
*/
|
|
71
|
-
function _canLoadAsFile(modulePath: string): boolean {
|
|
72
|
-
return fs.existsSync(modulePath) || fs.existsSync(modulePath + '.js');
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Attempt to load the user's module.
|
|
77
|
-
* Attempts to directly resolve the module relative to the application root,
|
|
78
|
-
* then falls back to the more general require().
|
|
79
|
-
*/
|
|
80
|
-
function _tryRequire(appRoot: string, moduleRoot: string, module: string): any {
|
|
81
|
-
const lambdaStylePath = path.resolve(appRoot, moduleRoot, module);
|
|
82
|
-
if (_canLoadAsFile(lambdaStylePath)) {
|
|
83
|
-
return require(lambdaStylePath);
|
|
84
|
-
} else {
|
|
85
|
-
// Why not just require(module)?
|
|
86
|
-
// Because require() is relative to __dirname, not process.cwd()
|
|
87
|
-
const nodeStylePath = require.resolve(module, {
|
|
88
|
-
paths: [appRoot, moduleRoot],
|
|
89
|
-
});
|
|
90
|
-
return require(nodeStylePath);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Load the user's application or throw a descriptive error.
|
|
96
|
-
* @throws Runtime errors in two cases
|
|
97
|
-
* 1 - UserCodeSyntaxError if there's a syntax error while loading the module
|
|
98
|
-
* 2 - ImportModuleError if the module cannot be found
|
|
99
|
-
*/
|
|
100
|
-
function _loadUserApp(
|
|
101
|
-
appRoot: string,
|
|
102
|
-
moduleRoot: string,
|
|
103
|
-
module: string
|
|
104
|
-
): any {
|
|
105
|
-
try {
|
|
106
|
-
return _tryRequire(appRoot, moduleRoot, module);
|
|
107
|
-
} catch (e: any) {
|
|
108
|
-
if (e instanceof SyntaxError) {
|
|
109
|
-
throw new UserCodeSyntaxError(<any>e);
|
|
110
|
-
} else if (e.code !== undefined && e.code === 'MODULE_NOT_FOUND') {
|
|
111
|
-
throw new ImportModuleError(e);
|
|
112
|
-
} else {
|
|
113
|
-
throw e;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function _throwIfInvalidHandler(fullHandlerString: string): void {
|
|
119
|
-
if (fullHandlerString.includes(RELATIVE_PATH_SUBSTRING)) {
|
|
120
|
-
throw new MalformedHandlerName(
|
|
121
|
-
`'${fullHandlerString}' is not a valid handler name. Use absolute paths when specifying root directories in handler names.`
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Load the user's function with the approot and the handler string.
|
|
128
|
-
* @param appRoot {string}
|
|
129
|
-
* The path to the application root.
|
|
130
|
-
* @param handlerString {string}
|
|
131
|
-
* The user-provided handler function in the form 'module.function'.
|
|
132
|
-
* @return userFuction {function}
|
|
133
|
-
* The user's handler function. This function will be passed the event body,
|
|
134
|
-
* the context object, and the callback function.
|
|
135
|
-
* @throws In five cases:-
|
|
136
|
-
* 1 - if the handler string is incorrectly formatted an error is thrown
|
|
137
|
-
* 2 - if the module referenced by the handler cannot be loaded
|
|
138
|
-
* 3 - if the function in the handler does not exist in the module
|
|
139
|
-
* 4 - if a property with the same name, but isn't a function, exists on the
|
|
140
|
-
* module
|
|
141
|
-
* 5 - the handler includes illegal character sequences (like relative paths
|
|
142
|
-
* for traversing up the filesystem '..')
|
|
143
|
-
* Errors for scenarios known by the runtime, will be wrapped by Runtime.* errors.
|
|
144
|
-
*/
|
|
145
|
-
export const load = function (
|
|
146
|
-
appRoot: string,
|
|
147
|
-
fullHandlerString: string
|
|
148
|
-
): HandlerFunction {
|
|
149
|
-
_throwIfInvalidHandler(fullHandlerString);
|
|
150
|
-
|
|
151
|
-
const [moduleRoot, moduleAndHandler] =
|
|
152
|
-
_moduleRootAndHandler(fullHandlerString);
|
|
153
|
-
const [module, handlerPath] = _splitHandlerString(moduleAndHandler);
|
|
154
|
-
|
|
155
|
-
const userApp = _loadUserApp(appRoot, moduleRoot, module);
|
|
156
|
-
const handlerFunc = _resolveHandler(userApp, handlerPath);
|
|
157
|
-
|
|
158
|
-
if (!handlerFunc) {
|
|
159
|
-
throw new HandlerNotFound(
|
|
160
|
-
`${fullHandlerString} is undefined or not exported`
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (typeof handlerFunc !== 'function') {
|
|
165
|
-
throw new HandlerNotFound(`${fullHandlerString} is not a function`);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return handlerFunc;
|
|
169
|
-
};
|