@dan-uni/dan-any 0.6.9 → 0.7.4

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.test.ts CHANGED
@@ -75,4 +75,26 @@ describe('其它', () => {
75
75
  it('合并范围内重复', () => {
76
76
  console.info(pool.merge(10).minify())
77
77
  })
78
+ it('禁止DMID生成', () => {
79
+ console.info(
80
+ UniPool.fromBiliXML(xml, {
81
+ dmid: false,
82
+ }),
83
+ )
84
+ })
85
+ it('自定义DMID生成长度', () => {
86
+ console.info(
87
+ UniPool.fromBiliXML(xml, {
88
+ dmid: 64,
89
+ }),
90
+ )
91
+ })
92
+ it('自定义DMID生成器', () => {
93
+ console.info(
94
+ UniPool.fromBiliXML(xml, {
95
+ dmid: (_content, _senderID, ctime = new Date(), _extraStr) =>
96
+ ctime.toString() + Math.random(),
97
+ }),
98
+ )
99
+ })
78
100
  })
package/src/index.ts CHANGED
@@ -94,6 +94,7 @@ type UniPoolPipeSync = (that: UniPool) => UniPool
94
94
 
95
95
  interface Options {
96
96
  dedupe?: boolean
97
+ dmid?: boolean | number | UniIDTools.DMIDGenerator
97
98
  }
98
99
 
99
100
  export class UniPool {
@@ -223,8 +224,8 @@ export class UniPool {
223
224
  platform: s.platform.sort((a, b) => b.count - a.count)[0].val,
224
225
  }
225
226
  }
226
- static create() {
227
- return new UniPool([])
227
+ static create(options?: Options) {
228
+ return new UniPool([], options)
228
229
  }
229
230
  /**
230
231
  * 合并弹幕/弹幕库
@@ -255,9 +256,11 @@ export class UniPool {
255
256
  * 基于DMID的基本去重功能,用于解决该class下dans为array而非Set的问题
256
257
  */
257
258
  private dedupe() {
258
- const map = new Map()
259
- this.dans.forEach((d) => map.set(d.DMID || d.toDMID(), d))
260
- this.dans = [...map.values()]
259
+ if (this.options.dmid !== false) {
260
+ const map = new Map()
261
+ this.dans.forEach((d) => map.set(d.DMID || d.toDMID(), d))
262
+ this.dans = [...map.values()]
263
+ }
261
264
  this.options.dedupe = false
262
265
  }
263
266
  /**
@@ -395,18 +398,22 @@ export class UniPool {
395
398
  }
396
399
  }
397
400
  }
398
- static fromPb(bin: Uint8Array | ArrayBuffer) {
401
+ static fromPb(bin: Uint8Array | ArrayBuffer, options?: Options) {
399
402
  const data = fromBinary(DanmakuReplySchema, new Uint8Array(bin))
400
403
  return new UniPool(
401
404
  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
- }),
405
+ UniDM.create(
406
+ {
407
+ ...d,
408
+ mode: d.mode as number,
409
+ ctime: timestampDate(d.ctime || timestampNow()),
410
+ pool: d.pool as number,
411
+ attr: d.attr as UniDMTools.DMAttr[],
412
+ },
413
+ options,
414
+ ),
409
415
  ),
416
+ options,
410
417
  )
411
418
  }
412
419
  /**
@@ -437,7 +444,7 @@ export class UniPool {
437
444
  }),
438
445
  )
439
446
  }
440
- static fromBiliXML(xml: string) {
447
+ static fromBiliXML(xml: string, options?: Options) {
441
448
  const parser = new XMLParser({ ignoreAttributes: false }),
442
449
  oriData: DM_XML_Bili = parser.parse(xml),
443
450
  dans = oriData.i.d
@@ -460,9 +467,11 @@ export class UniPool {
460
467
  weight: Number.parseInt(p_arr[8]),
461
468
  },
462
469
  BigInt(oriData.i.chatid),
470
+ options,
463
471
  )
464
472
  })
465
473
  .filter((d) => d !== null),
474
+ options,
466
475
  )
467
476
  }
468
477
  toBiliXML(): string {
@@ -496,31 +505,38 @@ export class UniPool {
496
505
  },
497
506
  })
498
507
  }
499
- static fromBiliGrpc(bin: Uint8Array | ArrayBuffer) {
508
+ static fromBiliGrpc(bin: Uint8Array | ArrayBuffer, options?: Options) {
500
509
  const data = fromBinary(DmSegMobileReplySchema, new Uint8Array(bin)),
501
510
  json = data.elems
502
511
  return new UniPool(
503
512
  json.map((d) => {
504
- return UniDM.fromBili({ ...d, progress: d.progress / 1000 })
513
+ return UniDM.fromBili(
514
+ { ...d, progress: d.progress / 1000 },
515
+ d.oid,
516
+ options,
517
+ )
505
518
  }),
519
+ options,
506
520
  )
507
521
  }
508
522
  /**
509
523
  * @param bin 符合`DmWebViewReplySchema`(bili视频meta)的protobuf二进制
510
524
  */
511
- static fromBiliCommandGrpc(bin: Uint8Array | ArrayBuffer) {
525
+ static fromBiliCommandGrpc(bin: Uint8Array | ArrayBuffer, options?: Options) {
512
526
  const data = fromBinary(DmWebViewReplySchema, new Uint8Array(bin)),
513
527
  json = data.commandDms
514
528
  return new UniPool(
515
529
  json.map((d) => {
516
- return UniDM.fromBiliCommand(d)
530
+ return UniDM.fromBiliCommand(d, d.oid, options)
517
531
  }),
532
+ options,
518
533
  )
519
534
  }
520
535
  static fromDplayer(
521
536
  json: DM_JSON_Dplayer,
522
537
  playerID: string,
523
538
  domain = 'other',
539
+ options?: Options,
524
540
  ) {
525
541
  return new UniPool(
526
542
  json.data.map((d) => {
@@ -537,8 +553,10 @@ export class UniPool {
537
553
  },
538
554
  playerID,
539
555
  domain,
556
+ options,
540
557
  )
541
558
  }),
559
+ options,
542
560
  )
543
561
  }
544
562
  toDplayer(): DM_JSON_Dplayer {
@@ -554,6 +572,7 @@ export class UniPool {
554
572
  json: DM_JSON_Artplayer[],
555
573
  playerID: string,
556
574
  domain = 'other',
575
+ options?: Options,
557
576
  ) {
558
577
  return new UniPool(
559
578
  json.map((d) => {
@@ -570,8 +589,10 @@ export class UniPool {
570
589
  },
571
590
  playerID,
572
591
  domain,
592
+ options,
573
593
  )
574
594
  }),
595
+ options,
575
596
  )
576
597
  }
577
598
  toArtplayer(): DM_JSON_Artplayer[] {
@@ -587,7 +608,11 @@ export class UniPool {
587
608
  }
588
609
  })
