Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,252 @@
|
||||
import { createHash, createHmac } from "node:crypto";
|
||||
|
||||
import {
|
||||
StorageAdapterError,
|
||||
createS3SignedUrlAdapter,
|
||||
createS3StorageAdapter,
|
||||
type CreateSignedDownloadUrlInput,
|
||||
type S3StorageAdapterConfig,
|
||||
type SignedDownloadSignerInput,
|
||||
type SignedDownloadUrlResult,
|
||||
type StorageAdapter,
|
||||
type UploadBody,
|
||||
} from "@toolctrl/storage";
|
||||
|
||||
import { getApiRuntimeConfig } from "./config.js";
|
||||
|
||||
/**
|
||||
* Caches the per-config S3 storage adapter, signed-URL adapter and the per-day
|
||||
* SigV4 signing key to avoid reconstructing them on every hot-path call.
|
||||
*
|
||||
* Cache key intentionally hashes the secret access key (never stores it raw)
|
||||
* and includes endpoint/region/accessKeyId/forcePathStyle so a credential
|
||||
* rotation transparently misses the cache and rebuilds the adapter.
|
||||
*/
|
||||
const storageAdapterCache = new Map<string, StorageAdapter>();
|
||||
const signedUrlAdapterCache = new Map<string, ReturnType<typeof createS3SignedUrlAdapter>>();
|
||||
const signingKeyCache = new Map<string, Buffer>();
|
||||
|
||||
function storageAdapterCacheKey(config: S3StorageAdapterConfig): string {
|
||||
const secretFingerprint = createHash("sha256").update(config.secretAccessKey ?? "").digest("hex");
|
||||
return createHash("sha256")
|
||||
.update(
|
||||
JSON.stringify({
|
||||
endpoint: config.endpoint,
|
||||
region: config.region,
|
||||
bucket: config.bucket,
|
||||
accessKeyId: config.accessKeyId,
|
||||
sessionToken: config.sessionToken ?? null,
|
||||
publicBaseUrl: config.publicBaseUrl ?? null,
|
||||
forcePathStyle: config.forcePathStyle ?? null,
|
||||
secretFingerprint,
|
||||
}),
|
||||
)
|
||||
.digest("hex");
|
||||
}
|
||||
|
||||
function getCachedStorageAdapter(config: S3StorageAdapterConfig): StorageAdapter {
|
||||
const key = storageAdapterCacheKey(config);
|
||||
let adapter = storageAdapterCache.get(key);
|
||||
if (!adapter) {
|
||||
adapter = createS3StorageAdapter(config);
|
||||
storageAdapterCache.set(key, adapter);
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
function getCachedSignedUrlAdapter(config: S3StorageAdapterConfig) {
|
||||
const key = storageAdapterCacheKey(config);
|
||||
let adapter = signedUrlAdapterCache.get(key);
|
||||
if (!adapter) {
|
||||
adapter = createS3SignedUrlAdapter({
|
||||
bucket: config.bucket,
|
||||
downloadSigner: (signerInput) => createS3SignedGetUrl(config, signerInput),
|
||||
uploadSigner: () => {
|
||||
throw new StorageAdapterError("signed_upload_failed", "Signed upload URLs are not supported by the API storage helper.", {
|
||||
operation: "createSignedUploadUrl",
|
||||
bucket: config.bucket,
|
||||
});
|
||||
},
|
||||
});
|
||||
signedUrlAdapterCache.set(key, adapter);
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
/** Test-only: drop cached adapters and signing keys. */
|
||||
export function resetStorageCachesForTests() {
|
||||
storageAdapterCache.clear();
|
||||
signedUrlAdapterCache.clear();
|
||||
signingKeyCache.clear();
|
||||
}
|
||||
|
||||
/** Test-only: inspect cache sizes for assertions. */
|
||||
export function storageCacheStatsForTests() {
|
||||
return {
|
||||
adapters: storageAdapterCache.size,
|
||||
signedUrlAdapters: signedUrlAdapterCache.size,
|
||||
signingKeys: signingKeyCache.size,
|
||||
};
|
||||
}
|
||||
|
||||
function createStorageAdapterConfig(bucket: string): S3StorageAdapterConfig {
|
||||
const { storage } = getApiRuntimeConfig();
|
||||
|
||||
return {
|
||||
endpoint: storage.endpoint ?? "",
|
||||
region: storage.region ?? "",
|
||||
bucket,
|
||||
accessKeyId: storage.accessKeyId ?? "",
|
||||
secretAccessKey: storage.secretAccessKey ?? "",
|
||||
sessionToken: storage.sessionToken,
|
||||
publicBaseUrl: storage.publicBaseUrl,
|
||||
forcePathStyle: storage.forcePathStyle === undefined ? undefined : storage.forcePathStyle.toLowerCase() === "true",
|
||||
};
|
||||
}
|
||||
|
||||
export async function createStorageFile(
|
||||
bucketId: string,
|
||||
path: string,
|
||||
file: UploadBody,
|
||||
contentType?: string,
|
||||
): Promise<{
|
||||
id: string;
|
||||
publicUrl: string | null;
|
||||
}> {
|
||||
const adapter = getCachedStorageAdapter(createStorageAdapterConfig(bucketId));
|
||||
const createdFile = await adapter.putObject({
|
||||
key: path,
|
||||
body: file,
|
||||
contentType,
|
||||
});
|
||||
|
||||
return {
|
||||
id: createdFile.key,
|
||||
publicUrl: createdFile.publicUrl,
|
||||
};
|
||||
}
|
||||
|
||||
export type CreateStorageSignedDownloadUrlInput = CreateSignedDownloadUrlInput;
|
||||
export type StorageSignedDownloadUrlResult = Pick<SignedDownloadUrlResult, "url" | "method" | "expiresAt" | "expiresInSeconds" | "headers">;
|
||||
|
||||
export async function createStorageSignedDownloadUrl(
|
||||
bucketId: string,
|
||||
input: CreateStorageSignedDownloadUrlInput,
|
||||
): Promise<StorageSignedDownloadUrlResult> {
|
||||
const adapterConfig = createStorageAdapterConfig(bucketId);
|
||||
const adapter = getCachedSignedUrlAdapter(adapterConfig);
|
||||
const result = await adapter.createSignedDownloadUrl(input);
|
||||
|
||||
return {
|
||||
url: result.url,
|
||||
method: result.method,
|
||||
expiresAt: result.expiresAt,
|
||||
expiresInSeconds: result.expiresInSeconds,
|
||||
headers: result.headers,
|
||||
};
|
||||
}
|
||||
|
||||
function createS3SignedGetUrl(config: S3StorageAdapterConfig, input: SignedDownloadSignerInput): string {
|
||||
const missingFields = ["endpoint", "region", "bucket", "accessKeyId", "secretAccessKey"].filter((field) => !isPresent(config[field as keyof S3StorageAdapterConfig]));
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
throw new StorageAdapterError("config_missing", `Missing signed URL storage configuration: ${missingFields.join(", ")}`, {
|
||||
operation: "createStorageSignedDownloadUrl",
|
||||
bucket: isPresent(config.bucket) ? config.bucket : undefined,
|
||||
key: input.key,
|
||||
missingFields,
|
||||
});
|
||||
}
|
||||
|
||||
const endpoint = new URL(config.endpoint);
|
||||
const bucketSegment = encodePathSegment(config.bucket);
|
||||
const keyPath = input.key.split("/").map(encodePathSegment).join("/");
|
||||
const pathname = config.forcePathStyle ? `${trimTrailingSlash(endpoint.pathname)}/${bucketSegment}/${keyPath}` : `${trimTrailingSlash(endpoint.pathname)}/${keyPath}`;
|
||||
const host = config.forcePathStyle ? endpoint.host : `${config.bucket}.${endpoint.host}`;
|
||||
const url = new URL(endpoint.toString());
|
||||
url.host = host;
|
||||
url.pathname = pathname;
|
||||
url.search = "";
|
||||
if (input.responseContentType) {
|
||||
url.searchParams.set("response-content-type", input.responseContentType);
|
||||
}
|
||||
if (input.responseContentDisposition) {
|
||||
url.searchParams.set("response-content-disposition", input.responseContentDisposition);
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const amzDate = toAmzDate(now);
|
||||
const dateStamp = amzDate.slice(0, 8);
|
||||
const credentialScope = `${dateStamp}/${config.region}/s3/aws4_request`;
|
||||
const signedHeaders = "host";
|
||||
url.searchParams.set("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
||||
url.searchParams.set("X-Amz-Credential", `${config.accessKeyId}/${credentialScope}`);
|
||||
url.searchParams.set("X-Amz-Date", amzDate);
|
||||
url.searchParams.set("X-Amz-Expires", String(input.expiresInSeconds));
|
||||
url.searchParams.set("X-Amz-SignedHeaders", signedHeaders);
|
||||
if (config.sessionToken) {
|
||||
url.searchParams.set("X-Amz-Security-Token", config.sessionToken);
|
||||
}
|
||||
|
||||
const canonicalQuery = canonicalizeSearchParams(url.searchParams);
|
||||
const canonicalRequest = ["GET", url.pathname, canonicalQuery, `host:${host}\n`, signedHeaders, "UNSIGNED-PAYLOAD"].join("\n");
|
||||
const stringToSign = ["AWS4-HMAC-SHA256", amzDate, credentialScope, sha256Hex(canonicalRequest)].join("\n");
|
||||
const signature = hmacHex(getSigningKey(config.secretAccessKey, dateStamp, config.region), stringToSign);
|
||||
url.searchParams.set("X-Amz-Signature", signature);
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
function canonicalizeSearchParams(params: URLSearchParams): string {
|
||||
return [...params.entries()]
|
||||
.sort(([leftKey, leftValue], [rightKey, rightValue]) => leftKey.localeCompare(rightKey) || leftValue.localeCompare(rightValue))
|
||||
.map(([key, value]) => `${encodeRfc3986(key)}=${encodeRfc3986(value)}`)
|
||||
.join("&");
|
||||
}
|
||||
|
||||
function getSigningKey(secretAccessKey: string, dateStamp: string, region: string): Buffer {
|
||||
const secretFingerprint = createHash("sha256").update(secretAccessKey).digest("hex");
|
||||
const cacheKey = `${secretFingerprint}:${region}:${dateStamp}`;
|
||||
const cached = signingKeyCache.get(cacheKey);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
const dateKey = hmacBuffer(`AWS4${secretAccessKey}`, dateStamp);
|
||||
const regionKey = hmacBuffer(dateKey, region);
|
||||
const serviceKey = hmacBuffer(regionKey, "s3");
|
||||
const signingKey = hmacBuffer(serviceKey, "aws4_request");
|
||||
signingKeyCache.set(cacheKey, signingKey);
|
||||
return signingKey;
|
||||
}
|
||||
|
||||
function hmacBuffer(key: string | Buffer, value: string): Buffer {
|
||||
return createHmac("sha256", key).update(value).digest();
|
||||
}
|
||||
|
||||
function hmacHex(key: string | Buffer, value: string): string {
|
||||
return createHmac("sha256", key).update(value).digest("hex");
|
||||
}
|
||||
|
||||
function sha256Hex(value: string): string {
|
||||
return createHash("sha256").update(value).digest("hex");
|
||||
}
|
||||
|
||||
function toAmzDate(date: Date): string {
|
||||
return date.toISOString().replace(/[:-]|\.\d{3}/g, "");
|
||||
}
|
||||
|
||||
function encodePathSegment(segment: string): string {
|
||||
return encodeRfc3986(segment);
|
||||
}
|
||||
|
||||
function encodeRfc3986(value: string): string {
|
||||
return encodeURIComponent(value).replace(/[!'()*]/g, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`);
|
||||
}
|
||||
|
||||
function trimTrailingSlash(pathname: string): string {
|
||||
return pathname.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
function isPresent(value: unknown): value is string {
|
||||
return typeof value === "string" && value.trim().length > 0;
|
||||
}
|
||||
Reference in new issue
Block a user