Skip to content

Commit 78a4d88

Browse files
authored
feat: upgrade assemblyscript faas demo(#256) (#486)
1 parent 8d89e8e commit 78a4d88

File tree

10 files changed

+264
-95
lines changed

10 files changed

+264
-95
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Build
2+
3+
The Node.js version should >=16.14.0.
4+
5+
* npm i
6+
* npm run asbuild
7+
* Copy build/function_1.wasm and build/function_2.wasm to your actual directory.
8+
* Test the demo follow https://mosn.io/layotto/#/zh/start/faas/start.
+13-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
{
22
"targets": {
3-
"debug": {
4-
"binaryFile": "build/untouched.wasm",
5-
"textFile": "build/untouched.wat",
6-
"sourceMap": true,
7-
"debug": true
3+
"client": {
4+
"outFile": "build/function_1.wasm",
5+
"sourceMap": false,
6+
"optimizeLevel": 3,
7+
"shrinkLevel": 0,
8+
"converge": false,
9+
"noAssert": false
810
},
9-
"release": {
10-
"binaryFile": "build/optimized.wasm",
11-
"textFile": "build/optimized.wat",
12-
"sourceMap": true,
11+
"server": {
12+
"outFile": "build/function_2.wasm",
13+
"sourceMap": false,
1314
"optimizeLevel": 3,
1415
"shrinkLevel": 0,
1516
"converge": false,
1617
"noAssert": false
1718
}
1819
},
19-
"options": {}
20+
"options": {
21+
"bindings": "esm"
22+
}
2023
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {
2+
BufferTypeValues,
3+
Context,
4+
FilterDataStatusValues,
5+
get_buffer_bytes,
6+
log,
7+
LogLevelValues,
8+
registerRootContext,
9+
RootContext,
10+
set_http_response_body,
11+
} from "@nobodyiam/proxy-runtime/assembly";
12+
import { invokeService, registerId } from "./proxy";
13+
14+
export * from "@nobodyiam/proxy-runtime/assembly/proxy";
15+
16+
function getBookName(body: string): string | null {
17+
const parts = body.split("&");
18+
for (let index = 0; index < parts.length; ++index) {
19+
const item = parts[index];
20+
if (item.startsWith("name=")) {
21+
return item.slice("name=".length);
22+
}
23+
}
24+
return null;
25+
}
26+
27+
class ClientRootHttpContext extends RootContext {
28+
createContext(context_id: u32): Context {
29+
return new ClientHttpContext(context_id, this);
30+
}
31+
}
32+
33+
class ClientHttpContext extends Context {
34+
constructor(context_id: u32, root_context: ClientRootHttpContext) {
35+
super(context_id, root_context);
36+
}
37+
38+
onRequestBody(body_buffer_length: usize, _end_of_stream: bool): FilterDataStatusValues {
39+
const name = getBookName(
40+
String.UTF8.decode(get_buffer_bytes(BufferTypeValues.HttpRequestBody, 0, body_buffer_length as u32))
41+
);
42+
if (name === null) {
43+
log(LogLevelValues.error, "Param 'name' not found");
44+
} else {
45+
set_http_response_body(`There are ${String.UTF8.decode(invokeService("id_2", "", name))} inventories for ${name}.\n`);
46+
}
47+
return FilterDataStatusValues.Continue
48+
}
49+
}
50+
51+
export function _start(): void {
52+
registerRootContext((context_id: u32) => {
53+
return new ClientRootHttpContext(context_id);
54+
}, "");
55+
}
56+
57+
export function proxy_get_id(): void {
58+
registerId("id_1");
59+
}

demo/faas/code/assemblyscript/assembly/index.ts

-37
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import * as imports from '@nobodyiam/proxy-runtime/assembly/imports';
2+
import { free } from '@nobodyiam/proxy-runtime/assembly/malloc';
3+
import { WasmResultValues, BufferTypeValues, LogLevelValues, log } from "@nobodyiam/proxy-runtime/assembly";
4+
5+
class ArrayBufferReference {
6+
private buffer: usize;
7+
private size: usize;
8+
constructor() {
9+
}
10+
sizePtr(): usize {
11+
return changetype<usize>(this) + offsetof<ArrayBufferReference>("size");
12+
}
13+
bufferPtr(): usize {
14+
return changetype<usize>(this) + offsetof<ArrayBufferReference>("buffer");
15+
}
16+
// Before calling toArrayBuffer below, you must call out to the host to fill in the values.
17+
// toArrayBuffer below **must** be called once and only once.
18+
toArrayBuffer(): ArrayBuffer {
19+
if (this.size == 0) {
20+
return new ArrayBuffer(0);
21+
}
22+
let array = changetype<ArrayBuffer>(this.buffer);
23+
// host code used malloc to allocate this buffer.
24+
// release the allocated ptr. array variable will retain it, so it won't be actually free (as it is ref counted).
25+
free(this.buffer);
26+
// should we return a this sliced up to size?
27+
return array;
28+
}
29+
}
30+
var globalArrayBufferReference = new ArrayBufferReference();
31+
type ptr<T> = usize;
32+
33+
export function registerId(id: string): WasmResultValues {
34+
const idBuffer = String.UTF8.encode(id);
35+
const result = imports.proxy_set_buffer_bytes(BufferTypeValues.CallData, 0, id.length,
36+
changetype<usize>(idBuffer), idBuffer.byteLength);
37+
if (result != WasmResultValues.Ok) {
38+
// @ts-ignore
39+
log(LogLevelValues.critical, `Unable to set http response body: ${id} with result: ${result}`);
40+
}
41+
return result;
42+
}
43+
44+
// @ts-ignore: decorator
45+
@external("env", "proxy_invoke_service")
46+
declare function proxy_invoke_service(
47+
idPtr: ptr<u8>,
48+
idSize: usize,
49+
methodPtr: ptr<u8>,
50+
messageSize: ptr<usize>,
51+
paramPtr: ptr<u8>,
52+
paramSize: usize,
53+
resultPtr: ptr<ptr<u8>>,
54+
resultSize: ptr<usize>,
55+
): u32;
56+
57+
export function invokeService(id: string, method: string, param: string): ArrayBuffer {
58+
const idBuffer = String.UTF8.encode(id);
59+
const methodBuffer = String.UTF8.encode(method);
60+
const paramBuffer = String.UTF8.encode(param);
61+
let result = proxy_invoke_service(
62+
changetype<usize>(idBuffer), idBuffer.byteLength,
63+
changetype<usize>(methodBuffer), methodBuffer.byteLength,
64+
changetype<usize>(paramBuffer), paramBuffer.byteLength,
65+
globalArrayBufferReference.bufferPtr(),
66+
globalArrayBufferReference.sizePtr(),
67+
);
68+
if (result == WasmResultValues.Ok) {
69+
return globalArrayBufferReference.toArrayBuffer();
70+
}
71+
return new ArrayBuffer(0);
72+
}
73+
74+
// @ts-ignore: decorator
75+
@external("env", "proxy_get_state")
76+
declare function proxy_get_state(
77+
storeNamePtr: ptr<u8>,
78+
storeNameSize: usize,
79+
keyPtr: ptr<u8>,
80+
keySize: ptr<usize>,
81+
resultPtr: ptr<ptr<u8>>,
82+
resultSize: ptr<usize>,
83+
): u32;
84+
85+
export function getState(storeName: string, key: string): ArrayBuffer {
86+
const storeNameBuffer = String.UTF8.encode(storeName);
87+
const keyBuffer = String.UTF8.encode(key);
88+
let result = proxy_get_state(
89+
changetype<usize>(storeNameBuffer), storeNameBuffer.byteLength,
90+
changetype<usize>(keyBuffer), keyBuffer.byteLength,
91+
globalArrayBufferReference.bufferPtr(),
92+
globalArrayBufferReference.sizePtr(),
93+
);
94+
if (result == WasmResultValues.Ok) {
95+
return globalArrayBufferReference.toArrayBuffer();
96+
}
97+
return new ArrayBuffer(0);
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
BufferTypeValues,
3+
Context,
4+
FilterDataStatusValues,
5+
get_buffer_bytes,
6+
registerRootContext,
7+
RootContext,
8+
set_http_response_body,
9+
} from "@nobodyiam/proxy-runtime/assembly";
10+
import { getState, registerId } from "./proxy";
11+
12+
export * from "@nobodyiam/proxy-runtime/assembly/proxy";
13+
14+
class ServerRootHttpContext extends RootContext {
15+
createContext(context_id: u32): Context {
16+
return new ServerHttpContext(context_id, this);
17+
}
18+
}
19+
20+
class ServerHttpContext extends Context {
21+
constructor(context_id: u32, root_context: ServerRootHttpContext) {
22+
super(context_id, root_context);
23+
}
24+
25+
onRequestBody(body_buffer_length: usize, _end_of_stream: bool): FilterDataStatusValues {
26+
let name = String.UTF8.decode(get_buffer_bytes(BufferTypeValues.HttpRequestBody, 0, body_buffer_length as u32));
27+
set_http_response_body(String.UTF8.decode(getState("redis", name)));
28+
return FilterDataStatusValues.Continue
29+
}
30+
}
31+
32+
export function _start(): void {
33+
registerRootContext((context_id: u32) => {
34+
return new ServerRootHttpContext(context_id);
35+
}, "");
36+
}
37+
38+
export function proxy_get_id(): void {
39+
registerId("id_2");
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

demo/faas/code/assemblyscript/index.js

-5
This file was deleted.

0 commit comments

Comments
 (0)