serverless-spy 1.2.0 → 1.3.1
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/listener/ServerlessSpyListener.d.ts +4 -1
- package/lib/listener/ServerlessSpyListener.js +1 -1
- package/lib/listener/ServerlessSpyListener.mjs +1 -1
- package/lib/listener/SpyHandlers.ts.d.ts +22 -0
- package/lib/listener/SpyHandlers.ts.js +1 -1
- package/lib/listener/SpyHandlers.ts.mjs +1 -1
- package/lib/src/ServerlessSpy.d.ts +1 -0
- package/lib/src/ServerlessSpy.js +20 -8
- package/lib/src/ServerlessSpy.mjs +19 -7
- package/listener/ServerlessSpyListener.ts +9 -0
- package/listener/SpyHandlers.ts.ts +50 -0
- 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
package/extension/aws/Errors.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
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/Errors/index.ts
|
|
8
|
-
*
|
|
9
|
-
* Defines custom error types throwable by the runtime.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
'use strict';
|
|
13
|
-
|
|
14
|
-
import util from 'util';
|
|
15
|
-
|
|
16
|
-
export function isError(obj: any): obj is Error {
|
|
17
|
-
return (
|
|
18
|
-
obj &&
|
|
19
|
-
obj.name &&
|
|
20
|
-
obj.message &&
|
|
21
|
-
obj.stack &&
|
|
22
|
-
typeof obj.name === 'string' &&
|
|
23
|
-
typeof obj.message === 'string' &&
|
|
24
|
-
typeof obj.stack === 'string'
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface RuntimeErrorResponse {
|
|
29
|
-
errorType: string;
|
|
30
|
-
errorMessage: string;
|
|
31
|
-
trace: string[];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Attempt to convert an object into a response object.
|
|
36
|
-
* This method accounts for failures when serializing the error object.
|
|
37
|
-
*/
|
|
38
|
-
export function toRuntimeResponse(error: unknown): RuntimeErrorResponse {
|
|
39
|
-
try {
|
|
40
|
-
if (util.types.isNativeError(error) || isError(error)) {
|
|
41
|
-
if (!error.stack) {
|
|
42
|
-
throw new Error('Error stack is missing.');
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
45
|
-
errorType: error.name,
|
|
46
|
-
errorMessage: error.message,
|
|
47
|
-
trace: error.stack.split('\n') || [],
|
|
48
|
-
};
|
|
49
|
-
} else {
|
|
50
|
-
return {
|
|
51
|
-
errorType: typeof error,
|
|
52
|
-
errorMessage: (error as any).toString(),
|
|
53
|
-
trace: [],
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
} catch (_err) {
|
|
57
|
-
return {
|
|
58
|
-
errorType: 'handled',
|
|
59
|
-
errorMessage:
|
|
60
|
-
'callback called with Error argument, but there was a problem while retrieving one or more of its message, name, and stack',
|
|
61
|
-
trace: [],
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Format an error with the expected properties.
|
|
68
|
-
* For compatability, the error string always starts with a tab.
|
|
69
|
-
*/
|
|
70
|
-
export const toFormatted = (error: unknown): string => {
|
|
71
|
-
try {
|
|
72
|
-
return (
|
|
73
|
-
'\t' + JSON.stringify(error, (_k, v) => _withEnumerableProperties(v))
|
|
74
|
-
);
|
|
75
|
-
} catch (err) {
|
|
76
|
-
return '\t' + JSON.stringify(toRuntimeResponse(error));
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Error name, message, code, and stack are all members of the superclass, which
|
|
82
|
-
* means they aren't enumerable and don't normally show up in JSON.stringify.
|
|
83
|
-
* This method ensures those interesting properties are available along with any
|
|
84
|
-
* user-provided enumerable properties.
|
|
85
|
-
*/
|
|
86
|
-
function _withEnumerableProperties(error: any) {
|
|
87
|
-
if (error instanceof Error) {
|
|
88
|
-
const extendedError: ExtendedError = <ExtendedError>(<any>error);
|
|
89
|
-
const ret: any = Object.assign(
|
|
90
|
-
{
|
|
91
|
-
errorType: extendedError.name,
|
|
92
|
-
errorMessage: extendedError.message,
|
|
93
|
-
code: extendedError.code,
|
|
94
|
-
},
|
|
95
|
-
extendedError
|
|
96
|
-
);
|
|
97
|
-
if (typeof extendedError.stack === 'string') {
|
|
98
|
-
ret.stack = extendedError.stack.split('\n');
|
|
99
|
-
}
|
|
100
|
-
return ret;
|
|
101
|
-
} else {
|
|
102
|
-
return error;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export class ExtendedError extends Error {
|
|
107
|
-
code?: number;
|
|
108
|
-
custom?: string;
|
|
109
|
-
reason?: string;
|
|
110
|
-
promise?: Promise<any>;
|
|
111
|
-
|
|
112
|
-
constructor(reason?: string) {
|
|
113
|
-
super(reason); // 'Error' breaks prototype chain here
|
|
114
|
-
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export class ImportModuleError extends ExtendedError {}
|
|
119
|
-
export class HandlerNotFound extends ExtendedError {}
|
|
120
|
-
export class MalformedHandlerName extends ExtendedError {}
|
|
121
|
-
export class UserCodeSyntaxError extends ExtendedError {}
|
|
122
|
-
export class UnhandledPromiseRejection extends ExtendedError {
|
|
123
|
-
constructor(reason?: string, promise?: Promise<any>) {
|
|
124
|
-
super(reason);
|
|
125
|
-
this.reason = reason;
|
|
126
|
-
this.promise = promise;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const errorClasses = [
|
|
131
|
-
ImportModuleError,
|
|
132
|
-
HandlerNotFound,
|
|
133
|
-
MalformedHandlerName,
|
|
134
|
-
UserCodeSyntaxError,
|
|
135
|
-
UnhandledPromiseRejection,
|
|
136
|
-
];
|
|
137
|
-
|
|
138
|
-
errorClasses.forEach((e) => {
|
|
139
|
-
e.prototype.name = `Runtime.${e.name}`;
|
|
140
|
-
});
|
|
@@ -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
|
-
};
|