UNPKG

17.9 kBJavaScriptView Raw
1(function webpackUniversalModuleDefinition(root, factory) {
2 if(typeof exports === 'object' && typeof module === 'object')
3 module.exports = factory();
4 else if(typeof define === 'function' && define.amd)
5 define([], factory);
6 else {
7 var a = factory();
8 for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
9 }
10})(global, function() {
11return /******/ (function(modules) { // webpackBootstrap
12/******/ // The module cache
13/******/ var installedModules = {};
14/******/
15/******/ // The require function
16/******/ function __webpack_require__(moduleId) {
17/******/
18/******/ // Check if module is in cache
19/******/ if(installedModules[moduleId]) {
20/******/ return installedModules[moduleId].exports;
21/******/ }
22/******/ // Create a new module (and put it into the cache)
23/******/ var module = installedModules[moduleId] = {
24/******/ i: moduleId,
25/******/ l: false,
26/******/ exports: {}
27/******/ };
28/******/
29/******/ // Execute the module function
30/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31/******/
32/******/ // Flag the module as loaded
33/******/ module.l = true;
34/******/
35/******/ // Return the exports of the module
36/******/ return module.exports;
37/******/ }
38/******/
39/******/
40/******/ // expose the modules object (__webpack_modules__)
41/******/ __webpack_require__.m = modules;
42/******/
43/******/ // expose the module cache
44/******/ __webpack_require__.c = installedModules;
45/******/
46/******/ // define getter function for harmony exports
47/******/ __webpack_require__.d = function(exports, name, getter) {
48/******/ if(!__webpack_require__.o(exports, name)) {
49/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
50/******/ }
51/******/ };
52/******/
53/******/ // define __esModule on exports
54/******/ __webpack_require__.r = function(exports) {
55/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
56/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
57/******/ }
58/******/ Object.defineProperty(exports, '__esModule', { value: true });
59/******/ };
60/******/
61/******/ // create a fake namespace object
62/******/ // mode & 1: value is a module id, require it
63/******/ // mode & 2: merge all properties of value into the ns
64/******/ // mode & 4: return value when already ns object
65/******/ // mode & 8|1: behave like require
66/******/ __webpack_require__.t = function(value, mode) {
67/******/ if(mode & 1) value = __webpack_require__(value);
68/******/ if(mode & 8) return value;
69/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
70/******/ var ns = Object.create(null);
71/******/ __webpack_require__.r(ns);
72/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
73/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
74/******/ return ns;
75/******/ };
76/******/
77/******/ // getDefaultExport function for compatibility with non-harmony modules
78/******/ __webpack_require__.n = function(module) {
79/******/ var getter = module && module.__esModule ?
80/******/ function getDefault() { return module['default']; } :
81/******/ function getModuleExports() { return module; };
82/******/ __webpack_require__.d(getter, 'a', getter);
83/******/ return getter;
84/******/ };
85/******/
86/******/ // Object.prototype.hasOwnProperty.call
87/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
88/******/
89/******/ // __webpack_public_path__
90/******/ __webpack_require__.p = "";
91/******/
92/******/
93/******/ // Load entry module and return exports
94/******/ return __webpack_require__(__webpack_require__.s = "./src/Plugins/BodyParser/main.ts");
95/******/ })
96/************************************************************************/
97/******/ ({
98
99/***/ "./node_modules/content-type/index.js":
100/*!********************************************!*\
101 !*** ./node_modules/content-type/index.js ***!
102 \********************************************/
103/*! no static exports found */
104/***/ (function(module, exports, __webpack_require__) {
105
106"use strict";
107/*!
108 * content-type
109 * Copyright(c) 2015 Douglas Christopher Wilson
110 * MIT Licensed
111 */
112
113
114
115/**
116 * RegExp to match *( ";" parameter ) in RFC 7231 sec 3.1.1.1
117 *
118 * parameter = token "=" ( token / quoted-string )
119 * token = 1*tchar
120 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
121 * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
122 * / DIGIT / ALPHA
123 * ; any VCHAR, except delimiters
124 * quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
125 * qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
126 * obs-text = %x80-FF
127 * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
128 */
129var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g
130var TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/
131var TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/
132
133/**
134 * RegExp to match quoted-pair in RFC 7230 sec 3.2.6
135 *
136 * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
137 * obs-text = %x80-FF
138 */
139var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g
140
141/**
142 * RegExp to match chars that must be quoted-pair in RFC 7230 sec 3.2.6
143 */
144var QUOTE_REGEXP = /([\\"])/g
145
146/**
147 * RegExp to match type in RFC 7231 sec 3.1.1.1
148 *
149 * media-type = type "/" subtype
150 * type = token
151 * subtype = token
152 */
153var TYPE_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/
154
155/**
156 * Module exports.
157 * @public
158 */
159
160exports.format = format
161exports.parse = parse
162
163/**
164 * Format object to media type.
165 *
166 * @param {object} obj
167 * @return {string}
168 * @public
169 */
170
171function format (obj) {
172 if (!obj || typeof obj !== 'object') {
173 throw new TypeError('argument obj is required')
174 }
175
176 var parameters = obj.parameters
177 var type = obj.type
178
179 if (!type || !TYPE_REGEXP.test(type)) {
180 throw new TypeError('invalid type')
181 }
182
183 var string = type
184
185 // append parameters
186 if (parameters && typeof parameters === 'object') {
187 var param
188 var params = Object.keys(parameters).sort()
189
190 for (var i = 0; i < params.length; i++) {
191 param = params[i]
192
193 if (!TOKEN_REGEXP.test(param)) {
194 throw new TypeError('invalid parameter name')
195 }
196
197 string += '; ' + param + '=' + qstring(parameters[param])
198 }
199 }
200
201 return string
202}
203
204/**
205 * Parse media type to object.
206 *
207 * @param {string|object} string
208 * @return {Object}
209 * @public
210 */
211
212function parse (string) {
213 if (!string) {
214 throw new TypeError('argument string is required')
215 }
216
217 // support req/res-like objects as argument
218 var header = typeof string === 'object'
219 ? getcontenttype(string)
220 : string
221
222 if (typeof header !== 'string') {
223 throw new TypeError('argument string is required to be a string')
224 }
225
226 var index = header.indexOf(';')
227 var type = index !== -1
228 ? header.substr(0, index).trim()
229 : header.trim()
230
231 if (!TYPE_REGEXP.test(type)) {
232 throw new TypeError('invalid media type')
233 }
234
235 var obj = new ContentType(type.toLowerCase())
236
237 // parse parameters
238 if (index !== -1) {
239 var key
240 var match
241 var value
242
243 PARAM_REGEXP.lastIndex = index
244
245 while ((match = PARAM_REGEXP.exec(header))) {
246 if (match.index !== index) {
247 throw new TypeError('invalid parameter format')
248 }
249
250 index += match[0].length
251 key = match[1].toLowerCase()
252 value = match[2]
253
254 if (value[0] === '"') {
255 // remove quotes and escapes
256 value = value
257 .substr(1, value.length - 2)
258 .replace(QESC_REGEXP, '$1')
259 }
260
261 obj.parameters[key] = value
262 }
263
264 if (index !== header.length) {
265 throw new TypeError('invalid parameter format')
266 }
267 }
268
269 return obj
270}
271
272/**
273 * Get content-type from req/res objects.
274 *
275 * @param {object}
276 * @return {Object}
277 * @private
278 */
279
280function getcontenttype (obj) {
281 var header
282
283 if (typeof obj.getHeader === 'function') {
284 // res-like
285 header = obj.getHeader('content-type')
286 } else if (typeof obj.headers === 'object') {
287 // req-like
288 header = obj.headers && obj.headers['content-type']
289 }
290
291 if (typeof header !== 'string') {
292 throw new TypeError('content-type header is missing from object')
293 }
294
295 return header
296}
297
298/**
299 * Quote a string if necessary.
300 *
301 * @param {string} val
302 * @return {string}
303 * @private
304 */
305
306function qstring (val) {
307 var str = String(val)
308
309 // no need to quote tokens
310 if (TOKEN_REGEXP.test(str)) {
311 return str
312 }
313
314 if (str.length > 0 && !TEXT_REGEXP.test(str)) {
315 throw new TypeError('invalid parameter value')
316 }
317
318 return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"'
319}
320
321/**
322 * Class to represent a content type.
323 * @private
324 */
325function ContentType (type) {
326 this.parameters = Object.create(null)
327 this.type = type
328}
329
330
331/***/ }),
332
333/***/ "./src/Plugins/BodyParser/Constants/ContentType.ts":
334/*!*********************************************************!*\
335 !*** ./src/Plugins/BodyParser/Constants/ContentType.ts ***!
336 \*********************************************************/
337/*! no static exports found */
338/***/ (function(module, exports, __webpack_require__) {
339
340"use strict";
341
342Object.defineProperty(exports, "__esModule", { value: true });
343var ContentType;
344(function (ContentType) {
345 ContentType["APPLICATION_JSON"] = "application/json";
346})(ContentType || (ContentType = {}));
347exports.default = ContentType;
348
349
350/***/ }),
351
352/***/ "./src/Plugins/BodyParser/Constants/ErrorTypes.ts":
353/*!********************************************************!*\
354 !*** ./src/Plugins/BodyParser/Constants/ErrorTypes.ts ***!
355 \********************************************************/
356/*! no static exports found */
357/***/ (function(module, exports, __webpack_require__) {
358
359"use strict";
360
361Object.defineProperty(exports, "__esModule", { value: true });
362var ErrorTypes;
363(function (ErrorTypes) {
364 ErrorTypes["BODY_PARSER_OPTION_TYPE_INVALID"] = "BODY_PARSER_OPTION_TYPE_INVALID";
365 ErrorTypes["BODY_PARSER_REQUEST_HEADERS_MISSING"] = "BODY_PARSER_REQUEST_HEADERS_MISSING";
366 ErrorTypes["BODY_PARSER_REQUEST_HEADERS_CONTENT_TYPE_KEY_INVALID"] = "BODY_PARSER_REQUEST_HEADERS_CONTENT_TYPE_KEY_INVALID";
367 ErrorTypes["BODY_PARSER_REQUEST_HEADERS_CONTENT_TYPE_VALUE_INVALID"] = "BODY_PARSER_REQUEST_HEADERS_CONTENT_TYPE_VALUE_INVALID";
368 ErrorTypes["BODY_PARSER_JSON_PARSE_FAILED"] = "BODY_PARSER_JSON_PARSE_FAILED";
369})(ErrorTypes || (ErrorTypes = {}));
370exports.default = ErrorTypes;
371
372
373/***/ }),
374
375/***/ "./src/Plugins/BodyParser/Constants/ParsingOption.ts":
376/*!***********************************************************!*\
377 !*** ./src/Plugins/BodyParser/Constants/ParsingOption.ts ***!
378 \***********************************************************/
379/*! no static exports found */
380/***/ (function(module, exports, __webpack_require__) {
381
382"use strict";
383
384Object.defineProperty(exports, "__esModule", { value: true });
385var ParsingOption;
386(function (ParsingOption) {
387 ParsingOption["JSON"] = "json";
388 ParsingOption["RAW"] = "raw";
389 ParsingOption["TEXT"] = "text";
390 ParsingOption["URL_ENCODED"] = "urlencoded";
391})(ParsingOption || (ParsingOption = {}));
392exports.default = ParsingOption;
393
394
395/***/ }),
396
397/***/ "./src/Plugins/BodyParser/Constants/RequestHeaders.ts":
398/*!************************************************************!*\
399 !*** ./src/Plugins/BodyParser/Constants/RequestHeaders.ts ***!
400 \************************************************************/
401/*! no static exports found */
402/***/ (function(module, exports, __webpack_require__) {
403
404"use strict";
405
406Object.defineProperty(exports, "__esModule", { value: true });
407var RequestHeaders;
408(function (RequestHeaders) {
409 RequestHeaders["CONTENT_TYPE"] = "content-type";
410})(RequestHeaders || (RequestHeaders = {}));
411exports.default = RequestHeaders;
412
413
414/***/ }),
415
416/***/ "./src/Plugins/BodyParser/Utils/toLowerCaseShallow.ts":
417/*!************************************************************!*\
418 !*** ./src/Plugins/BodyParser/Utils/toLowerCaseShallow.ts ***!
419 \************************************************************/
420/*! no static exports found */
421/***/ (function(module, exports, __webpack_require__) {
422
423"use strict";
424
425Object.defineProperty(exports, "__esModule", { value: true });
426var toLowerCaseShallow = function (obj) {
427 return Object.keys(obj).reduce(function (output, currentKey) {
428 var key = currentKey.toLowerCase();
429 var value = obj[currentKey];
430 if (typeof value === 'string')
431 value = value.toLowerCase();
432 output[key] = value;
433 return output;
434 }, {});
435};
436exports.default = toLowerCaseShallow;
437
438
439/***/ }),
440
441/***/ "./src/Plugins/BodyParser/bodyParser.ts":
442/*!**********************************************!*\
443 !*** ./src/Plugins/BodyParser/bodyParser.ts ***!
444 \**********************************************/
445/*! no static exports found */
446/***/ (function(module, exports, __webpack_require__) {
447
448"use strict";
449
450Object.defineProperty(exports, "__esModule", { value: true });
451var _a;
452var ParsingOption_1 = __webpack_require__(/*! ./Constants/ParsingOption */ "./src/Plugins/BodyParser/Constants/ParsingOption.ts");
453var json_1 = __webpack_require__(/*! ./json */ "./src/Plugins/BodyParser/json.ts");
454var bodyParser = (_a = {},
455 _a[ParsingOption_1.default.JSON] = json_1.default,
456 _a);
457exports.default = bodyParser;
458
459
460/***/ }),
461
462/***/ "./src/Plugins/BodyParser/json.ts":
463/*!****************************************!*\
464 !*** ./src/Plugins/BodyParser/json.ts ***!
465 \****************************************/
466/*! no static exports found */
467/***/ (function(module, exports, __webpack_require__) {
468
469"use strict";
470
471Object.defineProperty(exports, "__esModule", { value: true });
472var contentType = __webpack_require__(/*! content-type */ "./node_modules/content-type/index.js");
473var createError_1 = __webpack_require__(/*! ../../Utils/createError */ "./src/Utils/createError.ts");
474var ContentType_1 = __webpack_require__(/*! ./Constants/ContentType */ "./src/Plugins/BodyParser/Constants/ContentType.ts");
475var ErrorTypes_1 = __webpack_require__(/*! ./Constants/ErrorTypes */ "./src/Plugins/BodyParser/Constants/ErrorTypes.ts");
476var RequestHeaders_1 = __webpack_require__(/*! ./Constants/RequestHeaders */ "./src/Plugins/BodyParser/Constants/RequestHeaders.ts");
477var toLowerCaseShallow_1 = __webpack_require__(/*! ./Utils/toLowerCaseShallow */ "./src/Plugins/BodyParser/Utils/toLowerCaseShallow.ts");
478var jsonParser = function (handler) {
479 var request = handler.event;
480 if (!request.headers) {
481 throw createError_1.default({
482 type: ErrorTypes_1.default.BODY_PARSER_REQUEST_HEADERS_MISSING
483 });
484 }
485 var content;
486 request.headers = toLowerCaseShallow_1.default(request.headers);
487 try {
488 content = contentType.parse(handler.event);
489 }
490 catch (e) {
491 throw createError_1.default({
492 type: ErrorTypes_1.default.BODY_PARSER_REQUEST_HEADERS_CONTENT_TYPE_KEY_INVALID,
493 details: RequestHeaders_1.default.CONTENT_TYPE,
494 source: e
495 });
496 }
497 if (content.type !== ContentType_1.default.APPLICATION_JSON) {
498 throw createError_1.default({
499 type: ErrorTypes_1.default.BODY_PARSER_REQUEST_HEADERS_CONTENT_TYPE_VALUE_INVALID,
500 details: content.type
501 });
502 }
503 try {
504 request.body = JSON.parse(request.body);
505 }
506 catch (e) {
507 throw createError_1.default({
508 type: ErrorTypes_1.default.BODY_PARSER_JSON_PARSE_FAILED,
509 details: e
510 });
511 }
512};
513exports.default = jsonParser;
514
515
516/***/ }),
517
518/***/ "./src/Plugins/BodyParser/main.ts":
519/*!****************************************!*\
520 !*** ./src/Plugins/BodyParser/main.ts ***!
521 \****************************************/
522/*! no static exports found */
523/***/ (function(module, exports, __webpack_require__) {
524
525"use strict";
526
527Object.defineProperty(exports, "__esModule", { value: true });
528var createError_1 = __webpack_require__(/*! ../../Utils/createError */ "./src/Utils/createError.ts");
529var bodyParser_1 = __webpack_require__(/*! ./bodyParser */ "./src/Plugins/BodyParser/bodyParser.ts");
530var ErrorTypes_1 = __webpack_require__(/*! ./Constants/ErrorTypes */ "./src/Plugins/BodyParser/Constants/ErrorTypes.ts");
531var ParsingOption_1 = __webpack_require__(/*! ./Constants/ParsingOption */ "./src/Plugins/BodyParser/Constants/ParsingOption.ts");
532var validate = function (config) {
533 if (!Object['values'](ParsingOption_1.default).includes(config.type)) {
534 return createError_1.default({
535 type: ErrorTypes_1.default.BODY_PARSER_OPTION_TYPE_INVALID,
536 details: 'one of json|raw|text|urlencoded'
537 });
538 }
539};
540var bodyParserPlugin = function (config) {
541 var pluginManifest = {
542 plugin: {
543 onRequest: function (handler) {
544 var error = validate(config);
545 if (error) {
546 throw error;
547 }
548 var parser = bodyParser_1.default[config.type];
549 parser(handler);
550 }
551 }
552 };
553 return pluginManifest;
554};
555exports.default = bodyParserPlugin;
556
557
558/***/ }),
559
560/***/ "./src/Utils/createError.ts":
561/*!**********************************!*\
562 !*** ./src/Utils/createError.ts ***!
563 \**********************************/
564/*! no static exports found */
565/***/ (function(module, exports, __webpack_require__) {
566
567"use strict";
568
569Object.defineProperty(exports, "__esModule", { value: true });
570var createError = function (errorEvent) {
571 return errorEvent;
572};
573exports.default = createError;
574
575
576/***/ })
577
578/******/ });
579});
580//# sourceMappingURL=plugin-body-parser.js.map
\No newline at end of file