-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blob #71
Merged
Merged
Blob #71
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
00fe977
First attempt at implementing Blob
aaroncox d6bed6d
Returning Blob from API call and allowing ABI to use it
aaroncox a0684a6
Removed Blob from builtins
aaroncox 0b08402
Fix for non-builtin
aaroncox eb9420b
Renamed methods
aaroncox 1cd6b9b
Tests against nodeos weirdness
aaroncox 03cad98
Removed unused import
aaroncox c5a42ab
Removed Buffer and used atob and btoa
aaroncox a8526b7
Adding checks for Buffer to maintain nodejs v14 support
aaroncox c0028ec
Fixed comment header on from
aaroncox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,88 @@ | ||
import {ABISerializableObject} from '../serializer/serializable' | ||
import {ABIEncoder} from '../serializer/encoder' | ||
import {arrayEquals, isInstanceOf} from '../utils' | ||
|
||
export type BlobType = Blob | string | ||
|
||
export class Blob implements ABISerializableObject { | ||
static abiName = 'blob' | ||
|
||
/** | ||
* Create a new Bytes instance. | ||
* @note Make sure to take a [[copy]] before mutating the bytes as the underlying source is not copied here. | ||
*/ | ||
static from(value: BlobType): Blob { | ||
if (isInstanceOf(value, this)) { | ||
return value | ||
} | ||
if (typeof value === 'string') { | ||
return this.fromString(value) | ||
} | ||
throw new Error('Invalid blob') | ||
} | ||
|
||
static fromString(value: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to handle the invalid base64 padding nodeos outputs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I have tests ensuring the invalid stuff works here: 1cd6b9b |
||
// If buffer is available, use it (maintains support for nodejs 14) | ||
if (typeof Buffer === 'function') { | ||
return new this(new Uint8Array(Buffer.from(value, 'base64'))) | ||
} | ||
// fix up base64 padding from nodeos | ||
switch (value.length % 4) { | ||
case 2: | ||
value += '==' | ||
break | ||
case 3: | ||
value += '=' | ||
break | ||
case 1: | ||
value = value.substring(0, value.length - 1) | ||
break | ||
} | ||
const string = atob(value) | ||
const array = new Uint8Array(string.length) | ||
for (let i = 0; i < string.length; i++) { | ||
array[i] = string.charCodeAt(i) | ||
} | ||
return new this(array) | ||
} | ||
|
||
readonly array: Uint8Array | ||
|
||
constructor(array: Uint8Array) { | ||
this.array = array | ||
} | ||
|
||
equals(other: BlobType): boolean { | ||
const self = this.constructor as typeof Blob | ||
try { | ||
return arrayEquals(this.array, self.from(other).array) | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
get base64String(): string { | ||
// If buffer is available, use it (maintains support for nodejs 14) | ||
if (typeof Buffer === 'function') { | ||
return Buffer.from(this.array).toString('base64') | ||
} | ||
return btoa(this.utf8String) | ||
} | ||
|
||
/** UTF-8 string representation of this instance. */ | ||
get utf8String(): string { | ||
return new TextDecoder().decode(this.array) | ||
} | ||
|
||
toABI(encoder: ABIEncoder) { | ||
encoder.writeArray(this.array) | ||
} | ||
|
||
toString() { | ||
return this.base64String | ||
} | ||
|
||
toJSON() { | ||
return this.toString() | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring says Bytes and references non existent method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bytes
is where I copied the class structure from instead of writing it from scratch... my sloppy copy/pasta. Whoops 😅