Adding parameter and response validation.

Adding prettier formatting support.
Add warning when no targets are selected.
This commit is contained in:
K35 2022-01-01 00:39:15 +00:00
parent cc6035eef0
commit 7da1cf0841
7 changed files with 248 additions and 132 deletions

View File

@ -1,54 +1,71 @@
import { AddValueRequest, AddValueResponse, Logging } from "./out/index"
import { AddValueRequest, AddValueResponse, Logging } from "./out/index";
// Logging.verbose = false;
import * as Client from "./out/index_client"
import * as Server from "./out/index_server"
import * as Client from "./out/index_client";
import * as Server from "./out/index_server";
const client = new Client.ServiceProvider(msg=>{
const client = new Client.ServiceProvider((msg) => {
session.onMessage(msg);
})
});
const server = new Server.ServiceProvider()
const server = new Server.ServiceProvider();
const session = server.getSession((msg) => {
client.onPacket(msg);
})
});
class TestService extends Server.TestService<undefined> {
async AddValuesSingleParam(request: AddValueRequest, ctx: undefined): Promise<AddValueResponse> {
async AddValuesSingleParam(
request: AddValueRequest,
ctx: undefined
): Promise<AddValueResponse> {
return {
value: request.value1 + request.value2
value: request.value1 + request.value2,
};
}
}
async AddValuesMultipleParams(value1: number, value2: number, ctx: undefined): Promise<number> {
async AddValuesMultipleParams(
value1: number,
value2: number,
ctx: undefined
): Promise<number> {
return value1 + value2;
}
OnEvent(param1: string, ctx: undefined): void {
console.log("Received notification", param1);
}
}
server.addService(new TestService())
server.addService(new TestService());
const test = new Client.TestService(client);
async function run() {
console.log("Testing AddValuesSingleParam")
console.log(await test.AddValuesSingleParam({
console.log("Testing AddValuesSingleParam");
console.log(
await test.AddValuesSingleParam({
value1: 1,
value2: 2
}));
value2: 2,
})
);
console.log("Testing AddValuesMultipleParams")
console.log("Testing AddValuesMultipleParams");
console.log(await test.AddValuesMultipleParams(1, 2));
console.log("Testing Notification")
console.log("Testing Notification");
test.OnEvent("Hi, this is an event");
console.log("Let verification fail!");
await test
//@ts-ignore
.AddValuesMultipleParams("asd", 2)
.then(() => {
console.log("!!!!This should have failed!!!!");
})
.catch((err) => {
console.log("Found expected error!", err.message);
});
}
run();

View File

@ -11,6 +11,7 @@
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/node": "^17.0.5",
"@types/prettier": "^2.4.2",
"@types/yargs": "^17.0.8",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"

View File

@ -143,12 +143,16 @@ export default function startCompile(options: CompileOptions) {
FS.writeFileSync(options.emitDefinitions, JSON.stringify(ir));
}
if(options.targets.length <= 0) {
console.log(Color.yellow("WARNING:"), "No targets selected!");
}
options.targets.forEach(target => {
const tg = Targets.get(target.type) as any
if(!tg) {
console.log(Color.red("ERROR:"), "Target not supported!");
return;
}
compile(ir, new tg(target.output)); //TODO: implement
compile(ir, new tg(target.output));
})
}

View File

