swarpc 0.2.3 → 0.3.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/dist/swarpc.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { ProceduresMap, SwarpcClient, SwarpcServer } from "./types";
1
+ import { type ProceduresMap, type SwarpcClient, type SwarpcServer } from "./types.js";
2
+ export type { ProceduresMap, SwarpcClient, SwarpcServer } from "./types.js";
2
3
  export declare function Server<Procedures extends ProceduresMap>(procedures: Procedures, { worker }?: {
3
4
  worker?: Worker;
4
5
  }): SwarpcServer<Procedures>;
@@ -1 +1 @@
1
- {"version":3,"file":"swarpc.d.ts","sourceRoot":"","sources":["../src/swarpc.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,YAAY,EACZ,YAAY,EAEb,MAAM,SAAS,CAAA;AAEhB,wBAAgB,MAAM,CAAC,UAAU,SAAS,aAAa,EACrD,UAAU,EAAE,UAAU,EACtB,EAAE,MAAM,EAAE,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAO,GACnC,YAAY,CAAC,UAAU,CAAC,CAyE1B;AAwDD,wBAAgB,MAAM,CAAC,UAAU,SAAS,aAAa,EACrD,UAAU,EAAE,UAAU,EACtB,EAAE,MAAM,EAAE,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAO,GACnC,YAAY,CAAC,UAAU,CAAC,CAiC1B"}
1
+ {"version":3,"file":"swarpc.d.ts","sourceRoot":"","sources":["../src/swarpc.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,YAAY,EAClB,MAAM,YAAY,CAAA;AAEnB,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE3E,wBAAgB,MAAM,CAAC,UAAU,SAAS,aAAa,EACrD,UAAU,EAAE,UAAU,EACtB,EAAE,MAAM,EAAE,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAO,GACnC,YAAY,CAAC,UAAU,CAAC,CAyE1B;AA4DD,wBAAgB,MAAM,CAAC,UAAU,SAAS,aAAa,EACrD,UAAU,EAAE,UAAU,EACtB,EAAE,MAAM,EAAE,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAO,GACnC,YAAY,CAAC,UAAU,CAAC,CAmC1B"}
package/dist/swarpc.js CHANGED
@@ -1,16 +1,17 @@
1
1
  import { type } from "arktype";
2
+ import { zImplementations, zProcedures, } from "./types.js";
2
3
  export function Server(procedures, { worker } = {}) {
3
4
  const instance = {
4
- procedures,
5
- implementations: {},
6
- start: () => { },
5
+ [zProcedures]: procedures,
6
+ [zImplementations]: {},
7
+ start: (self) => { },
7
8
  };
8
9
  for (const functionName in procedures) {
9
10
  instance[functionName] = ((implementation) => {
10
- if (!instance.procedures[functionName]) {
11
+ if (!instance[zProcedures][functionName]) {
11
12
  throw new Error(`No procedure found for function name: ${functionName}`);
12
13
  }
13
- instance.implementations[functionName] = implementation;
14
+ instance[zImplementations][functionName] = implementation;
14
15
  });
15
16
  }
16
17
  const PayloadSchema = type.or(...Object.entries(procedures).map(([functionName, { input }]) => ({
@@ -38,7 +39,7 @@ export function Server(procedures, { worker } = {}) {
38
39
  message: "message" in error ? error.message : String(error),
39
40
  },
40
41
  });
41
- const implementation = instance.implementations[functionName];
42
+ const implementation = instance[zImplementations][functionName];
42
43
  if (!implementation) {
43
44
  await postError("No implementation found");
44
45
  return;
@@ -74,6 +75,7 @@ async function startClientListener(worker) {
74
75
  }
75
76
  }
76
77
  const w = worker ?? navigator.serviceWorker;
78
+ console.log("[SWARPC Client] Starting client listener on", w);
77
79
  w.addEventListener("message", (event) => {
78
80
  const { functionName, requestId, ...data } = event.data || {};
79
81
  if (!requestId) {
@@ -98,7 +100,7 @@ async function startClientListener(worker) {
98
100
  _clientListenerStarted = true;
99
101
  }
100
102
  export function Client(procedures, { worker } = {}) {
101
- const instance = { procedures };
103
+ const instance = { [zProcedures]: procedures };
102
104
  for (const functionName of Object.keys(procedures)) {
103
105
  instance[functionName] = (async (input, onProgress = () => { }) => {
104
106
  procedures[functionName].input.assert(input);
package/dist/types.d.ts CHANGED
@@ -6,17 +6,20 @@ export type Procedure<I extends Type, P extends Type, S extends Type> = {
6
6
  };
7
7
  export type ProcedureImplementation<I extends Type, P extends Type, S extends Type> = (input: I["inferOut"], onProgress: (progress: P["inferOut"]) => void) => Promise<S["inferOut"]>;
8
8
  export type ProceduresMap = Record<string, Procedure<Type, Type, Type>>;
9
+ export type ImplementationsMap<Procedures extends ProceduresMap> = {
10
+ [F in keyof Procedures]: ProcedureImplementation<Procedures[F]["input"], Procedures[F]["progress"], Procedures[F]["success"]>;
11
+ };
9
12
  export type ClientMethod<P extends Procedure<Type, Type, Type>> = (input: P["input"]["inferIn"], onProgress?: (progress: P["progress"]["inferOut"]) => void) => Promise<P["success"]["inferOut"]>;
13
+ export declare const zImplementations: unique symbol;
14
+ export declare const zProcedures: unique symbol;
10
15
  export type SwarpcClient<Procedures extends ProceduresMap> = {
11
- procedures: Procedures;
16
+ [zProcedures]: Procedures;
12
17
  } & {
13
18
  [F in keyof Procedures]: ClientMethod<Procedures[F]>;
14
19
  };
15
20
  export type SwarpcServer<Procedures extends ProceduresMap> = {
16
- procedures: Procedures;
17
- implementations: {
18
- [F in keyof Procedures]: ProcedureImplementation<Procedures[F]["input"], Procedures[F]["progress"], Procedures[F]["success"]>;
19
- };
21
+ [zProcedures]: Procedures;
22
+ [zImplementations]: ImplementationsMap<Procedures>;
20
23
  start(self: Window): void;
21
24
  } & {
22
25
  [F in keyof Procedures]: (impl: ProcedureImplementation<Procedures[F]["input"], Procedures[F]["progress"], Procedures[F]["success"]>) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,SAAS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zB,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CACzB,KACE,IAAI;CACV,CAAA"}
package/dist/types.js CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ export const zImplementations = Symbol("SWARPC implementations");
2
+ export const zProcedures = Symbol("SWARPC procedures");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swarpc",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "Full type-safe RPC library for service worker -- move things off of the UI thread with ease!",
5
5
  "keywords": [
6
6
  "service-workers",
package/src/swarpc.ts CHANGED
@@ -1,24 +1,31 @@
1
1
  import { type } from "arktype"
2
- import type { ProceduresMap, SwarpcClient, SwarpcServer } from "./types.js"
2
+ import {
3
+ ImplementationsMap,
4
+ zImplementations,
5
+ zProcedures,
6
+ type ProceduresMap,
7
+ type SwarpcClient,
8
+ type SwarpcServer,
9
+ } from "./types.js"
3
10
 
4
- export type { ProceduresMap, SwarpcClient, SwarpcServer }
11
+ export type { ProceduresMap, SwarpcClient, SwarpcServer } from "./types.js"
5
12
 
6
13
  export function Server<Procedures extends ProceduresMap>(
7
14
  procedures: Procedures,
8
15
  { worker }: { worker?: Worker } = {}
9
16
  ): SwarpcServer<Procedures> {
10
17
  const instance = {
11
- procedures,
12
- implementations: {} as SwarpcServer<Procedures>["implementations"],
13
- start: () => {},
18
+ [zProcedures]: procedures,
19
+ [zImplementations]: {} as ImplementationsMap<Procedures>,
20
+ start: (self: Window) => {},
14
21
  } as SwarpcServer<Procedures>
15
22
 
16
23
  for (const functionName in procedures) {
17
24
  instance[functionName] = ((implementation) => {
18
- if (!instance.procedures[functionName]) {
25
+ if (!instance[zProcedures][functionName]) {
19
26
  throw new Error(`No procedure found for function name: ${functionName}`)
20
27
  }
21
- instance.implementations[functionName] = implementation as any
28
+ instance[zImplementations][functionName] = implementation as any
22
29
  }) as SwarpcServer<Procedures>[typeof functionName]
23
30
  }
24
31
 
@@ -60,7 +67,7 @@ export function Server<Procedures extends ProceduresMap>(
60
67
  },
61
68
  })
62
69
 
63
- const implementation = instance.implementations[functionName]
70
+ const implementation = instance[zImplementations][functionName]
64
71
  if (!implementation) {
65
72
  await postError("No implementation found")
66
73
  return
@@ -109,6 +116,7 @@ async function startClientListener(worker?: Worker) {
109
116
  }
110
117
 
111
118
  const w = worker ?? navigator.serviceWorker
119
+ console.log("[SWARPC Client] Starting client listener on", w)
112
120
  w.addEventListener("message", (event) => {
113
121
  const { functionName, requestId, ...data } =
114
122
  (event as MessageEvent).data || {}
@@ -142,7 +150,9 @@ export function Client<Procedures extends ProceduresMap>(
142
150
  procedures: Procedures,
143
151
  { worker }: { worker?: Worker } = {}
144
152
  ): SwarpcClient<Procedures> {
145
- const instance = { procedures } as Partial<SwarpcClient<Procedures>>
153
+ const instance = { [zProcedures]: procedures } as Partial<
154
+ SwarpcClient<Procedures>
155
+ >
146
156
 
147
157
  for (const functionName of Object.keys(procedures) as Array<
148
158
  keyof Procedures
package/src/types.ts CHANGED
@@ -1,10 +1,10 @@
1
- import type { Type } from "arktype";
1
+ import type { Type } from "arktype"
2
2
 
3
3
  export type Procedure<I extends Type, P extends Type, S extends Type> = {
4
- input: I;
5
- progress: P;
6
- success: S;
7
- };
4
+ input: I
5
+ progress: P
6
+ success: S
7
+ }
8
8
 
9
9
  export type ProcedureImplementation<
10
10
  I extends Type,
@@ -13,31 +13,36 @@ export type ProcedureImplementation<
13
13
  > = (
14
14
  input: I["inferOut"],
15
15
  onProgress: (progress: P["inferOut"]) => void
16
- ) => Promise<S["inferOut"]>;
16
+ ) => Promise<S["inferOut"]>
17
17
 
18
- export type ProceduresMap = Record<string, Procedure<Type, Type, Type>>;
18
+ export type ProceduresMap = Record<string, Procedure<Type, Type, Type>>
19
+
20
+ export type ImplementationsMap<Procedures extends ProceduresMap> = {
21
+ [F in keyof Procedures]: ProcedureImplementation<
22
+ Procedures[F]["input"],
23
+ Procedures[F]["progress"],
24
+ Procedures[F]["success"]
25
+ >
26
+ }
19
27
 
20
28
  export type ClientMethod<P extends Procedure<Type, Type, Type>> = (
21
29
  input: P["input"]["inferIn"],
22
30
  onProgress?: (progress: P["progress"]["inferOut"]) => void
23
- ) => Promise<P["success"]["inferOut"]>;
31
+ ) => Promise<P["success"]["inferOut"]>
32
+
33
+ export const zImplementations = Symbol("SWARPC implementations")
34
+ export const zProcedures = Symbol("SWARPC procedures")
24
35
 
25
36
  export type SwarpcClient<Procedures extends ProceduresMap> = {
26
- procedures: Procedures;
37
+ [zProcedures]: Procedures
27
38
  } & {
28
- [F in keyof Procedures]: ClientMethod<Procedures[F]>;
29
- };
39
+ [F in keyof Procedures]: ClientMethod<Procedures[F]>
40
+ }
30
41
 
31
42
  export type SwarpcServer<Procedures extends ProceduresMap> = {
32
- procedures: Procedures;
33
- implementations: {
34
- [F in keyof Procedures]: ProcedureImplementation<
35
- Procedures[F]["input"],
36
- Procedures[F]["progress"],
37
- Procedures[F]["success"]
38
- >;
39
- };
40
- start(self: Window): void;
43
+ [zProcedures]: Procedures
44
+ [zImplementations]: ImplementationsMap<Procedures>
45
+ start(self: Window): void
41
46
  } & {
42
47
  [F in keyof Procedures]: (
43
48
  impl: ProcedureImplementation<
@@ -45,5 +50,5 @@ export type SwarpcServer<Procedures extends ProceduresMap> = {
45
50
  Procedures[F]["progress"],
46
51
  Procedures[F]["success"]
47
52
  >
48
- ) => void;
49
- };
53
+ ) => void
54
+ }