serverless-simple-middleware 0.0.59 → 0.0.60

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.
Files changed (45) hide show
  1. package/dist/aws/config.d.ts +3 -3
  2. package/dist/aws/config.js +18 -13
  3. package/dist/aws/define.js +4 -2
  4. package/dist/aws/index.d.ts +3 -3
  5. package/dist/aws/index.js +8 -3
  6. package/dist/aws/simple.d.ts +11 -11
  7. package/dist/aws/simple.js +666 -403
  8. package/dist/index.d.ts +3 -3
  9. package/dist/index.js +8 -3
  10. package/dist/middleware/aws.d.ts +4 -4
  11. package/dist/middleware/aws.js +125 -38
  12. package/dist/middleware/base.d.ts +11 -10
  13. package/dist/middleware/base.js +104 -66
  14. package/dist/middleware/build.d.ts +2 -2
  15. package/dist/middleware/build.js +218 -91
  16. package/dist/middleware/index.d.ts +10 -10
  17. package/dist/middleware/index.js +21 -16
  18. package/dist/middleware/logger.d.ts +2 -2
  19. package/dist/middleware/logger.js +70 -13
  20. package/dist/middleware/mysql.d.ts +7 -12
  21. package/dist/middleware/mysql.js +268 -173
  22. package/dist/middleware/trace.d.ts +2 -2
  23. package/dist/middleware/trace.js +198 -99
  24. package/dist/utils/index.d.ts +2 -2
  25. package/dist/utils/index.js +7 -2
  26. package/dist/utils/logger.d.ts +2 -2
  27. package/dist/utils/logger.js +31 -25
  28. package/dist/utils/misc.d.ts +1 -1
  29. package/dist/utils/misc.js +5 -3
  30. package/package.json +3 -6
  31. package/src/aws/config.ts +1 -1
  32. package/src/aws/index.ts +3 -3
  33. package/src/aws/simple.ts +3 -4
  34. package/src/index.ts +3 -3
  35. package/src/middleware/aws.ts +3 -3
  36. package/src/middleware/base.ts +19 -12
  37. package/src/middleware/build.ts +3 -3
  38. package/src/middleware/index.ts +10 -10
  39. package/src/middleware/logger.ts +2 -2
  40. package/src/middleware/mysql.ts +7 -74
  41. package/src/middleware/trace.ts +4 -4
  42. package/src/utils/index.ts +2 -2
  43. package/src/utils/logger.ts +1 -1
  44. package/tsconfig.json +4 -5
  45. package/.nvmrc +0 -1
