Skip to content

Commit

Permalink
Merge pull request #11 from jacklehamster/master
Browse files Browse the repository at this point in the history
Add fix to allow uploading Blob to Github
  • Loading branch information
leobenkel authored Jul 13, 2024
2 parents b2fc9ae + f976706 commit 6405acd
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/out/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@the-brains/github-db",
"version": "1.0.5",
"version": "1.0.7",
"repository": {
"type": "git",
"url": "https://github.com/the-brains/GithubDB.git"
Expand Down Expand Up @@ -35,7 +35,7 @@
"build:declaration": "tsc --emitDeclarationOnly --project tsconfig.types.json",
"build:example": "cd examples && bun i && bun run build && cd ..",
"auto-publish": "npm explore @dobuki/auto-publish -- bun run auto-publish \"$(pwd)\"",
"test": "echo NoTest"
"test": "bun test"
},
"devDependencies": {
"@babel/cli": "^7.24.5",
Expand Down
8 changes: 4 additions & 4 deletions src/github-api/github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />

const deepEqual = require("deep-equal");
const mimeTypes = require('mimetypes');
import { compare } from "./util/compare";

const EXTENSION_REGEX = /\.\w+/;

Expand Down Expand Up @@ -100,7 +100,7 @@ export class GithubApi {
default:
{
const types = mimeTypes.detectMimeType(extension);
const response = await fetch(`data:${types};base64,${data.content}`);
const response = await fetch(`data:${types};base64,${data.content.replaceAll('\n', '')}`);
return {
data: await response.blob(),
sha: data.sha,
Expand Down Expand Up @@ -164,9 +164,9 @@ export class GithubApi {
const value = typeof(valueOrCall) === "function" ? await valueOrCall(data) : valueOrCall;

if (data.data) {
if (deepEqual(value, data.data)) {
if (await compare(value, data.data)) {
return data;
}
}
}
const hasExtension = EXTENSION_REGEX.test(key);
const path = `contents/data/${key}${hasExtension ? "" : ".json"}`;
Expand Down
57 changes: 57 additions & 0 deletions src/github-api/util/compare.test.ts
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);
});
});
34 changes: 34 additions & 0 deletions src/github-api/util/compare.ts
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);
}
Binary file added src/github-api/util/test-data/dragon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/github-api/util/test-data/youtube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"module": "amd",
"target": "es5",
"strict": true,
"forceConsistentCasingInFileNames": true,
},
}

0 comments on commit 6405acd

Please sign in to comment.