streams
The streams built-in module is available to use in your EdgeWorkers code bundles. It exports implementations of ByteLengthQueuingStrategy, CountQueuingStrategy, ReadableStream, TransformStream, and WritableStream, as described in the WhatWG stream specification. It also includes implementations of CompressionStream, and DecompressionStream as described in the WhatWG Compression specification.
import { createResponse } from 'create-response';
import { httpRequest } from 'http-request';
import { ReadableStream, WritableStream, CompressionStream, DecompressionStream } from 'streams';
import { TextEncoderStream, TextDecoderStream } from 'text-encode-transform';
class UppercaseStream {
constructor () {
let readController = null;
this.readable = new ReadableStream({
start (controller) {
readController = controller;
}
});
this.writable = new WritableStream({
write (text) {
readController.enqueue(text.toUpperCase());
},
close (controller) {
readController.close();
}
});
}
}
export async function responseProvider (request) {
let options = { headers: { 'accept-encoding': 'gzip'}, preserveEncoding: true };
const response = await httpRequest('http://example.com/javascripts/jquery-3.3.1.min.js', options);
return createResponse(
response.status,
{'content-encoding': 'gzip'},
response.body
.pipeThrough(new DecompressionStream('gzip'))
.pipeThrough(new TextDecoderStream())
.pipeThrough(new UppercaseStream())
.pipeThrough(new TextEncoderStream())
.pipeThrough(new CompressionStream('gzip'))
);
}When you use a CompressionStream to compress a response body it does not automatically add a
Content-Encodingheader to the response headers. You should use an EdgeWorker to set this header explicitly if required.
Updated 3 days ago
Did this page help you?
