-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node-fetch):
WritableStream
, TransformStream
& `CompressionS…
…tream` (#1495)
- Loading branch information
Showing
21 changed files
with
554 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@whatwg-node/server-plugin-cookies': patch | ||
'@whatwg-node/node-fetch': patch | ||
'@whatwg-node/server': patch | ||
'@whatwg-node/fetch': patch | ||
--- | ||
|
||
Implement \`CompressionStream\`, \`WritableStream\` and \`TransformStream\` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { createBrotliCompress, createDeflate, createDeflateRaw, createGzip } from 'node:zlib'; | ||
import { PonyfillTransformStream } from './TransformStream.js'; | ||
|
||
export type PonyfillCompressionFormat = | ||
| 'x-gzip' | ||
| 'gzip' | ||
| 'x-deflate' | ||
| 'deflate' | ||
| 'deflate-raw' | ||
| 'br'; | ||
|
||
export class PonyfillCompressionStream | ||
extends PonyfillTransformStream | ||
implements CompressionStream | ||
{ | ||
constructor(compressionFormat: PonyfillCompressionFormat) { | ||
switch (compressionFormat) { | ||
case 'x-gzip': | ||
case 'gzip': | ||
super(createGzip()); | ||
break; | ||
case 'x-deflate': | ||
case 'deflate': | ||
super(createDeflate()); | ||
break; | ||
case 'deflate-raw': | ||
super(createDeflateRaw()); | ||
break; | ||
case 'br': | ||
super(createBrotliCompress()); | ||
break; | ||
default: | ||
throw new Error(`Unsupported compression format: ${compressionFormat}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { createBrotliDecompress, createGunzip, createInflate, createInflateRaw } from 'node:zlib'; | ||
import { PonyfillCompressionFormat } from './CompressionStream.js'; | ||
import { PonyfillTransformStream } from './TransformStream.js'; | ||
|
||
export class PonyfillDecompressionStream | ||
extends PonyfillTransformStream | ||
implements DecompressionStream | ||
{ | ||
constructor(compressionFormat: PonyfillCompressionFormat) { | ||
switch (compressionFormat) { | ||
case 'x-gzip': | ||
case 'gzip': | ||
super(createGunzip()); | ||
break; | ||
case 'x-deflate': | ||
case 'deflate': | ||
super(createInflate()); | ||
break; | ||
case 'deflate-raw': | ||
super(createInflateRaw()); | ||
break; | ||
case 'br': | ||
super(createBrotliDecompress()); | ||
break; | ||
default: | ||
throw new Error(`Unsupported compression format: ${compressionFormat}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Transform } from 'node:stream'; | ||
import { PonyfillReadableStream } from './ReadableStream.js'; | ||
import { PonyfillWritableStream } from './WritableStream.js'; | ||
|
||
export class PonyfillTransformStream<I = any, O = any> implements TransformStream<I, O> { | ||
transform: Transform; | ||
writable: PonyfillWritableStream<I>; | ||
readable: PonyfillReadableStream<O>; | ||
|
||
constructor(transformer?: Transformer<I, O> | Transform) { | ||
if (transformer instanceof Transform) { | ||
this.transform = transformer; | ||
} else if (transformer) { | ||
const controller: TransformStreamDefaultController = { | ||
enqueue(chunk: O) { | ||
transform.push(chunk); | ||
}, | ||
error(reason: any) { | ||
transform.destroy(reason); | ||
}, | ||
terminate() { | ||
transform.end(); | ||
}, | ||
get desiredSize() { | ||
return transform.writableLength; | ||
}, | ||
}; | ||
const transform = new Transform({ | ||
read() {}, | ||
write(chunk: I, _encoding: BufferEncoding, callback: (error?: Error | null) => void) { | ||
try { | ||
const result = transformer.transform?.(chunk, controller); | ||
if (result instanceof Promise) { | ||
result.then( | ||
() => { | ||
callback(); | ||
}, | ||
err => { | ||
callback(err); | ||
}, | ||
); | ||
} else { | ||
callback(); | ||
} | ||
} catch (err) { | ||
callback(err as Error); | ||
} | ||
}, | ||
final(callback: (error?: Error | null) => void) { | ||
try { | ||
const result = transformer.flush?.(controller); | ||
if (result instanceof Promise) { | ||
result.then( | ||
() => { | ||
callback(); | ||
}, | ||
err => { | ||
callback(err); | ||
}, | ||
); | ||
} else { | ||
callback(); | ||
} | ||
} catch (err) { | ||
callback(err as Error); | ||
} | ||
}, | ||
}); | ||
this.transform = transform; | ||
} else { | ||
this.transform = new Transform(); | ||
} | ||
this.writable = new PonyfillWritableStream(this.transform); | ||
this.readable = new PonyfillReadableStream(this.transform); | ||
} | ||
} |
Oops, something went wrong.