Skip to content

Commit

Permalink
📦 updates HDK for v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed May 8, 2023
1 parent 7105616 commit a425f0b
Show file tree
Hide file tree
Showing 27 changed files with 646 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ host
simple-host
simple-hello-host
simple-talk-host
cracke/cracker
*.wasm
site
3 changes: 2 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ tasks:
release:
env:
#TAG: "v0.0.1"
TAG: "v0.0.2" # next release
TAG: "v0.0.2"
#TAG: "v0.0.3" # next release
cmds:
- echo "📦 Generating release..."
- git add .
Expand Down
53 changes: 30 additions & 23 deletions capsule.dk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,59 @@ import (
const isFailure = rune('F')
const isSuccess = rune('S')

/*
func main() {
panic("not implemented")
}
func success(buffer []byte) uint64 {
return copyBufferToMemory(append([]byte(string(isSuccess)), buffer...))
}
func failure(buffer []byte) uint64 {
return copyBufferToMemory(append([]byte(string(isFailure)), buffer...))
}
*/

// success appends the isSuccess byte to the beginning of the input buffer and returns the result.
//
// buffer: byte slice to append isSuccess byte to.
// []byte: byte slice with the appended isSuccess byte.
func success(buffer []byte) []byte {
return append([]byte(string(isSuccess)), buffer...)
}

// failure appends a string "isFailure" to the given byte slice buffer and returns the new slice.
//
// buffer: the byte slice to which "isFailure" is appended.
// Returns the new byte slice with the string "isFailure" appended to it.
func failure(buffer []byte) []byte {
return append([]byte(string(isFailure)), buffer...)
}




// Result function
// Result returns the data without the first byte if the first byte is isSuccess.
// Otherwise, it returns nil and an error with the data starting from the second byte.
//
// data: A byte slice containing the data to check.
// []byte: The data without the first byte if the first byte is isSuccess.
// error: If the first byte is not isSuccess, it returns an error with the data starting from the second byte.
func Result(data []byte,) ([]byte, error) {
if data[0] == byte(isSuccess) {
return data[1:], nil
}
return nil, errors.New(string(data[1:]))
}

// GetHandle returns the handle function
// GetHandle returns an exported function named "callHandle" from the given module.
//
// mod: The module to retrieve the function from.
//
// Returns: An exported function with the name "callHandle".
func GetHandle(mod api.Module) api.Function {
return mod.ExportedFunction("callHandle")
}

// GetHandleJSON returns the handle function
// GetHandleJSON returns the exported "callHandleJSON" function from the given module.
//
// mod: the module to retrieve the function from.
//
// returns: the exported "callHandleJSON" function.
func GetHandleJSON(mod api.Module) api.Function {
return mod.ExportedFunction("callHandleJSON")
}

// GetHandleHTTP returns the handle function
// GetHandleHTTP returns the exported 'callHandleHTTP' function from a given module.
//
// mod: The module containing the exported function.
//
// returns:
// - api.Function: the exported 'callHandleHTTP' function.
func GetHandleHTTP(mod api.Module) api.Function {
return mod.ExportedFunction("callHandleHTTP")
}

// TODO: handle the other handles
17 changes: 0 additions & 17 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,3 @@
## Create a Capsule application

Create a directory `say-hello`

```bash
mkdir say-hello
cd say-hello
```

Initialize a new project in `say-hello`:

```bash
go mod init say-hello
```

Install the Capsule **HDK** dependencies:
```bash
go get github.com/bots-garden/capsule-host-sdk
```
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Capsule Host SDK

!!! info "What's new?"
- `v0.0.2`: ✨ Redis support
- `v0.0.1`: 🎉 first release

## What is the Capsule Host SDK alias **Capsule HDK**?
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ require (
github.com/tetratelabs/wazero v1.1.0
)

require golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect
require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/redis/go-redis/v9 v9.0.4 // indirect
golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
github.com/redis/go-redis/v9 v9.0.4 h1:FC82T+CHJ/Q/PdyLW++GeCO+Ol59Y4T7R4jbgjvktgc=
github.com/redis/go-redis/v9 v9.0.4/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/tetratelabs/wazero v1.1.0 h1:EByoAhC+QcYpwSZJSs/aV0uokxPwBgKxfiokSUwAknQ=
github.com/tetratelabs/wazero v1.1.0/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM=
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use (
./samples/simple
./samples/simple-hello
./samples/simple-talk
./samples/cracker
)
8 changes: 6 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/redis/go-redis/v9 v9.0.4 h1:FC82T+CHJ/Q/PdyLW++GeCO+Ol59Y4T7R4jbgjvktgc=
github.com/redis/go-redis/v9 v9.0.4/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/tetratelabs/wazero v1.0.1 h1:xyWBoGyMjYekG3mEQ/W7xm9E05S89kJ/at696d/9yuc=
github.com/tetratelabs/wazero v1.0.1/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ=
github.com/tetratelabs/wazero v1.1.0 h1:EByoAhC+QcYpwSZJSs/aV0uokxPwBgKxfiokSUwAknQ=
github.com/tetratelabs/wazero v1.1.0/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
8 changes: 6 additions & 2 deletions hostfunc.envvar.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package capsule

// this hostfunction is a template for the other host functions
import (
"context"
"log"
Expand All @@ -10,7 +9,12 @@ import (
"github.com/tetratelabs/wazero/api"
)

// DefineHostFuncGetEnv defines a host function allowing the wasm guest to get an environment variable by name
// DefineHostFuncGetEnv defines a new host function to get the environment variable value.
//
// Parameters:
// - builder: the HostModuleBuilder to add the function to.
//
// Returns: nothing.
func DefineHostFuncGetEnv(builder wazero.HostModuleBuilder) {
builder.NewFunctionBuilder().
WithGoModuleFunction(getEnv,
Expand Down
14 changes: 8 additions & 6 deletions hostfunc.fileread.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package capsule

// this hostfunction is a template for the other host functions
import (
"context"
"log"
Expand All @@ -10,7 +9,14 @@ import (
"github.com/tetratelabs/wazero/api"
)

// DefineHostFuncReadFile defines a host function
// DefineHostFuncReadFile defines a function that reads a file from the host file system
// and returns its content as a string. The function takes in four parameters:
// - filePath: the pointer to the string representing the file path
// - filePathLen: the length of the file path string
// - returned: a pointer to the string where the file content will be stored
// - returnedLen: the length of the returned string
//
// The function returns an integer representing whether the operation was successful.
func DefineHostFuncReadFile(builder wazero.HostModuleBuilder) {
builder.NewFunctionBuilder().
WithGoModuleFunction(readFile,
Expand Down Expand Up @@ -57,7 +63,3 @@ var readFile = api.GoModuleFunc(func(ctx context.Context, module api.Module, par
params[0] = 0

})

/* Documentation:
*/
16 changes: 9 additions & 7 deletions hostfunc.filewrite.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package capsule

// this hostfunction is a template for the other host functions
import (
"context"
"log"
Expand All @@ -10,7 +9,15 @@ import (
"github.com/tetratelabs/wazero/api"
)

// DefineHostFuncWriteFile defines a host function
// DefineHostFuncWriteFile creates a new function called hostWriteFile in the
// HostModuleBuilder. It accepts the following parameters:
// - filePath (int32): position
// - filePath (int32): length
// - content (int32): position
// - content (int32): length
// - returned (int32): position
// - returned (int32): length
// The function returns an int32.
func DefineHostFuncWriteFile(builder wazero.HostModuleBuilder) {
builder.NewFunctionBuilder().
WithGoModuleFunction(writeFile,
Expand Down Expand Up @@ -66,8 +73,3 @@ var writeFile = api.GoModuleFunc(func(ctx context.Context, module api.Module, pa
params[0] = 0

})

/* Documentation:
! concurrent are not managed
? don't use it with the capsule-http runner
*/
16 changes: 11 additions & 5 deletions hostfunc.http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ type request struct {
Headers map[string]string `json:"Headers"`
}

// DefineHostFuncHTTP defines a host function
// DefineHostFuncHTTP defines the host module function for handling HTTP requests.
//
// Parameter(s):
// builder: the wazero.HostModuleBuilder used to define the function.
//
// Return(s):
// None.
func DefineHostFuncHTTP(builder wazero.HostModuleBuilder) {
builder.NewFunctionBuilder().
WithGoModuleFunction(http,
Expand Down Expand Up @@ -170,6 +176,10 @@ var http = api.GoModuleFunc(func(ctx context.Context, module api.Module, params

})

// buildResponseJSONString takes a resty.Response pointer as input and returns a JSON string and an error.
// The function builds a JSON string of the response headers and response body. If the response is in JSON format,
// the function includes the JSON body in the JSON string; otherwise, the function includes the text body in the JSON
// string with double quotes. The function also includes the status code in the JSON string.
func buildResponseJSONString(resp *resty.Response) (string, error) {
// build headers JSON string
// ! ATTENTION resp.Header() return a map[string]string[] (instead of map[string]string)
Expand Down Expand Up @@ -205,7 +215,3 @@ func buildResponseJSONString(resp *resty.Response) (string, error) {

return jsonHTTPResponse, err
}

/* Documentation:
*/
7 changes: 5 additions & 2 deletions hostfunc.log.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ var logString = api.GoModuleFunc(func(ctx context.Context, module api.Module, pa
params[0] = 0 // return 0
})


// DefineHostFuncLog defines a host function
// DefineHostFuncLog defines and exports a host module function called hostLogString.
// This function takes two parameters:
// - string position (i32)
// - string length (i32)
// It returns an i32 value.
func DefineHostFuncLog(builder wazero.HostModuleBuilder) {
// hostLogString
builder.NewFunctionBuilder().
Expand Down
31 changes: 23 additions & 8 deletions hostfunc.memorycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ import (

var memCache sync.Map

// DefineHostFuncCacheSet defines a host function
// DefineHostFuncCacheSet defines a new Go module function for setting values in
// the cache. It takes in 6 parameters:
// - key position (int32)
// - key length (int32)
// - value string position (int32)
// - value string length (int32)
// - returned position (int32)
// - returned length (int32)
// It returns an int32 value.
func DefineHostFuncCacheSet(builder wazero.HostModuleBuilder) {
builder.NewFunctionBuilder().
WithGoModuleFunction(cacheSet,
Expand Down Expand Up @@ -83,7 +91,11 @@ var cacheSet = api.GoModuleFunc(func(ctx context.Context, module api.Module, par
})


// DefineHostFuncCacheGet defines a host function
// DefineHostFuncCacheGet defines the Go function that calls the cacheGet function
// to get the value of a given key. The function takes in four parameters: the
// position of the key, the length of the key, the position of the returned value,
// and the length of the returned value. It returns an integer that represents
// the success or failure of the function call.
func DefineHostFuncCacheGet(builder wazero.HostModuleBuilder) {
builder.NewFunctionBuilder().
WithGoModuleFunction(cacheGet,
Expand Down Expand Up @@ -136,8 +148,12 @@ var cacheGet = api.GoModuleFunc(func(ctx context.Context, module api.Module, par

})


// DefineHostFuncCacheDel defines a host function
// DefineHostFuncCacheDel defines a Go function that deletes a cache entry.
//
// Parameters:
// - builder: a wazero.HostModuleBuilder object.
//
// Returns: nothing.
func DefineHostFuncCacheDel(builder wazero.HostModuleBuilder) {
builder.NewFunctionBuilder().
WithGoModuleFunction(cacheDel,
Expand Down Expand Up @@ -186,10 +202,9 @@ var cacheDel = api.GoModuleFunc(func(ctx context.Context, module api.Module, par

})




// DefineHostFuncCacheKeys defines a host function
// DefineHostFuncCacheKeys defines the host function hostCacheKeys which takes in
// filter position, filter length, returned position, and returned length as
// parameters of type i32 and returns an i32.
func DefineHostFuncCacheKeys(builder wazero.HostModuleBuilder) {
builder.NewFunctionBuilder().
WithGoModuleFunction(cacheKeys,
Expand Down
8 changes: 7 additions & 1 deletion hostfunc.print.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ var printString = api.GoModuleFunc(func(ctx context.Context, module api.Module,
})


// DefineHostFuncPrint defines a host function
// DefineHostFuncPrint defines the hostPrintString function which takes in a
// string position and string length as parameters, and returns an integer.
//
// builder: The HostModuleBuilder object.
//
// Returns: None.
func DefineHostFuncPrint(builder wazero.HostModuleBuilder) {
// hostPrintString
builder.NewFunctionBuilder().
Expand All @@ -40,3 +45,4 @@ func DefineHostFuncPrint(builder wazero.HostModuleBuilder) {
[]api.ValueType{api.ValueTypeI32}).
Export("hostPrintString")
}
// DefineHostFuncPrint defines a host function
Loading

0 comments on commit a425f0b

Please sign in to comment.