@camjn/getargv 0.0.4 → 0.0.7

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.txt ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2022, 2023
4
+ Camden Narzt <getargv@narzt.cam>. All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -5,11 +5,24 @@
5
5
 
6
6
  This module allows you to query the arguments of other processes on macOS.
7
7
 
8
- ## Installation
8
+ ## Module Installation
9
9
 
10
- Install the module and add to the application's package.json by executing:
10
+ - Install `libgetargv` to your system (see below).
11
+ - Install the module and add to the application's package.json by executing:
11
12
 
12
- $ npm i @camjn/getargv
13
+ $ npm install --save-optional @camjn/getargv
14
+
15
+ Saving this module as optional is important because it can only build on macOS, so npm needs to be able to exclude it on other OSs.
16
+
17
+ ## Library Installation
18
+
19
+ - To get access to `libgetargv`, sign up for an appropriate [sponsorship tier](https://github.com/sponsors/CamJN).
20
+ - Clone the `libgetargv` repo: `git clone https://github.com/getargv/getargv.git`.
21
+ - Running `make install_dylib`, installs the library to the `/usr/local/` prefix by default; you can change the install location with the `PREFIX` `make` variable: `make PREFIX=/opt install_dylib`.
22
+
23
+ ## Building `libgetargv`
24
+
25
+ I've built `libgetargv` on macOS 10.7-13, using only the <abbr title="Command Line Tools">CLT</abbr> package, not the full Xcode install. If you need to override variables, do so inside the `make` command, eg: `make EXTRA_CPPFLAGS=-DMACRO EXTRA_CFLAGS=-std=c17 dylib`.
13
26
 
14
27
  ## Usage
15
28
 
package/dist/binding.d.ts CHANGED
@@ -1,8 +1,3 @@
1
- interface GetArgv {
2
- readonly PID_MAX: number;
3
- readonly ARG_MAX: number;
4
- get_argv_of_pid(pid: number, nuls: boolean, skip: number): string;
5
- get_argv_and_argc_of_pid(pid: number): Array<string>;
6
- }
1
+ import { GetArgv } from './types';
7
2
  declare const addon: GetArgv;
8
3
  export = addon;
package/dist/binding.js CHANGED
@@ -1,7 +1,47 @@
1
1
  "use strict";
2
2
  const addon = require('../build/Release/getargv_native');
3
+ addon.as_string = function as_string(pid, encoding, nuls, skip, options) {
4
+ switch (arguments.length) {
5
+ case 0: throw new TypeError('The "pid" argument must be specified');
6
+ case 1: throw new TypeError('The "encoding" argument must be specified');
7
+ case 2:
8
+ case 3:
9
+ case 4:
10
+ case 5: break;
11
+ default: throw new TypeError('Too many arguments were specified');
12
+ }
13
+ if (typeof pid !== "number") {
14
+ throw new TypeError('The "pid" argument must be a number');
15
+ }
16
+ if (typeof encoding !== "string") {
17
+ throw new TypeError(`The "encoding" argument must be a string, is a: ${typeof encoding}`);
18
+ }
19
+ const decoder = new TextDecoder(encoding, options);
20
+ const array = addon.get_argv_of_pid(pid, nuls !== null && nuls !== void 0 ? nuls : false, skip !== null && skip !== void 0 ? skip : 0);
21
+ return decoder.decode(array);
22
+ };
23
+ addon.as_array = function as_array(pid, encoding, options) {
24
+ switch (arguments.length) {
25
+ case 0: throw new TypeError('The "pid" argument must be specified');
26
+ case 1: throw new TypeError('The "encoding" argument must be specified');
27
+ case 2:
28
+ case 3: break;
29
+ default: throw new TypeError('Too many arguments were specified');
30
+ }
31
+ if (typeof pid !== "number") {
32
+ throw new TypeError('The "pid" argument must be a number');
33
+ }
34
+ if (typeof encoding !== "string") {
35
+ throw new TypeError(`The "encoding" argument must be a string, is a: ${typeof encoding}`);
36
+ }
37
+ const decoder = new TextDecoder(encoding, options);
38
+ const array = addon.get_argv_and_argc_of_pid(pid);
39
+ return array.map(b => decoder.decode(b));
40
+ };
3
41
  exports.PID_MAX = addon.PID_MAX;
4
42
  exports.ARG_MAX = addon.ARG_MAX;
5
43
  exports.get_argv_of_pid = addon.get_argv_of_pid;
44
+ exports.as_string = addon.as_string;
6
45
  exports.get_argv_and_argc_of_pid = addon.get_argv_and_argc_of_pid;
46
+ exports.as_array = addon.as_array;
7
47
  module.exports = addon;
@@ -0,0 +1,100 @@
1
+ /// <reference types="node" />
2
+ type utf_8 = "unicode-1-1-utf-8" | "utf-8" | "utf8";
3
+ type ibm866 = "866" | "cp866" | "csibm866" | "ibm866";
4
+ type iso_8859_2 = "csisolatin2" | "iso-8859-2" | "iso-ir-101" | "iso8859-2" | "iso88592" | "iso_8859-2" | "iso_8859-2:1987" | "l2" | "latin2";
5
+ type iso_8859_3 = "csisolatin3" | "iso-8859-3" | "iso-ir-109" | "iso8859-3" | "iso88593" | "iso_8859-3" | "iso_8859-3:1988" | "l3" | "latin3";
6
+ type iso_8859_4 = "csisolatin4" | "iso-8859-4" | "iso-ir-110" | "iso8859-4" | "iso88594" | "iso_8859-4" | "iso_8859-4:1988" | "l4" | "latin4";
7
+ type iso_8859_5 = "csisolatincyrillic" | "cyrillic" | "iso-8859-5" | "iso-ir-144" | "iso88595" | "iso_8859-5" | "iso_8859-5:1988";
8
+ type iso_8859_6 = "arabic" | "asmo-708" | "csiso88596e" | "csiso88596i" | "csisolatinarabic" | "ecma-114" | "iso-8859-6" | "iso-8859-6-e" | "iso-8859-6-i" | "iso-ir-127" | "iso8859-6" | "iso88596" | "iso_8859-6" | "iso_8859-6:1987";
9
+ type iso_8859_7 = "csisolatingreek" | "ecma-118" | "elot_928" | "greek" | "greek8" | "iso-8859-7" | "iso-ir-126" | "iso8859-7" | "iso88597" | "iso_8859-7" | "iso_8859-7:1987" | "sun_eu_greek";
10
+ type iso_8859_8 = "csiso88598e" | "csisolatinhebrew" | "hebrew" | "iso-8859-8" | "iso-8859-8-e" | "iso-ir-138" | "iso8859-8" | "iso88598" | "iso_8859-8" | "iso_8859-8:1988" | "visual";
11
+ type iso_8859_8i = "csiso88598i" | "iso-8859-8-i" | "logical";
12
+ type iso_8859_10 = "csisolatin6" | "iso-8859-10" | "iso-ir-157" | "iso8859-10" | "iso885910" | "l6" | "latin6";
13
+ type iso_8859_13 = "iso-8859-13" | "iso8859-13" | "iso885913";
14
+ type iso_8859_14 = "iso-8859-14" | "iso8859-14" | "iso885914";
15
+ type iso_8859_15 = "csisolatin9" | "iso-8859-15" | "iso8859-15" | "iso885915" | "l9" | "latin9";
16
+ type iso_8859_16 = "iso-8859-16";
17
+ type koi8_r = "cskoi8r" | "koi" | "koi8" | "koi8-r" | "koi8_r";
18
+ type koi8_u = "koi8-u";
19
+ type macintosh = "csmacintosh" | "mac" | "macintosh" | "x-mac-roman";
20
+ type windows_874 = "dos-874" | "iso-8859-11" | "iso8859-11" | "iso885911" | "tis-620" | "windows-874";
21
+ type windows_1250 = "cp1250" | "windows-1250" | "x-cp1250";
22
+ type windows_1251 = "cp1251" | "windows-1251" | "x-cp1251";
23
+ type windows_1252 = "ansi_x3.4-1968" | "ascii" | "cp1252" | "cp819" | "csisolatin1" | "ibm819" | "iso-8859-1" | "iso-ir-100" | "iso8859-1" | "iso88591" | "iso_8859-1" | "iso_8859-1:1987" | "l1" | "latin1" | "us-ascii" | "windows-1252" | "x-cp1252";
24
+ type windows_1253 = "cp1253" | "windows-1253" | "x-cp1253";
25
+ type windows_1254 = "cp1254" | "csisolatin5" | "iso-8859-9" | "iso-ir-148" | "iso8859-9" | "iso88599" | "iso_8859-9" | "iso_8859-9:1989" | "l5" | "latin5" | "windows-1254" | "x-cp1254";
26
+ type windows_1255 = "cp1255" | "windows-1255" | "x-cp1255";
27
+ type windows_1256 = "cp1256" | "windows-1256" | "x-cp1256";
28
+ type windows_1257 = "cp1257" | "windows-1257" | "x-cp1257";
29
+ type windows_1258 = "cp1258" | "windows-1258" | "x-cp1258";
30
+ type x_mac_cyrillic = "x-mac-cyrillic" | "x-mac-ukrainian";
31
+ type gbk = "chinese" | "csgb2312" | "csiso58gb231280" | "gb2312" | "gb_2312" | "gb_2312-80" | "gbk" | "iso-ir-58" | "x-gbk";
32
+ type gb18030 = "gb18030";
33
+ type hz_gb_2312 = "hz-gb-2312";
34
+ type big5 = "big5" | "big5-hkscs" | "cn-big5" | "csbig5" | "x-x-big5";
35
+ type euc_jp = "cseucpkdfmtjapanese" | "euc-jp" | "x-euc-jp";
36
+ type iso_2022_jp = "csiso2022jp" | "iso-2022-jp";
37
+ type shift_jis = "csshiftjis" | "ms_kanji" | "shift-jis" | "shift_jis" | "sjis" | "windows-31j" | "x-sjis";
38
+ type euc_kr = "cseuckr" | "csksc56011987" | "euc-kr" | "iso-ir-149" | "korean" | "ks_c_5601-1987" | "ks_c_5601-1989" | "ksc5601" | "ksc_5601" | "windows-949";
39
+ type iso_2022_kr = "csiso2022kr" | "iso-2022-kr";
40
+ type utf_16be = "utf-16be";
41
+ type utf_16le = "utf-16" | "utf-16le";
42
+ type x_user_defined = "x-user-defined";
43
+ type replacement = "iso-2022-cn" | "iso-2022-cn-ext";
44
+ export type TextEncoding = utf_8 | ibm866 | iso_8859_2 | iso_8859_3 | iso_8859_4 | iso_8859_5 | iso_8859_6 | iso_8859_7 | iso_8859_8 | iso_8859_8i | iso_8859_10 | iso_8859_13 | iso_8859_14 | iso_8859_15 | iso_8859_16 | koi8_r | koi8_u | macintosh | windows_874 | windows_1250 | windows_1251 | windows_1252 | windows_1253 | windows_1254 | windows_1255 | windows_1256 | windows_1257 | windows_1258 | x_mac_cyrillic | gbk | gb18030 | hz_gb_2312 | big5 | euc_jp | iso_2022_jp | shift_jis | euc_kr | iso_2022_kr | utf_16be | utf_16le | x_user_defined | replacement;
45
+ export type TextDecoderOptions = ConstructorParameters<typeof TextDecoder>[1];
46
+ export interface GetArgv {
47
+ /** @constant
48
+ * @description the maximum value a pid may have on the system
49
+ */
50
+ readonly PID_MAX: number;
51
+ /** @constant
52
+ * @description the maximum number of bytes that can be passed as args to a process (including env vars, etc)
53
+ */
54
+ readonly ARG_MAX: number;
55
+ /**
56
+ * Gets the arguments of a process as an ArrayBuffer
57
+ * @param pid - The pid of the process whose arguments you want, must be between 0..{@link PID_MAX} inclusive.
58
+ * @param nuls - Whether to replace nul bytes with spaces for human consumption.
59
+ * @param skip - The number of leading args to skip past.
60
+ * @returns An ArrayBuffer representing the arguments of pid, formatted as requested.
61
+ * @throws {@link TypeError} if called with: missing pid argument, invalid number as pid, invalid number as skip.
62
+ * @throws {@link RangeError} if called with pid outside valid range: 0..{@link PID_MAX} inclusive.
63
+ * @throws {@link SystemError} if the underlying sysctl or parsing fails.
64
+ */
65
+ get_argv_of_pid(pid: number, nuls: boolean, skip: number): ArrayBuffer;
66
+ /**
67
+ * Gets the arguments of a process as a string
68
+ * @param pid - The pid of the process whose arguments you want, must be between 0..{@link PID_MAX} inclusive.
69
+ * @param encoding - The encoding to attempt to apply to the arguments. Must be an enumerated value of {@link TextEncoding}.
70
+ * @param nuls - Whether to replace nul bytes with spaces for human consumption.
71
+ * @param skip - The number of leading args to skip past.
72
+ * @param options - The options to use when decoding the process' arguments' bytes into a string.
73
+ * @returns A string representing the arguments of pid, formatted as requested.
74
+ * @throws {@link TypeError} if called with: missing pid, missing encoding argument, invalid number as pid, invalid number as skip, or if `options.fatal` is true and it encounters malformed data while decoding.
75
+ * @throws {@link RangeError} if called with invalid encoding argument or pid outside valid range: 0..{@link PID_MAX} inclusive.
76
+ * @throws {@link SystemError} if the underlying sysctl or parsing fails.
77
+ */
78
+ as_string(pid: number, encoding: TextEncoding, nuls?: boolean, skip?: number, options?: TextDecoderOptions): string;
79
+ /**
80
+ * Gets the arguments of a process as an array of ArrayBuffers
81
+ * @param pid - The pid of the process whose arguments you want, must be between 0..{@link PID_MAX} inclusive.
82
+ * @returns An array of ArrayBuffers representing the arguments of pid.
83
+ * @throws {@link TypeError} if called with: missing pid, too many arguments, invalid number as pid.
84
+ * @throws {@link RangeError} if called with pid outside valid range: 0..{@link PID_MAX} inclusive.
85
+ * @throws {@link SystemError} if the underlying sysctl or parsing fails.
86
+ */
87
+ get_argv_and_argc_of_pid(pid: number): Array<ArrayBuffer>;
88
+ /**
89
+ * Gets the arguments of a process as an array of strings
90
+ * @param pid - The pid of the process whose arguments you want, must be between 0..{@link PID_MAX} inclusive.
91
+ * @param encoding - The encoding to attempt to apply to the arguments. Must be an enumerated value of {@link TextEncoding}.
92
+ * @param options - The options to use when decoding the process' arguments' bytes into strings.
93
+ * @returns An array of strings representing the arguments of pid.
94
+ * @throws {@link TypeError} if called with: missing pid, missing encoding argument, too many arguments, invalid number as pid, or if `options.fatal` is true and it encounters malformed data while decoding.
95
+ * @throws {@link RangeError} if called with: invalid encoding argument, pid outside valid range: 0..{@link PID_MAX} inclusive.
96
+ * @throws {@link SystemError} if the underlying sysctl or parsing fails.
97
+ */
98
+ as_array(pid: number, encoding: TextEncoding, options?: TextDecoderOptions): Array<string>;
99
+ }
100
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -3,8 +3,10 @@
3
3
  "getargv": "lib/cli.js"
4
4
  },
