diff --git a/package-lock.json b/package-lock.json index 47661d2fa..71cc83b2d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1643,9 +1643,9 @@ "dev": true }, "@types/node": { - "version": "14.11.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.2.tgz", - "integrity": "sha512-jiE3QIxJ8JLNcb1Ps6rDbysDhN4xa8DJJvuC9prr6w+1tIh+QAbYyNF3tyiZNLDBIuBCf4KEcV2UvQm/V60xfA==", + "version": "14.14.6", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.6.tgz", + "integrity": "sha512-6QlRuqsQ/Ox/aJEQWBEJG7A9+u7oSYl3mem/K8IzxXG/kAGbV1YPD9Bg9Zw3vyxC/YP+zONKwy8hGkSt1jxFMw==", "dev": true }, "@types/normalize-package-data": { diff --git a/packages/ergo-compiler/package.json b/packages/ergo-compiler/package.json index 4f2171f4a..50dffd6c2 100644 --- a/packages/ergo-compiler/package.json +++ b/packages/ergo-compiler/package.json @@ -8,13 +8,15 @@ "directory": "packages/ergo-compiler" }, "main": "index.js", + "typings": "types/index.d.ts", "scripts": { "pretest": "npm run lint", "lint": "eslint .", "postlint": "npm run licchk", "licchk": "license-check", "test": "mocha", - "test:cov": "nyc npm run test" + "test:cov": "nyc npm run test", + "types:check": "tsc -p types/tsconfig.json" }, "contributors": [ { @@ -39,6 +41,7 @@ "winston": "3.2.1" }, "devDependencies": { + "@types/node": "14.14.6", "chai": "4.2.0", "chai-as-promised": "7.1.1", "chai-things": "0.2.0", @@ -48,7 +51,8 @@ "mocha": "5.2.0", "nyc": "13.3.0", "sinon": "6.3.5", - "sinon-chai": "3.2.0" + "sinon-chai": "3.2.0", + "typescript": "4.0.5" }, "license-check-config": { "src": [ @@ -89,4 +93,4 @@ "lines": 93 }, "gitHead": "bf65386e8590dff56f8b9bc0bb657263ff049437" -} \ No newline at end of file +} diff --git a/packages/ergo-compiler/types/index.d.ts b/packages/ergo-compiler/types/index.d.ts new file mode 100644 index 000000000..c16d1cbbe --- /dev/null +++ b/packages/ergo-compiler/types/index.d.ts @@ -0,0 +1,261 @@ +/// +/// +/// + +import { Logger, LeveledLogMethod } from 'winston'; +import { BaseFileException, Factory, Introspector, ModelManager, Serializer } from '@accordproject/concerto-core' + +// Winston logger + +declare module 'winston' { + interface Logger { + setup(fn: (process: any, env: string, logDir: string) => void): void; + entry: LeveledLogMethod; + exit: LeveledLogMethod; + } +} + +// Utils + +declare namespace Util { + function momentToJson(): any; + function setCurrentTime(currentTime: string): any; +} + +// Version + +declare const version: string + +// Exceptions + +declare interface FileLocation { + start: { line: number, column: number }; + end: { line: number, column: number }; +} + +declare class CompilerException extends BaseFileException { + constructor(message: string, fileLocation?: FileLocation, fullMessage?: string, fileName?: string, component?: string); +} + +declare class TypeException extends CompilerException { + constructor(message: string, fileLocation?: FileLocation, fullMessage?: string, fileName?: string, component?: string); +} + +// Target + +declare type Target = 'cicero' | 'es5' | 'es6' | 'java'; + +// Source + +declare interface Source { + name: string; + content: string; +} + +// Ergo Input +declare interface Input { + $class: string; +} + +// AP Model Manager + +declare abstract class APModelManager extends ModelManager { + getModels(): { name: string; content: string; }[]; +} + +// Function Declaration + +declare class FunctionDeclaration { + constructor( + modelManager: ModelManager, + language: string, + name: string, + visibility: string, + returnType: string, + throws: string, + parameterNames: string[], + parameterTypes: string[], + decorators: string[], + functionText: string + ); + getFunctionText(): string; + getThrows(): string; + getLanguage(): string; + getDecorators(): string[]; + getVisibility(): string; + getReturnType(): string; + getName(): string; + getParameterNames(): string[]; + getParameterTypes(): string[]; +} + +// Script + +declare class Script { + constructor( + modelManager: ModelManager, + identifier: string, + language: string, + contents: string, + contractName: string + ); + getIdentifier(): string; + getContractName(): string; + getLanguage(): string; + getContents(): string; + getFunctionDeclarations(): FunctionDeclaration[]; + getTokens(): any[]; +} + +// ScriptManager + +declare interface ScriptManagerOptions { + warnings?: boolean +} + +declare class ScriptManager { + constructor(target: Target, modelManager: ModelManager, options: ScriptManagerOptions); + changeTarget(target: Target, recompile: boolean): void; + createScript(identifier: string, language: string, contents: string): Script; + modifyScript(identifier: string, language: string, contents: string): void; + addTemplateFile(templateFile: string, fileName: string): void + addScript(script: Script): void; + updateScript(script: Script): void; + deleteScript(identifier: string): void; + getScripts(): Script[]; + getAllScripts(): Script[]; + getCombinedScripts(): string; + getTargetKind(target: Target): '.js' | '.ergo' | '.java'; + getScriptsForTarget(target: Target): Script[]; + getLogic(): Source[]; + clearScripts(): void; + private getScript(identifier: string): Script; + private getCompiledScript(): Script; + private getCompiledJavaScript(): string; + getScriptIdentifiers(): string[]; + compileLogic(force: boolean): Script; + allFunctionDeclarations(): FunctionDeclaration[]; + hasFunctionDeclaration(name: string): void; + hasDispatch(): void; + hasInit(): void; + + static _throwCompilerException(error: any): void; +} + +// Logic Manager + +declare interface LogicManagerOptions { + warnings?: boolean +} + +declare class LogicManager { + constructor(target: Target, options: LogicManagerOptions); + getTarget(): Target; + setTarget(target: Target, recompile: boolean): void; + setContractName(contractName: string): void; + getContractName(): string; + private getDispatchCall(): string; + private getInvokeCall(clauseName: string): string + getIntrospector(): Introspector; + getFactory(): Factory; + getSerializer(): Serializer; + getScriptManager(): ScriptManager; + getModelManager(): ModelManager; + addLogicFile(logicFile: string, fileName: string): void; + addTemplateFile(modelFile: string, fileName: string): void; + addModelFile(modelFile: string, fileName: string): void; + addModelFiles(modelFiles: string[], modelFileNames?: string[]): void; + validateModelFiles(): void; + registerCompiledLogicSync(): void; + compileLogicSync(force: boolean): Script; + compileLogic(force: boolean): Promise; + addErgoBuiltin(): void; + validateInput(input: any): any; + validateContract(contract: any, options: any): any; + validateInputRecord(input: any): any; + validateOutput(output: any): any; + validateOutputArray(outputArray: any[]): any[]; + updateModel(content: string, name: string): void; + updateLogic(content: string, name: string): void; +} + +// Compiler + +declare class Compiler { + static parseCTOtoJSON(ctoContent: string): any; + static contractCallName(contractName: string): string; + static contractCallNamePromise(contractName: string): string; + + static compileToJavaScript( + ergoSources: Source[], + ctoSources: Source[], + target: string, + link: boolean, + warnings: boolean + ): string; + + static compile( + ergoSources: Source[], + ctoSources: Source[], + target: string, + link: boolean, + warnings: boolean + ): any; + + static ergoErrorToString(error: any): string; + static ergoVerboseErrorToString(error: any): string; + static availableTargets(): string[]; + static isValidTarget(target: string): boolean; +} + +// File Loader + +declare class FileLoader { + static loadZipFileContents(zip: any, path: string, json?: boolean, required?: boolean): Promise; + static loadZipFilesContents(zip: any, regex: RegExp): Promise; + static loadZipFileBuffer(zip: any, path: string, required?: boolean): Promise; + static loadFileContents(path: string, fileName: string, json?: boolean, required?: boolean): Promise; + static loadFileBuffer(path: string, fileName: string, required?: boolean): Promise; + static loadFilesContents(path: string, regex: RegExp): Promise + static normalizeNLs(input: string): string; +} + +// Ergo Loader + +declare class ErgoLoader { + static fromDirectory(path: string, options: LogicManagerOptions): Promise; + static fromZip(buffer: Buffer, options: LogicManagerOptions): Promise; + static fromFiles(files: string[], options: LogicManagerOptions): Promise; +} + +// Exports + +export { + TypeException, + CompilerException, + APModelManager, + ScriptManager, + LogicManager, + Compiler, + FileLoader, + ErgoLoader, + Util, + Logger, + version, +} + +declare module '@accordproject/ergo-compiler' { + export { + TypeException, + CompilerException, + APModelManager, + ScriptManager, + LogicManager, + Compiler, + FileLoader, + ErgoLoader, + Util, + Logger, + version, + } +} diff --git a/packages/ergo-compiler/types/tsconfig.json b/packages/ergo-compiler/types/tsconfig.json new file mode 100644 index 000000000..fd8abbb42 --- /dev/null +++ b/packages/ergo-compiler/types/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "noEmit": true, + } +} \ No newline at end of file diff --git a/packages/ergo-engine/package.json b/packages/ergo-engine/package.json index 4478c5ec0..7304038cd 100644 --- a/packages/ergo-engine/package.json +++ b/packages/ergo-engine/package.json @@ -8,6 +8,7 @@ "directory": "packages/ergo-engine" }, "main": "index.js", + "typings": "types/index.d.ts", "scripts": { "webpack": "webpack --config webpack.config.js --mode production", "prepublishOnly": "npm run webpack", @@ -16,7 +17,8 @@ "postlint": "npm run licchk", "licchk": "license-check", "test": "mocha", - "test:cov": "nyc npm run test" + "test:cov": "nyc npm run test", + "types:check": "tsc -p types/tsconfig.json" }, "contributors": [ { @@ -42,6 +44,7 @@ "@babel/cli": "7.1.5", "@babel/core": "7.1.6", "@babel/preset-env": "7.1.6", + "@types/node": "14.14.6", "archiver": "2.1.1", "babel-core": "7.0.0-bridge.0", "babel-loader": "8.0.4", @@ -55,6 +58,7 @@ "mocha": "5.2.0", "nyc": "13.3.0", "raw-loader": "0.5.1", + "typescript": "4.0.5", "webpack": "4.16.3", "webpack-cli": "3.1.0" }, @@ -93,4 +97,4 @@ "lines": 93 }, "gitHead": "bf65386e8590dff56f8b9bc0bb657263ff049437" -} \ No newline at end of file +} diff --git a/packages/ergo-engine/types/index.d.ts b/packages/ergo-engine/types/index.d.ts new file mode 100644 index 000000000..3b1a5fd0a --- /dev/null +++ b/packages/ergo-engine/types/index.d.ts @@ -0,0 +1,59 @@ +/// +/// + +import { VMScript } from 'vm2' +import { ScriptManager, LogicManager } from '@accordproject/ergo-compiler' + +declare const version: string + +declare interface TriggerOutput { + clause: string; + request: any; + response: any; + state: any; + emit: any; +} + +declare interface InvokeOutput { + clause: string; + params: any; + response: any; + state: any; + emit: any; +} + +declare class Engine { + kind(): string; + compileVMScript(script: string): void; + runVMScriptCall(utcOffset: number, now: any, options: any, context: any, script: any, call: any): void; + clearCacheJsScript(): void; + cacheJsScript(scriptManager: ScriptManager, contractId: string): VMScript; + trigger(logic: LogicManager, contractId: string, contract: any, request: any, state: any, currentTime: string, options: any): TriggerOutput; + invoke(logic: LogicManager, contractId: string, clauseName: string, contract: any, params: any, state: any, currentTime: string, options: any): InvokeOutput; + init(logic: LogicManager, contractId: string, contract: any, params: any, currentTime: string, options: any): InvokeOutput; + calculate(logic: LogicManager, contractId: string, name: string, contract: any, currentTime: string, options: any): InvokeOutput; + compileAndInit(logic: LogicManager, contract: any, params: any, currentTime: string, options: any): Promise; + compileAndCalculate(logic: LogicManager, name: string, contract: any, currentTime: string, options: any): Promise; + compileAndInvoke(logic: LogicManager, clauseName: string, contract: any, params: any, state: any, currentTime: string, options: any): Promise; + compileAndTrigger(logic: LogicManager, contract: any, request: any, state: any, currentTime: string, options: any): Promise; +} + +declare class VMEngine extends Engine { + kind(): 'vm2'; + compileVMScript(script: string): VMScript; + runVMScriptCall(utcOffset: number, now: any, options: any, context: any, script: any, call: any): any; +} + +export { + Engine, + VMEngine, + version, +} + +declare module '@accordproject/ergo-engine' { + export { + Engine, + VMEngine, + version, + } +} \ No newline at end of file diff --git a/packages/ergo-engine/types/tsconfig.json b/packages/ergo-engine/types/tsconfig.json new file mode 100644 index 000000000..56b855545 --- /dev/null +++ b/packages/ergo-engine/types/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "noEmit": true + } +} \ No newline at end of file