@gcoredev/proxy-wasm-sdk-as 1.2.1-alpha.1 → 1.2.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 +22 -4
- package/assembly/fastedge/dictionary.ts +27 -0
- package/assembly/fastedge/env.ts +1 -1
- package/assembly/fastedge/index.ts +1 -0
- package/assembly/fastedge/secrets.ts +29 -5
- package/assembly/fastedge/utils/runtime.ts +11 -0
- package/assembly/imports.ts +10 -0
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -66,7 +66,7 @@ class AddHeader extends Context {
|
|
|
66
66
|
} else {
|
|
67
67
|
stream_context.headers.response.add(
|
|
68
68
|
"hello",
|
|
69
|
-
root_context.getConfiguration()
|
|
69
|
+
root_context.getConfiguration(),
|
|
70
70
|
);
|
|
71
71
|
}
|
|
72
72
|
return FilterHeadersStatusValues.Continue;
|
|
@@ -99,8 +99,26 @@ Please see [Envoy.md](./ENVOY.md)
|
|
|
99
99
|
|
|
100
100
|
## Examples
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
The `examples/` directory contains standalone examples demonstrating common use cases. Each example has its own `package.json`, `asconfig.json`, and `README.md`.
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
To build any example:
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
```sh
|
|
107
|
+
cd examples/<name>
|
|
108
|
+
pnpm install
|
|
109
|
+
pnpm run asbuild
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The compiled binary (`build/<name>.wasm`) can then be uploaded to the [FastEdge portal](https://portal.gcore.com).
|
|
113
|
+
|
|
114
|
+
| Example | Description |
|
|
115
|
+
| ------------------------------------------------------ | ------------------------------------------------------------------ |
|
|
116
|
+
| [body](./examples/body) | Read and modify request and response bodies |
|
|
117
|
+
| [geoBlock](./examples/geoBlock) | Block requests from specific countries using a `BLACKLIST` env var |
|
|
118
|
+
| [geoRedirect](./examples/geoRedirect) | Route requests to different origins based on country code |
|
|
119
|
+
| [headers](./examples/headers) | Add, remove, and replace HTTP request and response headers |
|
|
120
|
+
| [jwt](./examples/jwt) | Validate a JWT Bearer token using a secret variable |
|
|
121
|
+
| [kvStore](./examples/kvStore) | Query a FastEdge KV Store (get, scan, zrange, zscan, bfExists) |
|
|
122
|
+
| [logTime](./examples/logTime) | Log UTC timestamps at the request and response phases |
|
|
123
|
+
| [properties](./examples/properties) | Read and expose FastEdge runtime properties as response headers |
|
|
124
|
+
| [variablesAndSecrets](./examples/variablesAndSecrets/) | Access FastEdge environment varibales and secrets at runtime |
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as imports from "../imports";
|
|
2
|
+
|
|
3
|
+
import { globalArrayBufferReference, WasmResultValues } from "../runtime";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Function to get the value for the provided environment variable name.
|
|
7
|
+
* @param {string} name - The name of the environment variable.
|
|
8
|
+
* @returns {string} The value of the environment variable.
|
|
9
|
+
*/
|
|
10
|
+
function getEnv(name: string): string {
|
|
11
|
+
const buffer = String.UTF8.encode(name);
|
|
12
|
+
const status = imports.proxy_dictionary_get(
|
|
13
|
+
changetype<usize>(buffer),
|
|
14
|
+
buffer.byteLength,
|
|
15
|
+
globalArrayBufferReference.bufferPtr(),
|
|
16
|
+
globalArrayBufferReference.sizePtr(),
|
|
17
|
+
);
|
|
18
|
+
if (status == WasmResultValues.Ok) {
|
|
19
|
+
const arrBuff = globalArrayBufferReference.toArrayBuffer();
|
|
20
|
+
if (arrBuff.byteLength > 0) {
|
|
21
|
+
return String.UTF8.decode(arrBuff);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { getEnv };
|
package/assembly/fastedge/env.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* @deprecated Use {@link getEnv} instead. This function will be removed in a future version.
|
|
3
3
|
* @param {string} name - The name of the environment variable.
|
|
4
4
|
* @returns {string} The value of the environment variable.
|
|
5
5
|
*/
|
|
@@ -7,13 +7,13 @@ import { globalArrayBufferReference, WasmResultValues } from "../runtime";
|
|
|
7
7
|
* @param {string} name - The name of the secret variable.
|
|
8
8
|
* @returns {string} The value of the secret variable.
|
|
9
9
|
*/
|
|
10
|
-
function
|
|
10
|
+
function getSecret(name: string): string {
|
|
11
11
|
const buffer = String.UTF8.encode(name);
|
|
12
12
|
const status = imports.proxy_get_secret(
|
|
13
13
|
changetype<usize>(buffer),
|
|
14
14
|
buffer.byteLength,
|
|
15
15
|
globalArrayBufferReference.bufferPtr(),
|
|
16
|
-
globalArrayBufferReference.sizePtr()
|
|
16
|
+
globalArrayBufferReference.sizePtr(),
|
|
17
17
|
);
|
|
18
18
|
if (status == WasmResultValues.Ok) {
|
|
19
19
|
const arrBuff = globalArrayBufferReference.toArrayBuffer();
|
|
@@ -30,14 +30,14 @@ function getSecretVar(name: string): string {
|
|
|
30
30
|
* @param {u32} effectiveAt - The slot index of the secret. (effectiveAt >= secret_slots.slot)
|
|
31
31
|
* @returns {string} The value of the secret variable.
|
|
32
32
|
*/
|
|
33
|
-
function
|
|
33
|
+
function getSecretEffectiveAt(name: string, effectiveAt: u32): string {
|
|
34
34
|
const buffer = String.UTF8.encode(name);
|
|
35
35
|
const status = imports.proxy_get_effective_at_secret(
|
|
36
36
|
changetype<usize>(buffer),
|
|
37
37
|
buffer.byteLength,
|
|
38
38
|
effectiveAt,
|
|
39
39
|
globalArrayBufferReference.bufferPtr(),
|
|
40
|
-
globalArrayBufferReference.sizePtr()
|
|
40
|
+
globalArrayBufferReference.sizePtr(),
|
|
41
41
|
);
|
|
42
42
|
if (status == WasmResultValues.Ok) {
|
|
43
43
|
const arrBuff = globalArrayBufferReference.toArrayBuffer();
|
|
@@ -48,4 +48,28 @@ function getSecretVarEffectiveAt(name: string, effectiveAt: u32): string {
|
|
|
48
48
|
return "";
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
/**
|
|
52
|
+
* @deprecated Use {@link getSecret} instead. This function will be removed in a future version.
|
|
53
|
+
* @param {string} name - The name of the secret variable.
|
|
54
|
+
* @returns {string} The value of the secret variable.
|
|
55
|
+
*/
|
|
56
|
+
function getSecretVar(name: string): string {
|
|
57
|
+
return getSecret(name);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @deprecated Use {@link getSecretEffectiveAt} instead. This function will be removed in a future version.
|
|
62
|
+
* @param {string} name - The name of the secret variable.
|
|
63
|
+
* @param {u32} effectiveAt - The slot index of the secret. (effectiveAt >= secret_slots.slot)
|
|
64
|
+
* @returns {string} The value of the secret variable.
|
|
65
|
+
*/
|
|
66
|
+
function getSecretVarEffectiveAt(name: string, effectiveAt: u32): string {
|
|
67
|
+
return getSecretEffectiveAt(name, effectiveAt);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export {
|
|
71
|
+
getSecret,
|
|
72
|
+
getSecretEffectiveAt,
|
|
73
|
+
getSecretVar,
|
|
74
|
+
getSecretVarEffectiveAt,
|
|
75
|
+
};
|
|
@@ -11,6 +11,15 @@ function setLogLevel(level: LogLevelValues): void {
|
|
|
11
11
|
logLevel = level;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
const LOG_LEVEL_PREFIXES: string[] = [
|
|
15
|
+
"[TRACE]",
|
|
16
|
+
"[DEBUG]",
|
|
17
|
+
"[INFO]",
|
|
18
|
+
"[WARN]",
|
|
19
|
+
"[ERROR]",
|
|
20
|
+
"[CRITICAL]",
|
|
21
|
+
];
|
|
22
|
+
|
|
14
23
|
/**
|
|
15
24
|
* Temporary fix for proxy_log not being implemented in FastEdge.
|
|
16
25
|
* The function relies on @assemblyscript/wasi-shim to print to standard output.
|
|
@@ -20,6 +29,8 @@ function setLogLevel(level: LogLevelValues): void {
|
|
|
20
29
|
*/
|
|
21
30
|
function log(level: LogLevelValues, logMessage: string): void {
|
|
22
31
|
if (level >= logLevel) {
|
|
32
|
+
const prefix = LOG_LEVEL_PREFIXES[level];
|
|
33
|
+
logMessage = `${prefix}: ${logMessage}`;
|
|
23
34
|
process.stdout.write(logMessage + "\n");
|
|
24
35
|
}
|
|
25
36
|
}
|
package/assembly/imports.ts
CHANGED
|
@@ -180,6 +180,16 @@ export declare function proxy_call_foreign_function(function_name: ptr<char>,
|
|
|
180
180
|
|
|
181
181
|
// FastEdge HOST Apis
|
|
182
182
|
|
|
183
|
+
// Dictionary
|
|
184
|
+
// @ts-ignore: decorator
|
|
185
|
+
@external("env", "proxy_dictionary_get")
|
|
186
|
+
export declare function proxy_dictionary_get(
|
|
187
|
+
key_data: usize,
|
|
188
|
+
key_size: usize,
|
|
189
|
+
return_value_data: usize,
|
|
190
|
+
return_value_size: usize
|
|
191
|
+
): u32;
|
|
192
|
+
|
|
183
193
|
// Secrets
|
|
184
194
|
// @ts-ignore: decorator
|
|
185
195
|
@external("env", "proxy_get_secret")
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gcoredev/proxy-wasm-sdk-as",
|
|
3
3
|
"description": "Use this SDK to write extensions for the proxy WASM ABI",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.2",
|
|
5
5
|
"main": "assembly/index.ts",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"asbuild:debug": "asc assembly/index.ts --target debug",
|
|
@@ -13,11 +13,10 @@
|
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
15
15
|
"@semantic-release/changelog": "^6.0.3",
|
|
16
|
-
"assemblyscript": "^0.
|
|
16
|
+
"assemblyscript": "^0.28.9",
|
|
17
17
|
"http-server": "^14.1.1",
|
|
18
18
|
"local-web-server": "^5.4.0",
|
|
19
|
-
"
|
|
20
|
-
"typedoc": "^0.27.7"
|
|
19
|
+
"typedoc": "^0.28.16"
|
|
21
20
|
},
|
|
22
21
|
"directories": {
|
|
23
22
|
"doc": "docs"
|