@@ -1,194 +1,289 @@
1
- import { Kysely, MysqlDialect } from 'kysely';
2
- import * as mysql from 'mysql';
3
- import { getLogger } from '../utils/index.js';
4
- import { HandlerPluginBase } from './base.js';
5
- const logger = getLogger(__filename);
6
- export class ConnectionProxy {
7
- constructor(config) {
8
- this.query = (sql, params) => new Promise(async (resolve, reject) => {
9
- const connection = this.prepareConnection();
10
- await this.tryToInitializeSchema(false);
11
- if (process.env.NODE_ENV !== 'test') {
12
- logger.silly(`Execute query[${sql}] with params[${params}]`);
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 (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
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;
13
44
  }
14
- connection.query(sql, params, (err, result) => {
15
- if (err) {
16
- logger.error(`error occurred in database query=${sql}, error=${err}`);
17
- reject(err);
18
- }
19
- else {
20
- resolve(result);
21
- if (process.env.NODE_ENV !== 'test') {
22
- logger.silly(`DB result is ${JSON.stringify(result)}`);
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 mysql = require("mysql");
52
+ var utils_1 = require("../utils");
53
+ var base_1 = require("./base");
54
+ var logger = utils_1.getLogger(__filename);
55
+ var ConnectionProxy = /** @class */ (function () {
56
+ function ConnectionProxy(config) {
57
+ var _this = this;
58
+ this.query = function (sql, params) {
59
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
60
+ var connection;
61
+ return __generator(this, function (_a) {
62
+ switch (_a.label) {
63
+ case 0:
64
+ connection = this.prepareConnection();
65
+ return [4 /*yield*/, this.tryToInitializeSchema(false)];
66
+ case 1:
67
+ _a.sent();
68
+ if (process.env.NODE_ENV !== 'test') {
69
+ logger.silly("Execute query[" + sql + "] with params[" + params + "]");
70
+ }
71
+ connection.query(sql, params, function (err, result) {
72
+ if (err) {
73
+ logger.error("error occurred in database query=" + sql + ", error=" + err);
74
+ reject(err);
75
+ }
76
+ else {
77
+ resolve(result);
78
+ if (process.env.NODE_ENV !== 'test') {
79
+ logger.silly("DB result is " + JSON.stringify(result));
80
+ }
81
+ }
82
+ });
83
+ return [2 /*return*/];
23
84
  }
85
+ });
86
+ }); });
87
+ };
88
+ this.fetch = function (sql, params) {
89
+ return _this.query(sql, params).then(function (res) { return res || []; });
90
+ };
91
+ this.fetchOne = function (sql, params, defaultValue) {
92
+ return _this.fetch(sql, params).then(function (res) {
93
+ if (res === undefined || res[0] === undefined) {
94
+ // Makes it as non-null result.
95
+ return defaultValue || {};
24
96
  }
97
+ return res[0];
25
98
  });
26
- });
27
- this.fetch = (sql, params) => this.query(sql, params).then(res => res || []);
28
- this.fetchOne = (sql, params, defaultValue) => this.fetch(sql, params).then(res => {
29
- if (res === undefined || res[0] === undefined) {
30
- // Makes it as non-null result.
31
- return defaultValue || {};
32
- }
33
- return res[0];
34
- });
35
- this.beginTransaction = () => new Promise(async (resolve, reject) => {
36
- const connection = this.prepareConnection();
37
- await this.tryToInitializeSchema(false);
38
- connection.beginTransaction((err) => {
39
- if (err) {
40
- reject(err);
41
- return;
42
- }
43
- resolve();
44
- });
45
- });
46
- this.commit = () => new Promise(async (resolve, reject) => {
47
- const connection = this.prepareConnection();
48
- await this.tryToInitializeSchema(false);
49
- connection.commit((err) => {
50
- if (err) {
51
- reject(err);
52
- return;
53
- }
54
- resolve();
55
- });
56
- });
57
- this.rollback = () => new Promise(async (resolve, reject) => {
58
- const connection = this.prepareConnection();
59
- await this.tryToInitializeSchema(false);
60
- connection.rollback((err) => {
61
- if (err) {
62
- reject(err);
63
- return;
64
- }
65
- resolve();
66
- });
67
- });
68
- this.clearConnection = () => {
69
- if (this.connection) {
70
- this.connection.end();
71
- this.connection = undefined;
99
+ };
100
+ this.beginTransaction = function () {
101
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
102
+ var connection;
103
+ return __generator(this, function (_a) {
104
+ switch (_a.label) {
105
+ case 0:
106
+ connection = this.prepareConnection();
107
+ return [4 /*yield*/, this.tryToInitializeSchema(false)];
108
+ case 1:
109
+ _a.sent();
110
+ connection.beginTransaction(function (err) {
111
+ if (err) {
112
+ reject(err);
113
+ return;
114
+ }
115
+ resolve();
116
+ });
117
+ return [2 /*return*/];
118
+ }
119
+ });
120
+ }); });
121
+ };
122
+ this.commit = function () {
123
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
124
+ var connection;
125
+ return __generator(this, function (_a) {
126
+ switch (_a.label) {
127
+ case 0:
128
+ connection = this.prepareConnection();
129
+ return [4 /*yield*/, this.tryToInitializeSchema(false)];
130
+ case 1:
131
+ _a.sent();
132
+ connection.commit(function (err) {
133
+ if (err) {
134
+ reject(err);
135
+ return;
136
+ }
137
+ resolve();
138
+ });
139
+ return [2 /*return*/];
140
+ }
141
+ });
142
+ }); });
143
+ };
144
+ this.rollback = function () {
145
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
146
+ var connection;
147
+ return __generator(this, function (_a) {
148
+ switch (_a.label) {
149
+ case 0:
150
+ connection = this.prepareConnection();
151
+ return [4 /*yield*/, this.tryToInitializeSchema(false)];
152
+ case 1:
153
+ _a.sent();
154
+ connection.rollback(function (err) {
155
+ if (err) {
156
+ reject(err);
157
+ return;
158
+ }
159
+ resolve();
160
+ });
161
+ return [2 /*return*/];
162
+ }
163
+ });
164
+ }); });
165
+ };
166
+ this.clearConnection = function () {
167
+ if (_this.connection) {
168
+ _this.connection.end();
169
+ _this.connection = undefined;
72
170
  logger.verbose('Connection is end');
73
171
  }
74
172
  };
