Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ghoulscript): refactor rpc call #29

Merged
merged 6 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/ghoulscript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ await fs.writeFile(resolve(__dirname, './sample.jpg'), output)
| `format` | `String` | `jpg` | Render format, valid value is `jpg` or `png` |
| `args` | `String[]` | - | Additional arguments |

## getInfo (file: Buffer, options?: { password: string })
### getInfo (file: Buffer, options?: { password: string })

Extract pages info

Expand Down Expand Up @@ -188,7 +188,7 @@ console.log(info)
*/
```

## isRequirePassword (file: Buffer)
### isRequirePassword (file: Buffer)

Check document is require password or not to open.

Expand Down
5 changes: 4 additions & 1 deletion packages/ghoulscript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"test": "node --test"
},
"devDependencies": {
"@types/uuid": "^10",
"typescript": "5.5.4",
"vite": "5.3.4",
"vite-plugin-dts": "3.9.1",
Expand All @@ -31,7 +32,9 @@
"dependencies": {
"@privyid/ghostscript": "workspace:^",
"defu": "^6.1.4",
"ufo": "^1.5.3"
"sref": "^1.0.1",
"ufo": "^1.5.3",
"uuid": "^10.0.0"
},
"license": "AGPL-3.0-only",
"publishConfig": {
Expand Down
12 changes: 8 additions & 4 deletions packages/ghoulscript/src/rpc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ export type CommandArgs<C extends Commands> = Parameters<Core[C]>

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

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

return await (core[name] as (...args: any[]) => Promise<any>)(...args)
export async function callRPC<C extends Commands> (method: C, params: CommandArgs<C>): Promise<CommandResult<C>> {
const command: ((...args: any[]) => Promise<any>) | undefined = $core.get(method)

if (typeof command !== 'function')
throw new TypeError(`Invalid action: "${method}"`)

return await command(...params)
}
34 changes: 19 additions & 15 deletions packages/ghoulscript/src/rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import type {
CommandResult,
Commands,
} from './call'
import { useRef } from '../utils/use-ref'

export interface RPC <C extends Commands = any> {
id: number,
name: C,
args: CommandArgs<C>,
import useRef from 'sref'
import { v7 as uuid } from 'uuid'

export interface RPCRequest <C extends Commands = any> {
jsonrpc: '2.0',
id: number | string,
method: C,
params: CommandArgs<C>,
}

export interface RPCResult<C extends Commands = any> {
id: number,
export interface RPCResponse<C extends Commands = any> {
jsonrpc: '2.0',
id: number | string,
result: CommandResult<C>,
error?: undefined,
}
Expand All @@ -29,7 +32,7 @@ export async function useWorkerRPC () {
isLoading.value = true

try {
const { default: RpcWorker } = await import('./worker?worker&inline')
const { default: RpcWorker } = await import('./worker?worker')

worker.value = new RpcWorker({ name: 'rpc-worker' })
} finally {
Expand All @@ -44,12 +47,12 @@ export function setWorkerRPC (worker_: Worker) {
worker.value = worker_
}

export async function callWorkerRPC<C extends Commands, A extends CommandArgs<C>> (name: C, args: A) {
const id = Date.now()
export async function callWorkerRPC<C extends Commands, A extends CommandArgs<C>> (method: C, params: A) {
const id = uuid()
const worker = await useWorkerRPC()

return await new Promise<CommandResult<C>>((resolve, reject) => {
const onMessage = (event: MessageEvent<RPCResult<C>>) => {
const onMessage = (event: MessageEvent<RPCResponse<C>>) => {
if (event.data.id === id) {
cleanUp()

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

worker.postMessage({
id : id,
name: name,
args: args,
jsonrpc: '2.0',
id : id,
method : method,
params : params,
})
})
}
4 changes: 2 additions & 2 deletions packages/ghoulscript/src/rpc/worker.local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CommandArgs, Commands } from './call'

export async function callLocalRPC<C extends Commands, A extends CommandArgs<C>> (name: C, args: A) {
export async function callLocalRPC<C extends Commands, A extends CommandArgs<C>> (method: C, params: A) {
// eslint-disable-next-line unicorn/no-await-expression-member
return (await import('./call')).callRPC(name, args)
return (await import('./call')).callRPC(method, params)
}
16 changes: 9 additions & 7 deletions packages/ghoulscript/src/rpc/worker.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
/* eslint-disable @typescript-eslint/triple-slash-reference */
/* eslint-env serviceworker */

import type { RPC } from '.'
import type { RPCRequest } from '.'
import { callRPC } from './call'

self.addEventListener('message', (event: MessageEvent<RPC>) => {
self.addEventListener('message', (event: MessageEvent<RPCRequest>) => {
const rpc = event.data
const id = rpc.id

callRPC(rpc.name, rpc.args)
callRPC(rpc.method, rpc.params)
.then((result) => {
self.postMessage({
id,
result,
jsonrpc: '2.0',
id : id,
result : result,
})
})
.catch((error) => {
self.postMessage({
id,
error,
jsonrpc: '2.0',
id : id,
error : error,
})
})
})
86 changes: 0 additions & 86 deletions packages/ghoulscript/src/utils/use-ref.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/ghoulscript/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
nodePolyfills(),
externalizeDeps(),
],
base : './',
build: {
minify: false,
lib : {
Expand Down
26 changes: 26 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,12 @@ __metadata:
resolution: "@privyid/ghoulscript@workspace:packages/ghoulscript"
dependencies:
"@privyid/ghostscript": "workspace:^"
"@types/uuid": "npm:^10"
defu: "npm:^6.1.4"
sref: "npm:^1.0.1"
typescript: "npm:5.5.4"
ufo: "npm:^1.5.3"
uuid: "npm:^10.0.0"
vite: "npm:5.3.4"
vite-plugin-dts: "npm:3.9.1"
vite-plugin-externalize-deps: "npm:0.8.0"
Expand Down Expand Up @@ -988,6 +991,13 @@ __metadata:
languageName: node
linkType: hard

"@types/uuid@npm:^10":
version: 10.0.0
resolution: "@types/uuid@npm:10.0.0"
checksum: 10c0/9a1404bf287164481cb9b97f6bb638f78f955be57c40c6513b7655160beb29df6f84c915aaf4089a1559c216557dc4d2f79b48d978742d3ae10b937420ddac60
languageName: node
linkType: hard

"@typescript-eslint/eslint-plugin@npm:5.62.0":
version: 5.62.0
resolution: "@typescript-eslint/eslint-plugin@npm:5.62.0"
Expand Down Expand Up @@ -5882,6 +5892,13 @@ __metadata:
languageName: node
linkType: hard

"sref@npm:^1.0.1":
version: 1.0.1
resolution: "sref@npm:1.0.1"
checksum: 10c0/3d51cf4392e42f187a72c92df1c81227999df4511b43d64a33787faf84e83d89f483385b0f1fe0e54e9aa027397d0a81243b0200fea3dc0a4b0eb0fba6eb1ec4
languageName: node
linkType: hard

"ssri@npm:^10.0.0":
version: 10.0.6
resolution: "ssri@npm:10.0.6"
Expand Down Expand Up @@ -6443,6 +6460,15 @@ __metadata:
languageName: node
linkType: hard

"uuid@npm:^10.0.0":
version: 10.0.0
resolution: "uuid@npm:10.0.0"
bin:
uuid: dist/bin/uuid
checksum: 10c0/eab18c27fe4ab9fb9709a5d5f40119b45f2ec8314f8d4cf12ce27e4c6f4ffa4a6321dc7db6c515068fa373c075b49691ba969f0010bf37f44c37ca40cd6bf7fe
languageName: node
linkType: hard

"validate-npm-package-license@npm:^3.0.1":
version: 3.0.4
resolution: "validate-npm-package-license@npm:3.0.4"
Expand Down
Loading