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

fixes #6

Closed
wants to merge 3 commits into from
Closed
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
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js"
}
]
}
1 change: 1 addition & 0 deletions aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
89 changes: 51 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
Loading