本文整理匯總了TypeScript中stream.Stream.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Stream.on方法的具體用法?TypeScript Stream.on怎麽用?TypeScript Stream.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類stream.Stream
的用法示例。
在下文中一共展示了Stream.on方法的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: 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 => {
示例3:
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");
});
});
示例4: 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);
}
};
示例5: Promise
return new Promise(function(resolve, reject) {
var results = [];
stream
.on('data', function(result) {
results.push(result);
})
.on('end', function() {
resolve(
Buffer.isBuffer(results[0])
? Buffer.concat(results).toString()
: results
);
})
.on('error', reject);
});
示例6:
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");
});
});
示例7:
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}));
});
});
示例8: forwardEvents
/**
* Forward events from an API request stream to the user's stream.
* @param {Stream} stream - The API request stream.
*/
forwardEvents(stream: Stream) {
const eventsToForward = ['metadata', 'response', 'status'];
eventsToForward.forEach(event => {
stream.on(event, this.emit.bind(this, event));
});
// We also want to supply the status data as 'response' event to support
// the behavior of google-cloud-node expects.
// see:
// https://github.com/GoogleCloudPlatform/google-cloud-node/pull/1775#issuecomment-259141029
// https://github.com/GoogleCloudPlatform/google-cloud-node/blob/116436fa789d8b0f7fc5100b19b424e3ec63e6bf/packages/common/src/grpc-service.js#L355
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,
});
});
}
示例9:
eventsToForward.forEach(event => {
stream.on(event, this.emit.bind(this, event));
});
示例10: resolve
return new Promise<void>((resolve) => {
s.on('end', () => resolve());
})