-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
chore: es5 to es6, drop nodejs 12 #82
Conversation
And as usual feedback is welcome. I'm Dutch so direct feedback is appreciated ;-) |
This pull request fixes 3 alerts when merging 7b5d638 into 883c249 - view on LGTM.com fixed alerts:
|
This pull request fixes 3 alerts when merging 382ea7c into 883c249 - view on LGTM.com fixed alerts:
|
LGTM analysis highlighted the risk of prototype polution by using user data (e.g. clientID) as key to a plain object. |
This pull request fixes 3 alerts when merging 2737104 into 883c249 - view on LGTM.com fixed alerts:
|
Bumped into a suprise while testing abstract.js against the current version of this module. Going to try to work around this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks good so far. I'm still curious about how and if the iterators approach is faster or the same as using streams, could you try running some benchnamrks on aedes?
You can find them here: https://github.com/moscajs/aedes/tree/main/benchmarks
Of course you should use QoS > 1 and/or retained messages to test this
} | ||
|
||
module.exports = MemoryPersistence | ||
module.exports = () => { return new MemoryPersistence() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain me why this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course :-)
On pre-ES6 classes the constructor also doubles as a regular function so a new instance can be created by just calling the function eg.:
const instance = MemoryPersistence()
This is also why the current version starts with:
aedes-persistence/persistence.js
Lines 13 to 17 in 883c249
function MemoryPersistence () { | |
if (!(this instanceof MemoryPersistence)) { | |
return new MemoryPersistence() | |
} | |
On ES6 classes you can't do this:
const instance = MemoryPersistence()
As the class name refers to the class and not the constructor.
So we need to do:
const instance = new MemoryPersistence()
Which will invoke the constructor.
Now at this point all our users will expect us to provide a direct callable export.
Hence we need to export:
module.exports = () => { return new MemoryPersistence() }
So any importer can still do:
const instance = MemoryPersistence()
Ideally we would also export the class itself by adding:
module.exports.MemoryPersistence = MemoryPersistence
so future importers can do:
const { MemoryPersistence } = require ("aedes-persistence")
const persistence = new MemoryPersistence()
Which would make it more clear that persistence
is a class instance.
I added this last export at first but it lowered the coveralls score as we don't have any code using this new export. So I pulled it out ;-)
Hope this clarifies it a bit.
Feel free to ask more questions !
Kind regards,
Hans
I'll have a go as soon as I fixed the |
The missing iterator on From2 streams should be fixable by using Readable().wrap(). The following code: const from2 = require('from2')
const { Readable } = require('stream')
function makeStream() {
const queue = [{ a: 1 }, { b: 2 }, { c: 3 }]
return from2.obj(function match(size, next) {
let entry
if ((entry = queue.shift()) != null) {
setImmediate(next, null, entry)
return
}
if (!entry) this.push(null)
})
}
async function consume(stream) {
console.log("Consume")
for await (const value of stream) {
console.log({ value })
}
}
const legacy = makeStream()
consume(new Readable().wrap(legacy)) Should produce: Consume
{ value: { a: 1 } }
{ value: { b: 2 } }
{ value: { c: 3 } } but produces only: Consume So I'll need to do some more digging :-( |
Ok I found it: This works :-) const legacy = makeStream()
consume(new Readable({objectMode:true}).wrap(legacy)) Without |
I will try to do some benchmarks soon.
|
This pull request fixes 3 alerts when merging c4c87db into 883c249 - view on LGTM.com fixed alerts:
|
Although all checks passed I found some issue when trying to do the benchmarks. |
This pull request fixes 3 alerts when merging 4ae378c into 883c249 - view on LGTM.com fixed alerts:
|
I did some benchmarking using my laptop.
Original: [email protected]
Taking out the outliers at the start we end up with the following averages
Which seems to suggest imho that the generators are at least as good and maybe slightly better ;-) Kind regards, |
Btw: I also did the same benchmark on NodeJS: 14.19.1
Most of this will be due to perf improvements between Level@7 vs Level@8 though ;-) |
This pull request fixes 3 alerts when merging 73c50f8 into 883c249 - view on LGTM.com fixed alerts:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seriousme Thanks for this PR! Also thanks for doing detailed benchmark and a quick research about async iterators, everything looks good here :)
Items on the shopping list I hope to achieve in this PR:
All while maintaining the abstract interface, hence the two step approach so we can't accidentally mess up either of them ;-)
If I missed anything just let me know.
I created this as a draft PR so you can follow along.
Kind regards,
Hans