MCPcopy Create free account
hub / github.com/WCY-dt/EasyTransfer / encodeFileChunk

Method encodeFileChunk

client/src/utils/fileProtocol.ts:70–101  ·  view source on GitHub ↗

* Encode file chunk

(
    fileId: string,
    chunkIndex: number,
    chunkData: ArrayBuffer,
  )

Source from the content-addressed store, hash-verified

68 * Encode file chunk
69 */
70 static encodeFileChunk(
71 fileId: string,
72 chunkIndex: number,
73 chunkData: ArrayBuffer,
74 ): Uint8Array {
75 const fileIdBytes = new TextEncoder().encode(fileId)
76
77 // File ID length (2 bytes)
78 const fileIdLengthArray = new Uint8Array(2)
79 fileIdLengthArray[0] = (fileIdBytes.length & 0xff00) >> 8
80 fileIdLengthArray[1] = fileIdBytes.length & 0xff
81
82 // Chunk index (2 bytes)
83 const chunkIndexArray = new Uint8Array(2)
84 chunkIndexArray[0] = (chunkIndex & 0xff00) >> 8
85 chunkIndexArray[1] = chunkIndex & 0xff
86
87 // Combine data: [fileId length] + [fileId] + [chunk index] + [chunk data]
88 const totalLength = 2 + fileIdBytes.length + 2 + chunkData.byteLength
89 const encodedData = new Uint8Array(totalLength)
90
91 let offset = 0
92 encodedData.set(fileIdLengthArray, offset)
93 offset += 2
94 encodedData.set(fileIdBytes, offset)
95 offset += fileIdBytes.length
96 encodedData.set(chunkIndexArray, offset)
97 offset += 2
98 encodedData.set(new Uint8Array(chunkData), offset)
99
100 return encodedData
101 }
102
103 /**
104 * Encodes a metadata message

Callers 3

resendMissingChunksFunction · 0.80
sendSlicesFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected