MCPcopy Create free account
hub / github.com/Touffy/client-zip / normalizeInput

Function normalizeInput

src/input.ts:26–57  ·  view source on GitHub ↗
(input?: File | Response | BufferLike | StreamLike, modDate?: any, mode?: number)

Source from the content-addressed store, hash-verified

24export function normalizeInput(input: File | Response | BufferLike | StreamLike, modDate?: any, mode?: number): ZipFileDescription;
25export function normalizeInput(input: undefined, modDate?: any, mode?: number): ZipFolderDescription;
26export function normalizeInput(input?: File | Response | BufferLike | StreamLike, modDate?: any, mode?: number): ZipEntryDescription {
27 if (modDate !== undefined && !(modDate instanceof Date)) modDate = new Date(modDate)
28
29 const isFile = input !== undefined
30
31 if(!mode) {
32 mode = isFile ? 0o664 : 0o775
33 }
34
35 if (input instanceof File) return {
36 isFile,
37 modDate: modDate || new Date(input.lastModified),
38 bytes: input.stream(),
39 mode
40 }
41 if (input instanceof Response) return {
42 isFile,
43 modDate: modDate || new Date(input.headers.get("Last-Modified") || Date.now()),
44 bytes: input.body!,
45 mode
46 }
47
48 if (modDate === undefined) modDate = new Date()
49 else if (isNaN(modDate)) throw new Error("Invalid modification date.")
50 if (!isFile) return { isFile, modDate, mode }
51 if (typeof input === "string") return { isFile, modDate, bytes: encodeString(input), mode }
52 if (input instanceof Blob) return { isFile, modDate, bytes: input.stream(), mode }
53 if (input instanceof Uint8Array || input instanceof ReadableStream) return { isFile, modDate, bytes: input, mode }
54 if (input instanceof ArrayBuffer || ArrayBuffer.isView(input)) return { isFile, modDate, bytes: makeUint8Array(input), mode }
55 if (Symbol.asyncIterator in input) return { isFile, modDate, bytes: ReadableFromIterator(input[Symbol.asyncIterator]()), mode }
56 throw new TypeError("Unsupported input format.")
57}
58
59export function ReadableFromIterator<T extends BufferLike>(iter: AsyncIterator<T>, upstream: AsyncIterator<any> = iter) {
60 return new ReadableStream<Uint8Array>({

Callers 1

nextFunction · 0.90

Calls 3

encodeStringFunction · 0.90
makeUint8ArrayFunction · 0.90
ReadableFromIteratorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…