@gcoredev/proxy-wasm-sdk-as 1.2.1 → 1.2.3
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 +52 -0
- package/assembly/fastedge/env.ts +1 -1
- package/assembly/fastedge/index.ts +1 -0
- package/assembly/fastedge/secrets.ts +30 -6
- package/assembly/fastedge/utils/runtime.ts +11 -0
- package/assembly/imports.ts +10 -0
- package/assembly/runtime.ts +2 -2
- package/package.json +3 -11
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,52 @@
|
|
|
1
|
+
import * as imports from "../imports";
|
|
2
|
+
|
|
3
|
+
import { globalArrayBufferReference, WasmResultValues } from "../runtime";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Reads an environment variable by name using the WASI environment interface.
|
|
7
|
+
*
|
|
8
|
+
* Use this for normal-sized environment variables (under 64 KB). For values
|
|
9
|
+
* that may exceed the WASI 64 KB limit, use {@link getDictionary} instead.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} name - The name of the environment variable.
|
|
12
|
+
* @returns {string} The value, or an empty string if not found.
|
|
13
|
+
*/
|
|
14
|
+
function getEnv(name: string): string {
|
|
15
|
+
const hasKey = process.env.has(name);
|
|
16
|
+
if (hasKey) {
|
|
17
|
+
return process.env.get(name);
|
|
18
|
+
}
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Reads a dictionary value by name using the proxy-wasm dictionary API
|
|
24
|
+
* (`proxy_dictionary_get`).
|
|
25
|
+
*
|
|
26
|
+
* This bypasses the WASI 64 KB environment variable size limit and should
|
|
27
|
+
* be used when a value may be larger than 64 KB (e.g. large JSON configs,
|
|
28
|
+
* PEM certificates, policy documents).
|
|
29
|
+
*
|
|
30
|
+
* For normal-sized environment variables, prefer {@link getEnv}.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} name - The dictionary key to look up.
|
|
33
|
+
* @returns {string} The value, or an empty string if not found.
|
|
34
|
+
*/
|
|
35
|
+
function getDictionary(name: string): string {
|
|
36
|
+
const buffer = String.UTF8.encode(name);
|
|
37
|
+
const status = imports.proxy_dictionary_get(
|
|
38
|
+
changetype<usize>(buffer),
|
|
39
|
+
buffer.byteLength,
|
|
40
|
+
globalArrayBufferReference.bufferPtr(),
|
|
41
|
+
globalArrayBufferReference.sizePtr(),
|
|
42
|
+
);
|
|
43
|
+
if (status == WasmResultValues.Ok) {
|
|
44
|
+
const arrBuff = globalArrayBufferReference.toArrayBuffer();
|
|
45
|
+
if (arrBuff.byteLength > 0) {
|
|
46
|
+
return String.UTF8.decode(arrBuff);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return "";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { getEnv, getDictionary };
|
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();
|
|
@@ -27,17 +27,17 @@ function getSecretVar(name: string): string {
|
|
|
27
27
|
/**
|
|
28
28
|
* Function to get the value for the provided secret variable name from a specific slot.
|
|
29
29
|
* @param {string} name - The name of the secret variable.
|
|
30
|
-
* @param {u32} effectiveAt - The slot index
|
|
30
|
+
* @param {u32} effectiveAt - The numeric slot index to retrieve. Slots are defined in the FastEdge UI and are always numeric (e.g. incremental integers, or Date.now()-style values to represent a point in time).
|
|
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 numeric slot index to retrieve. Slots are defined in the FastEdge UI and are always numeric (e.g. incremental integers, or Date.now()-style values to represent a point in time).
|
|
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/assembly/runtime.ts
CHANGED
|
@@ -490,7 +490,7 @@ class HeaderStreamManipulator {
|
|
|
490
490
|
}
|
|
491
491
|
|
|
492
492
|
/**
|
|
493
|
-
* Replace a header.
|
|
493
|
+
* Replace a header value. No-op if the header is not present.
|
|
494
494
|
* @param key the header name.
|
|
495
495
|
* @param value the header value.
|
|
496
496
|
*/
|
|
@@ -1125,7 +1125,7 @@ export function deleteContext(context_id: u32): void {
|
|
|
1125
1125
|
/**
|
|
1126
1126
|
* Register a root context factory and make it available to the runtime.
|
|
1127
1127
|
* @param root_context_factory A function that creates a new root context.
|
|
1128
|
-
* @param name
|
|
1128
|
+
* @param name Accepted for API compatibility with the proxy-wasm spec but ignored by the FastEdge runtime. The value does not need to match any configuration — pass any descriptive string.
|
|
1129
1129
|
*/
|
|
1130
1130
|
export function registerRootContext(
|
|
1131
1131
|
context_factory: (context_id: u32) => RootContext,
|
package/package.json
CHANGED
|
@@ -1,26 +1,18 @@
|
|
|
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.3",
|
|
5
5
|
"main": "assembly/index.ts",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"asbuild:debug": "asc assembly/index.ts --target debug",
|
|
8
8
|
"asbuild:release": "asc assembly/index.ts --target release",
|
|
9
9
|
"asbuild": "npm run asbuild:debug && npm run asbuild:release",
|
|
10
|
-
"
|
|
11
|
-
"docs": "typedoc"
|
|
10
|
+
"generate:docs": "./fastedge-plugin-source/generate-docs.sh"
|
|
12
11
|
},
|
|
13
12
|
"devDependencies": {
|
|
14
13
|
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
15
14
|
"@semantic-release/changelog": "^6.0.3",
|
|
16
|
-
"assemblyscript": "^0.
|
|
17
|
-
"http-server": "^14.1.1",
|
|
18
|
-
"local-web-server": "^5.4.0",
|
|
19
|
-
"minimist": ">=1.2.2",
|
|
20
|
-
"typedoc": "^0.27.7"
|
|
21
|
-
},
|
|
22
|
-
"directories": {
|
|
23
|
-
"doc": "docs"
|
|
15
|
+
"assemblyscript": "^0.28.9"
|
|
24
16
|
},
|
|
25
17
|
"repository": {
|
|
26
18
|
"type": "git",
|