Skip to content

Ks2211/prettier #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json.schemastore.org/prettierrc",
"trailingComma": "all",
"trailingComma": "none",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
Expand Down
1,536 changes: 1,523 additions & 13 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 13 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"name": "@wasmcloud/wasmcloud-js",
"version": "1.0.3",
"version": "1.0.4",
"description": "wasmcloud host in JavaScript/Browser",
"main": "dist/wasmcloud.js",
"browser": "dist/wasmcloud.js",
"module": "dist/src/index.js",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"files": [
"dist",
Expand All @@ -13,21 +11,24 @@
"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'",
"test": "mocha",
"watch": "npm run clean && tsc -w --declrataion",
"prepare": "npm run build"
},
"prettier": ".prettierrc.json",
"prettier": "./.prettierrc.json",
"keywords": [
"wasmcloud",
"wasmcloud-host",
"wasmcloud-js",
"wasm"
],
"eslintConfig": {
Expand All @@ -53,18 +54,21 @@
"author": "ks2211 <kaushik@cosmonic.com>",
"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",
"prettier": "^2.3.2",
"ts-loader": "^9.2.5",
"ts-node": "^10.2.1",
"typescript": "^4.3.5",
Expand Down
64 changes: 38 additions & 26 deletions src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { NatsConnection, Subscription } from 'nats.ws';

import { createEventMessage, EventType } from './events';
import {
ActorClaims, ActorClaimsMessage, ActorStartedMessage, ActorHealthCheckPassMessage,
ActorClaims,
ActorClaimsMessage,
ActorStartedMessage,
ActorHealthCheckPassMessage,
InvocationMessage,
StopActorMessage
} from './types';
Expand All @@ -17,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 @@ -37,6 +41,7 @@ export class Actor {
}
};
this.wasm = wasm;
this.invocationCallback = invocationCallback;
}

async startActor(actorBuffer: Uint8Array) {
Expand All @@ -54,8 +59,8 @@ export class Actor {
const actorToStop: StopActorMessage = {
host_id: this.hostKey,
actor_ref: this.key
}
natsConn.publish(`wasmbus.ctl.${this.hostName}.cmd.${this.hostKey}.sa`, jsonEncode(actorToStop))
};
natsConn.publish(`wasmbus.ctl.${this.hostName}.cmd.${this.hostKey}.sa`, jsonEncode(actorToStop));
}

async publishActorStarted(natsConn: NatsConnection) {
Expand All @@ -69,59 +74,66 @@ export class Actor {
sub: this.claims.sub,
tags: '',
version: this.claims.wascap.ver
}
natsConn.publish(`lc.${this.hostName}.claims.${this.key}`, jsonEncode(claims))
};
natsConn.publish(`lc.${this.hostName}.claims.${this.key}`, jsonEncode(claims));

// publish actor_started
const actorStarted: ActorStartedMessage = {
api_version: 0,
instance_id: uuidv4(),
public_key: this.key
}
natsConn.publish(`wasmbus.evt.${this.hostName}`, jsonEncode(createEventMessage(this.hostKey, EventType.ActorStarted, actorStarted)));
};
natsConn.publish(
`wasmbus.evt.${this.hostName}`,
jsonEncode(createEventMessage(this.hostKey, EventType.ActorStarted, actorStarted))
);

// publish actor health_check
const actorHealthCheck: ActorHealthCheckPassMessage = {
instance_id: uuidv4(),
public_key: this.key
}
natsConn.publish(`wasmbus.evt.${this.hostName}`, jsonEncode(createEventMessage(this.hostKey, EventType.HealthCheckPass, actorHealthCheck)));

};
natsConn.publish(
`wasmbus.evt.${this.hostName}`,
jsonEncode(createEventMessage(this.hostKey, EventType.HealthCheckPass, actorHealthCheck))
);
}

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) {
const invocationData = decode(invocationMessage.data);
const invocation: InvocationMessage = (invocationData as InvocationMessage)
const invocation: InvocationMessage = invocationData as InvocationMessage;
const invocationResult: Uint8Array = await this.module.invoke(invocation.operation, invocation.msg);
invocationMessage.respond(encode({
invocation_id: (invocationData as any).id,
instance_id: uuidv4(),
msg: invocationResult
}));
if (invocationCallback) {
invocationCallback(invocationResult);
invocationMessage.respond(
encode({
invocation_id: (invocationData as any).id,
instance_id: uuidv4(),
msg: invocationResult
})
);
if (this.invocationCallback) {
this.invocationCallback(invocationResult);
}
}
throw new Error('actor.inovcation subscription closed');
}
}

export async function newActor(hostName: string, hostKey: string,
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;
}
}
10 changes: 5 additions & 5 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { uuidv4 } from './util'
import { uuidv4 } from './util';

export enum EventType {
HeartBeat = 'com.wasmcloud.lattice.host_heartbeat',
ActorStarted = 'com.wasmcloud.lattice.actor_started',
ActorStopped = 'com.wasmcloud.lattice.actor_stopped',
HealthCheckPass = 'com.wasmcloud.lattice.health_check_passed',
HealthCheckPass = 'com.wasmcloud.lattice.health_check_passed'
}

export type EventData = {
Expand All @@ -15,7 +15,7 @@ export type EventData = {
time: string;
type: EventType;
data: any;
}
};

export function createEventMessage(hostKey: string, eventType: EventType, data: any): EventData {
return {
Expand All @@ -26,5 +26,5 @@ export function createEventMessage(hostKey: string, eventType: EventType, data:
specversion: '1.0',
time: new Date().toISOString(),
type: eventType
}
}
};
}
30 changes: 16 additions & 14 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type ImageDigest = {
name: string;
digest: string;
registry: string;
}
};

type FetchActorDigestResponse = {
schemaVersion: number;
Expand All @@ -19,7 +19,7 @@ type FetchActorDigestResponse = {
layers: Array<{
annotations: {
['org.opencontainers.image.title']: string;
}
};
digest: string;
mediaType: string;
size: number;
Expand All @@ -33,13 +33,15 @@ export async function fetchActorDigest(actorRef: string, withTLS?: boolean): Pro
const registry: string = image[0];
const [name, version] = image[1].split(':');

const response: AxiosResponse = await axios.get(
`${withTLS ? 'https://' : 'http://'}${registry}/v2/${name}/manifests/${version}`,
{
const response: AxiosResponse = await axios
.get(`${withTLS ? 'https://' : 'http://'}${registry}/v2/${name}/manifests/${version}`, {
headers: {
'Accept': 'application/vnd.oci.image.manifest.v1+json'
Accept: 'application/vnd.oci.image.manifest.v1+json'
}
}).catch((err) => { throw err });
})
.catch(err => {
throw err;
});
const layers: FetchActorDigestResponse = response.data;

if (layers.layers.length === 0) {
Expand All @@ -50,17 +52,17 @@ export async function fetchActorDigest(actorRef: string, withTLS?: boolean): Pro
name,
digest: layers.layers[0].digest,
registry
}
};
}

export async function fetchActor(url: string): Promise<Uint8Array> {
const response: AxiosResponse = await axios.get(
url,
{
const response: AxiosResponse = await axios
.get(url, {
responseType: 'arraybuffer'
}
).catch((err) => { throw err });
})
.catch(err => {
throw err;
});

return new Uint8Array(response.data);
}

Loading