Skip to content

Commit

Permalink
made graphql file uploads work on node 14 (#726)
Browse files Browse the repository at this point in the history
* made graphql file uploads work on node 14

* lint
  • Loading branch information
rmgraham committed Nov 23, 2020
1 parent f68d4e1 commit 06529f2
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 28 deletions.
24 changes: 12 additions & 12 deletions app/apollo/resolvers/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const GraphqlFields = require('graphql-fields');
const conf = require('../../conf.js').conf;
const S3ClientClass = require('../../s3/s3Client');
const { WritableStreamBuffer } = require('stream-buffers');
const streamToString = require('stream-to-string');
const stream = require('stream');
const { applyQueryFieldsToChannels } = require('../utils/applyQueryFields');

const yaml = require('js-yaml');
const fs = require('fs');

const { ACTIONS, TYPES, CHANNEL_VERSION_YAML_MAX_SIZE_LIMIT_MB, CHANNEL_LIMITS, CHANNEL_VERSION_LIMITS } = require('../models/const');
const { whoIs, validAuth, getAllowedChannels, NotFoundError, RazeeValidationError, BasicRazeeError, RazeeQueryError} = require ('./common');
Expand Down Expand Up @@ -93,12 +93,12 @@ const channelResolvers = {
logger.debug({req_id, user: whoIs(me), org_id, channelName, versionName }, `${queryName} enter`);
return await channelResolvers.Query.channelVersion(parent, {orgId: org_id, channelName, versionName, _queryName: queryName }, context);
},

channelVersion: async(parent, { orgId: org_id, channelUuid, versionUuid, channelName, versionName, _queryName }, context) => {
const { models, me, req_id, logger } = context;
const queryName = _queryName ? `${_queryName}/channelVersion` : 'channelVersion';
logger.debug({req_id, user: whoIs(me), org_id, channelUuid, versionUuid, channelName, versionName}, `${queryName} enter`);

try{

const org = await models.Organization.findOne({ _id: org_id });
Expand All @@ -112,7 +112,7 @@ const channelResolvers = {
const channel = await models.Channel.findOne(channelFilter);
if(!channel){
throw new NotFoundError(`Could not find the channel with uuid/name ${channel_uuid}/channelName.`, context);
}
}
await validAuth(me, org_id, ACTIONS.READ, TYPES.CHANNEL, queryName, context, [channel.uuid, channel.name]);
const channel_uuid = channel.uuid; // in case query by channelName, populate channel_uuid

Expand Down Expand Up @@ -140,7 +140,7 @@ const channelResolvers = {
var path = `${parts.join('/')}`;

const s3Client = new S3ClientClass(conf);
deployableVersionObj.content = await s3Client.getAndDecryptFile(bucketName, path, orgKey, deployableVersionObj.iv);
deployableVersionObj.content = await s3Client.getAndDecryptFile(bucketName, decodeURIComponent(path), orgKey, deployableVersionObj.iv);
}
else {
throw new BasicRazeeError(`versionObj.location="${versionObj.location}" not implemented yet`, context);
Expand All @@ -149,7 +149,7 @@ const channelResolvers = {
}catch(err){
logger.error(err, `${queryName} encountered an error when serving ${req_id}.`);
throw new RazeeQueryError(`Query ${queryName} error. ${err.message}`, context);
}
}
}
},
Mutation: {
Expand All @@ -162,10 +162,10 @@ const channelResolvers = {
try {
// might not necessary with uunique index. Worth to check to return error better.
const channel = await models.Channel.findOne({ name, org_id });
if(channel){
if(channel){
throw new RazeeValidationError(`The channel name ${name} already exists.`, context);
}

// validate the number of total channels are under the limit
const total = await models.Channel.count({org_id});
if (total >= CHANNEL_LIMITS.MAX_TOTAL ) {
Expand Down Expand Up @@ -236,7 +236,7 @@ const channelResolvers = {
throw new RazeeValidationError('A "name" must be specified', context);
}
if(!type || type !== 'yaml' && type !== 'application/yaml'){
throw new RazeeValidationError('A "type" of application/yaml must be specified', context);
throw new RazeeValidationError('A "type" of application/yaml must be specified', context);
}
if(!channel_uuid){
throw new RazeeValidationError('A "channel_uuid" must be specified', context);
Expand Down Expand Up @@ -269,7 +269,7 @@ const channelResolvers = {
try {
if(file){
var tempFileStream = (await file).createReadStream();
content = await fs.promises.readFile(tempFileStream.path, 'utf8');
content = await streamToString(tempFileStream);
}
let yamlSize = Buffer.byteLength(content);
if(yamlSize > CHANNEL_VERSION_YAML_MAX_SIZE_LIMIT_MB * 1024 * 1024){
Expand Down Expand Up @@ -357,7 +357,7 @@ const channelResolvers = {
const { models, me, req_id, logger } = context;
const queryName = 'removeChannel';
logger.debug({ req_id, user: whoIs(me), org_id, uuid }, `${queryName} enter`);

try{
const channel = await models.Channel.findOne({ uuid, org_id });
if(!channel){
Expand Down Expand Up @@ -421,7 +421,7 @@ const channelResolvers = {
await s3Client.deleteObject(bucketName, path);
}
await models.DeployableVersion.deleteOne({ org_id, uuid});

const versionObjs = channel.versions;
const vIndex = versionObjs.findIndex(v => v.uuid === uuid);
versionObjs.splice(vIndex, 1);
Expand Down
2 changes: 1 addition & 1 deletion app/s3/s3Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module.exports = class S3Client {
get endpoint() {
return this._conf.endpoint;
}

get log() {
const nop = {
error: () => {},
Expand Down
115 changes: 101 additions & 14 deletions package-lock.json

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

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
"dockerlint": "npx dockerlint Dockerfile",
"markdownlint": "npx markdownlint README.md docs/",
"jsonlint": "npx jsonlint --quiet build/viewTemplate.json",
"shellcheck": "npx shellcheck build/*.sh"
"shellcheck": "npx shellcheck build/*.sh",
"preinstall": "npx npm-force-resolutions"
},
"keywords": [],
"author": "",
"contributors": [],
"resolutions": {
"graphql-upload": "^11.0.0",
"fs-capacitor": "^6.2.0"
},
"dependencies": {
"apollo-metrics": "^1.0.1",
"apollo-server": "^2.18.2",
Expand Down Expand Up @@ -75,13 +80,15 @@
"mongoose": "^5.10.11",
"mongoose-lean-virtuals": "^0.7.2",
"mustache": "^4.0.1",
"npm-force-resolutions": "0.0.3",
"object-hash": "^2.0.3",
"object-path": "^0.11.5",
"p-limit": "^3.0.2",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"prom-client": "^12.0.0",
"stream-buffers": "^3.0.2",
"stream-to-string": "^1.2.0",
"subscriptions-transport-ws": "^0.9.18",
"swagger-ui-express": "^4.1.4",
"uuid": "^8.3.1",
Expand Down

0 comments on commit 06529f2

Please sign in to comment.