From da7058e16c965e56b927a3fd2aee919e2315dc4a Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Thu, 7 Jun 2018 21:26:05 +0200 Subject: [PATCH] Add usage example for LipFuzzTransformer --- types/whatwg-streams/whatwg-streams-tests.ts | 35 +++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/types/whatwg-streams/whatwg-streams-tests.ts b/types/whatwg-streams/whatwg-streams-tests.ts index 141f47c820..0bb1068f31 100644 --- a/types/whatwg-streams/whatwg-streams-tests.ts +++ b/types/whatwg-streams/whatwg-streams-tests.ts @@ -362,8 +362,19 @@ class WebSocketSink implements WritableStreamSink { type Dictionary = { [key: string]: T } +declare function fetch(input?: Request | string): Promise; declare interface FetchEvent { - respondWith(promise: Promise): void; + readonly request: Request; + respondWith(promise: Promise): void; +} +declare class Request { + readonly url: string; +} +declare class Response { + constructor(body?: ReadableStream); + readonly body: ReadableStream; +} +declare class TextDecoderStream extends TransformStream { } class LipFuzzTransformer implements TransformStreamTransformer { @@ -411,3 +422,25 @@ class LipFuzzTransformer implements TransformStreamTransformer { return replacement; } } + +{ + const userName = ""; + const displayName = ""; + const icon = ""; + const date = ""; + const fetchEvent = {} as FetchEvent; + + const data = { userName, displayName, icon, date }; + const ts = new TransformStream(new LipFuzzTransformer(data)); + + fetchEvent.respondWith( + fetch(fetchEvent.request.url).then(response => { + const transformedBody = response.body + // Decode the binary-encoded response to string + .pipeThrough(new TextDecoderStream()) + // Apply the LipFuzzTransformer + .pipeThrough(ts); + return new Response(transformedBody); + }) + ); +}