#pragma once #include #include #include #include #include #include "asyncSignal.h" #include "pump.h" namespace sevenzip { /** * Exposes one archive entry as a pull-based JavaScript reader, opened on the libuv pool and backed by a decoder thread. * * Each read lends the producer a fresh JavaScript buffer to fill and resolves * with it once it is full, without blocking the event loop. Under Emscripten, * each read lends reused scratch memory instead and resolves with a copy of it. * A read pauses until the producer wakes it through AsyncSignal; only the * producer may block, while no buffer is lent. */ class EntryReader : public Napi::ObjectWrap { public: // The chunk size when the caller names none, matching the default // highWaterMark of a Node.js byte stream static constexpr size_t kDefaultChunkBytes = 1U << 16U; // 64 KiB // The largest chunk size. Each read allocates a buffer of the chunk size, and Node.js 22 // aborts the process when it cannot allocate one instead of throwing. This bound is far past // any useful chunk size, and small enough to allocate on 32-bit targets. static constexpr size_t kMaxChunkBytes = 64U << 20U; // 64 MiB /** Defines the JavaScript EntryReader class and its read and close methods. */ static Napi::Function GetClass(Napi::Env env); /** Validates `(path, formatIndex, entryPath?, entryIndex?, chunkBytes?)` and starts the decoder pump. */ explicit EntryReader(const Napi::CallbackInfo& info); /** Detaches callbacks and cancels any decoder still running without blocking the loop. */ ~EntryReader() override; /** Reader identity and its JavaScript wrapper cannot be copied. */ EntryReader(const EntryReader&) = delete; /** Reader identity and its JavaScript wrapper cannot be copy-assigned. */ EntryReader& operator=(const EntryReader&) = delete; /** Reader identity and its JavaScript wrapper cannot be moved. */ EntryReader(EntryReader&&) = delete; /** Reader identity and its JavaScript wrapper cannot be move-assigned. */ EntryReader& operator=(EntryReader&&) = delete; /** * Returns a promise for a full decompressed chunk, a shorter final chunk, * or null at end-of-stream; rejects native failure, concurrent reads, and reads after close. */ Napi::Value Read(const Napi::CallbackInfo& info); /** * Idempotently cancels, resolves a paused read with end-of-stream, and returns a promise that * resolves once the producer has closed every file the archive opened (like Node.js * fs.close()). */ Napi::Value Close(const Napi::CallbackInfo& info); /** * Retries a paused read, or resolves a pending close once the producer has exited, on the event * loop; public for the native callback, not the JavaScript surface. */ void OnProducerReady(Napi::Env env); private: /** * Lets the reader and producer outlive one another through a shared signal * while keeping the JavaScript wrapper pointer confined to the loop thread. */ struct Bridge { std::shared_ptr signal; // Touched only on the event loop thread: set at construction, cleared by // ~EntryReader, and read by the AsyncSignal callback, all three // on that one thread, so a reader that is gone is simply seen as null // rather than raced with EntryReader* reader = nullptr; // Set by the producer once it has closed every file and will touch nothing else, before // the notification that reports it; read on the event loop thread std::atomic exited{false}; }; /** * Attempts one nonblocking pump read and resolves or rejects `deferred` when * possible. Returns false and rearms notification while pending; sets * `finished` to prevent callers from resolving or rejecting the promise twice. */ bool TryFinishRead(Napi::Env env, const Napi::Promise::Deferred& deferred, bool* finished); /** * The bytes left of the entry's declared size, or nothing until the Pump has read that size, * when the format records none, or once the entry has outgrown it */ [[nodiscard]] std::optional Remaining() const; /** Releases the lent memory; only while nothing is lent, or after the Pump is cancelled. */ void ReleaseLent(); /** Releases a paused read's object and loop references; must be last because it may destroy `this`. */ void ReleasePending(Napi::Env env); /** * Resolves every close() waiting on the producer, if it has exited, and releases their object * and loop references; must be last because it may destroy `this`. */ void FinishClose(Napi::Env env); std::shared_ptr pump_; size_t chunkBytes_ = kDefaultChunkBytes; // The bytes lent for the read in flight, at most chunkBytes_ size_t lentBytes_ = 0; // The bytes every read so far has resolved with uint64_t delivered_ = 0; #ifdef __EMSCRIPTEN__ // The memory lent to the producer for each read, which the read copies into // a Buffer. Lending Buffers would exhaust the WebAssembly heap, because // emnapi frees their memory only on garbage collection. std::unique_ptr scratch_; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) #else // The buffer lent to the producer for the read in flight, held so that V8 // cannot collect it while the producer is writing into it. Released only // once the slot has handed it back, or after the Pump is cancelled. Napi::Reference > lent_; #endif Napi::ObjectReference readFailure_; std::shared_ptr bridge_; // The one outstanding read, if it could not be answered immediately. Also // the "a read is in flight" flag, since there is only ever one. std::optional pending_; // Every close() still waiting for the producer to exit. Each call gets its own promise, because // a promise value is only valid within the call that created it. std::vector closeWaiters_; // False until the constructor has run to completion. Its argument checks // report a TypeError and return, leaving no Pump and no bridge; a caller // that held on to the half-built object must be told the reader was never // opened, not handed the empty read a null Pump would otherwise look like. bool constructed_ = false; bool closed_ = false; }; } // namespace sevenzip