@zygout/eris 0.0.0-stage → 0.19.0

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 (88) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +80 -3
  3. package/esm.mjs +72 -0
  4. package/index.d.ts +4397 -0
  5. package/index.js +75 -0
  6. package/lib/Client.js +4683 -0
  7. package/lib/Constants.d.ts +988 -0
  8. package/lib/Constants.js +1161 -0
  9. package/lib/command/Command.js +552 -0
  10. package/lib/command/CommandClient.js +460 -0
  11. package/lib/errors/DiscordHTTPError.js +63 -0
  12. package/lib/errors/DiscordRESTError.js +77 -0
  13. package/lib/gateway/Shard.js +3029 -0
  14. package/lib/gateway/ShardManager.js +164 -0
  15. package/lib/rest/Endpoints.js +172 -0
  16. package/lib/rest/RequestHandler.js +459 -0
  17. package/lib/structures/ApplicationCommand.js +125 -0
  18. package/lib/structures/AutoModerationRule.js +143 -0
  19. package/lib/structures/AutocompleteInteraction.js +80 -0
  20. package/lib/structures/Base.js +81 -0
  21. package/lib/structures/CategoryChannel.js +77 -0
  22. package/lib/structures/Channel.js +98 -0
  23. package/lib/structures/CommandInteraction.js +473 -0
  24. package/lib/structures/ComponentInteraction.js +538 -0
  25. package/lib/structures/DMChannel.js +278 -0
  26. package/lib/structures/Entitlement.js +77 -0
  27. package/lib/structures/ExtendedUser.js +46 -0
  28. package/lib/structures/ForumChannel.js +24 -0
  29. package/lib/structures/GroupChannel.js +106 -0
  30. package/lib/structures/Guild.js +1677 -0
  31. package/lib/structures/GuildAuditLogEntry.js +190 -0
  32. package/lib/structures/GuildChannel.js +128 -0
  33. package/lib/structures/GuildIntegration.js +99 -0
  34. package/lib/structures/GuildPreview.js +97 -0
  35. package/lib/structures/GuildScheduledEvent.js +176 -0
  36. package/lib/structures/GuildTemplate.js +76 -0
  37. package/lib/structures/GuildTextableChannel.js +260 -0
  38. package/lib/structures/Interaction.js +133 -0
  39. package/lib/structures/Invite.js +143 -0
  40. package/lib/structures/Lobby.js +108 -0
  41. package/lib/structures/LobbyMember.js +59 -0
  42. package/lib/structures/MediaChannel.js +216 -0
  43. package/lib/structures/Member.js +371 -0
  44. package/lib/structures/Message.js +708 -0
  45. package/lib/structures/ModalSubmitInteraction.js +478 -0
  46. package/lib/structures/NewsChannel.js +34 -0
  47. package/lib/structures/NewsThreadChannel.js +12 -0
  48. package/lib/structures/Permission.js +80 -0
  49. package/lib/structures/PermissionOverwrite.js +32 -0
  50. package/lib/structures/PingInteraction.js +39 -0
  51. package/lib/structures/PrivateThreadChannel.js +25 -0
  52. package/lib/structures/PublicThreadChannel.js +26 -0
  53. package/lib/structures/Role.js +163 -0
  54. package/lib/structures/SKU.js +80 -0
  55. package/lib/structures/SoundboardSound.js +79 -0
  56. package/lib/structures/StageChannel.js +59 -0
  57. package/lib/structures/StageInstance.js +55 -0
  58. package/lib/structures/Subscription.js +69 -0
  59. package/lib/structures/TextChannel.js +202 -0
  60. package/lib/structures/ThreadChannel.js +143 -0
  61. package/lib/structures/ThreadMember.js +59 -0
  62. package/lib/structures/UnavailableGuild.js +26 -0
  63. package/lib/structures/UnknownInteraction.js +502 -0
  64. package/lib/structures/User.js +296 -0
  65. package/lib/structures/VoiceChannel.js +166 -0
  66. package/lib/structures/VoiceState.js +84 -0
  67. package/lib/util/BrowserWebSocket.js +76 -0
  68. package/lib/util/Bucket.js +89 -0
  69. package/lib/util/Collection.js +202 -0
  70. package/lib/util/MultipartData.js +70 -0
  71. package/lib/util/Opus.js +33 -0
  72. package/lib/util/SequentialBucket.js +83 -0
  73. package/lib/util/emitDeprecation.js +26 -0
  74. package/lib/voice/Piper.js +300 -0
  75. package/lib/voice/SharedStream.js +238 -0
  76. package/lib/voice/VoiceConnection.js +1440 -0
  77. package/lib/voice/VoiceConnectionManager.js +151 -0
  78. package/lib/voice/VoiceDataStream.js +22 -0
  79. package/lib/voice/streams/BaseTransformer.js +40 -0
  80. package/lib/voice/streams/DCAOpusTransformer.js +78 -0
  81. package/lib/voice/streams/FFmpegDuplex.js +192 -0
  82. package/lib/voice/streams/FFmpegOggTransformer.js +35 -0
  83. package/lib/voice/streams/FFmpegPCMTransformer.js +26 -0
  84. package/lib/voice/streams/OggOpusTransformer.js +108 -0
  85. package/lib/voice/streams/PCMOpusTransformer.js +61 -0
  86. package/lib/voice/streams/VolumeTransformer.js +50 -0
  87. package/lib/voice/streams/WebmOpusTransformer.js +258 -0
  88. package/package.json +82 -6
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+
3
+ const Base = require("../structures/Base");
4
+ const Collection = require("../util/Collection");
5
+
6
+ class VoiceConnectionManager extends Collection {
7
+ constructor(vcObject) {
8
+ super(vcObject || require("./VoiceConnection"));
9
+ this.pendingGuilds = {};
10
+ }
11
+
12
+ join(guildID, channelID, options) {
13
+ const connection = this.get(guildID);
14
+ if (connection && connection.ws) {
15
+ connection.switchChannel(channelID);
16
+ if (connection.ready) {
17
+ return Promise.resolve(connection);
18
+ } else {
19
+ return new Promise((res, rej) => {
20
+ const disconnectHandler = (err) => {
21
+ connection.removeListener("ready", readyHandler);
22
+ connection.removeListener("error", errorHandler);
23
+ rej(err || new Error("Disconnected"));
24
+ };
25
+ const readyHandler = () => {
26
+ connection.removeListener("disconnect", disconnectHandler);
27
+ connection.removeListener("error", errorHandler);
28
+ res(connection);
29
+ };
30
+ const errorHandler = (err) => {
31
+ connection.removeListener("disconnect", disconnectHandler);
32
+ connection.removeListener("ready", readyHandler);
33
+ connection.disconnect();
34
+ rej(err);
35
+ };
36
+ connection.once("ready", readyHandler).once("disconnect", disconnectHandler).once("error", errorHandler);
37
+ });
38
+ }
39
+ }
40
+ return new Promise((res, rej) => {
41
+ this.pendingGuilds[guildID] = {
42
+ channelID: channelID,
43
+ options: options || {},
44
+ res: res,
45
+ rej: rej,
46
+ timeout: setTimeout(() => {
47
+ delete this.pendingGuilds[guildID];
48
+ rej(new Error("Voice connection timeout"));
49
+ }, 10000),
50
+ };
51
+ });
52
+ }
53
+
54
+ leave(guildID) {
55
+ const connection = this.get(guildID);
56
+ if (!connection) {
57
+ return;
58
+ }
59
+ connection.disconnect();
60
+ connection._destroy();
61
+ this.remove(connection);
62
+ }
63
+
64
+ switch(guildID, channelID) {
65
+ const connection = this.get(guildID);
66
+ if (!connection) {
67
+ return;
68
+ }
69
+ connection.switchChannel(channelID);
70
+ }
71
+
72
+ voiceServerUpdate(data) {
73
+ if (this.pendingGuilds[data.guild_id] && this.pendingGuilds[data.guild_id].timeout) {
74
+ clearTimeout(this.pendingGuilds[data.guild_id].timeout);
75
+ this.pendingGuilds[data.guild_id].timeout = null;
76
+ }
77
+ let connection = this.get(data.guild_id);
78
+ if (!connection) {
79
+ if (!this.pendingGuilds[data.guild_id]) {
80
+ return;
81
+ }
82
+ const options = this.pendingGuilds[data.guild_id].options;
83
+ connection = this.add(new this.baseObject(data.guild_id, {
84
+ daveEncryption: options.daveEncryption,
85
+ decryptionFailureTolerance: options.decryptionFailureTolerance,
86
+ shard: data.shard,
87
+ opusOnly: options.opusOnly,
88
+ shared: options.shared,
89
+ }));
90
+ }
91
+ connection.connect({
92
+ channel_id: (this.pendingGuilds[data.guild_id] || connection).channelID,
93
+ endpoint: data.endpoint,
94
+ token: data.token,
95
+ session_id: data.session_id,
96
+ user_id: data.user_id,
97
+ });
98
+ if (!this.pendingGuilds[data.guild_id] || this.pendingGuilds[data.guild_id].waiting) {
99
+ return;
100
+ }
101
+ this.pendingGuilds[data.guild_id].waiting = true;
102
+ const disconnectHandler = (err) => {
103
+ connection = this.get(data.guild_id);
104
+ if (connection) {
105
+ connection.removeListener("ready", readyHandler);
106
+ connection.removeListener("error", errorHandler);
107
+ }
108
+ if (this.pendingGuilds[data.guild_id]) {
109
+ this.pendingGuilds[data.guild_id].rej(err || new Error("Disconnected"));
110
+ delete this.pendingGuilds[data.guild_id];
111
+ }
112
+ };
113
+ const readyHandler = () => {
114
+ connection = this.get(data.guild_id);
115
+ if (connection) {
116
+ connection.removeListener("disconnect", disconnectHandler);
117
+ connection.removeListener("error", errorHandler);
118
+ }
119
+ if (this.pendingGuilds[data.guild_id]) {
120
+ this.pendingGuilds[data.guild_id].res(connection);
121
+ delete this.pendingGuilds[data.guild_id];
122
+ }
123
+ };
124
+ const errorHandler = (err) => {
125
+ connection = this.get(data.guild_id);
126
+ if (connection) {
127
+ connection.removeListener("disconnect", disconnectHandler);
128
+ connection.removeListener("ready", readyHandler);
129
+ connection.disconnect();
130
+ }
131
+ if (this.pendingGuilds[data.guild_id]) {
132
+ this.pendingGuilds[data.guild_id].rej(err);
133
+ delete this.pendingGuilds[data.guild_id];
134
+ }
135
+ };
136
+ connection.once("ready", readyHandler).once("disconnect", disconnectHandler).once("error", errorHandler);
137
+ }
138
+
139
+ toString() {
140
+ return "[VoiceConnectionManager]";
141
+ }
142
+
143
+ toJSON(props = []) {
144
+ return Base.prototype.toJSON.call(this, [
145
+ "pendingGuilds",
146
+ ...props,
147
+ ]);
148
+ }
149
+ }
150
+
151
+ module.exports = VoiceConnectionManager;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ let EventEmitter;
4
+ try {
5
+ EventEmitter = require("eventemitter3");
6
+ } catch {
7
+ EventEmitter = require("events").EventEmitter;
8
+ }
9
+
10
+ /**
11
+ * Represents a voice data stream
12
+ * @extends EventEmitter
13
+ * @prop {String} type The targeted voice data type for the stream, either "opus" or "pcm"
14
+ */
15
+ class VoiceDataStream extends EventEmitter {
16
+ constructor(type) {
17
+ super();
18
+ this.type = type;
19
+ }
20
+ }
21
+
22
+ module.exports = VoiceDataStream;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ const util = require("util");
4
+ const Base = require("../../structures/Base");
5
+ const TransformStream = require("stream").Transform;
6
+
7
+ class BaseTransformer extends TransformStream {
8
+ constructor(options = {}) {
9
+ if (options.allowHalfOpen === undefined) {
10
+ options.allowHalfOpen = true;
11
+ }
12
+ if (options.highWaterMark === undefined) {
13
+ options.highWaterMark = 0;
14
+ }
15
+ super(options);
16
+ this.manualCB = false;
17
+ }
18
+
19
+ setTransformCB(cb) {
20
+ if (this.manualCB) {
21
+ this.transformCB();
22
+ this._transformCB = cb;
23
+ } else {
24
+ cb();
25
+ }
26
+ }
27
+
28
+ transformCB() {
29
+ if (this._transformCB) {
30
+ this._transformCB();
31
+ this._transformCB = null;
32
+ }
33
+ }
34
+
35
+ [util.inspect.custom]() {
36
+ return Base.prototype[util.inspect.custom].call(this);
37
+ }
38
+ }
39
+
40
+ module.exports = BaseTransformer;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ const BaseTransformer = require("./BaseTransformer");
4
+
5
+ class DCAOpusTransformer extends BaseTransformer {
6
+ constructor(options = {}) {
7
+ super(options);
8
+
9
+ this._remainder = null;
10
+ }
11
+
12
+ process(buffer) {
13
+ if (buffer.length - buffer._index < 2) {
14
+ return true;
15
+ }
16
+
17
+ const opusLen = buffer.readInt16LE(buffer._index);
18
+ buffer._index += 2;
19
+
20
+ if (buffer.length - buffer._index < opusLen) {
21
+ return true;
22
+ }
23
+
24
+ buffer._index += opusLen;
25
+ this.push(buffer.subarray(buffer._index - opusLen, buffer._index));
26
+ }
27
+
28
+ _transform(chunk, enc, cb) {
29
+ if (this._remainder) {
30
+ chunk = Buffer.concat([this._remainder, chunk]);
31
+ this._remainder = null;
32
+ }
33
+
34
+ if (!this.head) {
35
+ if (chunk.length < 4) {
36
+ this._remainder = chunk;
37
+ return cb();
38
+ } else {
39
+ const dcaVersion = chunk.subarray(0, 4);
40
+ if (dcaVersion[0] !== 68 || dcaVersion[1] !== 67 || dcaVersion[2] !== 65) { // DCA0 or invalid
41
+ this.head = true; // Attempt to play as if it were a DCA0 file
42
+ } else if (dcaVersion[3] === 49) { // DCA1
43
+ if (chunk.length < 8) {
44
+ this._remainder = chunk;
45
+ return cb();
46
+ }
47
+ const jsonLength = chunk.subarray(4, 8).readInt32LE(0);
48
+ if (chunk.length < 8 + jsonLength) {
49
+ this._remainder = chunk;
50
+ return cb();
51
+ }
52
+ const jsonMetadata = chunk.subarray(8, 8 + jsonLength);
53
+ this.emit("debug", jsonMetadata);
54
+ chunk = chunk.subarray(8 + jsonLength);
55
+ this.head = true;
56
+ } else {
57
+ this.emit("error", new Error("Unsupported DCA version: " + dcaVersion.toString()));
58
+ }
59
+ }
60
+ }
61
+
62
+ chunk._index = 0;
63
+
64
+ while (chunk._index < chunk.length) {
65
+ const offset = chunk._index;
66
+ const ret = this.process(chunk);
67
+ if (ret) {
68
+ this._remainder = chunk.subarray(offset);
69
+ cb();
70
+ return;
71
+ }
72
+ }
73
+
74
+ this.setTransformCB(cb);
75
+ }
76
+ }
77
+
78
+ module.exports = DCAOpusTransformer;
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+ const util = require("util");
3
+ const Base = require("../../structures/Base");
4
+ const ChildProcess = require("child_process");
5
+ const DuplexStream = require("stream").Duplex;
6
+ const PassThroughStream = require("stream").PassThrough;
7
+
8
+ const delegateEvents = {
9
+ readable: "_reader",
10
+ data: "_reader",
11
+ end: "_reader",
12
+ drain: "_writer",
13
+ finish: "_writer",
14
+ };
15
+
16
+ class FFmpegDuplex extends DuplexStream {
17
+ constructor(command, options = {}) {
18
+ if (options.highWaterMark === undefined) {
19
+ options.highWaterMark = 0;
20
+ }
21
+ super(options);
22
+
23
+ this.command = command;
24
+ this._reader = new PassThroughStream(options);
25
+ this._writer = new PassThroughStream(options);
26
+
27
+ this._onError = this.emit.bind(this, "error");
28
+
29
+ this._reader.on("error", this._onError);
30
+ this._writer.on("error", this._onError);
31
+
32
+ this._readableState = this._reader._readableState;
33
+ this._writableState = this._writer._writableState;
34
+
35
+ ["on", "once", "removeListener", "removeListeners", "listeners"].forEach((method) => {
36
+ const og = DuplexStream.prototype[method];
37
+
38
+ this[method] = function (ev, fn) {
39
+ const substream = delegateEvents[ev];
40
+ if (substream) {
41
+ return this[substream][method](ev, fn);
42
+ } else {
43
+ return og.call(this, ev, fn);
44
+ }
45
+ };
46
+ });
47
+ }
48
+
49
+ destroy() {
50
+ }
51
+
52
+ end(chunk, enc, cb) {
53
+ return this._writer.end(chunk, enc, cb);
54
+ }
55
+
56
+ kill() {
57
+ }
58
+
59
+ noop() {
60
+ }
61
+
62
+ pipe(dest, opts) {
63
+ return this._reader.pipe(dest, opts);
64
+ }
65
+
66
+ read(size) {
67
+ return this._reader.read(size);
68
+ }
69
+
70
+ setEncoding(enc) {
71
+ return this._reader.setEncoding(enc);
72
+ }
73
+
74
+ spawn(args, options = {}) {
75
+ let ex, exited, killed, ended;
76
+ let stderr = [];
77
+
78
+ const onStdoutEnd = () => {
79
+ if (exited && !ended) {
80
+ ended = true;
81
+ this._reader.end();
82
+ setImmediate(this.emit.bind(this, "close"));
83
+ }
84
+ };
85
+
86
+ const onStderrData = (chunk) => {
87
+ stderr.push(chunk);
88
+ };
89
+
90
+ const cleanup = () => {
91
+ this._process = this._stderr = this._stdout = this._stdin = stderr = ex = killed = null;
92
+
93
+ this.kill = this.destroy = this.noop;
94
+ };
95
+
96
+ const onExit = (code, signal) => {
97
+ if (exited) {
98
+ return;
99
+ }
100
+ exited = true;
101
+
102
+ if (killed) {
103
+ if (ex) {
104
+ this.emit("error", ex);
105
+ }
106
+ this.emit("close");
107
+ } else if (code === 0 && signal == null) {
108
+ // All is well
109
+ onStdoutEnd();
110
+ } else {
111
+ // Everything else
112
+ ex = new Error("Command failed: " + Buffer.concat(stderr).toString("utf8"));
113
+ ex.killed = this._process.killed || killed;
114
+ ex.code = code;
115
+ ex.signal = signal;
116
+ this.emit("error", ex);
117
+ this.emit("close");
118
+ }
119
+
120
+ cleanup();
121
+ };
122
+
123
+ const onError = (err) => {
124
+ ex = err;
125
+ this._stdout.destroy();
126
+ this._stderr.destroy();
127
+ onExit();
128
+ };
129
+
130
+ const kill = () => {
131
+ if (killed) {
132
+ return;
133
+ }
134
+ this._stdout.destroy();
135
+ this._stderr.destroy();
136
+
137
+ killed = true;
138
+
139
+ try {
140
+ this._process.kill(options.killSignal || "SIGTERM");
141
+ setTimeout(() => this._process && this._process.kill("SIGKILL"), 2000);
142
+ } catch (e) {
143
+ ex = e;
144
+ onExit();
145
+ }
146
+ };
147
+
148
+ this._process = ChildProcess.spawn(this.command, args, options);
149
+ this._stdin = this._process.stdin;
150
+ this._stdout = this._process.stdout;
151
+ this._stderr = this._process.stderr;
152
+ this._writer.pipe(this._stdin);
153
+ this._stdout.pipe(this._reader, {
154
+ end: false,
155
+ });
156
+ this.kill = this.destroy = kill;
157
+
158
+ this._stderr.on("data", onStderrData);
159
+
160
+ // In some cases ECONNRESET can be emitted by stdin because the process is not interested in any
161
+ // more data but the _writer is still piping. Forget about errors emitted on stdin and stdout
162
+ this._stdin.on("error", this.noop);
163
+ this._stdout.on("error", this.noop);
164
+
165
+ this._stdout.on("end", onStdoutEnd);
166
+
167
+ this._process.once("close", onExit);
168
+ this._process.once("error", onError);
169
+
170
+ return this;
171
+ }
172
+
173
+ unpipe(dest) {
174
+ return this._reader.unpipe(dest) || this.kill();
175
+ }
176
+
177
+ write(chunk, enc, cb) {
178
+ return this._writer.write(chunk, enc, cb);
179
+ }
180
+
181
+ [util.inspect.custom]() {
182
+ return Base.prototype[util.inspect.custom].call(this);
183
+ }
184
+ }
185
+
186
+ FFmpegDuplex.prototype.addListener = FFmpegDuplex.prototype.on;
187
+
188
+ FFmpegDuplex.spawn = function (connection, args, options) {
189
+ return new FFmpegDuplex(connection, options).spawn(args, options);
190
+ };
191
+
192
+ module.exports = FFmpegDuplex;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ const FFmpegDuplex = require("./FFmpegDuplex");
4
+
5
+ module.exports = function (options = {}) {
6
+ if (!options.command) {
7
+ throw new Error("Invalid converter command");
8
+ }
9
+ if (options.frameDuration === undefined) {
10
+ options.frameDuration = 60;
11
+ }
12
+ let inputArgs = [
13
+ "-analyzeduration", "0",
14
+ "-loglevel", "24",
15
+ ].concat(options.inputArgs || []);
16
+ if (options.format === "pcm") {
17
+ inputArgs = inputArgs.concat(
18
+ "-f", "s16le",
19
+ "-ar", "48000",
20
+ "-ac", "2",
21
+ );
22
+ }
23
+ inputArgs = inputArgs.concat(
24
+ "-i", options.input || "-",
25
+ "-vn",
26
+ );
27
+ const outputArgs = [
28
+ "-c:a", "libopus",
29
+ "-vbr", "on",
30
+ "-frame_duration", "" + options.frameDuration,
31
+ "-f", "ogg",
32
+ "-",
33
+ ];
34
+ return FFmpegDuplex.spawn(options.command, inputArgs.concat(options.encoderArgs || [], outputArgs));
35
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ const FFmpegDuplex = require("./FFmpegDuplex");
4
+
5
+ module.exports = function (options = {}) {
6
+ if (!options.command) {
7
+ throw new Error("Invalid converter command");
8
+ }
9
+ if (options.samplingRate === undefined) {
10
+ options.samplingRate = 48000;
11
+ }
12
+ const inputArgs = [
13
+ "-analyzeduration", "0",
14
+ "-loglevel", "24",
15
+ ].concat(options.inputArgs || [],
16
+ "-i", options.input || "-",
17
+ "-vn",
18
+ );
19
+ const outputArgs = [
20
+ "-f", "s16le",
21
+ "-ar", "" + options.samplingRate,
22
+ "-ac", "2",
23
+ "-",
24
+ ];
25
+ return FFmpegDuplex.spawn(options.command, inputArgs.concat(options.encoderArgs || [], outputArgs));
26
+ };
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+
3
+ const BaseTransformer = require("./BaseTransformer");
4
+
5
+ class OggOpusTransformer extends BaseTransformer {
6
+ constructor(options = {}) {
7
+ super(options);
8
+
9
+ this._remainder = null;
10
+ this._bitstream = null;
11
+ }
12
+
13
+ process(buffer) {
14
+ if (buffer.length - buffer._index <= 26) {
15
+ return true;
16
+ }
17
+
18
+ if (buffer.toString("utf8", buffer._index, buffer._index + 4) !== "OggS") {
19
+ return new Error("Invalid OGG magic string: " + buffer.toString("utf8", buffer._index, buffer._index + 4));
20
+ }
21
+
22
+ const typeFlag = buffer.readUInt8(buffer._index + 5);
23
+ if (typeFlag === 1) {
24
+ return new Error("OGG continued page not supported");
25
+ }
26
+
27
+ const bitstream = buffer.readUInt32BE(buffer._index + 14);
28
+
29
+ buffer._index += 26;
30
+
31
+ const segmentCount = buffer.readUInt8(buffer._index);
32
+ if (buffer.length - buffer._index - 1 < segmentCount) {
33
+ return true;
34
+ }
35
+
36
+ const segments = [];
37
+ let size = 0;
38
+ let byte = 0;
39
+ let total = 0;
40
+ let i = 0;
41
+ for (; i < segmentCount; i++) {
42
+ byte = buffer.readUInt8(++buffer._index);
43
+ if (byte < 255) {
44
+ segments.push(size + byte);
45
+ size = 0;
46
+ } else {
47
+ size += byte;
48
+ }
49
+ total += byte;
50
+ }
51
+
52
+ ++buffer._index;
53
+
54
+ if (buffer.length - buffer._index < total) {
55
+ return true;
56
+ }
57
+
58
+ for (let segment of segments) {
59
+ buffer._index += segment;
60
+ byte = (segment = buffer.subarray(buffer._index - segment, buffer._index)).toString("utf8", 0, 8);
61
+ if (this.head) {
62
+ if (byte === "OpusTags") {
63
+ this.emit("debug", segment.toString());
64
+ } else if (bitstream === this._bitstream) {
65
+ this.push(segment);
66
+ }
67
+ } else if (byte === "OpusHead") {
68
+ this._bitstream = bitstream;
69
+ this.emit("debug", (this.head = segment.toString()));
70
+ } else {
71
+ this.emit("debug", "Invalid codec: " + byte);
72
+ }
73
+ }
74
+ }
75
+
76
+ _final(cb) {
77
+ if (!this._bitstream) {
78
+ this.emit("error", new Error("No Opus stream was found"));
79
+ }
80
+ cb();
81
+ }
82
+
83
+ _transform(chunk, enc, cb) {
84
+ if (this._remainder) {
85
+ chunk = Buffer.concat([this._remainder, chunk]);
86
+ this._remainder = null;
87
+ }
88
+
89
+ chunk._index = 0;
90
+
91
+ while (chunk._index < chunk.length) {
92
+ const offset = chunk._index;
93
+ const ret = this.process(chunk);
94
+ if (ret) {
95
+ this._remainder = chunk.subarray(offset);
96
+ if (ret instanceof Error) {
97
+ this.emit("error", ret);
98
+ }
99
+ cb();
100
+ return;
101
+ }
102
+ }
103
+
104
+ this.setTransformCB(cb);
105
+ }
106
+ }
107
+
108
+ module.exports = OggOpusTransformer;