@dan-uni/dan-any 0.6.8 → 0.7.3

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
  * 合并弹幕/弹幕库
@@ -395,18 +396,22 @@ export class UniPool {
395
396
  }
396
397
  }
397
398
  }
398
- static fromPb(bin: Uint8Array | ArrayBuffer) {
399
+ static fromPb(bin: Uint8Array | ArrayBuffer, options?: Options) {
399
400
  const data = fromBinary(DanmakuReplySchema, new Uint8Array(bin))
400
401
  return new UniPool(
401
402
  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
- }),
403
+ UniDM.create(
404
+ {
405
+ ...d,
406
+ mode: d.mode as number,
407
+ ctime: timestampDate(d.ctime || timestampNow()),
408
+ pool: d.pool as number,
409
+ attr: d.attr as UniDMTools.DMAttr[],
410
+ },
411
+ options,
412
+ ),
409
413
  ),
414
+ options,
410
415
  )
411
416
  }
412
417
  /**
@@ -437,7 +442,7 @@ export class UniPool {
437
442
  }),
438
443
  )
439
444
  }
440
- static fromBiliXML(xml: string) {
445
+ static fromBiliXML(xml: string, options?: Options) {
441
446
  const parser = new XMLParser({ ignoreAttributes: false }),
442
447
  oriData: DM_XML_Bili = parser.parse(xml),
443
448
  dans = oriData.i.d
@@ -460,9 +465,11 @@ export class UniPool {
460
465
  weight: Number.parseInt(p_arr[8]),
461
466
  },
462
467
  BigInt(oriData.i.chatid),
468
+ options,
463
469
  )
464
470
  })
465
471
  .filter((d) => d !== null),
472
+ options,
466
473
  )
467
474
  }
468
475
  toBiliXML(): string {
@@ -496,31 +503,38 @@ export class UniPool {
496
503
  },
497
504
  })
498
505
  }
499
- static fromBiliGrpc(bin: Uint8Array | ArrayBuffer) {
506
+ static fromBiliGrpc(bin: Uint8Array | ArrayBuffer, options?: Options) {
500
507
  const data = fromBinary(DmSegMobileReplySchema, new Uint8Array(bin)),
501
508
  json = data.elems
502
509
  return new UniPool(
503
510
  json.map((d) => {
504
- return UniDM.fromBili({ ...d, progress: d.progress / 1000 })
511
+ return UniDM.fromBili(
512
+ { ...d, progress: d.progress / 1000 },
513
+ d.oid,
514
+ options,
515
+ )
505
516
  }),
517
+ options,
506
518
  )
507
519
  }
508
520
  /**
509
521
  * @param bin 符合`DmWebViewReplySchema`(bili视频meta)的protobuf二进制
510
522
  */
511
- static fromBiliCommandGrpc(bin: Uint8Array | ArrayBuffer) {
523
+ static fromBiliCommandGrpc(bin: Uint8Array | ArrayBuffer, options?: Options) {
512
524
  const data = fromBinary(DmWebViewReplySchema, new Uint8Array(bin)),
513
525
  json = data.commandDms
514
526
  return new UniPool(
515
527
  json.map((d) => {
516
- return UniDM.fromBiliCommand(d)
528
+ return UniDM.fromBiliCommand(d, d.oid, options)
517
529
  }),
530
+ options,
518
531
  )
519
532
  }
520
533
  static fromDplayer(
521
534
  json: DM_JSON_Dplayer,
522
535
  playerID: string,
523
536
  domain = 'other',
537
+ options?: Options,
524
538
  ) {
525
539
  return new UniPool(
526
540
  json.data.map((d) => {
@@ -537,8 +551,10 @@ export class UniPool {
537
551
  },
538
552
  playerID,
539
553
  domain,
554
+ options,
540
555
  )
541
556
  }),
557
+ options,
542
558
  )
543
559
  }
544
560
  toDplayer(): DM_JSON_Dplayer {
@@ -554,6 +570,7 @@ export class UniPool {
554
570
  json: DM_JSON_Artplayer[],
555
571
  playerID: string,
556
572
  domain = 'other',
573
+ options?: Options,
557
574
  ) {
558
575
  return new UniPool(
559
576
  json.map((d) => {
@@ -570,8 +587,10 @@ export class UniPool {
570
587
  },
571
588
  playerID,
572
589
  domain,
590
+ options,
573
591
  )
574
592
  }),
593
+ options,
575
594
  )
576
595
  }
577
596
  toArtplayer(): DM_JSON_Artplayer[] {
@@ -587,7 +606,11 @@ export class UniPool {
587
606
  }
588
607
  })
589
608
  }
590
- static fromDDPlay(json: DM_JSON_DDPlay, episodeId: string) {
609
+ static fromDDPlay(
610
+ json: DM_JSON_DDPlay,
611
+ episodeId: string,
612
+ options?: Options,
613
+ ) {
591
614
  return new UniPool(
592
615
  json.comments.map((d) => {
593
616
  const p_arr = d.p.split(',')
@@ -601,8 +624,11 @@ export class UniPool {
601
624
  uid: p_arr[3],
602
625
  },
603
626
  episodeId,
627
+ undefined, //使用默认
628
+ options,
604
629
  )
605
630
  }),
631
+ options,
606
632
  )
607
633
  }
608
634
  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,20 +978,27 @@ export class UniDM {
930
978
  args: DMDDplay,
931
979
  episodeId: string,
932
980
  domain = PlatformDanmakuOnlySource.DanDanPlay,
981
+ options?: Options,
933
982
  ) {
934
- const SOID = ID.fromUnknown(`def::${episodeId}`, domain)
935
- return this.create({
936
- ...args,
937
- SOID: SOID.toString(),
938
- // progress: args.progress,
939
- mode: this.transMode(args.mode, 'ddplay'),
940
- // fontsize: 25,
941
- // color: args.color,
942
- senderID: args.uid,
943
- content: args.m,
944
- platform: domain,
945
- DMID: args.cid.toString(), //无需 new ID() 获取带suffix的ID
946
- })
983
+ const SOID = ID.fromUnknown(
984
+ `def_${PlatformDanmakuOnlySource.DanDanPlay}+${episodeId}`,
985
+ domain,
986
+ )
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
+ )
947
1002
  }
948
1003
  @Expose()
949
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"}