-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs: allow position
parameter to be a BigInt
in read and readSync
#36190
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,6 @@ const { | |
BigIntPrototypeToString, | ||
MathMax, | ||
Number, | ||
NumberIsSafeInteger, | ||
ObjectCreate, | ||
ObjectDefineProperties, | ||
ObjectDefineProperty, | ||
|
@@ -75,7 +74,8 @@ const { | |
ERR_FS_FILE_TOO_LARGE, | ||
ERR_INVALID_ARG_VALUE, | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM | ||
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM, | ||
ERR_OUT_OF_RANGE, | ||
}, | ||
hideStackFrames, | ||
uvErrmapGet, | ||
|
@@ -545,9 +545,23 @@ function read(fd, buffer, offset, length, position, callback) { | |
|
||
validateOffsetLengthRead(offset, length, buffer.byteLength); | ||
|
||
if (!NumberIsSafeInteger(position)) | ||
if (position == null) | ||
position = -1; | ||
|
||
if (typeof position === 'number') { | ||
validateInteger(position, 'position'); | ||
} else if (typeof position === 'bigint') { | ||
if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) { | ||
throw new ERR_OUT_OF_RANGE('position', | ||
`>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`, | ||
position); | ||
} | ||
Comment on lines
+554
to
+558
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth put this in a function to avoid duplicating the code twice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Originally posted by @RaisinTen in #36190 (comment) Like this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure this can be done in a future PR :) |
||
} else { | ||
throw new ERR_INVALID_ARG_TYPE('position', | ||
['integer', 'bigint'], | ||
position); | ||
} | ||
|
||
function wrapper(err, bytesRead) { | ||
// Retain a reference to buffer so that it can't be GC'ed too soon. | ||
callback(err, bytesRead || 0, buffer); | ||
|
@@ -597,9 +611,23 @@ function readSync(fd, buffer, offset, length, position) { | |
|
||
validateOffsetLengthRead(offset, length, buffer.byteLength); | ||
|
||
if (!NumberIsSafeInteger(position)) | ||
if (position == null) | ||
position = -1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could the optimized to have a if (position == null) {
position = -1;
}
else if (typeof position === 'number') {
// ...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, that would work. Do you want to open a PR with this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aduh95 if possible, you can do it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually thinking about doing what was discussed here: #36190 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rentalhost see #37101. |
||
|
||
if (typeof position === 'number') { | ||
validateInteger(position, 'position'); | ||
} else if (typeof position === 'bigint') { | ||
if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) { | ||
throw new ERR_OUT_OF_RANGE('position', | ||
`>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`, | ||
position); | ||
} | ||
} else { | ||
throw new ERR_INVALID_ARG_TYPE('position', | ||
['integer', 'bigint'], | ||
position); | ||
} | ||
|
||
const ctx = {}; | ||
const result = binding.read(fd, buffer, offset, length, position, | ||
undefined, ctx); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily something to be done in this PR and I haven't looked super-closely but I wonder if this bit of code means that the docs shouldn't say the default is
null
but instead should say the default is-1
(and be sure to explain what-1
means and make sure that passing-1
explicitly works). Saying the thing has to be a an integer (or bigint, as added in this PR), but then saying the default is something that is not an integer or bigint (null
), seems confusing and not quite right.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I was also thinking about removing the
null
-check and using adefault function parameter
forposition
by setting it to-1
in the function prototype itself. Should I add that change?