sonic-ws 1.0.0-beta.2 → 1.0.0-beta.3-clean

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,2 +1,68 @@
1
1
  # sonic-ws
2
- Bandwidth efficient (but stupid) websocket library
2
+
3
+ ## INFO
4
+
5
+ WebSocket library focused on bandwidth efficiency and security.
6
+
7
+ It can reduce packet size by up to 70% and process large packets in microseconds.
8
+
9
+ It has low latency and optimized data transfer.
10
+
11
+ While still simple and efficient, SonicWS provides efficient packet transfers.
12
+
13
+ ## SAMPLES
14
+
15
+ ### Importing:
16
+ Node (Client & Server):
17
+ ```js
18
+ import { PacketType, SonicWS, SonicWSServer, CreatePacket } from "sonic-ws";
19
+ ```
20
+ Browser (Client):
21
+ ```html
22
+ <script src="https://raw.githubusercontent.com/cutelittlelily/sonic-ws/refs/heads/main/bundled/SonicWS_bundle.js"></script>
23
+ ```
24
+
25
+ ### Server:
26
+ ```js
27
+ const wss = new SonicWSServer(
28
+ [CreatePacket("pong", PacketType.INTS_D, 1)], // client-sent packets
29
+ [CreatePacket("ping", PacketType.INTS_D, 1)], // server-sent packets
30
+ { port: 1234 }
31
+ );
32
+
33
+ wss.on_connect((socket) => {
34
+
35
+ console.log("Socket connection:", socket.id);
36
+
37
+ socket.on("pong", (num) => {
38
+ console.log("Ponged!", num);
39
+ });
40
+
41
+ setInterval(() => {
42
+ socket.send("ping", Date.now());
43
+ }, 10000);
44
+
45
+ });
46
+
47
+ wss.on_ready(() => {
48
+ console.log("Server ready!");
49
+ });
50
+ ```
51
+
52
+ ### Client:
53
+ ```js
54
+ const ws = new SonicWS("ws://localhost:1234");
55
+
56
+ ws.on_ready(() => {
57
+ console.log("Connected to server");
58
+ });
59
+
60
+ ws.on("ping", (num) => {
61
+ console.log("Pinged!", num);
62
+ ws.send("pong", Date.now());
63
+ })
64
+
65
+ ws.on_close((event) => {
66
+ console.log("closed client: " + event.code);
67
+ });
68
+ ```
@@ -27,8 +27,8 @@ exports.PacketValidityProcessors = (_a = {},
27
27
  _a[PacketType.NONE] = function (data) { return data == ""; },
28
28
  _a[PacketType.RAW] = function () { return true; },
29
29
  _a[PacketType.INTS_C] = function (data, cap) { return data.length == cap; },
30
- _a[PacketType.INTS_D] = function (data, cap) { return data.length > 0 && (0, CodePointUtil_1.processCharCodes)(data).length % data[0].charCodeAt(0) + 1 <= cap; },
31
- _a[PacketType.DECIMAL] = function (data, cap) { return data.length > 0 && (0, CodePointUtil_1.processCharCodes)(data).length % data[0].charCodeAt(0) * 2 + 1 <= cap; },
30
+ _a[PacketType.INTS_D] = function (data, cap) { return data.length > 0 && ((0, CodePointUtil_1.processCharCodes)(data).length - 1) % data[0].charCodeAt(0) <= cap; },
31
+ _a[PacketType.DECIMAL] = function (data, cap) { return data.length > 0 && ((0, CodePointUtil_1.processCharCodes)(data).length - 1) % data[0].charCodeAt(0) * 2 <= cap; },
32
32
  _a[PacketType.BOOLEAN] = function (data) { return data == CodePointUtil_1.NULL || data == ""; },
33
33
  _a);
34
34
  // todo: code points might make it need to substring(2) but idk if i need to care abt that-
@@ -20,5 +20,6 @@ function emitPacket(packets, send, tag) {
20
20
  var packet = packets.getPacket(tag);
21
21
  if (values.length > packet.dataCap)
22
22
  throw new Error("Packet \"".concat(tag, "\" only allows ").concat(packet.dataCap, " values!"));
23
- send(code + PacketType_1.PacketSendProcessors[packet.type].apply(PacketType_1.PacketSendProcessors, values));
23
+ var data = code + PacketType_1.PacketSendProcessors[packet.type].apply(PacketType_1.PacketSendProcessors, values);
24
+ send(data);
24
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sonic-ws",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.3-clean",
4
4
  "description": "A bandwidth efficient websocket library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "build": "npm run build_node && npm run build_web",
12
12
 
13
13
  "test_web": "npm run build && node test-site/server.mjs",
14
- "test_node": "npm run build_node && node test.mjs"
14
+ "test_node": "npm run build && node test.mjs"
15
15
  },
