New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

dslink

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dslink - npm Package Compare versions

Comparing version

to
2.1.10-beta.0

export { HttpClientLink } from './src/nodejs/client-link';
export { DSLink } from './src/nodejs/cli-link-wrapper';
export { Table, TableColumn } from './src/common/table';
export { LocalNode, NodeProvider } from './src/responder/node_state';
export { BaseLocalNode } from './src/responder/base-local-node';

@@ -5,0 +6,0 @@ export { RootNode } from './src/responder/node/root-node';

@@ -10,2 +10,5 @@ "use strict";

exports.TableColumn = table_1.TableColumn;
var node_state_1 = require("./src/responder/node_state");
exports.LocalNode = node_state_1.LocalNode;
exports.NodeProvider = node_state_1.NodeProvider;
var base_local_node_1 = require("./src/responder/base-local-node");

@@ -12,0 +15,0 @@ exports.BaseLocalNode = base_local_node_1.BaseLocalNode;

@@ -16,5 +16,4 @@ import { BrowserUserLink } from './src/browser/browser-user-link';

* - a child has updated internally (same as the above condition), and the child is defined in watchChildren
* @param watchChildren defines the children nodes that will trigger the callback on its internal change
*/
export declare function useDsaQuery(link: BrowserUserLink, path: string, query: NodeQueryStructure, callback?: Listener<NodeQueryResult>, watchChildren?: '*' | string[]): void;
export declare function useDsaQuery(link: BrowserUserLink, path: string, query: NodeQueryStructure, callback?: Listener<NodeQueryResult>, delay?: number): NodeQueryResult;
/**

@@ -29,4 +28,3 @@ * Query a child node and its children

* - a child has updated internally (same as the above condition), and the child is defined in watchChildren
* @param watchChildren defines the children nodes that will trigger the callback on its internal change
*/
export declare function useDsaChildQuery(node: NodeQueryResult, callback?: Listener<NodeQueryResult>, watchChildren?: '*' | string[]): void;
export declare function useDsaChildQuery(node: NodeQueryResult, callback?: Listener<NodeQueryResult>): NodeQueryResult;

@@ -6,29 +6,52 @@ "use strict";