5
5
  "devDependencies": {
6
- "@types/node": "^18.11.18",
7
- "typescript": "^4.9.4"
6
+ "@types/node": "^18.15.5",
7
+ "typedoc": "^0.23.28",
8
+ "typedoc-plugin-missing-exports": "^1.0.0",
9
+ "typescript": "^5.0.2"
8
10
  },
9
11
  "os": [
10
12
  "darwin"
@@ -16,17 +18,21 @@
16
18
  "name": "@camjn/getargv",
17
19
  "homepage": "https://getargv.narzt.cam/",
18
20
  "author": "Camden Narzt <getargv@narzt.cam>",
19
- "version": "0.0.4",
21
+ "version": "0.0.7",
20
22
  "description": "This library allows you to query the arguments of other processes on macOS.",
21
23
  "main": "dist/binding.js",
22
24
  "scripts": {
25
+ "prebuild": "tsc",
23
26
  "build": "node-gyp configure build",
24
- "pretest": "tsc",
27
+ "pretest": "npm run build",
25
28
  "test": "node --napi-modules ./test/test_binding.js",
26
- "precli": "tsc",
27
- "prepack": "tsc",
29
+ "prepack": "npm run build",
30
+ "precli": "npm run build",
28
31
  "cli": "node dist/cli.js",
29
- "console": "node --napi-modules -r ./build/Release/getargv_native -e 'const GetArgv = require.cache[Object.keys(require.cache)[0]].exports;' -i"
32
+ "preconsole": "npm run build",
33
+ "console": "node --napi-modules -r ./dist/binding.js -e 'const GetArgv = require.cache[Object.keys(require.cache)[0]].exports;' -i",
34
+ "docs": "typedoc --tsconfig $PWD/tsconfig.json lib/types.ts",
35
+ "start": "npm run console"
30
36
  },
31
37
  "files": [
32
38
  "/README.md",
package/src/getargv.c CHANGED
@@ -19,7 +19,7 @@ napi_value get_undefined(napi_env env) {
19
19
  }
20
20
 
21
21
  napi_value create_system_error(napi_env env, errno_t err) {
22
- //we skip checking status in here because we already have a failure
22
+ // we skip checking status in here because we already have a failure
23
23
  napi_value code;
24
24
  napi_create_string_utf8(env, "ERR_SYSTEM_ERROR", NAPI_AUTO_LENGTH, &code);
25
25
 
@@ -129,15 +129,16 @@ napi_value GetArgvOfPid(napi_env env, napi_callback_info info) {
129
129
 
130
130
  napi_throw(env, error);
131
131
  } else {
132
- if (napi_create_string_utf8(env, result.start_pointer,
132
+ char *innerbuff = NULL;
133
+ if (napi_create_arraybuffer(env,
133
134
  result.end_pointer - result.start_pointer + 1,
134
- &args) != napi_ok) {
135
- // args not guaranteed to be utf8 or even latin1/ascii, but ruby, swift,
136
- // and node all want strings to be properly encoded
135
+ (void **)&innerbuff, &args) != napi_ok) {
137
136
  napi_throw_error(env, "ERR_INTERNAL_ASSERTION",
138
137
  "Unable to create return value");
138
+ } else {
139
+ memcpy(innerbuff, result.start_pointer, result.end_pointer - result.start_pointer + 1);
139
140
  }
140
- free(result.buffer);
141
+ free_ArgvResult(&result);
141
142
  }
142
143
 
143
144
  return args;
@@ -158,24 +159,24 @@ napi_value GetArgvAndArgcOfPid(napi_env env, napi_callback_info info) {
158
159
  "Unable to create return value");
159
160
  } else {
160
161
  for (size_t i = 0; i < result.argc; i++) {
161
- napi_value string;
162
- // args not guaranteed to be utf8 or even latin1/ascii, but ruby, swift,
163
- // and node all want strings to be properly encoded...
164
- if (napi_ok != napi_create_string_utf8(env, result.argv[i],
165
- NAPI_AUTO_LENGTH, &string)) {
162
+ napi_value buffer;
163
+ char *innerbuff = NULL;
164
+ size_t len = strlen(result.argv[i]) + 1;
165
+ if (napi_create_arraybuffer(env, len, (void **)&innerbuff, &buffer) !=
166
+ napi_ok) {
166
167
  napi_throw_error(env, "ERR_INTERNAL_ASSERTION",
167
- "Unable to create string");
168
+ "Unable to create ArrayBuffer");
168
169
  break;
169
170
  }
170
- if (napi_ok != napi_set_element(env, args, i, string)) {
171
+ memcpy(innerbuff, result.argv[i], len);
172
+ if (napi_ok != napi_set_element(env, args, i, buffer)) {
171
173
  napi_throw_error(env, "ERR_INTERNAL_ASSERTION",
172
174
  "Unable to populate array");
173
175
  break;
174
176
  }
175
177
  }
176
178
  }
177
- free(result.argv);
178
- free(result.buffer);
179
+ free_ArgvArgcResult(&result);
179
180
  }
180
181
 
181
182
  return args;