Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
CH3CHO authored Aug 14, 2024
2 parents 3f8ef30 + d31c978 commit 97260ee
Show file tree
Hide file tree
Showing 32 changed files with 2,544 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<h1 align="center">
<img src="https://img.alicdn.com/imgextra/i2/O1CN01NwxLDd20nxfGBjxmZ_!!6000000006895-2-tps-960-290.png" alt="Higress" width="240" height="72.5">
<br>
AI Native API Gateway
AI Gateway
</h1>
<h4 align="center"> AI Native API Gateway </h4>

[![Build Status](https://github.com/alibaba/higress/actions/workflows/build-and-test.yaml/badge.svg?branch=main)](https://github.com/alibaba/higress/actions)
[![license](https://img.shields.io/github/license/alibaba/higress.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
Expand Down
53 changes: 53 additions & 0 deletions plugins/wasm-assemblyscript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## 介绍

此 SDK 用于使用 AssemblyScript 语言开发 Higress 的 Wasm 插件。

### 如何使用SDK

创建一个新的 AssemblyScript 项目。

```
npm init
npm install --save-dev assemblyscript
npx asinit .
```

在asconfig.json文件中,作为传递给asc编译器的选项之一,包含"use": "abort=abort_proc_exit"。

```
{
"options": {
"use": "abort=abort_proc_exit"
}
}
```

`"@higress/proxy-wasm-assemblyscript-sdk": "^0.0.1"``"@higress/wasm-assemblyscript": "^0.0.3"`添加到你的依赖项中,然后运行`npm install`

### 本地构建

```
npm run asbuild
```

构建结果将在`build`文件夹中。其中,`debug.wasm``release.wasm`是已编译的文件,在生产环境中建议使用`release.wasm`

注:如果需要插件带有 name section 信息需要带上`"debug": true`,编译参数解释详见[using-the-compiler](https://www.assemblyscript.org/compiler.html#using-the-compiler)

```json
"release": {
"outFile": "build/release.wasm",
"textFile": "build/release.wat",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"noAssert": false,
"debug": true
}
```

### AssemblyScript 限制

此 SDK 使用的 AssemblyScript 版本为`0.27.29`,参考[AssemblyScript Status](https://www.assemblyscript.org/status.html)该版本尚未支持闭包、异常、迭代器等特性,并且JSON,正则表达式等功能还尚未在标准库中实现,暂时需要使用社区提供的实现。

23 changes: 23 additions & 0 deletions plugins/wasm-assemblyscript/asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"targets": {
"debug": {
"outFile": "build/debug.wasm",
"textFile": "build/debug.wat",
"sourceMap": true,
"debug": true
},
"release": {
"outFile": "build/release.wasm",
"textFile": "build/release.wat",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"noAssert": false
}
},
"options": {
"bindings": "esm",
"use": "abort=abort_proc_exit"
}
}
214 changes: 214 additions & 0 deletions plugins/wasm-assemblyscript/assembly/cluster_wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import {
log,
LogLevelValues,
get_property,
WasmResultValues,
} from "@higress/proxy-wasm-assemblyscript-sdk/assembly";
import { getRequestHost } from "./request_wrapper";

export abstract class Cluster {
abstract clusterName(): string;
abstract hostName(): string;
}

export class RouteCluster extends Cluster {
host: string;
constructor(host: string = "") {
super();
this.host = host;
}

clusterName(): string {
let result = get_property("cluster_name");
if (result.status != WasmResultValues.Ok) {
log(LogLevelValues.error, "get route cluster failed");
return "";
}
return String.UTF8.decode(result.returnValue);
}

hostName(): string {
if (this.host != "") {
return this.host;
}
return getRequestHost();
}
}

export class K8sCluster extends Cluster {
serviceName: string;
namespace: string;
port: i64;
version: string;
host: string;

constructor(
serviceName: string,
namespace: string,
port: i64,
version: string = "",
host: string = ""
) {
super();
this.serviceName = serviceName;
this.namespace = namespace;
this.port = port;
this.version = version;
this.host = host;
}

clusterName(): string {
let namespace = this.namespace != "" ? this.namespace : "default";
return `outbound|${this.port}|${this.version}|${this.serviceName}.${namespace}.svc.cluster.local`;
}

hostName(): string {
if (this.host != "") {
return this.host;
}
return `${this.serviceName}.${this.namespace}.svc.cluster.local`;
}
}

export class NacosCluster extends Cluster {
serviceName: string;
group: string;
namespaceID: string;
port: i64;
isExtRegistry: boolean;
version: string;
host: string;

constructor(
serviceName: string,
namespaceID: string,
port: i64,
// use DEFAULT-GROUP by default
group: string = "DEFAULT-GROUP",
// set true if use edas/sae registry
isExtRegistry: boolean = false,
version: string = "",
host: string = ""
) {
super();
this.serviceName = serviceName;
this.group = group.replace("_", "-");
this.namespaceID = namespaceID;
this.port = port;
this.isExtRegistry = isExtRegistry;
this.version = version;
this.host = host;
}

clusterName(): string {
let tail = "nacos" + (this.isExtRegistry ? "-ext" : "");
return `outbound|${this.port}|${this.version}|${this.serviceName}.${this.group}.${this.namespaceID}.${tail}`;
}

hostName(): string {
if (this.host != "") {
return this.host;
}
return this.serviceName;
}
}

export class StaticIpCluster extends Cluster {
serviceName: string;
port: i64;
host: string;

constructor(serviceName: string, port: i64, host: string = "") {
super()
this.serviceName = serviceName;
this.port = port;
this.host = host;
}

clusterName(): string {
return `outbound|${this.port}||${this.serviceName}.static`;
}

hostName(): string {
if (this.host != "") {
return this.host;
}
return this.serviceName;
}
}

export class DnsCluster extends Cluster {
serviceName: string;
domain: string;
port: i64;

constructor(serviceName: string, domain: string, port: i64) {
super();
this.serviceName = serviceName;
this.domain = domain;
this.port = port;
}

clusterName(): string {
return `outbound|${this.port}||${this.serviceName}.dns`;
}

hostName(): string {
return this.domain;
}
}

export class ConsulCluster extends Cluster {
serviceName: string;
datacenter: string;
port: i64;
host: string;

constructor(
serviceName: string,
datacenter: string,
port: i64,
host: string = ""
) {
super();
this.serviceName = serviceName;
this.datacenter = datacenter;
this.port = port;
this.host = host;
}

clusterName(): string {
return `outbound|${this.port}||${this.serviceName}.${this.datacenter}.consul`;
}

hostName(): string {
if (this.host != "") {
return this.host;
}
return this.serviceName;
}
}

export class FQDNCluster extends Cluster {
fqdn: string;
host: string;
port: i64;

constructor(fqdn: string, port: i64, host: string = "") {
super();
this.fqdn = fqdn;
this.host = host;
this.port = port;
}

clusterName(): string {
return `outbound|${this.port}||${this.fqdn}`;
}

hostName(): string {
if (this.host != "") {
return this.host;
}
return this.fqdn;
}
}
Loading

0 comments on commit 97260ee

Please sign in to comment.