-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mqtt): Stream map serialization to improve memory usage with lar…
…ge maps
- Loading branch information
Showing
5 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const stream = require("stream"); | ||
|
||
class BufferWriteStream extends stream.Writable { | ||
constructor() { | ||
super(); | ||
|
||
this.chunks = []; | ||
} | ||
|
||
_write(chunk, encoding, done) { | ||
this.chunks.push(chunk); | ||
done(); | ||
} | ||
|
||
getBuffer() { | ||
return Buffer.concat(this.chunks); | ||
} | ||
} | ||
|
||
module.exports = BufferWriteStream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const BufferWriteStream = require("./BufferWriteStream"); | ||
const stream = require("stream"); | ||
const zlib = require("zlib"); | ||
const {JsonStream} = require("jetsons"); | ||
|
||
function stringifyAndCompress(data, algorithm) { | ||
return new Promise((resolve, reject) => { | ||
const bufferStream = new BufferWriteStream(); | ||
|
||
stream.pipeline( | ||
new JsonStream(data), | ||
algorithm, | ||
bufferStream, | ||
|
||
(err) => { | ||
if (err !== null && err !== undefined) { | ||
reject(err); | ||
} else { | ||
resolve(bufferStream.getBuffer()); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
|
||
function stringifyAndDeflate(data) { | ||
return stringifyAndCompress(data, zlib.createDeflate()); | ||
} | ||
|
||
function stringifyAndGZip(data) { | ||
return stringifyAndCompress(data, zlib.createGzip()); | ||
} | ||
|
||
module.exports = { | ||
stringifyAndDeflate: stringifyAndDeflate, | ||
stringifyAndGZip: stringifyAndGZip | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.