index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. module.exports =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/ // The require function
  6. /******/ function __webpack_require__(moduleId) {
  7. /******/ // Check if module is in cache
  8. /******/ if(installedModules[moduleId])
  9. /******/ return installedModules[moduleId].exports;
  10. /******/ // Create a new module (and put it into the cache)
  11. /******/ var module = installedModules[moduleId] = {
  12. /******/ exports: {},
  13. /******/ id: moduleId,
  14. /******/ loaded: false
  15. /******/ };
  16. /******/ // Execute the module function
  17. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  18. /******/ // Flag the module as loaded
  19. /******/ module.loaded = true;
  20. /******/ // Return the exports of the module
  21. /******/ return module.exports;
  22. /******/ }
  23. /******/ // expose the modules object (__webpack_modules__)
  24. /******/ __webpack_require__.m = modules;
  25. /******/ // expose the module cache
  26. /******/ __webpack_require__.c = installedModules;
  27. /******/ // __webpack_public_path__
  28. /******/ __webpack_require__.p = "";
  29. /******/ // Load entry module and return exports
  30. /******/ return __webpack_require__(0);
  31. /******/ })
  32. /************************************************************************/
  33. /******/ ([
  34. /* 0 */
  35. /***/ function(module, exports, __webpack_require__) {
  36. module.exports = __webpack_require__(1);
  37. /***/ },
  38. /* 1 */
  39. /***/ function(module, exports, __webpack_require__) {
  40. 'use strict';
  41. Object.defineProperty(exports, "__esModule", {
  42. value: true
  43. });
  44. var _xmlParser = __webpack_require__(2);
  45. var _xmlParser2 = _interopRequireDefault(_xmlParser);
  46. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  47. var onerror = function onerror() {};
  48. exports.default = function () {
  49. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { onerror: onerror };
  50. return function* (next) {
  51. /**
  52. * only parse and set this.request.body when
  53. * 1. type is xml (text/xml and application/xml)
  54. * 2. method is post/put/patch
  55. * 3. this.request.body is undefined
  56. */
  57. if (this.request.body === undefined && this.is('text/xml', 'xml') && /^(POST|PUT|PATCH)$/i.test(this.method)) {
  58. if (!options.encoding && this.request.charset) {
  59. options.encoding = this.request.charset;
  60. }
  61. try {
  62. this.request.body = yield (0, _xmlParser2.default)(this.req, options);
  63. } catch (err) {
  64. // if want to throw error, set onerror to null
  65. if (options.onerror) {
  66. options.onerror(err, this);
  67. } else {
  68. throw err;
  69. }
  70. }
  71. }
  72. yield next;
  73. };
  74. };
  75. /***/ },
  76. /* 2 */
  77. /***/ function(module, exports, __webpack_require__) {
  78. 'use strict';
  79. Object.defineProperty(exports, "__esModule", {
  80. value: true
  81. });
  82. var _xml2js = __webpack_require__(3);
  83. var _rawBody = __webpack_require__(4);
  84. var _rawBody2 = _interopRequireDefault(_rawBody);
  85. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  86. var convertXml2Json = function convertXml2Json() /* str, options */{
  87. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  88. args[_key] = arguments[_key];
  89. }
  90. return new Promise(function (resolve, reject) {
  91. var cb = function cb(err, result) {
  92. err ? reject(err) : resolve(result);
  93. };
  94. _xml2js.parseString.apply(undefined, args.concat([cb]));
  95. });
  96. };
  97. var parse = function parse(request) {
  98. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  99. _ref$limit = _ref.limit,
  100. limit = _ref$limit === undefined ? '1mb' : _ref$limit,
  101. _ref$encoding = _ref.encoding,
  102. encoding = _ref$encoding === undefined ? 'utf8' : _ref$encoding,
  103. length = _ref.length,
  104. xmlOptions = _ref.xmlOptions;
  105. var len = request.headers['content-length'];
  106. // set options
  107. if (len) {
  108. length = len = ~~len;
  109. }
  110. return (0, _rawBody2.default)(request, { limit: limit, encoding: encoding, length: length }).then(function (str) {
  111. return convertXml2Json(str, xmlOptions || {}).catch(function (err) {
  112. err = typeof err === 'string' ? new Error(err) : err;
  113. err.status = 400;
  114. err.body = str;
  115. throw err;
  116. });
  117. }).catch(function (err) {
  118. throw err;
  119. });
  120. };
  121. exports.default = parse;
  122. /***/ },
  123. /* 3 */
  124. /***/ function(module, exports) {
  125. module.exports = require("xml2js");
  126. /***/ },
  127. /* 4 */
  128. /***/ function(module, exports) {
  129. module.exports = require("raw-body");
  130. /***/ }
  131. /******/ ]);