Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
Started work on npm command, get and upload working
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfl committed Jan 3, 2011
1 parent 6bb8f60 commit 92f11f3
Show file tree
Hide file tree
Showing 4 changed files with 423 additions and 0 deletions.
144 changes: 144 additions & 0 deletions npm_command/base64.js
@@ -0,0 +1,144 @@
/**
*
* Base64 encode / decode
* http://www.webtoolkit.info/
*
**/

var Base64 = {

// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;

input = Base64._utf8_encode(input);

while (i < input.length) {

chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);

enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;

if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}

output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

}

return output;
},

// public method for decoding
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;

input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

while (i < input.length) {

enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

output = output + String.fromCharCode(chr1);

if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}

}

output = Base64._utf8_decode(output);

return output;

},

// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";

for (var n = 0; n < string.length; n++) {

var c = string.charCodeAt(n);

if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}

}

return utftext;
},

// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;

while ( i < utftext.length ) {

c = utftext.charCodeAt(i);

if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}

}

return string;
}

}

exports.base64 = Base64;
197 changes: 197 additions & 0 deletions npm_command/jsapp.js
@@ -0,0 +1,197 @@
var remote_end = "http://localhost/ajax";
var config_file = process.env.HOME + "/.jsapp";


var debug = console.log; // function () {}

var http = require('http');
var url = require('url');
var fs = require('fs');

var base64 = require('./base64.js').base64;

var config={};

function save_config() {
fs.writeFileSync(config_file, JSON.stringify(config));
}

try {
config = JSON.parse(fs.readFileSync(config_file));
if(!config.user || !config.pass) throw 1;
if(config.remote) config.remote = remote_end;
}catch(e) {
console.log("There was a problem reading your config file, we are going to set it up now\nIf you do not all ready have an account, go to http://jsapp.us and press ctrl+l");
var prompt = require('./prompt.js');
prompt("JsApp username: ", config.user, false, function (err, user) {
prompt("password: ", "", true, function (err, pass) {
config = {user: user, pass: base64.encode(pass), remote: remote_end};
_get_token(function () {console.log("You are now logged in");});
});
});
}

function _get_token (callback) {
if(!config.pass || !config.user) {
console.log("No logins");
process.exit(2);
}
var u = url.parse(config.remote);
var connect = http.createClient(u.port || 80, u.hostname);
var send = JSON.stringify({
"actions": [
{
"action": "login",
"user": config.user,
"pass": base64.decode(config.pass)
}
]
});
var request = connect.request('POST', u.pathname, {
'host': u.hostname,
'Content-length': send.length
});
debug(">", send);
request.write(send);
request.on('response', function (response) {
var data="";
response.on('data', function (d) {data+=d;});
response.on('end', function () {
debug("<", data);
var j = JSON.parse(data);
if(!j.data[0].ok) {
console.log("Your login has been rejected");
config.pass = null;
save_config();
process.exit(2);
}else{
config.token = base64.encode(j.data[0].token);
save_config();
callback();
}
});
});
}

function send(action, callback) {
callback = callback || function () {};
var u = url.parse(config.remote);
var connect = http.createClient(u.port || 80, u.hostname);
var s = JSON.stringify({
"user": config.user,
"token": base64.decode(config.token),
"actions": [
action
]
});
var request = connect.request('POST', u.pathname, {
'host': u.hostname,
'Content-length': s.length
});
debug(">", s);
request.write(s);
request.on('response', function (response) {
var data="";
response.on('data', function (d) {data+=d;});
response.on('end', function () {
debug("<", data);
var j = JSON.parse(data);
if(j.user != config.user) {
_get_token(function () {
send(action, callback);
});
}else{
callback(j.data[0]);
}
});
});
}

var help = {
"help": "Print out useful help information\nusage: jsapp help [command name]",
"upload": "Upload a file\nThis command uploads a file from the local file system to the vitural file system on the server\nusage: jsapp upload local-name [remote-name]",
"puts": "alias to upload",
"get": "Download a file from the server\nusage: jsapp get remote-name [local-name]"
};

var commands = {
"help": function (c) {
if(!c) c = process.argv[2];
if(c) {
if(help[c])
console.log(help[c]);
else
console.log("Command "+c+" not found");
return;
}
var print = [
"jsapp is the command line interface to http://jsapp.us",
"if you are reading this then you must all ready be logged in",
"",
"for more information on a command do: jsapp help [command]",
""];
for(var n in help) {
print.push(n+"\t"+help[n].split('\n')[0])
}

console.log(print.join('\n'));
},
"upload": function () {
if(process.argv.length < 3) return commands.help('upload');
try{
var content = fs.readFileSync(process.argv[2]).toString();
}catch(e) { return console.log("File not found"); }
send({"action": "save", "name": process.argv[3] || process.argv[2], "val": content});
},
"puts": function () { commands.upload(); },
"get": function () {
if(process.argv.length < 3) return commands.help("get");
send({"action": "open", "name": process.argv[2]}, function (d) {
if(d.ok) {
fs.writeFileSync(process.argv[3] || process.argv[2], d.val);
}else{
console.log("File was not found on the server");
}
});
},
"test": function () {

},
"list": function () {
send({"action":"list"}, function (d) {
var print="Files:\n";
for(var a=0;a<d[0].length;a++)
print+=d[0][a]+"\t";
print+="\nDomains:\n";
for(var a=0;a<d[1].length;a++)
print+=d[1][a]+"\t";
console.log(print);
});
},
"ls": function () { commands.list(); },
"deploy": function () {

},
"share": function () {

},
"delete": function () {
},
"rm": function () { commands['delete'](); },
"rename": function () {
},
"mv": function () { commands.rename(); }
};

process.argv.shift();

if(config.pass) {
if(commands[process.argv[1]]) {
commands[process.argv[1]]();
}else{
if(process.argv[1]) console.log("Command was not found\n");
commands['help']();
process.exit(1);
}
}

16 changes: 16 additions & 0 deletions npm_command/package.json
@@ -0,0 +1,16 @@
{
"name": "jsapp",
"description": "A command line interface to jsapp.us",
"version": "0.0.1",
"homepage": "http://jsapp.us",
"author": "Matthew Francis-Landau <matthew@matthewfl.com> (http://matthewfl.com)",
"directories": {
"bin":"."
},
"bin": {
"jsapp":"./jsapp.js"
},
"engines": {
"node": "*"
}
}

0 comments on commit 92f11f3

Please sign in to comment.