本文整理汇总了TypeScript中stream.Readable.pipe方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Readable.pipe方法的具体用法?TypeScript Readable.pipe怎么用?TypeScript Readable.pipe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stream.Readable
的用法示例。
在下文中一共展示了Readable.pipe方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
export default async function (
stream: Readable,
result: ScrapeResult,
next: Next
): Promise<ScrapeResult> {
const { url } = result
if (result.encodingFormat !== 'text/html') {
return next(stream)
}
const [parsed] = await Promise.all([
parseHtml(stream.pipe(new PassThrough()), url),
next(stream.pipe(new PassThrough()))
])
const { twitter, html, icons, dublincore, applinks, sailthru, alternate } = parsed
const [jsonld, rdfa, microdata] = await Promise.all([
parsed.jsonld ? Jsonld.expand(parsed.jsonld, { base: url }).catch(() => undefined) : undefined,
parsed.rdfa ? Jsonld.expand(parsed.rdfa, { base: url }).catch(() => undefined) : undefined,
parsed.microdata ? Jsonld.expand(parsed.microdata, { base: url }).catch(() => undefined) : undefined
])
return Object.assign(result, {
jsonld,
rdfa,
microdata,
twitter,
html,
icons,
dublincore,
applinks,
sailthru,
alternate
})
}
示例2: mkdirp
return mkdirp(targetDirName, void 0, token).then(() => new Promise((c, e) => {
if (token.isCancellationRequested) {
return;
}
try {
istream = createWriteStream(targetFileName, { mode });
istream.once('close', () => c(null));
istream.once('error', e);
stream.once('error', e);
stream.pipe(istream);
} catch (error) {
e(error);
}
}));
示例3: createWriteStream
return Promise.resolve(mkdirp(targetDirName, undefined, token)).then(() => new Promise<void>((c, e) => {
if (token.isCancellationRequested) {
return;
}
try {
istream = createWriteStream(targetFileName, { mode });
istream.once('close', () => c());
istream.once('error', e);
stream.once('error', e);
stream.pipe(istream);
} catch (error) {
e(error);
}
}));
示例4: lint
lint(html: string): Promise<Issue[]> {
var parser: SAXParser = new SAXParser({ locationInfo: true });
var parseState: ParseState = new ParseState(this.scopes, this.voids);
var stream: Readable = new Readable();
parseState.initPreRules(parser);
let rules = this.rules;
rules.forEach((rule) => {
rule.init(parser, parseState);
});
parseState.initPostRules(parser);
stream.push(html);
stream.push(null);
var work = stream.pipe(parser);
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;
});
}
示例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: unwrapBody
let requestHandler = (req : Request, response : Response) => {
let args = endpoint.params.map(x => req.params[x]);
args = args.concat(endpoint.query.map(x => req.query[x]));
args = args.concat(endpoint.headers.map(x => req.get(x)));
let body = unwrapBody(req.body);
if(body) {
args.push(body);
}
let handler = target[endpoint.handler];
let result;
try {
result = handler.apply(target, args);
} catch(error) {
let wrappedError = wrapErrorBody(error); //TODO: this error handler should be configurable
console.error('Request Error: ' + JSON.stringify(wrappedError));
response.status(500).send(wrappedError);
return;
}
if(isPromise(result)) {
let promise : Promise<any> = result;
promise.then(x => response.send(x))
.catch(error => {
let wrappedError = wrapErrorBody(error); //TODO: this error handler should be configurable
console.error('Request Error: ' + JSON.stringify(wrappedError));
response.status(500).send(wrappedError);
});
} else if(isReadableStream(result)) {
let readableStream : Readable = result;
readableStream.pipe(response);
} else {
response.send(result);
}
};
示例7: catch
const logStreamInLogger = (
stream: ReadableStream | null,
loggerLevel: Level,
) => {
if (!stream) return;
stream.pipe(split()).on('data', (line: string) => {
if (line.length === 0) return;
if (line.startsWith('{') && line.endsWith('}')) {
try {
const json: object = JSON.parse(line);
logger.log('', json, loggerLevel);
return;
} catch (err) {}
}
outputLogger.log(line, undefined, loggerLevel);
});
};
示例8: it
it('should throw if input is streamed', (done: Function) => {
const transform = new PboTransformStream().on('error', (err: Error) => {
expect(err).to.be.an('Error');
expect(err.message).to.equal('Streaming input is not supported');
done();
});
const emitter = new Readable({
objectMode: true,
read: () => {
const file = new File({ contents: new Duplex() });
emitter.push(file);
emitter.push(null);
}
});
emitter.pipe(transform);
});
示例9: coalesce
exportSchemaToFile: ({ pubsub, file, schema }: any) => {
const sql = `SELECT c.TABLE_NAME, c.TABLE_SCHEMA, '' AS TABLE_TEXT, t.TABLE_TYPE, c.COLUMN_NAME, coalesce(pgd.description, '') AS COLUMN_TEXT, c.DATA_TYPE, coalesce(c.NUMERIC_PRECISION, c.CHARACTER_MAXIMUM_LENGTH) LENGTH, c.NUMERIC_SCALE
FROM information_schema.tables t
join information_schema.columns c on t.table_schema=c.table_schema and t.table_name=c.table_name
join pg_catalog.pg_statio_all_tables pt on pt.schemaname=t.table_schema and pt.relname=t.table_name
left outer join pg_catalog.pg_description pgd on pgd.objoid=pt.relid and pgd.objsubid=c.ordinal_position
where t.table_schema=$1
order by c.TABLE_NAME, c.ORDINAL_POSITION
`
const handleError = err => pubsub.emit('export-error', err)
const stream = new Readable({ objectMode: true })
stream._read = () => {
// noop
}
client.query(
{
text: sql,
values: [schema],
rowMode: 'array',
},
(err, result) => {
if (err) {
handleError(err)
} else {
result.rows.map(row => stream.push(row))
stream.push(null)
}
}
)
return stream
.pipe(createGroupTablesTransform(pubsub))
.on('error', handleError)
.pipe(JSONStream.stringify())
.on('error', handleError)
.on('end', () => console.log('end json stringify'))
.pipe(createWriteStream(file))
.on('end', () => console.log('end write'))
.on('error', handleError)
},
示例10: Promise
return new Promise((resolve, reject) => {
try {
const [nsPath, dataPath, metaPath] = this.getPaths(ns, key)
ensurePath(nsPath)
let headers = {}
if (opts && opts.headers) {
headers = { ...headers, ...opts.headers }
}
const stream = fs.createWriteStream(dataPath)
value.pipe(stream)
fs.writeFileSync(metaPath, JSON.stringify(headers), "utf8")
resolve(true)
} catch (err) {
log.warn("[FileSystemBlobStore] set error", err)
reject(err)
}
})