diff --git a/.gitignore b/.gitignore index 4fb8eb6..dc4c711 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ # Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore *.so +*.a *.dylib +build/ # Logs logs diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 0000000..9402057 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,16 @@ +{ + 'targets': [ + { + 'target_name': 'dal', + 'sources': [ + 'cgo/dal.h', + 'cgo/dal.cc' + ], + 'libraries': [ '../cgo/dal.a' ], + 'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], + "include_dirs": [ + " +#include +#include "dal.h" + +static void _InitSQLite(const Napi::CallbackInfo& args) { + Napi::Buffer buf = args[0].As>(); + char * charstr = reinterpret_cast(buf.Data()); + InitSQLite(charstr); +} + +static Napi::Value Handle(const Napi::CallbackInfo& args) { + Napi::Buffer buf = args[0].As>(); + char * charstr = reinterpret_cast(buf.Data()); + GoSlice result = HandleQuery(charstr); + return Napi::Buffer::Copy(args.Env(), reinterpret_cast(result.data), result.len); +} + + +static Napi::Object Init(Napi::Env env, Napi::Object exports) { + exports["InitSQLite"] = Napi::Function::New(env, _InitSQLite); + exports["Handle"] = Napi::Function::New(env, Handle); + return exports; +} + +NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) + diff --git a/cgo/dal.go b/cgo/dal.go index b2a9f7a..05b46e5 100644 --- a/cgo/dal.go +++ b/cgo/dal.go @@ -4,8 +4,8 @@ package main import "C" import ( "strings" - "unsafe" + _ "github.com/mattn/go-sqlite3" "l12.xyz/dal/facade" ) @@ -17,15 +17,11 @@ func InitSQLite(pragmas *C.char) { } //export HandleQuery -func HandleQuery(input *C.char) *C.char { +func HandleQuery(input *C.char) []byte { var in, out []byte - inPtr := unsafe.Pointer(input) - defer C.free(inPtr) - - in = C.GoBytes(inPtr, C.int(len(C.GoString(input)))) + in = []byte(C.GoString(input)) facade.HandleQuery(&in, &out) - output := C.CString(string(out)) - return output + return out } func main() {} diff --git a/cgo/dal.h b/cgo/dal.h index dab4483..4abe57e 100644 --- a/cgo/dal.h +++ b/cgo/dal.h @@ -79,7 +79,7 @@ extern "C" { #endif extern void InitSQLite(char* pragmas); -extern char* HandleQuery(char* input); +extern GoSlice HandleQuery(char* input); #ifdef __cplusplus } diff --git a/cgo/go.mod b/cgo/go.mod index f703f8f..c01c7e5 100644 --- a/cgo/go.mod +++ b/cgo/go.mod @@ -16,7 +16,10 @@ replace l12.xyz/dal/utils v0.0.0 => ../pkg/utils replace l12.xyz/dal/proto v0.0.0 => ../pkg/proto -require l12.xyz/dal/facade v0.0.0 +require ( + github.com/mattn/go-sqlite3 v1.14.22 + l12.xyz/dal/facade v0.0.0 +) require l12.xyz/dal/handler v0.0.0 // indirect diff --git a/dal/SQLite.ts b/dal/SQLite.ts new file mode 100644 index 0000000..29a42a1 --- /dev/null +++ b/dal/SQLite.ts @@ -0,0 +1,3 @@ +import { createRequire } from 'node:module'; +const require = createRequire(import.meta.url); +export default require('../build/Release/dal.node'); \ No newline at end of file diff --git a/dal/__test__/sqlite.node.cjs b/dal/__test__/sqlite.node.cjs new file mode 100644 index 0000000..eb31662 --- /dev/null +++ b/dal/__test__/sqlite.node.cjs @@ -0,0 +1,6 @@ +const fs = require('fs'); +const dal = require('../../build/Release/dal.node'); +dal.InitSQLite(Buffer.from([])); +const buf = fs.readFileSync('./pkg/__test__/proto_test.msgpack'); +data = dal.Handle(buf); +console.log(data); \ No newline at end of file diff --git a/package.json b/package.json index 146a99a..89a978f 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,8 @@ "type": "module", "devDependencies": { "@types/bun": "latest", + "node-addon-api": "^8.1.0", + "node-gyp": "^10.2.0", "prettier": "^3.3.3" }, "peerDependencies": { @@ -16,6 +18,8 @@ "test:client": "bun test:*", "test:dal": "bun test dal/__test__", "test:serve": "cd dal/__test__/srv && go run main.go", - "fmt": "prettier --write ." + "fmt": "prettier --write .", + "cgo:prepare":"cd cgo && go build -buildmode=c-archive -o dal.a ./dal.go", + "cgo:build": "node-gyp configure build" } } diff --git a/pkg/__test__/proto_test.go b/pkg/__test__/proto_test.go index 2a36ce8..48c7844 100644 --- a/pkg/__test__/proto_test.go +++ b/pkg/__test__/proto_test.go @@ -1,6 +1,7 @@ package tests import ( + "fmt" "os" "testing" @@ -15,6 +16,7 @@ func TestProtoMessagePack(t *testing.T) { t.Fatalf("failed to read file: %v", err) } req := proto.Request{} + fmt.Println(message) req.UnmarshalMsg(message) query, err := req.Parse(adapter.CommonDialect{}) if err != nil { diff --git a/pkg/__test__/proto_test.msgpack b/pkg/__test__/proto_test.msgpack index 322ecf4..7e385a2 100644 --- a/pkg/__test__/proto_test.msgpack +++ b/pkg/__test__/proto_test.msgpack @@ -1 +1 @@ -‚¢db¯database.sqlite¨commands’‚¦method¢In¤args‘¤data‚¦method¤Find¤args‘‚¡a¡b£$gt \ No newline at end of file +ƒ¢id ¢db¯database.sqlite¨commands’‚¦method¢In¤args‘¤data‚¦method¤Find¤args‘‚¡a¡b£$gt \ No newline at end of file diff --git a/pkg/__test__/proto_test.ts b/pkg/__test__/proto_test.ts index 671d4d1..18b347f 100644 --- a/pkg/__test__/proto_test.ts +++ b/pkg/__test__/proto_test.ts @@ -2,6 +2,7 @@ import { encode } from "https://deno.land/x/msgpack@v1.2/mod.ts"; const Query = { + id: 9, db: "database.sqlite", commands: [ { method: "In", args: ["data"] }, diff --git a/pkg/facade/SQLIteShared.go b/pkg/facade/SQLIteShared.go index 720b06b..4a1ddab 100644 --- a/pkg/facade/SQLIteShared.go +++ b/pkg/facade/SQLIteShared.go @@ -31,6 +31,7 @@ func HandleQuery(input *[]byte, output *[]byte) int { req := proto.Request{} _, err := req.UnmarshalMsg(*input) if err != nil { + log.Println(*input) log.Printf("failed to unmarshal request: %v", err) return 1 } diff --git a/pkg/facade/go.mod b/pkg/facade/go.mod index e33992d..a1cc137 100644 --- a/pkg/facade/go.mod +++ b/pkg/facade/go.mod @@ -18,8 +18,8 @@ require ( github.com/tinylib/msgp v1.2.0 // indirect l12.xyz/dal/builder v0.0.0 // indirect l12.xyz/dal/filters v0.0.0 // indirect - l12.xyz/dal/proto v0.0.0 // indirect l12.xyz/dal/utils v0.0.0 // indirect + l12.xyz/dal/proto v0.0.0 ) replace l12.xyz/dal/proto v0.0.0 => ../proto