All files storagePublicPrefixes.ts

93.33% Statements 14/15
100% Branches 9/9
100% Functions 2/2
93.33% Lines 14/15

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          161x 3x 158x 161x 161x 2x 153x     42x 30x 30x         42x 42x       42x       42x                  
import { validateStoragePathPrefix } from '@transloadit/utils'
import { z } from 'zod'
 
/** Normalize a public directory, never implicit workspace-wide access. */
export function normalizeStoragePublicPrefix(prefix: string): string {
  if (typeof prefix !== 'string' || prefix === '' || prefix === '/')
    throw new TypeError('A public prefix must name a non-root directory')
  const normalized = prefix.endsWith('/') ? prefix : `${prefix}/`
  validateStoragePathPrefix(normalized, 0, 'public prefix')
  if (new TextEncoder().encode(normalized).byteLength > 512)
    throw new TypeError('A public prefix must be at most 512 UTF-8 bytes')
  return normalized
}
 
const prefixSchema = z.string().refine((prefix) => {
  try {
    return normalizeStoragePublicPrefix(prefix) === prefix
  } catch {
    return false
  }
}, 'Expected a normalized public directory')
const publicPrefixSchema = z.object({ prefix: prefixSchema, created_at: z.string() })
export const storagePublicPrefixesSchema = z.object({
  ok: z.literal('STORAGE_PUBLIC_PREFIXES_LISTED'),
  public_prefixes: z.array(publicPrefixSchema),
})
export const storagePublicPrefixDeclaredSchema = publicPrefixSchema.extend({
  ok: z.literal('STORAGE_PUBLIC_PREFIX_DECLARED'),
  created: z.boolean(),
})
export const storagePublicPrefixRevokedSchema = z.object({
  ok: z.literal('STORAGE_PUBLIC_PREFIX_REVOKED'),
  prefix: prefixSchema,
  deleted: z.boolean(),
})
 
export type StoragePublicPrefixes = z.infer<typeof storagePublicPrefixesSchema>
export type StoragePublicPrefixDeclared = z.infer<typeof storagePublicPrefixDeclaredSchema>
export type StoragePublicPrefixRevoked = z.infer<typeof storagePublicPrefixRevokedSchema>