From 8cb3aae5af4c8e57ebcbc51b9a11425baac14952 Mon Sep 17 00:00:00 2001 From: Peter van Vliet Date: Thu, 15 Aug 2024 14:51:54 +0200 Subject: [PATCH] #296: make it work --- examples/concepts/hello-world/package.json | 2 +- .../hello-world/services/standalone.json | 2 +- examples/concepts/segmentation/package.json | 10 +-- .../segmentation/services/standalone.json | 2 +- .../caching/src/application/ModuleBuilder.ts | 14 +-- .../application/ClassSourceBuilder.spec.ts | 0 .../test/application/ImportRewriter.spec.ts | 23 +++++ .../application/LocalModuleBuilder.spec.ts | 0 .../application/RemoteModuleBuilder.spec.ts | 0 packages/caching/test/fixtures/application.ts | 9 ++ packages/caching/test/fixtures/codes.ts | 61 +++++++++++++ packages/caching/test/fixtures/index.ts | 9 ++ packages/caching/test/fixtures/modules.ts | 21 +++++ packages/caching/test/fixtures/repository.ts | 8 ++ .../caching/test/fixtures/segmentModules.ts | 17 ++++ .../test/fixtures/segmentProcedures.ts | 17 ++++ .../caching/test/fixtures/segmentation.ts | 8 ++ packages/caching/test/fixtures/segments.ts | 13 +++ packages/jitar/src/client.ts | 1 - packages/runtime/src/RuntimeBuilder.ts | 6 +- packages/runtime/src/client.ts | 4 +- .../runtime/src/definitions/ExecutionScope.ts | 18 ++-- packages/runtime/src/globals.ts | 4 +- packages/runtime/src/hooks.ts | 18 ---- packages/runtime/src/lib.ts | 2 - packages/runtime/src/models/Import.ts | 36 ++++---- .../runtime/src/services/DummyRepository.ts | 5 +- packages/runtime/src/services/LocalGateway.ts | 5 +- .../runtime/src/services/LocalRepository.ts | 87 ++----------------- packages/runtime/src/services/LocalWorker.ts | 11 +-- .../runtime/src/services/ProcedureRuntime.ts | 24 ++--- packages/runtime/src/services/Proxy.ts | 13 +-- .../runtime/src/services/RemoteGateway.ts | 3 +- .../runtime/src/services/RemoteRepository.ts | 10 +-- packages/runtime/src/services/RemoteWorker.ts | 3 +- packages/runtime/src/services/Repository.ts | 16 +--- packages/runtime/src/services/Runtime.ts | 7 +- packages/runtime/src/services/Standalone.ts | 11 +-- packages/runtime/src/utils/FileHelper.ts | 2 +- packages/runtime/src/utils/ModuleLoader.ts | 42 +-------- .../runtime/src/utils/RemoteClassLoader.ts | 6 +- packages/server-nodejs/src/JitarServer.ts | 10 +-- .../src/controllers/ModulesController.ts | 18 ++-- 43 files changed, 287 insertions(+), 291 deletions(-) create mode 100644 packages/caching/test/application/ClassSourceBuilder.spec.ts create mode 100644 packages/caching/test/application/ImportRewriter.spec.ts create mode 100644 packages/caching/test/application/LocalModuleBuilder.spec.ts create mode 100644 packages/caching/test/application/RemoteModuleBuilder.spec.ts create mode 100644 packages/caching/test/fixtures/application.ts create mode 100644 packages/caching/test/fixtures/codes.ts create mode 100644 packages/caching/test/fixtures/index.ts create mode 100644 packages/caching/test/fixtures/modules.ts create mode 100644 packages/caching/test/fixtures/repository.ts create mode 100644 packages/caching/test/fixtures/segmentModules.ts create mode 100644 packages/caching/test/fixtures/segmentProcedures.ts create mode 100644 packages/caching/test/fixtures/segmentation.ts create mode 100644 packages/caching/test/fixtures/segments.ts diff --git a/examples/concepts/hello-world/package.json b/examples/concepts/hello-world/package.json index 5549043e..026c7693 100644 --- a/examples/concepts/hello-world/package.json +++ b/examples/concepts/hello-world/package.json @@ -4,7 +4,7 @@ "type": "module", "scripts": { "build": "tsc", - "standalone": "node --experimental-network-imports dist/jitar.js --config=services/standalone.json" + "standalone": "node .jitar/jitar.shared.js --config=services/standalone.json" }, "dependencies": { "jitar": "*" diff --git a/examples/concepts/hello-world/services/standalone.json b/examples/concepts/hello-world/services/standalone.json index 9117bcfa..538f071c 100644 --- a/examples/concepts/hello-world/services/standalone.json +++ b/examples/concepts/hello-world/services/standalone.json @@ -2,6 +2,6 @@ "url": "http://127.0.0.1:3000", "standalone": { - + "segments": ["default"] } } \ No newline at end of file diff --git a/examples/concepts/segmentation/package.json b/examples/concepts/segmentation/package.json index ad204f92..94800042 100644 --- a/examples/concepts/segmentation/package.json +++ b/examples/concepts/segmentation/package.json @@ -4,11 +4,11 @@ "private": true, "scripts": { "build": "tsc", - "standalone": "node --experimental-network-imports dist/jitar.js --config=services/standalone.json", - "repo": "node --experimental-network-imports dist/jitar.js --config=services/repository.json", - "gateway": "node --experimental-network-imports dist/jitar.js --config=services/gateway.json", - "worker-data": "node --experimental-network-imports dist/jitar.js --config=services/data.json", - "worker-process": "node --experimental-network-imports dist/jitar.js --config=services/process.json" + "standalone": "node .jitar/jitar.shared.js --config=services/standalone.json", + "repo": "node .jitar/jitar.shared.js --config=services/repository.json", + "gateway": "node .jitar/jitar.shared.js --config=services/gateway.json", + "worker-data": "node .jitar/jitar.shared.js --config=services/data.json", + "worker-process": "node .jitar/jitar.shared.js --config=services/process.json" }, "dependencies": { "jitar": "*" diff --git a/examples/concepts/segmentation/services/standalone.json b/examples/concepts/segmentation/services/standalone.json index 9117bcfa..4f9723c9 100644 --- a/examples/concepts/segmentation/services/standalone.json +++ b/examples/concepts/segmentation/services/standalone.json @@ -2,6 +2,6 @@ "url": "http://127.0.0.1:3000", "standalone": { - + "segments": [ "process", "data" ] } } \ No newline at end of file diff --git a/packages/caching/src/application/ModuleBuilder.ts b/packages/caching/src/application/ModuleBuilder.ts index 08852706..0ecd6db8 100644 --- a/packages/caching/src/application/ModuleBuilder.ts +++ b/packages/caching/src/application/ModuleBuilder.ts @@ -42,15 +42,17 @@ export default class ModuleBuilder if (moduleSegments.length === 0) { - return this.#buildSharedModule(module, segmentation); + await this.#buildSharedModule(module, segmentation); } + else + { + // Otherwise, it is a segment module that can be called remotely - // Otherwise, it is a segment module that can be called remotely - - const segmentBuilds = moduleSegments.map(segment => this.#buildSegmentModule(module, segment, segmentation)); - const remoteBuild = this.#buildRemoteModule(module, moduleSegments); + const segmentBuilds = moduleSegments.map(segment => this.#buildSegmentModule(module, segment, segmentation)); + const remoteBuild = this.#buildRemoteModule(module, moduleSegments); - await Promise.all([...segmentBuilds, remoteBuild]); + await Promise.all([...segmentBuilds, remoteBuild]); + } this.#fileManager.delete(module.filename); } diff --git a/packages/caching/test/application/ClassSourceBuilder.spec.ts b/packages/caching/test/application/ClassSourceBuilder.spec.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/caching/test/application/ImportRewriter.spec.ts b/packages/caching/test/application/ImportRewriter.spec.ts new file mode 100644 index 00000000..3bc9340a --- /dev/null +++ b/packages/caching/test/application/ImportRewriter.spec.ts @@ -0,0 +1,23 @@ + +import { describe, expect, it } from 'vitest'; + +import { MODULES, SEGMENTATION, SEGMENTS } from '../fixtures'; + +import ImportRewriter from '../../src/application/ImportRewriter'; + +describe('application/ImportRewriter', () => +{ + describe('.rewrite()', () => + { + it('should ...', () => + { + const rewriter = new ImportRewriter(MODULES.C, SEGMENTATION, SEGMENTS.FIRST); + + const code = rewriter.rewrite(); + + console.log(code); + + expect(true).toBe(true); + }); + }); +}); diff --git a/packages/caching/test/application/LocalModuleBuilder.spec.ts b/packages/caching/test/application/LocalModuleBuilder.spec.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/caching/test/application/RemoteModuleBuilder.spec.ts b/packages/caching/test/application/RemoteModuleBuilder.spec.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/caching/test/fixtures/application.ts b/packages/caching/test/fixtures/application.ts new file mode 100644 index 00000000..714bab0c --- /dev/null +++ b/packages/caching/test/fixtures/application.ts @@ -0,0 +1,9 @@ + +import Application from '../../src/application/models/Application'; + +import { REPOSITORY } from './repository'; +import { SEGMENTATION } from './segmentation'; + +const APPLICATION = new Application(REPOSITORY, SEGMENTATION); + +export { APPLICATION }; diff --git a/packages/caching/test/fixtures/codes.ts b/packages/caching/test/fixtures/codes.ts new file mode 100644 index 00000000..266c6edb --- /dev/null +++ b/packages/caching/test/fixtures/codes.ts @@ -0,0 +1,61 @@ + +const A = +` +import b from './b'; + +export default async function a() { + await b(); +} +`; + +const B = +` +export default async function b() { } +`; + +const C = +` +import D from '../shared/d'; +import e from '../second/e'; + +export default async function c() { + await e(); +} +`; + +const D = +` +export default class D {} +`; + +const E = +` +import D from '../shared/d'; +import f from './f'; + +export default async function e() { + await f(); +} +`; + +const F = +` +import g, { h } from './g'; + +export default async function f() { + await g(); +} +`; + +const G = +` +export default async function g() { } + +export async function h() { } + +export class i {} +`; + +const CODES = { A, B, C, D, E, F, G }; + +export { CODES }; diff --git a/packages/caching/test/fixtures/index.ts b/packages/caching/test/fixtures/index.ts new file mode 100644 index 00000000..2629d975 --- /dev/null +++ b/packages/caching/test/fixtures/index.ts @@ -0,0 +1,9 @@ + +export * from './application'; +export * from './codes'; +export * from './modules'; +export * from './repository'; +export * from './segmentation'; +export * from './segmentModules'; +export * from './segmentProcedures'; +export * from './segments'; diff --git a/packages/caching/test/fixtures/modules.ts b/packages/caching/test/fixtures/modules.ts new file mode 100644 index 00000000..f9ee8e6f --- /dev/null +++ b/packages/caching/test/fixtures/modules.ts @@ -0,0 +1,21 @@ + +import { Reflector } from '@jitar/reflection'; + +import Module from '../../src/module/models/Module'; + +import { CODES } from './codes'; + +const reflector = new Reflector(); + +const MODULES = +{ + A: new Module('domain/first/a.js', CODES.A, reflector.parse(CODES.A)), + B: new Module('domain/first/b.js', CODES.B, reflector.parse(CODES.B)), + C: new Module('domain/first/c.js', CODES.C, reflector.parse(CODES.C)), + D: new Module('domain/shared/d.js', CODES.D, reflector.parse(CODES.D)), + E: new Module('domain/second/e.js', CODES.E, reflector.parse(CODES.E)), + F: new Module('domain/second/f.js', CODES.F, reflector.parse(CODES.F)), + G: new Module('domain/second/g.js', CODES.G, reflector.parse(CODES.G)) +}; + +export { MODULES }; diff --git a/packages/caching/test/fixtures/repository.ts b/packages/caching/test/fixtures/repository.ts new file mode 100644 index 00000000..14cdb76b --- /dev/null +++ b/packages/caching/test/fixtures/repository.ts @@ -0,0 +1,8 @@ + +import Repository from '../../src/module/models/Repository'; + +import { MODULES } from './modules'; + +const REPOSITORY = new Repository(Object.values(MODULES)); + +export { REPOSITORY }; diff --git a/packages/caching/test/fixtures/segmentModules.ts b/packages/caching/test/fixtures/segmentModules.ts new file mode 100644 index 00000000..1ff9fa39 --- /dev/null +++ b/packages/caching/test/fixtures/segmentModules.ts @@ -0,0 +1,17 @@ + +import Module from '../../src/segment/models/Module'; + +const SEGMENT_MODULES = +{ + // First segment + A: new Module('domain/first/a.js', 'domain/first', { 'default': { 'access': 'public' } }), + B: new Module('domain/first/b.js', 'domain/first', { 'default': { 'access': 'private' } }), + C: new Module('domain/first/c.js', 'domain/first', { 'default': { 'access': 'protected' } }), + + // Second segment + E: new Module('domain/second/e.js', 'domain/second', { 'default': { 'access': 'public' } }), + F: new Module('domain/second/f.js', 'domain/second', { 'default': { 'access': 'private' } }), + G: new Module('domain/second/g.js', 'domain/second', { 'default': { 'access': 'private' } }) +}; + +export { SEGMENT_MODULES }; diff --git a/packages/caching/test/fixtures/segmentProcedures.ts b/packages/caching/test/fixtures/segmentProcedures.ts new file mode 100644 index 00000000..c06f02b1 --- /dev/null +++ b/packages/caching/test/fixtures/segmentProcedures.ts @@ -0,0 +1,17 @@ + +import Procedure from '../../src/segment/models/Procedure'; + +const SEGMENT_PROCEDURES = +{ + // First segment + A: new Procedure('domain/first/a', []), + B: new Procedure('domain/first/b', []), + C: new Procedure('domain/first/c', []), + + // Second segment + E: new Procedure('domain/second/e', []), + F: new Procedure('domain/second/f', []), + G: new Procedure('domain/second/g', []) +}; + +export { SEGMENT_PROCEDURES }; diff --git a/packages/caching/test/fixtures/segmentation.ts b/packages/caching/test/fixtures/segmentation.ts new file mode 100644 index 00000000..5a09b373 --- /dev/null +++ b/packages/caching/test/fixtures/segmentation.ts @@ -0,0 +1,8 @@ + +import Segmentation from '../../src/segment/models/Segmentation'; + +import { SEGMENTS } from './segments'; + +const SEGMENTATION = new Segmentation([SEGMENTS.FIRST, SEGMENTS.SECOND]); + +export { SEGMENTATION }; diff --git a/packages/caching/test/fixtures/segments.ts b/packages/caching/test/fixtures/segments.ts new file mode 100644 index 00000000..6a056c69 --- /dev/null +++ b/packages/caching/test/fixtures/segments.ts @@ -0,0 +1,13 @@ + +import Segment from '../../src/segment/models/Segment'; + +import { SEGMENT_MODULES as MODULES } from './segmentModules'; +import { SEGMENT_PROCEDURES as PROCEDURES } from './segmentProcedures'; + +const SEGMENTS = +{ + FIRST: new Segment('first', [MODULES.A, MODULES.B, MODULES.C], [PROCEDURES.A, PROCEDURES.B, PROCEDURES.C]), + SECOND: new Segment('second', [MODULES.E, MODULES.F, MODULES.G], [PROCEDURES.E, PROCEDURES.F, PROCEDURES.G]), +}; + +export { SEGMENTS }; diff --git a/packages/jitar/src/client.ts b/packages/jitar/src/client.ts index 402bf5c8..efdad152 100644 --- a/packages/jitar/src/client.ts +++ b/packages/jitar/src/client.ts @@ -13,7 +13,6 @@ export NamedParameter, ArrayParameter, ObjectParameter, - Import, BadRequest, Forbidden, NotFound, diff --git a/packages/runtime/src/RuntimeBuilder.ts b/packages/runtime/src/RuntimeBuilder.ts index c41cb1bf..57bee4fb 100644 --- a/packages/runtime/src/RuntimeBuilder.ts +++ b/packages/runtime/src/RuntimeBuilder.ts @@ -128,7 +128,7 @@ export default class RuntimeBuilder throw new RuntimeNotBuilt('Repository is not set for the gateway'); } - const gateway = new LocalGateway(this.#repository, this.#url, trustKey); + const gateway = new LocalGateway(this.#url, trustKey); gateway.healthCheckFiles = this.#healthChecks; gateway.middlewareFiles = this.#middlewares; @@ -142,7 +142,7 @@ export default class RuntimeBuilder throw new RuntimeNotBuilt('Repository is not set for the worker'); } - const worker = new LocalWorker(this.#repository, this.#gateway, this.#url, trustKey); + const worker = new LocalWorker(this.#gateway, this.#url, trustKey); worker.segmentNames = this.#segments; worker.healthCheckFiles = this.#healthChecks; worker.middlewareFiles = this.#middlewares; @@ -190,7 +190,7 @@ export default class RuntimeBuilder repository.assets = this.#assets; repository.overrides = this.#overrides; - const worker = new LocalWorker(repository, this.#gateway, this.#url, trustKey); + const worker = new LocalWorker(this.#gateway, this.#url, trustKey); worker.segmentNames = this.#segments; const standalone = new Standalone(repository, worker, this.#url); diff --git a/packages/runtime/src/client.ts b/packages/runtime/src/client.ts index c3c4d956..31e56429 100644 --- a/packages/runtime/src/client.ts +++ b/packages/runtime/src/client.ts @@ -1,17 +1,15 @@ import LocalWorker from './services/LocalWorker.js'; import RemoteGateway from './services/RemoteGateway.js'; -import RemoteRepository from './services/RemoteRepository.js'; let client: LocalWorker | undefined = undefined; const resolvers: ((client: LocalWorker) => void)[] = []; export async function startClient(remoteUrl: string, segmentNames: string[] = [], middlewares: string[] = []): Promise { - const repository = new RemoteRepository(remoteUrl); const gateway = new RemoteGateway(remoteUrl); - const worker = new LocalWorker(repository, gateway); + const worker = new LocalWorker(gateway); worker.segmentNames = new Set(segmentNames); worker.middlewareFiles = new Set(middlewares); diff --git a/packages/runtime/src/definitions/ExecutionScope.ts b/packages/runtime/src/definitions/ExecutionScope.ts index c9133170..a92f49e4 100644 --- a/packages/runtime/src/definitions/ExecutionScope.ts +++ b/packages/runtime/src/definitions/ExecutionScope.ts @@ -1,13 +1,13 @@ -const ExecutionScopes = -{ - APPLICATION: 'application', - RUNTIME: 'runtime' -} as const; +// const ExecutionScopes = +// { +// APPLICATION: 'application', +// RUNTIME: 'runtime' +// } as const; -Object.freeze(ExecutionScopes); +// Object.freeze(ExecutionScopes); -type Keys = keyof typeof ExecutionScopes; -type ExecutionScope = typeof ExecutionScopes[Keys]; +// type Keys = keyof typeof ExecutionScopes; +// type ExecutionScope = typeof ExecutionScopes[Keys]; -export { ExecutionScope, ExecutionScopes }; +// export { ExecutionScope, ExecutionScopes }; diff --git a/packages/runtime/src/globals.ts b/packages/runtime/src/globals.ts index 5216c1ff..c5bc044c 100644 --- a/packages/runtime/src/globals.ts +++ b/packages/runtime/src/globals.ts @@ -1,18 +1,16 @@ import ProcedureNotAccessible from './errors/ProcedureNotAccessible.js'; -import { importModule, runProcedure } from './hooks.js'; +import { runProcedure } from './hooks.js'; declare global { - const __import: typeof importModule; const __run: typeof runProcedure; } export const globals = globalThis as Record; // Available for external use -globals.__import = importModule; globals.__run = runProcedure; // Internal use only diff --git a/packages/runtime/src/hooks.ts b/packages/runtime/src/hooks.ts index 7e32a0b5..ed0966d6 100644 --- a/packages/runtime/src/hooks.ts +++ b/packages/runtime/src/hooks.ts @@ -1,11 +1,8 @@ import RuntimeNotAvailable from './errors/RuntimeNotAvailable.js'; -import Import from './models/Import.js'; import Request from './models/Request.js'; import ProcedureRuntime from './services/ProcedureRuntime.js'; import VersionParser from './utils/VersionParser.js'; -import Environment from './utils/Environment.js'; -import { ExecutionScope } from './lib.js'; let _runtime: ProcedureRuntime; @@ -24,21 +21,6 @@ export function getRuntime(): ProcedureRuntime return _runtime; } -export async function importModule(caller: string, specifier: string, executionScope: ExecutionScope, extractDefault: boolean): Promise -{ - const runtime = getRuntime(); - - if (Environment.isBrowser() && specifier === 'JITAR_LIBRARY_NAME') - { - specifier = 'RUNTIME_HOOKS_LOCATION'; - } - - const importModel = new Import(caller, specifier, executionScope, extractDefault); - const module = await runtime.import(importModel); - - return importModel.extractDefault && module.default !== undefined ? module.default : module; -} - export async function runProcedure(fqn: string, versionNumber: string, args: object, sourceRequest?: Request): Promise { const runtime = getRuntime(); diff --git a/packages/runtime/src/lib.ts b/packages/runtime/src/lib.ts index 34ac0633..536f8b58 100644 --- a/packages/runtime/src/lib.ts +++ b/packages/runtime/src/lib.ts @@ -2,7 +2,6 @@ // Definitions export * from './definitions/AccessLevel.js'; export * from './definitions/Files.js'; -export * from './definitions/ExecutionScope.js'; // Generic errors export { default as BadRequest } from './errors/generic/BadRequest.js'; @@ -40,7 +39,6 @@ export { default as Middleware } from './interfaces/Middleware.js'; export { default as ArrayParameter } from './models/ArrayParameter.js'; export { default as File } from './models/File.js'; export { default as Implementation } from './models/Implementation.js'; -export { default as Import } from './models/Import.js'; export { default as NamedParameter } from './models/NamedParameter.js'; export { default as ObjectParameter } from './models/ObjectParameter.js'; export { default as Procedure } from './models/Procedure.js'; diff --git a/packages/runtime/src/models/Import.ts b/packages/runtime/src/models/Import.ts index e293540a..9cf23b98 100644 --- a/packages/runtime/src/models/Import.ts +++ b/packages/runtime/src/models/Import.ts @@ -1,26 +1,22 @@ -import { ExecutionScope } from '../definitions/ExecutionScope.js'; +// import { ExecutionScope } from '../definitions/ExecutionScope.js'; -export default class Import -{ - #caller: string; - #specifier: string; - #scope: ExecutionScope; - #extractDefault: boolean; +// export default class Import +// { +// #specifier: string; +// #scope: ExecutionScope; +// #extractDefault: boolean; - constructor(caller: string, specifier: string, scope: ExecutionScope, extractDefault: boolean = true) - { - this.#caller = caller; - this.#specifier = specifier; - this.#scope = scope; - this.#extractDefault = extractDefault; - } +// constructor(specifier: string, scope: ExecutionScope, extractDefault: boolean = true) +// { +// this.#specifier = specifier; +// this.#scope = scope; +// this.#extractDefault = extractDefault; +// } - get caller() { return this.#caller; } +// get specifier() { return this.#specifier; } - get specifier() { return this.#specifier; } +// get scope() { return this.#scope; } - get scope() { return this.#scope; } - - get extractDefault() { return this.#extractDefault; } -} +// get extractDefault() { return this.#extractDefault; } +// } diff --git a/packages/runtime/src/services/DummyRepository.ts b/packages/runtime/src/services/DummyRepository.ts index ed206dec..6558c1d3 100644 --- a/packages/runtime/src/services/DummyRepository.ts +++ b/packages/runtime/src/services/DummyRepository.ts @@ -2,7 +2,6 @@ import RepositoryNotAvailable from '../errors/RepositoryNotAvailable.js'; import File from '../models/File.js'; -import Import from '../models/Import.js'; import Module from '../types/Module.js'; import Repository from './Repository.js'; @@ -20,13 +19,13 @@ export default class DummyRepository extends Repository } // eslint-disable-next-line @typescript-eslint/no-unused-vars - async readModule(importModel: Import): Promise + async readModule(specifier: string): Promise { throw new RepositoryNotAvailable(); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - async loadModule(importModel: Import): Promise + async loadModule(specifier: string): Promise { throw new RepositoryNotAvailable(); } diff --git a/packages/runtime/src/services/LocalGateway.ts b/packages/runtime/src/services/LocalGateway.ts index 0802db28..1f7ddfeb 100644 --- a/packages/runtime/src/services/LocalGateway.ts +++ b/packages/runtime/src/services/LocalGateway.ts @@ -8,7 +8,6 @@ import Response from '../models/Response.js'; import Gateway from './Gateway.js'; import Worker from './Worker.js'; import WorkerBalancer from './WorkerBalancer.js'; -import Repository from './Repository.js'; export default class LocalGateway extends Gateway { @@ -16,9 +15,9 @@ export default class LocalGateway extends Gateway #balancers: Map = new Map(); #trustKey?: string; - constructor(repository: Repository, url?: string, trustKey?: string) + constructor(url?: string, trustKey?: string) { - super(repository, url); + super(url); this.#trustKey = trustKey; } diff --git a/packages/runtime/src/services/LocalRepository.ts b/packages/runtime/src/services/LocalRepository.ts index f32098d3..fe948423 100644 --- a/packages/runtime/src/services/LocalRepository.ts +++ b/packages/runtime/src/services/LocalRepository.ts @@ -2,10 +2,8 @@ import FileNotFound from '../errors/FileNotFound.js'; import InvalidSegmentFile from '../errors/InvalidSegmentFile.js'; -import { ExecutionScopes } from '../definitions/ExecutionScope.js'; import FileManager from '../interfaces/FileManager.js'; import File from '../models/File.js'; -import Import from '../models/Import.js'; import Module from '../types/Module.js'; import FileHelper from '../utils/FileHelper.js'; import ModuleLoader from '../utils/ModuleLoader.js'; @@ -74,8 +72,7 @@ export default class LocalRepository extends Repository const relativeFilename = FileHelper.createRepositoryFilename(name); const absoluteFilename = this.#fileManager.getAbsoluteLocation(relativeFilename); - const importModel = new Import('', absoluteFilename, ExecutionScopes.APPLICATION); - const module = await ModuleLoader.load(importModel); + const module = await ModuleLoader.load(absoluteFilename); const files = module.files as string[]; if (files === undefined) @@ -124,47 +121,8 @@ export default class LocalRepository extends Repository return this.#readFile(filename); } - readModule(importModel: Import): Promise + async readModule(specifier: string): Promise { - const { specifier, caller } = importModel; - - console.log('CALLER', caller); - - if (FileHelper.isSegmentFilename(caller)) - { - return this.#readWorkerModule(specifier); - } - - if (FileHelper.isSegmentFilename(specifier)) - { - return this.#readWorkerModule(specifier); - } - - if (this.#isSegmented(specifier) === false) - { - console.log('NOT SEGMENTED', specifier); - return this.#readWorkerModule(specifier); - } - - console.log('IS SEGMENTED', specifier); - - return this.#inSameSegment(caller, specifier) - ? this.#readWorkerModule(specifier) - : this.#readRemoteModule(specifier); - } - - loadModule(importModel: Import): Promise - { - const filename = this.#getModuleFilename(importModel.specifier); - const fileImportModel = new Import(importModel.caller, filename, importModel.scope); - - return ModuleLoader.load(fileImportModel); - } - - async #readWorkerModule(specifier: string): Promise - { - console.log('WORKER MODULE', specifier); - const filename = this.#getModuleFilename(specifier); const file = await this.#readFile(filename); const code = file.content.toString(); @@ -172,14 +130,11 @@ export default class LocalRepository extends Repository return new File(filename, 'application/javascript', code); } - #readRemoteModule(specifier: string): Promise + loadModule(specifier: string): Promise { - console.log('REMOTE MODULE', specifier); - - const relativeFilename = FileHelper.assureRelativeFilenameWithExtension(specifier); - const remoteFilename = FileHelper.convertToRemoteFilename(relativeFilename); + const filename = this.#getModuleFilename(specifier); - return this.#readFile(remoteFilename); + return ModuleLoader.load(filename); } #getModuleFilename(filename: string): string @@ -206,36 +161,4 @@ export default class LocalRepository extends Repository { return this.#fileManager.read(filename); } - - #inSameSegment(firstFilename: string, secondFilename: string): boolean - { - const firstSegments = this.#filterSegments(firstFilename); - const secondSegments = this.#filterSegments(secondFilename); - - return firstSegments.some(segmentName => secondSegments.includes(segmentName)); - } - - #filterSegments(filename: string): string[] - { - const segmentNames = [...this.#segments.keys()]; - - return segmentNames.filter(segmentName => - { - const filenames = this.#segments.get(segmentName) ?? []; - - return filenames.includes(filename); - }); - } - - #isSegmented(filename: string): boolean - { - const segmentNames = [...this.#segments.keys()]; - - return segmentNames.some(segmentName => - { - const filenames = this.#segments.get(segmentName) ?? []; - - return filenames.includes(filename); - }); - } } diff --git a/packages/runtime/src/services/LocalWorker.ts b/packages/runtime/src/services/LocalWorker.ts index bcfe776a..1cdf488e 100644 --- a/packages/runtime/src/services/LocalWorker.ts +++ b/packages/runtime/src/services/LocalWorker.ts @@ -1,12 +1,9 @@ -import { ExecutionScopes } from '../definitions/ExecutionScope.js'; - import Unauthorized from '../errors/generic/Unauthorized.js'; import ImplementationNotFound from '../errors/ImplementationNotFound.js'; import ProcedureNotFound from '../errors/ProcedureNotFound.js'; import InvalidTrustKey from '../errors/InvalidTrustKey.js'; -import Import from '../models/Import.js'; import Procedure from '../models/Procedure.js'; import Request from '../models/Request.js'; import Response from '../models/Response.js'; @@ -16,7 +13,6 @@ import FileHelper from '../utils/FileHelper.js'; import Gateway from './Gateway.js'; import Worker from './Worker.js'; -import Repository from './Repository.js'; import { setRuntime } from '../hooks.js'; @@ -31,9 +27,9 @@ export default class LocalWorker extends Worker #segmentNames: Set = new Set(); #segments: Map = new Map(); - constructor(repository: Repository, gateway?: Gateway, url?: string, trustKey?: string, argumentConstructor = new ArgumentConstructor()) + constructor(gateway?: Gateway, url?: string, trustKey?: string, argumentConstructor = new ArgumentConstructor()) { - super(repository, url); + super(url); this.#gateway = gateway; this.#trustKey = trustKey; @@ -122,8 +118,7 @@ export default class LocalWorker extends Worker async #loadSegment(name: string): Promise { const filename = FileHelper.createWorkerFilename(name); - const importModel = new Import('', filename, ExecutionScopes.APPLICATION); - const module = await this.import(importModel); + const module = await this.import(filename); const segment = module.segment as Segment; this.addSegment(segment); diff --git a/packages/runtime/src/services/ProcedureRuntime.ts b/packages/runtime/src/services/ProcedureRuntime.ts index d1483e2d..50e55cea 100644 --- a/packages/runtime/src/services/ProcedureRuntime.ts +++ b/packages/runtime/src/services/ProcedureRuntime.ts @@ -1,11 +1,8 @@ -import { ExecutionScopes } from '../definitions/ExecutionScope.js'; - import InvalidMiddleware from '../errors/InvalidMiddleware.js'; import Middleware from '../interfaces/Middleware.js'; import Runner from '../interfaces/Runner.js'; -import Import from '../models/Import.js'; import Request from '../models/Request.js'; import Response from '../models/Response.js'; @@ -13,27 +10,23 @@ import Response from '../models/Response.js'; import Module from '../types/Module.js'; import NextHandler from '../types/NextHandler.js'; +import ModuleLoader from '../utils/ModuleLoader.js'; + import ProcedureRunner from './ProcedureRunner.js'; -import Repository from './Repository.js'; import Runtime from './Runtime.js'; export default abstract class ProcedureRuntime extends Runtime implements Runner { - #repository: Repository; #middlewareFiles: Set = new Set(); #middlewares: Middleware[] = []; - constructor(repository: Repository, url?: string) + constructor(url?: string) { super(url); - this.#repository = repository; - this.#middlewares.push(new ProcedureRunner(this)); } - get repository() { return this.#repository; } - set middlewareFiles(filenames: Set) { this.#middlewareFiles = filenames; @@ -41,8 +34,6 @@ export default abstract class ProcedureRuntime extends Runtime implements Runner async start(): Promise { - await this.#repository.start(); - await super.start(); await this.#importMiddlewares(); @@ -52,8 +43,6 @@ export default abstract class ProcedureRuntime extends Runtime implements Runner { this.#clearMiddlewares(); - await this.#repository.stop(); - await super.stop(); } @@ -63,9 +52,9 @@ export default abstract class ProcedureRuntime extends Runtime implements Runner abstract run(request: Request): Promise; - import(importModel: Import): Promise + import(specifier: string): Promise { - return this.#repository.import(importModel); + return ModuleLoader.load(specifier); } addMiddleware(middleware: Middleware) @@ -114,8 +103,7 @@ export default abstract class ProcedureRuntime extends Runtime implements Runner async #importMiddleware(specifier: string): Promise { - const importModel = new Import('', specifier, ExecutionScopes.APPLICATION); - const module = await this.import(importModel); + const module = await this.import(specifier); const middleware = module.default as Middleware; if (middleware?.handle === undefined) diff --git a/packages/runtime/src/services/Proxy.ts b/packages/runtime/src/services/Proxy.ts index 1e3e986e..d843089c 100644 --- a/packages/runtime/src/services/Proxy.ts +++ b/packages/runtime/src/services/Proxy.ts @@ -2,7 +2,6 @@ import File from '../models/File.js'; import Request from '../models/Request.js'; import Response from '../models/Response.js'; -import Import from '../models/Import.js'; import RemoteGateway from './RemoteGateway.js'; import RemoteWorker from './RemoteWorker.js'; @@ -11,15 +10,19 @@ import ProcedureRuntime from './ProcedureRuntime.js'; export default class Proxy extends ProcedureRuntime { + #repository: RemoteRepository; #runner: RemoteGateway | RemoteWorker; constructor(repository: RemoteRepository, runner: RemoteGateway | RemoteWorker, url?: string) { - super(repository, url); + super(url); + this.#repository = repository; this.#runner = runner; } + get repository() { return this.#repository; } + get runner() { return this.#runner; } async start(): Promise @@ -50,12 +53,12 @@ export default class Proxy extends ProcedureRuntime readAsset(filename: string): Promise { - return this.repository.readAsset(filename); + return this.#repository.readAsset(filename); } - readModule(importModel: Import): Promise + readModule(specifier: string): Promise { - return this.repository.readModule(importModel); + return this.#repository.readModule(specifier); } run(request: Request): Promise diff --git a/packages/runtime/src/services/RemoteGateway.ts b/packages/runtime/src/services/RemoteGateway.ts index 6fb8e137..690071e1 100644 --- a/packages/runtime/src/services/RemoteGateway.ts +++ b/packages/runtime/src/services/RemoteGateway.ts @@ -4,7 +4,6 @@ import NotImplemented from '../errors/generic/NotImplemented.js'; import Request from '../models/Request.js'; import Response from '../models/Response.js'; -import DummyRepository from './DummyRepository.js'; import Gateway from './Gateway.js'; import Worker from './Worker.js'; import Remote from './Remote.js'; @@ -16,7 +15,7 @@ export default class RemoteGateway extends Gateway constructor(url: string) { - super(new DummyRepository(), url); + super(url); this.#remote = new Remote(url); } diff --git a/packages/runtime/src/services/RemoteRepository.ts b/packages/runtime/src/services/RemoteRepository.ts index 03ba5776..6c849146 100644 --- a/packages/runtime/src/services/RemoteRepository.ts +++ b/packages/runtime/src/services/RemoteRepository.ts @@ -1,6 +1,5 @@ import File from '../models/File.js'; -import Import from '../models/Import.js'; import Module from '../types/Module.js'; import ModuleLoader from '../utils/ModuleLoader.js'; @@ -37,15 +36,14 @@ export default class RemoteRepository extends Repository return this.#remote.loadFile(filename); } - readModule(importModel: Import): Promise + readModule(specifier: string): Promise { - console.log('RemoteRepository.readModule', importModel.specifier, importModel.caller); - return this.#remote.loadFile(`modules/${importModel.specifier}?caller=${importModel.caller}`); + return this.#remote.loadFile(`modules/${specifier}`); } - loadModule(importModel: Import): Promise + loadModule(specifier: string): Promise { - return ModuleLoader.load(importModel); + return ModuleLoader.load(specifier); } #getModuleBaseUrl(): string diff --git a/packages/runtime/src/services/RemoteWorker.ts b/packages/runtime/src/services/RemoteWorker.ts index cd754272..20708d5a 100644 --- a/packages/runtime/src/services/RemoteWorker.ts +++ b/packages/runtime/src/services/RemoteWorker.ts @@ -2,7 +2,6 @@ import Request from '../models/Request.js'; import Response from '../models/Response.js'; -import DummyRepository from './DummyRepository.js'; import Worker from './Worker.js'; import Remote from './Remote.js'; @@ -13,7 +12,7 @@ export default class RemoteWorker extends Worker constructor(url: string) { - super(new DummyRepository(), url); + super(url); this.#remote = new Remote(url); } diff --git a/packages/runtime/src/services/Repository.ts b/packages/runtime/src/services/Repository.ts index d1b1cee8..d8fd6461 100644 --- a/packages/runtime/src/services/Repository.ts +++ b/packages/runtime/src/services/Repository.ts @@ -1,8 +1,5 @@ -import { ExecutionScopes } from '../definitions/ExecutionScope.js'; - import File from '../models/File.js'; -import Import from '../models/Import.js'; import Module from '../types/Module.js'; import ModuleLoader from '../utils/ModuleLoader.js'; @@ -10,19 +7,14 @@ import Runtime from './Runtime.js'; export default abstract class Repository extends Runtime { - async import(importModel: Import): Promise + async import(specifier: string): Promise { - if (importModel.scope === ExecutionScopes.RUNTIME) - { - return ModuleLoader.load(importModel); - } - - return this.loadModule(importModel); + return ModuleLoader.load(specifier); } abstract readAsset(filename: string): Promise; - abstract readModule(importModel: Import): Promise; + abstract readModule(specifier: string): Promise; - abstract loadModule(importModel: Import): Promise; + abstract loadModule(specifier: string): Promise; } diff --git a/packages/runtime/src/services/Runtime.ts b/packages/runtime/src/services/Runtime.ts index 023b39c3..0315e94f 100644 --- a/packages/runtime/src/services/Runtime.ts +++ b/packages/runtime/src/services/Runtime.ts @@ -1,8 +1,6 @@ -import { ExecutionScopes } from '../definitions/ExecutionScope.js'; import InvalidHealthCheck from '../errors/InvalidHealthCheck.js'; import HealthCheck from '../interfaces/HealthCheck.js'; -import Import from '../models/Import.js'; import Module from '../types/Module.js'; export default abstract class Runtime @@ -23,7 +21,7 @@ export default abstract class Runtime this.#healthCheckFiles = filenames; } - abstract import(importModel: Import): Promise; + abstract import(specifier: string): Promise; start(): Promise { @@ -91,8 +89,7 @@ export default abstract class Runtime async #importHealthCheck(specifier: string): Promise { - const importModel = new Import('', specifier, ExecutionScopes.APPLICATION); - const module = await this.import(importModel); + const module = await this.import(specifier); const healthCheck = module.default as HealthCheck; if (healthCheck?.isHealthy === undefined) diff --git a/packages/runtime/src/services/Standalone.ts b/packages/runtime/src/services/Standalone.ts index 9e515264..8ebfe799 100644 --- a/packages/runtime/src/services/Standalone.ts +++ b/packages/runtime/src/services/Standalone.ts @@ -2,7 +2,6 @@ import File from '../models/File.js'; import Request from '../models/Request.js'; import Response from '../models/Response.js'; -import Import from '../models/Import.js'; import LocalWorker from './LocalWorker.js'; import LocalRepository from './LocalRepository.js'; @@ -10,12 +9,14 @@ import ProcedureRuntime from './ProcedureRuntime.js'; export default class Standalone extends ProcedureRuntime { + #repository: LocalRepository; #worker: LocalWorker; constructor(repository: LocalRepository, worker: LocalWorker, url?: string) { - super(repository, url); + super(url); + this.#repository = repository this.#worker = worker; } @@ -51,12 +52,12 @@ export default class Standalone extends ProcedureRuntime readAsset(filename: string): Promise { - return this.repository.readAsset(filename); + return this.#repository.readAsset(filename); } - readModule(importModel: Import): Promise + readModule(specifier: string): Promise { - return this.repository.readModule(importModel); + return this.#repository.readModule(specifier); } run(request: Request): Promise diff --git a/packages/runtime/src/utils/FileHelper.ts b/packages/runtime/src/utils/FileHelper.ts index 2604f787..1e3c1dfc 100644 --- a/packages/runtime/src/utils/FileHelper.ts +++ b/packages/runtime/src/utils/FileHelper.ts @@ -82,7 +82,7 @@ export default class FileHelper static createWorkerFilename(name: string): string { - return `./${name}.segment.worker.js`; + return `./${name}.segment.js`; } static createRepositoryFilename(name: string): string diff --git a/packages/runtime/src/utils/ModuleLoader.ts b/packages/runtime/src/utils/ModuleLoader.ts index 0607cd4e..70aaab7f 100644 --- a/packages/runtime/src/utils/ModuleLoader.ts +++ b/packages/runtime/src/utils/ModuleLoader.ts @@ -1,13 +1,9 @@ -import ModuleNotAccessible from '../errors/ModuleNotAccessible.js'; import ModuleNotLoaded from '../errors/ModuleNotLoaded.js'; -import Import from '../models/Import.js'; import Module from '../types/Module.js'; import ModuleImporter from '../types/ModuleImporter.js'; -import Environment from './Environment.js'; import UrlRewriter from './UrlRewriter.js'; -import FileHelper from './FileHelper.js'; const APPLICATION_MODULE_INDICATORS = ['.', '/', 'http:', 'https:']; @@ -26,43 +22,7 @@ export default class ModuleLoader _import = importer; } - static async load(importModel: Import): Promise - { - const { specifier, caller } = importModel; - - if (this.#isSystemModule(specifier)) - { - return this.#import(specifier); - } - - if (specifier.startsWith('/jitar')) - { - return Environment.isServer() - ? this.#import('JITAR_LIBRARY_NAME') - : this.#import(specifier); - } - - const filename = FileHelper.assureExtension(specifier); - const url = UrlRewriter.addBase(filename, _baseUrl); - - if (url.startsWith(_baseUrl) === false) - { - throw new ModuleNotAccessible(specifier); - } - - const aaa = _baseUrl.startsWith('//') ? url : `${url}?caller=${caller}`; - - console.log('ModuleLoader.load', aaa); - - return this.#import(aaa); - } - - static #isSystemModule(specifier: string): boolean - { - return APPLICATION_MODULE_INDICATORS.some((indicator: string) => specifier.startsWith(indicator)) === false; - } - - static async #import(specifier: string): Promise + static async load(specifier: string): Promise { try { diff --git a/packages/runtime/src/utils/RemoteClassLoader.ts b/packages/runtime/src/utils/RemoteClassLoader.ts index d90f23ce..0c3aecc9 100644 --- a/packages/runtime/src/utils/RemoteClassLoader.ts +++ b/packages/runtime/src/utils/RemoteClassLoader.ts @@ -1,9 +1,6 @@ import { ClassLoader, Loadable, ClassNotFound, InvalidClass } from '@jitar/serialization'; -import Import from '../models/Import.js'; -import { ExecutionScopes } from '../definitions/ExecutionScope.js'; - import ModuleLoader from './ModuleLoader.js'; export default class RemoteClassLoader implements ClassLoader @@ -15,8 +12,7 @@ export default class RemoteClassLoader implements ClassLoader throw new ClassNotFound(loadable.name); } - const importModel = new Import('', loadable.source, ExecutionScopes.APPLICATION); - const module = await ModuleLoader.load(importModel); + const module = await ModuleLoader.load(loadable.source); const clazz = (module[loadable.name] ?? module['default']) as Function; if (clazz === undefined) diff --git a/packages/server-nodejs/src/JitarServer.ts b/packages/server-nodejs/src/JitarServer.ts index 34b64cc0..ffee7f2a 100644 --- a/packages/server-nodejs/src/JitarServer.ts +++ b/packages/server-nodejs/src/JitarServer.ts @@ -3,7 +3,7 @@ import express, { Express } from 'express'; import { Server } from 'http'; import { Logger } from 'tslog'; -import { LocalGateway, LocalWorker, LocalRepository, Proxy, Runtime, RemoteClassLoader, ExecutionScopes, Standalone, Import } from '@jitar/runtime'; +import { LocalGateway, LocalWorker, LocalRepository, Proxy, Runtime, RemoteClassLoader, Standalone } from '@jitar/runtime'; import { ClassLoader, Serializer, SerializerBuilder, ValueSerializer } from '@jitar/serialization'; import ServerOptions from './configuration/ServerOptions.js'; @@ -222,9 +222,7 @@ export default class JitarServer for (const setUpScript of setUpScripts) { - const importModel = new Import('', setUpScript, ExecutionScopes.APPLICATION); - - await runtime.import(importModel); + await runtime.import(setUpScript); } } @@ -243,9 +241,7 @@ export default class JitarServer for (const tearDownScript of tearDownScripts) { - const importModel = new Import('', tearDownScript, ExecutionScopes.APPLICATION); - - await runtime.import(importModel); + await runtime.import(tearDownScript); } } diff --git a/packages/server-nodejs/src/controllers/ModulesController.ts b/packages/server-nodejs/src/controllers/ModulesController.ts index d0e32474..e7d8646f 100644 --- a/packages/server-nodejs/src/controllers/ModulesController.ts +++ b/packages/server-nodejs/src/controllers/ModulesController.ts @@ -2,7 +2,7 @@ import express, { Request, Response } from 'express'; import { Logger } from 'tslog'; -import { LocalRepository, Standalone, Import, ExecutionScopes } from '@jitar/runtime'; +import { LocalRepository, Standalone } from '@jitar/runtime'; import { Serializer } from '@jitar/serialization'; import Headers from '../definitions/Headers'; @@ -25,24 +25,16 @@ export default class ModulesController async getModule(request: Request, response: Response): Promise { - const caller = request.query.caller ?? ''; - - if (typeof caller !== 'string') - { - return response.status(400).send('Invalid caller.'); - } - - this.#logger.info(`Get module for -> '${caller}'`); + //this.#logger.info(`Get module for -> '${caller}'`); const pathKey = '/modules'; const specifier = request.path.substring(pathKey.length); try { - const importModel = new Import(caller, specifier, ExecutionScopes.APPLICATION); - const file = await this.#repository.readModule(importModel); + const file = await this.#repository.readModule(specifier); - this.#logger.info(`Got module -> '${specifier}' (${caller})`); + this.#logger.info(`Got module -> '${specifier}'`); response.setHeader(Headers.CONTENT_TYPE, file.type); @@ -52,7 +44,7 @@ export default class ModulesController { const message = error instanceof Error ? error.message : String(error); - this.#logger.error(`Failed to get module -> '${specifier}' (${caller}) | ${message}`); + this.#logger.error(`Failed to get module -> '${specifier}' | ${message}`); const data = this.#serializer.serialize(error);