@@ -20,7 +20,6 @@ import * as fs from 'fs';
20
20
import { dirname , join , resolve } from 'path' ;
21
21
import { promisify } from 'util' ;
22
22
23
- const exists = promisify ( fs . exists ) ;
24
23
const stat = promisify ( fs . stat ) ;
25
24
const writeFile = promisify ( fs . writeFile ) ;
26
25
const readFile = promisify ( fs . readFile ) ;
@@ -29,6 +28,17 @@ const mkdir = promisify(fs.mkdir);
29
28
// tslint:disable-next-line:max-line-length
30
29
import { getModelArtifactsInfoForJSON , toArrayBuffer , toBuffer } from './io_utils' ;
31
30
31
+ function notExist ( name : string ) : ( e : NodeJS . ErrnoException ) => never {
32
+ return e => {
33
+ switch ( e . code ) {
34
+ case 'ENOENT' :
35
+ throw new Error ( `${ name } ${ e . path } does not exist: loading failed` ) ;
36
+ default :
37
+ throw e ;
38
+ }
39
+ } ;
40
+ }
41
+
32
42
export class NodeFileSystem implements tfc . io . IOHandler {
33
43
static readonly URL_SCHEME = 'file://' ;
34
44
@@ -107,13 +117,11 @@ export class NodeFileSystem implements tfc.io.IOHandler {
107
117
// https://github.com/tensorflow/tfjs/issues/343
108
118
}
109
119
110
- if ( ! await exists ( this . path ) ) {
111
- throw new Error ( `Path ${ this . path } does not exist: loading failed.` ) ;
112
- }
120
+ const info = await stat ( this . path ) . catch ( notExist ( 'Path' ) ) ;
113
121
114
122
// `this.path` can be either a directory or a file. If it is a file, assume
115
123
// it is model.json file.
116
- if ( ( await stat ( this . path ) ) . isFile ( ) ) {
124
+ if ( info . isFile ( ) ) {
117
125
const modelJSON = JSON . parse ( await readFile ( this . path , 'utf8' ) ) ;
118
126
119
127
const modelArtifacts : tfc . io . ModelArtifacts = {
@@ -126,11 +134,8 @@ export class NodeFileSystem implements tfc.io.IOHandler {
126
134
for ( const group of modelJSON . weightsManifest ) {
127
135
for ( const path of group . paths ) {
128
136
const weightFilePath = join ( dirName , path ) ;
129
- if ( ! await exists ( weightFilePath ) ) {
130
- throw new Error ( `Weight file ${
131
- weightFilePath } does not exist: loading failed`) ;
132
- }
133
- const buffer = await readFile ( weightFilePath ) ;
137
+ const buffer = await readFile ( weightFilePath )
138
+ . catch ( notExist ( 'Weight file' ) ) ;
134
139
buffers . push ( buffer ) ;
135
140
}
136
141
weightSpecs . push ( ...group . weights ) ;
0 commit comments