Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hub.js

Package Overview
Dependencies
Maintainers
1
Versions
114
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hub.js - npm Package Compare versions

Comparing version 0.0.4 to 0.0.6

98

dist/browser.es.js
import hash from 'string-hash';
import { create, parse, subscribe } from 'brisky-struct';
import { c, create, parse, set, subscribe } from 'brisky-struct';
import bs from 'brisky-stamp';

@@ -80,7 +80,6 @@

while (p) {
if (p.url && !p.context) { hub = p; }
if (p.url && !p._c) { hub = p; }
p = p.parent(); // needs to walk over context (for multi server)
}
if (hub && !hub.receiveOnly && struct.key !== 'clients') {
// console.log('✍️ go send --->', hub.id, struct.path(), '✍️', val)
if (struct === hub) {

@@ -154,6 +153,6 @@ if (val === null) {

if (/^.+=>/.test(val)) {
if (!/^\(.+\) +=>/.test(val)) {
if (!/^(\(){0, 1}.+(\)){0, 1} +=>/.test(val)) {
val = val.replace(/^(.*?)( +=>)/, '($1)$2');
}
if (!/^.+=> +{(.*?)}/.test(val)) {
if (!/^(.*?)+=> +{(.*?)}$/.test(val) && val.indexOf('return ') === -1) {
val = val.replace(/^(.+=> *?)(.*?)/, '$1 { return $2') + ' }';

@@ -166,2 +165,3 @@ }

result['$fn|' + i] = val;
// also here we need to rewrite client to use client id
} else if (typeof obj[i] !== 'object') {

@@ -237,7 +237,4 @@ // sync later!

const stamp = bs.create('upstream');
// console.log(`\n🌚 receive on ${hub.id} from ${hub.url} -->`, stamp, ((encodeURI(data).split(/%..|./).length - 1) / 1e3).toFixed(2) + 'kb 🌚')
// console.log(' ', JSON.stringify(JSON.parse(data), false, 2))
hub.set(JSON.parse(data), stamp);
bs.close(stamp);
// console.log('🌚 done 🌚\n')
};

@@ -265,6 +262,4 @@ };

hub.on((val, stamp, t) => {
if (val === null && !t.context && t === hub) {
console.log('remove subs');
if (val === null && !t._c && t === hub) {
hub.subscriptions = [];
// console.log(`☠️ REMOVE CLIENT ${hub.id} ☠️`)
removeUrl(hub);

@@ -318,7 +313,3 @@ removeSocket(hub);

hub.clientContext = val;
if (hub.connected && hub.connected.compute() === true) {
// console.log('ai need to re-send some stuff')
// console.log('this will tigh in to sendMeta')
meta(hub);
}
if (hub.connected && hub.connected.compute() === true) meta(hub);
}

@@ -346,3 +337,2 @@ };

const key = hash(JSON.stringify(parsed));
// why not just add them to client... so much easier
if (!this.upstreamSubscriptions) {

@@ -363,3 +353,2 @@ this.upstreamSubscriptions = { [key]: parsed };

// export { props, define }

@@ -380,34 +369,33 @@

// import { struct } from 'brisky-struct'
const props$1 = { contextKey: true };
const clientHub = {
type: 'hub',
props: {
cache: true,
upstreamSubscriptions: true,
// isUpstream: true,
// myupstream subs
resolve: true,
socket: true // only nessecary for downstream clients
const define$1 = {
getContext (val) {
var result = find(this, val);
if (!result) {
result = this.create({ contextKey: val }, false);
}
return result
}
};
const props$1 = {
clients: { // need to exclude on context
props: {
default: clientHub
const find = (hub, val) => {
const instances = hub.instances;
if (instances) {
let i = instances.length;
while (i--) {
if (instances[i].contextKey === val) {
// console.log('found context --->', val, instances[i].clients.keys())
return instances[i]
}
}
},
client: true // test if you can subscribe to it
}
};
var clients = Object.freeze({
props: props$1
var context$1 = Object.freeze({
props: props$1,
define: define$1
});
// need to be able to set timestamp mode on bs -- allways fo ts on bs
const hub = create({

@@ -419,12 +407,34 @@ type: 'hub',

default: 'self',
id: (t, val) => { t.set({ define: { id: val } }); }
id: (t, val) => { t.set({ define: { id: val } }); },
clients: (t, val, key, stamp) => {
if (!t.clients) {
t.clients = c(clients, val, stamp, t, key);
} else {
set(t.clients, val, stamp);
}
},
client: true
},
id: id,
id,
inject: [
client,
server,
clients
context$1,
client
]
});
const clients = create({
props: {
default: hub.create({
props: {
cache: true,
upstreamSubscriptions: true,
resolve: true,
socket: true,
context: true
}
})
}
});
// import bs from 'brisky-stamp'

@@ -431,0 +441,0 @@

@@ -84,7 +84,6 @@ 'use strict';

while (p) {
if (p.url && !p.context) { hub = p; }
if (p.url && !p._c) { hub = p; }
p = p.parent(); // needs to walk over context (for multi server)
}
if (hub && !hub.receiveOnly && struct.key !== 'clients') {
// console.log('✍️ go send --->', hub.id, struct.path(), '✍️', val)
if (struct === hub) {

@@ -158,6 +157,6 @@ if (val === null) {

if (/^.+=>/.test(val)) {
if (!/^\(.+\) +=>/.test(val)) {
if (!/^(\(){0, 1}.+(\)){0, 1} +=>/.test(val)) {
val = val.replace(/^(.*?)( +=>)/, '($1)$2');
}
if (!/^.+=> +{(.*?)}/.test(val)) {
if (!/^(.*?)+=> +{(.*?)}$/.test(val) && val.indexOf('return ') === -1) {
val = val.replace(/^(.+=> *?)(.*?)/, '$1 { return $2') + ' }';

@@ -170,2 +169,3 @@ }

result['$fn|' + i] = val;
// also here we need to rewrite client to use client id
} else if (typeof obj[i] !== 'object') {

@@ -241,7 +241,4 @@ // sync later!

const stamp = bs.create('upstream');
// console.log(`\n🌚 receive on ${hub.id} from ${hub.url} -->`, stamp, ((encodeURI(data).split(/%..|./).length - 1) / 1e3).toFixed(2) + 'kb 🌚')
// console.log(' ', JSON.stringify(JSON.parse(data), false, 2))
hub.set(JSON.parse(data), stamp);
bs.close(stamp);
// console.log('🌚 done 🌚\n')
};

@@ -269,6 +266,4 @@ };

hub.on((val, stamp, t) => {
if (val === null && !t.context && t === hub) {
console.log('remove subs');
if (val === null && !t._c && t === hub) {
hub.subscriptions = [];
// console.log(`☠️ REMOVE CLIENT ${hub.id} ☠️`)
removeUrl(hub);

@@ -322,7 +317,3 @@ removeSocket(hub);

hub.clientContext = val;
if (hub.connected && hub.connected.compute() === true) {
// console.log('ai need to re-send some stuff')
// console.log('this will tigh in to sendMeta')
meta(hub);
}
if (hub.connected && hub.connected.compute() === true) meta(hub);
}

@@ -350,3 +341,2 @@ };

const key = hash(JSON.stringify(parsed));
// why not just add them to client... so much easier
if (!this.upstreamSubscriptions) {

@@ -367,3 +357,2 @@ this.upstreamSubscriptions = { [key]: parsed };

// export { props, define }

@@ -384,34 +373,33 @@

// import { struct } from 'brisky-struct'
const props$1 = { contextKey: true };
const clientHub = {
type: 'hub',
props: {
cache: true,
upstreamSubscriptions: true,
// isUpstream: true,
// myupstream subs
resolve: true,
socket: true // only nessecary for downstream clients
const define$1 = {
getContext (val) {
var result = find(this, val);
if (!result) {
result = this.create({ contextKey: val }, false);
}
return result
}
};
const props$1 = {
clients: { // need to exclude on context
props: {
default: clientHub
const find = (hub, val) => {
const instances = hub.instances;
if (instances) {
let i = instances.length;
while (i--) {
if (instances[i].contextKey === val) {
// console.log('found context --->', val, instances[i].clients.keys())
return instances[i]
}
}
},
client: true // test if you can subscribe to it
}
};
var clients = Object.freeze({
props: props$1
var context$1 = Object.freeze({
props: props$1,
define: define$1
});
// need to be able to set timestamp mode on bs -- allways fo ts on bs
const hub = briskyStruct.create({

@@ -423,12 +411,34 @@ type: 'hub',

default: 'self',
id: (t, val) => { t.set({ define: { id: val } }); }
id: (t, val) => { t.set({ define: { id: val } }); },
clients: (t, val, key, stamp) => {
if (!t.clients) {
t.clients = briskyStruct.c(clients, val, stamp, t, key);
} else {
briskyStruct.set(t.clients, val, stamp);
}
},
client: true
},
id: id,
id,
inject: [
client,
server,
clients
context$1,
client
]
});
const clients = briskyStruct.create({
props: {
default: hub.create({
props: {
cache: true,
upstreamSubscriptions: true,
resolve: true,
socket: true,
context: true
}
})
}
});
// import bs from 'brisky-stamp'

@@ -435,0 +445,0 @@

import hash from 'string-hash';
import { create, get, getKeys, parse, subscribe } from 'brisky-struct';
import { c, create, get, getKeys, parse, set, subscribe } from 'brisky-struct';
import bs from 'brisky-stamp';

@@ -79,7 +79,6 @@ import WebSocket from 'uws';

while (p) {
if (p.url && !p.context) { hub = p; }
if (p.url && !p._c) { hub = p; }
p = p.parent(); // needs to walk over context (for multi server)
}
if (hub && !hub.receiveOnly && struct.key !== 'clients') {
// console.log('✍️ go send --->', hub.id, struct.path(), '✍️', val)
if (struct === hub) {

@@ -150,6 +149,6 @@ if (val === null) {

if (/^.+=>/.test(val)) {
if (!/^\(.+\) +=>/.test(val)) {
if (!/^(\(){0, 1}.+(\)){0, 1} +=>/.test(val)) {
val = val.replace(/^(.*?)( +=>)/, '($1)$2');
}
if (!/^.+=> +{(.*?)}/.test(val)) {
if (!/^(.*?)+=> +{(.*?)}$/.test(val) && val.indexOf('return ') === -1) {
val = val.replace(/^(.+=> *?)(.*?)/, '$1 { return $2') + ' }';

@@ -162,2 +161,3 @@ }

result['$fn|' + i] = val;
// also here we need to rewrite client to use client id
} else if (typeof obj[i] !== 'object') {

@@ -233,7 +233,4 @@ // sync later!

const stamp = bs.create('upstream');
// console.log(`\n🌚 receive on ${hub.id} from ${hub.url} -->`, stamp, ((encodeURI(data).split(/%..|./).length - 1) / 1e3).toFixed(2) + 'kb 🌚')
// console.log(' ', JSON.stringify(JSON.parse(data), false, 2))
hub.set(JSON.parse(data), stamp);
bs.close(stamp);
// console.log('🌚 done 🌚\n')
};

@@ -261,6 +258,4 @@ };

hub.on((val, stamp, t) => {
if (val === null && !t.context && t === hub) {
console.log('remove subs');
if (val === null && !t._c && t === hub) {
hub.subscriptions = [];
// console.log(`☠️ REMOVE CLIENT ${hub.id} ☠️`)
removeUrl(hub);

@@ -314,7 +309,3 @@ removeSocket(hub);

hub.clientContext = val;
if (hub.connected && hub.connected.compute() === true) {
// console.log('ai need to re-send some stuff')
// console.log('this will tigh in to sendMeta')
meta(hub);
}
if (hub.connected && hub.connected.compute() === true) meta(hub);
}

@@ -342,3 +333,2 @@ };

const key = hash(JSON.stringify(parsed));
// why not just add them to client... so much easier
if (!this.upstreamSubscriptions) {

@@ -359,3 +349,2 @@ this.upstreamSubscriptions = { [key]: parsed };

// export { props, define }

@@ -409,3 +398,3 @@

i = i.slice(4);
// need to fix buble stuff in these fn creations -- prop need to add buble
// need to fix bublé stuff in these fn creations -- prop need to add buble
// runtime in a hub

@@ -453,3 +442,3 @@ let pass;

}
if (!isEmpty) { client.socket.send(JSON.stringify(client.inProgress)); }
if (!isEmpty) client.socket.send(JSON.stringify(client.inProgress));
client.inProgress = false;

@@ -473,7 +462,8 @@ }

if (tree.$t && tree.$t._p && !tree.$t._p[tree.$t.key]) {
serialize$2(hub.id, client, progress(client), subs, tree.$t, null, hub.serverIndex, tree);
serialize$2(hub.id, client, progress(client), subs, tree.$t, null, get(hub, 'serverIndex'), tree);
}
}
if (t.val !== void 0 || val === null || subs.val === true) {
serialize$2(hub.id, client, progress(client), subs, t, val, hub.serverIndex, tree);
// opt this line
serialize$2(hub.id, client, progress(client), subs, t, val, get(hub, 'serverIndex'), tree);
}

@@ -483,17 +473,9 @@ };

const cache = (client, struct, stamp, level, val) => {
if (!client.cache) { client.cache = {}; }
// console.log('CACHE IT---->', struct.path())
if (!client.cache) client.cache = {};
client.cache[struct.uid()] = stamp[0];
};
// const isCached = () => {}
const isCached = (client, struct, stamp) => client.cache &&
client.cache[struct.uid()] === stamp[0];
const isCached = (client, struct, stamp) => {
// // console.log('CACHE:', struct.path(), client.cache, c, stamp)
// if (client.cache && client.cache[struct.uid()] === stamp[0]) {
// console.log('BLOCKED BY CACHE')
// }
return client.cache && client.cache[struct.uid()] === stamp[0]
};
const setStamp = (s, stamp, src, struct, id, client, level) => {

@@ -507,4 +489,4 @@ cache(client, struct, stamp, level);

const serialize$2 = (id, client, t, subs, struct, val, level) => {
const stamp = struct.stamp;
if (stamp && (val === null || !isCached(client, struct, stamp))) {
const stamp = get(struct, 'stamp');
if ((stamp && (val === null || !isCached(client, struct, stamp))) || subs.val === true) {
const src = bs.src(stamp);

@@ -521,5 +503,3 @@ if (src !== client.key && bs.src(t.tStamp) !== client.key) {

const keys = getKeys(struct);
if (keys) {
deepSerialize(keys, id, client, t, subs, struct, val, level);
}
if (keys) deepSerialize(keys, id, client, t, subs, struct, val, level);
}

@@ -532,5 +512,3 @@ }

var s = t;
// var prev = t
for (let i = level; i < len; i++) {
// prev = s
let t = s[path[i]];

@@ -561,5 +539,3 @@ if (!t) {

const keys = getKeys(struct);
if (keys) {
deepSerialize(keys, id, client, t, subs, struct, val, level);
}
if (keys) deepSerialize(keys, id, client, t, subs, struct, val, level);
}

@@ -575,3 +551,3 @@ }

let prop = get(struct, keys[i]);
if (get(prop, 'type') === 'hub') {
if (prop && get(prop, 'type') === 'hub') {
serialize$2(id, client, t, subs, prop, val, level);

@@ -581,11 +557,11 @@ }

}
// feel really shacky /w context :/
// if (struct._removed) {
// for (let i = 0, len = struct._removed.length; i < len; i++) {
// let prop = struct._removed[i]
// if (get(prop, 'type') === 'hub') {
// serialize(id, client, t, subs, prop, null, level)
// }
// }
// }
// feels really shacky /w context :/ needs tests
if (struct._removed) {
for (let i = 0, len = struct._removed.length; i < len; i++) {
let prop = struct._removed[i];
if (prop && get(prop, 'type') === 'hub') {
serialize$2(id, client, t, subs, prop, null, level);
}
}
}
};

@@ -601,8 +577,10 @@

t = hub;
// switch etc
if (meta.subscriptions) {
if (client.context != meta.context) { // eslint-disable-line
client.set(null);
t = create$2(hub, socket, meta);
client = socket.client;
} else if (meta.subscriptions) {
incomingSubscriptions(t, client, meta, client.id);
bs.close();
}
// console.log('perhaps replace something (e.g. subs or whatever')
} else {

@@ -617,5 +595,2 @@ t = create$2(hub, socket, meta);

if (payload) {
// console.log(`\n👾👾👾👾👾 in ${hub.id} 👾👾👾👾👾`)
// console.log(JSON.stringify(payload, false, 2))
// var d = Date.now()
if (meta && meta.resolve) {

@@ -629,3 +604,2 @@ client.resolve = meta.resolve;

bs.close();
// console.log(`👾👾👾👾👾 ${Date.now() - d} ms 👾👾👾👾👾`)
}

@@ -637,14 +611,6 @@ };

const id = meta.id;
// console.log(`\n🐠 create client "${id}" on "${hub.id}" 🐠`)
var t;
if (meta.context) {
// console.log('got context! go do it!')
} else {
t = hub; // will also become secured -- default will ofc be IP
}
t.set({ clients: { [id]: { socket } } }, stamp);
const t = meta.context ? hub.getContext(meta.context) : hub;
t.set({ clients: { [id]: { socket, context: meta.context } } }, stamp);
const client = socket.client = t.clients[id];
if (meta.subscriptions) {
incomingSubscriptions(t, client, meta, id);
}
if (meta.subscriptions) incomingSubscriptions(t, client, meta, id);
bs.close(stamp);

@@ -655,10 +621,4 @@ return t

const incomingSubscriptions = (hub, client, meta, id) => {
const update = (t, type, subs, tree) => {
send$1(hub, client, t, type, subs, tree);
};
if (!client.upstreamSubscriptions) {
client.upstreamSubscriptions = {};
}
const update = (t, type, subs, tree) => send$1(hub, client, t, type, subs, tree);
if (!client.upstreamSubscriptions) client.upstreamSubscriptions = {};
for (let key in meta.subscriptions) {

@@ -670,4 +630,3 @@ let uid = key + '-' + id;

subscribe(hub, subs, update);
// can also just add the function to the map
hub.subscriptions[hub.subscriptions.length - 1].id = id; // it aint pretty but works...
hub.subscriptions[hub.subscriptions.length - 1].id = id;
}

@@ -680,6 +639,6 @@ }

const removeSubscriptions = (t, id) => {
let i = t.subscriptions ? t.subscriptions.length : 0;
while (i--) { // clean this up with system in struct
if (t.subscriptions[i].id === id) {
t.subscriptions.splice(i, 1);
if (t.subscriptions) {
let i = t.subscriptions.length;
while (i--) { // clean this up with unsubscribe in struct
if (t.subscriptions[i].id === id) t.subscriptions.splice(i, 1);
}

@@ -702,6 +661,8 @@ }

const id = client.key;
// console.log(`CLIENT DC ${hub.id} - REMOVE THE CLIENT CONTEXT (missing)`)
const t = client.parent(2);
removeSubscriptions(t, id);
client.set(null, stamp);
if (client.context && t.clients.keys().length === (t.url ? 1 : 0)) {
t.set(null, stamp);
}
bs.close();

@@ -712,3 +673,2 @@ }

socket.on('close', close);
// socket.on('error', () => close()) // need to do something here as well no leaks!

@@ -753,4 +713,3 @@ });

hub.on((val, stamp, t) => {
if (val === null && !t.context && t === hub) {
// console.log(`☠️ REMOVE ${hub.id} SERVER ☠️`)
if (val === null && !t._c && t === hub) {
removeServer(hub);

@@ -763,3 +722,2 @@ removePort(hub);

if (hub.server) {
// console.log('🚀 SWITCH SERVER 🚀')
removeServer(hub);

@@ -790,12 +748,9 @@ }

remove$: (val, stamp, struct) => {
if (val === null && (!struct.context || struct.contextLevel === 1)) {
// console.log('remove handler -- need to 100x double check')
// if a server is there... --- this is wrong
if (val === null && (!struct._c || struct._cLevel === 1)) {
let p = struct;
let hub;
while (p) {
if (p.port && !p.context) { hub = p; }
if (p.port && !p._c) { hub = p; }
p = p.parent();
}
if (hub) {

@@ -833,34 +788,33 @@ const target = struct.parent();

// import { struct } from 'brisky-struct'
const props$2 = { contextKey: true };
const clientHub = {
type: 'hub',
props: {
cache: true,
upstreamSubscriptions: true,
// isUpstream: true,
// myupstream subs
resolve: true,
socket: true // only nessecary for downstream clients
const define$1 = {
getContext (val) {
var result = find(this, val);
if (!result) {
result = this.create({ contextKey: val }, false);
}
return result
}
};
const props$2 = {
clients: { // need to exclude on context
props: {
default: clientHub
const find = (hub, val) => {
const instances = hub.instances;
if (instances) {
let i = instances.length;
while (i--) {
if (instances[i].contextKey === val) {
// console.log('found context --->', val, instances[i].clients.keys())
return instances[i]
}
}
},
client: true // test if you can subscribe to it
}
};
var clients = Object.freeze({
props: props$2
var context$1 = Object.freeze({
props: props$2,
define: define$1
});
// need to be able to set timestamp mode on bs -- allways fo ts on bs
const hub = create({

@@ -872,12 +826,34 @@ type: 'hub',

default: 'self',
id: (t, val) => { t.set({ define: { id: val } }); }
id: (t, val) => { t.set({ define: { id: val } }); },
clients: (t, val, key, stamp) => {
if (!t.clients) {
t.clients = c(clients, val, stamp, t, key);
} else {
set(t.clients, val, stamp);
}
},
client: true
},
id: id,
id,
inject: [
client,
server,
clients
context$1,
client
]
});
const clients = create({
props: {
default: hub.create({
props: {
cache: true,
upstreamSubscriptions: true,
resolve: true,
socket: true,
context: true
}
})
}
});
// import bs from 'brisky-stamp'

@@ -884,0 +860,0 @@

@@ -83,7 +83,6 @@ 'use strict';

while (p) {
if (p.url && !p.context) { hub = p; }
if (p.url && !p._c) { hub = p; }
p = p.parent(); // needs to walk over context (for multi server)
}
if (hub && !hub.receiveOnly && struct.key !== 'clients') {
// console.log('✍️ go send --->', hub.id, struct.path(), '✍️', val)
if (struct === hub) {

@@ -154,6 +153,6 @@ if (val === null) {

if (/^.+=>/.test(val)) {
if (!/^\(.+\) +=>/.test(val)) {
if (!/^(\(){0, 1}.+(\)){0, 1} +=>/.test(val)) {
val = val.replace(/^(.*?)( +=>)/, '($1)$2');
}
if (!/^.+=> +{(.*?)}/.test(val)) {
if (!/^(.*?)+=> +{(.*?)}$/.test(val) && val.indexOf('return ') === -1) {
val = val.replace(/^(.+=> *?)(.*?)/, '$1 { return $2') + ' }';

@@ -166,2 +165,3 @@ }

result['$fn|' + i] = val;
// also here we need to rewrite client to use client id
} else if (typeof obj[i] !== 'object') {

@@ -237,7 +237,4 @@ // sync later!

const stamp = bs.create('upstream');
// console.log(`\n🌚 receive on ${hub.id} from ${hub.url} -->`, stamp, ((encodeURI(data).split(/%..|./).length - 1) / 1e3).toFixed(2) + 'kb 🌚')
// console.log(' ', JSON.stringify(JSON.parse(data), false, 2))
hub.set(JSON.parse(data), stamp);
bs.close(stamp);
// console.log('🌚 done 🌚\n')
};

@@ -265,6 +262,4 @@ };

hub.on((val, stamp, t) => {
if (val === null && !t.context && t === hub) {
console.log('remove subs');
if (val === null && !t._c && t === hub) {
hub.subscriptions = [];
// console.log(`☠️ REMOVE CLIENT ${hub.id} ☠️`)
removeUrl(hub);

@@ -318,7 +313,3 @@ removeSocket(hub);

hub.clientContext = val;
if (hub.connected && hub.connected.compute() === true) {
// console.log('ai need to re-send some stuff')
// console.log('this will tigh in to sendMeta')
meta(hub);
}
if (hub.connected && hub.connected.compute() === true) meta(hub);
}

@@ -346,3 +337,2 @@ };

const key = hash(JSON.stringify(parsed));
// why not just add them to client... so much easier
if (!this.upstreamSubscriptions) {

@@ -363,3 +353,2 @@ this.upstreamSubscriptions = { [key]: parsed };

// export { props, define }

@@ -413,3 +402,3 @@

i = i.slice(4);
// need to fix buble stuff in these fn creations -- prop need to add buble
// need to fix bublé stuff in these fn creations -- prop need to add buble
// runtime in a hub

@@ -457,3 +446,3 @@ let pass;

}
if (!isEmpty) { client.socket.send(JSON.stringify(client.inProgress)); }
if (!isEmpty) client.socket.send(JSON.stringify(client.inProgress));
client.inProgress = false;

@@ -477,7 +466,8 @@ }

if (tree.$t && tree.$t._p && !tree.$t._p[tree.$t.key]) {
serialize$2(hub.id, client, progress(client), subs, tree.$t, null, hub.serverIndex, tree);
serialize$2(hub.id, client, progress(client), subs, tree.$t, null, briskyStruct.get(hub, 'serverIndex'), tree);
}
}
if (t.val !== void 0 || val === null || subs.val === true) {
serialize$2(hub.id, client, progress(client), subs, t, val, hub.serverIndex, tree);
// opt this line
serialize$2(hub.id, client, progress(client), subs, t, val, briskyStruct.get(hub, 'serverIndex'), tree);
}

@@ -487,17 +477,9 @@ };

const cache = (client, struct, stamp, level, val) => {
if (!client.cache) { client.cache = {}; }
// console.log('CACHE IT---->', struct.path())
if (!client.cache) client.cache = {};
client.cache[struct.uid()] = stamp[0];
};
// const isCached = () => {}
const isCached = (client, struct, stamp) => client.cache &&
client.cache[struct.uid()] === stamp[0];
const isCached = (client, struct, stamp) => {
// // console.log('CACHE:', struct.path(), client.cache, c, stamp)
// if (client.cache && client.cache[struct.uid()] === stamp[0]) {
// console.log('BLOCKED BY CACHE')
// }
return client.cache && client.cache[struct.uid()] === stamp[0]
};
const setStamp = (s, stamp, src, struct, id, client, level) => {

@@ -511,4 +493,4 @@ cache(client, struct, stamp, level);

const serialize$2 = (id, client, t, subs, struct, val, level) => {
const stamp = struct.stamp;
if (stamp && (val === null || !isCached(client, struct, stamp))) {
const stamp = briskyStruct.get(struct, 'stamp');
if ((stamp && (val === null || !isCached(client, struct, stamp))) || subs.val === true) {
const src = bs.src(stamp);

@@ -525,5 +507,3 @@ if (src !== client.key && bs.src(t.tStamp) !== client.key) {

const keys = briskyStruct.getKeys(struct);
if (keys) {
deepSerialize(keys, id, client, t, subs, struct, val, level);
}
if (keys) deepSerialize(keys, id, client, t, subs, struct, val, level);
}

@@ -536,5 +516,3 @@ }

var s = t;
// var prev = t
for (let i = level; i < len; i++) {
// prev = s
let t = s[path[i]];

@@ -565,5 +543,3 @@ if (!t) {

const keys = briskyStruct.getKeys(struct);
if (keys) {
deepSerialize(keys, id, client, t, subs, struct, val, level);
}
if (keys) deepSerialize(keys, id, client, t, subs, struct, val, level);
}

@@ -579,3 +555,3 @@ }

let prop = briskyStruct.get(struct, keys[i]);
if (briskyStruct.get(prop, 'type') === 'hub') {
if (prop && briskyStruct.get(prop, 'type') === 'hub') {
serialize$2(id, client, t, subs, prop, val, level);

@@ -585,11 +561,11 @@ }

}
// feel really shacky /w context :/
// if (struct._removed) {
// for (let i = 0, len = struct._removed.length; i < len; i++) {
// let prop = struct._removed[i]
// if (get(prop, 'type') === 'hub') {
// serialize(id, client, t, subs, prop, null, level)
// }
// }
// }
// feels really shacky /w context :/ needs tests
if (struct._removed) {
for (let i = 0, len = struct._removed.length; i < len; i++) {
let prop = struct._removed[i];
if (prop && briskyStruct.get(prop, 'type') === 'hub') {
serialize$2(id, client, t, subs, prop, null, level);
}
}
}
};

@@ -605,8 +581,10 @@

t = hub;
// switch etc
if (meta.subscriptions) {
if (client.context != meta.context) { // eslint-disable-line
client.set(null);
t = create$2(hub, socket, meta);
client = socket.client;
} else if (meta.subscriptions) {
incomingSubscriptions(t, client, meta, client.id);
bs.close();
}
// console.log('perhaps replace something (e.g. subs or whatever')
} else {

@@ -621,5 +599,2 @@ t = create$2(hub, socket, meta);

if (payload) {
// console.log(`\n👾👾👾👾👾 in ${hub.id} 👾👾👾👾👾`)
// console.log(JSON.stringify(payload, false, 2))
// var d = Date.now()
if (meta && meta.resolve) {

@@ -633,3 +608,2 @@ client.resolve = meta.resolve;

bs.close();
// console.log(`👾👾👾👾👾 ${Date.now() - d} ms 👾👾👾👾👾`)
}

@@ -641,14 +615,6 @@ };

const id = meta.id;
// console.log(`\n🐠 create client "${id}" on "${hub.id}" 🐠`)
var t;
if (meta.context) {
// console.log('got context! go do it!')
} else {
t = hub; // will also become secured -- default will ofc be IP
}
t.set({ clients: { [id]: { socket } } }, stamp);
const t = meta.context ? hub.getContext(meta.context) : hub;
t.set({ clients: { [id]: { socket, context: meta.context } } }, stamp);
const client = socket.client = t.clients[id];
if (meta.subscriptions) {
incomingSubscriptions(t, client, meta, id);
}
if (meta.subscriptions) incomingSubscriptions(t, client, meta, id);
bs.close(stamp);

@@ -659,10 +625,4 @@ return t

const incomingSubscriptions = (hub, client, meta, id) => {
const update = (t, type, subs, tree) => {
send$1(hub, client, t, type, subs, tree);
};
if (!client.upstreamSubscriptions) {
client.upstreamSubscriptions = {};
}
const update = (t, type, subs, tree) => send$1(hub, client, t, type, subs, tree);
if (!client.upstreamSubscriptions) client.upstreamSubscriptions = {};
for (let key in meta.subscriptions) {

@@ -674,4 +634,3 @@ let uid = key + '-' + id;

briskyStruct.subscribe(hub, subs, update);
// can also just add the function to the map
hub.subscriptions[hub.subscriptions.length - 1].id = id; // it aint pretty but works...
hub.subscriptions[hub.subscriptions.length - 1].id = id;
}

@@ -684,6 +643,6 @@ }

const removeSubscriptions = (t, id) => {
let i = t.subscriptions ? t.subscriptions.length : 0;
while (i--) { // clean this up with system in struct
if (t.subscriptions[i].id === id) {
t.subscriptions.splice(i, 1);
if (t.subscriptions) {
let i = t.subscriptions.length;
while (i--) { // clean this up with unsubscribe in struct
if (t.subscriptions[i].id === id) t.subscriptions.splice(i, 1);
}

@@ -706,6 +665,8 @@ }

const id = client.key;
// console.log(`CLIENT DC ${hub.id} - REMOVE THE CLIENT CONTEXT (missing)`)
const t = client.parent(2);
removeSubscriptions(t, id);
client.set(null, stamp);
if (client.context && t.clients.keys().length === (t.url ? 1 : 0)) {
t.set(null, stamp);
}
bs.close();

@@ -716,3 +677,2 @@ }

socket.on('close', close);
// socket.on('error', () => close()) // need to do something here as well no leaks!

@@ -757,4 +717,3 @@ });

hub.on((val, stamp, t) => {
if (val === null && !t.context && t === hub) {
// console.log(`☠️ REMOVE ${hub.id} SERVER ☠️`)
if (val === null && !t._c && t === hub) {
removeServer(hub);

@@ -767,3 +726,2 @@ removePort(hub);

if (hub.server) {
// console.log('🚀 SWITCH SERVER 🚀')
removeServer(hub);

@@ -794,12 +752,9 @@ }

remove$: (val, stamp, struct) => {
if (val === null && (!struct.context || struct.contextLevel === 1)) {
// console.log('remove handler -- need to 100x double check')
// if a server is there... --- this is wrong
if (val === null && (!struct._c || struct._cLevel === 1)) {
let p = struct;
let hub;
while (p) {
if (p.port && !p.context) { hub = p; }
if (p.port && !p._c) { hub = p; }
p = p.parent();
}
if (hub) {

@@ -837,34 +792,33 @@ const target = struct.parent();

// import { struct } from 'brisky-struct'
const props$2 = { contextKey: true };
const clientHub = {
type: 'hub',
props: {
cache: true,
upstreamSubscriptions: true,
// isUpstream: true,
// myupstream subs
resolve: true,
socket: true // only nessecary for downstream clients
const define$1 = {
getContext (val) {
var result = find(this, val);
if (!result) {
result = this.create({ contextKey: val }, false);
}
return result
}
};
const props$2 = {
clients: { // need to exclude on context
props: {
default: clientHub
const find = (hub, val) => {
const instances = hub.instances;
if (instances) {
let i = instances.length;
while (i--) {
if (instances[i].contextKey === val) {
// console.log('found context --->', val, instances[i].clients.keys())
return instances[i]
}
}
},
client: true // test if you can subscribe to it
}
};
var clients = Object.freeze({
props: props$2
var context$1 = Object.freeze({
props: props$2,
define: define$1
});
// need to be able to set timestamp mode on bs -- allways fo ts on bs
const hub = briskyStruct.create({

@@ -876,12 +830,34 @@ type: 'hub',

default: 'self',
id: (t, val) => { t.set({ define: { id: val } }); }
id: (t, val) => { t.set({ define: { id: val } }); },
clients: (t, val, key, stamp) => {
if (!t.clients) {
t.clients = briskyStruct.c(clients, val, stamp, t, key);
} else {
briskyStruct.set(t.clients, val, stamp);
}
},
client: true
},
id: id,
id,
inject: [
client,
server,
clients
context$1,
client
]
});
const clients = briskyStruct.create({
props: {
default: hub.create({
props: {
cache: true,
upstreamSubscriptions: true,
resolve: true,
socket: true,
context: true
}
})
}
});
// import bs from 'brisky-stamp'

@@ -888,0 +864,0 @@

{
"name": "hub.js",
"description": "Seamless realtime communcation",
"version": "0.0.4",
"version": "0.0.6",
"main": "dist/index.js",

@@ -44,3 +44,3 @@ "module": "dist/index.es.js",

"brisky-stamp": "^3.0.11",
"brisky-struct": "^1.0.6",
"brisky-struct": "^1.0.10",
"string-hash": "^1.1.0",

@@ -47,0 +47,0 @@ "uws": "0.12.0",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc