Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8c5c52a

Browse files
committedNov 4, 2024·
Fix bug in bun when uploading files
1 parent 6332979 commit 8c5c52a

File tree

3 files changed

+60
-34
lines changed

3 files changed

+60
-34
lines changed
 

‎.changeset/rare-planets-smoke.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'e2b': patch
3+
---
4+
5+
Add explicit content header to fix bug in bun when uploading files

‎packages/js-sdk/src/envd/api.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,29 @@ import createClient, { FetchResponse } from 'openapi-fetch'
33
import type { components, paths } from './schema.gen'
44
import { ConnectionConfig } from '../connectionConfig'
55
import { createApiLogger } from '../logs'
6-
import { SandboxError, InvalidArgumentError, NotFoundError, NotEnoughSpaceError, formatSandboxTimeoutError, AuthenticationError } from '../errors'
6+
import {
7+
SandboxError,
8+
InvalidArgumentError,
9+
NotFoundError,
10+
NotEnoughSpaceError,
11+
formatSandboxTimeoutError,
12+
AuthenticationError,
13+
} from '../errors'
714
import { StartResponse } from './process/process_pb'
815
import { Code, ConnectError } from '@connectrpc/connect'
916
import { WatchDirResponse } from './filesystem/filesystem_pb'
1017

11-
export async function handleEnvdApiError<A, B, C extends `${string}/${string}`>(res: FetchResponse<A, B, C>) {
18+
export async function handleEnvdApiError<A, B, C extends `${string}/${string}`>(
19+
res: FetchResponse<A, B, C>
20+
) {
1221
if (!res.error) {
1322
return
1423
}
1524

16-
const message: string = typeof res.error == 'string' ? res.error : res.error?.message || await res.response.text()
25+
const message: string =
26+
typeof res.error == 'string'
27+
? res.error
28+
: res.error?.message || (await res.response.text())
1729

1830
switch (res.response.status) {
1931
case 400:
@@ -23,7 +35,9 @@ export async function handleEnvdApiError<A, B, C extends `${string}/${string}`>(
2335
case 404:
2436
return new NotFoundError(message)
2537
case 429:
26-
return new SandboxError(`${res.response.status}: ${message}: The requests are being rate limited.`)
38+
return new SandboxError(
39+
`${res.response.status}: ${message}: The requests are being rate limited.`
40+
)
2741
case 502:
2842
return formatSandboxTimeoutError(message)
2943
case 507:
@@ -33,7 +47,9 @@ export async function handleEnvdApiError<A, B, C extends `${string}/${string}`>(
3347
}
3448
}
3549

36-
export async function handleProcessStartEvent(events: AsyncIterable<StartResponse>) {
50+
export async function handleProcessStartEvent(
51+
events: AsyncIterable<StartResponse>
52+
) {
3753
let startEvent: StartResponse
3854

3955
try {
@@ -54,7 +70,9 @@ export async function handleProcessStartEvent(events: AsyncIterable<StartRespons
5470
return startEvent.event.event.value.pid
5571
}
5672

57-
export async function handleWatchDirStartEvent(events: AsyncIterable<WatchDirResponse>) {
73+
export async function handleWatchDirStartEvent(
74+
events: AsyncIterable<WatchDirResponse>
75+
) {
5876
let startEvent: WatchDirResponse
5977

6078
try {

‎packages/js-sdk/src/sandbox/filesystem/index.ts

+31-28
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export interface WatchOpts extends FilesystemRequestOpts {
8383
/**
8484
* Timeout for the watch operation in **milliseconds**.
8585
* You can pass `0` to disable the timeout.
86-
*
86+
*
8787
* @default 60_000 // 60 seconds
8888
*/
8989
timeoutMs?: number
@@ -111,13 +111,13 @@ export class Filesystem {
111111

112112
/**
113113
* Read file content as a `string`.
114-
*
114+
*
115115
* You can pass `text`, `bytes`, `blob`, or `stream` to `opts.format` to change the return type.
116-
*
116+
*
117117
* @param path path to the file.
118118
* @param opts connection options.
119119
* @param [opts.format] format of the file content—`text` by default.
120-
*
120+
*
121121
* @returns file content as string
122122
*/
123123
async read(
@@ -126,13 +126,13 @@ export class Filesystem {
126126
): Promise<string>
127127
/**
128128
* Read file content as a `Uint8Array`.
129-
*
129+
*
130130
* You can pass `text`, `bytes`, `blob`, or `stream` to `opts.format` to change the return type.
131-
*
131+
*
132132
* @param path path to the file.
133133
* @param opts connection options.
134134
* @param [opts.format] format of the file content—`bytes`.
135-
*
135+
*
136136
* @returns file content as `Uint8Array`
137137
*/
138138
async read(
@@ -141,13 +141,13 @@ export class Filesystem {
141141
): Promise<Uint8Array>
142142
/**
143143
* Read file content as a `Blob`.
144-
*
144+
*
145145
* You can pass `text`, `bytes`, `blob`, or `stream` to `opts.format` to change the return type.
146-
*
146+
*
147147
* @param path path to the file.
148148
* @param opts connection options.
149149
* @param [opts.format] format of the file content—`blob`.
150-
*
150+
*
151151
* @returns file content as `Blob`
152152
*/
153153
async read(
@@ -156,13 +156,13 @@ export class Filesystem {
156156
): Promise<Blob>
157157
/**
158158
* Read file content as a `ReadableStream`.
159-
*
159+
*
160160
* You can pass `text`, `bytes`, `blob`, or `stream` to `opts.format` to change the return type.
161-
*
161+
*
162162
* @param path path to the file.
163163
* @param opts connection options.
164164
* @param [opts.format] format of the file content—`stream`.
165-
*
165+
*
166166
* @returns file content as `ReadableStream`
167167
*/
168168
async read(
@@ -207,18 +207,18 @@ export class Filesystem {
207207

208208
/**
209209
* Write content to a file.
210-
*
211-
*
210+
*
211+
*
212212
* Writing to a file that doesn't exist creates the file.
213-
*
213+
*
214214
* Writing to a file that already exists overwrites the file.
215-
*
215+
*
216216
* Writing to a file at path that doesn't exist creates the necessary directories.
217217
*
218218
* @param path path to file.
219219
* @param data data to write to the file. Data can be a string, `ArrayBuffer`, `Blob`, or `ReadableStream`.
220220
* @param opts connection options.
221-
*
221+
*
222222
* @returns information about the written file
223223
*/
224224
async write(
@@ -243,7 +243,10 @@ export class Filesystem {
243243
return fd
244244
},
245245
body: {},
246-
signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),
246+
headers: {
247+
'Content-Type': 'multipart/form-data',
248+
'Bun-Content-Type': 'temporary-fix', // https://github.com/oven-sh/bun/issues/14988
249+
},
247250
})
248251

249252
const err = await handleEnvdApiError(res)
@@ -264,7 +267,7 @@ export class Filesystem {
264267
*
265268
* @param path path to the directory.
266269
* @param opts connection options.
267-
*
270+
*
268271
* @returns list of entries in the sandbox filesystem directory.
269272
*/
270273
async list(path: string, opts?: FilesystemRequestOpts): Promise<EntryInfo[]> {
@@ -302,7 +305,7 @@ export class Filesystem {
302305
*
303306
* @param path path to a new directory. For example '/dirA/dirB' when creating 'dirB'.
304307
* @param opts connection options.
305-
*
308+
*
306309
* @returns `true` if the directory was created, `false` if it already exists.
307310
*/
308311
async makeDir(path: string, opts?: FilesystemRequestOpts): Promise<boolean> {
@@ -333,7 +336,7 @@ export class Filesystem {
333336
* @param oldPath path to the file or directory to rename.
334337
* @param newPath new path for the file or directory.
335338
* @param opts connection options.
336-
*
339+
*
337340
* @returns information about renamed file or directory.
338341
*/
339342
async rename(
@@ -370,7 +373,7 @@ export class Filesystem {
370373

371374
/**
372375
* Remove a file or directory.
373-
*
376+
*
374377
* @param path path to a file or directory.
375378
* @param opts connection options.
376379
*/
@@ -393,7 +396,7 @@ export class Filesystem {
393396
*
394397
* @param path path to a file or a directory
395398
* @param opts connection options.
396-
*
399+
*
397400
* @returns `true` if the file or directory exists, `false` otherwise
398401
*/
399402
async exists(path: string, opts?: FilesystemRequestOpts): Promise<boolean> {
@@ -424,13 +427,13 @@ export class Filesystem {
424427
* @param path path to directory to watch.
425428
* @param onEvent callback to call when an event in the directory occurs.
426429
* @param opts connection options.
427-
*
430+
*
428431
* @returns `WatchHandle` object for stopping watching directory.
429432
*/
430433
async watchDir(
431434
path: string,
432435
onEvent: (event: FilesystemEvent) => void | Promise<void>,
433-
opts?: WatchOpts,
436+
opts?: WatchOpts
434437
): Promise<WatchHandle> {
435438
const requestTimeoutMs =
436439
opts?.requestTimeoutMs ?? this.connectionConfig.requestTimeoutMs
@@ -439,8 +442,8 @@ export class Filesystem {
439442

440443
const reqTimeout = requestTimeoutMs
441444
? setTimeout(() => {
442-
controller.abort()
443-
}, requestTimeoutMs)
445+
controller.abort()
446+
}, requestTimeoutMs)
444447
: undefined
445448

446449
const events = this.rpc.watchDir(

0 commit comments

Comments
 (0)
Please sign in to comment.