@@ -16,7 +16,7 @@ import {
1616import scramjetWASM from "../../scramjet/packages/core/dist/scramjet.wasm.wasm?url" ;
1717import scramjetAll from "../../scramjet/packages/core/dist/scramjet.js?url" ;
1818import injectScript from "../../inject/dist/inject.js?url" ;
19- import { BareClient } from "@mercuryworkshop/bare-mux-custom" ;
19+ import { BareClient , type BareHeaders } from "@mercuryworkshop/bare-mux-custom" ;
2020import { ElementType , type Handler , Parser } from "htmlparser2" ;
2121import { type ChildNode , DomHandler , Element , Comment , Node } from "domhandler" ;
2222import * as tldts from "tldts" ;
@@ -360,9 +360,12 @@ export class IsolatedFrame {
360360 }
361361}
362362
363- function isDownload ( responseHeaders : any , destination : string ) : boolean {
363+ function isDownload (
364+ responseHeaders : BareHeaders ,
365+ destination : string
366+ ) : boolean {
364367 if ( [ "document" , "iframe" ] . includes ( destination ) ) {
365- const header = responseHeaders [ "content-disposition" ] ;
368+ const header = responseHeaders [ "content-disposition" ] ?. [ 0 ] ;
366369 if ( header ) {
367370 if ( header === "inline" ) {
368371 return false ; // force it to show in browser
@@ -383,7 +386,7 @@ function isDownload(responseHeaders: any, destination: string): boolean {
383386 "application/xml" ,
384387 "application/pdf" ,
385388 ] ;
386- const contentType = responseHeaders [ "content-type" ]
389+ const contentType = responseHeaders [ "content-type" ] ?. [ 0 ]
387390 ?. split ( ";" ) [ 0 ]
388391 . trim ( )
389392 . toLowerCase ( ) ;
@@ -403,6 +406,14 @@ function isDownload(responseHeaders: any, destination: string): boolean {
403406 return false ;
404407}
405408
409+ export type RawDownload = {
410+ filename : string | null ;
411+ url : string ;
412+ type : string ;
413+ body : BodyType ;
414+ length : number ;
415+ } ;
416+
406417const methods = {
407418 async fetch (
408419 data : ScramjetFetchContext ,
@@ -476,19 +487,30 @@ const methods = {
476487
477488 const fetchresponse = await controller . fetchHandler . handleFetch ( data ) ;
478489
490+ console . log ( fetchresponse . headers ) ;
479491 if (
480492 isDownload ( fetchresponse . headers , data . destination ) &&
481493 fetchresponse . status === 200
482494 ) {
483495 let filename : string | null = null ;
484- const disp = fetchresponse . headers [ "content-disposition" ] ;
496+ const disp = fetchresponse . headers [ "content-disposition" ] ?. [ 0 ] ;
485497 if ( typeof disp === "string" ) {
486498 const filenameMatch = disp . match ( / f i l e n a m e = [ " ' ] ? ( [ ^ " ' ; \n ] * ) [ " ' ] ? / i) ;
487499 if ( filenameMatch && filenameMatch [ 1 ] ) {
488500 filename = filenameMatch [ 1 ] ;
489501 }
490502 }
491- const length = fetchresponse . headers [ "content-length" ] ;
503+ const length = fetchresponse . headers [ "content-length" ] [ 0 ] ;
504+
505+ browser . startDownload ( {
506+ filename,
507+ url : unrewriteUrl ( data . rawUrl , { prefix : controller . prefix } as any ) ,
508+ type :
509+ fetchresponse . headers [ "content-type" ] [ 0 ] ||
510+ "application/octet-stream" ,
511+ length : parseInt ( length ) ,
512+ body : fetchresponse . body ,
513+ } ) ;
492514
493515 // endless vortex reference
494516 await new Promise ( ( ) => { } ) ;
0 commit comments