http-wasm-host-go

module
v0.0.0-...-767c95f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2023 License: AGPL-3.0

README

Build Go Report Card License

http-wasm Host Library for Go

http-wasm defines HTTP functions implemented in WebAssembly. This repository includes http_handler ABI middleware for various HTTP server libraries written in Go.

WARNING: This is an early draft

The current maturity phase is early draft. Once this is integrated with coraza and dapr, we can begin discussions about compatability.

个人理解

wasm/host处理流程

  • handler.Middleware作为包装器
type middleware struct {
	host            handler.Host // host实现
	runtime         wazero.Runtime // 运行时
	guestModule     wazero.CompiledModule //编译的模式
	moduleConfig    wazero.ModuleConfig //模块配置
	guestConfig     []byte // wasm二进制文件
	logger          api.Logger
	pool            sync.Pool
	features        handler.Features
	instanceCounter uint64
}
  • handler.Host接口增加函数
	// GetTemplate supports the WebAssembly function export FuncGetTemplate.
	GetTemplate(ctx context.Context) string

	// SetTemplate supports the WebAssembly function export FuncSetTemplate.
	SetTemplate(ctx context.Context, template string)
  • 默认空实现
func (UnimplementedHost) GetTemplate(context.Context) string { return "" }
func (UnimplementedHost) SetTemplate(context.Context, string) 
  • handler.middleware.go#692文件中,声明导入函数,这样在guest/wasm中就能调用了,都是通过指针地址进行数据交换的.
    //getTemplate
	NewFunctionBuilder().
	WithGoModuleFunction(wazeroapi.GoModuleFunc(m.getTemplate), []wazeroapi.ValueType{i32, i32}, []wazeroapi.ValueType{i32}).
	WithParameterNames("buf", "buf_limit").Export(handler.FuncGetTemplate).
	NewFunctionBuilder().
	//setTemplate
	WithGoModuleFunction(wazeroapi.GoModuleFunc(m.setTemplate), []wazeroapi.ValueType{i32, i32}, []wazeroapi.ValueType{}).
	WithParameterNames("template", "template_len").Export(handler.FuncSetTemplate).

Directories

Path Synopsis
api
internal
testing
handlertest
Package handlertest implements support for testing implementations of HTTP handlers.
Package handlertest implements support for testing implementations of HTTP handlers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL