| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9692208 commit 5b1fd10
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -318,6 +318,10 @@ are part of the channel. | |||
| 318 | 318 | ### `port.postMessage(value[, transferList])` | |
| 319 | 319 | <!-- YAML | |
| 320 | 320 | added: v10.5.0 | |
| 321 | + changes: | ||
| 322 | + - version: REPLACEME | ||
| 323 | + pr-url: https://github.com/nodejs/node/pull/33772 | ||
| 324 | + description: Added `FileHandle` to the list of transferable types. | ||
| 321 | 325 | --> | |
| 322 | 326 | ||
| 323 | 327 | * `value` {any} | |
@@ -335,7 +339,8 @@ In particular, the significant differences to `JSON` are: | |||
| 335 | 339 | * `value` may contain typed arrays, both using `ArrayBuffer`s | |
| 336 | 340 | and `SharedArrayBuffer`s. | |
| 337 | 341 | * `value` may contain [`WebAssembly.Module`][] instances. | |
| 338 | - * `value` may not contain native (C++-backed) objects other than `MessagePort`s. | ||
| 342 | + * `value` may not contain native (C++-backed) objects other than `MessagePort`s | ||
| 343 | + and [`FileHandle`][]s. | ||
| 339 | 344 | ||
| 340 | 345 | ```js | |
| 341 | 346 | const { MessageChannel } = require('worker_threads'); | |
@@ -349,7 +354,8 @@ circularData.foo = circularData; | |||
| 349 | 354 | port2.postMessage(circularData); | |
| 350 | 355 | ``` | |
| 351 | 356 | ||
| 352 | - `transferList` may be a list of `ArrayBuffer` and `MessagePort` objects. | ||
| 357 | + `transferList` may be a list of [`ArrayBuffer`][], [`MessagePort`][] and | ||
| 358 | + [`FileHandle`][] objects. | ||
| 353 | 359 | After transferring, they will not be usable on the sending side of the channel | |
| 354 | 360 | anymore (even if they are not contained in `value`). Unlike with | |
| 355 | 361 | [child processes][], transferring handles such as network sockets is currently | |
@@ -810,13 +816,15 @@ active handle in the event system. If the worker is already `unref()`ed calling | |||
| 810 | 816 | ||
| 811 | 817 | [`'close'` event]: #worker_threads_event_close | |
| 812 | 818 | [`'exit'` event]: #worker_threads_event_exit | |
| 819 | + [`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer | ||
| 813 | 820 | [`AsyncResource`]: async_hooks.html#async_hooks_class_asyncresource | |
| 814 | 821 | [`Buffer`]: buffer.html | |
| 815 | 822 | [`Buffer.allocUnsafe()`]: buffer.html#buffer_class_method_buffer_allocunsafe_size | |
| 816 | 823 | [`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`]: errors.html#errors_err_missing_message_port_in_transfer_list | |
| 817 | 824 | [`ERR_WORKER_NOT_RUNNING`]: errors.html#ERR_WORKER_NOT_RUNNING | |
| 818 | 825 | [`EventEmitter`]: events.html | |
| 819 | 826 | [`EventTarget`]: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget | |
| 827 | + [`FileHandle`]: fs.html#fs_class_filehandle | ||
| 820 | 828 | [`MessagePort`]: #worker_threads_class_messageport | |
| 821 | 829 | [`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer | |
| 822 | 830 | [`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,13 +65,17 @@ const { promisify } = require('internal/util'); | |||
| 65 | 65 | const kHandle = Symbol('kHandle'); | |
| 66 | 66 | const kFd = Symbol('kFd'); | |
| 67 | 67 | const { kUsePromises } = binding; | |
| 68 | + const { | ||
| 69 | + JSTransferable, kDeserialize, kTransfer, kTransferList | ||
| 70 | + } = require('internal/worker/js_transferable'); | ||
| 68 | 71 | ||
| 69 | 72 | const getDirectoryEntriesPromise = promisify(getDirents); | |
| 70 | 73 | ||
| 71 | - class FileHandle { | ||
| 74 | + class FileHandle extends JSTransferable { | ||
| 72 | 75 | constructor(filehandle) { | |
| 76 | + super(); | ||
| 73 | 77 | this[kHandle] = filehandle; | |
| 74 | - this[kFd] = filehandle.fd; | ||
| 78 | + this[kFd] = filehandle ? filehandle.fd : -1; | ||
| 75 | 79 | } | |
| 76 | 80 | ||
| 77 | 81 | getAsyncId() { | |
@@ -142,6 +146,26 @@ class FileHandle { | |||
| 142 | 146 | this[kFd] = -1; | |
| 143 | 147 | return this[kHandle].close(); | |
| 144 | 148 | } | |
| 149 | + | ||
| 150 | + [kTransfer]() { | ||
| 151 | + const handle = this[kHandle]; | ||
| 152 | + this[kFd] = -1; | ||
| 153 | + this[kHandle] = null; | ||
| 154 | + | ||
| 155 | + return { | ||
| 156 | + data: { handle }, | ||
| 157 | + deserializeInfo: 'internal/fs/promises:FileHandle' | ||
| 158 | + }; | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + [kTransferList]() { | ||
| 162 | + return [ this[kHandle] ]; | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + [kDeserialize]({ handle }) { | ||
| 166 | + this[kHandle] = handle; | ||
| 167 | + this[kFd] = handle.fd; | ||
| 168 | + } | ||
| 145 | 169 | } | |
| 146 | 170 | ||
| 147 | 171 | function validateFileHandle(handle) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | + const { Error } = primordials; | ||
| 2 | 3 | const { | |
| 3 | 4 | messaging_deserialize_symbol, | |
| 4 | 5 | messaging_transfer_symbol, | |
@@ -17,6 +18,13 @@ function setup() { | |||
| 17 | 18 | setDeserializerCreateObjectFunction((deserializeInfo) => { | |
| 18 | 19 | const [ module, ctor ] = deserializeInfo.split(':'); | |
| 19 | 20 | const Ctor = require(module)[ctor]; | |
| 21 | + if (typeof Ctor !== 'function' || | ||
| 22 | + !(Ctor.prototype instanceof JSTransferable)) { | ||
| 23 | + // Not one of the official errors because one should not be able to get | ||
| 24 | + // here without messing with Node.js internals. | ||
| 25 | + // eslint-disable-next-line no-restricted-syntax | ||
| 26 | + throw new Error(`Unknown deserialize spec ${deserializeInfo}`); | ||
| 27 | + } | ||
| 20 | 28 | return new Ctor(); | |
| 21 | 29 | }); | |
| 22 | 30 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -190,6 +190,40 @@ void FileHandle::MemoryInfo(MemoryTracker* tracker) const { | |||
| 190 | 190 | tracker->TrackField("current_read", current_read_); | |
| 191 | 191 | } | |
| 192 | 192 | ||
| 193 | + FileHandle::TransferMode FileHandle::GetTransferMode() const { | ||
| 194 | + return reading_ || closing_ || closed_ ? | ||
| 195 | + TransferMode::kUntransferable : TransferMode::kTransferable; | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + std::unique_ptr<worker::TransferData> FileHandle::TransferForMessaging() { | ||
| 199 | + CHECK_NE(GetTransferMode(), TransferMode::kUntransferable); | ||
| 200 | + auto ret = std::make_unique<TransferData>(fd_); | ||
| 201 | + closed_ = true; | ||
| 202 | + return ret; | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + FileHandle::TransferData::TransferData(int fd) : fd_(fd) {} | ||
| 206 | + | ||
| 207 | + FileHandle::TransferData::~TransferData() { | ||
| 208 | + if (fd_ > 0) { | ||
| 209 | + uv_fs_t close_req; | ||
| 210 | + CHECK_EQ(0, uv_fs_close(nullptr, &close_req, fd_, nullptr)); | ||
| 211 | + uv_fs_req_cleanup(&close_req); | ||
| 212 | + } | ||
| 213 | + } | ||
| 214 | + | ||
| 215 | + BaseObjectPtr<BaseObject> FileHandle::TransferData::Deserialize( | ||
| 216 | + Environment* env, | ||
| 217 | + v8::Local<v8::Context> context, | ||
| 218 | + std::unique_ptr<worker::TransferData> self) { | ||
| 219 | + BindingData* bd = Environment::GetBindingData<BindingData>(context); | ||
| 220 | + if (bd == nullptr) return {}; | ||
| 221 | + | ||
| 222 | + int fd = fd_; | ||
| 223 | + fd_ = -1; | ||
| 224 | + return BaseObjectPtr<BaseObject> { FileHandle::New(bd, fd) }; | ||
| 225 | + } | ||
| 226 | + | ||
| 193 | 227 | // Close the file descriptor if it hasn't already been closed. A process | |
| 194 | 228 | // warning will be emitted using a SetImmediate to avoid calling back to | |
| 195 | 229 | // JS during GC. If closing the fd fails at this point, a fatal exception | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | 6 | #include "node.h" | |
| 7 | 7 | #include "aliased_buffer.h" | |
| 8 | + #include "node_messaging.h" | ||
| 8 | 9 | #include "stream_base.h" | |
| 9 | 10 | #include <iostream> | |
| 10 | 11 | ||
@@ -273,7 +274,28 @@ class FileHandle final : public AsyncWrap, public StreamBase { | |||
| 273 | 274 | FileHandle(const FileHandle&&) = delete; | |
| 274 | 275 | FileHandle& operator=(const FileHandle&&) = delete; | |
| 275 | 276 | ||
| 277 | + TransferMode GetTransferMode() const override; | ||
| 278 | + std::unique_ptr<worker::TransferData> TransferForMessaging() override; | ||
| 279 | + | ||
| 276 | 280 | private: | |
| 281 | + class TransferData : public worker::TransferData { | ||
| 282 | + public: | ||
| 283 | + explicit TransferData(int fd); | ||
| 284 | + ~TransferData(); | ||
| 285 | + | ||
| 286 | + BaseObjectPtr<BaseObject> Deserialize( | ||
| 287 | + Environment* env, | ||
| 288 | + v8::Local<v8::Context> context, | ||
| 289 | + std::unique_ptr<worker::TransferData> self) override; | ||
| 290 | + | ||
| 291 | + SET_NO_MEMORY_INFO() | ||
| 292 | + SET_MEMORY_INFO_NAME(FileHandleTransferData) | ||
| 293 | + SET_SELF_SIZE(TransferData) | ||
| 294 | + | ||
| 295 | + private: | ||
| 296 | + int fd_; | ||
| 297 | + }; | ||
| 298 | + | ||
| 277 | 299 | FileHandle(BindingData* binding_data, v8::Local<v8::Object> obj, int fd); | |
| 278 | 300 | ||
| 279 | 301 | // Synchronous close that emits a warning | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const fs = require('fs').promises; | ||
| 5 | + const { MessageChannel } = require('worker_threads'); | ||
| 6 | + const { once } = require('events'); | ||
| 7 | + | ||
| 8 | + // Test that overriding the internal kTransfer method of a JSTransferable does | ||
| 9 | + // not enable loading arbitrary code from internal Node.js core modules. | ||
| 10 | + | ||
| 11 | + (async function() { | ||
| 12 | + const fh = await fs.open(__filename); | ||
| 13 | + assert.strictEqual(fh.constructor.name, 'FileHandle'); | ||
| 14 | + | ||
| 15 | + const kTransfer = Object.getOwnPropertySymbols(Object.getPrototypeOf(fh)) | ||
| 16 | + .filter((symbol) => symbol.description === 'messaging_transfer_symbol')[0]; | ||
| 17 | + assert.strictEqual(typeof kTransfer, 'symbol'); | ||
| 18 | + fh[kTransfer] = () => { | ||
| 19 | + return { | ||
| 20 | + data: '✨', | ||
| 21 | + deserializeInfo: 'net:Socket' | ||
| 22 | + }; | ||
| 23 | + }; | ||
| 24 | + | ||
| 25 | + const { port1, port2 } = new MessageChannel(); | ||
| 26 | + port1.postMessage(fh, [ fh ]); | ||
| 27 | + port2.on('message', common.mustNotCall()); | ||
| 28 | + | ||
| 29 | + const [ exception ] = await once(process, 'uncaughtException'); | ||
| 30 | + | ||
| 31 | + assert.strictEqual(exception.message, 'Unknown deserialize spec net:Socket'); | ||
| 32 | + port2.close(); | ||
| 33 | + })().then(common.mustCall()); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const fs = require('fs').promises; | ||
| 5 | + const { MessageChannel } = require('worker_threads'); | ||
| 6 | + const { once } = require('events'); | ||
| 7 | + | ||
| 8 | + // Test that overriding the internal kTransfer method of a JSTransferable does | ||
| 9 | + // not enable loading arbitrary code from the disk. | ||
| 10 | + | ||
| 11 | + module.exports = { | ||
| 12 | + NotARealClass: common.mustNotCall() | ||
| 13 | + }; | ||
| 14 | + | ||
| 15 | + (async function() { | ||
| 16 | + const fh = await fs.open(__filename); | ||
| 17 | + assert.strictEqual(fh.constructor.name, 'FileHandle'); | ||
| 18 | + | ||
| 19 | + const kTransfer = Object.getOwnPropertySymbols(Object.getPrototypeOf(fh)) | ||
| 20 | + .filter((symbol) => symbol.description === 'messaging_transfer_symbol')[0]; | ||
| 21 | + assert.strictEqual(typeof kTransfer, 'symbol'); | ||
| 22 | + fh[kTransfer] = () => { | ||
| 23 | + return { | ||
| 24 | + data: '✨', | ||
| 25 | + deserializeInfo: `${__filename}:NotARealClass` | ||
| 26 | + }; | ||
| 27 | + }; | ||
| 28 | + | ||
| 29 | + const { port1, port2 } = new MessageChannel(); | ||
| 30 | + port1.postMessage(fh, [ fh ]); | ||
| 31 | + port2.on('message', common.mustNotCall()); | ||
| 32 | + | ||
| 33 | + const [ exception ] = await once(process, 'uncaughtException'); | ||
| 34 | + | ||
| 35 | + assert.match(exception.message, /Missing internal module/); | ||
| 36 | + port2.close(); | ||
| 37 | + })().then(common.mustCall()); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,65 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const fs = require('fs').promises; | ||
| 5 | + const vm = require('vm'); | ||
| 6 | + const { MessageChannel, moveMessagePortToContext } = require('worker_threads'); | ||
| 7 | + const { once } = require('events'); | ||
| 8 | + | ||
| 9 | + (async function() { | ||
| 10 | + const fh = await fs.open(__filename); | ||
| 11 | + | ||
| 12 | + const { port1, port2 } = new MessageChannel(); | ||
| 13 | + | ||
| 14 | + assert.throws(() => { | ||
| 15 | + port1.postMessage(fh); | ||
| 16 | + }, { | ||
| 17 | + // See the TODO about error code in node_messaging.cc. | ||
| 18 | + code: 'ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST' | ||
| 19 | + }); | ||
| 20 | + | ||
| 21 | + // Check that transferring FileHandle instances works. | ||
| 22 | + assert.notStrictEqual(fh.fd, -1); | ||
| 23 | + port1.postMessage(fh, [ fh ]); | ||
| 24 | + assert.strictEqual(fh.fd, -1); | ||
| 25 | + | ||
| 26 | + const [ fh2 ] = await once(port2, 'message'); | ||
| 27 | + assert.strictEqual(Object.getPrototypeOf(fh2), Object.getPrototypeOf(fh)); | ||
| 28 | + | ||
| 29 | + assert.deepStrictEqual(await fh2.readFile(), await fs.readFile(__filename)); | ||
| 30 | + await fh2.close(); | ||
| 31 | + | ||
| 32 | + assert.rejects(() => fh.readFile(), { code: 'EBADF' }); | ||
| 33 | + })().then(common.mustCall()); | ||
| 34 | + | ||
| 35 | + (async function() { | ||
| 36 | + // Check that there is no crash if the message is never read. | ||
| 37 | + const fh = await fs.open(__filename); | ||
| 38 | + | ||
| 39 | + const { port1 } = new MessageChannel(); | ||
| 40 | + | ||
| 41 | + assert.notStrictEqual(fh.fd, -1); | ||
| 42 | + port1.postMessage(fh, [ fh ]); | ||
| 43 | + assert.strictEqual(fh.fd, -1); | ||
| 44 | + })().then(common.mustCall()); | ||
| 45 | + | ||
| 46 | + (async function() { | ||
| 47 | + // Check that in the case of a context mismatch the message is discarded. | ||
| 48 | + const fh = await fs.open(__filename); | ||
| 49 | + | ||
| 50 | + const { port1, port2 } = new MessageChannel(); | ||
| 51 | + | ||
| 52 | + const ctx = vm.createContext(); | ||
| 53 | + const port2moved = moveMessagePortToContext(port2, ctx); | ||
| 54 | + port2moved.onmessage = common.mustCall((msgEvent) => { | ||
| 55 | + assert.strictEqual(msgEvent.data, 'second message'); | ||
| 56 | + port1.close(); | ||
| 57 | + }); | ||
| 58 | + port2moved.start(); | ||
| 59 | + | ||
| 60 | + assert.notStrictEqual(fh.fd, -1); | ||
| 61 | + port1.postMessage(fh, [ fh ]); | ||
| 62 | + assert.strictEqual(fh.fd, -1); | ||
| 63 | + | ||
| 64 | + port1.postMessage('second message'); | ||
| 65 | + })().then(common.mustCall()); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments