@libp2p/utils 6.7.2-6059227cb → 6.7.2-87bc8d4fb

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 (159) hide show
  1. package/README.md +1 -1
  2. package/dist/index.min.js +1 -7
  3. package/dist/index.min.js.map +4 -4
  4. package/dist/src/abort-options.d.ts +7 -0
  5. package/dist/src/abort-options.d.ts.map +1 -0
  6. package/dist/src/abort-options.js +14 -0
  7. package/dist/src/abort-options.js.map +1 -0
  8. package/dist/src/abstract-stream.d.ts +130 -14
  9. package/dist/src/abstract-stream.d.ts.map +1 -1
  10. package/dist/src/abstract-stream.js +321 -39
  11. package/dist/src/abstract-stream.js.map +1 -1
  12. package/dist/src/array-equals.d.ts +24 -0
  13. package/dist/src/array-equals.d.ts.map +1 -0
  14. package/dist/src/array-equals.js +31 -0
  15. package/dist/src/array-equals.js.map +1 -0
  16. package/dist/src/close-source.d.ts +4 -0
  17. package/dist/src/close-source.d.ts.map +1 -0
  18. package/dist/src/close-source.js +11 -0
  19. package/dist/src/close-source.js.map +1 -0
  20. package/dist/src/close.d.ts +21 -0
  21. package/dist/src/close.d.ts.map +1 -0
  22. package/dist/src/close.js +49 -0
  23. package/dist/src/close.js.map +1 -0
  24. package/dist/src/errors.d.ts +0 -8
  25. package/dist/src/errors.d.ts.map +1 -1
  26. package/dist/src/errors.js +0 -8
  27. package/dist/src/errors.js.map +1 -1
  28. package/dist/src/get-thin-waist-addresses.browser.d.ts +1 -1
  29. package/dist/src/get-thin-waist-addresses.browser.d.ts.map +1 -1
  30. package/dist/src/get-thin-waist-addresses.browser.js +3 -4
  31. package/dist/src/get-thin-waist-addresses.browser.js.map +1 -1
  32. package/dist/src/get-thin-waist-addresses.d.ts +1 -1
  33. package/dist/src/get-thin-waist-addresses.d.ts.map +1 -1
  34. package/dist/src/get-thin-waist-addresses.js +9 -7
  35. package/dist/src/get-thin-waist-addresses.js.map +1 -1
  36. package/dist/src/index.d.ts +1 -31
  37. package/dist/src/index.d.ts.map +1 -1
  38. package/dist/src/index.js +1 -31
  39. package/dist/src/index.js.map +1 -1
  40. package/dist/src/merge-options.d.ts +7 -0
  41. package/dist/src/merge-options.d.ts.map +1 -0
  42. package/dist/src/merge-options.js +128 -0
  43. package/dist/src/merge-options.js.map +1 -0
  44. package/dist/src/multiaddr/is-global-unicast.d.ts.map +1 -1
  45. package/dist/src/multiaddr/is-global-unicast.js +9 -8
  46. package/dist/src/multiaddr/is-global-unicast.js.map +1 -1
  47. package/dist/src/multiaddr/is-ip-based.d.ts +6 -0
  48. package/dist/src/multiaddr/is-ip-based.d.ts.map +1 -0
  49. package/dist/src/multiaddr/is-ip-based.js +18 -0
  50. package/dist/src/multiaddr/is-ip-based.js.map +1 -0
  51. package/dist/src/multiaddr/is-link-local.d.ts.map +1 -1
  52. package/dist/src/multiaddr/is-link-local.js +16 -11
  53. package/dist/src/multiaddr/is-link-local.js.map +1 -1
  54. package/dist/src/multiaddr/is-loopback.d.ts.map +1 -1
  55. package/dist/src/multiaddr/is-loopback.js +5 -12
  56. package/dist/src/multiaddr/is-loopback.js.map +1 -1
  57. package/dist/src/multiaddr/is-network-address.d.ts.map +1 -1
  58. package/dist/src/multiaddr/is-network-address.js +16 -4
  59. package/dist/src/multiaddr/is-network-address.js.map +1 -1
  60. package/dist/src/multiaddr/is-private.d.ts.map +1 -1
  61. package/dist/src/multiaddr/is-private.js +10 -9
  62. package/dist/src/multiaddr/is-private.js.map +1 -1
  63. package/dist/src/queue/index.d.ts +6 -3
  64. package/dist/src/queue/index.d.ts.map +1 -1
  65. package/dist/src/queue/index.js +4 -20
  66. package/dist/src/queue/index.js.map +1 -1
  67. package/dist/src/rate-limiter.d.ts +15 -1
  68. package/dist/src/rate-limiter.d.ts.map +1 -1
  69. package/dist/src/rate-limiter.js +14 -1
  70. package/dist/src/rate-limiter.js.map +1 -1
  71. package/dist/src/stream-to-ma-conn.d.ts +23 -0
  72. package/dist/src/stream-to-ma-conn.d.ts.map +1 -0
  73. package/dist/src/stream-to-ma-conn.js +75 -0
  74. package/dist/src/stream-to-ma-conn.js.map +1 -0
  75. package/package.json +162 -15
  76. package/src/abort-options.ts +20 -0
  77. package/src/abstract-stream.ts +464 -51
  78. package/src/array-equals.ts +34 -0
  79. package/src/close-source.ts +14 -0
  80. package/src/close.ts +65 -0
  81. package/src/errors.ts +0 -10
  82. package/src/get-thin-waist-addresses.browser.ts +4 -5
  83. package/src/get-thin-waist-addresses.ts +12 -8
  84. package/src/index.ts +1 -31
  85. package/src/merge-options.ts +161 -0
  86. package/src/multiaddr/is-global-unicast.ts +11 -8
  87. package/src/multiaddr/is-ip-based.ts +21 -0
  88. package/src/multiaddr/is-link-local.ts +20 -11
  89. package/src/multiaddr/is-loopback.ts +7 -12
  90. package/src/multiaddr/is-network-address.ts +19 -4
  91. package/src/multiaddr/is-private.ts +14 -9
  92. package/src/queue/index.ts +11 -24
  93. package/src/rate-limiter.ts +30 -3
  94. package/src/stream-to-ma-conn.ts +106 -0
  95. package/dist/src/abstract-message-stream.d.ts +0 -129
  96. package/dist/src/abstract-message-stream.d.ts.map +0 -1
  97. package/dist/src/abstract-message-stream.js +0 -393
  98. package/dist/src/abstract-message-stream.js.map +0 -1
  99. package/dist/src/abstract-multiaddr-connection.d.ts +0 -26
  100. package/dist/src/abstract-multiaddr-connection.d.ts.map +0 -1
  101. package/dist/src/abstract-multiaddr-connection.js +0 -66
  102. package/dist/src/abstract-multiaddr-connection.js.map +0 -1
  103. package/dist/src/abstract-stream-muxer.d.ts +0 -53
  104. package/dist/src/abstract-stream-muxer.d.ts.map +0 -1
  105. package/dist/src/abstract-stream-muxer.js +0 -169
  106. package/dist/src/abstract-stream-muxer.js.map +0 -1
  107. package/dist/src/length-prefixed-decoder.d.ts +0 -37
  108. package/dist/src/length-prefixed-decoder.d.ts.map +0 -1
  109. package/dist/src/length-prefixed-decoder.js +0 -64
  110. package/dist/src/length-prefixed-decoder.js.map +0 -1
  111. package/dist/src/message-queue.d.ts +0 -61
  112. package/dist/src/message-queue.d.ts.map +0 -1
  113. package/dist/src/message-queue.js +0 -93
  114. package/dist/src/message-queue.js.map +0 -1
  115. package/dist/src/mock-muxer.d.ts +0 -57
  116. package/dist/src/mock-muxer.d.ts.map +0 -1
  117. package/dist/src/mock-muxer.js +0 -204
  118. package/dist/src/mock-muxer.js.map +0 -1
  119. package/dist/src/mock-stream.d.ts +0 -31
  120. package/dist/src/mock-stream.d.ts.map +0 -1
  121. package/dist/src/mock-stream.js +0 -69
  122. package/dist/src/mock-stream.js.map +0 -1
  123. package/dist/src/multiaddr/get-net-config.d.ts +0 -55
  124. package/dist/src/multiaddr/get-net-config.d.ts.map +0 -1
  125. package/dist/src/multiaddr/get-net-config.js +0 -54
  126. package/dist/src/multiaddr/get-net-config.js.map +0 -1
  127. package/dist/src/multiaddr/index.d.ts +0 -7
  128. package/dist/src/multiaddr/index.d.ts.map +0 -1
  129. package/dist/src/multiaddr/index.js +0 -7
  130. package/dist/src/multiaddr/index.js.map +0 -1
  131. package/dist/src/multiaddr/utils.d.ts +0 -5
  132. package/dist/src/multiaddr/utils.d.ts.map +0 -1
  133. package/dist/src/multiaddr/utils.js +0 -32
  134. package/dist/src/multiaddr/utils.js.map +0 -1
  135. package/dist/src/multiaddr-connection-pair.d.ts +0 -25
  136. package/dist/src/multiaddr-connection-pair.d.ts.map +0 -1
  137. package/dist/src/multiaddr-connection-pair.js +0 -103
  138. package/dist/src/multiaddr-connection-pair.js.map +0 -1
  139. package/dist/src/stream-pair.d.ts +0 -42
  140. package/dist/src/stream-pair.d.ts.map +0 -1
  141. package/dist/src/stream-pair.js +0 -40
  142. package/dist/src/stream-pair.js.map +0 -1
  143. package/dist/src/stream-utils.d.ts +0 -191
  144. package/dist/src/stream-utils.d.ts.map +0 -1
  145. package/dist/src/stream-utils.js +0 -370
  146. package/dist/src/stream-utils.js.map +0 -1
  147. package/src/abstract-message-stream.ts +0 -553
  148. package/src/abstract-multiaddr-connection.ts +0 -93
  149. package/src/abstract-stream-muxer.ts +0 -239
  150. package/src/length-prefixed-decoder.ts +0 -98
  151. package/src/message-queue.ts +0 -156
  152. package/src/mock-muxer.ts +0 -304
  153. package/src/mock-stream.ts +0 -101
  154. package/src/multiaddr/get-net-config.ts +0 -112
  155. package/src/multiaddr/index.ts +0 -6
  156. package/src/multiaddr/utils.ts +0 -46
  157. package/src/multiaddr-connection-pair.ts +0 -147
  158. package/src/stream-pair.ts +0 -90
  159. package/src/stream-utils.ts +0 -865
