Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ namespace nbs {

uint32_t subtype = 0;
if (jsObject.Has("subtype")) {
subtype = jsObject.Get("subtype").As<Napi::Number>().Uint32Value();
if (jsObject.Get("subtype").IsNumber()) {
subtype = jsObject.Get("subtype").As<Napi::Number>().Uint32Value();
}
else if (!jsObject.Get("subtype").IsUndefined()) {
throw std::runtime_error("expected `subtype` to be a number");
}
}

// Check types are valid
uint64_t timestamp = 0;
try {
timestamp = timestamp::FromJsValue(jsObject.Get("timestamp"), env);
Expand All @@ -46,9 +50,6 @@ namespace nbs {
throw std::runtime_error(std::string("error in `type`: ") + ex.what());
}

if (!jsObject.Get("subtype").IsNumber()) {
throw std::runtime_error("expected `subtype` to be number");
}
if (!jsObject.Get("payload").IsBuffer()) {
throw std::runtime_error("expected `payload` to be buffer object");
}
Expand Down
13 changes: 13 additions & 0 deletions tests/test_encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ test('NbsEncoder.write() throws for invalid arguments', () => {
'NbsEncoder.write() throws with incorrect types for timestamp properties'
);

assert.throws(
() => {
encoder.write({
timestamp: { seconds: 1897, nanos: 0 },
type: pingType,
subtype: 'string',
payload: Buffer.from('ping.699', 'utf8'),
});
},
/invalid type for argument `packet`: expected `subtype` to be a number/,
'NbsEncoder.write() throws with incorrect type for subtype'
);

encoder.close();
});
});
Expand Down