-2

How does JavaScript handle it when a Blob or File has a length greater than Number.MAX_SAFE_INTEGER bytes (8 PiB; 9 PB)?

let file = await filePrompt("Please upload a ten petabyte file.");
let len = file.byteLength;
for ( let i = ((len <= Number.MAX_SAFE_INTEGER) ? 0 : BigInt(0)) ; i < len ; i++ ) {
    …
}

In the above code, for example, will typeof len === 'bigint'?

(I do not have access to a runtime environment with enough resources to test this.)

  • you have a 9 million gig file? how is that even stored? – Dan Jun 29 '22 at 21:43
  • In what context is your code running? Browser or NodeJS? – Abir Taheer Jun 29 '22 at 21:47
  • Are there actually any filesystems that are able to hold files this size? This seems like a theoretical problem that can't actually exist. – Barmar Jun 29 '22 at 22:18
  • Also, think about how long that upload is going to take. – Barmar Jun 29 '22 at 22:23
  • 1
    "*I do not have access to a runtime environment with enough resources to test this*" - few people will have a system where they can actually store petabytes of data. However, that shouldn't stop you from creating an empty file that just has such a humongous size: [windows](https://stackoverflow.com/q/982659/1048572) [linux](https://stackoverflow.com/q/139261/1048572) [macos](https://stackoverflow.com/q/26796729/1048572) – Bergi Jun 29 '22 at 22:40
  • @Bergi True as to the file _on-filesystem_... though I do not think FS-holes will survive the translation over to a RAM-stored `File` or `ArrayBuffer` – JamesTheAwesomeDude Jun 29 '22 at 22:45
  • 1
    @JamesTheAwesomeDude an arraybuffer… no forget it. But I don't think a `File` is keeping its contents in ram already - you have to use a `FileReader` to read it, which I would assume does read it from disk. And it should be possible to [`.stream()` it](https://developer.mozilla.org/en-US/docs/Web/API/Blob/stream)… – Bergi Jun 29 '22 at 22:48
  • @Bergi Alas… `dd: failed to truncate to 9007199254740992 bytes in output file '8PiB.bin': File too large`; `fallocate: fallocate failed: File too large` – JamesTheAwesomeDude Jul 11 '22 at 17:33

1 Answers1

2

The internal representation of a File size is an unsigned long long. For exposing this to JavaScript, the WebIDL spec says:

The result of converting an IDL unsigned long long value to an ECMAScript value is a Number value that represents the closest numeric value to the unsigned long long, choosing the numeric value with an even significand if there are two equally close values. If the unsigned long long is less than or equal to 253 − 1, then the Number will be able to represent exactly the same value as the unsigned long long.

So yes, it might lose precision. It will not produce a BigInt in those cases.

However, I am reasonably certain that most browsers will fail to open such a file in the first place, and will throw an exception instead.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375