Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deno runtime support #5

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
cmake_minimum_required (VERSION 3.24)
project(node_abieos VERSION 2.2 LANGUAGES CXX C)

add_definitions(-DNAPI_VERSION=8)
# add_definitions(-DNAPI_EXPERIMENTAL)

include_directories(${CMAKE_JS_INC})
cmake_minimum_required (VERSION 3.21.4)
project(deno_abieos VERSION 2.2 LANGUAGES CXX C)

set(default_build_type "Release")
set(CMAKE_CXX_STANDARD 17)
Expand All @@ -15,6 +10,8 @@ option(ABIEOS_NO_INT128 "disable use of __int128" OFF)

find_package(Threads)

include_directories(include)

add_library(abieos STATIC src/abi.cpp src/crypto.cpp include/eosio/fpconv.c)
target_include_directories(abieos PUBLIC include)

Expand All @@ -25,11 +22,6 @@ endif()
set_target_properties(abieos PROPERTIES POSITION_INDEPENDENT_CODE ON)

add_library(${PROJECT_NAME} SHARED src/main.cpp src/abieos.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB};abieos;${CMAKE_THREAD_LIBS_INIT})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".so")
target_link_libraries(${PROJECT_NAME} abieos;${CMAKE_THREAD_LIBS_INIT})
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wno-unused-parameter)

execute_process(COMMAND node -p "require('node-addon-api').include" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE NODE_ADDON_API_DIR)
string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR})
46 changes: 46 additions & 0 deletions deno_dist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# node-abieos

