Skip to content

Commit

Permalink
Merge pull request #110 from gregdhill/read-header
Browse files Browse the repository at this point in the history
add method to stop reading stream once header received
  • Loading branch information
Greg Hill committed Mar 27, 2020
2 parents e87f9ee + 9293c2c commit 042602e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# [Monax Hoard](https://github.com/monax/hoard) Changelog
## [8.2.3] - 2020-03-27
### Added
- ReadHeader will stop once it gets the head


## [8.2.2] - 2020-03-25
### Fixed
- Chunk plaintext out if data too big
Expand Down Expand Up @@ -197,6 +202,7 @@ This is the first Hoard open source release and includes:
- Hoar-Daemon hoard
- Hoar-Control hoarctl CLI

[8.2.3]: https://github.com/monax/hoard/compare/v8.2.2...v8.2.3
[8.2.2]: https://github.com/monax/hoard/compare/v8.2.1...v8.2.2
[8.2.1]: https://github.com/monax/hoard/compare/v8.2.0...v8.2.1
[8.2.0]: https://github.com/monax/hoard/compare/v8.1.0...v8.2.0
Expand Down
4 changes: 2 additions & 2 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
### Fixed
- Chunk plaintext out if data too big
### Added
- ReadHeader will stop once it gets the head

4 changes: 4 additions & 0 deletions hoard-js/src/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Write,
PlaintextAndGrantSpec,
Header,
ReadHeader,
} from './index';
import * as assert from 'assert';

Expand Down Expand Up @@ -103,6 +104,9 @@ export const example = async function (data: string | Uint8Array, salt: string |
plaintext = ReducePlaintext(plaintexts);
assert.deepStrictEqual(plaintext.toObject(), expected.toObject());

let header = await ReadHeader(hoard.unsealGet(grant));
assert.strictEqual(header.getSalt_asB64(), "Zm9v");

// A symmetric grant allows us to encrypt the reference
// through secrets configured on the hoard daemon
ptgs = [
Expand Down
14 changes: 14 additions & 0 deletions hoard-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ export async function ReadUntil<T>(accum: T[], stream: grpc.ClientReadableStream
})
}

export async function ReadHeader(stream: grpc.ClientReadableStream<Plaintext>) {
return new Promise<Header>((resolve, reject) => {
stream.on('data', (data: Plaintext) => {
if (data.hasHead()) {
stream.cancel();
resolve(data.getHead());
}
});
stream.on('error', (err: { code: grpc.status; }) => err.code === grpc.status.CANCELLED ? resolve() : reject(err));
stream.on('close', () => reject("no header found"));
stream.on('end', () => reject("no header found"));
})
}

export async function Write<A, B>(input: A[], fn: (callback: grpc.requestCallback<B>) => grpc.ClientWritableStream<A>): Promise<B> {
return new Promise((resolve, reject) => {
let stream = fn((err, grt) => err ? reject(err) : resolve(grt));
Expand Down
4 changes: 4 additions & 0 deletions project/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func FullVersion() string {
// release tagging script: ./scripts/tag_release.sh
var History relic.ImmutableHistory = relic.NewHistory("Monax Hoard", "https://github.com/monax/hoard").
MustDeclareReleases(
"8.2.3 - 2020-03-27",
`### Added
- ReadHeader will stop once it gets the head
`,
"8.2.2 - 2020-03-25",
`### Fixed
- Chunk plaintext out if data too big
Expand Down

0 comments on commit 042602e

Please sign in to comment.