dsh-cait 1.0.3 → 1.0.5

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/client/rpc.js CHANGED
@@ -1,93 +1,93 @@
1
- class RpcError extends Error {
2
- constructor(message, code) {
3
- super(message)
4
- this.name = 'RpcError'
5
- this.code = code
6
- }
7
- }
8
-
9
- async function rpc(method, params = {}) {
10
- let response
11
- try {
12
- response = await fetch(API, {
13
- method: 'POST',
14
- credentials: 'same-origin',
15
- cache: 'no-store',
16
- headers: { 'Content-Type': 'application/json' },
17
- body: JSON.stringify({ method, params }),
18
- })
19
- } catch (error) {
20
- throw new RpcError(`无法连接 CAIT 服务:${error?.message ?? error}`, 'NETWORK')
21
- }
22
-
23
- let payload = null
24
- try {
25
- payload = await response.json()
26
- } catch {
27
- payload = null
28
- }
29
-
30
- if (!response.ok || !payload?.ok) {
31
- const message = payload?.error?.message || `CAIT 服务返回 HTTP ${response.status}`
32
- throw new RpcError(message, payload?.error?.code || 'HTTP')
33
- }
34
- return payload.data
35
- }
36
-
37
- function escapeRegExp(value) {
38
- return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
39
- }
40
-
41
- function formatBytes(bytes) {
42
- if (!Number.isFinite(bytes)) return ''
43
- if (bytes < 1024) return `${bytes} B`
44
- if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
45
- return `${(bytes / 1024 / 1024).toFixed(1)} MB`
46
- }
47
-
48
- function stripExtension(name) {
49
- return String(name || '').replace(/\.[^.]+$/, '')
50
- }
51
-
52
- function filePayload(file) {
53
- const extension = (file.name.match(/\.([a-z0-9]+)$/i)?.[1] || '').toLowerCase()
54
- if (extension === 'docx' || extension === 'xlsx' || extension === 'pptx') {
55
- return file.arrayBuffer().then((buffer) => {
56
- const bytes = new Uint8Array(buffer)
57
- let binary = ''
58
- const chunk = 0x8000
59
- for (let offset = 0; offset < bytes.length; offset += chunk) {
60
- binary += String.fromCharCode.apply(null, bytes.subarray(offset, offset + chunk))
61
- }
62
- return { name: file.name, base64: btoa(binary) }
63
- })
64
- }
65
- return file.text().then((text) => ({ name: file.name, text }))
66
- }
67
-
68
- function downloadFile(filename, content, mime) {
69
- const blob = new Blob([content], { type: mime || 'application/octet-stream' })
70
- const url = URL.createObjectURL(blob)
71
- const link = document.createElement('a')
72
- link.href = url
73
- link.download = filename || 'cait-export'
74
- link.rel = 'noopener'
75
- document.body.appendChild(link)
76
- link.click()
77
- link.remove()
78
- setTimeout(() => URL.revokeObjectURL(url), 2000)
79
- }
80
-
81
- function decodeBase64(value) {
82
- const binary = atob(String(value ?? ''))
83
- const bytes = new Uint8Array(binary.length)
84
- for (let index = 0; index < binary.length; index += 1) {
85
- bytes[index] = binary.charCodeAt(index)
86
- }
87
- return bytes
88
- }
89
-
90
- async function copyText(value) {
91
- if (!navigator.clipboard?.writeText) throw new Error('当前环境不支持剪贴板。')
92
- await navigator.clipboard.writeText(value)
93
- }
1
+ class RpcError extends Error {
2
+ constructor(message, code) {
3
+ super(message)
4
+ this.name = 'RpcError'
5
+ this.code = code
6
+ }
7
+ }
8
+
9
+ async function rpc(method, params = {}) {
10
+ let response
11
+ try {
12
+ response = await fetch(API, {
13
+ method: 'POST',
14
+ credentials: 'same-origin',
15
+ cache: 'no-store',
16
+ headers: { 'Content-Type': 'application/json' },
17
+ body: JSON.stringify({ method, params }),
18
+ })
19
+ } catch (error) {
20
+ throw new RpcError(`无法连接 CAIT 服务:${error?.message ?? error}`, 'NETWORK')
21
+ }
22
+
23
+ let payload = null
24
+ try {
25
+ payload = await response.json()
26
+ } catch {
27
+ payload = null
28
+ }
29
+
30
+ if (!response.ok || !payload?.ok) {
31
+ const message = payload?.error?.message || `CAIT 服务返回 HTTP ${response.status}`
32
+ throw new RpcError(message, payload?.error?.code || 'HTTP')
33
+ }
34
+ return payload.data
35
+ }
36
+
37
+ function escapeRegExp(value) {
38
+ return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
39
+ }
40
+
41
+ function formatBytes(bytes) {
42
+ if (!Number.isFinite(bytes)) return ''
43
+ if (bytes < 1024) return `${bytes} B`
44
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
45
+ return `${(bytes / 1024 / 1024).toFixed(1)} MB`
46
+ }
47
+
48
+ function stripExtension(name) {
49
+ return String(name || '').replace(/\.[^.]+$/, '')
50
+ }
51
+
52
+ function filePayload(file) {
53
+ const extension = (file.name.match(/\.([a-z0-9]+)$/i)?.[1] || '').toLowerCase()
54
+ if (extension === 'docx' || extension === 'xlsx' || extension === 'pptx' || extension === 'epub') {
55
+ return file.arrayBuffer().then((buffer) => {
56
+ const bytes = new Uint8Array(buffer)
57
+ let binary = ''
58
+ const chunk = 0x8000
59
+ for (let offset = 0; offset < bytes.length; offset += chunk) {
60
+ binary += String.fromCharCode.apply(null, bytes.subarray(offset, offset + chunk))
61
+ }
62
+ return { name: file.name, base64: btoa(binary) }
63
+ })
64
+ }
65
+ return file.text().then((text) => ({ name: file.name, text }))
66
+ }
67
+
68
+ function downloadFile(filename, content, mime) {
69
+ const blob = new Blob([content], { type: mime || 'application/octet-stream' })
70
+ const url = URL.createObjectURL(blob)
71
+ const link = document.createElement('a')
72
+ link.href = url
73
+ link.download = filename || 'cait-export'
74
+ link.rel = 'noopener'
75
+ document.body.appendChild(link)
76
+ link.click()
77
+ link.remove()
78
+ setTimeout(() => URL.revokeObjectURL(url), 2000)
79
+ }
80
+
81
+ function decodeBase64(value) {
82
+ const binary = atob(String(value ?? ''))
83
+ const bytes = new Uint8Array(binary.length)
84
+ for (let index = 0; index < binary.length; index += 1) {
85
+ bytes[index] = binary.charCodeAt(index)
86
+ }
87
+ return bytes
88
+ }
89
+
90
+ async function copyText(value) {
91
+ if (!navigator.clipboard?.writeText) throw new Error('当前环境不支持剪贴板。')
92
+ await navigator.clipboard.writeText(value)
93
+ }