Git Product home page Git Product logo

write's Issues

Unkown encoding "BBB"

In the master branch, the code is using fs.writeFileSync("file.txt", {}, "BBB");, but the node docs says that the signature is fs.writeFileSync("file.txt", "BBB").

Am I missing something?

Using: node v5.7.0.

Possible race condition if 2 calls write to the same file

I think there is a race condition when 2 write() calls write to the same file at almost the same time, like the test case below:

const targetFile = path.join(os.tmpdir(), 'test.txt');

fs.rmSync(targetFile, {force: true});

const content1 = Buffer.alloc(100 * 1024 * 1024); // A will finish writing later than B
const content2 = Buffer.alloc(50 * 1024 * 1024);

crypto.randomFillSync(content1);
crypto.randomFillSync(content2);

process.on('uncaughtException', (e) =>
{
    console.error(e);
});

process.on('beforeExit', () =>
{
    fs.rmSync(targetFile, {force: true});
});

// A
write(targetFile, content1, {overwrite: true}, err =>
{
    if (err)
    {
        console.error(err);
    }
    else
    {
        const targetContent = fs.readFileSync(targetFile);
        assert.ok(targetContent.equals(content1), `The content of targetFile is not content1!`);
    }
});


// B
write(targetFile, content2, {overwrite: true}, err =>
{
    if (err)
    {
        console.error(err);
    }
    else
    {
        const targetContent = fs.readFileSync(targetFile);
        assert.ok(targetContent.equals(content2), `The content of targetFile is not content2!`);
    }
});

And the execution result is:

AssertionError [ERR_ASSERTION]: The content of targetFile is not content2!
...
AssertionError [ERR_ASSERTION]: The content of targetFile is not content1!
...

targetFile may become a corrupted file with content different from either content1 or content2.

I think the bug is caused by the use of stream:

write/index.js

Lines 58 to 61 in f537eb6

fs.createWriteStream(destpath, opts)
.on('error', err => reject(err))
.on('close', resolve)
.end(ensureNewline(data, opts));

Both A and B can create write streams and write to targetFile at the same time. I think write() should check whether a file is being written before writing to it.

Not working with node 8

When I call the write function and give it a directory that doesn't exist, I get this error.

Error: ENOENT: no such file or directory, open '<project_dir>/<directory_that_doesn't_exist>/<file>'

node version: 8.17.0
write version: 2.0.0

Suggestion

Because node 8 does not support the recursive option (used here) you could use something like sindresorhus/make-dir which only uses the recursive option if the node version is >=10.12.0 (sindresorhus/make-dir/blob/master/index.js#L12).

Module not found: Can't resolve 'fs'

error - ./node_modules/add-filename-increment/index.js:3:0
Module not found: Can't resolve 'fs'

Import trace for requested module:
./node_modules/write/index.js

I get this error when I try to use write. npm says the fs plugin has been removed.

`increment` doesn't work as expected

Hello!

I've found this issue when tried to use the increment option. In the empty folder I expect the following code to create foo.txt file:

write.sync('foo.txt', 'some data', { increment: true });

But instead it creates foo (2).txt on Windows platform. I dug into the sources and found the fs option in add-filename-increment library. The following code will work correctly:

write.sync('foo.txt', 'some data', {
  increment: true,
  fs: true
});

Uncaught TypeError: xfs.mkdir is not a function

Uncaught TypeError: xfs.mkdir is not a function index.js?0191:27

I'm using it as per the docs

      writeFile('Report.csv', csv, function(err) {
        if (err) throw err;
      });

csv is a valid object using the json2csv module

Not sure what I'm missing here.
Could be related to #3 ?

Namespace '"fs"' has no exported members - Error

I am running Node 12.14.1 and when trying to build my typescript project, your module throw the following errors:

"Namespace '"fs"' has no exported member 'WriteFileOptions'."
"Namespace '"fs"' has no exported member 'MakeDirectoryOptions'."

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.