mirror of
https://github.com/fluencelabs/db-connector
synced 2025-04-24 20:32:13 +00:00
init
This commit is contained in:
commit
12e2fc6a2d
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
.idea
|
||||
build
|
||||
|
||||
!node_modules/
|
||||
|
||||
node_modules/*
|
||||
!node_modules/crossmodule/
|
||||
|
||||
node_modules/crossmodule/*
|
||||
!node_modules/crossmodule/assembly
|
12
assembly/database.ts
Normal file
12
assembly/database.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import {StringInvoke, API} from "../node_modules/crossmodule/assembly/index"
|
||||
|
||||
export declare function allocate(size: usize): i32;
|
||||
export declare function deallocate(ptr: i32, size: usize): void;
|
||||
export declare function invoke(ptr: i32, size: usize): i32;
|
||||
export declare function store(ptr: usize, byte: u8): void;
|
||||
export declare function load(ptr: usize): u8;
|
||||
|
||||
export function getStringInvoker(): StringInvoke {
|
||||
let api = new API(invoke, allocate, deallocate, store, load);
|
||||
return new StringInvoke(api);
|
||||
}
|
7
assembly/index.ts
Normal file
7
assembly/index.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import {getStringInvoker} from "./database";
|
||||
|
||||
export function query(request: string): string {
|
||||
let stringInvoker = getStringInvoker();
|
||||
let result = stringInvoker.invoke(request);
|
||||
return result;
|
||||
}
|
6
assembly/tsconfig.json
Normal file
6
assembly/tsconfig.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "../node_modules/assemblyscript/std/assembly.json",
|
||||
"include": [
|
||||
"./**/*.ts"
|
||||
]
|
||||
}
|
109
node_modules/crossmodule/assembly/index.ts
generated
vendored
Normal file
109
node_modules/crossmodule/assembly/index.ts
generated
vendored
Normal file
@ -0,0 +1,109 @@
|
||||
export class API {
|
||||
|
||||
invokeImpl: (ptr: i32, size: i32) => i32;
|
||||
allocateImpl: (size: i32) => i32;
|
||||
deallocateImpl: (ptr: i32, size: i32) => void;
|
||||
storeImpl: (ptr: i32, byte: u8) => void;
|
||||
loadImpl: (ptr: i32) => u8;
|
||||
|
||||
constructor(
|
||||
invokeImpl: (ptr: i32, size: i32) => i32,
|
||||
allocateImpl: (size: i32) => i32,
|
||||
deallocateImpl: (ptr: i32, size: i32) => void,
|
||||
storeImpl: (ptr: i32, byte: u8) => void,
|
||||
loadImpl: (ptr: i32) => u8
|
||||
) {
|
||||
this.invokeImpl = invokeImpl;
|
||||
this.allocateImpl = allocateImpl;
|
||||
this.deallocateImpl = deallocateImpl;
|
||||
this.storeImpl = storeImpl;
|
||||
this.loadImpl = loadImpl;
|
||||
}
|
||||
|
||||
invoke(ptr: i32, size: i32): i32 {
|
||||
return this.invokeImpl(ptr, size);
|
||||
}
|
||||
|
||||
allocate(size: i32) :i32 {
|
||||
return this.allocateImpl(size);
|
||||
}
|
||||
|
||||
deallocate(ptr: i32, size: i32): void {
|
||||
this.deallocateImpl(size, size);
|
||||
}
|
||||
|
||||
store(ptr: i32, byte: u8): void {
|
||||
this.storeImpl(ptr, byte);
|
||||
}
|
||||
|
||||
load(ptr: i32): u8 {
|
||||
return this.loadImpl(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
export class ByteInvoke {
|
||||
|
||||
api: API;
|
||||
|
||||
constructor(api: API) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
getBytes(remotePtr: i32): Uint8Array {
|
||||
let lenBytes: u8[] = new Array(4);
|
||||
for (let i = 0; i < 4; i++) {
|
||||
lenBytes[i] = this.api.load(remotePtr + i);
|
||||
}
|
||||
|
||||
let resultLen: i32 = 0;
|
||||
|
||||
for (let i = 0; i < 4; i++) {
|
||||
resultLen = resultLen | (lenBytes[i] << (8*(i - 4) as u8))
|
||||
}
|
||||
|
||||
let resultBytes = new Uint8Array(resultLen);
|
||||
|
||||
for (let i = 0; i < resultLen; i++) {
|
||||
resultBytes[i] = this.api.load(remotePtr + i + 4);
|
||||
}
|
||||
|
||||
this.api.deallocate(remotePtr, resultLen + 4);
|
||||
|
||||
return resultBytes;
|
||||
}
|
||||
|
||||
sendBytes(localPtr: i32, len: i32): i32 {
|
||||
let addr = this.api.allocate(len);
|
||||
|
||||
for (let i = 0; i < len; i++) {
|
||||
let b: u8 = load<u8>(localPtr + i) as u8;
|
||||
this.api.store(addr + i, b);
|
||||
}
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
invoke(ptr: i32, len: i32): Uint8Array {
|
||||
let requestPtr = this.sendBytes(ptr, len);
|
||||
let resultPtr = this.api.invokeImpl(requestPtr, len);
|
||||
return this.getBytes(resultPtr);
|
||||
}
|
||||
}
|
||||
|
||||
export class StringInvoke {
|
||||
|
||||
byteInvoker: ByteInvoke;
|
||||
|
||||
constructor(api: API) {
|
||||
this.byteInvoker = new ByteInvoke(api);
|
||||
}
|
||||
|
||||
invoke(request: string): string {
|
||||
let utf8ptr = request.toUTF8();
|
||||
let len = request.length;
|
||||
|
||||
let resultBytes = this.byteInvoker.invoke(utf8ptr, len);
|
||||
|
||||
return String.fromUTF8(resultBytes.buffer.data, resultBytes.length);
|
||||
}
|
||||
}
|
4691
package-lock.json
generated
Normal file
4691
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
34
package.json
Normal file
34
package.json
Normal file
@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "db-connector",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug",
|
||||
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --validate --optimize"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/node": "^10.12.3",
|
||||
"assemblyscript": "AssemblyScript/assemblyscript",
|
||||
"ts-node": "^7.0.1",
|
||||
"ava": "^1.4.0",
|
||||
"typescript": "^3.1.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"crossmodule": "github:fluencelabs/crossmodule"
|
||||
},
|
||||
"ava": {
|
||||
"compileEnhancements": true,
|
||||
"extensions": [
|
||||
"ts"
|
||||
],
|
||||
"require": [
|
||||
"ts-node/register/transpile-only"
|
||||
],
|
||||
"files": [
|
||||
"tests/**/*.spec.ts"
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user