@@ -7,84 +7,87 @@ import { headStream } from "../headStream.browser";
77import { ChecksumStream as ChecksumStreamWeb } from "./ChecksumStream.browser" ;
88import { createChecksumStream } from "./createChecksumStream.browser" ;
99
10- ( typeof ReadableStream === "function" && process . version >= "v18" ? describe : describe . skip ) ( "Checksum streams" , ( ) => {
11- /**
12- * Hash "algorithm" that appends all data together.
13- */
14- class Appender implements Checksum {
15- public hash = "" ;
16- async digest ( ) : Promise < Uint8Array > {
17- return Buffer . from ( this . hash ) ;
18- }
19- reset ( ) : void {
20- throw new Error ( "Function not implemented." ) ;
21- }
22- update ( chunk : Uint8Array ) : void {
23- this . hash += toUtf8 ( chunk ) ;
10+ ( typeof ReadableStream === "function" && process . version >= "v18" ? describe : describe . skip ) (
11+ "Checksum streams" ,
12+ ( ) => {
13+ /**
14+ * Hash "algorithm" that appends all data together.
15+ */
16+ class Appender implements Checksum {
17+ public hash = "" ;
18+ async digest ( ) : Promise < Uint8Array > {
19+ return Buffer . from ( this . hash ) ;
20+ }
21+ reset ( ) : void {
22+ throw new Error ( "Function not implemented." ) ;
23+ }
24+ update ( chunk : Uint8Array ) : void {
25+ this . hash += toUtf8 ( chunk ) ;
26+ }
2427 }
25- }
2628
27- const canonicalData = new Uint8Array ( "abcdefghijklmnopqrstuvwxyz" . split ( "" ) . map ( ( _ ) => _ . charCodeAt ( 0 ) ) ) ;
29+ const canonicalData = new Uint8Array ( "abcdefghijklmnopqrstuvwxyz" . split ( "" ) . map ( ( _ ) => _ . charCodeAt ( 0 ) ) ) ;
2830
29- const canonicalUtf8 = toUtf8 ( canonicalData ) ;
30- const canonicalBase64 = toBase64 ( canonicalUtf8 ) ;
31-
32- describe ( createChecksumStream . name + " webstreams API" , ( ) => {
33- if ( typeof ReadableStream !== "function" ) {
34- // test not applicable to Node.js 16.
35- return ;
36- }
31+ const canonicalUtf8 = toUtf8 ( canonicalData ) ;
32+ const canonicalBase64 = toBase64 ( canonicalUtf8 ) ;
3733
38- const makeStream = ( ) => {
39- return new ReadableStream ( {
40- start ( controller ) {
41- canonicalData . forEach ( ( byte ) => {
42- controller . enqueue ( new Uint8Array ( [ byte ] ) ) ;
43- } ) ;
44- controller . close ( ) ;
45- } ,
46- } ) ;
47- } ;
34+ describe ( createChecksumStream . name + " webstreams API" , ( ) => {
35+ if ( typeof ReadableStream !== "function" ) {
36+ // test not applicable to Node.js 16.
37+ return ;
38+ }
4839
49- it ( "should extend a ReadableStream" , async ( ) => {
50- const stream = makeStream ( ) ;
51- const checksumStream = createChecksumStream ( {
52- expectedChecksum : canonicalBase64 ,
53- checksum : new Appender ( ) ,
54- checksumSourceLocation : "my-header" ,
55- source : stream ,
56- } ) ;
40+ const makeStream = ( ) => {
41+ return new ReadableStream ( {
42+ start ( controller ) {
43+ canonicalData . forEach ( ( byte ) => {
44+ controller . enqueue ( new Uint8Array ( [ byte ] ) ) ;
45+ } ) ;
46+ controller . close ( ) ;
47+ } ,
48+ } ) ;
49+ } ;
5750
58- expect ( checksumStream ) . toBeInstanceOf ( ReadableStream ) ;
59- expect ( checksumStream ) . toBeInstanceOf ( ChecksumStreamWeb ) ;
51+ it ( "should extend a ReadableStream" , async ( ) => {
52+ const stream = makeStream ( ) ;
53+ const checksumStream = createChecksumStream ( {
54+ expectedChecksum : canonicalBase64 ,
55+ checksum : new Appender ( ) ,
56+ checksumSourceLocation : "my-header" ,
57+ source : stream ,
58+ } ) ;
6059
61- const collected = toUtf8 ( await headStream ( checksumStream , Infinity ) ) ;
62- expect ( collected ) . toEqual ( canonicalUtf8 ) ;
63- expect ( stream . locked ) . toEqual ( true ) ;
60+ expect ( checksumStream ) . toBeInstanceOf ( ReadableStream ) ;
61+ expect ( checksumStream ) . toBeInstanceOf ( ChecksumStreamWeb ) ;
6462
65- // expectation is that it is resolved.
66- expect ( await checksumStream . getReader ( ) . closed ) ;
67- } ) ;
63+ const collected = toUtf8 ( await headStream ( checksumStream , Infinity ) ) ;
64+ expect ( collected ) . toEqual ( canonicalUtf8 ) ;
65+ expect ( stream . locked ) . toEqual ( true ) ;
6866
69- it ( "should throw during stream read if the checksum does not match" , async ( ) => {
70- const stream = makeStream ( ) ;
71- const checksumStream = createChecksumStream ( {
72- expectedChecksum : "different-expected-checksum" ,
73- checksum : new Appender ( ) ,
74- checksumSourceLocation : "my-header" ,
75- source : stream ,
67+ // expectation is that it is resolved.
68+ expect ( await checksumStream . getReader ( ) . closed ) ;
7669 } ) ;
7770
78- try {
79- toUtf8 ( await headStream ( checksumStream , Infinity ) ) ;
80- throw new Error ( "stream was read successfully" ) ;
81- } catch ( e : unknown ) {
82- expect ( String ( e ) ) . toEqual (
83- `Error: Checksum mismatch: expected "different-expected-checksum" but` +
84- ` received "${ canonicalBase64 } "` +
85- ` in response header "my-header".`
86- ) ;
87- }
71+ it ( "should throw during stream read if the checksum does not match" , async ( ) => {
72+ const stream = makeStream ( ) ;
73+ const checksumStream = createChecksumStream ( {
74+ expectedChecksum : "different-expected-checksum" ,
75+ checksum : new Appender ( ) ,
76+ checksumSourceLocation : "my-header" ,
77+ source : stream ,
78+ } ) ;
79+
80+ try {
81+ toUtf8 ( await headStream ( checksumStream , Infinity ) ) ;
82+ throw new Error ( "stream was read successfully" ) ;
83+ } catch ( e : unknown ) {
84+ expect ( String ( e ) ) . toEqual (
85+ `Error: Checksum mismatch: expected "different-expected-checksum" but` +
86+ ` received "${ canonicalBase64 } "` +
87+ ` in response header "my-header".`
88+ ) ;
89+ }
90+ } ) ;
8891 } ) ;
89- } ) ;
90- } ) ;
92+ }
93+ ) ;
0 commit comments