@@ -43,7 +43,7 @@ export default class SerializeTopojson extends Command {
4343 cli . action . start ( `Reading base TopoJSON: ${ s3URI } ` ) ;
4444 const baseTopojson = await ( flags . input === "json"
4545 ? this . readJson ( s3URI )
46- : flags . output === "buf"
46+ : flags . input === "buf"
4747 ? this . readBuf ( s3URI )
4848 : this . readPbf ( s3URI ) ) ;
4949 cli . action . stop ( ) ;
@@ -61,15 +61,7 @@ export default class SerializeTopojson extends Command {
6161 // Reads a TopoJSON file from S3, given the S3 run directory
6262 async readJson ( inputS3Dir : string ) : Promise < Topology < Objects < { } > > > {
6363 this . log ( "Reading topo.json" ) ;
64- const uriComponents = inputS3Dir . split ( "/" ) ;
65- const bucket = uriComponents [ 2 ] ;
66- const key = `${ uriComponents . slice ( 3 ) . join ( "/" ) } topo.json` ;
67- const response : any = await new S3 ( )
68- . getObject ( {
69- Bucket : bucket ,
70- Key : key
71- } )
72- . promise ( ) ;
64+ const response : any = await new S3 ( ) . getObject ( this . s3Options ( inputS3Dir , "json" ) ) . promise ( ) ;
7365
7466 const objects = await new Promise ( resolve =>
7567 createReadStream ( response . Body as Buffer )
@@ -120,76 +112,50 @@ export default class SerializeTopojson extends Command {
120112 } as Topology < Objects < { } > > ;
121113 }
122114
123- jsonOptions ( inputS3Dir : string ) {
124- const uriComponents = inputS3Dir . split ( "/" ) ;
125- return {
126- Bucket : uriComponents [ 2 ] ,
127- Key : `${ uriComponents . slice ( 3 ) . join ( "/" ) } topo.json`
128- } ;
129- }
130-
131- bufOptions ( inputS3Dir : string ) {
132- const uriComponents = inputS3Dir . split ( "/" ) ;
133- return {
134- Bucket : uriComponents [ 2 ] ,
135- Key : `${ uriComponents . slice ( 3 ) . join ( "/" ) } topo.buf`
136- } ;
115+ read ( inputS3Dir : string , ext : string ) {
116+ this . log ( `Reading topo.${ ext } ` ) ;
117+ const s3Client = new S3 ( ) ;
118+ return s3Client . getObject ( this . s3Options ( inputS3Dir , ext ) ) . promise ( ) ;
137119 }
138120
139- pbfOptions ( inputS3Dir : string ) {
121+ s3Options ( inputS3Dir : string , ext : string ) {
140122 const uriComponents = inputS3Dir . split ( "/" ) ;
141123 return {
142124 Bucket : uriComponents [ 2 ] ,
143- Key : `${ uriComponents . slice ( 3 ) . join ( "/" ) } topo.pbf `
125+ Key : `${ uriComponents . slice ( 3 ) . join ( "/" ) } topo.${ ext } `
144126 } ;
145127 }
146128
147129 async readBuf ( inputS3Dir : string ) {
148- this . log ( "Reading topo.buf" ) ;
149-
150- const s3Client = new S3 ( ) ;
151- const resp = await s3Client . getObject ( this . bufOptions ( inputS3Dir ) ) . promise ( ) ;
130+ const resp = await this . read ( inputS3Dir , "buf" ) ;
152131 return deserialize ( resp . Body as Buffer ) ;
153132 }
154133
155134 async readPbf ( inputS3Dir : string ) {
156- this . log ( "Reading topo.buf" ) ;
157-
158- const s3Client = new S3 ( ) ;
159- const resp = await s3Client . getObject ( this . bufOptions ( inputS3Dir ) ) . promise ( ) ;
135+ const resp = await this . read ( inputS3Dir , "pbf" ) ;
160136 return decode ( new Pbf ( resp . Body as Buffer ) ) as Topology ;
161137 }
162138
163- writeJson ( inputS3Dir : string , topology : Topology < Objects < { } > > ) {
164- this . log ( "Streaming topo.json" ) ;
139+ write ( inputS3Dir : string , ext : string , body : string | Buffer | Uint8Array ) {
140+ this . log ( `Writing topo.${ ext } ` ) ;
165141 const s3Client = new S3 ( ) ;
166142 return s3Client
167143 . upload ( {
168- Body : JSON . stringify ( topology ) ,
169- ...this . jsonOptions ( inputS3Dir )
144+ Body : body ,
145+ ...this . s3Options ( inputS3Dir , ext )
170146 } )
171147 . promise ( ) ;
172148 }
173149
150+ writeJson ( inputS3Dir : string , topology : Topology < Objects < { } > > ) {
151+ return this . write ( inputS3Dir , "json" , JSON . stringify ( topology ) ) ;
152+ }
153+
174154 writeBuf ( inputS3Dir : string , topology : Topology < Objects < { } > > ) {
175- this . log ( "Streaming topo.buf" ) ;
176- const s3Client = new S3 ( ) ;
177- return s3Client
178- . upload ( {
179- Body : serialize ( topology ) ,
180- ...this . bufOptions ( inputS3Dir )
181- } )
182- . promise ( ) ;
155+ return this . write ( inputS3Dir , "buf" , serialize ( topology ) ) ;
183156 }
184157
185158 writePbf ( inputS3Dir : string , topology : Topology < Objects < { } > > ) {
186- this . log ( "Streaming topo.pbf" ) ;
187- const s3Client = new S3 ( ) ;
188- return s3Client
189- . upload ( {
190- Body : encode ( topology , new Pbf ( ) ) ,
191- ...this . pbfOptions ( inputS3Dir )
192- } )
193- . promise ( ) ;
159+ return this . write ( inputS3Dir , "pbf" , encode ( topology , new Pbf ( ) ) ) ;
194160 }
195161}
0 commit comments