Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* chore: node 22 prebuilts

* fix: CVE-2024-21521 and any future attempts for these

* chore: fix destructor
  • Loading branch information
vladfrangu authored Jul 11, 2024
1 parent b541c5b commit 8f52fa0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
fail-fast: false
matrix:
os: [macos-11, macos-12, macos-13, ubuntu-20.04, ubuntu-22.04, windows-2019]
node: [18, 20, 21]
node: [18, 20, 21, 22]
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -68,7 +68,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
steps:
- name: Setup env with Node v${{ matrix.node }}
run: |
Expand Down Expand Up @@ -116,7 +116,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-20.04, ubuntu-22.04]
node: [18, 20, 21]
node: [18, 20, 21, 22]
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -142,7 +142,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
steps:
- name: Setup env with Node v${{ matrix.node }}
run: |
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
fail-fast: false
matrix:
os: [macos-11, macos-12, macos-13, ubuntu-20.04, ubuntu-22.04, windows-2019]
node: [18, 20, 21]
node: [18, 20, 21, 22]
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
steps:
- name: Setup env with Node v${{ matrix.node }}
run: |
Expand Down Expand Up @@ -138,7 +138,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-20.04, ubuntu-22.04]
node: [18, 20, 21]
node: [18, 20, 21, 22]
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down Expand Up @@ -171,7 +171,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
steps:
- name: Setup env with Node v${{ matrix.node }}
run: |
Expand Down
17 changes: 10 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"dependencies": {
"@discordjs/node-pre-gyp": "^0.4.5",
"node-addon-api": "^5.0.0"
"node-addon-api": "^8.1.0"
},
"devDependencies": {
"@types/node": "^18.11.2",
Expand Down
60 changes: 59 additions & 1 deletion src/node-opus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ Object OpusEncoder::Init(Napi::Env env, Object exports) {
OpusEncoder::OpusEncoder(const CallbackInfo& args): ObjectWrap<OpusEncoder>(args) {
this->encoder = nullptr;
this->decoder = nullptr;
this->outPcm = nullptr;

if (args.Length() < 2) {
Napi::RangeError::New(args.Env(), "Expected 2 arguments").ThrowAsJavaScriptException();
return;
}

if (!args[0].IsNumber() || !args[1].IsNumber()) {
Napi::TypeError::New(args.Env(), "Expected rate and channels to be numbers").ThrowAsJavaScriptException();
return;
}

this->rate = args[0].ToNumber().Int32Value();
this->channels = args[1].ToNumber().Int32Value();
this->application = OPUS_APPLICATION_AUDIO;
Expand All @@ -57,7 +69,7 @@ OpusEncoder::~OpusEncoder() {
this->encoder = nullptr;
this->decoder = nullptr;

delete this->outPcm;
if (this->outPcm) delete this->outPcm;
this->outPcm = nullptr;
}

Expand Down Expand Up @@ -87,6 +99,11 @@ Napi::Value OpusEncoder::Encode(const CallbackInfo& args) {
return env.Null();
}

if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected 1 argument").ThrowAsJavaScriptException();
return env.Null();
}

if (!args[0].IsBuffer()) {
Napi::TypeError::New(env, "Provided input needs to be a buffer").ThrowAsJavaScriptException();
return env.Null();
Expand All @@ -102,11 +119,19 @@ Napi::Value OpusEncoder::Encode(const CallbackInfo& args) {
Buffer<char> actualBuf = Buffer<char>::Copy(env, reinterpret_cast<char*>(this->outOpus), compressedLength);

if (!actualBuf.IsEmpty()) return actualBuf;

Napi::Error::New(env, "Could not encode the data").ThrowAsJavaScriptException();
return env.Null();
}

Napi::Value OpusEncoder::Decode(const CallbackInfo& args) {
Napi::Env env = args.Env();

if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected 1 argument").ThrowAsJavaScriptException();
return env.Null();
}

if (!args[0].IsBuffer()) {
Napi::TypeError::New(env, "Provided input needs to be a buffer").ThrowAsJavaScriptException();
return env.Null();
Expand Down Expand Up @@ -140,11 +165,24 @@ Napi::Value OpusEncoder::Decode(const CallbackInfo& args) {
Buffer<char> actualBuf = Buffer<char>::Copy(env, reinterpret_cast<char*>(this->outPcm), decodedLength);

if (!actualBuf.IsEmpty()) return actualBuf;

Napi::Error::New(env, "Could not decode the data").ThrowAsJavaScriptException();
return env.Null();
}

void OpusEncoder::ApplyEncoderCTL(const CallbackInfo& args) {
Napi::Env env = args.Env();

if (args.Length() < 2) {
Napi::RangeError::New(env, "Expected 2 arguments").ThrowAsJavaScriptException();
return;
}

if (!args[0].IsNumber() || !args[1].IsNumber()) {
Napi::TypeError::New(env, "Expected ctl and value to be numbers").ThrowAsJavaScriptException();
return;
}

int ctl = args[0].ToNumber().Int32Value();
int value = args[1].ToNumber().Int32Value();

Expand All @@ -162,6 +200,16 @@ void OpusEncoder::ApplyEncoderCTL(const CallbackInfo& args) {
void OpusEncoder::ApplyDecoderCTL(const CallbackInfo& args) {
Napi::Env env = args.Env();

if (args.Length() < 2) {
Napi::RangeError::New(env, "Expected 2 arguments").ThrowAsJavaScriptException();
return;
}

if (!args[0].IsNumber() || !args[1].IsNumber()) {
Napi::TypeError::New(env, "Expected ctl and value to be numbers").ThrowAsJavaScriptException();
return;
}

int ctl = args[0].ToNumber().Int32Value();
int value = args[1].ToNumber().Int32Value();

Expand All @@ -179,6 +227,16 @@ void OpusEncoder::ApplyDecoderCTL(const CallbackInfo& args) {
void OpusEncoder::SetBitrate(const CallbackInfo& args) {
Napi::Env env = args.Env();

if (args.Length() < 1) {
Napi::RangeError::New(env, "Expected 1 argument").ThrowAsJavaScriptException();
return;
}

if (!args[0].IsNumber()) {
Napi::TypeError::New(env, "Expected bitrate to be a number").ThrowAsJavaScriptException();
return;
}

int bitrate = args[0].ToNumber().Int32Value();

if (this->EnsureEncoder() != OPUS_OK) {
Expand Down

0 comments on commit 8f52fa0

Please sign in to comment.