All files / lib/attachment-archive/worker attachment-source.ts

88.23% Statements 15/17
77.77% Branches 14/18
100% Functions 2/2
88.23% Lines 15/17

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                                                                                1x                                         5x   5x 2x                     2x 2x               2x       2x                           5x 5x         2x       1x     1x             1x               1x          
import {
  getAttachmentErrorMessage,
  isAttachmentAccessDeniedError,
  isAttachmentNotFoundError,
  isLegacyAttachmentUnavailableError,
} from "../attachment-errors";
import { AttachmentBucketMap, resolveTargetBucket } from "../bucket-routing";
import { AttachmentArchiveSourceAttachment } from "../types";
 
interface LoadArchiveAttachmentParams<TBody> {
  attachment: AttachmentArchiveSourceAttachment;
  attachmentBucketMap: AttachmentBucketMap;
  consumer: string;
  getAttachmentBody: (bucket: string, key: string) => Promise<TBody>;
  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,
  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 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 (
      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,
    };
  }
}