Created
August 17, 2019 18:14
-
-
Save calbertts/9a27ad61160d85472848a5a3d19fae19 to your computer and use it in GitHub Desktop.
Run external process from QuickJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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