@dan-uni/dan-any 0.5.0 → 0.6.8

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/src/index.ts CHANGED
@@ -1,4 +1,7 @@
1
+ import 'reflect-metadata/lite'
2
+
1
3
  import { XMLBuilder, XMLParser } from 'fast-xml-parser'
4
+ import JSONbig from 'json-bigint'
2
5
  import type { Options as AssGenOptions } from './ass-gen'
3
6
  import type { CommandDm as DM_JSON_BiliCommandGrpc } from './proto/gen/bili/dm_pb'
4
7
 
@@ -24,6 +27,10 @@ import { UniID as ID } from './utils/id-gen'
24
27
  import * as UniIDTools from './utils/id-gen'
25
28
  import * as platform from './utils/platform'
26
29
 
30
+ const JSON = JSONbig({
31
+ useNativeBigInt: true,
32
+ })
33
+
27
34
  export interface DM_XML_Bili {
28
35
  i: {
29
36
  chatserver: string
@@ -273,7 +280,7 @@ export class UniPool {
273
280
  ]
274
281
  >(
275
282
  ([result, cache, mergeObj], danmaku) => {
276
- const key = ['SOID', 'content', 'mode', 'platform', 'pool']
283
+ const key = ['content', 'mode', 'platform', 'pool']
277
284
  .map((k) => danmaku[k as keyof UniDM])
278
285
  .join('|')
279
286
  const cached = cache[key]
@@ -281,7 +288,7 @@ export class UniPool {
281
288
  if (
282
289
  cached &&
283
290
  danmaku.progress - lastAppearTime <= lifetime &&
284
- danmaku.isSameAs(cached)
291
+ danmaku.isSameAs(cached, { skipDanuniMerge: true })
285
292
  ) {
286
293
  const senders = mergeObj[key].senders
287
294
  senders.push(danmaku.senderID)
@@ -289,7 +296,9 @@ export class UniPool {
289
296
  extra.danuni = extra.danuni || {}
290
297
  extra.danuni.merge = {
291
298
  count: senders.length,
292
- duration: danmaku.progress - cached.progress,
299
+ duration: Number.parseFloat(
300
+ (danmaku.progress - cached.progress).toFixed(3),
301
+ ),
293
302
  senders,
294
303
  taolu_count: senders.length,
295
304
  taolu_senders: senders,
@@ -321,7 +330,7 @@ export class UniPool {
321
330
  // 处理结果,删除senders<=1的merge字段
322
331
  const [result, _cache, mergeObj] = mergeContext
323
332
  result.forEach((danmaku, i) => {
324
- const key = ['SOID', 'content', 'mode', 'platform', 'pool']
333
+ const key = ['content', 'mode', 'platform', 'pool']
325
334
  .map((k) => danmaku[k as keyof UniDM])
326
335
  .join('|')
327
336
  const extra = result[i].extra,
@@ -349,8 +358,8 @@ export class UniPool {
349
358
  } else {
350
359
  result[i].senderID = 'merge[bot]@dan-any'
351
360
  result[i].attr
352
- ? result[i].attr.push('Protect')
353
- : (result[i].attr = ['Protect'])
361
+ ? result[i].attr.push(UniDMTools.DMAttr.Protect)
362
+ : (result[i].attr = [UniDMTools.DMAttr.Protect])
354
363
  }
355
364
  }
356
365
  })
@@ -389,24 +398,14 @@ export class UniPool {
389
398
  static fromPb(bin: Uint8Array | ArrayBuffer) {
390
399
  const data = fromBinary(DanmakuReplySchema, new Uint8Array(bin))
391
400
  return new UniPool(
392
- data.danmakus.map(
393
- (d) =>
394
- new UniDM(
395
- d.SOID,
396
- d.progress,
397
- d.mode as number,
398
- d.fontsize,
399
- d.color,
400
- d.senderID,
401
- d.content,
402
- timestampDate(d.ctime || timestampNow()),
403
- d.weight,
404
- d.pool as number,
405
- d.attr as UniDMTools.DMAttr[],
406
- d.platform,
407
- d.extra,
408
- d.DMID,
409
- ),
401
+ data.danmakus.map((d) =>
402
+ UniDM.create({
403
+ ...d,
404
+ mode: d.mode as number,
405
+ ctime: timestampDate(d.ctime || timestampNow()),
406
+ pool: d.pool as number,
407
+ attr: d.attr as UniDMTools.DMAttr[],
408
+ }),
410
409
  ),
411
410
  )
412
411
  }
@@ -443,35 +442,41 @@ export class UniPool {
443
442
  oriData: DM_XML_Bili = parser.parse(xml),
444
443
  dans = oriData.i.d
445
444
  return new UniPool(
446
- dans.map((d) => {
447
- const p_str = d['@_p'],
448
- p_arr = p_str.split(',')
449
- return UniDM.fromBili(
450
- {
451
- content: d['#text'],
452
- progress: Number.parseFloat(p_arr[0]),
453
- mode: Number.parseInt(p_arr[1]),
454
- fontsize: Number.parseInt(p_arr[2]),
455
- color: Number.parseInt(p_arr[3]),
456
- ctime: BigInt(p_arr[4]),
457
- pool: Number.parseInt(p_arr[5]),
458
- midHash: p_arr[6],
459
- id: BigInt(p_arr[7]),
460
- weight: Number.parseInt(p_arr[8]),
461
- },
462
- BigInt(oriData.i.chatid),
463
- )
464
- }),
445
+ dans
446
+ .map((d) => {
447
+ const p_str = d['@_p'],
448
+ p_arr = p_str.split(',')
449
+ return UniDM.fromBili(
450
+ {
451
+ content: d['#text'],
452
+ progress: Number.parseFloat(p_arr[0]),
453
+ mode: Number.parseInt(p_arr[1]),
454
+ fontsize: Number.parseInt(p_arr[2]),
455
+ color: Number.parseInt(p_arr[3]),
456
+ ctime: BigInt(p_arr[4]),
457
+ pool: Number.parseInt(p_arr[5]),
458
+ midHash: p_arr[6],
459
+ id: BigInt(p_arr[7]),
460
+ weight: Number.parseInt(p_arr[8]),
461
+ },
462
+ BigInt(oriData.i.chatid),
463
+ )
464
+ })
465
+ .filter((d) => d !== null),
465
466
  )
466
467
  }
467
468
  toBiliXML(): string {
468
469
  const genCID = (id: string) => {
469
470
  const UniID = ID.fromString(id)
470
471
  if (UniID.domain === platform.PlatformVideoSource.Bilibili) {
471
- const cid = Number(UniID.id.replaceAll('def::', ''))
472
+ const cid = UniID.id.replaceAll(
473
+ `def_${platform.PlatformVideoSource.Bilibili}+`,
474
+ '',
475
+ )
476
+
472
477
  if (cid) return cid
473
478
  }
474
- return Number.parseInt(Buffer.from(id).toString('hex'), 16)
479
+ return Number.parseInt(Buffer.from(id).toString('hex'), 16).toString()
475
480
  }
476
481
  const builder = new XMLBuilder({ ignoreAttributes: false })
477
482
  return builder.build({
@@ -1,4 +1,4 @@
1
- // @generated by protoc-gen-es v2.6.0 with parameter "target=ts"
1
+ // @generated by protoc-gen-es v2.6.2 with parameter "target=ts"
2
2
  // @generated from file bili/dm.proto (package bilibili.community.service.dm.v1, syntax proto3)
3
3
  /* eslint-disable */
4
4
 
@@ -1,4 +1,4 @@
1
- // @generated by protoc-gen-es v2.6.0 with parameter "target=ts"
1
+ // @generated by protoc-gen-es v2.6.2 with parameter "target=ts"
2
2
  // @generated from file danuni.proto (package danuni.danmaku.v1, syntax proto3)
3
3
  /* eslint-disable */
4
4
 
@@ -114,13 +114,16 @@ describe('其它', () => {
114
114
  UniDM.create({ ...commonSample }),
115
115
  UniDM.create({ ...commonSample, extra: { artplayer: { border: true } } }),
116
116
  ]
117
+ let counter = 0
117
118
  for (const pool of pool2) {
118
119
  console.info(pool.extraStr)
119
120
  console.info(pool2[0].isSameAs(pool))
121
+ if (counter <= 2) expect(pool2[0].extraStr).toBe(undefined)
122
+ counter++
120
123
  }
121
124
  expect(pool2[0].isSameAs(pool2[1])).toBe(true)
122
125
  expect(pool2[0].isSameAs(pool2[2])).toBe(true)
123
- expect(pool2[0].isSameAs(pool2[3])).toBe(true)
126
+ expect(pool2[0].isSameAs(pool2[3])).toBe(false)
124
127
  expect(pool2[0].isSameAs(pool2[4])).toBe(false)
125
128
  })
126
129
  })