-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Full in-memory database support (#52)
- Loading branch information
1 parent
58de444
commit 2722197
Showing
18 changed files
with
392 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
type DatabaseFileInput = | ||
| File | ||
| Blob | ||
| ArrayBuffer | ||
| Uint8Array | ||
| ReadableStream<Uint8Array>; | ||
|
||
export function normalizeDatabaseFile( | ||
dbFile: DatabaseFileInput, | ||
convertStreamTo: 'callback' | ||
): Promise<ArrayBuffer | Uint8Array | (() => Promise<Uint8Array | undefined>)>; | ||
export function normalizeDatabaseFile( | ||
dbFile: DatabaseFileInput, | ||
convertStreamTo: 'buffer' | ||
): Promise<ArrayBuffer | Uint8Array>; | ||
export function normalizeDatabaseFile( | ||
dbFile: DatabaseFileInput, | ||
convertStreamTo?: undefined | ||
): Promise<ArrayBuffer | Uint8Array | ReadableStream<Uint8Array>>; | ||
export async function normalizeDatabaseFile( | ||
dbFile: DatabaseFileInput, | ||
convertStreamTo?: 'callback' | 'buffer' | ||
): Promise< | ||
| ArrayBuffer | ||
| Uint8Array | ||
| ReadableStream<Uint8Array> | ||
| (() => Promise<Uint8Array | undefined>) | ||
> { | ||
let bufferOrStream: ArrayBuffer | Uint8Array | ReadableStream<Uint8Array>; | ||
|
||
if (dbFile instanceof Blob) { | ||
bufferOrStream = dbFile.stream(); | ||
} else { | ||
bufferOrStream = dbFile; | ||
} | ||
|
||
if (bufferOrStream instanceof ReadableStream && convertStreamTo) { | ||
const stream = bufferOrStream; | ||
const reader = stream.getReader(); | ||
|
||
switch (convertStreamTo) { | ||
case 'callback': | ||
return async () => { | ||
const chunk = await reader.read(); | ||
return chunk.value; | ||
}; | ||
|
||
case 'buffer': | ||
const chunks: Uint8Array[] = []; | ||
let streamDone = false; | ||
|
||
while (!streamDone) { | ||
const chunk = await reader.read(); | ||
if (chunk.value) chunks.push(chunk.value); | ||
streamDone = chunk.done; | ||
} | ||
|
||
const arrayLength = chunks.reduce((length, chunk) => { | ||
return length + chunk.length; | ||
}, 0); | ||
const buffer = new Uint8Array(arrayLength); | ||
let offset = 0; | ||
|
||
chunks.forEach((chunk) => { | ||
buffer.set(chunk, offset); | ||
offset += chunk.length; | ||
}); | ||
|
||
return buffer; | ||
} | ||
} else { | ||
return bufferOrStream; | ||
} | ||
} |
Oops, something went wrong.