diff --git a/package-lock.json b/package-lock.json index 957ac02e89..49bb0f9e17 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12575,7 +12575,8 @@ }, "kind-of": { "version": "6.0.2", - "resolved": "" + "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", + "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==" } } }, @@ -19274,7 +19275,8 @@ }, "ini": { "version": "1.3.5", - "resolved": "", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", + "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==", "optional": true }, "is-fullwidth-code-point": { @@ -35575,21 +35577,11 @@ } } }, - "request-promise": { - "version": "4.2.5", - "resolved": "https://registry.npmjs.org/request-promise/-/request-promise-4.2.5.tgz", - "integrity": "sha512-ZgnepCykFdmpq86fKGwqntyTiUrHycALuGggpyCZwMvGaZWgxW6yagT0FHkgo5LzYvOaCNvxYwWYIjevSH1EDg==", - "requires": { - "bluebird": "^3.5.0", - "request-promise-core": "1.1.3", - "stealthy-require": "^1.1.1", - "tough-cookie": "^2.3.3" - } - }, "request-promise-core": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.3.tgz", "integrity": "sha512-QIs2+ArIGQVp5ZYbWD5ZLCY29D5CfWizP8eWnm8FoGD1TX61veauETVQbrV60662V0oFBkrDOuaBI8XgtuyYAQ==", + "dev": true, "requires": { "lodash": "^4.17.15" } @@ -36951,7 +36943,8 @@ }, "kind-of": { "version": "6.0.2", - "resolved": "" + "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", + "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==" } } }, @@ -37223,7 +37216,8 @@ "stealthy-require": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz", - "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=" + "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=", + "dev": true }, "store2": { "version": "2.11.1", diff --git a/package.json b/package.json index ba8fa0991e..29ef1c35a5 100644 --- a/package.json +++ b/package.json @@ -216,8 +216,6 @@ "redux-devtools-dock-monitor": "^1.1.3", "redux-devtools-log-monitor": "^1.4.0", "redux-thunk": "^2.3.0", - "request": "^2.88.2", - "request-promise": "^4.2.5", "reselect": "^4.0.0", "s3-policy-v4": "0.0.3", "sass-extract": "^2.1.0", diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 8066604401..5334828256 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -3,7 +3,7 @@ import format from 'date-fns/format'; import isUrl from 'is-url'; import jsdom, { serializeDocument } from 'jsdom'; import isAfter from 'date-fns/isAfter'; -import request from 'request'; +import axios from 'axios'; import slugify from 'slugify'; import Project from '../models/project'; import User from '../models/user'; @@ -125,7 +125,7 @@ export function getProjectsForUserId(userId) { export function getProjectAsset(req, res) { Project.findById(req.params.project_id) .populate('user', 'username') - .exec((err, project) => { // eslint-disable-line + .exec(async (err, project) => { // eslint-disable-line if (err) { return res .status(404) @@ -145,15 +145,15 @@ export function getProjectAsset(req, res) { if (!resolvedFile.url) { return res.send(resolvedFile.content); } - request( - { method: 'GET', url: resolvedFile.url, encoding: null }, - (innerErr, response, body) => { - if (innerErr) { - return res.status(404).send({ message: 'Asset does not exist' }); - } - return res.send(body); - } - ); + + try { + const { data } = await axios.get(resolvedFile.url, { + responseType: 'arraybuffer' + }); + res.send(data); + } catch (error) { + res.status(404).send({ message: 'Asset does not exist' }); + } }); } @@ -198,7 +198,7 @@ function bundleExternalLibs(project, zip, callback) { let numScriptsResolved = 0; let numScriptTags = 0; - function resolveScriptTagSrc(scriptTag, document) { + async function resolveScriptTagSrc(scriptTag, document) { const path = scriptTag.src.split('/'); const filename = path[path.length - 1]; const { src } = scriptTag; @@ -212,23 +212,21 @@ function bundleExternalLibs(project, zip, callback) { return; } - request( - { method: 'GET', url: src, encoding: null }, - (err, response, body) => { - if (err) { - console.log(err); - } else { - zip.append(body, { name: filename }); - scriptTag.src = filename; - } + try { + const { data } = await axios.get(src, { + responseType: 'arraybuffer' + }); + zip.append(data, { name: filename }); + scriptTag.src = filename; + } catch (err) { + console.log(err); + } - numScriptsResolved += 1; - if (numScriptsResolved === numScriptTags) { - indexHtml.content = serializeDocument(document); - callback(); - } - } - ); + numScriptsResolved += 1; + if (numScriptsResolved === numScriptTags) { + indexHtml.content = serializeDocument(document); + callback(); + } } jsdom.env(indexHtml.content, (innerErr, window) => { @@ -264,7 +262,7 @@ function buildZip(project, req, res) { ); zip.pipe(res); - function addFileToZip(file, path) { + async function addFileToZip(file, path) { if (file.fileType === 'folder') { const newPath = file.name === 'root' ? path : `${path}${file.name}/`; file.children.forEach((fileId) => { @@ -274,16 +272,18 @@ function buildZip(project, req, res) { })(); }); } else if (file.url) { - request( - { method: 'GET', url: file.url, encoding: null }, - (err, response, body) => { - zip.append(body, { name: `${path}${file.name}` }); - numCompletedFiles += 1; - if (numCompletedFiles === numFiles) { - zip.finalize(); - } - } - ); + try { + const { data } = await axios.get(file.url, { + responseType: 'arraybuffer' + }); + zip.append(data, { name: `${path}${file.name}` }); + } catch (err) { + console.log(err); + } + numCompletedFiles += 1; + if (numCompletedFiles === numFiles) { + zip.finalize(); + } } else { zip.append(file.content, { name: `${path}${file.name}` }); numCompletedFiles += 1; diff --git a/server/scripts/examples-gg-latest.js b/server/scripts/examples-gg-latest.js index 8e8025a6ff..42d2d3f476 100644 --- a/server/scripts/examples-gg-latest.js +++ b/server/scripts/examples-gg-latest.js @@ -1,4 +1,4 @@ -import rp from 'request-promise'; +import axios from 'axios'; import Q from 'q'; import mongoose from 'mongoose'; import objectID from 'bson-objectid'; @@ -106,7 +106,7 @@ const insert = function insert(_mainString, _insString, _pos) { /* --- data processing --- */ // 1. first get the top level directories P and M // https://api.github.com/repos/generative-design/Code-Package-p5.js/contents?ref=pre-release -function getCodePackage() { +async function getCodePackage() { const sketchRootList = []; const options = { // url: 'https://api.github.com/repos/generative-design/Code-Package-p5.js/contents', @@ -118,26 +118,23 @@ function getCodePackage() { Authorization: `Basic ${Buffer.from( `${clientId}:${clientSecret}` ).toString('base64')}` - }, - json: true + } }; - return rp(options) - .then((res) => { - res.forEach((metadata) => { - if ( - metadata.name.endsWith('P') === true || - metadata.name.endsWith('M') === true - ) { - sketchRootList.push(metadata); - } - }); - - return sketchRootList; - }) - .catch((err) => { - throw err; + try { + const { data } = await axios.request(options); + data.forEach((metadata) => { + if ( + metadata.name.endsWith('P') === true || + metadata.name.endsWith('M') === true + ) { + sketchRootList.push(metadata); + } }); + return sketchRootList; + } catch (err) { + throw err; + } } // 2. get the list of all the top-level sketch directories in P and M @@ -145,7 +142,7 @@ function getSketchDirectories(sketchRootList) { // console.log(sketchRootList); return Q.all( - sketchRootList.map((sketches) => { + sketchRootList.map(async (sketches) => { // console.log(sketches) const options = { url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${sketches.path}${branchRef}`, @@ -155,19 +152,16 @@ function getSketchDirectories(sketchRootList) { Authorization: `Basic ${Buffer.from( `${clientId}:${clientSecret}` ).toString('base64')}` - }, - json: true + } }; - return rp(options) - .then((res) => { - const sketchDirs = flatten(res); - - return sketchDirs; - }) - .catch((err) => { - throw err; - }); + try { + const { data } = await axios.request(options); + const sketchDirs = flatten(data); + return sketchDirs; + } catch (err) { + throw err; + } }) ).then((output) => { const sketchList = []; @@ -186,7 +180,7 @@ function getSketchDirectories(sketchRootList) { // 3. For each sketch item in the sketchList, append the tree contents to each item function appendSketchItemLinks(sketchList) { return Q.all( - sketchList.map((sketches) => { + sketchList.map(async (sketches) => { const options = { // url: `${sketches.url}?client_id=${clientId}&client_secret=${clientSecret}`, url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${sketches.path}${branchRef}`, @@ -196,15 +190,16 @@ function appendSketchItemLinks(sketchList) { Authorization: `Basic ${Buffer.from( `${clientId}:${clientSecret}` ).toString('base64')}` - }, - json: true + } }; - return rp(options).then((res) => { - sketches.tree = res; - + try { + const { data } = await axios.request(options); + sketches.tree = data; return sketchList; - }); + } catch (err) { + throw err; + } }) ); } @@ -214,7 +209,7 @@ function getSketchItems(sketchList) { // const completeSketchPkg = []; /* eslint-disable */ - return Q.all(sketchList[0].map(sketch => Q.all(sketch.tree.map((item) => { + return Q.all(sketchList[0].map(async sketch => Q.all(sketch.tree.map((item) => { if (item.name === 'data') { const options = { url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${item.path}${branchRef}`, @@ -222,16 +217,16 @@ function getSketchItems(sketchList) { headers: { ...headers, Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}` - }, - json: true + } }; - return rp(options).then((res) => { - sketch.data = res; + try { + const { data } = axios.request(options); + sketch.data = data; return sketch; - }).catch((err) => { + } catch (err) { throw err; - }); + } } // pass })))).then(() => sketchList[0]); @@ -399,7 +394,7 @@ function formatAllSketches(sketchList) { // get all the sketch data content and download to the newProjects array function getAllSketchContent(newProjectList) { /* eslint-disable */ - return Q.all(newProjectList.map(newProject => Q.all(newProject.files.map((sketchFile, i) => { + return Q.all(newProjectList.map(newProject => Q.all(newProject.files.map(async (sketchFile, i) => { /* sketchFile.name.endsWith(".mp4") !== true && sketchFile.name.endsWith(".ogg") !== true && @@ -427,12 +422,13 @@ function getAllSketchContent(newProjectList) { }; // console.log("CONVERT ME!") - return rp(options).then((res) => { - newProject.files[i].content = res; + try { + const { data } = await axios.request(options); + newProject.files[i].content = data; return newProject; - }).catch((err) => { + } catch (err) { throw err; - }); + } } if (newProject.files[i].url) { return new Promise((resolve, reject) => { diff --git a/server/scripts/examples-ml5.js b/server/scripts/examples-ml5.js index 6f4249da69..4cd68c5786 100644 --- a/server/scripts/examples-ml5.js +++ b/server/scripts/examples-ml5.js @@ -1,5 +1,5 @@ import fs from 'fs'; -import rp from 'request-promise'; +import axios from 'axios'; import Q from 'q'; import { ok } from 'assert'; @@ -30,8 +30,7 @@ const githubRequestOptions = { Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString( 'base64' )}` - }, - json: true + } }; const editorRequestOptions = { @@ -42,8 +41,7 @@ const editorRequestOptions = { Authorization: `Basic ${Buffer.from( `${editorUsername}:${personalAccessToken}` ).toString('base64')}` - }, - json: true + } }; /** @@ -77,8 +75,12 @@ async function fetchFileContent(item) { options.url !== null || options.url !== '' ) { - file.content = await rp(options); - + try { + const { data } = await axios.request(options); + file.content = data; + } catch (err) { + throw err; + } // NOTE: remove the URL property if there's content // Otherwise the p5 editor will try to pull from that url if (file.content !== null) delete file.url; @@ -111,9 +113,9 @@ async function getCategories() { try { const options = Object.assign({}, githubRequestOptions); options.url = `${options.url}/examples/p5js${branchRef}`; - const results = await rp(options); + const { data } = await axios.request(options); - return results; + return data; } catch (err) { return err; } @@ -131,11 +133,11 @@ async function getCategoryExamples(sketchRootList) { // let options = Object.assign({url: `${requestOptions.url}/${categories.path}${branchRef}`}, requestOptions) const options = Object.assign({}, githubRequestOptions); options.url = `${options.url}${categories.path}${branchRef}`; - - const sketchDirs = await rp(options); + // console.log(options) try { - const result = flatten(sketchDirs); + const { data } = await axios.request(options); + const result = flatten(data); return result; } catch (err) { @@ -168,7 +170,12 @@ async function traverseSketchTree(parentObject) { const options = Object.assign({}, githubRequestOptions); options.url = `${options.url}${parentObject.path}${branchRef}`; - output.tree = await rp(options); + try { + const { data } = await axios.request(options); + output.tree = data; + } catch (err) { + throw err; + } output.tree = output.tree.map((file) => traverseSketchTree(file)); @@ -280,9 +287,12 @@ async function getProjectsList() { const options = Object.assign({}, editorRequestOptions); options.url = `${options.url}/sketches`; - const results = await rp(options); - - return results.sketches; + try { + const { data } = await axios.request(options); + return data.sketches; + } catch (err) { + throw err; + } } /** @@ -293,24 +303,26 @@ async function deleteProject(project) { options.method = 'DELETE'; options.url = `${options.url}/sketches/${project.id}`; - const results = await rp(options); - - return results; + try { + const { data } = await axios.request(options); + return data; + } catch (err) { + throw err; + } } /** * Create a new project */ async function createProject(project) { - try { - const options = Object.assign({}, editorRequestOptions); - options.method = 'POST'; - options.url = `${options.url}/sketches`; - options.body = project; - - const results = await rp(options); + const options = Object.assign({}, editorRequestOptions); + options.method = 'POST'; + options.url = `${options.url}/sketches`; + options.data = project; - return results; + try { + const { data } = await axios.request(options); + return data; } catch (err) { throw err; } diff --git a/server/scripts/examples.js b/server/scripts/examples.js index 902ef0ac87..1424b20a6d 100644 --- a/server/scripts/examples.js +++ b/server/scripts/examples.js @@ -1,4 +1,4 @@ -import rp from 'request-promise'; +import axios from 'axios'; import Q from 'q'; import mongoose from 'mongoose'; import objectID from 'bson-objectid'; @@ -59,12 +59,11 @@ async function getCategories() { Authorization: `Basic ${Buffer.from( `${clientId}:${clientSecret}` ).toString('base64')}` - }, - json: true + } }; try { - const res = await rp(options); - res.forEach((metadata) => { + const { data } = await axios.request(options); + data.forEach((metadata) => { let category = ''; for (let j = 1; j < metadata.name.split('_').length; j += 1) { category += `${metadata.name.split('_')[j]} `; @@ -92,9 +91,9 @@ function getSketchesInCategories(categories) { json: true }; try { - const res = await rp(options); + const { data } = await axios.request(options); const projectsInOneCategory = []; - res.forEach((example) => { + data.forEach((example) => { let projectName; if (example.name === '02_Instance_Container.js') { for (let i = 1; i < 5; i += 1) { @@ -147,7 +146,7 @@ function getSketchContent(projectsInAllCategories) { } }; try { - const res = await rp(options); + const { data } = await axios.request(options); const noNumberprojectName = project.projectName.replace( /(\d+)/g, '' @@ -155,7 +154,7 @@ function getSketchContent(projectsInAllCategories) { if (noNumberprojectName === 'Instance Mode: Instance Container ') { for (let i = 0; i < 4; i += 1) { const splitedRes = `${ - res.split('*/')[1].split('')[i] + data.split('*/')[1].split('')[i] }\n`; project.sketchContent = splitedRes.replace( 'p5.js', @@ -163,7 +162,7 @@ function getSketchContent(projectsInAllCategories) { ); } } else { - project.sketchContent = res; + project.sketchContent = data; } return project; } catch (error) { @@ -217,15 +216,14 @@ async function addAssetsToProject(assets, response, project) { Authorization: `Basic ${Buffer.from( `${clientId}:${clientSecret}` ).toString('base64')}` - }, - json: true + } }; // a function to await for the response that contains the content of asset file const doRequest = async (optionsAsset) => { try { - const res = await rp(optionsAsset); - return res; + const { data } = await axios.request(optionsAsset); + return data; } catch (error) { throw error; } @@ -273,12 +271,11 @@ async function createProjectsInP5user(projectsInAllCategories) { Authorization: `Basic ${Buffer.from( `${clientId}:${clientSecret}` ).toString('base64')}` - }, - json: true + } }; try { - const res = await rp(options); + const { data } = await axios.request(options); const user = await User.findOne({ username: 'p5' }).exec(); await Q.all( projectsInAllCategories.map((projectsInOneCategory) => @@ -382,7 +379,7 @@ async function createProjectsInP5user(projectsInAllCategories) { []; try { - await addAssetsToProject(assetsInProject, res, newProject); + await addAssetsToProject(assetsInProject, data, newProject); const savedProject = await newProject.save(); console.log( `Created a new project in p5 user: ${savedProject.name}` diff --git a/server/scripts/update-syntax-highlighting.js b/server/scripts/update-syntax-highlighting.js index 294d17fe51..b20f791e1b 100644 --- a/server/scripts/update-syntax-highlighting.js +++ b/server/scripts/update-syntax-highlighting.js @@ -1,11 +1,11 @@ const fs = require('fs'); const process = require('process'); -const request = require('request'); +const axios = require('axios'); -request('https://p5js.org/reference/data.json', (err, res) => { - if (!err) { - const result = res.toJSON(); - const data = JSON.parse(result.body); +axios + .get('https://p5js.org/reference/data.json') + .then((response) => { + const { data } = response; const arr = data.classitems; const p5VariableKeywords = {}; @@ -50,7 +50,7 @@ request('https://p5js.org/reference/data.json', (err, res) => { } } ); - } else { - console.log("Error!! Couldn't fetch the data.json file"); - } -}); + }) + .catch((err) => { + throw err; + });