@camjn/getargv 0.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/binding.gyp ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "targets": [{
3
+ "target_name": "getargv_native",
4
+ "sources": [ "src/getargv.c" ],
5
+ "defines": [ "_PID_MAX=<!@(man setaudit_addr | egrep -oe 'PID_MAX.*[0-9]+' | awk -F'[^0-9]' '{print $NF}')" ],
6
+ "include_dirs": [
7
+ "<!@(pkg-config getargv --cflags-only-I | sed s/-I//g)",
8
+ ],
9
+ "link_settings": {
10
+ "libraries": [
11
+ "<!@(pkg-config getargv --libs)",
12
+ ]
13
+ },
14
+ "xcode_settings": {
15
+ "MACOSX_DEPLOYMENT_TARGET": "10.7"
16
+ }
17
+ }]
18
+ }
package/lib/binding.ts ADDED
@@ -0,0 +1,11 @@
1
+ interface CBinding {
2
+
3
+ get_argv_of_pid(pid: number, nuls: boolean, skip: number): string
4
+
5
+ get_argv_and_argc_of_pid(pid: number): Array<string>
6
+
7
+ }
8
+
9
+ const addon: CBinding = require('../build/Release/getargv_native');
10
+
11
+ export = addon
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "devDependencies": {
3
+ "@types/node": "^18.11.18",
4
+ "typescript": "^4.9.4"
5
+ },
6
+ "bugs": {
7
+ "url": "https://github.com/getargv/getargv.js/issues"
8
+ },
9
+ "name": "@camjn/getargv",
10
+ "homepage": "https://getargv.narzt.cam/",
11
+ "author": "Camden Narzt <getargv@narzt.cam>",
12
+ "version": "0.0.1",
13
+ "description": "This library allows you to query the arguments of other processes on macOS.",
14
+ "main": "lib/binding.js",
15
+ "scripts": {
16
+ "build":"node-gyp configure build",
17
+ "pretest": "tsc",
18
+ "test": "node --napi-modules ./test/test_binding.js"
19
+ },
20
+ "gypfile": true,
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/getargv/getargv.js"
24
+ },
25
+ "keywords": [
26
+ "macOS",
27
+ "KERN_PROCARGS2",
28
+ "arguments",
29
+ "processes"
30
+ ],
31
+ "license": "BSD-3-Clause"
32
+ }
package/src/getargv.c ADDED
@@ -0,0 +1,119 @@
1
+ #include <errno.h>
2
+ #include <libgetargv.h>
3
+ #include <node_api.h>
4
+ #include <stdint.h>
5
+ #include <string.h>
6
+ #include <sys/sysctl.h>
7
+
8
+ // if cannot prevent non macos installs: use this error
9
+ // ERR_FEATURE_UNAVAILABLE_ON_PLATFORM
10
+ // process.platform != 'darwin'
11
+
12
+ napi_value GetArgvOfPid(napi_env env, napi_callback_info info) {
13
+ size_t argc = 3;
14
+ napi_value argv[argc];
15
+
16
+ if (napi_get_cb_info(env, info, &argc, argv, NULL, NULL) != napi_ok) {
17
+ napi_throw_error(env, "ERR_AMBIGUOUS_ARGUMENT",
18
+ "Failed to parse arguments");
19
+ }
20
+ if (argc < 1) {
21
+ napi_throw_type_error(env, "ERR_MISSING_ARGS",
22
+ "The \"pid\" argument must be specified");
23
+ } else if (argc > 3) {
24
+ napi_throw_type_error(env, "ERR_TOO_MANY_ARGS",
25
+ "Too many arguments were provided, max: 3");
26
+ }
27
+
28
+ pid_t pid = 0;
29
+ if (napi_get_value_int32(env, argv[0], &pid) != napi_ok) {
30
+ napi_throw_type_error(env, "ERR_INVALID_ARG_TYPE",
31
+ "Invalid number was passed as first argument");
32
+ }
33
+ if (pid < 0 || pid > _PID_MAX) {
34
+ // or ERR_OUT_OF_RANGE
35
+ napi_throw_range_error(env, "ERR_INVALID_ARG_VALUE",
36
+ "Invalid PID was passed as first argument");
37
+ }
38
+
39
+ bool nuls = false;
40
+ if (argc > 1) {
41
+ if (napi_get_value_bool(env, argv[1], &nuls) != napi_ok) {
42
+ napi_throw_type_error(env, "ERR_INVALID_ARG_TYPE",
43
+ "Invalid bool was passed as second argument");
44
+ }
45
+ }
46
+
47
+ int64_t skip = 0;
48
+ if (argc > 2) {
49
+ if (napi_get_value_int64(env, argv[2], &skip) != napi_ok) {
50
+ napi_throw_type_error(env, "ERR_INVALID_ARG_TYPE",
51
+ "Invalid number was passed as third argument");
52
+ }
53
+ if (skip < 0 || skip > ARG_MAX) {
54
+ // or ERR_OUT_OF_RANGE
55
+ napi_throw_range_error(env, "ERR_INVALID_ARG_VALUE",
56
+ "Invalid number was passed as third argument");
57
+ }
58
+ }
59
+
60
+ struct GetArgvOptions options = {.pid = pid, .nuls = nuls, .skip = skip};
61
+ struct ArgvResult result;
62
+
63
+ if (!get_argv_of_pid(&options, &result)) {
64
+ errno_t err = errno;
65
+
66
+ napi_value code;
67
+ if (napi_ok != napi_create_string_utf8(env, "ERR_SYSTEM_ERROR", NAPI_AUTO_LENGTH, &code)) {
68
+ napi_throw_error(env, "ERR_INTERNAL_ASSERTION", "Failed to create String object");
69
+ }
70
+
71
+ napi_value msg;
72
+ if (napi_ok != napi_create_string_utf8(env, strerror(err), NAPI_AUTO_LENGTH, &msg)) {
73
+ napi_throw_error(env, "ERR_INTERNAL_ASSERTION", "Failed to create String object");
74
+ }
75
+
76
+ napi_value error;
77
+ if (napi_ok != napi_create_error(env, code, msg, &error)) {
78
+ napi_throw_error(env, "ERR_INTERNAL_ASSERTION", "Failed to create Error object");
79
+ }
80
+
81
+ napi_value value;
82
+ if (napi_ok != napi_create_int32(env, err, &value)) {
83
+ napi_throw_error(env, "ERR_INTERNAL_ASSERTION", "Failed to create number");
84
+ }
85
+
86
+ if (napi_ok != napi_set_named_property(env, error, "errno", value)) {
87
+ napi_throw_error(env, "ERR_INTERNAL_ASSERTION", "Failed to set property on Error object");
88
+ }
89
+
90
+ napi_throw(env, error);
91
+ }
92
+
93
+ napi_value args;
94
+ if (napi_create_string_utf8(env, result.start_pointer, result.end_pointer - result.start_pointer + 1, &args) != napi_ok) {
95
+ napi_throw_error(env, NULL, "Unable to create return value");
96
+ }
97
+
98
+ return args;
99
+ }
100
+
101
+ napi_value Init(napi_env env, napi_value exports) {
102
+ // Module initialization code goes here
103
+ napi_status status;
104
+ napi_value fn;
105
+
106
+ status = napi_create_function(env, "get_argv_of_pid", NAPI_AUTO_LENGTH, GetArgvOfPid, NULL, &fn);
107
+ if (status != napi_ok) {
108
+ napi_throw_error(env, NULL, "Unable to wrap native function");
109
+ }
110
+
111
+ status = napi_set_named_property(env, exports, "get_argv_of_pid", fn);
112
+ if (status != napi_ok) {
113
+ napi_throw_error(env, NULL, "Unable to populate exports");
114
+ }
115
+
116
+ return exports;
117
+ }
118
+
119
+ NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
@@ -0,0 +1,24 @@
1
+ const Getargv = require("../dist/binding.js");
2
+ const assert = require("assert");
3
+ const process = require("process");
4
+
5
+ function expected_args() {
6
+ return [process.argv0, ...process.execArgv, ...process.argv.slice(1)].map(a => a.replace(process.cwd(), "."));
7
+ }
8
+
9
+ assert(Getargv, "The export is undefined");
10
+
11
+ function test_Get_Argv_Of_Pid() {
12
+ const result = Getargv.get_argv_of_pid(process.pid);
13
+ assert.strictEqual(result, expected_args().join("\0") + "\0", "Unexpected value returned");
14
+ }
15
+
16
+ function test_Get_Argv_And_Argc_Of_Pid() {
17
+ const result = Getargv.get_argv_and_argc_of_pid(process.pid);
18
+ assert.deepStrictEqual(result, expected_args(), "Unexpected value returned");
19
+ }
20
+
21
+ assert.doesNotThrow(test_Get_Argv_Of_Pid, undefined, "test_Get_Argv_Of_Pid threw an expection");
22
+ assert.doesNotThrow(test_Get_Argv_And_Argc_Of_Pid, undefined, "test_Get_Argv_And_Argc_Of_Pid threw an expection");
23
+
24
+ console.log("Tests passed & everything looks OK!");
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "lib": [ "esnext" ],
5
+ "module": "commonjs",
6
+ "outDir": "./dist",
7
+ "rootDir": "./lib",
8
+ "strict": true,
9
+ "target": "es6",
10
+ "types": [
11
+ "node"
12
+ ],
13
+ "typeRoots": [
14
+ "node_modules/@types"
15
+ ]
16
+ }
17
+ }