@contrast/agent 4.4.1 → 4.6.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.
Files changed (156) hide show
  1. package/bin/VERSION +1 -1
  2. package/bin/linux/contrast-service +0 -0
  3. package/bin/mac/contrast-service +0 -0
  4. package/bin/windows/contrast-service.exe +0 -0
  5. package/lib/assess/hapi/route-coverage.js +3 -3
  6. package/lib/assess/membrane/index.js +2 -8
  7. package/lib/assess/membrane/source-membrane.js +3 -4
  8. package/lib/assess/models/base-event.js +2 -2
  9. package/lib/assess/models/call-context.js +0 -3
  10. package/lib/assess/policy/propagators.json +20 -0
  11. package/lib/assess/policy/signatures.json +103 -0
  12. package/lib/assess/propagators/path/common.js +165 -36
  13. package/lib/assess/propagators/path/join.js +5 -1
  14. package/lib/assess/propagators/path/normalize.js +5 -1
  15. package/lib/assess/propagators/path/resolve.js +11 -2
  16. package/lib/assess/response-scanning/autocomplete-missing.js +0 -2
  17. package/lib/assess/response-scanning/parameter-pollution.js +0 -2
  18. package/lib/core/arch-components/dynamodb.js +1 -2
  19. package/lib/core/arch-components/dynamodbv3.js +44 -0
  20. package/lib/core/arch-components/index.js +1 -0
  21. package/lib/core/async-storage/hooks/bluebird.js +20 -0
  22. package/lib/core/config/options.js +3 -2
  23. package/lib/core/express/utils.js +1 -1
  24. package/lib/core/logger/debug-logger.js +15 -17
  25. package/lib/core/stacktrace.js +3 -4
  26. package/lib/feature-set.js +2 -1
  27. package/lib/hooks/encoding.js +1 -1
  28. package/lib/hooks/frameworks/base.js +8 -2
  29. package/lib/hooks/frameworks/http.js +23 -16
  30. package/lib/hooks/frameworks/http2.js +73 -0
  31. package/lib/hooks/frameworks/index.js +8 -3
  32. package/lib/hooks/http.js +112 -128
  33. package/lib/hooks/patcher.js +10 -12
  34. package/lib/hooks/require.js +16 -22
  35. package/lib/instrumentation.js +0 -3
  36. package/lib/protect/analysis/aho-corasick.js +13 -30
  37. package/lib/protect/rules/cmd-injection-command-backdoors/backdoor-detector.js +3 -3
  38. package/lib/protect/rules/signatures/reflected-xss/helpers/function-call.js +1 -1
  39. package/lib/protect/rules/xss/helpers/function-call.js +1 -1
  40. package/lib/util/clean-stack.js +1 -1
  41. package/lib/util/clean-string/brackets.js +3 -3
  42. package/lib/util/clean-string/concatenations.js +1 -1
  43. package/lib/util/clean-string/util.js +1 -2
  44. package/lib/util/ip-analyzer.js +1 -1
  45. package/lib/util/some.js +27 -0
  46. package/lib/util/xml-analyzer/external-entity-finder.js +1 -1
  47. package/package.json +14 -15
  48. package/lib/hooks/frameworks/https.js +0 -42
  49. package/node_modules/bindings/LICENSE.md +0 -22
  50. package/node_modules/bindings/README.md +0 -98
  51. package/node_modules/bindings/bindings.js +0 -221
  52. package/node_modules/bindings/package.json +0 -32
  53. package/node_modules/file-uri-to-path/.npmignore +0 -1
  54. package/node_modules/file-uri-to-path/.travis.yml +0 -30
  55. package/node_modules/file-uri-to-path/History.md +0 -21
  56. package/node_modules/file-uri-to-path/LICENSE +0 -20
  57. package/node_modules/file-uri-to-path/README.md +0 -74
  58. package/node_modules/file-uri-to-path/index.d.ts +0 -2
  59. package/node_modules/file-uri-to-path/index.js +0 -66
  60. package/node_modules/file-uri-to-path/package.json +0 -36
  61. package/node_modules/file-uri-to-path/test/test.js +0 -24
  62. package/node_modules/file-uri-to-path/test/tests.json +0 -13
  63. package/node_modules/glossy/LICENSE +0 -19
  64. package/node_modules/glossy/README.md +0 -129
  65. package/node_modules/glossy/index.js +0 -12
  66. package/node_modules/glossy/lib/glossy/parse.js +0 -520
  67. package/node_modules/glossy/lib/glossy/produce.js +0 -459
  68. package/node_modules/glossy/package.json +0 -47
  69. package/node_modules/glossy/test/decide.js +0 -7
  70. package/node_modules/glossy/test/decode_pri.js +0 -24
  71. package/node_modules/glossy/test/parse_3164.js +0 -104
  72. package/node_modules/glossy/test/parse_5424.js +0 -106
  73. package/node_modules/glossy/test/parse_5848.js +0 -40
  74. package/node_modules/glossy/test/parse_8601.js +0 -14
  75. package/node_modules/glossy/test/parse_rfc3339.js +0 -9
  76. package/node_modules/glossy/test/produce.js +0 -162
  77. package/node_modules/glossy/test/runner.js +0 -40
  78. package/node_modules/glossy/test/structure_data.js +0 -24
  79. package/node_modules/nan/CHANGELOG.md +0 -537
  80. package/node_modules/nan/LICENSE.md +0 -13
  81. package/node_modules/nan/README.md +0 -455
  82. package/node_modules/nan/doc/asyncworker.md +0 -146
  83. package/node_modules/nan/doc/buffers.md +0 -54
  84. package/node_modules/nan/doc/callback.md +0 -76
  85. package/node_modules/nan/doc/converters.md +0 -41
  86. package/node_modules/nan/doc/errors.md +0 -226
  87. package/node_modules/nan/doc/json.md +0 -62
  88. package/node_modules/nan/doc/maybe_types.md +0 -583
  89. package/node_modules/nan/doc/methods.md +0 -664
  90. package/node_modules/nan/doc/new.md +0 -147
  91. package/node_modules/nan/doc/node_misc.md +0 -123
  92. package/node_modules/nan/doc/object_wrappers.md +0 -263
  93. package/node_modules/nan/doc/persistent.md +0 -296
  94. package/node_modules/nan/doc/scopes.md +0 -73
  95. package/node_modules/nan/doc/script.md +0 -38
  96. package/node_modules/nan/doc/string_bytes.md +0 -62
  97. package/node_modules/nan/doc/v8_internals.md +0 -199
  98. package/node_modules/nan/doc/v8_misc.md +0 -85
  99. package/node_modules/nan/include_dirs.js +0 -1
  100. package/node_modules/nan/nan.h +0 -2898
  101. package/node_modules/nan/nan_callbacks.h +0 -88
  102. package/node_modules/nan/nan_callbacks_12_inl.h +0 -514
  103. package/node_modules/nan/nan_callbacks_pre_12_inl.h +0 -520
  104. package/node_modules/nan/nan_converters.h +0 -72
  105. package/node_modules/nan/nan_converters_43_inl.h +0 -68
  106. package/node_modules/nan/nan_converters_pre_43_inl.h +0 -42
  107. package/node_modules/nan/nan_define_own_property_helper.h +0 -29
  108. package/node_modules/nan/nan_implementation_12_inl.h +0 -430
  109. package/node_modules/nan/nan_implementation_pre_12_inl.h +0 -263
  110. package/node_modules/nan/nan_json.h +0 -166
  111. package/node_modules/nan/nan_maybe_43_inl.h +0 -356
  112. package/node_modules/nan/nan_maybe_pre_43_inl.h +0 -268
  113. package/node_modules/nan/nan_new.h +0 -340
  114. package/node_modules/nan/nan_object_wrap.h +0 -156
  115. package/node_modules/nan/nan_persistent_12_inl.h +0 -132
  116. package/node_modules/nan/nan_persistent_pre_12_inl.h +0 -242
  117. package/node_modules/nan/nan_private.h +0 -73
  118. package/node_modules/nan/nan_string_bytes.h +0 -305
  119. package/node_modules/nan/nan_typedarray_contents.h +0 -96
  120. package/node_modules/nan/nan_weak.h +0 -437
  121. package/node_modules/nan/package.json +0 -41
  122. package/node_modules/nan/tools/1to2.js +0 -412
  123. package/node_modules/nan/tools/README.md +0 -14
  124. package/node_modules/nan/tools/package.json +0 -19
  125. package/node_modules/unix-dgram/LICENSE +0 -13
  126. package/node_modules/unix-dgram/README.md +0 -107
  127. package/node_modules/unix-dgram/binding.gyp +0 -20
  128. package/node_modules/unix-dgram/build/Makefile +0 -324
  129. package/node_modules/unix-dgram/build/Release/.deps/Release/obj.target/unix_dgram/src/unix_dgram.o.d +0 -58
  130. package/node_modules/unix-dgram/build/Release/.deps/Release/obj.target/unix_dgram.node.d +0 -1
  131. package/node_modules/unix-dgram/build/Release/.deps/Release/unix_dgram.node.d +0 -1
  132. package/node_modules/unix-dgram/build/Release/obj.target/unix_dgram/src/unix_dgram.o +0 -0
  133. package/node_modules/unix-dgram/build/Release/obj.target/unix_dgram.node +0 -0
  134. package/node_modules/unix-dgram/build/Release/unix_dgram.node +0 -0
  135. package/node_modules/unix-dgram/build/binding.Makefile +0 -6
  136. package/node_modules/unix-dgram/build/config.gypi +0 -213
  137. package/node_modules/unix-dgram/build/unix_dgram.target.mk +0 -159
  138. package/node_modules/unix-dgram/lib/unix_dgram.js +0 -168
  139. package/node_modules/unix-dgram/package.json +0 -36
  140. package/node_modules/unix-dgram/src/unix_dgram.cc +0 -404
  141. package/node_modules/unix-dgram/src/win_dummy.cc +0 -7
  142. package/node_modules/unix-dgram/test/test-connect-callback.js +0 -68
  143. package/node_modules/unix-dgram/test/test-connect.js +0 -53
  144. package/node_modules/unix-dgram/test/test-dgram-unix.js +0 -58
  145. package/node_modules/unix-dgram/test/test-send-error.js +0 -26
  146. package/node_modules/winston-syslog/.eslintrc +0 -7
  147. package/node_modules/winston-syslog/.travis.yml +0 -14
  148. package/node_modules/winston-syslog/CHANGELOG.md +0 -9
  149. package/node_modules/winston-syslog/LICENSE +0 -20
  150. package/node_modules/winston-syslog/README.md +0 -135
  151. package/node_modules/winston-syslog/lib/utils.js +0 -26
  152. package/node_modules/winston-syslog/lib/winston-syslog.js +0 -385
  153. package/node_modules/winston-syslog/package.json +0 -56
  154. package/node_modules/winston-syslog/test/format-test.js +0 -122
  155. package/node_modules/winston-syslog/test/syslog-test.js +0 -95
  156. package/node_modules/winston-syslog/test/unix-connect-test.js +0 -133
