Something went wrong. Try again.
javascript for 🐜's, a tiny runtime with big ambitions antjs.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103const { spawnSync } = require('child_process');const path = require('path');
function fail(message) { throw new Error(message);}
function runInPty() { const helper = path.join(__dirname, 'fixtures', 'tty_readstream_setencoding_child.cjs'); const script = `import os, select, signal, sys, time
exec_path, helper = sys.argv[1], sys.argv[2]pid, master = os.forkpty()
if pid == 0: os.execv(exec_path, [exec_path, helper])
buf = bytearray()sent = Falseexit_code = Nonedeadline = time.time() + 7.0
while time.time() < deadline: done, status = os.waitpid(pid, os.WNOHANG) if done == pid: exit_code = os.waitstatus_to_exitcode(status) break
r, _, _ = select.select([master], [], [], 0.1) if master not in r: continue
try: chunk = os.read(master, 4096) except OSError: break
if not chunk: break
buf.extend(chunk) if (not sent) and b'READY' in buf: os.write(master, b'A\\xe2\\x82\\xac\\n') sent = True
if exit_code is None: os.kill(pid, signal.SIGKILL) _, status = os.waitpid(pid, 0) exit_code = os.waitstatus_to_exitcode(status)
while True: r, _, _ = select.select([master], [], [], 0.05) if master not in r: break try: chunk = os.read(master, 4096) except OSError: break if not chunk: break buf.extend(chunk)
sys.stdout.buffer.write(bytes(buf))sys.exit(exit_code)`;
if (process.platform === 'win32') { console.log('skipping tty.ReadStream setEncoding test on win32'); process.exit(0); }
return spawnSync('python3', ['-c', script, process.execPath, helper], { encoding: 'utf8', timeout: 9000, });}
const result = runInPty();
if (result.error && result.error.code === 'ENOENT') { console.log('skipping tty.ReadStream setEncoding test because `python3` is unavailable'); process.exit(0);}
if (result.error) throw result.error;
const output = `${result.stdout || ''}${result.stderr || ''}`;
if (result.status !== 0) { fail(`child exited ${result.status}\n${output}`);}
if (!output.includes('READY')) { fail(`expected READY banner\n${output}`);}
if (!output.includes('DATA "A€\\n"')) { fail(`expected decoded tty.ReadStream string payload\n${output}`);}
console.log('tty.ReadStream setEncoding decodes tty data');