@higress/wasm-assemblyscript
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,9 +0,18 @@ | ||
export {RouteCluster, K8sCluster, NacosCluster, ConsulCluster, FQDNCluster, StaticIpCluster} from "./cluster_wrapper" | ||
export {HttpClient, ClusterClient} from "./http_wrapper" | ||
export {RouteCluster, | ||
K8sCluster, | ||
NacosCluster, | ||
ConsulCluster, | ||
FQDNCluster, | ||
StaticIpCluster} from "./cluster_wrapper" | ||
export {HttpClient, | ||
ClusterClient} from "./http_wrapper" | ||
export {Log} from "./log_wrapper" | ||
export {SetCtx, HttpContext, ParseConfigBy, ProcessRequestBodyBy, ProcessRequestHeadersBy, ProcessResponseBodyBy, ProcessResponseHeadersBy, logger} from "./plugin_wrapper" | ||
export {SetCtx, | ||
HttpContext, | ||
ParseConfigBy, | ||
ProcessRequestBodyBy, | ||
ProcessRequestHeadersBy, | ||
ProcessResponseBodyBy, | ||
ProcessResponseHeadersBy, | ||
Logger, RegisteTickFunc} from "./plugin_wrapper" | ||
export {ParseResult} from "./rule_matcher" |
@@ -13,2 +13,4 @@ import { Log } from "./log_wrapper"; | ||
BufferTypeValues, | ||
set_tick_period_milliseconds, | ||
get_current_time_nanoseconds | ||
} from "@higress/proxy-wasm-assemblyscript-sdk/assembly"; | ||
@@ -58,3 +60,3 @@ import { | ||
export var logger: Log = new Log(""); | ||
export var Logger: Log = new Log(""); | ||
@@ -70,2 +72,3 @@ class CommonRootCtx<PluginConfig> extends RootContext { | ||
onHttpResponseBody: OnHttpBodyFunc<PluginConfig> | null; | ||
onTickFuncs: Array<TickFuncEntry>; | ||
@@ -75,3 +78,3 @@ constructor(context_id: u32, pluginName: string, setFuncs: usize[]) { | ||
this.pluginName = pluginName; | ||
logger = new Log(this.pluginName); | ||
Logger = new Log(this.pluginName); | ||
this.hasCustomConfig = true; | ||
@@ -84,2 +87,3 @@ this.onHttpRequestHeaders = null; | ||
this.ruleMatcher = new RuleMatcher<PluginConfig>(); | ||
this.onTickFuncs = new Array<TickFuncEntry>(); | ||
for (let i = 0; i < setFuncs.length; i++) { | ||
@@ -103,3 +107,2 @@ changetype<Closure<PluginConfig>>(setFuncs[i]).lambdaFn( | ||
const data = this.getConfiguration(); | ||
let jsonData: JSON.Obj; | ||
@@ -126,6 +129,48 @@ if (data.length == 0) { | ||
} | ||
return this.ruleMatcher.parseRuleConfig(jsonData, this.parseConfig as ParseConfigFunc<PluginConfig>); | ||
if (!this.ruleMatcher.parseRuleConfig(jsonData, this.parseConfig as ParseConfigFunc<PluginConfig>)) { | ||
return false; | ||
} | ||
if (globalOnTickFuncs.length > 0) { | ||
this.onTickFuncs = globalOnTickFuncs; | ||
set_tick_period_milliseconds(100); | ||
} | ||
return true; | ||
} | ||
onTick(): void { | ||
for (let i = 0; i < this.onTickFuncs.length; i++) { | ||
const tickFuncEntry = this.onTickFuncs[i]; | ||
const now = getCurrentTimeMilliseconds(); | ||
if (tickFuncEntry.lastExecuted + tickFuncEntry.tickPeriod <= now) { | ||
tickFuncEntry.tickFunc(); | ||
tickFuncEntry.lastExecuted = getCurrentTimeMilliseconds(); | ||
} | ||
} | ||
} | ||
} | ||
function getCurrentTimeMilliseconds(): u64 { | ||
return get_current_time_nanoseconds() / 1000000; | ||
} | ||
class TickFuncEntry { | ||
lastExecuted: u64; | ||
tickPeriod: u64; | ||
tickFunc: () => void; | ||
constructor(lastExecuted: u64, tickPeriod: u64, tickFunc: () => void) { | ||
this.lastExecuted = lastExecuted; | ||
this.tickPeriod = tickPeriod; | ||
this.tickFunc = tickFunc; | ||
} | ||
} | ||
var globalOnTickFuncs = new Array<TickFuncEntry>(); | ||
export function RegisteTickFunc(tickPeriod: i64, tickFunc: () => void): void { | ||
globalOnTickFuncs.push(new TickFuncEntry(0, tickPeriod, tickFunc)); | ||
} | ||
class Closure<PluginConfig> { | ||
@@ -132,0 +177,0 @@ lambdaFn: (closure: usize, ctx: CommonRootCtx<PluginConfig>) => void; |
{ | ||
"name": "@higress/wasm-assemblyscript", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "assembly/index.ts", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -33,2 +33,2 @@ ## 介绍 | ||
构建结果将在“build”文件夹中。其中,`debug.wasm`和`release.wasm`是已编译的文件。 | ||
构建结果将在`build`文件夹中。其中,`debug.wasm`和`release.wasm`是已编译的文件。 |
46564
1045