1- import * as fs from "node: fs" ;
1+ import * as fs from "fs" ;
22
33import { defined , exceptionToString } from "@glideapps/ts-necessities" ;
44import { isNode } from "browser-or-node" ;
@@ -13,82 +13,79 @@ import { getStream } from "./get-stream";
1313import { fetch } from "./$fetch" ;
1414
1515interface HttpHeaders {
16- [ key : string ] : string ;
16+ [ key : string ] : string ;
1717}
1818
1919function parseHeaders ( httpHeaders ?: string [ ] ) : HttpHeaders {
20- if ( ! Array . isArray ( httpHeaders ) ) {
21- return { } ;
22- }
20+ if ( ! Array . isArray ( httpHeaders ) ) {
21+ return { } ;
22+ }
2323
24- return httpHeaders . reduce ( ( result : HttpHeaders , httpHeader : string ) => {
25- if ( httpHeader !== undefined && httpHeader . length > 0 ) {
26- const split = httpHeader . indexOf ( ":" ) ;
24+ return httpHeaders . reduce ( ( result : HttpHeaders , httpHeader : string ) => {
25+ if ( httpHeader !== undefined && httpHeader . length > 0 ) {
26+ const split = httpHeader . indexOf ( ":" ) ;
2727
28- if ( split < 0 ) {
29- return panic ( `Could not parse HTTP header "${ httpHeader } ".` ) ;
30- }
28+ if ( split < 0 ) {
29+ return panic ( `Could not parse HTTP header "${ httpHeader } ".` ) ;
30+ }
3131
32- const key = httpHeader . slice ( 0 , split ) . trim ( ) ;
33- const value = httpHeader . slice ( split + 1 ) . trim ( ) ;
34- result [ key ] = value ;
35- }
32+ const key = httpHeader . slice ( 0 , split ) . trim ( ) ;
33+ const value = httpHeader . slice ( split + 1 ) . trim ( ) ;
34+ result [ key ] = value ;
35+ }
3636
37- return result ;
38- } , { } as HttpHeaders ) ;
37+ return result ;
38+ } , { } as HttpHeaders ) ;
3939}
4040
4141export async function readableFromFileOrURL (
42- fileOrURL : string ,
43- httpHeaders ?: string [ ] ,
42+ fileOrURL : string ,
43+ httpHeaders ?: string [ ]
4444) : Promise < Readable > {
45- try {
46- if ( isURL ( fileOrURL ) ) {
47- const response = await fetch ( fileOrURL , {
48- headers : parseHeaders ( httpHeaders ) ,
49- } ) ;
50-
51- return defined ( response . body ) as unknown as Readable ;
52- }
53-
54- if ( isNode ) {
55- if ( fileOrURL === "-" ) {
56- // Cast node readable to isomorphic readable from readable-stream
57- return process . stdin as unknown as Readable ;
58- }
59-
60- const filePath = fs . lstatSync ( fileOrURL ) . isSymbolicLink ( )
61- ? fs . readlinkSync ( fileOrURL )
62- : fileOrURL ;
63- if ( fs . existsSync ( filePath ) ) {
64- // Cast node readable to isomorphic readable from readable-stream
65- return fs . createReadStream (
66- filePath ,
67- "utf8" ,
68- ) as unknown as Readable ;
69- }
70- }
71- } catch ( e ) {
72- return messageError ( "MiscReadError" , {
73- fileOrURL,
74- message : exceptionToString ( e ) ,
75- } ) ;
45+ try {
46+ if ( isURL ( fileOrURL ) ) {
47+ const response = await fetch ( fileOrURL , {
48+ headers : parseHeaders ( httpHeaders ) ,
49+ } ) ;
50+
51+ return defined ( response . body ) as unknown as Readable ;
7652 }
7753
78- return messageError ( "DriverInputFileDoesNotExist" , { filename : fileOrURL } ) ;
54+ if ( isNode ) {
55+ if ( fileOrURL === "-" ) {
56+ // Cast node readable to isomorphic readable from readable-stream
57+ return process . stdin as unknown as Readable ;
58+ }
59+
60+ const filePath = fs . lstatSync ( fileOrURL ) . isSymbolicLink ( )
61+ ? fs . readlinkSync ( fileOrURL )
62+ : fileOrURL ;
63+ if ( fs . existsSync ( filePath ) ) {
64+ // Cast node readable to isomorphic readable from readable-stream
65+ return fs . createReadStream ( filePath , "utf8" ) as unknown as Readable ;
66+ }
67+ }
68+ } catch ( e ) {
69+ return messageError ( "MiscReadError" , {
70+ fileOrURL,
71+ message : exceptionToString ( e ) ,
72+ } ) ;
73+ }
74+
75+ return messageError ( "DriverInputFileDoesNotExist" , { filename : fileOrURL } ) ;
7976}
8077
8178export async function readFromFileOrURL (
82- fileOrURL : string ,
83- httpHeaders ?: string [ ] ,
79+ fileOrURL : string ,
80+ httpHeaders ?: string [ ]
8481) : Promise < string > {
85- const readable = await readableFromFileOrURL ( fileOrURL , httpHeaders ) ;
86- try {
87- return await getStream ( readable ) ;
88- } catch ( e ) {
89- return messageError ( "MiscReadError" , {
90- fileOrURL,
91- message : exceptionToString ( e ) ,
92- } ) ;
93- }
82+ const readable = await readableFromFileOrURL ( fileOrURL , httpHeaders ) ;
83+ try {
84+ return await getStream ( readable ) ;
85+ } catch ( e ) {
86+ return messageError ( "MiscReadError" , {
87+ fileOrURL,
88+ message : exceptionToString ( e ) ,
89+ } ) ;
90+ }
9491}
0 commit comments