Skip to content

Commit

Permalink
webpack fixes for wasm code (can import it as a library), add invocat…
Browse files Browse the repository at this point in the history
…ion callbacks to launchActor (and cleanup), fix test infra docker

Signed-off-by: ks2211 <[email protected]>
  • Loading branch information
ks2211 committed Aug 31, 2021
1 parent fdbc218 commit 4397914
Show file tree
Hide file tree
Showing 8 changed files with 1,600 additions and 157 deletions.
1,522 changes: 1,516 additions & 6 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
"wasmcloud-rs-js"
],
"scripts": {
"build": "npm run clean && npm run build:browser && npm run build:cjs",
"build:browser": "webpack --mode=production",
"build:cjs": "tsc --declaration && webpack --mode=production --env target=cjs",
"build": "npm run clean && npm run build:browser && npm run build:ts && npm run build:cjs",
"build:browser": "webpack",
"build:ts": "tsc --declaration && npm run build:wasmbundle",
"build:cjs": "webpack --env target=cjs",
"build:wasm": "cd wasmcloud-rs-js && wasm-pack build",
"build:wasmbundle": "webpack --env target=wasm",
"clean": "rm -rf ./dist/ && rm -rf ./wasmcloud-rs-js/pkg/",
"lint": "eslint --ext .ts src test",
"format": "prettier --write 'src/**/*.ts' 'test/**/*.ts'",
Expand Down Expand Up @@ -52,15 +54,17 @@
"author": "ks2211 <[email protected]>",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@types/chai": "^4.2.21",
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.29.2",
"@wasm-tool/wasm-pack-plugin": "^1.5.0",
"babel-loader": "^8.2.2",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"copy-webpack-plugin": "^9.0.1",
"eslint": "^7.32.0",
"mocha": "^9.0.3",
"path": "^0.12.7",
Expand Down
16 changes: 9 additions & 7 deletions src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export class Actor {
hostKey: string;
hostName: string;
wasm: any;
invocationCallback?: Function;

constructor(hostName: string = 'default', hostKey: string, wasm: any) {
constructor(hostName: string = 'default', hostKey: string, wasm: any, invocationCallback?: Function) {
this.key = '';
this.hostName = hostName;
this.hostKey = hostKey;
Expand All @@ -40,6 +41,7 @@ export class Actor {
}
};
this.wasm = wasm;
this.invocationCallback = invocationCallback;
}

async startActor(actorBuffer: Uint8Array) {
Expand Down Expand Up @@ -97,7 +99,7 @@ export class Actor {
);
}

async subscribeInvocations(natsConn: NatsConnection, invocationCallback?: Function) {
async subscribeInvocations(natsConn: NatsConnection) {
// subscribe to topic, wait for invokes, invoke the host, if callback set, send message
const invocationsTopic: Subscription = natsConn.subscribe(`wasmbus.rpc.${this.hostName}.${this.key}`);
for await (const invocationMessage of invocationsTopic) {
Expand All @@ -111,26 +113,26 @@ export class Actor {
msg: invocationResult
})
);
if (invocationCallback) {
invocationCallback(invocationResult);
if (this.invocationCallback) {
this.invocationCallback(invocationResult);
}
}
throw new Error('actor.inovcation subscription closed');
}
}

export async function newActor(
export async function startActor(
hostName: string,
hostKey: string,
actorModule: Uint8Array,
natsConn: NatsConnection,
wasm: any,
invocationCallback?: Function
): Promise<Actor> {
const actor: Actor = new Actor(hostName, hostKey, wasm);
const actor: Actor = new Actor(hostName, hostKey, wasm, invocationCallback);
await actor.startActor(actorModule);
await actor.publishActorStarted(natsConn);
Promise.all([actor.subscribeInvocations(natsConn, invocationCallback)]).catch(err => {
Promise.all([actor.subscribeInvocations(natsConn)]).catch(err => {
throw err;
});
return actor;
Expand Down
14 changes: 9 additions & 5 deletions src/host.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { encode } from '@msgpack/msgpack';
import { connect, ConnectionOptions, NatsConnection, Subscription } from 'nats.ws';

import { Actor, newActor } from './actor';
import { Actor, startActor } from './actor';
import { createEventMessage, EventType } from './events';
import { fetchActor, fetchActorDigest, ImageDigest } from './fetch';
import {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class Host {
heartbeatInterval: number,
natsConnOpts: Array<string> | ConnectionOptions,
wasm: any,
invocationCallbacks?: InvocationCallbacks
invocationCallbacks: InvocationCallbacks
) {
const hostKey = new wasm.HostKey();
this.name = name;
Expand Down Expand Up @@ -109,12 +109,15 @@ export class Host {
this.eventsSubscription = null;
}

async launchActor(actorRef: string) {
async launchActor(actorRef: string, invocationCallback?: Function) {
const actor: LaunchActorMessage = {
actor_ref: actorRef,
host_id: this.key
};
this.natsConn.publish(`wasmbus.ctl.${this.name}.cmd.${this.key}.la`, jsonEncode(actor));
if (invocationCallback) {
this.invocationCallbacks![actorRef] = invocationCallback;
}
}

async stopActor(actorRef: string) {
Expand All @@ -123,6 +126,7 @@ export class Host {
actor_ref: actorRef
};
this.natsConn.publish(`wasmbus.ctl.${this.name}.cmd.${this.key}.sa`, jsonEncode(actorToStop));
delete this.invocationCallbacks![actorRef];
}

async listenLaunchActor() {
Expand All @@ -147,7 +151,7 @@ export class Host {
url = actorRef;
}
const actorModule: Uint8Array = await fetchActor(url);
const actor: Actor = await newActor(
const actor: Actor = await startActor(
this.name,
this.key,
actorModule,
Expand Down Expand Up @@ -241,7 +245,7 @@ export async function startHost(
heartbeatInterval ? heartbeatInterval : HOST_HEARTBEAT_INTERVAL,
natsConnection,
wasm,
invocationCallbacks
invocationCallbacks || {}
);
await host.startHost();
return host;
Expand Down
4 changes: 1 addition & 3 deletions test/infra/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ services:
volumes:
- ./docker-registry.yml:/etc/docker/registry/config.yml
nats:
image: nats:2.1.9
image: nats:latest
ports:
- "6222:6222"
- "4222:4222"
- "8222:8222"
volumes:
- ./nats.conf:/etc/nats.conf
command: "-c=/etc/nats.conf -js"
97 changes: 0 additions & 97 deletions test/infra/nats.conf
Original file line number Diff line number Diff line change
@@ -1,103 +1,6 @@
listen: localhost:4222

websocket {
# host: "hostname"
port: 4222

# This will optionally specify what host:port for websocket
# connections to be advertised in the cluster.
#
# advertise: "host:port"

# TLS configuration is required by default
#
#tls {
#cert_file: "/path/to/cert.pem"
#key_file: "/path/to/key.pem"
#}

# For test environments, you can disable the need for TLS
# by explicitly setting this option to `true`
#
no_tls: true

# [Cross-origin resource sharing option](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
#
# IMPORTANT! This option is used only when the http request presents an Origin
# header, which is the case for web browsers. If no Origin header is present,
# this check will not be performed.
#
# When set to `true`, the HTTP origin header must match the request’s hostname.
# The default is `false`.
#
# same_origin: true

# [Cross-origin resource sharing option](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
#
# IMPORTANT! This option is used only when the http request presents an Origin
# header, which is the case for web browsers. If no Origin header is present,
# this check will not be performed.
#
# List of accepted origins. When empty, and `same_origin` is `false`, clients from any origin are allowed to connect.
# This list specifies the only accepted values for the client's request Origin header. The scheme,
# host and port must match. By convention, the absence of TCP port in the URL will be port 80
# for an "http://" scheme, and 443 for "https://".
#
# allowed_origins [
# "http://www.example.com"
# "https://www.other-example.com"
# ]

# This enables support for compressed websocket frames
# in the server. For compression to be used, both server
# and client have to support it.
#
# compression: true

# This is the total time allowed for the server to
# read the client request and write the response back
# to the client. This includes the time needed for the
# TLS handshake.
#
# handshake_timeout: "2s"

# Name for an HTTP cookie, that if present will be used as a client JWT.
# If the client specifies a JWT in the CONNECT protocol, this option is ignored.
# The cookie should be set by the HTTP server as described [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies).
# This setting is useful when generating NATS `Bearer` client JWTs as the
# result of some authentication mechanism. The HTTP server after correct
# authentication can issue a JWT for the user, that is set securely preventing
# access by unintended scripts. Note these JWTs must be [NATS JWTs](https://docs.nats.io/nats-server/configuration/securing_nats/jwt).
#
# jwt_cookie: "my_jwt_cookie_name"

# If no user name is provided when a websocket client connects, will default
# this user name in the authentication phase. If specified, this will
# override, for websocket clients, any `no_auth_user` value defined in the
# main configuration file.
# Note that this is not compatible with running the server in operator mode.
#
# no_auth_user: "my_username_for_apps_not_providing_credentials"

# See below to know what is the normal way of limiting websocket clients
# to specific users.
# If there are no users specified in the configuration, this simple authorization
# block allows you to override the values that would be configured in the
# equivalent block in the main section.
#
# authorization {
# # If this is specified, the client has to provide the same username
# # and password to be able to connect.
# # username: "my_user_name"
# # password: "my_password"
#
# # If this is specified, the password field in the CONNECT has to
# # match this token.
# # token: "my_token"
#
# # This overrides the main's authorization timeout. For consistency
# # with the main's authorization configuration block, this is expressed
# # as a number of seconds.
# # timeout: 2.0
#}
}
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
{
"include": [
"src/**/*.ts",
"wasmcloud-rs-js/pkg/*"
// "wasmcloud-rs-js/pkg/*"
],
"exclude": [
"node_modules"
],
"compilerOptions": {
"rootDir": "./",
"baseUrl": ".",
"paths": {
"../wasmcloud-rs-js/pkg/": ["./wasmcloud-rs-js/pkg/"]
},
"target": "ES2018",
"module": "commonjs",
"lib": ["es2018", "DOM"],
Expand Down
Loading

0 comments on commit 4397914

Please sign in to comment.