@dan-uni/dan-any 1.2.7 → 1.3.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.
Files changed (46) hide show
  1. package/README.md +1 -0
  2. package/buf.gen.yaml +4 -2
  3. package/buf.yaml +12 -0
  4. package/dist/browser/index.min.js +13384 -1
  5. package/dist/browser/plugins/bili.min.js +77 -1
  6. package/dist/browser/plugins/index.min.js +2 -2
  7. package/dist/browser/plugins/stats.min.js +10 -1
  8. package/dist/browser/rslib-runtime.min.js +5 -5
  9. package/dist/browser/src/index.d.ts +10 -7
  10. package/dist/browser/src/proto/gen/{bili → bilibili/community/service/dm/v1}/dm_pb.d.ts +2 -2
  11. package/dist/browser/src/proto/gen/{danuni_pb.d.ts → danuni/danmaku/v1/danmaku_pb.d.ts} +45 -41
  12. package/dist/browser/src/utils/fileParser.d.ts +3 -0
  13. package/dist/node/index.js +1959 -1
  14. package/dist/node/plugins/bili.js +77 -1
  15. package/dist/node/plugins/index.js +2 -2
  16. package/dist/node/plugins/stats.js +10 -1
  17. package/dist/node/rslib-runtime.js +5 -5
  18. package/dist/node/src/index.d.ts +10 -7
  19. package/dist/node/src/proto/gen/{bili → bilibili/community/service/dm/v1}/dm_pb.d.ts +2 -2
  20. package/dist/{umd/src/proto/gen/danuni_pb.d.ts → node/src/proto/gen/danuni/danmaku/v1/danmaku_pb.d.ts} +45 -41
  21. package/dist/node/src/utils/fileParser.d.ts +3 -0
  22. package/dist/umd/index.umd.min.js +1146 -336
  23. package/dist/umd/plugins/bili.umd.min.js +1143 -334
  24. package/dist/umd/plugins/index.umd.min.js +1143 -334
  25. package/dist/umd/src/index.d.ts +10 -7
  26. package/dist/umd/src/proto/gen/{bili → bilibili/community/service/dm/v1}/dm_pb.d.ts +2 -2
  27. package/dist/{node/src/proto/gen/danuni_pb.d.ts → umd/src/proto/gen/danuni/danmaku/v1/danmaku_pb.d.ts} +45 -41
  28. package/dist/umd/src/utils/fileParser.d.ts +3 -0
  29. package/package.json +5 -7
  30. package/src/index.test.ts +23 -1
  31. package/src/index.ts +153 -59
  32. package/src/proto/gen/{bili → bilibili/community/service/dm/v1}/dm_pb.ts +104 -104
  33. package/src/proto/gen/danuni/danmaku/v1/danmaku_pb.ts +223 -0
  34. package/src/proto/src/danuni/danmaku/v1/danmaku.proto +52 -0
  35. package/src/utils/dm-gen.ts +1 -1
  36. package/src/utils/fileParser.ts +37 -0
  37. package/dist/browser/17.min.js +0 -13299
  38. package/dist/browser/705.min.js +0 -77
  39. package/dist/browser/898.min.js +0 -10
  40. package/dist/node/17.js +0 -1874
  41. package/dist/node/705.js +0 -77
  42. package/dist/node/898.js +0 -10
  43. package/src/proto/gen/danuni_pb.ts +0 -219
  44. package/src/proto/src/danuni.proto +0 -50
  45. /package/dist/browser/{17.min.js.LICENSE.txt → index.min.js.LICENSE.txt} +0 -0
  46. /package/src/proto/src/{bili → bilibili/community/service/dm/v1}/dm.proto +0 -0
package/src/index.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import 'reflect-metadata/lite'
2
2
 
3
- import { isJSON, isObject, isString } from 'class-validator'
3
+ import { isJSON, isString } from 'class-validator'
4
4
  import { XMLBuilder, XMLParser } from 'fast-xml-parser'
5
5
  import JSONbig from 'json-bigint'
6
6
  import type { Options as AssGenOptions, CanvasCtx } from './ass-gen'
