All files / src/server/files storage.ts

100% Statements 1/1
100% Branches 0/0
100% Functions 1/1
100% Lines 1/1

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                                                      51x    
// File storage, independent of the provider (CLAUDE.md, "Cloudflare stays
// inside src/server/platform/"). Receipts and attachments are stored under a
// key the server generates; D1 keeps only the key and metadata (type, size).
// Files are private: they are read only through the Worker, after an access
// check (response.ts builds the response).
 
export interface StoredFile {
  body: ReadableStream<Uint8Array>
  contentType: string
  size: number
}
 
export interface FileStorage {
  /** Stores a file, replacing any file with the same key. */
  put(key: string, bytes: Uint8Array, contentType: string): Promise<void>
  /** The file, or null when there is none. */
  get(key: string): Promise<StoredFile | null>
  /** Removes a file. Removing a missing key is not an error. */
  delete(key: string): Promise<void>
  /** Removes files, any number of them. Missing keys are not an error. */
  deleteMany(keys: readonly string[]): Promise<void>
}
 
export type FileKind = 'receipts' | 'attachments'
 
/** A new storage key, e.g. "receipts/2b1c…". Never derived from user input. */
export function newFileKey(kind: FileKind): string {
  return `${kind}/${crypto.randomUUID()}`
}