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/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "webpeerjs",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Simple peer-to-peer with IPFS",
5
5
  "main": "./dist/umd/webpeerjs.js",
6
6
  "module": "./dist/esm/webpeerjs.js",
7
- "imports": {
8
- "#/pubsub-peer-discovery/*.js": "@libp2p/pubsub-peer-discovery/dist/src/*.js"
9
- },
10
7
  "exports": {
11
8
  ".": {
12
9
  "import": "./dist/esm/webpeerjs.js",
@@ -25,7 +22,7 @@
25
22
  "eslint:fix": "eslint ./src --fix",
26
23
 
27
24
  "removedir": "node -e \"var fs = require('fs'); try{process.argv.slice(1).map((fpath) => fs.rmdirSync(fpath, { recursive: true }))}catch(err){console.log(`Dist not found`)}; process.exit(0);\"",
28
- "build-all": "tsc -p config/tsconfig-rollup.json && rollup -c temp/config/rollup.config.build.js && echo {\"type\": \"commonjs\",\"types\": \"webpeerjs.d.ts\"}>dist\\umd\\package.json && echo {\"type\": \"module\",\"types\": \"webpeerjs.d.ts\"}>dist\\esm\\package.json",
25
+ "build-all": "tsc -p config/tsconfig-rollup.json && rollup -c temp/config/rollup.config.build.js && echo {\"type\": \"commonjs\"}>dist\\umd\\package.json && echo {\"type\": \"module\"}>dist\\esm\\package.json",
29
26
  "build-types": "tsc -p config/tsconfig-esm.json",
30
27
  "build": "npm run removedir dist temp && npm run build-all && npm run build-types",
31
28
 
package/src/peer.js ADDED
@@ -0,0 +1,64 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable complexity */
3
+ /* eslint-disable @typescript-eslint/no-namespace */
4
+ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
+ /* eslint-disable @typescript-eslint/no-empty-interface */
6
+ import { decodeMessage, encodeMessage, message } from 'protons-runtime';
7
+ import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc';
8
+ export var Peer;
9
+ (function (Peer) {
10
+ let _codec;
11
+ Peer.codec = () => {
12
+ if (_codec == null) {
13
+ _codec = message((obj, w, opts = {}) => {
14
+ if (opts.lengthDelimited !== false) {
15
+ w.fork();
16
+ }
17
+ if ((obj.publicKey != null && obj.publicKey.byteLength > 0)) {
18
+ w.uint32(10);
19
+ w.bytes(obj.publicKey);
20
+ }
21
+ if (obj.addrs != null) {
22
+ for (const value of obj.addrs) {
23
+ w.uint32(18);
24
+ w.bytes(value);
25
+ }
26
+ }
27
+ if (opts.lengthDelimited !== false) {
28
+ w.ldelim();
29
+ }
30
+ }, (reader, length) => {
31
+ const obj = {
32
+ publicKey: uint8ArrayAlloc(0),
33
+ addrs: []
34
+ };
35
+ const end = length == null ? reader.len : reader.pos + length;
36
+ while (reader.pos < end) {
37
+ const tag = reader.uint32();
38
+ switch (tag >>> 3) {
39
+ case 1: {
40
+ obj.publicKey = reader.bytes();
41
+ break;
42
+ }
43
+ case 2: {
44
+ obj.addrs.push(reader.bytes());
45
+ break;
46
+ }
47
+ default: {
48
+ reader.skipType(tag & 7);
49
+ break;
50
+ }
51
+ }
52
+ }
53
+ return obj;
54
+ });
55
+ }
56
+ return _codec;
57
+ };
58
+ Peer.encode = (obj) => {
59
+ return encodeMessage(obj, Peer.codec());
60
+ };
61
+ Peer.decode = (buf) => {
62
+ return decodeMessage(buf, Peer.codec());
63
+ };
64
+ })(Peer || (Peer = {}));
package/src/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as config from './config'
2
2
 
3
- import { Peer as PBPeer } from '#/pubsub-peer-discovery/peer.js'
3
+ import { Peer as PBPeer } from './peer'
4
4
 
5
5
  import { Key } from 'interface-datastore'
6
6
 
package/src/webpeerjs.js CHANGED
@@ -217,6 +217,7 @@ class webpeerjs{
217
217
  const json = JSON.parse(msg)
218
218
  const prefix = json.prefix
219
219
  const room = json.room
220
+ const rooms = json.rooms
220
221
  const message = json.message
221
222
  const signal = json.signal
222
223
  const id = json.id
@@ -250,7 +251,9 @@ class webpeerjs{
250
251
  if(this.#rooms[room]){
251
252
 
252
253
  //inbound message
253
- this.#rooms[room].onMessage(message,id)
254
+ if(message){
255
+ this.#rooms[room].onMessage(message,id)
256
+ }
254
257
 
255
258
  //update room members
256
259
  if(!this.#rooms[room].members.includes(id)){
@@ -260,15 +263,26 @@ class webpeerjs{
260
263
  }
261
264
  }
262
265
 
266
+ if(rooms){
267
+ for(const room of Object.keys(this.#rooms)){
268
+ //update room members
269
+ if(!this.#rooms[room].members.includes(id)){
270
+ this.#rooms[room].members.push(id)
271
+ this.#rooms[room].onMembers(this.#rooms[room].members)
272
+ }
273
+ }
274
+ }
275
+
263
276
  if(signal){
264
277
 
265
278
  //repply announce with ping
266
279
  if(signal == 'announce'){
267
- setTimeout(()=>{this.#ping()},1000)
280
+ setTimeout(()=>{this.#ping('yes')},1000)
281
+ //console.log('rooms',rooms)
268
282
  }
269
283
 
270
284
  if(signal == 'ping'){
271
- //do nothing
285
+ //console.log('rooms',rooms)
272
286
  }
273
287
 
274
288
  //update connected webpeers
@@ -593,7 +607,7 @@ class webpeerjs{
593
607
  //announce and ping via pupsub peer discovery
594
608
  async #announce(){
595
609
  const topics = config.CONFIG_PUBSUB_PEER_DISCOVERY
596
- const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address})
610
+ const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
597
611
  const peer = {
598
612
  publicKey: this.#libp2p.peerId.publicKey,
599
613
  addrs: [uint8ArrayFromString(data)],
@@ -605,7 +619,7 @@ class webpeerjs{
605
619
  }
606
620
  async #ping(){
607
621
  const topics = config.CONFIG_PUBSUB_PEER_DISCOVERY
608
- const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address})
622
+ const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
609
623
  const peer = {
610
624
  publicKey: this.#libp2p.peerId.publicKey,
611
625
  addrs: [uint8ArrayFromString(data)],