@ -6,7 +6,8 @@ import {
Step,
} from "../ir";
import { CompileTarget, } from "../compile";
import { CompileTarget } from "../compile";
import * as prettier from "prettier";
type lineAppender = (ind: number, line: string | string[]) => void;
@ -40,10 +41,7 @@ export class TypescriptTarget extends CompileTarget {
a(
0,
def.depends.map((dep) =>
this.generateImport(
`${dep}, { verify_${dep} }`,
"./" + dep
)
this.generateImport(`${dep}, { verify_${dep} }`, "./" + dep)
)
);
}
@ -53,14 +51,13 @@ export class TypescriptTarget extends CompileTarget {
}
private writeFormattedFile(file: string, code: string) {
this.writeFile(file, code);
//TODO: Add Prettier back
// const formatted = format(code, {
// parser: "typescript",
// tabWidth: 3,
// });
// this.writeFile(file, code);
const formatted = prettier.format(code, {
parser: "typescript",
tabWidth: 3,
});
// this.writeFile(file, formatted);
this.writeFile(file, formatted);
}
generateType(def: TypeDefinition) {
@ -91,23 +88,24 @@ export class TypescriptTarget extends CompileTarget {
a(0, ``);
a(1, `constructor(init?:Partial<${def.name}>){`);
a(2, `if(init){`)
def.fields.forEach(field=>{
a(3, `if(init["${field.name}"])`)
a(4, `this.${field.name} = init["${field.name}"];`)
})
a(2, `if(init){`);
def.fields.forEach((field) => {
a(3, `if(init["${field.name}"])`);
a(4, `this.${field.name} = init["${field.name}"];`);
});
a(2, `}`);
a(1, `}`);
a(0, ``);
a(0, ``);
a(1, `static verify(data: ${def.name}){`);
a(2, `return verify_${def.name}(data);`);
a(1, `}`)
a(0, "}");
a(1, `}`);
a(0, `}`);
a(0, ``);
a(0, `export function verify_${def.name}(data: ${def.name}): boolean {`);
{
@ -117,19 +115,41 @@ export class TypescriptTarget extends CompileTarget {
`if(data["${field.name}"] !== null && data["${field.name}"] !== undefined ) {`
);
const ap: lineAppender = (i, l) => a(i + 2, l);
const verifyType = ( )=>{};
a(2, "// TODO: Implement")
//TODO: Build verification
// if (field.array) {
// a(2, `if(!Array.isArray(data["${field.name}"])) return false`);
// a(2, `if(!(data["${field.name}"].some(e=>))) return false`)
// serializeArray(field, `data["${field.name}"]`, ap);
// } else if (field.map) {
// serializeMap(field, `data["${field.name}"]`, ap);
// } else {
// serializeType(field, `data["${field.name}"]`, true, ap);
// }
const verifyType = (varName: string) => {
switch (field.type) {
case "string":
a(2, `if(typeof ${varName} !== "string") return false;`);
break;
case "number":
a(2, `if(typeof ${varName} !== "number") return false;`);
break;
case "boolean":
a(2, `if(typeof ${varName} !== "boolean") return false;`);
break;
default:
a(
2,
`if(!verify_${field.type}(${varName})) return false;`
);
}
};
if (field.array) {
a(2, `if(!Array.isArray(data["${field.name}"])) return false`);
a(2, `for(const elm of data["${field.name}"]) {`);
verifyType("elm");
a(2, `}`);
} else if (field.map) {
a(
2,
`if(typeof data["${field.name}"] !== "object") return false`
);
a(2, `for(const key in data["${field.name}"]) {`);
verifyType(`data["${field.name}"][key]`);
a(2, `}`);
} else {
verifyType(`data["${field.name}"]`);
}
a(1, "}");
a(0, ``);
});
@ -157,18 +177,25 @@ export class TypescriptTarget extends CompileTarget {
a(0, `export default ${def.name}`);
a(
0,
`export function verify_${def.name} (data: ${def.name}): boolean {`
);
a(0, `export function verify_${def.name} (data: ${def.name}): boolean {`);
a(1, `return ${def.name}[data] != undefined`);
a(0, "}");
this.writeFormattedFile(this.getFileName(def.name), lines.join("\n"));
}
generateServiceClient(def: ServiceDefinition) {
this.writeFile(
"service_client.ts",
this.generateImport(
"{ RequestObject, ResponseObject, ErrorCodes, Logging }",
"./service_base"
) +
"\n\n" +
this.getTemplate("ts_service_client.ts")
);
let lines: string[] = [];
const a: lineAppender = (i, t) => {
if (!Array.isArray(t)) {
@ -185,14 +212,12 @@ export class TypescriptTarget extends CompileTarget {
});
a(0, `}`);
this.writeFile(
"service_client.ts",
a(
0,
this.generateImport(
"{ RequestObject, ResponseObject, ErrorCodes, Logging }",
"{ verify_number, verify_string, verify_boolean }",
"./service_base"
) +
"\n\n" +
this.getTemplate("ts_service_client.ts")
)
);
a(
@ -203,16 +228,18 @@ export class TypescriptTarget extends CompileTarget {
)
);
a(0, ``);
a(0, `export class ${def.name} extends Service {`);
a(1, `constructor(provider: ServiceProvider){`);
a(2, `super(provider, "${def.name}");`);
a(1, `}`);
for (const fnc of def.functions) {
const params = fnc.inputs.map(e=>`${e.name}: ${toJSType(e.type)}`).join(",");
//TODO: Prio 1 : Verify response!
const params = fnc.inputs
.map((e) => `${e.name}: ${toJSType(e.type)}`)
.join(",");
//TODO: Prio 2 : Add optional parameters to this and the declaration file
//TODO: Prio 3 : Maybe verify params? But the server will to this regardless so... Maybe not?`
if (!fnc.return) {
a(1, `${fnc.name}(${params}): void {`);
a(2, `this._provider.sendMessage({`);
@ -224,7 +251,7 @@ export class TypescriptTarget extends CompileTarget {
} else {
const retType = fnc.return ? toJSType(fnc.return) : "void";
a(1, `${fnc.name}(${params}): Promise<${retType}> {`);
a(2, `return new Promise<${retType}>((ok, err) => {`)
a(2, `return new Promise<${retType}>((ok, err) => {`);
a(3, `this._provider.sendMessage({`);
a(4, `jsonrpc: "2.0",`);
a(4, `id: getRandomID(16),`);
@ -233,6 +260,12 @@ export class TypescriptTarget extends CompileTarget {
a(3, `}, {`);
a(4, `ok, err`);
a(3, `});`);
a(2, `}).then(result => {`);
a(
3,
`if(!verify_${fnc.return}(result)) throw new Error("Invalid result data!");`
);
a(3, `return result;`);
a(2, `});`);
a(1, `}`);
}
@ -274,14 +307,17 @@ export class TypescriptTarget extends CompileTarget {
});
a(0, `}`);
a(0, this.generateImport("{ Service }", "./service_server"));
a(
0,
this.generateImport(
"{ Service }",
"./service_server"
"{ verify_number, verify_string, verify_boolean }",
"./service_base"
)
);
a(0, ``);
a(0, `export abstract class ${def.name}<T> extends Service<T> {`);
a(1, `public name = "${def.name}";`);
@ -290,33 +326,46 @@ export class TypescriptTarget extends CompileTarget {
for (const fnc of def.functions) {
a(2, `this.functions.add("${fnc.name}")`);
}
a(1, `}`)
a(1, `}`);
a(0, ``);
for (const fnc of def.functions) {
const params = [...fnc.inputs.map(e=>`${e.name}: ${toJSType(e.type)}`), `ctx: T`].join(", ");
const retVal = fnc.return ? `Promise<${toJSType(fnc.return)}>` : `void`;
a(1, `abstract ${fnc.name}(${params}): ${retVal};`)
const params = [
...fnc.inputs.map((e) => `${e.name}: ${toJSType(e.type)}`),
`ctx: T`,
].join(", ");
const retVal = fnc.return
? `Promise<${toJSType(fnc.return)}>`
: `void`;
a(1, `abstract ${fnc.name}(${params}): ${retVal};`);
// a(0, ``);
a(1, `_${fnc.name}(params: any[] | any, ctx: T): ${retVal} {`);
a(2, `let p: any[] = [];`);
a(2, `if(Array.isArray(params)){`);
//TODO: Verify params!
a(3, `p = params;`);
a(2, `} else {`);
for (const param of fnc.inputs) {
a(3, `p.push(params["${param.name}"])`);
}
a(2, `}`);
a(2, `p.push(ctx);`) //TODO: Either this or [...p, ctx] but idk
a(2, ``);
for(let i = 0; i < fnc.inputs.length; i++) {
a(2, `if(p[${i}] !== null && p[${i}] !== undefined) {`);
a(2, `if(!verify_${fnc.inputs[i].type}(p[${i}])) throw new Error("Parameter verification failed!")`);
a(2, `}`);
}
a(2, ``);
a(2, `p.push(ctx);`);
a(2, `return this.${fnc.name}.call(this, ...p);`);
a(1, `}`)
a(1, `}`);
a(0, ``);
}
a(0, `}`)
a(0, `}`);
this.writeFormattedFile(
this.getFileName(def.name + "_server"),
@ -356,20 +405,31 @@ export class TypescriptTarget extends CompileTarget {
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
};
let hasService = false;
steps.forEach(([type, def]) => {
switch (type) {
case "type":
a(0, this.generateImport(`${def.name}, { verify_${def.name} }`, "./" + def.name))
a(
0,
this.generateImport(
`${def.name}, { verify_${def.name} }`,
"./" + def.name
)
);
a(0, `export { verify_${def.name} }`)
a(0, `export { verify_${def.name} }`);
a(0, `export type { ${def.name} }`);
a(0, ``);
break;
case "enum":
a(0, this.generateImport(`${def.name}, { verify_${def.name} }`, "./" + def.name))
a(0, `export { ${def.name}, verify_${def.name} }`)
a(
0,
this.generateImport(
`${def.name}, { verify_${def.name} }`,
"./" + def.name
)
);
a(0, `export { ${def.name}, verify_${def.name} }`);
a(0, ``);
break;
@ -386,22 +446,24 @@ export class TypescriptTarget extends CompileTarget {
a(0, `export * as Client from "./index_client${ext}"`);
a(0, `export * as Server from "./index_server${ext}"`);
a(0, `export { Logging } from "./service_base${ext}"`);
a(0,``)
//TODO: Export service globals
a(0, ``);
}
ac(0, `export { ${def.name} } from "./${def.name}_client${ext}"`);
as(0, `export { ${def.name} } from "./${def.name}_server${ext}"`);
ac(
0,
`export { ${def.name} } from "./${def.name}_client${ext}"`
);
as(
0,
`export { ${def.name} } from "./${def.name}_server${ext}"`
);
ac(0, ``);
as(0, ``);
break;
}
})
});
this.writeFormattedFile(
this.getFileName("index"),
lines.join("\n")
);
this.writeFormattedFile(this.getFileName("index"), lines.join("\n"));
this.writeFormattedFile(
this.getFileName("index_client"),

View File

@ -2,10 +2,10 @@ export const Logging = {
verbose: false,
log(...args: any[]) {
if (Logging.verbose) {
console.log(...args)
}
}
console.log(...args);
}
},
};
export enum ErrorCodes {
ParseError = -32700,
@ -25,6 +25,24 @@ export interface RequestObject {
export interface ResponseObject {
jsonrpc: "2.0";
result?: any;
error?: { code: ErrorCodes, message:string, data?: any}
error?: { code: ErrorCodes; message: string; data?: any };
id: string;
}
export function verify_number(data: any) {
if (typeof data !== "number") return false;
return true;
}
export function verify_string(data: any) {
if (typeof data !== "string") return false;
return true;
}
export function verify_boolean(data: any) {
if (typeof data !== "boolean") return false;
return true;
}

View File

@ -34,7 +34,13 @@ export class ServiceProvider {
} else {
Logging.log("CLIENT: Determined type is Notification");
//Notification. Send to Notification handler
//TODO: Implement
const [srvName, fncName] = msg.method.split(".");
let service = this.services.get(srvName)
if(!service) {
Logging.log("CLIENT: Did not find Service wanted by Notification!", srvName);
} else {
//TODO: Implement Event thingy (or so :))
}
}
} else {
Logging.log("CLIENT: Determined type is Response");

View File

@ -21,12 +21,13 @@ __metadata:
languageName: node
linkType: hard
"@hibas123/SimpleRPC@workspace:.":
"@hibas123/jrpcgen@workspace:.":
version: 0.0.0-use.local
resolution: "@hibas123/SimpleRPC@workspace:."
resolution: "@hibas123/jrpcgen@workspace:."
dependencies:
"@types/debug": ^4.1.7
"@types/node": ^17.0.5
"@types/prettier": ^2.4.2
"@types/yargs": ^17.0.8
chalk: 4
debug: ^4.3.3
@ -88,6 +89,13 @@ __metadata:
languageName: node
linkType: hard
"@types/prettier@npm:^2.4.2":
version: 2.4.2
resolution: "@types/prettier@npm:2.4.2"
checksum: 76e230b2d11028af11fe12e09b2d5b10b03738e9abf819ae6ebb0f78cac13d39f860755ce05ac3855b608222518d956628f5d00322dc206cc6d1f2d8d1519f1e
languageName: node
linkType: hard
"@types/yargs-parser@npm:*":
version: 20.2.1
resolution: "@types/yargs-parser@npm:20.2.1"