From af55c3d9f349abd77e0db2ca67f5445035fa322b Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Sun, 30 Jul 2023 14:57:43 +0300 Subject: [PATCH 1/4] Rename `Place` to `Code` --- lib/application.js | 12 ++++++------ lib/{place.js => code.js} | 4 ++-- test/application.js | 6 +++--- test/bus.js | 4 ++-- test/{place.js => code.js} | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) rename lib/{place.js => code.js} (97%) rename test/{place.js => code.js} (86%) diff --git a/lib/application.js b/lib/application.js index c7ff5eee..8c2b81d0 100644 --- a/lib/application.js +++ b/lib/application.js @@ -4,7 +4,7 @@ const { node, npm, metarhia, wt } = require('./deps.js'); const { MessageChannel, parentPort, threadId, workerData } = wt; const { Error, DomainError } = metarhia.metautil; const { Api } = require('./api.js'); -const { Place } = require('./place.js'); +const { Code } = require('./code.js'); const { Static } = require('./static.js'); const { Cert } = require('./cert.js'); const { Schemas } = require('./schemas.js'); @@ -52,10 +52,10 @@ class Application extends node.events.EventEmitter { this.cert = new Cert('cert', this, { ext: ['pem', 'domains'] }); this.resources = new Static('resources', this); this.api = new Api('api', this); - this.lib = new Place('lib', this); - this.db = new Place('db', this); - this.bus = new Place('bus', this); - this.domain = new Place('domain', this); + this.lib = new Code('lib', this); + this.db = new Code('db', this); + this.bus = new Code('bus', this); + this.domain = new Code('domain', this); this.starts = []; this.config = null; @@ -120,7 +120,7 @@ class Application extends node.events.EventEmitter { async stopPlace(name) { if (!this.sandbox) return; - const place = this.sandbox[name]; + const place = this[name]; for (const moduleName of Object.keys(place)) { const module = place[moduleName]; if (typeof module.stop === 'function') await this.execute(module.stop); diff --git a/lib/place.js b/lib/code.js similarity index 97% rename from lib/place.js rename to lib/code.js index bb9fecc9..02bac5b6 100644 --- a/lib/place.js +++ b/lib/code.js @@ -4,7 +4,7 @@ const { metarhia } = require('./deps.js'); const { Cache } = require('./cache.js'); const bus = require('./bus.js'); -class Place extends Cache { +class Code extends Cache { constructor(place, application) { super(place, application); this.tree = {}; @@ -70,4 +70,4 @@ class Place extends Cache { } } -module.exports = { Place }; +module.exports = { Code }; diff --git a/test/application.js b/test/application.js index 4548af9f..441cb1ad 100644 --- a/test/application.js +++ b/test/application.js @@ -20,9 +20,9 @@ metatests.test('lib/application', (test) => { test.strictSame(application.cert.constructor.name, 'Cert'); test.strictSame(application.resources.constructor.name, 'Static'); test.strictSame(application.api.constructor.name, 'Api'); - test.strictSame(application.lib.constructor.name, 'Place'); - test.strictSame(application.db.constructor.name, 'Place'); - test.strictSame(application.bus.constructor.name, 'Place'); + test.strictSame(application.lib.constructor.name, 'Code'); + test.strictSame(application.db.constructor.name, 'Code'); + test.strictSame(application.bus.constructor.name, 'Code'); test.strictSame(application.starts, []); test.strictSame(application.config, null); test.strictSame(application.logger, null); diff --git a/test/bus.js b/test/bus.js index 08366d2a..b347b4f1 100644 --- a/test/bus.js +++ b/test/bus.js @@ -2,7 +2,7 @@ const path = require('node:path'); const metatests = require('metatests'); -const { Place } = require('../lib/place.js'); +const { Code } = require('../lib/code.js'); const root = process.cwd(); @@ -17,7 +17,7 @@ const application = { }; metatests.testAsync('lib/bus', async (test) => { - const bus = new Place('bus', application); + const bus = new Code('bus', application); test.strictSame(bus.place, 'bus'); test.strictSame(bus.path, path.join(root, 'test/bus')); test.strictSame(typeof bus.application, 'object'); diff --git a/test/place.js b/test/code.js similarity index 86% rename from test/place.js rename to test/code.js index 5f9c1a4d..882b3f0e 100644 --- a/test/place.js +++ b/test/code.js @@ -2,7 +2,7 @@ const path = require('node:path'); const metatests = require('metatests'); -const { Place } = require('../lib/place.js'); +const { Code } = require('../lib/code.js'); const root = process.cwd(); @@ -16,8 +16,8 @@ const application = { }, }; -metatests.testAsync('lib/place', async (test) => { - const cache = new Place('cache', application); +metatests.testAsync('lib/code', async (test) => { + const cache = new Code('cache', application); test.strictSame(cache.place, 'cache'); test.strictSame(typeof cache.path, 'string'); test.strictSame(typeof cache.application, 'object'); From 6d22d393db2c0b9a53100e8815ff40c5777e7f85 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Sun, 30 Jul 2023 15:52:09 +0300 Subject: [PATCH 2/4] Rename `Cache` to `Place` --- lib/api.js | 4 +-- lib/code.js | 4 +-- lib/{cache.js => place.js} | 4 +-- lib/schemas.js | 4 +-- lib/static.js | 4 +-- test/cache.js | 20 +++++++------- test/code.js | 26 +++++++++---------- test/{cache => lib}/.eslintrc.json | 0 test/{cache => lib}/example/add.js | 0 test/{cache => lib}/example/cache.js | 0 test/{cache => lib}/example/doSomething.js | 0 test/{cache => lib}/example/start.js | 0 test/{cache => lib}/example/stop.js | 0 test/{cache => lib}/example/storage/set.js | 0 .../example/submodule1/method1.js | 0 .../example/submodule1/method2.js | 0 .../example/submodule2/method1.js | 0 .../example/submodule2/method2.js | 0 .../example/submodule2/nested1/method1.js | 0 .../example/submodule3/nested2/method1.js | 0 test/{cache => lib}/utils.js | 0 test/static.js | 2 +- types/core.d.ts | 10 ++++--- 23 files changed, 41 insertions(+), 37 deletions(-) rename lib/{cache.js => place.js} (95%) rename test/{cache => lib}/.eslintrc.json (100%) rename test/{cache => lib}/example/add.js (100%) rename test/{cache => lib}/example/cache.js (100%) rename test/{cache => lib}/example/doSomething.js (100%) rename test/{cache => lib}/example/start.js (100%) rename test/{cache => lib}/example/stop.js (100%) rename test/{cache => lib}/example/storage/set.js (100%) rename test/{cache => lib}/example/submodule1/method1.js (100%) rename test/{cache => lib}/example/submodule1/method2.js (100%) rename test/{cache => lib}/example/submodule2/method1.js (100%) rename test/{cache => lib}/example/submodule2/method2.js (100%) rename test/{cache => lib}/example/submodule2/nested1/method1.js (100%) rename test/{cache => lib}/example/submodule3/nested2/method1.js (100%) rename test/{cache => lib}/utils.js (100%) diff --git a/lib/api.js b/lib/api.js index bb2fedd4..524d0d12 100644 --- a/lib/api.js +++ b/lib/api.js @@ -2,9 +2,9 @@ const { node, npm, metarhia } = require('./deps.js'); const { Procedure } = require('./procedure.js'); -const { Cache } = require('./cache.js'); +const { Place } = require('./place.js'); -class Api extends Cache { +class Api extends Place { constructor(place, application) { super(place, application); this.collection = {}; diff --git a/lib/code.js b/lib/code.js index 02bac5b6..2cacb79d 100644 --- a/lib/code.js +++ b/lib/code.js @@ -1,10 +1,10 @@ 'use strict'; const { metarhia } = require('./deps.js'); -const { Cache } = require('./cache.js'); +const { Place } = require('./place.js'); const bus = require('./bus.js'); -class Code extends Cache { +class Code extends Place { constructor(place, application) { super(place, application); this.tree = {}; diff --git a/lib/cache.js b/lib/place.js similarity index 95% rename from lib/cache.js rename to lib/place.js index 6dc30a00..c672cefb 100644 --- a/lib/cache.js +++ b/lib/place.js @@ -2,7 +2,7 @@ const { node, metarhia } = require('./deps.js'); -class Cache { +class Place { constructor(place, application) { this.place = place; this.path = application.absolute(place); @@ -28,4 +28,4 @@ class Cache { } } -module.exports = { Cache }; +module.exports = { Place }; diff --git a/lib/schemas.js b/lib/schemas.js index c926dbe5..53005e10 100644 --- a/lib/schemas.js +++ b/lib/schemas.js @@ -1,9 +1,9 @@ 'use strict'; const { node, metarhia } = require('./deps.js'); -const { Cache } = require('./cache.js'); +const { Place } = require('./place.js'); -class Schemas extends Cache { +class Schemas extends Place { constructor(place, application) { super(place, application); this.model = null; diff --git a/lib/static.js b/lib/static.js index 4f64f8e1..f6751f08 100644 --- a/lib/static.js +++ b/lib/static.js @@ -1,12 +1,12 @@ 'use strict'; const { node, metarhia } = require('./deps.js'); -const { Cache } = require('./cache.js'); +const { Place } = require('./place.js'); const WIN = process.platform === 'win32'; const MAX_FILE_SIZE = '10 mb'; -class Static extends Cache { +class Static extends Place { constructor(place, application, options = {}) { super(place, application); this.files = new Map(); diff --git a/test/cache.js b/test/cache.js index dfd28a43..2dd5fbd2 100644 --- a/test/cache.js +++ b/test/cache.js @@ -2,7 +2,7 @@ const path = require('node:path'); const metatests = require('metatests'); -const { Cache } = require('../lib/cache.js'); +const { Place } = require('../lib/place.js'); const root = process.cwd(); @@ -16,26 +16,26 @@ const application = { }, }; -metatests.testAsync('lib/cache', async (test) => { +metatests.testAsync('lib/place', async (test) => { test.plan(17); - test.strictSame(typeof Cache, 'function'); - test.strictSame(Cache.name, 'Cache'); + test.strictSame(typeof Place, 'function'); + test.strictSame(Place.name, 'Place'); - class EmptyCache extends Cache { + class EmptyPlace extends Place { constructor(place, application) { super(place, application); this.empty = true; } async change(filePath) { - test.strictSame(this.constructor.name, 'EmptyCache'); + test.strictSame(this.constructor.name, 'EmptyPlace'); test.strictSame(typeof filePath, 'string'); } } - const cache = new EmptyCache('cache', application); - await cache.load(); - test.strictSame(cache.place, 'cache'); - test.strictSame(cache.empty, true); + const place = new EmptyPlace('lib', application); + await place.load(); + test.strictSame(place.place, 'lib'); + test.strictSame(place.empty, true); }); diff --git a/test/code.js b/test/code.js index 882b3f0e..dbcb8a40 100644 --- a/test/code.js +++ b/test/code.js @@ -17,18 +17,18 @@ const application = { }; metatests.testAsync('lib/code', async (test) => { - const cache = new Code('cache', application); - test.strictSame(cache.place, 'cache'); - test.strictSame(typeof cache.path, 'string'); - test.strictSame(typeof cache.application, 'object'); - test.strictSame(cache.tree, {}); - await cache.load(); - test.strictSame(Object.keys(cache.tree), ['example', 'utils']); - test.strictSame(cache.tree.example.parent, cache.tree); - test.strictSame(typeof cache.tree.example.add, 'object'); - test.strictSame(typeof cache.tree.example.doSomething, 'function'); - test.strictSame(typeof cache.tree.example.stop, 'function'); - test.strictSame(typeof cache.tree.example.start, 'function'); - test.strictSame(cache.tree.utils.UNITS.length, 9); + const code = new Code('lib', application); + test.strictSame(code.place, 'lib'); + test.strictSame(typeof code.path, 'string'); + test.strictSame(typeof code.application, 'object'); + test.strictSame(code.tree, {}); + await code.load(); + test.strictSame(Object.keys(code.tree), ['example', 'utils']); + test.strictSame(code.tree.example.parent, code.tree); + test.strictSame(typeof code.tree.example.add, 'object'); + test.strictSame(typeof code.tree.example.doSomething, 'function'); + test.strictSame(typeof code.tree.example.stop, 'function'); + test.strictSame(typeof code.tree.example.start, 'function'); + test.strictSame(code.tree.utils.UNITS.length, 9); test.end(); }); diff --git a/test/cache/.eslintrc.json b/test/lib/.eslintrc.json similarity index 100% rename from test/cache/.eslintrc.json rename to test/lib/.eslintrc.json diff --git a/test/cache/example/add.js b/test/lib/example/add.js similarity index 100% rename from test/cache/example/add.js rename to test/lib/example/add.js diff --git a/test/cache/example/cache.js b/test/lib/example/cache.js similarity index 100% rename from test/cache/example/cache.js rename to test/lib/example/cache.js diff --git a/test/cache/example/doSomething.js b/test/lib/example/doSomething.js similarity index 100% rename from test/cache/example/doSomething.js rename to test/lib/example/doSomething.js diff --git a/test/cache/example/start.js b/test/lib/example/start.js similarity index 100% rename from test/cache/example/start.js rename to test/lib/example/start.js diff --git a/test/cache/example/stop.js b/test/lib/example/stop.js similarity index 100% rename from test/cache/example/stop.js rename to test/lib/example/stop.js diff --git a/test/cache/example/storage/set.js b/test/lib/example/storage/set.js similarity index 100% rename from test/cache/example/storage/set.js rename to test/lib/example/storage/set.js diff --git a/test/cache/example/submodule1/method1.js b/test/lib/example/submodule1/method1.js similarity index 100% rename from test/cache/example/submodule1/method1.js rename to test/lib/example/submodule1/method1.js diff --git a/test/cache/example/submodule1/method2.js b/test/lib/example/submodule1/method2.js similarity index 100% rename from test/cache/example/submodule1/method2.js rename to test/lib/example/submodule1/method2.js diff --git a/test/cache/example/submodule2/method1.js b/test/lib/example/submodule2/method1.js similarity index 100% rename from test/cache/example/submodule2/method1.js rename to test/lib/example/submodule2/method1.js diff --git a/test/cache/example/submodule2/method2.js b/test/lib/example/submodule2/method2.js similarity index 100% rename from test/cache/example/submodule2/method2.js rename to test/lib/example/submodule2/method2.js diff --git a/test/cache/example/submodule2/nested1/method1.js b/test/lib/example/submodule2/nested1/method1.js similarity index 100% rename from test/cache/example/submodule2/nested1/method1.js rename to test/lib/example/submodule2/nested1/method1.js diff --git a/test/cache/example/submodule3/nested2/method1.js b/test/lib/example/submodule3/nested2/method1.js similarity index 100% rename from test/cache/example/submodule3/nested2/method1.js rename to test/lib/example/submodule3/nested2/method1.js diff --git a/test/cache/utils.js b/test/lib/utils.js similarity index 100% rename from test/cache/utils.js rename to test/lib/utils.js diff --git a/test/static.js b/test/static.js index 218fdf99..1de12c69 100644 --- a/test/static.js +++ b/test/static.js @@ -15,7 +15,7 @@ const application = { }; metatests.testAsync('lib/static load', async (test) => { - const cache = new Static('cache', application); + const cache = new Static('lib', application); test.strictSame(cache.files instanceof Map, true); test.strictSame(cache.files.size, 0); test.strictSame(cache.ext, undefined); diff --git a/types/core.d.ts b/types/core.d.ts index 7a72ab97..6a8af033 100644 --- a/types/core.d.ts +++ b/types/core.d.ts @@ -18,7 +18,11 @@ export interface InvokeTarget { args: object; } -export interface Cache { +export interface Static { + get(name: string): unknown; +} + +export interface Schemas { get(name: string): unknown; } @@ -29,8 +33,8 @@ export interface Listener { export interface Application extends EventEmitter { worker: { id: string }; server: { host: string; port: number; protocol: string }; - resources: Cache; - schemas: Cache; + resources: Static; + schemas: Schemas; scheduler: Scheduler; introspect: () => Promise; invoke: (target: InvokeTarget) => Promise; From cd4372e97969d7bea4db62818976712e7f86ea2d Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Sun, 30 Jul 2023 18:13:20 +0300 Subject: [PATCH 3/4] Rename `Place.place` to `Place.name` --- lib/api.js | 4 ++-- lib/cert.js | 4 ++-- lib/code.js | 8 ++++---- lib/place.js | 6 +++--- lib/schemas.js | 4 ++-- lib/static.js | 4 ++-- test/bus.js | 2 +- test/cache.js | 2 +- test/code.js | 2 +- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/api.js b/lib/api.js index 524d0d12..b28c5d69 100644 --- a/lib/api.js +++ b/lib/api.js @@ -5,8 +5,8 @@ const { Procedure } = require('./procedure.js'); const { Place } = require('./place.js'); class Api extends Place { - constructor(place, application) { - super(place, application); + constructor(name, application) { + super(name, application); this.collection = {}; this.signatures = {}; } diff --git a/lib/cert.js b/lib/cert.js index c66136a8..6938c2ca 100644 --- a/lib/cert.js +++ b/lib/cert.js @@ -4,8 +4,8 @@ const { node, metarhia, wt } = require('./deps.js'); const { Static } = require('./static.js'); class Cert extends Static { - constructor(place, application, options = {}) { - super(place, application, options); + constructor(name, application, options = {}) { + super(name, application, options); this.domains = new Map(); } diff --git a/lib/code.js b/lib/code.js index 2cacb79d..cb847215 100644 --- a/lib/code.js +++ b/lib/code.js @@ -5,8 +5,8 @@ const { Place } = require('./place.js'); const bus = require('./bus.js'); class Code extends Place { - constructor(place, application) { - super(place, application); + constructor(name, application) { + super(name, application); this.tree = {}; } @@ -55,12 +55,12 @@ class Code extends Place { async change(filePath) { if (!filePath.endsWith('.js')) return; - const { application, path, place } = this; + const { application, path, name } = this; const options = { context: application.sandbox, filename: filePath }; try { const { exports } = await metarhia.metavm.readScript(filePath, options); const relPath = filePath.substring(path.length + 1); - const exp = place === 'bus' ? bus.prepare(exports, application) : exports; + const exp = name === 'bus' ? bus.prepare(exports, application) : exports; this.set(relPath, exp); } catch (error) { if (error.code !== 'ENOENT') { diff --git a/lib/place.js b/lib/place.js index c672cefb..87ad89da 100644 --- a/lib/place.js +++ b/lib/place.js @@ -3,9 +3,9 @@ const { node, metarhia } = require('./deps.js'); class Place { - constructor(place, application) { - this.place = place; - this.path = application.absolute(place); + constructor(name, application) { + this.name = name; + this.path = application.absolute(name); this.application = application; } diff --git a/lib/schemas.js b/lib/schemas.js index 53005e10..055e643a 100644 --- a/lib/schemas.js +++ b/lib/schemas.js @@ -4,8 +4,8 @@ const { node, metarhia } = require('./deps.js'); const { Place } = require('./place.js'); class Schemas extends Place { - constructor(place, application) { - super(place, application); + constructor(name, application) { + super(name, application); this.model = null; } diff --git a/lib/static.js b/lib/static.js index f6751f08..a8371cf9 100644 --- a/lib/static.js +++ b/lib/static.js @@ -7,8 +7,8 @@ const WIN = process.platform === 'win32'; const MAX_FILE_SIZE = '10 mb'; class Static extends Place { - constructor(place, application, options = {}) { - super(place, application); + constructor(name, application, options = {}) { + super(name, application); this.files = new Map(); this.ext = options.ext; this.maxFileSize = -1; diff --git a/test/bus.js b/test/bus.js index b347b4f1..a5b46c05 100644 --- a/test/bus.js +++ b/test/bus.js @@ -18,7 +18,7 @@ const application = { metatests.testAsync('lib/bus', async (test) => { const bus = new Code('bus', application); - test.strictSame(bus.place, 'bus'); + test.strictSame(bus.name, 'bus'); test.strictSame(bus.path, path.join(root, 'test/bus')); test.strictSame(typeof bus.application, 'object'); test.strictSame(bus.tree, {}); diff --git a/test/cache.js b/test/cache.js index 2dd5fbd2..7017203a 100644 --- a/test/cache.js +++ b/test/cache.js @@ -36,6 +36,6 @@ metatests.testAsync('lib/place', async (test) => { const place = new EmptyPlace('lib', application); await place.load(); - test.strictSame(place.place, 'lib'); + test.strictSame(place.name, 'lib'); test.strictSame(place.empty, true); }); diff --git a/test/code.js b/test/code.js index dbcb8a40..66acf130 100644 --- a/test/code.js +++ b/test/code.js @@ -18,7 +18,7 @@ const application = { metatests.testAsync('lib/code', async (test) => { const code = new Code('lib', application); - test.strictSame(code.place, 'lib'); + test.strictSame(code.name, 'lib'); test.strictSame(typeof code.path, 'string'); test.strictSame(typeof code.application, 'object'); test.strictSame(code.tree, {}); From 1cc45ba2b5a3304f9f338a0f0463e47620b0575b Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Sun, 30 Jul 2023 19:06:40 +0300 Subject: [PATCH 4/4] Refactor `Code.stop` --- lib/application.js | 15 +++------------ lib/code.js | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/application.js b/lib/application.js index 8c2b81d0..f0d7561b 100644 --- a/lib/application.js +++ b/lib/application.js @@ -111,22 +111,13 @@ class Application extends node.events.EventEmitter { async shutdown() { this.finalization = true; - await this.stopPlace('domain'); - await this.stopPlace('db'); - await this.stopPlace('lib'); + await this.domain.stop(); + await this.db.stop(); + await this.lib.stop(); if (this.server) await this.server.close(); if (this.logger) await this.logger.close(); } - async stopPlace(name) { - if (!this.sandbox) return; - const place = this[name]; - for (const moduleName of Object.keys(place)) { - const module = place[moduleName]; - if (typeof module.stop === 'function') await this.execute(module.stop); - } - } - createSandbox() { const { config, console, resources, schemas } = this; const { server: { host, port, protocol } = {} } = this; diff --git a/lib/code.js b/lib/code.js index cb847215..9fd4c066 100644 --- a/lib/code.js +++ b/lib/code.js @@ -10,11 +10,20 @@ class Code extends Place { this.tree = {}; } - stop(name, method) { + async stop() { + for (const moduleName of Object.keys(this.tree)) { + const module = this.tree[moduleName]; + if (typeof module.stop === 'function') { + await this.application.execute(module.stop); + } + } + } + + stopModule(name, module) { const timeout = this.application.config.server.timeouts.watch; setTimeout(() => { if (this.tree[name] !== undefined) return; - this.application.execute(method); + this.application.execute(module.stop); }, timeout); } @@ -27,7 +36,7 @@ class Code extends Place { let next = level[name]; if (depth === last) { if (unit === null) { - if (name === 'stop') this.stop(names[0], level.stop); + if (name === 'stop') this.stopModule(names[0], level); delete level[name]; return; }