@akala/pm 15.9.6 → 15.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "pm": "dist/esm/cli.js",
5
5
  "pm-fork": "dist/esm/fork.js"
6
6
  },
7
- "version": "15.9.6",
7
+ "version": "15.10.0",
8
8
  "scripts": {
9
9
  "test": "echo 1",
10
10
  "generate": "akala sdk generate dist/esm/commands commands.json --name pm && akala sdk generate dist/esm/cli-commands cli-commands.json --name pm",
@@ -29,11 +29,11 @@
29
29
  "src/fork.ts"
30
30
  ],
31
31
  "dependencies": {
32
- "@akala/cli": "^5.13.4",
33
- "@akala/commands": "^17.7.0",
34
- "@akala/config": "^7.0.6",
32
+ "@akala/cli": "^5.13.5",
33
+ "@akala/commands": "^17.7.1",
34
+ "@akala/config": "^7.0.7",
35
35
  "@akala/core": "^42.3.0",
36
- "@akala/fs": "^2.1.27",
36
+ "@akala/fs": "^2.2.0",
37
37
  "@akala/json-rpc-ws": "^14.3.17",
38
38
  "reflect-metadata": "^0.2.2"
39
39
  },
@@ -1,12 +1,34 @@
1
+ import fsHandler from '@akala/fs';
1
2
  import type State from '../state.js';
2
3
  import type { SidecarMetadata } from '../state.js';
3
4
  import { isAbsolute, resolve } from "path";
5
+ import { pathToFileURL } from 'url';
6
+ import { Metadata } from '@akala/commands';
4
7
 
5
- export default async function map<TName extends string>(config: State['config'], name: TName, targetPath: string | URL, runtime: SidecarMetadata['type'], cwd?: string, options?: { commandable?: boolean })
8
+ export default async function map<TName extends string>(config: State['config'], name: TName, targetPath: string | URL, runtimeName: SidecarMetadata['type'], cwd?: string, options?: { commandable?: boolean })
6
9
  {
7
10
  if (typeof targetPath == 'string' && !URL.canParse(targetPath) && !isAbsolute(targetPath))
8
11
  targetPath = resolve(cwd || process.cwd(), targetPath);
9
- config.containers.set(name, { type: runtime, path: targetPath.toString(), commandable: !!options?.commandable });
12
+
13
+ let dependencies: string[];
14
+ if (!!options?.commandable)
15
+ {
16
+ const url = targetPath instanceof URL ? targetPath : !URL.canParse(targetPath) ? pathToFileURL(targetPath) : new URL(targetPath);
17
+
18
+ const fs = await fsHandler.process(url);
19
+
20
+ const container = await fs.readFile<Metadata.Container>(url, { encoding: 'json' });
21
+
22
+ if (container.dependencies)
23
+ dependencies = container.dependencies;
24
+ }
25
+
26
+ config.containers.set(name, {
27
+ type: runtimeName,
28
+ path: targetPath.toString(),
29
+ commandable: !!options?.commandable,
30
+ dependencies
31
+ });
10
32
  await config.commit();
11
33
  return name
12
34
  }
@@ -13,7 +13,7 @@ export default async function start(this: State, name: string, context?: CliCont
13
13
  inspect: undefined, new: undefined, wait: undefined
14
14
  })];
15
15
 
