@gcoredev/proxy-wasm-sdk-as 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -26
- package/assembly/fastedge.ts +66 -0
- package/assembly/imports.ts +24 -0
- package/assembly/runtime.ts +8 -3
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# WebAssembly for Proxies (AssemblyScript SDK)
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
[](https://github.com/G-Core/proxy-wasm-sdk-as)
|
|
5
4
|
[](https://github.com/G-Core/proxy-wasm-sdk-as)
|
|
6
5
|
[](https://github.com/G-Core/proxy-wasm-sdk-as)
|
|
@@ -8,8 +7,9 @@
|
|
|
8
7
|
[](https://www.npmjs.com/package/@gcoredev/proxy-wasm-sdk-as)
|
|
9
8
|
|
|
10
9
|
This is a friendly fork of https://github.com/Kong/proxy-wasm-assemblyscript-sdk/,
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
mantained to address an incompatibility between the AssemblyScript SDK and the Rust SDK,
|
|
11
|
+
|
|
12
|
+
It also adds some FastEdge specific functionality.
|
|
13
13
|
|
|
14
14
|
## How to use this SDK
|
|
15
15
|
|
|
@@ -32,21 +32,21 @@ the options passed to `asc` compiler:
|
|
|
32
32
|
}
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
Add `"@gcoredev/proxy-wasm-sdk-as": "
|
|
35
|
+
Add `"@gcoredev/proxy-wasm-sdk-as": "^1.0.2"` to your dependencies then run `npm install`.
|
|
36
36
|
|
|
37
37
|
## Hello, World
|
|
38
38
|
|
|
39
39
|
Copy this into assembly/index.ts:
|
|
40
40
|
|
|
41
41
|
```ts
|
|
42
|
-
export * from "@gcoredev/proxy-wasm-sdk-as/proxy";
|
|
42
|
+
export * from "@gcoredev/proxy-wasm-sdk-as/assembly/proxy";
|
|
43
43
|
import {
|
|
44
44
|
RootContext,
|
|
45
45
|
Context,
|
|
46
46
|
registerRootContext,
|
|
47
47
|
FilterHeadersStatusValues,
|
|
48
48
|
stream_context,
|
|
49
|
-
} from "@gcoredev/proxy-wasm-sdk-as";
|
|
49
|
+
} from "@gcoredev/proxy-wasm-sdk-as/assembly";
|
|
50
50
|
|
|
51
51
|
class AddHeaderRoot extends RootContext {
|
|
52
52
|
createContext(context_id: u32): Context {
|
|
@@ -77,7 +77,7 @@ registerRootContext((context_id: u32) => {
|
|
|
77
77
|
}, "add_header");
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
-
##
|
|
80
|
+
## Build
|
|
81
81
|
|
|
82
82
|
To build, simply run:
|
|
83
83
|
|
|
@@ -85,25 +85,17 @@ To build, simply run:
|
|
|
85
85
|
npm run asbuild
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
-
build results will be in the build folder. `
|
|
89
|
-
file that we will use (you only need one of them, if unsure use `
|
|
88
|
+
build results will be in the build folder. `debug.wasm` and `release.wasm` are the compiled
|
|
89
|
+
file that we will use (you only need one of them, if unsure use `release.wasm`).
|
|
90
90
|
|
|
91
91
|
## Run
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
vm_config:
|
|
103
|
-
vm_id: "my_vm_id"
|
|
104
|
-
runtime: "envoy.wasm.runtime.v8"
|
|
105
|
-
code:
|
|
106
|
-
local:
|
|
107
|
-
filename: /PATH/TO/CODE/build/optimized.wasm
|
|
108
|
-
allow_precompiled: false
|
|
109
|
-
```
|
|
93
|
+
These binaries can then be uploaded and attached to your CDN applications within the FastEdge UI portal.
|
|
94
|
+
|
|
95
|
+
For some binaries (the above example for instance) you can test localy using envoy.
|
|
96
|
+
|
|
97
|
+
Please see [Envoy.md](./ENVOY.md)
|
|
98
|
+
|
|
99
|
+
## Examples
|
|
100
|
+
|
|
101
|
+
For more examples on how to use this `proxy-wasm-sdk-as` please see our [examples repo](https://github.com/G-Core/FastEdge-examples/tree/main/assemblyscript)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import * as imports from "./imports";
|
|
2
|
+
|
|
3
|
+
import { globalArrayBufferReference, LogLevelValues } from "./runtime";
|
|
4
|
+
|
|
5
|
+
let logLevel: LogLevelValues = LogLevelValues.info;
|
|
6
|
+
|
|
7
|
+
export function setLogLevel(level: LogLevelValues): void {
|
|
8
|
+
logLevel = level;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function log(level: LogLevelValues, logMessage: string): void {
|
|
12
|
+
// Temporary fix for proxy_log not being implemented in fastedge:
|
|
13
|
+
// relies on @assemblyscript/wasi-shim to print to standard output
|
|
14
|
+
if (level >= logLevel) {
|
|
15
|
+
process.stdout.write(logMessage + "\n");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getEnvVar(key: string): string {
|
|
20
|
+
const hasKey = process.env.has(key);
|
|
21
|
+
if (hasKey) {
|
|
22
|
+
return process.env.get(key);
|
|
23
|
+
}
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function getSecretVar(key: string): string {
|
|
28
|
+
const buffer = String.UTF8.encode(key);
|
|
29
|
+
const status = imports.proxy_get_secret(
|
|
30
|
+
changetype<usize>(buffer),
|
|
31
|
+
buffer.byteLength,
|
|
32
|
+
globalArrayBufferReference.bufferPtr(),
|
|
33
|
+
globalArrayBufferReference.sizePtr()
|
|
34
|
+
);
|
|
35
|
+
if (status != 0) {
|
|
36
|
+
// Something went wrong - returns 0 with an empty ArrayBuffer if not found
|
|
37
|
+
return "";
|
|
38
|
+
}
|
|
39
|
+
const arrBuff = globalArrayBufferReference.toArrayBuffer();
|
|
40
|
+
if (arrBuff.byteLength == 0) {
|
|
41
|
+
return ""; // Not found
|
|
42
|
+
}
|
|
43
|
+
return String.UTF8.decode(arrBuff);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function getSecretVarEffectiveAt(key: string, at: u32): string {
|
|
47
|
+
const buffer = String.UTF8.encode(key);
|
|
48
|
+
const status = imports.proxy_get_effective_at_secret(
|
|
49
|
+
changetype<usize>(buffer),
|
|
50
|
+
buffer.byteLength,
|
|
51
|
+
at,
|
|
52
|
+
globalArrayBufferReference.bufferPtr(),
|
|
53
|
+
globalArrayBufferReference.sizePtr()
|
|
54
|
+
);
|
|
55
|
+
if (status != 0) {
|
|
56
|
+
// Something went wrong - returns 0 with an empty ArrayBuffer if not found
|
|
57
|
+
return "";
|
|
58
|
+
}
|
|
59
|
+
const arrBuff = globalArrayBufferReference.toArrayBuffer();
|
|
60
|
+
if (arrBuff.byteLength == 0) {
|
|
61
|
+
return ""; // Not found
|
|
62
|
+
}
|
|
63
|
+
return String.UTF8.decode(arrBuff);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { LogLevelValues };
|
package/assembly/imports.ts
CHANGED
|
@@ -173,3 +173,27 @@ export declare function proxy_call_foreign_function(function_name: ptr<char>,
|
|
|
173
173
|
name_size: size_t, arguments: ptr<char>,
|
|
174
174
|
arguments_size: size_t, results: ptr<ptr<char>>,
|
|
175
175
|
results_size: ptr<size_t>): WasmResult;
|
|
176
|
+
|
|
177
|
+
// FastEdge HOST Apis
|
|
178
|
+
|
|
179
|
+
// Secrets
|
|
180
|
+
// @ts-ignore: decorator
|
|
181
|
+
@external("env", "proxy_get_secret")
|
|
182
|
+
export declare function proxy_get_secret(
|
|
183
|
+
key_data: usize,
|
|
184
|
+
key_size: usize,
|
|
185
|
+
return_value_data: usize,
|
|
186
|
+
return_value_size: usize
|
|
187
|
+
): u32;
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
// @ts-ignore: decorator
|
|
191
|
+
@external("env", "proxy_get_effective_at_secret")
|
|
192
|
+
export declare function proxy_get_effective_at_secret(
|
|
193
|
+
key_data: usize,
|
|
194
|
+
key_size: usize,
|
|
195
|
+
at: u32,
|
|
196
|
+
return_value_data: usize,
|
|
197
|
+
return_value_size: usize
|
|
198
|
+
): u32;
|
|
199
|
+
|
package/assembly/runtime.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as imports from "./imports";
|
|
2
2
|
import { free } from "./malloc";
|
|
3
3
|
|
|
4
|
+
import { log as wasiLog } from "./fastedge";
|
|
5
|
+
|
|
4
6
|
// abort function.
|
|
5
7
|
// use with:
|
|
6
8
|
// --use abort=index/abort_proc_exit
|
|
@@ -69,7 +71,7 @@ class ArrayBufferReference {
|
|
|
69
71
|
}
|
|
70
72
|
}
|
|
71
73
|
|
|
72
|
-
|
|
74
|
+
export const globalArrayBufferReference = new ArrayBufferReference();
|
|
73
75
|
let globalU32Ref = new Reference<u32>();
|
|
74
76
|
let globalLogLevelRef = new Reference<imports.LogLevel>();
|
|
75
77
|
let globalU64Ref = new Reference<u64>();
|
|
@@ -217,8 +219,11 @@ export enum StreamTypeValues {
|
|
|
217
219
|
export function log(level: LogLevelValues, logMessage: string): void {
|
|
218
220
|
// from the docs:
|
|
219
221
|
// Like JavaScript, AssemblyScript stores strings in UTF-16 encoding represented by the API as UCS-2,
|
|
220
|
-
let buffer = String.UTF8.encode(logMessage);
|
|
221
|
-
imports.proxy_log(level as imports.LogLevel, changetype<usize>(buffer), buffer.byteLength);
|
|
222
|
+
// let buffer = String.UTF8.encode(logMessage);
|
|
223
|
+
// imports.proxy_log(level as imports.LogLevel, changetype<usize>(buffer), buffer.byteLength);
|
|
224
|
+
|
|
225
|
+
// Temporary fix for proxy_log not being implemented in fastedge:
|
|
226
|
+
wasiLog(level, logMessage);
|
|
222
227
|
}
|
|
223
228
|
|
|
224
229
|
export function logLevel(): LogLevelValues {
|
package/package.json
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
"docs": "typedoc"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
|
+
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
10
11
|
"@semantic-release/changelog": "^6.0.3",
|
|
11
12
|
"assemblyscript": "^0.27.34",
|
|
12
13
|
"http-server": "^14.1.1",
|
|
@@ -16,7 +17,7 @@
|
|
|
16
17
|
},
|
|
17
18
|
"name": "@gcoredev/proxy-wasm-sdk-as",
|
|
18
19
|
"description": "Use this SDK to write extensions for the proxy WASM ABI",
|
|
19
|
-
"version": "1.0.
|
|
20
|
+
"version": "1.0.2",
|
|
20
21
|
"main": "assembly/index.ts",
|
|
21
22
|
"directories": {
|
|
22
23
|
"doc": "docs"
|