75
- this.onPluginCreated = async () => this.tryToInitializeSchema(true);
76
- this.prepareConnection = () => {
77
- if (this.connection) {
78
- return this.connection;
173
+ this.onPluginCreated = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
174
+ return [2 /*return*/, this.tryToInitializeSchema(true)];
175
+ }); }); };
176
+ this.prepareConnection = function () {
177
+ if (_this.connection) {
178
+ return _this.connection;
79
179
  }
80
- this.connection = mysql.createConnection(this.pluginConfig.config);
81
- this.connection.connect();
82
- return this.connection;
180
+ _this.connection = mysql.createConnection(_this.pluginConfig.config);
181
+ _this.connection.connect();
182
+ return _this.connection;
83
183
  };
84
- this.changeDatabase = (dbName) => new Promise((resolve, reject) => this.prepareConnection().changeUser({
85
- database: dbName,
86
- }, err => (err ? reject(err) : resolve())));
87
- this.tryToInitializeSchema = async (initial) => {
88
- const { eager = false, ignoreError = false, database = '', tables = {} } = this.pluginConfig.schema || {};
89
- if (initial && !eager) {
90
- return;
91
- }
92
- // This method can be called twice when eager option is on,
93
- // so this flag should be set and checked at first.
94
- if (this.initialized) {
95
- return;
96
- }
97
- this.initialized = true;
98
- try {
99
- if (database) {
100
- logger.debug(`Prepare a database[${this.dbName}]`);
101
- logger.stupid(this.dbName, database);
102
- const result = await this.query(database);
103
- logger.debug(`Database[${this.dbName}] is initialized: ${JSON.stringify(result)}`);
104
- }
105
- if (this.dbName) {
106
- await this.changeDatabase(this.dbName);
107
- logger.verbose(`Database[${this.dbName}] is connected.`);
108
- }
109
- for (const [name, query] of Object.entries(tables)) {
110
- logger.debug(`Prepare a table[${name}]`);
111
- logger.stupid(name, query);
112
- const result = await this.query(query);
113
- logger.debug(`Table[${name}] is initialized: ${JSON.stringify(result)}`);
114
- }
115
- logger.verbose(`Database schema is initialized.`);
116
- }
117
- catch (error) {
118
- logger.warn(error);
119
- if (!ignoreError) {
120
- throw error;
121
- }
122
- }
184
+ this.changeDatabase = function (dbName) {
185
+ return new Promise(function (resolve, reject) {
186
+ return _this.prepareConnection().changeUser({
187
+ database: dbName,
188
+ }, function (err) { return (err ? reject(err) : resolve()); });
189
+ });
123
190
  };
191
+ this.tryToInitializeSchema = function (initial) { return __awaiter(_this, void 0, void 0, function () {
192
+ var _a, _b, eager, _c, ignoreError, _d, database, _e, tables, result, _i, _f, _g, name_1, query, result, error_1;
193
+ return __generator(this, function (_h) {
194
+ switch (_h.label) {
195
+ case 0:
196
+ _a = this.pluginConfig.schema || {}, _b = _a.eager, eager = _b === void 0 ? false : _b, _c = _a.ignoreError, ignoreError = _c === void 0 ? false : _c, _d = _a.database, database = _d === void 0 ? '' : _d, _e = _a.tables, tables = _e === void 0 ? {} : _e;
197
+ if (initial && !eager) {
198
+ return [2 /*return*/];
199
+ }
200
+ // This method can be called twice when eager option is on,
201
+ // so this flag should be set and checked at first.
202
+ if (this.initialized) {
203
+ return [2 /*return*/];
204
+ }
205
+ this.initialized = true;
206
+ _h.label = 1;
207
+ case 1:
208
+ _h.trys.push([1, 10, , 11]);
209
+ if (!database) return [3 /*break*/, 3];
210
+ logger.debug("Prepare a database[" + this.dbName + "]");
211
+ logger.stupid(this.dbName, database);
212
+ return [4 /*yield*/, this.query(database)];
213
+ case 2:
214
+ result = _h.sent();
215
+ logger.debug("Database[" + this.dbName + "] is initialized: " + JSON.stringify(result));
216
+ _h.label = 3;
217
+ case 3:
218
+ if (!this.dbName) return [3 /*break*/, 5];
219
+ return [4 /*yield*/, this.changeDatabase(this.dbName)];
220
+ case 4:
221
+ _h.sent();
222
+ logger.verbose("Database[" + this.dbName + "] is connected.");
223
+ _h.label = 5;
224
+ case 5:
225
+ _i = 0, _f = Object.entries(tables);
226
+ _h.label = 6;
227
+ case 6:
228
+ if (!(_i < _f.length)) return [3 /*break*/, 9];
229
+ _g = _f[_i], name_1 = _g[0], query = _g[1];
230
+ logger.debug("Prepare a table[" + name_1 + "]");
231
+ logger.stupid(name_1, query);
232
+ return [4 /*yield*/, this.query(query)];
233
+ case 7:
234
+ result = _h.sent();
235
+ logger.debug("Table[" + name_1 + "] is initialized: " + JSON.stringify(result));
236
+ _h.label = 8;
237
+ case 8:
238
+ _i++;
239
+ return [3 /*break*/, 6];
240
+ case 9:
241
+ logger.verbose("Database schema is initialized.");
242
+ return [3 /*break*/, 11];
243
+ case 10:
244
+ error_1 = _h.sent();
245
+ logger.warn(error_1);
246
+ if (!ignoreError) {
247
+ throw error_1;
248
+ }
249
+ return [3 /*break*/, 11];
250
+ case 11: return [2 /*return*/];
251
+ }
252
+ });
253
+ }); };
124
254
  this.pluginConfig = config;
125
255
  if (config.schema && config.schema.database) {
126
256
  this.dbName = config.config.database;
127
257
  config.config.database = undefined;
128
258
  }
129
259
  }
