osra 0.1.0 → 0.1.1

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.
@@ -0,0 +1,8 @@
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>;
@@ -0,0 +1,66 @@
1
+ export declare const OSRA_MESSAGE_PROPERTY: "__OSRA__";
2
+ export declare const OSRA_MESSAGE_KEY: "__OSRA_DEFAULT_KEY__";
3
+ export declare const OSRA_PROXY: "__OSRA_PROXY__";
4
+ export type JsonPropertyKey = string | number;
5
+ export type JsonCloneType = boolean | null | number | string | {
6
+ [key: string]: JsonCloneType;
7
+ } | Array<JsonCloneType>;
8
+ export type StructuredCloneType = JsonCloneType | void | undefined | BigInt | Date | RegExp | Blob | File | FileList | ArrayBuffer | ArrayBufferView | ImageBitmap | ImageData | {
9
+ [key: string]: StructuredCloneType;
10
+ } | Array<StructuredCloneType> | Map<StructuredCloneType, StructuredCloneType> | Set<StructuredCloneType>;
11
+ export type TransferableObject = SharedArrayBuffer | ArrayBuffer | MessagePort | ReadableStream;
12
+ export type StructuredCloneTransferableType = StructuredCloneType | TransferableObject | {
13
+ [key: string]: StructuredCloneTransferableType;
14
+ } | Array<StructuredCloneTransferableType> | Map<StructuredCloneTransferableType, StructuredCloneTransferableType> | Set<StructuredCloneTransferableType>;
15
+ export type ProxiableType = Promise<StructuredCloneTransferableProxiableType> | Error | MessagePort | ReadableStream | ((...args: any[]) => Promise<StructuredCloneTransferableProxiableType>) | {
16
+ [key: string]: StructuredCloneTransferableProxiableType;
17
+ } | Array<StructuredCloneTransferableProxiableType> | Map<StructuredCloneTransferableProxiableType, StructuredCloneTransferableProxiableType> | Set<StructuredCloneTransferableProxiableType>;
18
+ export type StructuredCloneTransferableProxiableType = ProxiableType | StructuredCloneTransferableType;
19
+ type PortOrJsonPort<JsonOnly extends boolean> = JsonOnly extends true ? {
20
+ portId: string;
21
+ } : {
22
+ port: MessagePort;
23
+ };
24
+ export type ProxiedFunctionType<JsonOnly extends boolean> = ({
25
+ type: 'function';
26
+ } & PortOrJsonPort<JsonOnly>);
27
+ export type ProxiedMessagePortType<JsonOnly extends boolean> = ({
28
+ type: 'messagePort';
29
+ } & PortOrJsonPort<JsonOnly>);
30
+ export type ProxiedPromiseType<JsonOnly extends boolean> = ({
31
+ type: 'promise';
32
+ } & PortOrJsonPort<JsonOnly>);
33
+ export type ProxiedReadableStreamType<JsonOnly extends boolean> = ({
34
+ type: 'readableStream';
35
+ } & PortOrJsonPort<JsonOnly>);
36
+ export type ProxiedErrorType = ({
37
+ type: 'error';
38
+ message: string;
39
+ stack?: string;
40
+ });
41
+ export type ProxiedType<JsonOnly extends boolean> = {
42
+ [OSRA_PROXY]: true;
43
+ } & (ProxiedFunctionType<JsonOnly> | ProxiedMessagePortType<JsonOnly> | ProxiedPromiseType<JsonOnly> | ProxiedReadableStreamType<JsonOnly> | ProxiedErrorType);
44
+ export type OsraMessage = {
45
+ [OSRA_MESSAGE_PROPERTY]: true;
46
+ key: string;
47
+ } & ({
48
+ type: 'ready';
49
+ envCheck: {
50
+ buffer: ArrayBuffer;
51
+ port: MessagePort;
52
+ };
53
+ } | {
54
+ type: 'init';
55
+ data: StructuredCloneTransferableType;
56
+ } | {
57
+ type: 'message';
58
+ portId: string;
59
+ data: any;
60
+ } | {
61
+ type: 'port-closed';
62
+ portId: string;
63
+ });
64
+ export type RemoteTarget = Window | ServiceWorker | Worker | MessagePort;
65
+ export type LocalTarget = WindowEventHandlers | ServiceWorkerContainer | Worker | SharedWorker;
66
+ export {};
@@ -0,0 +1,60 @@
1
+ import type { StructuredCloneTransferableType, TransferableObject, ProxiedErrorType, ProxiedFunctionType, ProxiedMessagePortType, ProxiedPromiseType, StructuredCloneTransferableProxiableType } from './types';
2
+ export declare const isClonable: (value: any) => boolean;
3
+ export declare const isTransferable: (value: any) => boolean;
4
+ export declare const getTransferableObjects: (value: any) => TransferableObject[];
5
+ export type EnvCheck = {
6
+ uuid: string;
7
+ supportsPorts: boolean;
8
+ jsonOnly: boolean;
9
+ };
10
+ export type Context = {
11
+ envCheck: EnvCheck;
12
+ addIncomingProxiedMessagePort: (portId: string) => MessagePort;
13
+ addOutgoingProxiedMessagePort: (port: MessagePort) => string;
14
+ finalizationRegistry: FinalizationRegistry<number>;
15
+ };
16
+ export declare const proxiedFunctionToFunction: <JsonOnly extends boolean>(proxiedFunction: ProxiedFunctionType<JsonOnly>, context: Context) => (...args: StructuredCloneTransferableType[]) => Promise<unknown>;
17
+ export declare const proxiedMessagePortToMessagePort: <JsonOnly extends boolean>(proxiedMessagePort: ProxiedMessagePortType<JsonOnly>, context: Context) => MessagePort;
18
+ export declare const proxiedErrorToError: (proxiedError: ProxiedErrorType, context: Context) => Error;
19
+ export declare const proxiedPromiseToPromise: <JsonOnly extends boolean>(proxiedPromise: ProxiedPromiseType<JsonOnly>, context: Context) => Promise<unknown>;
20
+ export declare const replaceIncomingProxiedTypes: (value: StructuredCloneTransferableType, context: Context) => StructuredCloneTransferableProxiableType;
21
+ export declare const errorToProxiedError: (error: Error, _: Context) => {
22
+ __OSRA_PROXY__: boolean;
23
+ type: string;
24
+ message: string;
25
+ stack: string | undefined;
26
+ };
27
+ export declare const messagePortToProxiedMessagePort: (port: MessagePort, context: Context) => {
28
+ port: MessagePort;
29
+ __OSRA_PROXY__: boolean;
30
+ type: string;
31
+ } | {
32
+ portId: string;
33
+ __OSRA_PROXY__: boolean;
34
+ type: string;
35
+ };
36
+ export declare const promiseToProxiedPromise: (promise: Promise<StructuredCloneTransferableType>, context: Context) => {
37
+ __OSRA_PROXY__: boolean;
38
+ type: string;
39
+ port: any;
40
+ };
41
+ export declare const functionToProxiedFunction: (func: Function, context: Context) => {
42
+ __OSRA_PROXY__: boolean;
43
+ type: string;
44
+ port: any;
45
+ };
46
+ export declare const replaceOutgoingProxiedTypes: <T extends StructuredCloneTransferableProxiableType>(value: T, context: Context) => any;
47
+ export declare const replaceRecursive: <T extends StructuredCloneTransferableProxiableType, T2 extends (value: any) => any>(value: T, shouldReplace: (value: Parameters<T2>[0]) => boolean, replaceFunction: T2) => any;
48
+ export declare const makeNumberAllocator: () => {
49
+ alloc: () => number;
50
+ free: (number: any) => void;
51
+ };
52
+ type NumberAllocator = ReturnType<typeof makeNumberAllocator>;
53
+ export declare const makeAllocator: <T>({ numberAllocator }: {
54
+ numberAllocator: NumberAllocator;
55
+ }) => {
56
+ alloc: (value: T) => number;
57
+ get: (id: number) => T | undefined;
58
+ free: (id: number) => void;
59
+ };
60
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "osra",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Easy communication between workers",
5
5
  "files": [
6
6
  "build"
@@ -8,7 +8,7 @@
8
8
  "main": "build/index.js",
9
9
  "type": "module",
10
10
  "scripts": {
11
- "build": "vite build",
11
+ "build": "vite build && tsc",
12
12
  "build-watch": "vite build --watch",
13
13
  "build-test": "vite build --config vite.test.config.ts",
14
14
  "build-test-watch": "vite build --config vite.test.config.ts --watch",