Skip to content
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

INTEGRATION [PR#1858 > development/8.1] ARSN-183 type check stream #1859

Merged
merged 6 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * as constants from './lib/constants';
export * as https from './lib/https';
export * as metrics from './lib/metrics';
export * as network from './lib/network';
export * as stream from './lib/stream';

export const db = require('./lib/db');
export const errorUtils = require('./lib/errorUtils');
Expand Down Expand Up @@ -189,10 +190,6 @@ export const pensieve = {
credentialUtils: require('./lib/executables/pensieveCreds/utils'),
};

export const stream = {
readJSONStreamObject: require('./lib/stream/readJSONStreamObject'),
};

export const patches = {
locationConstraints: require('./lib/patches/locationConstraints'),
};
1 change: 1 addition & 0 deletions lib/stream/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as readJSONStreamObject } from './readJSONStreamObject';
38 changes: 0 additions & 38 deletions lib/stream/readJSONStreamObject.js

This file was deleted.

42 changes: 42 additions & 0 deletions lib/stream/readJSONStreamObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import errors from '../errors';
import * as stream from 'stream';
import joi from 'joi';

/**
* read a JSON object from a stream returned as a javascript object,
* handle errors.
*
* @param s - Readable stream
* @param [joiSchema] - optional validation schema for the JSON object
* @return a Promise resolved with the parsed JSON object as a result
*/
export default async function readJSONStreamObject<Data = any>(
s: stream.Readable,
joiSchema?: joi.Schema<Data>
): Promise<Data> {
return new Promise((resolve, reject) => {
const contentsChunks: any = [];
s.on('data', (chunk) => contentsChunks.push(chunk));
s.on('end', () => {
const contents = contentsChunks.join('');
try {
const parsedContents = JSON.parse(contents);
if (joiSchema) {
const { error, value } = joiSchema.validate(parsedContents);
if (error) {
throw error;
}
return resolve(value);
}
return resolve(parsedContents);
} catch (err: any) {
return reject(
errors.InvalidArgument.customizeDescription(
`invalid input: ${err.message}`
)
);
}
});
s.once('error', reject);
});
}
2 changes: 1 addition & 1 deletion tests/unit/stream/readJSONStreamObject.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert');
const stream = require('stream');
const joi = require('joi');
const readJSONStreamObject = require('../../../lib/stream/readJSONStreamObject');
const readJSONStreamObject = require('../../../lib/stream/readJSONStreamObject').default;

class ReqStream extends stream.Readable {
constructor(contents) {
Expand Down