diff --git a/src/server/Server.js b/src/server/Server.js index 7b2e22b3..5ebd1237 100644 --- a/src/server/Server.js +++ b/src/server/Server.js @@ -599,7 +599,7 @@ Invalid certificate files, please check your: throw err; } } else { - // generate certs + // generate self signed certs or reused already existing certs // -------------------------------------------------------- const cert = await this.#db.get('httpsCert'); const key = await this.#db.get('httpsKey'); @@ -1138,9 +1138,6 @@ Invalid certificate files, please check your: Object.assign(this[kServerApplicationTemplateOptions], options); } - - -// /** @private */ [kServerIsProtectedRole](role) { if (this.#config.env.auth && Array.isArray(this.#config.env.auth.clients)) { diff --git a/src/server/ServerStateManager.js b/src/server/ServerStateManager.js index cfa249d2..f83c80d7 100644 --- a/src/server/ServerStateManager.js +++ b/src/server/ServerStateManager.js @@ -566,7 +566,7 @@ class ServerStateManager extends BaseStateManager { * name: { type: 'string', required: true }, * hookTriggered: { type: 'boolean', default: false }, * }); - * server.stateManager.addCreateHook('hooked', initValues => { + * server.stateManager.registerCreateHook('hooked', initValues => { * return { * ...initValues * hookTriggered: true, @@ -580,13 +580,13 @@ class ServerStateManager extends BaseStateManager { * const values = state.getValues(); * assert.deepEqual(result, { value: 'coucou', hookTriggered: true }); */ - addCreateHook(className, createHook) { + registerCreateHook(className, createHook) { if (!this.#classes.has(className)) { - throw new TypeError(`Cannot execute 'addCreateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`); + throw new TypeError(`Cannot execute 'registerCreateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`); } if (!isFunction(createHook)) { - throw new TypeError(`Cannot execute 'addCreateHook' on 'BaseStateManager': argument 2 must be a function`); + throw new TypeError(`Cannot execute 'registerCreateHook' on 'BaseStateManager': argument 2 must be a function`); } const hooks = this.#createHooksByClassName.get(className); @@ -620,7 +620,7 @@ class ServerStateManager extends BaseStateManager { * name: { type: 'string', required: true }, * hookTriggered: { type: 'boolean', default: false }, * }); - * server.stateManager.addDeleteHook('hooked', async currentValues => { + * server.stateManager.registerDeleteHook('hooked', async currentValues => { * await doSomethingWithValues(currentValues) * }); * @@ -628,13 +628,13 @@ class ServerStateManager extends BaseStateManager { * // later * await state.delete(); */ - addDeleteHook(className, deleteHook) { + registerDeleteHook(className, deleteHook) { if (!this.#classes.has(className)) { - throw new TypeError(`Cannot execute 'addDeleteHook' on 'BaseStateManager': SharedState class '${className}' does not exists`); + throw new TypeError(`Cannot execute 'registerDeleteHook' on 'BaseStateManager': SharedState class '${className}' does not exists`); } if (!isFunction(deleteHook)) { - throw new TypeError(`Cannot execute 'addDeleteHook' on 'BaseStateManager': argument 2 must be a function`); + throw new TypeError(`Cannot execute 'registerDeleteHook' on 'BaseStateManager': argument 2 must be a function`); } const hooks = this.#deleteHooksByClassName.get(className); @@ -670,7 +670,7 @@ class ServerStateManager extends BaseStateManager { * value: { type: 'string', default: null, nullable: true }, * numUpdates: { type: 'integer', default: 0 }, * }); - * server.stateManager.addUpdateHook('hooked', updates => { + * server.stateManager.registerUpdateHook('hooked', updates => { * return { * ...updates * numUpdates: currentValues.numUpdates + 1, @@ -683,13 +683,13 @@ class ServerStateManager extends BaseStateManager { * const values = state.getValues(); * assert.deepEqual(result, { value: 'test', numUpdates: 1 }); */ - addUpdateHook(className, updateHook) { + registerUpdateHook(className, updateHook) { if (!this.#classes.has(className)) { - throw new TypeError(`Cannot execute 'addUpdateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`); + throw new TypeError(`Cannot execute 'registerUpdateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`); } if (!isFunction(updateHook)) { - throw new TypeError(`Cannot execute 'addUpdateHook' on 'BaseStateManager': argument 2 must be a function`); + throw new TypeError(`Cannot execute 'registerUpdateHook' on 'BaseStateManager': argument 2 must be a function`); } const hooks = this.#updateHooksByClassName.get(className); @@ -697,14 +697,6 @@ class ServerStateManager extends BaseStateManager { return () => hooks.delete(updateHook); } - - /** - * @deprecated Use {@link ServerStateManager#addUpdateHook} instead. - */ - registerUpdateHook(className, updateHook) { - logger.deprecated('ServerStateManager#registerUpdateHook', 'ServerStateManager#addUpdateHook', '4.0.0-alpha.29'); - return this.addUpdateHook(className, updateHook); - } } export default ServerStateManager; diff --git a/tests/misc/deprecated.spec.js b/tests/misc/deprecated.spec.js index ab122b7e..1992d7c5 100644 --- a/tests/misc/deprecated.spec.js +++ b/tests/misc/deprecated.spec.js @@ -70,18 +70,6 @@ describe('from v4.0.0-alpha.29', () => { server.stateManager.deleteClass('a'); await server.stop(); }); - - it('ServerStateManager#registerUpdateHook()', async () => { - const server = new Server(config); - server.stateManager.defineClass('a', a); - await server.start(); - - await server.stateManager.onUpdateHook('a', () => {}); // actual - await server.stateManager.registerUpdateHook('a', () => {}); // deprecated - - server.stateManager.deleteClass('a'); - await server.stop(); - }); }); describe('# removed API', () => { diff --git a/tests/states/SharedStateParameterDescription.spec.js b/tests/states/SharedStateParameterDescription.spec.js index 89fe3e65..7972deb0 100644 --- a/tests/states/SharedStateParameterDescription.spec.js +++ b/tests/states/SharedStateParameterDescription.spec.js @@ -170,7 +170,7 @@ describe('# SharedStateParameterDescription', () => { let subscribeCalledBeforeHook = false; - server.stateManager.onUpdateHook('immediate-test', (updates, currentValues) => { + server.stateManager.registerUpdateHook('immediate-test', (updates, currentValues) => { try { // assert.fail(); assert.isTrue(subscribeCalledBeforeHook, 'subscribe should be called before hook'); diff --git a/tests/states/StateManager.spec.js b/tests/states/StateManager.spec.js index d604396c..dee1c399 100644 --- a/tests/states/StateManager.spec.js +++ b/tests/states/StateManager.spec.js @@ -711,7 +711,7 @@ describe(`# StateManager`, () => { }); }); - describe(`## [server] onCreateHook(className, createHook)`, () => { + describe(`## [server] registerCreateHook(className, createHook)`, () => { const hookSchema = { name: { type: 'string', @@ -730,7 +730,7 @@ describe(`# StateManager`, () => { let errored = false; try { - server.stateManager.onCreateHook('hooked', () => {}); + server.stateManager.registerCreateHook('hooked', () => {}); } catch (err) { console.log(err.message); errored = true; @@ -743,10 +743,10 @@ describe(`# StateManager`, () => { server.stateManager.defineClass('hooked', hookSchema); let errored = false; - assert.throws(() => server.stateManager.onCreateHook('hooked', null)); - assert.throws(() => server.stateManager.onCreateHook('hooked', {})); - assert.doesNotThrow(() => server.stateManager.onCreateHook('hooked', () => {})); - assert.doesNotThrow(() => server.stateManager.onCreateHook('hooked', async () => {})); + assert.throws(() => server.stateManager.registerCreateHook('hooked', null)); + assert.throws(() => server.stateManager.registerCreateHook('hooked', {})); + assert.doesNotThrow(() => server.stateManager.registerCreateHook('hooked', () => {})); + assert.doesNotThrow(() => server.stateManager.registerCreateHook('hooked', async () => {})); }); it(`should execute hook on creation of state of given class`, async () => { @@ -756,8 +756,8 @@ describe(`# StateManager`, () => { let hookedCalled = false; let otherCalled = false; - server.stateManager.onCreateHook('hooked', () => hookedCalled = true);; - server.stateManager.onCreateHook('other', () => otherCalled = true);; + server.stateManager.registerCreateHook('hooked', () => hookedCalled = true);; + server.stateManager.registerCreateHook('other', () => otherCalled = true);; const _ = await server.stateManager.create('hooked'); @@ -767,7 +767,7 @@ describe(`# StateManager`, () => { it(`should allow to modify init values`, async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onCreateHook('hooked', (initValues) => { + server.stateManager.registerCreateHook('hooked', (initValues) => { if (initValues.name === 'test') { return { value: 'ok', @@ -783,7 +783,7 @@ describe(`# StateManager`, () => { it(`should throw if create hook explicitly return null`, async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onCreateHook('hooked', (initValues) => { + server.stateManager.registerCreateHook('hooked', (initValues) => { return null; }); @@ -801,7 +801,7 @@ describe(`# StateManager`, () => { it(`should implicitly continue if hook returns undefined`, async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onCreateHook('hooked', initValues => { + server.stateManager.registerCreateHook('hooked', initValues => { if (initValues.name === 'test') { return undefined; } @@ -814,7 +814,7 @@ describe(`# StateManager`, () => { it(`should support async hooks`, async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onCreateHook('hooked', async (initValues) => { + server.stateManager.registerCreateHook('hooked', async (initValues) => { await delay(20); if (initValues.name === 'test') { return { @@ -831,7 +831,7 @@ describe(`# StateManager`, () => { it(`should allow to chain hooks`, async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onCreateHook('hooked', async (initValues) => { + server.stateManager.registerCreateHook('hooked', async (initValues) => { await delay(20); if (initValues.name === 'test') { return { @@ -841,7 +841,7 @@ describe(`# StateManager`, () => { } }); - server.stateManager.onCreateHook('hooked', (initValues) => { + server.stateManager.registerCreateHook('hooked', (initValues) => { // first callback has been properly executed const expected = { name: 'test', value: 'ok1' }; assert.deepEqual(initValues, expected) @@ -859,7 +859,7 @@ describe(`# StateManager`, () => { }); }); - describe(`## [server] onDeleteHook(className, createHook)`, () => { + describe(`## [server] registerDeleteHook(className, createHook)`, () => { const hookSchema = { value: { type: 'integer', @@ -871,7 +871,7 @@ describe(`# StateManager`, () => { let errored = false; try { - server.stateManager.onDeleteHook('hooked', () => {}); + server.stateManager.registerDeleteHook('hooked', () => {}); } catch (err) { console.log(err.message); errored = true; @@ -884,10 +884,10 @@ describe(`# StateManager`, () => { server.stateManager.defineClass('hooked', hookSchema); let errored = false; - assert.throws(() => server.stateManager.onDeleteHook('hooked', null)); - assert.throws(() => server.stateManager.onDeleteHook('hooked', {})); - assert.doesNotThrow(() => server.stateManager.onDeleteHook('hooked', () => {})); - assert.doesNotThrow(() => server.stateManager.onDeleteHook('hooked', async () => {})); + assert.throws(() => server.stateManager.registerDeleteHook('hooked', null)); + assert.throws(() => server.stateManager.registerDeleteHook('hooked', {})); + assert.doesNotThrow(() => server.stateManager.registerDeleteHook('hooked', () => {})); + assert.doesNotThrow(() => server.stateManager.registerDeleteHook('hooked', async () => {})); }); @@ -895,7 +895,7 @@ describe(`# StateManager`, () => { server.stateManager.defineClass('hooked', hookSchema); let called = false; - server.stateManager.onDeleteHook('hooked', finalValues => called = true); + server.stateManager.registerDeleteHook('hooked', finalValues => called = true); const state = await server.stateManager.create('hooked'); assert.equal(called, false); @@ -908,7 +908,7 @@ describe(`# StateManager`, () => { server.stateManager.defineClass('hooked', hookSchema); let called = false; - server.stateManager.onDeleteHook('hooked', async finalValues => { + server.stateManager.registerDeleteHook('hooked', async finalValues => { await delay(20); called = true; }); @@ -923,7 +923,7 @@ describe(`# StateManager`, () => { server.stateManager.defineClass('hooked', hookSchema); let called = false; - server.stateManager.onDeleteHook('hooked', async finalValues => called = true); + server.stateManager.registerDeleteHook('hooked', async finalValues => called = true); const _ = await server.stateManager.create('hooked'); assert.equal(called, false); @@ -935,7 +935,7 @@ describe(`# StateManager`, () => { server.stateManager.defineClass('hooked', hookSchema); let called = false; - server.stateManager.onDeleteHook('hooked', async finalValues => called = true); + server.stateManager.registerDeleteHook('hooked', async finalValues => called = true); const _ = await client.stateManager.create('hooked'); assert.equal(called, false); @@ -950,14 +950,14 @@ describe(`# StateManager`, () => { let called1 = false; let called2 = false; - server.stateManager.onDeleteHook('hooked', async finalValues => { + server.stateManager.registerDeleteHook('hooked', async finalValues => { await delay(20); called1 = true; assert.equal(finalValues.value, 0); return { value: 1 }; }); - server.stateManager.onDeleteHook('hooked', finalValues => { + server.stateManager.registerDeleteHook('hooked', finalValues => { assert.equal(finalValues.value, 1); called2 = true; }); @@ -974,12 +974,12 @@ describe(`# StateManager`, () => { let called1 = false; let called2 = false; - server.stateManager.onDeleteHook('hooked', async finalValues => { + server.stateManager.registerDeleteHook('hooked', async finalValues => { called1 = true; return null; }); - server.stateManager.onDeleteHook('hooked', finalValues => called2 = true); + server.stateManager.registerDeleteHook('hooked', finalValues => called2 = true); const state = await server.stateManager.create('hooked'); await state.delete(); // order is guaranteed @@ -993,11 +993,11 @@ describe(`# StateManager`, () => { let called1 = false; let called2 = false; - server.stateManager.onDeleteHook('hooked', async finalValues => { + server.stateManager.registerDeleteHook('hooked', async finalValues => { called1 = true; }); - server.stateManager.onDeleteHook('hooked', finalValues => { + server.stateManager.registerDeleteHook('hooked', finalValues => { assert.equal(finalValues.value, 42); called2 = true }); @@ -1009,7 +1009,7 @@ describe(`# StateManager`, () => { }); }); - describe('## [server] onUpdateHook(className, updateHook)', () => { + describe('## [server] registerUpdateHook(className, updateHook)', () => { const hookSchema = { name: { type: 'string', @@ -1028,7 +1028,7 @@ describe(`# StateManager`, () => { let errored = false; try { - server.stateManager.onUpdateHook('hooked', () => {}); + server.stateManager.registerUpdateHook('hooked', () => {}); } catch (err) { console.log(err.message); errored = true; @@ -1041,15 +1041,15 @@ describe(`# StateManager`, () => { server.stateManager.defineClass('hooked', hookSchema); let errored = false; - assert.throws(() => server.stateManager.onUpdateHook('hooked', null)); - assert.throws(() => server.stateManager.onUpdateHook('hooked', {})); - assert.doesNotThrow(() => server.stateManager.onUpdateHook('hooked', () => {})); - assert.doesNotThrow(() => server.stateManager.onUpdateHook('hooked', async () => {})); + assert.throws(() => server.stateManager.registerUpdateHook('hooked', null)); + assert.throws(() => server.stateManager.registerUpdateHook('hooked', {})); + assert.doesNotThrow(() => server.stateManager.registerUpdateHook('hooked', () => {})); + assert.doesNotThrow(() => server.stateManager.registerUpdateHook('hooked', async () => {})); }); it('should properly execute hook', async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { updates.value = `${updates.name}-value`; return updates; }); @@ -1097,7 +1097,7 @@ describe(`# StateManager`, () => { it('hook API should be `hook(updates, currentValues)`', async () => { return new Promise(async (resolve, reject) => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { try { assert.deepEqual(updates, { name: 'test' }); assert.deepEqual(currentValues, { name: null, value: null }); @@ -1119,7 +1119,7 @@ describe(`# StateManager`, () => { it('should not mess around when several states of same kind are created', async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { // just apply name to value updates.value = `${updates.name}-value`; return updates; @@ -1144,7 +1144,7 @@ describe(`# StateManager`, () => { it('should support asynchronous operation', async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onUpdateHook('hooked', async (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', async (updates, currentValues) => { return new Promise(resolve => { setTimeout(() => { resolve({ @@ -1171,11 +1171,11 @@ describe(`# StateManager`, () => { it('should apply several hooks in registation order', async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { return { ...updates, value: 'ok-1' }; }); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { return { ...updates, value: 'ok-2' }; }); @@ -1195,11 +1195,11 @@ describe(`# StateManager`, () => { it('should unregister hooks properly', async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { return { ...updates, value: 'ok-1' }; }); - const unregister = server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + const unregister = server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { return { ...updates, value: 'ok-2' }; }); @@ -1223,12 +1223,12 @@ describe(`# StateManager`, () => { it('should abort when explicitly returning `null`', async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { assert.ok('hook called'); return null; }); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { assert.fail('should not be called', updates); return { ...updates, value: 'ok-2' }; }); @@ -1257,7 +1257,7 @@ describe(`# StateManager`, () => { }, }); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { assert.ok('hook called'); return null; }); @@ -1279,7 +1279,7 @@ describe(`# StateManager`, () => { it('should continue when implicitly returning `undefined`', async () => { server.stateManager.defineClass('hooked', hookSchema); - server.stateManager.onUpdateHook('hooked', (updates, currentValues) => { + server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => { if (updates.name === 'test-1') { return { ...updates,