/** @ignore */
function useRawDsaQuery(link, pathOrNode, query, callback, watchChildren) {
if (typeof watchChildren === 'string') {
watchChildren = [watchChildren];
}
function useRawDsaQuery(link, pathOrNode, query, callback, delay = 0) {
const callbackRef = react_1.useRef();
callbackRef.current = callback;
const delayRef = react_1.useRef();
delayRef.current = Math.max(delay, 0); // delay must >= 0
const callbackTimerRef = react_1.useRef(false);
const rootNodeCache = react_1.useRef();
const [, forceUpdate] = react_1.useState(1);
callbackRef.current = callback;
let childCallback = react_1.useCallback((node) => {
if (rootNodeCache.current) {
rootCallback(rootNodeCache.current);
const watchingNodes = react_1.useRef(new WeakSet());
const executeCallback = react_1.useCallback(() => {
if (callbackRef.current) {
callbackRef.current(rootNodeCache.current);
}
else {
// force a state change to render
forceUpdate((v) => -v);
}
callbackTimerRef.current = null;
}, []);
const delayedCallback = react_1.useCallback(() => {
if (callbackTimerRef.current) {
return;
}
if (callbackTimerRef.current === false && rootNodeCache.current) {
executeCallback();
}
else {
callbackTimerRef.current = setTimeout(executeCallback, delayRef.current);
}
}, []);
const childCallback = react_1.useCallback((node) => {
for (let [name, child] of node.children) {
if (!watchingNodes.current.has(child)) {
watchingNodes.current.add(child);
child.listen(childCallback, false);
childCallback(child);
}
}
delayedCallback();
}, []);
const rootCallback = react_1.useCallback((node) => {
rootNodeCache.current = node;
if (watchChildren) {
for (let [name, child] of node.children) {
if (watchChildren[0] === '*' || watchChildren.includes(name)) {
child.listen(childCallback, false);
}
for (let [name, child] of node.children) {
if (!watchingNodes.current.has(child)) {
watchingNodes.current.add(child);
child.listen(childCallback, false);
childCallback(child);
}
}
if (callbackRef.current) {
callbackRef.current(node);
}
// force render on node change
forceUpdate((v) => -v);
delayedCallback();
}, []);

@@ -49,2 +72,3 @@ react_1.useEffect(() => {

}, [link, pathOrNode]);
return rootNodeCache.current;
}

@@ -62,6 +86,5 @@ /**

* - a child has updated internally (same as the above condition), and the child is defined in watchChildren
* @param watchChildren defines the children nodes that will trigger the callback on its internal change
*/
function useDsaQuery(link, path, query, callback, watchChildren) {
return useRawDsaQuery(link, path, query, callback, watchChildren);
function useDsaQuery(link, path, query, callback, delay) {
return useRawDsaQuery(link, path, query, callback, delay);
}

@@ -78,8 +101,7 @@ exports.useDsaQuery = useDsaQuery;

* - a child has updated internally (same as the above condition), and the child is defined in watchChildren
* @param watchChildren defines the children nodes that will trigger the callback on its internal change
*/
function useDsaChildQuery(node, callback, watchChildren) {
return useRawDsaQuery(null, node, null, callback, watchChildren);
function useDsaChildQuery(node, callback) {
return useRawDsaQuery(null, node, null, callback);
}
exports.useDsaChildQuery = useDsaChildQuery;
//# sourceMappingURL=react-hook.js.map

@@ -6,2 +6,3 @@ "use strict";

const async_1 = require("../utils/async");
const codec_1 = require("../utils/codec");
const logger_1 = require("../utils/logger");

@@ -60,3 +61,3 @@ let logger = logger_1.logger.tag('ws');

m = this.codec.decodeBinaryFrame(bytes);
// logger.fine("$m");
logger.trace(() => 'receive' + codec_1.DsJson.encode(m, true));
if (typeof m['salt'] === 'string') {

@@ -95,3 +96,3 @@ this.clientLink.updateSalt(m['salt']);

m = this.codec.decodeStringFrame(e.data);
// logger.fine("$m");
logger.trace(() => 'receive' + codec_1.DsJson.encode(m, true));
let needAck = false;

@@ -263,3 +264,3 @@ if (Array.isArray(m['responses']) && m['responses'].length > 0) {

}
// logger.fine("send: $m");
logger.trace(() => 'send' + codec_1.DsJson.encode(m, true));
let encoded = this.codec.encodeFrame(m);

@@ -266,0 +267,0 @@ try {

@@ -84,3 +84,10 @@ "use strict";

let processors = this._processors;
this._processors = [];
if (processors.length > 32) {
processors = this._processors.slice(0, 32);
this._processors = this._processors.slice(32);
this._conn.sendWhenReady(this);
}
else {
this._processors = [];
}
for (let proc of processors) {

@@ -87,0 +94,0 @@ proc.startSendingData(currentTime, waitingAckId);

@@ -99,3 +99,3 @@ import Denque from 'denque';

getRequester(dsId: string): Requester;
getResponder(dsId: string, nodeProvider: NodeProvider, sessionId?: string, trusted?: boolean): Responder;
getResponder(dsId: string, nodeProvider: NodeStore, sessionId?: string, trusted?: boolean): Responder;
updateLinkData(dsId: string, m: any): void;

@@ -133,5 +133,4 @@ }

}
export interface NodeProvider {
export interface NodeStore {
getNode(path: string): LocalNode;
getOrCreateNode(path: string, addToTree?: boolean): LocalNode;
}

@@ -159,3 +159,20 @@ "use strict";

let processingPaths = this._changedPaths;
this._changedPaths = new Set();
if (processingPaths.size > 32) {
processingPaths = new Set();
let pendingPaths = new Set();
let count = 0;
for (let path of this._changedPaths) {
if (++count > 32) {
pendingPaths.add(path);
}
else {
processingPaths.add(path);
}
}
this._changedPaths = pendingPaths;
this.prepareSending();
}
else {
this._changedPaths = new Set();
}
for (let path of processingPaths) {

@@ -177,2 +194,6 @@ if (this.subscriptions.has(path)) {

for (let [sid, sub] of this.toRemove) {
if (removeSids.length >= 32) {
this.prepareSending();
break;
}
if (sub.callbacks.size === 0) {

@@ -184,5 +205,5 @@ removeSids.push(sid);

}
this.toRemove.delete(sid);
}
this.requester._sendRequest({ method: 'unsubscribe', sids: removeSids }, null);
this.toRemove.clear();
}

@@ -189,0 +210,0 @@ }

@@ -7,2 +7,3 @@ import { Listener, Stream } from '../utils/async';

import { ValueUpdate, ValueUpdateCallback } from '../common/value';
import { NodeStore } from '../common/interfaces';
export declare class LocalNode extends Node<LocalNode> {

@@ -37,2 +38,4 @@ provider: NodeProvider;

onValueChange(newVal: any): boolean;
useVirtualList: boolean;
virtualList(updates: any[]): void;
save(): {

@@ -50,5 +53,6 @@ [key: string]: any;

}
export declare class NodeProvider {
export declare class NodeProvider implements NodeStore {
/** @ignore */
_states: Map<string, NodeState>;
getVirtualNode(path: string): LocalNode;
getNode(path: string): LocalNode;

@@ -72,2 +76,5 @@ createState(path: string): NodeState;

finishSaveTimer(): void;
addProfile(path: string, data: {
[key: string]: any;
}): void;
}

@@ -74,0 +81,0 @@ export interface Subscriber {

@@ -8,2 +8,3 @@ "use strict";

const interfaces_1 = require("../common/interfaces");
const static_node_1 = require("./node/static-node");
class LocalNode extends node_1.Node {

@@ -154,2 +155,3 @@ constructor(path, provider) {

}
virtualList(updates) { }
save() {

@@ -196,2 +198,5 @@ return null;

}
getVirtualNode(path) {
return null;
}
getNode(path) {

@@ -209,2 +214,8 @@ if (this._states.has(path)) {

this._states.set(path, state);
if (!state._node) {
let virtualNode = this.getVirtualNode(path);
if (virtualNode) {
state.setNode(virtualNode);
}
}
return state;

@@ -248,2 +259,9 @@ }

}
addProfile(path, data) {
let nodePath = `/defs/profile/${path}`;
let state = this.createState(nodePath);
let profileNode = new static_node_1.StaticNode(nodePath, this);
state.setNode(profileNode);
profileNode.load(data);
}
}

@@ -250,0 +268,0 @@ exports.NodeProvider = NodeProvider;

import { BaseLocalNode } from '../base-local-node';
import { NodeProvider } from '../node_state';
export declare class RootNode extends BaseLocalNode {
constructor(data?: {
[key: string]: any;
});
}, provider?: NodeProvider);
}

@@ -6,4 +6,7 @@ "use strict";

class RootNode extends base_local_node_1.BaseLocalNode {
constructor(data) {
super('/', new node_state_1.NodeProvider());
constructor(data, provider) {
if (!provider) {
provider = new node_state_1.NodeProvider();
}
super('/', provider);
this.provider.setRoot(this);

@@ -10,0 +13,0 @@ if (data) {

@@ -212,4 +212,7 @@ "use strict";

let parentNode = this.nodeProvider.getNode(path.parentPath);
let node = this.nodeProvider.getNode(path.path);
let node = parentNode.getChild(path.name);
if (node == null) {
node = this.nodeProvider.getNode(path.path);
}
if (node == null) {
this.closeResponse(m['rid'], null, interfaces_1.DsError.NOT_IMPLEMENTED);

@@ -216,0 +219,0 @@ return;

@@ -41,2 +41,5 @@ "use strict";

}
else if (node.useVirtualList) {
node.virtualList(updates);
}
else {

@@ -118,3 +121,5 @@ let updateIs;

this.changes.clear();
this.responder.updateResponse(this, updates, { streamStatus: 'open' });
if (updates.length) {
this.responder.updateResponse(this, updates, { streamStatus: 'open' });
}
}

@@ -121,0 +126,0 @@ ackReceived(receiveAckId, startTime, currentTime) {

@@ -67,7 +67,12 @@ "use strict";

let updates = [];
let count = 0;
for (let subscriber of this.changed) {
if (++count > 32) {
this.prepareSending();
break;
}
updates = updates.concat(subscriber.process(waitingAckId));
this.changed.delete(subscriber);
}
this.responder.updateResponse(this, updates);
this.changed.clear();
}

@@ -74,0 +79,0 @@ ackReceived(receiveAckId, startTime, currentTime) {

{
"name": "dslink",
"license": "Apache-2.0",
"version": "2.1.5",
"version": "2.1.10-beta.0",
"main": "js/node.js",

@@ -19,2 +19,3 @@ "repository": {

"@types/mocha": "^5.2.7",
"@types/moment-timezone": "^0.5.12",
"@types/msgpack-lite": "^0.1.7",

@@ -44,2 +45,3 @@ "@types/node": "^12.12.11",

"denque": "^1.4.1",
"moment-timezone": "^0.5.27",
"msgpack-lite": "^0.1.26",

@@ -49,6 +51,2 @@ "ws": "^7.2.0",

},
"peerDependencies": {
"react": "^16.10.2",
"react-dom": "^16.10.2"
},
"scripts": {

@@ -55,0 +53,0 @@ "js-example": "parcel example/browser/basic.html --out-dir temp --open --no-source-maps ",

@@ -5,2 +5,3 @@ export {HttpClientLink} from './src/nodejs/client-link';

export {Table, TableColumn} from './src/common/table';
export {LocalNode, NodeProvider} from './src/responder/node_state';
export {BaseLocalNode} from './src/responder/base-local-node';

@@ -7,0 +8,0 @@ export {RootNode} from './src/responder/node/root-node';

@@ -13,30 +13,55 @@ import {BrowserUserLink} from './src/browser/browser-user-link';

callback?: Listener<NodeQueryResult>,
watchChildren?: '*' | string[]
) {
if (typeof watchChildren === 'string') {
watchChildren = [watchChildren];
}
delay = 0
): NodeQueryResult {
const callbackRef = useRef<Listener<NodeQueryResult>>();
callbackRef.current = callback;
const delayRef = useRef<number>();
delayRef.current = Math.max(delay, 0); // delay must >= 0
const callbackTimerRef = useRef<any>(false);
const rootNodeCache = useRef<NodeQueryResult>();
const [, forceUpdate] = useState(1);
callbackRef.current = callback;
let childCallback = useCallback((node: NodeQueryResult) => {
if (rootNodeCache.current) {
rootCallback(rootNodeCache.current);
const watchingNodes = useRef(new WeakSet<NodeQueryResult>());
const executeCallback = useCallback(() => {
if (callbackRef.current) {
callbackRef.current(rootNodeCache.current);
} else {
// force a state change to render
forceUpdate((v) => -v);
}
callbackTimerRef.current = null;
}, []);
const delayedCallback = useCallback(() => {
if (callbackTimerRef.current) {
return;
}
if (callbackTimerRef.current === false && rootNodeCache.current) {
executeCallback();
} else {
callbackTimerRef.current = setTimeout(executeCallback, delayRef.current);
}
}, []);
const childCallback = useCallback((node: NodeQueryResult) => {
for (let [name, child] of node.children) {
if (!watchingNodes.current.has(child)) {
watchingNodes.current.add(child);
child.listen(childCallback, false);
childCallback(child);
}
}
delayedCallback();
}, []);
const rootCallback = useCallback((node: NodeQueryResult) => {
rootNodeCache.current = node;
if (watchChildren) {
for (let [name, child] of node.children) {
if (watchChildren[0] === '*' || watchChildren.includes(name)) {
child.listen(childCallback, false);
}
for (let [name, child] of node.children) {
if (!watchingNodes.current.has(child)) {
watchingNodes.current.add(child);
child.listen(childCallback, false);
childCallback(child);
}
}
if (callbackRef.current) {
callbackRef.current(node);
}
// force render on node change
forceUpdate((v) => -v);
delayedCallback();
}, []);

@@ -56,2 +81,4 @@ useEffect(() => {

}, [link, pathOrNode]);
return rootNodeCache.current;
}

@@ -70,3 +97,2 @@

* - a child has updated internally (same as the above condition), and the child is defined in watchChildren
* @param watchChildren defines the children nodes that will trigger the callback on its internal change
*/

@@ -78,5 +104,5 @@ export function useDsaQuery(

callback?: Listener<NodeQueryResult>,
watchChildren?: '*' | string[]
delay?: number
) {
return useRawDsaQuery(link, path, query, callback, watchChildren);
return useRawDsaQuery(link, path, query, callback, delay);
}

@@ -93,10 +119,5 @@

* - a child has updated internally (same as the above condition), and the child is defined in watchChildren
* @param watchChildren defines the children nodes that will trigger the callback on its internal change
*/
export function useDsaChildQuery(
node: NodeQueryResult,
callback?: Listener<NodeQueryResult>,
watchChildren?: '*' | string[]
) {
return useRawDsaQuery(null, node, null, callback, watchChildren);
export function useDsaChildQuery(node: NodeQueryResult, callback?: Listener<NodeQueryResult>) {
return useRawDsaQuery(null, node, null, callback);
}
/// a client link for both http and ws
import {ClientLink, DummyECDH, ECDH, NodeProvider} from '../common/interfaces';
import {ClientLink, DummyECDH, ECDH} from '../common/interfaces';
import {Completer} from '../utils/async';

@@ -4,0 +4,0 @@ import {Requester} from '../requester/requester';

@@ -11,3 +11,3 @@ import {

import {Completer} from '../utils/async';
import {DsCodec} from '../utils/codec';
import {DsCodec, DsJson} from '../utils/codec';
import {logger as mainLogger} from '../utils/logger';

@@ -151,3 +151,3 @@

m = this.codec.decodeBinaryFrame(bytes);
// logger.fine("$m");
logger.trace(() => 'receive' + DsJson.encode(m, true));

@@ -186,3 +186,3 @@ if (typeof m['salt'] === 'string') {

m = this.codec.decodeStringFrame(e.data);
// logger.fine("$m");
logger.trace(() => 'receive' + DsJson.encode(m, true));

@@ -277,3 +277,3 @@ let needAck = false;

// logger.fine("send: $m");
logger.trace(() => 'send' + DsJson.encode(m, true));
let encoded = this.codec.encodeFrame(m);

@@ -280,0 +280,0 @@

@@ -100,3 +100,9 @@ import {ConnectionChannel, ConnectionProcessor, ProcessorResult} from './interfaces';

let processors: ConnectionProcessor[] = this._processors;
this._processors = [];
if (processors.length > 32) {
processors = this._processors.slice(0, 32);
this._processors = this._processors.slice(32);
this._conn.sendWhenReady(this);
} else {
this._processors = [];
}
for (let proc of processors) {

@@ -103,0 +109,0 @@ proc.startSendingData(currentTime, waitingAckId);

@@ -235,3 +235,3 @@ import Denque from 'denque';

getResponder(dsId: string, nodeProvider: NodeProvider, sessionId?: string, trusted?: boolean): Responder;
getResponder(dsId: string, nodeProvider: NodeStore, sessionId?: string, trusted?: boolean): Responder;

@@ -333,11 +333,5 @@ updateLinkData(dsId: string, m: any): void;

/// A single node provider can be reused by multiple responder.
export interface NodeProvider {
export interface NodeStore {
/// Gets an existing node.
getNode(path: string): LocalNode;
/// Gets a node at the given [path] if it exists.
/// If it does not exist, create a new node and return it.
///
/// When [addToTree] is false, the node will not be inserted into the node provider.
getOrCreateNode(path: string, addToTree?: boolean): LocalNode;
}

@@ -175,3 +175,18 @@ import {Closable} from '../../utils/async';

let processingPaths: Set<string> = this._changedPaths;
this._changedPaths = new Set<string>();
if (processingPaths.size > 32) {
processingPaths = new Set<string>();
let pendingPaths = new Set<string>();
let count = 0;
for (let path of this._changedPaths) {
if (++count > 32) {
pendingPaths.add(path);
} else {
processingPaths.add(path);
}
}
this._changedPaths = pendingPaths;
this.prepareSending();
} else {
this._changedPaths = new Set<string>();
}
for (let path of processingPaths) {

@@ -193,2 +208,6 @@ if (this.subscriptions.has(path)) {

for (let [sid, sub] of this.toRemove) {
if (removeSids.length >= 32) {
this.prepareSending();
break;
}
if (sub.callbacks.size === 0) {

@@ -200,5 +219,5 @@ removeSids.push(sid);

}
this.toRemove.delete(sid);
}
this.requester._sendRequest({method: 'unsubscribe', sids: removeSids}, null);
this.toRemove.clear();
}

@@ -205,0 +224,0 @@ }

@@ -8,3 +8,4 @@ import {Listener, Stream} from '../utils/async';

import {ValueUpdate, ValueUpdateCallback} from '../common/value';
import {DsError} from '../common/interfaces';
import {DsError, NodeStore} from '../common/interfaces';
import {StaticNode} from './node/static-node';

@@ -187,2 +188,6 @@ export class LocalNode extends Node<LocalNode> {

useVirtualList: boolean;
virtualList(updates: any[]) {}
save(): {[key: string]: any} {

@@ -210,6 +215,10 @@ return null;

export class NodeProvider {
export class NodeProvider implements NodeStore {
/** @ignore */
_states: Map<string, NodeState> = new Map<string, NodeState>();
getVirtualNode(path: string): LocalNode {
return null;
}
getNode(path: string): LocalNode {

@@ -228,2 +237,8 @@ if (this._states.has(path)) {

this._states.set(path, state);
if (!state._node) {
let virtualNode = this.getVirtualNode(path);
if (virtualNode) {
state.setNode(virtualNode);
}
}
return state;

@@ -300,2 +315,10 @@ }

}
addProfile(path: string, data: {[key: string]: any}) {
let nodePath = `/defs/profile/${path}`;
let state = this.createState(nodePath);
let profileNode = new StaticNode(nodePath, this);
state.setNode(profileNode);
profileNode.load(data);
}
}

@@ -302,0 +325,0 @@

@@ -5,4 +5,7 @@ import {BaseLocalNode} from '../base-local-node';

export class RootNode extends BaseLocalNode {
constructor(data?: {[key: string]: any}) {
super('/', new NodeProvider());
constructor(data?: {[key: string]: any}, provider?: NodeProvider) {
if (!provider) {
provider = new NodeProvider();
}
super('/', provider);
this.provider.setRoot(this);

@@ -9,0 +12,0 @@ if (data) {

@@ -248,4 +248,7 @@ /// a responder for one connection

let node: LocalNode = this.nodeProvider.getNode(path.path);
let node: LocalNode = parentNode.getChild(path.name);
if (node == null) {
node = this.nodeProvider.getNode(path.path);
}
if (node == null) {
this.closeResponse(m['rid'], null, DsError.NOT_IMPLEMENTED);

@@ -252,0 +255,0 @@ return;

@@ -47,2 +47,4 @@ import {NodeState} from '../node_state';

updates.push(['$disconnectedTs', this.state._disconnectedTs]);
} else if (node.useVirtualList) {
node.virtualList(updates);
} else {

@@ -122,4 +124,5 @@ let updateIs: any;

this.changes.clear();
this.responder.updateResponse(this, updates, {streamStatus: 'open'});
if (updates.length) {
this.responder.updateResponse(this, updates, {streamStatus: 'open'});
}
}

@@ -126,0 +129,0 @@

@@ -79,7 +79,12 @@ // part of dslink.responder;

let updates: any[] = [];
let count = 0;
for (let subscriber of this.changed) {
if (++count > 32) {
this.prepareSending();
break;
}
updates = updates.concat(subscriber.process(waitingAckId));
this.changed.delete(subscriber);
}
this.responder.updateResponse(this, updates);
this.changed.clear();
}

@@ -86,0 +91,0 @@

@@ -18,2 +18,3 @@ import {MockBroker} from './utils/mock-broker';

import {InvokeResponse} from '../src/responder/response/invoke';
import {sleep} from '../src/utils/async';

@@ -206,2 +207,21 @@ class TestStreamAction extends ActionNode {

});
it('split requests', async function() {
let count = 0;
let checked = 0;
for (let i = 0; i < 1000; ++i) {
requester.invoke(resolve('act'), {value: i}, (update: RequesterInvokeUpdate) => {
++count;
});
}
while (count < 1000) {
await sleep(0);
// shouldn't receive a lot of updates at same time
assert.isTrue(count - checked < 100);
checked = count;
}
assert.equal(count, 1000);
});
});