@@ -1,26 +0,0 @@
1
- import { AbstractMessageStream } from './abstract-message-stream.ts';
2
- import type { MessageStreamInit } from './abstract-message-stream.ts';
3
- import type { CounterGroup, Logger, MultiaddrConnection, MessageStreamDirection, AbortOptions } from '@libp2p/interface';
4
- import type { Multiaddr } from '@multiformats/multiaddr';
5
- export interface AbstractMultiaddrConnectionInit extends Omit<MessageStreamInit, 'log'> {
6
- remoteAddr: Multiaddr;
7
- direction: MessageStreamDirection;
8
- log: Logger;
9
- inactivityTimeout?: number;
10
- localAddr?: Multiaddr;
11
- metricPrefix?: string;
12
- metrics?: CounterGroup;
13
- }
14
- export declare abstract class AbstractMultiaddrConnection extends AbstractMessageStream implements MultiaddrConnection {
15
- remoteAddr: Multiaddr;
16
- private metricPrefix;
17
- private metrics?;
18
- constructor(init: AbstractMultiaddrConnectionInit);
19
- close(options?: AbortOptions): Promise<void>;
20
- /**
21
- * Wait for any unsent data to be written to the underlying resource, then
22
- * close the resource and resolve the returned promise
23
- */
24
- abstract sendClose(options?: AbortOptions): Promise<void>;
25
- }
26
- //# sourceMappingURL=abstract-multiaddr-connection.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"abstract-multiaddr-connection.d.ts","sourceRoot":"","sources":["../../src/abstract-multiaddr-connection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAA;AACrE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACxH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,MAAM,WAAW,+BAAgC,SAAQ,IAAI,CAAC,iBAAiB,EAAE,KAAK,CAAC;IACrF,UAAU,EAAE,SAAS,CAAA;IACrB,SAAS,EAAE,sBAAsB,CAAA;IACjC,GAAG,EAAE,MAAM,CAAA;IACX,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,OAAO,CAAC,EAAE,YAAY,CAAA;CACvB;AAED,8BAAsB,2BAA4B,SAAQ,qBAAsB,YAAW,mBAAmB;IACrG,UAAU,EAAE,SAAS,CAAA;IAE5B,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,OAAO,CAAC,CAAc;gBAEjB,IAAI,EAAE,+BAA+B;IA0B5C,KAAK,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;IAuCnD;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;CAC3D"}
@@ -1,66 +0,0 @@
1
- import { pEvent } from 'p-event';
2
- import { AbstractMessageStream } from "./abstract-message-stream.js";
3
- export class AbstractMultiaddrConnection extends AbstractMessageStream {
4
- remoteAddr;
5
- metricPrefix;
6
- metrics;
7
- constructor(init) {
8
- super(init);
9
- this.metricPrefix = init.metricPrefix ?? '';
10
- this.metrics = init.metrics;
11
- this.remoteAddr = init.remoteAddr;
12
- this.addEventListener('close', (evt) => {
13
- this.metrics?.increment({ [`${this.metricPrefix}end`]: true });
14
- if (evt.error != null) {
15
- if (evt.local) {
16
- this.metrics?.increment({ [`${this.metricPrefix}abort`]: true });
17
- }
18
- else {
19
- this.metrics?.increment({ [`${this.metricPrefix}reset`]: true });
20
- }
21
- }
22
- else {
23
- if (evt.local) {
24
- this.metrics?.increment({ [`${this.metricPrefix}_local_close`]: true });
25
- }
26
- else {
27
- this.metrics?.increment({ [`${this.metricPrefix}_remote_close`]: true });
28
- }
29
- }
30
- });
31
- }
32
- async close(options) {
33
- if (this.status !== 'open') {
34
- return;
35
- }
36
- this.status = 'closing';
37
- this.writeStatus = 'closing';
38
- this.remoteWriteStatus = 'closing';
39
- this.remoteReadStatus = 'closing';
40
- // if we are currently sending data, wait for all the data to be written
41
- // into the underlying transport
42
- if (this.sendingData || this.writeBuffer.byteLength > 0) {
43
- this.log('waiting for write queue to become idle before closing writable end of stream, %d unsent bytes', this.writeBuffer.byteLength);
44
- await pEvent(this, 'idle', {
45
- ...options,
46
- rejectionEvents: [
47
- 'close'
48
- ]
49
- });
50
- }
51
- // now that the underlying transport has all the data, if the buffer is full
52
- // wait for it to be emptied
53
- if (this.writableNeedsDrain) {
54
- this.log('waiting for write queue to drain before closing writable end of stream, %d unsent bytes', this.writeBuffer.byteLength);
55
- await pEvent(this, 'drain', {
56
- ...options,
57
- rejectionEvents: [
58
- 'close'
59
- ]
60
- });
61
- }
62
- await this.sendClose(options);
63
- this.onTransportClosed();
64
- }
65
- }
66
- //# sourceMappingURL=abstract-multiaddr-connection.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"abstract-multiaddr-connection.js","sourceRoot":"","sources":["../../src/abstract-multiaddr-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AAepE,MAAM,OAAgB,2BAA4B,SAAQ,qBAAqB;IACtE,UAAU,CAAW;IAEpB,YAAY,CAAQ;IACpB,OAAO,CAAe;IAE9B,YAAa,IAAqC;QAChD,KAAK,CAAC,IAAI,CAAC,CAAA;QAEX,IAAI,CAAC,YAA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QAED,IAAI,CAAC,MAAM,GAAG,SAAS,CAAA;QACvB,IAAI,CAAC,WAAW,GAAG,SAAS,CAAA;QAC5B,IAAI,CAAC,iBAAiB,GAAG,SAAS,CAAA;QAClC,IAAI,CAAC,gBAAgB,GAAG,SAAS,CAAA;QAEjC,wEAAwE;QACxE,gCAAgC;QAChC,IAAI,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,WAAW,CAAC,UAAU,GAAG,CAAC,EAAE,CAAC;YACxD,IAAI,CAAC,GAAG,CAAC,+FAA+F,EAAE,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,CAAA;YACtI,MAAM,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE;gBACzB,GAAG,OAAO;gBACV,eAAe,EAAE;oBACf,OAAO;iBACR;aACF,CAAC,CAAA;QACJ,CAAC;QAED,4EAA4E;QAC5E,4BAA4B;QAC5B,IAAI,IAAI,CAAC,kBAAkB,EAAE,CAAC;YAC5B,IAAI,CAAC,GAAG,CAAC,yFAAyF,EAAE,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,CAAA;YAChI,MAAM,MAAM,CAAC,IAAI,EAAE,OAAO,EAAE;gBAC1B,GAAG,OAAO;gBACV,eAAe,EAAE;oBACf,OAAO;iBACR;aACF,CAAC,CAAA;QACJ,CAAC;QAED,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;QAE7B,IAAI,CAAC,iBAAiB,EAAE,CAAA;IAC1B,CAAC;CAOF"}
@@ -1,53 +0,0 @@
1
- import { TypedEventEmitter } from '@libp2p/interface';
2
- import type { AbstractStream } from './abstract-stream.ts';
3
- import type { AbortOptions, CounterGroup, CreateStreamOptions, EventHandler, Logger, MessageStream, StreamMuxer, StreamMuxerEvents, StreamMuxerOptions, StreamMuxerStatus, StreamOptions } from '@libp2p/interface';
4
- import type { Uint8ArrayList } from 'uint8arraylist';
5
- export interface AbstractStreamMuxerInit extends StreamMuxerOptions {
6
- /**
7
- * The protocol name for the muxer
8
- */
9
- protocol: string;
10
- /**
11
- * The name of the muxer, used to create a new logging scope from the passed
12
- * connection's logger
13
- */
14
- name: string;
15
- /**
16
- * A counter for muxer metrics
17
- */
18
- metrics?: CounterGroup;
19
- }
20
- export declare abstract class AbstractStreamMuxer<MuxedStream extends AbstractStream = AbstractStream> extends TypedEventEmitter<StreamMuxerEvents<MuxedStream>> implements StreamMuxer<MuxedStream> {
21
- streams: MuxedStream[];
22
- protocol: string;
23
- status: StreamMuxerStatus;
24
- protected log: Logger;
25
- protected maConn: MessageStream;
26
- protected streamOptions?: StreamOptions;
27
- protected earlyStreams: MuxedStream[];
28
- protected maxEarlyStreams: number;
29
- private readonly metrics?;
30
- constructor(maConn: MessageStream, init: AbstractStreamMuxerInit);
31
- send(data: Uint8Array | Uint8ArrayList): boolean;
32
- close(options?: AbortOptions): Promise<void>;
33
- abort(err: Error): void;
34
- onTransportClosed(err?: Error): void;
35
- createStream(options?: CreateStreamOptions): Promise<MuxedStream>;
36
- /**
37
- * Extending classes should invoke this method when a new stream was created
38
- * by the remote muxer
39
- */
40
- onRemoteStream(stream: MuxedStream): void;
41
- private cleanUpStream;
42
- addEventListener<K extends keyof StreamMuxerEvents<MuxedStream>>(type: K, listener: EventHandler<StreamMuxerEvents<MuxedStream>[K]> | null, options?: boolean | AddEventListenerOptions): void;
43
- addEventListener(type: string, listener: EventHandler<Event>, options?: boolean | AddEventListenerOptions): void;
44
- /**
45
- * A new outgoing stream needs to be created
46
- */
47
- abstract onCreateStream(options: CreateStreamOptions): MuxedStream | Promise<MuxedStream>;
48
- /**
49
- * Multiplexed data was received from the remote muxer
50
- */
51
- abstract onData(data: Uint8Array | Uint8ArrayList): void;
52
- }
53
- //# sourceMappingURL=abstract-stream-muxer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"abstract-stream-muxer.d.ts","sourceRoot":"","sources":["../../src/abstract-stream-muxer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,iBAAiB,EAAE,MAAM,m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AAE,GAAG,EAAE,KAAK,GAAG,IAAI;IAcxB,iBAAiB,CAAE,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI;IAc/B,YAAY,CAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,CAAC;IAoBxE;;;OAGG;IACH,cAAc,CAAE,MAAM,EAAE,WAAW,GAAG,IAAI;IAqB1C,OAAO,CAAC,aAAa;IAuBrB,gBAAgB,CAAC,CAAC,SAAS,MAAM,iBAAiB,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,YAAY,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,uBAAuB,GAAG,IAAI;IAC9L,gBAAgB,CAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,uBAAuB,GAAG,IAAI;IAsBjH;;OAEG;IACH,QAAQ,CAAC,cAAc,CAAE,OAAO,EAAE,mBAAmB,GAAG,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAE1F;;OAEG;IACH,QAAQ,CAAC,MAAM,CAAE,IAAI,EAAE,UAAU,GAAG,cAAc,GAAG,IAAI;CAC1D"}
@@ -1,169 +0,0 @@
1
- import { MuxerClosedError, TypedEventEmitter } from '@libp2p/interface';
2
- import { raceSignal } from 'race-signal';
3
- import { MaxEarlyStreamsError } from "./errors.js";
4
- import { isPromise } from "./is-promise.js";
5
- export class AbstractStreamMuxer extends TypedEventEmitter {
6
- streams;
7
- protocol;
8
- status;
9
- log;
10
- maConn;
11
- streamOptions;
12
- earlyStreams;
13
- maxEarlyStreams;
14
- metrics;
15
- constructor(maConn, init) {
16
- super();
17
- this.maConn = maConn;
18
- this.protocol = init.protocol;
19
- this.streams = [];
20
- this.earlyStreams = [];
21
- this.status = 'open';
22
- this.log = maConn.log.newScope(init.name);
23
- this.streamOptions = init.streamOptions;
24
- this.maxEarlyStreams = init.maxEarlyStreams ?? 10;
25
- this.metrics = init.metrics;
26
- // read/write all data from/to underlying maConn
27
- const muxerMaConnOnMessage = (evt) => {
28
- try {
29
- this.onData(evt.data);
30
- }
31
- catch (err) {
32
- this.abort(err);
33
- this.maConn.abort(err);
34
- }
35
- };
36
- this.maConn.addEventListener('message', muxerMaConnOnMessage);
37
- // signal stream writers when the underlying connection can accept more data
38
- const muxerMaConnOnDrain = () => {
39
- this.log('underlying stream drained, signal %d streams to continue writing', this.streams.length);
40
- this.streams.forEach(stream => {
41
- stream.onMuxerDrain();
42
- });
43
- };
44
- this.maConn.addEventListener('drain', muxerMaConnOnDrain);
45
- const muxerOnMaConnClose = () => {
46
- this.log('underlying stream closed with status %s and %d streams', this.status, this.streams.length);
47
- this.onTransportClosed();
48
- };
49
- this.maConn.addEventListener('close', muxerOnMaConnClose);
50
- }
51
- send(data) {
52
- const result = this.maConn.send(data);
53
- if (result === false) {
54
- this.log('underlying stream saturated, signal %d streams to pause writing', this.streams.length);
55
- this.streams.forEach(stream => {
56
- stream.onMuxerNeedsDrain();
57
- });
58
- }
59
- return result;
60
- }
61
- async close(options) {
62
- if (this.status === 'closed' || this.status === 'closing') {
63
- return;
64
- }
65
- this.status = 'closing';
66
- await raceSignal(Promise.all([...this.streams].map(async (s) => {
67
- await s.close(options);
68
- })), options?.signal);
69
- this.status = 'closed';
70
- }
71
- abort(err) {
72
- if (this.status === 'closed') {
73
- return;
74
- }
75
- this.status = 'closing';
76
- [...this.streams].forEach(s => {
77
- s.abort(err);
78
- });
79
- this.status = 'closed';
80
- }
81
- onTransportClosed(err) {
82
- this.status = 'closing';
83
- try {
84
- [...this.streams].forEach(stream => {
85
- stream.onTransportClosed(err);
86
- });
87
- }
88
- catch (err) {
89
- this.abort(err);
90
- }
91
- this.status = 'closed';
92
- }
93
- async createStream(options) {
94
- if (this.status !== 'open') {
95
- throw new MuxerClosedError();
96
- }
97
- let stream = this.onCreateStream({
98
- ...this.streamOptions,
99
- ...options
100
- });
101
- if (isPromise(stream)) {
102
- stream = await stream;
103
- }
104
- this.streams.push(stream);
105
- this.cleanUpStream(stream);
106
- return stream;
107
- }
108
- /**
109
- * Extending classes should invoke this method when a new stream was created
110
- * by the remote muxer
111
- */
112
- onRemoteStream(stream) {
113
- this.streams.push(stream);
114
- this.cleanUpStream(stream);
115
- if (this.listenerCount('stream') === 0) {
116
- // no listener has been added for the stream event yet, store the stream
117
- // to emit it later
118
- this.earlyStreams.push(stream);
119
- if (this.earlyStreams.length > this.maxEarlyStreams) {
120
- this.abort(new MaxEarlyStreamsError(`Too many early streams were opened - ${this.earlyStreams.length}/${this.maxEarlyStreams}`));
121
- }
122
- return;
123
- }
124
- this.safeDispatchEvent('stream', {
125
- detail: stream
126
- });
127
- }
128
- cleanUpStream(stream) {
129
- const muxerOnStreamEnd = (evt) => {
130
- const index = this.streams.findIndex(s => s === stream);
131
- if (index !== -1) {
132
- this.streams.splice(index, 1);
133
- }
134
- if (evt.error != null) {
135
- if (evt.local) {
136
- this.metrics?.increment({ [`${stream.direction}_stream_reset`]: true });
137
- }
138
- else {
139
- this.metrics?.increment({ [`${stream.direction}_stream_abort`]: true });
140
- }
141
- }
142
- else {
143
- this.metrics?.increment({ [`${stream.direction}_stream_end`]: true });
144
- }
145
- };
146
- stream.addEventListener('close', muxerOnStreamEnd);
147
- this.metrics?.increment({ [`${stream.direction}_stream`]: true });
148
- }
149
- addEventListener(...args) {
150
- // @ts-expect-error cannot ensure args has enough members
151
- super.addEventListener.apply(this, args);
152
- // if a 'stream' listener is being added and we have early streams, emit
153
- // them
154
- if (args[0] === 'stream' && this.earlyStreams.length > 0) {
155
- // event listeners can be added in constructors and often use object
156
- // properties - if this the case we can access a class member before it
157
- // has been initialized so dispatch the message in the microtask queue
158
- queueMicrotask(() => {
159
- this.earlyStreams.forEach(stream => {
160
- this.safeDispatchEvent('stream', {
161
- detail: stream
162
- });
163
- });
164
- this.earlyStreams = [];
165
- });
166
- }
167
- }
168
- }
169
- //# sourceMappingURL=abstract-stream-muxer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"abstract-stream-muxer.js","sourceRoot":"","sources":["../../src/abstract-stream-muxer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAuB3C,MAAM,OAAgB,mBAA0E,SAAQ,iBAAiD;IAChJ,OAAO,CAAe;IACtB,QAAQ,CAAQ;IAChB,MAAM,CAAmB;IAEtB,GAAG,CAAQ;IACX,MAAM,CAAe;IACrB,aAAa,CAAgB;IAC7B,YAAY,CAAe;IAC3B,eAAe,CAAQ;IAEhB,OAAO,CAAe;IAEvC,YAAa,MAAqB,EAAE,IAA6B;QAC/D,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAA;QAC7B,IAAI,CAAC,OAAO,GAAG,EAAE,CAAA;QACjB,IAAI,CAAC,YAAY,GAAG,EAAE,CAAA;QACtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACzC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,CAAA;QACvC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,eAAe,IAAI,EAAE,CAAA;QACjD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAA;QAE3B,gDAAgD;QAChD,MAAM,oBAAoB,GAAG,CAAC,GAAu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yDAAyD;QACzD,KAAK,CAAC,gBAAgB,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;QAExC,wEAAwE;QACxE,OAAO;QACP,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACzD,oEAAoE;YACpE,uEAAuE;YACvE,sEAAsE;YACtE,cAAc,CAAC,GAAG,EAAE;gBAClB,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;oBACjC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,EAAE;wBAC/B,MAAM,EAAE,MAAM;qBACf,CAAC,CAAA;gBACJ,CAAC,CAAC,CAAA;gBACF,IAAI,CAAC,YAAY,GAAG,EAAE,CAAA;YACxB,CAAC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;CAWF"}
@@ -1,37 +0,0 @@
1
- import { Uint8ArrayList } from 'uint8arraylist';
2
- export interface LengthPrefixedDecoderInit {
3
- /**
4
- * How large the internal buffer is allowed to grow - attempting to store more
5
- * data than this will throw
6
- */
7
- maxBufferSize?: number;
8
- /**
9
- * Throw an error if the message that would be read from the buffer is larger
10
- * than this value
11
- */
12
- maxDataLength?: number;
13
- /**
14
- * Read a varint from the buffer
15
- */
16
- lengthDecoder?(data: Uint8ArrayList | Uint8Array): number;
17
- /**
18
- * Return how many bytes it takes to encode the passed value
19
- */
20
- encodingLength?(length: number): number;
21
- }
22
- /**
23
- * Decode length-prefixed data from a buffer
24
- */
25
- export declare class LengthPrefixedDecoder {
26
- private readonly buffer;
27
- private readonly maxBufferSize;
28
- private readonly lengthDecoder;
29
- private readonly maxDataLength;
30
- private readonly encodingLength;
31
- constructor(init?: LengthPrefixedDecoderInit);
32
- /**
33
- * Decodes length-prefixed data
34
- */
35
- decode(buf: Uint8Array | Uint8ArrayList): Generator<Uint8ArrayList>;
36
- }
37
- //# sourceMappingURL=length-prefixed-decoder.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"length-prefixed-decoder.d.ts","sourceRoot":"","sources":["../../src/length-prefixed-decoder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAM/C,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB;;OAEG;IACH,aAAa,CAAC,CAAC,IAAI,EAAE,cAAc,GAAG,UAAU,GAAG,MAAM,CAAA;IAEzD;;OAEG;IACH,cAAc,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;CACxC;AAED;;GAEG;AACH,qBAAa,qBAAqB;IAChC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;IACvC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAQ;IACtC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA+C;IAC7E,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAQ;IACtC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA4B;gBAE9C,IAAI,GAAE,yBAA8B;IAQjD;;OAEG;IACD,MAAM,CAAE,GAAG,EAAE,UAAU,GAAG,cAAc,GAAG,SAAS,CAAC,cAAc,CAAC;CA4CvE"}
@@ -1,64 +0,0 @@
1
- import { InvalidParametersError } from '@libp2p/interface';
2
- import * as varint from 'uint8-varint';
3
- import { Uint8ArrayList } from 'uint8arraylist';
4
- import { InvalidMessageLengthError } from "./stream-utils.js";
5
- const DEFAULT_MAX_BUFFER_SIZE = 1024 * 1024 * 4;
6
- const DEFAULT_MAX_DATA_LENGTH = 1024 * 1024 * 4;
7
- /**
8
- * Decode length-prefixed data from a buffer
9
- */
10
- export class LengthPrefixedDecoder {
11
- buffer;
12
- maxBufferSize;
13
- lengthDecoder;
14
- maxDataLength;
15
- encodingLength;
16
- constructor(init = {}) {
17
- this.buffer = new Uint8ArrayList();
18
- this.maxBufferSize = init.maxBufferSize ?? DEFAULT_MAX_BUFFER_SIZE;
19
- this.maxDataLength = init.maxDataLength ?? DEFAULT_MAX_DATA_LENGTH;
20
- this.lengthDecoder = init.lengthDecoder ?? varint.decode;
21
- this.encodingLength = init.encodingLength ?? varint.encodingLength;
22
- }
23
- /**
24
- * Decodes length-prefixed data
25
- */
26
- *decode(buf) {
27
- this.buffer.append(buf);
28
- if (this.buffer.byteLength > this.maxBufferSize) {
29
- throw new InvalidParametersError(`Buffer length limit exceeded - ${this.buffer.byteLength}/${this.maxBufferSize}`);
30
- }
31
- // Loop to consume as many bytes from the buffer as possible
32
- // Eg: when a single chunk contains several frames
33
- while (true) {
34
- let dataLength;
35
- try {
36
- dataLength = this.lengthDecoder(this.buffer);
37
- }
38
- catch (err) {
39
- if (err instanceof RangeError) {
40
- // ignore errors where we don't have enough data to read the length
41
- // prefix
42
- break;
43
- }
44
- throw err;
45
- }
46
- if (dataLength < 0 || dataLength > this.maxDataLength) {
47
- throw new InvalidMessageLengthError('Invalid message length');
48
- }
49
- const lengthLength = this.encodingLength(dataLength);
50
- const chunkLength = lengthLength + dataLength;
51
- if (this.buffer.byteLength >= chunkLength) {
52
- const buf = this.buffer.sublist(lengthLength, chunkLength);
53
- this.buffer.consume(chunkLength);
54
- if (buf.byteLength > 0) {
55
- yield buf;
56
- }
57
- }
58
- else {
59
- break;
60
- }
61
- }
62
- }
63
- }
64
- //# sourceMappingURL=length-prefixed-decoder.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"length-prefixed-decoder.js","sourceRoot":"","sources":["../../src/length-prefixed-decoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAA;AA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E;oBACnE,SAAS;oBACT,MAAK;gBACP,CAAC;gBAED,MAAM,GAAG,CAAA;YACX,CAAC;YAED,IAAI,UAAU,GAAG,CAAC,IAAI,UAAU,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;gBACtD,MAAM,IAAI,yBAAyB,CAAC,wBAAwB,CAAC,CAAA;YAC/D,CAAC;YAED,MAAM,YAAY,GAAG,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC,CAAA;YACpD,MAAM,WAAW,GAAG,YAAY,GAAG,UAAU,CAAA;YAE7C,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,WAAW,EAAE,CAAC;gBAC1C,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,EAAE,WAAW,CAAC,CAAA;gBAE1D,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAA;gBAEhC,IAAI,GAAG,CAAC,UAAU,GAAG,CAAC,EAAE,CAAC;oBACvB,MAAM,GAAG,CAAA;gBACX,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,MAAK;YACP,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
@@ -1,61 +0,0 @@
1
- import { StreamMessageEvent } from '@libp2p/interface';
2
- import { TypedEventEmitter } from 'main-event';
3
- import type { Logger } from '@libp2p/interface';
4
- export interface MessageQueueEvents {
5
- /**
6
- * Message data
7
- */
8
- message: StreamMessageEvent;
9
- /**
10
- * Emitted when the queue is empty
11
- */
12
- drain: Event;
13
- /**
14
- * The remote closed the connection abruptly
15
- */
16
- reset: Event;
17
- }
18
- export interface MessageQueueInit {
19
- /**
20
- * How much delay there should be between each message send in ms (note that
21
- * even 0 introduces a small delay)
22
- *
23
- * @default 0
24
- */
25
- delay?: number;
26
- /**
27
- * How many messages to hold in the send queue before applying backpressure to
28
- * the sender
29
- */
30
- capacity?: number;
31
- /**
32
- * Data messages larger than this size will be chunked into smaller messages.
33
- *
34
- * Defaults to the maximum TCP package size.
35
- *
36
- * @default 65_536
37
- */
38
- chunkSize?: number;
39
- }
40
- /**
41
- * Accepts events to emit after a short delay, and with a configurable maximum
42
- * queue capacity after which the send method will return false to let us
43
- * simulate write backpressure.
44
- */
45
- export declare class MessageQueue<Events> extends TypedEventEmitter<Events & MessageQueueEvents> {
46
- needsDrain: boolean;
47
- private queue;
48
- private capacity;
49
- private delay;
50
- private log;
51
- private chunkSize;
52
- constructor(init: MessageQueueInit & {
53
- log: Logger;
54
- });
55
- send(evt: Event): boolean;
56
- pause(): void;
57
- resume(): void;
58
- onIdle(): Promise<void>;
59
- size(): number;
60
- }
61
- //# sourceMappingURL=message-queue.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"message-queue.d.ts","sourceRoot":"","sources":["../../src/message-queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAEtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAI9C,OAAO,KAAK,EAAgB,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAI7D,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,OAAO,EAAE,kBAAkB,CAAA;IAE3B;;OAEG;IACH,KAAK,EAAE,KAAK,CAAA;IAEZ;;OAEG;IACH,KAAK,EAAE,KAAK,CAAA;CACb;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IAEjB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAMD;;;;GAIG;AACH,qBAAa,YAAY,CAAC,MAAM,CAAE,SAAQ,iBAAiB,CAAC,MAAM,GAAG,kBAAkB,CAAC;IAC/E,UAAU,EAAE,OAAO,CAAA;IAE1B,OAAO,CAAC,KAAK,CAAqC;IAClD,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,GAAG,CAAQ;IACnB,OAAO,CAAC,SAAS,CAAQ;gBAEZ,IAAI,EAAE,gBAAgB,GAAG;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE;IAuBrD,IAAI,CAAE,GAAG,EAAE,KAAK,GAAG,OAAO;IA2C1B,KAAK,IAAK,IAAI;IAId,MAAM,IAAK,IAAI;IAIf,MAAM,IAAK,OAAO,CAAC,IAAI,CAAC;IAIxB,IAAI,IAAK,MAAM;CAGhB"}
@@ -1,93 +0,0 @@
1
- import { StreamMessageEvent } from '@libp2p/interface';
2
- import delay from 'delay';
3
- import { TypedEventEmitter } from 'main-event';
4
- import { raceSignal } from 'race-signal';
5
- import { isUint8ArrayList, Uint8ArrayList } from 'uint8arraylist';
6
- import { Queue } from './queue/index.js';
7
- const DEFAULT_CHUNK_SIZE = 1024 * 64;
8
- /**
9
- * Accepts events to emit after a short delay, and with a configurable maximum
10
- * queue capacity after which the send method will return false to let us
11
- * simulate write backpressure.
12
- */
13
- export class MessageQueue extends TypedEventEmitter {
14
- needsDrain;
15
- queue;
16
- capacity;
17
- delay;
18
- log;
19
- chunkSize;
20
- constructor(init) {
21
- super();
22
- this.needsDrain = false;
23
- this.queue = new Queue({
24
- concurrency: 1
25
- });
26
- this.capacity = init.capacity ?? 5;
27
- this.delay = init.delay ?? 0;
28
- this.log = init.log;
29
- this.chunkSize = init.chunkSize ?? DEFAULT_CHUNK_SIZE;
30
- this.queue.addEventListener('idle', () => {
31
- if (this.needsDrain) {
32
- this.log('network send queue drained');
33
- this.needsDrain = false;
34
- this.safeDispatchEvent('drain');
35
- }
36
- else {
37
- this.log('network send queue idle');
38
- }
39
- });
40
- }
41
- send(evt) {
42
- if (isMessageEvent(evt)) {
43
- // chunk outgoing messages to match TCP packet sizes
44
- const data = new Uint8ArrayList(evt.data);
45
- while (data.byteLength > 0) {
46
- const end = Math.min(this.chunkSize, data.byteLength);
47
- const chunk = data.sublist(0, end);
48
- data.consume(chunk.byteLength);
49
- const chunkEvent = new StreamMessageEvent(chunk);
50
- this.queue.add(async (opts) => {
51
- if (this.delay > 0) {
52
- await raceSignal(delay(this.delay), opts.signal);
53
- }
54
- this.dispatchEvent(opts.evt);
55
- }, {
56
- evt: chunkEvent
57
- });
58
- }
59
- }
60
- else {
61
- this.queue.add(async (opts) => {
62
- if (this.delay > 0) {
63
- await raceSignal(delay(this.delay), opts.signal);
64
- }
65
- this.dispatchEvent(opts.evt);
66
- }, {
67
- evt
68
- });
69
- }
70
- if (this.queue.size >= this.capacity) {
71
- this.log('network send queue full');
72
- this.needsDrain = true;
73
- return false;
74
- }
75
- return true;
76
- }
77
- pause() {
78
- this.queue.pause();
79
- }
80
- resume() {
81
- this.queue.resume();
82
- }
83
- onIdle() {
84
- return this.queue.onIdle();
85
- }
86
- size() {
87
- return this.queue.size;
88
- }
89
- }
90
- function isMessageEvent(evt) {
91
- return evt?.data instanceof Uint8Array || isUint8ArrayList(evt?.data);
92
- }
93
- //# sourceMappingURL=message-queue.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"message-queue.js","sourceRoot":"","sources":["../../src/message-queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AACjE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAGxC,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAAE,CAAA;AAgDpC;;;;GAIG;AACH,MAAM,OAAO,YAAqB,SAAQ,iBAA8C;IAC/E,UAAU,CAAS;IAElB,KAAK,CAAqC;IAC1C,QAAQ,CAAQ;IAChB,KAAK,CAAQ;IACb,GAAG,CA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