本文整理汇总了TypeScript中stream.Stream类的典型用法代码示例。如果您正苦于以下问题:TypeScript Stream类的具体用法?TypeScript Stream怎么用?TypeScript Stream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Stream类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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:
api.get(async (req: Request, res: Response) => {
let stream: Stream = await objectModel
.find({})
.stream({ transform: JSON.stringify });
stream.on("data", (doc: Object) => {
res.write(doc, "ascii");
}).on("error", (err: Error) => {
console.log(err);
}).on("close", () => {
res.end("Done");
});
});
示例3:
api.get(async (req: Request, res: Response) => {
let stream: Stream = await complexModel
.find({})
.select({_id: 0, name: 1})
.lean(true)
.stream({ transform: JSON.stringify });
stream.on("data", (doc: Object) => {
res.write(doc, "ascii");
complexIO.emit("complex", doc);
}).on("error", (err: Error) => {
console.log(err);
}).on("close", () => {
res.end("Done");
});
});
示例4: 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);
}
});
示例5:
api.get(async (req: Request, res: Response) => {
let stream: Stream = await bimModel
.find({})
.lean(true)
.stream({ transform: JSON.stringify });
res.type("application/json");
stream.on("data", (doc: string) => {
// res.write();
entityIO.emit("entity", doc);
}).on("error", (err: Error) => {
console.log(err);
}).on("close", () => {
res.end(JSON.stringify({completed: true}));
});
});
示例6: 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 });
}
});
示例7: 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;
});
}
示例8: 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 => {
示例9: 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);
}
};
示例10:
stream.on('metadata', metadata => {
// Create a response object with succeeds.
// TODO: unify this logic with the decoration of gRPC response when it's
// added. see: https://github.com/googleapis/gax-nodejs/issues/65
stream.emit('response', {
code: 200,
details: '',
message: 'OK',
metadata,
});
});