Node.js native binding for [abieos](https://github.com/EOSIO/abieos), with some improvements:

- Contracts can be directly updated on the map
- Added `abieos_delete_contract`

Made with ♥ by [EOS Rio](https://eosrio.io/)

----
**Only Linux is supported for now, import will be null on others**

- Typescript typings included
- Prebuilt binary included (Clang 9.0.0 required to build)

### Install

```shell script
npm i @eosrio/node-abieos --save
```

### Usage

CommonJS
```js
const nodeAbieos = require('@eosrio/node-abieos');
```

ES Modules
```typescript
import * as nodeAbieos from "@eosrio/node-abieos";
```

Check the [/examples](https://github.com/eosrio/node-abieos/tree/master/examples) folder for implementation examples

### Building

env setup instructions soon
```shell script
git clone https://github.com/eosrio/node-abieos.git
cd node-abieos
# linux
npm run build:linux
# windows
npm run build:win
```
20 changes: 20 additions & 0 deletions deno_dist/abieos.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Buffer } from "node:buffer";
/// <reference types="node" />
declare let abieos: any | null;
export declare class Abieos {
private static instance;
static native: typeof abieos;
private constructor();
static getInstance(): Abieos;
stringToName(nameString: string): BigInteger;
jsonToHex(contractName: string, type: string, json: string | object): string;
hexToJson(contractName: string, type: string, hex: string): any;
binToJson(contractName: string, type: string, buffer: Buffer): any;
loadAbi(contractName: string, abi: string | object): boolean;
loadAbiHex(contractName: string, abihex: string): boolean;
getTypeForAction(contractName: string, actionName: string): string;
getTypeForTable(contractName: string, table_name: string): string;
deleteContract(contractName: string): boolean;
}
export {};
//# sourceMappingURL=abieos.d.ts.map
1 change: 1 addition & 0 deletions deno_dist/abieos.d.ts.map

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

100 changes: 100 additions & 0 deletions deno_dist/abieos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
let modulePath = "../dist/abieos.node";
let abieos = null;
if (process.platform === 'linux') {
abieos = require(modulePath);
}
else if (process.platform === 'win32') {
// throw new Error(`${process.platform} is not supported by node-abieos`);
abieos = null;
}
export class Abieos {
constructor() {
Abieos.native = abieos;
}
static getInstance() {
if (!Abieos.instance) {
Abieos.instance = new Abieos();
}
return Abieos.instance;
}
stringToName(nameString) {
return Abieos.native.string_to_name(nameString);
}
jsonToHex(contractName, type, json) {
const jsonData = typeof json === 'object' ? JSON.stringify(json) : json;
const data = Abieos.native.json_to_hex(contractName, type, jsonData);
if (data === 'PARSING_ERROR') {
throw new Error('failed to parse data');
}
else {
return data;
}
}
hexToJson(contractName, type, hex) {
const data = Abieos.native.hex_to_json(contractName, type, hex);
if (data) {
try {
return JSON.parse(data);
}
catch (e) {
throw new Error('failed to parse json string: ' + data);
}
}
else {
throw new Error('failed to parse hex data');
}
}
binToJson(contractName, type, buffer) {
const data = Abieos.native.bin_to_json(contractName, type, buffer);
if (data[0] === '{' || data[0] === '[') {
try {
return JSON.parse(data);
}
catch (e) {
throw new Error('json parse error');
}
}
else {
throw new Error(data);
}
}
loadAbi(contractName, abi) {
if (typeof abi === 'string') {
return Abieos.native.load_abi(contractName, abi);
}
else {
if (typeof abi === 'object') {
return Abieos.native.load_abi(contractName, JSON.stringify(abi));
}
else {
throw new Error('ABI must be a String or Object');
}
}
}
loadAbiHex(contractName, abihex) {
return Abieos.native.load_abi_hex(contractName, abihex);
}
getTypeForAction(contractName, actionName) {
const result = Abieos.native.get_type_for_action(contractName, actionName);
if (result === 'NOT_FOUND') {
throw new Error('action ' + actionName + ' not found on contract ' + contractName);
}
else {
return result;
}
}
getTypeForTable(contractName, table_name) {
const result = Abieos.native.get_type_for_table(contractName, table_name);
if (result === 'NOT_FOUND') {
throw new Error('table ' + table_name + ' not found on contract ' + contractName);
}
else {
return result;
}
}
deleteContract(contractName) {
return Abieos.native.delete_contract(contractName);
}
}
114 changes: 114 additions & 0 deletions deno_dist/abieos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { createRequire } from "https://deno.land/[email protected]/node/module.ts"
let modulePath = '';
let metaUrl = '';
if (import.meta.url.startsWith('file://')) {
metaUrl = `${import.meta.url}`;
modulePath = `../dist/abieos.node`;
} else {
metaUrl = `${Deno.mainModule}`;
modulePath = `../../bin/abieos.node`;
const require = createRequire(metaUrl);

const paths = ['abieos.node', '../abieos.node', '../../abieos.node', '../../bin/abieos.node', '../bin/abieos.node', './bin/abieos.node'];
for (const maybePath of paths) {
try {
require(maybePath);
modulePath = maybePath;
console.log('gotcha', maybePath)
break;
} catch (e) {
console.log('wrong', e);
}
}
}
const require = createRequire(metaUrl);
let abieos = require(modulePath);
export class Abieos {
constructor() {
Abieos.native = abieos;
}
static getInstance() {
if (!Abieos.instance) {
Abieos.instance = new Abieos();
}
return Abieos.instance;
}
stringToName(nameString) {
return Abieos.native.string_to_name(nameString);
}
jsonToHex(contractName, type, json) {
const jsonData = typeof json === 'object' ? JSON.stringify(json) : json;
const data = Abieos.native.json_to_hex(contractName, type, jsonData);
if (data === 'PARSING_ERROR') {
throw new Error('failed to parse data');
}
else {
return data;
}
}
hexToJson(contractName, type, hex) {
const data = Abieos.native.hex_to_json(contractName, type, hex);
if (data) {
try {
return JSON.parse(data);
}
catch (e) {
throw new Error('failed to parse json string: ' + data);
}
}
else {
throw new Error('failed to parse hex data');
}
}
binToJson(contractName, type, buffer) {
const data = Abieos.native.bin_to_json(contractName, type, buffer);
if (data[0] === '{' || data[0] === '[') {
try {
return JSON.parse(data);
}
catch (e) {
throw new Error('json parse error');
}
}
else {
throw new Error(data);
}
}
loadAbi(contractName, abi) {
if (typeof abi === 'string') {
return Abieos.native.load_abi(contractName, abi);
}
else {
if (typeof abi === 'object') {
return Abieos.native.load_abi(contractName, JSON.stringify(abi));
}
else {
throw new Error('ABI must be a String or Object');
}
}
}
loadAbiHex(contractName, abihex) {
return Abieos.native.load_abi_hex(contractName, abihex);
}
getTypeForAction(contractName, actionName) {
const result = Abieos.native.get_type_for_action(contractName, actionName);
if (result === 'NOT_FOUND') {
throw new Error('action ' + actionName + ' not found on contract ' + contractName);
}
else {
return result;
}
}
getTypeForTable(contractName, table_name) {
const result = Abieos.native.get_type_for_table(contractName, table_name);
if (result === 'NOT_FOUND') {
throw new Error('table ' + table_name + ' not found on contract ' + contractName);
}
else {
return result;
}
}
deleteContract(contractName) {
return Abieos.native.delete_contract(contractName);
}
}
1 change: 1 addition & 0 deletions deno_dist/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./abieos.ts";
Binary file added dist/abieos.so
Binary file not shown.
Loading