awesome-emulator-cli 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +58 -2
- package/bin/awesome-emulator.js +6 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +122 -0
- package/dist/container-lifecycle.d.ts +7 -0
- package/dist/container-lifecycle.js +68 -0
- package/dist/docker.d.ts +7 -0
- package/dist/docker.js +54 -0
- package/dist/emulator-environment.d.ts +20 -0
- package/dist/emulator-environment.js +47 -0
- package/dist/emulator-image.d.ts +6 -0
- package/dist/emulator-image.js +72 -0
- package/dist/emulator-options.d.ts +16 -0
- package/dist/emulator-options.js +48 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +158 -0
- package/dist/process.d.ts +1 -0
- package/dist/process.js +66 -0
- package/dist/signals.d.ts +4 -0
- package/dist/signals.js +12 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 c19yamamoto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Awesome Emulator CLI
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Awesome Emulator CLI for use with AWS Emulator in local tests from npm scripts or Node.js. It runs the upstream Linux package in Docker.
|
|
4
|
+
|
|
5
|
+
This is an independent project and is not affiliated with or endorsed by Amazon Web Services.
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- Node.js 22 or later
|
|
10
|
+
- Docker Engine or Docker Desktop
|
|
11
|
+
- x86_64/amd64 or ARM64/aarch64
|
|
12
|
+
|
|
13
|
+
On first start, Docker downloads the selected AWS Emulator RPM and its signature, verifies the signature, and builds a local image. Later starts reuse the image for that version and architecture.
|
|
14
|
+
|
|
15
|
+
## Run a test command
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install --save-dev awesome-emulator-cli
|
|
19
|
+
npx awesome-emulator run -- npm test
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`run` starts the emulator, supplies the connection settings to the child process, then stops the container. Use `--version` to select a release and `--port 0` to choose a free local port. Run `npx awesome-emulator --help` for all options.
|
|
23
|
+
|
|
24
|
+
## Use from Node.js
|
|
25
|
+
|
|
26
|
+
Use the API when your test harness needs to control the emulator lifecycle directly:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import { startEmulator } from "awesome-emulator-cli";
|
|
30
|
+
|
|
31
|
+
const emulator = await startEmulator({ port: 0, telemetry: "off" });
|
|
32
|
+
try {
|
|
33
|
+
console.log(emulator.endpointUrl, emulator.environment);
|
|
34
|
+
} finally {
|
|
35
|
+
await emulator.stop();
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Pass an `AbortSignal` as `signal` to cancel image building or startup. `NODE_EXTRA_CA_CERTS` is read when Node.js starts. For an already-running Node.js process, configure the AWS SDK's HTTPS handler with `emulator.caFile`; use `awesome-emulator run` when you want the wrapper to start a child process with TLS settings already applied.
|
|
40
|
+
|
|
41
|
+
## Local files and telemetry
|
|
42
|
+
|
|
43
|
+
The emulator writes its config, credentials, and CA certificate under `~/.aws/emulator/<port>/` (Windows: `%USERPROFILE%\.aws\emulator\<port>\`). The emulator keeps resources in memory; a clean stop removes the per-port files and resources.
|
|
44
|
+
|
|
45
|
+
Telemetry is on by default. AWS says it collects aggregate usage and error data, not requests, responses, code, credentials, or personal data. Turn it off for a run with:
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
npx awesome-emulator run --telemetry off -- npm test
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Read the [AWS Emulator telemetry details](https://docs.aws.amazon.com/emulator/latest/userguide/telemetry.html) before choosing whether to leave telemetry on.
|
|
52
|
+
|
|
53
|
+
AWS Emulator is for building and testing. Do not use it for production workloads or security testing. See the [official limitations](https://docs.aws.amazon.com/emulator/latest/userguide/) and [security guidance](https://docs.aws.amazon.com/emulator/latest/userguide/security.html).
|
|
54
|
+
|
|
55
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) to contribute and [SECURITY.md](./SECURITY.md) to report a vulnerability.
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
|
|
59
|
+
MIT. See [LICENSE](./LICENSE). This license covers the wrapper; downloaded AWS Emulator binaries retain their own license and notices.
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { runWithEmulator, startEmulator } from "./index.js";
|
|
3
|
+
import { FORWARDED_SIGNALS, getSignalExitCode } from "./signals.js";
|
|
4
|
+
function usage() {
|
|
5
|
+
console.log(`Usage:
|
|
6
|
+
awesome-emulator start [options]
|
|
7
|
+
awesome-emulator run [options] -- <command> [args...]
|
|
8
|
+
|
|
9
|
+
Options:
|
|
10
|
+
-v, --version <version> AWS Emulator version (default: 1.0.522)
|
|
11
|
+
-p, --port <port> Host port; 0 chooses an available port (default: 9297)
|
|
12
|
+
-t, --telemetry <on|off> Telemetry setting (default: on)
|
|
13
|
+
--memory <limit> Container memory limit (default: 2g)
|
|
14
|
+
-h, --help Show this help`);
|
|
15
|
+
}
|
|
16
|
+
function parse(argv) {
|
|
17
|
+
const command = argv[0];
|
|
18
|
+
if (command === "-h" || command === "--help")
|
|
19
|
+
return { help: true };
|
|
20
|
+
const options = {};
|
|
21
|
+
const commandArgs = [];
|
|
22
|
+
let separator = false;
|
|
23
|
+
for (let index = 1; index < argv.length; index += 1) {
|
|
24
|
+
const token = argv[index];
|
|
25
|
+
if (separator) {
|
|
26
|
+
commandArgs.push(token);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (token === "--") {
|
|
30
|
+
separator = true;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (token === "-h" || token === "--help")
|
|
34
|
+
return { help: true };
|
|
35
|
+
const optionNames = {
|
|
36
|
+
"-v": "version", "--version": "version",
|
|
37
|
+
"-p": "port", "--port": "port",
|
|
38
|
+
"-t": "telemetry", "--telemetry": "telemetry",
|
|
39
|
+
"--memory": "memory",
|
|
40
|
+
};
|
|
41
|
+
const key = optionNames[token];
|
|
42
|
+
if (!key)
|
|
43
|
+
throw new Error(`Unknown option: ${token}`);
|
|
44
|
+
const value = argv[index + 1];
|
|
45
|
+
if (!value || value.startsWith("--"))
|
|
46
|
+
throw new Error(`Missing value for ${token}`);
|
|
47
|
+
options[key] = value;
|
|
48
|
+
index += 1;
|
|
49
|
+
}
|
|
50
|
+
return { help: false, command, options, commandArgs };
|
|
51
|
+
}
|
|
52
|
+
function waitForAbort(signal) {
|
|
53
|
+
if (signal.aborted)
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
return new Promise((resolve) => signal.addEventListener("abort", () => resolve(), { once: true }));
|
|
56
|
+
}
|
|
57
|
+
function signalExitCode(signal) {
|
|
58
|
+
return getSignalExitCode(signal.reason);
|
|
59
|
+
}
|
|
60
|
+
function formatError(error) {
|
|
61
|
+
if (error instanceof AggregateError) {
|
|
62
|
+
const causes = [...error.errors].map(formatError);
|
|
63
|
+
return causes.length > 0 ? `${error.message}\n${causes.map((cause) => ` ${cause}`).join("\n")}` : error.message;
|
|
64
|
+
}
|
|
65
|
+
return error instanceof Error ? error.message : String(error);
|
|
66
|
+
}
|
|
67
|
+
async function main(signal) {
|
|
68
|
+
const parsed = parse(process.argv.slice(2));
|
|
69
|
+
if (parsed.help || !parsed.command || parsed.command === "help") {
|
|
70
|
+
usage();
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
if (parsed.command === "run") {
|
|
74
|
+
const [program, ...args] = parsed.commandArgs;
|
|
75
|
+
if (!program)
|
|
76
|
+
throw new Error("Usage: awesome-emulator run [options] -- <command> [args...]");
|
|
77
|
+
return runWithEmulator(program, args, { ...parsed.options, signal });
|
|
78
|
+
}
|
|
79
|
+
if (parsed.command === "start") {
|
|
80
|
+
const emulator = await startEmulator({ ...parsed.options, signal });
|
|
81
|
+
try {
|
|
82
|
+
if (!signal.aborted) {
|
|
83
|
+
console.log(JSON.stringify({
|
|
84
|
+
version: emulator.version,
|
|
85
|
+
endpointUrl: emulator.endpointUrl,
|
|
86
|
+
healthCheckUrl: emulator.healthCheckUrl,
|
|
87
|
+
configFile: emulator.configFile,
|
|
88
|
+
credentialsFile: emulator.credentialsFile,
|
|
89
|
+
caFile: emulator.caFile,
|
|
90
|
+
environment: emulator.environment,
|
|
91
|
+
}, null, 2));
|
|
92
|
+
}
|
|
93
|
+
await waitForAbort(signal);
|
|
94
|
+
return signalExitCode(signal);
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
await emulator.stop();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
throw new Error(`Unknown command: ${parsed.command}`);
|
|
101
|
+
}
|
|
102
|
+
const controller = new AbortController();
|
|
103
|
+
const signalHandlers = new Map();
|
|
104
|
+
for (const signal of FORWARDED_SIGNALS) {
|
|
105
|
+
const handler = () => controller.abort(signal);
|
|
106
|
+
signalHandlers.set(signal, handler);
|
|
107
|
+
process.once(signal, handler);
|
|
108
|
+
}
|
|
109
|
+
main(controller.signal).then((code) => {
|
|
110
|
+
process.exitCode = code;
|
|
111
|
+
}).catch((error) => {
|
|
112
|
+
if (controller.signal.aborted && error instanceof Error && error.name === "AbortError") {
|
|
113
|
+
process.exitCode = signalExitCode(controller.signal);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
console.error(`awesome-emulator: ${formatError(error)}`);
|
|
117
|
+
process.exitCode = 1;
|
|
118
|
+
}
|
|
119
|
+
}).finally(() => {
|
|
120
|
+
for (const [signal, handler] of signalHandlers)
|
|
121
|
+
process.removeListener(signal, handler);
|
|
122
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type DockerRunner } from "./docker.js";
|
|
2
|
+
export declare function stopContainer(containerId: string, runDocker?: DockerRunner): Promise<void>;
|
|
3
|
+
export declare function createRetryableStop(stop: () => Promise<void>): () => Promise<void>;
|
|
4
|
+
export declare function startContainer(containerName: string, args: string[], signal: AbortSignal | undefined, waitUntilHealthy: (containerId: string, signal?: AbortSignal) => Promise<void>, runDocker?: DockerRunner): Promise<string>;
|
|
5
|
+
export declare function withEmulator<T extends {
|
|
6
|
+
stop: () => Promise<void>;
|
|
7
|
+
}, R>(start: () => Promise<T>, run: (session: T) => Promise<R>): Promise<R>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { docker, throwIfAborted } from "./docker.js";
|
|
2
|
+
function isMissingContainer(error) {
|
|
3
|
+
return error instanceof Error && /no such (?:container|object)/i.test(error.message);
|
|
4
|
+
}
|
|
5
|
+
export async function stopContainer(containerId, runDocker = docker) {
|
|
6
|
+
let stopError;
|
|
7
|
+
try {
|
|
8
|
+
await runDocker(["stop", "--time", "10", containerId], { capture: true });
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
if (isMissingContainer(error))
|
|
12
|
+
return;
|
|
13
|
+
stopError = error;
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
await runDocker(["rm", "--force", containerId], { capture: true });
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (isMissingContainer(error))
|
|
20
|
+
return;
|
|
21
|
+
const failures = stopError ? [stopError, error] : [error];
|
|
22
|
+
throw new AggregateError(failures, `Failed to stop or remove Docker container ${containerId}.`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function createRetryableStop(stop) {
|
|
26
|
+
let pending;
|
|
27
|
+
return () => {
|
|
28
|
+
if (!pending) {
|
|
29
|
+
pending = Promise.resolve().then(stop).catch((error) => {
|
|
30
|
+
pending = undefined;
|
|
31
|
+
throw error;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return pending;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export async function startContainer(containerName, args, signal, waitUntilHealthy, runDocker = docker) {
|
|
38
|
+
throwIfAborted(signal);
|
|
39
|
+
let cleanupTarget = containerName;
|
|
40
|
+
try {
|
|
41
|
+
const containerId = await runDocker(args, { capture: true, signal });
|
|
42
|
+
if (!containerId)
|
|
43
|
+
throw new Error("Docker did not return a container ID.");
|
|
44
|
+
cleanupTarget = containerId;
|
|
45
|
+
throwIfAborted(signal);
|
|
46
|
+
await waitUntilHealthy(containerId, signal);
|
|
47
|
+
throwIfAborted(signal);
|
|
48
|
+
return containerId;
|
|
49
|
+
}
|
|
50
|
+
catch (startupError) {
|
|
51
|
+
try {
|
|
52
|
+
await stopContainer(cleanupTarget, runDocker);
|
|
53
|
+
}
|
|
54
|
+
catch (cleanupError) {
|
|
55
|
+
throw new AggregateError([startupError, cleanupError], `AWS Emulator startup failed (${startupError instanceof Error ? startupError.message : String(startupError)}) and Docker container cleanup also failed (${cleanupTarget}).`);
|
|
56
|
+
}
|
|
57
|
+
throw startupError;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export async function withEmulator(start, run) {
|
|
61
|
+
const session = await start();
|
|
62
|
+
try {
|
|
63
|
+
return await run(session);
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
await session.stop();
|
|
67
|
+
}
|
|
68
|
+
}
|
package/dist/docker.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type DockerOptions = {
|
|
2
|
+
capture?: boolean;
|
|
3
|
+
signal?: AbortSignal;
|
|
4
|
+
};
|
|
5
|
+
export type DockerRunner = (args: string[], options?: DockerOptions) => Promise<string>;
|
|
6
|
+
export declare function throwIfAborted(signal?: AbortSignal): void;
|
|
7
|
+
export declare const docker: DockerRunner;
|
package/dist/docker.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
export function throwIfAborted(signal) {
|
|
3
|
+
if (!signal?.aborted)
|
|
4
|
+
return;
|
|
5
|
+
if (signal.reason instanceof Error)
|
|
6
|
+
throw signal.reason;
|
|
7
|
+
const suffix = signal.reason ? ` (${String(signal.reason)})` : "";
|
|
8
|
+
const error = new Error(`Operation aborted${suffix}.`);
|
|
9
|
+
error.name = "AbortError";
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
export const docker = (args, { capture = false, signal } = {}) => new Promise((resolve, reject) => {
|
|
13
|
+
const options = {
|
|
14
|
+
stdio: capture ? ["ignore", "pipe", "pipe"] : "inherit",
|
|
15
|
+
windowsHide: true,
|
|
16
|
+
};
|
|
17
|
+
if (signal)
|
|
18
|
+
options.signal = signal;
|
|
19
|
+
const child = spawn("docker", args, options);
|
|
20
|
+
let stdout = "";
|
|
21
|
+
let stderr = "";
|
|
22
|
+
let settled = false;
|
|
23
|
+
const fail = (error) => {
|
|
24
|
+
if (settled)
|
|
25
|
+
return;
|
|
26
|
+
settled = true;
|
|
27
|
+
reject(error);
|
|
28
|
+
};
|
|
29
|
+
if (capture && child.stdout && child.stderr) {
|
|
30
|
+
child.stdout.setEncoding("utf8");
|
|
31
|
+
child.stderr.setEncoding("utf8");
|
|
32
|
+
child.stdout.on("data", (chunk) => { stdout += chunk; });
|
|
33
|
+
child.stderr.on("data", (chunk) => { stderr += chunk; });
|
|
34
|
+
}
|
|
35
|
+
child.once("error", (error) => {
|
|
36
|
+
if (error.code === "ENOENT") {
|
|
37
|
+
fail(new Error("Docker was not found. Install Docker Desktop or Docker Engine and make sure `docker` is on PATH."));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
fail(error);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
child.once("close", (code, signalName) => {
|
|
44
|
+
if (settled)
|
|
45
|
+
return;
|
|
46
|
+
if (code === 0) {
|
|
47
|
+
settled = true;
|
|
48
|
+
resolve(stdout.trim());
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const detail = (stderr || stdout).trim();
|
|
52
|
+
fail(new Error(`docker ${args[0]} failed${signalName ? ` (${signalName})` : ` with exit code ${code}`}${detail ? `:\n${detail}` : "."}`));
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type EmulatorEnvironment = {
|
|
2
|
+
AWS_ENDPOINT_URL: string;
|
|
3
|
+
AWS_SHARED_CREDENTIALS_FILE: string;
|
|
4
|
+
AWS_CONFIG_FILE: string;
|
|
5
|
+
AWS_CA_BUNDLE: string;
|
|
6
|
+
AWS_EC2_METADATA_DISABLED: "true";
|
|
7
|
+
AWS_REGION: string;
|
|
8
|
+
AWS_DEFAULT_REGION: string;
|
|
9
|
+
NODE_EXTRA_CA_CERTS: string;
|
|
10
|
+
};
|
|
11
|
+
export type EmulatorConnectionSettings = {
|
|
12
|
+
endpointUrl: string;
|
|
13
|
+
healthCheckUrl: string;
|
|
14
|
+
configFile: string;
|
|
15
|
+
credentialsFile: string;
|
|
16
|
+
caFile: string;
|
|
17
|
+
environment: EmulatorEnvironment;
|
|
18
|
+
};
|
|
19
|
+
export declare function createEmulatorConnectionSettings(port: number, stateDirectory: string): EmulatorConnectionSettings;
|
|
20
|
+
export declare function createChildEnvironment(parentEnvironment: NodeJS.ProcessEnv, emulatorEnvironment: EmulatorEnvironment): NodeJS.ProcessEnv;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
const AWS_CREDENTIAL_ENVIRONMENT_KEYS = [
|
|
3
|
+
"AWS_ACCESS_KEY_ID",
|
|
4
|
+
"AWS_SECRET_ACCESS_KEY",
|
|
5
|
+
"AWS_SESSION_TOKEN",
|
|
6
|
+
"AWS_PROFILE",
|
|
7
|
+
"AWS_DEFAULT_PROFILE",
|
|
8
|
+
"AWS_ROLE_ARN",
|
|
9
|
+
"AWS_WEB_IDENTITY_TOKEN_FILE",
|
|
10
|
+
"AWS_CONTAINER_CREDENTIALS_FULL_URI",
|
|
11
|
+
"AWS_CONTAINER_CREDENTIALS_RELATIVE_URI",
|
|
12
|
+
"AWS_CONTAINER_AUTHORIZATION_TOKEN",
|
|
13
|
+
"AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE",
|
|
14
|
+
"AWS_IGNORE_CONFIGURED_ENDPOINT_URLS",
|
|
15
|
+
];
|
|
16
|
+
export function createEmulatorConnectionSettings(port, stateDirectory) {
|
|
17
|
+
const endpointUrl = `https://127.0.0.1:${port}`;
|
|
18
|
+
const portDirectory = path.join(stateDirectory, String(port));
|
|
19
|
+
const environment = {
|
|
20
|
+
AWS_ENDPOINT_URL: endpointUrl,
|
|
21
|
+
AWS_SHARED_CREDENTIALS_FILE: path.join(portDirectory, "emulator_credentials"),
|
|
22
|
+
AWS_CONFIG_FILE: path.join(portDirectory, "config"),
|
|
23
|
+
AWS_CA_BUNDLE: path.join(portDirectory, "ca.pem"),
|
|
24
|
+
AWS_EC2_METADATA_DISABLED: "true",
|
|
25
|
+
AWS_REGION: "us-east-1",
|
|
26
|
+
AWS_DEFAULT_REGION: "us-east-1",
|
|
27
|
+
NODE_EXTRA_CA_CERTS: path.join(portDirectory, "ca.pem"),
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
endpointUrl,
|
|
31
|
+
healthCheckUrl: `${endpointUrl}/__admin/health`,
|
|
32
|
+
configFile: environment.AWS_CONFIG_FILE,
|
|
33
|
+
credentialsFile: environment.AWS_SHARED_CREDENTIALS_FILE,
|
|
34
|
+
caFile: environment.AWS_CA_BUNDLE,
|
|
35
|
+
environment,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function createChildEnvironment(parentEnvironment, emulatorEnvironment) {
|
|
39
|
+
const childEnvironment = { ...parentEnvironment, ...emulatorEnvironment };
|
|
40
|
+
for (const key of AWS_CREDENTIAL_ENVIRONMENT_KEYS)
|
|
41
|
+
delete childEnvironment[key];
|
|
42
|
+
for (const key of Object.keys(childEnvironment)) {
|
|
43
|
+
if (key.startsWith("AWS_ENDPOINT_URL_"))
|
|
44
|
+
delete childEnvironment[key];
|
|
45
|
+
}
|
|
46
|
+
return childEnvironment;
|
|
47
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type EmulatorArchitecture = {
|
|
2
|
+
docker: "amd64" | "arm64";
|
|
3
|
+
rpm: "x86_64" | "aarch64";
|
|
4
|
+
};
|
|
5
|
+
export declare function getArchitecture(): EmulatorArchitecture;
|
|
6
|
+
export declare function ensureEmulatorImage(version: string, architecture: EmulatorArchitecture, signal?: AbortSignal): Promise<string>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { docker, throwIfAborted } from "./docker.js";
|
|
5
|
+
const PACKAGE_ROOT = "https://downloads.aws.com/aws-emulator/packages/aws-emulator/LTS";
|
|
6
|
+
const PACKAGE_SIGNING_KEY = "https://downloads.aws.com/keys/gpg/current.asc";
|
|
7
|
+
const PACKAGE_SIGNING_FINGERPRINT = "98BCF336A3BE53B734E967C52E5BF49BD940F624";
|
|
8
|
+
const CONTAINER_PORT = 9297;
|
|
9
|
+
export function getArchitecture() {
|
|
10
|
+
if (process.arch === "x64")
|
|
11
|
+
return { docker: "amd64", rpm: "x86_64" };
|
|
12
|
+
if (process.arch === "arm64")
|
|
13
|
+
return { docker: "arm64", rpm: "aarch64" };
|
|
14
|
+
throw new Error(`AWS Emulator is not available for this architecture: ${process.arch}.`);
|
|
15
|
+
}
|
|
16
|
+
export async function ensureEmulatorImage(version, architecture, signal) {
|
|
17
|
+
throwIfAborted(signal);
|
|
18
|
+
const image = `awesome-emulator-cli:${version}-${architecture.docker}`;
|
|
19
|
+
try {
|
|
20
|
+
await docker(["image", "inspect", image], { capture: true, signal });
|
|
21
|
+
throwIfAborted(signal);
|
|
22
|
+
return image;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
throwIfAborted(signal);
|
|
26
|
+
// Build the official Linux package into a local image on first use.
|
|
27
|
+
}
|
|
28
|
+
const context = await mkdtemp(path.join(os.tmpdir(), "awesome-emulator-cli-"));
|
|
29
|
+
const dockerfile = `FROM public.ecr.aws/amazonlinux/amazonlinux:2023-minimal
|
|
30
|
+
ARG AWS_EMULATOR_VERSION
|
|
31
|
+
ARG AWS_EMULATOR_RPM_ARCH
|
|
32
|
+
RUN set -eu; \\
|
|
33
|
+
export GNUPGHOME=/tmp/aws-emulator-gnupg; \\
|
|
34
|
+
mkdir -m 0700 "$GNUPGHOME"; \\
|
|
35
|
+
dnf swap -y gnupg2-minimal gnupg2-full; \\
|
|
36
|
+
curl -fsSLo /tmp/aws-emulator.rpm ${PACKAGE_ROOT}/aws-emulator-\${AWS_EMULATOR_VERSION}-1-\${AWS_EMULATOR_RPM_ARCH}.rpm; \\
|
|
37
|
+
curl -fsSLo /tmp/aws-emulator.rpm.sig ${PACKAGE_ROOT}/aws-emulator-\${AWS_EMULATOR_VERSION}-1-\${AWS_EMULATOR_RPM_ARCH}.rpm.sig; \\
|
|
38
|
+
curl -fsSLo /tmp/aws-publishing.asc ${PACKAGE_SIGNING_KEY}; \\
|
|
39
|
+
gpg --batch --import /tmp/aws-publishing.asc; \\
|
|
40
|
+
fingerprint="$(gpg --batch --with-colons --fingerprint ssm-publishing@amazon.com | awk -F: '$1 == "fpr" { print $10; exit }')"; \\
|
|
41
|
+
test "$fingerprint" = "${PACKAGE_SIGNING_FINGERPRINT}"; \\
|
|
42
|
+
gpg --batch --verify /tmp/aws-emulator.rpm.sig /tmp/aws-emulator.rpm; \\
|
|
43
|
+
dnf remove -y gnupg2-smime; \\
|
|
44
|
+
dnf swap -y gnupg2-full gnupg2-minimal; \\
|
|
45
|
+
rpm -i /tmp/aws-emulator.rpm; \\
|
|
46
|
+
mkdir -p /home/emulator/.aws/emulator; \\
|
|
47
|
+
chown -R 1000:1000 /home/emulator; \\
|
|
48
|
+
rm -rf "$GNUPGHOME" /tmp/aws-emulator.rpm /tmp/aws-emulator.rpm.sig /tmp/aws-publishing.asc; \\
|
|
49
|
+
dnf clean all
|
|
50
|
+
USER 1000:1000
|
|
51
|
+
ENV HOME=/home/emulator
|
|
52
|
+
EXPOSE ${CONTAINER_PORT}
|
|
53
|
+
ENTRYPOINT ["aws-emulator", "-b", "0.0.0.0"]
|
|
54
|
+
`;
|
|
55
|
+
try {
|
|
56
|
+
throwIfAborted(signal);
|
|
57
|
+
await writeFile(path.join(context, "Dockerfile"), dockerfile, "utf8");
|
|
58
|
+
await docker([
|
|
59
|
+
"build",
|
|
60
|
+
"--platform", `linux/${architecture.docker}`,
|
|
61
|
+
"--build-arg", `AWS_EMULATOR_VERSION=${version}`,
|
|
62
|
+
"--build-arg", `AWS_EMULATOR_RPM_ARCH=${architecture.rpm}`,
|
|
63
|
+
"--tag", image,
|
|
64
|
+
context,
|
|
65
|
+
], { signal });
|
|
66
|
+
throwIfAborted(signal);
|
|
67
|
+
return image;
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
await rm(context, { recursive: true, force: true });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type StartEmulatorOptions = {
|
|
2
|
+
version?: string;
|
|
3
|
+
port?: number | string;
|
|
4
|
+
telemetry?: string;
|
|
5
|
+
memory?: string;
|
|
6
|
+
startupTimeoutMs?: number;
|
|
7
|
+
signal?: AbortSignal;
|
|
8
|
+
};
|
|
9
|
+
export type ResolvedEmulatorOptions = {
|
|
10
|
+
version: string;
|
|
11
|
+
requestedPort: number;
|
|
12
|
+
telemetry: "on" | "off";
|
|
13
|
+
memory: string;
|
|
14
|
+
startupTimeoutMs: number;
|
|
15
|
+
};
|
|
16
|
+
export declare function resolveEmulatorOptions(options: StartEmulatorOptions, environment?: NodeJS.ProcessEnv): ResolvedEmulatorOptions;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const DEFAULT_VERSION = "1.0.522";
|
|
2
|
+
const DEFAULT_PORT = 9297;
|
|
3
|
+
const DEFAULT_MEMORY = "2g";
|
|
4
|
+
function normalizeVersion(value, environment) {
|
|
5
|
+
const version = value ?? environment.AWS_EMULATOR_VERSION ?? DEFAULT_VERSION;
|
|
6
|
+
if (!/^\d+\.\d+\.\d+$/.test(String(version))) {
|
|
7
|
+
throw new Error(`Invalid AWS Emulator version: ${version}. Use a version such as 1.0.522.`);
|
|
8
|
+
}
|
|
9
|
+
return String(version);
|
|
10
|
+
}
|
|
11
|
+
function normalizePort(value, environment) {
|
|
12
|
+
const port = Number(value ?? environment.AWS_EMULATOR_PORT ?? DEFAULT_PORT);
|
|
13
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
14
|
+
throw new Error(`Invalid port: ${value}. Use a number from 0 to 65535.`);
|
|
15
|
+
}
|
|
16
|
+
return port;
|
|
17
|
+
}
|
|
18
|
+
function normalizeTelemetry(value, environment) {
|
|
19
|
+
const telemetry = String(value ?? environment.EMULATOR_TELEMETRY ?? "on").toLowerCase();
|
|
20
|
+
if (["on", "true", "1"].includes(telemetry))
|
|
21
|
+
return "on";
|
|
22
|
+
if (["off", "false", "0"].includes(telemetry))
|
|
23
|
+
return "off";
|
|
24
|
+
throw new Error(`Invalid telemetry setting: ${value}. Use on or off.`);
|
|
25
|
+
}
|
|
26
|
+
function normalizeMemory(value) {
|
|
27
|
+
const memory = String(value ?? DEFAULT_MEMORY);
|
|
28
|
+
if (!/^\d+(?:\.\d+)?[kmgt]$/i.test(memory) || Number.parseFloat(memory) <= 0) {
|
|
29
|
+
throw new Error(`Invalid memory limit: ${value}. Use a Docker memory value such as 2g.`);
|
|
30
|
+
}
|
|
31
|
+
return memory;
|
|
32
|
+
}
|
|
33
|
+
function normalizeStartupTimeout(value) {
|
|
34
|
+
const timeout = Number(value ?? 120_000);
|
|
35
|
+
if (!Number.isInteger(timeout) || timeout < 1000) {
|
|
36
|
+
throw new Error("startupTimeoutMs must be an integer of at least 1000.");
|
|
37
|
+
}
|
|
38
|
+
return timeout;
|
|
39
|
+
}
|
|
40
|
+
export function resolveEmulatorOptions(options, environment = process.env) {
|
|
41
|
+
return {
|
|
42
|
+
version: normalizeVersion(options.version, environment),
|
|
43
|
+
requestedPort: normalizePort(options.port, environment),
|
|
44
|
+
telemetry: normalizeTelemetry(options.telemetry, environment),
|
|
45
|
+
memory: normalizeMemory(options.memory),
|
|
46
|
+
startupTimeoutMs: normalizeStartupTimeout(options.startupTimeoutMs),
|
|
47
|
+
};
|
|
48
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type EmulatorEnvironment } from "./emulator-environment.js";
|
|
2
|
+
import { type StartEmulatorOptions } from "./emulator-options.js";
|
|
3
|
+
export type { StartEmulatorOptions } from "./emulator-options.js";
|
|
4
|
+
export type { EmulatorEnvironment } from "./emulator-environment.js";
|
|
5
|
+
export type EmulatorSession = {
|
|
6
|
+
version: string;
|
|
7
|
+
port: number;
|
|
8
|
+
endpointUrl: string;
|
|
9
|
+
healthCheckUrl: string;
|
|
10
|
+
configFile: string;
|
|
11
|
+
credentialsFile: string;
|
|
12
|
+
caFile: string;
|
|
13
|
+
environment: EmulatorEnvironment;
|
|
14
|
+
containerId: string;
|
|
15
|
+
stop: () => Promise<void>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Start AWS Emulator in a local Docker container.
|
|
19
|
+
*
|
|
20
|
+
* The returned object includes SDK environment variables and an idempotent
|
|
21
|
+
* stop() function. Port 0 asks the wrapper to choose an available local port.
|
|
22
|
+
*/
|
|
23
|
+
export declare function startEmulator(options?: StartEmulatorOptions): Promise<EmulatorSession>;
|
|
24
|
+
/**
|
|
25
|
+
* Run a child process with AWS Emulator settings and stop the emulator afterward.
|
|
26
|
+
*/
|
|
27
|
+
export declare function runWithEmulator(command: string, args?: string[], options?: StartEmulatorOptions): Promise<number>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile } from "node:fs/promises";
|
|
3
|
+
import https from "node:https";
|
|
4
|
+
import net from "node:net";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { createRetryableStop, startContainer, stopContainer, withEmulator } from "./container-lifecycle.js";
|
|
8
|
+
import { docker, throwIfAborted } from "./docker.js";
|
|
9
|
+
import { createChildEnvironment, createEmulatorConnectionSettings } from "./emulator-environment.js";
|
|
10
|
+
import { ensureEmulatorImage, getArchitecture } from "./emulator-image.js";
|
|
11
|
+
import { resolveEmulatorOptions } from "./emulator-options.js";
|
|
12
|
+
import { runCommand } from "./process.js";
|
|
13
|
+
async function getAvailablePort() {
|
|
14
|
+
const server = net.createServer();
|
|
15
|
+
await new Promise((resolve, reject) => {
|
|
16
|
+
server.once("error", reject);
|
|
17
|
+
server.listen(0, "127.0.0.1", resolve);
|
|
18
|
+
});
|
|
19
|
+
const address = server.address();
|
|
20
|
+
if (!address || typeof address === "string")
|
|
21
|
+
throw new Error("Could not determine an available local port.");
|
|
22
|
+
const { port } = address;
|
|
23
|
+
await new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve()));
|
|
24
|
+
return port;
|
|
25
|
+
}
|
|
26
|
+
function requestHealth(port, ca, signal) {
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
const request = https.get({
|
|
29
|
+
hostname: "127.0.0.1",
|
|
30
|
+
port,
|
|
31
|
+
path: "/__admin/health",
|
|
32
|
+
ca,
|
|
33
|
+
timeout: 1000,
|
|
34
|
+
signal,
|
|
35
|
+
}, (response) => {
|
|
36
|
+
let body = "";
|
|
37
|
+
response.setEncoding("utf8");
|
|
38
|
+
response.on("data", (chunk) => { body += chunk; });
|
|
39
|
+
response.on("end", () => resolve(response.statusCode === 200 && body.trim() === "ok"));
|
|
40
|
+
});
|
|
41
|
+
request.once("error", () => resolve(false));
|
|
42
|
+
request.once("timeout", () => request.destroy());
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const delay = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
46
|
+
function createContainerRunArguments({ architecture, containerName, image, memory, port, stateDirectory, telemetry, }) {
|
|
47
|
+
const hostUser = process.platform === "linux"
|
|
48
|
+
&& typeof process.getuid === "function"
|
|
49
|
+
&& typeof process.getgid === "function"
|
|
50
|
+
? ["--user", `${process.getuid()}:${process.getgid()}`]
|
|
51
|
+
: [];
|
|
52
|
+
return [
|
|
53
|
+
"run", "--detach",
|
|
54
|
+
"--platform", `linux/${architecture.docker}`,
|
|
55
|
+
...hostUser,
|
|
56
|
+
"--memory", memory,
|
|
57
|
+
"--memory-swap", memory,
|
|
58
|
+
"--publish", `127.0.0.1:${port}:${port}`,
|
|
59
|
+
"--mount", `type=bind,source=${stateDirectory},target=/home/emulator/.aws/emulator`,
|
|
60
|
+
"--env", "HOME=/home/emulator",
|
|
61
|
+
"--name", containerName,
|
|
62
|
+
image,
|
|
63
|
+
"-p", String(port),
|
|
64
|
+
"-t", telemetry,
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
async function waitUntilHealthy(containerId, port, stateDirectory, timeoutMs, signal) {
|
|
68
|
+
const deadline = Date.now() + timeoutMs;
|
|
69
|
+
const caFile = path.join(stateDirectory, String(port), "ca.pem");
|
|
70
|
+
while (Date.now() < deadline) {
|
|
71
|
+
throwIfAborted(signal);
|
|
72
|
+
try {
|
|
73
|
+
const ca = await readFile(caFile);
|
|
74
|
+
if (await requestHealth(port, ca, signal))
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// The container creates its certificate before the health route is ready.
|
|
79
|
+
}
|
|
80
|
+
throwIfAborted(signal);
|
|
81
|
+
try {
|
|
82
|
+
const running = await docker(["inspect", "--format", "{{.State.Running}}", containerId], { capture: true, signal });
|
|
83
|
+
if (running !== "true") {
|
|
84
|
+
const logs = await docker(["logs", "--tail", "100", containerId], { capture: true }).catch(() => "");
|
|
85
|
+
throw new Error(`AWS Emulator container exited before becoming healthy.${logs ? `\n${logs}` : ""}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throwIfAborted(signal);
|
|
90
|
+
if (error instanceof Error && error.message.startsWith("AWS Emulator container exited"))
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
await delay(250);
|
|
94
|
+
}
|
|
95
|
+
throwIfAborted(signal);
|
|
96
|
+
const logs = await docker(["logs", "--tail", "100", containerId], { capture: true }).catch(() => "");
|
|
97
|
+
throw new Error(`AWS Emulator did not become healthy within ${timeoutMs} ms.${logs ? `\n${logs}` : ""}`);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Start AWS Emulator in a local Docker container.
|
|
101
|
+
*
|
|
102
|
+
* The returned object includes SDK environment variables and an idempotent
|
|
103
|
+
* stop() function. Port 0 asks the wrapper to choose an available local port.
|
|
104
|
+
*/
|
|
105
|
+
export async function startEmulator(options = {}) {
|
|
106
|
+
throwIfAborted(options.signal);
|
|
107
|
+
const { version, requestedPort, telemetry, memory, startupTimeoutMs } = resolveEmulatorOptions(options);
|
|
108
|
+
const port = requestedPort === 0 ? await getAvailablePort() : requestedPort;
|
|
109
|
+
throwIfAborted(options.signal);
|
|
110
|
+
const architecture = getArchitecture();
|
|
111
|
+
const stateDirectory = path.join(os.homedir(), ".aws", "emulator");
|
|
112
|
+
await mkdir(stateDirectory, { recursive: true, mode: 0o700 });
|
|
113
|
+
const image = await ensureEmulatorImage(version, architecture, options.signal);
|
|
114
|
+
throwIfAborted(options.signal);
|
|
115
|
+
const containerName = `awesome-emulator-cli-${process.pid}-${randomBytes(4).toString("hex")}`;
|
|
116
|
+
const runArguments = createContainerRunArguments({
|
|
117
|
+
architecture,
|
|
118
|
+
containerName,
|
|
119
|
+
image,
|
|
120
|
+
memory,
|
|
121
|
+
port,
|
|
122
|
+
stateDirectory,
|
|
123
|
+
telemetry,
|
|
124
|
+
});
|
|
125
|
+
const runningContainerId = await startContainer(containerName, runArguments, options.signal, (id, signal) => waitUntilHealthy(id, port, stateDirectory, startupTimeoutMs, signal));
|
|
126
|
+
const stop = createRetryableStop(() => stopContainer(runningContainerId));
|
|
127
|
+
try {
|
|
128
|
+
throwIfAborted(options.signal);
|
|
129
|
+
}
|
|
130
|
+
catch (startupError) {
|
|
131
|
+
try {
|
|
132
|
+
await stop();
|
|
133
|
+
}
|
|
134
|
+
catch (cleanupError) {
|
|
135
|
+
throw new AggregateError([startupError, cleanupError], `AWS Emulator startup was interrupted and container cleanup also failed (${runningContainerId}).`);
|
|
136
|
+
}
|
|
137
|
+
throw startupError;
|
|
138
|
+
}
|
|
139
|
+
const connectionSettings = createEmulatorConnectionSettings(port, stateDirectory);
|
|
140
|
+
return {
|
|
141
|
+
version,
|
|
142
|
+
port,
|
|
143
|
+
...connectionSettings,
|
|
144
|
+
containerId: runningContainerId,
|
|
145
|
+
stop,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Run a child process with AWS Emulator settings and stop the emulator afterward.
|
|
150
|
+
*/
|
|
151
|
+
export async function runWithEmulator(command, args = [], options = {}) {
|
|
152
|
+
if (!command)
|
|
153
|
+
throw new Error("A command is required.");
|
|
154
|
+
return withEmulator(() => startEmulator(options), async (emulator) => {
|
|
155
|
+
const childEnvironment = createChildEnvironment(process.env, emulator.environment);
|
|
156
|
+
return runCommand(command, args, childEnvironment, options.signal);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runCommand(command: string, args: string[], env: NodeJS.ProcessEnv, signal?: AbortSignal): Promise<number>;
|
package/dist/process.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { FORWARDED_SIGNALS, getSignalExitCode, isForwardedSignal } from "./signals.js";
|
|
3
|
+
export function runCommand(command, args, env, signal) {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
const child = spawn(command, args, {
|
|
6
|
+
env,
|
|
7
|
+
stdio: "inherit",
|
|
8
|
+
shell: process.platform === "win32",
|
|
9
|
+
windowsHide: false,
|
|
10
|
+
});
|
|
11
|
+
let receivedSignal;
|
|
12
|
+
let settled = false;
|
|
13
|
+
const handlers = new Map();
|
|
14
|
+
let abortHandler;
|
|
15
|
+
const cleanup = () => {
|
|
16
|
+
for (const [signal, handler] of handlers)
|
|
17
|
+
process.removeListener(signal, handler);
|
|
18
|
+
if (abortHandler)
|
|
19
|
+
signal?.removeEventListener("abort", abortHandler);
|
|
20
|
+
};
|
|
21
|
+
const forwardSignal = (signal) => {
|
|
22
|
+
if (receivedSignal)
|
|
23
|
+
return;
|
|
24
|
+
receivedSignal = signal;
|
|
25
|
+
child.kill(signal);
|
|
26
|
+
};
|
|
27
|
+
for (const signal of FORWARDED_SIGNALS) {
|
|
28
|
+
const handler = () => forwardSignal(signal);
|
|
29
|
+
handlers.set(signal, handler);
|
|
30
|
+
process.once(signal, handler);
|
|
31
|
+
}
|
|
32
|
+
if (signal) {
|
|
33
|
+
abortHandler = () => {
|
|
34
|
+
const reason = signal.reason;
|
|
35
|
+
const abortSignal = isForwardedSignal(reason) ? reason : "SIGTERM";
|
|
36
|
+
forwardSignal(abortSignal);
|
|
37
|
+
};
|
|
38
|
+
if (signal.aborted)
|
|
39
|
+
abortHandler();
|
|
40
|
+
else
|
|
41
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
42
|
+
}
|
|
43
|
+
child.once("error", (error) => {
|
|
44
|
+
if (settled)
|
|
45
|
+
return;
|
|
46
|
+
settled = true;
|
|
47
|
+
cleanup();
|
|
48
|
+
reject(error);
|
|
49
|
+
});
|
|
50
|
+
child.once("close", (code, signal) => {
|
|
51
|
+
if (settled)
|
|
52
|
+
return;
|
|
53
|
+
settled = true;
|
|
54
|
+
cleanup();
|
|
55
|
+
if (receivedSignal) {
|
|
56
|
+
resolve(getSignalExitCode(receivedSignal));
|
|
57
|
+
}
|
|
58
|
+
else if (signal) {
|
|
59
|
+
resolve(1);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
resolve(code ?? 1);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const FORWARDED_SIGNALS: readonly ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
2
|
+
export type ForwardedSignal = (typeof FORWARDED_SIGNALS)[number];
|
|
3
|
+
export declare function isForwardedSignal(value: unknown): value is ForwardedSignal;
|
|
4
|
+
export declare function getSignalExitCode(value: unknown): number;
|
package/dist/signals.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const FORWARDED_SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
2
|
+
const SIGNAL_EXIT_CODES = {
|
|
3
|
+
SIGHUP: 129,
|
|
4
|
+
SIGINT: 130,
|
|
5
|
+
SIGTERM: 143,
|
|
6
|
+
};
|
|
7
|
+
export function isForwardedSignal(value) {
|
|
8
|
+
return typeof value === "string" && Object.hasOwn(SIGNAL_EXIT_CODES, value);
|
|
9
|
+
}
|
|
10
|
+
export function getSignalExitCode(value) {
|
|
11
|
+
return isForwardedSignal(value) ? SIGNAL_EXIT_CODES[value] : 1;
|
|
12
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "awesome-emulator-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Awesome Emulator CLI for use with AWS Emulator in local tests from npm scripts or Node.js. It runs the upstream Linux package in Docker.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"awesome-emulator": "./bin/awesome-emulator.js"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"prebuild": "node scripts/clean-dist.js",
|
|
19
|
+
"build": "tsc -p tsconfig.json",
|
|
20
|
+
"test": "npm run build && node --test",
|
|
21
|
+
"test:integration": "node --test test/integration.test.js",
|
|
22
|
+
"prepare": "npm run build",
|
|
23
|
+
"prepack": "npm run build",
|
|
24
|
+
"check:release-version": "node scripts/check-release-version.js"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"bin/",
|
|
31
|
+
"dist/",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/c19yamamoto/awesome-emulator-cli.git"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/c19yamamoto/awesome-emulator-cli#readme",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/c19yamamoto/awesome-emulator-cli/issues"
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"aws",
|
|
45
|
+
"emulator",
|
|
46
|
+
"testing",
|
|
47
|
+
"integration-tests"
|
|
48
|
+
],
|
|
49
|
+
"license": "MIT",
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/node": "^26.6.6",
|
|
55
|
+
"typescript": "^7.0.2"
|
|
56
|
+
}
|
|
57
|
+
}
|