sanack / node-jq

Node.js wrapper for jq
MIT License
276 stars 51 forks source link

Using bun results in "TypeError: process.stdin.setEncoding is not a function." #681

Closed jhohlfeld closed 4 months ago

jhohlfeld commented 4 months ago

Description

I am using bun runtime and came across an issue that might be relevant for node-jq.

When running this example

jq(".", { foo: "bar" }, { input: "json" }).then(console.log);

bun raises a TypeError: process.stdin.setEncoding is not a function..

The error comes from this line: https://github.com/sanack/node-jq/blob/d339ef9c1713eddc78d5a68b0f5891ba040c3d62/src/exec.js#L35

Following the implementation, I found the spawning childProcess here: https://github.com/sanack/node-jq/blob/d339ef9c1713eddc78d5a68b0f5891ba040c3d62/src/exec.js#L13

Now my understanding is that process.stdin in a child process (subprocess.stdin) is a Writeable stream. However, according to nodejs' documentation, the Writable interface only has setDefaultEncoding, not setEncoding.

Weirdly enough, the node runtime does define setEncoding on the Writable implementation in subprocess.stdin. From my perspective, the way bun is treating the implementation, it's more adhering to the documented behaviour.

Maybe I am missing something and you may find it unrelated to node-jq. However, I still wanted to mention and would appreciate you feedback on it.

Repo for reproduction: https://github.com/jhohlfeld/nodejq-issue-681

Error Message & Stack Trace

TypeError: process.stdin.setEncoding is not a function. (In 'process.stdin.setEncoding("utf-8")', 'process.stdin.setEncoding' is undefined)
      at /Users/jakob/nodejq-issue/node_modules/node-jq/lib/exec.js:44:13
      at new Promise (:1:21)
      at exec (/Users/jakob/nodejq-issue/node_modules/node-jq/lib/exec.js:15:12)
      at /Users/jakob/nodejq-issue/node_modules/node-jq/lib/jq.js:17:13
      at new Promise (:1:21)
      at run (/Users/jakob/nodejq-issue/node_modules/node-jq/lib/jq.js:15:12)
      at /Users/jakob/nodejq-issue/index.ts:3:1

Environment

Tell us which operating system you are using, as well as which versions of Node.js, npm/yarn, and node-jq.

Bun 1.1.5 Node 21.6.0 darwin 23.4.0 node-jq version: 4.3.1

jhohlfeld commented 4 months ago

Hey @mackermans could you take a look into this? It would be an easy fix and really could help me a lot using node-jq with bun.

May I offer to post a merge request?

davesnx commented 4 months ago

Hi, happy to review a PR adding bun and ensure works in both runtimes