Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4714,6 +4714,20 @@ replacing it keeps being called by [`server.listen()`][], and it will be
removed in a future version of Node.js. Use [`server.listen()`][] instead of
calling or overriding `_listen2`.

### DEP0209: `process.title`

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/62804
description: Documentation-only deprecation.
-->

Type: Documentation-only

The `process.title` property is deprecated. Use [`process.getTitle()`][] to
read the current title and [`process.setTitle()`][] to change it.

[DEP0142]: #dep0142-repl_builtinlibs
[DEP0156]: #dep0156-aborted-property-and-abort-aborted-event-in-http
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
Expand Down Expand Up @@ -4815,7 +4829,9 @@ calling or overriding `_listen2`.
[`process.exit()`]: process.md#processexitcode
[`process.exitCode`]: process.md#processexitcode_1
[`process.getActiveResourcesInfo()`]: process.md#processgetactiveresourcesinfo
[`process.getTitle()`]: process.md#processgettitle
[`process.mainModule`]: process.md#processmainmodule
[`process.setTitle()`]: process.md#processsettitletitle
[`punycode`]: punycode.md
[`readable.readableEnded`]: stream.md#readablereadableended
[`request.abort()`]: http.md#requestabort
Expand Down
47 changes: 47 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -4315,8 +4315,12 @@ argument to the function, to get a diff reading.

<!-- YAML
added: v0.1.104
deprecated: REPLACEME
-->

> Stability: 0 - Deprecated. Use [`process.getTitle()`][] to read the title
> and [`process.setTitle()`][] to change it.

* Type: {string}

