JamesTheAwesomeDude
JamesTheAwesomeDude

Reputation: 1053

File.length greater than 8 pebibytes?

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.)

Upvotes: -2

Views: 112

Answers (1)

Bergi
Bergi

Reputation: 664494

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.

Upvotes: 2

Related Questions