@@ -30,24 +30,49 @@ class DocumentReferenceHandler extends stream_1.Transform {
3030 } ) ;
3131 this . options = options ;
3232 }
33- async downloadAttachment ( url ) {
34- if ( url . search ( / ^ h t t p s ? : \/ \/ . + / ) === - 1 ) {
35- url = new url_1 . URL ( url , this . options . baseUrl ) . href ;
33+ async downloadAttachment ( attachment ) {
34+ if ( ! attachment . url ) {
35+ throw new Error ( "DocumentReferenceHandler.downloadAttachment called on attachment that has no 'url'" ) ;
3636 }
37+ // If the url is relative then convert it to absolute on the same base
38+ const url = new url_1 . URL ( attachment . url , this . options . baseUrl ) ;
39+
3740 const res = await this . options . request ( {
3841 url,
3942 responseType : "buffer" ,
40- throwHttpErrors : false
43+ throwHttpErrors : false ,
44+ headers : {
45+ accept : attachment . contentType || "application/json+fhir"
46+ }
4147 } ) ;
48+
4249 if ( res . statusCode >= 400 ) {
4350 throw new errors_1 . FileDownloadError ( {
44- fileUrl : url ,
51+ fileUrl : attachment . url ,
4552 body : null ,
4653 code : res . statusCode
4754 } ) ;
4855 }
49- this . options . onDownloadComplete ( url , res . body . byteLength ) ;
50- return res ;
56+
57+ const contentType = res . headers [ "content-type" ] || "" ;
58+
59+ // We may have gotten back a Binary FHIR resource
60+ if ( contentType . match ( / \b a p p l i c a t i o n \/ j s o n ( \+ f h i r ) ? \b / ) ) {
61+ const json = JSON . parse ( res . body . toString ( "utf8" ) ) ;
62+ const { resourceType, contentType, data } = json ;
63+ if ( resourceType === "Binary" ) {
64+ const buffer = Buffer . from ( data , "base64" ) ;
65+ this . options . onDownloadComplete ( attachment . url , buffer . byteLength ) ;
66+ return { contentType, data : buffer } ;
67+ }
68+ }
69+
70+ this . options . onDownloadComplete ( attachment . url , res . body . byteLength ) ;
71+
72+ return {
73+ contentType : contentType || attachment . contentType || "" ,
74+ data : res . body
75+ } ;
5176 }
5277 async inlineAttachmentData ( node , data ) {
5378 if ( node . contentType == "application/pdf" && this . options . pdfToText ) {
@@ -65,28 +90,27 @@ class DocumentReferenceHandler extends stream_1.Transform {
6590 if ( ! attachment . url ) {
6691 continue ;
6792 }
68- const response = await this . downloadAttachment ( attachment . url ) ;
69- if ( this . canPutAttachmentInline ( response , attachment . contentType ) ) {
70- await this . inlineAttachmentData ( attachment , response . body ) ;
93+ const response = await this . downloadAttachment ( attachment ) ;
94+ if ( this . canPutAttachmentInline ( response . data , response . contentType ) ) {
95+ await this . inlineAttachmentData ( attachment , response . data ) ;
7196 }
7297 else {
7398 const fileName = Date . now ( ) + "-" + node_jose_1 . default . util . randomBytes ( 6 ) . toString ( "hex" ) + ( 0 , path_1 . extname ) ( attachment . url ) ;
74- await this . options . save ( fileName , stream_1 . Readable . from ( response . body ) , "attachments" ) ;
99+ await this . options . save ( fileName , stream_1 . Readable . from ( response . data ) , "attachments" ) ;
75100 attachment . url = `./attachments/${ fileName } ` ;
76101 }
77102 this . emit ( "attachment" ) ;
78103 }
79104 return resource ;
80105 }
81- canPutAttachmentInline ( response , contentType ) {
82- if ( response . body . byteLength > this . options . inlineAttachments ) {
106+ canPutAttachmentInline ( data , contentType ) {
107+ if ( data . byteLength > this . options . inlineAttachments ) {
83108 return false ;
84109 }
85- const type = contentType || response . headers [ "content-type" ] || "" ;
86- if ( ! type ) {
110+ if ( ! contentType ) {
87111 return false ;
88112 }
89- if ( ! this . options . inlineAttachmentTypes . find ( m => type . startsWith ( m ) ) ) {
113+ if ( ! this . options . inlineAttachmentTypes . find ( m => contentType . startsWith ( m ) ) ) {
90114 return false ;
91115 }
92116 return true ;
0 commit comments