swarpc 0.1.1 → 0.1.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/README.md CHANGED
@@ -10,14 +10,15 @@ RPC for Service Workers -- move that heavy computation off of your UI thread!
10
10
  ## Installation
11
11
 
12
12
  ```bash
13
- npm add swarpc
13
+ npm add swarpc arktype
14
14
  ```
15
15
 
16
16
  ## Usage
17
17
 
18
18
  ### 1. Declare your procedures in a shared file
19
19
 
20
- ```javascript
20
+ ```typescript
21
+ import type { ProceduresMap } from "swarpc"
21
22
  import { type } from "arktype"
22
23
 
23
24
  export const procedures = {
@@ -25,7 +26,7 @@ export const procedures = {
25
26
  // Input for the procedure
26
27
  input: type({ query: "string", "pageSize?": "number" }),
27
28
  // Function to be called whenever you can update progress while the procedure is running -- long computations are a first-class concern here. Examples include using the fetch-progress NPM package.
28
- progress: type({ transmitted: "number", total: "number" }),
29
+ progress: type({ transferred: "number", total: "number" }),
29
30
  // Output of a successful procedure call
30
31
  success: type({
31
32
  id: "string",
@@ -33,7 +34,7 @@ export const procedures = {
33
34
  genres: "string[]",
34
35
  }).array(),
35
36
  },
36
- }
37
+ } as const satisfies ProceduresMap
37
38
  ```
38
39
 
39
40
  ### 2. Register your procedures in the service worker
@@ -58,6 +59,7 @@ swarpc.searchIMDb(async ({ query, pageSize = 10 }, onProgress) => {
58
59
  return fetch(`https://rest.imdbapi.dev/v2/search/titles?${queryParams}`)
59
60
  .then(fetchProgress({ onProgress }))
60
61
  .then((response) => response.json())
62
+ .then(({ titles } => titles)
61
63
  })
62
64
 
63
65
  // ...
@@ -84,9 +86,9 @@ Here's a Svelte example!
84
86
 
85
87
  <search>
86
88
  <input type="text" bind:value={query} placeholder="Search IMDb" />
