swarpc 0.1.0 → 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 +107 -0
- package/dist/swarpc.d.ts +19 -0
- package/dist/swarpc.d.ts.map +1 -0
- package/dist/typings.d.ts +18 -0
- package/dist/typings.d.ts.map +1 -0
- package/package.json +10 -1
- package/src/{swarp.js → swarpc.js} +5 -40
- package/src/typings.js +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
<div align=center>
|
|
2
|
+
<h1>sw&rpc</h1>
|
|
3
|
+
|
|
4
|
+
RPC for Service Workers -- move that heavy computation off of your UI thread!
|
|
5
|
+
|
|
6
|
+
</div>
|
|
7
|
+
|
|
8
|
+
* * *
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm add swarpc arktype
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### 1. Declare your procedures in a shared file
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import type { ProceduresMap } from "swarpc"
|
|
22
|
+
import { type } from "arktype"
|
|
23
|
+
|
|
24
|
+
export const procedures = {
|
|
25
|
+
searchIMDb: {
|
|
26
|
+
// Input for the procedure
|
|
27
|
+
input: type({ query: "string", "pageSize?": "number" }),
|
|
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.
|
|
29
|
+
progress: type({ transferred: "number", total: "number" }),
|
|
30
|
+
// Output of a successful procedure call
|
|
31
|
+
success: type({
|
|
32
|
+
id: "string",
|
|
33
|
+
primary_title: "string",
|
|
34
|
+
genres: "string[]",
|
|
35
|
+
}).array(),
|
|
36
|
+
},
|
|
37
|
+
} as const satisfies ProceduresMap
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 2. Register your procedures in the service worker
|
|
41
|
+
|
|
42
|
+
In your service worker file:
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
import fetchProgress from "fetch-progress"
|
|
46
|
+
import { Server } from "swarpc"
|
|
47
|
+
import { procedures } from "./procedures.js"
|
|
48
|
+
|
|
49
|
+
// 1. Give yourself a server instance
|
|
50
|
+
const swarpc = Server(procedures)
|
|
51
|
+
|
|
52
|
+
// 2. Implement your procedures
|
|
53
|
+
swarpc.searchIMDb(async ({ query, pageSize = 10 }, onProgress) => {
|
|
54
|
+
const queryParams = new URLSearchParams({
|
|
55
|
+
page_size: pageSize.toString(),
|
|
56
|
+
query,
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
return fetch(`https://rest.imdbapi.dev/v2/search/titles?${queryParams}`)
|
|
60
|
+
.then(fetchProgress({ onProgress }))
|
|
61
|
+
.then((response) => response.json())
|
|
62
|
+
.then(({ titles } => titles)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// ...
|
|
66
|
+
|
|
67
|
+
// 3. Start the event listener
|
|
68
|
+
swarpc.start(self)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 3. Call your procedures from the client
|
|
72
|
+
|
|
73
|
+
Here's a Svelte example!
|
|
74
|
+
|
|
75
|
+
```svelte
|
|
76
|
+
<script>
|
|
77
|
+
import { Client } from "swarpc"
|
|
78
|
+
import { procedures } from "./procedures.js"
|
|
79
|
+
|
|
80
|
+
const swarpc = Client(procedures)
|
|
81
|
+
|
|
82
|
+
let query = $state("")
|
|
83
|
+
let results = $state([])
|
|
84
|
+
let progress = $state(0)
|
|
85
|
+
</script>
|
|
86
|
+
|
|
87
|
+
<search>
|
|
88
|
+
<input type="text" bind:value={query} placeholder="Search IMDb" />
|
|
89
|
+
<button onclick={async () => {
|
|
90
|
+
results = await swarpc.searchIMDb({ query }, (p) => {
|
|
91
|
+
progress = p.transferred / p.total
|
|
92
|
+
})
|
|
93
|
+
}}>
|
|
94
|
+
Search
|
|
95
|
+
</button>
|
|
96
|
+
</search>
|
|
97
|
+
|
|
98
|
+
{#if progress > 0 && progress < 1}
|
|
99
|
+
<progress value={progress} max="1" />
|
|
100
|
+
{/if}
|
|
101
|
+
|
|
102
|
+
<ul>
|
|
103
|
+
{#each results as { id, primary_title, genres } (id)}
|
|
104
|
+
<li>{primary_title} - {genres.join(", ")}</li>
|
|
105
|
+
{/each}
|
|
106
|
+
</ul>
|
|
107
|
+
```
|
package/dist/swarpc.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { ProceduresMap, SwarpcClient, SwarpcServer } from './typings.js'
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @template {ProceduresMap} Procedures
|
|
6
|
+
* @param {Procedures} procedures
|
|
7
|
+
* @returns {SwarpcServer<Procedures>}
|
|
8
|
+
*/
|
|
9
|
+
export function Server<Procedures extends ProceduresMap>(procedures: Procedures): SwarpcServer<Procedures>;
|
|
10
|
+
/**
|
|
11
|
+
* @template {ProceduresMap} Procedures
|
|
12
|
+
* @param {Procedures} procedures
|
|
13
|
+
* @returns {SwarpcClient<Procedures>}
|
|
14
|
+
*/
|
|
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';
|
|
19
|
+
//# sourceMappingURL=swarpc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,I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AAC,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.
|
|
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",
|
|
@@ -20,11 +20,20 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"author": "Gwenn Le Bihan <gwenn.lebihan7@gmail.com>",
|
|
22
22
|
"type": "module",
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"src"
|
|
26
|
+
],
|
|
23
27
|
"main": "src/swarpc.js",
|
|
28
|
+
"types": "dist/swarpc.d.ts",
|
|
24
29
|
"scripts": {
|
|
30
|
+
"build": "tsc",
|
|
25
31
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
26
32
|
},
|
|
27
33
|
"dependencies": {
|
|
28
34
|
"arktype": "^2.1.20"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"typescript": "^5.8.3"
|
|
29
38
|
}
|
|
30
39
|
}
|
|
@@ -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
|
-
* @
|
|
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<any, any, any>>} 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 {
|
|
10
|
+
* @returns {SwarpcServer<Procedures>}
|
|
36
11
|
*/
|
|
37
12
|
export function Server(procedures) {
|
|
38
|
-
/** @type {
|
|
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 {
|
|
95
|
+
* @returns {SwarpcClient<Procedures>}
|
|
131
96
|
*/
|
|
132
97
|
export function Client(procedures) {
|
|
133
|
-
/** @type {
|
|
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 {}
|