docs(canvas): explain compute workers and display resource lifetimes

This commit is contained in:
2026-11-12 00:19:19 +08:00
parent af8e2fe76b
commit b471d88520
12 changed files with 136 additions and 0 deletions
@@ -1,17 +1,25 @@
/**
* 专用 Worker 内的摘要算法选择:小于等于64MiB可用原生 SubtleCrypto,较大文件分块流式 SHA。
* 整缓冲读取只允许发生在 Worker,取消由宿主终止该请求的 Worker;本函数不访问存储或文档。
*/
import {fault} from '../canvas-core/values.ts';
import {importSourceDigest} from './import-integrity.ts';
// Each of the browser pool's two active Workers admits at most 64 MiB; larger inputs stream.
/** 单任务 native 缓冲阈值,不是整个应用内存硬上限;总并发由外部 pool 决定。 */
export const nativeDigestByteLimit=64*1024*1024;
/** Dedicated Worker only: whole-buffer native hashing never moves onto the UI thread.
* The host still owns cancellation by terminating the request's Worker. */
/** 验证 Blob,按能力/大小选 native 或增量实现,均返回相同的完整原字节摘要。 */
export async function digestWorkerSource(bytes:Blob,subtle:Pick<SubtleCrypto,'digest'>|null|undefined=globalThis.crypto?.subtle):Promise<string>{
if(!(bytes instanceof Blob))throw fault('invalid_digest_input');
// native digest 必须返回32字节;输出固定64字符十六进制,不接受变长响应。
if(subtle&&bytes.size<=nativeDigestByteLimit){
const input=await bytes.arrayBuffer(),digest=await subtle.digest('SHA-256',input);
if(digest.byteLength!==32)throw fault('invalid_digest_response');
return [...new Uint8Array(digest)].map(value=>value.toString(16).padStart(2,'0')).join('');
}
// Worker fallback 每块1MiB,只做微任务让出;UI 让出策略由 UI fallback 的端口另行决定。
return importSourceDigest(bytes,undefined,{chunkBytes:1024*1024,yieldTask:()=>Promise.resolve()});
}