dsh-session-superagent 0.10.12 → 2.0.0

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.
@@ -0,0 +1,268 @@
1
+ /**
2
+ * 水位 v2 · 状态房。
3
+ *
4
+ * 纯函数、无 I/O、不理解 GitHub/Gitee 字段。watcher 的其余配置字段原样保留;
5
+ * 本模块只拥有 watermarkVersion / round / obs 三个键。
6
+ */
7
+
8
+ // KB: concurrent-isolation.md —— 状态写锁与行级合并的共同约束。
9
+ export const WATERMARK_V2 = 2
10
+
11
+ function isObject(value) {
12
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
13
+ }
14
+
15
+ function assertKey(key) {
16
+ const text = String(key || '')
17
+ if (!text) throw new TypeError('observation key is required')
18
+ return text
19
+ }
20
+
21
+ function assertCursor(cursor) {
22
+ if (typeof cursor === 'string' && cursor) return cursor
23
+ if (typeof cursor === 'number' && Number.isSafeInteger(cursor) && cursor >= 0) return cursor
24
+ throw new TypeError('cursor must be a non-empty string or a non-negative safe integer')
25
+ }
26
+
27
+ function validRound(value, fallback = 0) {
28
+ const round = value === undefined ? fallback : Number(value)
29
+ if (!Number.isSafeInteger(round) || round < 0) throw new TypeError('round must be a non-negative safe integer')
30
+ return round
31
+ }
32
+
33
+ function nextRound(state, requested) {
34
+ const round = requested === undefined ? state.round + 1 : validRound(requested)
35
+ if (round <= state.round) throw new RangeError(`round must advance beyond ${state.round}`)
36
+ return round
37
+ }
38
+
39
+ function asV2(input) {
40
+ if (input && input.watermarkVersion !== undefined) {
41
+ if (input.watermarkVersion !== WATERMARK_V2) {
42
+ throw new Error(`watermarkVersion ${input.watermarkVersion} is newer than supported ${WATERMARK_V2}`)
43
+ }
44
+ return input
45
+ }
46
+ return createWatermarkState(input)
47
+ }
48
+
49
+ function eventAt(options) {
50
+ return options.at === undefined ? new Date().toISOString() : String(options.at)
51
+ }
52
+
53
+ function cloneEvent(event) {
54
+ return event ? { ...event } : null
55
+ }
56
+
57
+ function cloneRow(row) {
58
+ return {
59
+ ...row,
60
+ delivered: cloneEvent(row.delivered),
61
+ handled: cloneEvent(row.handled),
62
+ }
63
+ }
64
+
65
+ export function createWatermarkState(fields = {}) {
66
+ const source = isObject(fields) ? fields : {}
67
+ return {
68
+ ...source,
69
+ watermarkVersion: WATERMARK_V2,
70
+ round: validRound(source.round),
71
+ obs: isObject(source.obs) ? { ...source.obs } : {},
72
+ }
73
+ }
74
+
75
+ export function observe(input, key, cursor, options = {}) {
76
+ const state = asV2(input)
77
+ const rowKey = assertKey(key)
78
+ const position = assertCursor(cursor)
79
+ const previous = state.obs[rowKey]
80
+ if (previous && !previous.legacy && options.force !== true) return state
81
+
82
+ const round = nextRound(state, options.round)
83
+ let row
84
+ if (!previous) {
85
+ row = { cursor: position, delivered: null, handled: null, retries: 0, round }
86
+ if (options.force === true) row.observedRound = round
87
+ } else {
88
+ row = {
89
+ ...cloneRow(previous),
90
+ cursor: options.legacyCursor === undefined ? position : assertCursor(options.legacyCursor),
91
+ round,
92
+ }
93
+ const alignedCursor = row.cursor
94
+ if (row.delivered) row.delivered.cursor = alignedCursor
95
+ if (row.handled) row.handled.cursor = alignedCursor
96
+ if (options.force === true) row.observedRound = round
97
+ delete row.legacy
98
+ }
99
+ return { ...state, round, obs: { ...state.obs, [rowKey]: row } }
100
+ }
101
+
102
+ export function deliver(input, key, cursor, options = {}) {
103
+ const state = asV2(input)
104
+ const rowKey = assertKey(key)
105
+ const position = assertCursor(cursor)
106
+ const round = nextRound(state, options.round)
107
+ const previous = state.obs[rowKey] || { cursor: position, delivered: null, handled: null, retries: 0 }
108
+ const sameDelivery = previous.delivered && previous.delivered.cursor === position
109
+ const row = {
110
+ ...cloneRow(previous),
111
+ cursor: position,
112
+ delivered: { cursor: position, round, at: eventAt(options) },
113
+ retries: sameDelivery ? (Number(previous.retries) || 0) + 1 : 1,
114
+ round,
115
+ }
116
+ delete row.legacy
117
+ delete row.undelivered
118
+ return { ...state, round, obs: { ...state.obs, [rowKey]: row } }
119
+ }
120
+
121
+ export function handle(input, key, cursor, options = {}) {
122
+ const state = asV2(input)
123
+ const rowKey = assertKey(key)
124
+ const position = assertCursor(cursor)
125
+ const round = nextRound(state, options.round)
126
+ const previous = state.obs[rowKey] || { delivered: null, handled: null, retries: 0 }
127
+ const row = {
128
+ ...cloneRow(previous),
129
+ cursor: position,
130
+ handled: { cursor: position, round, at: eventAt(options) },
131
+ retries: 0,
132
+ round,
133
+ }
134
+ delete row.undelivered
135
+ return { ...state, round, obs: { ...state.obs, [rowKey]: row } }
136
+ }
137
+
138
+ function latestEvent(left, right) {
139
+ if (!left) return cloneEvent(right)
140
+ if (!right) return cloneEvent(left)
141
+ const leftRound = validRound(left.round)
142
+ const rightRound = validRound(right.round)
143
+ if (leftRound !== rightRound) return cloneEvent(leftRound > rightRound ? left : right)
144
+ const leftKey = typeof left.cursor + ':' + String(left.cursor)
145
+ const rightKey = typeof right.cursor + ':' + String(right.cursor)
146
+ return cloneEvent(leftKey >= rightKey ? left : right)
147
+ }
148
+
149
+ function mergeRow(left, right) {
150
+ if (!left) return cloneRow(right)
151
+ if (!right) return cloneRow(left)
152
+ const leftRound = validRound(left.round)
153
+ const rightRound = validRound(right.round)
154
+ let source
155
+ if (leftRound !== rightRound) source = leftRound > rightRound ? left : right
156
+ else {
157
+ const leftHandled = Number(left.handled && left.handled.round) || 0
158
+ const rightHandled = Number(right.handled && right.handled.round) || 0
159
+ if (leftHandled !== rightHandled) source = leftHandled > rightHandled ? left : right
160
+ else source = right
161
+ }
162
+ const row = {
163
+ ...cloneRow(source),
164
+ delivered: latestEvent(left.delivered, right.delivered),
165
+ handled: latestEvent(left.handled, right.handled),
166
+ round: Math.max(leftRound, rightRound),
167
+ }
168
+ const deliveredRound = Number(row.delivered && row.delivered.round) || 0
169
+ const handledRound = Number(row.handled && row.handled.round) || 0
170
+ const winningEvent = row.handled && handledRound >= deliveredRound ? row.handled : row.delivered
171
+ const winningRound = Math.max(deliveredRound, handledRound)
172
+ if (winningEvent && winningRound >= row.round) row.cursor = winningEvent.cursor
173
+ if (row.delivered) {
174
+ delete row.undelivered
175
+ if (row.handled && handledRound >= deliveredRound) {
176
+ row.retries = 0
177
+ } else {
178
+ const matches = [left, right].filter((candidate) => candidate.delivered
179
+ && candidate.delivered.cursor === row.delivered.cursor
180
+ && (Number(candidate.delivered.round) || 0) === deliveredRound)
181
+ row.retries = matches.length > 0 ? Math.max(...matches.map((candidate) => Number(candidate.retries) || 0)) : 0
182
+ }
183
+ }
184
+ return row
185
+ }
186
+
187
+ export function mergeWatermarkStates(stored, polled) {
188
+ const left = asV2(stored)
189
+ const right = asV2(polled)
190
+ const obs = {}
191
+ for (const key of new Set([...Object.keys(left.obs || {}), ...Object.keys(right.obs || {})])) {
192
+ obs[key] = mergeRow(left.obs[key], right.obs[key])
193
+ }
194
+ return {
195
+ ...left,
196
+ watermarkVersion: WATERMARK_V2,
197
+ round: Math.max(validRound(left.round), validRound(right.round)),
198
+ obs,
199
+ }
200
+ }
201
+
202
+ function legacyKey(kind, number) {
203
+ return kind === 'pr' ? `pr:${number}/comment` : `issue:${number}`
204
+ }
205
+
206
+ export function migrateV1Watermark(entry = {}) {
207
+ if (entry.watermarkVersion !== undefined) {
208
+ if (entry.watermarkVersion === WATERMARK_V2) return entry
209
+ throw new Error(`watermarkVersion ${entry.watermarkVersion} is newer than supported ${WATERMARK_V2}`)
210
+ }
211
+ const kind = entry.kind === 'pr' ? 'pr' : 'issue'
212
+ const seen = kind === 'pr'
213
+ ? (isObject(entry.seenComments) ? entry.seenComments : {})
214
+ : (isObject(entry.seen) ? entry.seen : {})
215
+ const pending = isObject(entry.pending) ? entry.pending : {}
216
+ const acked = isObject(entry.acked) ? entry.acked : {}
217
+ const lastAcked = isObject(entry.lastAcked) ? entry.lastAcked : {}
218
+ const lastNumbers = new Set((Array.isArray(lastAcked.numbers) ? lastAcked.numbers : []).map(String))
219
+ const numbers = new Set([
220
+ ...Object.keys(seen), ...Object.keys(pending), ...Object.keys(acked), ...lastNumbers,
221
+ ])
222
+ const ordered = [...numbers].sort((a, b) => Number(a) - Number(b) || String(a).localeCompare(String(b)))
223
+ const obs = {}
224
+ let round = validRound(entry.round)
225
+ for (const number of ordered) {
226
+ const key = legacyKey(kind, number)
227
+ const pendingRecord = isObject(pending[number]) ? pending[number] : null
228
+ const ackRecord = isObject(acked[number]) ? acked[number] : null
229
+ const seenCursor = seen[number] ?? (pendingRecord && pendingRecord.at) ?? (ackRecord && ackRecord.at) ?? lastAcked.at ?? 'unknown'
230
+ const cursor = `v1:${String(seenCursor)}`
231
+ const row = {
232
+ cursor,
233
+ delivered: null,
234
+ handled: null,
235
+ retries: pendingRecord ? Number(pendingRecord.tries) || 0 : 0,
236
+ round,
237
+ legacy: true,
238
+ }
239
+ if (kind === 'pr' && seen[number] !== undefined && !pendingRecord) {
240
+ round += 1
241
+ row.delivered = { cursor, round, at: String(entry.lastWake || entry.lastPoll || '') }
242
+ row.round = round
243
+ }
244
+ if (pendingRecord) {
245
+ round += 1
246
+ if ((Number(pendingRecord.tries) || 0) === 0) {
247
+ row.undelivered = true
248
+ } else {
249
+ row.delivered = { cursor, round, at: String(pendingRecord.at || '') }
250
+ }
251
+ row.round = round
252
+ }
253
+ if (ackRecord || lastNumbers.has(String(number))) {
254
+ round += 1
255
+ row.handled = { cursor, round, at: String((ackRecord && ackRecord.at) || lastAcked.at || '') }
256
+ row.round = round
257
+ row.retries = 0
258
+ }
259
+ obs[key] = row
260
+ }
261
+ return {
262
+ ...entry,
263
+ watermarkVersion: WATERMARK_V2,
264
+ round,
265
+ obs,
266
+ v1Migrated: true,
267
+ }
268
+ }
@@ -1,25 +1,38 @@
1
- /**
2
- * watchers.json 的显式 schema 迁移入口。
3
- *
4
- * 吸收官方 migration 的最小纪律:先迁移、再 normalize;未知未来版本拒绝;
5
- * 失败交给 readState 的 .bak 恢复,不把半新半旧的状态当成正常状态。
6
- * 这里只做我们实际需要的 v0 → v1,不搬 SQLite 迁移框架。
7
- */
1
+ /** watchers.json schema 迁移:未知未来版本拒绝,失败交给 readState 的 .bak。 */
8
2
  export const CURRENT_STATE_SCHEMA = 1
