Skip to content

Commit

Permalink
feat: Use the native ffi bindings for the Verifier instead of the rub…
Browse files Browse the repository at this point in the history
…y bindings
  • Loading branch information
TimothyJones committed Jun 23, 2021
1 parent e5c7b1a commit 1aea16f
Show file tree
Hide file tree
Showing 18 changed files with 745 additions and 357 deletions.
379 changes: 314 additions & 65 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"chalk": "2.3.1",
"check-types": "7.3.0",
"cross-spawn": "^7.0.1",
"ffi-napi": "^4.0.3",
"libnpmconfig": "^1.2.1",
"mkdirp": "1.0.0",
"pino": "^6.11.0",
Expand All @@ -79,6 +80,7 @@
"@types/cross-spawn": "^6.0.1",
"@types/decompress": "^4.2.3",
"@types/express": "4.11.1",
"@types/ffi-napi": "^2.4.3",
"@types/jest": "^25.2.3",
"@types/mkdirp": "^0.5.2",
"@types/mocha": "2.2.48",
Expand All @@ -90,8 +92,8 @@
"@types/unixify": "^1.0.0",
"@types/unzipper": "^0.10.2",
"@types/url-join": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^2.3.2",
"@typescript-eslint/parser": "^2.3.2",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"basic-auth": "2.0.0",
"body-parser": "1.18.2",
"chai": "4.1.2",
Expand All @@ -106,7 +108,7 @@
"jest": "^26.5.6",
"mocha": "^7.1.1",
"nodemon": "^2.0.4",
"prettier": "^1.18.2",
"prettier": "^2.3.0",
"sinon": "9.2.4",
"snyk": "^1.605.0",
"standard-version": "^9.1.0",
Expand Down
57 changes: 57 additions & 0 deletions src/ffi/ffi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// https://github.com/node-ffi/node-ffi/wiki/Node-FFI-Tutorial
import ffi = require('ffi-napi');

// I am so so sorry about these types. They exist
// to infer the returned type of the library
// using the object that we pass in to describe the functions
type AsyncFfiCall<Args extends unknown[], ReturnType> = {
async: (
...args: [...Args, (err: Error, ret: ReturnType) => void]
) => ReturnType;
};

type FfiFunction<T> = T extends (...a: infer Args) => infer ReturnType
? T & AsyncFfiCall<Args, ReturnType>
: never;

type StringType = 'string' | 'void' | 'int' | 'double' | 'float';

type ActualType<T> = [T] extends ['string']
? string
: [T] extends ['void']
? void
: [T] extends ['int' | 'double' | 'float']
? number
: never;

type ArrayActualType<Tuple extends [...Array<unknown>]> = {
[Index in keyof Tuple]: ActualType<Tuple[Index]>;
} & { length: Tuple['length'] };

type TupleType = [StringType, Array<StringType>];

type FunctionFromArray<A extends TupleType> = A extends [
r: infer ReturnTypeString,
args: [...infer ArgArrayType]
]
? (...args: ArrayActualType<ArgArrayType>) => ActualType<ReturnTypeString>
: never;

type LibDescription<Functions extends string> = {
[k in Functions]: [StringType, Array<StringType>];
};

type FfiBinding<T> = T extends LibDescription<string>
? {
[Key in keyof T]: FfiFunction<FunctionFromArray<T[Key]>>;
}
: never;

// This function exists to wrap the untyped ffi lib, and return
// the typed version
export const initialiseFfi = <T>(
binaryPath: string,
description: T
): FfiBinding<T> =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ffi.Library(binaryPath, description as { [k: string]: any });
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import pact from './pact';
module.exports = exports = pact;
export default pact;

export * from './verifier';
export * from './verifier/verifier';

export * from './server';

Expand Down
3 changes: 2 additions & 1 deletion src/pact.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as path from 'path';
import serverFactory, { Server, ServerOptions } from './server';
import stubFactory, { Stub, StubOptions } from './stub';
import verifierFactory, { VerifierOptions } from './verifier';
import verifierFactory from './verifier/verifier';
import { VerifierOptions } from './verifier/types';
import messageFactory, { MessageOptions } from './message';
import publisherFactory, { PublisherOptions } from './publisher';
import canDeployFactory, {
Expand Down
267 changes: 0 additions & 267 deletions src/verifier.ts

This file was deleted.

Loading

0 comments on commit 1aea16f

Please sign in to comment.