Skip to content

Instantly share code, notes, and snippets.

@calbertts
Created August 17, 2019 18:14
Show Gist options
  • Save calbertts/9a27ad61160d85472848a5a3d19fae19 to your computer and use it in GitHub Desktop.
Save calbertts/9a27ad61160d85472848a5a3d19fae19 to your computer and use it in GitHub Desktop.
Run external process from QuickJS
import * as std from "std"
import * as os from "os"
import { open, close } from "process"
const CHUNK_SIZE = 200000 // 200KB
const fd = open('curl 2>/dev/null https://jsonplaceholder.typicode.com/users', 'r')
os.setReadHandler(fd, function() {
let readBuf = new Uint8Array(CHUNK_SIZE)
let l = os.read(fd, readBuf.buffer, 0, readBuf.length)
if (!l) {
std.out.printf("\n")
os.setReadHandler(fd, null)
close(fd)
}
let string = String.fromCharCode.apply(null, readBuf)
std.out.printf("%s", string)
})
console.log('This line appears first, because the download runs asynchronously')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment