本文整理汇总了TypeScript中http.ClientRequest.once方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ClientRequest.once方法的具体用法?TypeScript ClientRequest.once怎么用?TypeScript ClientRequest.once使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.ClientRequest
的用法示例。
在下文中一共展示了ClientRequest.once方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: uploadProgress
export function uploadProgress(request: ClientRequest, emitter: EventEmitter, uploadBodySize?: number): void {
const uploadEventFrequency = 150;
let uploaded = 0;
let progressInterval: NodeJS.Timeout;
emitter.emit('uploadProgress', {
percent: 0,
transferred: 0,
total: uploadBodySize
});
request.once('error', () => {
clearInterval(progressInterval);
});
request.once('response', () => {
clearInterval(progressInterval);
emitter.emit('uploadProgress', {
percent: 1,
transferred: uploaded,
total: uploadBodySize
});
});
request.once('socket', (socket: Socket) => {
const onSocketConnect = (): void => {
progressInterval = setInterval(() => {
const lastUploaded = uploaded;
/* istanbul ignore next: see #490 (occurs randomly!) */
const headersSize = (request as any)._header ? Buffer.byteLength((request as any)._header) : 0;
uploaded = socket.bytesWritten - headersSize;
// Don't emit events with unchanged progress and
// prevent last event from being emitted, because
// it's emitted when `response` is emitted
if (uploaded === lastUploaded || uploaded === uploadBodySize) {
return;
}
emitter.emit('uploadProgress', {
percent: uploadBodySize ? uploaded / uploadBodySize : 0,
transferred: uploaded,
total: uploadBodySize
});
}, uploadEventFrequency);
};
/* istanbul ignore next: hard to test */
if (socket.connecting) {
socket.once('connect', onSocketConnect);
} else if (socket.writable) {
// The socket is being reused from pool,
// so the connect event will not be emitted
onSocketConnect();
}
});
}
示例2:
return new Promise<number>((resolve, reject)=> {
request.once('response', (msg: IncomingMessage) => {
this._getLengthFromHeaders(msg.headers)
.then(resolve)
.catch(reject);
});
});
示例3: addTimeout
request.once('socket', (socket: net.Socket): void => {
const {socketPath} = request as any;
/* istanbul ignore next: hard to test */
if (socket.connecting) {
if (delays.lookup !== undefined && !socketPath && !net.isIP(hostname || host)) {
const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup');
socket.once('lookup', cancelTimeout);
}
if (delays.connect !== undefined) {
const timeConnect = () => addTimeout(delays.connect!, timeoutHandler, 'connect');
if (socketPath || net.isIP(hostname || host)) {
socket.once('connect', timeConnect());
} else {
socket.once('lookup', (error: Error): void => {
if (error === null) {
socket.once('connect', timeConnect());
}
});
}
}
if (delays.secureConnect !== undefined && options.protocol === 'https:') {
socket.once('connect', (): void => {
const cancelTimeout = addTimeout(delays.secureConnect!, timeoutHandler, 'secureConnect');
socket.once('secureConnect', cancelTimeout);
});
}
}
if (delays.send !== undefined) {
const timeRequest = () => addTimeout(delays.send!, timeoutHandler, 'send');
/* istanbul ignore next: hard to test */
if (socket.connecting) {
socket.once('connect', (): void => {
request.once('upload-complete', timeRequest());
});
} else {
request.once('upload-complete', timeRequest());
}
}
});
示例4: resolve
return new Promise<Error>((resolve)=> {
request.once('error', (err)=> {
resolve(err);
});
});
示例5: default
export default (request: ClientRequest, delays: Delays, options: any) => {
/* istanbul ignore next: this makes sure timed-out isn't called twice */
if (Reflect.has(request, reentry)) {
return noop;
}
(request as any)[reentry] = true;
let stopNewTimeouts = false;
const addTimeout = (delay: number, callback: (...args: any) => void, ...args: any): (() => void) => {
// An error had been thrown before. Going further would result in uncaught errors.
// See https://github.com/sindresorhus/got/issues/631#issuecomment-435675051
if (stopNewTimeouts) {
return noop;
}
// Event loop order is timers, poll, immediates.
// The timed event may emit during the current tick poll phase, so
// defer calling the handler until the poll phase completes.
let immediate: NodeJS.Immediate;
const timeout: NodeJS.Timeout = setTimeout(() => {
immediate = setImmediate(callback, delay, ...args);
/* istanbul ignore next: added in node v9.7.0 */
if (immediate.unref) {
immediate.unref();
}
}, delay);
/* istanbul ignore next: in order to support electron renderer */
if (timeout.unref) {
timeout.unref();
}
const cancel = (): void => {
clearTimeout(timeout);
clearImmediate(immediate);
};
cancelers.push(cancel);
return cancel;
};
const {host, hostname} = options;
const timeoutHandler = (delay: number, event: string): void => {
request.emit('error', new TimeoutError(delay, event));
request.abort();
};
const cancelers: Array<() => void> = [];
const cancelTimeouts = (): void => {
stopNewTimeouts = true;
cancelers.forEach(cancelTimeout => cancelTimeout());
};
request.on('error', (error: Error): void => {
if (error.message !== 'socket hang up') {
cancelTimeouts();
}
});
request.once('response', response => {
response.once('end', cancelTimeouts);
});
if (delays.request !== undefined) {
addTimeout(delays.request, timeoutHandler, 'request');
}
if (delays.socket !== undefined) {
const socketTimeoutHandler = (): void => {
timeoutHandler(delays.socket!, 'socket');
};
request.setTimeout(delays.socket, socketTimeoutHandler);
// `request.setTimeout(0)` causes a memory leak.
// We can just remove the listener and forget about the timer - it's unreffed.
// See https://github.com/sindresorhus/got/issues/690
cancelers.push((): void => {
request.removeListener('timeout', socketTimeoutHandler);
});
}
request.once('socket', (socket: net.Socket): void => {
const {socketPath} = request as any;
/* istanbul ignore next: hard to test */
if (socket.connecting) {
if (delays.lookup !== undefined && !socketPath && !net.isIP(hostname || host)) {
const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup');
socket.once('lookup', cancelTimeout);
}
if (delays.connect !== undefined) {
const timeConnect = () => addTimeout(delays.connect!, timeoutHandler, 'connect');
if (socketPath || net.isIP(hostname || host)) {
socket.once('connect', timeConnect());
} else {
//.........这里部分代码省略.........
示例6: timeRequest
socket.once('connect', (): void => {
request.once('upload-complete', timeRequest());
});