-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Correctly report error codes Classical off-by-one mistake. We incorrectly reported error codes as 'undefined' when doing "new ThemisError(subsystem, errorCode)". This affects only human-readable description, not the 'errorCode' property, but it's very annoying for developers. Use ThemisError.describe only when there are 3 (three) or more arguments with human-readable error causes. * Move 'coerceToBytes' into utility module This function is going to be useful in multiple places for type checking of user byte arrays. Let's move it into a common utility module. While we're here, let it throw 'TypeError' instead of 'ThemisError' which should be more recognizable (and does not expect Themis "subsystem").
- Loading branch information
Showing
3 changed files
with
37 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2019 Cossack Labs Limited | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file | ||
* Miscellaneous utilities. | ||
*/ | ||
|
||
/** | ||
* Convert an object into a byte buffer. | ||
* | ||
* @throws TypeError if input type is not supported | ||
*/ | ||
module.exports.coerceToBytes = function(buffer) { | ||
if (buffer instanceof Uint8Array) { | ||
return buffer | ||
} | ||
if (buffer instanceof ArrayBuffer) { | ||
return new Uint8Array(buffer) | ||
} | ||
throw new TypeError('type mismatch, expect "Uint8Array" or "ArrayBuffer"') | ||
} |