Skip to content

Commit

Permalink
fix: Node.js < 18 media uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Dec 25, 2022
1 parent 4660b92 commit 987dac7
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/serializers/serializer-native-impl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BodyInit } from '@mastojs/ponyfills';
import { Blob, FormData } from '@mastojs/ponyfills';
import { FormData } from '@mastojs/ponyfills';
import { camelCase, snakeCase } from 'change-case';

import { MastoDeserializeError } from '../errors';
Expand All @@ -23,17 +23,20 @@ export class SerializerNativeImpl implements Serializer {
case 'multipart/form-data': {
const formData = new FormData();
for (const [key, value] of Object.entries(flattenObject(data))) {
if (value instanceof Blob) {
// `form-data` module has an issue that they doesn't set filename
// https://github.com/neet/masto.js/issues/481
// https://github.com/mastodon/mastodon/issues/17622

// `form-data` module has an issue that they doesn't set filename
// https://github.com/neet/masto.js/issues/481
// https://github.com/mastodon/mastodon/issues/17622
if (
globalThis.Buffer != undefined &&
value instanceof globalThis.Buffer
) {
// We set `blob` as filename, which is the default for Blob defined by the spec
// https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
formData.append(key, value, 'blob');
} else {
formData.append(key, value);
continue;
}

formData.append(key, value);
}
return formData;
}
Expand Down

0 comments on commit 987dac7

Please sign in to comment.