87
- <button on:click={async () => {
89
+ <button onclick={async () => {
88
90
  results = await swarpc.searchIMDb({ query }, (p) => {
89
- progress = p.transmitted / p.total
91
+ progress = p.transferred / p.total
90
92
  })
91
93
  }}>
92
94
  Search
package/dist/swarpc.d.ts CHANGED
@@ -1,64 +1,19 @@
1
1
  /**
2
- * @import { Type } from 'arktype';
3
- */
4
- /**
5
- * @template {Type} I
6
- * @template {Type} P
7
- * @template {Type} S
8
- * @typedef {Object} Procedure
9
- * @property {I} input
10
- * @property {P} progress
11
- * @property {S} success
12
- */
13
- /**
14
- * @template {Type} I
15
- * @template {Type} P
16
- * @template {Type} S
17
- * @typedef {(input: I['inferOut'], onProgress: (progress: P['inferOut']) => void) => Promise<NoInfer<S>['inferOut'] | NoInfer<S>['inferOut']>} ProcedureImplementation
18
- */
19
- /**
20
- * @typedef {Record<string, Procedure<Type, Type, Type>>} ProceduresMap
21
- */
22
- /**
23
- * @template {ProceduresMap} Procedures
24
- * @typedef {{ procedures: Procedures, implementations: {[F in keyof Procedures]: ProcedureImplementation<Procedures[F]['input'], Procedures[F]['progress'], Procedures[F]['success']> }, start: (self: Window) => void } & { [F in keyof Procedures]: (impl: NoInfer<ProcedureImplementation<Procedures[F]['input'], Procedures[F]['progress'], Procedures[F]['success'] >>) => void }} SwarpServer
2
+ * @import { ProceduresMap, SwarpcClient, SwarpcServer } from './typings.js'
25
3
  */
26
4
  /**
27
5
  * @template {ProceduresMap} Procedures
28
6
  * @param {Procedures} procedures
29
- * @returns {SwarpServer<Procedures>}
30
- */
31
- export function Server<Procedures extends ProceduresMap>(procedures: Procedures): SwarpServer<Procedures>;
32
- /**
33
- * @template {Procedure<Type, Type, Type>} P
34
- * @typedef {(input: P['input']['inferOut'], onProgress?: (progress: P['progress']['inferOut']) => void) => Promise<P['success']['inferOut']>} ClientMethod
35
- */
36
- /**
37
- * @template {ProceduresMap} Procedures
38
- * @typedef {{ procedures: Procedures } & { [F in keyof Procedures]: ClientMethod<Procedures[F]> }} SwarpClient
7
+ * @returns {SwarpcServer<Procedures>}
39
8
  */
9
+ export function Server<Procedures extends ProceduresMap>(procedures: Procedures): SwarpcServer<Procedures>;
40
10
  /**
41
11
  * @template {ProceduresMap} Procedures
42
12
  * @param {Procedures} procedures
43
- * @returns {SwarpClient<Procedures>}
13
+ * @returns {SwarpcClient<Procedures>}
44
14
  */
45
- export function Client<Procedures extends ProceduresMap>(procedures: Procedures): SwarpClient<Procedures>;
46
- export type Procedure<I extends Type, P extends Type, S extends Type> = {
47
- input: I;
48
- progress: P;
49
- success: S;
50
- };
51
- export type ProcedureImplementation<I extends Type, P extends Type, S extends Type> = (input: I["inferOut"], onProgress: (progress: P["inferOut"]) => void) => Promise<NoInfer<S>["inferOut"] | NoInfer<S>["inferOut"]>;
52
- export type ProceduresMap = Record<string, Procedure<Type, Type, Type>>;
53
- export type SwarpServer<Procedures extends ProceduresMap> = {
54
- procedures: Procedures;
55
- implementations: { [F in keyof Procedures]: ProcedureImplementation<Procedures[F]["input"], Procedures[F]["progress"], Procedures[F]["success"]>; };
56
- start: (self: Window) => void;
57
- } & { [F in keyof Procedures]: (impl: NoInfer<ProcedureImplementation<Procedures[F]["input"], Procedures[F]["progress"], Procedures[F]["success"]>>) => void; };
58
- export type ClientMethod<P extends Procedure<Type, Type, Type>> = (input: P["input"]["inferOut"], onProgress?: (progress: P["progress"]["inferOut"]) => void) => Promise<P["success"]["inferOut"]>;
59
- export type SwarpClient<Procedures extends ProceduresMap> = {
60
- procedures: Procedures;
61
- } & { [F in keyof Procedures]: ClientMethod<Procedures[F]>; };
62
- import type { Type } from 'arktype';
63
- import type { Type as Type_1 } from 'arktype';
15
+ export function Client<Procedures extends ProceduresMap>(procedures: Procedures): SwarpcClient<Procedures>;
16
+ import type { ProceduresMap } from './typings.js';
17
+ import type { SwarpcServer } from './typings.js';
18
+ import type { SwarpcClient } from './typings.js';
64
19
  //# sourceMappingURL=swarpc.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"swarpc.d.ts","sourceRoot":"","sources":["../src/swarpc.js"],"names":[],"mappings":"AACA;;GAEG;AAEH;;;;;;;;GAQG;AAEH;;;;;GAKG;AAEH;;GAEG;AAEH;;;GAGG;AAEH;;;;GAIG;AACH,u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C,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAE,CAAC,KAAK,IAAI,GAAE;yBAyF7U,CAAC,SAA9B,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAE,IAC7B,CAAC,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC,UAAU,CAAC,KAAK,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,CAAC;wBAIhH,UAAU,SAAzB,aAAc,IACf;IAAE,UAAU,EAAE,UAAU,CAAA;CAAE,GAAG,GAAG,CAAC,IAAI,MAAM,UAAU,GAAG,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GAAE;0BAzHzE,SAAS;oCAAT,SAAS"}
1
+ {"version":3,"file":"swarpc.d.ts","sourceRoot":"","sources":["../src/swarpc.js"],"names":[],"mappings":"AAEA;;GAEG;AAEH;;;;GAIG;AACH,uBAJ6B,UAAU,SAA1B,aAAe,cACjB,UAAU,GACR,aAAa,UAAU,CAAC,CAgFpC;AAED;;;;GAIG;AACH,uBAJ6B,UAAU,SAA1B,aAAe,cACjB,UAAU,GACR,aAAa,UAAU,CAAC,CA+BpC;mCA1H6D,cAAc;kCAAd,cAAc;kCAAd,cAAc"}
@@ -0,0 +1,18 @@
1
+ export type Procedure<I extends Type, P extends Type, S extends Type> = {
2
+ input: I;
3
+ progress: P;
4
+ success: S;
5
+ };
6
+ export type ProcedureImplementation<I extends Type, P extends Type, S extends Type> = (input: I["inferOut"], onProgress: (progress: P["inferOut"]) => void) => Promise<NoInfer<S>["inferOut"] | NoInfer<S>["inferOut"]>;
7
+ export type ProceduresMap = Record<string, Procedure<Type, Type, Type>>;
8
+ export type ClientMethod<P extends Procedure<Type, Type, Type>> = (input: P["input"]["inferOut"], onProgress?: (progress: P["progress"]["inferOut"]) => void) => Promise<P["success"]["inferOut"]>;
9
+ export type SwarpcClient<Procedures extends ProceduresMap> = {
10
+ procedures: Procedures;
11
+ } & { [F in keyof Procedures]: ClientMethod<Procedures[F]>; };
12
+ export type SwarpcServer<Procedures extends ProceduresMap> = {
13
+ procedures: Procedures;
14
+ implementations: { [F in keyof Procedures]: ProcedureImplementation<Procedures[F]["input"], Procedures[F]["progress"], Procedures[F]["success"]>; };
15
+ start: (self: Window) => void;
16
+ } & { [F in keyof Procedures]: (impl: NoInfer<ProcedureImplementation<Procedures[F]["input"], Procedures[F]["progress"], Procedures[F]["success"]>>) => void; };
17
+ import type { Type } from 'arktype';
18
+ //# sourceMappingURL=typings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typings.d.ts","sourceRoot":"","sources":["../src/typings.js"],"names":[],"mappings":"sBAKoB,CAAC,SAAR,IAAM,EACC,CAAC,SAAR,IAAM,EACC,CAAC,SAAR,IAAM;WAEL,CAAC;cACD,CAAC;aACD,CAAC;;oCAIK,CAAC,SAAR,IAAM,EACC,CAAC,SAAR,IAAM,EACC,CAAC,SAAR,IAAM,IACN,CAAC,KAAK,EAAE,CAAC,CAAC,UAAU,CAAC,EAAE,UAAU,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,UAAU,CAAC,KAAK,IAAI,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;4BAIjI,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;yBAIb,CAAC,SAA9B,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAE,IAC7B,CAAC,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC,UAAU,CAAC,KAAK,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,CAAC;yBAIhH,UAAU,SAAzB,aAAc,IACf;IAAE,UAAU,EAAE,UAAU,CAAA;CAAE,GAAG,GAAG,CAAC,IAAI,MAAM,UAAU,GAAG,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GAAE;yBAIrE,UAAU,SAAzB,aAAc,IACf;IAAE,UAAU,EAAE,UAAU,CAAC;IAAC,eAAe,EAAE,GAAE,CAAC,IAAI,MAAM,UAAU,GAAG,uBAAuB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,GAAE,CAAC;IAAC,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;CAAG,GAAG,GAAG,CAAC,IAAI,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,uBAAuB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAE,CAAC,KAAK,IAAI,GAAE;0BApC/V,SAAS"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swarpc",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
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.js CHANGED
@@ -1,41 +1,16 @@
1
1
  import { type } from "arktype"
2
- /**
3
- * @import { Type } from 'arktype';
4
- */
5
-
6
- /**
7
- * @template {Type} I
8
- * @template {Type} P
9
- * @template {Type} S
10
- * @typedef {Object} Procedure
11
- * @property {I} input
12
- * @property {P} progress
13
- * @property {S} success
14
- */
15
2
 
16
3
  /**
17
- * @template {Type} I
18
- * @template {Type} P
19
- * @template {Type} S
20
- * @typedef {(input: I['inferOut'], onProgress: (progress: P['inferOut']) => void) => Promise<NoInfer<S>['inferOut'] | NoInfer<S>['inferOut']>} ProcedureImplementation
21
- */
22
-
23
- /**
24
- * @typedef {Record<string, Procedure<Type, Type, Type>>} ProceduresMap
25
- */
26
-
27
- /**
28
- * @template {ProceduresMap} Procedures
29
- * @typedef {{ procedures: Procedures, implementations: {[F in keyof Procedures]: ProcedureImplementation<Procedures[F]['input'], Procedures[F]['progress'], Procedures[F]['success']> }, start: (self: Window) => void } & { [F in keyof Procedures]: (impl: NoInfer<ProcedureImplementation<Procedures[F]['input'], Procedures[F]['progress'], Procedures[F]['success'] >>) => void }} SwarpServer
4
+ * @import { ProceduresMap, SwarpcClient, SwarpcServer } from './typings.js'
30
5
  */
31
6
 
32
7
  /**
33
8
  * @template {ProceduresMap} Procedures
34
9
  * @param {Procedures} procedures
35
- * @returns {SwarpServer<Procedures>}
10
+ * @returns {SwarpcServer<Procedures>}
36
11
  */
37
12
  export function Server(procedures) {
38
- /** @type {SwarpServer<Procedures>} */
13
+ /** @type {SwarpcServer<Procedures>} */
39
14
  // @ts-expect-error
40
15
  const instance = {
41
16
  procedures,
@@ -114,23 +89,13 @@ export function Server(procedures) {
114
89
  return instance
115
90
  }
116
91
 
117
- /**
118
- * @template {Procedure<Type, Type, Type>} P
119
- * @typedef {(input: P['input']['inferOut'], onProgress?: (progress: P['progress']['inferOut']) => void) => Promise<P['success']['inferOut']>} ClientMethod
120
- */
121
-
122
- /**
123
- * @template {ProceduresMap} Procedures
124
- * @typedef {{ procedures: Procedures } & { [F in keyof Procedures]: ClientMethod<Procedures[F]> }} SwarpClient
125
- */
126
-
127
92
  /**
128
93
  * @template {ProceduresMap} Procedures
129
94
  * @param {Procedures} procedures
130
- * @returns {SwarpClient<Procedures>}
95
+ * @returns {SwarpcClient<Procedures>}
131
96
  */
132
97
  export function Client(procedures) {
133
- /** @type {SwarpClient<Procedures>} */
98
+ /** @type {SwarpcClient<Procedures>} */
134
99
  // @ts-expect-error
135
100
  const instance = { procedures }
136
101
 
package/src/typings.js ADDED
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @import { Type } from 'arktype';
3
+ */
4
+
5
+ /**
6
+ * @template {Type} I
7
+ * @template {Type} P
8
+ * @template {Type} S
9
+ * @typedef {Object} Procedure
10
+ * @property {I} input
11
+ * @property {P} progress
12
+ * @property {S} success
13
+ */
14
+
15
+ /**
16
+ * @template {Type} I
17
+ * @template {Type} P
18
+ * @template {Type} S
19
+ * @typedef {(input: I['inferOut'], onProgress: (progress: P['inferOut']) => void) => Promise<NoInfer<S>['inferOut'] | NoInfer<S>['inferOut']>} ProcedureImplementation
20
+ */
21
+
22
+ /**
23
+ * @typedef {Record<string, Procedure<Type, Type, Type>>} ProceduresMap
24
+ */
25
+
26
+ /**
27
+ * @template {Procedure<Type, Type, Type>} P
28
+ * @typedef {(input: P['input']['inferOut'], onProgress?: (progress: P['progress']['inferOut']) => void) => Promise<P['success']['inferOut']>} ClientMethod
29
+ */
30
+
31
+ /**
32
+ * @template {ProceduresMap} Procedures
33
+ * @typedef {{ procedures: Procedures } & { [F in keyof Procedures]: ClientMethod<Procedures[F]> }} SwarpcClient
34
+ */
35
+
36
+ /**
37
+ * @template {ProceduresMap} Procedures
38
+ * @typedef {{ procedures: Procedures, implementations: {[F in keyof Procedures]: ProcedureImplementation<Procedures[F]['input'], Procedures[F]['progress'], Procedures[F]['success']> }, start: (self: Window) => void } & { [F in keyof Procedures]: (impl: NoInfer<ProcedureImplementation<Procedures[F]['input'], Procedures[F]['progress'], Procedures[F]['success'] >>) => void }} SwarpcServer
39
+ */
40
+
41
+ // Required, otherwise nothing can be imported from this file
42
+ export {}