概述TransformStream 提供可组合的数据管道能力。本文给出能力检测与端到端处理示例。能力检测与处理管道const supportsTS = typeof TransformStream === 'function';
function toUpperTransform() {
return new TransformStream({
transform(chunk, controller) {
const s = typeof chunk === 'string' ? chunk : new TextDecoder().decode(chunk);
controller.enqueue(new TextEncoder().encode(s.toUpperCase()));
}
});
}
async function processText(text) {
if (!supportsTS) return text.toUpperCase();
const rs = new Blob([text]).stream();
const out = rs.pipeThrough(toUpperTransform());
return await new Response(out).text();
}

发表评论 取消回复