code
stringlengths 24
2.07M
| docstring
stringlengths 25
85.3k
| func_name
stringlengths 1
92
| language
stringclasses 1
value | repo
stringlengths 5
64
| path
stringlengths 4
172
| url
stringlengths 44
218
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
function isFinish(msg, protos){
return (!protos.__tags[peekHead().tag]);
} | Test if the given msg is finished | isFinish | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
function peekHead(){
var tag = codec.decodeUInt32(peekBytes());
return {
type : tag&0x7,
tag : tag>>3
};
} | Get tag head without move the offset | peekHead | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
function decodeProp(type, protos){
switch(type){
case 'uInt32':
return codec.decodeUInt32(getBytes());
case 'int32' :
case 'sInt32' :
return codec.decodeSInt32(getBytes());
case 'float' :
var float = codec.decodeFloat(buffer, offset);
offset += 4;
return float;
case 'double' :
var double = codec.decodeDouble(buffer, offset);
offset += 8;
return double;
case 'string' :
var length = codec.decodeUInt32(getBytes());
var str = codec.decodeStr(buffer, offset, length);
offset += length;
return str;
default :
if(!!protos && !!protos.__messages[type]){
var length = codec.decodeUInt32(getBytes());
var msg = {};
decodeMsg(msg, protos.__messages[type], offset+length);
return msg;
}
break;
}
} | Get tag head without move the offset | decodeProp | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
function decodeArray(array, type, protos){
if(util.isSimpleType(type)){
var length = codec.decodeUInt32(getBytes());
for(var i = 0; i < length; i++){
array.push(decodeProp(type));
}
}else{
array.push(decodeProp(type, protos));
}
} | Get tag head without move the offset | decodeArray | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
function getBytes(flag){
var bytes = [];
var pos = offset;
flag = flag || false;
var b;
do{
b = buffer[pos];
bytes.push(b);
pos++;
}while(b >= 128);
if(!flag){
offset = pos;
}
return bytes;
} | Get tag head without move the offset | getBytes | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
function peekBytes(){
return getBytes(true);
} | Get tag head without move the offset | peekBytes | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
initWebSocket = function(url,cb) {
console.log('connect to ' + url);
var onopen = function(event){
var obj = Package.encode(Package.TYPE_HANDSHAKE, Protocol.strencode(JSON.stringify(handshakeBuffer)));
send(obj);
};
var onmessage = function(event) {
processPackage(Package.decode(event.data), cb);
// new package arrived, update the heartbeat timeout
if(heartbeatTimeout) {
nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
}
};
var onerror = function(event) {
pinus.emit('io-error', event);
console.error('socket error: ', event);
};
var onclose = function(event){
pinus.emit('close',event);
console.error('socket close: ', event);
};
socket = new WebSocket(url);
socket.binaryType = 'arraybuffer';
socket.onopen = onopen;
socket.onmessage = onmessage;
socket.onerror = onerror;
socket.onclose = onclose;
} | Get tag head without move the offset | initWebSocket | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
onopen = function(event){
var obj = Package.encode(Package.TYPE_HANDSHAKE, Protocol.strencode(JSON.stringify(handshakeBuffer)));
send(obj);
} | Get tag head without move the offset | onopen | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
onmessage = function(event) {
processPackage(Package.decode(event.data), cb);
// new package arrived, update the heartbeat timeout
if(heartbeatTimeout) {
nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
}
} | Get tag head without move the offset | onmessage | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
onerror = function(event) {
pinus.emit('io-error', event);
console.error('socket error: ', event);
} | Get tag head without move the offset | onerror | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
onclose = function(event){
pinus.emit('close',event);
console.error('socket close: ', event);
} | Get tag head without move the offset | onclose | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
sendMessage = function(reqId, route, msg) {
var type = reqId ? Message.TYPE_REQUEST : Message.TYPE_NOTIFY;
//compress message by protobuf
var protos = !!pinus.data.protos?pinus.data.protos.client:{};
if(!!protos[route]){
msg = protobuf.encode(route, msg);
}else{
msg = Protocol.strencode(JSON.stringify(msg));
}
var compressRoute = 0;
if(pinus.dict && pinus.dict[route]){
route = pinus.dict[route];
compressRoute = 1;
}
msg = Message.encode(reqId, type, compressRoute, route, msg);
var packet = Package.encode(Package.TYPE_DATA, msg);
send(packet);
} | Get tag head without move the offset | sendMessage | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
heartbeat = function(data) {
if(!heartbeatInterval) {
// no heartbeat
return;
}
var obj = Package.encode(Package.TYPE_HEARTBEAT);
if(heartbeatTimeoutId) {
clearTimeout(heartbeatTimeoutId);
heartbeatTimeoutId = null;
}
if(heartbeatId) {
// already in a heartbeat interval
return;
}
heartbeatId = setTimeout(function() {
heartbeatId = null;
send(obj);
nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, heartbeatTimeout);
}, heartbeatInterval);
} | Get tag head without move the offset | heartbeat | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
heartbeatTimeoutCb = function() {
var gap = nextHeartbeatTimeout - Date.now();
if(gap > gapThreshold) {
heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, gap);
} else {
console.error('server heartbeat timeout');
pinus.emit('heartbeat timeout');
pinus.disconnect();
}
} | Get tag head without move the offset | heartbeatTimeoutCb | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
handshake = function(data){
data = JSON.parse(Protocol.strdecode(data));
if(data.code === RES_OLD_CLIENT) {
pinus.emit('error', 'client version not fullfill');
return;
}
if(data.code !== RES_OK) {
pinus.emit('error', 'handshake fail');
return;
}
handshakeInit(data);
var obj = Package.encode(Package.TYPE_HANDSHAKE_ACK);
send(obj);
if(initCallback) {
initCallback(socket);
initCallback = null;
}
} | Get tag head without move the offset | handshake | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
onData = function(data){
//probuff decode
var msg = Message.decode(data);
if(msg.id > 0){
msg.route = routeMap[msg.id];
delete routeMap[msg.id];
if(!msg.route){
return;
}
}
msg.body = deCompose(msg);
processMessage(pinus, msg);
} | Get tag head without move the offset | onData | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
processMessage = function(pinus, msg) {
if(!msg.id) {
// server push message
pinus.emit(msg.route, msg.body);
return;
}
//if have a id then find the callback function with the request
var cb = callbacks[msg.id];
delete callbacks[msg.id];
if(typeof cb !== 'function') {
return;
}
cb(msg.body);
return;
} | Get tag head without move the offset | processMessage | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
processMessageBatch = function(pinus, msgs) {
for(var i=0, l=msgs.length; i<l; i++) {
processMessage(pinus, msgs[i]);
}
} | Get tag head without move the offset | processMessageBatch | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
deCompose = function(msg){
var protos = !!pinus.data.protos?pinus.data.protos.server:{};
var abbrs = pinus.data.abbrs;
var route = msg.route;
//Decompose route from dict
if(msg.compressRoute) {
if(!abbrs[route]){
return {};
}
route = msg.route = abbrs[route];
}
if(!!protos[route]){
return protobuf.decode(route, msg.body);
}else{
return JSON.parse(Protocol.strdecode(msg.body));
}
return msg;
} | Get tag head without move the offset | deCompose | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
handshakeInit = function(data){
if(data.sys && data.sys.heartbeat) {
heartbeatInterval = data.sys.heartbeat * 1000; // heartbeat interval
heartbeatTimeout = heartbeatInterval * 2; // max heartbeat timeout
} else {
heartbeatInterval = 0;
heartbeatTimeout = 0;
}
initData(data);
if(typeof handshakeCallback === 'function') {
handshakeCallback(data.user);
}
} | Get tag head without move the offset | handshakeInit | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
initData = function(data){
if(!data || !data.sys) {
return;
}
pinus.data = pinus.data || {};
var dict = data.sys.dict;
var protos = data.sys.protos;
//Init compress dict
if(dict){
pinus.data.dict = dict;
pinus.data.abbrs = {};
for(var route in dict){
pinus.data.abbrs[dict[route]] = route;
}
}
//Init protobuf protos
if(protos){
pinus.data.protos = {
server : protos.server || {},
client : protos.client || {}
};
if(!!protobuf){
protobuf.init({encoderProtos: protos.client, decoderProtos: protos.server});
}
}
} | Get tag head without move the offset | initData | javascript | node-pinus/pinus | examples/ssl-connector/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/ssl-connector/web-server/public/js/lib/build/build.js | MIT |
function require(path, parent, orig) {
var resolved = require.resolve(path);
// lookup failed
if (null == resolved) {
orig = orig || path;
parent = parent || 'root';
var err = new Error('Failed to require "' + orig + '" from "' + parent + '"');
err.path = orig;
err.parent = parent;
err.require = true;
throw err;
}
var module = require.modules[resolved];
// perform real require()
// by invoking the module's
// registered function
if (!module._resolving && !module.exports) {
var mod = {};
mod.exports = {};
mod.client = mod.component = true;
module._resolving = true;
module.call(this, mod.exports, require.relative(resolved), mod);
delete module._resolving;
module.exports = mod.exports;
}
return module.exports;
} | Require the given path.
@param {String} path
@return {Object} exports
@api public | require | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
copyArray = function(dest, doffset, src, soffset, length) {
if('function' === typeof src.copy) {
// Buffer
src.copy(dest, doffset, soffset, soffset + length);
} else {
// Uint8Array
for(var index=0; index<length; index++){
dest[doffset++] = src[soffset++];
}
}
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | copyArray | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
msgHasId = function(type) {
return type === Message.TYPE_REQUEST || type === Message.TYPE_RESPONSE;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | msgHasId | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
msgHasRoute = function(type) {
return type === Message.TYPE_REQUEST || type === Message.TYPE_NOTIFY ||
type === Message.TYPE_PUSH;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | msgHasRoute | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
caculateMsgIdBytes = function(id) {
var len = 0;
do {
len += 1;
id >>= 7;
} while(id > 0);
return len;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | caculateMsgIdBytes | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
encodeMsgFlag = function(type, compressRoute, buffer, offset) {
if(type !== Message.TYPE_REQUEST && type !== Message.TYPE_NOTIFY &&
type !== Message.TYPE_RESPONSE && type !== Message.TYPE_PUSH) {
throw new Error('unkonw message type: ' + type);
}
buffer[offset] = (type << 1) | (compressRoute ? 1 : 0);
return offset + MSG_FLAG_BYTES;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgFlag | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
encodeMsgId = function(id, buffer, offset) {
do{
var tmp = id % 128;
var next = Math.floor(id/128);
if(next !== 0){
tmp = tmp + 128;
}
buffer[offset++] = tmp;
id = next;
} while(id !== 0);
return offset;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgId | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
encodeMsgRoute = function(compressRoute, route, buffer, offset) {
if (compressRoute) {
if(route > MSG_ROUTE_CODE_MAX){
throw new Error('route number is overflow');
}
buffer[offset++] = (route >> 8) & 0xff;
buffer[offset++] = route & 0xff;
} else {
if(route) {
buffer[offset++] = route.length & 0xff;
copyArray(buffer, offset, route, 0, route.length);
offset += route.length;
} else {
buffer[offset++] = 0;
}
}
return offset;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgRoute | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
encodeMsgBody = function(msg, buffer, offset) {
copyArray(buffer, offset, msg, 0, msg.length);
return offset + msg.length;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgBody | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function checkMsg(msg, protos){
if(!protos){
return false;
}
for(var name in protos){
var proto = protos[name];
//All required element must exist
switch(proto.option){
case 'required' :
if(typeof(msg[name]) === 'undefined'){
console.warn('no property exist for required! name: %j, proto: %j, msg: %j', name, proto, msg);
return false;
}
case 'optional' :
if(typeof(msg[name]) !== 'undefined'){
var message = protos.__messages[proto.type] || MsgEncoder.protos['message ' + proto.type];
if(!!message && !checkMsg(msg[name], message)){
console.warn('inner proto error! name: %j, proto: %j, msg: %j', name, proto, msg);
return false;
}
}
break;
case 'repeated' :
//Check nest message in repeated elements
var message = protos.__messages[proto.type] || MsgEncoder.protos['message ' + proto.type];
if(!!msg[name] && !!message){
for(var i = 0; i < msg[name].length; i++){
if(!checkMsg(msg[name][i], message)){
return false;
}
}
}
break;
}
}
return true;
} | Check if the msg follow the defination in the protos | checkMsg | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function encodeMsg(buffer, offset, protos, msg){
for(var name in msg){
if(!!protos[name]){
var proto = protos[name];
switch(proto.option){
case 'required' :
case 'optional' :
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = encodeProp(msg[name], proto.type, offset, buffer, protos);
break;
case 'repeated' :
if(msg[name].length > 0){
offset = encodeArray(msg[name], proto, offset, buffer, protos);
}
break;
}
}
}
return offset;
} | Check if the msg follow the defination in the protos | encodeMsg | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function encodeProp(value, type, offset, buffer, protos){
switch(type){
case 'uInt32':
offset = writeBytes(buffer, offset, codec.encodeUInt32(value));
break;
case 'int32' :
case 'sInt32':
offset = writeBytes(buffer, offset, codec.encodeSInt32(value));
break;
case 'float':
writeBytes(buffer, offset, codec.encodeFloat(value));
offset += 4;
break;
case 'double':
writeBytes(buffer, offset, codec.encodeDouble(value));
offset += 8;
break;
case 'string':
var length = codec.byteLength(value);
//Encode length
offset = writeBytes(buffer, offset, codec.encodeUInt32(length));
//write string
codec.encodeStr(buffer, offset, value);
offset += length;
break;
default :
var message = protos.__messages[type] || MsgEncoder.protos['message ' + type];
if(!!message){
//Use a tmp buffer to build an internal msg
var tmpBuffer = new ArrayBuffer(codec.byteLength(JSON.stringify(value))*2);
var length = 0;
length = encodeMsg(tmpBuffer, length, message, value);
//Encode length
offset = writeBytes(buffer, offset, codec.encodeUInt32(length));
//contact the object
for(var i = 0; i < length; i++){
buffer[offset] = tmpBuffer[i];
offset++;
}
}
break;
}
return offset;
} | Check if the msg follow the defination in the protos | encodeProp | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function encodeArray(array, proto, offset, buffer, protos){
var i = 0;
if(util.isSimpleType(proto.type)){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = writeBytes(buffer, offset, codec.encodeUInt32(array.length));
for(i = 0; i < array.length; i++){
offset = encodeProp(array[i], proto.type, offset, buffer);
}
}else{
for(i = 0; i < array.length; i++){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = encodeProp(array[i], proto.type, offset, buffer, protos);
}
}
return offset;
} | Encode reapeated properties, simple msg and object are decode differented | encodeArray | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function writeBytes(buffer, offset, bytes){
for(var i = 0; i < bytes.length; i++, offset++){
buffer[offset] = bytes[i];
}
return offset;
} | Encode reapeated properties, simple msg and object are decode differented | writeBytes | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function encodeTag(type, tag){
var value = constant.TYPES[type]||2;
return codec.encodeUInt32((tag<<3)|value);
} | Encode reapeated properties, simple msg and object are decode differented | encodeTag | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function isFinish(msg, protos){
return (!protos.__tags[peekHead().tag]);
} | Test if the given msg is finished | isFinish | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function peekHead(){
var tag = codec.decodeUInt32(peekBytes());
return {
type : tag&0x7,
tag : tag>>3
};
} | Get tag head without move the offset | peekHead | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function decodeProp(type, protos){
switch(type){
case 'uInt32':
return codec.decodeUInt32(getBytes());
case 'int32' :
case 'sInt32' :
return codec.decodeSInt32(getBytes());
case 'float' :
var float = codec.decodeFloat(buffer, offset);
offset += 4;
return float;
case 'double' :
var double = codec.decodeDouble(buffer, offset);
offset += 8;
return double;
case 'string' :
var length = codec.decodeUInt32(getBytes());
var str = codec.decodeStr(buffer, offset, length);
offset += length;
return str;
default :
var message = protos && (protos.__messages[type] || MsgDecoder.protos['message ' + type]);
if(!!message){
var length = codec.decodeUInt32(getBytes());
var msg = {};
decodeMsg(msg, message, offset+length);
return msg;
}
break;
}
} | Get tag head without move the offset | decodeProp | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function decodeArray(array, type, protos){
if(util.isSimpleType(type)){
var length = codec.decodeUInt32(getBytes());
for(var i = 0; i < length; i++){
array.push(decodeProp(type));
}
}else{
array.push(decodeProp(type, protos));
}
} | Get tag head without move the offset | decodeArray | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function getBytes(flag){
var bytes = [];
var pos = offset;
flag = flag || false;
var b;
do{
b = buffer[pos];
bytes.push(b);
pos++;
}while(b >= 128);
if(!flag){
offset = pos;
}
return bytes;
} | Get tag head without move the offset | getBytes | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
function peekBytes(){
return getBytes(true);
} | Get tag head without move the offset | peekBytes | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
connect = function(params, url, cb) {
console.log('connect to ' + url);
var params = params || {};
var maxReconnectAttempts = params.maxReconnectAttempts || DEFAULT_MAX_RECONNECT_ATTEMPTS;
reconnectUrl = url;
//Add protobuf version
if(window.localStorage && window.localStorage.getItem('protos') && protoVersion === 0) {
var protos = JSON.parse(window.localStorage.getItem('protos'));
protoVersion = protos.version || 0;
serverProtos = protos.server || {};
clientProtos = protos.client || {};
if(!!protobuf) {
protobuf.init({encoderProtos: clientProtos, decoderProtos: serverProtos});
}
if(!!decodeIO_protobuf) {
decodeIO_encoder = decodeIO_protobuf.loadJson(clientProtos);
decodeIO_decoder = decodeIO_protobuf.loadJson(serverProtos);
}
}
//Set protoversion
handshakeBuffer.sys.protoVersion = protoVersion;
var onopen = function(event) {
if(!!reconnect) {
pomelo.emit('reconnect');
}
reset();
var obj = Package.encode(Package.TYPE_HANDSHAKE, Protocol.strencode(JSON.stringify(handshakeBuffer)));
send(obj);
};
var onmessage = function(event) {
processPackage(Package.decode(event.data), cb);
// new package arrived, update the heartbeat timeout
if(heartbeatTimeout) {
nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
}
};
var onerror = function(event) {
pomelo.emit('io-error', event);
console.error('socket error: ', event);
};
var onclose = function(event) {
pomelo.emit('close',event);
pomelo.emit('disconnect', event);
console.error('socket close: ', event);
if(!!params.reconnect && reconnectAttempts < maxReconnectAttempts) {
reconnect = true;
reconnectAttempts++;
reconncetTimer = setTimeout(function() {
connect(params, reconnectUrl, cb);
}, reconnectionDelay);
reconnectionDelay *= 2;
}
};
socket = new WebSocket(url);
socket.binaryType = 'arraybuffer';
socket.onopen = onopen;
socket.onmessage = onmessage;
socket.onerror = onerror;
socket.onclose = onclose;
} | Get tag head without move the offset | connect | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
onopen = function(event) {
if(!!reconnect) {
pomelo.emit('reconnect');
}
reset();
var obj = Package.encode(Package.TYPE_HANDSHAKE, Protocol.strencode(JSON.stringify(handshakeBuffer)));
send(obj);
} | Get tag head without move the offset | onopen | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
onmessage = function(event) {
processPackage(Package.decode(event.data), cb);
// new package arrived, update the heartbeat timeout
if(heartbeatTimeout) {
nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
}
} | Get tag head without move the offset | onmessage | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
onerror = function(event) {
pomelo.emit('io-error', event);
console.error('socket error: ', event);
} | Get tag head without move the offset | onerror | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
onclose = function(event) {
pomelo.emit('close',event);
pomelo.emit('disconnect', event);
console.error('socket close: ', event);
if(!!params.reconnect && reconnectAttempts < maxReconnectAttempts) {
reconnect = true;
reconnectAttempts++;
reconncetTimer = setTimeout(function() {
connect(params, reconnectUrl, cb);
}, reconnectionDelay);
reconnectionDelay *= 2;
}
} | Get tag head without move the offset | onclose | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
reset = function() {
reconnect = false;
reconnectionDelay = 1000 * 5;
reconnectAttempts = 0;
clearTimeout(reconncetTimer);
} | Get tag head without move the offset | reset | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
sendMessage = function(reqId, route, msg) {
if(useCrypto) {
msg = JSON.stringify(msg);
var sig = rsa.signString(msg, "sha256");
msg = JSON.parse(msg);
msg['__crypto__'] = sig;
}
if(encode) {
msg = encode(reqId, route, msg);
}
var packet = Package.encode(Package.TYPE_DATA, msg);
send(packet);
} | Get tag head without move the offset | sendMessage | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
heartbeat = function(data) {
if(!heartbeatInterval) {
// no heartbeat
return;
}
var obj = Package.encode(Package.TYPE_HEARTBEAT);
if(heartbeatTimeoutId) {
clearTimeout(heartbeatTimeoutId);
heartbeatTimeoutId = null;
}
if(heartbeatId) {
// already in a heartbeat interval
return;
}
heartbeatId = setTimeout(function() {
heartbeatId = null;
send(obj);
nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, heartbeatTimeout);
}, heartbeatInterval);
} | Get tag head without move the offset | heartbeat | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
heartbeatTimeoutCb = function() {
var gap = nextHeartbeatTimeout - Date.now();
if(gap > gapThreshold) {
heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, gap);
} else {
console.error('server heartbeat timeout');
pomelo.emit('heartbeat timeout');
pomelo.disconnect();
}
} | Get tag head without move the offset | heartbeatTimeoutCb | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
handshake = function(data) {
data = JSON.parse(Protocol.strdecode(data));
if(data.code === RES_OLD_CLIENT) {
pomelo.emit('error', 'client version not fullfill');
return;
}
if(data.code !== RES_OK) {
pomelo.emit('error', 'handshake fail');
return;
}
handshakeInit(data);
var obj = Package.encode(Package.TYPE_HANDSHAKE_ACK);
send(obj);
if(initCallback) {
initCallback(socket);
}
} | Get tag head without move the offset | handshake | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
onData = function(data) {
var msg = data;
if(decode) {
msg = decode(msg);
}
processMessage(pomelo, msg);
} | Get tag head without move the offset | onData | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
onKick = function(data) {
data = JSON.parse(Protocol.strdecode(data));
pomelo.emit('onKick', data);
} | Get tag head without move the offset | onKick | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
processPackage = function(msgs) {
if(Array.isArray(msgs)) {
for(var i=0; i<msgs.length; i++) {
var msg = msgs[i];
handlers[msg.type](msg.body);
}
} else {
handlers[msgs.type](msgs.body);
}
} | Get tag head without move the offset | processPackage | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
processMessage = function(pomelo, msg) {
if(!msg.id) {
// server push message
pomelo.emit(msg.route, msg.body);
return;
}
//if have a id then find the callback function with the request
var cb = callbacks[msg.id];
delete callbacks[msg.id];
if(typeof cb !== 'function') {
return;
}
cb(msg.body);
return;
} | Get tag head without move the offset | processMessage | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
processMessageBatch = function(pomelo, msgs) {
for(var i=0, l=msgs.length; i<l; i++) {
processMessage(pomelo, msgs[i]);
}
} | Get tag head without move the offset | processMessageBatch | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
deCompose = function(msg) {
var route = msg.route;
//Decompose route from dict
if(msg.compressRoute) {
if(!abbrs[route]){
return {};
}
route = msg.route = abbrs[route];
}
if(protobuf && serverProtos[route]) {
return protobuf.decode(route, msg.body);
} else if(decodeIO_decoder && decodeIO_decoder.lookup(route)) {
return decodeIO_decoder.build(route).decode(msg.body);
} else {
return JSON.parse(Protocol.strdecode(msg.body));
}
return msg;
} | Get tag head without move the offset | deCompose | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
handshakeInit = function(data) {
if(data.sys && data.sys.heartbeat) {
heartbeatInterval = data.sys.heartbeat * 1000; // heartbeat interval
heartbeatTimeout = heartbeatInterval * 2; // max heartbeat timeout
} else {
heartbeatInterval = 0;
heartbeatTimeout = 0;
}
initData(data);
if(typeof handshakeCallback === 'function') {
handshakeCallback(data.user);
}
} | Get tag head without move the offset | handshakeInit | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
initData = function(data) {
if(!data || !data.sys) {
return;
}
dict = data.sys.dict;
var protos = data.sys.protos;
//Init compress dict
if(dict) {
dict = dict;
abbrs = {};
for(var route in dict) {
abbrs[dict[route]] = route;
}
}
//Init protobuf protos
if(protos) {
protoVersion = protos.version || 0;
serverProtos = protos.server || {};
clientProtos = protos.client || {};
//Save protobuf protos to localStorage
window.localStorage.setItem('protos', JSON.stringify(protos));
if(!!protobuf) {
protobuf.init({encoderProtos: protos.client, decoderProtos: protos.server});
}
if(!!decodeIO_protobuf) {
decodeIO_encoder = decodeIO_protobuf.loadJson(clientProtos);
decodeIO_decoder = decodeIO_protobuf.loadJson(serverProtos);
}
}
} | Get tag head without move the offset | initData | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/build/build.js | MIT |
copyArray = function(dest, doffset, src, soffset, length) {
if('function' === typeof src.copy) {
// Buffer
src.copy(dest, doffset, soffset, soffset + length);
} else {
// Uint8Array
for(var index=0; index<length; index++){
dest[doffset++] = src[soffset++];
}
}
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | copyArray | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | MIT |
msgHasId = function(type) {
return type === Message.TYPE_REQUEST || type === Message.TYPE_RESPONSE;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | msgHasId | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | MIT |
msgHasRoute = function(type) {
return type === Message.TYPE_REQUEST || type === Message.TYPE_NOTIFY ||
type === Message.TYPE_PUSH;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | msgHasRoute | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | MIT |
caculateMsgIdBytes = function(id) {
var len = 0;
do {
len += 1;
id >>= 7;
} while(id > 0);
return len;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | caculateMsgIdBytes | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | MIT |
encodeMsgFlag = function(type, compressRoute, buffer, offset) {
if(type !== Message.TYPE_REQUEST && type !== Message.TYPE_NOTIFY &&
type !== Message.TYPE_RESPONSE && type !== Message.TYPE_PUSH) {
throw new Error('unkonw message type: ' + type);
}
buffer[offset] = (type << 1) | (compressRoute ? 1 : 0);
return offset + MSG_FLAG_BYTES;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgFlag | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | MIT |
encodeMsgId = function(id, buffer, offset) {
do{
var tmp = id % 128;
var next = Math.floor(id/128);
if(next !== 0){
tmp = tmp + 128;
}
buffer[offset++] = tmp;
id = next;
} while(id !== 0);
return offset;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgId | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | MIT |
encodeMsgRoute = function(compressRoute, route, buffer, offset) {
if (compressRoute) {
if(route > MSG_ROUTE_CODE_MAX){
throw new Error('route number is overflow');
}
buffer[offset++] = (route >> 8) & 0xff;
buffer[offset++] = route & 0xff;
} else {
if(route) {
buffer[offset++] = route.length & 0xff;
copyArray(buffer, offset, route, 0, route.length);
offset += route.length;
} else {
buffer[offset++] = 0;
}
}
return offset;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgRoute | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | MIT |
encodeMsgBody = function(msg, buffer, offset) {
copyArray(buffer, offset, msg, 0, msg.length);
return offset + msg.length;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgBody | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/NetEase-pomelo-protocol/lib/protocol.js | MIT |
function checkMsg(msg, protos){
if(!protos){
return false;
}
for(var name in protos){
var proto = protos[name];
//All required element must exist
switch(proto.option){
case 'required' :
if(typeof(msg[name]) === 'undefined'){
console.warn('no property exist for required! name: %j, proto: %j, msg: %j', name, proto, msg);
return false;
}
case 'optional' :
if(typeof(msg[name]) !== 'undefined'){
var message = protos.__messages[proto.type] || MsgEncoder.protos['message ' + proto.type];
if(!!message && !checkMsg(msg[name], message)){
console.warn('inner proto error! name: %j, proto: %j, msg: %j', name, proto, msg);
return false;
}
}
break;
case 'repeated' :
//Check nest message in repeated elements
var message = protos.__messages[proto.type] || MsgEncoder.protos['message ' + proto.type];
if(!!msg[name] && !!message){
for(var i = 0; i < msg[name].length; i++){
if(!checkMsg(msg[name][i], message)){
return false;
}
}
}
break;
}
}
return true;
} | Check if the msg follow the defination in the protos | checkMsg | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function encodeMsg(buffer, offset, protos, msg){
for(var name in msg){
if(!!protos[name]){
var proto = protos[name];
switch(proto.option){
case 'required' :
case 'optional' :
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = encodeProp(msg[name], proto.type, offset, buffer, protos);
break;
case 'repeated' :
if(msg[name].length > 0){
offset = encodeArray(msg[name], proto, offset, buffer, protos);
}
break;
}
}
}
return offset;
} | Check if the msg follow the defination in the protos | encodeMsg | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function encodeProp(value, type, offset, buffer, protos){
switch(type){
case 'uInt32':
offset = writeBytes(buffer, offset, codec.encodeUInt32(value));
break;
case 'int32' :
case 'sInt32':
offset = writeBytes(buffer, offset, codec.encodeSInt32(value));
break;
case 'float':
writeBytes(buffer, offset, codec.encodeFloat(value));
offset += 4;
break;
case 'double':
writeBytes(buffer, offset, codec.encodeDouble(value));
offset += 8;
break;
case 'string':
var length = codec.byteLength(value);
//Encode length
offset = writeBytes(buffer, offset, codec.encodeUInt32(length));
//write string
codec.encodeStr(buffer, offset, value);
offset += length;
break;
default :
var message = protos.__messages[type] || MsgEncoder.protos['message ' + type];
if(!!message){
//Use a tmp buffer to build an internal msg
var tmpBuffer = new ArrayBuffer(codec.byteLength(JSON.stringify(value))*2);
var length = 0;
length = encodeMsg(tmpBuffer, length, message, value);
//Encode length
offset = writeBytes(buffer, offset, codec.encodeUInt32(length));
//contact the object
for(var i = 0; i < length; i++){
buffer[offset] = tmpBuffer[i];
offset++;
}
}
break;
}
return offset;
} | Check if the msg follow the defination in the protos | encodeProp | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function encodeArray(array, proto, offset, buffer, protos){
var i = 0;
if(util.isSimpleType(proto.type)){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = writeBytes(buffer, offset, codec.encodeUInt32(array.length));
for(i = 0; i < array.length; i++){
offset = encodeProp(array[i], proto.type, offset, buffer);
}
}else{
for(i = 0; i < array.length; i++){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = encodeProp(array[i], proto.type, offset, buffer, protos);
}
}
return offset;
} | Encode reapeated properties, simple msg and object are decode differented | encodeArray | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function writeBytes(buffer, offset, bytes){
for(var i = 0; i < bytes.length; i++, offset++){
buffer[offset] = bytes[i];
}
return offset;
} | Encode reapeated properties, simple msg and object are decode differented | writeBytes | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function encodeTag(type, tag){
var value = constant.TYPES[type]||2;
return codec.encodeUInt32((tag<<3)|value);
} | Encode reapeated properties, simple msg and object are decode differented | encodeTag | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function isFinish(msg, protos){
return (!protos.__tags[peekHead().tag]);
} | Test if the given msg is finished | isFinish | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function peekHead(){
var tag = codec.decodeUInt32(peekBytes());
return {
type : tag&0x7,
tag : tag>>3
};
} | Get tag head without move the offset | peekHead | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function decodeProp(type, protos){
switch(type){
case 'uInt32':
return codec.decodeUInt32(getBytes());
case 'int32' :
case 'sInt32' :
return codec.decodeSInt32(getBytes());
case 'float' :
var float = codec.decodeFloat(buffer, offset);
offset += 4;
return float;
case 'double' :
var double = codec.decodeDouble(buffer, offset);
offset += 8;
return double;
case 'string' :
var length = codec.decodeUInt32(getBytes());
var str = codec.decodeStr(buffer, offset, length);
offset += length;
return str;
default :
var message = protos && (protos.__messages[type] || MsgDecoder.protos['message ' + type]);
if(!!message){
var length = codec.decodeUInt32(getBytes());
var msg = {};
decodeMsg(msg, message, offset+length);
return msg;
}
break;
}
} | Get tag head without move the offset | decodeProp | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function decodeArray(array, type, protos){
if(util.isSimpleType(type)){
var length = codec.decodeUInt32(getBytes());
for(var i = 0; i < length; i++){
array.push(decodeProp(type));
}
}else{
array.push(decodeProp(type, protos));
}
} | Get tag head without move the offset | decodeArray | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function getBytes(flag){
var bytes = [];
var pos = offset;
flag = flag || false;
var b;
do{
b = buffer[pos];
bytes.push(b);
pos++;
}while(b >= 128);
if(!flag){
offset = pos;
}
return bytes;
} | Get tag head without move the offset | getBytes | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function peekBytes(){
return getBytes(true);
} | Get tag head without move the offset | peekBytes | javascript | node-pinus/pinus | examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat/web-server/public/js/lib/components/pomelonode-pomelo-protobuf/lib/client/protobuf.js | MIT |
function require(path, parent, orig) {
var resolved = require.resolve(path);
// lookup failed
if (null == resolved) {
orig = orig || path;
parent = parent || 'root';
var err = new Error('Failed to require "' + orig + '" from "' + parent + '"');
err.path = orig;
err.parent = parent;
err.require = true;
throw err;
}
var module = require.modules[resolved];
// perform real require()
// by invoking the module's
// registered function
if (!module._resolving && !module.exports) {
var mod = {};
mod.exports = {};
mod.client = mod.component = true;
module._resolving = true;
module.call(this, mod.exports, require.relative(resolved), mod);
delete module._resolving;
module.exports = mod.exports;
}
return module.exports;
} | Require the given path.
@param {String} path
@return {Object} exports
@api public | require | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
copyArray = function(dest, doffset, src, soffset, length) {
if('function' === typeof src.copy) {
// Buffer
src.copy(dest, doffset, soffset, soffset + length);
} else {
// Uint8Array
for(var index=0; index<length; index++){
dest[doffset++] = src[soffset++];
}
}
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | copyArray | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
msgHasId = function(type) {
return type === Message.TYPE_REQUEST || type === Message.TYPE_RESPONSE;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | msgHasId | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
msgHasRoute = function(type) {
return type === Message.TYPE_REQUEST || type === Message.TYPE_NOTIFY ||
type === Message.TYPE_PUSH;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | msgHasRoute | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
caculateMsgIdBytes = function(id) {
var len = 0;
do {
len += 1;
id >>= 7;
} while(id > 0);
return len;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | caculateMsgIdBytes | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
encodeMsgFlag = function(type, compressRoute, buffer, offset) {
if(type !== Message.TYPE_REQUEST && type !== Message.TYPE_NOTIFY &&
type !== Message.TYPE_RESPONSE && type !== Message.TYPE_PUSH) {
throw new Error('unkonw message type: ' + type);
}
buffer[offset] = (type << 1) | (compressRoute ? 1 : 0);
return offset + MSG_FLAG_BYTES;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgFlag | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
encodeMsgId = function(id, buffer, offset) {
do{
var tmp = id % 128;
var next = Math.floor(id/128);
if(next !== 0){
tmp = tmp + 128;
}
buffer[offset++] = tmp;
id = next;
} while(id !== 0);
return offset;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgId | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
encodeMsgRoute = function(compressRoute, route, buffer, offset) {
if (compressRoute) {
if(route > MSG_ROUTE_CODE_MAX){
throw new Error('route number is overflow');
}
buffer[offset++] = (route >> 8) & 0xff;
buffer[offset++] = route & 0xff;
} else {
if(route) {
buffer[offset++] = route.length & 0xff;
copyArray(buffer, offset, route, 0, route.length);
offset += route.length;
} else {
buffer[offset++] = 0;
}
}
return offset;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgRoute | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
encodeMsgBody = function(msg, buffer, offset) {
copyArray(buffer, offset, msg, 0, msg.length);
return offset + msg.length;
} | Message protocol decode.
@param {Buffer|Uint8Array} buffer message bytes
@return {Object} message object | encodeMsgBody | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function checkMsg(msg, protos){
if(!protos){
return false;
}
for(var name in protos){
var proto = protos[name];
//All required element must exist
switch(proto.option){
case 'required' :
if(typeof(msg[name]) === 'undefined'){
console.warn('no property exist for required! name: %j, proto: %j, msg: %j', name, proto, msg);
return false;
}
case 'optional' :
if(typeof(msg[name]) !== 'undefined'){
var message = protos.__messages[proto.type] || MsgEncoder.protos['message ' + proto.type];
if(!!message && !checkMsg(msg[name], message)){
console.warn('inner proto error! name: %j, proto: %j, msg: %j', name, proto, msg);
return false;
}
}
break;
case 'repeated' :
//Check nest message in repeated elements
var message = protos.__messages[proto.type] || MsgEncoder.protos['message ' + proto.type];
if(!!msg[name] && !!message){
for(var i = 0; i < msg[name].length; i++){
if(!checkMsg(msg[name][i], message)){
return false;
}
}
}
break;
}
}
return true;
} | Check if the msg follow the defination in the protos | checkMsg | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function encodeMsg(buffer, offset, protos, msg) {
if (msg instanceof Map) {
for (const [key, value] of msg) {
if (!!protos[key]) {
let proto = protos[key];
offset = _encodeMsg(buffer, offset, protos, proto, value);
}
}
}
else {
for (let name in msg) {
if (!!protos[name]) {
let proto = protos[name];
offset = _encodeMsg(buffer, offset, protos, proto, msg[name]);
}
}
}
return offset;
} | Check if the msg follow the defination in the protos | encodeMsg | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function _encodeMsg(buffer, offset, protos, proto, value) {
switch (proto.option) {
case 'required':
case 'optional':
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = encodeProp(value, proto.type, offset, buffer, protos);
break;
case 'repeated':
if (!!value && value.length > 0) {
offset = encodeArray(value, proto, offset, buffer, protos);
}
break;
case 'map':
if (!!value) {
offset = encodeMap(value, proto, offset, buffer, protos);
}
;
break;
case 'obj':
if (!!value) {
offset = encodeObject(value, proto, offset, buffer, protos);
}
;
break;
}
return offset;
} | Check if the msg follow the defination in the protos | _encodeMsg | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function encodeMap(map, proto, offset, buffer, protos) {
const size = map.size;
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = writeBytes(buffer, offset, codec.encodeUInt32(size));
for (const [key, value] of map) {
let message = protos.__messages[proto.type] || MsgEncoder.protos['message ' + proto.type];
// map key
offset = encodeProp(key, message.key.type, offset, buffer, protos);
offset = encodeProp(value, message.value.type, offset, buffer, protos);
}
return offset;
} | Check if the msg follow the defination in the protos | encodeMap | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function encodeObject(obj, proto, offset, buffer, protos) {
const keys = Object.keys(obj);
if (keys.length === 0) {
return offset;
}
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = writeBytes(buffer, offset, codec.encodeUInt32(keys.length));
for (let key in obj) {
let message = protos.__messages[proto.type] || MsgEncoder.protos['message ' + proto.type];
// map key
offset = encodeProp(key, message.key.type, offset, buffer, protos);
const value = obj[key];
offset = encodeProp(value, message.value.type, offset, buffer, protos);
}
return offset;
} | Check if the msg follow the defination in the protos | encodeObject | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function encodeProp(value, type, offset, buffer, protos){
switch(type){
case 'uInt32':
offset = writeBytes(buffer, offset, codec.encodeUInt32(value));
break;
case 'int32' :
case 'sInt32':
offset = writeBytes(buffer, offset, codec.encodeSInt32(value));
break;
case 'float':
writeBytes(buffer, offset, codec.encodeFloat(value));
offset += 4;
break;
case 'double':
writeBytes(buffer, offset, codec.encodeDouble(value));
offset += 8;
break;
case 'string':
var length = codec.byteLength(value);
//Encode length
offset = writeBytes(buffer, offset, codec.encodeUInt32(length));
//write string
codec.encodeStr(buffer, offset, value);
offset += length;
break;
case 'bool':
const intValue = value ? 1 : 0;
offset = writeBytes(buffer, offset, codec.encodeUInt32(intValue));
break;
default :
var message = protos.__messages[type] || MsgEncoder.protos['message ' + type];
if(!!message){
//Use a tmp buffer to build an internal msg
var tmpBuffer = new ArrayBuffer(codec.byteLength(JSON.stringify(value))*2);
var length = 0;
length = encodeMsg(tmpBuffer, length, message, value);
//Encode length
offset = writeBytes(buffer, offset, codec.encodeUInt32(length));
//contact the object
for(var i = 0; i < length; i++){
buffer[offset] = tmpBuffer[i];
offset++;
}
}
break;
}
return offset;
} | Check if the msg follow the defination in the protos | encodeProp | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function encodeArray(array, proto, offset, buffer, protos){
var i = 0;
if(util.isSimpleType(proto.type)){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = writeBytes(buffer, offset, codec.encodeUInt32(array.length));
for(i = 0; i < array.length; i++){
offset = encodeProp(array[i], proto.type, offset, buffer);
}
}else{
for(i = 0; i < array.length; i++){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = encodeProp(array[i], proto.type, offset, buffer, protos);
}
}
return offset;
} | Encode reapeated properties, simple msg and object are decode differented | encodeArray | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function writeBytes(buffer, offset, bytes){
for(var i = 0; i < bytes.length; i++, offset++){
buffer[offset] = bytes[i];
}
return offset;
} | Encode reapeated properties, simple msg and object are decode differented | writeBytes | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function encodeTag(type, tag){
var value = constant.TYPES[type]||2;
return codec.encodeUInt32((tag<<3)|value);
} | Encode reapeated properties, simple msg and object are decode differented | encodeTag | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function isFinish(msg, protos){
return (!protos.__tags[peekHead().tag]);
} | Test if the given msg is finished | isFinish | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
function peekHead(){
var tag = codec.decodeUInt32(peekBytes());
return {
type : tag&0x7,
tag : tag>>3
};
} | Get tag head without move the offset | peekHead | javascript | node-pinus/pinus | examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | https://github.com/node-pinus/pinus/blob/master/examples/websocket-chat-ts-run/web-server/public/js/lib/build/build.js | MIT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.