From 4b01d047a3832646a0407468f7c03beecd5b0080 Mon Sep 17 00:00:00 2001 From: Florian Schopp Date: Thu, 2 Nov 2023 13:30:36 +0100 Subject: [PATCH 1/8] Fixed async fetching --- examples/todo/client/client.ts | 40 +++++++++++--- examples/todo/server/index.ts | 14 ++++- package.json | 2 +- src/2_jsonrpc/index.ts | 12 ++--- src/3_jet/daemon/index.ts | 20 ++----- src/3_jet/daemon/subscription.ts | 1 + src/3_jet/peer/index.ts | 91 ++++++++++++++++++++++---------- test/jsonrpc/index.test.ts | 12 ++--- 8 files changed, 125 insertions(+), 67 deletions(-) diff --git a/examples/todo/client/client.ts b/examples/todo/client/client.ts index 4e147e5..93e7d82 100644 --- a/examples/todo/client/client.ts +++ b/examples/todo/client/client.ts @@ -1,12 +1,19 @@ /* * Jet client-server communications: */ -import { Fetcher, Peer, PublishMessage, ValueType } from '../../../lib' +import { Fetcher, Peer, PublishMessage } from '../../../lib' import { Todo } from '../server/Todo' import './base.css' const todoList: Record = {} -const peer = new Peer({ url: `ws://localhost:8081/` }) +const peer = new Peer({ + url: `ws://localhost:8081/` + // log: { + // logCallbacks: [console.log], + // logName: 'Peer 2', + // logLevel: LogLevel.socket + // } +}) const addTodo = (title: string) => { peer.call('todo/add', [title]) @@ -36,6 +43,24 @@ const todos = new Fetcher() } renderTodos() }) + +const test2 = new Fetcher().path('startsWith', 'te') + +const test = new Fetcher() + .path('startsWith', 'test') + .addListener('data', (todo: PublishMessage) => { + switch (todo.event) { + case 'Add': + console.log('add', todo.value) + case 'Change': + console.log('change', todo.value) + // todoList[todo.path] = todo.value + break + case 'Remove': + delete todoList[todo.path] + break + } + }) /* * GUI Logic: */ @@ -127,9 +152,12 @@ document.getElementById('todo-form')!.addEventListener('submit', (event) => { peer .connect() .then(() => peer.authenticate('Admin', 'test')) - .then(() => peer.get({ path: { startsWith: 'test' } })) - .then((val) => console.log(val)) - // .then(() => peer.authenticate('Operator', '')) - .then(() => peer.fetch(todos)) + .then(() => + peer.batch(() => { + peer.fetch(test) + peer.fetch(test2) + peer.fetch(todos) + }) + ) .then(() => renderTodos()) .catch((ex) => console.log(ex)) diff --git a/examples/todo/server/index.ts b/examples/todo/server/index.ts index c1ea408..9dde24d 100644 --- a/examples/todo/server/index.ts +++ b/examples/todo/server/index.ts @@ -9,9 +9,14 @@ var daemon = new Daemon({ password: 'test', features: { fetch: 'full', - batches: true, - asNotification: true + batches: false, + asNotification: false } + // log: { + // logCallbacks: [console.log], + // logName: 'Daemon', + // logLevel: LogLevel.socket + // } }) daemon.listen({ @@ -23,6 +28,11 @@ console.log('listening on port', port) // Create Jet Peer var peer = new Peer({ url: `ws://localhost:8081/` + // log: { + // logCallbacks: [console.log], + // logName: 'Peer 1', + // logLevel: LogLevel.socket + // } }) // const peer2 = new jet.Peer({ // port: internalPort,log:{logCallbacks:[console.log],logname:"Peer 2",loglevel:jet.LogLevel.debug} diff --git a/package.json b/package.json index 0a8f691..35a7bdd 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "prettier": "prettier --write $npm_package_config_files", "eslint": "eslint --fix --max-warnings 0 --ignore-path .prettierignore $npm_package_config_files", "ex:chat": "npm run build && concurrently \"webpack serve --config examples/chat/webpack.config.cjs\" \"wait-on examples/chat/dist/server.cjs && node examples/chat/dist/server.cjs\"", - "ex:todo": "npm run build && concurrently \"webpack serve --config examples/todo/webpack.config.cjs\" \"wait-on examples/todo/dist/server.cjs && node examples/todo/dist/server.cjs\"", + "ex:todo": "rm -r examples/todo/dist && npm run build && concurrently \"webpack serve --config examples/todo/webpack.config.cjs\" \"wait-on examples/todo/dist/server.cjs && node examples/todo/dist/server.cjs\"", "ex:balls": "npm run build && concurrently \"webpack serve --config examples/balls/webpack.config.cjs\" \"wait-on examples/balls/dist/server.cjs && node examples/balls/dist/server.cjs\"" }, "dependencies": { diff --git a/src/2_jsonrpc/index.ts b/src/2_jsonrpc/index.ts index f0d8abe..064e833 100644 --- a/src/2_jsonrpc/index.ts +++ b/src/2_jsonrpc/index.ts @@ -59,7 +59,6 @@ export class JsonRPC extends EventEmitter { reject: (value: JsonRPCError | PromiseLike) => void } > = {} - batchPromises: Promise[] = [] requestId = '' resolveDisconnect!: (value: void | PromiseLike) => void // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -71,11 +70,10 @@ export class JsonRPC extends EventEmitter { connectPromise!: Promise logger: Logger abortController!: AbortController - sendImmediate: boolean + constructor(logger: Logger, config?: JsonRpcConfig, sock?: Socket) { super() this.config = config || {} - this.sendImmediate = config?.batches ? false : true this.createDisconnectPromise() this.createConnectPromise() this.logger = logger @@ -241,6 +239,7 @@ export class JsonRPC extends EventEmitter { if (!this._isOpen) return Promise.reject(new ConnectionClosed()) if (id) this.messages.push({ method: id, params: message } as Message) else this.messages.push(message as Message) + if (!this.config.batches) this.send() } /** @@ -265,7 +264,6 @@ export class JsonRPC extends EventEmitter { */ respond = (id: string, params: ValueType, success: boolean) => { this.queue({ id, [success ? 'result' : 'error']: params }) - if (this.sendImmediate) this.send() } successCb = (id: string, result: ValueType) => { @@ -285,7 +283,9 @@ export class JsonRPC extends EventEmitter { */ sendRequest = ( method: string, - params: JsonParams + params: JsonParams, + // Jet Peer uses send immediate to call all functions without delay + sendImmediate = false ): Promise => new Promise((resolve, reject) => { if (!this._isOpen) reject(new ConnectionClosed()) @@ -303,7 +303,7 @@ export class JsonRPC extends EventEmitter { method, params }) - if (this.sendImmediate) this.send() + if (sendImmediate) this.send() } }) } diff --git a/src/3_jet/daemon/index.ts b/src/3_jet/daemon/index.ts index a135e60..b9caa8c 100644 --- a/src/3_jet/daemon/index.ts +++ b/src/3_jet/daemon/index.ts @@ -334,27 +334,15 @@ export class Daemon extends EventEmitter { 'set', (peer: JsonRPC, id: string, params: PathParams) => this.forward('set', peer.user, params) - .then((res) => { - newPeer.respond(id, res, true) - newPeer.send() - }) - .catch((err) => { - newPeer.respond(id, err, false) - newPeer.send() - }) + .then((res) => newPeer.respond(id, res, true)) + .catch((err) => newPeer.respond(id, err, false)) ) newPeer.addListener( 'call', (peer: JsonRPC, id: string, params: PathParams) => this.forward('call', peer.user, params) - .then((res) => { - newPeer.respond(id, res, true) - newPeer.send() - }) - .catch((err) => { - newPeer.respond(id, err, false) - newPeer.send() - }) + .then((res) => newPeer.respond(id, res, true)) + .catch((err) => newPeer.respond(id, err, false)) ) }) this.jsonRPCServer.addListener('disconnect', (peer: JsonRPC) => { diff --git a/src/3_jet/daemon/subscription.ts b/src/3_jet/daemon/subscription.ts index 7f65401..1ff1e0a 100644 --- a/src/3_jet/daemon/subscription.ts +++ b/src/3_jet/daemon/subscription.ts @@ -58,6 +58,7 @@ export class Subscription { send = () => { this.messages.forEach((msg) => this.owner?.queue(msg, this.id)) + this.owner?.send() this.messages = [] } } diff --git a/src/3_jet/peer/index.ts b/src/3_jet/peer/index.ts index 06d077f..863db34 100644 --- a/src/3_jet/peer/index.ts +++ b/src/3_jet/peer/index.ts @@ -79,6 +79,8 @@ export interface PeerConfig extends JsonRpcConfig { export class Peer extends EventEmitter { #config: PeerConfig #jsonrpc: JsonRPC + //All requests are send immediately except the batch function is called + #sendImmediate = true #daemonInfo: InfoOptions = fallbackDaemonInfo #routes: Record> = {} #fetcher: Record = {} @@ -189,7 +191,7 @@ export class Peer extends EventEmitter { if (Object.keys(this.#fetcher).length === 2) { const param = { id: fetchSimpleId } return this.#jsonrpc - .sendRequest('unfetch', param) + .sendRequest('unfetch', param, this.#sendImmediate) .then(() => delete this.#fetcher[id]) .then(() => Promise.resolve()) } else { @@ -197,10 +199,12 @@ export class Peer extends EventEmitter { return Promise.resolve() } } else { - return this.#jsonrpc.sendRequest('unfetch', { id }).then(() => { - delete this.#fetcher[id] - return Promise.resolve() - }) + return this.#jsonrpc + .sendRequest('unfetch', { id }, this.#sendImmediate) + .then(() => { + delete this.#fetcher[id] + return Promise.resolve() + }) } } fetchFull = () => this.#daemonInfo.features?.fetch === 'full' @@ -225,7 +229,7 @@ export class Peer extends EventEmitter { } ) return this.#jsonrpc - .sendRequest('fetch', params) + .sendRequest('fetch', params, this.#sendImmediate) .then(() => Promise.resolve()) } const sub = new Subscription(fetcher.message) @@ -241,7 +245,7 @@ export class Peer extends EventEmitter { this.#fetcher[fetchSimpleId] = new Fetcher() const params = { id: fetchSimpleId, path: { startsWith: '' } } return this.#jsonrpc - .sendRequest('fetch', params) + .sendRequest('fetch', params, this.#sendImmediate) .then(() => Promise.resolve()) } else { return Promise.resolve() @@ -276,11 +280,19 @@ export class Peer extends EventEmitter { * }) */ authenticate = (user: string, password: string) => { - return this.#jsonrpc.sendRequest('authenticate', { user, password }) + return this.#jsonrpc.sendRequest( + 'authenticate', + { user, password }, + this.#sendImmediate + ) } addUser = (user: string, password: string, groups: string[]) => { - return this.#jsonrpc.sendRequest('addUser', { user, password, groups }) + return this.#jsonrpc.sendRequest( + 'addUser', + { user, password, groups }, + this.#sendImmediate + ) } connect = (controller: AbortController = new AbortController()) => this.#jsonrpc @@ -288,7 +300,7 @@ export class Peer extends EventEmitter { .then(() => this.info()) .then((daemonInfo) => { this.#daemonInfo = daemonInfo || fallbackDaemonInfo - this.#jsonrpc.sendImmediate = + this.#jsonrpc.config.batches = !this.#daemonInfo.features?.batches || true return Promise.resolve() }) @@ -310,9 +322,11 @@ export class Peer extends EventEmitter { * */ batch = (action: () => void) => { - this.#jsonrpc.sendImmediate = false + if (!this.#daemonInfo.features?.batches) + throw 'Daemon does not support batches' + this.#sendImmediate = false action() - this.#jsonrpc.sendImmediate = true + this.#sendImmediate = true return this.#jsonrpc.send() } @@ -323,29 +337,39 @@ export class Peer extends EventEmitter { * @returns {external:Promise} */ get = (expression: JsonParams) => - this.#jsonrpc.sendRequest<{ path: string; value: T }[]>('get', expression) + this.#jsonrpc.sendRequest<{ path: string; value: T }[]>( + 'get', + expression, + this.#sendImmediate + ) /** * Adds a state or method to the Daemon. * - * @param {(State|Method)} content To content to be added. + * @param {(State|Method)} content The content to be added. * @returns {external:Promise} Gets resolved as soon as the content has been added to the Daemon. */ add = (stateOrMethod: Method | State) => { if (isState(stateOrMethod)) { stateOrMethod.addListener('change', (newValue: ValueType) => { - this.#jsonrpc.sendRequest('change', { - path: stateOrMethod._path, - value: newValue - }) + this.#jsonrpc.sendRequest( + 'change', + { + path: stateOrMethod._path, + value: newValue + }, + this.#sendImmediate + ) }) } - return this.#jsonrpc.sendRequest('add', stateOrMethod.toJson()).then(() => { - this.#routes[stateOrMethod._path] = - // eslint-disable-next-line @typescript-eslint/no-explicit-any - stateOrMethod as any as State - return Promise.resolve() - }) + return this.#jsonrpc + .sendRequest('add', stateOrMethod.toJson(), this.#sendImmediate) + .then(() => { + this.#routes[stateOrMethod._path] = + // eslint-disable-next-line @typescript-eslint/no-explicit-any + stateOrMethod as any as State + return Promise.resolve() + }) } /** @@ -356,7 +380,11 @@ export class Peer extends EventEmitter { */ remove = (stateOrMethod: Method | State) => this.#jsonrpc - .sendRequest('remove', { path: stateOrMethod.path() }) + .sendRequest( + 'remove', + { path: stateOrMethod.path() }, + this.#sendImmediate + ) .then(() => Promise.resolve()) /** @@ -374,14 +402,19 @@ export class Peer extends EventEmitter { ): Promise => { const params = { path: path } as JsonParams if (callparams) params.args = callparams - return this.#jsonrpc.sendRequest('call', params) + return this.#jsonrpc.sendRequest( + 'call', + params, + this.#sendImmediate + ) } /** * Info * @private */ - info = () => this.#jsonrpc.sendRequest('info', {}) + info = () => + this.#jsonrpc.sendRequest('info', {}, this.#sendImmediate) /** * Authenticate @@ -398,7 +431,7 @@ export class Peer extends EventEmitter { * @private */ configure = (params: JsonParams) => - this.#jsonrpc.sendRequest('config', params) + this.#jsonrpc.sendRequest('config', params, this.#sendImmediate) /** * Set a {State} to another value. @@ -410,7 +443,7 @@ export class Peer extends EventEmitter { * */ set = (path: string, value: ValueType) => - this.#jsonrpc.sendRequest('set', { path, value }) + this.#jsonrpc.sendRequest('set', { path, value }, this.#sendImmediate) } export default Peer diff --git a/test/jsonrpc/index.test.ts b/test/jsonrpc/index.test.ts index 0ed972b..2fb704c 100644 --- a/test/jsonrpc/index.test.ts +++ b/test/jsonrpc/index.test.ts @@ -457,14 +457,12 @@ describe('Testing JsonRpc', () => { const jsonrpc = new JsonRPC(new Logger()) jsonrpc.connect().then(() => { sock.emit('message', { data: json }) - expect(jsonrpc.sendRequest('add', { path: 'foo' }, true)).rejects.toEqual( - { - code: -32602, - data: { - pathNotExists: 'Foo' - } + expect(jsonrpc.sendRequest('add', { path: 'foo' })).rejects.toEqual({ + code: -32602, + data: { + pathNotExists: 'Foo' } - ) + }) done() }) From 79442032ee9303d36c9bf81bccbb26f2b0067c7b Mon Sep 17 00:00:00 2001 From: Florian Schopp Date: Thu, 2 Nov 2023 14:02:59 +0100 Subject: [PATCH 2/8] Fixed tests --- test/jsonrpc/index.test.ts | 16 ++-- test/mocks/peer.ts | 2 + test/peer/peer.test.ts | 176 ++++++++++++++++++++++++++----------- 3 files changed, 136 insertions(+), 58 deletions(-) diff --git a/test/jsonrpc/index.test.ts b/test/jsonrpc/index.test.ts index 2fb704c..b1f6472 100644 --- a/test/jsonrpc/index.test.ts +++ b/test/jsonrpc/index.test.ts @@ -65,7 +65,7 @@ describe('Testing JsonRpc', () => { done() }) const jsonrpc = new JsonRPC(new Logger(), {}, sock) - jsonrpc.sendRequest('add', { path: 'foo', value: 3 }) + jsonrpc.sendRequest('add', { path: 'foo', value: 3 }, true) }) it('Should test batch send', (done) => { const sock = sockMock() @@ -493,13 +493,13 @@ describe('Testing JsonRpc', () => { }) const jsonrpc = new JsonRPC(new Logger(), { batches: true }) jsonrpc.connect().then(async () => { - jsonrpc.sendImmediate = false - jsonrpc.sendRequest('add', { path: 'foo', value: 3 }) - jsonrpc.sendRequest('add', { path: 'foo1', value: 4 }).catch((ex) => { - expect(ex).toEqual({ code: 0, name: 'error' }) - done() - }) - jsonrpc.sendImmediate = true + jsonrpc.sendRequest('add', { path: 'foo', value: 3 }, false) + jsonrpc + .sendRequest('add', { path: 'foo1', value: 4 }, false) + .catch((ex) => { + expect(ex).toEqual({ code: 0, name: 'error' }) + done() + }) jsonrpc.send() }) diff --git a/test/mocks/peer.ts b/test/mocks/peer.ts index 40c858e..7bd7d8a 100644 --- a/test/mocks/peer.ts +++ b/test/mocks/peer.ts @@ -11,6 +11,7 @@ export const fullFetcherPeer = (): any => { respond: jest.fn(), send: jest.fn(), queue: jest.fn(), + config: {}, sendRequest: jest .fn() .mockImplementation((method) => @@ -33,6 +34,7 @@ export const simpleFecherPeer = (): any => { ...(jest.createMockFromModule('../../src/2_jsonrpc') as JsonRPC.JsonRPC), connect: () => Promise.resolve(), callbacks: {}, + config: {}, _isOpen: true, respond: jest.fn(), send: jest.fn(), diff --git a/test/peer/peer.test.ts b/test/peer/peer.test.ts index f3da20e..114afa8 100644 --- a/test/peer/peer.test.ts +++ b/test/peer/peer.test.ts @@ -237,7 +237,7 @@ describe('Testing Peer', () => { const peer = new Peer() peer.connect().then(() => { expect(connectSpy).toBeCalled() - expect(sendSpy).toBeCalledWith('info', {}) + expect(sendSpy).toBeCalledWith('info', {}, true) expect(peer.isConnected()).toBe(true) done() }) @@ -251,7 +251,11 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.add(new State('My path', 3)).catch((ex) => { - expect(sendSpy).toBeCalledWith('add', { path: 'My path', value: 3 }) + expect(sendSpy).toBeCalledWith( + 'add', + { path: 'My path', value: 3 }, + true + ) expect(ex).toBe('invalid path') done() }) @@ -263,9 +267,17 @@ describe('Testing Peer', () => { const peer = new Peer() const myState = new State('My path', 4) peer.add(myState).then(() => { - expect(sendSpy).toBeCalledWith('add', { path: 'My path', value: 4 }) + expect(sendSpy).toBeCalledWith( + 'add', + { path: 'My path', value: 4 }, + true + ) myState.value(6) - expect(sendSpy).toBeCalledWith('change', { path: 'My path', value: 6 }) + expect(sendSpy).toBeCalledWith( + 'change', + { path: 'My path', value: 6 }, + true + ) done() }) }) @@ -275,7 +287,7 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.add(new Method('My path')).then(() => { - expect(sendSpy).toBeCalledWith('add', { path: 'My path' }) + expect(sendSpy).toBeCalledWith('add', { path: 'My path' }, true) done() }) }) @@ -285,7 +297,7 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.add(new Method('My path')).then(() => { - expect(sendSpy).toBeCalledWith('add', { path: 'My path' }) + expect(sendSpy).toBeCalledWith('add', { path: 'My path' }, true) done() }) }) @@ -296,13 +308,31 @@ describe('Testing Peer', () => { const peer = new Peer() peer.batch(() => jest.fn()) }) + it('Should fail to create batch', (done) => { + const sendSpy = jest.fn().mockReturnValue(Promise.resolve({})) + const connSpy = jest + .fn() + .mockReturnValue(Promise.resolve({ features: { batches: false } })) + const jsonrpc = { + ...fullFetcherPeer(), + sendRequest: sendSpy, + connect: connSpy + } + + jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) + const peer = new Peer() + peer.connect().then(() => { + expect(() => peer.batch(() => jest.fn())).toThrow('') + done() + }) + }) it('Should send configure', (done) => { const sendSpy = jest.fn().mockReturnValue(Promise.resolve({})) const jsonrpc = { ...fullFetcherPeer(), sendRequest: sendSpy } jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.configure({}).then(() => { - expect(sendSpy).toBeCalledWith('config', {}) + expect(sendSpy).toBeCalledWith('config', {}, true) done() }) }) @@ -314,7 +344,7 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.remove(new State('My path', 5)).catch((ex) => { - expect(sendSpy).toBeCalledWith('remove', { path: 'My path' }) + expect(sendSpy).toBeCalledWith('remove', { path: 'My path' }, true) expect(ex).toBe('invalid path') done() }) @@ -325,7 +355,7 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.remove(new State('My path', 5)).then(() => { - expect(sendSpy).toBeCalledWith('remove', { path: 'My path' }) + expect(sendSpy).toBeCalledWith('remove', { path: 'My path' }, true) done() }) }) @@ -336,11 +366,15 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.addUser('Admin', 'admin', ['test']).then(() => { - expect(sendSpy).toBeCalledWith('addUser', { - password: 'admin', - user: 'Admin', - groups: ['test'] - }) + expect(sendSpy).toBeCalledWith( + 'addUser', + { + password: 'admin', + user: 'Admin', + groups: ['test'] + }, + true + ) done() }) }) @@ -350,10 +384,14 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.authenticate('Admin', 'admin').then(() => { - expect(sendSpy).toBeCalledWith('authenticate', { - password: 'admin', - user: 'Admin' - }) + expect(sendSpy).toBeCalledWith( + 'authenticate', + { + password: 'admin', + user: 'Admin' + }, + true + ) done() }) }) @@ -364,7 +402,11 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.get({ path: { startsWith: 'a' } }).catch((ex) => { - expect(sendSpy).toBeCalledWith('get', { path: { startsWith: 'a' } }) + expect(sendSpy).toBeCalledWith( + 'get', + { path: { startsWith: 'a' } }, + true + ) expect(ex).toBe('invalid path') done() }) @@ -375,7 +417,11 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.get({ path: { startsWith: 'a' } }).then((res) => { - expect(sendSpy).toBeCalledWith('get', { path: { startsWith: 'a' } }) + expect(sendSpy).toBeCalledWith( + 'get', + { path: { startsWith: 'a' } }, + true + ) expect(res).toBe(5) done() }) @@ -389,10 +435,14 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.set('Foo', 5).catch((ex) => { - expect(sendSpy).toBeCalledWith('set', { - path: 'Foo', - value: 5 - }) + expect(sendSpy).toBeCalledWith( + 'set', + { + path: 'Foo', + value: 5 + }, + true + ) expect(ex).toBe('invalid path') done() }) @@ -403,7 +453,7 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.set('Foo', 5).then((res) => { - expect(sendSpy).toBeCalledWith('set', { path: 'Foo', value: 5 }) + expect(sendSpy).toBeCalledWith('set', { path: 'Foo', value: 5 }, true) expect(res).toBe(5) done() }) @@ -416,10 +466,14 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.call('Foo', [5]).catch((ex) => { - expect(sendSpy).toBeCalledWith('call', { - path: 'Foo', - args: [5] - }) + expect(sendSpy).toBeCalledWith( + 'call', + { + path: 'Foo', + args: [5] + }, + true + ) expect(ex).toBe('invalid path') done() }) @@ -430,10 +484,14 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.call('Foo', { abc: 4 }).then((res) => { - expect(sendSpy).toBeCalledWith('call', { - path: 'Foo', - args: { abc: 4 } - }) + expect(sendSpy).toBeCalledWith( + 'call', + { + path: 'Foo', + args: { abc: 4 } + }, + true + ) expect(res).toEqual({}) done() }) @@ -446,10 +504,14 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.call('Foo', [5]).catch((ex) => { - expect(sendSpy).toBeCalledWith('call', { - path: 'Foo', - args: [5] - }) + expect(sendSpy).toBeCalledWith( + 'call', + { + path: 'Foo', + args: [5] + }, + true + ) expect(ex).toBe('invalid path') done() }) @@ -460,10 +522,14 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.call('Foo', { abc: 4 }).then((res) => { - expect(sendSpy).toBeCalledWith('call', { - path: 'Foo', - args: { abc: 4 } - }) + expect(sendSpy).toBeCalledWith( + 'call', + { + path: 'Foo', + args: { abc: 4 } + }, + true + ) expect(res).toEqual({}) done() }) @@ -476,7 +542,11 @@ describe('Testing Peer', () => { jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc) const peer = new Peer() peer.fetch(new Fetcher()).catch((ex) => { - expect(sendSpy).toBeCalledWith('fetch', expect.objectContaining({})) + expect(sendSpy).toBeCalledWith( + 'fetch', + expect.objectContaining({}), + true + ) expect(ex).toBe('invalid path') done() }) @@ -500,7 +570,8 @@ describe('Testing Peer', () => { 'fetch', expect.objectContaining({ path: { startsWith: 'a' } - }) + }), + true ) }) .then(() => peer.fetch(new Fetcher().path('equals', 'b'))) @@ -509,7 +580,8 @@ describe('Testing Peer', () => { 'fetch', expect.objectContaining({ path: { equals: 'b' } - }) + }), + true ) done() @@ -537,10 +609,14 @@ describe('Testing Peer', () => { ) .then(() => peer.fetch(new Fetcher().path('startsWith', 'f'))) .then(() => { - expect(mockPeer.sendRequest).toBeCalledWith('fetch', { - id: 'fetch_all', - path: { startsWith: '' } - }) + expect(mockPeer.sendRequest).toBeCalledWith( + 'fetch', + { + id: 'fetch_all', + path: { startsWith: '' } + }, + true + ) }) .then(() => peer.fetch(new Fetcher().path('equals', 'b'))) .then(() => { @@ -577,7 +653,7 @@ describe('Testing Peer', () => { .then(() => peer.fetch(fetcher)) .then(() => peer.unfetch(fetcher)) .then(() => { - expect(sendSpy).toBeCalledWith('unfetch', expect.anything()) + expect(sendSpy).toBeCalledWith('unfetch', expect.anything(), true) done() }) }) @@ -609,7 +685,7 @@ describe('Testing Peer', () => { .then(() => { //Only send unfetch event when no fetchers are registered anymore expect(sendSpy).toBeCalledTimes(3) - expect(sendSpy).toBeCalledWith('unfetch', { id: 'fetch_all' }) + expect(sendSpy).toBeCalledWith('unfetch', { id: 'fetch_all' }, true) done() }) }) From db0dfe93b0ec33b9e986930ef0466bc8bb4555fc Mon Sep 17 00:00:00 2001 From: Florian Schopp Date: Thu, 2 Nov 2023 14:15:15 +0100 Subject: [PATCH 3/8] Fixed examples --- examples/todo/client/client.ts | 26 +------------------------- examples/todo/server/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 27 deletions(-) diff --git a/examples/todo/client/client.ts b/examples/todo/client/client.ts index 93e7d82..5ea84e8 100644 --- a/examples/todo/client/client.ts +++ b/examples/todo/client/client.ts @@ -43,24 +43,6 @@ const todos = new Fetcher() } renderTodos() }) - -const test2 = new Fetcher().path('startsWith', 'te') - -const test = new Fetcher() - .path('startsWith', 'test') - .addListener('data', (todo: PublishMessage) => { - switch (todo.event) { - case 'Add': - console.log('add', todo.value) - case 'Change': - console.log('change', todo.value) - // todoList[todo.path] = todo.value - break - case 'Remove': - delete todoList[todo.path] - break - } - }) /* * GUI Logic: */ @@ -152,12 +134,6 @@ document.getElementById('todo-form')!.addEventListener('submit', (event) => { peer .connect() .then(() => peer.authenticate('Admin', 'test')) - .then(() => - peer.batch(() => { - peer.fetch(test) - peer.fetch(test2) - peer.fetch(todos) - }) - ) + .then(() => peer.fetch(todos)) .then(() => renderTodos()) .catch((ex) => console.log(ex)) diff --git a/examples/todo/server/index.ts b/examples/todo/server/index.ts index 9dde24d..f66ecb1 100644 --- a/examples/todo/server/index.ts +++ b/examples/todo/server/index.ts @@ -9,8 +9,8 @@ var daemon = new Daemon({ password: 'test', features: { fetch: 'full', - batches: false, - asNotification: false + batches: true, + asNotification: true } // log: { // logCallbacks: [console.log], From 34ef7407655502133bcbea0731d203ae5f970a0d Mon Sep 17 00:00:00 2001 From: Florian Schopp Date: Thu, 2 Nov 2023 14:23:14 +0100 Subject: [PATCH 4/8] Deleted filelogger --- src/3_jet/log.ts | 32 ---------------------- test/log.test.ts | 70 +----------------------------------------------- 2 files changed, 1 insertion(+), 101 deletions(-) diff --git a/src/3_jet/log.ts b/src/3_jet/log.ts index bee69dd..fafd41c 100644 --- a/src/3_jet/log.ts +++ b/src/3_jet/log.ts @@ -1,4 +1,3 @@ -import * as fs from 'fs' export enum LogLevel { 'socket' = 1, 'debug', @@ -22,7 +21,6 @@ export class Logger { logName: string logLevel: LogLevel callBacks: LogFunction[] | undefined - stream: fs.WriteStream | undefined /** * Constructor to create a new Logger instance * @param settings @@ -31,9 +29,6 @@ export class Logger { this.logName = settings.logName this.logLevel = settings.logLevel || LogLevel['none'] this.callBacks = settings.logCallbacks - if (settings.logFile) { - this.stream = fs.createWriteStream(settings.logFile) - } } /** * Function that transforms a message into a string of the format "