Skip to content

Commit

Permalink
feat: convertStreamToString (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvolek authored Jun 12, 2023
1 parent 5ed6e16 commit cf41c25
Show file tree
Hide file tree
Showing 19 changed files with 514 additions and 6 deletions.
117 changes: 117 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `convertStreamToString` function
- pm2 metrics

## 2.5.0 - 2023-05-17
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"fastify": "^4.10.2",
"prettier": "2.6.2",
"rimraf": "^3.0.2",
"sinon": "^15.1.0",
"typescript": "^4.7.4",
"vite": "^3.0.4",
"vitest": "^0.21.0"
Expand Down
27 changes: 27 additions & 0 deletions src/fastify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FastifyError, FastifyRequest, FastifyReply } from 'fastify';
import stream from 'stream';

type FastifyErrorWithStatusCode = FastifyError & {
statusCode: NonNullable<FastifyError['statusCode']>;
Expand Down Expand Up @@ -253,3 +254,29 @@ export const handleInvalidXpubIndex = (reply: FastifyReply) => {
export const handleInvalidXpubRole = (reply: FastifyReply) => {
return handle400Custom(reply, 'Missing, out of range or malformed role.');
};

export const convertStreamToString = async (payloadStream: unknown) => {
const payload = await new Promise<string>((resolve, reject) => {
if (payloadStream instanceof stream.Readable) {
// receive error response from the payload stream
const chunks: Buffer[] = [];

payloadStream.on('data', chunk => {
chunks.push(chunk);
});
payloadStream.on('error', error => {
reject(error);
});
payloadStream.on('end', () => {
resolve(Buffer.concat(chunks).toString());
});
} else if (typeof payloadStream === 'string') {
// If payloadStream is just a string then return it
resolve(payloadStream);
} else {
reject(new Error(`Invalid payload type: ${typeof payload}`));
}
});

return payload;
};
24 changes: 24 additions & 0 deletions test/tests/cardano.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect, describe, test } from 'vitest';
import * as cardanoUtils from '../../src/cardano';
import stream from 'stream';
import * as fastifyUtils from '../../src/fastify';
import * as fixtures from '../fixtures/cardano';

describe('cardano utils', () => {
Expand All @@ -16,3 +18,25 @@ describe('cardano utils', () => {
});
});
});

describe('fastify utils', () => {
test('returns a string from a readable stream', async () => {
const payload = 'hello world';
const payloadStream = new stream.Readable({
read() {
this.push(payload);
this.push(null);
},
});
const result = await fastifyUtils.convertStreamToString(payloadStream);

expect(result).toBe(payload);
});

test('returns the original string payload', async () => {
const payload = 'hello world';
const result = await fastifyUtils.convertStreamToString(payload);

expect(result).toBe(payload);
});
});
Loading

0 comments on commit cf41c25

Please sign in to comment.