Skip to content

Commit

Permalink
update bun to 1.1.6 (#12)
Browse files Browse the repository at this point in the history
What works on bun `1.1.6` and node `20`:

|                                | macOS  | Linux  |
| ------------------------------ | ------ | ------ |
| 1. node + create/read stream   | ok     | ok     |
| 2. bun + create/read stream    | broken | broken |
| 3. bun + Bun.file/Bun.write | broken | sometimes? (works in ~75% of
runs) |
| 4. bun + onData + write stream | broken | ok |

Code below:

<details><summary>1. node + create/read stream</summary>

```js
const fs = require("fs");
const { Pty } = require("../ruspty");

const pty = new Pty({
  command: "/bin/sh",
  envs: {
    TERM: "xterm-256color",
  },
  onExit: (err, exitCode) => {
    console.log({ err, exitCode });

    pty.close();
  },
});

const fd = pty.fd();

const readStream = fs.createReadStream("", { fd });
const writeStream = fs.createWriteStream("", { fd });

readStream.on("data", (chunk) => {
  console.log(Buffer.from(chunk).toString());
});

writeStream.write("ls --color=auto\n");
writeStream.write("exit\n");
```

</details>

<details><summary>2. bun + create/read stream</summary>

```js
const fs = require("fs");
const { Pty } = require("../ruspty");

const pty = new Pty({
  command: "/bin/sh",
  envs: {
    TERM: "xterm-256color",
  },
  onExit: (err, exitCode) => {
    console.log({ err, exitCode });

    pty.close();
  },
});

const fd = pty.fd();

const readStream = fs.createReadStream("", { fd });
const writeStream = fs.createWriteStream("", { fd });

readStream.on("data", (chunk) => {
  console.log(Buffer.from(chunk).toString());
});

writeStream.write("ls --color=auto\n");
writeStream.write("exit\n");
```

</details>

<details><summary>3. bun + Bun.file/Bun.write</summary>

```js
const { Pty } = require("../ruspty");

const pty = new Pty({
  command: "/bin/sh",
  envs: {
    TERM: "xterm-256color",
  },
  onExit: (err, exitCode) => {
    console.log({ err, exitCode });

    pty.close();
  },
});

const file = Bun.file(pty.fd());

async function read() {
  const stream = file.stream();

  for await (const chunk of stream) {
    console.log(Buffer.from(chunk).toString());
  }
}

read();

Bun.write(pty.fd(), "ls --color=auto\n");
Bun.write(pty.fd(), "exit\n");
```

</details>

<details><summary>4. bun + onData + write stream</summary>

```js
const fs = require("fs");
const { Pty } = require("../ruspty");

const pty = new Pty({
  command: "/bin/sh",
  envs: {
    TERM: "xterm-256color",
  },
  onExit: (err, exitCode) => {
    console.log({ err, exitCode });

    pty.close();
  },
  onData: (err, data) => {
    if (err) {
      console.log({ err });
    }

    console.log(Buffer.from(data).toString());
  },
});

const fd = pty.fd();

const writeStream = fs.createWriteStream("", { fd });

writeStream.write("ls --color=auto\n");
writeStream.write("exit\n");

// we have to wait and hope for the best
setTimeout(() => {}, 3000);
```

</details>

---------

Co-authored-by: lhchavez <[email protected]>
  • Loading branch information
szymonkaliski and lhchavez authored May 6, 2024
1 parent c9e3bce commit 5a9c0ea
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 66 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.0.26
bun-version: 1.1.6
- name: Setup node
uses: actions/setup-node@v4
if: ${{ !matrix.settings.docker }}
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.0.26
bun-version: 1.1.6
- name: Setup node
uses: actions/setup-node@v4
with:
Expand Down Expand Up @@ -149,7 +149,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.0.26
bun-version: 1.1.6
- name: Setup node
uses: actions/setup-node@v4
with:
Expand All @@ -165,7 +165,7 @@ jobs:
run: ls -R .
shell: bash
- name: Test bindings
run: docker run --rm -v $(pwd):/build -w /build oven/bun:1.0.26 bun test
run: docker run --rm -v $(pwd):/build -w /build oven/bun:1.1.6 bun test

publish:
name: Publish
Expand All @@ -178,7 +178,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.0.26
bun-version: 1.1.6
- name: Setup node
uses: actions/setup-node@v4
with:
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 72 additions & 58 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ function macOSLinuxCatBufferCompare(
return a1 === a2;
}

const IS_LINUX = os.type() === 'Linux';
const IS_DARWIN = os.type() === 'Darwin';

const testSkipOnDarwin = IS_DARWIN ? test.skip : test;

// These two functions ensure that there are no extra open file descriptors after each test
// finishes. Only works on Linux.
if (os.type() !== 'Darwin') {
if (IS_LINUX) {
beforeEach(async () => {
for (const filename of await readdir(procSelfFd)) {
try {
Expand Down Expand Up @@ -58,7 +63,8 @@ if (os.type() !== 'Darwin') {
}

describe('PTY', () => {
test('spawns and exits', (done) => {
// TODO: Reenable once https://github.com/oven-sh/bun/issues/9907 is fixed
test.skip('spawns and exits', (done) => {
const message = 'hello from a pty';
let buffer = '';

Expand All @@ -85,7 +91,6 @@ describe('PTY', () => {
if (err.code && err.code.indexOf('EIO') !== -1) {
return;
}
console.log('err', { err });
throw err;
});
});
Expand All @@ -104,16 +109,14 @@ describe('PTY', () => {
});
});

test('can be written to', (done) => {
// TODO: Reenable on Linux once https://github.com/oven-sh/bun/issues/9907 is fixed
test.skip('can be written to', (done) => {
// The message should end in newline so that the EOT can signal that the input has ended and not
// just the line.
const message = 'hello cat\n';
let buffer: Buffer | undefined;

const result = Buffer.from([
104, 101, 108, 108, 111, 32, 99, 97, 116, 13, 10, 104, 101, 108, 108, 111,
32, 99, 97, 116, 13, 10,
]);
const result = Buffer.from('hello cat\r\nhello cat\r\n');

const pty = new Pty({
command: '/bin/cat',
Expand Down Expand Up @@ -151,7 +154,8 @@ describe('PTY', () => {
});
});

test('can be resized', (done) => {
// TODO: Reenable on Linux once https://github.com/oven-sh/bun/issues/9907 is fixed
test.skip('can be resized', (done) => {
const pty = new Pty({
command: '/bin/sh',
size: { rows: 24, cols: 80 },
Expand Down Expand Up @@ -198,7 +202,7 @@ describe('PTY', () => {
});
});

test('respects working directory', (done) => {
testSkipOnDarwin('respects working directory', (done) => {
const cwd = process.cwd();
let buffer = '';

Expand All @@ -213,22 +217,30 @@ describe('PTY', () => {

done();
},
onData: (err, data) => {
if (IS_LINUX) {
expect(err).toBeNull();
buffer += data.toString();
}
},
});

const readStream = fs.createReadStream('', { fd: pty.fd() });
if (IS_DARWIN) {
const readStream = fs.createReadStream('', { fd: pty.fd() });

readStream.on('data', (chunk) => {
buffer += chunk.toString();
});
readStream.on('error', (err: any) => {
if (err.code && err.code.indexOf('EIO') !== -1) {
return;
}
throw err;
});
readStream.on('data', (chunk) => {
buffer += chunk.toString();
});
readStream.on('error', (err: any) => {
if (err.code && err.code.indexOf('EIO') !== -1) {
return;
}
throw err;
});
}
});

test('respects env', (done) => {
testSkipOnDarwin('respects env', (done) => {
const message = 'hello from env';
let buffer: Buffer | undefined;

Expand All @@ -247,30 +259,35 @@ describe('PTY', () => {

done();
},
onData: (err, data) => {
if (IS_LINUX) {
expect(err).toBeNull();
buffer = data;
}
},
});

const readStream = fs.createReadStream('', { fd: pty.fd() });
if (IS_DARWIN) {
const readStream = fs.createReadStream('', { fd: pty.fd() });

readStream.on('data', (chunk) => {
assert(Buffer.isBuffer(chunk));
buffer = chunk;
});
readStream.on('error', (err: any) => {
if (err.code && err.code.indexOf('EIO') !== -1) {
return;
}
throw err;
});
readStream.on('data', (chunk) => {
assert(Buffer.isBuffer(chunk));
buffer = chunk;
});
readStream.on('error', (err: any) => {
if (err.code && err.code.indexOf('EIO') !== -1) {
return;
}
throw err;
});
}
});

test('works with Bun.read & Bun.write', (done) => {
const message = 'hello bun\n';
let buffer: Uint8Array | undefined;

const result = new Uint8Array([
104, 101, 108, 108, 111, 32, 98, 117, 110, 13, 10, 104, 101, 108, 108,
111, 32, 98, 117, 110, 13, 10,
]);
const result = Buffer.from('hello bun\r\nhello bun\r\n');

const pty = new Pty({
command: '/bin/cat',
Expand Down Expand Up @@ -302,29 +319,26 @@ describe('PTY', () => {
});

// This test is not supported on Darwin at all.
(os.type() !== 'Darwin' ? test : test.skip)(
'works with data callback',
(done) => {
const message = 'hello bun\n';
let buffer = '';

const pty = new Pty({
command: '/bin/cat',
onExit: () => {
expect(buffer).toBe('hello bun\r\nhello bun\r\n');
pty.close();

done();
},
onData: (err, chunk) => {
expect(err).toBeNull();
buffer += chunk.toString();
},
});
testSkipOnDarwin('works with data callback', (done) => {
const message = 'hello bun\n';
let buffer = '';

const pty = new Pty({
command: '/bin/cat',
onExit: () => {
expect(buffer).toBe('hello bun\r\nhello bun\r\n');
pty.close();

done();
},
onData: (err, chunk) => {
expect(err).toBeNull();
buffer += chunk.toString();
},
});

Bun.write(pty.fd(), message + EOT + EOT);
},
);
Bun.write(pty.fd(), message + EOT + EOT);
});

test("doesn't break when executing non-existing binary", (done) => {
try {
Expand Down

0 comments on commit 5a9c0ea

Please sign in to comment.