Busboy中的.ON方法在哪里?
我一直在查看Make-Middleware文件中Multer的源代码,并且遇到了附加到新的Busboy对象的.ON
方法,所以我跳到了Busboy源代码,以查看。剂量完全做。
我尝试console.log
busboy新对象,以查看对象中的所有方法,但.on
方法确实显示了。
make-middleware.js
var Busboy = require('busboy');
var busboy;
try{
busboy = new Busboy({
headers: req.headers,
limits: limits,
preservePath: preservePath
})
}catch(err){
return next(err);
}
busboy.on('field', function (fieldname, value, fieldnameTruncated, valueTruncated) {
// code
});
busboy-> lib-> main.js
:
var fs = require('fs'),
WritableStream = require('stream').Writable ||
require('readable-stream').Writable,
inherits = require('util').inherits;
var parseParams = require('./utils').parseParams;
function Busboy(opts) {
if (!(this instanceof Busboy))
return new Busboy(opts);
if (opts.highWaterMark !== undefined)
WritableStream.call(this, {
highWaterMark: opts.highWaterMark
});
else
WritableStream.call(this);
this._done = false;
this._parser = undefined;
this._finished = false;
this.opts = opts;
if (opts.headers && typeof opts.headers['content-type'] === 'string')
this.parseHeaders(opts.headers);
else
throw new Error('Missing Content-Type');
}
inherits(Busboy, WritableStream);
Busboy.prototype.emit = function (ev) {
if (ev === 'finish') {
if (!this._done) {
this._parser && this._parser.end();
return;
} else if (this._finished) {
return;
}
this._finished = true;
}
WritableStream.prototype.emit.apply(this, arguments);
};
Busboy.prototype.parseHeaders = function (headers) {
this._parser = undefined;
if (headers['content-type']) {
var parsed = parseParams(headers['content-type']),
matched, type;
for (var i = 0; i < TYPES.length; ++i) {
type = TYPES[i];
if (typeof type.detect === 'function')
matched = type.detect(parsed);
else
matched = type.detect.test(parsed[0]);
if (matched)
break;
}
if (matched) {
var cfg = {
limits: this.opts.limits,
headers: headers,
parsedConType: parsed,
highWaterMark: undefined,
fileHwm: undefined,
defCharset: undefined,
preservePath: false
};
if (this.opts.highWaterMark)
cfg.highWaterMark = this.opts.highWaterMark;
if (this.opts.fileHwm)
cfg.fileHwm = this.opts.fileHwm;
cfg.defCharset = this.opts.defCharset;
cfg.preservePath = this.opts.preservePath;
this._parser = type(this, cfg);
return;
}
}
throw new Error('Unsupported content type: ' + headers['content-type']);
};
Busboy.prototype._write = function (chunk, encoding, cb) {
if (!this._parser)
return cb(new Error('Not ready to parse. Missing Content-Type?'));
this._parser.write(chunk, cb);
};
var TYPES = [
require('./types/multipart'),
require('./types/urlencoded'),
];
module.exports = Busboy;
console.log(busbboy)
:
<ref *1> Busboy {
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: null,
closed: false
},
_events: [Object: null prototype] { field: [Function (anonymous)] },
_eventsCount: 1,
_maxListeners: undefined,
_done: false,
_parser: Multipart {
_needDrain: false,
_pause: false,
_cb: undefined,
_nparts: 0,
_boy: [Circular *1],
parser: Dicer {
_writableState: [WritableState],
_events: [Object: null prototype],
_eventsCount: 4,
_maxListeners: undefined,
_bparser: [SBMH],
_headerFirst: undefined,
_dashes: 0,
_parts: 0,
_finished: false,
_realFinish: false,
_isPreamble: true,
_justMatched: false,
_firstWrite: true,
_inHeader: true,
_part: undefined,
_cb: undefined,
_ignoreData: false,
_partOpts: {},
_pause: false,
_hparser: [HeaderParser],
[Symbol(kCapture)]: false
}
},
_finished: false,
opts: {
headers: {
host: 'localhost:8080',
connection: 'keep-alive',
'content-length': '257644',
'cache-control': 'max-age=0',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
'sec-ch-ua-mobile': '?0',
'upgrade-insecure-requests': '1',
origin: 'http://localhost:8080',
'content-type': 'multipart/form-data;',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
referer: 'http://localhost:8080/',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'sec-gpc': '1'
},
limits: { fileSize: 1000000 },
},
[Symbol(kCapture)]: false
}
i was looking into the source code of multer in the make-middleware file and I came across the .on
method attached to a new busboy object, so i jumped to busboy source code to see what the .on
method do and i cannot find it defined in the main.js
file, I couldn't figure out what is the .on
method or what dose it do exactly.
I tried to console.log
busboy new object to see all the methods in the object but the .on
method did show.
make-middleware.js
var Busboy = require('busboy');
var busboy;
try{
busboy = new Busboy({
headers: req.headers,
limits: limits,
preservePath: preservePath
})
}catch(err){
return next(err);
}
busboy.on('field', function (fieldname, value, fieldnameTruncated, valueTruncated) {
// code
});
busboy->lib->main.js
:
var fs = require('fs'),
WritableStream = require('stream').Writable ||
require('readable-stream').Writable,
inherits = require('util').inherits;
var parseParams = require('./utils').parseParams;
function Busboy(opts) {
if (!(this instanceof Busboy))
return new Busboy(opts);
if (opts.highWaterMark !== undefined)
WritableStream.call(this, {
highWaterMark: opts.highWaterMark
});
else
WritableStream.call(this);
this._done = false;
this._parser = undefined;
this._finished = false;
this.opts = opts;
if (opts.headers && typeof opts.headers['content-type'] === 'string')
this.parseHeaders(opts.headers);
else
throw new Error('Missing Content-Type');
}
inherits(Busboy, WritableStream);
Busboy.prototype.emit = function (ev) {
if (ev === 'finish') {
if (!this._done) {
this._parser && this._parser.end();
return;
} else if (this._finished) {
return;
}
this._finished = true;
}
WritableStream.prototype.emit.apply(this, arguments);
};
Busboy.prototype.parseHeaders = function (headers) {
this._parser = undefined;
if (headers['content-type']) {
var parsed = parseParams(headers['content-type']),
matched, type;
for (var i = 0; i < TYPES.length; ++i) {
type = TYPES[i];
if (typeof type.detect === 'function')
matched = type.detect(parsed);
else
matched = type.detect.test(parsed[0]);
if (matched)
break;
}
if (matched) {
var cfg = {
limits: this.opts.limits,
headers: headers,
parsedConType: parsed,
highWaterMark: undefined,
fileHwm: undefined,
defCharset: undefined,
preservePath: false
};
if (this.opts.highWaterMark)
cfg.highWaterMark = this.opts.highWaterMark;
if (this.opts.fileHwm)
cfg.fileHwm = this.opts.fileHwm;
cfg.defCharset = this.opts.defCharset;
cfg.preservePath = this.opts.preservePath;
this._parser = type(this, cfg);
return;
}
}
throw new Error('Unsupported content type: ' + headers['content-type']);
};
Busboy.prototype._write = function (chunk, encoding, cb) {
if (!this._parser)
return cb(new Error('Not ready to parse. Missing Content-Type?'));
this._parser.write(chunk, cb);
};
var TYPES = [
require('./types/multipart'),
require('./types/urlencoded'),
];
module.exports = Busboy;
console.log(busboy)
:
<ref *1> Busboy {
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: null,
closed: false
},
_events: [Object: null prototype] { field: [Function (anonymous)] },
_eventsCount: 1,
_maxListeners: undefined,
_done: false,
_parser: Multipart {
_needDrain: false,
_pause: false,
_cb: undefined,
_nparts: 0,
_boy: [Circular *1],
parser: Dicer {
_writableState: [WritableState],
_events: [Object: null prototype],
_eventsCount: 4,
_maxListeners: undefined,
_bparser: [SBMH],
_headerFirst: undefined,
_dashes: 0,
_parts: 0,
_finished: false,
_realFinish: false,
_isPreamble: true,
_justMatched: false,
_firstWrite: true,
_inHeader: true,
_part: undefined,
_cb: undefined,
_ignoreData: false,
_partOpts: {},
_pause: false,
_hparser: [HeaderParser],
[Symbol(kCapture)]: false
}
},
_finished: false,
opts: {
headers: {
host: 'localhost:8080',
connection: 'keep-alive',
'content-length': '257644',
'cache-control': 'max-age=0',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
'sec-ch-ua-mobile': '?0',
'upgrade-insecure-requests': '1',
origin: 'http://localhost:8080',
'content-type': 'multipart/form-data;',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
referer: 'http://localhost:8080/',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'sec-gpc': '1'
},
limits: { fileSize: 1000000 },
},
[Symbol(kCapture)]: false
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论