UNPKG

@adonisjs/websocket-packet

Version:
2 lines (1 loc) 1.75 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t.adonis=t.adonis||{},t.adonis.wsp=e())}(this,function(){"use strict";var t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},e={OPEN:0,JOIN:1,LEAVE:2,JOIN_ACK:3,JOIN_ERROR:4,LEAVE_ACK:5,LEAVE_ERROR:6,EVENT:7,PING:8,PONG:9};function o(t,e,o){return o.forEach(function(t){!function(t,e){if(!t||"string"!=typeof t)throw new Error(e)}(e[t],"expected "+t+" to be a valid string")}),{t:t,d:e}}var n={};return Object.keys(e).forEach(function(o){var i=o.toLowerCase().replace(/^\w|_(\w)/g,function(t,e){return e?e.toUpperCase():t.toUpperCase()});n["is"+i+"Packet"]=function(n){return!(!n||"object"!==(void 0===n?"undefined":t(n))||n.t!==e[o])}}),n.hasTopic=function(t){return!!(t&&t.d&&t.d.topic)},n.isValidJoinPacket=n.hasTopic,n.isValidLeavePacket=n.hasTopic,n.isValidEventPacket=n.hasTopic,n.joinPacket=function(t){return o(e.JOIN,{topic:t},["topic"])},n.leavePacket=function(t){return o(e.LEAVE,{topic:t},["topic"])},n.joinAckPacket=function(t){return o(e.JOIN_ACK,{topic:t},["topic"])},n.joinErrorPacket=function(t,n){return o(e.JOIN_ERROR,{topic:t,message:n},["topic","message"])},n.leaveAckPacket=function(t){return o(e.LEAVE_ACK,{topic:t},["topic"])},n.leaveErrorPacket=function(t,n){return o(e.LEAVE_ERROR,{topic:t,message:n},["topic","message"])},n.eventPacket=function(t,n,i){return o(e.EVENT,{topic:t,event:n,data:i=i||""},["topic","event"])},n.pingPacket=function(){return{t:e.PING}},n.pongPacket=function(){return{t:e.PONG}},Object.assign({codes:e},n)});