589
610
  }
590
- static fromDDPlay(json: DM_JSON_DDPlay, episodeId: string) {
611
+ static fromDDPlay(
612
+ json: DM_JSON_DDPlay,
613
+ episodeId: string,
614
+ options?: Options,
615
+ ) {
591
616
  return new UniPool(
592
617
  json.comments.map((d) => {
593
618
  const p_arr = d.p.split(',')
@@ -601,8 +626,11 @@ export class UniPool {
601
626
  uid: p_arr[3],
602
627
  },
603
628
  episodeId,
629
+ undefined, //使用默认
630
+ options,
604
631
  )
605
632
  }),
633
+ options,
606
634
  )
607
635
  }
608
636
  toDDplay(): DM_JSON_DDPlay {
@@ -17,7 +17,7 @@ import JSONbig from 'json-bigint'
17
17
  import type { DM_JSON_BiliCommandGrpc } from '..'
18
18
  import type { PlatformDanmakuSource } from './platform'
19
19
 
20
- import { createDMID, UniID as ID } from './id-gen'
20
+ import { createDMID, DMIDGenerator, UniID as ID } from './id-gen'
21
21
  import {
22
22
  PlatformDanmakuOnlySource,
23
23
  PlatformDanmakuSources,
@@ -71,7 +71,7 @@ const toBits = (number: number) => {
71
71
  // bits.unshift(number & 1) // (0|1)[]
72
72
  number >>= 1
73
73
  } while (number)
74
- return bits.reverse()
74
+ return bits.toReversed()
75
75
  }
76
76
 
77
77
  export enum DMAttr {
@@ -334,6 +334,10 @@ export interface UniDMObj {
334
334
  DMID: string
335
335
  }
336
336
 
337
+ interface Options {
338
+ dmid?: boolean | number | DMIDGenerator
339
+ }
340
+
337
341
  export class UniDM {
338
342
  /**
339
343
  * 资源ID
@@ -456,9 +460,15 @@ export class UniDM {
456
460
  @IsOptional()
457
461
  @Expose()
458
462
  public DMID?: string
463
+ private options: Options = { dmid: createDMID }
459
464
  @Expose()
460
- init() {
465
+ init(options?: Options) {
466
+ this.options = options || this.options
461
467
  const def = new UniDM()
468
+
469
+ if (this.options.dmid === undefined || this.options.dmid === true)
470
+ this.options.dmid = createDMID
471
+
462
472
  if (!this.SOID) this.SOID = def.SOID
463
473
  if (!this.progress) this.progress = def.progress
464
474
  if (!this.mode) this.mode = def.mode
@@ -471,7 +481,7 @@ export class UniDM {
471
481
  if (!this.pool) this.pool = def.pool
472
482
  if (!this.attr) this.attr = def.attr
473
483
 
474
- if (!this.DMID) this.DMID = this.toDMID()
484
+ if (!this.DMID && this.options.dmid !== false) this.DMID = this.toDMID()
475
485
  this.progress = Number.parseFloat(this.progress.toFixed(3))
476
486
  if (this.extraStr)
477
487
  this.extraStr = JSON.stringify(
@@ -498,7 +508,7 @@ export class UniDM {
498
508
  async validate() {
499
509
  return validateOrReject(this)
500
510
  }
501
- static create(pjson?: Partial<UniDMObj>) {
511
+ static create(pjson?: Partial<UniDMObj>, options?: Options) {
502
512
  return pjson
503
513
  ? plainToInstance(
504
514
  UniDM,
@@ -511,7 +521,7 @@ export class UniDM {
511
521
  }
512
522
  : pjson,
513
523
  { excludeExtraneousValues: true },
514
- ).init()
524
+ ).init(options)
515
525
  : new UniDM()
516
526
  }
517
527
  @Expose()
@@ -537,7 +547,23 @@ export class UniDM {
537
547
  */
538
548
  @Expose()
539
549
  toDMID() {
540
- return createDMID(this.content, this.senderID, this.ctime, this.extraStr)
550
+ if (this.options.dmid === false) return
551
+ else if (this.options.dmid === true)
552
+ return createDMID(this.content, this.senderID, this.ctime, this.extraStr)
553
+ else if (typeof this.options.dmid === 'number')
554
+ return createDMID(
555
+ this.content,
556
+ this.senderID,
557
+ this.ctime,
558
+ this.extraStr,
559
+ this.options.dmid,
560
+ )
561
+ return this.options.dmid!(
562
+ this.content,
563
+ this.senderID,
564
+ this.ctime,
565
+ this.extraStr,
566
+ )
541
567
  }
542
568
  @Expose()
543
569
  isSameAs(dan: UniDM, options?: { skipDanuniMerge?: boolean }): boolean {
@@ -717,7 +743,7 @@ export class UniDM {
717
743
  }
718
744
  return mode
719
745
  }
720
- static fromBili(args: DMBili, cid?: bigint) {
746
+ static fromBili(args: DMBili, cid?: bigint, options?: Options) {
721
747
  interface TExtra extends Extra {
722
748
  bili: ExtraBili
723
749
  }
@@ -757,25 +783,28 @@ export class UniDM {
757
783
  mode = Modes.Normal
758
784
  break
759
785
  }
760
- return this.create({
761
- ...args,
762
- SOID: SOID.toString(),
763
- // progress: args.progress,
764
- mode,
765
- // fontsize: args.fontsize,
766
- // color: args.color,
767
- senderID: senderID.toString(),
768
- // content: args.content,
769
- ctime: this.transCtime(args.ctime, 's'),
770
- weight: args.weight ? args.weight : pool === Pools.Ix ? 1 : 0,
771
- pool,
772
- attr: DMAttrUtils.fromBin(args.attr, PlatformVideoSource.Bilibili),
773
- platform: PlatformVideoSource.Bilibili,
774
- // 需改进,7=>advanced 8=>code 9=>bas 互动=>command
775
- // 同时塞进无法/无需直接解析的数据
776
- // 另开一个解析器,为大部分播放器(无法解析该类dm)做文本类型降级处理
777
- extra,
778
- })
786
+ return this.create(
787
+ {
788
+ ...args,
789
+ SOID,
790
+ // progress: args.progress,
791
+ mode,
792
+ // fontsize: args.fontsize,
793
+ // color: args.color,
794
+ senderID: senderID.toString(),
795
+ // content: args.content,
796
+ ctime: this.transCtime(args.ctime, 's'),
797
+ weight: args.weight ? args.weight : pool === Pools.Ix ? 1 : 0,
798
+ pool,
799
+ attr: DMAttrUtils.fromBin(args.attr, PlatformVideoSource.Bilibili),
800
+ platform: PlatformVideoSource.Bilibili,
801
+ // 需改进,7=>advanced 8=>code 9=>bas 互动=>command
802
+ // 同时塞进无法/无需直接解析的数据
803
+ // 另开一个解析器,为大部分播放器(无法解析该类dm)做文本类型降级处理
804
+ extra,
805
+ },
806
+ options,
807
+ )
779
808
  }
780
809
  @Expose()
781
810
  toBiliXML(options?: { skipBiliCommand: boolean }) {
@@ -833,45 +862,56 @@ export class UniDM {
833
862
  ].join(','),
834
863
  }
835
864
  }
836
- static fromBiliCommand(args: DMBiliCommand, cid?: bigint) {
865
+ static fromBiliCommand(args: DMBiliCommand, cid?: bigint, options?: Options) {
837
866
  if (args.oid && !cid) cid = args.oid
838
- const SOID = ID.fromBili({ cid }),
867
+ const SOID = `def_${PlatformVideoSource.Bilibili}+${ID.fromBili({ cid })}`,
839
868
  senderID = ID.fromBili({ mid: args.mid })
840
- return this.create({
841
- ...args,
842
- SOID: SOID.toString(),
843
- // progress: args.progress,
844
- mode: Modes.Ext,
845
- // fontsize: args.fontsize,
846
- // color: args.color,
847
- senderID: senderID.toString(),
848
- // content: args.content,
849
- ctime: new Date(`${args.ctime} GMT+0800`), // 无视本地时区,按照B站的东8区计算时间
850
- weight: 10,
851
- pool: Pools.Adv,
852
- attr: [DMAttr.Protect],
853
- platform: PlatformVideoSource.Bilibili,
854
- extra: {
855
- bili: {
856
- command: args,
869
+ return this.create(
870
+ {
871
+ ...args,
872
+ SOID,
873
+ // progress: args.progress,
874
+ mode: Modes.Ext,
875
+ // fontsize: args.fontsize,
876
+ // color: args.color,
877
+ senderID: senderID.toString(),
878
+ // content: args.content,
879
+ ctime: new Date(`${args.ctime} GMT+0800`), // 无视本地时区,按照B站的东8区计算时间
880
+ weight: 10,
881
+ pool: Pools.Adv,
882
+ attr: [DMAttr.Protect],
883
+ platform: PlatformVideoSource.Bilibili,
884
+ extra: {
885
+ bili: {
886
+ command: args,
887
+ },
857
888
  },
858
889
  },
859
- })
890
+ options,
891
+ )
860
892
  }
861
- static fromDplayer(args: DMDplayer, playerID: string, domain: string) {
893
+ static fromDplayer(
894
+ args: DMDplayer,
895
+ playerID: string,
896
+ domain: string,
897
+ options?: Options,
898
+ ) {
862
899
  const SOID = ID.fromUnknown(playerID, domain),
863
900
  senderID = ID.fromUnknown(args.midHash, domain)
864
- return this.create({
865
- ...args,
866
- SOID: SOID.toString(),
867
- // progress: args.progress,
868
- mode: this.transMode(args.mode, 'dplayer'),
869
- // fontsize: 25,
870
- // color: args.color,
871
- senderID: senderID.toString(),
872
- // content: args.content,
873
- platform: domain,
874
- })
901
+ return this.create(
902
+ {
903
+ ...args,
904
+ SOID: SOID.toString(),
905
+ // progress: args.progress,
906
+ mode: this.transMode(args.mode, 'dplayer'),
907
+ // fontsize: 25,
908
+ // color: args.color,
909
+ senderID: senderID.toString(),
910
+ // content: args.content,
911
+ platform: domain,
912
+ },
913
+ options,
914
+ )
875
915
  }
876
916
  @Expose()
877
917
  toDplayer(): DMDplayer {
@@ -886,7 +926,12 @@ export class UniDM {
886
926
  content: this.content,
887
927
  }
888
928
  }
889
- static fromArtplayer(args: DMArtplayer, playerID: string, domain: string) {
929
+ static fromArtplayer(
930
+ args: DMArtplayer,
931
+ playerID: string,
932
+ domain: string,
933
+ options?: Options,
934
+ ) {
890
935
  const SOID = ID.fromUnknown(playerID, domain),
891
936
  senderID = ID.fromUnknown('', domain)
892
937
  let extra = args.border
@@ -900,18 +945,21 @@ export class UniDM {
900
945
  }
901
946
  else extra = { artplayer: { style: args.style } }
902
947
  }
903
- return this.create({
904
- ...args,
905
- SOID: SOID.toString(),
906
- // progress: args.progress,
907
- mode: this.transMode(args.mode, 'artplayer'),
908
- // fontsize: 25,
909
- // color: args.color,
910
- senderID: senderID.toString(),
911
- // content: args.content,
912
- platform: domain,
913
- extra, //optional BigINt parser
914
- })
948
+ return this.create(
949
+ {
950
+ ...args,
951
+ SOID: SOID.toString(),
952
+ // progress: args.progress,
953
+ mode: this.transMode(args.mode, 'artplayer'),
954
+ // fontsize: 25,
955
+ // color: args.color,
956
+ senderID: senderID.toString(),
957
+ // content: args.content,
958
+ platform: domain,
959
+ extra, //optional BigINt parser
960
+ },
961
+ options,
962
+ )
915
963
  }
916
964
  @Expose()
917
965
  toArtplayer(): DMArtplayer {
@@ -930,23 +978,27 @@ export class UniDM {
930
978
  args: DMDDplay,
931
979
  episodeId: string,
932
980
  domain = PlatformDanmakuOnlySource.DanDanPlay,
981
+ options?: Options,
933
982
  ) {
934
983
  const SOID = ID.fromUnknown(
935
984
  `def_${PlatformDanmakuOnlySource.DanDanPlay}+${episodeId}`,
936
985
  domain,
937
986
  )
938
- return this.create({
939
- ...args,
940
- SOID: SOID.toString(),
941
- // progress: args.progress,
942
- mode: this.transMode(args.mode, 'ddplay'),
943
- // fontsize: 25,
944
- // color: args.color,
945
- senderID: args.uid,
946
- content: args.m,
947
- platform: domain,
948
- DMID: args.cid.toString(), //无需 new ID() 获取带suffix的ID
949
- })
987
+ return this.create(
988
+ {
989
+ ...args,
990
+ SOID: SOID.toString(),
991
+ // progress: args.progress,
992
+ mode: this.transMode(args.mode, 'ddplay'),
993
+ // fontsize: 25,
994
+ // color: args.color,
995
+ senderID: args.uid,
996
+ content: args.m,
997
+ platform: domain,
998
+ DMID: args.cid.toString(), //无需 new ID() 获取带suffix的ID
999
+ },
1000
+ options,
1001
+ )
950
1002
  }
951
1003
  @Expose()
952
1004
  toDDplay(): DMDDplay {
@@ -62,10 +62,18 @@ export class UniID {
62
62
  }
63
63
  }
64
64
 
65
+ export type DMIDGenerator = (
66
+ content?: string,
67
+ senderID?: string,
68
+ ctime?: ctime,
69
+ extraStr?: string,
70
+ slice?: number,
71
+ ) => string
72
+
65
73
  export function createDMID(
66
74
  content: string = '',
67
- senderID: string,
68
- ctime: ctime,
75
+ senderID: string = UniID.fromNull().toString(),
76
+ ctime: ctime = new Date().toISOString(),
69
77
  extraStr?: string,
70
78
  slice = 8,
71
79
  ) {
package/tsconfig.json CHANGED
@@ -9,7 +9,7 @@
9
9
  // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
10
10
  // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
11
11
  /* Language and Environment */
12
- "target": "ES2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
12
+ "target": "ES2023" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
13
13
  "emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */,
14
14
  // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
15
15
  // "jsx": "preserve", /* Specify what JSX code is generated. */
@@ -1 +1 @@
1
- {"version":"5.8.3"}
1
+ {"version":"5.9.2"}