-
-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathjson.js
More file actions
39 lines (29 loc) · 1.11 KB
/
json.js
File metadata and controls
39 lines (29 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* eslint-disable no-underscore-dangle */
import JSONParser from "../parsers/JSON.js";
export const jsonType = "json";
// the `options` is also available through the `this.options` / `formidable.options`
export default function plugin(formidable, options) {
// the `this` context is always formidable, as the first argument of a plugin
// but this allows us to customize/test each plugin
/* istanbul ignore next */
const self = this || formidable;
if (/^[^;]*json/i.test(self.headers["content-type"])) {
init.call(self, self, options);
}
return self;
}
// Note that it's a good practice (but it's up to you) to use the `this.options` instead
// of the passed `options` (second) param, because when you decide
// to test the plugin you can pass custom `this` context to it (and so `this.options`)
function init(_self, _opts) {
this.type = jsonType;
const parser = new JSONParser(this.options);
parser.on("data", (fields) => {
this.fields = Object.assign(Object.create(null), fields);
});
parser.once("end", () => {
this.ended = true;
this._maybeEnd();
});
this._parser = parser;
}