serverless-simple-middleware 0.0.53 → 0.0.56

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 (51) hide show
  1. package/.prettierignore +2 -2
  2. package/README.md +4 -4
  3. package/dist/aws/config.d.ts +15 -15
  4. package/dist/aws/config.js +29 -29
  5. package/dist/aws/define.d.ts +21 -21
  6. package/dist/aws/define.js +8 -8
  7. package/dist/aws/index.d.ts +3 -3
  8. package/dist/aws/index.js +8 -8
  9. package/dist/aws/simple.d.ts +44 -44
  10. package/dist/aws/simple.js +659 -656
  11. package/dist/index.d.ts +3 -3
  12. package/dist/index.js +8 -8
  13. package/dist/middleware/aws.d.ts +25 -25
  14. package/dist/middleware/aws.js +128 -128
  15. package/dist/middleware/base.d.ts +54 -54
  16. package/dist/middleware/base.js +140 -140
  17. package/dist/middleware/build.d.ts +3 -3
  18. package/dist/middleware/build.js +234 -234
  19. package/dist/middleware/index.d.ts +12 -12
  20. package/dist/middleware/index.js +22 -22
  21. package/dist/middleware/logger.d.ts +18 -18
  22. package/dist/middleware/logger.js +71 -71
  23. package/dist/middleware/mysql.d.ts +44 -44
  24. package/dist/middleware/mysql.js +289 -289
  25. package/dist/middleware/trace.d.ts +86 -86
  26. package/dist/middleware/trace.js +255 -255
  27. package/dist/utils/index.d.ts +2 -2
  28. package/dist/utils/index.js +7 -7
  29. package/dist/utils/logger.d.ts +26 -26
  30. package/dist/utils/logger.js +73 -73
  31. package/dist/utils/misc.d.ts +1 -1
  32. package/dist/utils/misc.js +9 -9
  33. package/jest.config.js +7 -7
  34. package/package.json +61 -61
  35. package/src/aws/config.ts +46 -46
  36. package/src/aws/define.ts +29 -29
  37. package/src/aws/index.ts +3 -3
  38. package/src/aws/simple.ts +529 -531
  39. package/src/index.ts +3 -3
  40. package/src/middleware/aws.ts +78 -78
  41. package/src/middleware/base.ts +164 -164
  42. package/src/middleware/build.ts +173 -173
  43. package/src/middleware/index.ts +20 -20
  44. package/src/middleware/logger.ts +28 -28
  45. package/src/middleware/mysql.ts +210 -210
  46. package/src/middleware/trace.ts +269 -269
  47. package/src/utils/index.ts +2 -2
  48. package/src/utils/logger.ts +94 -94
  49. package/src/utils/misc.ts +11 -11
  50. package/tsconfig.json +15 -15
  51. package/tslint.json +12 -12
