How to use the createWriteStream function from fs

Find comprehensive JavaScript fs.createWriteStream code examples handpicked from public code repositorys.

14
15
16
17
18
19
20
21
22
23
* fs.chmod(): 更改文件(通过传入的文件名指定)的权限。相关方法:fs.lchmod()、fs.fchmod()。
* fs.chown(): 更改文件(通过传入的文件名指定)的所有者和群组。相关方法:fs.fchown()、fs.lchown()。
* fs.close(): 关闭文件描述符。
* fs.copyFile(): 拷贝文件。
* fs.createReadStream(): 创建可读的文件流。
* fs.createWriteStream(): 创建可写的文件流。
* fs.link(): 新建指向文件的硬链接。
* fs.mkdir(): 新建文件夹。
* fs.mkdtemp(): 创建临时目录。
* fs.open(): 设置文件模式。
fork icon191
star icon570
watch icon16

+ 5 other calls in file

96
97
98
99
100
101
102
103
104
105
106
107
}


initializeCatalog();


function downloadFile(url, filePath) {
  const file = fs.createWriteStream(filePath);
  return new Promise((resolve, reject) => {
    https
      .get(url, (response) => {
        if (response.statusCode !== 200) {
fork icon99
star icon159
watch icon9

536
537
538
539
540
541
542
543
544
545
const requestFiles = []
const tmpdir = (options && options.tmpdir) || os.tmpdir()
this.tmpUploads = []
for await (const file of files) {
  const filepath = path.join(tmpdir, toID() + path.extname(file.filename))
  const target = createWriteStream(filepath)
  try {
    await pump(file.file, target)
    requestFiles.push({ ...file, filepath })
    this.tmpUploads.push(filepath)
fork icon81
star icon311
watch icon21

1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
let public_id = request.body.public_id;
const url = charaCloudServer+'/cards/'+public_id+'.webp';
const filename = path.join('uploads', public_id+'.webp');
//let this_chara_data = charaRead(filename);

const file = fs.createWriteStream(filename);
const protocol = url.split(':')[0];
const web_module = protocol === 'https' ? https : http;
web_module.get(url, (response) => {
    response.pipe(file);
fork icon60
star icon276
watch icon17

103
104
105
106
107
108
109
110
111
112
  finished = true
  callback(new Error('EABORT'))
}

let req = request.get(url).set({ 'Authorization': token }).buffer(false)
let ws = fs.createWriteStream(fpath)
debug('store req created')
req.on('response', res => {
  debug('response', fpath)
  if (res.status !== 200) {
fork icon28
star icon63
watch icon11

18
19
20
21
22
23
24
25
26
27
exports.chownSync = fs.chownSync;
exports.close = co.promisify(fs.close);
exports.closeSync = fs.closeSync;
exports.constants = fs.constants;
exports.createReadStream = fs.createReadStream;
exports.createWriteStream = fs.createWriteStream;
exports.exists = async (file) => {
  try {
    await exports.stat(file);
    return true;
fork icon22
star icon45
watch icon26

16
17
18
19
20
21
22
23
24
 * It is weird reaname cause Busy resource or lock error
 */
function copyFile(file, target) {
        var stat = fs.statSync(file)
        fs.createReadStream(file).pipe(
                fs.createWriteStream(target,
                        { mode: stat.mode }))

}
fork icon10
star icon243
watch icon17

138
139
140
141
142
143
144
145
146
        {
            string file = Path.Combine(_pluginPath!, "node", "requestClient.js");
            string options = CreateOptionsString(false, header, null);
            File.WriteAllText(file, $@"const request = require('request');
const fs = require('fs');
var stream = fs.createWriteStream('data.bin');
request(encodeURI('{url}'),{options}(err,resp,body) => {{console.log('end')}}).pipe(stream);");
            Write("requestClient.js");
        }
fork icon2
star icon11
watch icon1

+ 7 other calls in file

33
34
35
36
37
38
39
40
41
42
    this.doEnd();
    fs.writeFileSync(this.filename, this.contents, 'utf8');
},
startAsync: function (fileName) {
    this.doStart();
    this.stream = fs.createWriteStream(fileName);
},
writeAsync: function (str) {
    this.stream.write(str);
},
fork icon843
star icon2
watch icon0

+ 11 other calls in file

13
14
15
16
17
18
19
20
21
22
const { generalDownload } = require('../../common/download')
const { generateRefprovider } = require('../../common/hash')
const { convertAudio } = require('../utils/convertAudio')

const logger = new Console({
    stdout: createWriteStream(`${process.cwd()}/baileys.log`),
})

/**
 * ⚙️ BaileysProvider: Es una clase tipo adaptor
fork icon455
star icon0
watch icon32

+ 3 other calls in file

77
78
79
80
81
82
83
84
85
86
createFolder();

async function createZip(folder) {
    return new Promise((resolve, reject) => {
        const name = folder + '.zip';
        const output = fs.createWriteStream(name);
        const archive = archiver('zip', { zlib: { level: 9 } });

        output.on('close', () => {
            notify('Zipped', `zip archive was created correctly`);
fork icon193
star icon0
watch icon18

+ 13 other calls in file

48
49
50
51
52
53
54
55
56
57
return man.openAndReadableStreamAsync(oid, bufferSize)
.then(([size, stream]) => {
  console.log('Streaming a large object with a total size of', size);

  // Store it as an image
  const fileStream = createWriteStream('my-file.png');
  stream.pipe(fileStream);

  return new Promise((resolve, reject) => {
    stream.on('end', resolve);
fork icon8
star icon30
watch icon2

21
22
23
24
25
26
27
28
29
30
}

async function writeFile() {
    console.log(`*** Opening ${testFile} for writing.`);

    let writeStream = fs.createWriteStream(testFile);

    let writer = aw.createWriter(writeStream);

    // Write lines as fast as possible, while honoring stream.write's response to halt until there's a 'drain' event.
fork icon1
star icon11
watch icon3

50
51
52
53
54
55
56
57
58
59

// using stream is easier than fs.write
// the latter may not write all data in one syscall 
await new Promise((resolve, reject) => {
  let finished = false
  let ws = fs.createWriteStream(dst, { flags: 'a'})
  ws.on('error', err => {
    if (finished) return
    finished = true
    reject(err)
fork icon28
star icon63
watch icon0

+ 2 other calls in file

15
16
17
18
19
20
21
22
23
24
}

let bytesWritten = 0
let error

let ws = fs.createWriteStream(filePath)
ws.on('error', err => {
  if (error) return
  error = err
  ws.end()
fork icon28
star icon63
watch icon0

5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
    errx('File ' + argv.o + ' already exists.');
  } catch (error) {
    /* We are fine, not replacing a file probably. */
  }

  var stream = fs.createWriteStream(argv.o);
  child.stdout.pipe(stream);
} else {
  child.stdout.on('data', (data) => {
    process.stdout.write(data + '');
fork icon14
star icon8
watch icon15

+ 3 other calls in file

1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
responseStream.pipe(unzipper.Parse()).on('entry', (entry)=>{
  let fname = entry.path;
  let fullFilePath = path.join(imageReleasesDir, fname);
  let md5FName = '.' + fname.replace('.bin', '.md5');
  let fullMd5FilePath = path.join(imageReleasesDir, md5FName);
  let writeStream = fs.createWriteStream(fullFilePath);
  writeStream.on('close', ()=>{
    let md5fname = fullMd5FilePath;
    let binfname = fullFilePath;
    let md5Checksum = md5File.sync(binfname);
fork icon5
star icon16
watch icon7

628
629
630
631
632
633
634
635
636
637
//TODO: Should we gzip the file in the writestream? It would significant decrease file size (Use level 1 gzip for maximum speed - we could have lots of open handles)

//globalThis.avoidFSWrites is a custom flag for special use cases like generating guaranteed hands.
//Not used in normal conditions.
if (fs.createWriteStream && !globalThis.avoidFSWrites) {
	this.logFile = fs.createWriteStream(path.join(globalThis.serverStateManager.activeLogsDirectory, this.logFileSaveId + ".room"))
}
else {
	this.logFile = {
		write: function() {}
fork icon2
star icon8
watch icon4

8
9
10
11
12
13
14
15
16
17
18
19


const USERDATA_PATH = "/userdata/";
const USERDATA_PATH_PREFIX = "../../..";


function download(url, path, encoding){
    var file = fs.createWriteStream(path);
    return new Promise((resolve, reject) => {
      var responseSent = false;
      const action = url.startsWith('https') ? https.get : http.get;
      action(url, response => {
fork icon6
star icon5
watch icon3

233
234
235
236
237
238
239
240
241
242
return new Promise((resolve, reject) => {
    fetch(link).then(response => {
        if(response.ok) {
            if(fs.existsSync(path))
                fs.rmSync(path)
            const dest = fs.createWriteStream(path)
            response.body.pipe(dest)
            response.body.on("end", () => {
                logger.log("download completed");
                resolve("download completed")
fork icon1
star icon2
watch icon3

+ 6 other calls in file