node-ipconfig 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 node-ipconfig contributors
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 node-ipconfig contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,135 +1,146 @@
1
- # node-ipconfig
2
-
3
- Promise-based JavaScript access to Windows network configuration through the native `ipconfig.exe`. It has no runtime dependencies and never invokes a shell. Parsed configuration is returned as a JavaScript object by default (ready to use as JSON); you can also serialize it as JSON text, XML, YAML, CSV, or NDJSON.
4
-
5
- ## Requirements and compatibility
6
-
7
- - Node.js 18 or newer.
8
- - Command execution is supported on Windows only. The pure `parseIpconfig(text)` parser runs on Windows, macOS, and Linux.
9
- - Both ESM and CommonJS package entry points are provided. Public command methods return Promises.
10
-
11
- ```sh
12
- npm install node-ipconfig
13
- ```
14
-
15
- The command wrapper requires Windows. The parser and output formatters can run on any supported Node.js platform.
16
-
17
- ## Quick start
18
-
19
- ### ESM
20
-
21
- ```js
22
- import ipconfig from 'node-ipconfig';
23
-
24
- const config = await ipconfig.getConfig({ all: true });
25
- console.log(config.hostname);
26
- for (const adapter of config.adapters) {
27
- console.log(adapter.name, adapter.ipv4, adapter.ipv6);
28
- }
29
- ```
30
-
31
- Select a serialized format when an integration needs a document rather than a JavaScript object:
32
-
33
- ```js
34
- const xml = await ipconfig.getConfig({ all: true, format: 'xml' });
35
- const yaml = await ipconfig.getConfig({ all: true, format: 'yaml' });
36
- const csv = await ipconfig.getConfig({ all: true, format: 'csv' });
37
- ```
38
-
39
- ### CommonJS
40
-
41
- ```js
42
- const ipconfig = require('node-ipconfig');
43
-
44
- async function main() {
45
- const adapters = await ipconfig.getAdapters({ all: true });
46
- console.log(await ipconfig.getIPv4());
47
- }
48
- main().catch(console.error);
49
- ```
50
-
51
- ## API
52
-
53
- Configuration methods accept `QueryOptions`: `all` (boolean, defaults to false), `timeout` (positive milliseconds, defaults to 15000), `signal` (`AbortSignal`), `encoding` (Node.js encoding name, defaults to UTF-8), and `rawResult` (boolean). `getConfig` additionally accepts `format`. Unknown keys and invalid values throw `TypeError`.
54
-
55
- - `getConfig(options?)` runs `ipconfig` or `/all` and returns a `Config` object by default (`format: 'json'`). Supported formats are `json`, `xml`, `yaml`, `csv`, and `ndjson`. JSON returns the object; the other formats return a string. CSV has one row per adapter, with nested arrays and objects encoded as JSON cells. NDJSON emits one adapter record per line, including the hostname when available.
56
- - `getRawOutput(options?)` returns stdout as a string. With `{ rawResult: true }`, it returns `{ stdout, stderr, exitCode }`.
57
- - `getAdapters(options?)` returns `Adapter[]`.
58
- - `getAdapter(nameOrIndex, options?)` returns one adapter; string matching is case-insensitive and the numeric index is zero-based. Missing adapters reject with `IpconfigError` code `ENOADAPTER`.
59
- - `getIPv4()` / `getIPv6()` return flat arrays of address strings from detailed configuration.
60
- - `getDefaultGateway()` / `getDnsServers()` return arrays of `{ adapter, address }` records.
61
- - `getMacAddresses()` returns `{ adapter, address }` records for adapters with a physical address.
62
- - `getHostname()` returns the host name reported by Node.js `os.hostname()`.
63
- - `parseIpconfig(text)` synchronously parses captured text without running a process.
64
- - `createIpconfig(options?)` creates an isolated API instance; its `platform` and `exec` options support testing with a mocked process runner.
65
- - `execute(command, options?)` permits only the command names listed below. It returns stdout, or the execution result when `rawResult: true`.
66
-
67
- ### Output formats
68
-
69
- ```js
70
- const config = await ipconfig.getConfig(); // Config object (default JSON data)
71
- const jsonText = JSON.stringify(config, null, 2);
72
- const xml = await ipconfig.getConfig({ format: 'xml' });
73
- const yaml = await ipconfig.getConfig({ format: 'yaml' });
74
- const csv = await ipconfig.getConfig({ format: 'csv' });
75
- const lines = await ipconfig.getConfig({ format: 'ndjson' });
76
- ```
77
-
78
- The formatter preserves the parsed configuration fields. XML values are escaped. YAML is emitted as a YAML document. CSV uses a header row and one row per adapter; complex values are JSON-encoded in their cells. NDJSON emits one JSON adapter record per line. The original native text is available separately through `getRawOutput()`.
79
-
80
- ### Data schema
81
-
82
- `Config` is `{ hostname?: string, adapters: Adapter[] }`. Adapter fields include `name`, `type`, `addresses: IpAddress[]`, `ipv4: string[]`, `ipv6: string[]`, `dnsServers: string[]`, `defaultGateways: string[]`, and `connectionSpecificDnsSuffix` (string or `null`). When available, adapters also include `description`, `macAddress`, `subnetMask`, `ipv4PrefixLength`, `dhcpEnabled`, `autoconfigurationEnabled`, `dhcpServer`, `leaseObtained`, `leaseExpires`, and `mediaState`. Unknown labeled fields are preserved in `fields`. Older scalar fields such as `ipv4Address` and `ipv6Address` remain available for compatibility; when multiple addresses exist, those scalars contain the last parsed address. `IpAddress` contains `address`, `family` (4 or 6), and optional `scope` or `prefixLength`.
83
-
84
- ## Commands and side effects
85
-
86
- `execute` accepts `config`, `all`, `release`, `release6`, `renew`, `renew6`, `flushdns`, `displaydns`, `registerdns`, `showclassid`, `setclassid`, `showclassid6`, or `setclassid6`. Command options are limited to `timeout`, `signal`, `encoding`, `rawResult`, plus `adapter` where needed and `classId` for `setclassid` variants. Adapter names and class IDs are passed as distinct process arguments and reject control characters and slash-prefixed option injection.
87
-
88
- Convenience methods include `flushDns(options?)`, `displayDns(options?)`, `registerDns(options?)`, `release(options?)`, and `renew(options?)`. `release` and `renew` accept optional `{ adapter, ipv6 }`; IPv6 selects `/release6` or `/renew6`. `showClassId({ adapter })`, `setClassId({ adapter, classId? })`, and their `...6` counterparts wrap class ID operations.
89
-
90
- Configuration reads, `/displaydns`, and `/showclassid` are read-only. `/flushdns` clears the DNS resolver cache. `/registerdns`, DHCP release/renew, and class ID setting can change network state. Those commands are only run when the corresponding method or `execute` call is explicitly invoked; discovery never runs them. DHCP operations can briefly interrupt connectivity. Windows permissions may vary; run an appropriately authorized terminal when Windows denies an operation.
91
-
92
- | JavaScript method | Native command | Effect |
93
- | --- | --- | --- |
94
- | `getConfig()` / `getRawOutput()` | `ipconfig` | Read current configuration |
95
- | `{ all: true }` | `ipconfig /all` | Read detailed adapter configuration |
96
- | `flushDns()` | `ipconfig /flushdns` | Clear DNS resolver cache |
97
- | `displayDns()` / `getDnsCache()` | `ipconfig /displaydns` | Read cached DNS records |
98
- | `registerDns()` | `ipconfig /registerdns` | Refresh DHCP leases and register DNS names |
99
- | `release({ adapter?, ipv6? })` | `/release` or `/release6` | Release DHCP configuration; may interrupt connectivity |
100
- | `renew({ adapter?, ipv6? })` | `/renew` or `/renew6` | Renew DHCP configuration; may interrupt connectivity |
101
- | `showClassId({ adapter })` / `showClassId6(...)` | `/showclassid` or `/showclassid6` | Read DHCP class ID |
102
- | `setClassId({ adapter, classId? })` / `setClassId6(...)` | `/setclassid` or `/setclassid6` | Change DHCP class ID |
103
-
104
- You may also call `execute` with an allowlisted command name (`config`, `all`, `release`, `release6`, `renew`, `renew6`, `flushdns`, `displaydns`, `registerdns`, `showclassid`, `setclassid`, `showclassid6`, `setclassid6`). Commands that change system state are never run during discovery.
105
-
106
- ## Raw output, encoding, and localization
107
-
108
- `getRawOutput()` preserves the decoded command text, while `rawResult: true` also returns stderr and exit code. The default decoder is UTF-8; set `encoding` to a Node-supported encoding when the machine's OEM code page requires it. This avoids extra code-page dependencies, but callers must know the output encoding for their system. The parser recognizes common English, German, and Spanish labels, common adapter header forms, and preserves unknown labeled values. Windows localization and version changes mean parsing is best effort; use raw output when exact text matters.
109
-
110
- ## Errors and security
111
-
112
- Unsupported operating systems reject with `IpconfigError` code `ENOTSUP`. Process startup, permission, timeout, cancellation, and command errors reject with `IpconfigError`, including available command, stdout, stderr, exit code, and cause details. A command can emit useful output alongside a non-zero exit status; that text is returned, and `{ rawResult: true }` exposes the exit code. No API accepts arbitrary command strings or starts a shell. All executable arguments come from a fixed allowlist and validated values.
113
-
114
- ## Examples
115
-
116
- Runnable ESM example: [`examples/basic.js`](examples/basic.js).
117
-
118
- For full API details, configuration fields, output format behavior, command effects, options, and errors, see the [API guide](docs/API.md).
119
-
120
- ```js
121
- const gateways = await ipconfig.getDefaultGateway();
122
- const dns = await ipconfig.getDnsServers();
123
- const wifi = await ipconfig.getAdapter('Wi-Fi');
124
- const raw = await ipconfig.getRawOutput({ all: true });
125
-
126
- // Explicit operations: these can change machine state.
127
- await ipconfig.flushDns();
128
- await ipconfig.renew({ adapter: wifi.name });
129
- ```
130
-
131
- ## Development
132
-
133
- `npm test` runs the platform-independent mocked suite. `npm run lint` performs JavaScript syntax checks, and `npm run typecheck` checks the published declarations with TypeScript. CI runs the suite on Windows, Linux, and macOS for supported Node.js releases.
134
-
135
- MIT licensed; see [LICENSE](LICENSE).
1
+ # node-ipconfig
2
+
3
+ Promise-based JavaScript access to Windows network configuration through the native `ipconfig.exe`. It has no runtime dependencies and never invokes a shell. Parsed configuration is returned as a JavaScript object by default (ready to use as JSON); you can also serialize it as JSON text, XML, YAML, CSV, or NDJSON.
4
+
5
+ ## Requirements and compatibility
6
+
7
+ - Node.js 18 or newer.
8
+ - Command execution is supported on Windows only. The pure `parseIpconfig(text)` parser runs on Windows, macOS, and Linux.
9
+ - Both ESM and CommonJS package entry points are provided. Public command methods return Promises.
10
+
11
+ ## Installation
12
+
13
+ Install the package with your preferred package manager:
14
+
15
+ ```sh
16
+ # npm
17
+ npm install node-ipconfig
18
+
19
+ # pnpm
20
+ pnpm add node-ipconfig
21
+
22
+ # Yarn
23
+ yarn add node-ipconfig
24
+ ```
25
+
26
+ The command wrapper requires Windows. The parser and output formatters can run on any supported Node.js platform.
27
+
28
+ ## Quick start
29
+
30
+ ### ESM
31
+
32
+ ```js
33
+ import ipconfig from 'node-ipconfig';
34
+
35
+ const config = await ipconfig.getConfig({ all: true });
36
+ console.log(config.hostname);
37
+ for (const adapter of config.adapters) {
38
+ console.log(adapter.name, adapter.ipv4, adapter.ipv6);
39
+ }
40
+ ```
41
+
42
+ Select a serialized format when an integration needs a document rather than a JavaScript object:
43
+
44
+ ```js
45
+ const xml = await ipconfig.getConfig({ all: true, format: 'xml' });
46
+ const yaml = await ipconfig.getConfig({ all: true, format: 'yaml' });
47
+ const csv = await ipconfig.getConfig({ all: true, format: 'csv' });
48
+ ```
49
+
50
+ ### CommonJS
51
+
52
+ ```js
53
+ const ipconfig = require('node-ipconfig');
54
+
55
+ async function main() {
56
+ const adapters = await ipconfig.getAdapters({ all: true });
57
+ console.log(await ipconfig.getIPv4());
58
+ }
59
+ main().catch(console.error);
60
+ ```
61
+
62
+ ## API
63
+
64
+ Configuration methods accept `QueryOptions`: `all` (boolean, defaults to false), `timeout` (positive milliseconds, defaults to 15000), `signal` (`AbortSignal`), `encoding` (Node.js encoding name, defaults to UTF-8), and `rawResult` (boolean). `getConfig` additionally accepts `format`. Unknown keys and invalid values throw `TypeError`.
65
+
66
+ - `getConfig(options?)` runs `ipconfig` or `/all` and returns a `Config` object by default (`format: 'json'`). Supported formats are `json`, `xml`, `yaml`, `csv`, and `ndjson`. JSON returns the object; the other formats return a string. CSV has one row per adapter, with nested arrays and objects encoded as JSON cells. NDJSON emits one adapter record per line, including the hostname when available.
67
+ - `getRawOutput(options?)` returns stdout as a string. With `{ rawResult: true }`, it returns `{ stdout, stderr, exitCode }`.
68
+ - `getAdapters(options?)` returns `Adapter[]`.
69
+ - `getAdapter(nameOrIndex, options?)` returns one adapter; string matching is case-insensitive and the numeric index is zero-based. Missing adapters reject with `IpconfigError` code `ENOADAPTER`.
70
+ - `getIPv4()` / `getIPv6()` return flat arrays of address strings from detailed configuration.
71
+ - `getDefaultGateway()` / `getDnsServers()` return arrays of `{ adapter, address }` records.
72
+ - `getMacAddresses()` returns `{ adapter, address }` records for adapters with a physical address.
73
+ - `getHostname()` returns the host name reported by Node.js `os.hostname()`.
74
+ - `parseIpconfig(text)` synchronously parses captured text without running a process.
75
+ - `createIpconfig(options?)` creates an isolated API instance; its `platform` and `exec` options support testing with a mocked process runner.
76
+ - `execute(command, options?)` permits only the command names listed below. It returns stdout, or the execution result when `rawResult: true`.
77
+
78
+ ### Output formats
79
+
80
+ ```js
81
+ const config = await ipconfig.getConfig(); // Config object (default JSON data)
82
+ const jsonText = JSON.stringify(config, null, 2);
83
+ const xml = await ipconfig.getConfig({ format: 'xml' });
84
+ const yaml = await ipconfig.getConfig({ format: 'yaml' });
85
+ const csv = await ipconfig.getConfig({ format: 'csv' });
86
+ const lines = await ipconfig.getConfig({ format: 'ndjson' });
87
+ ```
88
+
89
+ The formatter preserves the parsed configuration fields. XML values are escaped. YAML is emitted as a YAML document. CSV uses a header row and one row per adapter; complex values are JSON-encoded in their cells. NDJSON emits one JSON adapter record per line. The original native text is available separately through `getRawOutput()`.
90
+
91
+ ### Data schema
92
+
93
+ `Config` is `{ hostname?: string, adapters: Adapter[] }`. Adapter fields include `name`, `type`, `addresses: IpAddress[]`, `ipv4: string[]`, `ipv6: string[]`, `dnsServers: string[]`, `defaultGateways: string[]`, and `connectionSpecificDnsSuffix` (string or `null`). When available, adapters also include `description`, `macAddress`, `subnetMask`, `ipv4PrefixLength`, `dhcpEnabled`, `autoconfigurationEnabled`, `dhcpServer`, `leaseObtained`, `leaseExpires`, and `mediaState`. Unknown labeled fields are preserved in `fields`. Older scalar fields such as `ipv4Address` and `ipv6Address` remain available for compatibility; when multiple addresses exist, those scalars contain the last parsed address. `IpAddress` contains `address`, `family` (4 or 6), and optional `scope` or `prefixLength`.
94
+
95
+ ## Commands and side effects
96
+
97
+ `execute` accepts `config`, `all`, `release`, `release6`, `renew`, `renew6`, `flushdns`, `displaydns`, `registerdns`, `showclassid`, `setclassid`, `showclassid6`, or `setclassid6`. Command options are limited to `timeout`, `signal`, `encoding`, `rawResult`, plus `adapter` where needed and `classId` for `setclassid` variants. Adapter names and class IDs are passed as distinct process arguments and reject control characters and slash-prefixed option injection.
98
+
99
+ Convenience methods include `flushDns(options?)`, `displayDns(options?)`, `registerDns(options?)`, `release(options?)`, and `renew(options?)`. `release` and `renew` accept optional `{ adapter, ipv6 }`; IPv6 selects `/release6` or `/renew6`. `showClassId({ adapter })`, `setClassId({ adapter, classId? })`, and their `...6` counterparts wrap class ID operations.
100
+
101
+ Configuration reads, `/displaydns`, and `/showclassid` are read-only. `/flushdns` clears the DNS resolver cache. `/registerdns`, DHCP release/renew, and class ID setting can change network state. Those commands are only run when the corresponding method or `execute` call is explicitly invoked; discovery never runs them. DHCP operations can briefly interrupt connectivity. Windows permissions may vary; run an appropriately authorized terminal when Windows denies an operation.
102
+
103
+ | JavaScript method | Native command | Effect |
104
+ | --- | --- | --- |
105
+ | `getConfig()` / `getRawOutput()` | `ipconfig` | Read current configuration |
106
+ | `{ all: true }` | `ipconfig /all` | Read detailed adapter configuration |
107
+ | `flushDns()` | `ipconfig /flushdns` | Clear DNS resolver cache |
108
+ | `displayDns()` / `getDnsCache()` | `ipconfig /displaydns` | Read cached DNS records |
109
+ | `registerDns()` | `ipconfig /registerdns` | Refresh DHCP leases and register DNS names |
110
+ | `release({ adapter?, ipv6? })` | `/release` or `/release6` | Release DHCP configuration; may interrupt connectivity |
111
+ | `renew({ adapter?, ipv6? })` | `/renew` or `/renew6` | Renew DHCP configuration; may interrupt connectivity |
112
+ | `showClassId({ adapter })` / `showClassId6(...)` | `/showclassid` or `/showclassid6` | Read DHCP class ID |
113
+ | `setClassId({ adapter, classId? })` / `setClassId6(...)` | `/setclassid` or `/setclassid6` | Change DHCP class ID |
114
+
115
+ You may also call `execute` with an allowlisted command name (`config`, `all`, `release`, `release6`, `renew`, `renew6`, `flushdns`, `displaydns`, `registerdns`, `showclassid`, `setclassid`, `showclassid6`, `setclassid6`). Commands that change system state are never run during discovery.
116
+
117
+ ## Raw output, encoding, and localization
118
+
119
+ `getRawOutput()` preserves the decoded command text, while `rawResult: true` also returns stderr and exit code. The default decoder is UTF-8; set `encoding` to a Node-supported encoding when the machine's OEM code page requires it. This avoids extra code-page dependencies, but callers must know the output encoding for their system. The parser recognizes common English, German, and Spanish labels, common adapter header forms, and preserves unknown labeled values. Windows localization and version changes mean parsing is best effort; use raw output when exact text matters.
120
+
121
+ ## Errors and security
122
+
123
+ Unsupported operating systems reject with `IpconfigError` code `ENOTSUP`. Process startup, permission, timeout, cancellation, and command errors reject with `IpconfigError`, including available command, stdout, stderr, exit code, and cause details. A command can emit useful output alongside a non-zero exit status; that text is returned, and `{ rawResult: true }` exposes the exit code. No API accepts arbitrary command strings or starts a shell. All executable arguments come from a fixed allowlist and validated values.
124
+
125
+ ## Examples
126
+
127
+ Runnable ESM example: [`examples/basic.js`](examples/basic.js).
128
+
129
+ For full API details, configuration fields, output format behavior, command effects, options, and errors, see the [API guide](docs/API.md).
130
+
131
+ ```js
132
+ const gateways = await ipconfig.getDefaultGateway();
133
+ const dns = await ipconfig.getDnsServers();
134
+ const wifi = await ipconfig.getAdapter('Wi-Fi');
135
+ const raw = await ipconfig.getRawOutput({ all: true });
136
+
137
+ // Explicit operations: these can change machine state.
138
+ await ipconfig.flushDns();
139
+ await ipconfig.renew({ adapter: wifi.name });
140
+ ```
141
+
142
+ ## Development
143
+
144
+ `npm test` runs the platform-independent mocked suite. `npm run lint` performs JavaScript syntax checks, and `npm run typecheck` checks the published declarations with TypeScript. CI runs the suite on Windows, Linux, and macOS for supported Node.js releases.
145
+
146
+ MIT licensed; see [LICENSE](LICENSE).
package/docs/API.md CHANGED
@@ -1,134 +1,134 @@
1
- # API guide
2
-
3
- `node-ipconfig` wraps Windows `ipconfig.exe`. Its command methods return Promises and require Windows; `parseIpconfig()` and the format examples can be used on any supported Node.js platform. The package has no runtime dependencies.
4
-
5
- ## Install
6
-
7
- ```sh
8
- npm install node-ipconfig
9
- ```
10
-
11
- Requires Node.js 18 or newer.
12
-
13
- ## Get started
14
-
15
- ### ECMAScript modules
16
-
17
- ```js
18
- import ipconfig from 'node-ipconfig';
19
-
20
- const config = await ipconfig.getConfig({ all: true });
21
- console.log(config.hostname);
22
- for (const adapter of config.adapters) {
23
- console.log(adapter.name, adapter.ipv4, adapter.ipv6);
24
- }
25
- ```
26
-
27
- ### CommonJS
28
-
29
- ```js
30
- const ipconfig = require('node-ipconfig');
31
-
32
- async function main() {
33
- const adapters = await ipconfig.getAdapters({ all: true });
34
- console.log(adapters);
35
- }
36
- main().catch(console.error);
37
- ```
38
-
39
- ## Configuration and formats
40
-
41
- `getConfig(options?)` executes `ipconfig` and parses its output. Set `all: true` to execute `ipconfig /all`. By default it returns a JavaScript `Config` object (JSON-compatible data):
42
-
43
- ```js
44
- const config = await ipconfig.getConfig();
45
- const detailed = await ipconfig.getConfig({ all: true });
46
- ```
47
-
48
- Set `format` to serialize the parsed configuration. JSON is the default and remains an object; the other formats return strings.
49
-
50
- ```js
51
- const jsonData = await ipconfig.getConfig({ format: 'json' });
52
- const xml = await ipconfig.getConfig({ all: true, format: 'xml' });
53
- const yaml = await ipconfig.getConfig({ all: true, format: 'yaml' });
54
- const csv = await ipconfig.getConfig({ all: true, format: 'csv' });
55
- const ndjson = await ipconfig.getConfig({ all: true, format: 'ndjson' });
56
- ```
57
-
58
- CSV has one row per adapter; nested values are JSON-encoded in cells. NDJSON has one adapter record per line. XML values are escaped. YAML is emitted as a YAML document. Supported format names are `json`, `xml`, `yaml`, `csv`, and `ndjson`; unsupported names throw `TypeError`.
59
-
60
- `getRawOutput(options?)` returns the original decoded text. Set `rawResult: true` to receive `{ stdout, stderr, exitCode }` instead.
61
-
62
- ## Discovery methods
63
-
64
- | Method | Result |
65
- | --- | --- |
66
- | `getConfig(options?)` | Parsed configuration, or serialized output when a non-JSON format is selected |
67
- | `getAdapters(options?)` | Adapter array |
68
- | `getAdapter(nameOrIndex, options?)` | One adapter by case-insensitive name or zero-based index |
69
- | `getIPv4()` / `getIPv6()` | Flat arrays of IP address strings |
70
- | `getDefaultGateway()` | `{ adapter, address }` records |
71
- | `getDnsServers()` | `{ adapter, address }` records |
72
- | `getMacAddresses()` | `{ adapter, address }` records |
73
- | `getHostname()` | Host name from Node.js `os.hostname()` |
74
- | `getRawOutput(options?)` | Native command output as text or execution result |
75
- | `parseIpconfig(text)` | Synchronous parser for captured output; no command execution |
76
-
77
- Detailed fields include adapter type and description, MAC address, IPv4 and IPv6 addresses, subnet mask and prefix length, gateways, DNS servers, DHCP and autoconfiguration state, DHCP server, lease timestamps, media state, and preserved unknown labeled fields. Parsing recognizes common English, German, and Spanish labels; it is best-effort across Windows versions and locales.
78
-
79
- ## Options
80
-
81
- Configuration queries accept:
82
-
83
- - `all`: boolean, defaults to `false`.
84
- - `format`: `json`, `xml`, `yaml`, `csv`, or `ndjson`; only supported by `getConfig` and defaults to `json`.
85
- - `timeout`: positive milliseconds, defaults to 15000.
86
- - `signal`: `AbortSignal` to cancel execution.
87
- - `encoding`: Node.js text encoding, defaults to `utf8`.
88
- - `rawResult`: return `{ stdout, stderr, exitCode }` for raw command methods.
89
-
90
- Unknown options and invalid values throw `TypeError`.
91
-
92
- ## Explicit commands
93
-
94
- Operations that change network state are only executed when called explicitly. `execute(command, options?)` accepts the allowlisted names below; it never accepts arbitrary command text.
95
-
96
- | Method | `execute` name / native switch | Notes |
97
- | --- | --- | --- |
98
- | `execute('config')` | `ipconfig` | Read current configuration |
99
- | `execute('all')` | `/all` | Detailed configuration |
100
- | `flushDns()` | `/flushdns` | Clears the DNS resolver cache |
101
- | `displayDns()` / `getDnsCache()` | `/displaydns` | Displays cached DNS records |
102
- | `registerDns()` | `/registerdns` | Initiates manual DNS registration |
103
- | `release(options?)` | `/release` or `/release6` | Releases DHCP configuration; may interrupt connectivity |
104
- | `renew(options?)` | `/renew` or `/renew6` | Renews DHCP configuration; may interrupt connectivity |
105
- | `showClassId({ adapter })` | `/showclassid` | Displays a DHCP class ID |
106
- | `setClassId({ adapter, classId? })` | `/setclassid` | Sets or clears a DHCP class ID |
107
- | `showClassId6({ adapter })` | `/showclassid6` | IPv6 class ID operation |
108
- | `setClassId6({ adapter, classId? })` | `/setclassid6` | Sets or clears an IPv6 class ID |
109
-
110
- `release()` and `renew()` accept `{ adapter, ipv6 }`; set `ipv6: true` to choose the IPv6 command. `adapter` and `classId` values are passed as separate process arguments, not through a shell. Read-only discovery never runs a state-changing command.
111
-
112
- Run release, renew, DNS registration, and class ID changes only when their effects are appropriate. DHCP release/renew can disrupt connectivity. Windows may require an appropriately authorized terminal for some operations.
113
-
114
- ## Errors and platform support
115
-
116
- Command methods reject with `IpconfigError`. Its `code` identifies unsupported platforms (`ENOTSUP`), missing executables (`ENOENT`), permissions (`EACCES`), timeouts (`ETIMEDOUT`), cancellation (`ABORT_ERR`), or command failures. Available command, stdout, stderr, exit code, and cause are attached to the error. Unsupported option names, command names, or argument values throw `TypeError`.
117
-
118
- ## Testing with a custom runner
119
-
120
- `createIpconfig()` makes the command runner injectable for tests:
121
-
122
- ```js
123
- import { createIpconfig } from 'node-ipconfig';
124
-
125
- const api = createIpconfig({
126
- platform: 'win32',
127
- exec(file, args, options, callback) {
128
- callback(null, 'Windows IP Configuration', '');
129
- }
130
- });
131
- const config = await api.getConfig();
132
- ```
133
-
134
- The parser can also be tested directly with fixture text, without mocking or requiring Windows.
1
+ # API guide
2
+
3
+ `node-ipconfig` wraps Windows `ipconfig.exe`. Its command methods return Promises and require Windows; `parseIpconfig()` and the format examples can be used on any supported Node.js platform. The package has no runtime dependencies.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install node-ipconfig
9
+ ```
10
+
11
+ Requires Node.js 18 or newer.
12
+
13
+ ## Get started
14
+
15
+ ### ECMAScript modules
16
+
17
+ ```js
18
+ import ipconfig from 'node-ipconfig';
19
+
20
+ const config = await ipconfig.getConfig({ all: true });
21
+ console.log(config.hostname);
22
+ for (const adapter of config.adapters) {
23
+ console.log(adapter.name, adapter.ipv4, adapter.ipv6);
24
+ }
25
+ ```
26
+
27
+ ### CommonJS
28
+
29
+ ```js
30
+ const ipconfig = require('node-ipconfig');
31
+
32
+ async function main() {
33
+ const adapters = await ipconfig.getAdapters({ all: true });
34
+ console.log(adapters);
35
+ }
36
+ main().catch(console.error);
37
+ ```
38
+
39
+ ## Configuration and formats
40
+
41
+ `getConfig(options?)` executes `ipconfig` and parses its output. Set `all: true` to execute `ipconfig /all`. By default it returns a JavaScript `Config` object (JSON-compatible data):
42
+
43
+ ```js
44
+ const config = await ipconfig.getConfig();
45
+ const detailed = await ipconfig.getConfig({ all: true });
46
+ ```
47
+
48
+ Set `format` to serialize the parsed configuration. JSON is the default and remains an object; the other formats return strings.
49
+
50
+ ```js
51
+ const jsonData = await ipconfig.getConfig({ format: 'json' });
52
+ const xml = await ipconfig.getConfig({ all: true, format: 'xml' });
53
+ const yaml = await ipconfig.getConfig({ all: true, format: 'yaml' });
54
+ const csv = await ipconfig.getConfig({ all: true, format: 'csv' });
55
+ const ndjson = await ipconfig.getConfig({ all: true, format: 'ndjson' });
56
+ ```
57
+
58
+ CSV has one row per adapter; nested values are JSON-encoded in cells. NDJSON has one adapter record per line. XML values are escaped. YAML is emitted as a YAML document. Supported format names are `json`, `xml`, `yaml`, `csv`, and `ndjson`; unsupported names throw `TypeError`.
59
+
60
+ `getRawOutput(options?)` returns the original decoded text. Set `rawResult: true` to receive `{ stdout, stderr, exitCode }` instead.
61
+
62
+ ## Discovery methods
63
+
64
+ | Method | Result |
65
+ | --- | --- |
66
+ | `getConfig(options?)` | Parsed configuration, or serialized output when a non-JSON format is selected |
67
+ | `getAdapters(options?)` | Adapter array |
68
+ | `getAdapter(nameOrIndex, options?)` | One adapter by case-insensitive name or zero-based index |
69
+ | `getIPv4()` / `getIPv6()` | Flat arrays of IP address strings |
70
+ | `getDefaultGateway()` | `{ adapter, address }` records |
71
+ | `getDnsServers()` | `{ adapter, address }` records |
72
+ | `getMacAddresses()` | `{ adapter, address }` records |
73
+ | `getHostname()` | Host name from Node.js `os.hostname()` |
74
+ | `getRawOutput(options?)` | Native command output as text or execution result |
75
+ | `parseIpconfig(text)` | Synchronous parser for captured output; no command execution |
76
+
77
+ Detailed fields include adapter type and description, MAC address, IPv4 and IPv6 addresses, subnet mask and prefix length, gateways, DNS servers, DHCP and autoconfiguration state, DHCP server, lease timestamps, media state, and preserved unknown labeled fields. Parsing recognizes common English, German, and Spanish labels; it is best-effort across Windows versions and locales.
78
+
79
+ ## Options
80
+
81
+ Configuration queries accept:
82
+
83
+ - `all`: boolean, defaults to `false`.
84
+ - `format`: `json`, `xml`, `yaml`, `csv`, or `ndjson`; only supported by `getConfig` and defaults to `json`.
85
+ - `timeout`: positive milliseconds, defaults to 15000.
86
+ - `signal`: `AbortSignal` to cancel execution.
87
+ - `encoding`: Node.js text encoding, defaults to `utf8`.
88
+ - `rawResult`: return `{ stdout, stderr, exitCode }` for raw command methods.
89
+
90
+ Unknown options and invalid values throw `TypeError`.
91
+
92
+ ## Explicit commands
93
+
94
+ Operations that change network state are only executed when called explicitly. `execute(command, options?)` accepts the allowlisted names below; it never accepts arbitrary command text.
95
+
96
+ | Method | `execute` name / native switch | Notes |
97
+ | --- | --- | --- |
98
+ | `execute('config')` | `ipconfig` | Read current configuration |
99
+ | `execute('all')` | `/all` | Detailed configuration |
100
+ | `flushDns()` | `/flushdns` | Clears the DNS resolver cache |
101
+ | `displayDns()` / `getDnsCache()` | `/displaydns` | Displays cached DNS records |
102
+ | `registerDns()` | `/registerdns` | Initiates manual DNS registration |
103
+ | `release(options?)` | `/release` or `/release6` | Releases DHCP configuration; may interrupt connectivity |
104
+ | `renew(options?)` | `/renew` or `/renew6` | Renews DHCP configuration; may interrupt connectivity |
105
+ | `showClassId({ adapter })` | `/showclassid` | Displays a DHCP class ID |
106
+ | `setClassId({ adapter, classId? })` | `/setclassid` | Sets or clears a DHCP class ID |
107
+ | `showClassId6({ adapter })` | `/showclassid6` | IPv6 class ID operation |
108
+ | `setClassId6({ adapter, classId? })` | `/setclassid6` | Sets or clears an IPv6 class ID |
109
+
110
+ `release()` and `renew()` accept `{ adapter, ipv6 }`; set `ipv6: true` to choose the IPv6 command. `adapter` and `classId` values are passed as separate process arguments, not through a shell. Read-only discovery never runs a state-changing command.
111
+
112
+ Run release, renew, DNS registration, and class ID changes only when their effects are appropriate. DHCP release/renew can disrupt connectivity. Windows may require an appropriately authorized terminal for some operations.
113
+
114
+ ## Errors and platform support
115
+
116
+ Command methods reject with `IpconfigError`. Its `code` identifies unsupported platforms (`ENOTSUP`), missing executables (`ENOENT`), permissions (`EACCES`), timeouts (`ETIMEDOUT`), cancellation (`ABORT_ERR`), or command failures. Available command, stdout, stderr, exit code, and cause are attached to the error. Unsupported option names, command names, or argument values throw `TypeError`.
117
+
118
+ ## Testing with a custom runner
119
+
120
+ `createIpconfig()` makes the command runner injectable for tests:
121
+
122
+ ```js
123
+ import { createIpconfig } from 'node-ipconfig';
124
+
125
+ const api = createIpconfig({
126
+ platform: 'win32',
127
+ exec(file, args, options, callback) {
128
+ callback(null, 'Windows IP Configuration', '');
129
+ }
130
+ });
131
+ const config = await api.getConfig();
132
+ ```
133
+
134
+ The parser can also be tested directly with fixture text, without mocking or requiring Windows.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-ipconfig",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A safe JavaScript API for Windows ipconfig output",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -34,5 +34,8 @@
34
34
  "url": "https://github.com/aymen94/node-ipconfig.git"
35
35
  },
36
36
  "bugs": { "url": "https://github.com/aymen94/node-ipconfig/issues" },
37
- "homepage": "https://github.com/aymen94/node-ipconfig#readme"
37
+ "homepage": "https://github.com/aymen94/node-ipconfig#readme",
38
+ "devDependencies": {
39
+ "typescript": "^6.0.3"
40
+ }
38
41
  }