@@ -1,269 +1,269 @@
1
- import * as AWS from 'aws-sdk'; // tslint:disable-line
2
- import { v4 as uuid4 } from 'uuid';
3
- import {
4
- AWSComponent,
5
- loadAWSConfig,
6
- SimpleAWS,
7
- SimpleAWSConfigLoadParam,
8
- } from '../aws';
9
- import { getLogger, stringifyError } from '../utils';
10
-
11
- import { $enum } from 'ts-enum-util';
12
- import { HandlerAuxBase, HandlerContext, HandlerPluginBase } from './base';
13
-
14
- const logger = getLogger(__filename);
15
-
16
- interface ITracerLog {
17
- uuid: string;
18
- timestamp: number;
19
- route: string;
20
- key: string;
21
- system: string;
22
- action: string;
23
- attribute: string;
24
- body: string;
25
- error: boolean;
26
- client: string;
27
- version: string;
28
- }
29
-
30
- interface ITracerLogInput {
31
- route?: string;
32
- key?: string;
33
- system?: string;
34
- action?: string;
35
- attribute: string;
36
- body: string;
37
- error?: boolean;
38
- client?: string;
39
- version?: string;
40
- }
41
-
42
- export class TracerLog implements ITracerLog {
43
- public readonly uuid: string;
44
- public readonly timestamp: number;
45
-
46
- constructor(
47
- public readonly route: string,
48
- public readonly key: string,
49
- public readonly system: string,
50
- public readonly action: string,
51
- public readonly attribute: string,
52
- public readonly body: string,
53
- public readonly error: boolean,
54
- public readonly client: string,
55
- public readonly version: string,
56
- ) {
57
- this.uuid = uuid4();
58
- this.timestamp = Date.now();
59
- }
60
- }
61
-
62
- export class Tracer {
63
- private queueName: string;
64
- private sqs: AWS.SQS;
65
- private buffer: TracerLog[];
66
-
67
- constructor(queueName: string, sqs: AWS.SQS) {
68
- this.queueName = queueName;
69
- this.sqs = sqs;
70
- this.buffer = [];
71
- }
72
-
73
- public push = (log: TracerLog) => this.buffer.push(log);
74
-
75
- public flush = async () => {
76
- if (this.buffer.length === 0) {
77
- return;
78
- }
79
- try {
80
- const urlResult = await this.sqs
81
- .getQueueUrl({
82
- QueueName: this.queueName,
83
- })
84
- .promise();
85
- logger.stupid(`urlResult`, urlResult);
86
- if (!urlResult.QueueUrl) {
87
- throw new Error(`No queue url with name[${this.queueName}]`);
88
- }
89
- const eventQueueUrl = urlResult.QueueUrl;
90
-
91
- const chunkSize = 10;
92
- for (let begin = 0; begin < this.buffer.length; begin += chunkSize) {
93
- const end = Math.min(this.buffer.length, begin + chunkSize);
94
- const subset = this.buffer.slice(begin, end);
95
- const sendBatchResult = await this.sqs
96
- .sendMessageBatch({
97
- QueueUrl: eventQueueUrl,
98
- Entries: subset.map(each => ({
99
- Id: `${each.key}_${each.uuid}`,
100
- MessageBody: JSON.stringify(each),
101
- })),
102
- })
103
- .promise();
104
- logger.stupid(`sendBatchResult`, sendBatchResult);
105
- }
106
-
107
- this.buffer = [];
108
- } catch (error) {
109
- logger.warn(`Error in eventSource: ${error}`);
110
- }
111
- };
112
- }
113
-
114
- export class TracerWrapper {
115
- constructor(
116
- private tracer: Tracer,
117
- private route: string,
118
- private system: string,
119
- private key: string,
120
- private action: string,
121
- private client: string,
122
- private version: string,
123
- ) {}
124
-
125
- public push = (attribute: string, body: string, error: boolean = false) => {
126
- this.tracer.push(
127
- new TracerLog(
128
- this.route,
129
- this.key,
130
- this.system,
131
- this.action,
132
- attribute,
133
- body,
134
- error,
135
- this.client,
136
- this.version,
137
- ),
138
- );
139
- };
140
-
141
- public send = (log: ITracerLogInput) => {
142
- this.tracer.push(
143
- new TracerLog(
144
- log.route || this.route,
145
- log.key || this.key,
146
- log.system || this.system,
147
- log.action || this.action,
148
- log.attribute,
149
- log.body,
150
- log.error || false,
151
- log.client || this.client,
152
- log.version || this.version,
153
- ),
154
- );
155
- };
156
- }
157
-
158
- export interface TracerPluginOptions {
159
- route: string;
160
- queueName: string;
161
- system: string;
162
-
163
- awsConfig?: SimpleAWSConfigLoadParam;
164
- region?: string;
165
- }
166
-
167
- export interface TracerPluginAux extends HandlerAuxBase {
168
- tracer: (key: string, action: string) => TracerWrapper;
169
- }
170
-
171
- export class TracerPlugin extends HandlerPluginBase<TracerPluginAux> {
172
- private tracer: Tracer;
173
- private options: TracerPluginOptions;
174
- private last: { key: string; action: string };
175
- private client: { agent: string; version: string };
176
-
177
- constructor(options: TracerPluginOptions) {
178
- super();
179
- this.options = options;
180
- this.last = {
181
- key: 'nothing',
182
- action: 'unknown',
183
- };
184
- this.client = {
185
- agent: '',
186
- version: '',
187
- };
188
- }
189
-
190
- public create = async () => {
191
- const awsConfig = this.options.awsConfig
192
- ? await loadAWSConfig(this.options.awsConfig)
193
- : undefined;
194
-
195
- const sqs = (() => {
196
- if (!awsConfig) {
197
- return new AWS.SQS({
198
- region: this.options.region,
199
- });
200
- }
201
- $enum(AWSComponent).forEach(eachComponent => {
202
- const config = awsConfig.get(eachComponent);
203
- if (config) {
204
- config.region = this.options.region;
205
- }
206
- });
207
- return new SimpleAWS(awsConfig).sqs;
208
- })();
209
-
210
- this.tracer = new Tracer(this.options.queueName, sqs);
211
- const tracer = (key: string, action: string) => {
212
- this.last = { key, action };
213
- return new TracerWrapper(
214
- this.tracer,
215
- this.options.route,
216
- this.options.system,
217
- key,
218
- action,
219
- this.client.agent,
220
- this.client.version,
221
- );
222
- };
223
- return { tracer };
224
- };
225
-
226
- public begin = ({ request }: HandlerContext<TracerPluginAux>) => {
227
- this.client.version = request.header('X-Version') || '0.0.0';
228
- this.client.agent = (() => {
229
- const fromHeader = request.header('User-Agent');
230
- if (fromHeader) {
231
- return fromHeader;
232
- }
233
- if (
234
- request.context &&
235
- request.context.identity &&
236
- request.context.identity.userAgent
237
- ) {
238
- return request.context.identity.userAgent;
239
- }
240
- return '';
241
- })();
242
- };
243
-
244
- public end = () => this.tracer.flush();
245
-
246
- public error = ({ request, aux }: HandlerContext<TracerPluginAux>) => {
247
- if (!aux) {
248
- console.warn('Aux is not initialized');
249
- return;
250
- }
251
- if (!request.lastError) {
252
- return;
253
- }
254
-
255
- const { key, action } = this.last;
256
- aux
257
- .tracer(key, action)
258
- .push(
259
- 'error',
260
- typeof request.lastError === 'string'
261
- ? request.lastError
262
- : stringifyError(request.lastError),
263
- true,
264
- );
265
- };
266
- }
267
-
268
- const build = (options: TracerPluginOptions) => new TracerPlugin(options);
269
- export default build;
1
+ import * as AWS from 'aws-sdk'; // tslint:disable-line
2
+ import { v4 as uuid4 } from 'uuid';
3
+ import {
4
+ AWSComponent,
5
+ loadAWSConfig,
6
+ SimpleAWS,
7
+ SimpleAWSConfigLoadParam,
8
+ } from '../aws';
9
+ import { getLogger, stringifyError } from '../utils';
10
+
11
+ import { $enum } from 'ts-enum-util';
12
+ import { HandlerAuxBase, HandlerContext, HandlerPluginBase } from './base';
13
+
14
+ const logger = getLogger(__filename);
15
+
16
+ interface ITracerLog {
17
+ uuid: string;
18
+ timestamp: number;
19
+ route: string;
20
+ key: string;
21
+ system: string;
22
+ action: string;
23
+ attribute: string;
24
+ body: string;
25
+ error: boolean;
26
+ client: string;
27
+ version: string;
28
+ }
29
+
30
+ interface ITracerLogInput {
31
+ route?: string;
32
+ key?: string;
33
+ system?: string;
34
+ action?: string;
35
+ attribute: string;
36
+ body: string;
37
+ error?: boolean;
38
+ client?: string;
39
+ version?: string;
40
+ }
41
+
42
+ export class TracerLog implements ITracerLog {
43
+ public readonly uuid: string;
44
+ public readonly timestamp: number;
45
+
46
+ constructor(
47
+ public readonly route: string,
48
+ public readonly key: string,
49
+ public readonly system: string,
50
+ public readonly action: string,
51
+ public readonly attribute: string,
52
+ public readonly body: string,
53
+ public readonly error: boolean,
54
+ public readonly client: string,
55
+ public readonly version: string,
56
+ ) {
57
+ this.uuid = uuid4();
58
+ this.timestamp = Date.now();
59
+ }
60
+ }
61
+
62
+ export class Tracer {
63
+ private queueName: string;
64
+ private sqs: AWS.SQS;
65
+ private buffer: TracerLog[];
66
+
67
+ constructor(queueName: string, sqs: AWS.SQS) {
68
+ this.queueName = queueName;
69
+ this.sqs = sqs;
70
+ this.buffer = [];
71
+ }
72
+
73
+ public push = (log: TracerLog) => this.buffer.push(log);
74
+
75
+ public flush = async () => {
76
+ if (this.buffer.length === 0) {
77
+ return;
78
+ }
79
+ try {
80
+ const urlResult = await this.sqs
81
+ .getQueueUrl({
82
+ QueueName: this.queueName,
83
+ })
84
+ .promise();
85
+ logger.stupid(`urlResult`, urlResult);
86
+ if (!urlResult.QueueUrl) {
87
+ throw new Error(`No queue url with name[${this.queueName}]`);
88
+ }
89
+ const eventQueueUrl = urlResult.QueueUrl;
90
+
91
+ const chunkSize = 10;
92
+ for (let begin = 0; begin < this.buffer.length; begin += chunkSize) {
93
+ const end = Math.min(this.buffer.length, begin + chunkSize);
94
+ const subset = this.buffer.slice(begin, end);
95
+ const sendBatchResult = await this.sqs
96
+ .sendMessageBatch({
97
+ QueueUrl: eventQueueUrl,
98
+ Entries: subset.map(each => ({
99
+ Id: `${each.key}_${each.uuid}`,
100
+ MessageBody: JSON.stringify(each),
101
+ })),
102
+ })
103
+ .promise();
104
+ logger.stupid(`sendBatchResult`, sendBatchResult);
105
+ }
106
+
107
+ this.buffer = [];
108
+ } catch (error) {
109
+ logger.warn(`Error in eventSource: ${error}`);
110
+ }
111
+ };
112
+ }
113
+
114
+ export class TracerWrapper {
115
+ constructor(
116
+ private tracer: Tracer,
117
+ private route: string,
118
+ private system: string,
119
+ private key: string,
120
+ private action: string,
121
+ private client: string,
122
+ private version: string,
123
+ ) {}
124
+
125
+ public push = (attribute: string, body: string, error: boolean = false) => {
126
+ this.tracer.push(
127
+ new TracerLog(
128
+ this.route,
129
+ this.key,
130
+ this.system,
131
+ this.action,
132
+ attribute,
133
+ body,
134
+ error,
135
+ this.client,
136
+ this.version,
137
+ ),
138
+ );
139
+ };
140
+
141
+ public send = (log: ITracerLogInput) => {
142
+ this.tracer.push(
143
+ new TracerLog(
144
+ log.route || this.route,
145
+ log.key || this.key,
146
+ log.system || this.system,
147
+ log.action || this.action,
148
+ log.attribute,
149
+ log.body,
150
+ log.error || false,
151
+ log.client || this.client,
152
+ log.version || this.version,
153
+ ),
154
+ );
155
+ };
156
+ }
157
+
158
+ export interface TracerPluginOptions {
159
+ route: string;
160
+ queueName: string;
161
+ system: string;
162
+
163
+ awsConfig?: SimpleAWSConfigLoadParam;
164
+ region?: string;
165
+ }
166
+
167
+ export interface TracerPluginAux extends HandlerAuxBase {
168
+ tracer: (key: string, action: string) => TracerWrapper;
169
+ }
170
+
171
+ export class TracerPlugin extends HandlerPluginBase<TracerPluginAux> {
172
+ private tracer: Tracer;
173
+ private options: TracerPluginOptions;
174
+ private last: { key: string; action: string };
175
+ private client: { agent: string; version: string };
176
+
177
+ constructor(options: TracerPluginOptions) {
178
+ super();
179
+ this.options = options;
180
+ this.last = {
181
+ key: 'nothing',
182
+ action: 'unknown',
183
+ };
184
+ this.client = {
185
+ agent: '',
186
+ version: '',
187
+ };
188
+ }
189
+
190
+ public create = async () => {
191
+ const awsConfig = this.options.awsConfig
192
+ ? await loadAWSConfig(this.options.awsConfig)
193
+ : undefined;
194
+
195
+ const sqs = (() => {
196
+ if (!awsConfig) {
197
+ return new AWS.SQS({
198
+ region: this.options.region,
199
+ });
200
+ }
201
+ $enum(AWSComponent).forEach(eachComponent => {
202
+ const config = awsConfig.get(eachComponent);
203
+ if (config) {
204
+ config.region = this.options.region;
205
+ }
206
+ });
207
+ return new SimpleAWS(awsConfig).sqs;
208
+ })();
209
+
210
+ this.tracer = new Tracer(this.options.queueName, sqs);
211
+ const tracer = (key: string, action: string) => {
212
+ this.last = { key, action };
213
+ return new TracerWrapper(
214
+ this.tracer,
215
+ this.options.route,
216
+ this.options.system,
217
+ key,
218
+ action,
219
+ this.client.agent,
220
+ this.client.version,
221
+ );
222
+ };
223
+ return { tracer };
224
+ };
225
+
226
+ public begin = ({ request }: HandlerContext<TracerPluginAux>) => {
227
+ this.client.version = request.header('X-Version') || '0.0.0';
228
+ this.client.agent = (() => {
229
+ const fromHeader = request.header('User-Agent');
230
+ if (fromHeader) {
231
+ return fromHeader;
232
+ }
233
+ if (
234
+ request.context &&
235
+ request.context.identity &&
236
+ request.context.identity.userAgent
237
+ ) {
238
+ return request.context.identity.userAgent;
239
+ }
240
+ return '';
241
+ })();
242
+ };
243
+
244
+ public end = () => this.tracer.flush();
245
+
246
+ public error = ({ request, aux }: HandlerContext<TracerPluginAux>) => {
247
+ if (!aux) {
248
+ console.warn('Aux is not initialized');
249
+ return;
250
+ }
251
+ if (!request.lastError) {
252
+ return;
253
+ }
254
+
255
+ const { key, action } = this.last;
256
+ aux
257
+ .tracer(key, action)
258
+ .push(
259
+ 'error',
260
+ typeof request.lastError === 'string'
261
+ ? request.lastError
262
+ : stringifyError(request.lastError),
263
+ true,
264
+ );
265
+ };
266
+ }
267
+
268
+ const build = (options: TracerPluginOptions) => new TracerPlugin(options);
269
+ export default build;
@@ -1,2 +1,2 @@
1
- export * from './logger';
2
- export * from './misc';
1
+ export * from './logger';
2
+ export * from './misc';