serverless-simple-middleware 0.0.62 → 0.0.65
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/dist/aws/config.js +11 -12
- package/dist/aws/index.d.ts +1 -1
- package/dist/aws/index.js +1 -1
- package/dist/aws/simple.d.ts +1 -1
- package/dist/aws/simple.js +534 -786
- package/dist/internal/AwsError.js +10 -28
- package/dist/middleware/aws.js +42 -125
- package/dist/middleware/base.d.ts +4 -4
- package/dist/middleware/base.js +92 -102
- package/dist/middleware/build.js +101 -244
- package/dist/middleware/database/connectionProxy.d.ts +19 -0
- package/dist/middleware/database/connectionProxy.js +136 -0
- package/dist/middleware/database/index.d.ts +2 -0
- package/dist/middleware/database/index.js +7 -0
- package/dist/middleware/database/sqlClient.d.ts +8 -0
- package/dist/middleware/database/sqlClient.js +65 -0
- package/dist/middleware/index.d.ts +4 -3
- package/dist/middleware/index.js +8 -7
- package/dist/middleware/logger.js +13 -69
- package/dist/middleware/mysql.d.ts +11 -23
- package/dist/middleware/mysql.js +25 -288
- package/dist/middleware/trace.d.ts +1 -1
- package/dist/middleware/trace.js +144 -216
- package/dist/utils/logger.js +29 -30
- package/dist/utils/misc.js +6 -6
- package/package.json +5 -3
- package/src/aws/index.ts +1 -1
- package/src/aws/simple.ts +3 -3
- package/src/middleware/base.ts +24 -7
- package/src/middleware/database/connectionProxy.ts +174 -0
- package/src/middleware/database/index.ts +14 -0
- package/src/middleware/database/sqlClient.ts +95 -0
- package/src/middleware/index.ts +3 -2
- package/src/middleware/mysql.ts +16 -178
- package/src/middleware/trace.ts +1 -1
- package/tsconfig.json +2 -2
package/dist/middleware/trace.js
CHANGED
|
@@ -1,66 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
-
function step(op) {
|
|
31
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
-
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;
|
|
34
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
-
switch (op[0]) {
|
|
36
|
-
case 0: case 1: t = op; break;
|
|
37
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
-
default:
|
|
41
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
-
if (t[2]) _.ops.pop();
|
|
46
|
-
_.trys.pop(); continue;
|
|
47
|
-
}
|
|
48
|
-
op = body.call(thisArg, _);
|
|
49
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
3
|
exports.TracerPlugin = exports.TracerWrapper = exports.Tracer = exports.TracerLog = void 0;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const aws_1 = require("../aws");
|
|
6
|
+
const utils_1 = require("../utils");
|
|
7
|
+
const client_sqs_1 = require("@aws-sdk/client-sqs");
|
|
8
|
+
const ts_enum_util_1 = require("ts-enum-util");
|
|
9
|
+
const base_1 = require("./base");
|
|
10
|
+
const logger = (0, utils_1.getLogger)(__filename);
|
|
11
|
+
class TracerLog {
|
|
12
|
+
route;
|
|
13
|
+
key;
|
|
14
|
+
system;
|
|
15
|
+
action;
|
|
16
|
+
attribute;
|
|
17
|
+
body;
|
|
18
|
+
error;
|
|
19
|
+
client;
|
|
20
|
+
version;
|
|
21
|
+
uuid;
|
|
22
|
+
timestamp;
|
|
23
|
+
constructor(route, key, system, action, attribute, body, error, client, version) {
|
|
64
24
|
this.route = route;
|
|
65
25
|
this.key = key;
|
|
66
26
|
this.system = system;
|
|
@@ -73,76 +33,61 @@ var TracerLog = /** @class */ (function () {
|
|
|
73
33
|
this.uuid = (0, uuid_1.v4)();
|
|
74
34
|
this.timestamp = Date.now();
|
|
75
35
|
}
|
|
76
|
-
|
|
77
|
-
}());
|
|
36
|
+
}
|
|
78
37
|
exports.TracerLog = TracerLog;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
var urlResult, eventQueueUrl, chunkSize, begin, end, subset, sendBatchResult, error_1;
|
|
85
|
-
return __generator(this, function (_a) {
|
|
86
|
-
switch (_a.label) {
|
|
87
|
-
case 0:
|
|
88
|
-
if (this.buffer.length === 0) {
|
|
89
|
-
return [2 /*return*/];
|
|
90
|
-
}
|
|
91
|
-
_a.label = 1;
|
|
92
|
-
case 1:
|
|
93
|
-
_a.trys.push([1, 7, , 8]);
|
|
94
|
-
return [4 /*yield*/, this.sqs.getQueueUrl({
|
|
95
|
-
QueueName: this.queueName,
|
|
96
|
-
})];
|
|
97
|
-
case 2:
|
|
98
|
-
urlResult = _a.sent();
|
|
99
|
-
logger.stupid("urlResult", urlResult);
|
|
100
|
-
if (!urlResult.QueueUrl) {
|
|
101
|
-
throw new Error("No queue url with name[".concat(this.queueName, "]"));
|
|
102
|
-
}
|
|
103
|
-
eventQueueUrl = urlResult.QueueUrl;
|
|
104
|
-
chunkSize = 10;
|
|
105
|
-
begin = 0;
|
|
106
|
-
_a.label = 3;
|
|
107
|
-
case 3:
|
|
108
|
-
if (!(begin < this.buffer.length)) return [3 /*break*/, 6];
|
|
109
|
-
end = Math.min(this.buffer.length, begin + chunkSize);
|
|
110
|
-
subset = this.buffer.slice(begin, end);
|
|
111
|
-
return [4 /*yield*/, this.sqs.sendMessageBatch({
|
|
112
|
-
QueueUrl: eventQueueUrl,
|
|
113
|
-
Entries: subset.map(function (each) { return ({
|
|
114
|
-
Id: "".concat(each.key, "_").concat(each.uuid),
|
|
115
|
-
MessageBody: JSON.stringify(each),
|
|
116
|
-
}); }),
|
|
117
|
-
})];
|
|
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: ".concat(error_1));
|
|
131
|
-
return [3 /*break*/, 8];
|
|
132
|
-
case 8: return [2 /*return*/];
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}); };
|
|
38
|
+
class Tracer {
|
|
39
|
+
queueName;
|
|
40
|
+
sqs;
|
|
41
|
+
buffer;
|
|
42
|
+
constructor(queueName, sqs) {
|
|
136
43
|
this.queueName = queueName;
|
|
137
44
|
this.sqs = sqs;
|
|
138
45
|
this.buffer = [];
|
|
139
46
|
}
|
|
140
|
-
|
|
141
|
-
|
|
47
|
+
push = (log) => this.buffer.push(log);
|
|
48
|
+
flush = async () => {
|
|
49
|
+
if (this.buffer.length === 0) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const urlResult = await this.sqs.getQueueUrl({
|
|
54
|
+
QueueName: this.queueName,
|
|
55
|
+
});
|
|
56
|
+
logger.stupid(`urlResult`, urlResult);
|
|
57
|
+
if (!urlResult.QueueUrl) {
|
|
58
|
+
throw new Error(`No queue url with name[${this.queueName}]`);
|
|
59
|
+
}
|
|
60
|
+
const eventQueueUrl = urlResult.QueueUrl;
|
|
61
|
+
const chunkSize = 10;
|
|
62
|
+
for (let begin = 0; begin < this.buffer.length; begin += chunkSize) {
|
|
63
|
+
const end = Math.min(this.buffer.length, begin + chunkSize);
|
|
64
|
+
const subset = this.buffer.slice(begin, end);
|
|
65
|
+
const sendBatchResult = await this.sqs.sendMessageBatch({
|
|
66
|
+
QueueUrl: eventQueueUrl,
|
|
67
|
+
Entries: subset.map((each) => ({
|
|
68
|
+
Id: `${each.key}_${each.uuid}`,
|
|
69
|
+
MessageBody: JSON.stringify(each),
|
|
70
|
+
})),
|
|
71
|
+
});
|
|
72
|
+
logger.stupid(`sendBatchResult`, sendBatchResult);
|
|
73
|
+
}
|
|
74
|
+
this.buffer = [];
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
logger.warn(`Error in eventSource: ${error}`);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
142
81
|
exports.Tracer = Tracer;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
82
|
+
class TracerWrapper {
|
|
83
|
+
tracer;
|
|
84
|
+
route;
|
|
85
|
+
system;
|
|
86
|
+
key;
|
|
87
|
+
action;
|
|
88
|
+
client;
|
|
89
|
+
version;
|
|
90
|
+
constructor(tracer, route, system, key, action, client, version) {
|
|
146
91
|
this.tracer = tracer;
|
|
147
92
|
this.route = route;
|
|
148
93
|
this.system = system;
|
|
@@ -150,106 +95,89 @@ var TracerWrapper = /** @class */ (function () {
|
|
|
150
95
|
this.action = action;
|
|
151
96
|
this.client = client;
|
|
152
97
|
this.version = version;
|
|
153
|
-
this.push = function (attribute, body, error) {
|
|
154
|
-
if (error === void 0) { error = false; }
|
|
155
|
-
_this.tracer.push(new TracerLog(_this.route, _this.key, _this.system, _this.action, attribute, body, error, _this.client, _this.version));
|
|
156
|
-
};
|
|
157
|
-
this.send = function (log) {
|
|
158
|
-
_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
|
-
};
|
|
160
98
|
}
|
|
161
|
-
|
|
162
|
-
|
|
99
|
+
push = (attribute, body, error = false) => {
|
|
100
|
+
this.tracer.push(new TracerLog(this.route, this.key, this.system, this.action, attribute, body, error, this.client, this.version));
|
|
101
|
+
};
|
|
102
|
+
send = (log) => {
|
|
103
|
+
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));
|
|
104
|
+
};
|
|
105
|
+
}
|
|
163
106
|
exports.TracerWrapper = TracerWrapper;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
case 0:
|
|
174
|
-
if (!this.options.awsConfig) return [3 /*break*/, 2];
|
|
175
|
-
return [4 /*yield*/, (0, 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 client_sqs_1.SQS({
|
|
187
|
-
region: _this.options.region,
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
(0, 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 }];
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}); };
|
|
207
|
-
_this.begin = function (_a) {
|
|
208
|
-
var request = _a.request;
|
|
209
|
-
_this.client.version = request.header('X-Version') || '0.0.0';
|
|
210
|
-
_this.client.agent = (function () {
|
|
211
|
-
var fromHeader = request.header('User-Agent');
|
|
212
|
-
if (fromHeader) {
|
|
213
|
-
return fromHeader;
|
|
214
|
-
}
|
|
215
|
-
if (request.context &&
|
|
216
|
-
request.context.identity &&
|
|
217
|
-
request.context.identity.userAgent) {
|
|
218
|
-
return request.context.identity.userAgent;
|
|
219
|
-
}
|
|
220
|
-
return '';
|
|
221
|
-
})();
|
|
222
|
-
};
|
|
223
|
-
_this.end = function () { return _this.tracer.flush(); };
|
|
224
|
-
_this.error = function (_a) {
|
|
225
|
-
var request = _a.request, aux = _a.aux;
|
|
226
|
-
if (!aux) {
|
|
227
|
-
console.warn('Aux is not initialized');
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
if (!request.lastError) {
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
var _b = _this.last, key = _b.key, action = _b.action;
|
|
234
|
-
aux
|
|
235
|
-
.tracer(key, action)
|
|
236
|
-
.push('error', typeof request.lastError === 'string'
|
|
237
|
-
? request.lastError
|
|
238
|
-
: (0, utils_1.stringifyError)(request.lastError), true);
|
|
239
|
-
};
|
|
240
|
-
_this.options = options;
|
|
241
|
-
_this.last = {
|
|
107
|
+
class TracerPlugin extends base_1.HandlerPluginBase {
|
|
108
|
+
tracer;
|
|
109
|
+
options;
|
|
110
|
+
last;
|
|
111
|
+
client;
|
|
112
|
+
constructor(options) {
|
|
113
|
+
super();
|
|
114
|
+
this.options = options;
|
|
115
|
+
this.last = {
|
|
242
116
|
key: 'nothing',
|
|
243
117
|
action: 'unknown',
|
|
244
118
|
};
|
|
245
|
-
|
|
119
|
+
this.client = {
|
|
246
120
|
agent: '',
|
|
247
121
|
version: '',
|
|
248
122
|
};
|
|
249
|
-
return _this;
|
|
250
123
|
}
|
|
251
|
-
|
|
252
|
-
|
|
124
|
+
create = async () => {
|
|
125
|
+
const awsConfig = this.options.awsConfig
|
|
126
|
+
? await (0, aws_1.loadAWSConfig)(this.options.awsConfig)
|
|
127
|
+
: undefined;
|
|
128
|
+
const sqs = (() => {
|
|
129
|
+
if (!awsConfig) {
|
|
130
|
+
return new client_sqs_1.SQS({
|
|
131
|
+
region: this.options.region,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
(0, ts_enum_util_1.$enum)(aws_1.AWSComponent).forEach((eachComponent) => {
|
|
135
|
+
const config = awsConfig.get(eachComponent);
|
|
136
|
+
if (config) {
|
|
137
|
+
config.region = this.options.region;
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
return new aws_1.SimpleAWS(awsConfig).sqs;
|
|
141
|
+
})();
|
|
142
|
+
this.tracer = new Tracer(this.options.queueName, sqs);
|
|
143
|
+
const tracer = (key, action) => {
|
|
144
|
+
this.last = { key, action };
|
|
145
|
+
return new TracerWrapper(this.tracer, this.options.route, this.options.system, key, action, this.client.agent, this.client.version);
|
|
146
|
+
};
|
|
147
|
+
return { tracer };
|
|
148
|
+
};
|
|
149
|
+
begin = ({ request }) => {
|
|
150
|
+
this.client.version = request.header('X-Version') || '0.0.0';
|
|
151
|
+
this.client.agent = (() => {
|
|
152
|
+
const fromHeader = request.header('User-Agent');
|
|
153
|
+
if (fromHeader) {
|
|
154
|
+
return fromHeader;
|
|
155
|
+
}
|
|
156
|
+
if (request.context &&
|
|
157
|
+
request.context.identity &&
|
|
158
|
+
request.context.identity.userAgent) {
|
|
159
|
+
return request.context.identity.userAgent;
|
|
160
|
+
}
|
|
161
|
+
return '';
|
|
162
|
+
})();
|
|
163
|
+
};
|
|
164
|
+
end = () => this.tracer.flush();
|
|
165
|
+
error = ({ request, aux }) => {
|
|
166
|
+
if (!aux) {
|
|
167
|
+
console.warn('Aux is not initialized');
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (!request.lastError) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const { key, action } = this.last;
|
|
174
|
+
aux
|
|
175
|
+
.tracer(key, action)
|
|
176
|
+
.push('error', typeof request.lastError === 'string'
|
|
177
|
+
? request.lastError
|
|
178
|
+
: (0, utils_1.stringifyError)(request.lastError), true);
|
|
179
|
+
};
|
|
180
|
+
}
|
|
253
181
|
exports.TracerPlugin = TracerPlugin;
|
|
254
|
-
|
|
182
|
+
const build = (options) => new TracerPlugin(options);
|
|
255
183
|
exports.default = build;
|
package/dist/utils/logger.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getLogger = exports.Logger = exports.currentLogLevel = exports.LogLevel = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const simple_staging_1 = require("simple-staging");
|
|
6
|
+
const ts_enum_util_1 = require("ts-enum-util");
|
|
7
|
+
const misc_1 = require("./misc");
|
|
8
8
|
var LogLevel;
|
|
9
9
|
(function (LogLevel) {
|
|
10
10
|
LogLevel["Error"] = "error";
|
|
@@ -15,7 +15,7 @@ var LogLevel;
|
|
|
15
15
|
LogLevel["Silly"] = "silly";
|
|
16
16
|
LogLevel["Stupid"] = "stupid";
|
|
17
17
|
})(LogLevel || (exports.LogLevel = LogLevel = {}));
|
|
18
|
-
|
|
18
|
+
const severity = (level) => {
|
|
19
19
|
switch (level) {
|
|
20
20
|
case LogLevel.Error:
|
|
21
21
|
return 100;
|
|
@@ -38,35 +38,34 @@ var severity = function (level) {
|
|
|
38
38
|
exports.currentLogLevel = (0, ts_enum_util_1.$enum)(LogLevel).asValueOrDefault(process.env.LOG_LEVEL, simple_staging_1.envDefault.level !== simple_staging_1.StagingLevel.Release
|
|
39
39
|
? LogLevel.Verbose
|
|
40
40
|
: LogLevel.Debug);
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this.log = function (level, message) {
|
|
46
|
-
if (_this.severity >= severity(level)) {
|
|
47
|
-
console.log("[".concat(new Date().toISOString(), "][").concat(level.toUpperCase(), "][").concat(_this.name, "] ").concat(message instanceof Error ? (0, misc_1.stringifyError)(message) : message));
|
|
48
|
-
}
|
|
49
|
-
return message;
|
|
50
|
-
};
|
|
51
|
-
this.error = function (message) { return _this.log(LogLevel.Error, message); };
|
|
52
|
-
this.warn = function (message) { return _this.log(LogLevel.Warn, message); };
|
|
53
|
-
this.info = function (message) { return _this.log(LogLevel.Info, message); };
|
|
54
|
-
this.debug = function (message) { return _this.log(LogLevel.Debug, message); };
|
|
55
|
-
this.verbose = function (message) { return _this.log(LogLevel.Verbose, message); };
|
|
56
|
-
this.silly = function (message) { return _this.log(LogLevel.Silly, message); };
|
|
57
|
-
this.stupid = function (message, object, replacer) {
|
|
58
|
-
_this.log(LogLevel.Stupid, "".concat(message, ": ").concat(JSON.stringify(object, replacer)));
|
|
59
|
-
return object;
|
|
60
|
-
};
|
|
41
|
+
class Logger {
|
|
42
|
+
name;
|
|
43
|
+
severity;
|
|
44
|
+
constructor(name, level = exports.currentLogLevel) {
|
|
61
45
|
this.name = name;
|
|
62
46
|
this.severity = severity(level);
|
|
63
47
|
}
|
|
64
|
-
|
|
65
|
-
|
|
48
|
+
log = (level, message) => {
|
|
49
|
+
if (this.severity >= severity(level)) {
|
|
50
|
+
console.log(`[${new Date().toISOString()}][${level.toUpperCase()}][${this.name}] ${message instanceof Error ? (0, misc_1.stringifyError)(message) : message}`);
|
|
51
|
+
}
|
|
52
|
+
return message;
|
|
53
|
+
};
|
|
54
|
+
error = (message) => this.log(LogLevel.Error, message);
|
|
55
|
+
warn = (message) => this.log(LogLevel.Warn, message);
|
|
56
|
+
info = (message) => this.log(LogLevel.Info, message);
|
|
57
|
+
debug = (message) => this.log(LogLevel.Debug, message);
|
|
58
|
+
verbose = (message) => this.log(LogLevel.Verbose, message);
|
|
59
|
+
silly = (message) => this.log(LogLevel.Silly, message);
|
|
60
|
+
stupid = (message, object, replacer) => {
|
|
61
|
+
this.log(LogLevel.Stupid, `${message}: ${JSON.stringify(object, replacer)}`);
|
|
62
|
+
return object;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
66
65
|
exports.Logger = Logger;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
const loggers = {};
|
|
67
|
+
const getLogger = (fileName, level) => {
|
|
68
|
+
const name = (0, path_1.basename)(fileName);
|
|
70
69
|
if (loggers[name] === undefined) {
|
|
71
70
|
loggers[name] = new Logger(name, level);
|
|
72
71
|
}
|
package/dist/utils/misc.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.stringifyError = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.getOwnPropertyNames(error).forEach(
|
|
4
|
+
const AwsError_1 = require("../internal/AwsError");
|
|
5
|
+
const stringifyError = (err, replacer, space) => {
|
|
6
|
+
const error = isAWSv3Error(err) ? new AwsError_1.AwsError(err) : err;
|
|
7
|
+
const plainObject = {};
|
|
8
|
+
Object.getOwnPropertyNames(error).forEach((key) => {
|
|
9
9
|
plainObject[key] = error[key];
|
|
10
10
|
});
|
|
11
11
|
return JSON.stringify(plainObject, replacer, space);
|
|
12
12
|
};
|
|
13
13
|
exports.stringifyError = stringifyError;
|
|
14
|
-
|
|
14
|
+
const isAWSv3Error = (error) => {
|
|
15
15
|
return error instanceof Error && 'name' in error && '$metadata' in error;
|
|
16
16
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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.65",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"author": "VoyagerX",
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
"homepage": "https://github.com/v6x/serverless-simple-middleware#readme",
|
|
23
23
|
"scripts": {
|
|
24
24
|
"lint": "tslint --project tsconfig.json",
|
|
25
|
+
"clean": "rm -rf dist && rm -rf node_modules && yarn install",
|
|
25
26
|
"build": "tsc --project tsconfig.json",
|
|
26
27
|
"build:watch": "tsc --project tsconfig.json --watch",
|
|
27
28
|
"test": "jest --config jest.config.js",
|
|
28
29
|
"test:watch": "jest --config jest.config.js --watch",
|
|
29
|
-
"deploy": "yarn build && yarn publish"
|
|
30
|
-
"clean": "rm -rf dist && rm -rf node_modules && yarn install"
|
|
30
|
+
"deploy": "yarn build && yarn publish"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@aws-sdk/client-dynamodb": "^3.828.0",
|
|
@@ -40,7 +40,9 @@
|
|
|
40
40
|
"@types/aws-lambda": "8",
|
|
41
41
|
"@types/mysql": "^2.15.5",
|
|
42
42
|
"cross-fetch": "^2.2.2",
|
|
43
|
+
"kysely": "^0.28.2",
|
|
43
44
|
"mysql": "^2.16.0",
|
|
45
|
+
"mysql2": "^3.14.1",
|
|
44
46
|
"nanoid": "4.0.2",
|
|
45
47
|
"p-limit": "^2.0.0",
|
|
46
48
|
"simple-staging": "^0.0.12",
|
package/src/aws/index.ts
CHANGED
package/src/aws/simple.ts
CHANGED
|
@@ -6,13 +6,12 @@ import {
|
|
|
6
6
|
import { envDefault as currentStage } from 'simple-staging';
|
|
7
7
|
|
|
8
8
|
import * as fs from 'fs';
|
|
9
|
-
import * as os from 'os';
|
|
10
9
|
import { nanoid } from 'nanoid/non-secure';
|
|
10
|
+
import * as os from 'os';
|
|
11
11
|
|
|
12
12
|
import { getLogger, stringifyError } from '../utils';
|
|
13
13
|
import { SimpleAWSConfig } from './config';
|
|
14
14
|
|
|
15
|
-
import { AWSComponent, SQSMessageBody } from './define';
|
|
16
15
|
import { DynamoDB, DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
|
17
16
|
import {
|
|
18
17
|
AbortMultipartUploadCommand,
|
|
@@ -32,9 +31,10 @@ import {
|
|
|
32
31
|
} from '@aws-sdk/client-s3';
|
|
33
32
|
import { SQS } from '@aws-sdk/client-sqs';
|
|
34
33
|
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
|
|
34
|
+
import { Upload } from '@aws-sdk/lib-storage';
|
|
35
35
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
36
36
|
import { PresignerOptions } from '../internal/s3';
|
|
37
|
-
import {
|
|
37
|
+
import { AWSComponent, SQSMessageBody } from './define';
|
|
38
38
|
|
|
39
39
|
const logger = getLogger(__filename);
|
|
40
40
|
|
package/src/middleware/base.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type {
|
|
2
|
+
APIGatewayEvent,
|
|
3
|
+
APIGatewayEventRequestContext,
|
|
4
|
+
} from 'aws-lambda';
|
|
2
5
|
import { getLogger } from '../utils/logger';
|
|
3
6
|
|
|
4
7
|
const logger = getLogger(__filename);
|
|
@@ -8,8 +11,8 @@ export interface RequestAuxBase {
|
|
|
8
11
|
}
|
|
9
12
|
|
|
10
13
|
export class HandlerRequest {
|
|
11
|
-
public event:
|
|
12
|
-
public context:
|
|
14
|
+
public event: APIGatewayEvent;
|
|
15
|
+
public context: APIGatewayEventRequestContext;
|
|
13
16
|
public lastError: Error | string | undefined;
|
|
14
17
|
|
|
15
18
|
private lazyBody?: any;
|
|
@@ -110,15 +113,29 @@ export class HandlerResponse {
|
|
|
110
113
|
key: string,
|
|
111
114
|
value: string,
|
|
112
115
|
domain?: string,
|
|
113
|
-
|
|
116
|
+
sameSite?: 'None' | 'Lax' | 'Strict',
|
|
117
|
+
secure?: boolean,
|
|
114
118
|
path?: string,
|
|
119
|
+
httpOnly?: boolean,
|
|
120
|
+
maxAgeSeconds?: number,
|
|
115
121
|
) {
|
|
116
122
|
const keyValueStr = `${key}=${value}`;
|
|
117
123
|
const domainStr = domain ? `Domain=${domain}` : '';
|
|
118
|
-
const sameSiteStr =
|
|
119
|
-
const secureStr =
|
|
124
|
+
const sameSiteStr = sameSite ? `SameSite=${sameSite}` : '';
|
|
125
|
+
const secureStr = secure ? 'Secure' : '';
|
|
120
126
|
const pathStr = path !== undefined ? `Path=${path}` : '';
|
|
121
|
-
const
|
|
127
|
+
const httpOnlyStr = httpOnly ? 'HttpOnly' : '';
|
|
128
|
+
const maxAgeStr =
|
|
129
|
+
maxAgeSeconds || maxAgeSeconds === 0 ? `Max-Age=${maxAgeSeconds}` : '';
|
|
130
|
+
const cookieStr = [
|
|
131
|
+
keyValueStr,
|
|
132
|
+
domainStr,
|
|
133
|
+
sameSiteStr,
|
|
134
|
+
secureStr,
|
|
135
|
+
pathStr,
|
|
136
|
+
httpOnlyStr,
|
|
137
|
+
maxAgeStr,
|
|
138
|
+
]
|
|
122
139
|
.filter((x) => x)
|
|
123
140
|
.join('; ');
|
|
124
141
|
this.cookies.push(cookieStr);
|