16
16
  "repository": {
17
17
  "type": "git",
@@ -1 +0,0 @@
1
- (()=>{"use strict";function t(t,e){return function(t,e,r){if(r||2===arguments.length)for(var n,i=0,o=e.length;i<o;i++)!n&&i in e||(n||(n=Array.prototype.slice.call(e,0,i)),n[i]=e[i]);return t.concat(n||Array.prototype.slice.call(e))}([],Array(Math.ceil(t.length/e)),!0).map((function(r,n){return t.slice(n*e,n*e+e)}))}var e,r,n,i,o,s=String.fromCharCode(0),a=Math.floor(27647.5),c=a+1,u=[];function h(t){return t.split("").map((function(t){return t.charCodeAt(0)}))}function f(t){return t<a?t:-t+a}function l(t){if(t>=a||t<-a-1)throw new Error("INT_C Numbers must be within range -".concat(a+1," and ").concat(a));return String.fromCharCode(function(t){return t<0?-t+a:t}(t))}function p(t){if(0==t)return 1;for(var e=1,r=c;t>r;)e++,r*=c;return e}function d(t,e){if(0==t)return s;var r=t<0;t=Math.abs(t);for(var n="",i=0;i<e-1;i++){var o=Math.pow(c,e-i-1),u=Math.floor(t/o);n+=String.fromCharCode(u),t-=u*o}return n+=String.fromCharCode(t%c),r?h(n).map((function(t){return String.fromCharCode(t>0?t+a:t)})).join(""):n}function g(t){return h(t).reduce((function(t,e,r,n){return t+f(e)*function(t){var e;return null!==(e=u[t])&&void 0!==e?e:u[t]=Math.pow(c,t)}(n.length-r-1)}),0)}new TextEncoder,(o=i||(i={}))[o.NONE=0]="NONE",o[o.RAW=1]="RAW",o[o.STRING=1]="STRING",o[o.INTS_C=2]="INTS_C",o[o.INTS_D=3]="INTS_D",o[o.DECIMAL=4]="DECIMAL",o[o.BOOLEAN=6]="BOOLEAN";var y=((e={})[i.NONE]=function(t){return""==t},e[i.RAW]=function(){return!0},e[i.INTS_C]=function(t,e){return t.length==e},e[i.INTS_D]=function(t,e){return t.length>0&&h(t).length%t[0].charCodeAt(0)+1<=e},e[i.DECIMAL]=function(t,e){return t.length>0&&h(t).length%t[0].charCodeAt(0)*2+1<=e},e[i.BOOLEAN]=function(t){return t==s||""==t},e),v=((r={})[i.NONE]=function(t){return""},r[i.RAW]=function(t){return t},r[i.INTS_C]=function(t){return h(t).map(f)},r[i.INTS_D]=function(e){return t(h(e.substring(1)),e[0].charCodeAt(0)).map((function(t){return String.fromCharCode.apply(String,t)})).map(g)},r[i.DECIMAL]=function(e){var r=h(e),n=r.shift(),i=t(r,n).map((function(t){return String.fromCharCode.apply(String,t)})).map(g);return parseFloat(i[0]+"."+i[1])},r[i.BOOLEAN]=function(t){return t==s},r),C=((n={})[i.NONE]=function(t){return""},n[i.RAW]=function(t){return t.toString()},n[i.INTS_C]=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return t.map(l).join("")},n[i.INTS_D]=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var r=t.reduce((function(t,e){return Math.max(t,p(e))}),1),n=t.map((function(t){return d(t,r)})).join("");return String.fromCharCode(r)+n},n[i.DECIMAL]=function(t){var e=t.toString().split("."),r=parseFloat(e[0])||0,n=e.length>1&&parseFloat(e[1])||0,i=Math.max(p(r),p(n));return String.fromCharCode(i)+d(r,i)+d(n,i)},n[i.BOOLEAN]=function(t){return t?s:""},n),S=function(){function t(t,e,r,n){this.tag=t,this.type=e,this.dataCap=r,this.dontSpread=n}return t.prototype.serialize=function(){return"".concat(this.dontSpread?1:0).concat(String.fromCharCode(this.dataCap+1)).concat(String.fromCharCode(this.type+1)).concat(String.fromCharCode(this.tag.length+1)).concat(this.tag)},t.deserialize=function(e,r){var n="1"==e[r],i=e.charCodeAt(r+1)-1,o=e.charCodeAt(r+2)-1,s=e.charCodeAt(r+3)-1;return[new t(e.substring(r+4,r+4+s),o,i,n),s]},t.deserializeAll=function(t){for(var e=[],r=0;r<t.length;){var n=this.deserialize(t,r),i=n[0],o=n[1];e.push(i),r+=4+o}return e},t}(),m=function(){function t(t){this.key=1,this.keys={},this.tags={},this.packets=t,this.packetMap={},this.createPackets(t)}return t.prototype.createKey=function(t){t.includes(",")?console.log('Tag "'.concat(t,'" is invalid; keys cannot contain commas.')):(this.keys[t]=this.key,this.tags[this.key]=t,this.key++)},t.prototype.createPackets=function(t){for(var e=0,r=t;e<r.length;e++){var n=r[e];this.createKey(n.tag),this.packetMap[n.tag]=n}},t.prototype.get=function(t){return this.keys[t]},t.prototype.getChar=function(t){return String.fromCharCode(this.get(t))},t.prototype.getPacket=function(t){return this.packetMap[t]},t.prototype.has=function(t){return null!=this.tags[t.charCodeAt(0)]},t.prototype.getKeys=function(){return this.keys},t.prototype.serialize=function(){return this.packets.map((function(t){return t.serialize()})).join(",")},t.empty=function(){return new t([])},t}(),w=function(){function t(t,e){this.processor=v[t.type],this.validifier=y[t.type],this.listener=e,this.dontSpread=t.dontSpread,this.dataCap=t.dataCap}return t.prototype.listen=function(t){try{if(!this.validifier(t,this.dataCap))return!1;var e=this.processor(t);return Array.isArray(e)&&!this.dontSpread?this.listener.apply(this,e):this.listener(e),!0}catch(t){return!1}},t}();function k(t,e,r){for(var n=[],i=3;i<arguments.length;i++)n[i-3]=arguments[i];var o=t.getChar(r);if(null==o)throw new Error('Tag "'.concat(r,'" has not been created!'));var s=t.getPacket(r);if(n.length>s.dataCap)throw new Error('Packet "'.concat(r,'" only allows ').concat(s.dataCap," values!"));e(o+C[s.type].apply(C,n))}var A,E,N=function(){function t(t){var e=this;this.clientPackets=m.empty(),this.serverPackets=m.empty(),this.ws=t,this.listeners={message:[],close:[],event:{}},this.preListen={},this.keyHandler=function(t){return e.serverKeyHandler(t)},this.ws.addEventListener("message",this.keyHandler),this.ws.addEventListener("close",(function(t){e.listeners.close.forEach((function(e){return e(t)}))}))}return t.prototype.serverKeyHandler=function(t){var e=this,r=t.data.toString();if(!r.startsWith("SWS"))throw this.ws.close(),new Error("The server requested is not a Sonic WS server.");var n=r.charCodeAt(3);if(0!=n)throw this.ws.close(),new Error("Version mismatch: ".concat(n>0?"client":"server"," is outdated (server: ").concat(n,", client: ").concat(0,")"));var i=r.substring(4).split(s),o=i[0],a=i[1];this.clientPackets.createPackets(S.deserializeAll(o)),this.serverPackets.createPackets(S.deserializeAll(a)),Object.keys(this.preListen).forEach((function(t){return e.preListen[t].forEach((function(r){var n=e.serverPackets.getPacket(t),i=new w(n,r);e.listen(t,i)}))})),this.ws.removeEventListener("message",this.keyHandler),this.ws.addEventListener("message",(function(t){return e.messageHandler(t)}))},t.prototype.messageHandler=function(t){var e,r=t.data.toString();if(this.listeners.message.forEach((function(t){return t(r)})),!(r.length<1)){var n=r.substring(0,1),i=r.substring(1),o=n.charCodeAt(0);null!=o&&(null===(e=this.listeners.event[o])||void 0===e||e.forEach((function(t){return t.listen(i)})))}},t.prototype.listen=function(t,e){var r=this.serverPackets.get(t);r?(this.listeners.event[r]||(this.listeners.event[r]=[]),this.listeners.event[r].push(e)):console.log("Key is not available on server: "+t)},t.prototype.raw_onmessage=function(t){this.listeners.message.push(t)},t.prototype.raw_send=function(t){this.ws.send(t)},t.prototype.send=function(t){for(var e=this,r=[],n=1;n<arguments.length;n++)r[n-1]=arguments[n];k.apply(void 0,function(t,e,r){if(r||2===arguments.length)for(var n,i=0,o=e.length;i<o;i++)!n&&i in e||(n||(n=Array.prototype.slice.call(e,0,i)),n[i]=e[i]);return t.concat(n||Array.prototype.slice.call(e))}([this.clientPackets,function(t){return e.raw_send(t)},t],r,!1))},t.prototype.on_ready=function(t){this.ws.readyState===WebSocket.OPEN?t():this.ws.addEventListener("open",t)},t.prototype.on_close=function(t){this.listeners.close.push(t)},t.prototype.on=function(t,e){if(this.ws.readyState!==WebSocket.OPEN)return this.preListen[t]||(this.preListen[t]=[]),void this.preListen[t].push(e);var r=this.serverPackets.getPacket(t),n=new w(r,e);this.listen(t,n)},t}(),_=(A=function(t,e){return A=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},A(t,e)},function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function r(){this.constructor=t}A(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)});window.SonicWS=((E=function(t){function e(e,r){var n=new WebSocket(e,r);return t.call(this,n)||this}return _(e,t),e}(N)).PacketType=i,E)})();
package/webpack.config.js DELETED
@@ -1,17 +0,0 @@
1
- const path = require('path');
2
-
3
- module.exports = {
4
- entry: './html/ws/client/browser/ClientBrowser.js',
5
- output: {
6
- filename: 'SonicWS_bundle.js',
7
- path: path.resolve(__dirname, 'bundled'),
8
- },
9
- resolve: {
10
- extensions: ['.js'],
11
- },
12
- module: {
13
- rules: [
14
- ],
15
- },
16
- mode: 'production',
17
- };