iosignal 9.6.0 → 9.13.0
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/dist/browser/esm/io.js +5 -5
- package/dist/browser/esm/io.js.map +1 -1
- package/dist/browser/iife/io.js +5 -5
- package/dist/browser/iife/io.js.map +1 -1
- package/dist/node/iosignal.cjs +434 -125
- package/dist/node/iosignal.d.cts +94 -1
- package/dist/node/iosignal.d.ts +94 -1
- package/dist/node/iosignal.js +434 -125
- package/dist/types/common/payload.d.ts +9 -0
- package/package.json +1 -1
package/dist/node/iosignal.cjs
CHANGED
|
@@ -18,7 +18,7 @@ var fs = require('fs');
|
|
|
18
18
|
var process$1 = require('process');
|
|
19
19
|
var path = require('path');
|
|
20
20
|
|
|
21
|
-
var version$1 = "9.
|
|
21
|
+
var version$1 = "9.13.0";
|
|
22
22
|
var pkg = {
|
|
23
23
|
version: version$1};
|
|
24
24
|
|
|
@@ -596,12 +596,12 @@ const STATUS$1 = {
|
|
|
596
596
|
ERROR: 255
|
|
597
597
|
};
|
|
598
598
|
|
|
599
|
-
const encoder$
|
|
599
|
+
const encoder$3 = new TextEncoder();
|
|
600
600
|
|
|
601
601
|
|
|
602
602
|
function getSignalPack(tag, ...args) {
|
|
603
603
|
if (typeof tag !== 'string') throw TypeError('tag should be string.')
|
|
604
|
-
let tagEncoded = encoder$
|
|
604
|
+
let tagEncoded = encoder$3.encode(tag);
|
|
605
605
|
if (tagEncoded.byteLength > SIZE_LIMIT.TAG_LEN1) throw TypeError('tag UTF-8 byte length exceeds 255')
|
|
606
606
|
let payload = parsePayload(args);
|
|
607
607
|
|
|
@@ -643,7 +643,7 @@ function parsePayload(args) {
|
|
|
643
643
|
} else if (args.length == 1) {
|
|
644
644
|
if (typeof args[0] === 'string' || typeof args[0] === 'number') {
|
|
645
645
|
type = PAYLOAD_TYPE.TEXT;
|
|
646
|
-
pack = encoder$
|
|
646
|
+
pack = encoder$3.encode(args[0] + "."); // add null byte area.
|
|
647
647
|
pack[pack.byteLength - 1] = 0; // set byte-zero : '\0' in C/C++
|
|
648
648
|
|
|
649
649
|
} else if (ArrayBuffer.isView(args[0]) || args[0] instanceof ArrayBuffer) {
|
|
@@ -651,7 +651,7 @@ function parsePayload(args) {
|
|
|
651
651
|
pack = $$1.B8(args[0]);
|
|
652
652
|
} else if (typeof args[0] === 'object') {
|
|
653
653
|
type = PAYLOAD_TYPE.OBJECT;
|
|
654
|
-
pack = encoder$
|
|
654
|
+
pack = encoder$3.encode(JSON.stringify(args[0]));
|
|
655
655
|
} else {
|
|
656
656
|
throw new Error('unknown payload arguments')
|
|
657
657
|
}
|
|
@@ -665,7 +665,7 @@ function parsePayload(args) {
|
|
|
665
665
|
type = PAYLOAD_TYPE.MBA;
|
|
666
666
|
} else {
|
|
667
667
|
type = PAYLOAD_TYPE.MJSON;
|
|
668
|
-
pack = encoder$
|
|
668
|
+
pack = encoder$3.encode(JSON.stringify(args));
|
|
669
669
|
}
|
|
670
670
|
|
|
671
671
|
}
|
|
@@ -677,6 +677,42 @@ function getPayloadFromSignalPack(signalPack) {
|
|
|
677
677
|
let tagLen = signalPack.readUint8(1);
|
|
678
678
|
return signalPack.subarray(3 + tagLen)
|
|
679
679
|
}
|
|
680
|
+
const decoder$5 = new TextDecoder();
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Arguments of a SIGNAL payload, as IO emits them after the tag: EMPTY [], TEXT [string] (the
|
|
684
|
+
* trailing NUL that C/C++ senders add is removed), BINARY [the buffer itself], OBJECT [value],
|
|
685
|
+
* MJSON [...values], MBA [...args]. null for an unknown payload type. Throws on malformed JSON/MBA.
|
|
686
|
+
* @param {number} payloadType
|
|
687
|
+
* @param {Uint8Array} payloadBuffer
|
|
688
|
+
* @returns {any[] | null}
|
|
689
|
+
*/
|
|
690
|
+
function decodeSignalArgs(payloadType, payloadBuffer) {
|
|
691
|
+
switch (payloadType) {
|
|
692
|
+
case PAYLOAD_TYPE.EMPTY:
|
|
693
|
+
return []
|
|
694
|
+
case PAYLOAD_TYPE.TEXT: {
|
|
695
|
+
const end = payloadBuffer[payloadBuffer.byteLength - 1] === 0 ? payloadBuffer.byteLength - 1 : payloadBuffer.byteLength;
|
|
696
|
+
return [decoder$5.decode(payloadBuffer.subarray(0, end))]
|
|
697
|
+
}
|
|
698
|
+
case PAYLOAD_TYPE.BINARY:
|
|
699
|
+
return [payloadBuffer]
|
|
700
|
+
case PAYLOAD_TYPE.OBJECT:
|
|
701
|
+
return [JSON.parse(decoder$5.decode(payloadBuffer))]
|
|
702
|
+
case PAYLOAD_TYPE.MJSON: {
|
|
703
|
+
const values = JSON.parse(decoder$5.decode(payloadBuffer));
|
|
704
|
+
if (!Array.isArray(values)) throw new TypeError('MJSON payload is not an array')
|
|
705
|
+
return values
|
|
706
|
+
}
|
|
707
|
+
case PAYLOAD_TYPE.MBA: {
|
|
708
|
+
const mba = $$1.unpack(payloadBuffer);
|
|
709
|
+
if (!mba) throw new TypeError('malformed MBA payload')
|
|
710
|
+
return mba.args
|
|
711
|
+
}
|
|
712
|
+
default:
|
|
713
|
+
return null
|
|
714
|
+
}
|
|
715
|
+
}
|
|
680
716
|
|
|
681
717
|
const t=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]);function e(e,r,n,i,o){let f,s,u,h,a,c,l,p,y,g,d,b,w;for(;o>=64;){for(f=r[0],s=r[1],u=r[2],h=r[3],a=r[4],c=r[5],l=r[6],p=r[7],g=0;g<16;g++)d=i+4*g,e[g]=(255&n[d])<<24|(255&n[d+1])<<16|(255&n[d+2])<<8|255&n[d+3];for(g=16;g<64;g++)y=e[g-2],b=(y>>>17|y<<15)^(y>>>19|y<<13)^y>>>10,y=e[g-15],w=(y>>>7|y<<25)^(y>>>18|y<<14)^y>>>3,e[g]=(b+e[g-7]|0)+(w+e[g-16]|0);for(g=0;g<64;g++)b=(((a>>>6|a<<26)^(a>>>11|a<<21)^(a>>>25|a<<7))+(a&c^~a&l)|0)+(p+(t[g]+e[g]|0)|0)|0,w=((f>>>2|f<<30)^(f>>>13|f<<19)^(f>>>22|f<<10))+(f&s^f&u^s&u)|0,p=l,l=c,c=a,a=h+b|0,h=u,u=s,s=f,f=b+w|0;r[0]+=f,r[1]+=s,r[2]+=u,r[3]+=h,r[4]+=a,r[5]+=c,r[6]+=l,r[7]+=p,i+=64,o-=64;}return i}const r=function(){function t(){this.digestLength=32,this.blockSize=64,this.state=new Int32Array(8),this.temp=new Int32Array(64),this.buffer=new Uint8Array(128),this.bufferLength=0,this.bytesHashed=0,this.finished=false,this.reset();}return t.prototype.reset=function(){return this.state[0]=1779033703,this.state[1]=3144134277,this.state[2]=1013904242,this.state[3]=2773480762,this.state[4]=1359893119,this.state[5]=2600822924,this.state[6]=528734635,this.state[7]=1541459225,this.bufferLength=0,this.bytesHashed=0,this.finished=false,this},t.prototype.clean=function(){for(var t=0;t<this.buffer.length;t++)this.buffer[t]=0;for(t=0;t<this.temp.length;t++)this.temp[t]=0;this.reset();},t.prototype.update=function(t,r){if(void 0===r&&(r=t.length),this.finished)throw new Error("SHA256: can't update because hash was finished.");let n=0;if(this.bytesHashed+=r,this.bufferLength>0){for(;this.bufferLength<64&&r>0;)this.buffer[this.bufferLength++]=t[n++],r--;64===this.bufferLength&&(e(this.temp,this.state,this.buffer,0,64),this.bufferLength=0);}for(r>=64&&(n=e(this.temp,this.state,t,n,r),r%=64);r>0;)this.buffer[this.bufferLength++]=t[n++],r--;return this},t.prototype.finish=function(t){if(!this.finished){const t=this.bytesHashed,n=this.bufferLength,i=t/536870912|0,o=t<<3,f=t%64<56?64:128;this.buffer[n]=128;for(var r=n+1;r<f-8;r++)this.buffer[r]=0;this.buffer[f-8]=i>>>24&255,this.buffer[f-7]=i>>>16&255,this.buffer[f-6]=i>>>8&255,this.buffer[f-5]=i>>>0&255,this.buffer[f-4]=o>>>24&255,this.buffer[f-3]=o>>>16&255,this.buffer[f-2]=o>>>8&255,this.buffer[f-1]=o>>>0&255,e(this.temp,this.state,this.buffer,0,f),this.finished=true;}for(r=0;r<8;r++)t[4*r+0]=this.state[r]>>>24&255,t[4*r+1]=this.state[r]>>>16&255,t[4*r+2]=this.state[r]>>>8&255,t[4*r+3]=this.state[r]>>>0&255;return this},t.prototype.digest=function(){const t=new Uint8Array(this.digestLength);return this.finish(t),t},t.prototype._saveState=function(t){for(let e=0;e<this.state.length;e++)t[e]=this.state[e];},t.prototype._restoreState=function(t,e){for(let e=0;e<this.state.length;e++)this.state[e]=t[e];this.bytesHashed=e,this.finished=false,this.bufferLength=0;},t}(),n=function(){function t(t){this.inner=new r,this.outer=new r,this.blockSize=this.inner.blockSize,this.digestLength=this.inner.digestLength;const e=new Uint8Array(this.blockSize);if(t.length>this.blockSize)(new r).update(t).finish(e).clean();else for(var n=0;n<t.length;n++)e[n]=t[n];for(n=0;n<e.length;n++)e[n]^=54;this.inner.update(e);for(n=0;n<e.length;n++)e[n]^=106;this.outer.update(e),this.istate=new Uint32Array(8),this.ostate=new Uint32Array(8),this.inner._saveState(this.istate),this.outer._saveState(this.ostate);for(n=0;n<e.length;n++)e[n]=0;}return t.prototype.reset=function(){return this.inner._restoreState(this.istate,this.inner.blockSize),this.outer._restoreState(this.ostate,this.outer.blockSize),this},t.prototype.clean=function(){for(let t=0;t<this.istate.length;t++)this.ostate[t]=this.istate[t]=0;this.inner.clean(),this.outer.clean();},t.prototype.update=function(t){return this.inner.update(t),this},t.prototype.finish=function(t){return this.outer.finished?this.outer.finish(t):(this.inner.finish(t),this.outer.update(t,this.digestLength).finish(t)),this},t.prototype.digest=function(){const t=new Uint8Array(this.digestLength);return this.finish(t),t},t}();function i(t){const e=(new r).update(t),n=e.digest();return e.clean(),n}var o,f,s,u={},h={},a={};
|
|
682
718
|
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */function c(){return f||(f=1,a.read=function(t,e,r,n,i){var o,f,s=8*i-n-1,u=(1<<s)-1,h=u>>1,a=-7,c=r?i-1:0,l=r?-1:1,p=t[e+c];for(c+=l,o=p&(1<<-a)-1,p>>=-a,a+=s;a>0;o=256*o+t[e+c],c+=l,a-=8);for(f=o&(1<<-a)-1,o>>=-a,a+=n;a>0;f=256*f+t[e+c],c+=l,a-=8);if(0===o)o=1-h;else {if(o===u)return f?NaN:1/0*(p?-1:1);f+=Math.pow(2,n),o-=h;}return (p?-1:1)*f*Math.pow(2,o-n)},a.write=function(t,e,r,n,i,o){var f,s,u,h=8*o-i-1,a=(1<<h)-1,c=a>>1,l=23===i?Math.pow(2,-24)-Math.pow(2,-77):0,p=n?0:o-1,y=n?1:-1,g=e<0||0===e&&1/e<0?1:0;for(e=Math.abs(e),isNaN(e)||e===1/0?(s=isNaN(e)?1:0,f=a):(f=Math.floor(Math.log(e)/Math.LN2),e*(u=Math.pow(2,-f))<1&&(f--,u*=2),(e+=f+c>=1?l/u:l*Math.pow(2,1-c))*u>=2&&(f++,u/=2),f+c>=a?(s=0,f=a):f+c>=1?(s=(e*u-1)*Math.pow(2,i),f+=c):(s=e*Math.pow(2,c-1)*Math.pow(2,i),f=0));i>=8;t[r+p]=255&s,p+=y,s/=256,i-=8);for(f=f<<i|s,h+=i;h>0;t[r+p]=255&f,p+=y,f/=256,h-=8);t[r+p-y]|=128*g;}),a}
|
|
@@ -705,13 +741,13 @@ const t=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1
|
|
|
705
741
|
*/
|
|
706
742
|
|
|
707
743
|
const Buffer$1 = $$1.Buffer;
|
|
708
|
-
const encoder$
|
|
744
|
+
const encoder$2 = new TextEncoder();
|
|
709
745
|
const decoder$4 = new TextDecoder();
|
|
710
746
|
|
|
711
747
|
// Wire tag lengths use one byte, regardless of JavaScript string length.
|
|
712
748
|
function encodeTag(tag, allowEmpty = true) {
|
|
713
749
|
if (typeof tag !== 'string') throw TypeError('tag should be string.')
|
|
714
|
-
const encoded = encoder$
|
|
750
|
+
const encoded = encoder$2.encode(tag);
|
|
715
751
|
if ((!allowEmpty && encoded.byteLength === 0) || encoded.byteLength > SIZE_LIMIT.TAG_LEN1) {
|
|
716
752
|
throw TypeError('tag UTF-8 byte length must be ' + (allowEmpty ? '0' : '1') + '~255')
|
|
717
753
|
}
|
|
@@ -1291,83 +1327,15 @@ class IOCore extends EventEmitter {
|
|
|
1291
1327
|
> cid_sub message: tag includes cid and @ both : 'cid@*'
|
|
1292
1328
|
> ch_sub message: else.
|
|
1293
1329
|
*/
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
if (tag
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
this.emit('message', tag);
|
|
1304
|
-
}
|
|
1305
|
-
break;
|
|
1306
|
-
|
|
1307
|
-
case PAYLOAD_TYPE.TEXT:
|
|
1308
|
-
// !! Must remove null char before decode in JS.
|
|
1309
|
-
// string payload contains null char for the c/cpp devices.
|
|
1310
|
-
let payloadStringWithoutNull = payloadBuffer;
|
|
1311
|
-
if (payloadBuffer[payloadBuffer.byteLength - 1] === 0) {
|
|
1312
|
-
payloadStringWithoutNull = payloadBuffer.subarray(0, payloadBuffer.byteLength - 1);
|
|
1313
|
-
}
|
|
1314
|
-
let oneString = decoder$4.decode(payloadStringWithoutNull);
|
|
1315
|
-
if (tag.indexOf('@') === 0) {
|
|
1316
|
-
this.emit('@', tag, oneString);
|
|
1317
|
-
if (tag !== '@') this.emit(tag, tag, oneString);
|
|
1318
|
-
} else {
|
|
1319
|
-
this.emit(tag, tag, oneString);
|
|
1320
|
-
this.emit('message', tag, oneString);
|
|
1321
|
-
}
|
|
1322
|
-
break;
|
|
1323
|
-
|
|
1324
|
-
case PAYLOAD_TYPE.BINARY:
|
|
1325
|
-
if (tag.indexOf('@') === 0) {
|
|
1326
|
-
this.emit('@', tag, payloadBuffer);
|
|
1327
|
-
if (tag !== '@') this.emit(tag, tag, payloadBuffer);
|
|
1328
|
-
} else {
|
|
1329
|
-
this.emit(tag, tag, payloadBuffer);
|
|
1330
|
-
this.emit('message', tag, payloadBuffer);
|
|
1331
|
-
}
|
|
1332
|
-
break;
|
|
1333
|
-
|
|
1334
|
-
case PAYLOAD_TYPE.OBJECT:
|
|
1335
|
-
let oneObjectBuffer = decoder$4.decode(payloadBuffer);
|
|
1336
|
-
let oneJSONObject = JSON.parse(oneObjectBuffer);
|
|
1337
|
-
if (tag.indexOf('@') === 0) {
|
|
1338
|
-
this.emit('@', tag, oneJSONObject);
|
|
1339
|
-
if (tag !== '@') this.emit(tag, tag, oneJSONObject);
|
|
1340
|
-
} else {
|
|
1341
|
-
this.emit(tag, tag, oneJSONObject);
|
|
1342
|
-
this.emit('message', tag, oneJSONObject);
|
|
1343
|
-
}
|
|
1344
|
-
break;
|
|
1345
|
-
|
|
1346
|
-
case PAYLOAD_TYPE.MJSON:
|
|
1347
|
-
let mjsonBuffer = decoder$4.decode(payloadBuffer);
|
|
1348
|
-
let mjson = JSON.parse(mjsonBuffer);
|
|
1349
|
-
if (tag.indexOf('@') === 0) {
|
|
1350
|
-
this.emit('@', tag, ...mjson);
|
|
1351
|
-
if (tag !== '@') this.emit(tag, tag, ...mjson);
|
|
1352
|
-
} else {
|
|
1353
|
-
this.emit(tag, tag, ...mjson);
|
|
1354
|
-
this.emit('message', tag, ...mjson);
|
|
1355
|
-
}
|
|
1356
|
-
break;
|
|
1357
|
-
|
|
1358
|
-
case PAYLOAD_TYPE.MBA:
|
|
1359
|
-
let mbaObject = $$1.unpack(payloadBuffer);
|
|
1360
|
-
if (tag.indexOf('@') === 0) {
|
|
1361
|
-
this.emit('@', tag, ...mbaObject.args);
|
|
1362
|
-
if (tag !== '@') this.emit(tag, tag, ...mbaObject.args);
|
|
1363
|
-
} else {
|
|
1364
|
-
this.emit(tag, tag, ...mbaObject.args);
|
|
1365
|
-
this.emit('message', tag, ...mbaObject.args);
|
|
1366
|
-
}
|
|
1367
|
-
break;
|
|
1368
|
-
|
|
1369
|
-
default:
|
|
1370
|
-
// console.log('## Unkown payloadtype', payloadType)
|
|
1330
|
+
const args = decodeSignalArgs(payloadType, payloadBuffer);
|
|
1331
|
+
if (args) {
|
|
1332
|
+
if (tag.indexOf('@') === 0) {
|
|
1333
|
+
this.emit('@', tag, ...args);
|
|
1334
|
+
if (tag !== '@') this.emit(tag, tag, ...args);
|
|
1335
|
+
} else {
|
|
1336
|
+
this.emit(tag, tag, ...args);
|
|
1337
|
+
this.emit('message', tag, ...args);
|
|
1338
|
+
}
|
|
1371
1339
|
}
|
|
1372
1340
|
|
|
1373
1341
|
} catch (err) {
|
|
@@ -7527,6 +7495,7 @@ class RemoteCore {
|
|
|
7527
7495
|
this.did = ''; // Device Id
|
|
7528
7496
|
this.uid = ''; // User Id
|
|
7529
7497
|
this.nick = '';
|
|
7498
|
+
this.site = ''; // the Origin host of a browser connection, set by Remote
|
|
7530
7499
|
|
|
7531
7500
|
this.privateNode = false;
|
|
7532
7501
|
this.HOME_CHANNEL = '';
|
|
@@ -8181,6 +8150,19 @@ function getClientAddress(req, trust = serverOption.trustProxy) {
|
|
|
8181
8150
|
}
|
|
8182
8151
|
}
|
|
8183
8152
|
|
|
8153
|
+
// The site a browser connection came from: the host name of its Origin header ('bayo.tv'),
|
|
8154
|
+
// without scheme or port. '' when there is none (Node.js, apps, TCP) or it is 'null' (a file
|
|
8155
|
+
// page). A non-browser client can send any Origin, so this is for display only.
|
|
8156
|
+
function originHost(req) {
|
|
8157
|
+
const origin = req?.headers?.origin;
|
|
8158
|
+
if (typeof origin !== 'string' || !origin || origin === 'null' || origin.length > 512) return ''
|
|
8159
|
+
try {
|
|
8160
|
+
return new URL(origin).hostname.toLowerCase().replace(/^\[|\]$/g, '').slice(0, 253)
|
|
8161
|
+
} catch {
|
|
8162
|
+
return ''
|
|
8163
|
+
}
|
|
8164
|
+
}
|
|
8165
|
+
|
|
8184
8166
|
class Remote extends RemoteCore {
|
|
8185
8167
|
constructor(socket, req, manager) {
|
|
8186
8168
|
super(socket, manager);
|
|
@@ -8194,6 +8176,7 @@ class Remote extends RemoteCore {
|
|
|
8194
8176
|
const { ip, TLS } = getClientAddress(req, this.manager.server?.trustProxy);
|
|
8195
8177
|
this.ip = ip;
|
|
8196
8178
|
this.TLS = TLS;
|
|
8179
|
+
this.site = originHost(req);
|
|
8197
8180
|
} else {
|
|
8198
8181
|
this.TLS = false;
|
|
8199
8182
|
this.ip = normalizeIP(socket.remoteAddress) ?? '0.0.0.0';
|
|
@@ -8730,6 +8713,267 @@ function deliveryAllowed(target, sender) {
|
|
|
8730
8713
|
return policy.defaultAllow
|
|
8731
8714
|
}
|
|
8732
8715
|
|
|
8716
|
+
const encoder$1 = new TextEncoder();
|
|
8717
|
+
const NAME = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
8718
|
+
|
|
8719
|
+
/**
|
|
8720
|
+
* Validates one tag pattern of a host. A trailing '*' makes it a prefix; '*' is allowed nowhere else.
|
|
8721
|
+
* Channel patterns (inbound, and own without '@'): global channel tags, so no '@' and no leading
|
|
8722
|
+
* '#' (home channels belong to one network) or '$'. Unicast patterns (own, leading '@'): the topic
|
|
8723
|
+
* of '<cid>@topic' messages.
|
|
8724
|
+
* @returns {{ pattern: string, text: string, prefix: boolean, unicast: boolean }}
|
|
8725
|
+
*/
|
|
8726
|
+
function parsePattern(pattern, allowUnicast) {
|
|
8727
|
+
if (typeof pattern !== 'string' || pattern.length === 0) throw new TypeError('host tag must be a non-empty string')
|
|
8728
|
+
const prefix = pattern.endsWith('*');
|
|
8729
|
+
const text = prefix ? pattern.slice(0, -1) : pattern;
|
|
8730
|
+
if (text.includes('*')) throw new TypeError(`host tag ${pattern}: '*' is allowed only at the end`)
|
|
8731
|
+
if (text.includes(',')) throw new TypeError(`host tag ${pattern}: ',' separates tags`)
|
|
8732
|
+
if (encoder$1.encode(text).byteLength > SIZE_LIMIT.TAG_LEN1) throw new TypeError(`host tag ${pattern} exceeds 255 UTF-8 bytes`)
|
|
8733
|
+
const unicast = text.startsWith('@');
|
|
8734
|
+
if (unicast) {
|
|
8735
|
+
if (!allowUnicast) throw new TypeError(`host tag ${pattern}: inbound tags are channel tags (no '@')`)
|
|
8736
|
+
if (text.indexOf('@', 1) !== -1) throw new TypeError(`host tag ${pattern}: a unicast topic has one '@'`)
|
|
8737
|
+
if (text.length === 1) throw new TypeError(`host tag ${pattern}: empty topic`)
|
|
8738
|
+
} else {
|
|
8739
|
+
if (text.includes('@')) throw new TypeError(`host tag ${pattern}: channel tags have no '@'`)
|
|
8740
|
+
if (text.startsWith('#')) throw new TypeError(`host tag ${pattern}: home channel tags ('#...') cannot be hosted`)
|
|
8741
|
+
if (text.startsWith('$')) throw new TypeError(`host tag ${pattern}: '$' prefix is reserved`)
|
|
8742
|
+
if (text.length === 0) throw new TypeError(`host tag ${pattern}: empty tag`)
|
|
8743
|
+
}
|
|
8744
|
+
return { pattern, text, prefix, unicast }
|
|
8745
|
+
}
|
|
8746
|
+
|
|
8747
|
+
function overlaps(a, b) {
|
|
8748
|
+
if (a.prefix && b.text.startsWith(a.text)) return true
|
|
8749
|
+
if (b.prefix && a.text.startsWith(b.text)) return true
|
|
8750
|
+
return a.text === b.text
|
|
8751
|
+
}
|
|
8752
|
+
|
|
8753
|
+
/**
|
|
8754
|
+
* The tags all hosts of one server have claimed. Patterns never overlap, so a tag matches at most one.
|
|
8755
|
+
*/
|
|
8756
|
+
class HostTable {
|
|
8757
|
+
constructor() {
|
|
8758
|
+
this.exact = new Map(); // text -> entry
|
|
8759
|
+
this.prefixes = []; // entries
|
|
8760
|
+
this.hosts = new Map(); // name -> Host
|
|
8761
|
+
}
|
|
8762
|
+
|
|
8763
|
+
get size() {
|
|
8764
|
+
return this.hosts.size
|
|
8765
|
+
}
|
|
8766
|
+
|
|
8767
|
+
add(host, entries) {
|
|
8768
|
+
if (this.hosts.has(host.name)) throw new Error(`host ${host.name} already exists`)
|
|
8769
|
+
const all = [...this.exact.values(), ...this.prefixes];
|
|
8770
|
+
entries.forEach((entry, i) => {
|
|
8771
|
+
for (const other of [...all, ...entries.slice(0, i)]) {
|
|
8772
|
+
if (overlaps(entry, other)) {
|
|
8773
|
+
throw new Error(`host tag ${entry.pattern} overlaps ${other.pattern}` +
|
|
8774
|
+
(other.host === host ? '' : ` of host ${other.host.name}`))
|
|
8775
|
+
}
|
|
8776
|
+
}
|
|
8777
|
+
});
|
|
8778
|
+
for (const entry of entries) {
|
|
8779
|
+
if (entry.prefix) this.prefixes.push(entry);
|
|
8780
|
+
else this.exact.set(entry.text, entry);
|
|
8781
|
+
}
|
|
8782
|
+
this.hosts.set(host.name, host);
|
|
8783
|
+
}
|
|
8784
|
+
|
|
8785
|
+
remove(host) {
|
|
8786
|
+
if (this.hosts.get(host.name) !== host) return
|
|
8787
|
+
this.hosts.delete(host.name);
|
|
8788
|
+
for (const [text, entry] of this.exact) if (entry.host === host) this.exact.delete(text);
|
|
8789
|
+
this.prefixes = this.prefixes.filter(entry => entry.host !== host);
|
|
8790
|
+
}
|
|
8791
|
+
|
|
8792
|
+
// tag: a client channel tag, or '@topic' of a unicast. Returns the entry or undefined.
|
|
8793
|
+
match(tag) {
|
|
8794
|
+
const entry = this.exact.get(tag);
|
|
8795
|
+
if (entry) return entry
|
|
8796
|
+
for (const p of this.prefixes) if (tag.startsWith(p.text)) return p
|
|
8797
|
+
return undefined
|
|
8798
|
+
}
|
|
8799
|
+
|
|
8800
|
+
// A connection closed. ownsCid: it still owned its CID (no newer connection took it over).
|
|
8801
|
+
remoteClosed(remote, ownsCid) {
|
|
8802
|
+
if (!ownsCid || !remote.cid) return
|
|
8803
|
+
for (const host of this.hosts.values()) host._remoteClosed(remote);
|
|
8804
|
+
}
|
|
8805
|
+
}
|
|
8806
|
+
|
|
8807
|
+
/**
|
|
8808
|
+
* Game logic (or any server-side logic) inside the server process. Created by server.host().
|
|
8809
|
+
*
|
|
8810
|
+
* Events:
|
|
8811
|
+
* 'message' (from, tag, ...args) a client signal to one of the inbound tags. from is the
|
|
8812
|
+
* server's view of the sender (never anything in the payload).
|
|
8813
|
+
* 'leave' (from, reason) a participant's connection closed (it no longer has its CID)
|
|
8814
|
+
* 'error' (error) a 'message' or 'leave' handler threw
|
|
8815
|
+
* 'close' () the host was closed
|
|
8816
|
+
*/
|
|
8817
|
+
class Host extends require$$0$3 {
|
|
8818
|
+
|
|
8819
|
+
constructor(manager, name, options = {}) {
|
|
8820
|
+
super();
|
|
8821
|
+
if (typeof name !== 'string' || !NAME.test(name)) {
|
|
8822
|
+
throw new TypeError('host name: 1-64 characters of A-Z a-z 0-9 _ . -')
|
|
8823
|
+
}
|
|
8824
|
+
if (options === null || typeof options !== 'object') throw new TypeError('host options must be an object')
|
|
8825
|
+
for (const key of Object.keys(options)) {
|
|
8826
|
+
if (!['inbound', 'own', 'fanout', 'retain'].includes(key)) throw new TypeError(`unknown host option: ${key}`)
|
|
8827
|
+
}
|
|
8828
|
+
const list = (key) => {
|
|
8829
|
+
const value = options[key] ?? [];
|
|
8830
|
+
if (!Array.isArray(value)) throw new TypeError(`host option ${key} must be an array of tags`)
|
|
8831
|
+
return value
|
|
8832
|
+
};
|
|
8833
|
+
/** @type {Host} */
|
|
8834
|
+
const host = this;
|
|
8835
|
+
const inbound = list('inbound').map(p => ({ ...parsePattern(p, false), kind: 'inbound', host }));
|
|
8836
|
+
const own = list('own').map(p => ({ ...parsePattern(p, true), kind: 'own', host }));
|
|
8837
|
+
if (inbound.length === 0) throw new TypeError('host needs at least one inbound tag')
|
|
8838
|
+
|
|
8839
|
+
const fanout = options.fanout ?? null;
|
|
8840
|
+
if (fanout !== null && !(Number.isSafeInteger(fanout) && fanout > 0)) {
|
|
8841
|
+
throw new RangeError('host fanout must be null or a positive integer')
|
|
8842
|
+
}
|
|
8843
|
+
const retain = options.retain ?? {};
|
|
8844
|
+
for (const key of ['tags', 'bytes']) {
|
|
8845
|
+
if (retain[key] !== undefined && !(Number.isSafeInteger(retain[key]) && retain[key] > 0)) {
|
|
8846
|
+
throw new RangeError(`host retain.${key} must be a positive integer`)
|
|
8847
|
+
}
|
|
8848
|
+
}
|
|
8849
|
+
|
|
8850
|
+
this.name = name;
|
|
8851
|
+
this.manager = manager;
|
|
8852
|
+
this.fanout = fanout;
|
|
8853
|
+
this.retainTags = retain.tags ?? null; // null: the server's retain.tagsPerOwner
|
|
8854
|
+
this.retainBytes = retain.bytes ?? null; // null: the server's retain.bytesPerOwner
|
|
8855
|
+
this.retainOwnerKey = 'host:' + name;
|
|
8856
|
+
this.ownChannels = own.filter(e => !e.unicast);
|
|
8857
|
+
this.closed = false;
|
|
8858
|
+
/** @type {Map<string, Readonly<object>>} cid -> from, connections that sent to this host */
|
|
8859
|
+
this._participants = new Map();
|
|
8860
|
+
|
|
8861
|
+
manager.hosts.add(this, [...inbound, ...own]);
|
|
8862
|
+
// Values a client retained on these tags before the host claimed them are not the host's.
|
|
8863
|
+
for (const tag of [...manager.retain_messages.keys()]) {
|
|
8864
|
+
if (!tag.startsWith(manager.CHANNEL_PREFIX)) continue
|
|
8865
|
+
const entry = manager.hosts.match(tag.slice(manager.CHANNEL_PREFIX.length));
|
|
8866
|
+
if (entry?.host === this) manager.dropChannelRetain(tag);
|
|
8867
|
+
}
|
|
8868
|
+
}
|
|
8869
|
+
|
|
8870
|
+
/** Connections that have sent to this host and are still online: cid -> from. */
|
|
8871
|
+
get participants() {
|
|
8872
|
+
return this._participants
|
|
8873
|
+
}
|
|
8874
|
+
|
|
8875
|
+
/** Stop tracking a participant; no 'leave' is emitted for it (until it sends again). */
|
|
8876
|
+
forget(cid) {
|
|
8877
|
+
return this._participants.delete(cid)
|
|
8878
|
+
}
|
|
8879
|
+
|
|
8880
|
+
/**
|
|
8881
|
+
* Publish to one of the host's own channel tags. A tag with '$' is also retained, owned by the
|
|
8882
|
+
* host. Returns the number of connections it was delivered to.
|
|
8883
|
+
*/
|
|
8884
|
+
publish(tag, ...args) {
|
|
8885
|
+
this._open();
|
|
8886
|
+
if (typeof tag !== 'string') throw new TypeError('tag should be string.')
|
|
8887
|
+
const entry = this.manager.hosts.match(tag);
|
|
8888
|
+
if (tag.startsWith('@') || entry?.host !== this || entry.kind !== 'own') {
|
|
8889
|
+
throw new Error(`host ${this.name} does not own channel tag ${tag}`)
|
|
8890
|
+
}
|
|
8891
|
+
const message = getSignalPack(tag, ...args);
|
|
8892
|
+
return this.manager.hostPublish(this, this.manager.CHANNEL_PREFIX + tag, message)
|
|
8893
|
+
}
|
|
8894
|
+
|
|
8895
|
+
/**
|
|
8896
|
+
* Send to one connection. It receives '@' + topic. Returns false when the CID is not online
|
|
8897
|
+
* or its receive buffer is too small for the message.
|
|
8898
|
+
*/
|
|
8899
|
+
to(cid, topic, ...args) {
|
|
8900
|
+
this._open();
|
|
8901
|
+
if (typeof topic !== 'string' || topic.length === 0 || topic.includes('@')) {
|
|
8902
|
+
throw new TypeError('topic must be a non-empty string without @')
|
|
8903
|
+
}
|
|
8904
|
+
const remote = this.manager.cid2remote.get(cid);
|
|
8905
|
+
const message = getSignalPack('@' + topic, ...args);
|
|
8906
|
+
if (!remote || remote._closing) return false
|
|
8907
|
+
const skipped = remote.peerRxLimitSkipped;
|
|
8908
|
+
remote.send_enc_mode(message);
|
|
8909
|
+
return remote.peerRxLimitSkipped === skipped
|
|
8910
|
+
}
|
|
8911
|
+
|
|
8912
|
+
/** Release the tags (they become ordinary channels again) and the host's retained values. */
|
|
8913
|
+
close() {
|
|
8914
|
+
if (this.closed) return
|
|
8915
|
+
this.closed = true;
|
|
8916
|
+
this.manager.hosts.remove(this);
|
|
8917
|
+
const owner = this.manager.retainOwners.get(this.retainOwnerKey);
|
|
8918
|
+
if (owner) for (const tag of [...owner.tags]) this.manager.dropChannelRetain(tag);
|
|
8919
|
+
this._participants.clear();
|
|
8920
|
+
this.emit('close');
|
|
8921
|
+
}
|
|
8922
|
+
|
|
8923
|
+
_open() {
|
|
8924
|
+
if (this.closed) throw new Error(`host ${this.name} is closed`)
|
|
8925
|
+
}
|
|
8926
|
+
|
|
8927
|
+
// The server's view of a sender; reused while nothing in it changes.
|
|
8928
|
+
static from(remote) {
|
|
8929
|
+
const authorized = remote.boho?.isAuthorized === true;
|
|
8930
|
+
const f = remote._hostFrom;
|
|
8931
|
+
if (f && f.cid === remote.cid && f.uid === remote.uid && f.role === remote.role &&
|
|
8932
|
+
f.nick === remote.nick && f.authorized === authorized) return f
|
|
8933
|
+
return remote._hostFrom = Object.freeze({
|
|
8934
|
+
cid: remote.cid, uid: remote.uid || null, role: remote.role, authorized,
|
|
8935
|
+
nick: remote.nick || '', ssid: remote.ssid
|
|
8936
|
+
})
|
|
8937
|
+
}
|
|
8938
|
+
|
|
8939
|
+
// A SIGNAL from a client to one of the inbound tags. Returns false if it was not delivered.
|
|
8940
|
+
_receive(remote, tag, message) {
|
|
8941
|
+
if (message[0] !== IOMsg.SIGNAL || !remote.cid) return false
|
|
8942
|
+
const tagLen = message[1];
|
|
8943
|
+
if (message.byteLength < tagLen + 3) return false
|
|
8944
|
+
let args;
|
|
8945
|
+
try {
|
|
8946
|
+
args = decodeSignalArgs(message[2 + tagLen], message.subarray(3 + tagLen));
|
|
8947
|
+
} catch {
|
|
8948
|
+
return false
|
|
8949
|
+
}
|
|
8950
|
+
if (!args) return false
|
|
8951
|
+
const from = Host.from(remote);
|
|
8952
|
+
this._participants.set(from.cid, from);
|
|
8953
|
+
this._emitSafely('message', from, tag, ...args);
|
|
8954
|
+
return true
|
|
8955
|
+
}
|
|
8956
|
+
|
|
8957
|
+
_remoteClosed(remote) {
|
|
8958
|
+
const from = this._participants.get(remote.cid);
|
|
8959
|
+
if (!from) return
|
|
8960
|
+
this._participants.delete(remote.cid);
|
|
8961
|
+
this._emitSafely('leave', Host.from(remote), remote.closeReason ?? 'close');
|
|
8962
|
+
}
|
|
8963
|
+
|
|
8964
|
+
_emitSafely(name, ...args) {
|
|
8965
|
+
try {
|
|
8966
|
+
this.emit(name, ...args);
|
|
8967
|
+
} catch (error) {
|
|
8968
|
+
if (this.listenerCount('error')) this.emit('error', error);
|
|
8969
|
+
else console.error(`host ${this.name} ${name} handler error:`, error);
|
|
8970
|
+
}
|
|
8971
|
+
}
|
|
8972
|
+
}
|
|
8973
|
+
|
|
8974
|
+
// Characters of an anonymous CID after its '?': lowercase Crockford base32, easy to read aloud and type.
|
|
8975
|
+
const ANONYMOUS_CID_ALPHABET = '0123456789abcdefghjkmnpqrstvwxyz';
|
|
8976
|
+
|
|
8733
8977
|
const encoder = new TextEncoder();
|
|
8734
8978
|
const decoder$1 = new TextDecoder();
|
|
8735
8979
|
|
|
@@ -8748,7 +8992,10 @@ class Manager {
|
|
|
8748
8992
|
this.counters = { rxMessages: 0, rxBytes: 0, txMessages: 0, txBytes: 0,
|
|
8749
8993
|
publishes: 0, deliveryAttempts: 0, quotaExcluded: 0, oversizeRejected: 0, rateLimited: 0,
|
|
8750
8994
|
peerRxLimitSkipped: 0, // messages not sent: larger than the peer's advertised RX buffer
|
|
8751
|
-
policyDenied: 0
|
|
8995
|
+
policyDenied: 0, // unicasts and CID publications a device's delivery policy kept back
|
|
8996
|
+
hostInbound: 0, // client signals handed to a host (server.host)
|
|
8997
|
+
hostRejected: 0, // signals to a host tag that were not handed over (E2E, malformed payload)
|
|
8998
|
+
hostDenied: 0 }; // client signals to a tag a host owns, dropped
|
|
8752
8999
|
|
|
8753
9000
|
this.authManager = authManager;
|
|
8754
9001
|
|
|
@@ -8773,6 +9020,7 @@ class Manager {
|
|
|
8773
9020
|
this.retainExpiry = new Map(); // key: channel retain tag in a grace pool -> expiry time
|
|
8774
9021
|
this.retainBytes = 0; // all retained messages, channel and CID
|
|
8775
9022
|
this.channel_map = new Map(); // key: channelName -> value: < remote : Set >
|
|
9023
|
+
this.hosts = new HostTable(); // tags claimed by server.host() hosts
|
|
8776
9024
|
this.CHANNEL_PREFIX = 'CH:';
|
|
8777
9025
|
this.CID_PREFIX = 'CID:';
|
|
8778
9026
|
this.metrics = new Metrics(this);
|
|
@@ -8918,10 +9166,12 @@ class Manager {
|
|
|
8918
9166
|
if (this.connectionLogger) this.connectionLogger.log(remoteInfo);
|
|
8919
9167
|
// A replaced duplicate login no longer owns its CID; the new connection is still online.
|
|
8920
9168
|
const cidOwner = this.cid2remote.get(remote.cid);
|
|
8921
|
-
|
|
9169
|
+
const ownsCid = !remote.cid || !cidOwner || cidOwner === remote;
|
|
9170
|
+
if (ownsCid) {
|
|
8922
9171
|
this.deligateSignal(remote, '@state', 'close');
|
|
8923
9172
|
this.deligateSignal(remote, '@$state', 'close');
|
|
8924
9173
|
}
|
|
9174
|
+
if (this.hosts.size) this.hosts.remoteClosed(remote, ownsCid);
|
|
8925
9175
|
|
|
8926
9176
|
// remove subscriber
|
|
8927
9177
|
for (let ch of remote.channels.keys()) {
|
|
@@ -9044,6 +9294,26 @@ class Manager {
|
|
|
9044
9294
|
if (tag.indexOf('$') == 0) return ['err', 'prefix $ is reserved for userSet tag.']
|
|
9045
9295
|
|
|
9046
9296
|
let cidIndex = tag.indexOf('@');
|
|
9297
|
+
|
|
9298
|
+
// Hosts (server.host): a signal to an inbound tag goes to the host only, with its sender; a
|
|
9299
|
+
// client may not send to a tag a host owns. CID publications ('@topic') name their sender and
|
|
9300
|
+
// are never hosted.
|
|
9301
|
+
if (this.hosts.size && cidIndex !== 0) {
|
|
9302
|
+
const entry = this.hosts.match(cidIndex > 0 ? tag.slice(cidIndex) : tag);
|
|
9303
|
+
if (entry?.kind === 'inbound') {
|
|
9304
|
+
if (entry.host._receive(remote, tag, message)) {
|
|
9305
|
+
this.counters.hostInbound++;
|
|
9306
|
+
return ['ok', 1]
|
|
9307
|
+
}
|
|
9308
|
+
this.counters.hostRejected++;
|
|
9309
|
+
return ['err', 'Not accepted by the host']
|
|
9310
|
+
}
|
|
9311
|
+
if (entry) {
|
|
9312
|
+
this.counters.hostDenied++;
|
|
9313
|
+
if (this.server?.listenerCount?.('host:denied')) this.server.emit('host:denied', remote, tag, entry.host.name);
|
|
9314
|
+
return ['err', 'Tag is reserved by a host']
|
|
9315
|
+
}
|
|
9316
|
+
}
|
|
9047
9317
|
// A device whose policy also covers its publications: each subscriber is checked (see deliveryPolicy.js).
|
|
9048
9318
|
const publisher = cidIndex === 0 && remote.rxPolicy?.filterPublish ? remote : null;
|
|
9049
9319
|
if (cidIndex === 0) {
|
|
@@ -9108,36 +9378,39 @@ class Manager {
|
|
|
9108
9378
|
}
|
|
9109
9379
|
|
|
9110
9380
|
this.counters.publishes++;
|
|
9381
|
+
const sent = this.deliver(tag, message, remote.quota.fanout, publisher);
|
|
9382
|
+
if (sent === null) return ['err', 'No subscriber.']
|
|
9383
|
+
return ['ok', sent]
|
|
9384
|
+
}
|
|
9385
|
+
|
|
9386
|
+
// Sends a SIGNAL to the subscribers of a full channel key ('CH:…', 'CID:…', home channel), at
|
|
9387
|
+
// most fanout (null: all). publisher: a device whose delivery policy filters its publications.
|
|
9388
|
+
// Returns the number sent, or null when the channel has no subscribers.
|
|
9389
|
+
deliver(tag, message, fanout, publisher = null) {
|
|
9390
|
+
const remotes = this.channel_map.get(tag);
|
|
9391
|
+
if (!remotes || remotes.size === 0) return null
|
|
9392
|
+
const limit = fanout === null ? remotes.size : Math.min(fanout, remotes.size);
|
|
9393
|
+
this.counters.quotaExcluded += remotes.size - limit;
|
|
9111
9394
|
let sentCounter = 0;
|
|
9112
|
-
|
|
9113
|
-
|
|
9114
|
-
|
|
9115
|
-
|
|
9116
|
-
|
|
9117
|
-
const limit = fanout === null ? remotes.size : Math.min(fanout, remotes.size);
|
|
9118
|
-
this.counters.quotaExcluded += remotes.size - limit;
|
|
9119
|
-
let peers = remotes.values();
|
|
9120
|
-
while (sentCounter < limit) {
|
|
9121
|
-
let c = peers.next().value;
|
|
9122
|
-
if (!c) {
|
|
9123
|
-
// console.log('## null remote' )
|
|
9124
|
-
break;
|
|
9125
|
-
}
|
|
9126
|
-
if (publisher && !deliveryAllowed(publisher, c)) {
|
|
9127
|
-
this.deliveryDenied(publisher, c, 'publish');
|
|
9128
|
-
continue
|
|
9129
|
-
}
|
|
9130
|
-
// change: from v4.x allow receiveSelfMessages.
|
|
9131
|
-
c.send_enc_mode(message);
|
|
9132
|
-
sentCounter++;
|
|
9133
|
-
}
|
|
9134
|
-
|
|
9135
|
-
this.counters.deliveryAttempts += sentCounter;
|
|
9136
|
-
return ['ok', sentCounter]
|
|
9395
|
+
for (const c of remotes) {
|
|
9396
|
+
if (sentCounter >= limit) break;
|
|
9397
|
+
if (publisher && !deliveryAllowed(publisher, c)) {
|
|
9398
|
+
this.deliveryDenied(publisher, c, 'publish');
|
|
9399
|
+
continue
|
|
9137
9400
|
}
|
|
9401
|
+
// change: from v4.x allow receiveSelfMessages.
|
|
9402
|
+
c.send_enc_mode(message);
|
|
9403
|
+
sentCounter++;
|
|
9138
9404
|
}
|
|
9139
|
-
|
|
9140
|
-
return
|
|
9405
|
+
this.counters.deliveryAttempts += sentCounter;
|
|
9406
|
+
return sentCounter
|
|
9407
|
+
}
|
|
9408
|
+
|
|
9409
|
+
// A host's publication (Host.publish): full channel key, already packed.
|
|
9410
|
+
hostPublish(host, tag, message) {
|
|
9411
|
+
if (tag.includes('$') && this.retainOptions.enabled) this.storeChannelRetain(tag, message, null, host);
|
|
9412
|
+
this.counters.publishes++;
|
|
9413
|
+
return this.deliver(tag, message, host.fanout) ?? 0
|
|
9141
9414
|
}
|
|
9142
9415
|
|
|
9143
9416
|
|
|
@@ -9185,14 +9458,18 @@ class Manager {
|
|
|
9185
9458
|
return owner
|
|
9186
9459
|
}
|
|
9187
9460
|
|
|
9188
|
-
|
|
9189
|
-
|
|
9461
|
+
// host: a Host (server.host) writing its own tag, with its own budget; remote is then null.
|
|
9462
|
+
storeChannelRetain(tag, message, remote, host = null) {
|
|
9463
|
+
const owner = this.retainOwner(host ? host.retainOwnerKey
|
|
9464
|
+
: remote.boho.isAuthorized ? 'cid:' + remote.cid : 'conn:' + remote.ssid);
|
|
9465
|
+
const tagsPerOwner = host?.retainTags ?? this.retainOptions.tagsPerOwner;
|
|
9466
|
+
const bytesPerOwner = host?.retainBytes ?? this.retainOptions.bytesPerOwner;
|
|
9190
9467
|
const old = this.retain_messages.get(tag);
|
|
9191
9468
|
const freed = old?.byteLength ?? 0;
|
|
9192
9469
|
const room = old || (this.retain_messages.size < this.retainOptions.limitCounter &&
|
|
9193
|
-
owner.tags.size <
|
|
9470
|
+
owner.tags.size < tagsPerOwner);
|
|
9194
9471
|
const ownerBytes = owner.bytes - (this.retain_owner.get(tag) === owner ? freed : 0);
|
|
9195
|
-
if (room && this.retainFits(message, freed, ownerBytes,
|
|
9472
|
+
if (room && this.retainFits(message, freed, ownerBytes, bytesPerOwner)) {
|
|
9196
9473
|
if (old) this.releaseChannelRetain(tag, freed);
|
|
9197
9474
|
this.retain_messages.set(tag, this.retainCopy(message));
|
|
9198
9475
|
owner.bytes += message.byteLength;
|
|
@@ -9352,17 +9629,20 @@ class Manager {
|
|
|
9352
9629
|
}
|
|
9353
9630
|
|
|
9354
9631
|
|
|
9632
|
+
// One random byte per CID character.
|
|
9355
9633
|
anonymousCidRandom(size) {
|
|
9356
9634
|
return globalThis.crypto.getRandomValues(Buffer.alloc(size))
|
|
9357
9635
|
}
|
|
9358
9636
|
|
|
9359
|
-
// '?' +
|
|
9360
|
-
// synchronous turn (CID_REQ handler), so concurrent
|
|
9361
|
-
//
|
|
9637
|
+
// '?' + lowercase Crockford base32 (ANONYMOUS_CID_ALPHABET: no i, l, o, u), never one that is
|
|
9638
|
+
// online. Check and set run in the same synchronous turn (CID_REQ handler), so concurrent
|
|
9639
|
+
// requests cannot take the same CID. 4 characters normally; 8 if the short space keeps colliding.
|
|
9362
9640
|
issueAnonymousCid() {
|
|
9363
|
-
for (const [size, tries] of [[
|
|
9641
|
+
for (const [size, tries] of [[4, 8], [8, 8]]) {
|
|
9364
9642
|
for (let i = 0; i < tries; i++) {
|
|
9365
|
-
|
|
9643
|
+
let cid = '?';
|
|
9644
|
+
// 32 divides 256, so the low 5 bits of a random byte are uniform.
|
|
9645
|
+
for (const byte of this.anonymousCidRandom(size)) cid += ANONYMOUS_CID_ALPHABET[byte & 31];
|
|
9366
9646
|
if (!this.cid2remote.has(cid)) return cid
|
|
9367
9647
|
}
|
|
9368
9648
|
}
|
|
@@ -9655,7 +9935,6 @@ class BohoAuth {
|
|
|
9655
9935
|
//7. setting info.
|
|
9656
9936
|
peer.did = id;
|
|
9657
9937
|
peer.cid = authInfo.cid;
|
|
9658
|
-
peer.nick = authInfo.cid; // temporary: nick as cid
|
|
9659
9938
|
if (authInfo.uid) peer.uid = authInfo.uid;
|
|
9660
9939
|
// Delivery policy of an owned device, in place before the CID is reachable (step 9).
|
|
9661
9940
|
peer.ownerUid = uid;
|
|
@@ -9925,6 +10204,7 @@ class Server extends require$$0$3 {
|
|
|
9925
10204
|
this.membersOnly = boolean('membersOnly', options.membersOnly ?? serverOption.membersOnly);
|
|
9926
10205
|
this.serviceNames = new Set();
|
|
9927
10206
|
this.services = new Map(); // name -> service module (for lifecycle hooks)
|
|
10207
|
+
this.hosts = new Set(); // open hosts (server.host)
|
|
9928
10208
|
// Stays null for a CongSocket-only server, so close() does not wait on a WebSocket server.
|
|
9929
10209
|
this.wss = null;
|
|
9930
10210
|
this.port = null;
|
|
@@ -10050,6 +10330,25 @@ class Server extends require$$0$3 {
|
|
|
10050
10330
|
return this.manager.metrics.snapshot()
|
|
10051
10331
|
}
|
|
10052
10332
|
|
|
10333
|
+
/**
|
|
10334
|
+
* Server-side logic (a game, a room) in this process. Signals clients send to the inbound tags go
|
|
10335
|
+
* to the host only, with the server's view of the sender; clients cannot send to the tags it owns.
|
|
10336
|
+
* @param {string} name 1-64 characters of A-Z a-z 0-9 _ . -
|
|
10337
|
+
* @param {{ inbound: string[], own?: string[], fanout?: number|null,
|
|
10338
|
+
* retain?: { tags?: number, bytes?: number } }} options
|
|
10339
|
+
* inbound: channel tags clients send to the host (a trailing '*' is a prefix). Not relayed.
|
|
10340
|
+
* own: channel tags only the host publishes, and '@topic' unicast topics only the host sends.
|
|
10341
|
+
* fanout: most subscribers one publish reaches (default null: all).
|
|
10342
|
+
* retain: budget of the host's retained ('$') values (default: retain.tagsPerOwner/bytesPerOwner).
|
|
10343
|
+
* @returns {Host}
|
|
10344
|
+
*/
|
|
10345
|
+
host(name, options) {
|
|
10346
|
+
const host = new Host(this.manager, name, options);
|
|
10347
|
+
host.once('close', () => this.hosts.delete(host));
|
|
10348
|
+
this.hosts.add(host);
|
|
10349
|
+
return host
|
|
10350
|
+
}
|
|
10351
|
+
|
|
10053
10352
|
/**
|
|
10054
10353
|
* RPC service register.
|
|
10055
10354
|
* service: service_name <string>
|
|
@@ -10109,6 +10408,7 @@ class Server extends require$$0$3 {
|
|
|
10109
10408
|
*/
|
|
10110
10409
|
close(callback) {
|
|
10111
10410
|
console.log('closing iosignal server...');
|
|
10411
|
+
for (const host of [...this.hosts]) host.close();
|
|
10112
10412
|
if (this.manager) {
|
|
10113
10413
|
this.manager.close();
|
|
10114
10414
|
}
|
|
@@ -10211,6 +10511,9 @@ class FileKeyProvider {
|
|
|
10211
10511
|
}
|
|
10212
10512
|
|
|
10213
10513
|
const DEVICE_PREFIX = "device:";
|
|
10514
|
+
// Every device CID in use. The account service picks new CIDs outside it, so a device added here
|
|
10515
|
+
// is listed too and no member can be given its CID.
|
|
10516
|
+
const CID_SET = "sets_cid";
|
|
10214
10517
|
|
|
10215
10518
|
class RedisKeyProvider {
|
|
10216
10519
|
constructor(redisClient) {
|
|
@@ -10247,11 +10550,17 @@ class RedisKeyProvider {
|
|
|
10247
10550
|
const fields = { 'key': Base64hashKey, 'cid': cid };
|
|
10248
10551
|
if (role) fields.role = role;
|
|
10249
10552
|
if (quota) fields.quota = quota;
|
|
10250
|
-
|
|
10553
|
+
const old = await this.redis.hGet(DEVICE_PREFIX + id, 'cid');
|
|
10554
|
+
const result = await this.redis.hSet(DEVICE_PREFIX + id, fields);
|
|
10555
|
+
if (old && old !== cid) await this.redis.sRem(CID_SET, old);
|
|
10556
|
+
if (cid) await this.redis.sAdd(CID_SET, cid);
|
|
10557
|
+
return result
|
|
10251
10558
|
}
|
|
10252
10559
|
|
|
10253
10560
|
//sudoService call
|
|
10254
10561
|
async delAuth(id) {
|
|
10562
|
+
const cid = await this.redis.hGet(DEVICE_PREFIX + id, 'cid');
|
|
10563
|
+
if (cid) await this.redis.sRem(CID_SET, cid);
|
|
10255
10564
|
return this.redis.del(DEVICE_PREFIX + id)
|
|
10256
10565
|
}
|
|
10257
10566
|
|