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.
- package/dist/aws/config.d.ts +3 -3
- package/dist/aws/config.js +18 -13
- package/dist/aws/define.js +4 -2
- package/dist/aws/index.d.ts +3 -3
- package/dist/aws/index.js +8 -3
- package/dist/aws/simple.d.ts +11 -11
- package/dist/aws/simple.js +666 -403
- package/dist/index.d.ts +3 -3
- package/dist/index.js +8 -3
- package/dist/middleware/aws.d.ts +4 -4
- package/dist/middleware/aws.js +125 -38
- package/dist/middleware/base.d.ts +11 -10
- package/dist/middleware/base.js +104 -66
- package/dist/middleware/build.d.ts +2 -2
- package/dist/middleware/build.js +218 -91
- package/dist/middleware/index.d.ts +10 -10
- package/dist/middleware/index.js +21 -16
- package/dist/middleware/logger.d.ts +2 -2
- package/dist/middleware/logger.js +70 -13
- package/dist/middleware/mysql.d.ts +7 -12
- package/dist/middleware/mysql.js +268 -173
- package/dist/middleware/trace.d.ts +2 -2
- package/dist/middleware/trace.js +198 -99
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +7 -2
- package/dist/utils/logger.d.ts +2 -2
- package/dist/utils/logger.js +31 -25
- package/dist/utils/misc.d.ts +1 -1
- package/dist/utils/misc.js +5 -3
- package/package.json +3 -6
- package/src/aws/config.ts +1 -1
- package/src/aws/index.ts +3 -3
- package/src/aws/simple.ts +3 -4
- package/src/index.ts +3 -3
- package/src/middleware/aws.ts +3 -3
- package/src/middleware/base.ts +19 -12
- 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 +7 -74
- package/src/middleware/trace.ts +4 -4
- package/src/utils/index.ts +2 -2
- package/src/utils/logger.ts +1 -1
- package/tsconfig.json +4 -5
- package/.nvmrc +0 -1
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/logger';
|
|
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.APIGatewayEvent;
|
|
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 } {
|
|
34
34
|
return this.event.pathParameters || {};
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
get query(): { [key: string]: string
|
|
37
|
+
get query(): { [key: string]: string } {
|
|
38
38
|
return this.event.queryStringParameters || {};
|
|
39
39
|
}
|
|
40
40
|
|
|
@@ -50,30 +50,33 @@ export class HandlerRequest {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
const CORS_HEADERS = {
|
|
54
|
+
'Access-Control-Allow-Origin': '*',
|
|
55
|
+
'Access-Control-Allow-Headers': 'X-Version',
|
|
56
|
+
'Access-Control-Allow-Credentials': true,
|
|
57
|
+
};
|
|
58
|
+
|
|
53
59
|
export class HandlerResponse {
|
|
54
60
|
public callback: any;
|
|
55
61
|
public completed: boolean;
|
|
56
62
|
public result: any | Promise<any> | undefined;
|
|
57
63
|
|
|
58
|
-
private corsHeaders: { [header: string]: any };
|
|
59
64
|
private cookies: string[];
|
|
60
65
|
private crossOrigin?: string;
|
|
66
|
+
private customHeaders: { [header: string]: any };
|
|
61
67
|
|
|
62
68
|
constructor(callback: any) {
|
|
63
69
|
this.callback = callback;
|
|
64
70
|
this.completed = false;
|
|
65
|
-
this.corsHeaders = {
|
|
66
|
-
'Access-Control-Allow-Origin': '*',
|
|
67
|
-
'Access-Control-Allow-Headers': 'X-Version',
|
|
68
|
-
'Access-Control-Allow-Credentials': true,
|
|
69
|
-
};
|
|
70
71
|
this.cookies = [];
|
|
72
|
+
this.customHeaders = {};
|
|
71
73
|
}
|
|
72
74
|
|
|
73
75
|
public ok(body = {}, code = 200) {
|
|
74
76
|
logger.stupid(`ok`, body);
|
|
75
77
|
const headers = {
|
|
76
|
-
...
|
|
78
|
+
...CORS_HEADERS,
|
|
79
|
+
...this.customHeaders,
|
|
77
80
|
};
|
|
78
81
|
if (this.crossOrigin) {
|
|
79
82
|
headers['Access-Control-Allow-Origin'] = this.crossOrigin;
|
|
@@ -96,7 +99,7 @@ export class HandlerResponse {
|
|
|
96
99
|
logger.stupid(`fail`, body);
|
|
97
100
|
const result = this.callback(null, {
|
|
98
101
|
statusCode: code,
|
|
99
|
-
headers:
|
|
102
|
+
headers: CORS_HEADERS,
|
|
100
103
|
body: JSON.stringify(body),
|
|
101
104
|
});
|
|
102
105
|
this.completed = true;
|
|
@@ -124,6 +127,10 @@ export class HandlerResponse {
|
|
|
124
127
|
public setCrossOrigin = (origin?: string) => {
|
|
125
128
|
this.crossOrigin = origin;
|
|
126
129
|
};
|
|
130
|
+
|
|
131
|
+
public addHeader = (header: string, value: string) => {
|
|
132
|
+
this.customHeaders[header] = value;
|
|
133
|
+
};
|
|
127
134
|
}
|
|
128
135
|
|
|
129
136
|
export interface HandlerAuxBase {
|
package/src/middleware/build.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { getLogger } from '../utils/
|
|
1
|
+
import { getLogger } from '../utils/logger';
|
|
2
2
|
|
|
3
|
-
import { stringifyError } from '../utils
|
|
3
|
+
import { stringifyError } from '../utils';
|
|
4
4
|
import {
|
|
5
5
|
Handler,
|
|
6
6
|
HandlerAuxBase,
|
|
7
7
|
HandlerPluginBase,
|
|
8
8
|
HandlerRequest,
|
|
9
9
|
HandlerResponse,
|
|
10
|
-
} from './base
|
|
10
|
+
} from './base';
|
|
11
11
|
|
|
12
12
|
const logger = getLogger(__filename);
|
|
13
13
|
|
package/src/middleware/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import build from './build
|
|
1
|
+
import build from './build';
|
|
2
2
|
|
|
3
|
-
import aws from './aws
|
|
4
|
-
import logger from './logger
|
|
5
|
-
import mysql from './mysql
|
|
6
|
-
import trace from './trace
|
|
3
|
+
import aws from './aws';
|
|
4
|
+
import logger from './logger';
|
|
5
|
+
import mysql from './mysql';
|
|
6
|
+
import trace from './trace';
|
|
7
7
|
|
|
8
8
|
export const middleware = {
|
|
9
9
|
build,
|
|
@@ -13,8 +13,8 @@ export const middleware = {
|
|
|
13
13
|
mysql,
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export * from './base
|
|
17
|
-
export * from './aws
|
|
18
|
-
export * from './trace
|
|
19
|
-
export * from './logger
|
|
20
|
-
export * from './mysql
|
|
16
|
+
export * from './base';
|
|
17
|
+
export * from './aws';
|
|
18
|
+
export * from './trace';
|
|
19
|
+
export * from './logger';
|
|
20
|
+
export * from './mysql';
|
package/src/middleware/logger.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getLogger, Logger, LogLevel } from '../utils
|
|
2
|
-
import { HandlerAuxBase, HandlerPluginBase } from './base
|
|
1
|
+
import { getLogger, Logger, LogLevel } from '../utils';
|
|
2
|
+
import { HandlerAuxBase, HandlerPluginBase } from './base';
|
|
3
3
|
|
|
4
4
|
export interface LoggerPluginOptions {
|
|
5
5
|
name: string;
|
package/src/middleware/mysql.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Kysely, MysqlDialect, MysqlPool } from 'kysely';
|
|
2
1
|
import * as mysql from 'mysql';
|
|
3
2
|
|
|
4
|
-
import { getLogger } from '../utils
|
|
5
|
-
import { HandlerAuxBase, HandlerPluginBase } from './base
|
|
3
|
+
import { getLogger } from '../utils';
|
|
4
|
+
import { HandlerAuxBase, HandlerPluginBase } from './base';
|
|
6
5
|
|
|
7
6
|
const logger = getLogger(__filename);
|
|
8
7
|
|
|
@@ -128,7 +127,7 @@ export class ConnectionProxy {
|
|
|
128
127
|
};
|
|
129
128
|
|
|
130
129
|
private changeDatabase = (dbName: string) =>
|
|
131
|
-
new Promise
|
|
130
|
+
new Promise((resolve, reject) =>
|
|
132
131
|
this.prepareConnection().changeUser(
|
|
133
132
|
{
|
|
134
133
|
database: dbName,
|
|
@@ -183,95 +182,29 @@ export class ConnectionProxy {
|
|
|
183
182
|
};
|
|
184
183
|
}
|
|
185
184
|
|
|
186
|
-
interface
|
|
187
|
-
release: () => void;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
class LazyConnectionPool implements MysqlPool {
|
|
191
|
-
private connection: LazyMysqlPoolConnection | null = null;
|
|
192
|
-
|
|
193
|
-
constructor(private config: mysql.ConnectionConfig) {}
|
|
194
|
-
|
|
195
|
-
public getConnection = (
|
|
196
|
-
callback: (error: unknown, connection: LazyMysqlPoolConnection) => void,
|
|
197
|
-
): void => {
|
|
198
|
-
if (this.connection) {
|
|
199
|
-
callback(null, this.connection);
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
const conn = mysql.createConnection(this.config);
|
|
203
|
-
conn.connect((err: mysql.MysqlError) => {
|
|
204
|
-
if (err) {
|
|
205
|
-
callback(err, {} as LazyMysqlPoolConnection);
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
this.connection = this.addDummyRelease(conn);
|
|
209
|
-
callback(null, this.connection);
|
|
210
|
-
});
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
public end = (callback: (error: unknown) => void): void => {
|
|
214
|
-
if (this.connection) {
|
|
215
|
-
this.connection.end((err: mysql.MysqlError) => {
|
|
216
|
-
this.connection = null;
|
|
217
|
-
callback(err);
|
|
218
|
-
});
|
|
219
|
-
} else {
|
|
220
|
-
callback(null);
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
private addDummyRelease = (
|
|
225
|
-
connection: mysql.Connection,
|
|
226
|
-
): LazyMysqlPoolConnection =>
|
|
227
|
-
new Proxy(connection, {
|
|
228
|
-
get(target, prop, receiver) {
|
|
229
|
-
return prop === 'release'
|
|
230
|
-
? () => {}
|
|
231
|
-
: Reflect.get(target, prop, receiver);
|
|
232
|
-
},
|
|
233
|
-
}) as LazyMysqlPoolConnection;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
export type SQLClient<T = unknown> = Kysely<T>;
|
|
237
|
-
|
|
238
|
-
export interface MySQLPluginAux<T = unknown> extends HandlerAuxBase {
|
|
185
|
+
export interface MySQLPluginAux extends HandlerAuxBase {
|
|
239
186
|
db: ConnectionProxy;
|
|
240
|
-
database: SQLClient<T>;
|
|
241
187
|
}
|
|
242
188
|
|
|
243
|
-
export class MySQLPlugin
|
|
244
|
-
MySQLPluginAux<T>
|
|
245
|
-
> {
|
|
189
|
+
export class MySQLPlugin extends HandlerPluginBase<MySQLPluginAux> {
|
|
246
190
|
private proxy: ConnectionProxy;
|
|
247
|
-
private sqlClient: SQLClient<T>;
|
|
248
191
|
|
|
249
192
|
constructor(options: MySQLPluginOptions) {
|
|
250
193
|
super();
|
|
251
194
|
this.proxy = new ConnectionProxy(options);
|
|
252
|
-
this.sqlClient = new Kysely<T>({
|
|
253
|
-
dialect: new MysqlDialect({
|
|
254
|
-
pool: new LazyConnectionPool(options.config),
|
|
255
|
-
}),
|
|
256
|
-
});
|
|
257
195
|
}
|
|
258
196
|
|
|
259
197
|
public create = async () => {
|
|
260
198
|
await this.proxy.onPluginCreated();
|
|
261
|
-
return { db: this.proxy
|
|
199
|
+
return { db: this.proxy };
|
|
262
200
|
};
|
|
263
201
|
|
|
264
202
|
public end = () => {
|
|
265
203
|
if (this.proxy) {
|
|
266
204
|
this.proxy.clearConnection();
|
|
267
205
|
}
|
|
268
|
-
|
|
269
|
-
if (this.sqlClient) {
|
|
270
|
-
this.sqlClient.destroy();
|
|
271
|
-
}
|
|
272
206
|
};
|
|
273
207
|
}
|
|
274
208
|
|
|
275
|
-
const build =
|
|
276
|
-
new MySQLPlugin<T>(options);
|
|
209
|
+
const build = (options: MySQLPluginOptions) => new MySQLPlugin(options);
|
|
277
210
|
export default build;
|
package/src/middleware/trace.ts
CHANGED
|
@@ -5,11 +5,11 @@ import {
|
|
|
5
5
|
loadAWSConfig,
|
|
6
6
|
SimpleAWS,
|
|
7
7
|
SimpleAWSConfigLoadParam,
|
|
8
|
-
} from '../aws
|
|
9
|
-
import { getLogger } from '../utils
|
|
10
|
-
|
|
8
|
+
} from '../aws';
|
|
9
|
+
import { getLogger, stringifyError } from '../utils';
|
|
10
|
+
|
|
11
11
|
import { $enum } from 'ts-enum-util';
|
|
12
|
-
import { HandlerAuxBase, HandlerContext, HandlerPluginBase } from './base
|
|
12
|
+
import { HandlerAuxBase, HandlerContext, HandlerPluginBase } from './base';
|
|
13
13
|
|
|
14
14
|
const logger = getLogger(__filename);
|
|
15
15
|
|
package/src/utils/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './logger
|
|
2
|
-
export * from './misc
|
|
1
|
+
export * from './logger';
|
|
2
|
+
export * from './misc';
|
package/src/utils/logger.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { basename } from 'path';
|
|
2
2
|
import { envDefault as currentStage, StagingLevel } from 'simple-staging';
|
|
3
3
|
import { $enum } from 'ts-enum-util';
|
|
4
|
-
import { stringifyError } from './misc
|
|
4
|
+
import { stringifyError } from './misc';
|
|
5
5
|
|
|
6
6
|
export enum LogLevel {
|
|
7
7
|
Error = 'error',
|
package/tsconfig.json
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"target": "
|
|
4
|
-
"module": "
|
|
5
|
-
"
|
|
6
|
-
"lib": ["ES2020", "DOM"],
|
|
3
|
+
"target": "es5",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["es2017", "es7", "es6", "esnext", "dom"],
|
|
7
6
|
"rootDir": "src",
|
|
8
7
|
"outDir": "dist",
|
|
9
8
|
"declaration": true,
|
|
@@ -13,4 +12,4 @@
|
|
|
13
12
|
"noUnusedParameters": true
|
|
14
13
|
},
|
|
15
14
|
"exclude": ["node_modules", "dist", "__tests__"]
|
|
16
|
-
}
|
|
15
|
+
}
|
package/.nvmrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
v16
|