Skip to content

Commit 61e402d

Browse files
committed
chore(blob): up
1 parent 44a0561 commit 61e402d

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
async function streamToArrayBuffer(stream: ReadableStream, streamSize: number) {
2-
const result = new Uint8Array(streamSize)
3-
let bytesRead = 0
4-
const reader = stream.getReader()
5-
while (true) {
6-
const { done, value } = await reader.read()
7-
if (done) {
8-
break
9-
}
10-
result.set(value, bytesRead)
11-
bytesRead += value.length
12-
}
13-
return result
14-
}
15-
161
export default eventHandler(async (event) => {
172
const { pathname } = await getValidatedRouterParams(event, z.object({
183
pathname: z.string().min(1)
@@ -21,17 +6,12 @@ export default eventHandler(async (event) => {
216

227
const contentType = getHeader(event, 'content-type')
238
const contentLength = Number(getHeader(event, 'content-length') || '0')
24-
if (!query.contentType) {
25-
query.contentType = contentType
26-
}
27-
if (!query.contentLength) {
28-
query.contentLength = contentLength
29-
}
309

31-
// FIXME: find a way to re-stream the readable stream
32-
// const body = getRequestWebStream(event)!
33-
const stream = getRequestWebStream(event)!
34-
const body = await streamToArrayBuffer(stream, contentLength)
10+
const options = { ...query }
11+
if (!options.contentType) { options.contentType = contentType }
12+
if (!options.contentLength) { options.contentLength = contentLength }
13+
14+
const body = getRequestWebStream(event)!
3515

36-
return useBlob().put(pathname, body, query)
16+
return useBlob().put(pathname, body, options)
3717
})

_nuxthub/server/utils/blob.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ export function useBlob () {
8989
pathname = decodeURI(pathname)
9090
if (proxyURL) {
9191
const { contentType, contentLength, ...query } = options
92+
const headers: Record<string, string> = {}
93+
if (contentType) { headers['content-type'] = contentType }
94+
if (contentLength) { headers['content-length'] = contentLength }
9295
return await $fetch<BlobObject>(joinURL('/api/_hub/blob', pathname), {
9396
baseURL: proxyURL,
9497
method: 'PUT',
95-
// duplex: 'half',
96-
// responseType: 'stream',
98+
headers,
9799
body,
98100
query
99101
})
@@ -112,9 +114,7 @@ export function useBlob () {
112114
httpMetadata.contentLength = contentLength
113115
}
114116

115-
console.log('Pushing object on remote server...')
116117
const object = await bucket.put(pathname, body as any, { httpMetadata, customMetadata })
117-
console.log('Object pushed on remote server!')
118118

119119
return mapR2ObjectToBlob(object)
120120
},

0 commit comments

Comments
 (0)