@gcoredev/proxy-wasm-sdk-as 1.0.2 → 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.
@@ -1,4 +1,12 @@
1
- import { getBaseContext, getContext, getRootContext, ensureContext, ensureRootContext, deleteContext, PeerTypeValues } from "./runtime";
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(root_context_id: u32, configuration_size: u32): u32 {
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(root_context_id: u32, configuration_size: u32): u32 {
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(root_context_id: u32, configuration_size: u32): u32 {
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(root_context_id: u32, function_id: u32, data_size: u32): void {
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(context_id: u32, root_context_id: u32): void {
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(context_id: u32, headers: u32, end_of_stream: u32): FilterHeadersStatus {
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(headers, end_of_stream != 0) as FilterHeadersStatus;
51
- }
52
- export function proxy_on_request_body(context_id: u32, body_buffer_length: u32, end_of_stream: u32): FilterDataStatus {
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(body_buffer_length, end_of_stream != 0) as FilterDataStatus;
55
- }
56
- export function proxy_on_request_trailers(context_id: u32, trailers: u32): FilterTrailersStatus {
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(context_id: u32, nelements: u32): FilterMetadataStatus {
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(context_id: u32, headers: u32, end_of_stream: u32): FilterHeadersStatus {
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(headers, end_of_stream != 0) as FilterHeadersStatus;
67
- }
68
- export function proxy_on_response_body(context_id: u32, body_buffer_length: u32, end_of_stream: u32): FilterDataStatus {
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(body_buffer_length, end_of_stream != 0) as FilterDataStatus;
71
- }
72
- export function proxy_on_response_trailers(context_id: u32, trailers: u32): FilterTrailersStatus {
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(context_id: u32, nelements: u32): FilterMetadataStatus {
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(context_id: u32, token: u32, headers: u32, body_size: u32, trailers: u32): void {
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(context_id: u32, token: u32, headers: u32): void {
87
- getRootContext(context_id).on_grpc_receive_initial_metadata(token, headers)
88
- }
89
- export function proxy_on_grpc_trailing_metadata(context_id: u32, token: u32, trailers: u32): void {
90
- getRootContext(context_id).on_grpc_trailing_metadata(token, trailers)
91
- }
92
- export function proxy_on_grpc_receive(context_id: u32, token: u32, response_size: u32): void {
93
- getRootContext(context_id).on_grpc_receive(token, response_size)
94
- }
95
- export function proxy_on_grpc_close(context_id: u32, token: u32, status_code: u32): void {
96
- getRootContext(context_id).on_grpc_close(token, status_code)
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(context_id: u32, body_buffer_length: u32, end_of_stream: u32): FilterStatus {
101
- let ctx = getContext(context_id);
102
- return ctx.onDownstreamData(body_buffer_length, end_of_stream != 0) as FilterStatus;
103
- }
104
- export function proxy_on_upstream_data(context_id: u32, body_buffer_length: u32, end_of_stream: u32): FilterStatus {
105
- let ctx = getContext(context_id);
106
- return ctx.onUpstreamData(body_buffer_length, end_of_stream != 0) as FilterStatus;
107
- }
108
- export function proxy_on_upstream_connection_close(context_id: u32, peer_type: u32): void {
109
- let ctx = getContext(context_id);
110
- ctx.onUpstreamConnectionClose(peer_type as PeerTypeValues);
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(context_id: u32, peer_type: u32): void {
113
- let ctx = getContext(context_id);
114
- ctx.onDownstreamConnectionClose(peer_type as PeerTypeValues);
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
- let ctx = getContext(context_id);
118
- return ctx.onNewConnection() as FilterStatus;
223
+ let ctx = getContext(context_id);
224
+ return ctx.onNewConnection() as FilterStatus;
119
225
  }
120
226
 
121
227
  // The stream/vm has completed.
@@ -1,14 +1,18 @@
1
1
  import * as imports from "./imports";
2
2
 
3
- import { globalArrayBufferReference, LogLevelValues } from "./runtime";
3
+ import {
4
+ get_current_time_nanoseconds,
5
+ globalArrayBufferReference,
6
+ LogLevelValues,
7
+ } from "./runtime";
4
8
 
5
9
  let logLevel: LogLevelValues = LogLevelValues.info;
6
10
 
7
- export function setLogLevel(level: LogLevelValues): void {
11
+ function setLogLevel(level: LogLevelValues): void {
8
12
  logLevel = level;
9
13
  }
10
14
 
11
- export function log(level: LogLevelValues, logMessage: string): void {
15
+ function log(level: LogLevelValues, logMessage: string): void {
12
16
  // Temporary fix for proxy_log not being implemented in fastedge:
13
17
  // relies on @assemblyscript/wasi-shim to print to standard output
14
18
  if (level >= logLevel) {
@@ -16,7 +20,11 @@ export function log(level: LogLevelValues, logMessage: string): void {
16
20
  }
17
21
  }
18
22
 
19
- export function getEnvVar(key: string): string {
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 {
20
28
  const hasKey = process.env.has(key);
21
29
  if (hasKey) {
22
30
  return process.env.get(key);
@@ -24,7 +32,7 @@ export function getEnvVar(key: string): string {
24
32
  return "";
25
33
  }
26
34
 
27
- export function getSecretVar(key: string): string {
35
+ function getSecretVar(key: string): string {
28
36
  const buffer = String.UTF8.encode(key);
29
37
  const status = imports.proxy_get_secret(
30
38
  changetype<usize>(buffer),
@@ -43,7 +51,7 @@ export function getSecretVar(key: string): string {
43
51
  return String.UTF8.decode(arrBuff);
44
52
  }
45
53
 
46
- export function getSecretVarEffectiveAt(key: string, at: u32): string {
54
+ function getSecretVarEffectiveAt(key: string, at: u32): string {
47
55
  const buffer = String.UTF8.encode(key);
48
56
  const status = imports.proxy_get_effective_at_secret(
49
57
  changetype<usize>(buffer),
@@ -63,4 +71,12 @@ export function getSecretVarEffectiveAt(key: string, at: u32): string {
63
71
  return String.UTF8.decode(arrBuff);
64
72
  }
65
73
 
66
- export { LogLevelValues };
74
+ export {
75
+ getCurrentTime,
76
+ getEnvVar,
77
+ getSecretVar,
78
+ getSecretVarEffectiveAt,
79
+ log,
80
+ LogLevelValues,
81
+ setLogLevel,
82
+ };
@@ -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
package/assembly/index.ts CHANGED
@@ -1,15 +1,51 @@
1
1
  export {
2
- BaseContext, RootContext, Context, registerRootContext,
3
- BufferTypeValues, LogLevelValues, WasmResultValues, GrpcStatusValues,
4
- FilterStatusValues,
5
- FilterHeadersStatusValues,
6
- FilterMetadataStatusValues,
7
- FilterTrailersStatusValues,
8
- FilterDataStatusValues, stream_context,
9
- HeaderPair, Headers, makeHeaderPair,
10
- Gauge, Histogram, Counter, log,
11
- HttpCallback, send_local_response, send_http_response, continue_request, continue_response,
12
- proxy_set_effective_context, set_property, get_property, get_shared_data, GetSharedData, set_shared_data, set_tick_period_milliseconds,
13
- register_shared_queue, resolve_shared_queue, enqueue_shared_queue, dequeue_shared_queue,
14
- get_buffer_bytes, call_foreign_function, get_current_time_nanoseconds
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";
@@ -256,8 +256,12 @@ export function get_current_time_nanoseconds(): u64 {
256
256
 
257
257
  export function get_property(path: string): ArrayBuffer {
258
258
  let buffer = String.UTF8.encode(path);
259
- CHECK_RESULT(imports.proxy_get_property(changetype<usize>(buffer), buffer.byteLength, globalArrayBufferReference.bufferPtr(), globalArrayBufferReference.sizePtr()));
260
- return globalArrayBufferReference.toArrayBuffer();
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
261
265
  }
262
266
 
263
267
  export function set_property(path: string, data: ArrayBuffer): WasmResultValues {
@@ -627,6 +631,12 @@ export function get_buffer_bytes(typ: BufferTypeValues, start: u32, length: u32)
627
631
  return new ArrayBuffer(0);
628
632
  }
629
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
+
630
640
  // returning tuples is not supported.
631
641
  class BufferStatusResult {
632
642
  result: WasmResultValues;
@@ -1117,3 +1127,5 @@ export function registerRootContext(
1117
1127
  name: string): void {
1118
1128
  root_context_factory = context_factory;
1119
1129
  }
1130
+
1131
+
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "name": "@gcoredev/proxy-wasm-sdk-as",
19
19
  "description": "Use this SDK to write extensions for the proxy WASM ABI",
20
- "version": "1.0.2",
20
+ "version": "1.1.0",
21
21
  "main": "assembly/index.ts",
22
22
  "directories": {
23
23
  "doc": "docs"