@gcoredev/proxy-wasm-sdk-as 1.0.1 → 1.1.0
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/exports.ts +157 -51
- package/assembly/fastedge.ts +82 -0
- package/assembly/imports.ts +28 -0
- package/assembly/index.ts +49 -13
- package/assembly/runtime.ts +22 -5
- 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)
|
package/assembly/exports.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
getBaseContext,
|
|
3
|
+
getContext,
|
|
4
|
+
getRootContext,
|
|
5
|
+
ensureContext,
|
|
6
|
+
ensureRootContext,
|
|
7
|
+
deleteContext,
|
|
8
|
+
PeerTypeValues,
|
|
9
|
+
} from "./runtime";
|
|
2
10
|
|
|
3
11
|
///// CALLS IN
|
|
4
12
|
type FilterStatus = i32;
|
|
@@ -10,17 +18,26 @@ type GrpcStatus = i32;
|
|
|
10
18
|
type WasmOnDoneResult = u32;
|
|
11
19
|
|
|
12
20
|
// Calls in.
|
|
13
|
-
export function proxy_abi_version_0_2_1(): void {
|
|
21
|
+
export function proxy_abi_version_0_2_1(): void {}
|
|
14
22
|
|
|
15
|
-
export function proxy_on_vm_start(
|
|
23
|
+
export function proxy_on_vm_start(
|
|
24
|
+
root_context_id: u32,
|
|
25
|
+
configuration_size: u32
|
|
26
|
+
): u32 {
|
|
16
27
|
let root_context = getRootContext(root_context_id);
|
|
17
28
|
return root_context.onStart(configuration_size) ? 1 : 0;
|
|
18
29
|
}
|
|
19
|
-
export function proxy_validate_configuration(
|
|
30
|
+
export function proxy_validate_configuration(
|
|
31
|
+
root_context_id: u32,
|
|
32
|
+
configuration_size: u32
|
|
33
|
+
): u32 {
|
|
20
34
|
let root_context = getRootContext(root_context_id);
|
|
21
35
|
return root_context.validateConfiguration(configuration_size) ? 1 : 0;
|
|
22
36
|
}
|
|
23
|
-
export function proxy_on_configure(
|
|
37
|
+
export function proxy_on_configure(
|
|
38
|
+
root_context_id: u32,
|
|
39
|
+
configuration_size: u32
|
|
40
|
+
): u32 {
|
|
24
41
|
let root_context = getRootContext(root_context_id);
|
|
25
42
|
return root_context.onConfigure(configuration_size) ? 1 : 0;
|
|
26
43
|
}
|
|
@@ -28,7 +45,11 @@ export function proxy_on_tick(root_context_id: u32): void {
|
|
|
28
45
|
let root_context = getRootContext(root_context_id);
|
|
29
46
|
root_context.onTick();
|
|
30
47
|
}
|
|
31
|
-
export function proxy_on_foreign_function(
|
|
48
|
+
export function proxy_on_foreign_function(
|
|
49
|
+
root_context_id: u32,
|
|
50
|
+
function_id: u32,
|
|
51
|
+
data_size: u32
|
|
52
|
+
): void {
|
|
32
53
|
// TODO: implement me
|
|
33
54
|
}
|
|
34
55
|
|
|
@@ -37,7 +58,10 @@ export function proxy_on_queue_ready(root_context_id: u32, token: u32): void {
|
|
|
37
58
|
root_context.onQueueReady(token);
|
|
38
59
|
}
|
|
39
60
|
// Stream calls.
|
|
40
|
-
export function proxy_on_context_create(
|
|
61
|
+
export function proxy_on_context_create(
|
|
62
|
+
context_id: u32,
|
|
63
|
+
root_context_id: u32
|
|
64
|
+
): void {
|
|
41
65
|
if (root_context_id != 0) {
|
|
42
66
|
ensureContext(context_id, root_context_id);
|
|
43
67
|
} else {
|
|
@@ -45,77 +69,159 @@ export function proxy_on_context_create(context_id: u32, root_context_id: u32):
|
|
|
45
69
|
}
|
|
46
70
|
}
|
|
47
71
|
|
|
48
|
-
export function proxy_on_request_headers(
|
|
72
|
+
export function proxy_on_request_headers(
|
|
73
|
+
context_id: u32,
|
|
74
|
+
headers: u32,
|
|
75
|
+
end_of_stream: u32
|
|
76
|
+
): FilterHeadersStatus {
|
|
49
77
|
let ctx = getContext(context_id);
|
|
50
|
-
return ctx.onRequestHeaders(
|
|
51
|
-
|
|
52
|
-
|
|
78
|
+
return ctx.onRequestHeaders(
|
|
79
|
+
headers,
|
|
80
|
+
end_of_stream != 0
|
|
81
|
+
) as FilterHeadersStatus;
|
|
82
|
+
}
|
|
83
|
+
export function proxy_on_request_body(
|
|
84
|
+
context_id: u32,
|
|
85
|
+
body_buffer_length: u32,
|
|
86
|
+
end_of_stream: u32
|
|
87
|
+
): FilterDataStatus {
|
|
53
88
|
let ctx = getContext(context_id);
|
|
54
|
-
return ctx.onRequestBody(
|
|
55
|
-
|
|
56
|
-
|
|
89
|
+
return ctx.onRequestBody(
|
|
90
|
+
body_buffer_length,
|
|
91
|
+
end_of_stream != 0
|
|
92
|
+
) as FilterDataStatus;
|
|
93
|
+
}
|
|
94
|
+
export function proxy_on_request_trailers(
|
|
95
|
+
context_id: u32,
|
|
96
|
+
trailers: u32
|
|
97
|
+
): FilterTrailersStatus {
|
|
57
98
|
let ctx = getContext(context_id);
|
|
58
99
|
return ctx.onRequestTrailers(trailers) as FilterTrailersStatus;
|
|
59
100
|
}
|
|
60
|
-
export function proxy_on_request_metadata(
|
|
101
|
+
export function proxy_on_request_metadata(
|
|
102
|
+
context_id: u32,
|
|
103
|
+
nelements: u32
|
|
104
|
+
): FilterMetadataStatus {
|
|
61
105
|
let ctx = getContext(context_id);
|
|
62
106
|
return ctx.onRequestMetadata(nelements) as FilterMetadataStatus;
|
|
63
107
|
}
|
|
64
|
-
export function proxy_on_response_headers(
|
|
108
|
+
export function proxy_on_response_headers(
|
|
109
|
+
context_id: u32,
|
|
110
|
+
headers: u32,
|
|
111
|
+
end_of_stream: u32
|
|
112
|
+
): FilterHeadersStatus {
|
|
65
113
|
let ctx = getContext(context_id);
|
|
66
|
-
return ctx.onResponseHeaders(
|
|
67
|
-
|
|
68
|
-
|
|
114
|
+
return ctx.onResponseHeaders(
|
|
115
|
+
headers,
|
|
116
|
+
end_of_stream != 0
|
|
117
|
+
) as FilterHeadersStatus;
|
|
118
|
+
}
|
|
119
|
+
export function proxy_on_response_body(
|
|
120
|
+
context_id: u32,
|
|
121
|
+
body_buffer_length: u32,
|
|
122
|
+
end_of_stream: u32
|
|
123
|
+
): FilterDataStatus {
|
|
69
124
|
let ctx = getContext(context_id);
|
|
70
|
-
return ctx.onResponseBody(
|
|
71
|
-
|
|
72
|
-
|
|
125
|
+
return ctx.onResponseBody(
|
|
126
|
+
body_buffer_length,
|
|
127
|
+
end_of_stream != 0
|
|
128
|
+
) as FilterDataStatus;
|
|
129
|
+
}
|
|
130
|
+
export function proxy_on_response_trailers(
|
|
131
|
+
context_id: u32,
|
|
132
|
+
trailers: u32
|
|
133
|
+
): FilterTrailersStatus {
|
|
73
134
|
let ctx = getContext(context_id);
|
|
74
135
|
return ctx.onResponseTrailers(trailers) as FilterTrailersStatus;
|
|
75
136
|
}
|
|
76
|
-
export function proxy_on_response_metadata(
|
|
137
|
+
export function proxy_on_response_metadata(
|
|
138
|
+
context_id: u32,
|
|
139
|
+
nelements: u32
|
|
140
|
+
): FilterMetadataStatus {
|
|
77
141
|
let ctx = getContext(context_id);
|
|
78
142
|
return ctx.onResponseMetadata(nelements) as FilterMetadataStatus;
|
|
79
143
|
}
|
|
80
144
|
|
|
81
145
|
// HTTP/gRPC.
|
|
82
|
-
export function proxy_on_http_call_response(
|
|
146
|
+
export function proxy_on_http_call_response(
|
|
147
|
+
context_id: u32,
|
|
148
|
+
token: u32,
|
|
149
|
+
headers: u32,
|
|
150
|
+
body_size: u32,
|
|
151
|
+
trailers: u32
|
|
152
|
+
): void {
|
|
83
153
|
let ctx = getRootContext(context_id);
|
|
84
154
|
ctx.onHttpCallResponse(token, headers, body_size, trailers);
|
|
85
155
|
}
|
|
86
|
-
export function proxy_on_grpc_receive_initial_metadata(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
156
|
+
export function proxy_on_grpc_receive_initial_metadata(
|
|
157
|
+
context_id: u32,
|
|
158
|
+
token: u32,
|
|
159
|
+
headers: u32
|
|
160
|
+
): void {
|
|
161
|
+
getRootContext(context_id).on_grpc_receive_initial_metadata(token, headers);
|
|
162
|
+
}
|
|
163
|
+
export function proxy_on_grpc_trailing_metadata(
|
|
164
|
+
context_id: u32,
|
|
165
|
+
token: u32,
|
|
166
|
+
trailers: u32
|
|
167
|
+
): void {
|
|
168
|
+
getRootContext(context_id).on_grpc_trailing_metadata(token, trailers);
|
|
169
|
+
}
|
|
170
|
+
export function proxy_on_grpc_receive(
|
|
171
|
+
context_id: u32,
|
|
172
|
+
token: u32,
|
|
173
|
+
response_size: u32
|
|
174
|
+
): void {
|
|
175
|
+
getRootContext(context_id).on_grpc_receive(token, response_size);
|
|
176
|
+
}
|
|
177
|
+
export function proxy_on_grpc_close(
|
|
178
|
+
context_id: u32,
|
|
179
|
+
token: u32,
|
|
180
|
+
status_code: u32
|
|
181
|
+
): void {
|
|
182
|
+
getRootContext(context_id).on_grpc_close(token, status_code);
|
|
97
183
|
}
|
|
98
184
|
|
|
99
185
|
// NETWORK_FILTER support (TCP and, perhaps one day, UDP).
|
|
100
|
-
export function proxy_on_downstream_data(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
186
|
+
export function proxy_on_downstream_data(
|
|
187
|
+
context_id: u32,
|
|
188
|
+
body_buffer_length: u32,
|
|
189
|
+
end_of_stream: u32
|
|
190
|
+
): FilterStatus {
|
|
191
|
+
let ctx = getContext(context_id);
|
|
192
|
+
return ctx.onDownstreamData(
|
|
193
|
+
body_buffer_length,
|
|
194
|
+
end_of_stream != 0
|
|
195
|
+
) as FilterStatus;
|
|
196
|
+
}
|
|
197
|
+
export function proxy_on_upstream_data(
|
|
198
|
+
context_id: u32,
|
|
199
|
+
body_buffer_length: u32,
|
|
200
|
+
end_of_stream: u32
|
|
201
|
+
): FilterStatus {
|
|
202
|
+
let ctx = getContext(context_id);
|
|
203
|
+
return ctx.onUpstreamData(
|
|
204
|
+
body_buffer_length,
|
|
205
|
+
end_of_stream != 0
|
|
206
|
+
) as FilterStatus;
|
|
207
|
+
}
|
|
208
|
+
export function proxy_on_upstream_connection_close(
|
|
209
|
+
context_id: u32,
|
|
210
|
+
peer_type: u32
|
|
211
|
+
): void {
|
|
212
|
+
let ctx = getContext(context_id);
|
|
213
|
+
ctx.onUpstreamConnectionClose(peer_type as PeerTypeValues);
|
|
111
214
|
}
|
|
112
|
-
export function proxy_on_downstream_connection_close(
|
|
113
|
-
|
|
114
|
-
|
|
215
|
+
export function proxy_on_downstream_connection_close(
|
|
216
|
+
context_id: u32,
|
|
217
|
+
peer_type: u32
|
|
218
|
+
): void {
|
|
219
|
+
let ctx = getContext(context_id);
|
|
220
|
+
ctx.onDownstreamConnectionClose(peer_type as PeerTypeValues);
|
|
115
221
|
}
|
|
116
222
|
export function proxy_on_new_connection(context_id: u32): FilterStatus {
|
|
117
|
-
|
|
118
|
-
|
|
223
|
+
let ctx = getContext(context_id);
|
|
224
|
+
return ctx.onNewConnection() as FilterStatus;
|
|
119
225
|
}
|
|
120
226
|
|
|
121
227
|
// The stream/vm has completed.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import * as imports from "./imports";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
get_current_time_nanoseconds,
|
|
5
|
+
globalArrayBufferReference,
|
|
6
|
+
LogLevelValues,
|
|
7
|
+
} from "./runtime";
|
|
8
|
+
|
|
9
|
+
let logLevel: LogLevelValues = LogLevelValues.info;
|
|
10
|
+
|
|
11
|
+
function setLogLevel(level: LogLevelValues): void {
|
|
12
|
+
logLevel = level;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function log(level: LogLevelValues, logMessage: string): void {
|
|
16
|
+
// Temporary fix for proxy_log not being implemented in fastedge:
|
|
17
|
+
// relies on @assemblyscript/wasi-shim to print to standard output
|
|
18
|
+
if (level >= logLevel) {
|
|
19
|
+
process.stdout.write(logMessage + "\n");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function getCurrentTime(): u64 {
|
|
24
|
+
return get_current_time_nanoseconds() / 1_000_000; // Convert nanoseconds to milliseconds
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getEnvVar(key: string): string {
|
|
28
|
+
const hasKey = process.env.has(key);
|
|
29
|
+
if (hasKey) {
|
|
30
|
+
return process.env.get(key);
|
|
31
|
+
}
|
|
32
|
+
return "";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function getSecretVar(key: string): string {
|
|
36
|
+
const buffer = String.UTF8.encode(key);
|
|
37
|
+
const status = imports.proxy_get_secret(
|
|
38
|
+
changetype<usize>(buffer),
|
|
39
|
+
buffer.byteLength,
|
|
40
|
+
globalArrayBufferReference.bufferPtr(),
|
|
41
|
+
globalArrayBufferReference.sizePtr()
|
|
42
|
+
);
|
|
43
|
+
if (status != 0) {
|
|
44
|
+
// Something went wrong - returns 0 with an empty ArrayBuffer if not found
|
|
45
|
+
return "";
|
|
46
|
+
}
|
|
47
|
+
const arrBuff = globalArrayBufferReference.toArrayBuffer();
|
|
48
|
+
if (arrBuff.byteLength == 0) {
|
|
49
|
+
return ""; // Not found
|
|
50
|
+
}
|
|
51
|
+
return String.UTF8.decode(arrBuff);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getSecretVarEffectiveAt(key: string, at: u32): string {
|
|
55
|
+
const buffer = String.UTF8.encode(key);
|
|
56
|
+
const status = imports.proxy_get_effective_at_secret(
|
|
57
|
+
changetype<usize>(buffer),
|
|
58
|
+
buffer.byteLength,
|
|
59
|
+
at,
|
|
60
|
+
globalArrayBufferReference.bufferPtr(),
|
|
61
|
+
globalArrayBufferReference.sizePtr()
|
|
62
|
+
);
|
|
63
|
+
if (status != 0) {
|
|
64
|
+
// Something went wrong - returns 0 with an empty ArrayBuffer if not found
|
|
65
|
+
return "";
|
|
66
|
+
}
|
|
67
|
+
const arrBuff = globalArrayBufferReference.toArrayBuffer();
|
|
68
|
+
if (arrBuff.byteLength == 0) {
|
|
69
|
+
return ""; // Not found
|
|
70
|
+
}
|
|
71
|
+
return String.UTF8.decode(arrBuff);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export {
|
|
75
|
+
getCurrentTime,
|
|
76
|
+
getEnvVar,
|
|
77
|
+
getSecretVar,
|
|
78
|
+
getSecretVarEffectiveAt,
|
|
79
|
+
log,
|
|
80
|
+
LogLevelValues,
|
|
81
|
+
setLogLevel,
|
|
82
|
+
};
|
package/assembly/imports.ts
CHANGED
|
@@ -124,6 +124,10 @@ export declare function proxy_get_buffer_bytes(typ: BufferType, start: u32, leng
|
|
|
124
124
|
// @ts-ignore: decorator
|
|
125
125
|
@external("env", "proxy_get_buffer_status")
|
|
126
126
|
export declare function proxy_get_buffer_status(typ: BufferType, length_ptr: ptr<usize>, flags_ptr: ptr<u32>): WasmResult;
|
|
127
|
+
// @ts-ignore: decorator
|
|
128
|
+
@external("env", "proxy_set_buffer_bytes")
|
|
129
|
+
export declare function proxy_set_buffer_bytes(typ: BufferType, start: u32, length: u32, ptr: ptr<ptr<char>>, size: ptr<usize>): WasmResult;
|
|
130
|
+
|
|
127
131
|
|
|
128
132
|
// HTTP
|
|
129
133
|
// @ts-ignore: decorator
|
|
@@ -173,3 +177,27 @@ export declare function proxy_call_foreign_function(function_name: ptr<char>,
|
|
|
173
177
|
name_size: size_t, arguments: ptr<char>,
|
|
174
178
|
arguments_size: size_t, results: ptr<ptr<char>>,
|
|
175
179
|
results_size: ptr<size_t>): WasmResult;
|
|
180
|
+
|
|
181
|
+
// FastEdge HOST Apis
|
|
182
|
+
|
|
183
|
+
// Secrets
|
|
184
|
+
// @ts-ignore: decorator
|
|
185
|
+
@external("env", "proxy_get_secret")
|
|
186
|
+
export declare function proxy_get_secret(
|
|
187
|
+
key_data: usize,
|
|
188
|
+
key_size: usize,
|
|
189
|
+
return_value_data: usize,
|
|
190
|
+
return_value_size: usize
|
|
191
|
+
): u32;
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
// @ts-ignore: decorator
|
|
195
|
+
@external("env", "proxy_get_effective_at_secret")
|
|
196
|
+
export declare function proxy_get_effective_at_secret(
|
|
197
|
+
key_data: usize,
|
|
198
|
+
key_size: usize,
|
|
199
|
+
at: u32,
|
|
200
|
+
return_value_data: usize,
|
|
201
|
+
return_value_size: usize
|
|
202
|
+
): u32;
|
|
203
|
+
|
package/assembly/index.ts
CHANGED
|
@@ -1,15 +1,51 @@
|
|
|
1
1
|
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
BaseContext,
|
|
3
|
+
BufferTypeValues,
|
|
4
|
+
call_foreign_function,
|
|
5
|
+
Context,
|
|
6
|
+
continue_request,
|
|
7
|
+
continue_response,
|
|
8
|
+
Counter,
|
|
9
|
+
dequeue_shared_queue,
|
|
10
|
+
enqueue_shared_queue,
|
|
11
|
+
FilterDataStatusValues,
|
|
12
|
+
FilterHeadersStatusValues,
|
|
13
|
+
FilterMetadataStatusValues,
|
|
14
|
+
FilterStatusValues,
|
|
15
|
+
FilterTrailersStatusValues,
|
|
16
|
+
Gauge,
|
|
17
|
+
get_buffer_bytes,
|
|
18
|
+
get_current_time_nanoseconds,
|
|
19
|
+
get_property,
|
|
20
|
+
get_shared_data,
|
|
21
|
+
GetSharedData,
|
|
22
|
+
GrpcStatusValues,
|
|
23
|
+
HeaderPair,
|
|
24
|
+
Headers,
|
|
25
|
+
Histogram,
|
|
26
|
+
HttpCallback,
|
|
27
|
+
log,
|
|
28
|
+
LogLevelValues,
|
|
29
|
+
makeHeaderPair,
|
|
30
|
+
proxy_set_effective_context,
|
|
31
|
+
register_shared_queue,
|
|
32
|
+
registerRootContext,
|
|
33
|
+
resolve_shared_queue,
|
|
34
|
+
RootContext,
|
|
35
|
+
send_http_response,
|
|
36
|
+
send_local_response,
|
|
37
|
+
set_buffer_bytes,
|
|
38
|
+
set_property,
|
|
39
|
+
set_shared_data,
|
|
40
|
+
set_tick_period_milliseconds,
|
|
41
|
+
stream_context,
|
|
42
|
+
WasmResultValues,
|
|
15
43
|
} from "./runtime";
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
getCurrentTime,
|
|
47
|
+
getEnvVar,
|
|
48
|
+
getSecretVar,
|
|
49
|
+
getSecretVarEffectiveAt,
|
|
50
|
+
setLogLevel,
|
|
51
|
+
} from "./fastedge";
|
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 {
|
|
@@ -251,8 +256,12 @@ export function get_current_time_nanoseconds(): u64 {
|
|
|
251
256
|
|
|
252
257
|
export function get_property(path: string): ArrayBuffer {
|
|
253
258
|
let buffer = String.UTF8.encode(path);
|
|
254
|
-
|
|
255
|
-
|
|
259
|
+
const result = imports.proxy_get_property(changetype<usize>(buffer), buffer.byteLength, globalArrayBufferReference.bufferPtr(), globalArrayBufferReference.sizePtr());
|
|
260
|
+
CHECK_RESULT(result);
|
|
261
|
+
if (result == WasmResultValues.Ok) {
|
|
262
|
+
return globalArrayBufferReference.toArrayBuffer();
|
|
263
|
+
}
|
|
264
|
+
return new ArrayBuffer(0); // result == WasmResultValues.NotFound
|
|
256
265
|
}
|
|
257
266
|
|
|
258
267
|
export function set_property(path: string, data: ArrayBuffer): WasmResultValues {
|
|
@@ -622,6 +631,12 @@ export function get_buffer_bytes(typ: BufferTypeValues, start: u32, length: u32)
|
|
|
622
631
|
return new ArrayBuffer(0);
|
|
623
632
|
}
|
|
624
633
|
|
|
634
|
+
export function set_buffer_bytes(typ: BufferTypeValues, start: u32, length: u32, value: ArrayBuffer): WasmResultValues {
|
|
635
|
+
const result = imports.proxy_set_buffer_bytes(typ, start, length, changetype<usize>(value), value.byteLength);
|
|
636
|
+
wasiLog(LogLevelValues.info, 'Farq: setBuffer result: ' + result.toString());
|
|
637
|
+
return result
|
|
638
|
+
}
|
|
639
|
+
|
|
625
640
|
// returning tuples is not supported.
|
|
626
641
|
class BufferStatusResult {
|
|
627
642
|
result: WasmResultValues;
|
|
@@ -1112,3 +1127,5 @@ export function registerRootContext(
|
|
|
1112
1127
|
name: string): void {
|
|
1113
1128
|
root_context_factory = context_factory;
|
|
1114
1129
|
}
|
|
1130
|
+
|
|
1131
|
+
|
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.1.0",
|
|
20
21
|
"main": "assembly/index.ts",
|
|
21
22
|
"directories": {
|
|
22
23
|
"doc": "docs"
|