akundigital 0.1.0 → 0.2.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/README.md +5 -1
- package/dist/auth.d.ts +5 -0
- package/dist/auth.js +68 -0
- package/dist/auth.js.map +1 -0
- package/dist/commands.d.ts +3 -2
- package/dist/commands.js +47 -7
- package/dist/commands.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/token-store.d.ts +19 -0
- package/dist/token-store.js +35 -0
- package/dist/token-store.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,10 +11,14 @@ npx akundigital help
|
|
|
11
11
|
npx akundigital version
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
The
|
|
14
|
+
The CLI provides these commands:
|
|
15
15
|
|
|
16
16
|
- `help` — show available commands
|
|
17
17
|
- `version` — show the installed version
|
|
18
|
+
- `login <email> <password>` — authenticate with AkunDigital and save tokens locally
|
|
19
|
+
- `profile` — fetch and print the current user profile
|
|
20
|
+
|
|
21
|
+
Authentication tokens are stored in `~/.config/akundigital/tokens.json`. Expired access tokens are refreshed automatically with the saved refresh token.
|
|
18
22
|
|
|
19
23
|
Unknown commands return a non-zero exit code and display the usage guide. Future commands can be registered in `src/commands.ts`.
|
|
20
24
|
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { TokenSet, TokenStore } from "./token-store.js";
|
|
2
|
+
type FetchLike = typeof fetch;
|
|
3
|
+
export declare const login: (email: string, password: string, tokenStore: TokenStore, fetchImpl?: FetchLike, clientId?: string) => Promise<TokenSet>;
|
|
4
|
+
export declare const getProfile: (tokenStore: TokenStore, fetchImpl?: FetchLike, clientId?: string, now?: number) => Promise<unknown>;
|
|
5
|
+
export {};
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createTokenSet, isTokenExpired } from "./token-store.js";
|
|
2
|
+
const cognitoEndpoint = "https://cognito-idp.ap-southeast-3.amazonaws.com/";
|
|
3
|
+
const defaultClientId = "6vcd500elmtpkiks9qp83vs8fh";
|
|
4
|
+
const profileEndpoint = "https://6cr9nj44pd.execute-api.ap-southeast-3.amazonaws.com/v1/profile";
|
|
5
|
+
const requestCognito = async (target, parameters, fetchImpl, clientId) => {
|
|
6
|
+
const response = await fetchImpl(cognitoEndpoint, {
|
|
7
|
+
method: "POST",
|
|
8
|
+
headers: {
|
|
9
|
+
"Content-Type": "application/x-amz-json-1.1",
|
|
10
|
+
"X-Amz-Target": `AWSCognitoIdentityProviderService.InitiateAuth`,
|
|
11
|
+
},
|
|
12
|
+
body: JSON.stringify({ AuthFlow: target, ClientId: clientId, AuthParameters: parameters }),
|
|
13
|
+
});
|
|
14
|
+
const body = await response.json();
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
throw new Error(body.message ?? "Authentication failed");
|
|
17
|
+
}
|
|
18
|
+
return body;
|
|
19
|
+
};
|
|
20
|
+
const getAuthenticationResult = (response) => {
|
|
21
|
+
if (!response.AuthenticationResult?.AccessToken) {
|
|
22
|
+
throw new Error(response.message ?? response.ChallengeName ?? "Authentication did not return tokens");
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
AccessToken: response.AuthenticationResult.AccessToken,
|
|
26
|
+
IdToken: response.AuthenticationResult.IdToken,
|
|
27
|
+
RefreshToken: response.AuthenticationResult.RefreshToken,
|
|
28
|
+
ExpiresIn: response.AuthenticationResult.ExpiresIn,
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export const login = async (email, password, tokenStore, fetchImpl = fetch, clientId = defaultClientId) => {
|
|
32
|
+
const response = await requestCognito("USER_PASSWORD_AUTH", { USERNAME: email, PASSWORD: password }, fetchImpl, clientId);
|
|
33
|
+
const result = getAuthenticationResult(response);
|
|
34
|
+
const tokens = createTokenSet(result, result.RefreshToken);
|
|
35
|
+
await tokenStore.save(tokens);
|
|
36
|
+
return tokens;
|
|
37
|
+
};
|
|
38
|
+
const refresh = async (tokens, tokenStore, fetchImpl, clientId) => {
|
|
39
|
+
const response = await requestCognito("REFRESH_TOKEN_AUTH", { REFRESH_TOKEN: tokens.refreshToken }, fetchImpl, clientId);
|
|
40
|
+
const result = getAuthenticationResult(response);
|
|
41
|
+
const refreshedTokens = createTokenSet(result, tokens.refreshToken);
|
|
42
|
+
await tokenStore.save(refreshedTokens);
|
|
43
|
+
return refreshedTokens;
|
|
44
|
+
};
|
|
45
|
+
export const getProfile = async (tokenStore, fetchImpl = fetch, clientId = defaultClientId, now = Date.now()) => {
|
|
46
|
+
let tokens = await tokenStore.load();
|
|
47
|
+
if (!tokens) {
|
|
48
|
+
throw new Error("Not logged in. Run `akundigital login <email> <password>` first.");
|
|
49
|
+
}
|
|
50
|
+
if (isTokenExpired(tokens, now)) {
|
|
51
|
+
tokens = await refresh(tokens, tokenStore, fetchImpl, clientId);
|
|
52
|
+
}
|
|
53
|
+
let response = await fetchImpl(profileEndpoint, {
|
|
54
|
+
headers: { Authorization: `Bearer ${tokens.accessToken}` },
|
|
55
|
+
});
|
|
56
|
+
if (response.status === 401) {
|
|
57
|
+
tokens = await refresh(tokens, tokenStore, fetchImpl, clientId);
|
|
58
|
+
response = await fetchImpl(profileEndpoint, {
|
|
59
|
+
headers: { Authorization: `Bearer ${tokens.accessToken}` },
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const body = await response.json();
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
throw new Error(body.error ?? "Failed to get profile");
|
|
65
|
+
}
|
|
66
|
+
return body.data ?? body;
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=auth.js.map
|
package/dist/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElE,MAAM,eAAe,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AM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC;IACtF,CAAC;IACD,IAAI,cAAc,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;QAChC,MAAM,GAAG,MAAM,OAAO,CAAC,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;IAClE,CAAC;IAED,IAAI,QAAQ,GAAG,MAAM,SAAS,CAAC,eAAe,EAAE;QAC9C,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,MAAM,CAAC,WAAW,EAAE,EAAE;KAC3D,CAAC,CAAC;IACH,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;QAC5B,MAAM,GAAG,MAAM,OAAO,CAAC,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;QAChE,QAAQ,GAAG,MAAM,SAAS,CAAC,eAAe,EAAE;YAC1C,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,MAAM,CAAC,WAAW,EAAE,EAAE;SAC3D,CAAC,CAAC;IACL,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAwC,CAAC;IACzE,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,uBAAuB,CAAC,CAAC;IACzD,CAAC;IACD,OAAO,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC;AAC3B,CAAC,CAAC"}
|
package/dist/commands.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export type CommandContext = {
|
|
2
2
|
args: string[];
|
|
3
3
|
output: (message: string) => void;
|
|
4
|
+
error: (message: string) => void;
|
|
4
5
|
};
|
|
5
6
|
export type Command = {
|
|
6
7
|
description: string;
|
|
7
|
-
run: (context: CommandContext) => number
|
|
8
|
+
run: (context: CommandContext) => Promise<number>;
|
|
8
9
|
};
|
|
9
10
|
export declare const createCommands: (version: string) => Record<string, Command>;
|
|
10
|
-
export declare const runCommand: (commandName: string | undefined, args: string[], version: string, output: (message: string) => void, error: (message: string) => void) => number
|
|
11
|
+
export declare const runCommand: (commandName: string | undefined, args: string[], version: string, output: (message: string) => void, error: (message: string) => void) => Promise<number>;
|
package/dist/commands.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
+
import { getProfile, login } from "./auth.js";
|
|
2
|
+
import { createTokenStore } from "./token-store.js";
|
|
1
3
|
export const createCommands = (version) => ({
|
|
2
4
|
help: {
|
|
3
5
|
description: "Show available commands",
|
|
4
|
-
run: ({ output }) => {
|
|
6
|
+
run: async ({ output }) => {
|
|
5
7
|
output([
|
|
6
8
|
"Usage: akundigital <command> [arguments]",
|
|
7
9
|
"",
|
|
8
10
|
"Commands:",
|
|
9
11
|
" help Show available commands",
|
|
10
12
|
" version Show the installed version",
|
|
13
|
+
" login Log in with email and password",
|
|
14
|
+
" profile Show the current user profile",
|
|
11
15
|
"",
|
|
12
16
|
"Run `akundigital <command> --help` for command-specific help.",
|
|
13
17
|
].join("\n"));
|
|
@@ -16,26 +20,62 @@ export const createCommands = (version) => ({
|
|
|
16
20
|
},
|
|
17
21
|
version: {
|
|
18
22
|
description: "Show the installed version",
|
|
19
|
-
run: ({ output }) => {
|
|
23
|
+
run: async ({ output }) => {
|
|
20
24
|
output(version);
|
|
21
25
|
return 0;
|
|
22
26
|
},
|
|
23
27
|
},
|
|
28
|
+
login: {
|
|
29
|
+
description: "Log in with email and password",
|
|
30
|
+
run: async ({ args, output, error }) => {
|
|
31
|
+
if (args.length !== 2 || args.includes("--help")) {
|
|
32
|
+
error("Usage: akundigital login <email> <password>");
|
|
33
|
+
return 1;
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
await login(args[0], args[1], createTokenStore());
|
|
37
|
+
output("Logged in successfully.");
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
catch (loginError) {
|
|
41
|
+
error(loginError instanceof Error ? loginError.message : "Authentication failed");
|
|
42
|
+
return 1;
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
profile: {
|
|
47
|
+
description: "Show the current user profile",
|
|
48
|
+
run: async ({ args, output, error }) => {
|
|
49
|
+
if (args.length !== 0) {
|
|
50
|
+
error("Usage: akundigital profile");
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const profile = await getProfile(createTokenStore());
|
|
55
|
+
output(JSON.stringify(profile, null, 2));
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
catch (profileError) {
|
|
59
|
+
error(profileError instanceof Error ? profileError.message : "Failed to get profile");
|
|
60
|
+
return 1;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
},
|
|
24
64
|
});
|
|
25
|
-
export const runCommand = (commandName, args, version, output, error) => {
|
|
65
|
+
export const runCommand = async (commandName, args, version, output, error) => {
|
|
26
66
|
const commands = createCommands(version);
|
|
27
67
|
if (!commandName || commandName === "--help" || commandName === "-h") {
|
|
28
|
-
return commands.help.run({ args, output });
|
|
68
|
+
return commands.help.run({ args, output, error });
|
|
29
69
|
}
|
|
30
70
|
if (commandName === "--version" || commandName === "-v") {
|
|
31
|
-
return commands.version.run({ args, output });
|
|
71
|
+
return commands.version.run({ args, output, error });
|
|
32
72
|
}
|
|
33
73
|
const command = commands[commandName];
|
|
34
74
|
if (!command) {
|
|
35
75
|
error(`Unknown command: ${commandName}`);
|
|
36
|
-
commands.help.run({ args: [], output });
|
|
76
|
+
await commands.help.run({ args: [], output, error });
|
|
37
77
|
return 1;
|
|
38
78
|
}
|
|
39
|
-
return command.run({ args, output });
|
|
79
|
+
return command.run({ args, output, error });
|
|
40
80
|
};
|
|
41
81
|
//# sourceMappingURL=commands.js.map
|
package/dist/commands.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commands.js","sourceRoot":"","sources":["../src/commands.ts"],"names":[],"mappings":"
|
|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EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;IACpD,CAAC;IAED,IAAI,WAAW,KAAK,WAAW,IAAI,WAAW,KAAK,IAAI,EAAE,CAAC;QACxD,OAAO,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,OAAO,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC;IACtC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,KAAK,CAAC,oBAAoB,WAAW,EAAE,CAAC,CAAC;QACzC,MAAM,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QACrD,OAAO,CAAC,CAAC;IACX,CAAC;IAED,OAAO,OAAO,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;AAC9C,CAAC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { createRequire } from "node:module";
|
|
|
3
3
|
import { runCommand } from "./commands.js";
|
|
4
4
|
const require = createRequire(import.meta.url);
|
|
5
5
|
const packageJson = require("../package.json");
|
|
6
|
-
const exitCode = runCommand(process.argv[2], process.argv.slice(3), packageJson.version, (message) => console.log(message), (message) => console.error(message));
|
|
6
|
+
const exitCode = await runCommand(process.argv[2], process.argv.slice(3), packageJson.version, (message) => console.log(message), (message) => console.error(message));
|
|
7
7
|
if (exitCode !== 0) {
|
|
8
8
|
process.exitCode = exitCode;
|
|
9
9
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAwB,CAAC;AAEtE,MAAM,QAAQ,GAAG,UAAU,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAwB,CAAC;AAEtE,MAAM,QAAQ,GAAG,MAAM,UAAU,CAC/B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EACf,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EACrB,WAAW,CAAC,OAAO,EACnB,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EACjC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CACpC,CAAC;AAEF,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;IACnB,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type TokenSet = {
|
|
2
|
+
accessToken: string;
|
|
3
|
+
idToken?: string;
|
|
4
|
+
refreshToken: string;
|
|
5
|
+
issuedAt: string;
|
|
6
|
+
expiresAt: string;
|
|
7
|
+
};
|
|
8
|
+
export type TokenStore = {
|
|
9
|
+
load: () => Promise<TokenSet | undefined>;
|
|
10
|
+
save: (tokens: TokenSet) => Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
export declare const createTokenStore: (path?: string) => TokenStore;
|
|
13
|
+
export declare const isTokenExpired: (tokens: TokenSet, now?: number) => boolean;
|
|
14
|
+
export declare const createTokenSet: (result: {
|
|
15
|
+
AccessToken: string;
|
|
16
|
+
IdToken?: string;
|
|
17
|
+
RefreshToken?: string;
|
|
18
|
+
ExpiresIn?: number;
|
|
19
|
+
}, refreshToken: string | undefined, now?: Date) => TokenSet;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
const tokenDirectory = join(homedir(), ".config", "akundigital");
|
|
5
|
+
const tokenPath = join(tokenDirectory, "tokens.json");
|
|
6
|
+
export const createTokenStore = (path = tokenPath) => ({
|
|
7
|
+
async load() {
|
|
8
|
+
try {
|
|
9
|
+
const content = await readFile(path, "utf8");
|
|
10
|
+
return JSON.parse(content);
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
if (error.code === "ENOENT") {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
async save(tokens) {
|
|
20
|
+
const directory = join(path, "..");
|
|
21
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
22
|
+
const temporaryPath = `${path}.tmp`;
|
|
23
|
+
await writeFile(temporaryPath, `${JSON.stringify(tokens, null, 2)}\n`, { mode: 0o600 });
|
|
24
|
+
await rename(temporaryPath, path);
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
export const isTokenExpired = (tokens, now = Date.now()) => new Date(tokens.expiresAt).getTime() <= now;
|
|
28
|
+
export const createTokenSet = (result, refreshToken, now = new Date()) => ({
|
|
29
|
+
accessToken: result.AccessToken,
|
|
30
|
+
idToken: result.IdToken,
|
|
31
|
+
refreshToken: result.RefreshToken ?? refreshToken ?? "",
|
|
32
|
+
issuedAt: now.toISOString(),
|
|
33
|
+
expiresAt: new Date(now.getTime() + (result.ExpiresIn ?? 3600) * 1000).toISOString(),
|
|
34
|
+
});
|
|
35
|
+
//# sourceMappingURL=token-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-store.js","sourceRoot":"","sources":["../src/token-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAejC,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;AACjE,MAAM,SAAS,GAAG,IAAI,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;AAEtD,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,IAAI,GAAG,SAAS,EAAc,EAAE,CAAC,CAAC;IACjE,KAAK,CAAC,IAAI;QACR,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YAC7C,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAa,CAAC;QACzC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBACvD,OAAO,SAAS,CAAC;YACnB,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,MAAM;QACf,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACnC,MAAM,KAAK,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QACzD,MAAM,aAAa,GAAG,GAAG,IAAI,MAAM,CAAC;QACpC,MAAM,S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