fix(ext/node): add hasColors() to process.stdout and process.stderr#31985
fix(ext/node): add hasColors() to process.stdout and process.stderr#31985Tango992 merged 10 commits intodenoland:mainfrom
Conversation
|
@Tango992 can you review this one? Ideally instead of adding a custom unit test we could enable |
…tion This adds the missing `hasColors()` method to `process.stdout` and `process.stderr` streams, matching Node.js behavior. The implementation includes proper validation that throws `ERR_INVALID_ARG_TYPE` for invalid input types and `ERR_OUT_OF_RANGE` for invalid count values. Additionally, this implements the full `getColorDepth(env)` function from Node.js in tty.js, which detects color support based on environment variables like FORCE_COLOR, COLORTERM, TERM, and various CI environments (GitHub Actions, GitLab CI, etc.). Note: The Node.js test file `test-tty-color-support.js` cannot be enabled yet because it tries to redefine `process.platform`, which is not configurable in Deno. A TODO comment has been added to track this. Fixes denoland#27278 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1ea9dce to
1f99550
Compare
|
Thanks for the suggestion! I looked into enabling // TODO: Enable once process.platform is configurable.
// The test tries to redefine process.platform which Deno doesn't support.
// "pseudo-tty/test-tty-color-support.js": {},The test at lines 109-118 does: Object.defineProperty(process, 'platform', { value });Since
Would you prefer I take one of these approaches, or is the unit test approach acceptable for now? |
|
Changing |
- Make process.platform configurable and writable so tests can override it via Object.defineProperty() - Enable test-tty-color-support.js test which was previously disabled pending this change - Remove custom unit tests in favor of the Node.js test Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Done! I've made Changes:
|
Replace raw RegExp literals with SafeRegExp from primordials to fix the prefer-primordials lint error. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Use SafeMapIterator for CI_ENVS_MAP iteration - Replace 'in' operator with ObjectPrototypeHasOwnProperty - Replace remaining RegExp literals with SafeRegExp Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Tango992
left a comment
There was a problem hiding this comment.
Thanks, great work! Only some minor reviews. Also please address the lint errors so we could run the tests on CI
ext/node/polyfills/tty.js
Outdated
| @@ -1,19 +1,246 @@ | |||
| // Copyright 2018-2026 the Deno authors. MIT license. | |||
| // Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com). MIT license. | |||
There was a problem hiding this comment.
While we're at it, let's copy the whole license as defined here:
https://github.com/nodejs/node/blob/70f6b58ac655234435a99d72b857dd7b316d34bf/lib/internal/tty.js#L5-L21
| if (+OSRelease[0] >= 10) { | ||
| const build = +OSRelease[2]; | ||
| if (build >= 14931) { | ||
| return COLORS_16m; | ||
| } | ||
| if (build >= 10586) { | ||
| return COLORS_256; | ||
| } | ||
| } |
There was a problem hiding this comment.
I feel like we should add this comment to give people some context:
https://github.com/nodejs/node/blob/70f6b58ac655234435a99d72b857dd7b316d34bf/lib/internal/tty.js#L160-L162
|
Good catch on the license header. Added the full MIT license text. |
|
Closing this PR as part of a cleanup - I opened too many PRs across projects and want to be more respectful of maintainer time. I also used AI assistance without proper disclosure. Apologies for the noise, and thank you for your patience. |
|
@ddmoney420 this PR looks okay, I think we could land it with no problem |
|
Thanks @bartlomieju! Reopening - I closed this prematurely as part of cleaning up some other noisy PRs. Happy to address any remaining lint errors if needed. |
- Remove unused import `op_bootstrap_color_depth` - Rename COLORS_16m to COLORS_16M (camelcase lint rule)
|
Seems like the windows test still fails on this line: deno/tests/unit_node/tty_test.ts Line 43 in 0b38d25 Would you please take a look @ddmoney420 ? I'll try to investigate it later when I have access to my windows machine |
|
@Tango992 I looked into this — the test on line 43 expects
On Windows CI with limited color depth, Should I change the test to use |
passing empty object as parameter will lead to different result across OS's
That’s fine, I’ve committed a change for the tty test. Passing an empty object will return a different result in windows because we got caught on this if statement: Given this code and run it with Node.js: import tty from “node:tty”;
import { release } from “node:os”;
process.env.NO_COLOR = “1”;
console.log(tty.WriteStream.prototype.hasColors(), tty.WriteStream.prototype.hasColors({}))Linux & macos outputs |
Summary
hasColors()method toprocess.stdoutandprocess.stderrFixes #27278
Changes
ext/node/polyfills/_process/streams.mjsto addhasColorspropertytests/unit_node/process_test.tsImplementation
The
hasColorsmethod mirrors the logic fromtty.WriteStream.prototype.hasColors():op_bootstrap_color_depth()to get the current color depthtrueifcount <= 2 ** depthTest plan
process.stdout.hasColorsprocess.stderr.hasColors🤖 Generated with Claude Code