MCPcopy
hub / github.com/node-formidable/formidable / init

Function init

src/plugins/octetstream.js:23–84  ·  view source on GitHub ↗
(_self, _opts)

Source from the content-addressed store, hash-verified

21// of the passed `options` (second) param, because when you decide
22// to test the plugin you can pass custom `this` context to it (and so `this.options`)
23async function init(_self, _opts) {
24 this.type = octetStreamType;
25 const originalFilename = this.headers['x-file-name'];
26 const mimetype = this.headers['content-type'];
27
28 const thisPart = {
29 originalFilename,
30 mimetype,
31 };
32 const newFilename = this._getNewName(thisPart);
33 const filepath = this._joinDirectoryName(newFilename);
34 const file = await this._newFile({
35 newFilename,
36 filepath,
37 originalFilename,
38 mimetype,
39 });
40
41 this.emit('fileBegin', originalFilename, file);
42 file.open();
43 this.openedFiles.push(file);
44 this._flushing += 1;
45
46 this._parser = new OctetStreamParser(this.options);
47
48 // Keep track of writes that haven't finished so we don't emit the file before it's done being written
49 let outstandingWrites = 0;
50
51 this._parser.on('data', (buffer) => {
52 this.pause();
53 outstandingWrites += 1;
54
55 file.write(buffer, () => {
56 outstandingWrites -= 1;
57 this.resume();
58
59 if (this.ended) {
60 this._parser.emit('doneWritingFile');
61 }
62 });
63 });
64
65 this._parser.on('end', () => {
66 this._flushing -= 1;
67 this.ended = true;
68
69 const done = () => {
70 file.end(() => {
71 this.emit('file', 'file', file);
72 this._maybeEnd();
73 });
74 };
75
76 if (outstandingWrites === 0) {
77 done();
78 } else {
79 this._parser.once('doneWritingFile', done);
80 }

Callers

nothing calls this directly

Calls 7

doneFunction · 0.85
_joinDirectoryNameMethod · 0.80
_newFileMethod · 0.80
pauseMethod · 0.80
resumeMethod · 0.80
openMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…