diff options
author | Anna Henningsen <anna@addaleax.net> | 2020-06-06 17:27:30 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-06-14 15:53:40 +0300 |
commit | 8641d94189398063b20d5e38549bfd8f023af2d6 (patch) | |
tree | ecb18fb2e97803c51b6b5105361a3e4fb0a5904d /lib/internal/fs | |
parent | 9129cf21ab51432675521ea158191ae3f866cafb (diff) |
worker,fs: make FileHandle transferable
Allow passing `FileHandle` instances in the transfer list
of a `.postMessage()` call.
PR-URL: https://github.com/nodejs/node/pull/33772
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'lib/internal/fs')
-rw-r--r-- | lib/internal/fs/promises.js | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 05d82991552..717b26e59fa 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -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 { 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; + } } function validateFileHandle(handle) { |