Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AFatNiBBa committed Sep 5, 2021
1 parent 4b84152 commit e68ba16
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Build
build/

# Logs
logs
*.log
Expand Down
22 changes: 22 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\Users\\Game\\AppData\\Local\\node-gyp\\Cache\\15.1.0\\include\\node"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30037\\bin\\Hostx64\\x64\\cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
10 changes: 10 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

{
"targets": [
{
"target_name": "internal",
"sources": [ "src/internal.cpp" ],
"include_dirs": [ "node_modules/nan" ]
}
]
}
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "internal-prop",
"version": "1.0.0",
"description": "Gives access to native objects internal properties, such as proxy's [[Target]] and [[Handler]]",
"main": "src/main.js",
"scripts": {
"save": "npm publish & push"
},
"keywords": [
"proxy",
"jsproxy",
"promise",
"internal",
"native",
"target",
"handler",
"resonse",
"status"
],
"author": "AFatNiBBa",
"license": "ISC"
}
26 changes: 26 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# internal-prop
Gives access to native objects internal properties, such as a proxy's `[[Target]]` or `[[Handler]]`

## Warning
This module is native, it means that it contains c++ code that gets compiled every time the module is installed to ensure that the binaries can be read by your machine, the problem is that node apparently uses external tools that may be not present in your machine

## Usage
The module gives you indipendent functions that you can use to extract internal properties of objects
```js
const { fromProxy, fromPromise } = require("internal-prop");
```
Each function accept an object and returns an array of its internal properties <br>
If the object is not of the correct type (e.g. `fromProxy(obj)` wants `obj` to be a proxy) the function will return `null`

## Supported
- **`Proxy`**
- `fromProxy()` function
- The array contains the `[[Target]]` and then the `[[Handler]]`
- **`Promise`**
- `fromPromise()` function
- The array contains the `[[PromiseState]]` and then the `[[PromiseResult]]`
- The values can be:
- `"fulfilled"` and the result of the promise
- `"pending"` and `null`
- `"rejected"` and the error
59 changes: 59 additions & 0 deletions src/internal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

#include <node.h>

using namespace v8;

/**
* If the given object is a proxy it returns an array of the [[Target]] and the [[Handler]].
* If the object is not a proxy it returns 'null'.
*/
void fromProxy(const FunctionCallbackInfo<Value> &info) {
Isolate* isolate = info.GetIsolate();
if (!info[0]->IsProxy())
info.GetReturnValue().Set(Null(isolate));
else
{
Local<Proxy> temp = Local<Proxy>::Cast(info[0]);
Local<Value> out[] = {
temp->GetTarget(),
temp->GetHandler()
};
info.GetReturnValue().Set(Array::New(isolate, out, 2));
}
}

/**
* If the given object is a promise it returns an array of the [[PromiseState]] and the [[PromiseResult]].
* If the state is "pending" the result will be 'null'.
* If the object is not a promise it returns 'null'.
*/
void fromPromise(const FunctionCallbackInfo<Value> &info) {
Isolate* isolate = info.GetIsolate();
if (!info[0]->IsPromise())
info.GetReturnValue().Set(Null(isolate));
else
{
Local<Promise> temp = Local<Promise>::Cast(info[0]);
Promise::PromiseState state = temp->State();
Local<Value> out[] = {
String::NewFromUtf8(isolate, (
state == Promise::PromiseState::kFulfilled
? "fulfilled"
: state == Promise::PromiseState::kPending
? "pending"
: "rejected"
)).ToLocalChecked(),
state == Promise::PromiseState::kPending
? (Local<Value>)Null(isolate)
: temp->Result()
};
info.GetReturnValue().Set(Array::New(isolate, out, 2));
}
}

void init(Local<Object> exports, Local<Value> module, void* context) {
NODE_SET_METHOD(exports, "fromProxy", fromProxy);
NODE_SET_METHOD(exports, "fromPromise", fromPromise);
}

NODE_MODULE(internal, init)
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = require(`../build/Release/internal.node`);

0 comments on commit e68ba16

Please sign in to comment.