dsh-plugin-syncer 0.0.0-stage → 0.2.1

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/lib/util.js ADDED
@@ -0,0 +1,163 @@
1
+ import { createHash, randomBytes } from 'node:crypto'
2
+ import fs from 'node:fs'
3
+ import os from 'node:os'
4
+ import path from 'node:path'
5
+
6
+ export function sha256(text) {
7
+ return createHash('sha256').update(text, 'utf8').digest('hex')
8
+ }
9
+
10
+ /** Deterministic JSON with sorted object keys, so equal values hash equally. */
11
+ export function stableStringify(value) {
12
+ const seen = new WeakSet()
13
+ const walk = (v) => {
14
+ if (v === null) return 'null'
15
+ const type = typeof v
16
+ if (type === 'number' || type === 'string' || type === 'boolean') return JSON.stringify(v)
17
+ if (type === 'undefined') return 'null'
18
+ if (type === 'bigint') return JSON.stringify(v.toString())
19
+ if (Array.isArray(v)) return '[' + v.map(walk).join(',') + ']'
20
+ if (type === 'object') {
21
+ if (seen.has(v)) throw new Error('circular value')
22
+ seen.add(v)
23
+ const keys = Object.keys(v).filter((k) => v[k] !== undefined).sort()
24
+ const body = keys.map((k) => JSON.stringify(k) + ':' + walk(v[k])).join(',')
25
+ seen.delete(v)
26
+ return '{' + body + '}'
27
+ }
28
+ return JSON.stringify(String(v))
29
+ }
30
+ return walk(value)
31
+ }
32
+
33
+ export function valueHash(value) {
34
+ return sha256(stableStringify(value)).slice(0, 20)
35
+ }
36
+
37
+ export function isJsonSafe(value) {
38
+ try {
39
+ const round = JSON.parse(JSON.stringify(value))
40
+ return stableStringify(round) === stableStringify(value)
41
+ } catch {
42
+ return false
43
+ }
44
+ }
45
+
46
+ export function isPlainObject(value) {
47
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
48
+ }
49
+
50
+ export function sleep(ms, signal) {
51
+ return new Promise((resolve, reject) => {
52
+ if (signal?.aborted) return reject(signal.reason ?? new Error('aborted'))
53
+ const timer = setTimeout(() => {
54
+ signal?.removeEventListener('abort', onAbort)
55
+ resolve()
56
+ }, ms)
57
+ function onAbort() {
58
+ clearTimeout(timer)
59
+ reject(signal.reason ?? new Error('aborted'))
60
+ }
61
+ signal?.addEventListener('abort', onAbort, { once: true })
62
+ })
63
+ }
64
+
65
+ export function ensureDir(dir) {
66
+ fs.mkdirSync(dir, { recursive: true })
67
+ }
68
+
69
+ export function readJson(file) {
70
+ try {
71
+ return JSON.parse(fs.readFileSync(file, 'utf8'))
72
+ } catch {
73
+ return null
74
+ }
75
+ }
76
+
77
+ export function writeJson(file, value, { atomic = true } = {}) {
78
+ ensureDir(path.dirname(file))
79
+ const text = JSON.stringify(value, null, 2) + '\n'
80
+ if (!atomic) {
81
+ fs.writeFileSync(file, text, 'utf8')
82
+ return
83
+ }
84
+ const tmp = `${file}.${process.pid}.${Date.now()}.tmp`
85
+ fs.writeFileSync(tmp, text, 'utf8')
86
+ try {
87
+ fs.renameSync(tmp, file)
88
+ } catch {
89
+ fs.rmSync(tmp, { force: true })
90
+ fs.writeFileSync(file, text, 'utf8')
91
+ }
92
+ }
93
+
94
+ export function sanitizeId(value, fallback = 'device') {
95
+ const text = String(value ?? '')
96
+ .toLowerCase()
97
+ .trim()
98
+ .replace(/[^a-z0-9._-]+/g, '-')
99
+ .replace(/^[-.]+|[-.]+$/g, '')
100
+ .slice(0, 64)
101
+ return text || fallback
102
+ }
103
+
104
+ export function defaultDeviceId() {
105
+ const host = sanitizeId(os.hostname().replace(/\.local$/i, ''), 'mine')
106
+ return `${host}-${randomBytes(3).toString('hex')}`
107
+ }
108
+
109
+ export function nowIso() {
110
+ return new Date().toISOString()
111
+ }
112
+
113
+ export function clampInt(value, min, max, fallback) {
114
+ const n = Number(value)
115
+ if (!Number.isFinite(n)) return fallback
116
+ return Math.min(max, Math.max(min, Math.round(n)))
117
+ }
118
+
119
+ const SEMVER = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/
120
+
121
+ export function isSemver(value) {
122
+ return typeof value === 'string' && SEMVER.test(value)
123
+ }
124
+
125
+ function parseSemver(value) {
126
+ const match = SEMVER.exec(value)
127
+ if (!match) return null
128
+ return {
129
+ core: [Number(match[1]), Number(match[2]), Number(match[3])],
130
+ pre: match[4] ? match[4].split('.') : [],
131
+ }
132
+ }
133
+
134
+ /** Semver ordering: negative when a < b, positive when a > b, 0 when equal. */
135
+ export function compareSemver(a, b) {
136
+ const pa = parseSemver(a)
137
+ const pb = parseSemver(b)
138
+ if (!pa || !pb) return 0
139
+ for (let i = 0; i < 3; i += 1) {
140
+ if (pa.core[i] !== pb.core[i]) return pa.core[i] - pb.core[i]
141
+ }
142
+ if (pa.pre.length === 0 && pb.pre.length === 0) return 0
143
+ if (pa.pre.length === 0) return 1
144
+ if (pb.pre.length === 0) return -1
145
+ for (let i = 0; i < Math.max(pa.pre.length, pb.pre.length); i += 1) {
146
+ if (pa.pre[i] === undefined) return -1
147
+ if (pb.pre[i] === undefined) return 1
148
+ const aNum = /^\d+$/.test(pa.pre[i])
149
+ const bNum = /^\d+$/.test(pb.pre[i])
150
+ if (aNum && bNum) {
151
+ const diff = Number(pa.pre[i]) - Number(pb.pre[i])
152
+ if (diff !== 0) return diff
153
+ } else if (aNum) {
154
+ return -1
155
+ } else if (bNum) {
156
+ return 1
157
+ } else {
158
+ const diff = pa.pre[i].localeCompare(pb.pre[i])
159
+ if (diff !== 0) return diff
160
+ }
161
+ }
162
+ return 0
163
+ }
package/locale/en.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Settings sync",
4
+ "description": "Keeps plugin settings, enable/disable state and the plugin set in sync between machines through a private Git repository or a shared folder."
5
+ }
6
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "设置同步",
4
+ "description": "通过私有 Git 仓库或共享目录,在多台电脑之间同步插件设置、启用/停用状态和插件清单。"
5
+ }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "dsh-plugin-syncer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.1",
4
+ "description": "Two-way sync of DeepSeek Harness plugin settings between machines over a private Gitee/GitHub repository or a shared folder.",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "exports": {
8
+ ".": "./index.js",
9
+ "./locale/*.json": "./locale/*.json",
10
+ "./icon": "./icon.svg"
11
+ },
12
+ "files": [
13
+ "index.js",
14
+ "lib",
15
+ "cordis.patch.yml",
16
+ "locale/*.json",
17
+ "icon.svg",
18
+ "README.md"
19
+ ],
20
+ "dsh": {
21
+ "bundle": {
22
+ "patch": "./cordis.patch.yml"
23
+ }
24
+ },
25
+ "engines": {
26
+ "node": ">=20"
27
+ }
28
+ }