char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
f20b64f
main
1import { safePromise } from "ext:core/promises" ; 2import { 3ArrayBufferPrototypeGetResizable , 4getter , 5TypedArrayPrototypeGetBuffer , 6TypedArrayPrototypeGetByteLength , 7TypedArrayPrototypeGetTag , 8TypeError , 9Uint8Array , 10Uint8ArrayPrototypeSet , 11} from "ext:core/primordials" ; 12import { 13op_stdio_cancel_read , 14op_stdio_flush , 15op_stdio_read , 16op_stdio_write , 17} from "ext:core/ops" ; 18import { 19byobRequest as getRequest , 20close , 21enqueue , 22ReadableStream , 23ReadableStreamBYOBRequest , 24respond , 25WritableStream , 26} from "ext:webstreams/streams.js" ; 27const getView = getter ( ReadableStreamBYOBRequest . prototype , "view" ); 28 29let canceled = false ; 30const stdin = new ReadableStream < Uint8Array < ArrayBuffer >>({ 31type :"bytes" , 32async pull ( controller ) { 33const request = getRequest ( controller ); 34const view = request && getView ( request ); 35const bytes = await safePromise ( op_stdio_read ( 36view ?TypedArrayPrototypeGetByteLength ( view ) :64 * 1024 , 37)); 38if ( canceled ) return ; 39if ( bytes === null ) { 40close ( controller ); 41if ( request ) respond ( request , 0 ); 42} else if ( request ) { 43Uint8ArrayPrototypeSet ( view ! , bytes ); 44respond ( request , TypedArrayPrototypeGetByteLength ( bytes )); 45} else { 46enqueue ( controller , bytes ); 47} 48}, 49cancel () { 50canceled = true ; 51op_stdio_cancel_read (); 52}, 53}, { highWaterMark :0 }); 54 55const output = ( stderr :boolean ) => 56new WritableStream < Uint8Array < ArrayBufferLike >>({ 57write ( bytes ) { 58if ( TypedArrayPrototypeGetTag ( bytes ) !== "Uint8Array" ) { 59throw new TypeError ( "Stdio writes require Uint8Array chunks" ); 60} 61const buffer = TypedArrayPrototypeGetBuffer ( bytes ); 62let snapshot :boolean ; 63try { 64snapshot = ArrayBufferPrototypeGetResizable ( buffer ); 65} catch { 66snapshot = true ; // SharedArrayBuffer fails the ArrayBuffer brand check. 67} 68// serde_v8 needs fixed, unshared storage; the captured constructor copies only the view. 69if ( snapshot ) bytes = new Uint8Array ( bytes ); 70return op_stdio_write ([ stderr , bytes as Uint8Array < ArrayBuffer >]); 71}, 72close () { 73return op_stdio_flush ( stderr ); 74}, 75}, { 76highWaterMark :64 * 1024 , 77size ( bytes ) { 78return TypedArrayPrototypeGetTag ( bytes ) === "Uint8Array" 79 ?TypedArrayPrototypeGetByteLength ( bytes ) 80 :1 ; 81}, 82}); 83 84const stdout = output ( false ), stderr = output ( true ); 85export { stderr , stdin , stdout };