@awsless/ecs 0.0.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/dist/index.cjs ADDED
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ECSClient: () => import_client_ecs4.ECSClient,
24
+ ecsClient: () => ecsClient,
25
+ mockEcs: () => mockEcs,
26
+ runTask: () => runTask
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+ var import_client_ecs4 = require("@aws-sdk/client-ecs");
30
+
31
+ // src/client.ts
32
+ var import_client_ecs = require("@aws-sdk/client-ecs");
33
+ var import_utils = require("@awsless/utils");
34
+ var ecsClient = (0, import_utils.globalClient)(() => {
35
+ return new import_client_ecs.ECSClient({});
36
+ });
37
+
38
+ // src/commands.ts
39
+ var import_client_ecs2 = require("@aws-sdk/client-ecs");
40
+ var import_json = require("@awsless/json");
41
+ var runTask = async ({
42
+ client = ecsClient(),
43
+ cluster,
44
+ taskDefinition,
45
+ subnets,
46
+ securityGroups,
47
+ container,
48
+ payload,
49
+ assignPublicIp = true
50
+ }) => {
51
+ const result = await client.send(
52
+ new import_client_ecs2.RunTaskCommand({
53
+ cluster,
54
+ taskDefinition,
55
+ launchType: "FARGATE",
56
+ networkConfiguration: {
57
+ awsvpcConfiguration: {
58
+ subnets,
59
+ securityGroups,
60
+ assignPublicIp: assignPublicIp ? "ENABLED" : "DISABLED"
61
+ }
62
+ },
63
+ overrides: {
64
+ containerOverrides: [
65
+ {
66
+ name: container,
67
+ environment: payload !== void 0 ? [
68
+ {
69
+ name: "PAYLOAD",
70
+ value: (0, import_json.stringify)(payload)
71
+ }
72
+ ] : []
73
+ }
74
+ ]
75
+ },
76
+ count: 1
77
+ })
78
+ );
79
+ if (result.failures && result.failures.length > 0) {
80
+ const { reason, detail } = result.failures[0];
81
+ throw new Error(`ECS RunTask failed: ${reason}${detail ? ` - ${detail}` : ""}`);
82
+ }
83
+ return { taskArn: result.tasks?.[0]?.taskArn };
84
+ };
85
+
86
+ // src/mock.ts
87
+ var import_client_ecs3 = require("@aws-sdk/client-ecs");
88
+ var import_json2 = require("@awsless/json");
89
+ var import_utils2 = require("@awsless/utils");
90
+ var import_aws_sdk_vitest_mock = require("aws-sdk-vitest-mock");
91
+ var globalList = {};
92
+ var mockEcs = (tasks) => {
93
+ const alreadyMocked = Object.keys(globalList).length > 0;
94
+ const list = (0, import_utils2.mockObjectValues)(tasks);
95
+ Object.assign(globalList, list);
96
+ if (alreadyMocked) {
97
+ return list;
98
+ }
99
+ const client = (0, import_aws_sdk_vitest_mock.mockClient)(import_client_ecs3.ECSClient);
100
+ client.on(import_client_ecs3.RunTaskCommand).callsFake(async (input) => {
101
+ const name = input.taskDefinition ?? "";
102
+ const callback = globalList[name];
103
+ if (!callback) {
104
+ throw new TypeError(`ECS mock function not defined for: ${name}`);
105
+ }
106
+ const envVars = input.overrides?.containerOverrides?.[0]?.environment ?? [];
107
+ const payloadEntry = envVars.find((e) => e.name === "PAYLOAD");
108
+ const payload = payloadEntry?.value ? (0, import_json2.parse)(payloadEntry.value) : void 0;
109
+ await (0, import_utils2.nextTick)(callback, payload);
110
+ return {
111
+ tasks: [
112
+ {
113
+ taskArn: `arn:aws:ecs:us-east-1:000000000000:task/mock/${name}`
114
+ }
115
+ ],
116
+ failures: []
117
+ };
118
+ });
119
+ beforeEach && beforeEach(() => {
120
+ Object.values(globalList).forEach((fn) => {
121
+ fn.mockClear();
122
+ });
123
+ });
124
+ return list;
125
+ };
126
+ // Annotate the CommonJS export names for ESM import in node:
127
+ 0 && (module.exports = {
128
+ ECSClient,
129
+ ecsClient,
130
+ mockEcs,
131
+ runTask
132
+ });
@@ -0,0 +1,28 @@
1
+ import { ECSClient } from '@aws-sdk/client-ecs';
2
+ export { ECSClient } from '@aws-sdk/client-ecs';
3
+
4
+ declare const ecsClient: {
5
+ (): ECSClient;
6
+ set(client: ECSClient): void;
7
+ };
8
+
9
+ type RunTaskOptions = {
10
+ client?: ECSClient;
11
+ cluster: string;
12
+ taskDefinition: string;
13
+ subnets: string[];
14
+ securityGroups: string[];
15
+ container: string;
16
+ payload?: unknown;
17
+ assignPublicIp?: boolean;
18
+ };
19
+ declare const runTask: ({ client, cluster, taskDefinition, subnets, securityGroups, container, payload, assignPublicIp, }: RunTaskOptions) => Promise<{
20
+ taskArn: string | undefined;
21
+ }>;
22
+
23
+ type Tasks = {
24
+ [key: string]: (payload: any) => unknown;
25
+ };
26
+ declare const mockEcs: <T extends Tasks>(tasks: T) => { [P in keyof T]: any; };
27
+
28
+ export { type RunTaskOptions, ecsClient, mockEcs, runTask };
@@ -0,0 +1,28 @@
1
+ import { ECSClient } from '@aws-sdk/client-ecs';
2
+ export { ECSClient } from '@aws-sdk/client-ecs';
3
+
4
+ declare const ecsClient: {
5
+ (): ECSClient;
6
+ set(client: ECSClient): void;
7
+ };
8
+
9
+ type RunTaskOptions = {
10
+ client?: ECSClient;
11
+ cluster: string;
12
+ taskDefinition: string;
13
+ subnets: string[];
14
+ securityGroups: string[];
15
+ container: string;
16
+ payload?: unknown;
17
+ assignPublicIp?: boolean;
18
+ };
19
+ declare const runTask: ({ client, cluster, taskDefinition, subnets, securityGroups, container, payload, assignPublicIp, }: RunTaskOptions) => Promise<{
20
+ taskArn: string | undefined;
21
+ }>;
22
+
23
+ type Tasks = {
24
+ [key: string]: (payload: any) => unknown;
25
+ };
26
+ declare const mockEcs: <T extends Tasks>(tasks: T) => { [P in keyof T]: any; };
27
+
28
+ export { type RunTaskOptions, ecsClient, mockEcs, runTask };
package/dist/index.js ADDED
@@ -0,0 +1,104 @@
1
+ // src/index.ts
2
+ import { ECSClient as ECSClient4 } from "@aws-sdk/client-ecs";
3
+
4
+ // src/client.ts
5
+ import { ECSClient } from "@aws-sdk/client-ecs";
6
+ import { globalClient } from "@awsless/utils";
7
+ var ecsClient = globalClient(() => {
8
+ return new ECSClient({});
9
+ });
10
+
11
+ // src/commands.ts
12
+ import { RunTaskCommand } from "@aws-sdk/client-ecs";
13
+ import { stringify } from "@awsless/json";
14
+ var runTask = async ({
15
+ client = ecsClient(),
16
+ cluster,
17
+ taskDefinition,
18
+ subnets,
19
+ securityGroups,
20
+ container,
21
+ payload,
22
+ assignPublicIp = true
23
+ }) => {
24
+ const result = await client.send(
25
+ new RunTaskCommand({
26
+ cluster,
27
+ taskDefinition,
28
+ launchType: "FARGATE",
29
+ networkConfiguration: {
30
+ awsvpcConfiguration: {
31
+ subnets,
32
+ securityGroups,
33
+ assignPublicIp: assignPublicIp ? "ENABLED" : "DISABLED"
34
+ }
35
+ },
36
+ overrides: {
37
+ containerOverrides: [
38
+ {
39
+ name: container,
40
+ environment: payload !== void 0 ? [
41
+ {
42
+ name: "PAYLOAD",
43
+ value: stringify(payload)
44
+ }
45
+ ] : []
46
+ }
47
+ ]
48
+ },
49
+ count: 1
50
+ })
51
+ );
52
+ if (result.failures && result.failures.length > 0) {
53
+ const { reason, detail } = result.failures[0];
54
+ throw new Error(`ECS RunTask failed: ${reason}${detail ? ` - ${detail}` : ""}`);
55
+ }
56
+ return { taskArn: result.tasks?.[0]?.taskArn };
57
+ };
58
+
59
+ // src/mock.ts
60
+ import { ECSClient as ECSClient3, RunTaskCommand as RunTaskCommand2 } from "@aws-sdk/client-ecs";
61
+ import { parse } from "@awsless/json";
62
+ import { mockObjectValues, nextTick } from "@awsless/utils";
63
+ import { mockClient } from "aws-sdk-vitest-mock";
64
+ var globalList = {};
65
+ var mockEcs = (tasks) => {
66
+ const alreadyMocked = Object.keys(globalList).length > 0;
67
+ const list = mockObjectValues(tasks);
68
+ Object.assign(globalList, list);
69
+ if (alreadyMocked) {
70
+ return list;
71
+ }
72
+ const client = mockClient(ECSClient3);
73
+ client.on(RunTaskCommand2).callsFake(async (input) => {
74
+ const name = input.taskDefinition ?? "";
75
+ const callback = globalList[name];
76
+ if (!callback) {
77
+ throw new TypeError(`ECS mock function not defined for: ${name}`);
78
+ }
79
+ const envVars = input.overrides?.containerOverrides?.[0]?.environment ?? [];
80
+ const payloadEntry = envVars.find((e) => e.name === "PAYLOAD");
81
+ const payload = payloadEntry?.value ? parse(payloadEntry.value) : void 0;
82
+ await nextTick(callback, payload);
83
+ return {
84
+ tasks: [
85
+ {
86
+ taskArn: `arn:aws:ecs:us-east-1:000000000000:task/mock/${name}`
87
+ }
88
+ ],
89
+ failures: []
90
+ };
91
+ });
92
+ beforeEach && beforeEach(() => {
93
+ Object.values(globalList).forEach((fn) => {
94
+ fn.mockClear();
95
+ });
96
+ });
97
+ return list;
98
+ };
99
+ export {
100
+ ECSClient4 as ECSClient,
101
+ ecsClient,
102
+ mockEcs,
103
+ runTask
104
+ };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@awsless/ecs",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/awsless/awsless.git"
9
+ },
10
+ "bugs": {
11
+ "url": "https://github.com/awsless/awsless/issues"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "main": "./dist/index.cjs",
20
+ "module": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "require": "./dist/index.cjs",
26
+ "import": "./dist/index.js"
27
+ }
28
+ },
29
+ "dependencies": {
30
+ "@aws-sdk/client-ecs": "3.1024.0",
31
+ "aws-sdk-vitest-mock": "1.0.55",
32
+ "@awsless/json": "^0.0.11",
33
+ "@awsless/utils": "^0.0.2"
34
+ },
35
+ "peerDependencies": {
36
+ "vitest": ">=4"
37
+ },
38
+ "scripts": {
39
+ "test": "pnpm vitest run",
40
+ "build": "pnpm tsup src/index.ts --format cjs,esm --dts --clean",
41
+ "prepublish": "if pnpm test; then pnpm build; else exit; fi"
42
+ }
43
+ }