Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public disconnect (): void {
if (isNull(this.websocket)) {
throw new Error('Cannot disconnect on a non-open websocket');
}
// switch off autoConnect, we are in manual mode now
this.autoConnect = false;
// 1000 - Normal closure; the connection successfully completed
this.websocket.close(1000);
this.websocket = null;
}
protected _snapshot (dest: TrieDb, fn: ProgressCb | undefined, root: Uint8Array, keys: number, percent: number, depth: number): number {
// l.debug(() => ['snapshot', { root }]);
const [encoded, node] = this._getNodeRaw(root);
if (isNull(encoded) || isNull(node)) {
return keys;
}
dest.db.put(root, encoded);
fn && fn({ keys: ++keys, percent });
node.forEach((u8a): void => {
if (u8a && u8a.length === 32) {
keys = this._snapshot(dest, fn, u8a, keys, percent, depth + 1);
}
percent += (100 / node.length) / Math.pow(16, depth);
});
return keys;
private getParams (props: any): [boolean, any[]] {
const paramValue = paramPick
? paramPick(props)
: paramName
? props[paramName]
: undefined;
if (atProp) {
at = props[atProp];
}
// When we are specifying a param and have an invalid, don't use it. For 'params',
// we default to the original types, i.e. no validation (query app uses this)
if (!paramValid && paramName && (isUndefined(paramValue) || isNull(paramValue))) {
return [false, []];
}
const values = isUndefined(paramValue)
? params
: params.concat(
(Array.isArray(paramValue) && !(paramValue as any).toU8a)
? paramValue
: [paramValue]
);
return [true, values];
}
const [area, section, method, ...others] = endpoint.split('.');
assert(area.length && section.length && method.length && others.length === 0, `Invalid API format, expected <area>.<section>., found ${endpoint}`);
assert(['rpc', 'query', 'derive'].includes(area), `Unknown api.${area}, expected rpc, query or derive`);
assert(!at || area === 'query', 'Only able todo an at query on the api.query interface');
const apiSection = (api as any)[area][section];
assert(apiSection && apiSection[method], `Unable to find api.${area}.${section}.${method}`);
const meta = apiSection[method].meta;
if (area === 'query' && meta && meta.type.isMap) {
const arg = newParams[0];
assert(!isUndefined(arg) && !isNull(arg), `${meta.name} expects one argument`);
}
return [
apiSection[method],
newParams,
area === 'derive' || (area === 'query' && (!at && !atProp)) || method.startsWith('subscribe')
];
}
</section>
function generateIcon (seed: string = encodeAddress(randomAsU8a(32))): void {
const start = Date.now();
if (isNull(element)) {
throw new Error('Unable to find #demo element');
}
element.appendChild(
identicon(seed, 100, 'padded')
);
console.log(`Icon generated in ${(Date.now() - start)}ms`);
}
export function keyStartsWith (key: Uint8Array | null, partial: Uint8Array | null): boolean {
if (isNull(key) && isNull(partial)) {
return true;
} else if (isNull(key) || isNull(partial) || (key.length < partial.length)) {
return false;
}
for (let index = 0; index < partial.length; index++) {
if (key[index] !== partial[index]) {
return false;
}
}
return true;
}
export function isEmptyNode (node: Node): node is NodeEmpty {
return isNull(node);
}
if (!keyStartsWith(trieKey, currentKey)) {
return node;
} else if (nodeType === NodeType.LEAF) {
return keyEquals(trieKey, currentKey)
? null
: node;
}
const subKey = trieKey.subarray(currentKey.length);
const subNode = this._getNode(node[1]);
const newSub = this._del(subNode, subKey);
const encodedNewSub = this._persistNode(newSub);
if (keyEquals(encodedNewSub, node[1])) {
return node;
} else if (isNull(newSub)) {
return null;
}
if (isKvNode(newSub)) {
const subNibbles = decodeNibbles(newSub[0]);
const newKey = u8aConcat(currentKey, subNibbles);
return [encodeNibbles(newKey), newSub[1]];
} else if (isBranchNode(newSub)) {
return [encodeNibbles(currentKey), encodedNewSub];
}
throw new Error('Unreachable');
}
export function keyEquals (key: Uint8Array | null, test: Uint8Array | null): boolean {
if (isNull(key) && isNull(test)) {
return true;
} else if (isNull(key) || isNull(test) || (key.length !== test.length)) {
return false;
}
return keyStartsWith(key, test);
}