@@ -15,7 +15,7 @@ module.exports = mosh;
15
15
16
16
async function mosh ( ...args ) {
17
17
// it's cool cuz it's nerdy.
18
- const [ err , source , mode , write , ext ] = await preflightChecks ( ...args ) ;
18
+ const [ err , write , source , mode , ext ] = await preflightChecks ( ...args ) ;
19
19
20
20
// callback with error if using cb, else throw
21
21
if ( err ) {
@@ -35,133 +35,108 @@ async function mosh(...args) {
35
35
// encode image
36
36
const encodedMosh = encode ( imgBuff , [ width , height ] , ext ) ;
37
37
38
+ // return data if we aren't using a CB
38
39
if ( ! write ) return Buffer . from ( encodedMosh ) ;
39
-
40
40
write ( null , Buffer . from ( encodedMosh ) ) ;
41
41
}
42
42
43
43
async function preflightChecks ( ...args ) {
44
44
let [ source , mode , write ] = args ;
45
+ let ext ;
46
+
47
+ // source validation
48
+ const sourceIsBuffer = Buffer . isBuffer ( source ) ;
49
+ const sourceIsString = typeof source === "string" || source instanceof String ;
45
50
46
- // validate source image; may be a buffer or path.
47
- let sourceIsPath , sourcePath , ext ;
48
- if (
49
- ! source ||
50
- ( source &&
51
- ! Buffer . isBuffer ( source ) &&
52
- ! ( sourceIsPath = source . constructor . name === "String" ) )
53
- ) {
51
+ if ( ! source || ( ! sourceIsString && ! sourceIsBuffer ) ) {
54
52
return [
55
53
"Invalid image source -- source must be of type String (path) or Buffer." ,
56
- null ,
57
- null ,
58
54
write ,
59
55
] ;
60
56
}
61
57
62
- if ( sourceIsPath ) {
63
- if ( ! ( sourcePath = await validatePath ( source ) ) ) {
64
- return [
65
- `Invalid source path: ${ source } \n\tFile does not exist.` ,
66
- null ,
67
- null ,
68
- write ,
69
- ] ;
58
+ if ( sourceIsString ) {
59
+ const sourcePath = await validatePath ( source ) ;
60
+
61
+ if ( ! sourcePath ) {
62
+ return [ `Invalid source path: ${ source } \nFile does not exist.` , write ] ;
70
63
}
71
64
72
65
ext = parse ( sourcePath ) . ext . replace ( "." , "" ) ;
73
66
74
67
if ( ! validateExtension ( ext ) ) {
75
- return [ `Invalid file type: ${ ext } ` , null , null , write ] ;
68
+ return [ `Invalid file type: ${ ext } ` , write ] ;
76
69
}
77
70
78
- // load image data
79
71
try {
80
72
source = await readFile ( sourcePath ) ;
81
- } catch ( e ) {
82
- return [ e . message , null , null , write ] ;
73
+ } catch ( err ) {
74
+ return [ e . message , write ] ;
83
75
}
84
76
}
85
77
86
- // valid mode, if passed; checks against supported modes.
87
- let isString , isArray ;
88
- if (
89
- mode &&
90
- ! ( isString = mode . constructor . name === "String" ) &&
91
- ! ( isArray = mode . constructor . name === "Array" )
92
- ) {
78
+ // ext validation
79
+ if ( ! ext ) {
80
+ const fileType = await FileType . fromBuffer ( source ) ;
81
+
82
+ if ( fileType ?. ext && validateExtension ( fileType ?. ext ) ) {
83
+ ext = fileType . ext ;
84
+ } else {
85
+ return [ `Invalid file type, requires supported image file.` , write ] ;
86
+ }
87
+ }
88
+
89
+ // mode validation
90
+ const modeIsString = typeof mode === "string" || mode instanceof String ;
91
+ const modeIsArray = Array . isArray ( mode ) ;
92
+
93
+ if ( mode && ! modeIsString && ! modeIsArray ) {
93
94
return [
94
95
`Invalid mode: '${ mode } '; mode must be of type String or Array.` ,
95
- null ,
96
- null ,
97
96
write ,
98
97
] ;
99
- } else if ( mode ) {
100
- if ( isArray ) {
101
- let e = [ ] ;
102
- mode . forEach ( ( m , i ) => {
103
- if ( ! mosh . MODES [ m ] && m !== null ) e . push ( m ) ;
104
-
105
- // assign random mode for any null values
106
- if ( m === null ) mode . splice ( i , 1 , randomMode ( ) ) ;
107
- } ) ;
108
-
109
- if ( e . length > 0 )
110
- return [ `Invalid mosh modes: '${ e . join ( "," ) } '` , null , null , write ] ;
111
- }
112
-
113
- if ( isString ) {
114
- if ( ! mosh . MODES [ mode ] )
115
- return [ `Invalid mosh mode: '${ mode } '` , null , null , write ] ;
116
-
117
- mode = [ mode ] ;
118
- }
119
98
}
120
99
121
- if ( ! mode ) mode = [ randomMode ( ) ] ;
100
+ if ( mode && modeIsString ) {
101
+ if ( ! mosh . MODES [ mode ] ) {
102
+ return [ `Invalid mosh mode: '${ mode } '` , write ] ;
103
+ }
122
104
123
- // ext may be passed instead of write for buffer return
124
- if (
125
- write &&
126
- write . constructor . name === "String" &&
127
- validateExtension ( write . replace ( "." , "" ) )
128
- ) {
129
- ext = write ;
130
- write = null ;
105
+ mode = [ mode ] ;
131
106
}
132
107
133
- if ( ! ext ) {
134
- const fileType = await FileType . fromBuffer ( source ) ;
108
+ if ( mode && modeIsArray ) {
109
+ let e = [ ] ;
110
+ mode . forEach ( ( m , i ) => {
111
+ if ( ! mosh . MODES [ m ] && m != null ) e . push ( m ) ;
135
112
136
- if ( fileType && fileType . ext && validateExtension ( fileType . ext ) ) {
137
- ext = fileType . ext ;
138
- } else {
139
- return [
140
- `Invalid file type, requires supported image file.` ,
141
- null ,
142
- null ,
143
- write ,
144
- ] ;
113
+ // assign random mode for any null values
114
+ if ( m === null ) mode . splice ( i , 1 , randomMode ( ) ) ;
115
+ } ) ;
116
+
117
+ if ( e . length > 0 ) {
118
+ return [ `Invalid mosh modes: '${ e . join ( "," ) } '` , write ] ;
145
119
}
146
120
}
147
121
148
- // validate write, if passed; may be cb function or path.
149
- let writeIsPath , writePath ;
150
- if (
151
- write &&
152
- ! ( writeIsPath = write . constructor . name === "String" ) &&
153
- write . constructor . name !== "Function"
154
- ) {
155
- return [ `Invalid callback, or write path.` , null , null , write ] ;
122
+ if ( ! mode ) mode = [ randomMode ( ) ] ;
123
+
124
+ // write validation
125
+ const writeIsString = typeof write === "string" || write instanceof String ;
126
+ const writeIsCb = write instanceof Function ;
127
+
128
+ if ( write && ! writeIsString && ! writeIsCb ) {
129
+ return [ `Invalid callback, or write path.` , write ] ;
156
130
}
157
131
158
- if ( writeIsPath ) {
132
+ if ( writeIsString ) {
159
133
// bubble up path errors
160
134
const dir = dirname ( write ) ;
161
135
const filename = basename ( write ) ;
136
+ const writePath = await validatePath ( dir ) ;
162
137
163
- if ( ! ( writePath = await validatePath ( dir ) ) ) {
164
- return [ `Invalid write location: ${ dir } ` , null , null , write ] ;
138
+ if ( ! writePath ) {
139
+ return [ `Invalid write location: ${ dir } ` , write ] ;
165
140
}
166
141
167
142
// prepare write function
@@ -171,7 +146,7 @@ async function preflightChecks(...args) {
171
146
} ;
172
147
}
173
148
174
- return [ null , source , mode , write , ext ] ;
149
+ return [ null , write , source , mode , ext ] ;
175
150
}
176
151
177
152
async function validatePath ( fpath ) {
@@ -181,6 +156,7 @@ async function validatePath(fpath) {
181
156
}
182
157
183
158
try {
159
+ // stat will reject when the path is invalid
184
160
await stat ( fpath ) ;
185
161
return fpath ;
186
162
} catch ( err ) {
@@ -205,7 +181,7 @@ function validateExtension(ext) {
205
181
}
206
182
207
183
function handleError ( msg , cb ) {
208
- const usingCb = cb && cb . constructor . name === " Function" ;
184
+ const usingCb = cb && cb instanceof Function ;
209
185
const error = new Error ( msg ) ;
210
186
211
187
if ( usingCb ) cb ( error ) ;
0 commit comments