Skip to content

Commit 3c31520

Browse files
committed
Bug:修复HF渠道上传较大文本文件栈溢出的错误
1 parent fc9eab6 commit 3c31520

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

functions/utils/huggingfaceAPI.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,19 @@ export class HuggingFaceAPI {
426426
async commitDirectFile(filePath, file, commitMessage) {
427427
const url = `${this.baseURL}/api/datasets/${this.repo}/commit/main`;
428428

429-
const content = btoa(String.fromCharCode(...new Uint8Array(await file.arrayBuffer())));
429+
const bytes = new Uint8Array(await file.arrayBuffer());
430+
// 分块转换,避免大文件导致 Maximum call stack size exceeded
431+
const chunkSize = 4096;
432+
const parts = [];
433+
for (let i = 0; i < bytes.length; i += chunkSize) {
434+
const chunk = bytes.subarray(i, Math.min(i + chunkSize, bytes.length));
435+
let s = '';
436+
for (let j = 0; j < chunk.length; j++) {
437+
s += String.fromCharCode(chunk[j]);
438+
}
439+
parts.push(s);
440+
}
441+
const content = btoa(parts.join(''));
430442

431443
const body = [
432444
JSON.stringify({

0 commit comments

Comments
 (0)