Skip to content

Commit 486728f

Browse files
authored
Merge pull request #29 from privy-open-source/fix/validate-name-call
refactor(ghoulscript): refactor rpc call
2 parents a276498 + c4c410e commit 486728f

File tree

9 files changed

+71
-117
lines changed

9 files changed

+71
-117
lines changed

packages/ghoulscript/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ await fs.writeFile(resolve(__dirname, './sample.jpg'), output)
153153
| `format` | `String` | `jpg` | Render format, valid value is `jpg` or `png` |
154154
| `args` | `String[]` | - | Additional arguments |
155155

156-
## getInfo (file: Buffer, options?: { password: string })
156+
### getInfo (file: Buffer, options?: { password: string })
157157

158158
Extract pages info
159159

@@ -188,7 +188,7 @@ console.log(info)
188188
*/
189189
```
190190

191-
## isRequirePassword (file: Buffer)
191+
### isRequirePassword (file: Buffer)
192192

193193
Check document is require password or not to open.
194194

packages/ghoulscript/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"test": "node --test"
2323
},
2424
"devDependencies": {
25+
"@types/uuid": "^10",
2526
"typescript": "5.5.4",
2627
"vite": "5.3.4",
2728
"vite-plugin-dts": "3.9.1",
@@ -31,7 +32,9 @@
3132
"dependencies": {
3233
"@privyid/ghostscript": "workspace:^",
3334
"defu": "^6.1.4",
34-
"ufo": "^1.5.3"
35+
"sref": "^1.0.1",
36+
"ufo": "^1.5.3",
37+
"uuid": "^10.0.0"
3538
},
3639
"license": "AGPL-3.0-only",
3740
"publishConfig": {

packages/ghoulscript/src/rpc/call.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ export type CommandArgs<C extends Commands> = Parameters<Core[C]>
88

99
export type CommandResult<C extends Commands> = ReturnType<Core[C]>
1010

11-
export async function callRPC<C extends Commands> (name: C, args: CommandArgs<C>): Promise<CommandResult<C>> {
12-
if (typeof core[name] !== 'function')
13-
throw new TypeError(`Invalid action: "${name}"`)
11+
const $core = new Map(Object.entries(core))
1412

15-
return await (core[name] as (...args: any[]) => Promise<any>)(...args)
13+
export async function callRPC<C extends Commands> (method: C, params: CommandArgs<C>): Promise<CommandResult<C>> {
14+
const command: ((...args: any[]) => Promise<any>) | undefined = $core.get(method)
15+
16+
if (typeof command !== 'function')
17+
throw new TypeError(`Invalid action: "${method}"`)
18+
19+
return await command(...params)
1620
}

packages/ghoulscript/src/rpc/index.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import type {
44
CommandResult,
55
Commands,
66
} from './call'
7-
import { useRef } from '../utils/use-ref'
8-
9-
export interface RPC <C extends Commands = any> {
10-
id: number,
11-
name: C,
12-
args: CommandArgs<C>,
7+
import useRef from 'sref'
8+
import { v7 as uuid } from 'uuid'
9+
10+
export interface RPCRequest <C extends Commands = any> {
11+
jsonrpc: '2.0',
12+
id: number | string,
13+
method: C,
14+
params: CommandArgs<C>,
1315
}
1416

15-
export interface RPCResult<C extends Commands = any> {
16-
id: number,
17+
export interface RPCResponse<C extends Commands = any> {
18+
jsonrpc: '2.0',
19+
id: number | string,
1720
result: CommandResult<C>,
1821
error?: undefined,
1922
}
@@ -29,7 +32,7 @@ export async function useWorkerRPC () {
2932
isLoading.value = true
3033

3134
try {
32-
const { default: RpcWorker } = await import('./worker?worker&inline')
35+
const { default: RpcWorker } = await import('./worker?worker')
3336

3437
worker.value = new RpcWorker({ name: 'rpc-worker' })
3538
} finally {
@@ -44,12 +47,12 @@ export function setWorkerRPC (worker_: Worker) {
4447
worker.value = worker_
4548
}
4649

47-
export async function callWorkerRPC<C extends Commands, A extends CommandArgs<C>> (name: C, args: A) {
48-
const id = Date.now()
50+
export async function callWorkerRPC<C extends Commands, A extends CommandArgs<C>> (method: C, params: A) {
51+
const id = uuid()
4952
const worker = await useWorkerRPC()
5053

5154
return await new Promise<CommandResult<C>>((resolve, reject) => {
52-
const onMessage = (event: MessageEvent<RPCResult<C>>) => {
55+
const onMessage = (event: MessageEvent<RPCResponse<C>>) => {
5356
if (event.data.id === id) {
5457
cleanUp()
5558

@@ -74,9 +77,10 @@ export async function callWorkerRPC<C extends Commands, A extends CommandArgs<C>
7477
worker.addEventListener('error', onError)
7578

7679
worker.postMessage({
77-
id : id,
78-
name: name,
79-
args: args,
80+
jsonrpc: '2.0',
81+
id : id,
82+
method : method,
83+
params : params,
8084
})
8185
})
8286
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CommandArgs, Commands } from './call'
22

3-
export async function callLocalRPC<C extends Commands, A extends CommandArgs<C>> (name: C, args: A) {
3+
export async function callLocalRPC<C extends Commands, A extends CommandArgs<C>> (method: C, params: A) {
44
// eslint-disable-next-line unicorn/no-await-expression-member
5-
return (await import('./call')).callRPC(name, args)
5+
return (await import('./call')).callRPC(method, params)
66
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
/* eslint-disable @typescript-eslint/triple-slash-reference */
22
/* eslint-env serviceworker */
33

4-
import type { RPC } from '.'
4+
import type { RPCRequest } from '.'
55
import { callRPC } from './call'
66

7-
self.addEventListener('message', (event: MessageEvent<RPC>) => {
7+
self.addEventListener('message', (event: MessageEvent<RPCRequest>) => {
88
const rpc = event.data
99
const id = rpc.id
1010

11-
callRPC(rpc.name, rpc.args)
11+
callRPC(rpc.method, rpc.params)
1212
.then((result) => {
1313
self.postMessage({
14-
id,
15-
result,
14+
jsonrpc: '2.0',
15+
id : id,
16+
result : result,
1617
})
1718
})
1819
.catch((error) => {
1920
self.postMessage({
20-
id,
21-
error,
21+
jsonrpc: '2.0',
22+
id : id,
23+
error : error,
2224
})
2325
})
2426
})

packages/ghoulscript/src/utils/use-ref.ts

Lines changed: 0 additions & 86 deletions
This file was deleted.

packages/ghoulscript/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default defineConfig({
99
nodePolyfills(),
1010
externalizeDeps(),
1111
],
12+
base : './',
1213
build: {
1314
minify: false,
1415
lib : {

yarn.lock

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,12 @@ __metadata:
712712
resolution: "@privyid/ghoulscript@workspace:packages/ghoulscript"
713713
dependencies:
714714
"@privyid/ghostscript": "workspace:^"
715+
"@types/uuid": "npm:^10"
715716
defu: "npm:^6.1.4"
717+
sref: "npm:^1.0.1"
716718
typescript: "npm:5.5.4"
717719
ufo: "npm:^1.5.3"
720+
uuid: "npm:^10.0.0"
718721
vite: "npm:5.3.4"
719722
vite-plugin-dts: "npm:3.9.1"
720723
vite-plugin-externalize-deps: "npm:0.8.0"
@@ -988,6 +991,13 @@ __metadata:
988991
languageName: node
989992
linkType: hard
990993

994+
"@types/uuid@npm:^10":
995+
version: 10.0.0
996+
resolution: "@types/uuid@npm:10.0.0"
997+
checksum: 10c0/9a1404bf287164481cb9b97f6bb638f78f955be57c40c6513b7655160beb29df6f84c915aaf4089a1559c216557dc4d2f79b48d978742d3ae10b937420ddac60
998+
languageName: node
999+
linkType: hard
1000+
9911001
"@typescript-eslint/eslint-plugin@npm:5.62.0":
9921002
version: 5.62.0
9931003
resolution: "@typescript-eslint/eslint-plugin@npm:5.62.0"
@@ -5882,6 +5892,13 @@ __metadata:
58825892
languageName: node
58835893
linkType: hard
58845894

5895+
"sref@npm:^1.0.1":
5896+
version: 1.0.1
5897+
resolution: "sref@npm:1.0.1"
5898+
checksum: 10c0/3d51cf4392e42f187a72c92df1c81227999df4511b43d64a33787faf84e83d89f483385b0f1fe0e54e9aa027397d0a81243b0200fea3dc0a4b0eb0fba6eb1ec4
5899+
languageName: node
5900+
linkType: hard
5901+
58855902
"ssri@npm:^10.0.0":
58865903
version: 10.0.6
58875904
resolution: "ssri@npm:10.0.6"
@@ -6443,6 +6460,15 @@ __metadata:
64436460
languageName: node
64446461
linkType: hard
64456462

6463+
"uuid@npm:^10.0.0":
6464+
version: 10.0.0
6465+
resolution: "uuid@npm:10.0.0"
6466+
bin:
6467+
uuid: dist/bin/uuid
6468+
checksum: 10c0/eab18c27fe4ab9fb9709a5d5f40119b45f2ec8314f8d4cf12ce27e4c6f4ffa4a6321dc7db6c515068fa373c075b49691ba969f0010bf37f44c37ca40cd6bf7fe
6469+
languageName: node
6470+
linkType: hard
6471+
64466472
"validate-npm-package-license@npm:^3.0.1":
64476473
version: 3.0.4
64486474
resolution: "validate-npm-package-license@npm:3.0.4"

0 commit comments

Comments
 (0)