Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Multiple Image Blob Export #2094

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/dexie-export-import/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dexie-export-import",
"version": "4.1.2",
"version": "4.1.3",
"description": "Dexie addon that adds export and import capabilities",
"main": "dist/dexie-export-import.js",
"module": "dist/dexie-export-import.mjs",
Expand Down
3 changes: 2 additions & 1 deletion addons/dexie-export-import/src/tson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ TSON.finalize = async (items?: any[]) => {
const typeSpec = TSON.types[typeName];
if (typeSpec && typeSpec.finalize) {
const b = Dexie.getByKeyPath(item, arrayType ? "$." + keyPath : keyPath);
typeSpec.finalize(b, allChunks.slice(b.start, b.end));
typeSpec.finalize(b, allChunks.slice(b.data?.start, b.data?.end));
}
}
}
}
}
// Free up memory
blobsToAwait = [];
blobsToAwaitPos = 0;
}
27 changes: 24 additions & 3 deletions addons/dexie-export-import/test/basic-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,25 @@ promisedTest("export-format", async() => {
for (let i=0;i<256;++i) {
fullByteArray[i] = i;
}
const blob1 = new Blob(["1"])
const blob2 = new Blob(["2"])
const blob3 = new Blob(["3"])
await db.table("inbound").bulkAdd([{
id: 1,
date: new Date(1),
fullBlob: new Blob([fullByteArray]),
imageBlob: blob1,
binary: new Uint8Array([1,2,3]),
text: "foo",
bool: false
},{
id: 2,
foo: "bar"
foo: "bar",
imageBlob: blob2,
},{
id: 3,
bar: "foo"
bar: "foo",
imageBlob: blob3,
}]);

const blob = await db.export({prettyJson: true});
Expand All @@ -84,6 +90,8 @@ promisedTest("export-format", async() => {
await db.delete();
const importedDB = await Dexie.import(blob);
const outboundKeys = await importedDB.table('outbound').toCollection().primaryKeys();
const outboundValues = await importedDB.table('outbound').toArray();
const stringBlob = await readBlob(outboundValues[1].blob)
const inboundValues = await importedDB.table('inbound').toArray();
equal (outboundKeys[0], 2, "First key should be 2");
ok('getTime' in (outboundKeys[1] as Date), "Second outbound key should be a Date instance");
Expand All @@ -96,7 +104,20 @@ promisedTest("export-format", async() => {
const ba = new Uint8Array(ab);
console.log("byte array", ba);
deepEqual([].slice.call(ba), [].slice.call(fullByteArray), "The whole byte spectrum supported after redecoding blob");
//equal(firstBlobStr, "something", "First Blob should be 'something'");

const blob1A = await blob1.text()
const blob1B = await inboundValues[0].imageBlob.text()
deepEqual( blob1A, blob1B, "First Blob should be same as stored");

const blob2A = await blob2.text()
const blob2B = await inboundValues[1].imageBlob.text()
deepEqual( blob2A, blob2B, "Second Blob should be same as stored");

const blob3A = await blob3.text()
const blob3B = await inboundValues[2].imageBlob.text()
deepEqual( blob3A, blob3B, "Third Blob should be same as stored");

equal( stringBlob, "something", "First Blob should be 'something'");
equal( inboundValues[0].binary[0], 1, "First binary[0] should be 1");
equal( inboundValues[0].binary[1], 2, "First binary[0] should be 2");
equal( inboundValues[0].binary[2], 3, "First binary[0] should be 3");
Expand Down
16 changes: 2 additions & 14 deletions addons/dexie-export-import/test/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,11 @@ export function promisedTest(name: string, tester: ()=>Promise<any>) {
}

export function readBlob(blob: Blob): Promise<string> {
return new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.onabort = ev => reject(new Error("file read aborted"));
reader.onerror = ev => reject((ev.target as any).error);
reader.onload = ev => resolve((ev.target as any).result);
reader.readAsText(blob);
});
return blob.text();
}

export function readBlobBinary(blob: Blob): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
const reader = new FileReader();
reader.onabort = ev => reject(new Error("file read aborted"));
reader.onerror = ev => reject((ev.target as any).error);
reader.onload = ev => resolve((ev.target as any).result);
reader.readAsArrayBuffer(blob);
});
return blob.arrayBuffer();
}

// Must use this rather than QUnit's deepEqual() because that one fails on Safari when run via karma-browserstack-launcher
Expand Down
Loading