Skip to content

Commit

Permalink
fix serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
MHHukiewitz committed Dec 21, 2023
1 parent b835d32 commit 055f654
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/global.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const DEFAULT_API_V2 = "https://api2.aleph.im";
export const DEFAULT_API_V2 = "http://127.0.0.1:4024/";
15 changes: 9 additions & 6 deletions src/messages/store/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export async function Publish({
if (!hash) {
const buffer = await processFileObject(fileObject);
hash = await getHash(buffer, storageEngine, fileHash, APIServer);
if (fileObject instanceof File) {
fileObject = new File([buffer], fileObject.name);
} else {
fileObject = new Blob([buffer]);
}
}
const message = await createAndSendStoreMessage(
account,
Expand All @@ -68,7 +73,7 @@ export async function Publish({
APIServer,
inlineRequested,
sync,
fileObject,
fileObject as Blob | File | undefined,
);
return message;
}
Expand Down Expand Up @@ -101,7 +106,7 @@ async function createAndSendStoreMessage(
APIServer: string,
inlineRequested: boolean,
sync: boolean,
fileObject: Buffer | Blob | File | undefined,
fileObject: Blob | File | undefined,
) {
const timestamp = Date.now() / 1000;
const storeContent: StoreContent = {
Expand Down Expand Up @@ -168,7 +173,7 @@ type SignAndBroadcastConfiguration = {
sync: boolean;
};

async function sendMessage(configuration: SignAndBroadcastConfiguration, file: Buffer | Blob | File): Promise<any> {
async function sendMessage(configuration: SignAndBroadcastConfiguration, file: Blob | File): Promise<any> {
const form = new FormData();
const metadata = {
message: {
Expand All @@ -178,9 +183,7 @@ async function sendMessage(configuration: SignAndBroadcastConfiguration, file: B
sync: configuration.sync,
};

const fileBlob = file instanceof Blob ? file : new Blob([file]);
const fileName = file instanceof File ? file.name : "File";
form.append("file", fileBlob, fileName);
form.append("file", file);
form.append("metadata", JSON.stringify(metadata));

try {
Expand Down
15 changes: 3 additions & 12 deletions src/messages/store/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import shajs from "sha.js";

export async function blobToBuffer(blob: Blob): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
if (reader.result instanceof ArrayBuffer) {
resolve(Buffer.from(reader.result));
} else {
reject("Failed to convert Blob to Buffer.");
}
};
reader.readAsArrayBuffer(blob);
});
export async function blobToBuffer(blob: Blob | File): Promise<Buffer> {
const arrayBuffer = await blob.arrayBuffer();
return Buffer.from(arrayBuffer);
}

export function calculateSHA256Hash(data: ArrayBuffer | Buffer): string {
Expand Down
4 changes: 0 additions & 4 deletions tests/messages/store/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ describe("Store message publish", () => {
account: account,
fileObject: fileContent as unknown as File,
});
console.log(message);
const response = await store.Get({
fileHash: message.content.item_hash,
});
console.log(response);

const got = ArraybufferToString(response);
const expected = "x";
Expand All @@ -97,11 +95,9 @@ describe("Store message publish", () => {
account: account,
fileHash: helloWorldHash,
});
console.log(message);
const response = await store.Get({
fileHash: message.content.item_hash,
});
console.log(response);

const got = ArraybufferToString(response);
const expected = "hello world!";
Expand Down

0 comments on commit 055f654

Please sign in to comment.