7
- import type { CommandDm as DM_JSON_BiliCommandGrpc } from './proto/gen/bili/dm_pb'
7
+ import type { CommandDm as DM_JSON_BiliCommandGrpc } from './proto/gen/bilibili/community/service/dm/v1/dm_pb'
8
+ import type { Danmaku } from './proto/gen/danuni/danmaku/v1/danmaku_pb'
8
9
 
9
10
  import { create, fromBinary, toBinary } from '@bufbuild/protobuf'
10
11
  import {
@@ -19,12 +20,13 @@ import {
19
20
  // DanmakuElem as DM_JSON_BiliGrpc,
20
21
  DmSegMobileReplySchema,
21
22
  DmWebViewReplySchema,
22
- } from './proto/gen/bili/dm_pb'
23
- import { DanmakuReplySchema } from './proto/gen/danuni_pb'
23
+ } from './proto/gen/bilibili/community/service/dm/v1/dm_pb'
24
+ import { ListDanResponseSchema } from './proto/gen/danuni/danmaku/v1/danmaku_pb'
24
25
  // import type * as UniIDType from './utils/id-gen'
25
26
 
26
27
  import { UniDM } from './utils/dm-gen'
27
28
  import * as UniDMTools from './utils/dm-gen'
29
+ import { fileParser } from './utils/fileParser'
28
30
  import { UniID as ID } from './utils/id-gen'
29
31
  import * as UniIDTools from './utils/id-gen'
30
32
  import * as platform from './utils/platform'
@@ -44,6 +46,8 @@ interface DanUniConvertTip {
44
46
  data?: string
45
47
  }
46
48
 