130
- }
131
- class LazyConnectionPool {
132
- constructor(config) {
133
- this.config = config;
134
- this.connection = null;
135
- this.getConnection = (callback) => {
136
- if (this.connection) {
137
- callback(null, this.connection);
138
- return;
139
- }
140
- const conn = mysql.createConnection(this.config);
141
- conn.connect((err) => {
142
- if (err) {
143
- callback(err, {});
144
- return;
260
+ return ConnectionProxy;
261
+ }());
262
+ exports.ConnectionProxy = ConnectionProxy;
263
+ var MySQLPlugin = /** @class */ (function (_super) {
264
+ __extends(MySQLPlugin, _super);
265
+ function MySQLPlugin(options) {
266
+ var _this = _super.call(this) || this;
267
+ _this.create = function () { return __awaiter(_this, void 0, void 0, function () {
268
+ return __generator(this, function (_a) {
269
+ switch (_a.label) {
270
+ case 0: return [4 /*yield*/, this.proxy.onPluginCreated()];
271
+ case 1:
272
+ _a.sent();
273
+ return [2 /*return*/, { db: this.proxy }];
145
274
  }
146
- this.connection = this.addDummyRelease(conn);
147
- callback(null, this.connection);
148
275
  });
149
- };
150
- this.end = (callback) => {
151
- if (this.connection) {
152
- this.connection.end((err) => {
153
- this.connection = null;
154
- callback(err);
155
- });
156
- }
157
- else {
158
- callback(null);
159
- }
160
- };
161
- this.addDummyRelease = (connection) => new Proxy(connection, {
162
- get(target, prop, receiver) {
163
- return prop === 'release'
164
- ? () => { }
165
- : Reflect.get(target, prop, receiver);
166
- },
167
- });
168
- }
169
- }
170
- export class MySQLPlugin extends HandlerPluginBase {
171
- constructor(options) {
172
- super();
173
- this.create = async () => {
174
- await this.proxy.onPluginCreated();
175
- return { db: this.proxy, database: this.sqlClient };
176
- };
177
- this.end = () => {
178
- if (this.proxy) {
179
- this.proxy.clearConnection();
180
- }
181
- if (this.sqlClient) {
182
- this.sqlClient.destroy();
276
+ }); };
277
+ _this.end = function () {
278
+ if (_this.proxy) {
279
+ _this.proxy.clearConnection();
183
280
  }
184
281
  };
185
- this.proxy = new ConnectionProxy(options);
186
- this.sqlClient = new Kysely({
187
- dialect: new MysqlDialect({
188
- pool: new LazyConnectionPool(options.config),
189
- }),
190
- });
282
+ _this.proxy = new ConnectionProxy(options);
283
+ return _this;
191
284
  }
192
- }
193
- const build = (options) => new MySQLPlugin(options);
194
- export default build;
285
+ return MySQLPlugin;
286
+ }(base_1.HandlerPluginBase));
287
+ exports.MySQLPlugin = MySQLPlugin;
288
+ var build = function (options) { return new MySQLPlugin(options); };
289
+ exports.default = build;
@@ -1,6 +1,6 @@
1
1
  import * as AWS from 'aws-sdk';
2
- import { SimpleAWSConfigLoadParam } from '../aws/index.js';
3
- import { HandlerAuxBase, HandlerContext, HandlerPluginBase } from './base.js';
2
+ import { SimpleAWSConfigLoadParam } from '../aws';
3
+ import { HandlerAuxBase, HandlerContext, HandlerPluginBase } from './base';
4
4
  interface ITracerLog {
5
5
  uuid: string;
6
6
  timestamp: number;