Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

stream_base: introduce StreamBase

StreamBase is a brand new way to write C++ streams. The class itself is for separting StreamWrap (with the methods like .writeAsciiString, .writeBuffer, .writev, etc) from the HandleWrap class, making possible writing abstract C++ streams that are not bound to any uv socket.

Important part of the abstraction is following methods (which mimics libuv's stream API):

  • Events:
    • OnAlloc(size_t size, uv_buf_t*)
    • OnRead(ssize_t nread, const uv_buf_t*, uv_handle_type pending)
    • OnAfterWrite(WriteWrap*)
  • Wrappers:
    • DoShutdown(ShutdownWrap*)
    • DoTryWrite(uv_buf_t** bufs, size_t* count)
    • DoWrite(WriteWrap*, uv_buf_t*, size_t count, uv_stream_t* handle)
    • Error()
    • ClearError()

The implementation should provide all of these methods, thus providing the access to the underlying resource (be it uv handle, TLS socket, or anything else).

A C++ stream may consume input of other stream by replacing the event callbacks and proxying the writes. This kind of API is actually used now for the TLSWrap implementation, making possible to wrap TLS stream into another TLS stream. Thus legacy API calls are no longer required in _tls_wrap.js.


Reviewed-By: Trevor Norris

Reviewed-By: Chris Dickinson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment