From 583a10628ac32662d633b1817307e7ede49e4f3f Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Wed, 9 Jan 2019 16:55:52 +1100 Subject: [PATCH] run service_node in the browser instead of as a nodejs module --- Gruntfile.js | 5 +- libloki/service_nodes.js | 56 ++++++++++--------- libloki/test/index.html | 2 + libloki/test/node/.eslintrc.js | 22 -------- libloki/test/{node => }/service_nodes_test.js | 33 ++++++----- package.json | 2 +- 6 files changed, 54 insertions(+), 66 deletions(-) delete mode 100644 libloki/test/node/.eslintrc.js rename libloki/test/{node => }/service_nodes_test.js (57%) diff --git a/Gruntfile.js b/Gruntfile.js index 6db32d085..9ca02afd2 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -100,7 +100,10 @@ module.exports = grunt => { dest: 'js/libtextsecure.js', }, libloki: { - src: ['libloki/libloki-protocol.js'], + src: [ + 'libloki/libloki-protocol.js', + 'libloki/service_nodes.js', + ], dest: 'js/libloki.js', }, lokitest: { diff --git a/libloki/service_nodes.js b/libloki/service_nodes.js index a9dd72ebe..21e676493 100644 --- a/libloki/service_nodes.js +++ b/libloki/service_nodes.js @@ -1,29 +1,35 @@ -function consolidateLists(lists, threshold = 1){ - if (typeof threshold !== 'number') { - throw Error('Provided threshold is not a number'); - } +/* global window */ + +// eslint-disable-next-line func-names +(function () { + window.libloki = window.libloki || {}; + window.libloki.serviceNodes = window.libloki.serviceNodes || {}; - // calculate list size manually since `Set` - // does not have a `length` attribute - let numLists = 0; - const occurences = {}; - lists.forEach(list => { - numLists += 1; - list.forEach(item => { - if (!(item in occurences)) { - occurences[item] = 1; - } else { - occurences[item] += 1; - } + function consolidateLists(lists, threshold = 1){ + if (typeof threshold !== 'number') { + throw Error('Provided threshold is not a number'); + } + + // calculate list size manually since `Set` + // does not have a `length` attribute + let numLists = 0; + const occurences = {}; + lists.forEach(list => { + numLists += 1; + list.forEach(item => { + if (!(item in occurences)) { + occurences[item] = 1; + } else { + occurences[item] += 1; + } + }); }); - }); - const scaledThreshold = numLists * threshold; - return Object.entries(occurences) - .filter(keyValue => keyValue[1] >= scaledThreshold) - .map(keyValue => keyValue[0]); -} + const scaledThreshold = numLists * threshold; + return Object.entries(occurences) + .filter(keyValue => keyValue[1] >= scaledThreshold) + .map(keyValue => keyValue[0]); + } -module.exports = { - consolidateLists, -} + window.libloki.serviceNodes.consolidateLists = consolidateLists; +})(); diff --git a/libloki/test/index.html b/libloki/test/index.html index 83a5b8319..9bd8fde01 100644 --- a/libloki/test/index.html +++ b/libloki/test/index.html @@ -25,9 +25,11 @@ + + diff --git a/libloki/test/node/.eslintrc.js b/libloki/test/node/.eslintrc.js deleted file mode 100644 index 904f317ff..000000000 --- a/libloki/test/node/.eslintrc.js +++ /dev/null @@ -1,22 +0,0 @@ -// For reference: https://github.com/airbnb/javascript - -module.exports = { - env: { - mocha: true, - browser: false, - }, - - parserOptions: { - sourceType: 'module', - }, - - rules: { - // We still get the value of this rule, it just allows for dev deps - 'import/no-extraneous-dependencies': [ - 'error', - { - devDependencies: true, - }, - ], - }, -}; diff --git a/libloki/test/node/service_nodes_test.js b/libloki/test/service_nodes_test.js similarity index 57% rename from libloki/test/node/service_nodes_test.js rename to libloki/test/service_nodes_test.js index 9433ee934..e502e8532 100644 --- a/libloki/test/node/service_nodes_test.js +++ b/libloki/test/service_nodes_test.js @@ -1,58 +1,57 @@ -const ServiceNode = require('../../service_nodes'); -const { expect } = require('chai'); +/* global libloki, chai */ describe('ServiceNodes', () => { describe('#consolidateLists', () => { it('should throw when provided a non-iterable list', () => { - expect(() => ServiceNode.consolidateLists(null, 1)).to.throw(); + chai.expect(() => libloki.serviceNodes.consolidateLists(null, 1)).to.throw(); }); it('should throw when provided a non-iterable item in the list', () => { - expect(() => ServiceNode.consolidateLists([1, 2, 3], 1)).to.throw(); + chai.expect(() => libloki.serviceNodes.consolidateLists([1, 2, 3], 1)).to.throw(); }); it('should throw when provided a non-number threshold', () => { - expect(() => ServiceNode.consolidateLists([], 'a')).to.throw(); + chai.expect(() => libloki.serviceNodes.consolidateLists([], 'a')).to.throw(); }); it('should return an empty array when the input is an empty array', () => { - const result = ServiceNode.consolidateLists([]); - expect(result).to.deep.equal([]); + const result = libloki.serviceNodes.consolidateLists([]); + chai.expect(result).to.deep.equal([]); }); it('should return the input when only 1 list is provided', () => { - const result = ServiceNode.consolidateLists([['a', 'b', 'c']]); - expect(result).to.deep.equal(['a', 'b', 'c']); + const result = libloki.serviceNodes.consolidateLists([['a', 'b', 'c']]); + chai.expect(result).to.deep.equal(['a', 'b', 'c']); }); it('should return the union of all lists when threshold is 0', () => { - const result = ServiceNode.consolidateLists([ + const result = libloki.serviceNodes.consolidateLists([ ['a', 'b', 'c', 'h'], ['d', 'e', 'f', 'g'], ['g', 'h'], ], 0); - expect(result.sort()).to.deep.equal(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']); + chai.expect(result.sort()).to.deep.equal(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']); }); it('should return the intersection of all lists when threshold is 1', () => { - const result = ServiceNode.consolidateLists([ + const result = libloki.serviceNodes.consolidateLists([ ['a', 'b', 'c', 'd'], ['a', 'e', 'f', 'g'], ['a', 'h'], ], 1); - expect(result).to.deep.equal(['a']); + chai.expect(result).to.deep.equal(['a']); }); it('should return the elements that have an occurence >= the provided threshold', () => { - const result = ServiceNode.consolidateLists([ + const result = libloki.serviceNodes.consolidateLists([ ['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['a', 'b', 'c', 'd', 'e', 'f', 'h'], ['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['a', 'b', 'c', 'd', 'e', 'g', 'h'], ], 3/4); - expect(result).to.deep.equal(['a', 'b', 'c', 'd', 'e', 'f', 'g']); + chai.expect(result).to.deep.equal(['a', 'b', 'c', 'd', 'e', 'f', 'g']); }); it('should work with sets as well', () => { - const result = ServiceNode.consolidateLists(new Set([ + const result = libloki.serviceNodes.consolidateLists(new Set([ new Set(['a', 'b', 'c', 'd', 'e', 'f', 'g']), new Set(['a', 'b', 'c', 'd', 'e', 'f', 'h']), new Set(['a', 'b', 'c', 'd', 'e', 'f', 'g']), new Set(['a', 'b', 'c', 'd', 'e', 'g', 'h']), ]), 3/4); - expect(result).to.deep.equal(['a', 'b', 'c', 'd', 'e', 'f', 'g']); + chai.expect(result).to.deep.equal(['a', 'b', 'c', 'd', 'e', 'f', 'g']); }); }); }); diff --git a/package.json b/package.json index b1b3e914b..21dfe6bd7 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "test-lib-view": "NODE_ENV=test-lib yarn run start", "test-loki-view": "NODE_ENV=test-loki yarn run start", "test-electron": "yarn grunt test", - "test-node": "mocha --recursive test/app test/modules ts/test libloki/test/node", + "test-node": "mocha --recursive test/app test/modules ts/test", "test-node-coverage": "nyc --reporter=lcov --reporter=text mocha --recursive test/app test/modules ts/test", "eslint": "eslint .", "lint": "yarn format --list-different && yarn lint-windows",