9
3
 
4
+ function isPlainObject(value) {
5
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
6
+ }
7
+
8
+ /** 只增不改地补齐 kind / seenComments;无需变更时保留原引用。 */
9
+ export function migratePrWatchers(state) {
10
+ if (!isPlainObject(state) || !isPlainObject(state.watchers)) return state
11
+ let nextWatchers = null
12
+ for (const [key, entry] of Object.entries(state.watchers)) {
13
+ if (!isPlainObject(entry)) continue
14
+ const isPr = key.startsWith('pr:') || entry.kind === 'pr'
15
+ const next = { ...entry }
16
+ if (next.kind === undefined) next.kind = isPr ? 'pr' : 'issue'
17
+ if (isPr && next.seenComments === undefined) next.seenComments = {}
18
+ if (next.kind !== entry.kind || next.seenComments !== entry.seenComments) {
19
+ if (!nextWatchers) nextWatchers = { ...state.watchers }
20
+ nextWatchers[key] = next
21
+ }
22
+ }
23
+ return nextWatchers ? { ...state, watchers: nextWatchers } : state
24
+ }
25
+
10
26
  export function migrateState(input) {
11
- const raw = input && typeof input === 'object' && !Array.isArray(input) ? input : {}
27
+ const raw = isPlainObject(input) ? input : {}
12
28
  const version = raw.schemaVersion === undefined ? 0 : Number(raw.schemaVersion)
13
29
  if (!Number.isInteger(version) || version < 0) throw new Error('invalid state schemaVersion: ' + String(raw.schemaVersion))
14
- if (version > CURRENT_STATE_SCHEMA) {
15
- throw new Error('state schemaVersion ' + version + ' is newer than supported ' + CURRENT_STATE_SCHEMA)
16
- }
17
- if (version === CURRENT_STATE_SCHEMA) return { state: raw, changed: false, from: version }
18
- // v0:没有 schemaVersion 的历史文件。保留所有已有字段,只补版本与必需容器。
19
- const state = {
30
+ if (version > CURRENT_STATE_SCHEMA) throw new Error('state schemaVersion ' + version + ' is newer than supported ' + CURRENT_STATE_SCHEMA)
31
+ if (version === CURRENT_STATE_SCHEMA) return { state: migratePrWatchers(raw), changed: false, from: version }
32
+ const state = migratePrWatchers({
20
33
  ...raw,
21
34
  schemaVersion: CURRENT_STATE_SCHEMA,
22
- watchers: raw.watchers && typeof raw.watchers === 'object' && !Array.isArray(raw.watchers) ? raw.watchers : {},
23
- }
35
+ watchers: isPlainObject(raw.watchers) ? raw.watchers : {},
36
+ })
24
37
  return { state, changed: true, from: version }
25
38
  }