You've already forked codtracker-js
format: prettify entire project
This commit is contained in:
236
node_modules/undici/lib/api/api-pipeline.js
generated
vendored
236
node_modules/undici/lib/api/api-pipeline.js
generated
vendored
@ -1,252 +1,260 @@
|
||||
'use strict'
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
Readable,
|
||||
Duplex,
|
||||
PassThrough
|
||||
} = require('node:stream')
|
||||
const assert = require('node:assert')
|
||||
const { AsyncResource } = require('node:async_hooks')
|
||||
const { Readable, Duplex, PassThrough } = require('node:stream');
|
||||
const assert = require('node:assert');
|
||||
const { AsyncResource } = require('node:async_hooks');
|
||||
const {
|
||||
InvalidArgumentError,
|
||||
InvalidReturnValueError,
|
||||
RequestAbortedError
|
||||
} = require('../core/errors')
|
||||
const util = require('../core/util')
|
||||
const { addSignal, removeSignal } = require('./abort-signal')
|
||||
RequestAbortedError,
|
||||
} = require('../core/errors');
|
||||
const util = require('../core/util');
|
||||
const { addSignal, removeSignal } = require('./abort-signal');
|
||||
|
||||
function noop () {}
|
||||
function noop() {}
|
||||
|
||||
const kResume = Symbol('resume')
|
||||
const kResume = Symbol('resume');
|
||||
|
||||
class PipelineRequest extends Readable {
|
||||
constructor () {
|
||||
super({ autoDestroy: true })
|
||||
constructor() {
|
||||
super({ autoDestroy: true });
|
||||
|
||||
this[kResume] = null
|
||||
this[kResume] = null;
|
||||
}
|
||||
|
||||
_read () {
|
||||
const { [kResume]: resume } = this
|
||||
_read() {
|
||||
const { [kResume]: resume } = this;
|
||||
|
||||
if (resume) {
|
||||
this[kResume] = null
|
||||
resume()
|
||||
this[kResume] = null;
|
||||
resume();
|
||||
}
|
||||
}
|
||||
|
||||
_destroy (err, callback) {
|
||||
this._read()
|
||||
_destroy(err, callback) {
|
||||
this._read();
|
||||
|
||||
callback(err)
|
||||
callback(err);
|
||||
}
|
||||
}
|
||||
|
||||
class PipelineResponse extends Readable {
|
||||
constructor (resume) {
|
||||
super({ autoDestroy: true })
|
||||
this[kResume] = resume
|
||||
constructor(resume) {
|
||||
super({ autoDestroy: true });
|
||||
this[kResume] = resume;
|
||||
}
|
||||
|
||||
_read () {
|
||||
this[kResume]()
|
||||
_read() {
|
||||
this[kResume]();
|
||||
}
|
||||
|
||||
_destroy (err, callback) {
|
||||
_destroy(err, callback) {
|
||||
if (!err && !this._readableState.endEmitted) {
|
||||
err = new RequestAbortedError()
|
||||
err = new RequestAbortedError();
|
||||
}
|
||||
|
||||
callback(err)
|
||||
callback(err);
|
||||
}
|
||||
}
|
||||
|
||||
class PipelineHandler extends AsyncResource {
|
||||
constructor (opts, handler) {
|
||||
constructor(opts, handler) {
|
||||
if (!opts || typeof opts !== 'object') {
|
||||
throw new InvalidArgumentError('invalid opts')
|
||||
throw new InvalidArgumentError('invalid opts');
|
||||
}
|
||||
|
||||
if (typeof handler !== 'function') {
|
||||
throw new InvalidArgumentError('invalid handler')
|
||||
throw new InvalidArgumentError('invalid handler');
|
||||
}
|
||||
|
||||
const { signal, method, opaque, onInfo, responseHeaders } = opts
|
||||
const { signal, method, opaque, onInfo, responseHeaders } = opts;
|
||||
|
||||
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_PIPELINE')
|
||||
super('UNDICI_PIPELINE');
|
||||
|
||||
this.opaque = opaque || null
|
||||
this.responseHeaders = responseHeaders || null
|
||||
this.handler = handler
|
||||
this.abort = null
|
||||
this.context = null
|
||||
this.onInfo = onInfo || null
|
||||
this.opaque = opaque || null;
|
||||
this.responseHeaders = responseHeaders || null;
|
||||
this.handler = handler;
|
||||
this.abort = null;
|
||||
this.context = null;
|
||||
this.onInfo = onInfo || null;
|
||||
|
||||
this.req = new PipelineRequest().on('error', noop)
|
||||
this.req = new PipelineRequest().on('error', noop);
|
||||
|
||||
this.ret = new Duplex({
|
||||
readableObjectMode: opts.objectMode,
|
||||
autoDestroy: true,
|
||||
read: () => {
|
||||
const { body } = this
|
||||
const { body } = this;
|
||||
|
||||
if (body?.resume) {
|
||||
body.resume()
|
||||
body.resume();
|
||||
}
|
||||
},
|
||||
write: (chunk, encoding, callback) => {
|
||||
const { req } = this
|
||||
const { req } = this;
|
||||
|
||||
if (req.push(chunk, encoding) || req._readableState.destroyed) {
|
||||
callback()
|
||||
callback();
|
||||
} else {
|
||||
req[kResume] = callback
|
||||
req[kResume] = callback;
|
||||
}
|
||||
},
|
||||
destroy: (err, callback) => {
|
||||
const { body, req, res, ret, abort } = this
|
||||
const { body, req, res, ret, abort } = this;
|
||||
|
||||
if (!err && !ret._readableState.endEmitted) {
|
||||
err = new RequestAbortedError()
|
||||
err = new RequestAbortedError();
|
||||
}
|
||||
|
||||
if (abort && err) {
|
||||
abort()
|
||||
abort();
|
||||
}
|
||||
|
||||
util.destroy(body, err)
|
||||
util.destroy(req, err)
|
||||
util.destroy(res, err)
|
||||
util.destroy(body, err);
|
||||
util.destroy(req, err);
|
||||
util.destroy(res, err);
|
||||
|
||||
removeSignal(this)
|
||||
removeSignal(this);
|
||||
|
||||
callback(err)
|
||||
}
|
||||
callback(err);
|
||||
},
|
||||
}).on('prefinish', () => {
|
||||
const { req } = this
|
||||
const { req } = this;
|
||||
|
||||
// Node < 15 does not call _final in same tick.
|
||||
req.push(null)
|
||||
})
|
||||
req.push(null);
|
||||
});
|
||||
|
||||
this.res = null
|
||||
this.res = null;
|
||||
|
||||
addSignal(this, signal)
|
||||
addSignal(this, signal);
|
||||
}
|
||||
|
||||
onConnect (abort, context) {
|
||||
const { res } = this
|
||||
onConnect(abort, context) {
|
||||
const { res } = this;
|
||||
|
||||
if (this.reason) {
|
||||
abort(this.reason)
|
||||
return
|
||||
abort(this.reason);
|
||||
return;
|
||||
}
|
||||
|
||||
assert(!res, 'pipeline cannot be retried')
|
||||
assert(!res, 'pipeline cannot be retried');
|
||||
|
||||
this.abort = abort
|
||||
this.context = context
|
||||
this.abort = abort;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
onHeaders (statusCode, rawHeaders, resume) {
|
||||
const { opaque, handler, context } = this
|
||||
onHeaders(statusCode, rawHeaders, resume) {
|
||||
const { opaque, handler, context } = this;
|
||||
|
||||
if (statusCode < 200) {
|
||||
if (this.onInfo) {
|
||||
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
|
||||
this.onInfo({ statusCode, headers })
|
||||
const headers =
|
||||
this.responseHeaders === 'raw' ?
|
||||
util.parseRawHeaders(rawHeaders)
|
||||
: util.parseHeaders(rawHeaders);
|
||||
this.onInfo({ statusCode, headers });
|
||||
}
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
this.res = new PipelineResponse(resume)
|
||||
this.res = new PipelineResponse(resume);
|
||||
|
||||
let body
|
||||
let body;
|
||||
try {
|
||||
this.handler = null
|
||||
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
|
||||
this.handler = null;
|
||||
const headers =
|
||||
this.responseHeaders === 'raw' ?
|
||||
util.parseRawHeaders(rawHeaders)
|
||||
: util.parseHeaders(rawHeaders);
|
||||
body = this.runInAsyncScope(handler, null, {
|
||||
statusCode,
|
||||
headers,
|
||||
opaque,
|
||||
body: this.res,
|
||||
context
|
||||
})
|
||||
context,
|
||||
});
|
||||
} catch (err) {
|
||||
this.res.on('error', noop)
|
||||
throw err
|
||||
this.res.on('error', noop);
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (!body || typeof body.on !== 'function') {
|
||||
throw new InvalidReturnValueError('expected Readable')
|
||||
throw new InvalidReturnValueError('expected Readable');
|
||||
}
|
||||
|
||||
body
|
||||
.on('data', (chunk) => {
|
||||
const { ret, body } = this
|
||||
const { ret, body } = this;
|
||||
|
||||
if (!ret.push(chunk) && body.pause) {
|
||||
body.pause()
|
||||
body.pause();
|
||||
}
|
||||
})
|
||||
.on('error', (err) => {
|
||||
const { ret } = this
|
||||
const { ret } = this;
|
||||
|
||||
util.destroy(ret, err)
|
||||
util.destroy(ret, err);
|
||||
})
|
||||
.on('end', () => {
|
||||
const { ret } = this
|
||||
const { ret } = this;
|
||||
|
||||
ret.push(null)
|
||||
ret.push(null);
|
||||
})
|
||||
.on('close', () => {
|
||||
const { ret } = this
|
||||
const { ret } = this;
|
||||
|
||||
if (!ret._readableState.ended) {
|
||||
util.destroy(ret, new RequestAbortedError())
|
||||
util.destroy(ret, new RequestAbortedError());
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
this.body = body
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
onData (chunk) {
|
||||
const { res } = this
|
||||
return res.push(chunk)
|
||||
onData(chunk) {
|
||||
const { res } = this;
|
||||
return res.push(chunk);
|
||||
}
|
||||
|
||||
onComplete (trailers) {
|
||||
const { res } = this
|
||||
res.push(null)
|
||||
onComplete(trailers) {
|
||||
const { res } = this;
|
||||
res.push(null);
|
||||
}
|
||||
|
||||
onError (err) {
|
||||
const { ret } = this
|
||||
this.handler = null
|
||||
util.destroy(ret, err)
|
||||
onError(err) {
|
||||
const { ret } = this;
|
||||
this.handler = null;
|
||||
util.destroy(ret, err);
|
||||
}
|
||||
}
|
||||
|
||||
function pipeline (opts, handler) {
|
||||
function pipeline(opts, handler) {
|
||||
try {
|
||||
const pipelineHandler = new PipelineHandler(opts, handler)
|
||||
this.dispatch({ ...opts, body: pipelineHandler.req }, pipelineHandler)
|
||||
return pipelineHandler.ret
|
||||
const pipelineHandler = new PipelineHandler(opts, handler);
|
||||
this.dispatch({ ...opts, body: pipelineHandler.req }, pipelineHandler);
|
||||
return pipelineHandler.ret;
|
||||
} catch (err) {
|
||||
return new PassThrough().destroy(err)
|
||||
return new PassThrough().destroy(err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = pipeline
|
||||
module.exports = pipeline;
|
||||
|
Reference in New Issue
Block a user