-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from jacklehamster/master
Add fix to allow uploading Blob to Github
- Loading branch information
Showing
9 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,57 @@ | ||
import { it, describe, expect } from "bun:test"; | ||
import { compare } from "./compare"; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
|
||
function readFileAsBlob(filePath: string): Promise<Blob> { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(filePath, (err: any, data: Blob) => { | ||
if (err) reject(err); | ||
else resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
describe("compare", () => { | ||
it("should return true for equal objects", async () => { | ||
const a = { a: 1, b: 2 }; | ||
const b = { a: 1, b: 2 }; | ||
|
||
expect(await compare(a, b)).toBe(true); | ||
}); | ||
|
||
it("should return false for different objects", async () => { | ||
const a = { a: 1, b: 2 }; | ||
const b = { a: 1, b: 3 }; | ||
|
||
expect(await compare(a, b)).toBe(false); | ||
}); | ||
|
||
it("should return true for equal arrays", async () => { | ||
const a = [1, 2]; | ||
const b = [1, 2]; | ||
|
||
expect(await compare(a, b)).toBe(true); | ||
}); | ||
|
||
it("should compare two blobs", async () => { | ||
const a = new Blob(["test"]); | ||
const b = new Blob(["test"]); | ||
|
||
expect(await compare(a, b)).toBe(true); | ||
expect(await compare(a, new Blob(["test2"]))).toBe(false); | ||
}); | ||
|
||
it("should compare two blobs of images", async () => { | ||
const basePath = path.join(__dirname, 'test-data'); | ||
const dragonPath = path.join(basePath, 'dragon.png'); | ||
const youtubePath = path.join(basePath, 'youtube.png'); | ||
|
||
const dragonBlob = await readFileAsBlob(dragonPath); | ||
const youtubeBlob = await readFileAsBlob(youtubePath); | ||
|
||
expect(await compare(dragonBlob, dragonBlob)).toBe(true); | ||
expect(await compare(dragonBlob, youtubeBlob)).toBe(false); | ||
}); | ||
}); |
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,34 @@ | ||
const deepEqual = require('deep-equal'); | ||
|
||
// Helper function to compare ArrayBuffers | ||
function areArrayBuffersEqual(buffer1: Buffer | ArrayBuffer, buffer2: Buffer | ArrayBuffer) { | ||
if (buffer1.byteLength !== buffer2.byteLength) return false; | ||
const view1 = new Uint8Array(buffer1); | ||
const view2 = new Uint8Array(buffer2); | ||
for (let i = 0; i < view1.length; i++) { | ||
if (view1[i] !== view2[i]) return false; | ||
} | ||
return true; | ||
} | ||
|
||
// Main function to compare blobs/files | ||
async function compareBlobs(blobA: Blob, blobB: Blob) { | ||
let bufferA, bufferB; | ||
|
||
if (typeof window === 'undefined') { | ||
bufferA = Buffer.from(await blobA.arrayBuffer()); | ||
bufferB = Buffer.from(await blobB.arrayBuffer()); | ||
} else { | ||
// Browser environment | ||
bufferA = await blobA.arrayBuffer(); | ||
bufferB = await blobB.arrayBuffer(); | ||
} | ||
|
||
return areArrayBuffersEqual(bufferA, bufferB); | ||
} | ||
|
||
export async function compare(a: any, b: any): Promise<boolean> { | ||
if (typeof(a) !== typeof(b)) return false; | ||
if (a instanceof Blob && b instanceof Blob) return compareBlobs(a, b); | ||
return deepEqual(a, b); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
"module": "amd", | ||
"target": "es5", | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
}, | ||
} |