16
- const cp = ChildProcess.build(args, { ...context.options, inheritStdio: true }, context.abort.signal);
16
+ const cp = await ChildProcess.build(args, { ...context.options, inheritStdio: true }, context.abort.signal);
17
17
  cp.on('exit', function (...args: unknown[])
18
18
  {
19
19
  console.log(args);
@@ -8,9 +8,10 @@ import getRandomName from "./name.js";
8
8
  import { type ProxyConfiguration, unwrap } from "@akala/config";
9
9
  import path from 'path'
10
10
  import { fileURLToPath } from 'url'
11
- import { type RuntimeInstance } from "../runtimes/shared.js";
11
+ import { RuntimeEventMap, type RuntimeInstance } from "../runtimes/shared.js";
12
12
  import ChildProcess from "../runtimes/child_process.js";
13
13
  import Worker from "../runtimes/worker.js";
14
+ import Docker from "../runtimes/docker.js";
14
15
 
15
16
 
16
17
  //eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -99,11 +100,13 @@ export default async function start(this: State, pm: pmContainer.container & Con
99
100
  switch (def?.type)
100
101
  {
101
102
  case 'worker':
102
- cp = Worker.build(args, options);
103
+ cp = await Worker.build(args, options);
103
104
  break;
105
+ case 'docker':
106
+ cp = await Docker.build(args, options);
104
107
  case 'nodejs':
105
108
  args.push('--pm-sock', 'ipc://')
106
- cp = ChildProcess.build(args, { ...options, keepAttached: true });
109
+ cp = await ChildProcess.build(args, { ...options, keepAttached: true }) as RuntimeInstance<RuntimeEventMap>;
107
110
  break;
108
111
  default:
109
112
  throw new ErrorWithStatus(400, `container with type ${this.config.containers[name]?.type} are not yet supported`);
@@ -7,17 +7,25 @@ import { EventEmitter, SocketAdapter, type IEvent } from "@akala/core";
7
7
  import { fileURLToPath } from "url";
8
8
  import { JsonRpcSocketAdapter, Payload } from "@akala/json-rpc-ws";
9
9
 
10
- export type ChildProcessRuntimeEventMap = {
10
+ export interface ChildProcessRuntimeEventMap extends RuntimeEventMap
11
+ {
11
12
  "close": IEvent<[code: number | null, signal: NodeJS.Signals | null], void>
12
13
  "disconnect": IEvent<[], void>
13
14
  "error": IEvent<[err: Error], void>
14
- "exit": IEvent<[code: number | null, signal: NodeJS.Signals | null], void>
15
15
  "message": IEvent<[message: Serializable, sendHandle: SendHandle], void>
16
16
  "spawn": IEvent<[], void>
17
17
 
18
- } & RuntimeEventMap;
18
+ }
19
19
 
20
- export type ChildProcessRuntimeOptions = { new?: boolean, name: string, keepAttached?: boolean, inspect?: boolean, verbose?: number, wait?: boolean, inheritStdio?: boolean }
20
+ export type ChildProcessRuntimeOptions = {
21
+ new?: boolean,
22
+ name: string,
23
+ keepAttached?: boolean,
24
+ inspect?: boolean,
25
+ verbose?: number,
26
+ wait?: boolean,
27
+ inheritStdio?: boolean
28
+ }
21
29
 
22
30
  export default class Runtime extends EventEmitter<ChildProcessRuntimeEventMap> implements RuntimeInstance<ChildProcessRuntimeEventMap>
23
31
  {
@@ -40,7 +48,7 @@ export default class Runtime extends EventEmitter<ChildProcessRuntimeEventMap> i
40
48
  this.stderrPrefixer.pipe(process.stderr);
41
49
  this.stdoutPrefixer = this.cp.stdout?.pipe(new NewLinePrefixer(options.name + ' ', { useColors: process.stdout.isTTY }), { end: true });
42
50
  this.stdoutPrefixer.pipe(process.stdout);
43
- this.on('disconnect', () =>
51
+ this.cp.on('disconnect', () =>
44
52
  {
45
53
  this.cp.stderr?.unpipe();
46
54
  this.cp.stdout?.unpipe();
@@ -90,9 +98,7 @@ export default class Runtime extends EventEmitter<ChildProcessRuntimeEventMap> i
90
98
 
91
99
  public static build(args: string[], options: ChildProcessRuntimeOptions, signal?: AbortSignal)
92
100
  {
93
- return new Runtime(args, options, signal) as Runtime & RuntimeInstance;
94
-
95
-
101
+ return Promise.resolve(new Runtime(args, options, signal));
96
102
  }
97
103
 
98
104
  public unref()
@@ -50,13 +50,13 @@ function demuxDockerStream(stream: Readable, stdout: PassThrough, stderr: PassTh
50
50
  });
51
51
  }
52
52
 
53
- export class Runtime extends EventEmitter<RuntimeEventMap> implements RuntimeInstance
53
+ export default class Runtime extends EventEmitter<RuntimeEventMap> implements RuntimeInstance
54
54
  {
55
55
  static readonly name = "docker";
56
56
 
57
57
  static async build(
58
58
  args: string[],
59
- options: { new?: boolean; name: string; keepAttached?: boolean; inspect?: boolean; verbose?: boolean; wait?: boolean }
59
+ options: { new?: boolean; name: string; keepAttached?: boolean; inspect?: boolean; verbose?: number; wait?: boolean }
60
60
  ): Promise<Runtime>
61
61
  {
62
62
  const image = args[0];
@@ -5,7 +5,7 @@ import { Readable } from "stream";
5
5
  export interface Runtime
6
6
  {
7
7
  name: string;
8
- build(args: string[], options: { new?: boolean, name: string, keepAttached?: boolean, inspect?: boolean, verbose?: boolean, wait?: boolean }): RuntimeInstance;
8
+ build(args: string[], options: { new?: boolean, name: string, keepAttached?: boolean, inspect?: boolean, verbose?: number, wait?: boolean }): Promise<RuntimeInstance>;
9
9
  }
10
10
 
11
11
  export interface RuntimeEventMap
@@ -15,7 +15,7 @@ export default class Runtime extends EventEmitter<RuntimeEventMap> implements Ru
15
15
  private readonly cp: Worker;
16
16
  public readonly adapter: SocketAdapter<Payload<Readable>>;
17
17
  private _running: boolean;
18
- constructor(args: string[], options: { new?: boolean, name: string, keepAttached?: boolean, inspect?: boolean, verbose?: number, wait?: boolean })
18
+ constructor(args: string[], options: { new?: boolean, name: string, keepAttached?: boolean, inspect?: boolean, verbose?: number, wait?: boolean }, signal?: AbortSignal)
19
19
  {
20
20
  super();
21
21
  this.cp = new Worker(require.resolve('../fork'), { argv: args, stderr: true, stdout: true })
@@ -23,15 +23,21 @@ export default class Runtime extends EventEmitter<RuntimeEventMap> implements Ru
23
23
  this.cp.stdout?.pipe(new NewLinePrefixer(options.name + ' ', { useColors: true })).pipe(process.stdout);
24
24
  this.adapter = new JsonRpcSocketAdapter(new MessagePortAdapter(this.cp));
25
25
  this.cp.on('exit', () => { this._running = false; this.emit('runningChanged') })
26
+
27
+ signal?.addEventListener('abort', () =>
28
+ {
29
+ this.stop();
30
+ })
26
31
  }
27
- stop(): Promise<number>
32
+
33
+ public stop()
28
34
  {
29
35
  return this.cp.terminate();
30
36
  }
31
37
 
32
- public static build(args: string[], options: { new?: boolean, name: string, keepAttached?: boolean, inspect?: boolean, verbose?: number, wait?: boolean })
38
+ public static build(args: string[], options: { new?: boolean, name: string, keepAttached?: boolean, inspect?: boolean, verbose?: number, wait?: boolean }, signal?: AbortSignal)
33
39
  {
34
- return new Runtime(args, options);
40
+ return Promise.resolve(new Runtime(args, options, signal));
35
41
  }
36
42
 
37
43
  get stderr(): Readable { return this.cp.stderr }
package/src/state.ts CHANGED
@@ -25,7 +25,7 @@ export interface SidecarMetadata
25
25
  stateless: boolean;
26
26
  dependencies?: string[];
27
27
  commandable: boolean;
28
- type: 'nodejs' | 'worker';
28
+ type: 'nodejs' | 'worker' | 'docker';
29
29
  }
30
30
 
31
31
  export interface SidecarConfiguration<T extends string | SerializableObject = SerializableObject>