@@ -4,6 +4,7 @@ const Buffer = require('safe-buffer').Buffer
4
4
const BB = require ( 'bluebird' )
5
5
6
6
const crypto = require ( 'crypto' )
7
+ const finished = BB . promisify ( require ( 'mississippi' ) . finished )
7
8
const path = require ( 'path' )
8
9
const Tacks = require ( 'tacks' )
9
10
const test = require ( 'tap' ) . test
@@ -37,15 +38,18 @@ test('read.stream: returns a stream with cache content data', function (t) {
37
38
stream . on ( 'error' , function ( e ) { throw e } )
38
39
let buf = ''
39
40
stream . on ( 'data' , function ( data ) { buf += data } )
40
- stream . on ( 'end' , function ( ) {
41
- t . ok ( true , 'stream completed successfully' )
42
- t . deepEqual ( Buffer . from ( buf ) , CONTENT , 'cache contents read correctly' )
43
- t . end ( )
44
- } )
41
+ return BB . join (
42
+ finished ( stream ) . then ( ( ) => Buffer . from ( buf ) ) ,
43
+ read ( CACHE , DIGEST ) ,
44
+ ( fromStream , fromBulk ) => {
45
+ t . deepEqual ( fromStream , CONTENT , 'stream data checks out' )
46
+ t . deepEqual ( fromBulk , CONTENT , 'promise data checks out' )
47
+ }
48
+ )
45
49
} )
46
50
47
- test ( 'read.stream : allows hashAlgorithm configuration' , function ( t ) {
48
- const CONTENT = 'foobarbaz'
51
+ test ( 'read: allows hashAlgorithm configuration' , function ( t ) {
52
+ const CONTENT = Buffer . from ( 'foobarbaz' )
49
53
const HASH = 'whirlpool'
50
54
const DIGEST = crypto . createHash ( HASH ) . update ( CONTENT ) . digest ( 'hex' )
51
55
const fixture = new Tacks ( CacheContent ( {
@@ -56,44 +60,78 @@ test('read.stream: allows hashAlgorithm configuration', function (t) {
56
60
stream . on ( 'error' , function ( e ) { throw e } )
57
61
let buf = ''
58
62
stream . on ( 'data' , function ( data ) { buf += data } )
59
- stream . on ( 'end' , function ( ) {
60
- t . ok ( true , 'stream completed successfully, off a sha512' )
61
- t . deepEqual ( buf , CONTENT , 'cache contents read correctly' )
62
- t . end ( )
63
- } )
63
+ return BB . join (
64
+ finished ( stream ) . then ( ( ) => Buffer . from ( buf ) ) ,
65
+ read ( CACHE , DIGEST , {
66
+ hashAlgorithm : HASH
67
+ } ) ,
68
+ ( fromStream , fromBulk ) => {
69
+ t . deepEqual ( fromStream , CONTENT , 'stream used algorithm' )
70
+ t . deepEqual ( fromBulk , CONTENT , 'promise used algorithm' )
71
+ }
72
+ )
64
73
} )
65
74
66
- test ( 'read.stream : errors if content missing' , function ( t ) {
75
+ test ( 'read: errors if content missing' , function ( t ) {
67
76
const stream = read . stream ( CACHE , 'whatnot' )
68
- stream . on ( 'error' , function ( e ) {
69
- t . ok ( e , 'got an error!' )
70
- t . equal ( e . code , 'ENOENT' , 'error uses ENOENT error code' )
71
- t . end ( )
72
- } )
73
77
stream . on ( 'data' , function ( data ) {
74
78
throw new Error ( 'unexpected data: ' + JSON . stringify ( data ) )
75
79
} )
76
80
stream . on ( 'end' , function ( ) {
77
81
throw new Error ( 'end was called even though stream errored' )
78
82
} )
83
+ return BB . join (
84
+ finished ( stream ) . catch ( { code : 'ENOENT' } , err => err ) ,
85
+ read ( CACHE , 'whatnot' ) . catch ( { code : 'ENOENT' } , err => err ) ,
86
+ ( streamErr , bulkErr ) => {
87
+ t . equal ( streamErr . code , 'ENOENT' , 'stream got the right error' )
88
+ t . equal ( bulkErr . code , 'ENOENT' , 'bulk got the right error' )
89
+ }
90
+ )
79
91
} )
80
92
81
- test ( 'read.stream : errors if content fails checksum' , function ( t ) {
82
- const CONTENT = 'foobarbaz'
93
+ test ( 'read: errors if content fails checksum' , function ( t ) {
94
+ const CONTENT = Buffer . from ( 'foobarbaz' )
83
95
const DIGEST = crypto . createHash ( 'sha512' ) . update ( CONTENT ) . digest ( 'hex' )
84
96
const fixture = new Tacks ( CacheContent ( {
85
97
[ DIGEST ] : CONTENT . slice ( 3 ) // invalid contents!
86
98
} ) )
87
99
fixture . create ( CACHE )
88
100
const stream = read . readStream ( CACHE , DIGEST )
89
- stream . on ( 'error' , function ( e ) {
90
- t . ok ( e , 'got an error!' )
91
- t . equal ( e . code , 'EBADCHECKSUM' , 'error uses EBADCHECKSUM error code' )
92
- t . end ( )
101
+ stream . on ( 'end' , function ( ) {
102
+ throw new Error ( 'end was called even though stream errored' )
93
103
} )
104
+ return BB . join (
105
+ finished ( stream ) . catch ( { code : 'EBADCHECKSUM' } , err => err ) ,
106
+ read ( CACHE , DIGEST ) . catch ( { code : 'EBADCHECKSUM' } , err => err ) ,
107
+ ( streamErr , bulkErr ) => {
108
+ t . equal ( streamErr . code , 'EBADCHECKSUM' , 'stream got the right error' )
109
+ t . equal ( bulkErr . code , 'EBADCHECKSUM' , 'bulk got the right error' )
110
+ }
111
+ )
112
+ } )
113
+
114
+ test ( 'read: errors if content size does not match size option' , function ( t ) {
115
+ const CONTENT = Buffer . from ( 'foobarbaz' )
116
+ const DIGEST = crypto . createHash ( 'sha512' ) . update ( CONTENT ) . digest ( 'hex' )
117
+ const fixture = new Tacks ( CacheContent ( {
118
+ [ DIGEST ] : CONTENT . slice ( 3 ) // bad size!
119
+ } ) )
120
+ fixture . create ( CACHE )
121
+ const stream = read . readStream ( CACHE , DIGEST , { size : CONTENT . length } )
94
122
stream . on ( 'end' , function ( ) {
95
123
throw new Error ( 'end was called even though stream errored' )
96
124
} )
125
+ return BB . join (
126
+ finished ( stream ) . catch ( { code : 'EBADSIZE' } , err => err ) ,
127
+ read ( CACHE , DIGEST , {
128
+ size : CONTENT . length
129
+ } ) . catch ( { code : 'EBADSIZE' } , err => err ) ,
130
+ ( streamErr , bulkErr ) => {
131
+ t . equal ( streamErr . code , 'EBADSIZE' , 'stream got the right error' )
132
+ t . equal ( bulkErr . code , 'EBADSIZE' , 'bulk got the right error' )
133
+ }
134
+ )
97
135
} )
98
136
99
137
test ( 'hasContent: returns true when a cache file exists' , function ( t ) {
0 commit comments