From 9293c2c6a20ff231cc39eb2338eed4ecf15b320a Mon Sep 17 00:00:00 2001 From: Gregory Hill Date: Fri, 27 Mar 2020 10:49:48 +0000 Subject: [PATCH] add method to stop reading stream once header received Signed-off-by: Gregory Hill --- CHANGELOG.md | 6 ++++++ NOTES.md | 4 ++-- hoard-js/src/examples.ts | 4 ++++ hoard-js/src/index.ts | 14 ++++++++++++++ project/history.go | 4 ++++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2c5ec9c..83cac851 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/NOTES.md b/NOTES.md index e2c7f792..78332c81 100644 --- a/NOTES.md +++ b/NOTES.md @@ -1,3 +1,3 @@ -### Fixed -- Chunk plaintext out if data too big +### Added +- ReadHeader will stop once it gets the head diff --git a/hoard-js/src/examples.ts b/hoard-js/src/examples.ts index 3cd28aa2..577e23c8 100644 --- a/hoard-js/src/examples.ts +++ b/hoard-js/src/examples.ts @@ -21,6 +21,7 @@ import { Write, PlaintextAndGrantSpec, Header, + ReadHeader, } from './index'; import * as assert from 'assert'; @@ -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 = [ diff --git a/hoard-js/src/index.ts b/hoard-js/src/index.ts index 52aefdb0..273b8499 100644 --- a/hoard-js/src/index.ts +++ b/hoard-js/src/index.ts @@ -171,6 +171,20 @@ export async function ReadUntil(accum: T[], stream: grpc.ClientReadableStream }) } +export async function ReadHeader(stream: grpc.ClientReadableStream) { + 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)); diff --git a/project/history.go b/project/history.go index d71cd062..f4f90628 100644 --- a/project/history.go +++ b/project/history.go @@ -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