Difference between revisions of "Namecoin SPA"

From Organic Design wiki
m (See also)
(nodes need to be <500 bytes not 1KB :-()
Line 1: Line 1:
I'm making a [[Single Page Application]] which exists entirely inside the [[Namecoin]] network's name:value storage system. Values can only be a maximum size of one kilobyte, so the boot-strapping has to be very modular.
+
I'm making a [[Single Page Application]] which exists entirely inside the [[Namecoin]] network's name:value storage system. Values can only be a maximum size of one kilobyte, so the boot-strapping has to be very modular. In fact it turns out that the current version [https://github.com/namecoin/namecoin/issues/3 has a bug] that prevents values from being updated if the current value is greater than 520 bytes! There's discussion about raising the limit at some point to around 9KB, but incurring steepwe transaction costs for updates greater than 1KB.
  
The root node contains a tiny Perl script which when executed on the command-line listens for incoming HTTP connections on port 2012. The request is treated as a requst for the content of a Namecoin node, but if there is no name specified, then a default node's content is returned.
+
;od_init
{{code|<perl>use IO::Socket;
+
{{code|<perl>sub n {
 +
my $c=shift;
 +
$_=`namecoind name_show d/$c`;
 +
$_=/"value"\s*:\s*"(.*?[^\\])"/s?$1:"404";
 +
s/\\"/"/g;s/\\n/\n/g;
 +
}
 +
sub d {
 +
my $s=shift;
 +
my $c=n($s);
 +
eval "sub $s {$c}";
 +
}
 +
d('od_http');</perl>}}
 +
 
 +
 
 +
;od_http
 +
{{code|<perl>d('od_recv');
 +
d('od_send');
 +
use IO::Socket;
 
sub Wait {wait;}
 
sub Wait {wait;}
 
$SIG{CHLD}=\&Wait;
 
$SIG{CHLD}=\&Wait;
Line 9: Line 26:
 
next if $pid = fork;
 
next if $pid = fork;
 
die "fork - $!\n" unless defined $pid;
 
die "fork - $!\n" unless defined $pid;
$i = '';while(<$c>){last if/^\r\n$/;$i.=$_;}
+
od_send(n(od_recv()));
$_=$i=~/^GET \/([a-z0-9]+)/?$1:"subjective";
 
$_=`namecoind name_show d/$i`;
 
$_=/"value"\s*:\s*"(.*?[^\\])"/s?$1:"404";
 
s/\\"/"/g;s/\\n/\n/g;
 
my $l=length $_;
 
select $c;
 
$|=1;
 
print $c "HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=UTF-8\n";
 
print $c "Connection: Keep-Alive\nKeep-Alive: timeout=15, max=100\nContent-Length: $l\n\n$_\r\n\r\n";
 
close($c);
 
 
exit(fork);
 
exit(fork);
 
}
 
}
Line 25: Line 32:
  
  
If any nodes are requested they'll usually just be some JSON text defining the nodes DNS connections to other IP addresses or domains. The default node's content is however not JSON, but a small XHTML document. This loads a simple container page that loads some useful libraries such as [[jQuery]] and jQueryUI, and then requests any node specified in the hash fragment of the URL by Ajax.
+
;od_recv
 +
{{code|<perl>$i = '';while(<$c>){last if/^\r\n$/;$i.=$_;}
 +
$_=$i=~/^GET \/([a-z0-9]+)/?$1:n("od_html").n("od_head")
 +
."<body><script>".n("od_js")."</script></body></html>";</perl>}}
 +
 
 +
 
 +
;od_send
 +
{{code|<perl>$l=length $_;
 +
select $c;
 +
$|=1;
 +
print $c "HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=UTF-8\nConnection: Keep-Alive\n"
 +
."Keep-Alive: timeout=15, max=100\nContent-Length: $l\n\n$_\r\n\r\n";
 +
close($c);</perl>}}
 +
 
 +
 
 +
;od_html
 
{{code|<xml><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
{{code|<xml><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"></xml>}}
<head>
+
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+
 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
+
;od_head
<script type="text/javascript" src="http://code.jquery.com/ui/jquery-ui-git.js"></script>
+
{{code|<xml><head>
<link rel="stylesheet" href="http://code.jquery.com/ui/jquery-ui-git.css" media="screen" />
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
+
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<body>
+
<script type="text/javascript" src="http://code.jquery.com/ui/jquery-ui-git.js"></script>
<script>
+
<link rel="stylesheet" href="http://code.jquery.com/ui/jquery-ui-git.css" media="screen" />
function hash() {
+
</head></xml>}}
var h=window.location.hash.substr(1);
+
 
if(h){
 
$.ajax({
 
type: "GET",
 
url: "/"+h,
 
success: function(d) { alert(d) }
 
});
 
} else alert("no hash");
 
}
 
$(window).bind("hashchange", hash);
 
hash();
 
</script>
 
</body>
 
</html></xml>}}
 
  
 +
;od_js
 +
{{code|<js>function hash() {
 +
  var h=window.location.hash.substr(1);
 +
  if(h){
 +
$.ajax({
 +
  type: "GET",
 +
  url: "/"+h,
 +
  success: function(d) { alert(d) }
 +
});
 +
  } else alert("no hash");
 +
}
 +
$(window).bind("hashchange", hash);
 +
hash();</js>}}
  
 
If no hash fragment is specified, then a default node is used which contains JavaScript that gets executed (this only happens for this known default node, not for any arbitrary nodes). This JavaScript is the basic Single Page Application, or "viewer application" that knows how to render the DNS relationships between the nodes recirsively, so that further nodes can build up more complex applicational functionality.
 
If no hash fragment is specified, then a default node is used which contains JavaScript that gets executed (this only happens for this known default node, not for any arbitrary nodes). This JavaScript is the basic Single Page Application, or "viewer application" that knows how to render the DNS relationships between the nodes recirsively, so that further nodes can build up more complex applicational functionality.

Revision as of 00:58, 26 August 2012

I'm making a Single Page Application which exists entirely inside the Namecoin network's name:value storage system. Values can only be a maximum size of one kilobyte, so the boot-strapping has to be very modular. In fact it turns out that the current version has a bug that prevents values from being updated if the current value is greater than 520 bytes! There's discussion about raising the limit at some point to around 9KB, but incurring steepwe transaction costs for updates greater than 1KB.

od_init
{{{1}}}


od_http
{{{1}}}


od_recv
{{{1}}}


od_send
{{{1}}}


od_html
<xml><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"></xml>


od_head
{{{1}}}


od_js
{{{1}}}

If no hash fragment is specified, then a default node is used which contains JavaScript that gets executed (this only happens for this known default node, not for any arbitrary nodes). This JavaScript is the basic Single Page Application, or "viewer application" that knows how to render the DNS relationships between the nodes recirsively, so that further nodes can build up more complex applicational functionality.

See also