format: prettify entire project
This commit is contained in:
197
node_modules/undici/lib/api/api-stream.js
generated
vendored
197
node_modules/undici/lib/api/api-stream.js
generated
vendored
@ -1,107 +1,119 @@
|
||||
'use strict'
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert')
|
||||
const { finished } = require('node:stream')
|
||||
const { AsyncResource } = require('node:async_hooks')
|
||||
const { InvalidArgumentError, InvalidReturnValueError } = require('../core/errors')
|
||||
const util = require('../core/util')
|
||||
const { addSignal, removeSignal } = require('./abort-signal')
|
||||
const assert = require('node:assert');
|
||||
const { finished } = require('node:stream');
|
||||
const { AsyncResource } = require('node:async_hooks');
|
||||
const {
|
||||
InvalidArgumentError,
|
||||
InvalidReturnValueError,
|
||||
} = require('../core/errors');
|
||||
const util = require('../core/util');
|
||||
const { addSignal, removeSignal } = require('./abort-signal');
|
||||
|
||||
function noop () {}
|
||||
function noop() {}
|
||||
|
||||
class StreamHandler extends AsyncResource {
|
||||
constructor (opts, factory, callback) {
|
||||
constructor(opts, factory, callback) {
|
||||
if (!opts || typeof opts !== 'object') {
|
||||
throw new InvalidArgumentError('invalid opts')
|
||||
throw new InvalidArgumentError('invalid opts');
|
||||
}
|
||||
|
||||
const { signal, method, opaque, body, onInfo, responseHeaders } = opts
|
||||
const { signal, method, opaque, body, onInfo, responseHeaders } = opts;
|
||||
|
||||
try {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new InvalidArgumentError('invalid callback')
|
||||
throw new InvalidArgumentError('invalid callback');
|
||||
}
|
||||
|
||||
if (typeof factory !== 'function') {
|
||||
throw new InvalidArgumentError('invalid factory')
|
||||
throw new InvalidArgumentError('invalid factory');
|
||||
}
|
||||
|
||||
if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
|
||||
throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
|
||||
if (
|
||||
signal &&
|
||||
typeof signal.on !== 'function' &&
|
||||
typeof signal.addEventListener !== 'function'
|
||||
) {
|
||||
throw new InvalidArgumentError(
|
||||
'signal must be an EventEmitter or EventTarget'
|
||||
);
|
||||
}
|
||||
|
||||
if (method === 'CONNECT') {
|
||||
throw new InvalidArgumentError('invalid method')
|
||||
throw new InvalidArgumentError('invalid method');
|
||||
}
|
||||
|
||||
if (onInfo && typeof onInfo !== 'function') {
|
||||
throw new InvalidArgumentError('invalid onInfo callback')
|
||||
throw new InvalidArgumentError('invalid onInfo callback');
|
||||
}
|
||||
|
||||
super('UNDICI_STREAM')
|
||||
super('UNDICI_STREAM');
|
||||
} catch (err) {
|
||||
if (util.isStream(body)) {
|
||||
util.destroy(body.on('error', noop), err)
|
||||
util.destroy(body.on('error', noop), err);
|
||||
}
|
||||
throw err
|
||||
throw err;
|
||||
}
|
||||
|
||||
this.responseHeaders = responseHeaders || null
|
||||
this.opaque = opaque || null
|
||||
this.factory = factory
|
||||
this.callback = callback
|
||||
this.res = null
|
||||
this.abort = null
|
||||
this.context = null
|
||||
this.trailers = null
|
||||
this.body = body
|
||||
this.onInfo = onInfo || null
|
||||
this.responseHeaders = responseHeaders || null;
|
||||
this.opaque = opaque || null;
|
||||
this.factory = factory;
|
||||
this.callback = callback;
|
||||
this.res = null;
|
||||
this.abort = null;
|
||||
this.context = null;
|
||||
this.trailers = null;
|
||||
this.body = body;
|
||||
this.onInfo = onInfo || null;
|
||||
|
||||
if (util.isStream(body)) {
|
||||
body.on('error', (err) => {
|
||||
this.onError(err)
|
||||
})
|
||||
this.onError(err);
|
||||
});
|
||||
}
|
||||
|
||||
addSignal(this, signal)
|
||||
addSignal(this, signal);
|
||||
}
|
||||
|
||||
onConnect (abort, context) {
|
||||
onConnect(abort, context) {
|
||||
if (this.reason) {
|
||||
abort(this.reason)
|
||||
return
|
||||
abort(this.reason);
|
||||
return;
|
||||
}
|
||||
|
||||
assert(this.callback)
|
||||
assert(this.callback);
|
||||
|
||||
this.abort = abort
|
||||
this.context = context
|
||||
this.abort = abort;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
onHeaders (statusCode, rawHeaders, resume, statusMessage) {
|
||||
const { factory, opaque, context, responseHeaders } = this
|
||||
onHeaders(statusCode, rawHeaders, resume, statusMessage) {
|
||||
const { factory, opaque, context, responseHeaders } = this;
|
||||
|
||||
const headers = responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
|
||||
const headers =
|
||||
responseHeaders === 'raw' ?
|
||||
util.parseRawHeaders(rawHeaders)
|
||||
: util.parseHeaders(rawHeaders);
|
||||
|
||||
if (statusCode < 200) {
|
||||
if (this.onInfo) {
|
||||
this.onInfo({ statusCode, headers })
|
||||
this.onInfo({ statusCode, headers });
|
||||
}
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
this.factory = null
|
||||
this.factory = null;
|
||||
|
||||
if (factory === null) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
const res = this.runInAsyncScope(factory, null, {
|
||||
statusCode,
|
||||
headers,
|
||||
opaque,
|
||||
context
|
||||
})
|
||||
context,
|
||||
});
|
||||
|
||||
if (
|
||||
!res ||
|
||||
@ -109,101 +121,102 @@ class StreamHandler extends AsyncResource {
|
||||
typeof res.end !== 'function' ||
|
||||
typeof res.on !== 'function'
|
||||
) {
|
||||
throw new InvalidReturnValueError('expected Writable')
|
||||
throw new InvalidReturnValueError('expected Writable');
|
||||
}
|
||||
|
||||
// TODO: Avoid finished. It registers an unnecessary amount of listeners.
|
||||
finished(res, { readable: false }, (err) => {
|
||||
const { callback, res, opaque, trailers, abort } = this
|
||||
const { callback, res, opaque, trailers, abort } = this;
|
||||
|
||||
this.res = null
|
||||
this.res = null;
|
||||
if (err || !res.readable) {
|
||||
util.destroy(res, err)
|
||||
util.destroy(res, err);
|
||||
}
|
||||
|
||||
this.callback = null
|
||||
this.runInAsyncScope(callback, null, err || null, { opaque, trailers })
|
||||
this.callback = null;
|
||||
this.runInAsyncScope(callback, null, err || null, { opaque, trailers });
|
||||
|
||||
if (err) {
|
||||
abort()
|
||||
abort();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
res.on('drain', resume)
|
||||
res.on('drain', resume);
|
||||
|
||||
this.res = res
|
||||
this.res = res;
|
||||
|
||||
const needDrain = res.writableNeedDrain !== undefined
|
||||
? res.writableNeedDrain
|
||||
: res._writableState?.needDrain
|
||||
const needDrain =
|
||||
res.writableNeedDrain !== undefined ?
|
||||
res.writableNeedDrain
|
||||
: res._writableState?.needDrain;
|
||||
|
||||
return needDrain !== true
|
||||
return needDrain !== true;
|
||||
}
|
||||
|
||||
onData (chunk) {
|
||||
const { res } = this
|
||||
onData(chunk) {
|
||||
const { res } = this;
|
||||
|
||||
return res ? res.write(chunk) : true
|
||||
return res ? res.write(chunk) : true;
|
||||
}
|
||||
|
||||
onComplete (trailers) {
|
||||
const { res } = this
|
||||
onComplete(trailers) {
|
||||
const { res } = this;
|
||||
|
||||
removeSignal(this)
|
||||
removeSignal(this);
|
||||
|
||||
if (!res) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
this.trailers = util.parseHeaders(trailers)
|
||||
this.trailers = util.parseHeaders(trailers);
|
||||
|
||||
res.end()
|
||||
res.end();
|
||||
}
|
||||
|
||||
onError (err) {
|
||||
const { res, callback, opaque, body } = this
|
||||
onError(err) {
|
||||
const { res, callback, opaque, body } = this;
|
||||
|
||||
removeSignal(this)
|
||||
removeSignal(this);
|
||||
|
||||
this.factory = null
|
||||
this.factory = null;
|
||||
|
||||
if (res) {
|
||||
this.res = null
|
||||
util.destroy(res, err)
|
||||
this.res = null;
|
||||
util.destroy(res, err);
|
||||
} else if (callback) {
|
||||
this.callback = null
|
||||
this.callback = null;
|
||||
queueMicrotask(() => {
|
||||
this.runInAsyncScope(callback, null, err, { opaque })
|
||||
})
|
||||
this.runInAsyncScope(callback, null, err, { opaque });
|
||||
});
|
||||
}
|
||||
|
||||
if (body) {
|
||||
this.body = null
|
||||
util.destroy(body, err)
|
||||
this.body = null;
|
||||
util.destroy(body, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function stream (opts, factory, callback) {
|
||||
function stream(opts, factory, callback) {
|
||||
if (callback === undefined) {
|
||||
return new Promise((resolve, reject) => {
|
||||
stream.call(this, opts, factory, (err, data) => {
|
||||
return err ? reject(err) : resolve(data)
|
||||
})
|
||||
})
|
||||
return err ? reject(err) : resolve(data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const handler = new StreamHandler(opts, factory, callback)
|
||||
const handler = new StreamHandler(opts, factory, callback);
|
||||
|
||||
this.dispatch(opts, handler)
|
||||
this.dispatch(opts, handler);
|
||||
} catch (err) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw err
|
||||
throw err;
|
||||
}
|
||||
const opaque = opts?.opaque
|
||||
queueMicrotask(() => callback(err, { opaque }))
|
||||
const opaque = opts?.opaque;
|
||||
queueMicrotask(() => callback(err, { opaque }));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = stream
|
||||
module.exports = stream;
|
||||
|
Reference in New Issue
Block a user