binary encoding bugfixes & export BinaryEncoder + BinaryDecoder

This commit is contained in:
Kevin Jahns
2017-07-24 15:37:04 +02:00
parent c97130abc4
commit eb4fb3a225
4 changed files with 19 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
/* global crypto */
import { BinaryDecoder, BinaryEncoder } from './Encoding.js'
/*
EventHandler is an helper class for constructing custom types.
@@ -24,7 +25,10 @@
*/
export default function Utils (Y) {
Y.utils = {}
Y.utils = {
BinaryDecoder: BinaryDecoder,
BinaryEncoder: BinaryEncoder
}
Y.utils.bubbleEvent = function (type, event) {
type.eventHandler.callEventListeners(event)
@@ -821,17 +825,17 @@ export default function Utils (Y) {
Y.utils.createSmallLookupBuffer = createSmallLookupBuffer
function generateUserId () {
if (crypto.getRandomValue != null) {
if (typeof crypto !== 'undefined' && crypto.getRandomValue != null) {
// browser
let arr = new Uint32Array(1)
crypto.getRandomValues(arr)
return arr[0]
} else if (crypto.randomBytes != null) {
} else if (typeof crypto !== 'undefined' && crypto.randomBytes != null) {
// node
let buf = crypto.randomBytes(4)
return new Uint32Array(buf.buffer)[0]
} else {
Math.ceil(Math.random() * 0xFFFFFFFF)
return Math.ceil(Math.random() * 0xFFFFFFFF)
}
}
Y.utils.generateUserId = generateUserId