本文整理汇总了TypeScript中http.IncomingMessage.pipe方法的典型用法代码示例。如果您正苦于以下问题:TypeScript IncomingMessage.pipe方法的具体用法?TypeScript IncomingMessage.pipe怎么用?TypeScript IncomingMessage.pipe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.IncomingMessage
的用法示例。
在下文中一共展示了IncomingMessage.pipe方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: default
export default (response: IncomingMessage, options: Options, emitter: EventEmitter) => {
const downloadBodySize = Number(response.headers['content-length']) || undefined;
const progressStream: TransformStream = downloadProgress(response, emitter, downloadBodySize);
mimicResponse(response, progressStream);
const newResponse = (
options.decompress === true &&
is.function_(decompressResponse) &&
options.method !== 'HEAD' ? decompressResponse(progressStream as unknown as IncomingMessage) : progressStream
) as Response;
if (!options.decompress && ['gzip', 'deflate', 'br'].includes(response.headers['content-encoding'] || '')) {
options.encoding = null;
}
emitter.emit('response', newResponse);
emitter.emit('downloadProgress', {
percent: 0,
transferred: 0,
total: downloadBodySize
});
response.pipe(progressStream);
};
示例2: callback
}, (response: IncomingMessage) => {
if (response.statusCode >= 400) {
callback(new Error("Request error, status " + response.statusCode + ": " + response.statusMessage))
return
}
const redirectUrl = response.headers.location
if (redirectUrl != null) {
if (redirectCount < maxRedirects) {
doDownload(redirectUrl, destination, redirectCount++, callback)
}
else {
callback(new Error("Too many redirects (> " + maxRedirects + ")"))
}
return
}
const downloadStream = createWriteStream(destination)
response.pipe(downloadStream)
downloadStream.on("finish", () => downloadStream.close(callback))
let ended = false
response.on("end", () => {
ended = true
})
response.on("close", () => {
if (!ended) {
callback(new Error("Request aborted"))
}
})
})
示例3: createOptions
client.fetch(url, createOptions(url), (err: Error, res: IncomingMessage) => {
if (err) return reject(err)
const ps = pauseStream()
// without pausing, gunzip/tar-fs would miss the beginning of the stream
res.pipe(ps.pause())
resolve(ps)
})
示例4: downloadWrite
export function downloadWrite(incomingMessage:IncomingMessage, user:IUser) {
const fullPath2Write = getDownloadFullPath(user.name);
let file:WriteStream = fs.createWriteStream(fullPath2Write);
incomingMessage.pipe(file);
file.on('finish', function () {
const ONE_K = 1024 * 1024;
if (file.bytesWritten < ONE_K) {
fs.unlinkSync(fullPath2Write);
}
file.close();
console.log(`下载任务结束: [${user.name}]`);
user.$isDownloading = false;
});
}
示例5: response
// Handle the HTTP response.
function response (res: IncomingMessage) {
const status = res.statusCode
const redirect = REDIRECT_STATUS[status]
// Handle HTTP redirects.
if (followRedirects && redirect != null && res.headers.location) {
const newUrl = urlLib.resolve(url, res.headers.location)
// Ignore the result of the response on redirect.
res.resume()
// Kill the old cookies on redirect.
request.remove('Cookie')
if (redirect === REDIRECT_TYPE.FOLLOW_WITH_GET) {
// Update the "Content-Length" for updated redirection body.
request.set('Content-Length', '0')
return get(newUrl, 'GET')
}
if (redirect === REDIRECT_TYPE.FOLLOW_WITH_CONFIRMATION) {
// Following HTTP spec by automatically redirecting with GET/HEAD.
if (arg.method === 'GET' || arg.method === 'HEAD') {
return get(newUrl, method, body)
}
// Allow the user to confirm redirect according to HTTP spec.
if (confirmRedirect(req, res)) {
return get(newUrl, method, body)
}
}
}
request.downloadLength = num(res.headers['content-length'], 0)
// Track download progress.
res.pipe(responseProxy)
return Promise.resolve({
body: responseProxy,
status: status,
statusText: res.statusMessage,
headers: res.headers,
rawHeaders: res.rawHeaders,
url: url
})
}
示例6: Promise
return new Promise((resolve, reject)=> {
let formData: any = {};
let hasError: string;
busboy.on('file', function(fieldName: string, stream: ReadableStream, filename: string, encoding: string, mimeType: string) {
//save tmp files
const tmpDir = (options.uploadDir ? options.uploadDir : os.tmpDir());
const tmpPath = tmp.tmpNameSync({template: tmpDir + '/upload-XXXXXXXXXXXXXXXXXXX'});
stream.pipe(fs.createWriteStream(tmpPath));
stream.on('end', function() {
//push file data
formData[fieldName] = {
fileName: filename,
mimeType: mimeType,
tmpPath: tmpPath
};
});
stream.on('limit', function() {
hasError = 'filesSizeLimit';
});
});
busboy.on('field', function(fieldName: string, val: any) {
//push text data
formData[fieldName] = val;
});
busboy.on('partsLimit', function() {
hasError = 'partsLimit';
});
busboy.on('filesLimit', function() {
hasError = 'filesNumberLimit';
});
busboy.on('fieldsLimit', function() {
hasError = 'fieldsLimit';
});
busboy.on('finish', function() {
if (hasError) {
reject(hasError);
} else {
resolve(formData);
}
});
req.pipe(busboy);
});
示例7: getInterval
(response: IncomingMessage) => {
if (options.timing) {
if (timings.lookup === undefined) {
timings.lookup = timings.socket;
}
if (timings.connect === undefined) {
timings.connect = timings.socket;
}
timings.response = getInterval(startTime);
}
let bodyStream;
const chunks: Buffer[] = [];
const encoding =
response.headers && response.headers["content-encoding"];
if (encoding === "gzip" || encoding === "deflate") {
response.on("error", reject);
bodyStream = response.pipe(zlib.createUnzip());
} else {
bodyStream = response;
}
bodyStream.on("error", reject);
bodyStream.on("data", chunk => {
if (chunk instanceof Buffer) {
chunks.push(chunk);
} else {
chunks.push(Buffer.from(chunk, "utf-8"));
}
});
bodyStream.on("end", () => {
const body = Buffer.concat(chunks).toString("utf8");
hasRequestEnded = true;
const serviceClientResponse = new ServiceClientResponse(
response.statusCode || 0,
response.headers,
body,
options
);
if (options.timing) {
timings.end = getInterval(startTime);
serviceClientResponse.timings = timings;
serviceClientResponse.timingPhases = makeTimingPhases(timings);
}
resolve(serviceClientResponse);
});
}
示例8:
let request = https.get(url, (response: IncomingMessage) => {
response.pipe(file);
file.on("finish", () => {
deferal.resolve();
});
}).on("error", (err: any) => {
示例9:
let returnResponse = () => {
res.setHeader('Content-Type', 'text/plain');
res.writeHead(200);
req.pipe(res);
};
示例10: createWriteStream
.then(() => {
const downloadStream = createWriteStream(destination)
response.pipe(downloadStream)
downloadStream.on("finish", () => downloadStream.close(callback))
})