@gcoredev/proxy-wasm-sdk-as 1.0.0 → 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 CHANGED
@@ -1,8 +1,15 @@
1
1
  # WebAssembly for Proxies (AssemblyScript SDK)
2
2
 
3
+ [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/G-Core/proxy-wasm-sdk-as/deploy.yaml)](https://github.com/G-Core/proxy-wasm-sdk-as)
4
+ [![GitHub commit activity](https://img.shields.io/github/commit-activity/t/G-Core/proxy-wasm-sdk-as)](https://github.com/G-Core/proxy-wasm-sdk-as)
5
+ [![GitHub top language](https://img.shields.io/github/languages/top/G-Core/proxy-wasm-sdk-as)](https://github.com/G-Core/proxy-wasm-sdk-as)
6
+ [![GitHub License](https://img.shields.io/github/license/G-Core/proxy-wasm-sdk-as)](https://github.com/G-Core/proxy-wasm-sdk-as/blob/main/LICENSE)
7
+ [![NPM Version](https://img.shields.io/npm/v/@gcoredev/proxy-wasm-sdk-as)](https://www.npmjs.com/package/@gcoredev/proxy-wasm-sdk-as)
8
+
3
9
  This is a friendly fork of https://github.com/Kong/proxy-wasm-assemblyscript-sdk/,
4
- temporarily mantained to address an incompatibility between the AssemblyScript
5
- SDK and the Rust SDK.
10
+ mantained to address an incompatibility between the AssemblyScript SDK and the Rust SDK,
11
+
12
+ It also adds some FastEdge specific functionality.
6
13
 
7
14
  ## How to use this SDK
8
15
 
@@ -25,21 +32,21 @@ the options passed to `asc` compiler:
25
32
  }
26
33
  ```
27
34
 
28
- Add `"@gcoredev/proxy-wasm-sdk-as": "0.0.1"` to your dependencies then run `npm install`.
35
+ Add `"@gcoredev/proxy-wasm-sdk-as": "^1.0.2"` to your dependencies then run `npm install`.
29
36
 
30
37
  ## Hello, World
31
38
 
32
39
  Copy this into assembly/index.ts:
33
40
 
34
41
  ```ts
35
- export * from "@gcoredev/proxy-wasm-sdk-as/proxy";
42
+ export * from "@gcoredev/proxy-wasm-sdk-as/assembly/proxy";
36
43
  import {
37
44
  RootContext,
38
45
  Context,
39
46
  registerRootContext,
40
47
  FilterHeadersStatusValues,
41
48
  stream_context,
42
- } from "@gcoredev/proxy-wasm-sdk-as";
49
+ } from "@gcoredev/proxy-wasm-sdk-as/assembly";
43
50
 
44
51
  class AddHeaderRoot extends RootContext {
45
52
  createContext(context_id: u32): Context {
@@ -70,7 +77,7 @@ registerRootContext((context_id: u32) => {
70
77
  }, "add_header");
71
78
  ```
72
79
 
73
- ## build
80
+ ## Build
74
81
 
75
82
  To build, simply run:
76
83
 
@@ -78,25 +85,17 @@ To build, simply run:
78
85
  npm run asbuild
79
86
  ```
80
87
 
81
- build results will be in the build folder. `untouched.wasm` and `optimized.wasm` are the compiled
82
- file that we will use (you only need one of them, if unsure use `optimized.wasm`).
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`).
83
90
 
84
91
  ## Run
85
92
 
86
- Configure envoy with your filter:
87
-
88
- ```yaml
89
- - name: envoy.filters.http.wasm
90
- config:
91
- config:
92
- name: "add_header"
93
- root_id: "add_header"
94
- configuration: "what ever you want"
95
- vm_config:
96
- vm_id: "my_vm_id"
97
- runtime: "envoy.wasm.runtime.v8"
98
- code:
99
- local:
100
- filename: /PATH/TO/CODE/build/optimized.wasm
101
- allow_precompiled: false
102
- ```
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 };
@@ -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
+
@@ -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
- var globalArrayBufferReference = new ArrayBufferReference();
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.0",
20
+ "version": "1.0.2",
20
21
  "main": "assembly/index.ts",
21
22
  "directories": {
22
23
  "doc": "docs"