-
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.
Node.js native extensions and V8 are hard. Initially it was expected that JsThemis has an interface similar to WasmThemis. Something like this: class SymmetricKey extends Buffer { constructor(array) { if (array) { super(array) } else { super(generateNewKey()) } } } Note SymmetricKey is a class which inherits from Buffer. Now consider the fact that JavaScript does not *really* have classes, it has objects, functions, and prototypes, with EcmaScript 6 classes being a nice syntax sugar on top of that. It turned out to be not obvious how to implement JavaScript inheritance from native JavaScript classes given the API provided by V8 (and I'm not even talking about the outstanding quality of V8 documentation /s). This is further complicated by the fact that all of the Buffer's constructors are considered deprecated, with Buffer.alloc() and Buffer.from() being the recommended way of constructing Buffers. I'm in a bit of a loss here so effectively SymmetricKey is now function SymmetricKey(array) { if (array) { return Buffer.from(array) } else { return generateKeyBuffer() } } This kinda works on the API level because classes are functions in JavaScript, but it's not strictly correct as "new SymmetricKey()" expression returns an instance of Buffer (not SymmetricKey). It's fine since that's all API that we need at the moment, but it's not clean. However, after spending around 4 hours trying to understand how to do inheritance, I kinda gave up. This should be enough for practical purposes for now. We will have to get back to this issue if we would like to provide our own wrapper over Buffer instances for all other keys, but for now it's good enough. *fingers crossed*
- Loading branch information
Showing
5 changed files
with
154 additions
and
3 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
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