The `process.title` property returns the current process title (i.e. returns
Expand All @@ -4336,6 +4340,46 @@ Assigning a value to `process.title` might not result in an accurate label
within process manager applications such as macOS Activity Monitor or Windows
Services Manager.

As an alternative to direct assignment, use [`process.setTitle()`][].

## `process.getTitle()`

<!-- YAML
added: REPLACEME
-->

* Returns: {string}

Returns the current process title. This is equivalent to reading
[`process.title`][].

## `process.setTitle(title)`

<!-- YAML
added: REPLACEME
-->

* `title` {string}

Sets the process title. This is equivalent to assigning a string to
[`process.title`][]. See [`process.title`][] for restrictions and platform
limitations.

If `title` is not a string, `setTitle()` throws a `TypeError`. This differs
from assigning to [`process.title`][], which coerces the value to a string.
Comment thread
JonathanLopes404 marked this conversation as resolved.

```mjs
import { setTitle } from 'node:process';

setTitle('my-service');
```

```cjs
const { setTitle } = require('node:process');

setTitle('my-service');
```

## `process.traceDeprecation`

<!-- YAML
Expand Down Expand Up @@ -4652,11 +4696,14 @@ cases:
[`process.execPath`]: #processexecpath
[`process.exit()`]: #processexitcode
[`process.exitCode`]: #processexitcode_1
[`process.getTitle()`]: #processgettitle
[`process.hrtime()`]: #processhrtimetime
[`process.hrtime.bigint()`]: #processhrtimebigint
[`process.kill()`]: #processkillpid-signal
[`process.permission.has()`]: #processpermissionhasscope-reference
[`process.setTitle()`]: #processsettitletitle
[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn
[`process.title`]: #processtitle
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
[`queueMicrotask()`]: globals.md#queuemicrotaskcallback
[`readable.read()`]: stream.md#readablereadsize
Expand Down
14 changes: 14 additions & 0 deletions lib/internal/bootstrap/switches/does_not_own_process_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const { unavailable } = require('internal/process/worker_thread_only');

process.abort = unavailable('process.abort()');
process.chdir = unavailable('process.chdir()');
process.getTitle = wrappedGetTitle;
process.setTitle = wrappedSetTitle;
process.umask = wrappedUmask;
process.cwd = rawMethods.cwd;

Expand All @@ -27,6 +29,9 @@ const {
ERR_WORKER_UNSUPPORTED_OPERATION,
},
} = require('internal/errors');
const {
validateString,
} = require('internal/validators');

function wrappedUmask(mask) {
// process.umask() is a read-only operation in workers.
Expand All @@ -36,3 +41,12 @@ function wrappedUmask(mask) {

return rawMethods.umask(mask);
}

function wrappedSetTitle(title) {
validateString(title, 'title');
process.title = title;
}

function wrappedGetTitle() {
return process.title;
}
11 changes: 11 additions & 0 deletions lib/internal/bootstrap/switches/does_own_process_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ process.abort = rawMethods.abort;
process.umask = wrappedUmask;
process.chdir = wrappedChdir;
process.cwd = wrappedCwd;
process.getTitle = wrappedGetTitle;
process.setTitle = wrappedSetTitle;

if (credentials.implementsPosixCredentials) {
const wrapped = wrapPosixCredentialSetters(credentials);
Expand Down Expand Up @@ -142,3 +144,12 @@ function wrappedCwd() {
cachedCwd = rawMethods.cwd();
return cachedCwd;
}

function wrappedSetTitle(title) {
validateString(title, 'title');
process.title = title;
}

function wrappedGetTitle() {
return process.title;
}
2 changes: 1 addition & 1 deletion test/doctool/test-doc-api-json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,5 @@ for await (const dirent of await fs.opendir(new URL('../../out/doc/api/', import
assert.partialDeepStrictEqual(allExpectedKeys, findAllKeys(json));
}

assert.strictEqual(numberOfDeprecatedSections, 45); // Increase this number every time a new API is deprecated.
assert.strictEqual(numberOfDeprecatedSections, 46); // Increase this number every time a new API is deprecated.
assert.strictEqual(numberOfRemovedAPIs, 46); // Increase this number every time a section is marked as removed.
29 changes: 29 additions & 0 deletions test/es-module/test-esm-process-set-title.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { isIBMi, isSunOS, isWindows, skip } from '../common/index.mjs';
import assert from 'node:assert';
import process, { getTitle, setTitle } from 'node:process';
import { isMainThread } from 'node:worker_threads';
import { describe, it } from 'node:test';

// FIXME add sunos support
if (isSunOS || isIBMi || isWindows) {
skip(`Unsupported platform [${process.platform}]`);
}

if (!isMainThread) {
skip('Setting the process title from Workers is not supported');
}

describe('ESM named import setTitle', () => {
it('sets process title through node:process', () => {
const title = String(process.pid);

assert.strictEqual(getTitle(), process.title);
setTitle(title);
assert.strictEqual(getTitle(), title);
assert.strictEqual(process.title, title);

assert.throws(() => setTitle(1), {
code: 'ERR_INVALID_ARG_TYPE',
});
});
});
15 changes: 14 additions & 1 deletion test/parallel/test-setproctitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,29 @@ if (!isMainThread) {
const assert = require('assert');
const { exec, execSync } = require('child_process');
const path = require('path');
const { getTitle, setTitle } = require('node:process');

// The title shouldn't be too long; libuv's uv_set_process_title() out of
// security considerations no longer overwrites envp, only argv, so the
// maximum title length is possibly quite short.
let title = String(process.pid);

assert.notStrictEqual(process.title, title);
process.title = title;
assert.strictEqual(getTitle(), process.title);
setTitle(title);
assert.strictEqual(getTitle(), title);
assert.strictEqual(process.title, title);

assert.throws(() => setTitle(1), {
code: 'ERR_INVALID_ARG_TYPE',
});

const legacyTitle = `${title}-legacy`;
process.title = legacyTitle;
assert.strictEqual(getTitle(), legacyTitle);
assert.strictEqual(process.title, legacyTitle);
title = legacyTitle;

try {
execSync('command -v ps');
} catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-worker-unsupported-things.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ if (!process.env.HAS_STARTED_WORKER) {
const after = before + ' in worker';
process.title = after;
assert.strictEqual(process.title, after);

process.setTitle(`${after} via function`);
assert.strictEqual(process.title, `${after} via function`);
}

{
Expand Down
Loading