本文整理汇总了TypeScript中stream.Stream.pipe方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Stream.pipe方法的具体用法?TypeScript Stream.pipe怎么用?TypeScript Stream.pipe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stream.Stream
的用法示例。
在下文中一共展示了Stream.pipe方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Promise
transform?: FileTransform) => new Promise((resolve, reject) => {
if (projectOptions.root === undefined) {
throw new Error('projectOptions.root is undefined');
}
root = projectOptions.root;
const config = new ProjectConfig(projectOptions);
const analyzer = new BuildAnalyzer(config);
bundler = new BuildBundler(config, analyzer, bundlerOptions);
bundledStream = mergeStream(analyzer.sources(), analyzer.dependencies());
if (transform) {
bundledStream = bundledStream.pipe(transform);
}
bundledStream = bundledStream.pipe(bundler);
bundler = new BuildBundler(config, analyzer);
files = new Map<LocalFsPath, File>();
bundledStream.on('data', (file: File) => {
files.set(file.path, file);
});
bundledStream.on('end', () => {
resolve(files);
});
bundledStream.on('error', (err: Error) => {
reject(err);
});
});
示例2: lint
lint(html: string|Stream): Promise<Issue[]> {
var parser: SAXParser = new SAXParser({ locationInfo: true });
var parseState: ParseState = new ParseState(this.scopes, this.voids);
parseState.initPreRules(parser);
let rules = this.rules;
rules.forEach((rule) => {
rule.init(parser, parseState);
});
parseState.initPostRules(parser);
var work:SAXParser;
if(typeof(html) === 'string')
{
var stream: Readable = new Readable();
stream.push(html);
stream.push(null);
work = stream.pipe(parser);
}else if(this.isStream(html))
{
work = html.pipe(parser);
}
else{
throw new Error("html isn't pipeable");
}
var completed = new Promise<void>(function (resolve, reject) {
work.on("end", () => {
parseState.finalise();
resolve();
});
});
var ruleTasks = [];
rules.forEach((rule) => {
let task = completed.then(() => {
return rule.finalise();
});
ruleTasks.push(task);
});
return Promise.all(ruleTasks).then(results => {
var all = new Array<Issue>();
results.forEach(parts => {
all = all.concat(parts);
});
return all;
});
}
示例3: rawRequest
req = rawRequest(opts, (res: http.IncomingMessage) => {
const followRedirects: number = isNumber(options.followRedirects) ? options.followRedirects : 3;
if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400 && followRedirects > 0 && res.headers['location']) {
request(assign({}, options, {
url: res.headers['location'],
followRedirects: followRedirects - 1
}), token).then(c, e);
} else {
let stream: Stream = res;
if (res.headers['content-encoding'] === 'gzip') {
stream = stream.pipe(createGunzip());
}
c({ res, stream } as IRequestContext);
}
});
示例4: rawRequest
req = rawRequest(opts, (res: http.ClientResponse) => {
const followRedirects = isNumber(options.followRedirects) ? options.followRedirects : 3;
if (res.statusCode >= 300 && res.statusCode < 400 && followRedirects > 0 && res.headers['location']) {
request(assign({}, options, {
url: res.headers['location'],
followRedirects: followRedirects - 1
})).done(c, e);
} else {
let stream: Stream = res;
if (res.headers['content-encoding'] === 'gzip') {
stream = stream.pipe(createGunzip());
}
c({ res, stream });
}
});
示例5: lint
lint(html: string | Stream, path?: string): Promise<Issue[]> {
var parser = this.parserBuilder.build();
parser.init(this.rules, this.ast, path);
if (typeof (html) === 'string') {
var stream: Readable = new Readable();
stream.push(html);
stream.push(null);
stream.pipe(parser);
} else if (this.isStream(html)) {
html.pipe(parser);
}
else {
throw new Error("html isn't pipeable");
}
var completed = new Promise<void>(function (resolve, reject) {
parser.on("end", () => {
parser.finalise();
resolve();
});
});
var ruleTasks = [];
this.rules.forEach((rule) => {
let task = completed.then(() => {
return rule.finalise(this.ast.root);
});
ruleTasks.push(task);
});
return Promise.all(ruleTasks).then(results => {
var all = new Array<Issue>();
results.forEach(parts => {
all = all.concat(parts);
});
all = all.sort((a, b) => (a.line - b.line) * 1000 + (a.column - b.column));
return all;
});
}
示例6: request
return request(options).then(result => new TPromise<IXHRResponse>((c, e, p) => {
const res = result.res;
let stream: Stream = res;
if (res.headers['content-encoding'] === 'gzip') {
stream = stream.pipe(createGunzip());
}
const data: string[] = [];
stream.on('data', c => data.push(c));
stream.on('end', () => {
const status = res.statusCode;
if (options.followRedirects > 0 && (status >= 300 && status <= 303 || status === 307)) {
let location = res.headers['location'];
if (location) {
let newOptions = {
type: options.type, url: location, user: options.user, password: options.password, responseType: options.responseType, headers: options.headers,
timeout: options.timeout, followRedirects: options.followRedirects - 1, data: options.data
};
xhr(newOptions).done(c, e, p);
return;
}
}
const response: IXHRResponse = {
responseText: data.join(''),
status,
getResponseHeader: header => res.headers[header],
readyState: 4
};
if ((status >= 200 && status < 300) || status === 1223) {
c(response);
} else {
e(response);
}
});
}, err => {
示例7: async
const extractEntry = async (entry: yauzl.Entry, err: Error, src: Stream) => {
if (err) {
throw err;
}
const entryPath = join(destination, entry.fileName);
await sf.mkdirp(dirname(entryPath));
const dst = createWriteStream(entryPath);
const progressStream = progress({
length: entry.uncompressedSize,
time: 100,
});
let progressFactor = entry.compressedSize / zipfile.fileSize;
progressStream.on("progress", info => {
opts.onProgress({
progress: progressOffset + info.percentage / 100 * progressFactor,
});
});
progressStream.pipe(dst);
src.pipe(progressStream);
await sf.promised(dst);
progressOffset += progressFactor;
await sf.chmod(entryPath, 0o755);
const fileBuffer = await sf.readFile(entryPath, { encoding: null });
const signedHash = crc32.buf(fileBuffer);
// this converts an int32 to an uint32 (which is what yauzl reports)
const hash = new Uint32Array([signedHash])[0];
if (hash !== entry.crc32) {
await sf.unlink(entryPath);
throw new Error(
`CRC32 mismatch for ${entry.fileName}: expected ${
entry.crc32
} got ${hash}`
);
}
};
示例8: onEnd
const send = (size: number, data: string | Stream | null) => {
response.writeHead(200, {
'Content-Type': contentType,
'Content-Length': size
});
if (!data) {
response.end();
return;
}
if (typeof data === 'string') {
response.end(data, onEnd);
} else {
data.on('end', () => onEnd());
data.on('error', (error: Error) => {
onEnd(error);
// If the read stream errors, the write stream has to be
// manually closed
response.end();
});
data.pipe(response);
}
};