import { CompilerService , ServiceInput , ServiceOutput } from "./types" ; import { sendRequestJSON , ServiceTypes } from "./sendRequest" ; import { decodeBinary } from "./utils" ; import * as Tar from "tar-js" ; import { base64EncodeBytes } from "../util" ; export class RustService implements CompilerService { async compile ( input ) { let result ; let fileRef ; const options = input . options ; const cargo = options [ "cargo" ] ; if ( cargo ) { const tarBuffer = new Tar ( ) ; const files = input . files ; Object . entries ( files ) . forEach ( ( [ name , file ] ) => { tarBuffer . append ( name , file . content , { } ) ; } ) ; const tar = base64EncodeBytes ( tarBuffer . out ) ; result = await sendRequestJSON ( { tar , options , } , ServiceTypes . Cargo ) ; fileRef = Object . keys ( files ) [ 0 ] ; } else { const files = Object . values ( input . files ) ; if ( files . length !== 1 ) { throw new Error ( `Supporting compilation of a single file, but ${ files . length } file(s) found` ) ; } fileRef = Object . keys ( input . files ) [ 0 ] ; const code = files [ 0 ] . content ; result = await sendRequestJSON ( { code , options , } , ServiceTypes . Rustc ) ; } const items = { } ; let content ; if ( result . success ) { content = await decodeBinary ( result . output ) ; } let console ; if ( result . tasks && result . tasks . length > 0 ) { console = result . tasks [ 0 ] . console ; } const extraItems = { } ; if ( result . wasmBindgenJs ) { extraItems [ "wasm_bindgen.js" ] = { content : result . wasmBindgenJs , } ; } return { success : result . success , items : { "a.wasm" : { content , fileRef , console , } , ... extraItems } , console : result . message } ; } }