@middy/util 3.0.0-alpha.2 → 3.0.0-alpha.6

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 (3) hide show
  1. package/index.js +103 -55
  2. package/package.json +5 -4
  3. package/codes.js +0 -66
package/index.js CHANGED
@@ -1,6 +1,4 @@
1
1
  import { Agent } from 'https';
2
- import statuses from './codes.js';
3
- import { inherits } from 'util';
4
2
  export const awsClientDefaultOptions = {
5
3
  httpOptions: {
6
4
  agent: new Agent({
@@ -26,7 +24,10 @@ export const createClient = async (options, request) => {
26
24
  let awsClientCredentials = {};
27
25
 
28
26
  if (options.awsClientAssumeRole) {
29
- if (!request) throw new Error('Request required when assuming role');
27
+ if (!request) {
28
+ throw new Error('Request required when assuming role');
29
+ }
30
+
30
31
  awsClientCredentials = await getInternal({
31
32
  credentials: options.awsClientAssumeRole
32
33
  }, request);
@@ -65,7 +66,7 @@ export const getInternal = async (variables, request) => {
65
66
  const rootOptionKey = pathOptionKey.shift();
66
67
  let valuePromise = request.internal[rootOptionKey];
67
68
 
68
- if (typeof valuePromise.then !== 'function') {
69
+ if (!isPromise(valuePromise)) {
69
70
  valuePromise = Promise.resolve(valuePromise);
70
71
  }
71
72
 
@@ -85,6 +86,9 @@ export const getInternal = async (variables, request) => {
85
86
  [sanitizeKey(key)]: values[index].value
86
87
  }), {});
87
88
  };
89
+
90
+ const isPromise = promise => typeof (promise === null || promise === void 0 ? void 0 : promise.then) === 'function';
91
+
88
92
  const sanitizeKeyPrefixLeadingNumber = /^([0-9])/;
89
93
  const sanitizeKeyRemoveDisallowedChar = /[^a-zA-Z0-9]+/g;
90
94
  export const sanitizeKey = key => {
@@ -153,16 +157,23 @@ export const clearCache = (keys = null) => {
153
157
  cache[cacheKey] = undefined;
154
158
  }
155
159
  };
156
- export const jsonSafeParse = (string, reviver) => {
157
- if (typeof string !== 'string') return string;
158
- const firstChar = string[0];
159
- if (firstChar !== '{' && firstChar !== '[' && firstChar !== '"') return string;
160
+ export const jsonSafeParse = (text, reviver) => {
161
+ if (typeof text !== 'string') return text;
162
+ const firstChar = text[0];
163
+ if (firstChar !== '{' && firstChar !== '[' && firstChar !== '"') return text;
164
+
165
+ try {
166
+ return JSON.parse(text, reviver);
167
+ } catch (e) {}
160
168
 
169
+ return text;
170
+ };
171
+ export const jsonSafeStringify = (value, replacer, space) => {
161
172
  try {
162
- return JSON.parse(string, reviver);
173
+ return JSON.stringify(value, replacer, space);
163
174
  } catch (e) {}
164
175
 
165
- return string;
176
+ return value;
166
177
  };
167
178
  export const normalizeHttpResponse = request => {
168
179
  var _response, _response2, _response3, _response4;
@@ -184,52 +195,89 @@ export const normalizeHttpResponse = request => {
184
195
  return response;
185
196
  };
186
197
  const createErrorRegexp = /[^a-zA-Z]/g;
187
- export const createError = (code, message, properties = {}) => {
188
- const name = statuses[code].replace(createErrorRegexp, '');
189
- const className = name.substr(-5) !== 'Error' ? name + 'Error' : name;
190
-
191
- function HttpError(message) {
192
- const msg = message ?? statuses[code];
193
- const err = new Error(msg);
194
- Error.captureStackTrace(err, HttpError);
195
- Object.setPrototypeOf(err, HttpError.prototype);
196
- Object.defineProperty(err, 'message', {
197
- enumerable: true,
198
- configurable: true,
199
- value: msg,
200
- writable: true
201
- });
202
- Object.defineProperty(err, 'name', {
203
- enumerable: false,
204
- configurable: true,
205
- value: className,
206
- writable: true
207
- });
208
- return err;
198
+ export class HttpError extends Error {
199
+ constructor(code, message, options = {}) {
200
+ if (message && typeof message !== 'string') {
201
+ options = message;
202
+ message = undefined;
203
+ }
204
+
205
+ message ?? (message = httpErrorCodes[code]);
206
+ super(message);
207
+ this.cause = options.cause;
208
+ const name = httpErrorCodes[code].replace(createErrorRegexp, '');
209
+ this.name = name.substr(-5) !== 'Error' ? name + 'Error' : name;
210
+ this.status = this.statusCode = code;
211
+ this.expose = options.expose ?? code < 500;
209
212
  }
210
213
 
211
- inherits(HttpError, Error);
212
- const desc = Object.getOwnPropertyDescriptor(HttpError, 'name');
213
- desc.value = className;
214
- Object.defineProperty(HttpError, 'name', desc);
215
- Object.assign(HttpError.prototype, {
216
- status: code,
217
- statusCode: code,
218
- expose: code < 500
219
- }, properties);
220
- return new HttpError(message);
214
+ }
215
+ export const createError = (code, message, properties = {}) => {
216
+ return new HttpError(code, message, properties);
221
217
  };
222
- export default {
223
- createPrefetchClient,
224
- createClient,
225
- canPrefetch,
226
- getInternal,
227
- sanitizeKey,
228
- processCache,
229
- getCache,
230
- modifyCache,
231
- clearCache,
232
- jsonSafeParse,
233
- normalizeHttpResponse,
234
- createError
218
+ const httpErrorCodes = {
219
+ 100: 'Continue',
220
+ 101: 'Switching Protocols',
221
+ 102: 'Processing',
222
+ 103: 'Early Hints',
223
+ 200: 'OK',
224
+ 201: 'Created',
225
+ 202: 'Accepted',
226
+ 203: 'Non-Authoritative Information',
227
+ 204: 'No Content',
228
+ 205: 'Reset Content',
229
+ 206: 'Partial Content',
230
+ 207: 'Multi-Status',
231
+ 208: 'Already Reported',
232
+ 226: 'IM Used',
233
+ 300: 'Multiple Choices',
234
+ 301: 'Moved Permanently',
235
+ 302: 'Found',
236
+ 303: 'See Other',
237
+ 304: 'Not Modified',
238
+ 305: 'Use Proxy',
239
+ 306: '(Unused)',
240
+ 307: 'Temporary Redirect',
241
+ 308: 'Permanent Redirect',
242
+ 400: 'Bad Request',
243
+ 401: 'Unauthorized',
244
+ 402: 'Payment Required',
245
+ 403: 'Forbidden',
246
+ 404: 'Not Found',
247
+ 405: 'Method Not Allowed',
248
+ 406: 'Not Acceptable',
249
+ 407: 'Proxy Authentication Required',
250
+ 408: 'Request Timeout',
251
+ 409: 'Conflict',
252
+ 410: 'Gone',
253
+ 411: 'Length Required',
254
+ 412: 'Precondition Failed',
255
+ 413: 'Payload Too Large',
256
+ 414: 'URI Too Long',
257
+ 415: 'Unsupported Media Type',
258
+ 416: 'Range Not Satisfiable',
259
+ 417: 'Expectation Failed',
260
+ 418: "I'm a teapot",
261
+ 421: 'Misdirected Request',
262
+ 422: 'Unprocessable Entity',
263
+ 423: 'Locked',
264
+ 424: 'Failed Dependency',
265
+ 425: 'Unordered Collection',
266
+ 426: 'Upgrade Required',
267
+ 428: 'Precondition Required',
268
+ 429: 'Too Many Requests',
269
+ 431: 'Request Header Fields Too Large',
270
+ 451: 'Unavailable For Legal Reasons',
271
+ 500: 'Internal Server Error',
272
+ 501: 'Not Implemented',
273
+ 502: 'Bad Gateway',
274
+ 503: 'Service Unavailable',
275
+ 504: 'Gateway Timeout',
276
+ 505: 'HTTP Version Not Supported',
277
+ 506: 'Variant Also Negotiates',
278
+ 507: 'Insufficient Storage',
279
+ 508: 'Loop Detected',
280
+ 509: 'Bandwidth Limit Exceeded',
281
+ 510: 'Not Extended',
282
+ 511: 'Network Authentication Required'
235
283
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/util",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.6",
4
4
  "description": "🛵 The stylish Node.js middleware engine for AWS Lambda (util package)",
5
5
  "type": "module",
6
6
  "engines": {
@@ -19,7 +19,8 @@
19
19
  ],
20
20
  "scripts": {
21
21
  "test": "npm run test:unit",
22
- "test:unit": "ava"
22
+ "test:unit": "ava",
23
+ "test:benchmark": "node __benchmarks__/index.js"
23
24
  },
24
25
  "license": "MIT",
25
26
  "keywords": [
@@ -45,12 +46,12 @@
45
46
  "url": "https://github.com/middyjs/middy/issues"
46
47
  },
47
48
  "devDependencies": {
48
- "@middy/core": "^3.0.0-alpha.2",
49
+ "@middy/core": "^3.0.0-alpha.6",
49
50
  "@types/aws-lambda": "^8.10.76",
50
51
  "@types/node": "^17.0.0",
51
52
  "aws-sdk": "^2.939.0",
52
53
  "aws-xray-sdk": "^3.3.3"
53
54
  },
54
55
  "homepage": "https://github.com/middyjs/middy#readme",
55
- "gitHead": "de30419273ecbff08f367f47c7e320ec981cf145"
56
+ "gitHead": "176660ed3e0716d6bfb635c77251b301e0e24720"
56
57
  }
package/codes.js DELETED
@@ -1,66 +0,0 @@
1
- export default {
2
- 100: 'Continue',
3
- 101: 'Switching Protocols',
4
- 102: 'Processing',
5
- 103: 'Early Hints',
6
- 200: 'OK',
7
- 201: 'Created',
8
- 202: 'Accepted',
9
- 203: 'Non-Authoritative Information',
10
- 204: 'No Content',
11
- 205: 'Reset Content',
12
- 206: 'Partial Content',
13
- 207: 'Multi-Status',
14
- 208: 'Already Reported',
15
- 226: 'IM Used',
16
- 300: 'Multiple Choices',
17
- 301: 'Moved Permanently',
18
- 302: 'Found',
19
- 303: 'See Other',
20
- 304: 'Not Modified',
21
- 305: 'Use Proxy',
22
- 306: '(Unused)',
23
- 307: 'Temporary Redirect',
24
- 308: 'Permanent Redirect',
25
- 400: 'Bad Request',
26
- 401: 'Unauthorized',
27
- 402: 'Payment Required',
28
- 403: 'Forbidden',
29
- 404: 'Not Found',
30
- 405: 'Method Not Allowed',
31
- 406: 'Not Acceptable',
32
- 407: 'Proxy Authentication Required',
33
- 408: 'Request Timeout',
34
- 409: 'Conflict',
35
- 410: 'Gone',
36
- 411: 'Length Required',
37
- 412: 'Precondition Failed',
38
- 413: 'Payload Too Large',
39
- 414: 'URI Too Long',
40
- 415: 'Unsupported Media Type',
41
- 416: 'Range Not Satisfiable',
42
- 417: 'Expectation Failed',
43
- 418: "I'm a teapot",
44
- 421: 'Misdirected Request',
45
- 422: 'Unprocessable Entity',
46
- 423: 'Locked',
47
- 424: 'Failed Dependency',
48
- 425: 'Unordered Collection',
49
- 426: 'Upgrade Required',
50
- 428: 'Precondition Required',
51
- 429: 'Too Many Requests',
52
- 431: 'Request Header Fields Too Large',
53
- 451: 'Unavailable For Legal Reasons',
54
- 500: 'Internal Server Error',
55
- 501: 'Not Implemented',
56
- 502: 'Bad Gateway',
57
- 503: 'Service Unavailable',
58
- 504: 'Gateway Timeout',
59
- 505: 'HTTP Version Not Supported',
60
- 506: 'Variant Also Negotiates',
61
- 507: 'Insufficient Storage',
62
- 508: 'Loop Detected',
63
- 509: 'Bandwidth Limit Exceeded',
64
- 510: 'Not Extended',
65
- 511: 'Network Authentication Required'
66
- }