49
+ export type DM_JSON_DanuniMin = Partial<UniDMTools.UniDMObj>[]
50
+
47
51
  export interface DM_XML_Bili {
48
52
  i: {
49
53
  chatserver: string
@@ -153,10 +157,11 @@ export interface DM_JSON_DDPlay {
153
157
 
154
158
  export enum DM_format {
155
159
  DanuniJson = 'danuni.json',
156
- DanuniPbBin = 'danuni.pb.bin',
160
+ DanuniMinJson = 'danuni.min.json',
161
+ DanuniPbBin = 'danuni.binpb',
157
162
  BiliXml = 'bili.xml',
158
- BiliPbBin = 'bili.pb.bin',
159
- BiliCmdPbBin = 'bili.cmd.pb.bin',
163
+ BiliPbBin = 'bili.binpb',
164
+ BiliCmdPbBin = 'bili.cmd.binpb',
160
165
  BiliUpJson = 'bili.up.json',
161
166
  DplayerJson = 'dplayer.json',
162
167
  ArtplayerJson = 'artplayer.json',
@@ -447,10 +452,120 @@ export class UniPool {
447
452
  file: unknown,
448
453
  options?: Options,
449
454
  /**
450
- * 加载指定解析模块,为空则全选
455
+ * 加载指定解析模块,为空则全选,为字符串则视为文件名解析加载模块
451
456
  */
452
- mod?: ('json' | 'str' | 'bin')[],
457
+ mod?: string | ('json' | 'str' | 'bin')[],
453
458
  ): { pool: UniPool; fmt: DM_format } {
459
+ const handlers = {
460
+ [DM_format.DanuniJson]: (json: UniDM[]) => ({
461
+ pool: new UniPool(json, options),
462
+ fmt: DM_format.DanuniJson,
463
+ }),
464
+ [DM_format.DanuniMinJson]: (json: DM_JSON_DanuniMin) => ({
465
+ pool: this.fromMin(json, options),
466
+ fmt: DM_format.DanuniMinJson,
467
+ }),
468
+ [DM_format.DanuniPbBin]: (
469
+ file: ArrayBuffer | Uint8Array<ArrayBufferLike>,
470
+ ) => ({
471
+ pool: this.fromPb(file),
472
+ fmt: DM_format.DanuniPbBin,
473
+ }),
474
+ [DM_format.BiliXml]: (file: string) => ({
475
+ pool: this.fromBiliXML(file, options),
476
+ fmt: DM_format.BiliXml,
477
+ }),
478
+ [DM_format.BiliPbBin]: (
479
+ file: ArrayBuffer | Uint8Array<ArrayBufferLike>,
480
+ ) => ({
481
+ pool: this.fromBiliGrpc(file),
482
+ fmt: DM_format.BiliPbBin,
483
+ }),
484
+ [DM_format.BiliCmdPbBin]: (
485
+ file: ArrayBuffer | Uint8Array<ArrayBufferLike>,
486
+ ) => ({
487
+ pool: this.fromBiliCommandGrpc(file),
488
+ fmt: DM_format.BiliCmdPbBin,
489
+ }),
490
+ [DM_format.BiliUpJson]: (json: DM_JSON_BiliUp) => ({
491
+ pool: this.fromBiliUp(json, options),
492
+ fmt: DM_format.BiliUpJson,
493
+ }),
494
+ [DM_format.DplayerJson]: (
495
+ json: DM_JSON_Dplayer & {
496
+ danuni?: DanUniConvertTip
497
+ },
498
+ ) => ({
499
+ pool: this.fromDplayer(
500
+ json,
501
+ json.danuni?.data ?? '',
502
+ undefined,
503
+ options,
504
+ ),
505
+ fmt: DM_format.DplayerJson,
506
+ }),
507
+ [DM_format.ArtplayerJson]: (
508
+ json: DM_JSON_Artplayer & {
509
+ danuni?: DanUniConvertTip
510
+ },
511
+ ) => ({
512
+ pool: this.fromArtplayer(
513
+ json,
514
+ json.danuni?.data ?? '',
515
+ undefined,
516
+ options,
517
+ ),
518
+ fmt: DM_format.ArtplayerJson,
519
+ }),
520
+ [DM_format.DdplayJson]: (
521
+ json: DM_JSON_DDPlay & {
522
+ danuni?: DanUniConvertTip
523
+ },
524
+ ) => ({
525
+ pool: this.fromDDPlay(json, json.danuni?.data ?? '', options),
526
+ fmt: DM_format.DdplayJson,
527
+ }),
528
+ [DM_format.CommonAss]: (file: string) => ({
529
+ pool: this.fromASS(file, options),
530
+ fmt: DM_format.CommonAss,
531
+ }),
532
+ }
533
+
534
+ if (typeof mod === 'string') {
535
+ const fn = mod
536
+ // 直接按照默认模式处理匹配后缀的文件
537
+ try {
538
+ if (fn.endsWith(DM_format.DanuniJson)) {
539
+ return handlers[DM_format.DanuniJson](fileParser(file, 'json'))
540
+ } else if (fn.endsWith(DM_format.DanuniMinJson))
541
+ return handlers[DM_format.DanuniMinJson](fileParser(file, 'json'))
542
+ else if (fn.endsWith(DM_format.DanuniPbBin))
543
+ return handlers[DM_format.DanuniPbBin](fileParser(file, 'bin'))
544
+ else if (fn.endsWith(DM_format.BiliXml))
545
+ return handlers[DM_format.BiliXml](fileParser(file, 'string'))
546
+ else if (fn.endsWith(DM_format.BiliPbBin))
547
+ return handlers[DM_format.BiliPbBin](fileParser(file, 'bin'))
548
+ else if (fn.endsWith(DM_format.BiliCmdPbBin))
549
+ return handlers[DM_format.BiliCmdPbBin](fileParser(file, 'bin'))
550
+ else if (fn.endsWith(DM_format.BiliUpJson))
551
+ return handlers[DM_format.BiliUpJson](fileParser(file, 'json'))
552
+ else if (fn.endsWith(DM_format.DplayerJson))
553
+ return handlers[DM_format.DplayerJson](fileParser(file, 'json'))
554
+ else if (fn.endsWith(DM_format.ArtplayerJson))
555
+ return handlers[DM_format.ArtplayerJson](fileParser(file, 'json'))
556
+ else if (fn.endsWith(DM_format.DdplayJson))
557
+ return handlers[DM_format.DdplayJson](fileParser(file, 'json'))
558
+ else if (fn.endsWith(DM_format.CommonAss))
559
+ return handlers[DM_format.CommonAss](fileParser(file, 'string'))
560
+ } catch {}
561
+ // 按照后缀设定启用模块
562
+ const ext = fn.split('.').pop()?.toLowerCase()
563
+ if (ext) {
564
+ if (ext === 'json') mod = ['json']
565
+ else if (['xml', 'ass'].includes(ext)) mod = ['str']
566
+ else if (['binpb', 'bin', 'so'].includes(ext)) mod = ['bin']
567
+ }
568
+ }
454
569
  if (!mod) mod = ['json', 'str', 'bin']
455
570
  const err = '无法识别该文件,请手动指定格式!'
456
571
  const parseJSON = (
@@ -461,42 +576,23 @@ export class UniPool {
461
576
  ): { pool: UniPool; fmt: DM_format } | undefined => {
462
577
  try {
463
578
  if (Array.isArray(json) && json.every((d) => d.SOID)) {
464
- return { pool: new UniPool(json, options), fmt: DM_format.DanuniJson }
579
+ return handlers[DM_format.DanuniMinJson](json) // 使用兼容性参数,danuni.min.json解析器可以解析danuni.json
465
580
  } else if (json.danmuku && json.danmuku.every((d) => d.text)) {
466
- return {
467
- pool: this.fromArtplayer(
468
- json,
469
- json.danuni?.data ?? '',
470
- undefined,
471
- options,
472
- ),
473
- fmt: DM_format.ArtplayerJson,
474
- }
581
+ return handlers[DM_format.ArtplayerJson](json)
475
582
  } else if (
476
583
  json.count &&
477
584
  json.comments &&
478
585
  Array.isArray(json.comments) &&
479
586
  json.comments.every((d) => d.m)
480
587
  ) {
481
- return {
482
- pool: this.fromDDPlay(json, json.danuni?.data ?? '', options),
483
- fmt: DM_format.DdplayJson,
484
- }
588
+ return handlers[DM_format.DdplayJson](json)
485
589
  } else if (
486
590
  json.code == 0 &&
487
591
  json.data &&
488
592
  Array.isArray(json.data) &&
489
593
  json.data.every((d) => Array.isArray(d))
490
594
  ) {
491
- return {
492
- pool: this.fromDplayer(
493
- json,
494
- json.danuni?.data ?? '',
495
- undefined,
496
- options,
497
- ),
498
- fmt: DM_format.DplayerJson,
499
- }
595
+ return handlers[DM_format.DplayerJson](json)
500
596
  } else if (
501
597
  json.code == 0 &&
502
598
  json.message == '0' &&
@@ -506,10 +602,7 @@ export class UniPool {
506
602
  Array.isArray(json.data.result) &&
507
603
  json.data.result.every((d) => d.id && d.oid)
508
604
  ) {
509
- return {
510
- pool: this.fromBiliUp(json, options),
511
- fmt: DM_format.BiliUpJson,
512
- }
605
+ return handlers[DM_format.BiliUpJson](json)
513
606
  }
514
607
  } catch {}
515
608
  }
@@ -529,33 +622,26 @@ export class UniPool {
529
622
  try {
530
623
  const xmlParser = new XMLParser({ ignoreAttributes: false })
531
624
  const xml = xmlParser.parse(file)
532
- if (xml?.i?.d)
533
- return {
534
- pool: this.fromBiliXML(file, options),
535
- fmt: DM_format.BiliXml,
536
- }
625
+ if (xml?.i?.d) return handlers[DM_format.BiliXml](file)
537
626
  } catch {}
538
627
  try {
539
- return { pool: this.fromASS(file, options), fmt: DM_format.CommonAss }
628
+ return handlers[DM_format.CommonAss](file)
540
629
  } catch {}
541
630
  }
542
631
  }
543
632
  let errmesg
544
- if (isObject(file)) {
633
+ if (typeof file === 'object' || Array.isArray(file)) {
545
634
  if (file instanceof ArrayBuffer || file instanceof Uint8Array) {
546
635
  // pure-bin (pb)
547
636
  if (mod.includes('bin')) {
548
637
  try {
549
- return { pool: this.fromPb(file), fmt: DM_format.DanuniPbBin }
638
+ return handlers[DM_format.DanuniPbBin](file)
550
639
  } catch {}
551
640
  try {
552
- return { pool: this.fromBiliGrpc(file), fmt: DM_format.BiliPbBin }
641
+ return handlers[DM_format.BiliPbBin](file)
553
642
  } catch {}
554
643
  try {
555
- return {
556
- pool: this.fromBiliCommandGrpc(file),
557
- fmt: DM_format.BiliCmdPbBin,
558
- }
644
+ return handlers[DM_format.BiliCmdPbBin](file)
559
645
  } catch {}
560
646
  }
561
647
  // str-bin (pure-str + json-str)
@@ -587,13 +673,15 @@ export class UniPool {
587
673
  switch (format) {
588
674
  case 'danuni.json':
589
675
  return this.dans
590
- case 'danuni.pb.bin':
676
+ case 'danuni.min.json':
677
+ return this.minify()
678
+ case 'danuni.binpb':
591
679
  return this.toPb()
592
680
  case 'bili.xml':
593
681
  return this.toBiliXML()
594
- // case 'bili.bin':
682
+ // case 'bili.binpb':
595
683
  // return this.toBiliBin()
596
- // case 'bili.cmd.bin':
684
+ // case 'bili.cmd.binpb':
597
685
  // return this.toBiliCmdBin()
598
686
  case 'dplayer.json':
599
687
  return this.toDplayer()
@@ -610,15 +698,21 @@ export class UniPool {
610
698
  }
611
699
  }
612
700
  }
701
+ static fromMin(json: DM_JSON_DanuniMin, options?: Options) {
702
+ return new UniPool(json.map((d: any) => UniDM.create(d, options)))
703
+ }
613
704
  static fromPb(bin: Uint8Array | ArrayBuffer, options?: Options) {
614
- const data = fromBinary(DanmakuReplySchema, new Uint8Array(bin))
705
+ const data = fromBinary(ListDanResponseSchema, new Uint8Array(bin))
615
706
  return new UniPool(
616
707
  data.danmakus.map((d) =>
617
708
  UniDM.create(
618
709
  {
619
710
  ...d,
711
+ SOID: d.soid,
712
+ DMID: d.dmid,
620
713
  progress: d.progress / 1000,
621
714
  mode: d.mode as number,
715
+ senderID: d.senderId,
622
716
  ctime: timestampDate(d.ctime || timestampNow()),
623
717
  pool: d.pool as number,
624
718
  attr: d.attr as UniDMTools.DMAttr[],
@@ -636,17 +730,17 @@ export class UniPool {
636
730
  */
637
731
  toPb() {
638
732
  return toBinary(
639
- DanmakuReplySchema,
640
- create(DanmakuReplySchema, {
733
+ ListDanResponseSchema,
734
+ create(ListDanResponseSchema, {
641
735
  danmakus: this.dans.map((d) => {
642
736
  return {
643
- SOID: d.SOID,
644
- DMID: d.DMID,
737
+ soid: d.SOID,
738
+ dmid: d.DMID ?? '',
645
739
  progress: Math.round(d.progress * 1000),
646
740
  mode: d.mode as number,
647
741
  fontsize: d.fontsize,
648
742
  color: d.color,
649
- senderID: d.senderID,
743
+ senderId: d.senderID,
650
744
  content: d.content,
651
745
  ctime: timestampFromDate(d.ctime),
652
746
  weight: d.weight,
@@ -654,7 +748,7 @@ export class UniPool {
654
748
  attr: d.attr,
655
749
  platform: d.platform,
656
750
  extra: d.extraStr,
657
- }
751
+ } satisfies Omit<Danmaku, '$typeName'>
658
752
  }),
659
753
  }),
660
754
  )