• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Mintbase / mintbase-js / 3767218335

pending completion
3767218335

push

github

GitHub
fix(storage): upload works in browser (#223)

185 of 256 branches covered (72.27%)

Branch coverage included in aggregate %.

21 of 21 new or added lines in 1 file covered. (100.0%)

454 of 495 relevant lines covered (91.72%)

3.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.18
/packages/storage/src/uploads.ts
1
import {
2✔
2
  MINTBASE_API_KEY_HEADER,
3
  MINTBASE_API_KEY,
4
  MINTBASE_API_ANON_USER,
5
} from '@mintbase-js/sdk';
6
import { ARWEAVE_SERVICE_HOST, MAX_UPLOAD_ERROR_MSG } from './constants';
2✔
7
import { ANON_USER_WARNING } from '@mintbase-js/sdk';
2✔
8
import superagent from 'superagent';
2✔
9

10
export const MAX_UPLOAD_BYTES = 31_457_280;
2✔
11

12
export type ArweaveResponse = {
13
  id: string;
14
  block: string;
15
  name: string;
16
  mimeType: string;
17
};
18

19
type HttpError = {
20
  status: number;
21
  response: Response;
22
};
23

24
/**
25
 * (NodeJS) upload a file via POST to upload service
26
 * @param file A file to upload
27
 * @param name The name of the file to upload
28
 */
29
export const uploadBuffer = async (
2✔
30
  file: Buffer,
31
  name: string,
32
): Promise<ArweaveResponse> => {
3✔
33
  if (MINTBASE_API_KEY == MINTBASE_API_ANON_USER) {
3✔
34
    console.warn(ANON_USER_WARNING);
3✔
35
  }
36

37
  const size = (file as Buffer).length;
3✔
38

39
  // if size is more than 30MB, throw since cloud run won't upload.
40
  if (size > MAX_UPLOAD_BYTES) {
3✔
41
    throw new Error(MAX_UPLOAD_ERROR_MSG);
1✔
42
  }
43

44
  try {
2✔
45
    const { body } = await superagent
2✔
46
      .post(ARWEAVE_SERVICE_HOST)
47
      .set({
48
        [MINTBASE_API_KEY_HEADER]: MINTBASE_API_KEY,
49
      })
50
      .attach('up', file, name);
51
    return body;
1✔
52
  } catch (err: unknown) {
53
    const httpError = err as HttpError;
1✔
54
    console.error(
1✔
55
      `Uploading file to arweave failed: ${httpError.status} ${httpError.response.text}`,
56
    );
57
    throw err;
×
58
  }
59
};
60

61
/**
62
 * (Browser) upload a file via POST to upload service
63
 * @param file A file to upload
64
 */
65
export const uploadFile = async (
2✔
66
  file: File,
67
): Promise<ArweaveResponse> => {
2✔
68
  if (MINTBASE_API_KEY == MINTBASE_API_ANON_USER) {
2✔
69
    console.warn(ANON_USER_WARNING);
2✔
70
  }
71
  
72

73
  if (file.size > MAX_UPLOAD_BYTES) {
2✔
74
    throw new Error(MAX_UPLOAD_ERROR_MSG);
1✔
75
  }
76

77
  const formdata = new FormData();
1✔
78
  formdata.append('up', file, 'name');
1✔
79

80
  try {
1✔
81
    const request = await fetch(ARWEAVE_SERVICE_HOST, {
1✔
82
      method: 'POST',
83
      headers: {
84
        'mb-api-key': MINTBASE_API_KEY,
85
      },
86
      body: formdata,
87
      redirect: 'follow',
88
    });
89

90
    if (request.status !== 200) {
1!
91
      throw new Error(
×
92
        `Error uploading via arweave service: ${await request.json()}`,
93
      );
94
    }
95

96
    const result = (await request.json()) as {
1✔
97
      id: string;
98
      block: string;
99
      name: string;
100
      mimeType: string;
101
    };
102

103
    return result;
1✔
104
  } catch (error: unknown) {
105
    console.error('Uploading file to arweave failed');
×
106
    throw error;
×
107
  }
108
};
109

110
// export const uploadBuffer = async (
111
//   buffer: Buffer,
112
//   contentType: string,
113
// ): Promise<string> => {
114
//   const bundlr = setup(supportedStorageServices.arweave.bundlr);
115

116
//   const uploadResult = await bundlr.uploader.upload(buffer, [
117
//     { name: 'Content-Type', value: contentType },
118
//   ]);
119

120
//   const { data } = uploadResult;
121

122
//   return data.id;
123
// };
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc