osra 0.1.2 → 0.2.0

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/README.md CHANGED
@@ -1,60 +1,55 @@
1
1
  # osra
2
- what that? https://github.com/GoogleChromeLabs/comlink but nicer to use.
2
+ whats that? A powerful communication library that's really easy to use.
3
3
  thats about it
4
4
 
5
- how to?
5
+ how?
6
6
  register your functions in the other context like so
7
7
  ```ts
8
- import { makeCallListener, registerListener } from 'osra'
8
+ import { expose } from 'osra'
9
9
 
10
10
  const resolvers = {
11
- 'test': makeCallListener(async (myData) => {
11
+ test: async (myData) => {
12
12
  // do work...
13
13
  return {
14
14
  foo: 1,
15
15
  bar: 'bar',
16
16
  baz: () => true
17
17
  }
18
- })
18
+ }
19
19
  }
20
20
 
21
- registerListener({ target: globalThis, resolvers })
21
+ export type Resolvers = typeof resolvers
22
+
23
+ expose(resolvers, { local: globalThis, remote: globalThis })
22
24
  ```
23
25
 
24
- and on your current context you can call it easily like
26
+ and on your current context with full typescript support you can call it easily like
25
27
  ```ts
26
- import { call } from 'osra'
28
+ import type { Resolvers } from './worker.ts'
27
29
 
28
- const worker = new Worker('/worker.js', { type: 'module' })
30
+ import { expose } from 'osra'
29
31
 
30
- call(worker)('test', { theDataThatWillBeSentToTheOtherContext: 1 })
31
- .then(({ foo, bar, baz }) => {
32
- // foo === 1
33
- // bar === 'bar'
34
- // baz === callable function that will return a promise with its response
35
- })
36
- ```
32
+ const worker = new Worker('/worker.js', { type: 'module' })
37
33
 
38
- all types of data supported by osra that will be correctly proxied/sent to the other context in addition of functions:
34
+ const { test } = await expose<Resolvers>({}, { local: worker, remote: worker })
39
35
 
40
- ```ts
41
- export type TransferableObject =
42
- ArrayBuffer | MessagePort | ReadableStream | WritableStream |
43
- TransformStream | /* AudioData | */ ImageBitmap /* | VideoFrame | OffscreenCanvas */
36
+ const { foo, bar, baz } = test()
37
+ // foo === 1
38
+ // bar === 'bar'
39
+ // baz === callable function that will return a promise with its response
40
+ ```
44
41
 
45
- export interface StructuredCloneObject {
46
- [key: string | number | symbol]: StructuredCloneType
47
- }
42
+ Supports almost any JS types like Promises, Functions, Streams, ect... and plans to support plugins for custom types.
43
+ From efficient transferable messaging transport to JSON only, it always works.
48
44
 
49
- export type StructuredCloneType =
50
- boolean | null | undefined | number | BigInt | string | Date | RegExp | Blob | File | FileList | ArrayBuffer | ArrayBufferView |
51
- ImageBitmap | ImageData | Array<StructuredCloneType> | StructuredCloneObject | Map<StructuredCloneType, StructuredCloneType> | Set<StructuredCloneType>
45
+ Todo:
52
46
 
53
- export interface StructuredCloneTransferableObject {
54
- [key: string | number | symbol]: StructuredCloneTransferableType
55
- }
47
+ docs about:
56
48
 
57
- export type StructuredCloneTransferableType =
58
- StructuredCloneType | TransferableObject | Array<StructuredCloneTransferableType> | StructuredCloneTransferableObject |
59
- Map<StructuredCloneTransferableType, StructuredCloneTransferableType> | Set<StructuredCloneTransferableType>
60
- ```
49
+ * Protocol mode:
50
+ * - Bidirectional mode
51
+ * - Unidirectional mode
52
+ * Transport modes:
53
+ * - Capable mode
54
+ * - Jsonable mode
55
+ * Revivables
package/build/index.d.ts CHANGED
@@ -1,8 +1,155 @@
1
- import type { RemoteTarget, LocalTarget, OsraMessage, StructuredCloneTransferableProxiableType } from './types';
2
- export * from './utils';
3
- export declare const expose: <T extends StructuredCloneTransferableProxiableType>(value: StructuredCloneTransferableProxiableType, { remote: _remote, local: _local, key, origin }: {
4
- remote: RemoteTarget | ((osraMessage: OsraMessage, transferables: Transferable[]) => void);
5
- local: LocalTarget | ((listener: (event: MessageEvent<OsraMessage>) => void) => void);
6
- key?: string;
7
- origin?: string;
8
- }) => Promise<T>;
1
+ declare type BidirectionalConnectionMessage = {
2
+ type: 'init';
3
+ remoteUuid: Uuid;
4
+ data: Capable;
5
+ }
6
+ /** message not needed if transferring MessagePort is supported */
7
+ | {
8
+ type: 'message';
9
+ remoteUuid: Uuid;
10
+ data: Capable;
11
+ /** uuid of the messagePort that the message was sent through */
12
+ portId: Uuid;
13
+ }
14
+ /** message not needed if transferring MessagePort is supported */
15
+ | {
16
+ type: 'message-port-close';
17
+ remoteUuid: Uuid;
18
+ /** uuid of the messagePort that closed */
19
+ portId: string;
20
+ };
21
+
22
+ declare type Capable = Structurable | TransferBox | Transferable | Revivable | {
23
+ [key: string]: Capable;
24
+ } | Array<Capable> | Map<Capable, Capable> | Set<Capable>;
25
+
26
+ declare type ConnectionMessage = BidirectionalConnectionMessage | UnidirectionalConnectionMessage;
27
+
28
+ declare type CustomTransport = {
29
+ isJson?: boolean;
30
+ } & ({
31
+ receive: ReceivePlatformTransport | ((listener: (event: Message, messageContext: MessageContext) => void) => void);
32
+ emit: EmitPlatformTransport | ((message: Message, transferables?: Transferable[]) => void);
33
+ } | {
34
+ receive: ReceivePlatformTransport | ((listener: (event: Message, messageContext: MessageContext) => void) => void);
35
+ } | {
36
+ emit: EmitPlatformTransport | ((message: Message, transferables?: Transferable[]) => void);
37
+ });
38
+
39
+ declare type EmitJsonPlatformTransport = WebSocket | WebExtPort;
40
+
41
+ declare type EmitPlatformTransport = EmitJsonPlatformTransport | Window | ServiceWorker | Worker | SharedWorker | MessagePort;
42
+
43
+ /**
44
+ * Protocol mode:
45
+ * - Bidirectional mode
46
+ * - Unidirectional mode
47
+ *
48
+ * Transport modes:
49
+ * - Capable mode
50
+ * - Jsonable mode
51
+ */
52
+ export declare const expose: <T extends Capable>(value: Capable, { transport: _transport, name, remoteName, key, origin, unregisterSignal, platformCapabilities: _platformCapabilities, transferAll, logger }: {
53
+ transport: Transport;
54
+ name?: string;
55
+ remoteName?: string;
56
+ key?: string;
57
+ origin?: string;
58
+ unregisterSignal?: AbortSignal;
59
+ platformCapabilities?: PlatformCapabilities;
60
+ transferAll?: boolean;
61
+ logger?: {};
62
+ }) => Promise<T>;
63
+
64
+ declare type Jsonable = boolean | null | number | string | {
65
+ [key: string]: Jsonable;
66
+ } | Array<Jsonable>;
67
+
68
+ declare type Message = MessageBase & MessageVariant;
69
+
70
+ declare type MessageBase = {
71
+ [OSRA_KEY]: string;
72
+ /** UUID of the client that sent the message */
73
+ uuid: Uuid;
74
+ name?: string;
75
+ };
76
+
77
+ declare type MessageContext = {
78
+ port?: MessagePort | WebExtPort;
79
+ sender?: WebExtSender;
80
+ receiveTransport?: ReceivePlatformTransport;
81
+ source?: MessageEventSource | null;
82
+ };
83
+
84
+ declare type MessageVariant = ProtocolMessage | ConnectionMessage;
85
+
86
+ declare const OSRA_BOX: "__OSRA_BOX__";
87
+
88
+ declare const OSRA_KEY: "__OSRA_KEY__";
89
+
90
+ declare type PlatformCapabilities = {
91
+ jsonOnly: boolean;
92
+ messagePort: boolean;
93
+ arrayBuffer: boolean;
94
+ transferable: boolean;
95
+ transferableStream: boolean;
96
+ };
97
+
98
+ declare type PlatformTransport = EmitPlatformTransport | ReceivePlatformTransport;
99
+
100
+ declare type ProtocolMessage = {
101
+ type: 'announce';
102
+ /** Only set when acknowledging a remote announcement */
103
+ remoteUuid?: Uuid;
104
+ } | {
105
+ /** uuid already taken, try announcing with another one */
106
+ type: 'reject-uuid-taken';
107
+ remoteUuid: Uuid;
108
+ } | {
109
+ type: 'close';
110
+ remoteUuid: Uuid;
111
+ };
112
+
113
+ declare type ReceiveJsonPlatformTransport = WebSocket | WebExtPort | WebExtOnConnect | WebExtOnMessage;
114
+
115
+ declare type ReceivePlatformTransport = ReceiveJsonPlatformTransport | Window | ServiceWorker | Worker | SharedWorker | MessagePort;
116
+
117
+ declare type Revivable = MessagePort | Promise<Capable> | TypedArray | ArrayBuffer | ReadableStream | Date | Error | ((...args: Capable[]) => Promise<Capable>);
118
+
119
+ declare type Structurable = Jsonable
120
+ /** not really structureable but here for convenience */
121
+ | void | undefined | BigInt | Date | RegExp | Blob | File | FileList | ArrayBuffer | ArrayBufferView | ImageBitmap | ImageData | {
122
+ [key: string]: Structurable;
123
+ } | Array<Structurable> | Map<Structurable, Structurable> | Set<Structurable>;
124
+
125
+ declare type TransferBox<T extends Transferable = Transferable> = {
126
+ [OSRA_BOX]: 'transferable';
127
+ value: T;
128
+ };
129
+
130
+ declare type Transport = PlatformTransport | CustomTransport;
131
+
132
+ declare type TypedArray = typeof typedArrays[number];
133
+
134
+ declare const typedArrays: (BigInt64Array<ArrayBuffer> | BigUint64Array<ArrayBuffer> | Int8Array<ArrayBuffer> | Float64Array<ArrayBuffer> | Uint8Array<ArrayBuffer> | Uint8ClampedArray<ArrayBuffer> | Int16Array<ArrayBuffer> | Uint16Array<ArrayBuffer> | Int32Array<ArrayBuffer> | Uint32Array<ArrayBuffer> | Float16Array<ArrayBuffer> | Float32Array<ArrayBuffer>)[];
135
+
136
+ declare type UnidirectionalConnectionMessage = {
137
+ type: 'message';
138
+ remoteUuid: Uuid;
139
+ data: Capable;
140
+ portId: Uuid;
141
+ };
142
+
143
+ declare type Uuid = `${string}-${string}-${string}-${string}-${string}`;
144
+
145
+ declare type WebExtOnConnect = WebExtRuntime['onConnect'];
146
+
147
+ declare type WebExtOnMessage = WebExtRuntime['onMessage'];
148
+
149
+ declare type WebExtPort = ReturnType<WebExtRuntime['connect']>;
150
+
151
+ declare type WebExtRuntime = typeof browser.runtime;
152
+
153
+ declare type WebExtSender = NonNullable<WebExtPort['sender']>;
154
+
155
+ export { }