@@ -1,147 +0,0 @@
1
- ## New
2
-
3
- NAN provides a `Nan::New()` helper for the creation of new JavaScript objects in a way that's compatible across the supported versions of V8.
4
-
5
- - <a href="#api_nan_new"><b><code>Nan::New()</code></b></a>
6
- - <a href="#api_nan_undefined"><b><code>Nan::Undefined()</code></b></a>
7
- - <a href="#api_nan_null"><b><code>Nan::Null()</code></b></a>
8
- - <a href="#api_nan_true"><b><code>Nan::True()</code></b></a>
9
- - <a href="#api_nan_false"><b><code>Nan::False()</code></b></a>
10
- - <a href="#api_nan_empty_string"><b><code>Nan::EmptyString()</code></b></a>
11
-
12
-
13
- <a name="api_nan_new"></a>
14
- ### Nan::New()
15
-
16
- `Nan::New()` should be used to instantiate new JavaScript objects.
17
-
18
- Refer to the specific V8 type in the [V8 documentation](https://v8docs.nodesource.com/node-8.16/d1/d83/classv8_1_1_data.html) for information on the types of arguments required for instantiation.
19
-
20
- Signatures:
21
-
22
- Return types are mostly omitted from the signatures for simplicity. In most cases the type will be contained within a `v8::Local<T>`. The following types will be contained within a `Nan::MaybeLocal<T>`: `v8::String`, `v8::Date`, `v8::RegExp`, `v8::Script`, `v8::UnboundScript`.
23
-
24
- Empty objects:
25
-
26
- ```c++
27
- Nan::New<T>();
28
- ```
29
-
30
- Generic single and multiple-argument:
31
-
32
- ```c++
33
- Nan::New<T>(A0 arg0);
34
- Nan::New<T>(A0 arg0, A1 arg1);
35
- Nan::New<T>(A0 arg0, A1 arg1, A2 arg2);
36
- Nan::New<T>(A0 arg0, A1 arg1, A2 arg2, A3 arg3);
37
- ```
38
-
39
- For creating `v8::FunctionTemplate` and `v8::Function` objects:
40
-
41
- _The definition of `Nan::FunctionCallback` can be found in the [Method declaration](./methods.md#api_nan_method) documentation._
42
-
43
- ```c++
44
- Nan::New<T>(Nan::FunctionCallback callback,
45
- v8::Local<v8::Value> data = v8::Local<v8::Value>());
46
- Nan::New<T>(Nan::FunctionCallback callback,
47
- v8::Local<v8::Value> data = v8::Local<v8::Value>(),
48
- A2 a2 = A2());
49
- ```
50
-
51
- Native number types:
52
-
53
- ```c++
54
- v8::Local<v8::Boolean> Nan::New<T>(bool value);
55
- v8::Local<v8::Int32> Nan::New<T>(int32_t value);
56
- v8::Local<v8::Uint32> Nan::New<T>(uint32_t value);
57
- v8::Local<v8::Number> Nan::New<T>(double value);
58
- ```
59
-
60
- String types:
61
-
62
- ```c++
63
- Nan::MaybeLocal<v8::String> Nan::New<T>(std::string const& value);
64
- Nan::MaybeLocal<v8::String> Nan::New<T>(const char * value, int length);
65
- Nan::MaybeLocal<v8::String> Nan::New<T>(const char * value);
66
- Nan::MaybeLocal<v8::String> Nan::New<T>(const uint16_t * value);
67
- Nan::MaybeLocal<v8::String> Nan::New<T>(const uint16_t * value, int length);
68
- ```
69
-
70
- Specialized types:
71
-
72
- ```c++
73
- v8::Local<v8::String> Nan::New<T>(v8::String::ExternalStringResource * value);
74
- v8::Local<v8::String> Nan::New<T>(Nan::ExternalOneByteStringResource * value);
75
- v8::Local<v8::RegExp> Nan::New<T>(v8::Local<v8::String> pattern, v8::RegExp::Flags flags);
76
- ```
77
-
78
- Note that `Nan::ExternalOneByteStringResource` maps to [`v8::String::ExternalOneByteStringResource`](https://v8docs.nodesource.com/node-8.16/d9/db3/classv8_1_1_string_1_1_external_one_byte_string_resource.html), and `v8::String::ExternalAsciiStringResource` in older versions of V8.
79
-
80
-
81
- <a name="api_nan_undefined"></a>
82
- ### Nan::Undefined()
83
-
84
- A helper method to reference the `v8::Undefined` object in a way that is compatible across all supported versions of V8.
85
-
86
- Signature:
87
-
88
- ```c++
89
- v8::Local<v8::Primitive> Nan::Undefined()
90
- ```
91
-
92
- <a name="api_nan_null"></a>
93
- ### Nan::Null()
94
-
95
- A helper method to reference the `v8::Null` object in a way that is compatible across all supported versions of V8.
96
-
97
- Signature:
98
-
99
- ```c++
100
- v8::Local<v8::Primitive> Nan::Null()
101
- ```
102
-
103
- <a name="api_nan_true"></a>
104
- ### Nan::True()
105
-
106
- A helper method to reference the `v8::Boolean` object representing the `true` value in a way that is compatible across all supported versions of V8.
107
-
108
- Signature:
109
-
110
- ```c++
111
- v8::Local<v8::Boolean> Nan::True()
112
- ```
113
-
114
- <a name="api_nan_false"></a>
115
- ### Nan::False()
116
-
117
- A helper method to reference the `v8::Boolean` object representing the `false` value in a way that is compatible across all supported versions of V8.
118
-
119
- Signature:
120
-
121
- ```c++
122
- v8::Local<v8::Boolean> Nan::False()
123
- ```
124
-
125
- <a name="api_nan_empty_string"></a>
126
- ### Nan::EmptyString()
127
-
128
- Call [`v8::String::Empty`](https://v8docs.nodesource.com/node-8.16/d2/db3/classv8_1_1_string.html#a7c1bc8886115d7ee46f1d571dd6ebc6d) to reference the empty string in a way that is compatible across all supported versions of V8.
129
-
130
- Signature:
131
-
132
- ```c++
133
- v8::Local<v8::String> Nan::EmptyString()
134
- ```
135
-
136
-
137
- <a name="api_nan_new_one_byte_string"></a>
138
- ### Nan::NewOneByteString()
139
-
140
- An implementation of [`v8::String::NewFromOneByte()`](https://v8docs.nodesource.com/node-8.16/d2/db3/classv8_1_1_string.html#a5264d50b96d2c896ce525a734dc10f09) provided for consistent availability and API across supported versions of V8. Allocates a new string from Latin-1 data.
141
-
142
- Signature:
143
-
144
- ```c++
145
- Nan::MaybeLocal<v8::String> Nan::NewOneByteString(const uint8_t * value,
146
- int length = -1)
147
- ```
@@ -1,123 +0,0 @@
1
- ## Miscellaneous Node Helpers
2
-
3
- - <a href="#api_nan_asyncresource"><b><code>Nan::AsyncResource</code></b></a>
4
- - <a href="#api_nan_make_callback"><b><code>Nan::MakeCallback()</code></b></a>
5
- - <a href="#api_nan_module_init"><b><code>NAN_MODULE_INIT()</code></b></a>
6
- - <a href="#api_nan_export"><b><code>Nan::Export()</code></b></a>
7
-
8
- <a name="api_nan_asyncresource"></a>
9
- ### Nan::AsyncResource
10
-
11
- This class is analogous to the `AsyncResource` JavaScript class exposed by Node's [async_hooks][] API.
12
-
13
- When calling back into JavaScript asynchronously, special care must be taken to ensure that the runtime can properly track
14
- async hops. `Nan::AsyncResource` is a class that provides an RAII wrapper around `node::EmitAsyncInit`, `node::EmitAsyncDestroy`,
15
- and `node::MakeCallback`. Using this mechanism to call back into JavaScript, as opposed to `Nan::MakeCallback` or
16
- `v8::Function::Call` ensures that the callback is executed in the correct async context. This ensures that async mechanisms
17
- such as domains and [async_hooks][] function correctly.
18
-
19
- Definition:
20
-
21
- ```c++
22
- class AsyncResource {
23
- public:
24
- AsyncResource(v8::Local<v8::String> name,
25
- v8::Local<v8::Object> resource = New<v8::Object>());
26
- AsyncResource(const char* name,
27
- v8::Local<v8::Object> resource = New<v8::Object>());
28
- ~AsyncResource();
29
-
30
- v8::MaybeLocal<v8::Value> runInAsyncScope(v8::Local<v8::Object> target,
31
- v8::Local<v8::Function> func,
32
- int argc,
33
- v8::Local<v8::Value>* argv);
34
- v8::MaybeLocal<v8::Value> runInAsyncScope(v8::Local<v8::Object> target,
35
- v8::Local<v8::String> symbol,
36
- int argc,
37
- v8::Local<v8::Value>* argv);
38
- v8::MaybeLocal<v8::Value> runInAsyncScope(v8::Local<v8::Object> target,
39
- const char* method,
40
- int argc,
41
- v8::Local<v8::Value>* argv);
42
- };
43
- ```
44
-
45
- * `name`: Identifier for the kind of resource that is being provided for diagnostics information exposed by the [async_hooks][]
46
- API. This will be passed to the possible `init` hook as the `type`. To avoid name collisions with other modules we recommend
47
- that the name include the name of the owning module as a prefix. For example `mysql` module could use something like
48
- `mysql:batch-db-query-resource`.
49
- * `resource`: An optional object associated with the async work that will be passed to the possible [async_hooks][]
50
- `init` hook. If this parameter is omitted, or an empty handle is provided, this object will be created automatically.
51
- * When calling JS on behalf of this resource, one can use `runInAsyncScope`. This will ensure that the callback runs in the
52
- correct async execution context.
53
- * `AsyncDestroy` is automatically called when an AsyncResource object is destroyed.
54
-
55
- For more details, see the Node [async_hooks][] documentation. You might also want to take a look at the documentation for the
56
- [N-API counterpart][napi]. For example usage, see the `asyncresource.cpp` example in the `test/cpp` directory.
57
-
58
- <a name="api_nan_make_callback"></a>
59
- ### Nan::MakeCallback()
60
-
61
- Deprecated wrappers around the legacy `node::MakeCallback()` APIs. Node.js 10+
62
- has deprecated these legacy APIs as they do not provide a mechanism to preserve
63
- async context.
64
-
65
- We recommend that you use the `AsyncResource` class and `AsyncResource::runInAsyncScope` instead of using `Nan::MakeCallback` or
66
- `v8::Function#Call()` directly. `AsyncResource` properly takes care of running the callback in the correct async execution
67
- context – something that is essential for functionality like domains, async_hooks and async debugging.
68
-
69
- Signatures:
70
-
71
- ```c++
72
- NAN_DEPRECATED
73
- v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
74
- v8::Local<v8::Function> func,
75
- int argc,
76
- v8::Local<v8::Value>* argv);
77
- NAN_DEPRECATED
78
- v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
79
- v8::Local<v8::String> symbol,
80
- int argc,
81
- v8::Local<v8::Value>* argv);
82
- NAN_DEPRECATED
83
- v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
84
- const char* method,
85
- int argc,
86
- v8::Local<v8::Value>* argv);
87
- ```
88
-
89
-
90
- <a name="api_nan_module_init"></a>
91
- ### NAN_MODULE_INIT()
92
-
93
- Used to define the entry point function to a Node add-on. Creates a function with a given `name` that receives a `target` object representing the equivalent of the JavaScript `exports` object.
94
-
95
- See example below.
96
-
97
- <a name="api_nan_export"></a>
98
- ### Nan::Export()
99
-
100
- A simple helper to register a `v8::FunctionTemplate` from a JavaScript-accessible method (see [Methods](./methods.md)) as a property on an object. Can be used in a way similar to assigning properties to `module.exports` in JavaScript.
101
-
102
- Signature:
103
-
104
- ```c++
105
- void Export(v8::Local<v8::Object> target, const char *name, Nan::FunctionCallback f)
106
- ```
107
-
108
- Also available as the shortcut `NAN_EXPORT` macro.
109
-
110
- Example:
111
-
112
- ```c++
113
- NAN_METHOD(Foo) {
114
- ...
115
- }
116
-
117
- NAN_MODULE_INIT(Init) {
118
- NAN_EXPORT(target, Foo);
119
- }
120
- ```
121
-
122
- [async_hooks]: https://nodejs.org/dist/latest-v9.x/docs/api/async_hooks.html
123
- [napi]: https://nodejs.org/dist/latest-v9.x/docs/api/n-api.html#n_api_custom_asynchronous_operations
@@ -1,263 +0,0 @@
1
- ## Object Wrappers
2
-
3
- The `ObjectWrap` class can be used to make wrapped C++ objects and a factory of wrapped objects.
4
-
5
- - <a href="#api_nan_object_wrap"><b><code>Nan::ObjectWrap</code></b></a>
6
-
7
-
8
- <a name="api_nan_object_wrap"></a>
9
- ### Nan::ObjectWrap()
10
-
11
- A reimplementation of `node::ObjectWrap` that adds some API not present in older versions of Node. Should be preferred over `node::ObjectWrap` in all cases for consistency.
12
-
13
- Definition:
14
-
15
- ```c++
16
- class ObjectWrap {
17
- public:
18
- ObjectWrap();
19
-
20
- virtual ~ObjectWrap();
21
-
22
- template <class T>
23
- static inline T* Unwrap(v8::Local<v8::Object> handle);
24
-
25
- inline v8::Local<v8::Object> handle();
26
-
27
- inline Nan::Persistent<v8::Object>& persistent();
28
-
29
- protected:
30
- inline void Wrap(v8::Local<v8::Object> handle);
31
-
32
- inline void MakeWeak();
33
-
34
- /* Ref() marks the object as being attached to an event loop.
35
- * Refed objects will not be garbage collected, even if
36
- * all references are lost.
37
- */
38
- virtual void Ref();
39
-
40
- /* Unref() marks an object as detached from the event loop. This is its
41
- * default state. When an object with a "weak" reference changes from
42
- * attached to detached state it will be freed. Be careful not to access
43
- * the object after making this call as it might be gone!
44
- * (A "weak reference" means an object that only has a
45
- * persistent handle.)
46
- *
47
- * DO NOT CALL THIS FROM DESTRUCTOR
48
- */
49
- virtual void Unref();
50
-
51
- int refs_; // ro
52
- };
53
- ```
54
-
55
- See the Node documentation on [Wrapping C++ Objects](https://nodejs.org/api/addons.html#addons_wrapping_c_objects) for more details.
56
-
57
- ### This vs. Holder
58
-
59
- When calling `Unwrap`, it is important that the argument is indeed some JavaScript object which got wrapped by a `Wrap` call for this class or any derived class.
60
- The `Signature` installed by [`Nan::SetPrototypeMethod()`](methods.md#api_nan_set_prototype_method) does ensure that `info.Holder()` is just such an instance.
61
- In Node 0.12 and later, `info.This()` will also be of such a type, since otherwise the invocation will get rejected.
62
- However, in Node 0.10 and before it was possible to invoke a method on a JavaScript object which just had the extension type in its prototype chain.
63
- In such a situation, calling `Unwrap` on `info.This()` will likely lead to a failed assertion causing a crash, but could lead to even more serious corruption.
64
-
65
- On the other hand, calling `Unwrap` in an [accessor](methods.md#api_nan_set_accessor) should not use `Holder()` if the accessor is defined on the prototype.
66
- So either define your accessors on the instance template,
67
- or use `This()` after verifying that it is indeed a valid object.
68
-
69
- ### Examples
70
-
71
- #### Basic
72
-
73
- ```c++
74
- class MyObject : public Nan::ObjectWrap {
75
- public:
76
- static NAN_MODULE_INIT(Init) {
77
- v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
78
- tpl->SetClassName(Nan::New("MyObject").ToLocalChecked());
79
- tpl->InstanceTemplate()->SetInternalFieldCount(1);
80
-
81
- Nan::SetPrototypeMethod(tpl, "getHandle", GetHandle);
82
- Nan::SetPrototypeMethod(tpl, "getValue", GetValue);
83
-
84
- constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
85
- Nan::Set(target, Nan::New("MyObject").ToLocalChecked(),
86
- Nan::GetFunction(tpl).ToLocalChecked());
87
- }
88
-
89
- private:
90
- explicit MyObject(double value = 0) : value_(value) {}
91
- ~MyObject() {}
92
-
93
- static NAN_METHOD(New) {
94
- if (info.IsConstructCall()) {
95
- double value = info[0]->IsUndefined() ? 0 : Nan::To<double>(info[0]).FromJust();
96
- MyObject *obj = new MyObject(value);
97
- obj->Wrap(info.This());
98
- info.GetReturnValue().Set(info.This());
99
- } else {
100
- const int argc = 1;
101
- v8::Local<v8::Value> argv[argc] = {info[0]};
102
- v8::Local<v8::Function> cons = Nan::New(constructor());
103
- info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
104
- }
105
- }
106
-
107
- static NAN_METHOD(GetHandle) {
108
- MyObject* obj = Nan::ObjectWrap::Unwrap<MyObject>(info.Holder());
109
- info.GetReturnValue().Set(obj->handle());
110
- }
111
-
112
- static NAN_METHOD(GetValue) {
113
- MyObject* obj = Nan::ObjectWrap::Unwrap<MyObject>(info.Holder());
114
- info.GetReturnValue().Set(obj->value_);
115
- }
116
-
117
- static inline Nan::Persistent<v8::Function> & constructor() {
118
- static Nan::Persistent<v8::Function> my_constructor;
119
- return my_constructor;
120
- }
121
-
122
- double value_;
123
- };
124
-
125
- NODE_MODULE(objectwrapper, MyObject::Init)
126
- ```
127
-
128
- To use in Javascript:
129
-
130
- ```Javascript
131
- var objectwrapper = require('bindings')('objectwrapper');
132
-
133
- var obj = new objectwrapper.MyObject(5);
134
- console.log('Should be 5: ' + obj.getValue());
135
- ```
136
-
137
- #### Factory of wrapped objects
138
-
139
- ```c++
140
- class MyFactoryObject : public Nan::ObjectWrap {
141
- public:
142
- static NAN_MODULE_INIT(Init) {
143
- v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
144
- tpl->InstanceTemplate()->SetInternalFieldCount(1);
145
-
146
- Nan::SetPrototypeMethod(tpl, "getValue", GetValue);
147
-
148
- constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
149
- }
150
-
151
- static NAN_METHOD(NewInstance) {
152
- v8::Local<v8::Function> cons = Nan::New(constructor());
153
- double value = info[0]->IsNumber() ? Nan::To<double>(info[0]).FromJust() : 0;
154
- const int argc = 1;
155
- v8::Local<v8::Value> argv[1] = {Nan::New(value)};
156
- info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
157
- }
158
-
159
- // Needed for the next example:
160
- inline double value() const {
161
- return value_;
162
- }
163
-
164
- private:
165
- explicit MyFactoryObject(double value = 0) : value_(value) {}
166
- ~MyFactoryObject() {}
167
-
168
- static NAN_METHOD(New) {
169
- if (info.IsConstructCall()) {
170
- double value = info[0]->IsNumber() ? Nan::To<double>(info[0]).FromJust() : 0;
171
- MyFactoryObject * obj = new MyFactoryObject(value);
172
- obj->Wrap(info.This());
173
- info.GetReturnValue().Set(info.This());
174
- } else {
175
- const int argc = 1;
176
- v8::Local<v8::Value> argv[argc] = {info[0]};
177
- v8::Local<v8::Function> cons = Nan::New(constructor());
178
- info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
179
- }
180
- }
181
-
182
- static NAN_METHOD(GetValue) {
183
- MyFactoryObject* obj = ObjectWrap::Unwrap<MyFactoryObject>(info.Holder());
184
- info.GetReturnValue().Set(obj->value_);
185
- }
186
-
187
- static inline Nan::Persistent<v8::Function> & constructor() {
188
- static Nan::Persistent<v8::Function> my_constructor;
189
- return my_constructor;
190
- }
191
-
192
- double value_;
193
- };
194
-
195
- NAN_MODULE_INIT(Init) {
196
- MyFactoryObject::Init(target);
197
- Nan::Set(target,
198
- Nan::New<v8::String>("newFactoryObjectInstance").ToLocalChecked(),
199
- Nan::GetFunction(
200
- Nan::New<v8::FunctionTemplate>(MyFactoryObject::NewInstance)).ToLocalChecked()
201
- );
202
- }
203
-
204
- NODE_MODULE(wrappedobjectfactory, Init)
205
- ```
206
-
207
- To use in Javascript:
208
-
209
- ```Javascript
210
- var wrappedobjectfactory = require('bindings')('wrappedobjectfactory');
211
-
212
- var obj = wrappedobjectfactory.newFactoryObjectInstance(10);
213
- console.log('Should be 10: ' + obj.getValue());
214
- ```
215
-
216
- #### Passing wrapped objects around
217
-
218
- Use the `MyFactoryObject` class above along with the following:
219
-
220
- ```c++
221
- static NAN_METHOD(Sum) {
222
- Nan::MaybeLocal<v8::Object> maybe1 = Nan::To<v8::Object>(info[0]);
223
- Nan::MaybeLocal<v8::Object> maybe2 = Nan::To<v8::Object>(info[1]);
224
-
225
- // Quick check:
226
- if (maybe1.IsEmpty() || maybe2.IsEmpty()) {
227
- // return value is undefined by default
228
- return;
229
- }
230
-
231
- MyFactoryObject* obj1 =
232
- Nan::ObjectWrap::Unwrap<MyFactoryObject>(maybe1.ToLocalChecked());
233
- MyFactoryObject* obj2 =
234
- Nan::ObjectWrap::Unwrap<MyFactoryObject>(maybe2.ToLocalChecked());
235
-
236
- info.GetReturnValue().Set(Nan::New<v8::Number>(obj1->value() + obj2->value()));
237
- }
238
-
239
- NAN_MODULE_INIT(Init) {
240
- MyFactoryObject::Init(target);
241
- Nan::Set(target,
242
- Nan::New<v8::String>("newFactoryObjectInstance").ToLocalChecked(),
243
- Nan::GetFunction(
244
- Nan::New<v8::FunctionTemplate>(MyFactoryObject::NewInstance)).ToLocalChecked()
245
- );
246
- Nan::Set(target,
247
- Nan::New<v8::String>("sum").ToLocalChecked(),
248
- Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Sum)).ToLocalChecked()
249
- );
250
- }
251
-
252
- NODE_MODULE(myaddon, Init)
253
- ```
254
-
255
- To use in Javascript:
256
-
257
- ```Javascript
258
- var myaddon = require('bindings')('myaddon');
259
-
260
- var obj1 = myaddon.newFactoryObjectInstance(5);
261
- var obj2 = myaddon.newFactoryObjectInstance(10);
262
- console.log('sum of object values: ' + myaddon.sum(obj1, obj2));
263
- ```