node-addon-api 8.3.1 → 8.5.0

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/README.md CHANGED
@@ -19,7 +19,7 @@ and exception handling semantics with low overhead.
19
19
  API references are available in the [doc](doc/README.md) directory.
20
20
 
21
21
  <!-- x-release-please-start-version -->
22
- ## Current version: 8.3.1
22
+ ## Current version: 8.5.0
23
23
  <!-- x-release-please-end -->
24
24
 
25
25
  (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
@@ -92,4 +92,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for more details on our philosophy around
92
92
 
93
93
  Licensed under [MIT](./LICENSE.md)
94
94
 
95
- [Node-API support matrix]: https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api_version_matrix
95
+ [Node-API support matrix]: https://nodejs.org/dist/latest/docs/api/n-api.html#node-api-version-matrix
package/napi-inl.h CHANGED
@@ -1577,6 +1577,11 @@ inline Object::PropertyLValue<Key>& Object::PropertyLValue<Key>::operator=(
1577
1577
  return *this;
1578
1578
  }
1579
1579
 
1580
+ template <typename Key>
1581
+ inline Value Object::PropertyLValue<Key>::AsValue() const {
1582
+ return Value(*this);
1583
+ }
1584
+
1580
1585
  template <typename Key>
1581
1586
  inline Object::PropertyLValue<Key>::PropertyLValue(Object object, Key key)
1582
1587
  : _env(object.Env()), _object(object), _key(key) {}
@@ -2806,8 +2811,95 @@ inline void Promise::CheckCast(napi_env env, napi_value value) {
2806
2811
  NAPI_CHECK(result, "Promise::CheckCast", "value is not promise");
2807
2812
  }
2808
2813
 
2814
+ inline Promise::Promise() : Object() {}
2815
+
2809
2816
  inline Promise::Promise(napi_env env, napi_value value) : Object(env, value) {}
2810
2817
 
2818
+ inline MaybeOrValue<Promise> Promise::Then(napi_value onFulfilled) const {
2819
+ EscapableHandleScope scope(_env);
2820
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
2821
+ Value thenMethod;
2822
+ if (!Get("then").UnwrapTo(&thenMethod)) {
2823
+ return Nothing<Promise>();
2824
+ }
2825
+ MaybeOrValue<Value> result =
2826
+ thenMethod.As<Function>().Call(*this, {onFulfilled});
2827
+ if (result.IsJust()) {
2828
+ return Just(scope.Escape(result.Unwrap()).As<Promise>());
2829
+ }
2830
+ return Nothing<Promise>();
2831
+ #else
2832
+ Function thenMethod = Get("then").As<Function>();
2833
+ MaybeOrValue<Value> result = thenMethod.Call(*this, {onFulfilled});
2834
+ if (scope.Env().IsExceptionPending()) {
2835
+ return Promise();
2836
+ }
2837
+ return scope.Escape(result).As<Promise>();
2838
+ #endif
2839
+ }
2840
+
2841
+ inline MaybeOrValue<Promise> Promise::Then(napi_value onFulfilled,
2842
+ napi_value onRejected) const {
2843
+ EscapableHandleScope scope(_env);
2844
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
2845
+ Value thenMethod;
2846
+ if (!Get("then").UnwrapTo(&thenMethod)) {
2847
+ return Nothing<Promise>();
2848
+ }
2849
+ MaybeOrValue<Value> result =
2850
+ thenMethod.As<Function>().Call(*this, {onFulfilled, onRejected});
2851
+ if (result.IsJust()) {
2852
+ return Just(scope.Escape(result.Unwrap()).As<Promise>());
2853
+ }
2854
+ return Nothing<Promise>();
2855
+ #else
2856
+ Function thenMethod = Get("then").As<Function>();
2857
+ MaybeOrValue<Value> result =
2858
+ thenMethod.Call(*this, {onFulfilled, onRejected});
2859
+ if (scope.Env().IsExceptionPending()) {
2860
+ return Promise();
2861
+ }
2862
+ return scope.Escape(result).As<Promise>();
2863
+ #endif
2864
+ }
2865
+
2866
+ inline MaybeOrValue<Promise> Promise::Catch(napi_value onRejected) const {
2867
+ EscapableHandleScope scope(_env);
2868
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
2869
+ Value catchMethod;
2870
+ if (!Get("catch").UnwrapTo(&catchMethod)) {
2871
+ return Nothing<Promise>();
2872
+ }
2873
+ MaybeOrValue<Value> result =
2874
+ catchMethod.As<Function>().Call(*this, {onRejected});
2875
+ if (result.IsJust()) {
2876
+ return Just(scope.Escape(result.Unwrap()).As<Promise>());
2877
+ }
2878
+ return Nothing<Promise>();
2879
+ #else
2880
+ Function catchMethod = Get("catch").As<Function>();
2881
+ MaybeOrValue<Value> result = catchMethod.Call(*this, {onRejected});
2882
+ if (scope.Env().IsExceptionPending()) {
2883
+ return Promise();
2884
+ }
2885
+ return scope.Escape(result).As<Promise>();
2886
+ #endif
2887
+ }
2888
+
2889
+ inline MaybeOrValue<Promise> Promise::Then(const Function& onFulfilled) const {
2890
+ return Then(static_cast<napi_value>(onFulfilled));
2891
+ }
2892
+
2893
+ inline MaybeOrValue<Promise> Promise::Then(const Function& onFulfilled,
2894
+ const Function& onRejected) const {
2895
+ return Then(static_cast<napi_value>(onFulfilled),
2896
+ static_cast<napi_value>(onRejected));
2897
+ }
2898
+
2899
+ inline MaybeOrValue<Promise> Promise::Catch(const Function& onRejected) const {
2900
+ return Catch(static_cast<napi_value>(onRejected));
2901
+ }
2902
+
2811
2903
  ////////////////////////////////////////////////////////////////////////////////
2812
2904
  // Buffer<T> class
2813
2905
  ////////////////////////////////////////////////////////////////////////////////
package/napi.h CHANGED
@@ -860,6 +860,9 @@ class Object : public TypeTaggable {
860
860
  template <typename ValueType>
861
861
  PropertyLValue& operator=(ValueType value);
862
862
 
863
+ /// Converts an L-value to a value. For convenience.
864
+ Value AsValue() const;
865
+
863
866
  private:
864
867
  PropertyLValue() = delete;
865
868
  PropertyLValue(Object object, Key key);
@@ -1571,7 +1574,18 @@ class Promise : public Object {
1571
1574
 
1572
1575
  static void CheckCast(napi_env env, napi_value value);
1573
1576
 
1577
+ Promise();
1574
1578
  Promise(napi_env env, napi_value value);
1579
+
1580
+ MaybeOrValue<Promise> Then(napi_value onFulfilled) const;
1581
+ MaybeOrValue<Promise> Then(napi_value onFulfilled,
1582
+ napi_value onRejected) const;
1583
+ MaybeOrValue<Promise> Catch(napi_value onRejected) const;
1584
+
1585
+ MaybeOrValue<Promise> Then(const Function& onFulfilled) const;
1586
+ MaybeOrValue<Promise> Then(const Function& onFulfilled,
1587
+ const Function& onRejected) const;
1588
+ MaybeOrValue<Promise> Catch(const Function& onRejected) const;
1575
1589
  };
1576
1590
 
1577
1591
  template <typename T>
package/package.json CHANGED
@@ -472,7 +472,7 @@
472
472
  "lint:fix": "eslint --fix && node tools/clang-format --fix"
473
473
  },
474
474
  "pre-commit": "lint",
475
- "version": "8.3.1",
475
+ "version": "8.5.0",
476
476
  "support": true,
477
477
  "engines": {
478
478
  "node": "^18 || ^20 || >= 21"