@cloudflare/realtimekit-sockrates 0.0.0 → 0.1.1
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/LICENSE +201 -0
- package/README.md +18 -0
- package/dist/index.cjs.js +1 -0
- package/dist/index.es.js +629 -0
- package/dist/types/Proto.d.ts +6 -0
- package/dist/types/Sockrates.d.ts +142 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/proto/message/v1/message.d.ts +30 -0
- package/dist/types/util/JitterBackoff.d.ts +14 -0
- package/dist/types/util/Logger.d.ts +6 -0
- package/dist/types/util/SockratesLogger.d.ts +17 -0
- package/package.json +41 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Cloudflare
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<!-- PROJECT LOGO -->
|
|
2
|
+
<p align="center">
|
|
3
|
+
<a href="https://realtime.cloudflare.com">
|
|
4
|
+
<img src="https://docs.realtime.cloudflare.com/logo/cf.svg" alt="Logo" width="120">
|
|
5
|
+
</a>
|
|
6
|
+
|
|
7
|
+
<h3 align="center">RealtimeKit Socket Client</h3>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
A high-performance WebSocket client designed for scalability and automatic reconnection handling.
|
|
11
|
+
<br />
|
|
12
|
+
Used internally by <a href="https://www.npmjs.com/package/@cloudflare/realtimekit"><strong>@cloudflare/realtimekit</strong></a> for call quality monitoring.
|
|
13
|
+
<br />
|
|
14
|
+
<br />
|
|
15
|
+
<a href="https://developers.cloudflare.com/realtime/realtimekit/"><strong>Explore the docs »</strong></a>
|
|
16
|
+
<br />
|
|
17
|
+
</p>
|
|
18
|
+
</p>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var Le=Object.defineProperty;var we=(n,e,t)=>e in n?Le(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var ne=(n,e,t)=>(we(n,typeof e!="symbol"?e+"":e,t),t),X=(n,e,t)=>{if(!e.has(n))throw TypeError("Cannot "+t)};var i=(n,e,t)=>(X(n,e,"read from private field"),t?t.call(n):e.get(n)),a=(n,e,t)=>{if(e.has(n))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(n):e.set(n,t)},u=(n,e,t,s)=>(X(n,e,"write to private field"),s?s.call(n,t):e.set(n,t),t);var f=(n,e,t)=>(X(n,e,"access private method"),t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const be=require("@protobuf-ts/runtime");var W={},_e={get exports(){return W},set exports(n){W=n}},A=typeof Reflect=="object"?Reflect:null,ie=A&&typeof A.apply=="function"?A.apply:function(e,t,s){return Function.prototype.apply.call(e,t,s)},$;A&&typeof A.ownKeys=="function"?$=A.ownKeys:Object.getOwnPropertySymbols?$=function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:$=function(e){return Object.getOwnPropertyNames(e)};function Oe(n){console&&console.warn&&console.warn(n)}var oe=Number.isNaN||function(e){return e!==e};function h(){h.init.call(this)}_e.exports=h;W.once=Se;h.EventEmitter=h;h.prototype._events=void 0;h.prototype._eventsCount=0;h.prototype._maxListeners=void 0;var se=10;function V(n){if(typeof n!="function")throw new TypeError('The "listener" argument must be of type Function. Received type '+typeof n)}Object.defineProperty(h,"defaultMaxListeners",{enumerable:!0,get:function(){return se},set:function(n){if(typeof n!="number"||n<0||oe(n))throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received '+n+".");se=n}});h.init=function(){(this._events===void 0||this._events===Object.getPrototypeOf(this)._events)&&(this._events=Object.create(null),this._eventsCount=0),this._maxListeners=this._maxListeners||void 0};h.prototype.setMaxListeners=function(e){if(typeof e!="number"||e<0||oe(e))throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received '+e+".");return this._maxListeners=e,this};function ce(n){return n._maxListeners===void 0?h.defaultMaxListeners:n._maxListeners}h.prototype.getMaxListeners=function(){return ce(this)};h.prototype.emit=function(e){for(var t=[],s=1;s<arguments.length;s++)t.push(arguments[s]);var r=e==="error",c=this._events;if(c!==void 0)r=r&&c.error===void 0;else if(!r)return!1;if(r){var o;if(t.length>0&&(o=t[0]),o instanceof Error)throw o;var v=new Error("Unhandled error."+(o?" ("+o.message+")":""));throw v.context=o,v}var _=c[e];if(_===void 0)return!1;if(typeof _=="function")ie(_,this,t);else for(var I=_.length,G=de(_,I),s=0;s<I;++s)ie(G[s],this,t);return!0};function ue(n,e,t,s){var r,c,o;if(V(t),c=n._events,c===void 0?(c=n._events=Object.create(null),n._eventsCount=0):(c.newListener!==void 0&&(n.emit("newListener",e,t.listener?t.listener:t),c=n._events),o=c[e]),o===void 0)o=c[e]=t,++n._eventsCount;else if(typeof o=="function"?o=c[e]=s?[t,o]:[o,t]:s?o.unshift(t):o.push(t),r=ce(n),r>0&&o.length>r&&!o.warned){o.warned=!0;var v=new Error("Possible EventEmitter memory leak detected. "+o.length+" "+String(e)+" listeners added. Use emitter.setMaxListeners() to increase limit");v.name="MaxListenersExceededWarning",v.emitter=n,v.type=e,v.count=o.length,Oe(v)}return n}h.prototype.addListener=function(e,t){return ue(this,e,t,!1)};h.prototype.on=h.prototype.addListener;h.prototype.prependListener=function(e,t){return ue(this,e,t,!0)};function Te(){if(!this.fired)return this.target.removeListener(this.type,this.wrapFn),this.fired=!0,arguments.length===0?this.listener.call(this.target):this.listener.apply(this.target,arguments)}function he(n,e,t){var s={fired:!1,wrapFn:void 0,target:n,type:e,listener:t},r=Te.bind(s);return r.listener=t,s.wrapFn=r,r}h.prototype.once=function(e,t){return V(t),this.on(e,he(this,e,t)),this};h.prototype.prependOnceListener=function(e,t){return V(t),this.prependListener(e,he(this,e,t)),this};h.prototype.removeListener=function(e,t){var s,r,c,o,v;if(V(t),r=this._events,r===void 0)return this;if(s=r[e],s===void 0)return this;if(s===t||s.listener===t)--this._eventsCount===0?this._events=Object.create(null):(delete r[e],r.removeListener&&this.emit("removeListener",e,s.listener||t));else if(typeof s!="function"){for(c=-1,o=s.length-1;o>=0;o--)if(s[o]===t||s[o].listener===t){v=s[o].listener,c=o;break}if(c<0)return this;c===0?s.shift():Ee(s,c),s.length===1&&(r[e]=s[0]),r.removeListener!==void 0&&this.emit("removeListener",e,v||t)}return this};h.prototype.off=h.prototype.removeListener;h.prototype.removeAllListeners=function(e){var t,s,r;if(s=this._events,s===void 0)return this;if(s.removeListener===void 0)return arguments.length===0?(this._events=Object.create(null),this._eventsCount=0):s[e]!==void 0&&(--this._eventsCount===0?this._events=Object.create(null):delete s[e]),this;if(arguments.length===0){var c=Object.keys(s),o;for(r=0;r<c.length;++r)o=c[r],o!=="removeListener"&&this.removeAllListeners(o);return this.removeAllListeners("removeListener"),this._events=Object.create(null),this._eventsCount=0,this}if(t=s[e],typeof t=="function")this.removeListener(e,t);else if(t!==void 0)for(r=t.length-1;r>=0;r--)this.removeListener(e,t[r]);return this};function ae(n,e,t){var s=n._events;if(s===void 0)return[];var r=s[e];return r===void 0?[]:typeof r=="function"?t?[r.listener||r]:[r]:t?Ce(r):de(r,r.length)}h.prototype.listeners=function(e){return ae(this,e,!0)};h.prototype.rawListeners=function(e){return ae(this,e,!1)};h.listenerCount=function(n,e){return typeof n.listenerCount=="function"?n.listenerCount(e):fe.call(n,e)};h.prototype.listenerCount=fe;function fe(n){var e=this._events;if(e!==void 0){var t=e[n];if(typeof t=="function")return 1;if(t!==void 0)return t.length}return 0}h.prototype.eventNames=function(){return this._eventsCount>0?$(this._events):[]};function de(n,e){for(var t=new Array(e),s=0;s<e;++s)t[s]=n[s];return t}function Ee(n,e){for(;e+1<n.length;e++)n[e]=n[e+1];n.pop()}function Ce(n){for(var e=new Array(n.length),t=0;t<e.length;++t)e[t]=n[t].listener||n[t];return e}function Se(n,e){return new Promise(function(t,s){function r(o){n.removeListener(e,c),s(o)}function c(){typeof n.removeListener=="function"&&n.removeListener("error",r),t([].slice.call(arguments))}le(n,e,c,{once:!0}),e!=="error"&&xe(n,r,{once:!0})})}function xe(n,e,t){typeof n.on=="function"&&le(n,"error",e,t)}function le(n,e,t,s){if(typeof n.on=="function")s.once?n.once(e,t):n.on(e,t);else if(typeof n.addEventListener=="function")n.addEventListener(e,function r(c){s.once&&n.removeEventListener(e,r),t(c)});else throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof n)}class Re extends be.MessageType{constructor(){super("message.v1.SocketMessage",[{no:1,name:"event",kind:"scalar",T:13},{no:2,name:"id",kind:"scalar",opt:!0,T:9},{no:3,name:"payload",kind:"scalar",opt:!0,T:12},{no:4,name:"metadata",kind:"scalar",opt:!0,T:12}])}}const Y=new Re;class re{static encode(e){return Y.toBinary(e)}static decode(e){return Y.fromBinary(new Uint8Array(e))}}function Ae(n,e){return Math.floor(Math.random()*(e-n+1)+n)}var T;class Me{constructor(e={}){ne(this,"opts");a(this,T,void 0);this.opts={initialTimeout:e.initialTimeout||1e3,maxTimeout:e.maxTimeout||1e4,factor:e.factor||2},u(this,T,0)}async wait(){u(this,T,i(this,T)+1);const e=Ae(0,Math.min(this.opts.maxTimeout,this.opts.initialTimeout*2**i(this,T)));await new Promise(t=>{setTimeout(t,e)})}getAttempts(){return i(this,T)}reset(){u(this,T,0)}}T=new WeakMap;const O={debug:0,info:1,warn:2,error:3};var E,C;class Ne{constructor(e){a(this,E,void 0);a(this,C,void 0);u(this,E,console),u(this,C,e)}debug(...e){O[i(this,C)]>O.debug||i(this,E).debug("[Sockrates]:",...e)}info(...e){O[i(this,C)]>O.info||i(this,E).info("[Sockrates]:",...e)}warn(...e){O[i(this,C)]>O.warn||i(this,E).warn("[Sockrates]:",...e)}error(...e){O[i(this,C)]>O.error||i(this,E).error("[Sockrates]:",...e)}}E=new WeakMap,C=new WeakMap;var me=(n=>(n[n.CONNECTING=0]="CONNECTING",n[n.OPEN=1]="OPEN",n[n.CLOSING=2]="CLOSING",n[n.CLOSED=3]="CLOSED",n))(me||{});const Pe="2",ke="3";var l,S,d,m,w,p,y,b,g,x,L,B,pe,M,D,H,ve,J,ge,j,Z,z,ye,N,K,P,q,U,ee,R,F,k,Q;class Ie{constructor(e,t){a(this,B);a(this,M);a(this,H);a(this,J);a(this,j);a(this,z);a(this,N);a(this,P);a(this,U);a(this,R);a(this,k);a(this,l,void 0);a(this,S,void 0);a(this,d,void 0);a(this,m,void 0);a(this,w,void 0);a(this,p,void 0);a(this,y,void 0);a(this,b,void 0);a(this,g,void 0);a(this,x,void 0);a(this,L,void 0);var s,r,c,o,v,_,I,G,te;u(this,S,e),u(this,w,[]),u(this,p,new W),u(this,y,!0),u(this,g,!1),u(this,d,t??{}),(s=i(this,d)).autoReconnect??(s.autoReconnect=!0),(r=i(this,d)).retryConnectionInterval??(r.retryConnectionInterval=1e3),(c=i(this,d)).pingTimeout??(c.pingTimeout=3e4),(o=i(this,d)).connectionTimeout??(o.connectionTimeout=5e3),(v=i(this,d)).debug??(v.debug=!0),(_=i(this,d)).maxReconnectionAttempts??(_.maxReconnectionAttempts=10),(I=i(this,d)).disconnectOnPingTimeout??(I.disconnectOnPingTimeout=!0),(G=i(this,d)).queueOnDisconnect??(G.queueOnDisconnect=!1),(te=i(this,d)).flushOnReconnect??(te.flushOnReconnect=!1),u(this,b,{code:void 0,reason:void 0}),u(this,m,i(this,d).logger??new Ne(i(this,d).debug?"debug":"info")),u(this,L,new Me)}get readyState(){var e;return(e=i(this,l))==null?void 0:e.readyState}get url(){return i(this,S)}updateURL(e){u(this,S,e),f(this,z,ye).call(this)}get config(){return i(this,d)}get sendQueue(){return i(this,w)}flush(){if(!i(this,d).queueOnDisconnect)return!1;const e=[];return i(this,w).forEach(t=>{this.send(t.event,t.id,t.payload,t.metadata)||e.push(t)}),u(this,w,e),i(this,w)}async connect(e=!1){if(!e&&[0,1].includes(this.readyState)){i(this,m).debug("Websocket was already connecting or connected.");return}if(i(this,y)!==!1)return new Promise((t,s)=>{f(this,R,F).call(this),f(this,k,Q).call(this);try{u(this,l,new WebSocket(f(this,B,pe).call(this,i(this,S)))),i(this,l).binaryType="arraybuffer",i(this,m).debug("Connecting");const r=setTimeout(()=>{i(this,m).debug("Connection timeout. Closing socket"),u(this,y,!0),f(this,k,Q).call(this),i(this,l).close(3001,"Connection Timeout"),i(this,d).autoReconnect&&!i(this,g)&&(i(this,p).emit("reconnecting"),f(this,N,K).call(this)),s(new Error("Connection timed out!"))},i(this,d).connectionTimeout);i(this,l).onopen=()=>{i(this,m).debug(`Ready State: ${me[i(this,l).readyState]}`),r&&clearTimeout(r),f(this,U,ee).call(this),u(this,b,{code:void 0,reason:void 0}),i(this,p).emit("connected"),i(this,d).flushOnReconnect&&this.flush(),t()},i(this,l).onclose=c=>{try{r&&clearTimeout(r);const{code:o,reason:v}=c;s(v),i(this,m).debug("Socket closed. Close event:",{event:c}),i(this,m).debug(`Connection closed code: ${o}`),i(this,m).debug(`Connection closed reason: ${v}`),i(this,g)||f(this,j,Z).call(this,o,v)}catch(o){f(this,M,D).call(this,o)}},i(this,l).onerror=c=>{f(this,M,D).call(this,c)},i(this,l).onmessage=c=>f(this,H,ve).call(this,c)}catch(r){f(this,M,D).call(this,r,s)}})}send(e,t,s,r){const c={event:e,id:t,payload:s,metadata:r};if(i(this,d).queueOnDisconnect&&(!i(this,l)||i(this,l).readyState!==1))return i(this,m).debug("Queuing message since socket is not connected!",c),i(this,w).push(c),!1;const o=re.encode(c);return f(this,P,q).call(this,o)}emit(e,t,s,r){return this.send(e,t,s,r)}sendRaw(e){return f(this,P,q).call(this,e)}receive(e,t){return i(this,p).on(e.toString(),t)}on(e,t){if(typeof e=="string"&&(e==="connected"||e==="disconnected"||e==="errored"||e==="reconnected"||e==="reconnecting"||e==="reconnectAttempt"||e==="reconnectFailure"||e==="failed")){i(this,p).on(e,t);return}this.receive(e,t)}removeAllListeners(){i(this,p).removeAllListeners()}removeReceiver(e,t){this.removeListener(e,t)}removeListener(e,t){i(this,p).removeListener(e.toString(),t)}removeReceivers(e){this.removeListeners(e)}removeListeners(e){i(this,p).listeners(e.toString()).map(t=>this.removeListener(e,t))}disconnect(){u(this,y,!1),f(this,R,F).call(this),this.removeAllListeners(),u(this,b,{code:1e3,reason:"Sockrates disconnect method called"}),i(this,l).close(1e3,"Sockrates disconnect method called.")}}l=new WeakMap,S=new WeakMap,d=new WeakMap,m=new WeakMap,w=new WeakMap,p=new WeakMap,y=new WeakMap,b=new WeakMap,g=new WeakMap,x=new WeakMap,L=new WeakMap,B=new WeakSet,pe=function(e){if(e.startsWith("ws://")||e.startsWith("wss://"))return e;if(e.startsWith("https://"))return`wss://${e.substring(8)}`;if(e.startsWith("http://"))return`ws://${e.substring(7)}`;throw new Error("Invalid URL. URL must start with http(s):// or ws(s)://.")},M=new WeakSet,D=function(e,t){i(this,m).error("Error:",{error:e}),i(this,p).emit("errored",{error:e}),t==null||t(e)},H=new WeakSet,ve=function(e){if(f(this,U,ee).call(this),e.data===Pe){i(this,m).debug("Received ping from server"),f(this,P,q).call(this,ke);return}const t=re.decode(e.data),{id:s,payload:r}=t;i(this,m).debug("Received message",{event:t.event,messageID:s}),i(this,p).emit(t.event.toString(),{id:s,payload:r})},J=new WeakSet,ge=function(){return i(this,l).readyState===1},j=new WeakSet,Z=function(e,t){u(this,b,{reason:t,code:e}),i(this,p).emit("disconnected",{code:e,reason:t})},z=new WeakSet,ye=function(){const{reason:e,code:t}=i(this,b);t&&t!==1e3&&i(this,y)&&i(this,d).autoReconnect&&!i(this,g)&&(i(this,m).debug(`Triggering reconnection due to ${e}.`),i(this,p).emit("reconnecting"),f(this,N,K).call(this))},N=new WeakSet,K=async function(e=!0){if(e&&i(this,g)){i(this,m).debug("Reconnect called when already in a reconnect loop. Ignoring.");return}if(i(this,g)||i(this,L).reset(),i(this,d).maxReconnectionAttempts!==null&&i(this,L).getAttempts()>=i(this,d).maxReconnectionAttempts){i(this,p).emit("failed"),u(this,g,!1);return}u(this,g,!0),f(this,k,Q).call(this),f(this,R,F).call(this);try{if(await i(this,L).wait(),i(this,y)===!1)return;if(i(this,m).debug(`Reconnection attempt ${i(this,L).getAttempts()}`),i(this,p).emit("reconnectAttempt",{attempt:i(this,L).getAttempts()}),await this.connect(),!f(this,J,ge).call(this))throw Error("Reconnect Failed");u(this,g,!1),u(this,b,{code:void 0,reason:void 0}),i(this,p).emit("reconnected")}catch{i(this,m).debug("Failed to reconnect."),i(this,p).emit("reconnectFailure",{attempt:i(this,L).getAttempts()}),f(this,N,K).call(this,!1)}},P=new WeakSet,q=function(e){try{return i(this,l).send(e),!0}catch(t){return i(this,m).error(t.message),!1}},U=new WeakSet,ee=function(){this.config.disconnectOnPingTimeout&&(i(this,m).debug("Resetting ping timeout"),f(this,R,F).call(this),u(this,x,setTimeout(()=>{var s;i(this,m).debug("Disconnecting the socket due to ping timeout"),u(this,y,!0);const e=3002,t="Ping timeout";(s=i(this,l))==null||s.close(e,t),f(this,j,Z).call(this,e,t)},i(this,d).pingTimeout)))},R=new WeakSet,F=function(){i(this,x)&&(clearTimeout(i(this,x)),u(this,x,void 0))},k=new WeakSet,Q=function(){i(this,l)&&(i(this,l).onopen=void 0,i(this,l).onerror=void 0,i(this,l).onmessage=void 0,i(this,l).onclose=void 0)};exports.SocketMessage=Y;exports.default=Ie;
|
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
var Le = Object.defineProperty;
|
|
2
|
+
var we = (n, e, t) => e in n ? Le(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
|
|
3
|
+
var te = (n, e, t) => (we(n, typeof e != "symbol" ? e + "" : e, t), t), X = (n, e, t) => {
|
|
4
|
+
if (!e.has(n))
|
|
5
|
+
throw TypeError("Cannot " + t);
|
|
6
|
+
};
|
|
7
|
+
var i = (n, e, t) => (X(n, e, "read from private field"), t ? t.call(n) : e.get(n)), a = (n, e, t) => {
|
|
8
|
+
if (e.has(n))
|
|
9
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
10
|
+
e instanceof WeakSet ? e.add(n) : e.set(n, t);
|
|
11
|
+
}, u = (n, e, t, s) => (X(n, e, "write to private field"), s ? s.call(n, t) : e.set(n, t), t);
|
|
12
|
+
var f = (n, e, t) => (X(n, e, "access private method"), t);
|
|
13
|
+
import { MessageType as be } from "@protobuf-ts/runtime";
|
|
14
|
+
var q = {}, _e = {
|
|
15
|
+
get exports() {
|
|
16
|
+
return q;
|
|
17
|
+
},
|
|
18
|
+
set exports(n) {
|
|
19
|
+
q = n;
|
|
20
|
+
}
|
|
21
|
+
}, S = typeof Reflect == "object" ? Reflect : null, ne = S && typeof S.apply == "function" ? S.apply : function(e, t, s) {
|
|
22
|
+
return Function.prototype.apply.call(e, t, s);
|
|
23
|
+
}, $;
|
|
24
|
+
S && typeof S.ownKeys == "function" ? $ = S.ownKeys : Object.getOwnPropertySymbols ? $ = function(e) {
|
|
25
|
+
return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e));
|
|
26
|
+
} : $ = function(e) {
|
|
27
|
+
return Object.getOwnPropertyNames(e);
|
|
28
|
+
};
|
|
29
|
+
function Ee(n) {
|
|
30
|
+
console && console.warn && console.warn(n);
|
|
31
|
+
}
|
|
32
|
+
var oe = Number.isNaN || function(e) {
|
|
33
|
+
return e !== e;
|
|
34
|
+
};
|
|
35
|
+
function h() {
|
|
36
|
+
h.init.call(this);
|
|
37
|
+
}
|
|
38
|
+
_e.exports = h;
|
|
39
|
+
q.once = xe;
|
|
40
|
+
h.EventEmitter = h;
|
|
41
|
+
h.prototype._events = void 0;
|
|
42
|
+
h.prototype._eventsCount = 0;
|
|
43
|
+
h.prototype._maxListeners = void 0;
|
|
44
|
+
var ie = 10;
|
|
45
|
+
function V(n) {
|
|
46
|
+
if (typeof n != "function")
|
|
47
|
+
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof n);
|
|
48
|
+
}
|
|
49
|
+
Object.defineProperty(h, "defaultMaxListeners", {
|
|
50
|
+
enumerable: !0,
|
|
51
|
+
get: function() {
|
|
52
|
+
return ie;
|
|
53
|
+
},
|
|
54
|
+
set: function(n) {
|
|
55
|
+
if (typeof n != "number" || n < 0 || oe(n))
|
|
56
|
+
throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + n + ".");
|
|
57
|
+
ie = n;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
h.init = function() {
|
|
61
|
+
(this._events === void 0 || this._events === Object.getPrototypeOf(this)._events) && (this._events = /* @__PURE__ */ Object.create(null), this._eventsCount = 0), this._maxListeners = this._maxListeners || void 0;
|
|
62
|
+
};
|
|
63
|
+
h.prototype.setMaxListeners = function(e) {
|
|
64
|
+
if (typeof e != "number" || e < 0 || oe(e))
|
|
65
|
+
throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + e + ".");
|
|
66
|
+
return this._maxListeners = e, this;
|
|
67
|
+
};
|
|
68
|
+
function ce(n) {
|
|
69
|
+
return n._maxListeners === void 0 ? h.defaultMaxListeners : n._maxListeners;
|
|
70
|
+
}
|
|
71
|
+
h.prototype.getMaxListeners = function() {
|
|
72
|
+
return ce(this);
|
|
73
|
+
};
|
|
74
|
+
h.prototype.emit = function(e) {
|
|
75
|
+
for (var t = [], s = 1; s < arguments.length; s++)
|
|
76
|
+
t.push(arguments[s]);
|
|
77
|
+
var r = e === "error", c = this._events;
|
|
78
|
+
if (c !== void 0)
|
|
79
|
+
r = r && c.error === void 0;
|
|
80
|
+
else if (!r)
|
|
81
|
+
return !1;
|
|
82
|
+
if (r) {
|
|
83
|
+
var o;
|
|
84
|
+
if (t.length > 0 && (o = t[0]), o instanceof Error)
|
|
85
|
+
throw o;
|
|
86
|
+
var v = new Error("Unhandled error." + (o ? " (" + o.message + ")" : ""));
|
|
87
|
+
throw v.context = o, v;
|
|
88
|
+
}
|
|
89
|
+
var _ = c[e];
|
|
90
|
+
if (_ === void 0)
|
|
91
|
+
return !1;
|
|
92
|
+
if (typeof _ == "function")
|
|
93
|
+
ne(_, this, t);
|
|
94
|
+
else
|
|
95
|
+
for (var I = _.length, G = de(_, I), s = 0; s < I; ++s)
|
|
96
|
+
ne(G[s], this, t);
|
|
97
|
+
return !0;
|
|
98
|
+
};
|
|
99
|
+
function ue(n, e, t, s) {
|
|
100
|
+
var r, c, o;
|
|
101
|
+
if (V(t), c = n._events, c === void 0 ? (c = n._events = /* @__PURE__ */ Object.create(null), n._eventsCount = 0) : (c.newListener !== void 0 && (n.emit(
|
|
102
|
+
"newListener",
|
|
103
|
+
e,
|
|
104
|
+
t.listener ? t.listener : t
|
|
105
|
+
), c = n._events), o = c[e]), o === void 0)
|
|
106
|
+
o = c[e] = t, ++n._eventsCount;
|
|
107
|
+
else if (typeof o == "function" ? o = c[e] = s ? [t, o] : [o, t] : s ? o.unshift(t) : o.push(t), r = ce(n), r > 0 && o.length > r && !o.warned) {
|
|
108
|
+
o.warned = !0;
|
|
109
|
+
var v = new Error("Possible EventEmitter memory leak detected. " + o.length + " " + String(e) + " listeners added. Use emitter.setMaxListeners() to increase limit");
|
|
110
|
+
v.name = "MaxListenersExceededWarning", v.emitter = n, v.type = e, v.count = o.length, Ee(v);
|
|
111
|
+
}
|
|
112
|
+
return n;
|
|
113
|
+
}
|
|
114
|
+
h.prototype.addListener = function(e, t) {
|
|
115
|
+
return ue(this, e, t, !1);
|
|
116
|
+
};
|
|
117
|
+
h.prototype.on = h.prototype.addListener;
|
|
118
|
+
h.prototype.prependListener = function(e, t) {
|
|
119
|
+
return ue(this, e, t, !0);
|
|
120
|
+
};
|
|
121
|
+
function Oe() {
|
|
122
|
+
if (!this.fired)
|
|
123
|
+
return this.target.removeListener(this.type, this.wrapFn), this.fired = !0, arguments.length === 0 ? this.listener.call(this.target) : this.listener.apply(this.target, arguments);
|
|
124
|
+
}
|
|
125
|
+
function he(n, e, t) {
|
|
126
|
+
var s = { fired: !1, wrapFn: void 0, target: n, type: e, listener: t }, r = Oe.bind(s);
|
|
127
|
+
return r.listener = t, s.wrapFn = r, r;
|
|
128
|
+
}
|
|
129
|
+
h.prototype.once = function(e, t) {
|
|
130
|
+
return V(t), this.on(e, he(this, e, t)), this;
|
|
131
|
+
};
|
|
132
|
+
h.prototype.prependOnceListener = function(e, t) {
|
|
133
|
+
return V(t), this.prependListener(e, he(this, e, t)), this;
|
|
134
|
+
};
|
|
135
|
+
h.prototype.removeListener = function(e, t) {
|
|
136
|
+
var s, r, c, o, v;
|
|
137
|
+
if (V(t), r = this._events, r === void 0)
|
|
138
|
+
return this;
|
|
139
|
+
if (s = r[e], s === void 0)
|
|
140
|
+
return this;
|
|
141
|
+
if (s === t || s.listener === t)
|
|
142
|
+
--this._eventsCount === 0 ? this._events = /* @__PURE__ */ Object.create(null) : (delete r[e], r.removeListener && this.emit("removeListener", e, s.listener || t));
|
|
143
|
+
else if (typeof s != "function") {
|
|
144
|
+
for (c = -1, o = s.length - 1; o >= 0; o--)
|
|
145
|
+
if (s[o] === t || s[o].listener === t) {
|
|
146
|
+
v = s[o].listener, c = o;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
if (c < 0)
|
|
150
|
+
return this;
|
|
151
|
+
c === 0 ? s.shift() : Te(s, c), s.length === 1 && (r[e] = s[0]), r.removeListener !== void 0 && this.emit("removeListener", e, v || t);
|
|
152
|
+
}
|
|
153
|
+
return this;
|
|
154
|
+
};
|
|
155
|
+
h.prototype.off = h.prototype.removeListener;
|
|
156
|
+
h.prototype.removeAllListeners = function(e) {
|
|
157
|
+
var t, s, r;
|
|
158
|
+
if (s = this._events, s === void 0)
|
|
159
|
+
return this;
|
|
160
|
+
if (s.removeListener === void 0)
|
|
161
|
+
return arguments.length === 0 ? (this._events = /* @__PURE__ */ Object.create(null), this._eventsCount = 0) : s[e] !== void 0 && (--this._eventsCount === 0 ? this._events = /* @__PURE__ */ Object.create(null) : delete s[e]), this;
|
|
162
|
+
if (arguments.length === 0) {
|
|
163
|
+
var c = Object.keys(s), o;
|
|
164
|
+
for (r = 0; r < c.length; ++r)
|
|
165
|
+
o = c[r], o !== "removeListener" && this.removeAllListeners(o);
|
|
166
|
+
return this.removeAllListeners("removeListener"), this._events = /* @__PURE__ */ Object.create(null), this._eventsCount = 0, this;
|
|
167
|
+
}
|
|
168
|
+
if (t = s[e], typeof t == "function")
|
|
169
|
+
this.removeListener(e, t);
|
|
170
|
+
else if (t !== void 0)
|
|
171
|
+
for (r = t.length - 1; r >= 0; r--)
|
|
172
|
+
this.removeListener(e, t[r]);
|
|
173
|
+
return this;
|
|
174
|
+
};
|
|
175
|
+
function ae(n, e, t) {
|
|
176
|
+
var s = n._events;
|
|
177
|
+
if (s === void 0)
|
|
178
|
+
return [];
|
|
179
|
+
var r = s[e];
|
|
180
|
+
return r === void 0 ? [] : typeof r == "function" ? t ? [r.listener || r] : [r] : t ? Ce(r) : de(r, r.length);
|
|
181
|
+
}
|
|
182
|
+
h.prototype.listeners = function(e) {
|
|
183
|
+
return ae(this, e, !0);
|
|
184
|
+
};
|
|
185
|
+
h.prototype.rawListeners = function(e) {
|
|
186
|
+
return ae(this, e, !1);
|
|
187
|
+
};
|
|
188
|
+
h.listenerCount = function(n, e) {
|
|
189
|
+
return typeof n.listenerCount == "function" ? n.listenerCount(e) : fe.call(n, e);
|
|
190
|
+
};
|
|
191
|
+
h.prototype.listenerCount = fe;
|
|
192
|
+
function fe(n) {
|
|
193
|
+
var e = this._events;
|
|
194
|
+
if (e !== void 0) {
|
|
195
|
+
var t = e[n];
|
|
196
|
+
if (typeof t == "function")
|
|
197
|
+
return 1;
|
|
198
|
+
if (t !== void 0)
|
|
199
|
+
return t.length;
|
|
200
|
+
}
|
|
201
|
+
return 0;
|
|
202
|
+
}
|
|
203
|
+
h.prototype.eventNames = function() {
|
|
204
|
+
return this._eventsCount > 0 ? $(this._events) : [];
|
|
205
|
+
};
|
|
206
|
+
function de(n, e) {
|
|
207
|
+
for (var t = new Array(e), s = 0; s < e; ++s)
|
|
208
|
+
t[s] = n[s];
|
|
209
|
+
return t;
|
|
210
|
+
}
|
|
211
|
+
function Te(n, e) {
|
|
212
|
+
for (; e + 1 < n.length; e++)
|
|
213
|
+
n[e] = n[e + 1];
|
|
214
|
+
n.pop();
|
|
215
|
+
}
|
|
216
|
+
function Ce(n) {
|
|
217
|
+
for (var e = new Array(n.length), t = 0; t < e.length; ++t)
|
|
218
|
+
e[t] = n[t].listener || n[t];
|
|
219
|
+
return e;
|
|
220
|
+
}
|
|
221
|
+
function xe(n, e) {
|
|
222
|
+
return new Promise(function(t, s) {
|
|
223
|
+
function r(o) {
|
|
224
|
+
n.removeListener(e, c), s(o);
|
|
225
|
+
}
|
|
226
|
+
function c() {
|
|
227
|
+
typeof n.removeListener == "function" && n.removeListener("error", r), t([].slice.call(arguments));
|
|
228
|
+
}
|
|
229
|
+
le(n, e, c, { once: !0 }), e !== "error" && Re(n, r, { once: !0 });
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
function Re(n, e, t) {
|
|
233
|
+
typeof n.on == "function" && le(n, "error", e, t);
|
|
234
|
+
}
|
|
235
|
+
function le(n, e, t, s) {
|
|
236
|
+
if (typeof n.on == "function")
|
|
237
|
+
s.once ? n.once(e, t) : n.on(e, t);
|
|
238
|
+
else if (typeof n.addEventListener == "function")
|
|
239
|
+
n.addEventListener(e, function r(c) {
|
|
240
|
+
s.once && n.removeEventListener(e, r), t(c);
|
|
241
|
+
});
|
|
242
|
+
else
|
|
243
|
+
throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof n);
|
|
244
|
+
}
|
|
245
|
+
class Ae extends be {
|
|
246
|
+
constructor() {
|
|
247
|
+
super("message.v1.SocketMessage", [
|
|
248
|
+
{
|
|
249
|
+
no: 1,
|
|
250
|
+
name: "event",
|
|
251
|
+
kind: "scalar",
|
|
252
|
+
T: 13
|
|
253
|
+
/*ScalarType.UINT32*/
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
no: 2,
|
|
257
|
+
name: "id",
|
|
258
|
+
kind: "scalar",
|
|
259
|
+
opt: !0,
|
|
260
|
+
T: 9
|
|
261
|
+
/*ScalarType.STRING*/
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
no: 3,
|
|
265
|
+
name: "payload",
|
|
266
|
+
kind: "scalar",
|
|
267
|
+
opt: !0,
|
|
268
|
+
T: 12
|
|
269
|
+
/*ScalarType.BYTES*/
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
no: 4,
|
|
273
|
+
name: "metadata",
|
|
274
|
+
kind: "scalar",
|
|
275
|
+
opt: !0,
|
|
276
|
+
T: 12
|
|
277
|
+
/*ScalarType.BYTES*/
|
|
278
|
+
}
|
|
279
|
+
]);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const se = new Ae();
|
|
283
|
+
class re {
|
|
284
|
+
static encode(e) {
|
|
285
|
+
return se.toBinary(e);
|
|
286
|
+
}
|
|
287
|
+
static decode(e) {
|
|
288
|
+
return se.fromBinary(new Uint8Array(e));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function Se(n, e) {
|
|
292
|
+
return Math.floor(Math.random() * (e - n + 1) + n);
|
|
293
|
+
}
|
|
294
|
+
var O;
|
|
295
|
+
class Ne {
|
|
296
|
+
constructor(e = {}) {
|
|
297
|
+
te(this, "opts");
|
|
298
|
+
a(this, O, void 0);
|
|
299
|
+
this.opts = {
|
|
300
|
+
initialTimeout: e.initialTimeout || 1e3,
|
|
301
|
+
maxTimeout: e.maxTimeout || 1e4,
|
|
302
|
+
factor: e.factor || 2
|
|
303
|
+
}, u(this, O, 0);
|
|
304
|
+
}
|
|
305
|
+
async wait() {
|
|
306
|
+
u(this, O, i(this, O) + 1);
|
|
307
|
+
const e = Se(0, Math.min(this.opts.maxTimeout, this.opts.initialTimeout * 2 ** i(this, O)));
|
|
308
|
+
await new Promise((t) => {
|
|
309
|
+
setTimeout(t, e);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
getAttempts() {
|
|
313
|
+
return i(this, O);
|
|
314
|
+
}
|
|
315
|
+
reset() {
|
|
316
|
+
u(this, O, 0);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
O = new WeakMap();
|
|
320
|
+
const E = {
|
|
321
|
+
debug: 0,
|
|
322
|
+
info: 1,
|
|
323
|
+
warn: 2,
|
|
324
|
+
error: 3
|
|
325
|
+
};
|
|
326
|
+
var T, C;
|
|
327
|
+
class Me {
|
|
328
|
+
constructor(e) {
|
|
329
|
+
a(this, T, void 0);
|
|
330
|
+
a(this, C, void 0);
|
|
331
|
+
u(this, T, console), u(this, C, e);
|
|
332
|
+
}
|
|
333
|
+
debug(...e) {
|
|
334
|
+
E[i(this, C)] > E.debug || i(this, T).debug("[Sockrates]:", ...e);
|
|
335
|
+
}
|
|
336
|
+
info(...e) {
|
|
337
|
+
E[i(this, C)] > E.info || i(this, T).info("[Sockrates]:", ...e);
|
|
338
|
+
}
|
|
339
|
+
warn(...e) {
|
|
340
|
+
E[i(this, C)] > E.warn || i(this, T).warn("[Sockrates]:", ...e);
|
|
341
|
+
}
|
|
342
|
+
error(...e) {
|
|
343
|
+
E[i(this, C)] > E.error || i(this, T).error("[Sockrates]:", ...e);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
T = new WeakMap(), C = new WeakMap();
|
|
347
|
+
var me = /* @__PURE__ */ ((n) => (n[n.CONNECTING = 0] = "CONNECTING", n[n.OPEN = 1] = "OPEN", n[n.CLOSING = 2] = "CLOSING", n[n.CLOSED = 3] = "CLOSED", n))(me || {});
|
|
348
|
+
const Pe = "2", ke = "3";
|
|
349
|
+
var l, x, d, m, w, p, y, b, g, R, L, B, pe, N, D, H, ve, J, ge, U, Y, z, ye, M, K, P, Q, j, Z, A, F, k, W;
|
|
350
|
+
class Ue {
|
|
351
|
+
constructor(e, t) {
|
|
352
|
+
a(this, B);
|
|
353
|
+
a(this, N);
|
|
354
|
+
a(this, H);
|
|
355
|
+
a(this, J);
|
|
356
|
+
/** NOTE(ishita1805): Updates disconnection state & emits disconnected event */
|
|
357
|
+
a(this, U);
|
|
358
|
+
/**
|
|
359
|
+
* NOTE(ishita1805): `_reconnect` is only called after the web-core updates the socket URL.
|
|
360
|
+
*/
|
|
361
|
+
a(this, z);
|
|
362
|
+
a(this, M);
|
|
363
|
+
a(this, P);
|
|
364
|
+
a(this, j);
|
|
365
|
+
a(this, A);
|
|
366
|
+
a(this, k);
|
|
367
|
+
a(this, l, void 0);
|
|
368
|
+
a(this, x, void 0);
|
|
369
|
+
a(this, d, void 0);
|
|
370
|
+
a(this, m, void 0);
|
|
371
|
+
a(this, w, void 0);
|
|
372
|
+
a(this, p, void 0);
|
|
373
|
+
a(this, y, void 0);
|
|
374
|
+
a(this, b, void 0);
|
|
375
|
+
a(this, g, void 0);
|
|
376
|
+
a(this, R, void 0);
|
|
377
|
+
a(this, L, void 0);
|
|
378
|
+
var s, r, c, o, v, _, I, G, ee;
|
|
379
|
+
u(this, x, e), u(this, w, []), u(this, p, new q()), u(this, y, !0), u(this, g, !1), u(this, d, t ?? {}), (s = i(this, d)).autoReconnect ?? (s.autoReconnect = !0), (r = i(this, d)).retryConnectionInterval ?? (r.retryConnectionInterval = 1e3), (c = i(this, d)).pingTimeout ?? (c.pingTimeout = 3e4), (o = i(this, d)).connectionTimeout ?? (o.connectionTimeout = 5e3), (v = i(this, d)).debug ?? (v.debug = !0), (_ = i(this, d)).maxReconnectionAttempts ?? (_.maxReconnectionAttempts = 10), (I = i(this, d)).disconnectOnPingTimeout ?? (I.disconnectOnPingTimeout = !0), (G = i(this, d)).queueOnDisconnect ?? (G.queueOnDisconnect = !1), (ee = i(this, d)).flushOnReconnect ?? (ee.flushOnReconnect = !1), u(this, b, {
|
|
380
|
+
code: void 0,
|
|
381
|
+
reason: void 0
|
|
382
|
+
}), u(this, m, i(this, d).logger ?? new Me(i(this, d).debug ? "debug" : "info")), u(this, L, new Ne());
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Ready state of the current WebSocket.
|
|
386
|
+
*/
|
|
387
|
+
get readyState() {
|
|
388
|
+
var e;
|
|
389
|
+
return (e = i(this, l)) == null ? void 0 : e.readyState;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Sockrates connection URL.
|
|
393
|
+
*/
|
|
394
|
+
get url() {
|
|
395
|
+
return i(this, x);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Update connection URL.
|
|
399
|
+
*/
|
|
400
|
+
updateURL(e) {
|
|
401
|
+
u(this, x, e), f(this, z, ye).call(this);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Configuration options passed to Sockrates.
|
|
405
|
+
*/
|
|
406
|
+
get config() {
|
|
407
|
+
return i(this, d);
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Messages that got queued due to the
|
|
411
|
+
* socket not being connected.
|
|
412
|
+
*/
|
|
413
|
+
get sendQueue() {
|
|
414
|
+
return i(this, w);
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Flushes all messages that were queued
|
|
418
|
+
* due to the socket not being connected.
|
|
419
|
+
*/
|
|
420
|
+
flush() {
|
|
421
|
+
if (!i(this, d).queueOnDisconnect)
|
|
422
|
+
return !1;
|
|
423
|
+
const e = [];
|
|
424
|
+
return i(this, w).forEach((t) => {
|
|
425
|
+
this.send(t.event, t.id, t.payload, t.metadata) || e.push(t);
|
|
426
|
+
}), u(this, w, e), i(this, w);
|
|
427
|
+
}
|
|
428
|
+
async connect(e = !1) {
|
|
429
|
+
if (!e && [
|
|
430
|
+
0,
|
|
431
|
+
1
|
|
432
|
+
/* OPEN */
|
|
433
|
+
].includes(this.readyState)) {
|
|
434
|
+
i(this, m).debug("Websocket was already connecting or connected.");
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (i(this, y) !== !1)
|
|
438
|
+
return new Promise((t, s) => {
|
|
439
|
+
f(this, A, F).call(this), f(this, k, W).call(this);
|
|
440
|
+
try {
|
|
441
|
+
u(this, l, new WebSocket(f(this, B, pe).call(this, i(this, x)))), i(this, l).binaryType = "arraybuffer", i(this, m).debug("Connecting");
|
|
442
|
+
const r = setTimeout(() => {
|
|
443
|
+
i(this, m).debug("Connection timeout. Closing socket"), u(this, y, !0), f(this, k, W).call(this), i(this, l).close(3001, "Connection Timeout"), i(this, d).autoReconnect && !i(this, g) && (i(this, p).emit(
|
|
444
|
+
"reconnecting"
|
|
445
|
+
/* reconnecting */
|
|
446
|
+
), f(this, M, K).call(this)), s(new Error("Connection timed out!"));
|
|
447
|
+
}, i(this, d).connectionTimeout);
|
|
448
|
+
i(this, l).onopen = () => {
|
|
449
|
+
i(this, m).debug(`Ready State: ${me[i(this, l).readyState]}`), r && clearTimeout(r), f(this, j, Z).call(this), u(this, b, {
|
|
450
|
+
code: void 0,
|
|
451
|
+
reason: void 0
|
|
452
|
+
}), i(this, p).emit(
|
|
453
|
+
"connected"
|
|
454
|
+
/* connected */
|
|
455
|
+
), i(this, d).flushOnReconnect && this.flush(), t();
|
|
456
|
+
}, i(this, l).onclose = (c) => {
|
|
457
|
+
try {
|
|
458
|
+
r && clearTimeout(r);
|
|
459
|
+
const { code: o, reason: v } = c;
|
|
460
|
+
s(v), i(this, m).debug("Socket closed. Close event:", { event: c }), i(this, m).debug(`Connection closed code: ${o}`), i(this, m).debug(`Connection closed reason: ${v}`), i(this, g) || f(this, U, Y).call(this, o, v);
|
|
461
|
+
} catch (o) {
|
|
462
|
+
f(this, N, D).call(this, o);
|
|
463
|
+
}
|
|
464
|
+
}, i(this, l).onerror = (c) => {
|
|
465
|
+
f(this, N, D).call(this, c);
|
|
466
|
+
}, i(this, l).onmessage = (c) => f(this, H, ve).call(this, c);
|
|
467
|
+
} catch (r) {
|
|
468
|
+
f(this, N, D).call(this, r, s);
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Send a message to the socket server.
|
|
474
|
+
* @returns {boolean} Returns true if the payload was sent successfully.
|
|
475
|
+
*/
|
|
476
|
+
send(e, t, s, r) {
|
|
477
|
+
const c = { event: e, id: t, payload: s, metadata: r };
|
|
478
|
+
if (i(this, d).queueOnDisconnect && (!i(this, l) || i(this, l).readyState !== 1))
|
|
479
|
+
return i(this, m).debug("Queuing message since socket is not connected!", c), i(this, w).push(c), !1;
|
|
480
|
+
const o = re.encode(c);
|
|
481
|
+
return f(this, P, Q).call(this, o);
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Alias for `send`.
|
|
485
|
+
*/
|
|
486
|
+
emit(e, t, s, r) {
|
|
487
|
+
return this.send(e, t, s, r);
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Send a raw message to the socket server.
|
|
491
|
+
*/
|
|
492
|
+
sendRaw(e) {
|
|
493
|
+
return f(this, P, Q).call(this, e);
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Alias to `on`.
|
|
497
|
+
*/
|
|
498
|
+
receive(e, t) {
|
|
499
|
+
return i(this, p).on(e.toString(), t);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Event listener that is fired on receiving socket messages.
|
|
503
|
+
*/
|
|
504
|
+
on(e, t) {
|
|
505
|
+
if (typeof e == "string" && (e === "connected" || e === "disconnected" || e === "errored" || e === "reconnected" || e === "reconnecting" || e === "reconnectAttempt" || e === "reconnectFailure" || e === "failed")) {
|
|
506
|
+
i(this, p).on(e, t);
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
this.receive(e, t);
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Remove all event listeners that were added using `on`.
|
|
513
|
+
*/
|
|
514
|
+
removeAllListeners() {
|
|
515
|
+
i(this, p).removeAllListeners();
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Alias to `removeListener`.
|
|
519
|
+
*/
|
|
520
|
+
removeReceiver(e, t) {
|
|
521
|
+
this.removeListener(e, t);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Remove a specific listener that was added using `on`.
|
|
525
|
+
*/
|
|
526
|
+
removeListener(e, t) {
|
|
527
|
+
i(this, p).removeListener(e.toString(), t);
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Alias to `removeListeners`.
|
|
531
|
+
*/
|
|
532
|
+
removeReceivers(e) {
|
|
533
|
+
this.removeListeners(e);
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Remove all listeners for a specific event.
|
|
537
|
+
*/
|
|
538
|
+
removeListeners(e) {
|
|
539
|
+
i(this, p).listeners(e.toString()).map(
|
|
540
|
+
(t) => this.removeListener(e, t)
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Disconnect the socket.
|
|
545
|
+
*/
|
|
546
|
+
disconnect() {
|
|
547
|
+
u(this, y, !1), f(this, A, F).call(this), this.removeAllListeners(), u(this, b, {
|
|
548
|
+
code: 1e3,
|
|
549
|
+
reason: "Sockrates disconnect method called"
|
|
550
|
+
}), i(this, l).close(1e3, "Sockrates disconnect method called.");
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
l = new WeakMap(), x = new WeakMap(), d = new WeakMap(), m = new WeakMap(), w = new WeakMap(), p = new WeakMap(), y = new WeakMap(), b = new WeakMap(), g = new WeakMap(), R = new WeakMap(), L = new WeakMap(), B = new WeakSet(), pe = function(e) {
|
|
554
|
+
if (e.startsWith("ws://") || e.startsWith("wss://"))
|
|
555
|
+
return e;
|
|
556
|
+
if (e.startsWith("https://"))
|
|
557
|
+
return `wss://${e.substring(8)}`;
|
|
558
|
+
if (e.startsWith("http://"))
|
|
559
|
+
return `ws://${e.substring(7)}`;
|
|
560
|
+
throw new Error("Invalid URL. URL must start with http(s):// or ws(s)://.");
|
|
561
|
+
}, N = new WeakSet(), D = function(e, t) {
|
|
562
|
+
i(this, m).error("Error:", { error: e }), i(this, p).emit("errored", { error: e }), t == null || t(e);
|
|
563
|
+
}, H = new WeakSet(), ve = function(e) {
|
|
564
|
+
if (f(this, j, Z).call(this), e.data === Pe) {
|
|
565
|
+
i(this, m).debug("Received ping from server"), f(this, P, Q).call(this, ke);
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
const t = re.decode(e.data), { id: s, payload: r } = t;
|
|
569
|
+
i(this, m).debug("Received message", { event: t.event, messageID: s }), i(this, p).emit(t.event.toString(), { id: s, payload: r });
|
|
570
|
+
}, J = new WeakSet(), ge = function() {
|
|
571
|
+
return i(this, l).readyState === 1;
|
|
572
|
+
}, U = new WeakSet(), Y = function(e, t) {
|
|
573
|
+
u(this, b, { reason: t, code: e }), i(this, p).emit("disconnected", { code: e, reason: t });
|
|
574
|
+
}, z = new WeakSet(), ye = function() {
|
|
575
|
+
const { reason: e, code: t } = i(this, b);
|
|
576
|
+
t && t !== 1e3 && i(this, y) && i(this, d).autoReconnect && !i(this, g) && (i(this, m).debug(`Triggering reconnection due to ${e}.`), i(this, p).emit(
|
|
577
|
+
"reconnecting"
|
|
578
|
+
/* reconnecting */
|
|
579
|
+
), f(this, M, K).call(this));
|
|
580
|
+
}, M = new WeakSet(), K = async function(e = !0) {
|
|
581
|
+
if (e && i(this, g)) {
|
|
582
|
+
i(this, m).debug("Reconnect called when already in a reconnect loop. Ignoring.");
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
if (i(this, g) || i(this, L).reset(), i(this, d).maxReconnectionAttempts !== null && i(this, L).getAttempts() >= i(this, d).maxReconnectionAttempts) {
|
|
586
|
+
i(this, p).emit(
|
|
587
|
+
"failed"
|
|
588
|
+
/* failed */
|
|
589
|
+
), u(this, g, !1);
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
u(this, g, !0), f(this, k, W).call(this), f(this, A, F).call(this);
|
|
593
|
+
try {
|
|
594
|
+
if (await i(this, L).wait(), i(this, y) === !1)
|
|
595
|
+
return;
|
|
596
|
+
if (i(this, m).debug(`Reconnection attempt ${i(this, L).getAttempts()}`), i(this, p).emit("reconnectAttempt", { attempt: i(this, L).getAttempts() }), await this.connect(), !f(this, J, ge).call(this))
|
|
597
|
+
throw Error("Reconnect Failed");
|
|
598
|
+
u(this, g, !1), u(this, b, {
|
|
599
|
+
code: void 0,
|
|
600
|
+
reason: void 0
|
|
601
|
+
}), i(this, p).emit(
|
|
602
|
+
"reconnected"
|
|
603
|
+
/* reconnected */
|
|
604
|
+
);
|
|
605
|
+
} catch {
|
|
606
|
+
i(this, m).debug("Failed to reconnect."), i(this, p).emit("reconnectFailure", { attempt: i(this, L).getAttempts() }), f(this, M, K).call(this, !1);
|
|
607
|
+
}
|
|
608
|
+
}, P = new WeakSet(), Q = function(e) {
|
|
609
|
+
try {
|
|
610
|
+
return i(this, l).send(e), !0;
|
|
611
|
+
} catch (t) {
|
|
612
|
+
return i(this, m).error(t.message), !1;
|
|
613
|
+
}
|
|
614
|
+
}, j = new WeakSet(), Z = function() {
|
|
615
|
+
this.config.disconnectOnPingTimeout && (i(this, m).debug("Resetting ping timeout"), f(this, A, F).call(this), u(this, R, setTimeout(() => {
|
|
616
|
+
var s;
|
|
617
|
+
i(this, m).debug("Disconnecting the socket due to ping timeout"), u(this, y, !0);
|
|
618
|
+
const e = 3002, t = "Ping timeout";
|
|
619
|
+
(s = i(this, l)) == null || s.close(e, t), f(this, U, Y).call(this, e, t);
|
|
620
|
+
}, i(this, d).pingTimeout)));
|
|
621
|
+
}, A = new WeakSet(), F = function() {
|
|
622
|
+
i(this, R) && (clearTimeout(i(this, R)), u(this, R, void 0));
|
|
623
|
+
}, k = new WeakSet(), W = function() {
|
|
624
|
+
i(this, l) && (i(this, l).onopen = void 0, i(this, l).onerror = void 0, i(this, l).onmessage = void 0, i(this, l).onclose = void 0);
|
|
625
|
+
};
|
|
626
|
+
export {
|
|
627
|
+
se as SocketMessage,
|
|
628
|
+
Ue as default
|
|
629
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import { SocketMessage } from './Proto';
|
|
3
|
+
import Logger from './util/Logger';
|
|
4
|
+
export interface SockratesConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Automatically reconnect on abnormal closure.
|
|
7
|
+
*/
|
|
8
|
+
autoReconnect?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Reconnection attempt interval in milliseconds.
|
|
11
|
+
*/
|
|
12
|
+
retryConnectionInterval?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Connection Timeout (The time we wait (in milliseconds) for the connection to be established)
|
|
15
|
+
*/
|
|
16
|
+
connectionTimeout?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Ping timeout (The time (in ms) after which if we don't receive a ping from the server, we treat the connection
|
|
19
|
+
* as disconnected)
|
|
20
|
+
*/
|
|
21
|
+
pingTimeout?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Enable debug mode.
|
|
24
|
+
* This works only with the default logger in Sockrates.
|
|
25
|
+
*/
|
|
26
|
+
debug?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* If set, we will switch to "failed" state if the number of reconnection attempts passes max attempts.
|
|
29
|
+
* Set this to zero to allow infinite attempts.
|
|
30
|
+
*/
|
|
31
|
+
maxReconnectionAttempts?: number;
|
|
32
|
+
/**
|
|
33
|
+
* A custom logger that implements the Logger abstract class.
|
|
34
|
+
* The `debug` flag does not work with a custom logger.
|
|
35
|
+
* Debug logs will always be logged.
|
|
36
|
+
*/
|
|
37
|
+
logger?: Logger;
|
|
38
|
+
/**
|
|
39
|
+
* If set, sockrates will disconnect on pingTimeouts and try to reconnect if autoReconnect is set.
|
|
40
|
+
*/
|
|
41
|
+
disconnectOnPingTimeout?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Queue socket messages until socket is connected/reconnected.
|
|
44
|
+
*/
|
|
45
|
+
queueOnDisconnect?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Flush the send queue on reconnection.
|
|
48
|
+
*/
|
|
49
|
+
flushOnReconnect?: boolean;
|
|
50
|
+
}
|
|
51
|
+
export declare enum StateEvents {
|
|
52
|
+
connected = "connected",
|
|
53
|
+
disconnected = "disconnected",
|
|
54
|
+
reconnecting = "reconnecting",
|
|
55
|
+
reconnectAttempt = "reconnectAttempt",
|
|
56
|
+
reconnectFailure = "reconnectFailure",
|
|
57
|
+
reconnected = "reconnected",
|
|
58
|
+
errored = "errored",
|
|
59
|
+
failed = "failed"
|
|
60
|
+
}
|
|
61
|
+
export type Listener = ((...args: any) => void) | ((message: {
|
|
62
|
+
id?: string;
|
|
63
|
+
payload?: Uint8Array;
|
|
64
|
+
}) => void);
|
|
65
|
+
type Message = Uint8Array | string;
|
|
66
|
+
export default class Sockrates {
|
|
67
|
+
#private;
|
|
68
|
+
constructor(url: string, config?: SockratesConfig);
|
|
69
|
+
/**
|
|
70
|
+
* Ready state of the current WebSocket.
|
|
71
|
+
*/
|
|
72
|
+
get readyState(): number;
|
|
73
|
+
/**
|
|
74
|
+
* Sockrates connection URL.
|
|
75
|
+
*/
|
|
76
|
+
get url(): string;
|
|
77
|
+
/**
|
|
78
|
+
* Update connection URL.
|
|
79
|
+
*/
|
|
80
|
+
updateURL(url: string): void;
|
|
81
|
+
/**
|
|
82
|
+
* Configuration options passed to Sockrates.
|
|
83
|
+
*/
|
|
84
|
+
get config(): SockratesConfig;
|
|
85
|
+
/**
|
|
86
|
+
* Messages that got queued due to the
|
|
87
|
+
* socket not being connected.
|
|
88
|
+
*/
|
|
89
|
+
get sendQueue(): SocketMessage[];
|
|
90
|
+
/**
|
|
91
|
+
* Flushes all messages that were queued
|
|
92
|
+
* due to the socket not being connected.
|
|
93
|
+
*/
|
|
94
|
+
flush(): boolean | SocketMessage[];
|
|
95
|
+
connect(force?: boolean): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Send a message to the socket server.
|
|
98
|
+
* @returns {boolean} Returns true if the payload was sent successfully.
|
|
99
|
+
*/
|
|
100
|
+
send(event: number, id?: string, payload?: Uint8Array, metadata?: Uint8Array): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Alias for `send`.
|
|
103
|
+
*/
|
|
104
|
+
emit(event: number, id?: string, payload?: Uint8Array, metadata?: Uint8Array): boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Send a raw message to the socket server.
|
|
107
|
+
*/
|
|
108
|
+
sendRaw(payload: Message): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Alias to `on`.
|
|
111
|
+
*/
|
|
112
|
+
receive(event: number, listener: Listener): EventEmitter<[never]>;
|
|
113
|
+
/**
|
|
114
|
+
* Event listener that is fired on receiving socket messages.
|
|
115
|
+
*/
|
|
116
|
+
on(event: number | keyof typeof StateEvents, listener: Listener): void;
|
|
117
|
+
/**
|
|
118
|
+
* Remove all event listeners that were added using `on`.
|
|
119
|
+
*/
|
|
120
|
+
removeAllListeners(): void;
|
|
121
|
+
/**
|
|
122
|
+
* Alias to `removeListener`.
|
|
123
|
+
*/
|
|
124
|
+
removeReceiver(event: number, receiver: Listener): void;
|
|
125
|
+
/**
|
|
126
|
+
* Remove a specific listener that was added using `on`.
|
|
127
|
+
*/
|
|
128
|
+
removeListener(event: number | keyof typeof StateEvents, listener: Listener): void;
|
|
129
|
+
/**
|
|
130
|
+
* Alias to `removeListeners`.
|
|
131
|
+
*/
|
|
132
|
+
removeReceivers(event: number): void;
|
|
133
|
+
/**
|
|
134
|
+
* Remove all listeners for a specific event.
|
|
135
|
+
*/
|
|
136
|
+
removeListeners(event: number | keyof typeof StateEvents): void;
|
|
137
|
+
/**
|
|
138
|
+
* Disconnect the socket.
|
|
139
|
+
*/
|
|
140
|
+
disconnect(): void;
|
|
141
|
+
}
|
|
142
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { MessageType } from "@protobuf-ts/runtime";
|
|
2
|
+
/**
|
|
3
|
+
* @generated from protobuf message message.v1.SocketMessage
|
|
4
|
+
*/
|
|
5
|
+
export interface SocketMessage {
|
|
6
|
+
/**
|
|
7
|
+
* @generated from protobuf field: uint32 event = 1;
|
|
8
|
+
*/
|
|
9
|
+
event: number;
|
|
10
|
+
/**
|
|
11
|
+
* @generated from protobuf field: optional string id = 2;
|
|
12
|
+
*/
|
|
13
|
+
id?: string;
|
|
14
|
+
/**
|
|
15
|
+
* @generated from protobuf field: optional bytes payload = 3;
|
|
16
|
+
*/
|
|
17
|
+
payload?: Uint8Array;
|
|
18
|
+
/**
|
|
19
|
+
* @generated from protobuf field: optional bytes metadata = 4;
|
|
20
|
+
*/
|
|
21
|
+
metadata?: Uint8Array;
|
|
22
|
+
}
|
|
23
|
+
declare class SocketMessage$Type extends MessageType<SocketMessage> {
|
|
24
|
+
constructor();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @generated MessageType for protobuf message message.v1.SocketMessage
|
|
28
|
+
*/
|
|
29
|
+
export declare const SocketMessage: SocketMessage$Type;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface BackoffOpts {
|
|
2
|
+
initialTimeout: number;
|
|
3
|
+
maxTimeout: number;
|
|
4
|
+
factor: number;
|
|
5
|
+
}
|
|
6
|
+
export default class JitterBackoff {
|
|
7
|
+
#private;
|
|
8
|
+
opts: BackoffOpts;
|
|
9
|
+
constructor(opts?: Partial<BackoffOpts>);
|
|
10
|
+
wait(): Promise<void>;
|
|
11
|
+
getAttempts(): number;
|
|
12
|
+
reset(): void;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import Logger from './Logger';
|
|
2
|
+
declare const logLevels: {
|
|
3
|
+
debug: number;
|
|
4
|
+
info: number;
|
|
5
|
+
warn: number;
|
|
6
|
+
error: number;
|
|
7
|
+
};
|
|
8
|
+
type LogLevel = keyof typeof logLevels;
|
|
9
|
+
export default class SockratesLogger implements Logger {
|
|
10
|
+
#private;
|
|
11
|
+
constructor(level: LogLevel);
|
|
12
|
+
debug(...args: any[]): void;
|
|
13
|
+
info(...args: any[]): void;
|
|
14
|
+
warn(...args: any[]): void;
|
|
15
|
+
error(...args: any[]): void;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudflare/realtimekit-sockrates",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A websocket library that uses protobuf for serializing data.",
|
|
5
|
+
"main": "dist/index.es.js",
|
|
6
|
+
"types": "dist/types/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.es.js",
|
|
10
|
+
"require": "./dist/index.cjs.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
4
13
|
"publishConfig": {
|
|
5
14
|
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "jest",
|
|
21
|
+
"build": "vite build && tsc",
|
|
22
|
+
"build:demo": "vite build -c vite.demo.config.ts",
|
|
23
|
+
"dev": "vite",
|
|
24
|
+
"audit": "npm audit --audit-level=critical",
|
|
25
|
+
"lint": "eslint . --ext .js,.ts,.tsx,.jsx",
|
|
26
|
+
"lint:fix": "npm run lint -- --fix"
|
|
27
|
+
},
|
|
28
|
+
"author": "Cloudflare",
|
|
29
|
+
"license": "Apache-2.0",
|
|
30
|
+
"homepage": "https://realtime.cloudflare.com",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/events": "^3.0.0",
|
|
33
|
+
"@types/jest": "^29.5.11",
|
|
34
|
+
"@types/node": "^20.10.6",
|
|
35
|
+
"jest": "^29.7.0",
|
|
36
|
+
"ts-jest": "^29.1.1",
|
|
37
|
+
"ts-node": "^10.9.2",
|
|
38
|
+
"typescript": "~5.5.4",
|
|
39
|
+
"vite": "4.1.2"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"events": "^3.3.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@protobuf-ts/runtime": "^2.7.0"
|
|
6
46
|
}
|
|
7
47
|
}
|