fs#createWriteStream JavaScript Examples
The following examples show how to use
fs#createWriteStream.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: update-user.service.js From saasgear with MIT License | 6 votes |
export async function changeUserAvatar(fileData, user) {
try {
const file = await fileData;
const fileName = `avatar-${user.id}-new-${new Date().getTime()}-${file.filename}`;
const stream = file.createReadStream();
await new Promise((resolve, reject) => {
const writeStream = createWriteStream(join(FOLDER_PATHS.avatarDir, fileName));
writeStream.on('finish', () => resolve());
writeStream.on('error', async (error) => reject(error));
stream.on('error', (error) => writeStream.destroy(error));
stream.pipe(writeStream);
});
await updateUser(user.id, { avatar_url: fileName });
return { url: fileName };
} catch (error) {
logger.error(error);
throw new ApolloError('Something went wrong!');
}
}
Example #2
Source File: compress.js From ucompress with ISC License | 6 votes |
br = (source, mode) => new Promise((res, rej) => {
const dest = source + '.br';
stat(source, (err, stats) => {
/* istanbul ignore next */
if (err) rej(err);
else pipeline(
createReadStream(source),
createBrotliCompress({
[BROTLI_PARAM_SIZE_HINT]: stats.size,
[BROTLI_PARAM_QUALITY]: BROTLI_MAX_QUALITY,
[BROTLI_PARAM_MODE]: mode == 'text' ?
BROTLI_MODE_TEXT : (
mode === 'font' ?
BROTLI_MODE_FONT :
/* istanbul ignore next */
BROTLI_MODE_GENERIC
)
}),
createWriteStream(dest),
err => {
/* istanbul ignore next */
if (err) rej(err);
else res(dest);
}
);
});
})
Example #3
Source File: compress.js From ucompress with ISC License | 6 votes |
deflate = source => new Promise((res, rej) => {
const dest = source + '.deflate';
pipeline(
createReadStream(source),
createDeflate(zlibDefaultOptions),
createWriteStream(dest),
err => {
/* istanbul ignore next */
if (err) rej(err);
else res(dest);
}
);
})
Example #4
Source File: compress.js From ucompress with ISC License | 6 votes |
gzip = source => new Promise((res, rej) => {
const dest = source + '.gzip';
pipeline(
createReadStream(source),
createGzip(zlibDefaultOptions),
createWriteStream(dest),
err => {
/* istanbul ignore next */
if (err) rej(err);
else res(dest);
}
);
})
Example #5
Source File: helpers.js From v8-deopt-viewer with MIT License | 6 votes |
export function decompress(inputPath) {
return new Promise((resolve, reject) => {
const stream = createReadStream(inputPath)
.pipe(zlib.createBrotliDecompress())
.pipe(createWriteStream(inputPath.replace(/.br$/, "")));
stream
.on("end", resolve)
.on("close", resolve)
.on("finish", resolve)
.on("error", reject);
});
}
Example #6
Source File: index.js From kit with MIT License | 6 votes |
/**
* @param {string} file
* @param {'gz' | 'br'} format
*/
async function compress_file(file, format = 'gz') {
const compress =
format == 'br'
? zlib.createBrotliCompress({
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
}
})
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
const source = createReadStream(file);
const destination = createWriteStream(`${file}.${format}`);
await pipe(source, compress, destination);
}
Example #7
Source File: index.js From kit with MIT License | 6 votes |
/**
* @param {string} file
* @param {'gz' | 'br'} format
*/
async function compress_file(file, format = 'gz') {
const compress =
format == 'br'
? zlib.createBrotliCompress({
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
}
})
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
const source = createReadStream(file);
const destination = createWriteStream(`${file}.${format}`);
await pipe(source, compress, destination);
}
Example #8
Source File: SitemapGenerator.js From sampo-ui with MIT License | 5 votes |
getURLs = async resultClasses => {
const sitemapStream = new SitemapAndIndexStream({
limit: 10000, // defaults to 45k
// SitemapAndIndexStream will call this user provided function every time
// it needs to create a new sitemap file. You merely need to return a stream
// for it to write the sitemap urls to and the expected url where that sitemap will be hosted
getSitemapStream: index => {
const sitemapStream = new SitemapStream({ hostname: sitemapConfig.baseUrl })
const fileName = `sitemap-${index}.xml.gz`
sitemapStream
.pipe(createGzip()) // compress the output
.pipe(createWriteStream(resolve(`${sitemapConfig.outputDir}/${fileName}`))) // write it to sitemap-NUMBER.xml.gz
return [`${sitemapConfig.sitemapUrl}/${fileName}`, sitemapStream]
}
})
sitemapStream
.pipe(createGzip()) // compress the output
.pipe(createWriteStream(resolve(`${sitemapConfig.outputDir}/sitemap-index.xml.gz`))) // write it to sitemap-index.xml.gz
// Add portal's main level URLs to sitemap
sitemapStream.write(createSitemapEntry({ path: null }))
sitemapStream.write(createSitemapEntry({ path: 'about' }))
sitemapStream.write(createSitemapEntry({ path: 'instructions' }))
sitemapStream.write(createSitemapEntry({ path: 'feedback' }))
// Then process each resultClass
for (const resultClass of resultClasses) {
if (resultClass.hasSearchPerspective) {
// If there is a search perspective, add it's URL to sitemap
const searchMode = resultClass.searchMode ? resultClass.searchMode : 'faceted-search'
sitemapStream.write(createSitemapEntry({ path: `${resultClass.perspectiveID}/${searchMode}/table` }))
}
const response = await queryInstancePageURLs(resultClass)
// Add instance page URLs to sitemap
response.data.forEach(item => sitemapStream.write(item))
}
sitemapStream.end()
}