-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Allow passing FileHandle objects via postMessage #33772
Changes from all commits
cbc35c3
915b90e
5f25ff9
b1aad92
877d631
758fe1a
82a2220
22e70e2
c74eeb2
a94759a
9f487ab
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 |
---|---|---|
|
@@ -65,13 +65,17 @@ const { promisify } = require('internal/util'); | |
const kHandle = Symbol('kHandle'); | ||
const kFd = Symbol('kFd'); | ||
const { kUsePromises } = binding; | ||
const { | ||
JSTransferable, kDeserialize, kTransfer, kTransferList | ||
} = require('internal/worker/js_transferable'); | ||
|
||
const getDirectoryEntriesPromise = promisify(getDirents); | ||
|
||
class FileHandle { | ||
class FileHandle extends JSTransferable { | ||
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. Does this have an impact on the memory consumption or size of 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. According to the inspector, this adds 32 bytes in total, which seems to make sense to me (1 × vpointer + 3 × pointer-size fields in For comparison, the 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.
That is actually pretty unfortunate to allocate a function per handle, but I guess maybe file handles are expected to be relatively expensive to hold? 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. @benjamingr I would love to see our objects become smaller in general, but I think it’s fine here. There’s also the native And as far as |
||
constructor(filehandle) { | ||
super(); | ||
this[kHandle] = filehandle; | ||
this[kFd] = filehandle.fd; | ||
this[kFd] = filehandle ? filehandle.fd : -1; | ||
} | ||
|
||
getAsyncId() { | ||
|
@@ -142,6 +146,26 @@ class FileHandle { | |
this[kFd] = -1; | ||
return this[kHandle].close(); | ||
} | ||
|
||
[kTransfer]() { | ||
const handle = this[kHandle]; | ||
this[kFd] = -1; | ||
this[kHandle] = null; | ||
|
||
return { | ||
data: { handle }, | ||
deserializeInfo: 'internal/fs/promises:FileHandle' | ||
}; | ||
} | ||
|
||
[kTransferList]() { | ||
return [ this[kHandle] ]; | ||
} | ||
|
||
[kDeserialize]({ handle }) { | ||
this[kHandle] = handle; | ||
this[kFd] = handle.fd; | ||
} | ||
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. @jasnell Happy to take suggestions on the API here. It’s a bit unfortunate that we can’t use the constructor and instead use this deserialization method to fill the object’s contents with the transferred data, but I don’t really know a way around this (See the comment in 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. This pattern is actually pretty common in JS for "hydrating" objects in my experience - though it is admittedly less neat than doing everything in the constructor and always getting a fully initialized object. 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'm good with this API. The one thing that I would be concerned about is whether we'll always be certain that 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 will always be called with the value in |
||
} | ||
|
||
function validateFileHandle(handle) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
const { Error } = primordials; | ||
const { | ||
messaging_deserialize_symbol, | ||
messaging_transfer_symbol, | ||
messaging_clone_symbol, | ||
messaging_transfer_list_symbol | ||
} = internalBinding('symbols'); | ||
const { | ||
JSTransferable, | ||
setDeserializerCreateObjectFunction | ||
} = internalBinding('messaging'); | ||
|
||
function setup() { | ||
// Register the handler that will be used when deserializing JS-based objects | ||
// from .postMessage() calls. The format of `deserializeInfo` is generally | ||
// 'module:Constructor', e.g. 'internal/fs/promises:FileHandle'. | ||
setDeserializerCreateObjectFunction((deserializeInfo) => { | ||
const [ module, ctor ] = deserializeInfo.split(':'); | ||
const Ctor = require(module)[ctor]; | ||
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. Should the module name be guarded with a whitelist here? I don't think it should since worker threads don't provide a sandbox but I just want to make sure that this isn't exposing a new attack surface somehow. 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. @benjamingr Hm yeah … this isn’t extending capabilities of the calling code in any way, but I agree that it might be good to be careful here. I’ll think about this a bit more. 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. @benjamingr I’ve updated this PR so that this function now verifies that the target constructor is a subclass of I think that’s as “locked down” as this can get. If user code makes other internal classes inherit from The one thing I could see happen would be side effects from What do you think? 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. Better :) 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.
Fwiw that's the part that initially bugged me, but like we both said it doesn't increase the attack surface at all from what I can tell. |
||
if (typeof Ctor !== 'function' || | ||
!(Ctor.prototype instanceof JSTransferable)) { | ||
// Not one of the official errors because one should not be able to get | ||
// here without messing with Node.js internals. | ||
// eslint-disable-next-line no-restricted-syntax | ||
throw new Error(`Unknown deserialize spec ${deserializeInfo}`); | ||
} | ||
return new Ctor(); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
setup, | ||
JSTransferable, | ||
kClone: messaging_clone_symbol, | ||
kDeserialize: messaging_deserialize_symbol, | ||
kTransfer: messaging_transfer_symbol, | ||
kTransferList: messaging_transfer_list_symbol | ||
}; |
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.
In the future, we might want to consider marking transferable objects with a special static read-only symbol property so we don't actually have to maintain this list. For instance, something like:
Then we wouldn't necessarily have to maintain a list of such objects in the docs, we would just say that
value
may be any object where[transferableSymbol] === true
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.
@jasnell I think that’s a good idea, but I do think it’s important to list the built-in types that are transferable here.
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.
Agreed, but I think we could put those in one place rather than having them listed in a couple different places (they're currently listed in the
value
and thetransferList
, etc)