querystring.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* eslint-disable no-underscore-dangle */
  2. import QuerystringParser from '../parsers/Querystring.js';
  3. export const querystringType = 'urlencoded';
  4. // the `options` is also available through the `this.options` / `formidable.options`
  5. export default function plugin(formidable, options) {
  6. // the `this` context is always formidable, as the first argument of a plugin
  7. // but this allows us to customize/test each plugin
  8. /* istanbul ignore next */
  9. const self = this || formidable;
  10. if (/urlencoded/i.test(self.headers['content-type'])) {
  11. init.call(self, self, options);
  12. }
  13. return self;
  14. };
  15. // Note that it's a good practice (but it's up to you) to use the `this.options` instead
  16. // of the passed `options` (second) param, because when you decide
  17. // to test the plugin you can pass custom `this` context to it (and so `this.options`)
  18. function init(_self, _opts) {
  19. this.type = querystringType;
  20. const parser = new QuerystringParser(this.options);
  21. parser.on('data', ({ key, value }) => {
  22. this.emit('field', key, value);
  23. });
  24. parser.once('end', () => {
  25. this.ended = true;
  26. this._maybeEnd();
  27. });
  28. this._parser = parser;
  29. return this;
  30. }