syscall-napi 0.0.6 → 0.0.8

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.
@@ -1,19 +1,10 @@
1
- {
2
- "parserOptions": {
3
- "ecmaVersion": 2021,
4
- "sourceType": "module",
5
- "requireConfigFile": false
6
- },
7
- "env": {
8
- "node": true,
9
- "es2020": true
10
- },
11
- "rules": {
1
+ export default {
2
+ rules: {
12
3
  "global-require": "off",
13
4
  "quote-props": ["warn", "consistent-as-needed"],
14
- "comma-dangle": ["error", {"arrays": "always-multiline", "objects": "always-multiline", "functions": "never"}],
15
- "id-length": ["error", {"min": 2, "properties": "always", "exceptions": ["i"]}],
16
- "quotes": ["error", "double", { "allowTemplateLiterals": true }],
5
+ "comma-dangle": ["error", { arrays: "always-multiline", objects: "always-multiline", functions: "never" }],
6
+ "id-length": ["error", { min: 2, properties: "always", exceptions: ["i"] }],
7
+ "quotes": ["error", "double", { allowTemplateLiterals: true }],
17
8
  "no-plusplus": "error",
18
9
  "no-nested-ternary": "error",
19
10
  "no-multiple-empty-lines": "error",
@@ -26,14 +17,14 @@
26
17
  "no-import-assign": "error",
27
18
  "no-multi-assign": "error",
28
19
  "keyword-spacing": "error",
29
- "max-len": [ "warn", { "code": 140 } ],
20
+ "max-len": [ "warn", { code: 140 } ],
30
21
  "max-params": ["error", 4],
31
22
  "max-statements": ["error", 15],
32
23
  "no-loss-of-precision": "error",
33
24
  "no-unreachable-loop": "error",
34
25
  "require-atomic-updates": "error",
35
26
  "complexity": ["error", 4],
36
- "max-statements-per-line": ["error", { "max": 1 }],
27
+ "max-statements-per-line": ["error", { max: 1 }],
37
28
  "no-tabs": "error",
38
29
  "no-negated-condition": "error",
39
30
  "no-use-before-define": "error",
@@ -50,10 +41,10 @@
50
41
  "no-new-object": "error",
51
42
  "no-new-wrappers": "error",
52
43
  "no-useless-concat": "error",
53
- "no-unused-vars": ["error", {"ignoreRestSiblings": true}],
44
+ "no-unused-vars": ["error", { ignoreRestSiblings: true }],
54
45
  "array-bracket-newline": ["error", "consistent"],
55
46
  "func-names": ["error", "never"],
56
- "func-style": ["error", "expression", { "allowArrowFunctions": true }],
47
+ "func-style": ["error", "expression", { allowArrowFunctions: true }],
57
48
  "max-depth": ["error", 4],
58
49
  "arrow-parens": "error",
59
50
  "no-confusing-arrow": "error",
@@ -77,15 +68,14 @@
77
68
  "yoda": "error",
78
69
  "indent": ["error", 2],
79
70
  "object-curly-spacing": [ "error", "always" ],
80
- "object-curly-newline": ["error", { "consistent": true, "multiline": true }],
71
+ "object-curly-newline": ["error", { consistent: true, multiline: true }],
81
72
  "space-before-blocks": "error",
82
73
  "space-before-function-paren": [ "error", "always" ],
83
74
  "spaced-comment": "error",
84
75
  "no-whitespace-before-property": "error",
85
- "brace-style": ["error", "1tbs", { "allowSingleLine": false }],
76
+ "brace-style": ["error", "1tbs", { allowSingleLine: false }],
86
77
  "eol-last": ["error", "always"],
87
78
  "func-call-spacing": ["error", "never"],
88
- "semi": ["error", "always"]
79
+ "semi": ["error", "always"],
89
80
  },
90
- "extends": "eslint:recommended"
91
- }
81
+ };
package/lib/index.js CHANGED
@@ -7,7 +7,7 @@ let native;
7
7
 
8
8
  try {
9
9
  native = require("../build/Release/syscall.node");
10
- } catch (ex) {
10
+ } catch {
11
11
  native = require("../build/Debug/syscall.node");
12
12
  }
13
13
 
package/native/napilib.h CHANGED
@@ -30,22 +30,6 @@ static napi_status napilib_maybe_throw_by_status(napi_env env, napi_status statu
30
30
  #define NAPILIB_CHECK(x) do { napi_status ret = x; if(ret != napi_ok) { return napilib_maybe_throw_by_status(env, ret); } } while(0)
31
31
  #define NAPILIB_CHECK_GOTO_FAIL(x) if((x) != napi_ok) { goto fail; }
32
32
 
33
- static napi_status napilib_create_error_by_errno(napi_env env, int err, napi_value* error) {
34
- napi_value code;
35
- napi_value message;
36
- char c_message[512];
37
-
38
- const char* orig_err = strerror(err);
39
-
40
- snprintf(c_message, sizeof(c_message), "%s", orig_err);
41
-
42
- NAPILIB_CHECK(napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &code));
43
- NAPILIB_CHECK(napi_create_string_utf8(env, c_message, NAPI_AUTO_LENGTH, &message));
44
- NAPILIB_CHECK(napi_create_error(env, code, message, error));
45
-
46
- return napi_ok;
47
- }
48
-
49
33
  static napi_status napilib_throw_error_by_errno(napi_env env, int err) {
50
34
  NAPILIB_CHECK(napi_throw_error(env, "", strerror(err)));
51
35
  return napi_pending_exception;
package/native/syscall.c CHANGED
@@ -11,7 +11,6 @@ struct syscall_ctx {
11
11
  long num;
12
12
  long native_args[7];
13
13
  long res;
14
- long error_code;
15
14
  napi_ref arg_refs[7];
16
15
  };
17
16
 
@@ -26,17 +25,14 @@ static void syscall_work(void* opaque) {
26
25
  ctx->native_args[4],
27
26
  ctx->native_args[5],
28
27
  ctx->native_args[6]);
29
- if(ctx->res < 0) {
30
- ctx->error_code = errno;
31
- } else {
32
- ctx->error_code = 0;
33
- }
34
28
  }
35
29
 
36
30
  static napi_status syscall_done(napi_env env, void* opaque, napi_deferred deferred) {
37
31
  struct syscall_ctx* ctx = (struct syscall_ctx*) opaque;
38
32
  long res = ctx->res;
39
- long error_code = ctx->error_code;
33
+ napi_value result;
34
+ napi_value js_errno;
35
+ napi_value js_ret;
40
36
  unsigned int i;
41
37
 
42
38
  for(i = 0; i < sizeof(ctx->arg_refs) / sizeof(ctx->arg_refs[0]); i += 1) {
@@ -48,18 +44,21 @@ static napi_status syscall_done(napi_env env, void* opaque, napi_deferred deferr
48
44
 
49
45
  free(ctx);
50
46
 
51
- if(res < 0) {
52
- napi_value error;
47
+ NAPILIB_CHECK(napi_create_object(env, &result));
53
48
 
54
- NAPILIB_CHECK(napilib_create_error_by_errno(env, error_code, &error));
55
- NAPILIB_CHECK(napi_reject_deferred(env, deferred, error));
49
+ if (res < 0) {
50
+ NAPILIB_CHECK(napi_create_int32(env, errno, &js_errno));
51
+ NAPILIB_CHECK(napi_set_named_property(env, result, "errno", js_errno));
56
52
  } else {
57
- napi_value result;
53
+ NAPILIB_CHECK(napi_create_int32(env, 0, &js_errno));
54
+ NAPILIB_CHECK(napi_set_named_property(env, result, "errno", js_errno));
58
55
 
59
- NAPILIB_CHECK(napi_create_bigint_int64(env, res, &result));
60
- NAPILIB_CHECK(napi_resolve_deferred(env, deferred, result));
56
+ NAPILIB_CHECK(napi_create_bigint_int64(env, res, &js_ret));
57
+ NAPILIB_CHECK(napi_set_named_property(env, result, "ret", js_ret));
61
58
  }
62
59
 
60
+ NAPILIB_CHECK(napi_resolve_deferred(env, deferred, result));
61
+
63
62
  return napi_ok;
64
63
  }
65
64
 
@@ -107,8 +106,11 @@ static napi_status syscall_async_entry(napi_env env, napi_value* args, int arg_c
107
106
  return napi_ok;
108
107
  }
109
108
 
110
- static napi_status syscall_sync_entry(napi_env env, napi_value* args, int arg_count, napi_value* result) {
109
+ static napi_status syscall_sync_entry(napi_env env, napi_value* args, int arg_count, napi_value* result_ptr) {
111
110
  int i;
111
+ napi_value result;
112
+ napi_value js_errno;
113
+ napi_value js_ret;
112
114
  struct syscall_ctx ctx;
113
115
  memset(&ctx, 0, sizeof(ctx));
114
116
 
@@ -147,14 +149,21 @@ static napi_status syscall_sync_entry(napi_env env, napi_value* args, int arg_co
147
149
  ctx.native_args[5],
148
150
  ctx.native_args[6]);
149
151
 
150
- if(ctx.res < 0) {
151
- napi_value error;
152
- NAPILIB_CHECK(napilib_create_error_by_errno(env, errno, &error));
153
- NAPILIB_CHECK(napi_throw(env, error));
152
+ NAPILIB_CHECK(napi_create_object(env, &result));
153
+
154
+ if (ctx.res < 0) {
155
+ NAPILIB_CHECK(napi_create_int32(env, errno, &js_errno));
156
+ NAPILIB_CHECK(napi_set_named_property(env, result, "errno", js_errno));
154
157
  } else {
155
- NAPILIB_CHECK(napi_create_bigint_int64(env, ctx.res, result));
158
+ NAPILIB_CHECK(napi_create_int32(env, 0, &js_errno));
159
+ NAPILIB_CHECK(napi_set_named_property(env, result, "errno", js_errno));
160
+
161
+ NAPILIB_CHECK(napi_create_bigint_int64(env, ctx.res, &js_ret));
162
+ NAPILIB_CHECK(napi_set_named_property(env, result, "ret", js_ret));
156
163
  }
157
164
 
165
+ *result_ptr = result;
166
+
158
167
  return napi_ok;
159
168
  }
160
169
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "syscall-napi",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.8",
5
5
  "description": "Node.js module to perform promise-based asynchronous syscalls",
6
6
  "main": "lib/index.js",
7
7
  "scripts": {
@@ -20,9 +20,11 @@
20
20
  },
21
21
  "homepage": "https://github.com/k13-engineering/node-syscall-napi#readme",
22
22
  "devDependencies": {
23
- "c8": "^7.9.0",
24
- "eslint": "^7.32.0",
25
- "mocha": "^9.1.1",
26
- "node-archibald": "^0.0.5"
23
+ "c8": "^10.1.2",
24
+ "eslint": "^9.7.0",
25
+ "mocha": "^10.6.0"
26
+ },
27
+ "dependencies": {
28
+ "node-archibald": "^0.0.6"
27
29
  }
28
30
  }
package/test/basic.js CHANGED
@@ -6,12 +6,14 @@ import sys from "../lib/index.js";
6
6
 
7
7
  describe("basic", () => {
8
8
  it("should run getpid() correctly [async]", async () => {
9
- const pid = await sys.syscall(sys.__NR_getpid);
9
+ const { errno, ret: pid } = await sys.syscall(sys.__NR_getpid);
10
+ assert.equal(errno, 0);
10
11
  assert.equal(pid, process.pid);
11
12
  });
12
13
 
13
14
  it("should run getpid() correctly [sync]", () => {
14
- const pid = sys.syscall.sync(sys.__NR_getpid);
15
+ const { errno, ret: pid } = sys.syscall.sync(sys.__NR_getpid);
16
+ assert.equal(errno, 0);
15
17
  assert.equal(pid, process.pid);
16
18
  });
17
19
  });