Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+165
@@ -0,0 +1,165 @@
|
||||
import { loadRequiredWebConfig, PORTAL_GDPR_EXPORT_MAX_BYTES, PRIVACY_GDPR_EXPORT_MAX_BYTES } from "@hivegrid/config";
|
||||
import { createS3SseCObjectReader, type S3SseCObjectReader, type S3SseCReaderConfig } from "@hivegrid/storage";
|
||||
|
||||
export const PORTAL_GDPR_EXPORT_DOWNLOAD_MAX_BYTES = PORTAL_GDPR_EXPORT_MAX_BYTES;
|
||||
export const PRIVACY_GDPR_EXPORT_DOWNLOAD_MAX_BYTES = PRIVACY_GDPR_EXPORT_MAX_BYTES;
|
||||
|
||||
export type GdprExportObject = {
|
||||
body: ReadableStream<Uint8Array>;
|
||||
contentLength: number;
|
||||
};
|
||||
|
||||
const GDPR_EXPORT_READ_TIMEOUT_MS = 60_000;
|
||||
let cachedReader: { config: S3SseCReaderConfig; reader: S3SseCObjectReader } | undefined;
|
||||
|
||||
export async function readGdprExportObject({
|
||||
bucket,
|
||||
key,
|
||||
maxBytes,
|
||||
expectedSizeBytes,
|
||||
signal,
|
||||
}: {
|
||||
bucket: string;
|
||||
key: string;
|
||||
maxBytes: number;
|
||||
expectedSizeBytes?: number;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<GdprExportObject> {
|
||||
const { storage } = loadRequiredWebConfig();
|
||||
if (!storage.bucket || bucket !== storage.bucket) {
|
||||
throw new Error("GDPR export bucket does not match the configured private bucket.");
|
||||
}
|
||||
|
||||
const readerConfig: S3SseCReaderConfig = {
|
||||
endpoint: storage.endpoint ?? "",
|
||||
region: storage.region ?? "",
|
||||
accessKeyId: storage.accessKeyId ?? "",
|
||||
secretAccessKey: storage.secretAccessKey ?? "",
|
||||
...(storage.sessionToken ? { sessionToken: storage.sessionToken } : {}),
|
||||
forcePathStyle: storage.forcePathStyle?.toLowerCase() === "true",
|
||||
customerKeyBase64: storage.gdprExportSseCKey ?? "",
|
||||
};
|
||||
const reader = getCachedReader(readerConfig);
|
||||
const timeoutController = new AbortController();
|
||||
const abortFromRequest = () => timeoutController.abort(signal?.reason);
|
||||
if (signal?.aborted) abortFromRequest();
|
||||
else signal?.addEventListener("abort", abortFromRequest, { once: true });
|
||||
const timeout = setTimeout(() => timeoutController.abort(new Error("GDPR export object read timed out.")), GDPR_EXPORT_READ_TIMEOUT_MS);
|
||||
const cleanup = () => {
|
||||
clearTimeout(timeout);
|
||||
signal?.removeEventListener("abort", abortFromRequest);
|
||||
};
|
||||
let handedOff = false;
|
||||
try {
|
||||
const object = await reader.readObject({
|
||||
bucket,
|
||||
key,
|
||||
maxBytes,
|
||||
...(expectedSizeBytes !== undefined ? { expectedSizeBytes } : {}),
|
||||
signal: timeoutController.signal,
|
||||
});
|
||||
if (timeoutController.signal.aborted) {
|
||||
await object.body.cancel(timeoutController.signal.reason).catch(() => undefined);
|
||||
throw timeoutController.signal.reason instanceof Error
|
||||
? timeoutController.signal.reason
|
||||
: new Error("GDPR export object read aborted.");
|
||||
}
|
||||
if (object.contentLength === undefined || !Number.isSafeInteger(object.contentLength) || object.contentLength < 0 || object.contentLength > maxBytes) {
|
||||
await object.body.cancel().catch(() => undefined);
|
||||
throw new Error("GDPR export object returned no valid bounded length.");
|
||||
}
|
||||
const body = keepAbortAndTimeoutUntilStreamFinished(object.body, timeoutController.signal, cleanup);
|
||||
handedOff = true;
|
||||
return { body, contentLength: object.contentLength };
|
||||
} finally {
|
||||
if (!handedOff) cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
export function createGdprExportDownloadResponse({
|
||||
object,
|
||||
filename,
|
||||
contentType,
|
||||
headers: initialHeaders,
|
||||
}: {
|
||||
object: GdprExportObject;
|
||||
filename: string;
|
||||
contentType: "application/json" | "application/zip";
|
||||
headers?: HeadersInit;
|
||||
}): Response {
|
||||
const headers = new Headers(initialHeaders);
|
||||
headers.set("Cache-Control", "private, no-store");
|
||||
headers.set("Content-Disposition", `attachment; filename="${sanitizeFilename(filename)}"`);
|
||||
headers.set("Content-Type", contentType);
|
||||
headers.set("X-Content-Type-Options", "nosniff");
|
||||
if (object.contentLength !== undefined) headers.set("Content-Length", String(object.contentLength));
|
||||
return new Response(object.body, { status: 200, headers });
|
||||
}
|
||||
|
||||
function getCachedReader(config: S3SseCReaderConfig): S3SseCObjectReader {
|
||||
if (!cachedReader || !sameReaderConfig(cachedReader.config, config)) {
|
||||
cachedReader = { config, reader: createS3SseCObjectReader(config) };
|
||||
}
|
||||
return cachedReader.reader;
|
||||
}
|
||||
|
||||
function sameReaderConfig(left: S3SseCReaderConfig, right: S3SseCReaderConfig): boolean {
|
||||
return (
|
||||
left.endpoint === right.endpoint &&
|
||||
left.region === right.region &&
|
||||
left.accessKeyId === right.accessKeyId &&
|
||||
left.secretAccessKey === right.secretAccessKey &&
|
||||
left.sessionToken === right.sessionToken &&
|
||||
left.forcePathStyle === right.forcePathStyle &&
|
||||
left.customerKeyBase64 === right.customerKeyBase64
|
||||
);
|
||||
}
|
||||
|
||||
function keepAbortAndTimeoutUntilStreamFinished(
|
||||
stream: ReadableStream<Uint8Array>,
|
||||
signal: AbortSignal,
|
||||
cleanup: () => void
|
||||
): ReadableStream<Uint8Array> {
|
||||
const reader = stream.getReader();
|
||||
let finished = false;
|
||||
const finish = () => {
|
||||
if (finished) return;
|
||||
finished = true;
|
||||
signal.removeEventListener("abort", abort);
|
||||
cleanup();
|
||||
};
|
||||
const abort = () => {
|
||||
void reader.cancel(signal.reason).catch(() => undefined);
|
||||
finish();
|
||||
};
|
||||
signal.addEventListener("abort", abort, { once: true });
|
||||
return new ReadableStream<Uint8Array>({
|
||||
async pull(controller) {
|
||||
try {
|
||||
if (signal.aborted) throw signal.reason instanceof Error ? signal.reason : new Error("GDPR export object read aborted.");
|
||||
const chunk = await reader.read();
|
||||
if (signal.aborted) throw signal.reason instanceof Error ? signal.reason : new Error("GDPR export object read aborted.");
|
||||
if (chunk.done) {
|
||||
finish();
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
controller.enqueue(chunk.value);
|
||||
} catch (error) {
|
||||
finish();
|
||||
controller.error(error);
|
||||
}
|
||||
},
|
||||
async cancel(reason) {
|
||||
try {
|
||||
await reader.cancel(reason);
|
||||
} finally {
|
||||
finish();
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function sanitizeFilename(filename: string): string {
|
||||
return filename.replace(/["\\\r\n]/gu, "").slice(0, 200);
|
||||
}
|
||||
Reference in new issue
Block a user