Skip to content

Commit

Permalink
connection verb types
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Sep 8, 2021
1 parent 6473686 commit 4d16a5c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/classes/action.ts
@@ -1,5 +1,6 @@
import { api } from "../index";
import type { LogLevels } from "../modules/log";
import { ConnectionVerb } from "./connection";
import { Inputs } from "./inputs";

/**
Expand Down Expand Up @@ -89,7 +90,7 @@ export abstract class Action {
}
if (
api.connections &&
api.connections.allowedVerbs.indexOf(this.name) >= 0
api.connections.allowedVerbs.indexOf(this.name as ConnectionVerb) >= 0
) {
throw new Error(
`action \`${this.name}\` is a reserved verb for connections. choose a new name`
Expand Down
32 changes: 26 additions & 6 deletions src/classes/connection.ts
Expand Up @@ -2,6 +2,23 @@ import * as uuid from "uuid";
import { api, chatRoom } from "./../index";
import { config } from "./../modules/config";

export const ConnectionVerbs = [
"quit",
"exit",
"paramAdd",
"paramDelete",
"paramView",
"paramsView",
"paramsDelete",
"roomAdd",
"roomLeave",
"roomView",
"detailsView",
"documentation",
"say",
] as const;
export type ConnectionVerb = typeof ConnectionVerbs[number];

/**
* The generic representation of a connection for all server types is an Actionhero.Connection. You will never be creating these yourself via an action or task, but you will find them in your Actions and Action Middleware.
*/
Expand Down Expand Up @@ -153,7 +170,10 @@ export class Connection {
/**
* Send a message to a connection. Uses Server#sendMessage.
*/
async sendMessage(message: string | object | Array<any>, verb?: string) {
async sendMessage(
message: string | object | Array<any>,
verb?: ConnectionVerb
) {
throw new Error("not implemented");
}

Expand Down Expand Up @@ -199,14 +219,10 @@ export class Connection {
delete api.connections.connections[this.id];
}

private set(key, value) {
this[key] = value;
}

/**
* Try to run a verb command for a connection
*/
private async verbs(verb: string, words: Array<string>) {
async verbs(verb: ConnectionVerb, words: Array<string>) {
let key: string;
let value: string;
let room: string;
Expand Down Expand Up @@ -299,6 +315,10 @@ export class Connection {
throw error;
}
}

private set(key: string, value: any) {
this[key] = value;
}
}

export interface ConnectionParams {
Expand Down
5 changes: 3 additions & 2 deletions src/classes/server.ts
Expand Up @@ -2,7 +2,7 @@ import { EventEmitter } from "events";
import { api, config } from "../index";
import { log, LogLevels } from "../modules/log";
import { ActionProcessor } from "./actionProcessor";
import { Connection } from "./connection";
import { Connection, ConnectionVerb } from "./connection";

interface ServerConfig {
[key: string]: any;
Expand All @@ -15,14 +15,15 @@ export abstract class Server extends EventEmitter {
/**The name & type of the server. */
type: string;
/**What connection verbs can connections of this type use? */
verbs?: Array<string>;
verbs?: ConnectionVerb[];
/**Shorthand for `api.config.servers[this.type]` */
config?: ServerConfig;
options?: {
[key: string]: any;
};
/** attributes of the server */
attributes: {
verbs?: ConnectionVerb[];
[key: string]: any;
};
/**Can connections of this server use the chat system? */
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Expand Up @@ -2,7 +2,11 @@
export { Api } from "./classes/api";
export { Process } from "./classes/process";
export { Initializer } from "./classes/initializer";
export { Connection } from "./classes/connection";
export {
Connection,
ConnectionVerb,
ConnectionVerbs,
} from "./classes/connection";
export { ExceptionReporter } from "./classes/exceptionReporter";
export { Action } from "./classes/action";
export { Task } from "./classes/task";
Expand Down
28 changes: 10 additions & 18 deletions src/initializers/connections.ts
@@ -1,4 +1,11 @@
import { api, redis, Initializer, Connection } from "../index";
import {
api,
redis,
Initializer,
Connection,
ConnectionVerbs,
ConnectionVerb,
} from "../index";

/**
* ```js
Expand Down Expand Up @@ -33,7 +40,7 @@ export interface ConnectionsApi {
[key: string]: ConnectionMiddleware;
};
globalMiddleware: Array<string>;
allowedVerbs: Array<string>;
allowedVerbs: ConnectionVerb[];
cleanConnection: Function;
apply: (
connectionId: string,
Expand All @@ -55,22 +62,7 @@ export class Connections extends Initializer {
connections: {},
middleware: {},
globalMiddleware: [],

allowedVerbs: [
"quit",
"exit",
"documentation",
"paramAdd",
"paramDelete",
"paramView",
"paramsView",
"paramsDelete",
"roomAdd",
"roomLeave",
"roomView",
"detailsView",
"say",
],
allowedVerbs: [...ConnectionVerbs],

/**
* Find a connection on any server in the cluster and call a method on it.
Expand Down
3 changes: 2 additions & 1 deletion src/initializers/specHelper.ts
@@ -1,4 +1,5 @@
import * as uuid from "uuid";
import { ConnectionVerbs } from "../classes/connection";
import { api, log, env, Initializer, Server, Connection } from "../index";

export interface SpecHelperApi {
Expand Down Expand Up @@ -41,7 +42,7 @@ export class SpecHelper extends Initializer {
logConnections: false,
logExits: false,
sendWelcomeMessage: true,
verbs: api.connections.allowedVerbs,
verbs: [...ConnectionVerbs],
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/servers/web.ts
Expand Up @@ -392,7 +392,7 @@ export class WebServer extends Server {
}
}

handleRequest(req, res) {
handleRequest(req: http.IncomingMessage, res: http.ServerResponse) {
const { fingerprint, headersHash } = this.fingerPrinter.fingerprint(req);
const responseHeaders = [];
const cookies = utils.parseCookies(req);
Expand Down

0 comments on commit 4d16a5c

Please sign in to comment.