-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#296: first draft of client creation
- Loading branch information
1 parent
e7d65d7
commit d299ff5
Showing
10 changed files
with
155 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,14 @@ | ||
|
||
import { LocalWorker, RemoteGateway } from './services'; | ||
import { SourceManager, ImportFunction, RemoteFileManager } from './source'; | ||
import type { Client } from './services'; | ||
|
||
let client: LocalWorker | undefined = undefined; | ||
const resolvers: ((client: LocalWorker) => void)[] = []; | ||
import RuntimeBuilder from './RuntimeBuilder'; | ||
|
||
// export async function startClient(remoteUrl: string, segmentNames: string[] = [], middlewares: string[] = []): Promise<LocalWorker> | ||
// { | ||
// const gateway = new RemoteGateway(remoteUrl); | ||
|
||
// const worker = new LocalWorker(gateway); | ||
// worker.segmentNames = new Set(segmentNames); | ||
// worker.middlewareFiles = new Set(middlewares); | ||
|
||
// await worker.start(); | ||
|
||
// client = worker; | ||
|
||
// resolvers.forEach((resolve) => resolve(worker)); | ||
// resolvers.length = 0; | ||
|
||
// return worker; | ||
// } | ||
|
||
export async function getClient(): Promise<LocalWorker> | ||
export async function startClient(remoteUrl: string, importFunction: ImportFunction, segmentNames: string[] = [], middlewares: string[] = []): Promise<Client> | ||
{ | ||
if (client === undefined) | ||
{ | ||
return new Promise((resolve) => | ||
{ | ||
resolvers.push(resolve); | ||
}); | ||
} | ||
const fileManager = new RemoteFileManager(remoteUrl); | ||
const sourceManager = new SourceManager(importFunction, fileManager); | ||
const runtimeBuilder = new RuntimeBuilder(sourceManager); | ||
|
||
return client; | ||
return runtimeBuilder.buildClient({ remoteUrl, segmentNames, middlewares }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
import { Request, Response, ExecutionManager, Runner } from '../execution'; | ||
import { MiddlewareManager, ProcedureRunner } from '../middleware'; | ||
|
||
import { setRuntime } from '../hooks.js'; | ||
|
||
import RemoteGateway from './gateway/RemoteGateway'; | ||
|
||
type Configuration = | ||
{ | ||
gateway: RemoteGateway; | ||
middlewareManager: MiddlewareManager; // object with all middleware loaded | ||
executionManager: ExecutionManager; // object with all segments loaded | ||
}; | ||
|
||
export default class Client implements Runner | ||
{ | ||
#gateway: RemoteGateway; | ||
|
||
#middlewareManager: MiddlewareManager; | ||
#executionManager: ExecutionManager; | ||
|
||
constructor(configuration: Configuration) | ||
{ | ||
this.#gateway = configuration.gateway; | ||
|
||
this.#middlewareManager = configuration.middlewareManager; | ||
this.#executionManager = configuration.executionManager; | ||
|
||
// TODO: Should be done when constructing the middleware manager | ||
this.#middlewareManager.addMiddleware(new ProcedureRunner(this.#executionManager)); | ||
|
||
setRuntime(this); | ||
} | ||
|
||
run(request: Request): Promise<Response> | ||
{ | ||
return this.#mustRunLocal(request) | ||
? this.#runLocal(request) | ||
: this.#runRemote(request); | ||
} | ||
|
||
#mustRunLocal(request: Request): boolean | ||
{ | ||
return this.#executionManager.hasProcedure(request.fqn); | ||
} | ||
|
||
#runLocal(request: Request): Promise<Response> | ||
{ | ||
return this.#middlewareManager.handle(request); | ||
} | ||
|
||
#runRemote(request: Request): Promise<Response> | ||
{ | ||
return this.#gateway.run(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
import { NotImplemented } from '../../errors'; | ||
|
||
import FileManager from '../interfaces/FileManager'; | ||
import File from '../models/File'; | ||
|
||
export default class RemoteFileManager implements FileManager | ||
{ | ||
#rootUrl: string; | ||
|
||
constructor(rootUrl: string) | ||
{ | ||
this.#rootUrl = rootUrl; | ||
} | ||
|
||
getRootLocation(): string | ||
{ | ||
return this.#rootUrl; | ||
} | ||
|
||
getAbsoluteLocation(filename: string): string | ||
{ | ||
return `${this.#rootUrl}/${filename}`; | ||
} | ||
|
||
getRelativeLocation(filename: string): string | ||
{ | ||
throw new NotImplemented(); | ||
} | ||
|
||
getType(filename: string): Promise<string> | ||
{ | ||
throw new NotImplemented(); | ||
} | ||
|
||
getContent(filename: string): Promise<Buffer | string> | ||
{ | ||
throw new NotImplemented(); | ||
} | ||
|
||
read(filename: string): Promise<File> | ||
{ | ||
throw new NotImplemented(); | ||
} | ||
|
||
write(filename: string, content: string): Promise<void> | ||
{ | ||
throw new NotImplemented(); | ||
} | ||
|
||
delete(filename: string): Promise<void> | ||
{ | ||
throw new NotImplemented(); | ||
} | ||
|
||
filter(pattern: string): Promise<string[]> | ||
{ | ||
throw new NotImplemented(); | ||
} | ||
} |