serverless-simple-middleware 0.0.57 → 0.0.59
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/.nvmrc +1 -0
- package/dist/aws/config.d.ts +3 -3
- package/dist/aws/config.js +13 -18
- package/dist/aws/define.js +2 -4
- package/dist/aws/index.d.ts +3 -3
- package/dist/aws/index.js +3 -8
- package/dist/aws/simple.d.ts +12 -11
- package/dist/aws/simple.js +403 -646
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -8
- package/dist/middleware/aws.d.ts +4 -4
- package/dist/middleware/aws.js +38 -125
- package/dist/middleware/base.d.ts +9 -9
- package/dist/middleware/base.js +60 -94
- package/dist/middleware/build.d.ts +2 -2
- package/dist/middleware/build.js +91 -218
- package/dist/middleware/index.d.ts +10 -10
- package/dist/middleware/index.js +16 -21
- package/dist/middleware/logger.d.ts +2 -2
- package/dist/middleware/logger.js +13 -70
- package/dist/middleware/mysql.d.ts +12 -7
- package/dist/middleware/mysql.js +173 -268
- package/dist/middleware/trace.d.ts +2 -2
- package/dist/middleware/trace.js +99 -198
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +2 -7
- package/dist/utils/logger.d.ts +2 -2
- package/dist/utils/logger.js +25 -31
- package/dist/utils/misc.d.ts +1 -1
- package/dist/utils/misc.js +3 -5
- package/package.json +6 -3
- package/src/aws/config.ts +1 -1
- package/src/aws/index.ts +3 -3
- package/src/aws/simple.ts +21 -3
- package/src/index.ts +3 -3
- package/src/middleware/aws.ts +3 -3
- package/src/middleware/base.ts +4 -4
- package/src/middleware/build.ts +3 -3
- package/src/middleware/index.ts +10 -10
- package/src/middleware/logger.ts +2 -2
- package/src/middleware/mysql.ts +74 -7
- package/src/middleware/trace.ts +4 -4
- package/src/utils/index.ts +2 -2
- package/src/utils/logger.ts +1 -1
- package/tsconfig.json +5 -4
package/dist/middleware/trace.js
CHANGED
|
@@ -1,62 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
function __() { this.constructor = d; }
|
|
12
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
15
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
20
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
24
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
25
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
26
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
27
|
-
function step(op) {
|
|
28
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
29
|
-
while (_) try {
|
|
30
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
31
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
32
|
-
switch (op[0]) {
|
|
33
|
-
case 0: case 1: t = op; break;
|
|
34
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
35
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
36
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
37
|
-
default:
|
|
38
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
39
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
40
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
41
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
42
|
-
if (t[2]) _.ops.pop();
|
|
43
|
-
_.trys.pop(); continue;
|
|
44
|
-
}
|
|
45
|
-
op = body.call(thisArg, _);
|
|
46
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
47
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
-
var AWS = require("aws-sdk"); // tslint:disable-line
|
|
52
|
-
var uuid_1 = require("uuid");
|
|
53
|
-
var aws_1 = require("../aws");
|
|
54
|
-
var utils_1 = require("../utils");
|
|
55
|
-
var ts_enum_util_1 = require("ts-enum-util");
|
|
56
|
-
var base_1 = require("./base");
|
|
57
|
-
var logger = utils_1.getLogger(__filename);
|
|
58
|
-
var TracerLog = /** @class */ (function () {
|
|
59
|
-
function TracerLog(route, key, system, action, attribute, body, error, client, version) {
|
|
1
|
+
import * as AWS from 'aws-sdk'; // tslint:disable-line
|
|
2
|
+
import { v4 as uuid4 } from 'uuid';
|
|
3
|
+
import { AWSComponent, loadAWSConfig, SimpleAWS, } from '../aws/index.js';
|
|
4
|
+
import { getLogger } from '../utils/index.js';
|
|
5
|
+
import { stringifyError } from '../utils/index.js';
|
|
6
|
+
import { $enum } from 'ts-enum-util';
|
|
7
|
+
import { HandlerPluginBase } from './base.js';
|
|
8
|
+
const logger = getLogger(__filename);
|
|
9
|
+
export class TracerLog {
|
|
10
|
+
constructor(route, key, system, action, attribute, body, error, client, version) {
|
|
60
11
|
this.route = route;
|
|
61
12
|
this.key = key;
|
|
62
13
|
this.system = system;
|
|
@@ -66,83 +17,56 @@ var TracerLog = /** @class */ (function () {
|
|
|
66
17
|
this.error = error;
|
|
67
18
|
this.client = client;
|
|
68
19
|
this.version = version;
|
|
69
|
-
this.uuid =
|
|
20
|
+
this.uuid = uuid4();
|
|
70
21
|
this.timestamp = Date.now();
|
|
71
22
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
case 3:
|
|
106
|
-
if (!(begin < this.buffer.length)) return [3 /*break*/, 6];
|
|
107
|
-
end = Math.min(this.buffer.length, begin + chunkSize);
|
|
108
|
-
subset = this.buffer.slice(begin, end);
|
|
109
|
-
return [4 /*yield*/, this.sqs
|
|
110
|
-
.sendMessageBatch({
|
|
111
|
-
QueueUrl: eventQueueUrl,
|
|
112
|
-
Entries: subset.map(function (each) { return ({
|
|
113
|
-
Id: each.key + "_" + each.uuid,
|
|
114
|
-
MessageBody: JSON.stringify(each),
|
|
115
|
-
}); }),
|
|
116
|
-
})
|
|
117
|
-
.promise()];
|
|
118
|
-
case 4:
|
|
119
|
-
sendBatchResult = _a.sent();
|
|
120
|
-
logger.stupid("sendBatchResult", sendBatchResult);
|
|
121
|
-
_a.label = 5;
|
|
122
|
-
case 5:
|
|
123
|
-
begin += chunkSize;
|
|
124
|
-
return [3 /*break*/, 3];
|
|
125
|
-
case 6:
|
|
126
|
-
this.buffer = [];
|
|
127
|
-
return [3 /*break*/, 8];
|
|
128
|
-
case 7:
|
|
129
|
-
error_1 = _a.sent();
|
|
130
|
-
logger.warn("Error in eventSource: " + error_1);
|
|
131
|
-
return [3 /*break*/, 8];
|
|
132
|
-
case 8: return [2 /*return*/];
|
|
23
|
+
}
|
|
24
|
+
export class Tracer {
|
|
25
|
+
constructor(queueName, sqs) {
|
|
26
|
+
this.push = (log) => this.buffer.push(log);
|
|
27
|
+
this.flush = async () => {
|
|
28
|
+
if (this.buffer.length === 0) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const urlResult = await this.sqs
|
|
33
|
+
.getQueueUrl({
|
|
34
|
+
QueueName: this.queueName,
|
|
35
|
+
})
|
|
36
|
+
.promise();
|
|
37
|
+
logger.stupid(`urlResult`, urlResult);
|
|
38
|
+
if (!urlResult.QueueUrl) {
|
|
39
|
+
throw new Error(`No queue url with name[${this.queueName}]`);
|
|
40
|
+
}
|
|
41
|
+
const eventQueueUrl = urlResult.QueueUrl;
|
|
42
|
+
const chunkSize = 10;
|
|
43
|
+
for (let begin = 0; begin < this.buffer.length; begin += chunkSize) {
|
|
44
|
+
const end = Math.min(this.buffer.length, begin + chunkSize);
|
|
45
|
+
const subset = this.buffer.slice(begin, end);
|
|
46
|
+
const sendBatchResult = await this.sqs
|
|
47
|
+
.sendMessageBatch({
|
|
48
|
+
QueueUrl: eventQueueUrl,
|
|
49
|
+
Entries: subset.map(each => ({
|
|
50
|
+
Id: `${each.key}_${each.uuid}`,
|
|
51
|
+
MessageBody: JSON.stringify(each),
|
|
52
|
+
})),
|
|
53
|
+
})
|
|
54
|
+
.promise();
|
|
55
|
+
logger.stupid(`sendBatchResult`, sendBatchResult);
|
|
133
56
|
}
|
|
134
|
-
|
|
135
|
-
|
|
57
|
+
this.buffer = [];
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
logger.warn(`Error in eventSource: ${error}`);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
136
63
|
this.queueName = queueName;
|
|
137
64
|
this.sqs = sqs;
|
|
138
65
|
this.buffer = [];
|
|
139
66
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
var TracerWrapper = /** @class */ (function () {
|
|
144
|
-
function TracerWrapper(tracer, route, system, key, action, client, version) {
|
|
145
|
-
var _this = this;
|
|
67
|
+
}
|
|
68
|
+
export class TracerWrapper {
|
|
69
|
+
constructor(tracer, route, system, key, action, client, version) {
|
|
146
70
|
this.tracer = tracer;
|
|
147
71
|
this.route = route;
|
|
148
72
|
this.system = system;
|
|
@@ -150,65 +74,46 @@ var TracerWrapper = /** @class */ (function () {
|
|
|
150
74
|
this.action = action;
|
|
151
75
|
this.client = client;
|
|
152
76
|
this.version = version;
|
|
153
|
-
this.push =
|
|
154
|
-
|
|
155
|
-
_this.tracer.push(new TracerLog(_this.route, _this.key, _this.system, _this.action, attribute, body, error, _this.client, _this.version));
|
|
77
|
+
this.push = (attribute, body, error = false) => {
|
|
78
|
+
this.tracer.push(new TracerLog(this.route, this.key, this.system, this.action, attribute, body, error, this.client, this.version));
|
|
156
79
|
};
|
|
157
|
-
this.send =
|
|
158
|
-
|
|
80
|
+
this.send = (log) => {
|
|
81
|
+
this.tracer.push(new TracerLog(log.route || this.route, log.key || this.key, log.system || this.system, log.action || this.action, log.attribute, log.body, log.error || false, log.client || this.client, log.version || this.version));
|
|
159
82
|
};
|
|
160
83
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (!this.options.awsConfig) return [3 /*break*/, 2];
|
|
175
|
-
return [4 /*yield*/, aws_1.loadAWSConfig(this.options.awsConfig)];
|
|
176
|
-
case 1:
|
|
177
|
-
_a = _b.sent();
|
|
178
|
-
return [3 /*break*/, 3];
|
|
179
|
-
case 2:
|
|
180
|
-
_a = undefined;
|
|
181
|
-
_b.label = 3;
|
|
182
|
-
case 3:
|
|
183
|
-
awsConfig = _a;
|
|
184
|
-
sqs = (function () {
|
|
185
|
-
if (!awsConfig) {
|
|
186
|
-
return new AWS.SQS({
|
|
187
|
-
region: _this.options.region,
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
ts_enum_util_1.$enum(aws_1.AWSComponent).forEach(function (eachComponent) {
|
|
191
|
-
var config = awsConfig.get(eachComponent);
|
|
192
|
-
if (config) {
|
|
193
|
-
config.region = _this.options.region;
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
return new aws_1.SimpleAWS(awsConfig).sqs;
|
|
197
|
-
})();
|
|
198
|
-
this.tracer = new Tracer(this.options.queueName, sqs);
|
|
199
|
-
tracer = function (key, action) {
|
|
200
|
-
_this.last = { key: key, action: action };
|
|
201
|
-
return new TracerWrapper(_this.tracer, _this.options.route, _this.options.system, key, action, _this.client.agent, _this.client.version);
|
|
202
|
-
};
|
|
203
|
-
return [2 /*return*/, { tracer: tracer }];
|
|
84
|
+
}
|
|
85
|
+
export class TracerPlugin extends HandlerPluginBase {
|
|
86
|
+
constructor(options) {
|
|
87
|
+
super();
|
|
88
|
+
this.create = async () => {
|
|
89
|
+
const awsConfig = this.options.awsConfig
|
|
90
|
+
? await loadAWSConfig(this.options.awsConfig)
|
|
91
|
+
: undefined;
|
|
92
|
+
const sqs = (() => {
|
|
93
|
+
if (!awsConfig) {
|
|
94
|
+
return new AWS.SQS({
|
|
95
|
+
region: this.options.region,
|
|
96
|
+
});
|
|
204
97
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
98
|
+
$enum(AWSComponent).forEach(eachComponent => {
|
|
99
|
+
const config = awsConfig.get(eachComponent);
|
|
100
|
+
if (config) {
|
|
101
|
+
config.region = this.options.region;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
return new SimpleAWS(awsConfig).sqs;
|
|
105
|
+
})();
|
|
106
|
+
this.tracer = new Tracer(this.options.queueName, sqs);
|
|
107
|
+
const tracer = (key, action) => {
|
|
108
|
+
this.last = { key, action };
|
|
109
|
+
return new TracerWrapper(this.tracer, this.options.route, this.options.system, key, action, this.client.agent, this.client.version);
|
|
110
|
+
};
|
|
111
|
+
return { tracer };
|
|
112
|
+
};
|
|
113
|
+
this.begin = ({ request }) => {
|
|
114
|
+
this.client.version = request.header('X-Version') || '0.0.0';
|
|
115
|
+
this.client.agent = (() => {
|
|
116
|
+
const fromHeader = request.header('User-Agent');
|
|
212
117
|
if (fromHeader) {
|
|
213
118
|
return fromHeader;
|
|
214
119
|
}
|
|
@@ -220,9 +125,8 @@ var TracerPlugin = /** @class */ (function (_super) {
|
|
|
220
125
|
return '';
|
|
221
126
|
})();
|
|
222
127
|
};
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
var request = _a.request, aux = _a.aux;
|
|
128
|
+
this.end = () => this.tracer.flush();
|
|
129
|
+
this.error = ({ request, aux }) => {
|
|
226
130
|
if (!aux) {
|
|
227
131
|
console.warn('Aux is not initialized');
|
|
228
132
|
return;
|
|
@@ -230,26 +134,23 @@ var TracerPlugin = /** @class */ (function (_super) {
|
|
|
230
134
|
if (!request.lastError) {
|
|
231
135
|
return;
|
|
232
136
|
}
|
|
233
|
-
|
|
137
|
+
const { key, action } = this.last;
|
|
234
138
|
aux
|
|
235
139
|
.tracer(key, action)
|
|
236
140
|
.push('error', typeof request.lastError === 'string'
|
|
237
141
|
? request.lastError
|
|
238
|
-
:
|
|
142
|
+
: stringifyError(request.lastError), true);
|
|
239
143
|
};
|
|
240
|
-
|
|
241
|
-
|
|
144
|
+
this.options = options;
|
|
145
|
+
this.last = {
|
|
242
146
|
key: 'nothing',
|
|
243
147
|
action: 'unknown',
|
|
244
148
|
};
|
|
245
|
-
|
|
149
|
+
this.client = {
|
|
246
150
|
agent: '',
|
|
247
151
|
version: '',
|
|
248
152
|
};
|
|
249
|
-
return _this;
|
|
250
153
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
var build = function (options) { return new TracerPlugin(options); };
|
|
255
|
-
exports.default = build;
|
|
154
|
+
}
|
|
155
|
+
const build = (options) => new TracerPlugin(options);
|
|
156
|
+
export default build;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './logger';
|
|
2
|
-
export * from './misc';
|
|
1
|
+
export * from './logger.js';
|
|
2
|
+
export * from './misc.js';
|
package/dist/utils/index.js
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
4
|
-
}
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
__export(require("./logger"));
|
|
7
|
-
__export(require("./misc"));
|
|
1
|
+
export * from './logger.js';
|
|
2
|
+
export * from './misc.js';
|
package/dist/utils/logger.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare enum LogLevel {
|
|
|
8
8
|
Stupid = "stupid"
|
|
9
9
|
}
|
|
10
10
|
export declare const currentLogLevel: LogLevel;
|
|
11
|
-
|
|
11
|
+
type LogMessage = string | Error;
|
|
12
12
|
export declare class Logger {
|
|
13
13
|
private name;
|
|
14
14
|
private severity;
|
|
@@ -22,5 +22,5 @@ export declare class Logger {
|
|
|
22
22
|
silly: (message: LogMessage) => LogMessage;
|
|
23
23
|
stupid: <T>(message: string, object: T, replacer?: ((key: string, value: T) => T) | undefined) => T;
|
|
24
24
|
}
|
|
25
|
-
export declare const getLogger: (fileName: string, level?: LogLevel
|
|
25
|
+
export declare const getLogger: (fileName: string, level?: LogLevel) => Logger;
|
|
26
26
|
export {};
|
package/dist/utils/logger.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
var misc_1 = require("./misc");
|
|
7
|
-
var LogLevel;
|
|
1
|
+
import { basename } from 'path';
|
|
2
|
+
import { envDefault as currentStage, StagingLevel } from 'simple-staging';
|
|
3
|
+
import { $enum } from 'ts-enum-util';
|
|
4
|
+
import { stringifyError } from './misc.js';
|
|
5
|
+
export var LogLevel;
|
|
8
6
|
(function (LogLevel) {
|
|
9
7
|
LogLevel["Error"] = "error";
|
|
10
8
|
LogLevel["Warn"] = "warn";
|
|
@@ -13,8 +11,8 @@ var LogLevel;
|
|
|
13
11
|
LogLevel["Verbose"] = "verbose";
|
|
14
12
|
LogLevel["Silly"] = "silly";
|
|
15
13
|
LogLevel["Stupid"] = "stupid";
|
|
16
|
-
})(LogLevel
|
|
17
|
-
|
|
14
|
+
})(LogLevel || (LogLevel = {}));
|
|
15
|
+
const severity = (level) => {
|
|
18
16
|
switch (level) {
|
|
19
17
|
case LogLevel.Error:
|
|
20
18
|
return 100;
|
|
@@ -34,38 +32,34 @@ var severity = function (level) {
|
|
|
34
32
|
return 1000;
|
|
35
33
|
}
|
|
36
34
|
};
|
|
37
|
-
|
|
35
|
+
export const currentLogLevel = $enum(LogLevel).asValueOrDefault(process.env.LOG_LEVEL, currentStage.level !== StagingLevel.Release
|
|
38
36
|
? LogLevel.Verbose
|
|
39
37
|
: LogLevel.Debug);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (_this.severity >= severity(level)) {
|
|
46
|
-
console.log("[" + new Date().toISOString() + "][" + level.toUpperCase() + "][" + _this.name + "] " + (message instanceof Error ? misc_1.stringifyError(message) : message));
|
|
38
|
+
export class Logger {
|
|
39
|
+
constructor(name, level = currentLogLevel) {
|
|
40
|
+
this.log = (level, message) => {
|
|
41
|
+
if (this.severity >= severity(level)) {
|
|
42
|
+
console.log(`[${new Date().toISOString()}][${level.toUpperCase()}][${this.name}] ${message instanceof Error ? stringifyError(message) : message}`);
|
|
47
43
|
}
|
|
48
44
|
return message;
|
|
49
45
|
};
|
|
50
|
-
this.error =
|
|
51
|
-
this.warn =
|
|
52
|
-
this.info =
|
|
53
|
-
this.debug =
|
|
54
|
-
this.verbose =
|
|
55
|
-
this.silly =
|
|
56
|
-
this.stupid =
|
|
57
|
-
|
|
46
|
+
this.error = (message) => this.log(LogLevel.Error, message);
|
|
47
|
+
this.warn = (message) => this.log(LogLevel.Warn, message);
|
|
48
|
+
this.info = (message) => this.log(LogLevel.Info, message);
|
|
49
|
+
this.debug = (message) => this.log(LogLevel.Debug, message);
|
|
50
|
+
this.verbose = (message) => this.log(LogLevel.Verbose, message);
|
|
51
|
+
this.silly = (message) => this.log(LogLevel.Silly, message);
|
|
52
|
+
this.stupid = (message, object, replacer) => {
|
|
53
|
+
this.log(LogLevel.Stupid, `${message}: ${JSON.stringify(object, replacer)}`);
|
|
58
54
|
return object;
|
|
59
55
|
};
|
|
60
56
|
this.name = name;
|
|
61
57
|
this.severity = severity(level);
|
|
62
58
|
}
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
exports.getLogger = function (fileName, level) {
|
|
68
|
-
var name = path_1.basename(fileName);
|
|
59
|
+
}
|
|
60
|
+
const loggers = {};
|
|
61
|
+
export const getLogger = (fileName, level) => {
|
|
62
|
+
const name = basename(fileName);
|
|
69
63
|
if (loggers[name] === undefined) {
|
|
70
64
|
loggers[name] = new Logger(name, level);
|
|
71
65
|
}
|
package/dist/utils/misc.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const stringifyError: (err: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number
|
|
1
|
+
export declare const stringifyError: (err: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number) => string;
|
package/dist/utils/misc.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var plainObject = {};
|
|
5
|
-
Object.getOwnPropertyNames(err).forEach(function (key) {
|
|
1
|
+
export const stringifyError = (err, replacer, space) => {
|
|
2
|
+
const plainObject = {};
|
|
3
|
+
Object.getOwnPropertyNames(err).forEach(key => {
|
|
6
4
|
plainObject[key] = err[key];
|
|
7
5
|
});
|
|
8
6
|
return JSON.stringify(plainObject, replacer, space);
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "serverless-simple-middleware",
|
|
3
3
|
"description": "Simple middleware to translate the interface of lambda's handler to request => response",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.59",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"author": "VoyagerX",
|
|
8
9
|
"license": "MIT",
|
|
9
10
|
"repository": {
|
|
@@ -19,6 +20,7 @@
|
|
|
19
20
|
"homepage": "https://github.com/v6x/serverless-simple-middleware#readme",
|
|
20
21
|
"scripts": {
|
|
21
22
|
"lint": "tslint --project tsconfig.json",
|
|
23
|
+
"clean": "rm -rf node_modules && yarn",
|
|
22
24
|
"build": "tsc --project tsconfig.json",
|
|
23
25
|
"build:watch": "tsc --project tsconfig.json --watch",
|
|
24
26
|
"test": "jest --config jest.config.js",
|
|
@@ -29,12 +31,13 @@
|
|
|
29
31
|
"@types/aws-lambda": "8",
|
|
30
32
|
"@types/mysql": "^2.15.5",
|
|
31
33
|
"cross-fetch": "^2.2.2",
|
|
34
|
+
"kysely": "^0.27.6",
|
|
32
35
|
"mysql": "^2.16.0",
|
|
33
36
|
"nanoid": "4.0.2",
|
|
34
37
|
"p-limit": "^2.0.0",
|
|
35
38
|
"simple-staging": "^0.0.12",
|
|
36
39
|
"ts-enum-util": "^3.1.0",
|
|
37
|
-
"typescript": "
|
|
40
|
+
"typescript": "5.3.3",
|
|
38
41
|
"uuid": "^3.3.2"
|
|
39
42
|
},
|
|
40
43
|
"optionalDependencies": {
|
|
@@ -58,4 +61,4 @@
|
|
|
58
61
|
"singleQuote": true,
|
|
59
62
|
"trailingComma": "all"
|
|
60
63
|
}
|
|
61
|
-
}
|
|
64
|
+
}
|
package/src/aws/config.ts
CHANGED
package/src/aws/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './define';
|
|
2
|
-
export * from './config';
|
|
3
|
-
export * from './simple';
|
|
1
|
+
export * from './define.js';
|
|
2
|
+
export * from './config.js';
|
|
3
|
+
export * from './simple.js';
|
package/src/aws/simple.ts
CHANGED
|
@@ -3,15 +3,16 @@ import * as fs from 'fs';
|
|
|
3
3
|
import * as os from 'os';
|
|
4
4
|
import { nanoid } from 'nanoid/non-secure';
|
|
5
5
|
|
|
6
|
-
import { getLogger
|
|
7
|
-
import { SimpleAWSConfig } from './config';
|
|
6
|
+
import { getLogger } from '../utils/index.js';
|
|
7
|
+
import { SimpleAWSConfig } from './config.js';
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
10
|
AWSComponent,
|
|
11
11
|
S3SignedUrlParams,
|
|
12
12
|
S3SignedUrlResult,
|
|
13
13
|
SQSMessageBody,
|
|
14
|
-
} from './define';
|
|
14
|
+
} from './define.js';
|
|
15
|
+
import { stringifyError } from '../utils/index.js';
|
|
15
16
|
|
|
16
17
|
const logger = getLogger(__filename);
|
|
17
18
|
|
|
@@ -313,6 +314,23 @@ export class SimpleAWS {
|
|
|
313
314
|
return key;
|
|
314
315
|
};
|
|
315
316
|
|
|
317
|
+
public uploadFromBuffer = async (
|
|
318
|
+
bucket: string,
|
|
319
|
+
key: string,
|
|
320
|
+
buffer: Buffer,
|
|
321
|
+
): Promise<string> => {
|
|
322
|
+
logger.debug(`Upload item[${key}] into bucket[${bucket}]`);
|
|
323
|
+
const putResult = await this.s3
|
|
324
|
+
.upload({
|
|
325
|
+
Bucket: bucket,
|
|
326
|
+
Key: key,
|
|
327
|
+
Body: buffer,
|
|
328
|
+
})
|
|
329
|
+
.promise();
|
|
330
|
+
logger.stupid(`putResult`, putResult);
|
|
331
|
+
return key;
|
|
332
|
+
};
|
|
333
|
+
|
|
316
334
|
public writeFile = async (
|
|
317
335
|
bucket: string,
|
|
318
336
|
key: string,
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './aws';
|
|
2
|
-
export * from './middleware';
|
|
3
|
-
export * from './utils';
|
|
1
|
+
export * from './aws/index.js';
|
|
2
|
+
export * from './middleware/index.js';
|
|
3
|
+
export * from './utils/index.js';
|
package/src/middleware/aws.ts
CHANGED
|
@@ -3,9 +3,9 @@ import {
|
|
|
3
3
|
SimpleAWS,
|
|
4
4
|
SimpleAWSConfig,
|
|
5
5
|
SimpleAWSConfigLoadParam,
|
|
6
|
-
} from '../aws';
|
|
7
|
-
import { getLogger } from '../utils';
|
|
8
|
-
import { HandlerAuxBase, HandlerPluginBase } from './base';
|
|
6
|
+
} from '../aws/index.js';
|
|
7
|
+
import { getLogger } from '../utils/index.js';
|
|
8
|
+
import { HandlerAuxBase, HandlerPluginBase } from './base.js';
|
|
9
9
|
|
|
10
10
|
const logger = getLogger(__filename);
|
|
11
11
|
|
package/src/middleware/base.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as awsTypes from 'aws-lambda'; // tslint:disable-line:no-implicit-dependencies
|
|
2
|
-
import { getLogger } from '../utils/
|
|
2
|
+
import { getLogger } from '../utils/index.js';
|
|
3
3
|
|
|
4
4
|
const logger = getLogger(__filename);
|
|
5
5
|
|
|
@@ -8,7 +8,7 @@ export interface RequestAuxBase {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export class HandlerRequest {
|
|
11
|
-
public event: awsTypes.
|
|
11
|
+
public event: awsTypes.APIGatewayProxyEvent;
|
|
12
12
|
public context: awsTypes.APIGatewayEventRequestContext;
|
|
13
13
|
public lastError: Error | string | undefined;
|
|
14
14
|
|
|
@@ -30,11 +30,11 @@ export class HandlerRequest {
|
|
|
30
30
|
return this.lazyBody || {};
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
get path(): { [key: string]: string } {
|
|
33
|
+
get path(): { [key: string]: string | undefined } {
|
|
34
34
|
return this.event.pathParameters || {};
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
get query(): { [key: string]: string } {
|
|
37
|
+
get query(): { [key: string]: string | undefined } {
|
|
38
38
|
return this.event.queryStringParameters || {};
|
|
39
39
|
}
|
|
40
40
|
|