初始化环境文件

This commit is contained in:
CN-JS-HuiBai
2026-04-04 12:49:09 +08:00
parent 07742d2688
commit c607af6fac
5971 changed files with 515160 additions and 18 deletions

View File

@@ -0,0 +1,13 @@
export class DefaultIdentityProviderConfig {
authSchemes = new Map();
constructor(config) {
for (const [key, value] of Object.entries(config)) {
if (value !== undefined) {
this.authSchemes.set(key, value);
}
}
}
getIdentityProvider(schemeId) {
return this.authSchemes.get(schemeId);
}
}

View File

@@ -0,0 +1,34 @@
import { HttpRequest } from "@smithy/protocol-http";
import { HttpApiKeyAuthLocation } from "@smithy/types";
export class HttpApiKeyAuthSigner {
async sign(httpRequest, identity, signingProperties) {
if (!signingProperties) {
throw new Error("request could not be signed with `apiKey` since the `name` and `in` signer properties are missing");
}
if (!signingProperties.name) {
throw new Error("request could not be signed with `apiKey` since the `name` signer property is missing");
}
if (!signingProperties.in) {
throw new Error("request could not be signed with `apiKey` since the `in` signer property is missing");
}
if (!identity.apiKey) {
throw new Error("request could not be signed with `apiKey` since the `apiKey` is not defined");
}
const clonedRequest = HttpRequest.clone(httpRequest);
if (signingProperties.in === HttpApiKeyAuthLocation.QUERY) {
clonedRequest.query[signingProperties.name] = identity.apiKey;
}
else if (signingProperties.in === HttpApiKeyAuthLocation.HEADER) {
clonedRequest.headers[signingProperties.name] = signingProperties.scheme
? `${signingProperties.scheme} ${identity.apiKey}`
: identity.apiKey;
}
else {
throw new Error("request can only be signed with `apiKey` locations `query` or `header`, " +
"but found: `" +
signingProperties.in +
"`");
}
return clonedRequest;
}
}

View File

@@ -0,0 +1,11 @@
import { HttpRequest } from "@smithy/protocol-http";
export class HttpBearerAuthSigner {
async sign(httpRequest, identity, signingProperties) {
const clonedRequest = HttpRequest.clone(httpRequest);
if (!identity.token) {
throw new Error("request could not be signed with `token` since the `token` is not defined");
}
clonedRequest.headers["Authorization"] = `Bearer ${identity.token}`;
return clonedRequest;
}
}

View File

@@ -0,0 +1,3 @@
export * from "./httpApiKeyAuth";
export * from "./httpBearerAuth";
export * from "./noAuth";

View File

@@ -0,0 +1,5 @@
export class NoAuthSigner {
async sign(httpRequest, identity, signingProperties) {
return httpRequest;
}
}

View File

@@ -0,0 +1,3 @@
export * from "./DefaultIdentityProviderConfig";
export * from "./httpAuthSchemes";
export * from "./memoizeIdentityProvider";

View File

@@ -0,0 +1,55 @@
export const createIsIdentityExpiredFunction = (expirationMs) => function isIdentityExpired(identity) {
return doesIdentityRequireRefresh(identity) && identity.expiration.getTime() - Date.now() < expirationMs;
};
export const EXPIRATION_MS = 300_000;
export const isIdentityExpired = createIsIdentityExpiredFunction(EXPIRATION_MS);
export const doesIdentityRequireRefresh = (identity) => identity.expiration !== undefined;
export const memoizeIdentityProvider = (provider, isExpired, requiresRefresh) => {
if (provider === undefined) {
return undefined;
}
const normalizedProvider = typeof provider !== "function" ? async () => Promise.resolve(provider) : provider;
let resolved;
let pending;
let hasResult;
let isConstant = false;
const coalesceProvider = async (options) => {
if (!pending) {
pending = normalizedProvider(options);
}
try {
resolved = await pending;
hasResult = true;
isConstant = false;
}
finally {
pending = undefined;
}
return resolved;
};
if (isExpired === undefined) {
return async (options) => {
if (!hasResult || options?.forceRefresh) {
resolved = await coalesceProvider(options);
}
return resolved;
};
}
return async (options) => {
if (!hasResult || options?.forceRefresh) {
resolved = await coalesceProvider(options);
}
if (isConstant) {
return resolved;
}
if (!requiresRefresh(resolved)) {
isConstant = true;
return resolved;
}
if (isExpired(resolved)) {
await coalesceProvider(options);
return resolved;
}
return resolved;
};
};