@ -23,7 +23,7 @@ import NotificationsHandler from './Components/NotificationsHandler.svelte';
import { fireNotification , getEmbedsFromCache , getSelectedFile } from "./utils" ;
import { getQueryProcessor , QueryProcessor } from "./websites" ;
import { ifetch , Platform , sendCmd , lqueue , supportedAltDomain , supportedMainDomain } from "./platform" ;
import { ifetch , Platform , sendCmd , lqueue , supportedAltDomain , supportedMainDomain , genPort } from "./platform" ;
import TextEmbeddingsSvelte from "./Components/TextEmbeddings.svelte" ;
import { HydrusClient } from "./hydrus" ;
import { registerPlugin } from 'linkifyjs' ;
@ -47,6 +47,7 @@ const processors: ImageProcessor[] =
let cappState : Parameters < typeof appState [ ' set ' ] > [ 0 ] ;
settings . subscribe ( async b = > {
if ( ! b ) return ;
csettings = b ;
if ( b . hyd ) {
// transition from disable to enabled
if ( b . ak ) {
@ -64,7 +65,6 @@ settings.subscribe(async b => {
}
}
}
csettings = b ;
//processors = [...(!csettings.te ? [thirdeye] : []),
// pngv3, pomf, jpg, webm, gif
//];
@ -212,6 +212,104 @@ const shouldUseCache = () => {
let cp : CommandProcessor ;
class BackgroundEmulator {
async bgCorsFetch ( c : MessagePort ,
pendingFetches : Map < MessagePort , { [ id in number ] : { fetchFully : boolean } } > ,
id : number ,
input : string ,
init? : RequestInit ) {
try {
const res = await ifetch ( input , init ) ;
// don't report progress because monkeys don't have a way to expose partial responses anyway
const headersStr = ( res as any ) . responseHeaders ;
const headerObj = headerStringToObject ( headersStr ) ;
c . postMessage ( {
id ,
ok : res.ok || true ,
setRes : true ,
headers : headerObj ,
responseHeaders : headersStr ,
redirected : res.redirected ,
type : res . type ,
url : res.url ,
status : res.status ,
bodyUsed : res.bodyUsed ,
statusText : res.statusText ,
} ) ;
if ( [ 'GET' , 'POST' ] . includes ( init ? . method || 'GET' ) ) {
const data = await res . arrayBuffer ( ) ;
c . postMessage ( {
id ,
pushData : {
data
}
} , [ data ] ) ;
}
// let's hope these are delivered in order :%)
c . postMessage ( {
id ,
pushData : {
}
} , [ ] ) ;
} catch ( e ) {
c . postMessage ( {
id ,
ok : false ,
setRes : true ,
headers : { } ,
responseHeaders : '' ,
redirected : false ,
status : 400 ,
bodyUsed : false ,
statusText : 'shit broke' ,
} ) ;
}
}
constructor ( private port : MessagePort ) {
const pendingFetches = new Map < MessagePort , { [ id in number ] : { fetchFully : boolean } } > ( ) ;
port . onmessage = async ( obj : MessageEvent ) = > {
const { id , name , args , sid , fid , url } = obj . data as any ;
if ( name == "keepAlive" ) {
console . log ( 'im alive, tho?' ) ;
return ;
}
if ( name == "abortCorsFetch" ) {
//chrome.runtime.sendMessage({ name, sid });
return ;
}
if ( name == "corsFetch" ) {
// this handles the reply
( this . bgCorsFetch as any ) ( port , pendingFetches , id , . . . args ) ;
return ;
}
if ( name == "revoke" ) {
URL . revokeObjectURL ( url ! ) ;
port . postMessage ( {
id , ok : 1
} ) ;
return ;
}
if ( name == "fullyRead" ) {
const obj = pendingFetches . get ( port ) ! ;
if ( obj && fid ! in obj )
obj [ fid ! ] . fetchFully = true ;
port . postMessage ( {
id , ok : 1
} ) ;
return ;
}
const res = await ( Platform as any ) [ name ] ( . . . args ) ;
port . postMessage ( {
id , res
} ) ;
} ;
}
}
class CommandProcessor {
processor = ProcessWorker ( ) ;
@ -229,110 +327,6 @@ class CommandProcessor {
let res : IteratorResult < any , any > ;
switch ( msg . data . type ) {
case 'ipc' :
{
const id = msg . data . msg . id ;
if ( execution_mode != "userscript" ) {
if ( msg . data . msg . name == 'corsFetch' ) {
sendCmd ( msg . data . msg , msg . data . tr ) ;
lqueue [ id ] = ( res : any ) = > {
this . processor . postMessage ( {
type : 'ipc' ,
id ,
res
} ) ;
} ;
} else {
// for complitude, but technically the webworker doesn't run anything besides corsFetch
const repl : any = await sendCmd ( msg . data . msg , msg . data . tr ) ;
repl . id = id ;
this . processor . postMessage ( {
type : 'ipc' ,
id ,
res : repl
} ) ;
}
} else {
if ( msg . data . msg . name == 'fullyRead' ) {
// ignore
this . processor . postMessage ( {
type : 'ipc' ,
res : {
id ,
ok : 1
}
} ) ;
}
if ( msg . data . msg . name == 'corsFetch' ) {
const { args } = msg . data . msg ;
try {
const res = await ifetch ( args [ 0 ] , args [ 1 ] ) ;
// don't report progress because monkeys don't have a way to expose partial responses anyway
const headersStr = ( res as any ) . responseHeaders ;
const headerObj = headerStringToObject ( headersStr ) ;
this . processor . postMessage ( {
type : 'ipc' ,
id ,
res : {
id ,
ok : res.ok || true ,
setRes : true ,
headers : headerObj ,
responseHeaders : headersStr ,
redirected : res.redirected ,
type : res . type ,
url : res.url ,
status : res.status ,
bodyUsed : res.bodyUsed ,
statusText : res.statusText ,
}
} ) ;
if ( ! args [ 1 ] . method || [ 'GET' , 'POST' ] . includes ( args [ 1 ] . method ) ) {
const data = await res . arrayBuffer ( ) ;
this . processor . postMessage ( {
type : 'ipc' ,
id ,
res : {
id ,
pushData : {
data
}
}
} , [ data ] ) ;
}
// let's hope these are delivered in order :%)
this . processor . postMessage ( {
type : 'ipc' ,
id ,
res : {
id ,
pushData : {
}
}
} , [ ] ) ;
} catch ( e ) {
this . processor . postMessage ( {
type : 'ipc' ,
id ,
res : {
id ,
ok : false ,
setRes : true ,
headers : { } ,
responseHeaders : '' ,
redirected : false ,
status : 400 ,
bodyUsed : false ,
statusText : 'shit broke' ,
}
} ) ;
}
}
// ignore other commands
}
} break ;
case 'reply' :
if ( msg . data . id in this . pendingprom ) {
this . pendingprom [ msg . data . id ] ( msg . data . res ) ;
@ -352,7 +346,20 @@ class CommandProcessor {
} ) ;
break ;
}
} ;
if ( execution_mode != "userscript" ) {
const ipcport = genPort ( ) ;
this . processor . postMessage ( { type : 'ipc' , port : ipcport } , [ ipcport ] ) ;
} else {
const nmc = new MessageChannel ( ) ;
const port1 = nmc . port1 ;
const port2 = nmc . port2 ;
new BackgroundEmulator ( port2 ) ;
const ipcport = port1 ;
this . processor . postMessage ( { type : 'ipc' , port : ipcport } , [ ipcport ] ) ;
}
}
serializeArg ( m : any ) {