Skip to content

Commit

Permalink
#296: make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
petermasking committed Aug 15, 2024
1 parent de66ec4 commit 8cb3aae
Show file tree
Hide file tree
Showing 43 changed files with 287 additions and 291 deletions.
2 changes: 1 addition & 1 deletion examples/concepts/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Expand Down
2 changes: 1 addition & 1 deletion examples/concepts/hello-world/services/standalone.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"url": "http://127.0.0.1:3000",
"standalone":
{

"segments": ["default"]
}
}
10 changes: 5 additions & 5 deletions examples/concepts/segmentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Expand Down
2 changes: 1 addition & 1 deletion examples/concepts/segmentation/services/standalone.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"url": "http://127.0.0.1:3000",
"standalone":
{

"segments": [ "process", "data" ]
}
}
14 changes: 8 additions & 6 deletions packages/caching/src/application/ModuleBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions packages/caching/test/application/ImportRewriter.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions packages/caching/test/fixtures/application.ts
Original file line number Diff line number Diff line change
@@ -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 };
61 changes: 61 additions & 0 deletions packages/caching/test/fixtures/codes.ts
Original file line number Diff line number Diff line change
@@ -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 };
9 changes: 9 additions & 0 deletions packages/caching/test/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -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';
21 changes: 21 additions & 0 deletions packages/caching/test/fixtures/modules.ts
Original file line number Diff line number Diff line change
@@ -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 };
8 changes: 8 additions & 0 deletions packages/caching/test/fixtures/repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import Repository from '../../src/module/models/Repository';

import { MODULES } from './modules';

const REPOSITORY = new Repository(Object.values(MODULES));

export { REPOSITORY };
17 changes: 17 additions & 0 deletions packages/caching/test/fixtures/segmentModules.ts
Original file line number Diff line number Diff line change
@@ -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 };
17 changes: 17 additions & 0 deletions packages/caching/test/fixtures/segmentProcedures.ts
Original file line number Diff line number Diff line change
@@ -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 };
8 changes: 8 additions & 0 deletions packages/caching/test/fixtures/segmentation.ts
Original file line number Diff line number Diff line change
@@ -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 };
13 changes: 13 additions & 0 deletions packages/caching/test/fixtures/segments.ts
Original file line number Diff line number Diff line change
@@ -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 };
1 change: 0 additions & 1 deletion packages/jitar/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export
NamedParameter,
ArrayParameter,
ObjectParameter,
Import,
BadRequest,
Forbidden,
NotFound,
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/src/RuntimeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions packages/runtime/src/client.ts
Original file line number Diff line number Diff line change
@@ -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<LocalWorker>
{
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);

Expand Down
18 changes: 9 additions & 9 deletions packages/runtime/src/definitions/ExecutionScope.ts
Original file line number Diff line number Diff line change
@@ -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 };
4 changes: 1 addition & 3 deletions packages/runtime/src/globals.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>;

// Available for external use
globals.__import = importModule;
globals.__run = runProcedure;

// Internal use only
Expand Down
18 changes: 0 additions & 18 deletions packages/runtime/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -24,21 +21,6 @@ export function getRuntime(): ProcedureRuntime
return _runtime;
}

export async function importModule(caller: string, specifier: string, executionScope: ExecutionScope, extractDefault: boolean): Promise<unknown>
{
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<unknown>
{
const runtime = getRuntime();
Expand Down
Loading

0 comments on commit 8cb3aae

Please sign in to comment.