From 0d0d8c84ee4578a1674fb6065fb9c119366f1764 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 15 Mar 2026 08:22:19 +0800 Subject: [PATCH] update deno commons mod --- libraries/deno-commons-mod.ts | 120 +++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 51 deletions(-) diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index 42f220c..e7b3c87 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -1315,6 +1315,60 @@ export async function fetchAlibabaCloudInstanceIdentityV1( return await pkcs7Response.text(); } +export type InvokeMethod = "GET" | "POST"; + +export interface SimpleResponse { + status: number; + message: string; +} + +export async function invokeViaAlibabaCloudInstanceIdentity( + method: InvokeMethod, + path: string, + params?: string[][], + body?: string, + mode?: AlibabaCloudInstanceIdentityMode, +): Promise { + const pkcs7 = await fetchAlibabaCloudInstanceIdentityV1( + `hatter.ink:${path}`, + mode, + ); + const url = []; + url.push(`https://global.hatter.ink${path}`); + if (params) { + for (let i = 0; i < params.length; i++) { + const param = params[i]; + url.push((i == 0) ? "?" : "&"); + url.push(encodeURIComponent(param[0])); + url.push("="); + url.push(encodeURIComponent(param[1])); + } + } + const httpResponse = await fetchDataWithTimeout( + url.join(""), + { + method: method, + headers: { + "Authorization": `PKCS7 ${pkcs7}`, + }, + }, + ); + if (httpResponse.status != 200) { + throw new Error(`Get ${path} failed: ${httpResponse.status}`); + } + const simpleResponse = await httpResponse + .json() as SimpleResponse; + log.debug("simpleResponse", simpleResponse); + if (simpleResponse.status != 200) { + throw new Error( + `Get ${path} failed: ${simpleResponse.status}, raw: ${ + JSON.stringify(simpleResponse) + }`, + ); + } + return simpleResponse; +} + interface GetSecretResponse { status: number; message: string; @@ -1334,29 +1388,13 @@ export async function getSecretValueViaAlibabaCloudInstanceIdentity( key: string, mode?: AlibabaCloudInstanceIdentityMode, ): Promise { - const pkcs7 = await fetchAlibabaCloudInstanceIdentityV1( - "hatter.ink:/secret/get.json", + const secretResponse = await invokeViaAlibabaCloudInstanceIdentity( + "GET", + "/secret/get.json", + [["name", key]], + null, mode, - ); - const httpSecretResponse = await fetchDataWithTimeout( - `https://global.hatter.ink/secret/get.json?name=${ - encodeURIComponent(key) - }`, - { - headers: { - "Authorization": `PKCS7 ${pkcs7}`, - }, - }, - ); - if (httpSecretResponse.status != 200) { - throw new Error(`Get secret failed: ${httpSecretResponse.status}`); - } - const secretResponse = await httpSecretResponse - .json() as GetSecretResponse; - log.debug("secretResponse", secretResponse); - if (secretResponse.status != 200) { - throw new Error(`Get secret failed: ${secretResponse.status}`); - } + ) as GetSecretResponse; return secretResponse.data.value; } @@ -1412,39 +1450,19 @@ export async function assumeRoleByKeyViaAlibabaCloudInstanceIdentity( roleArn: string, mode?: AlibabaCloudInstanceIdentityMode, ): Promise { - const pkcs7 = await fetchAlibabaCloudInstanceIdentityV1( - "hatter.ink:/cloud/alibaba_cloud/assume_role_by_key.json", + const assumeRoleResponse = await invokeViaAlibabaCloudInstanceIdentity( + "GET", + "/cloud/alibaba_cloud/assume_role_by_key.json", + [["roleArn", roleArn]], + null, mode, - ); - const httpAssumeRoleResponse = await fetchDataWithTimeout( - `https://global.hatter.ink/cloud/alibaba_cloud/assume_role_by_key.json?roleArn=${ - encodeURIComponent(roleArn) - }`, - { - headers: { - "Authorization": `PKCS7 ${pkcs7}`, - }, - }, - ); - if (httpAssumeRoleResponse.status != 200) { - throw new Error( - `Assume role by key failed: ${httpAssumeRoleResponse.status}`, - ); - } - const assumeRoleResponse = await httpAssumeRoleResponse - .json() as AssumeRoleByKeyResponse; - log.debug("assumeRoleResponse", assumeRoleResponse); - if (assumeRoleResponse.status != 200) { - throw new Error( - `Assume role by key failed: ${assumeRoleResponse.status}, raw: ${ - JSON.stringify(assumeRoleResponse) - }`, - ); - } + ) as AssumeRoleByKeyResponse; return assumeRoleResponse.data; } -export async function assumeRoleByKeyViaHatterCli(roleArn: string): Promise { +export async function assumeRoleByKeyViaHatterCli( + roleArn: string, +): Promise { const output = await execCommand("hatter", [ "cloud-key", "assume-role",