Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Web Debugging
      • Function Trigger
      • Environment Variable
      • Code Replica
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • Sample Functions
      • Example Overview
      • Obtaining Client URL Information
      • Customization Based on Client Geo Location
      • Obtaining Client Geo Location Information
      • Batch Redirect
      • URL rewrite based on regular expressions
      • Returning an HTML Page
      • Returning a JSON Object
      • Fetch Remote Resources
      • Authenticating a Request Header
      • Modifying a Response Header
      • Performing an A/B Test
      • Setting Cookies
      • Performing Redirect Based on the Request Location
      • Using the Cache API
      • Caching POST Requests
      • Responding in Streaming Mode
      • Merging Resources and Responding in Streaming Mode
      • Protecting Data from Tampering
      • Rewriting a m3u8 File and Configuring Authentication
      • Adaptive Image Resize
      • Image Adaptive WebP
      • Customize Referer restriction rules
      • Remote Authentication
      • HMAC Digital Signature
      • Naming a Downloaded File
      • Obtaining Client IP Address
      • Complex origin-pull URL rewriting
      • Web Bot Auth
    • Practical Tutorial
      • Overview
      • Origin retrieval based on user IP/geographic location
        • EdgeOne Implementation of Session Persistence Based on Client IP Addresses
        • EdgeOne Implementation of Origin-Pull Based on Client's Geo Location
      • APK dynamic packaging
        • EdgeOne enables dynamic packaging of Android APKs.
          • Feature Overview
          • Step 1: Preprocess the Android APK Parent Package
          • Step 2: Write the Channel Information into the APK Package with EdgeOne Edge Functions
      • Canary Release and Region-specific Execution
      • Adaptive Image Format Conversion via Edge Functions
      • Two Ways to Implement CDN Origin-pull Via Edge Function: Fetch and Passthrough
  • KV Storage
    • Overview
    • Operation Guide
  • Edge reasoning
    • Edge Inference Overview
    • Quick Guide

TransformStream

TransformStream consists of a pair of streams: a readable stream, called the readable side, and a writable stream, called the writable side. It is designed based on the Web APIs standard TransformStream.

Constructor

const { readable, writable } = new TransformStream(transformer?: any, writableStrategy?: WritableStrategy);

Parameters

Parameter Name
Type
Required
Description
transformer
any
No
Not supported yet. The passed value does not take effect and this parameter is automatically ignored.
writableStrategy
No
Writable-side policy configuration.

WritableStrategy

Property Name
Type
Required
Description
highWaterMark
number
Yes
Writable buffer size, in bytes. The default value is 32K, and the maximum value is 256K. If the value exceeds the maximum, it is automatically adjusted to 256K.

Properties

readable

readonly readable: ReadableStream;
The readable side. For more information, see ReadableStream.

writable

readonly writable: WritableStream;
The writable side. For more information, see WritableStream.

Sample Code

async function handleEvent(event) {
// Generate the readable endpoint and writable endpoint
const { readable, writable } = new TransformStream();
// Obtain remote resources
const response = await fetch('https://www.tencentcloud.com/');
// Stream the response to the client
response.body.pipeTo(writable);

return new Response(readable, response);
}

addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event));
});

FixedLengthStream

FixedLengthStream is a special implementation of TransformStream for creating streaming responses with a fixed content length. Response or Request objects constructed with FixedLengthStream automatically carry a Content-Length header whose value is the preset total number of bytes.
This API is suitable for scenarios where the response body size can be calculated in advance and the client experience can be improved by carrying the Content-Length header. For example, during file downloads, browsers or download managers typically obtain the total file size through the Content-Length header to calculate download progress and display a progress bar.

Constructor

const { readable, writable } = new FixedLengthStream(length: number | bigint);

Parameters

Property Name
Type
Required
Description
length
number | bigint
Yes
Specifies the total number of bytes in the stream. The maximum value is 2^53 - 1.

Sample Code


async function handleEvent(event) {
// Generate the readable endpoint and writable endpoint
const { readable, writable } = new FixedLengthStream(12);
const writer = writable.getWriter();
// Write data multiple times
writer.write("Hello");
writer.write(" ");
writer.write("World");
writer.write("!");
writer.close();
writer.releaseLock();
return event.respondWith(new Response(readable));
}

addEventListener('fetch', event => {
handleEvent(event);
});

References