@@ -6,14 +6,12 @@ import { logger } from '~/lib/logger'
66import { useStorageAdapter } from '~/lib/storage'
77
88// https://github.com/actions/toolkit/blob/340a6b15b5879eefe1412ee6c8606978b091d3e8/packages/cache/src/cache.ts#L470
9- const chunkSize = 64 * 1024 * 1024
9+ const MB = 1024 * 1024
1010
1111const pathParamsSchema = z . object ( {
1212 cacheId : z . coerce . number ( ) ,
1313} )
1414
15- const sizeByBlockId = new Map < string , number > ( )
16-
1715export default defineEventHandler ( async ( event ) => {
1816 const parsedPathParams = pathParamsSchema . safeParse ( event . context . params )
1917 if ( ! parsedPathParams . success )
@@ -24,7 +22,7 @@ export default defineEventHandler(async (event) => {
2422
2523 if ( getQuery ( event ) . comp === 'blocklist' ) {
2624 setResponseStatus ( event , 201 )
27- return 'ok'
25+ return
2826 }
2927
3028 const blockId = getQuery ( event ) ?. blockid as string
@@ -51,26 +49,40 @@ export default defineEventHandler(async (event) => {
5149 throw createError ( { statusCode : 400 , statusMessage : "'content-length' header is required" } )
5250 }
5351
54- sizeByBlockId . set ( blockId , contentLength )
52+ const userAgent = getHeader ( event , 'user-agent' )
53+
54+ // 1 MB for docker buildx
55+ // 64 MB for everything else
56+ const chunkSize = userAgent && userAgent . startsWith ( 'azsdk-go-azblob' ) ? MB : 64 * MB
5557 const start = chunkIndex * chunkSize
5658 const end = start + contentLength - 1
5759
5860 const adapter = await useStorageAdapter ( )
59- await adapter . uploadChunk ( cacheId , stream as ReadableStream < Buffer > , start , end )
61+ await adapter . uploadChunk ( {
62+ uploadId : cacheId ,
63+ chunkStream : stream as ReadableStream < Buffer > ,
64+ chunkStart : start ,
65+ chunkEnd : end ,
66+ chunkIndex,
67+ } )
6068
6169 setResponseStatus ( event , 201 )
6270} )
6371
64- /**
65- * Format (base64 decoded): 06a9ffa8-2e62-4e96-8e5b-15f24c117f1f000000000006
66- */
67- function getChunkIndexFromBlockId ( blockId : string ) {
68- const decoded = Buffer . from ( blockId , 'base64' ) . toString ( 'utf8' )
69- if ( decoded . length !== 48 ) return
72+ function getChunkIndexFromBlockId ( blockIdBase64 : string ) {
73+ const base64Decoded = Buffer . from ( blockIdBase64 , 'base64' )
7074
71- // slice off uuid and convert to number
72- const index = Number . parseInt ( decoded . slice ( 36 ) )
73- if ( Number . isNaN ( index ) ) return
75+ // 64 bytes used by docker buildx
76+ // 48 bytes used by everything else
77+ if ( base64Decoded . length === 64 ) {
78+ return base64Decoded . readUInt32BE ( 16 )
79+ } else if ( base64Decoded . length === 48 ) {
80+ const decoded = base64Decoded . toString ( 'utf8' )
7481
75- return index
82+ // slice off uuid and convert to number
83+ const index = Number . parseInt ( decoded . slice ( 36 ) )
84+ if ( Number . isNaN ( index ) ) return
85+
86+ return index
87+ }
7688}
0 commit comments