Git Product home page Git Product logo

Comments (5)

AlttiRi avatar AlttiRi commented on August 20, 2024

By the way, I expected that it will not support ADS (Alternate Data Streams) too, but it works fine.

import fs from "node:fs/promises";

await fs.writeFile("ads.txt", `data:ads.txt`);
await fs.writeFile("ads.txt:metadata", `metadata:ads.txt`); // Alternate Data Stream

console.log((await fs.readFile("ads.txt")).toString());
console.log((await fs.readFile("ads.txt:metadata")).toString());
console.log((await fs.stat("ads.txt:metadata")).size);

await fs.unlink("ads.txt");
data:ads.txt
metadata:ads.txt
16

Anyway, it makes sense to add a test for it too in fs.test.ts, just in case.

from bun.

AlttiRi avatar AlttiRi commented on August 20, 2024

It's strange, but adding \\?\ does not help. While it should help.

https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#skip-normalization

Unless the path starts exactly with \\?\ (note the use of the canonical backslash), it is normalized.

Why would you want to skip normalization? There are three major reasons:

  1. To get access to paths that are normally unavailable but are legal. A file or directory called hidden., for example, is impossible to access in any other way.
  2. To improve performance by skipping normalization if you've already normalized.
  3. To skip the MAX_PATH check for path length to allow for paths that are greater than 259 characters.
import fs from "node:fs/promises";
import path from "node:path";

for (const filename of [" spaces.txt ", ".dots.txt."]) {
    // const prefixed_filename = path.toNamespacedPath(path.resolve(filename)); // still does not work #8247
    const prefixed_filename = "\\\\?\\" + path.resolve(filename);
    console.log(prefixed_filename);

    try {
        await fs.writeFile(prefixed_filename, `data:"${filename}"`);
    } catch (e) { console.error(e); }

    try {
        console.log((await fs.readFile(prefixed_filename)).toString());
    } catch (e) { console.error(e); }

    try {
        console.log((await fs.stat(prefixed_filename)).size);
    } catch (e) { console.error(e); }

    try {
        await fs.unlink(filename);
        continue;
    } catch (e) { console.error(e); }

    try {
        const newName = filename.replaceAll(/^[ .]+|[ .]+$/g, "");
        await fs.rename(prefixed_filename, newName);
    } catch (e) { console.error(e); }
}

More over, in this case fs.writeFile fails too.


In this case the prefix works:

from bun.

AlttiRi avatar AlttiRi commented on August 20, 2024

Well, this works in Bun:

import fs from "node:fs/promises";
import path from "node:path";

for (const filename of [" spaces.txt ", ".dots.txt."]) {
    try {
        await fs.writeFile(filename, `data:"${filename}"`);
    } catch (e) { console.error(e); }

    const prefixed_filename = "\\\\?\\" + path.resolve(filename);
    console.log(prefixed_filename);

    try {
        console.log((await fs.readFile(prefixed_filename)).toString());
    } catch (e) { console.error(e); }

    try {
        console.log((await fs.stat(prefixed_filename)).size);
    } catch (e) { console.error(e); }

    try {
        await fs.unlink(filename);
        continue;
    } catch (e) { console.error(e); }

    try {
        const newName = filename.replaceAll(/^[ .]+|[ .]+$/g, "");
        await fs.rename(prefixed_filename, newName);
    } catch (e) { console.error(e); }
}

from bun.

AlttiRi avatar AlttiRi commented on August 20, 2024

fs.writeFile works without "\\\\?\\", but with the prefix it stops to work.
Other functions begin to work after adding of this prefix.
All examples above works in Node.js (regardless of the presence of the prefix).

from bun.

paperdave avatar paperdave commented on August 20, 2024

const prefixed_filename = "\\\\?\\" + path.resolve(filename);
is a simplified version of toNamespacedPath

i think once the path pr merges it might be worthwhile to use that api internally for all the fs stuff on windows. it probably should replace PosixToWinNormalizer, which is only really hitting SOME of the cases you present, and is why this only half works.

from bun.

Related Issues (20)

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.