webpeerjs 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  WebpeerJS enables browser to browser connectivity without a central server.
5
5
 
6
+ [Demo](https://nuzulul.github.io/webpeerjs/demo/)
7
+
6
8
  ## Example
7
9
 
8
10
  ```
@@ -16,14 +18,13 @@ void async function main() {
16
18
 
17
19
  const [send,listen,members] = node.joinRoom('myroom')
18
20
 
19
- send('hello')
20
-
21
21
  listen((message,id) => {
22
22
  console.log(`Message from ${id} : ${message}`)
23
23
  })
24
24
 
25
25
  members((data) => {
26
26
  console.log(`Members : ${data}`)
27
+ send('hello')
27
28
  })
28
29
 
29
30
  }()
@@ -31,10 +32,18 @@ void async function main() {
31
32
 
32
33
  ## Install
33
34
 
35
+ NPM :
36
+
34
37
  ```
35
38
  npm i webpeerjs
36
39
  ```
37
40
 
41
+ CDN :
42
+
43
+ ```
44
+ <script src="https://cdn.jsdelivr.net/npm/webpeerjs@0.0/dist/umd/webpeerjs.min.js"></script>
45
+ ```
46
+
38
47
  ## API
39
48
 
40
49
  - `createWebpeer()` Create a new local node.
@@ -1 +1 @@
1
- {"type": "module","types": "webpeerjs.d.ts"}
1
+ {"type": "module"}
@@ -1,4 +1,5 @@
1
- import { Peer } from '#/pubsub-peer-discovery/peer.js';
1
+ import { message, encodeMessage, decodeMessage } from 'protons-runtime';
2
+ import { alloc } from 'uint8arrays/alloc';
2
3
  import { Key } from 'interface-datastore';
3
4
  import { sha256 } from 'multiformats/hashes/sha2';
4
5
  import { createLibp2p } from 'libp2p';
@@ -294,6 +295,69 @@ const CONFIG_KNOWN_BOOTSTRAP_PEER_IDS = [
294
295
  '12D3KooWBdF3g6vSJFRPoZQo7BNnkNzaWb59gpyaVzsgtNTVeu8H'
295
296
  ];
296
297
 
298
+ /* eslint-disable import/export */
299
+ /* eslint-disable complexity */
300
+ /* eslint-disable @typescript-eslint/no-namespace */
301
+ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
302
+ /* eslint-disable @typescript-eslint/no-empty-interface */
303
+ var Peer;
304
+ (function (Peer) {
305
+ let _codec;
306
+ Peer.codec = () => {
307
+ if (_codec == null) {
308
+ _codec = message((obj, w, opts = {}) => {
309
+ if (opts.lengthDelimited !== false) {
310
+ w.fork();
311
+ }
312
+ if ((obj.publicKey != null && obj.publicKey.byteLength > 0)) {
313
+ w.uint32(10);
314
+ w.bytes(obj.publicKey);
315
+ }
316
+ if (obj.addrs != null) {
317
+ for (const value of obj.addrs) {
318
+ w.uint32(18);
319
+ w.bytes(value);
320
+ }
321
+ }
322
+ if (opts.lengthDelimited !== false) {
323
+ w.ldelim();
324
+ }
325
+ }, (reader, length) => {
326
+ const obj = {
327
+ publicKey: alloc(0),
328
+ addrs: []
329
+ };
330
+ const end = length == null ? reader.len : reader.pos + length;
331
+ while (reader.pos < end) {
332
+ const tag = reader.uint32();
333
+ switch (tag >>> 3) {
334
+ case 1: {
335
+ obj.publicKey = reader.bytes();
336
+ break;
337
+ }
338
+ case 2: {
339
+ obj.addrs.push(reader.bytes());
340
+ break;
341
+ }
342
+ default: {
343
+ reader.skipType(tag & 7);
344
+ break;
345
+ }
346
+ }
347
+ }
348
+ return obj;
349
+ });
350
+ }
351
+ return _codec;
352
+ };
353
+ Peer.encode = (obj) => {
354
+ return encodeMessage(obj, Peer.codec());
355
+ };
356
+ Peer.decode = (buf) => {
357
+ return decodeMessage(buf, Peer.codec());
358
+ };
359
+ })(Peer || (Peer = {}));
360
+
297
361
  const prefix = CONFIG_PREFIX;
298
362
 
299
363
  const mkErr = msg => new Error(`${prefix}: ${msg}`);
@@ -645,6 +709,7 @@ class webpeerjs{
645
709
  const json = JSON.parse(msg);
646
710
  const prefix = json.prefix;
647
711
  const room = json.room;
712
+ const rooms = json.rooms;
648
713
  const message = json.message;
649
714
  const signal = json.signal;
650
715
  const id = json.id;
@@ -678,7 +743,9 @@ class webpeerjs{
678
743
  if(this.#rooms[room]){
679
744
 
680
745
  //inbound message
681
- this.#rooms[room].onMessage(message,id);
746
+ if(message){
747
+ this.#rooms[room].onMessage(message,id);
748
+ }
682
749
 
683
750
  //update room members
684
751
  if(!this.#rooms[room].members.includes(id)){
@@ -688,15 +755,26 @@ class webpeerjs{
688
755
  }
689
756
  }
690
757
 
758
+ if(rooms){
759
+ for(const room of Object.keys(this.#rooms)){
760
+ //update room members
761
+ if(!this.#rooms[room].members.includes(id)){
762
+ this.#rooms[room].members.push(id);
763
+ this.#rooms[room].onMembers(this.#rooms[room].members);
764
+ }
765
+ }
766
+ }
767
+
691
768
  if(signal){
692
769
 
693
770
  //repply announce with ping
694
771
  if(signal == 'announce'){
695
- setTimeout(()=>{this.#ping();},1000);
772
+ setTimeout(()=>{this.#ping('yes');},1000);
773
+ //console.log('rooms',rooms)
696
774
  }
697
775
 
698
776
  if(signal == 'ping'){
699
- //do nothing
777
+ //console.log('rooms',rooms)
700
778
  }
701
779
 
702
780
  //update connected webpeers
@@ -1015,7 +1093,7 @@ class webpeerjs{
1015
1093
  //announce and ping via pupsub peer discovery
1016
1094
  async #announce(){
1017
1095
  const topics = CONFIG_PUBSUB_PEER_DISCOVERY;
1018
- const data = JSON.stringify({prefix:CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address});
1096
+ const data = JSON.stringify({prefix:CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms});
1019
1097
  const peer = {
1020
1098
  publicKey: this.#libp2p.peerId.publicKey,
1021
1099
  addrs: [uint8ArrayFromString(data)],
@@ -1027,7 +1105,7 @@ class webpeerjs{
1027
1105
  }
1028
1106
  async #ping(){
1029
1107
  const topics = CONFIG_PUBSUB_PEER_DISCOVERY;
1030
- const data = JSON.stringify({prefix:CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address});
1108
+ const data = JSON.stringify({prefix:CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms});
1031
1109
  const peer = {
1032
1110
  publicKey: this.#libp2p.peerId.publicKey,
1033
1111
  addrs: [uint8ArrayFromString(data)],
@@ -1 +1 @@
1
- {"type": "commonjs","types": "webpeerjs.d.ts"}
1
+ {"type": "commonjs"}