Code coverage report for core/messages.js

Statements: 100% (49 / 49)      Branches: 100% (25 / 25)      Functions: 100% (7 / 7)      Lines: 100% (41 / 41)      Ignored: none     

All files » core/ » messages.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                        1           1 265 261 261 307 307 4   257     1 253 309         1 787 608 599       1 68 66 62 60       1 37 34 32     1 339       1 1 1 1 1 1 1 1 1 1 1     1 1 1
/*
 * Copyright 2014-2016, Sébastien Piquemal <sebpiq@gmail.com>
 *
 * rhizome is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * rhizome is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with rhizome.  If not, see <http://www.gnu.org/licenses/>.
 */
"use strict";
 
// !!! This file must not use ES6 features, as it is browserified
 
var Buffer = require('buffer').Buffer
  , _ = require('underscore')
  , isBrowser = (typeof window !== 'undefined')
 
// Validates that argument list sent with a message is valid.
// Returns null if `args` is valid, a string indicating the error otherwise.
exports.validateArgs = function(args) {
  if (!_.isArray(args)) return '`args` should be an array'
  var arg, i, length
  for (i = 0, length = args.length; i < length; i++) {
    arg = args[i]
    if (!(_.isString(arg) || _.isNumber(arg) || _isBufferish(arg)))
      return 'argument ' + i + ', invalid type'
  }
  return null
}
 
exports.argsToString = function(args) {
  return '[' + args.map(function(arg) {
    return _isBufferish(arg) ? 'Blob(' + arg.length + ')' : arg
  }) + ']'
}
 
// Normalizes an address, removing the trailing slash
var normalizeAddress = exports.normalizeAddress = function(address) {
  if (address === '/') return address
  else if (_.last(address) === '/') return address.slice(0, -1)
  else return address
}
 
// Validates an address for subscription. Returns `null` if the address is valid, an error message otherwise.
exports.validateAddressForSub = function(address) {
  if (!_.isString(address)) return 'should be a string'
  if (address[0] !== '/') return 'should start with /'
  if (sysAddressRe.exec(address)) return 'system addresses are reserved for the system'
  return null
}
 
// Validates an address for sending. Returns `null` if the address is valid, an error message otherwise.
exports.validateAddressForSend = function(address) {
  if (address[0] !== '/') return 'should start with /'
  if (broadcastAddressRe.exec(address)) return 'broadcast addresses are reserved for the system'
  return null
}
 
var _isBufferish = function(arg) { 
  return (arg instanceof Buffer || arg instanceof ArrayBuffer)
}
 
// Regular expression for system addresses.
var sysAddressRe = exports.sysAddressRe = /^\/sys.*$/
exports.configureAddress = '/sys/config'
exports.configuredAddress = '/sys/configured'
exports.subscribeAddress = '/sys/subscribe'
exports.subscribedAddress = '/sys/subscribed'
exports.sendBlobAddress = '/sys/blob'
exports.errorAddress = '/sys/error'
exports.resendAddress = '/sys/resend'
exports.connectionStatusAddress = '/sys/connection'
exports.connectionsSendListAddress = '/sys/connections/sendlist'
exports.connectionsTakeListAddress = '/sys/connections' // + namespace
 
// Regular expression for broadcast addresses.
var broadcastAddressRe = exports.broadcastAddressRe = /^\/broadcast.*$/
exports.connectionCloseAddress = '/broadcast/close'
exports.connectionOpenAddress = '/broadcast/open'