mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-11 17:41:26 -06:00
27 lines
648 B
TypeScript
27 lines
648 B
TypeScript
// @ts-check
|
|
const fs = require("fs");
|
|
const File = require("./vinyl");
|
|
const { Readable } = require("stream");
|
|
|
|
/**
|
|
* @param {File} file
|
|
*/
|
|
function streamFromFile(file) {
|
|
return file.isBuffer() ? streamFromBuffer(file.contents) :
|
|
file.isStream() ? file.contents :
|
|
fs.createReadStream(file.path, { autoClose: true });
|
|
}
|
|
exports.streamFromFile = streamFromFile;
|
|
|
|
/**
|
|
* @param {Buffer} buffer
|
|
*/
|
|
function streamFromBuffer(buffer) {
|
|
return new Readable({
|
|
read() {
|
|
this.push(buffer);
|
|
this.push(null);
|
|
}
|
|
});
|
|
}
|
|
exports.streamFromBuffer = streamFromBuffer; |