Skip to content

Commit

Permalink
fix: Hack in bun upload by streaming into memory first.
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Feb 6, 2024
1 parent b0ad59e commit 7517600
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Data providers for IPFS connectivity

> ISSUE: When using the bun runtime and using createFileStream, the data has to be stored in memory due to an incompatibility with the FormData implementation. i.e. it will construct a Blob containing an arrayStream with the file data in memory before uploading the resulting array of bytes.
## How to get started

### Resolving URLs
Expand Down
23 changes: 22 additions & 1 deletion packages/data-provider-base/src/compatability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,23 @@ export async function getBlob(): Promise<typeof Blob> {
: ((await import("formdata-node").then(({ Blob }) => Blob)) as typeof Blob);
}

/**
* Return the file implementation in a way that works in node and browser
*
* @returns The Blob implementation
*/
export async function getFile(): Promise<typeof File> {
return typeof File !== "undefined" || "browser" in process
? File
: ((await import("formdata-node").then(({ File }) => File)) as typeof File);
}

/**
* Wrap a stream so that a readstream can be detected in node without
* loading the module in the browser.
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
export async function wrapStream(data: any): Promise<any> {
console.log(isNode);
if (isNode) {
const { ReadStream } = await import("node:fs");
if (data instanceof ReadStream) {
Expand All @@ -56,8 +67,18 @@ export async function wrapStream(data: any): Promise<any> {
}
output.push(chunk);
}
const Blob = await getBlob();
return new Blob(output);
}
// @ts-expect-error - check for browser
if (typeof Bun !== "undefined") {
// @ts-expect-error - check for browser
const file = Bun.file(data.path);
const File = await getFile();
return new File([await file.arrayBuffer()], "file", {
type: "application/octet-stream",
});
}
const { fileFromPath } = await import("formdata-node/file-from-path");
return await fileFromPath(data.path);
}
Expand Down
32 changes: 16 additions & 16 deletions packages/data-provider-base/src/formdata-base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,24 @@ export class BaseFormDataUploader {
...input,
body: dataContent as any,
})
.then((response) => {
.then(async (response) => {
const output = await response.text();
const [info] = output.split("\n");
if (response.status !== 200) {
return response.text().then((text) => {
let error = text;
try {
error = JSON.parse(text);
} catch {
// Ignore
}
error = (error as any).error || error;
throw new Error(
`unknown server response while pinning File to IPFS: ${
error || response.status
}`
);
});
let error = info;
try {
error = JSON.parse(info);
} catch {
// Ignore
}
error = (error as any).error || error;
throw new Error(
`unknown server response while pinning File to IPFS: ${
error || response.status
}`
);
}
return response.json() as Promise<any>;
return JSON.parse(info) as Promise<any>;
})
.catch(function (error) {
throw handleError(error);
Expand Down
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="astro/client" />

0 comments on commit 7517600

Please sign in to comment.