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

An error occurred when creating an attachment using Bun #1054

Open
AozoraDev opened this issue Mar 11, 2024 · 3 comments
Open

An error occurred when creating an attachment using Bun #1054

AozoraDev opened this issue Mar 11, 2024 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@AozoraDev
Copy link

I just followed the example code about creating a status with image in this repo and ran it using Bun, but the error suddenly occurred even though the image url was valid.

984 |                     throw new MastoUnexpectedError("The server returned data with an unknown encoding. The server may be down.");
985 |                 }
986 |                 const data = this.serializer.deserialize(encoding, yield error.text());
987 |                 const { error: message, errorDescription, details } = data, additionalProperties = __rest(data, ["error", "errorDescription", "details"]);
988 |                 return new MastoHttpError({
989 |                     statusCode: error.status,
                         ^
MastoHttpError: Validation failed: File can't be blank
 cause: {}
@neet neet added the bug Something isn't working label Mar 11, 2024
@neet
Copy link
Owner

neet commented Mar 11, 2024

@AozoraDev Hi, thank you for reporting an issue. I'm wishing to extend the support for non-Node.js platforms, but since I don't catch up with Bun, it's a really good experience.

Can you try this code as well? I changed fs.readFileSync to Bun-native Bun.file API and wrapped it by Blob.

import { createRestAPIClient } from "masto";

const masto = createRestAPIClient({
  url: "YOUR URL",
  accessToken: "YOUR TOKEN",
  log: "debug",
});

const attachment = await masto.v2.media.create({
  file: new Blob([Bun.file("./1.png")]),
  //    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  description: "Some image",
});

// Publish!
const status = await masto.v1.statuses.create({
  status: "Hello from #mastojs!",
  visibility: "private",
  mediaIds: [attachment.id],
});

console.log(status);

I locally verified this works, but this is still strange as BunFile should be compatible with Blob. Let me take some time to look into this issue (though this could be a bug from either Mastodon/Bun).

@neet neet added the help wanted Extra attention is needed label Mar 11, 2024
@AozoraDev
Copy link
Author

AozoraDev commented Mar 11, 2024

Yeah it works but somehow there's an error in the vs code.

Type 'Blob' is not assignable to type 'string | Blob'.
  Type 'Blob' is missing the following properties from type 'Blob': json, formDatats(2322)
index.d.ts(3837, 14): The expected type comes from property 'file' which is declared here on type 'CreateMediaAttachmentParams'

Well, i'm new at ts, so..... 👉👈

@AozoraDev
Copy link
Author

Well, using as Blob fixed it in the vscode but not in Bun.

How confusing....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants