rimless 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/guest.d.ts +6 -0
- package/lib/helpers.d.ts +65 -0
- package/lib/host.d.ts +14 -0
- package/lib/index.d.ts +5 -0
- package/lib/rpc.d.ts +76 -0
- package/lib/types.d.ts +62 -0
- package/package.json +1 -1
package/lib/guest.d.ts
ADDED
package/lib/helpers.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Guest, NodeWorker, Target, WorkerLike } from './types';
|
|
2
|
+
type RuntimeEnvironment = "browser" | "worker" | "node" | "bun";
|
|
3
|
+
export declare function getRuntimeEnvironment(): RuntimeEnvironment;
|
|
4
|
+
export declare function isBrowserEnv(): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* check if run in a webworker
|
|
7
|
+
*
|
|
8
|
+
* @returns boolean
|
|
9
|
+
*/
|
|
10
|
+
export declare function isWorker(): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* check if run in a Node.js environment
|
|
13
|
+
*
|
|
14
|
+
* @returns boolean
|
|
15
|
+
*/
|
|
16
|
+
export declare function isNodeEnv(): boolean;
|
|
17
|
+
export declare function isBunEnv(): boolean;
|
|
18
|
+
export declare function isServerEnv(): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* check if run in an iframe
|
|
21
|
+
*
|
|
22
|
+
* @returns boolean
|
|
23
|
+
*/
|
|
24
|
+
export declare function isIframe(): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* we cannot send functions through postMessage
|
|
27
|
+
* extract the path to all functions in the schema
|
|
28
|
+
*
|
|
29
|
+
* @param obj
|
|
30
|
+
*/
|
|
31
|
+
export declare function extractMethods(obj: any): Record<string, (...args: any) => any>;
|
|
32
|
+
/**
|
|
33
|
+
* convert the url into an origin (remove paths)
|
|
34
|
+
*
|
|
35
|
+
* @param url
|
|
36
|
+
*/
|
|
37
|
+
export declare function getOriginFromURL(url: string | null): string | null;
|
|
38
|
+
export declare function resolveTargetOrigin(origin?: string | null): string;
|
|
39
|
+
export declare function get(obj: any, path: string | Array<string | number>, defaultValue?: any): any;
|
|
40
|
+
export declare function set(obj: any, path: string | (string | number)[], value: any): any;
|
|
41
|
+
export declare function generateId(length?: number): string;
|
|
42
|
+
/**
|
|
43
|
+
* Get the appropriate target host for messaging based on the current environment
|
|
44
|
+
* @returns The messaging target for the current environment
|
|
45
|
+
*/
|
|
46
|
+
export declare function getTargetHost(): any;
|
|
47
|
+
/**
|
|
48
|
+
* Send a message to a target, handling different environments (iframe, web worker, node worker)
|
|
49
|
+
* @param target The target to send the message to
|
|
50
|
+
* @param message The message to send
|
|
51
|
+
* @param origin Optional origin for iframe communication
|
|
52
|
+
* @param transferables Optional transferables for postMessage
|
|
53
|
+
*/
|
|
54
|
+
export declare function postMessageToTarget(target: Target, message: any, origin?: string | null, transferables?: Transferable[]): void;
|
|
55
|
+
export declare function isNodeWorker(guest: Guest | Target): guest is NodeWorker;
|
|
56
|
+
export declare function isWorkerLike(guest: Guest): guest is WorkerLike;
|
|
57
|
+
export declare function addEventListener(target: Target, event: string, handler: EventListenerOrEventListenerObject): void;
|
|
58
|
+
export declare function removeEventListener(target: Target, event: string, handler: EventListenerOrEventListenerObject): void;
|
|
59
|
+
/**
|
|
60
|
+
* Normalize message event data across Web and Node.js environments
|
|
61
|
+
* In web, data is in event.data
|
|
62
|
+
* In Node.js, the event itself contains the data
|
|
63
|
+
*/
|
|
64
|
+
export declare function getEventData(event: any): any;
|
|
65
|
+
export {};
|
package/lib/host.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Connection, Guest, Schema } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Perform a handshake with the target iframe, when the handshake is confirmed
|
|
4
|
+
* resolve the connection object containing RPCs and properties
|
|
5
|
+
*
|
|
6
|
+
* @param guest
|
|
7
|
+
* @param schema
|
|
8
|
+
* @returns Promise
|
|
9
|
+
*/
|
|
10
|
+
declare function connect(guest: Guest, schema?: Schema): Promise<Connection>;
|
|
11
|
+
declare const _default: {
|
|
12
|
+
connect: typeof connect;
|
|
13
|
+
};
|
|
14
|
+
export default _default;
|
package/lib/index.d.ts
ADDED
package/lib/rpc.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Environment, RimlessEvent, Schema, Target } from './types';
|
|
2
|
+
/** Private symbol to which we will assign transferable objects */
|
|
3
|
+
declare const SYM_TRANSFERABLES: unique symbol;
|
|
4
|
+
/**
|
|
5
|
+
* for each function in methods
|
|
6
|
+
* 1. subscribe to an event that the remote can call
|
|
7
|
+
* 2. listen for calls from the remote. When called execute the function and emit the results.
|
|
8
|
+
*
|
|
9
|
+
* @param methods an object of method ids : methods from the local schema
|
|
10
|
+
* @param rpcConnectionID
|
|
11
|
+
* @param listenTo Environment to listen for incoming messages
|
|
12
|
+
* @param sendTo Target to send outgoing messages
|
|
13
|
+
* @param remote The remote API object for the current connection
|
|
14
|
+
* @return a function to cancel all subscriptions
|
|
15
|
+
*/
|
|
16
|
+
export declare function registerLocalMethods(methods: Record<string, (...args: any[]) => any> | undefined, rpcConnectionID: string, listenTo: Environment, sendTo: Target, remote: Schema): () => void;
|
|
17
|
+
/**
|
|
18
|
+
* Create a function that will make an RPC request to the remote with some arguments.
|
|
19
|
+
* Listen to an event that returns the results from the remote.
|
|
20
|
+
*
|
|
21
|
+
* @param rpcCallName
|
|
22
|
+
* @param rpcConnectionID
|
|
23
|
+
* @param event
|
|
24
|
+
* @param listeners
|
|
25
|
+
* @param guest
|
|
26
|
+
*
|
|
27
|
+
* @returns a promise with the result of the RPC
|
|
28
|
+
*/
|
|
29
|
+
export declare function createRPC(rpcCallName: string, rpcConnectionID: string, event: RimlessEvent, listeners: Array<() => void> | undefined, listenTo: Environment, sendTo: Target): (...args: any[]) => Promise<unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* create an object based on the remote schema's methods. Functions in that object will
|
|
32
|
+
* emit an event that will trigger the RPC on the remote.
|
|
33
|
+
*
|
|
34
|
+
* @param schema
|
|
35
|
+
* @param methods
|
|
36
|
+
* @param connectionID
|
|
37
|
+
* @param event
|
|
38
|
+
* @param guest
|
|
39
|
+
*/
|
|
40
|
+
export declare function registerRemoteMethods(schema: Schema | undefined, methodNames: Iterable<string> | undefined, connectionID: string, event: RimlessEvent, listenTo: Environment, sendTo: Target): {
|
|
41
|
+
remote: {
|
|
42
|
+
[x: string]: any;
|
|
43
|
+
};
|
|
44
|
+
unregisterRemote: () => void;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* This function is used by API schema declarations and remote function calls alike to
|
|
48
|
+
* indicate which variables should be declared as transferable over `postMessage` calls.
|
|
49
|
+
*
|
|
50
|
+
* @param cb a function that takes a transfer function as an argument and returns an object
|
|
51
|
+
* (in the loose, `typeof foo === "object"` sense)
|
|
52
|
+
* @return result the callback's return value, with an extra array of transferable objects
|
|
53
|
+
* assigned to rimless' private symbol `SYM_TRANSFERABLES`
|
|
54
|
+
*
|
|
55
|
+
* The `transfer(...)` function is called with an object to transfer; if there
|
|
56
|
+
* are many objects to transfer, you may call it multiple times. It will always
|
|
57
|
+
* return the input object. Calling `transfer` only modifies the callback
|
|
58
|
+
* result, not the transferred object itself (or objects themselves).
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* host.connect({
|
|
62
|
+
* foo: (...args) => {
|
|
63
|
+
* const foo = new ArrayBuffer(8);
|
|
64
|
+
* return withTransferable((transfer) => transfer(foo));
|
|
65
|
+
* }),
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* host.remote.foo(withTransferable((transfer) => ({
|
|
70
|
+
* stream: transfer(new ReadableStream()),
|
|
71
|
+
* })));
|
|
72
|
+
*/
|
|
73
|
+
export declare const withTransferable: <Transferable, Result extends object>(cb: (transfer: <T extends Transferable>(transferable: T) => T) => Result) => Result & {
|
|
74
|
+
[SYM_TRANSFERABLES]: Transferable[];
|
|
75
|
+
};
|
|
76
|
+
export {};
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export interface NodeWorker {
|
|
2
|
+
on(event: string, handler: any): void;
|
|
3
|
+
off(event: string, handler: any): void;
|
|
4
|
+
postMessage(message: any): void;
|
|
5
|
+
terminate(): void;
|
|
6
|
+
}
|
|
7
|
+
export type WorkerLike = Worker | NodeWorker;
|
|
8
|
+
export declare enum events {
|
|
9
|
+
MESSAGE = "message"
|
|
10
|
+
}
|
|
11
|
+
export declare enum actions {
|
|
12
|
+
HANDSHAKE_REQUEST = "RIMLESS/HANDSHAKE_REQUEST",
|
|
13
|
+
HANDSHAKE_REPLY = "RIMLESS/HANDSHAKE_REPLY",
|
|
14
|
+
RPC_REQUEST = "RIMLESS/RPC_REQUEST",
|
|
15
|
+
RPC_RESOLVE = "RIMLESS/RPC_RESOLVE",
|
|
16
|
+
RPC_REJECT = "RIMLESS/RPC_REJECT"
|
|
17
|
+
}
|
|
18
|
+
export type Schema = Record<string, any>;
|
|
19
|
+
export interface Connection {
|
|
20
|
+
id: string;
|
|
21
|
+
remote: Schema;
|
|
22
|
+
close: () => void;
|
|
23
|
+
}
|
|
24
|
+
export type Connections = Record<string, Connection>;
|
|
25
|
+
export interface RimlessEvent extends EventListener {
|
|
26
|
+
source?: Window;
|
|
27
|
+
origin?: string;
|
|
28
|
+
data: HandshakeRequestPayload | HandshakeConfirmationPayload | RPCRequestPayload | RPCResolvePayload;
|
|
29
|
+
}
|
|
30
|
+
export interface HandshakeRequestPayload {
|
|
31
|
+
action: actions.HANDSHAKE_REQUEST;
|
|
32
|
+
connectionID: string;
|
|
33
|
+
methodNames: string[];
|
|
34
|
+
schema: Schema;
|
|
35
|
+
}
|
|
36
|
+
export interface HandshakeConfirmationPayload {
|
|
37
|
+
action: actions.HANDSHAKE_REPLY;
|
|
38
|
+
connectionID: string;
|
|
39
|
+
methodNames: string[];
|
|
40
|
+
schema: Schema;
|
|
41
|
+
}
|
|
42
|
+
export interface RPCRequestPayload {
|
|
43
|
+
action: actions.RPC_REQUEST;
|
|
44
|
+
args: any[];
|
|
45
|
+
callID: string;
|
|
46
|
+
callName: string;
|
|
47
|
+
connectionID: string;
|
|
48
|
+
}
|
|
49
|
+
export interface RPCResolvePayload {
|
|
50
|
+
action: actions.RPC_RESOLVE | actions.RPC_REJECT;
|
|
51
|
+
result?: any | null;
|
|
52
|
+
error?: Error | null;
|
|
53
|
+
callID: string;
|
|
54
|
+
callName: string;
|
|
55
|
+
connectionID: string;
|
|
56
|
+
}
|
|
57
|
+
export interface EventHandlers {
|
|
58
|
+
onConnectionSetup: (remote: Schema) => Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
export type Guest = WorkerLike | HTMLIFrameElement;
|
|
61
|
+
export type Target = Window | WorkerLike;
|
|
62
|
+
export type Environment = Window | WorkerLike;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rimless",
|
|
3
3
|
"author": "Aurélien Franky",
|
|
4
|
-
"version": "0.8.
|
|
4
|
+
"version": "0.8.2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/au-re/rimless",
|
|
7
7
|
"description": "event base communication made easy with a promise-based API wrapping `postMessage`",
|