Reorganize the JS files.

nb/hide-http
Alex Kern 11 years ago
parent 982c041bea
commit 5eb9f91003

4
.gitignore vendored

@ -22,7 +22,9 @@ build/Release
# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules
node_modules/*
!node_modules/filepizza-peer
!node_modules/filepizza-socket
# Users Environment Variables
.lock-wscript

@ -1,22 +1,12 @@
The MIT License (MIT)
Copyright (c) 2015, Alex Kern
All rights reserved.
Copyright (c) 2015 Alexander Kern
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@ -11,4 +11,4 @@ Peer-to-peer file transfers in your browser
## License
MIT
BSD

@ -1,37 +0,0 @@
import uuid from 'node-uuid'
const id = uuid.v4()
if (typeof window === 'undefined') {
var peer = { id: id }
} else {
let Peer = require('peerjs')
var peer = new Peer(id, {
host: window.location.hostname,
port: window.location.port,
path: '/peer',
config: {
iceServers: [{
'url': 'stun:stun.l.google.com:19302'
}, {
'url': 'turn:52.12.205.113:3478?transport=udp',
'username': 'file',
'credential': 'pizza'
}, {
'url': 'turn:52.12.205.113:80?transport=udp',
'username': 'file',
'credential': 'pizza'
}, {
'url': 'turn:52.12.205.113:3478?transport=tcp',
'username': 'file',
'credential': 'pizza'
}, {
'url': 'turn:52.12.205.113:80?transport=tcp',
'username': 'file',
'credential': 'pizza'
}]
}
})
}
export default peer

@ -1,8 +0,0 @@
if (typeof window === 'undefined') {
var socket = {};
} else {
let io = require('socket.io-client');
var socket = io.connect();
}
export default socket;

@ -1,21 +1,21 @@
const rankSize = 16;
const rankSize = 16
function blobLength(b) {
if (typeof b.byteLength !== 'undefined') return b.byteLength;
if (typeof b.size !== 'undefined') return b.size;
return b.length;
if (typeof b.byteLength !== 'undefined') return b.byteLength
if (typeof b.size !== 'undefined') return b.size
return b.length
}
export default class ChunkedBlob {
constructor() {
this.size = 0;
this.ranks = [[]];
this.size = 0
this.ranks = [[]]
}
add(b) {
this.size += blobLength(b);
this.ranks[0].push(b);
this.size += blobLength(b)
this.ranks[0].push(b)
for (let i = 0; i < this.ranks.length; i++) {
let rank = this.ranks[i]

@ -1,4 +1,4 @@
import alt from '../alt';
import alt from '../alt'
export default alt.createActions(class DownloadActions {
constructor() {

@ -1,4 +1,4 @@
import alt from '../alt';
import alt from '../alt'
export default alt.createActions(class UploadActions {
constructor() {

@ -5,7 +5,7 @@ import DownloadStore from '../stores/DownloadStore'
import ProgressBar from './ProgressBar'
import React from 'react'
import Spinner from './Spinner'
import peer from '../peer'
import peer from 'filepizza-peer'
export default class DownloadPage extends React.Component {

@ -23,6 +23,11 @@ export default class ErrorPage extends React.Component {
return <div>
<h1>{this.state.status}</h1>
<p>{this.state.message}</p>
{this.state.stack
? <pre>{this.state.stack}</pre>
: null}
</div>
}

@ -6,7 +6,7 @@ import Spinner from './Spinner'
import Tempalink from './Tempalink'
import UploadActions from '../actions/UploadActions'
import UploadStore from '../stores/UploadStore'
import socket from '../socket'
import socket from 'filepizza-socket'
export default class UploadPage extends React.Component {

@ -1,6 +1,6 @@
import Arrow from './Arrow';
import Arrow from '@app/components/Arrow';
import React from 'react';
import UploadActions from '../actions/UploadActions';
import UploadActions from '@app/actions/UploadActions';
export default class UploadPage extends React.Component {

@ -0,0 +1,45 @@
import bases from 'bases'
import crypto from 'crypto'
var tokenLength = 8
var tokens = {}
exports.create = function (socket) {
var maxNum = Math.pow(62, tokenLength)
var numBytes = Math.ceil(Math.log(maxNum) / Math.log(256))
var token = ''
do {
do {
var bytes = crypto.randomBytes(numBytes)
var num = 0
for (var i = 0; i < bytes.length; i++) {
num += Math.pow(256, i) * bytes[i]
}
} while (num >= maxNum)
token = bases.toBase62(num)
} while (token in tokens)
const result = {
token: token,
socket: socket
}
tokens[token] = result
return result
}
exports.exists = function (token) {
return token in tokens
}
exports.find = function (token) {
return tokens[token]
}
exports.remove = function (client) {
delete tokens[client.token]
}

@ -1,11 +1,11 @@
var Upload = require('../Upload')
var db = require('../db')
var express = require('express')
var routes = module.exports = new express.Router()
routes.get('/d/:token', function (req, res, next) {
var uploader = Upload.find(req.params.token)
var uploader = db.find(req.params.token)
if (uploader) {
res.locals.data = {
DownloadStore: {

@ -2,13 +2,17 @@ module.exports = function (err, req, res, next) {
var status = err.status || 500
var message = err.message || ''
var stack = process.env.NODE_ENV === 'production' ? null : err.stack || null
console.log(err)
req.url = '/error'
res.status(status)
res.locals.data = {
ErrorStore: {
status: status,
message: message
message: message,
stack: stack
}
}

@ -2,7 +2,7 @@ var browserify = require('browserify-middleware')
var express = require('express')
var path = require('path')
var CLIENT_MODULE_PATH = path.resolve(__dirname, '../../client/index.js')
var CLIENT_MODULE_PATH = path.resolve(__dirname, '../client.js')
module.exports = browserify(CLIENT_MODULE_PATH, {
transform: 'babelify'

@ -1,7 +1,7 @@
var React = require('react')
var ReactRouter = require('react-router')
var alt = require('../../client/alt')
var clientRoutes = require('../../client/routes')
var alt = require('../alt')
var routes = require('../routes')
function isNotFound(state) {
for (var r of state.routes) {
@ -15,7 +15,7 @@ module.exports = function (req, res) {
alt.bootstrap(JSON.stringify(res.locals.data || {}))
ReactRouter.run(clientRoutes, req.url, function (Handler, state) {
ReactRouter.run(routes, req.url, function (Handler, state) {
var html = React.renderToString(<Handler data={alt.takeSnapshot()} />)
alt.flush()

@ -1,4 +1,4 @@
var Upload = require('./Upload')
var db = require('./db')
var express = require('express')
var http = require('http')
var path = require('path')
@ -19,14 +19,14 @@ server.listen(process.env.PORT || 3000, function () {
app.use('/peer', peer.ExpressPeerServer(server))
app.use(morgan('dev'))
app.get('/js', require('./routes/javascript'))
app.get('/css', require('./routes/css'))
app.use(require('./routes/static'))
app.get('/js', require('./middleware/javascript'))
app.get('/css', require('./middleware/css'))
app.use(require('./middleware/static'))
app.use([
require('./routes/bootstrap'),
require('./routes/error'),
require('./routes/react')
require('./middleware/bootstrap'),
require('./middleware/error'),
require('./middleware/react')
])
io.on('connection', function (socket) {
@ -34,19 +34,19 @@ io.on('connection', function (socket) {
var upload = null
socket.on('upload', function (metadata, res) {
if (!upload) upload = new Upload(socket)
if (!upload) upload = db.create(socket)
upload.metadata = metadata
res(upload.token)
})
socket.on('download', function (data) {
var uploader = Upload.find(data.token)
var uploader = db.find(data.token)
if (!uploader) return
uploader.socket.emit('download', data.peerID)
})
socket.on('disconnect', function () {
if (upload) Upload.remove(upload)
if (upload) db.remove(upload)
})
})

@ -1,8 +1,8 @@
import DownloadActions from '../actions/DownloadActions'
import DownloadFile from '../DownloadFile'
import peer from '../peer'
import peer from 'filepizza-peer'
import alt from '../alt'
import socket from '../socket'
import socket from 'filepizza-socket'
export default alt.createStore(class DownloadStore {

@ -5,6 +5,7 @@ export default alt.createStore(class ErrorStore {
constructor() {
this.status = 404
this.message = 'Not Found'
this.stack = null
}
}, 'ErrorStore')

@ -1,8 +1,8 @@
import UploadActions from '../actions/UploadActions'
import UploadFile from '../UploadFile'
import alt from '../alt'
import peer from '../peer'
import socket from '../socket'
import peer from 'filepizza-peer'
import socket from 'filepizza-socket'
export default alt.createStore(class UploadStore {

@ -0,0 +1,31 @@
var Peer = require('peerjs')
var uuid = require('node-uuid')
var id = uuid.v4()
module.exports = new Peer(id, {
host: window.location.hostname,
port: window.location.port,
path: '/peer',
config: {
iceServers: [{
'url': 'stun:stun.l.google.com:19302'
}, {
'url': 'turn:52.12.205.113:3478?transport=udp',
'username': 'file',
'credential': 'pizza'
}, {
'url': 'turn:52.12.205.113:80?transport=udp',
'username': 'file',
'credential': 'pizza'
}, {
'url': 'turn:52.12.205.113:3478?transport=tcp',
'username': 'file',
'credential': 'pizza'
}, {
'url': 'turn:52.12.205.113:80?transport=tcp',
'username': 'file',
'credential': 'pizza'
}]
}
})

@ -0,0 +1,11 @@
{
"name": "filepizza-peer",
"version": "0.0.0",
"main": "server.js",
"browser": "client.js",
"author": "Alex Kern <alex@kern.io>",
"dependencies": {
"peerjs": "^0.3.14",
"node-uuid": "^1.4.3"
}
}

@ -0,0 +1,5 @@
var uuid = require('node-uuid')
var id = uuid.v4()
module.exports = { id: id }

@ -0,0 +1,2 @@
var io = require('socket.io-client')
module.exports = io.connect()

@ -0,0 +1,10 @@
{
"name": "filepizza-socket",
"version": "0.0.0",
"main": "server.js",
"browser": "client.js",
"author": "Alex Kern <alex@kern.io>",
"dependencies": {
"socket.io-client": "^1.3.5"
}
}

@ -0,0 +1 @@
module.exports = {}

@ -2,16 +2,16 @@
"name": "filepizza",
"version": "0.0.0",
"description": "Peer-to-peer file transfers in your browser",
"main": "server/index.js",
"main": "lib/server.js",
"scripts": {
"start": "./node_modules/.bin/babel-node server/index.js"
"start": "./node_modules/.bin/babel-node lib/server.js"
},
"repository": {
"type": "git",
"url": "git@github.com:kern/filepizza.git"
},
"author": "Alex Kern <alex@kern.io>",
"license": "MIT",
"license": "BSD",
"bugs": {
"url": "https://github.com/kern/filepizza/issues"
},
@ -27,15 +27,14 @@
"express": "^4.12.0",
"morgan": "^1.5.2",
"nib": "^1.1.0",
"node-uuid": "^1.4.3",
"peer": "^0.2.8",
"peerjs": "^0.3.14",
"react": "^0.13.0",
"react-frozenhead": "^0.3.0",
"react-router": "^0.13.1",
"socket.io": "^1.3.5",
"socket.io-client": "^1.3.5",
"stylus": "^0.50.0"
"stylus": "^0.50.0",
"filepizza-socket": "./node_modules/filepizza-socket",
"filepizza-peer": "./node_modules/filepizza-peer"
},
"engines": {
"node": "0.10.x"

@ -1,42 +0,0 @@
var bases = require('bases');
var crypto = require('crypto');
var tokenLength = 8;
var tokens = {};
var Upload = module.exports = function (socket) {
var maxNum = Math.pow(62, tokenLength);
var numBytes = Math.ceil(Math.log(maxNum) / Math.log(256));
var token = '';
do {
do {
var bytes = crypto.randomBytes(numBytes);
var num = 0
for (var i = 0; i < bytes.length; i++) {
num += Math.pow(256, i) * bytes[i];
}
} while (num >= maxNum);
token = bases.toBase62(num);
} while (token in tokens);
this.token = token;
this.socket = socket;
tokens[this.token] = this;
};
Upload.exists = function (token) {
return token in tokens;
};
Upload.find = function (token) {
return tokens[token];
};
Upload.remove = function (client) {
delete tokens[client.token];
};
Loading…
Cancel
Save