Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 2x 9x 9x 6x 6x 6x 6x 2x 2x 1x 5x 5x 8x 8x 4x 2x 2x 1x 3x 1x 2x 2x 2x | import {
getAttachmentErrorMessage,
isAttachmentAccessDeniedError,
isAttachmentNotFoundError,
isLegacyAttachmentUnavailableError,
} from "../attachment-errors";
import { AttachmentBucketMap, resolveTargetBucket } from "../bucket-routing";
import { AttachmentArchiveSourceAttachment } from "../types";
import { classifyAttachmentArchiveAccessFailure } from "./failure-classification";
interface LoadArchiveAttachmentParams<TBody> {
attachment: AttachmentArchiveSourceAttachment;
attachmentBucketMap: AttachmentBucketMap;
consumer: string;
getAttachmentBody: (bucket: string, key: string) => Promise<TBody>;
getObjectTags?: (bucket: string, key: string) => Promise<Record<string, string>>;
logInfo?: (message: string) => void;
logWarn?: (message: string) => void;
}
type LoadArchiveAttachmentResult<TBody> =
| {
body: TBody;
skipped: false;
}
| {
skipped: true;
};
function logSkippedAttachment({
attachment,
consumer,
destinationBucket,
error,
logWarn,
}: {
attachment: AttachmentArchiveSourceAttachment;
consumer: string;
destinationBucket?: string;
error: unknown;
logWarn: (message: string) => void;
}) {
logWarn(
JSON.stringify({
event: "attachment_archive_attachment_skipped",
bucket: attachment.bucket,
destinationBucket,
key: attachment.key,
filename: attachment.filename,
consumer,
reason: getAttachmentErrorMessage(error),
}),
);
}
export async function loadArchiveAttachment<TBody>({
attachment,
attachmentBucketMap,
consumer,
getAttachmentBody,
getObjectTags,
logInfo = console.info,
logWarn = console.warn,
}: LoadArchiveAttachmentParams<TBody>): Promise<LoadArchiveAttachmentResult<TBody>> {
const resolution = resolveTargetBucket(attachment.bucket, attachmentBucketMap);
if (resolution.remapped) {
logInfo(
JSON.stringify({
event: "legacy_attachment_remap_applied",
bucket: attachment.bucket,
destinationBucket: resolution.destinationBucket,
key: attachment.key,
filename: attachment.filename,
consumer,
}),
);
try {
return {
body: await getAttachmentBody(resolution.destinationBucket, attachment.key),
skipped: false,
};
} catch (error) {
if (isAttachmentAccessDeniedError(error) && getObjectTags) {
const remappedFailure = await classifyAttachmentArchiveAccessFailure({
attachment: {
...attachment,
bucket: resolution.destinationBucket,
},
error,
getObjectTags,
});
if (remappedFailure) {
throw Object.assign(new Error(remappedFailure.failureMessage), remappedFailure, {
cause: error,
});
}
}
// If the mirrored legacy bucket is missing the object, S3 may surface either
// a not-found or access-denied style error depending on bucket permissions.
// In both cases we want to fall back to the original legacy upload bucket.
Iif (!isAttachmentNotFoundError(error) && !isAttachmentAccessDeniedError(error)) {
throw error;
}
logWarn(
JSON.stringify({
event: "legacy_attachment_remap_fallback",
bucket: attachment.bucket,
destinationBucket: resolution.destinationBucket,
key: attachment.key,
filename: attachment.filename,
consumer,
reason: getAttachmentErrorMessage(error),
}),
);
}
}
try {
return {
body: await getAttachmentBody(resolution.sourceBucket, attachment.key),
skipped: false,
};
} catch (error) {
if (resolution.remapped && isAttachmentAccessDeniedError(error) && getObjectTags) {
const sourceFailure = await classifyAttachmentArchiveAccessFailure({
attachment: {
...attachment,
bucket: resolution.sourceBucket,
},
error,
getObjectTags,
});
if (sourceFailure) {
throw Object.assign(new Error(sourceFailure.failureMessage), sourceFailure, {
cause: error,
});
}
}
if (
isAttachmentAccessDeniedError(error) &&
!isLegacyAttachmentUnavailableError(resolution.sourceBucket, error)
) {
throw error;
}
Iif (
!isAttachmentNotFoundError(error) &&
!isLegacyAttachmentUnavailableError(resolution.sourceBucket, error)
) {
throw error;
}
logSkippedAttachment({
attachment,
consumer,
destinationBucket: resolution.remapped ? resolution.destinationBucket : undefined,
error,
logWarn,
});
return {
skipped: true,
};
}
}
|