|
| 1 | +import path from "path"; |
| 2 | +import { PassThrough, pipeline } from "stream"; |
| 3 | +import tarFs from "tar-fs"; |
| 4 | +import { Logger } from "backfill-logger"; |
| 5 | +import { stat } from "fs-extra"; |
| 6 | +import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3"; |
| 7 | +import { Upload } from "@aws-sdk/lib-storage"; |
| 8 | +import { S3CacheStorageOptions } from "backfill-config"; |
| 9 | +import { CacheStorage } from "./CacheStorage"; |
| 10 | +import { TimeoutStream } from "./TimeoutStream"; |
| 11 | +import { SpongeStream } from "./SpongeStream"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Implementation of backfill storage using AWS S3. To use it, |
| 15 | + * specify a custom |
| 16 | + */ |
| 17 | +export class S3CacheStorage extends CacheStorage { |
| 18 | + private readonly s3Client: S3Client; |
| 19 | + |
| 20 | + constructor( |
| 21 | + private options: S3CacheStorageOptions, |
| 22 | + logger: Logger, |
| 23 | + cwd: string, |
| 24 | + incrementalCaching = false |
| 25 | + ) { |
| 26 | + super(logger, cwd, incrementalCaching); |
| 27 | + this.s3Client = new S3Client(options.clientConfig || {}); |
| 28 | + } |
| 29 | + |
| 30 | + protected async _fetch(hash: string): Promise<boolean> { |
| 31 | + try { |
| 32 | + const command = new GetObjectCommand({ |
| 33 | + Bucket: this.options.bucket, |
| 34 | + Key: (this.options.prefix ?? "") + hash, |
| 35 | + }); |
| 36 | + |
| 37 | + const response = await this.s3Client.send(command); |
| 38 | + |
| 39 | + if ( |
| 40 | + this.options.maxSize && |
| 41 | + response.ContentLength && |
| 42 | + response.ContentLength > this.options.maxSize |
| 43 | + ) { |
| 44 | + this.logger.verbose( |
| 45 | + `Object is too large to be downloaded: ${hash}, size: ${response.ContentLength} bytes` |
| 46 | + ); |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + const objectStream = response.Body; |
| 51 | + if (!objectStream) { |
| 52 | + throw new Error("Unable to fetch object."); |
| 53 | + } |
| 54 | + |
| 55 | + const tarWritableStream = tarFs.extract(this.cwd); |
| 56 | + |
| 57 | + const spongeStream = new SpongeStream(); |
| 58 | + |
| 59 | + const timeoutStream = new TimeoutStream( |
| 60 | + 10 * 60 * 1000, |
| 61 | + `The fetch request to ${hash} seems to be hanging` |
| 62 | + ); |
| 63 | + |
| 64 | + const extractionPipeline = new Promise<void>((resolve, reject) => |
| 65 | + pipeline( |
| 66 | + objectStream as any, |
| 67 | + spongeStream, |
| 68 | + timeoutStream, |
| 69 | + tarWritableStream, |
| 70 | + (err) => { |
| 71 | + if (err) { |
| 72 | + reject(err); |
| 73 | + } else { |
| 74 | + resolve(); |
| 75 | + } |
| 76 | + } |
| 77 | + ) |
| 78 | + ); |
| 79 | + |
| 80 | + await extractionPipeline; |
| 81 | + return true; |
| 82 | + } catch (error) { |
| 83 | + if (error && (error as any).name === "NoSuchKey") { |
| 84 | + return false; |
| 85 | + } else { |
| 86 | + throw error; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + protected async _put(hash: string, filesToCache: string[]): Promise<void> { |
| 92 | + const tarStream = tarFs.pack(this.cwd, { entries: filesToCache }); |
| 93 | + // If there's a maxSize limit, first sum up the total size of bytes of all the outputGlobbed files |
| 94 | + if (this.options.maxSize) { |
| 95 | + let total = 0; |
| 96 | + for (const file of filesToCache) { |
| 97 | + total = total + (await stat(path.join(this.cwd, file))).size; |
| 98 | + } |
| 99 | + |
| 100 | + if (total > this.options.maxSize) { |
| 101 | + this.logger.verbose( |
| 102 | + `The output is too large to be uploaded: ${hash}, size: ${total} bytes` |
| 103 | + ); |
| 104 | + return; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + const pass = new PassThrough(); |
| 109 | + tarStream.pipe(pass); |
| 110 | + |
| 111 | + const upload = new Upload({ |
| 112 | + client: this.s3Client, |
| 113 | + params: { |
| 114 | + Bucket: this.options.bucket, |
| 115 | + ContentType: "application/x-tar", |
| 116 | + Key: (this.options.prefix ?? "") + hash, |
| 117 | + Body: pass, |
| 118 | + }, |
| 119 | + }); |
| 120 | + await upload.done(); |
| 121 | + } |
| 122 | +} |
0 commit comments