@@ -44,6 +44,18 @@ Object OpusEncoder::Init(Napi::Env env, Object exports) {
4444OpusEncoder::OpusEncoder (const CallbackInfo& args): ObjectWrap<OpusEncoder>(args) {
4545 this ->encoder = nullptr ;
4646 this ->decoder = nullptr ;
47+ this ->outPcm = nullptr ;
48+
49+ if (args.Length () < 2 ) {
50+ Napi::RangeError::New (args.Env (), " Expected 2 arguments" ).ThrowAsJavaScriptException ();
51+ return ;
52+ }
53+
54+ if (!args[0 ].IsNumber () || !args[1 ].IsNumber ()) {
55+ Napi::TypeError::New (args.Env (), " Expected rate and channels to be numbers" ).ThrowAsJavaScriptException ();
56+ return ;
57+ }
58+
4759 this ->rate = args[0 ].ToNumber ().Int32Value ();
4860 this ->channels = args[1 ].ToNumber ().Int32Value ();
4961 this ->application = OPUS_APPLICATION_AUDIO;
@@ -57,7 +69,7 @@ OpusEncoder::~OpusEncoder() {
5769 this ->encoder = nullptr ;
5870 this ->decoder = nullptr ;
5971
60- delete this ->outPcm ;
72+ if ( this -> outPcm ) delete this ->outPcm ;
6173 this ->outPcm = nullptr ;
6274}
6375
@@ -87,6 +99,11 @@ Napi::Value OpusEncoder::Encode(const CallbackInfo& args) {
8799 return env.Null ();
88100 }
89101
102+ if (args.Length () < 1 ) {
103+ Napi::RangeError::New (env, " Expected 1 argument" ).ThrowAsJavaScriptException ();
104+ return env.Null ();
105+ }
106+
90107 if (!args[0 ].IsBuffer ()) {
91108 Napi::TypeError::New (env, " Provided input needs to be a buffer" ).ThrowAsJavaScriptException ();
92109 return env.Null ();
@@ -102,11 +119,19 @@ Napi::Value OpusEncoder::Encode(const CallbackInfo& args) {
102119 Buffer<char > actualBuf = Buffer<char >::Copy (env, reinterpret_cast <char *>(this ->outOpus ), compressedLength);
103120
104121 if (!actualBuf.IsEmpty ()) return actualBuf;
122+
123+ Napi::Error::New (env, " Could not encode the data" ).ThrowAsJavaScriptException ();
124+ return env.Null ();
105125}
106126
107127Napi::Value OpusEncoder::Decode (const CallbackInfo& args) {
108128 Napi::Env env = args.Env ();
109129
130+ if (args.Length () < 1 ) {
131+ Napi::RangeError::New (env, " Expected 1 argument" ).ThrowAsJavaScriptException ();
132+ return env.Null ();
133+ }
134+
110135 if (!args[0 ].IsBuffer ()) {
111136 Napi::TypeError::New (env, " Provided input needs to be a buffer" ).ThrowAsJavaScriptException ();
112137 return env.Null ();
@@ -140,11 +165,24 @@ Napi::Value OpusEncoder::Decode(const CallbackInfo& args) {
140165 Buffer<char > actualBuf = Buffer<char >::Copy (env, reinterpret_cast <char *>(this ->outPcm ), decodedLength);
141166
142167 if (!actualBuf.IsEmpty ()) return actualBuf;
168+
169+ Napi::Error::New (env, " Could not decode the data" ).ThrowAsJavaScriptException ();
170+ return env.Null ();
143171}
144172
145173void OpusEncoder::ApplyEncoderCTL (const CallbackInfo& args) {
146174 Napi::Env env = args.Env ();
147175
176+ if (args.Length () < 2 ) {
177+ Napi::RangeError::New (env, " Expected 2 arguments" ).ThrowAsJavaScriptException ();
178+ return ;
179+ }
180+
181+ if (!args[0 ].IsNumber () || !args[1 ].IsNumber ()) {
182+ Napi::TypeError::New (env, " Expected ctl and value to be numbers" ).ThrowAsJavaScriptException ();
183+ return ;
184+ }
185+
148186 int ctl = args[0 ].ToNumber ().Int32Value ();
149187 int value = args[1 ].ToNumber ().Int32Value ();
150188
@@ -162,6 +200,16 @@ void OpusEncoder::ApplyEncoderCTL(const CallbackInfo& args) {
162200void OpusEncoder::ApplyDecoderCTL (const CallbackInfo& args) {
163201 Napi::Env env = args.Env ();
164202
203+ if (args.Length () < 2 ) {
204+ Napi::RangeError::New (env, " Expected 2 arguments" ).ThrowAsJavaScriptException ();
205+ return ;
206+ }
207+
208+ if (!args[0 ].IsNumber () || !args[1 ].IsNumber ()) {
209+ Napi::TypeError::New (env, " Expected ctl and value to be numbers" ).ThrowAsJavaScriptException ();
210+ return ;
211+ }
212+
165213 int ctl = args[0 ].ToNumber ().Int32Value ();
166214 int value = args[1 ].ToNumber ().Int32Value ();
167215
@@ -179,6 +227,16 @@ void OpusEncoder::ApplyDecoderCTL(const CallbackInfo& args) {
179227void OpusEncoder::SetBitrate (const CallbackInfo& args) {
180228 Napi::Env env = args.Env ();
181229
230+ if (args.Length () < 1 ) {
231+ Napi::RangeError::New (env, " Expected 1 argument" ).ThrowAsJavaScriptException ();
232+ return ;
233+ }
234+
235+ if (!args[0 ].IsNumber ()) {
236+ Napi::TypeError::New (env, " Expected bitrate to be a number" ).ThrowAsJavaScriptException ();
237+ return ;
238+ }
239+
182240 int bitrate = args[0 ].ToNumber ().Int32Value ();
183241
184242 if (this ->EnsureEncoder () != OPUS_OK) {
0 commit comments