diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ab42783 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "pwa-node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/index.js" + } + ] +} \ No newline at end of file diff --git a/aws.js b/aws.js index d5463c2..7aacef1 100644 --- a/aws.js +++ b/aws.js @@ -11,6 +11,7 @@ const awsConfig = new AWS.Config({ const s3 = new AWS.S3(awsConfig); const getObject = (bucket, key) => { + console.log(bucket, key); return new Promise(async (resolve, reject) => { const params = { Bucket: bucket, Key: key }; s3.getObject(params, (error, data) => { diff --git a/index.js b/index.js index 1a530e1..16c7ef2 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,7 @@ const { SHA3 } = require('sha3'); const { getExt, makePromise } = require('./utils.js'); // const browserManager = require('./browser-manager.js'); const { MAX_SIZE, IPFS_HTTP_PORT, IPFS_PORT } = require('./constants.js'); +const {putObject, getObject} = require('./aws'); let CERT = undefined; let PRIVKEY = undefined; @@ -99,6 +100,49 @@ Error.stackTraceLimit = 300; }; const addUrl = `http://127.0.0.1:${IPFS_PORT}/api/v0/add`; + + const _handleIpfsRequest = async ( method , req, res) =>{ + const match = req.url.match(/^(\/ipfs)?(\/[a-z0-9]+)(?:\/([^\/]*))?$/i); + if (match) { + console.log('got match', req.url, match); + let url; + if (match[1]) { // /ipfs/ API + url = req.url; + } else { // our / API + url = (match[1] || '/ipfs') + match[2]; + } + + + if(method === 'GET' || method === 'HEAD'){ + const proxy = httpProxy.createProxyServer({}); + req.url = url; + proxy.on('proxyRes', (proxyRes, req, res) => { + debugger; + const filename = match[3] || ''; + const overrideContentTypeToJs = /\.(?:js|tjs|rtfjs)$/.test(filename); + if (overrideContentTypeToJs) { + proxyRes.headers['content-type'] = 'application/javascript'; + } + }); + proxy + .web(req, res, { + target: `http://127.0.0.1:${IPFS_HTTP_PORT}`, + // secure: false, + // changeOrigin: true, + }, err => { + console.warn(err.stack); + res.statusCode = 500; + res.end(); + }); + } else { + console.log('no match', req.url); + + res.statusCode = 404; + res.end(); + } + } + } + const _handleIpfs = async (req, res) => { const _respond = (statusCode, body) => { res.statusCode = statusCode; @@ -125,42 +169,7 @@ Error.stackTraceLimit = 300; res.statusCode = 200; res.end(); } else if (method === 'GET') { - const match = req.url.match(/^(\/ipfs)?(\/[a-z0-9]+)(?:\/([^\/]*))?$/i); - if (match) { - console.log('got match', req.url, match); - let url; - if (match[1]) { // /ipfs/ API - url = req.url; - } else { // our / API - url = (match[1] || '/ipfs') + match[2]; - } - const proxy = httpProxy.createProxyServer({}); - req.url = url; - proxy.on('proxyRes', (proxyRes, req, res) => { - const filename = match[3] || ''; - const overrideContentTypeToJs = /\.(?:js|tjs|rtfjs)$/.test(filename); - console.log('override content type? ' + filename + ' : ' + overrideContentTypeToJs); - if (overrideContentTypeToJs) { - proxyRes.headers['content-type'] = 'application/javascript'; - } - }); - proxy - .web(req, res, { - target: `http://127.0.0.1:${IPFS_HTTP_PORT}`, - // secure: false, - // changeOrigin: true, - }, err => { - console.warn(err.stack); - - res.statusCode = 500; - res.end(); - }); - } else { - console.log('no match', req.url); - - res.statusCode = 404; - res.end(); - } + _handleIpfsRequest('GET', req, res); } else if (method === 'POST') { const contentType = headers['content-type']; const contentLength = parseInt(headers['content-length'], 10) || 0; @@ -259,7 +268,11 @@ Error.stackTraceLimit = 300; } }; req.on('end', _end); - } else { + } else if(method === 'HEAD'){ + debugger; + _handleIpfsRequest('HEAD', req,res); + } + else { _respond(500, 'Method not available'); } } catch (err) { @@ -275,7 +288,7 @@ Error.stackTraceLimit = 300; try { const o = url.parse(protocol + '//' + (req.headers['host'] || '') + req.url); console.log('got req', req.method, o); - if (o.host === 'ipfs.exokit.org' || o.host === 'ipfs.webaverse.com') { + if (o.host === 'ipfs.exokit.org' || o.host === 'ipfs.webaverse.com' || o.host === 'local.webaverse.com') { if (o.pathname === '/upload-folder' && ['POST', 'OPTIONS'].includes(req.method)) { _handleUploadFolder(req, res); } else { diff --git a/package-lock.json b/package-lock.json index c41c19e..910d12a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.1", "license": "ISC", "dependencies": { + "aws-sdk": "^2.1099.0", "form-data": "^2.3.3", "http-proxy": "^1.18.1", "ipfs-http-client": "^55.0.0", @@ -242,6 +243,40 @@ "node": ">= 4.5.0" } }, + "node_modules/aws-sdk": { + "version": "2.1099.0", + "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.1099.0.tgz", + "integrity": "sha512-7pWBLU4q/obqkV+Vw6RrcRA2QJXX2i+CQ+OB88AtxoymvIq/vSGgJ72SrQDvzn1Bu1zIZKtTA+PTJkMQiLAh1w==", + "dependencies": { + "buffer": "4.9.2", + "events": "1.1.1", + "ieee754": "1.1.13", + "jmespath": "0.16.0", + "querystring": "0.2.0", + "sax": "1.2.1", + "url": "0.10.3", + "uuid": "3.3.2", + "xml2js": "0.4.19" + }, + "engines": { + "node": ">= 10.0.0" + } + }, + "node_modules/aws-sdk/node_modules/buffer": { + "version": "4.9.2", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.2.tgz", + "integrity": "sha512-xq+q3SRMOxGivLhBNaUdC64hDTQwejJ+H0T/NB1XMtTVEwNTrfFF3gAxiyW0Bu/xWEGhjVKgUcMhCrUy2+uCWg==", + "dependencies": { + "base64-js": "^1.0.2", + "ieee754": "^1.1.4", + "isarray": "^1.0.0" + } + }, + "node_modules/aws-sdk/node_modules/ieee754": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", + "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" + }, "node_modules/b64": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/b64/-/b64-3.0.3.tgz", @@ -1032,6 +1067,14 @@ "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz", "integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==" }, + "node_modules/events": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", + "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=", + "engines": { + "node": ">=0.4.x" + } + }, "node_modules/expand-brackets": { "version": "2.1.4", "resolved": "https://registry.npmjs.org/expand-brackets/-/expand-brackets-2.1.4.tgz", @@ -2023,8 +2066,7 @@ "node_modules/isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", - "dev": true + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" }, "node_modules/iso-url": { "version": "1.2.1", @@ -2096,6 +2138,14 @@ "readable-stream": "^3.6.0" } }, + "node_modules/jmespath": { + "version": "0.16.0", + "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.16.0.tgz", + "integrity": "sha512-9FzQjJ7MATs1tSpnco1K6ayiYE3figslrXA72G2HQ/n76RzvYlofyi5QM+iX4YRs/pu3yzxlVQSST23+dMDknw==", + "engines": { + "node": ">= 0.6.0" + } + }, "node_modules/jsonify": { "version": "0.0.0", "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", @@ -2830,6 +2880,20 @@ "once": "^1.3.1" } }, + "node_modules/punycode": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", + "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=" + }, + "node_modules/querystring": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", + "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=", + "deprecated": "The querystring API is considered Legacy. new code should use the URLSearchParams API instead.", + "engines": { + "node": ">=0.4.x" + } + }, "node_modules/react-native-fetch-api": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/react-native-fetch-api/-/react-native-fetch-api-2.0.0.tgz", @@ -3063,6 +3127,11 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "node_modules/sax": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", + "integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o=" + }, "node_modules/secure-keys": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/secure-keys/-/secure-keys-1.0.0.tgz", @@ -3660,6 +3729,15 @@ "integrity": "sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI=", "dev": true }, + "node_modules/url": { + "version": "0.10.3", + "resolved": "https://registry.npmjs.org/url/-/url-0.10.3.tgz", + "integrity": "sha1-Ah5NnHcF8hu/N9A861h2dAJ3TGQ=", + "dependencies": { + "punycode": "1.3.2", + "querystring": "0.2.0" + } + }, "node_modules/use": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz", @@ -3736,6 +3814,15 @@ "rimraf": "bin.js" } }, + "node_modules/uuid": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", + "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==", + "deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.", + "bin": { + "uuid": "bin/uuid" + } + }, "node_modules/varint": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/varint/-/varint-6.0.0.tgz", @@ -3824,6 +3911,23 @@ "node": ">=8.3.0" } }, + "node_modules/xml2js": { + "version": "0.4.19", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", + "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", + "dependencies": { + "sax": ">=0.6.0", + "xmlbuilder": "~9.0.1" + } + }, + "node_modules/xmlbuilder": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", + "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=", + "engines": { + "node": ">=4.0" + } + }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", @@ -4047,6 +4151,39 @@ "integrity": "sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==", "dev": true }, + "aws-sdk": { + "version": "2.1099.0", + "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.1099.0.tgz", + "integrity": "sha512-7pWBLU4q/obqkV+Vw6RrcRA2QJXX2i+CQ+OB88AtxoymvIq/vSGgJ72SrQDvzn1Bu1zIZKtTA+PTJkMQiLAh1w==", + "requires": { + "buffer": "4.9.2", + "events": "1.1.1", + "ieee754": "1.1.13", + "jmespath": "0.16.0", + "querystring": "0.2.0", + "sax": "1.2.1", + "url": "0.10.3", + "uuid": "3.3.2", + "xml2js": "0.4.19" + }, + "dependencies": { + "buffer": { + "version": "4.9.2", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.2.tgz", + "integrity": "sha512-xq+q3SRMOxGivLhBNaUdC64hDTQwejJ+H0T/NB1XMtTVEwNTrfFF3gAxiyW0Bu/xWEGhjVKgUcMhCrUy2+uCWg==", + "requires": { + "base64-js": "^1.0.2", + "ieee754": "^1.1.4", + "isarray": "^1.0.0" + } + }, + "ieee754": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", + "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" + } + } + }, "b64": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/b64/-/b64-3.0.3.tgz", @@ -4689,6 +4826,11 @@ "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz", "integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==" }, + "events": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", + "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=" + }, "expand-brackets": { "version": "2.1.4", "resolved": "https://registry.npmjs.org/expand-brackets/-/expand-brackets-2.1.4.tgz", @@ -5471,8 +5613,7 @@ "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", - "dev": true + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" }, "iso-url": { "version": "1.2.1", @@ -5538,6 +5679,11 @@ "readable-stream": "^3.6.0" } }, + "jmespath": { + "version": "0.16.0", + "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.16.0.tgz", + "integrity": "sha512-9FzQjJ7MATs1tSpnco1K6ayiYE3figslrXA72G2HQ/n76RzvYlofyi5QM+iX4YRs/pu3yzxlVQSST23+dMDknw==" + }, "jsonify": { "version": "0.0.0", "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", @@ -6127,6 +6273,16 @@ "once": "^1.3.1" } }, + "punycode": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", + "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=" + }, + "querystring": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", + "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" + }, "react-native-fetch-api": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/react-native-fetch-api/-/react-native-fetch-api-2.0.0.tgz", @@ -6320,6 +6476,11 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "sax": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", + "integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o=" + }, "secure-keys": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/secure-keys/-/secure-keys-1.0.0.tgz", @@ -6835,6 +6996,15 @@ "integrity": "sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI=", "dev": true }, + "url": { + "version": "0.10.3", + "resolved": "https://registry.npmjs.org/url/-/url-0.10.3.tgz", + "integrity": "sha1-Ah5NnHcF8hu/N9A861h2dAJ3TGQ=", + "requires": { + "punycode": "1.3.2", + "querystring": "0.2.0" + } + }, "use": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz", @@ -6898,6 +7068,11 @@ } } }, + "uuid": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", + "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" + }, "varint": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/varint/-/varint-6.0.0.tgz", @@ -6966,6 +7141,20 @@ "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.1.tgz", "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==" }, + "xml2js": { + "version": "0.4.19", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", + "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", + "requires": { + "sax": ">=0.6.0", + "xmlbuilder": "~9.0.1" + } + }, + "xmlbuilder": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", + "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=" + }, "xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/package.json b/package.json index 291f504..b0f2b58 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "index.js", "scripts": { "start": "sudo $(which node) $(which forever) start -a -l forever.log -o stdout.log -e stderr.log index.js", + "dev": "node index.js", "stop": "sudo $(which node) $(which forever) stop index.js; sudo killall -9 ipfs" }, "repository": { @@ -18,6 +19,7 @@ }, "homepage": "https://github.com/modulesio/webaverse-server#readme", "dependencies": { + "aws-sdk": "^2.1099.0", "form-data": "^2.3.3", "http-proxy": "^1.18.1", "ipfs-http-client": "^55.0.0",