| 1 | import { createHash, randomUUID } from 'node:crypto'; |
| 2 | import type { Readable |
| 3 | import { type Database, |
| 4 | import { Inject, Injectable |
| 5 | import { ConfigService } |
| 6 | import { |
| 7 | and, |
| 8 | eq, |
| 9 | inArray, |
| 10 | isNotNull, |
| 11 | isNull, |
| 12 | lt, |
| 13 | notExists, |
| 14 | sql, |
| 15 | } from 'drizzle-orm'; |
| 16 | import { alias } |
| 17 | |
| 18 | import { DATABASE } |
| 19 | import { |
| 20 | LFS_OID, |
| 21 | lfsEndpoint, |
| 22 | lfsObjectKey, |
| 23 | lfsUploadKey, |
| 24 | } from '../../../lib/git/lfs/lfs-objects.js'; |
| 25 | import { signLfsToken } |
| 26 | import { |
| 27 | InvalidLfsOidError, |
| 28 | LfsObjectMismatchError, |
| 29 | LfsObjectNotFoundError, |
| 30 | LfsObjectTooLargeError, |
| 31 | LfsUploadInProgressError, |
| 32 | } from '../../../lib/git/lfs/lfs.errors.js'; |
| 33 | import type { Repository |
| 34 | import type { Executor |
| 35 | import { PUT_OBJECT_MAX_BYTES } |
| 36 | import { RESERVATION_TTL } |
| 37 | import { |
| 38 | storageAccountOf, |
| 39 | billedKindOf, |
| 40 | } from '../../../lib/storage/storage-account.js'; |
| 41 | import { ContentLengthRequiredError } |
| 42 | import { S3Service } |
| 43 | import { StorageQuotaService } |
| 44 | |
| 45 | type LfsObject |
| 46 | |
| 47 | /** Git LFS objects, one copy per repository under `lfs/<repositoryId>/`. Bytes stream through the API both ways, never straight to the bucket (0025). */ |
| 48 | /** LFS objects whose upload into the repository finished. */ |
| 49 | function uploadedIn(repositoryId: |
| 50 | return and( |
| 51 | eq(schema.lfsObject |
| 52 | isNotNull(schema.lfsObject |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | @Injectable() |
| 57 | export class LfsService |
| 58 | private readonly baseUrl |
| 59 | private readonly secret |
| 60 | |
| 61 | constructor( |
| 62 | @Inject(DATABASE) |
| 63 | private readonly s3 |
| 64 | private readonly quota |
| 65 | config: ConfigService |
| 66 | ) { |
| 67 | // The clone URL's origin: LFS objects are served next to the git routes. |
| 68 | this.baseUrl = |
| 69 | this.secret = |
| 70 | } |
| 71 | |
| 72 | /** What `git-lfs-authenticate` prints over SSH: the HTTPS endpoint, and a token good for an hour on this repository alone. */ |
| 73 | authenticate({ |
| 74 | repositoryId, |
| 75 | path, |
| 76 | userId, |
| 77 | operation, |
| 78 | }: { |
| 79 | repositoryId: string |
| 80 | path: string |
| 81 | userId: string |
| 82 | operation: 'download |
| 83 | }) { |
| 84 | const expiresIn = |
| 85 | const token = |
| 86 | userId && |
| 87 | signLfsToken(this.secret |
| 88 | userId, |
| 89 | repositoryId, |
| 90 | operation, |
| 91 | expiresAt: Date.now |
| 92 | }); |
| 93 | return { |
| 94 | href: lfsEndpoint(this |
| 95 | header: token ? |
| 96 | expires_in: expiresIn, |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | /** Answers a batch request in the `basic` transfer: where to send each object the repository lacks, or where to fetch each it holds. Actions carry the request's own credentials, so the client needs no second lookup. */ |
| 101 | async batch({ |
| 102 | repositoryId, |
| 103 | path, |
| 104 | operation, |
| 105 | objects, |
| 106 | authorization, |
| 107 | }: { |
| 108 | repositoryId: string |
| 109 | /** `owner/repo`, as the client's remote spells it. */ |
| 110 | path: string |
| 111 | operation: 'download |
| 112 | objects: LfsObject |
| 113 | authorization: string |
| 114 | }) { |
| 115 | const stored = |
| 116 | ( |
| 117 | await this.db |
| 118 | .select({ oid: |
| 119 | .from(schema.lfsObject |
| 120 | .where( |
| 121 | and( |
| 122 | uploadedIn(repositoryId), |
| 123 | inArray( |
| 124 | schema.lfsObject.oid |
| 125 | objects.map(({ |
| 126 | ), |
| 127 | ), |
| 128 | ) |
| 129 | ).map(({ |
| 130 | ); |
| 131 | const header = |
| 132 | |
| 133 | return { |
| 134 | transfer: 'basic', |
| 135 | hash_algo: 'sha256', |
| 136 | objects: objects.map |
| 137 | const href = |
| 138 | const storedSize = |
| 139 | if (operation === |
| 140 | return storedSize === |
| 141 | ? { |
| 142 | oid, |
| 143 | size, |
| 144 | error: { code: 404 |
| 145 | } |
| 146 | : { |
| 147 | oid, |
| 148 | size: storedSize, |
| 149 | actions: { download: { |
| 150 | }; |
| 151 | } |
| 152 | if (storedSize !== |
| 153 | if (size > |
| 154 | const { message |
| 155 | return { oid, |
| 156 | } |
| 157 | return { oid, |
| 158 | }), |
| 159 | }; |
| 160 | } |
| 161 | |
| 162 | /** Streams one object to a staging key of its own, hashing it on the way, and copies it to the object's key only if the bytes hash to `oid`. Until then its row is a reservation against the account's quota. */ |
| 163 | async upload({ |
| 164 | repository, |
| 165 | oid, |
| 166 | contentLength, |
| 167 | body, |
| 168 | }: { |
| 169 | repository: Repository |
| 170 | oid: string |
| 171 | contentLength: string |
| 172 | body: Readable |
| 173 | }) { |
| 174 | // The oid becomes an object key. |
| 175 | if (!LFS_OID.test |
| 176 | const size = |
| 177 | if (!contentLength || |
| 178 | throw new ContentLengthRequiredError() |
| 179 | } |
| 180 | if (size > |
| 181 | throw new LfsObjectTooLargeError( |
| 182 | } |
| 183 | const row = |
| 184 | eq(schema.lfsObject |
| 185 | eq(schema.lfsObject |
| 186 | ); |
| 187 | |
| 188 | const [existing] |
| 189 | .select({ oid: |
| 190 | .from(schema.lfsObject |
| 191 | .where(and(row |
| 192 | if (existing) return |
| 193 | |
| 194 | const uploadId = |
| 195 | const mine = |
| 196 | await this.quota |
| 197 | storageAccountOf(repository), |
| 198 | billedKindOf(repository, ' |
| 199 | size, |
| 200 | async (tx) |
| 201 | // An upload that died with its process leaves a reservation behind; once it has lapsed it must not hold the oid forever. |
| 202 | await tx |
| 203 | .delete(schema.lfsObject |
| 204 | .where( |
| 205 | and( |
| 206 | row, |
| 207 | isNull(schema.lfsObject |
| 208 | lt(schema.lfsObject |
| 209 | ), |
| 210 | ); |
| 211 | const [reserved] |
| 212 | .insert(schema.lfsObject |
| 213 | .values({ repositoryId: |
| 214 | .onConflictDoNothing() |
| 215 | .returning({ oid: |
| 216 | if (!reserved) |
| 217 | }, |
| 218 | ); |
| 219 | |
| 220 | // Bytes that never verified must not reach the object's key, where another upload of the same oid may already have stored the real ones. |
| 221 | const staging = |
| 222 | const hash = |
| 223 | // A listener beside the upload's own reader, not a transform in front of it: `putStream` has to see the request's errors to abort. |
| 224 | body.on('data |
| 225 | try { |
| 226 | await this.s3 |
| 227 | Key: staging, |
| 228 | Body: body, |
| 229 | ContentLength: size, |
| 230 | ContentType: 'application/octet-stream', |
| 231 | }); |
| 232 | const actual = |
| 233 | if (actual !== |
| 234 | // Verified bytes are the same bytes whoever wrote them, so overwriting another upload's copy loses nothing. |
| 235 | await this.s3 |
| 236 | Bucket: this.s3 |
| 237 | Key: lfsObjectKey(repository |
| 238 | CopySource: `${this. |
| 239 | }); |
| 240 | } catch (error) |
| 241 | await this.db |
| 242 | throw error; |
| 243 | } finally { |
| 244 | await this.s3 |
| 245 | } |
| 246 | |
| 247 | const [uploaded] |
| 248 | .update(schema.lfsObject |
| 249 | .set({ uploadedAt: |
| 250 | .where(mine) |
| 251 | .returning({ oid: |
| 252 | // The repository was deleted meanwhile, or this reservation lapsed and another upload took the oid. Either way the stored bytes are not this upload's to remove. |
| 253 | if (!uploaded) |
| 254 | } |
| 255 | |
| 256 | async find(repositoryId |
| 257 | const [object] |
| 258 | .select({ size: |
| 259 | .from(schema.lfsObject |
| 260 | .where(and(uploadedIn |
| 261 | return object ?? |
| 262 | } |
| 263 | |
| 264 | async download(repositoryId |
| 265 | const object = |
| 266 | if (!object) |
| 267 | |
| 268 | const { Body |
| 269 | Bucket: this.s3 |
| 270 | Key: lfsObjectKey(repositoryId |
| 271 | }); |
| 272 | if (!Body) |
| 273 | return { size: |
| 274 | } |
| 275 | |
| 276 | /** What a fork copies, read before its reservation so the fork is billed for exactly what it copies. */ |
| 277 | objectsOf(repositoryId: |
| 278 | return this.db |
| 279 | .select({ oid: |
| 280 | .from(schema.lfsObject |
| 281 | .where(uploadedIn(repositoryId |
| 282 | } |
| 283 | |
| 284 | /** Of `oids`, the objects `fromRepositoryId` holds and `toRepositoryId` does not. */ |
| 285 | missingFrom( |
| 286 | toRepositoryId: string, |
| 287 | fromRepositoryId: string, |
| 288 | oids: string[] |
| 289 | ): Promise<LfsObject |
| 290 | if (oids.length |
| 291 | const target = |
| 292 | return this.db |
| 293 | .select({ oid: |
| 294 | .from(schema.lfsObject |
| 295 | .where( |
| 296 | and( |
| 297 | uploadedIn(fromRepositoryId), |
| 298 | inArray(schema.lfsObject |
| 299 | |
| 300 | notExists( |
| 301 | this.db |
| 302 | .select({ oid: |
| 303 | .from(target) |
| 304 | .where( |
| 305 | and( |
| 306 | eq(target.repositoryId |
| 307 | eq(target.oid |
| 308 | ), |
| 309 | ), |
| 310 | ), |
| 311 | ), |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | /** Copies the bytes of `objects` server side. Their rows are written apart, with { |
| 316 | async copy( |
| 317 | objects: LfsObject |
| 318 | fromRepositoryId: string, |
| 319 | toRepositoryId: string, |
| 320 | ) { |
| 321 | for (const { |
| 322 | await this.s3 |
| 323 | Bucket: this.s3 |
| 324 | Key: lfsObjectKey(toRepositoryId |
| 325 | CopySource: `${this. |
| 326 | }); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | async record(tx |
| 331 | if (objects.length |
| 332 | await tx |
| 333 | .insert(schema.lfsObject |
| 334 | .values( |
| 335 | objects.map(({ |
| 336 | repositoryId, |
| 337 | oid, |
| 338 | size, |
| 339 | uploadedAt: new Date |
| 340 | })), |
| 341 | ) |
| 342 | .onConflictDoNothing(); |
| 343 | } |
| 344 | |
| 345 | async read(repositoryId |
| 346 | const { Body |
| 347 | Bucket: this.s3 |
| 348 | Key: lfsObjectKey(repositoryId |
| 349 | }); |
| 350 | if (!Body) |
| 351 | return Buffer.from |
| 352 | } |
| 353 | |
| 354 | /** Every object of a repository, removed before its row takes theirs with it. */ |
| 355 | remove(repositoryId: |
| 356 | return this.s3 |
| 357 | } |
| 358 | } |