Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

fs.WriteStream truncation #6402

@isaacs

Description

@isaacs

This fails:

var assert = require('assert');

var fs = require('fs');
var https = require('https');
var url = require('url');
var path = require('path');
var stream = require('stream');

var resultFile = path.resolve(common.tmpDir, 'result');

var key = fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem');
var cert = fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem');

var PORT = common.PORT;

// number of bytes discovered empirically
var data = new Buffer(99653);
var chars = 'abcdefghijklmnopqrstuvwxyz';
data.fill(chars + '\n');
var filler = new Buffer(1024).fill(chars);
filler[filler.length - 1] = 10;
data.fill(filler.toString());

syntheticTest();

function syntheticTest() {
  var res = new stream.Readable({ highWaterMark: 1024 });
  var p = 0;
  res._read = function() {
    var len = Math.min(1024, data.length - p);
    console.error('_read data len', len, p, data.length);
    if (len <= 0)
      res.push(null);
    else {
      res.push(data.slice(p, len + p));
      p += len;
    }
  };

  test(res, cb);
}

function test(res, cb) {
  var out = fs.createWriteStream(resultFile);

  var bytes = 0;
  res.on('data', function(chunk) {
    bytes += chunk.length;
    console.error('bytes', chunk.length, bytes);
  });

  res.pipe(out).on('finish', function() {
    var tmp = fs.statSync(resultFile);
    assert.strictEqual(bytes, data.length);
    assert.strictEqual(tmp.size, data.length);
    console.log('ok');
    if (cb) cb();
  });
}

But it works with a synthetic write stream, so it looks like something to do with the FS write stream specifically.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions