Skip to content
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

fix: use stream helpers #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions guest.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

const duplexify = require('duplexify')
const { AbstractLevel, AbstractIterator } = require('abstract-level')
const eos = require('end-of-stream')
const lpstream = require('length-prefixed-stream')
const ModuleError = require('module-error')
const { input, output } = require('./tags')
const { Duplex, pipeline, finished } = require('stream')

const kExplicitClose = Symbol('explicitClose')
const kAbortRequests = Symbol('abortRequests')
Expand Down Expand Up @@ -108,10 +107,8 @@ class ManyLevelGuest extends AbstractLevel {
self[kFlushed]()
})

const proxy = duplexify()
proxy.setWritable(decode)
proxy.setReadable(encode)
eos(proxy, cleanup)
const proxy = Duplex.from({ writable: decode, readable: encode })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires node 16.8.0 and probably can't be browserified either because readable-stream is behind

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, on the browser point, perhaps it's time to let go of the old and build something web stream-based

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sad but true... to both.

Copy link
Contributor Author

@ronag ronag Mar 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use Duplex.from if available and otherwise fallback to duplexify?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things I want to do first:

  1. Benchmark Duplex.from, I didn't try that yet. And design a more suitable benchmark, because I've focused mainly on iterators so far, less on one-of operations like put() and get(). And swap level for memory-level to remove fs from the equation. And bypass net sockets.
  2. Check compat with length-prefixed-stream which uses readable-stream and therefore doesn't satisfy core's willEmitClose logic. And length-prefixed-stream has its own destroy() 😞. Not sure what the effect is though, if any. Can you help with that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need some time to think about which flavor of streams to go with. It's a difficult question.

Copy link
Contributor Author

@ronag ronag Mar 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would personally not use readable-stream or any of the non-core helpers in production. But that's just my opinion. IMHO They are basically unmaintained and have lots of bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indirectly that unfortunately includes any lib that uses them.

finished(proxy, cleanup)
this[kRpcStream] = proxy
return proxy

Expand Down Expand Up @@ -312,7 +309,7 @@ class ManyLevelGuest extends AbstractLevel {
this[kAbortRequests]('Aborted on database close()', 'LEVEL_DATABASE_NOT_OPEN')

if (this[kRpcStream]) {
eos(this[kRpcStream], () => {
finished(this[kRpcStream], () => {
this[kRpcStream] = null
this._close(cb)
})
Expand All @@ -330,7 +327,12 @@ class ManyLevelGuest extends AbstractLevel {
// For tests only so does not need error handling
this[kExplicitClose] = false
const remote = this[kRemote]()
remote.pipe(this.connect()).pipe(remote)
pipeline(
remote,
this.connect(),
remote,
() => {}
)
} else if (this[kExplicitClose]) {
throw new ModuleError('Cannot reopen many-level database after close()', {
code: 'LEVEL_NOT_SUPPORTED'
Expand Down
7 changes: 3 additions & 4 deletions host.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

const lpstream = require('length-prefixed-stream')
const ModuleError = require('module-error')
const eos = require('end-of-stream')
const duplexify = require('duplexify')
const { Duplex, finished } = require('stream')
const { input, output } = require('./tags')

const rangeOptions = new Set(['gt', 'gte', 'lt', 'lte'])
Expand Down Expand Up @@ -60,7 +59,7 @@ function createRpcStream (db, options, streamOptions) {
const readonly = options.readonly
const decode = lpstream.decode()
const encode = lpstream.encode()
const stream = duplexify(decode, encode)
const stream = Duplex.from({ writable: decode, readable: encode })

const preput = options.preput
const predel = options.predel
Expand All @@ -85,7 +84,7 @@ function createRpcStream (db, options, streamOptions) {

const iterators = new Map()

eos(stream, function () {
finished(stream, function () {
for (const iterator of iterators.values()) {
iterator.close()
}
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
],
"dependencies": {
"abstract-level": "^1.0.3",
"duplexify": "^4.1.1",
"end-of-stream": "^1.1.0",
"length-prefixed-stream": "^2.0.0",
"module-error": "^1.0.2",
"protocol-buffers-encodings": "^1.1.0"
Expand Down