forked from observing/thor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[minor] Adding a messages generator so you can supply it with your me…
…ssage file
- Loading branch information
Showing
3 changed files
with
70 additions
and
6 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,46 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Generate a UTF-8 messages that we will be send to a connected client. | ||
* | ||
* @async | ||
* @param {Number} size The specified in bytes for the message. | ||
* @param {Function} fn The callback function for the data. | ||
* @public | ||
*/ | ||
exports.utf8 = function utf(size, fn) { | ||
var key = 'utf8::'+ size | ||
, cached = cache[key]; | ||
|
||
// We have a cached version of this size, return that instead. | ||
if (cached) return fn(undefined, cached); | ||
|
||
cached = cache[key] = new Buffer(size).toString('utf-8'); | ||
fn(undefined, cached); | ||
}; | ||
|
||
/** | ||
* Generate a binary message that we will be send to a connected client. | ||
* | ||
* @async | ||
* @param {Number} size The specified in bytes for the message. | ||
* @param {Function} fn The callback function for the data. | ||
* @public | ||
*/ | ||
exports.binary = function binary(size, fn) { | ||
var key = 'binary::'+ size | ||
, cached = cache[key]; | ||
|
||
// We have a cached version of this size, return that instead. | ||
if (cached) return fn(undefined, cached); | ||
|
||
cached = cache[key] = new Buffer(size); | ||
fn(undefined, cached); | ||
}; | ||
|
||
// | ||
// The following is not needed to create a session file. We don't want to | ||
// re-create & re-allocate memory every time we receive a message so we cache | ||
// them in a variable. | ||
// | ||
var cache = Object.create(null); |
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