Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@procedure-rpc/procedure.js",
"version": "0.9.0",
"version": "0.10.0",
"title": "procedure.js",
"description": "The simple RPC framework for Node.js.",
"main": "./dist/index.js",
Expand Down
14 changes: 2 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ export class Procedure<Input = undefined, Output = undefined>

/**
* Initializes a new {@link Procedure}.
* @param {Callback<Input, Output>} callback The underlying callback function powering the procedure itself. The callback may be asynchronous.
* @param {(input: Input) => Output} callback The underlying callback function powering the procedure itself. The callback may be asynchronous.
* @param {Partial<ProcedureDefinitionOptions>} [options] Options for a {@link Procedure}. Defaults to `{}`.
* @template Input Type of input parameter the procedure accepts. Defaults to `undefined`.
* @template Output Type of output value the procedure returns. Defaults to `undefined`.
*/
constructor(
protected callback: Callback<Input, Output>,
protected callback: (input: Input) => Output,
options: Partial<ProcedureDefinitionOptions> = {}
) {
super();
Expand Down Expand Up @@ -470,16 +470,6 @@ export class Procedure<Input = undefined, Output = undefined>
}
export default Procedure;

/**
* Represents a simple callback function which can take a single input parameter.
* @template Input The type of input parameter the callback accepts. Defaults to `undefined`.
* @template Output The type of output value the callback returns. Defaults to `undefined`.
* @see {@link Procedure}
*/
export type Callback<Input = undefined, Output = undefined> = (
input: Input
) => Output;

/**
* A response from a {@link call Procedure call}.
* If the call returned successfully, the response will be of shape `{ output: Output }`, otherwise `{ error: ProcedureError }`.
Expand Down
65 changes: 32 additions & 33 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExtensionCodec } from '@msgpack/msgpack';
import Procedure, { call, ping, tryPing, isPing, Callback } from '../src';
import Procedure, { call, ping, tryPing, isPing } from '../src';
import {
ProcedureErrorCodes,
ProcedureInternalServerError,
Expand Down Expand Up @@ -329,7 +329,7 @@ describe('Procedure', () => {
});

describe('call(endpoint: string, input: Input | null, options: Partial<ProcedureCallOptions>): Promise<Output>', () => {
let fn: Callback<unknown, unknown>;
let fn: ReturnType<typeof jest.fn>;
let procedure: Procedure<unknown, unknown>;
let procedureEndpoint: string;
let input: unknown;
Expand All @@ -339,13 +339,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure
describe('when procedure callback: Callback<number, number> (simple accumulator function)', () => {
beforeEach(() => {
let i = 0;
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return (i += n);
}));
});
procedureEndpoint = 'inproc://Procedure/Add';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -528,13 +528,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure

describe('when procedure callback: Callback<number, null> (testing nullish returns)', () => {
beforeEach(() => {
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return null;
}));
});
procedureEndpoint = 'inproc://Procedure/ReturnsNull';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -717,13 +717,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure

describe('when procedure callback: Callback<number, void> (testing nullish returns)', () => {
beforeEach(() => {
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return;
}));
});
procedureEndpoint = 'inproc://Procedure/ReturnsVoid';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -916,13 +916,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure
describe('when procedure callback: Callback<number, number> (simple accumulator function)', () => {
beforeEach(() => {
let i = 0;
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return (i += n);
}));
});
procedureEndpoint = 'ipc://procedure/add';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -1105,13 +1105,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure

describe('when procedure callback: Callback<number, null> (testing nullish returns)', () => {
beforeEach(() => {
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return null;
}));
});
procedureEndpoint = 'ipc://procedure/returnsnull';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -1294,13 +1294,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure

describe('when procedure callback: Callback<number, void> (testing nullish returns)', () => {
beforeEach(() => {
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return;
}));
});
procedureEndpoint = 'ipc://procedure/returnsvoid';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -1486,13 +1486,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure
describe('when procedure callback: Callback<number, number> (simple accumulator function)', () => {
beforeEach(() => {
let i = 0;
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return (i += n);
}));
});
procedureEndpoint = 'tcp://127.0.0.1:33333';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -1675,13 +1675,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure

describe('when procedure callback: Callback<number, null> (testing nullish returns)', () => {
beforeEach(() => {
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return null;
}));
});
procedureEndpoint = 'tcp://127.0.0.1:33334';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -1864,13 +1864,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure

describe('when procedure callback: Callback<number, void> (testing nullish returns)', () => {
beforeEach(() => {
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return;
}));
});
procedureEndpoint = 'tcp://127.0.0.1:33335';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -2056,13 +2056,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure
describe('when procedure callback: Callback<number, number> (simple accumulator function)', () => {
beforeEach(() => {
let i = 0;
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return (i += n);
}));
});
procedureEndpoint = 'ws://127.0.0.1:33333';
procedure = new Procedure(fn, { workers: 3 });
procedure.bind(procedureEndpoint);
Expand Down Expand Up @@ -2245,13 +2245,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure

describe('when procedure callback: Callback<number, null> (testing nullish returns)', () => {
beforeEach(() => {
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return null;
}));
});

procedureEndpoint = 'ws://127.0.0.1:33334';
procedure = new Procedure(fn, { workers: 3 });
Expand Down Expand Up @@ -2435,13 +2435,13 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure

describe('when procedure callback: Callback<number, void> (testing nullish returns)', () => {
beforeEach(() => {
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return;
}));
});

procedureEndpoint = 'ws://127.0.0.1:33335';
procedure = new Procedure(fn, { workers: 3 });
Expand Down Expand Up @@ -2626,21 +2626,21 @@ describe('call(endpoint: string, input: Input | null, options: Partial<Procedure
});

describe('ping(endpoint: string, timeout: number | undefined = 100, signal?: AbortSignal): Promise<boolean>', () => {
let fn: Callback<unknown, unknown>;
let fn: ReturnType<typeof jest.fn>;
let procedure: Procedure<unknown, unknown>;
let procedureEndpoint: string;
let pingEndpoint: string | undefined;

describe('when procedure callback: Callback<number, number> (simple accumulator function)', () => {
beforeEach(() => {
let i = 0;
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return (i += n);
}));
});

procedureEndpoint = 'inproc://Procedure/Add';
procedure = new Procedure(fn, { workers: 3 });
Expand Down Expand Up @@ -2690,22 +2690,21 @@ describe('ping(endpoint: string, timeout: number | undefined = 100, signal?: Abo
});

describe('tryPing(endpoint: string, timeout: number | undefined = 100, signal?: AbortSignal): Promise<boolean>', () => {
let fn: Callback<unknown, unknown>;

let fn: ReturnType<typeof jest.fn>;
let procedure: Procedure<unknown, unknown>;
let procedureEndpoint: string;
let pingEndpoint: string | undefined;

describe('when procedure callback: Callback<number, number> (simple accumulator function)', () => {
beforeEach(() => {
let i = 0;
fn = jest.fn(<Callback<unknown, unknown>>((n: number) => {
fn = jest.fn((n: number) => {
if (typeof n !== 'number') {
throw new TypeError('Expected a number');
}

return (i += n);
}));
});

procedureEndpoint = 'inproc://Procedure/Add';
procedure = new Procedure(fn, { workers: 3 });
Expand Down