<?xml version="1.0" encoding="ISO-8859-1"?>
<opml version="2.0">
	<head>
		<title>lib/names.js</title>
		<dateModified>&lt;%dateModified%></dateModified>
		<expansionState></expansionState>
		<vertScrollState>1</vertScrollState>
		<windowTop>300</windowTop>
		<windowLeft>700</windowLeft>
		<windowBottom>900</windowBottom>
		<windowRight>1500</windowRight>
		</head>
	<body text="lib/names.js">
		<outline created="Tue, 21 Jan 2014 00:09:23 GMT" pgfnum="36074" text="var urlpack = require (&quot;url&quot;);"></outline>
		<outline text="var s3 = require (&quot;../lib/s3.js&quot;);"></outline>
		<outline text="var utils = require (&quot;daveutils&quot;);"></outline>
		<outline isComment="true" text="var utils = require (&quot;../lib/utils.js&quot;);"></outline>
		<outline text="var opml = require (&quot;../lib/opml.js&quot;);"></outline>
		<outline text=""></outline>
		<outline text="exports.init = init;"></outline>
		<outline text="exports.isNameAvailable = isNameAvailable; "></outline>
		<outline text="exports.reserveName = reserveName; "></outline>
		<outline text="exports.lookupName = lookupName;"></outline>
		<outline text="exports.serveThroughName = serveThroughName;"></outline>
		<outline text=""></outline>
		<outline text="var s3NamesPath;"></outline>
		<outline text=""></outline>
		<outline text="function init (s3Path) {">
			<outline isComment="true" text="console.log (&quot;names.init: s3Path == &quot; + s3Path);"></outline>
			<outline text="s3NamesPath = s3Path + &quot;names/&quot;;"></outline>
			<outline text="}"></outline>
			</outline>
		<outline created="Wed, 22 Jan 2014 15:21:12 GMT" pgfnum="36268" text="function cleanName (name) {">
			<outline text="if (name === undefined) {">
				<outline text="return (&quot;&quot;);"></outline>
				<outline text="}"></outline>
				</outline>
			<outline text="else {">
				<outline created="Wed, 22 Jan 2014 15:24:49 GMT" pgfnum="36279" text="var s = &quot;&quot;;"></outline>
				<outline created="Wed, 22 Jan 2014 15:24:49 GMT" pgfnum="36281" text="for (var i = 0; i &lt; name.length; i++) {">
					<outline created="Wed, 22 Jan 2014 15:23:24 GMT" pgfnum="36275" text="var ch = name [i];"></outline>
					<outline created="Wed, 22 Jan 2014 15:25:48 GMT" pgfnum="36283" text="if (utils.isAlpha (ch) || utils.isNumeric (ch)) {">
						<outline created="Wed, 22 Jan 2014 15:26:24 GMT" pgfnum="36285" text="s += ch;"></outline>
						<outline created="Wed, 22 Jan 2014 15:26:13 GMT" pgfnum="36284" text="}"></outline>
						</outline>
					<outline created="Wed, 22 Jan 2014 15:25:05 GMT" pgfnum="36282" text="}"></outline>
					</outline>
				<outline created="Wed, 22 Jan 2014 15:27:03 GMT" pgfnum="36286" text="return (s.toLowerCase (s));"></outline>
				<outline text="}"></outline>
				</outline>
			<outline created="Wed, 22 Jan 2014 15:21:19 GMT" pgfnum="36269" text="}"></outline>
			</outline>
		<outline text="function outlineToOPML (theOutline, fname) {">
			<outline text="var xmltext = &quot;&quot;, indentlevel = 0;"></outline>
			<outline text="function add (s) {">
				<outline created="Wed, 19 Jun 2013 18:22:33 GMT" pgfnum="26661" text="xmltext += utils.filledString (&quot;\t&quot;, indentlevel) + s + &quot;\n&quot;;"></outline>
				<outline text="}"></outline>
				</outline>
			<outline text="function dolevel (theNode) {">
				<outline text="var atts = &quot;&quot;;"></outline>
				<outline created="Sat, 09 Aug 2014 15:39:38 GMT" pgfnum="43186" text="for (var x in theNode) {">
					<outline text="if (x != &quot;subs&quot;) {">
						<outline created="Sat, 09 Aug 2014 15:40:19 GMT" pgfnum="43192" text="atts += &quot; &quot; + x + &quot;=\&quot;&quot; + utils.encodeXml (theNode [x]) + &quot;\&quot;&quot;;"></outline>
						<outline text="}"></outline>
						</outline>
					<outline created="Sat, 09 Aug 2014 15:39:43 GMT" pgfnum="43187" text="}"></outline>
					</outline>
				<outline text="if (theNode.subs === undefined) {">
					<outline text="add (&quot;&lt;outline&quot; + atts + &quot; /&gt;&quot;);"></outline>
					<outline text="}"></outline>
					</outline>
				<outline text="else {">
					<outline text="add (&quot;&lt;outline&quot; + atts + &quot; &gt;&quot;); indentlevel++;"></outline>
					<outline text="for (var i = 0; i &lt; theNode.subs.length; i++) {">
						<outline text="dolevel (theNode.subs [i]);"></outline>
						<outline text="}"></outline>
						</outline>
					<outline text="add (&quot;&lt;/outline&gt;&quot;); indentlevel--;"></outline>
					<outline text="}"></outline>
					</outline>
				<outline text="}"></outline>
				</outline>
			<outline text="add (&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;ISO-8859-1\&quot;?&gt;&quot;);"></outline>
			<outline text="add (&quot;&lt;opml version=\&quot;2.0\&quot;&gt;&quot;); indentlevel++;"></outline>
			<outline text="add (&quot;&lt;head&gt;&quot;); indentlevel++;"></outline>
			<outline text="add (&quot;&lt;title&gt;&quot; + fname + &quot;&lt;/title&gt;&quot;);"></outline>
			<outline text="add (&quot;&lt;/head&gt;&quot;); indentlevel--;"></outline>
			<outline text="add (&quot;&lt;body&gt;&quot;); indentlevel++;"></outline>
			<outline text=""></outline>
			<outline text="dolevel (theOutline);"></outline>
			<outline text=""></outline>
			<outline text="add (&quot;&lt;/body&gt;&quot;); indentlevel--;"></outline>
			<outline text="add (&quot;&lt;/opml&gt;&quot;); indentlevel--;"></outline>
			<outline text="return (xmltext);"></outline>
			<outline text="}"></outline>
			</outline>
		<outline created="Wed, 22 Jan 2014 22:55:27 GMT" pgfnum="36398" text="function isNameAvailable (name, callback) {">
			<outline text="name = cleanName (name);"></outline>
			<outline text="if (name.length == 0) {">
				<outline text="callback (name, false, &quot;The name is empty.&quot;);"></outline>
				<outline text="}"></outline>
				</outline>
			<outline text="else {">
				<outline text="if (name.length &lt; 4) {">
					<outline text="callback (name, false, &quot;The name must be at least 4 characters.&quot;);"></outline>
					<outline created="Wed, 22 Jan 2014 03:24:22 GMT" pgfnum="36241" text="}"></outline>
					</outline>
				<outline created="Wed, 22 Jan 2014 03:24:49 GMT" pgfnum="36244" text="else {">
					<outline text="var namepath = s3NamesPath + name + &quot;.json&quot;;"></outline>
					<outline created="Wed, 22 Jan 2014 23:07:46 GMT" pgfnum="36412" text="s3.getObjectMetadata (namepath, function (metadata) {">
						<outline created="Wed, 22 Jan 2014 23:11:04 GMT" pgfnum="36414" text="console.log (&quot;isNameAvailable: namepath == &quot; + namepath + &quot;, metadata == &quot; + JSON.stringify (metadata));"></outline>
						<outline text="if (metadata === null) {">
							<outline text="callback (name, true, &quot;The name is available.&quot;);"></outline>
							<outline text="}"></outline>
							</outline>
						<outline text="else {">
							<outline text="callback (name, false, &quot;The name is not available.&quot;);"></outline>
							<outline text="}"></outline>
							</outline>
						<outline created="Wed, 22 Jan 2014 23:07:55 GMT" pgfnum="36413" text="});"></outline>
						</outline>
					<outline created="Wed, 22 Jan 2014 03:24:51 GMT" pgfnum="36245" text="}"></outline>
					</outline>
				<outline text="}"></outline>
				</outline>
			<outline created="Wed, 22 Jan 2014 22:55:40 GMT" pgfnum="36399" text="}"></outline>
			</outline>
		<outline text="function reserveName (name, url, owner, callback) {">
			<outline text="name = cleanName (name);"></outline>
			<outline text="if (name.length &lt; 4) {">
				<outline text="callback (name, false, &quot;The name must be at least 4 characters.&quot;);"></outline>
				<outline text="}"></outline>
				</outline>
			<outline text="else {">
				<outline text="isNameAvailable (name, function (name, flAvailable, msg) {">
					<outline text="if (flAvailable) {">
						<outline text="var path = s3NamesPath + name + &quot;.json&quot;"></outline>
						<outline created="Wed, 22 Jan 2014 22:39:50 GMT" pgfnum="36388" text="var metadata = {">
							<outline created="Wed, 22 Jan 2014 23:44:41 GMT" pgfnum="36422" text="&quot;name&quot;: name,"></outline>
							<outline created="Wed, 22 Jan 2014 22:40:29 GMT" pgfnum="36393" text="&quot;opmlUrl&quot;: url,"></outline>
							<outline text="&quot;owner&quot;: owner,"></outline>
							<outline created="Wed, 22 Jan 2014 22:40:49 GMT" pgfnum="36394" text="&quot;whenCreated&quot;: new Date ().toString ()"></outline>
							<outline created="Wed, 22 Jan 2014 22:41:05 GMT" pgfnum="36395" text="};"></outline>
							</outline>
						<outline text="s3.newObject (path, utils.jsonStringify (metadata), &quot;application/json&quot;, &quot;private&quot;, function (err, data) {">
							<outline text="console.log (&quot;reserveName: path == &quot; + path);"></outline>
							<outline text="if (err) {">
								<outline text="callback (name, false, err.message);"></outline>
								<outline text="}"></outline>
								</outline>
							<outline text="else {">
								<outline text="callback (name, true, &quot;&quot;);"></outline>
								<outline text="}"></outline>
								</outline>
							<outline text="});"></outline>
							</outline>
						<outline text="}"></outline>
						</outline>
					<outline text="else {">
						<outline text="callback (name, false, &quot;The name \&quot;&quot; + name + &quot;\&quot; is already in use.&quot;);"></outline>
						<outline text="}"></outline>
						</outline>
					<outline text="});"></outline>
					</outline>
				<outline text="}"></outline>
				</outline>
			<outline text="}"></outline>
			</outline>
		<outline text="function lookupName (name, callback) {">
			<outline text="name = cleanName (name);"></outline>
			<outline text="var path = s3NamesPath + name + &quot;.json&quot;"></outline>
			<outline created="Wed, 22 Jan 2014 23:07:46 GMT" pgfnum="36412" text="s3.getObject (path, function (error, data) {">
				<outline created="Thu, 23 Jan 2014 18:37:27 GMT" pgfnum="36463" text="if (data != null) {">
					<outline text="callback (JSON.parse (data.Body));"></outline>
					<outline created="Thu, 23 Jan 2014 18:37:33 GMT" pgfnum="36464" text="}"></outline>
					</outline>
				<outline text="else {">
					<outline text="callback ({">
						<outline text="name: name,"></outline>
						<outline text="opmlUrl: undefined,"></outline>
						<outline text="owner: undefined,"></outline>
						<outline text="whenCreated: undefined"></outline>
						<outline text="});"></outline>
						</outline>
					<outline text="}"></outline>
					</outline>
				<outline created="Wed, 22 Jan 2014 23:07:55 GMT" pgfnum="36413" text="});"></outline>
				</outline>
			<outline text="}"></outline>
			</outline>
		<outline text="function searchOutlineForId (theOutline, id, callback) { //7/16/15 by DW">
			<outline text="function searchLevel (theNode) {">
				<outline text="if (theNode.created != undefined) {">
					<outline text="if (id == Number (new Date (theNode.created))) { //found it">
						<outline text="console.log (&quot;searchOutlineForId: found headline with id == &quot; + id + &quot; at &quot; + theNode.text);"></outline>
						<outline text="if (callback != undefined) {">
							<outline text="callback (theNode);"></outline>
							<outline text="}"></outline>
							</outline>
						<outline text="return (false);"></outline>
						<outline text="}"></outline>
						</outline>
					<outline text="}"></outline>
					</outline>
				<outline text="if (theNode.subs != undefined) {">
					<outline text="for (var i = 0; i &lt; theNode.subs.length; i++) {">
						<outline text="if (!searchLevel (theNode.subs [i])) {">
							<outline text="return (false);"></outline>
							<outline text="}"></outline>
							</outline>
						<outline text="}"></outline>
						</outline>
					<outline text="}"></outline>
					</outline>
				<outline text="return (true); //keep searching"></outline>
				<outline text="}"></outline>
				</outline>
			<outline text="searchLevel (theOutline);"></outline>
			<outline text="callback (undefined); "></outline>
			<outline text="}"></outline>
			</outline>
		<outline text="function serveThroughName (host, port, httpRequest, userDomain, callback) {">
			<outline created="Tue, 21 Jan 2014 00:21:43 GMT" pgfnum="36078" text="var parsedUrl = urlpack.parse (httpRequest.url, true);"></outline>
			<outline created="Mon, 21 Apr 2014 23:15:16 GMT" pgfnum="40413" text="var id = parsedUrl.query.id, format = parsedUrl.query.format;"></outline>
			<outline text="var whenstart = new Date ();"></outline>
			<outline text="function hasAcceptHeader (theHeader) {">
				<outline isComment="true" text="Changes">
					<outline text="6/24/15; 1:16:10 PM by DW">
						<outline isComment="true" text="header string looks like this: text/x-opml, */*"></outline>
						</outline>
					</outline>
				<outline isComment="true" text=""></outline>
				<outline isComment="true" text="console.log (&quot;hasAcceptHeader: theHeader == &quot; + theHeader);"></outline>
				<outline isComment="true" text="console.log (&quot;hasAcceptHeader: httpRequest.headers == &quot; + utils.jsonStringify (httpRequest.headers));"></outline>
				<outline isComment="true" text=""></outline>
				<outline text="if (httpRequest.headers.accept === undefined) {">
					<outline text="return (false);"></outline>
					<outline text="}"></outline>
					</outline>
				<outline text="else {">
					<outline text="var split = httpRequest.headers.accept.split (&quot;, &quot;);"></outline>
					<outline text="for (var i = 0; i &lt; split.length; i++) {">
						<outline text="if (split [i] == theHeader) {">
							<outline text="return (true);"></outline>
							<outline text="}"></outline>
							</outline>
						<outline text="}"></outline>
						</outline>
					<outline text="return (false);"></outline>
					<outline text="}"></outline>
					</outline>
				<outline text="}"></outline>
				</outline>
			<outline created="Mon, 10 Feb 2014 16:23:44 GMT" pgfnum="37363" text="function getNameFromSubdomain (subdomain) {">
				<outline created="Wed, 22 Jan 2014 20:13:15 GMT" pgfnum="36365" text="var sections = subdomain.split (&quot;.&quot;);"></outline>
				<outline created="Thu, 23 Jan 2014 16:56:22 GMT" pgfnum="36430" text="return (sections [0]);"></outline>
				<outline created="Mon, 10 Feb 2014 16:23:53 GMT" pgfnum="37364" text="}"></outline>
				</outline>
			<outline text="function serveOutline (theOutline, fname) {">
				<outline text="if (hasAcceptHeader (&quot;text/x-opml&quot;)) {">
					<outline isComment="true" text="console.log (&quot;serveOutline: serving OPML because the Accept header is there.&quot;);"></outline>
					<outline text="callback (true, 200, &quot;text/xml&quot;, outlineToOPML (theOutline, fname));"></outline>
					<outline text="}"></outline>
					</outline>
				<outline text="else {">
					<outline text="callback (true, 200, &quot;application/json&quot;, utils.jsonStringify (theOutline));"></outline>
					<outline text="}"></outline>
					</outline>
				<outline text="}"></outline>
				</outline>
			<outline text="if (userDomain === undefined) {">
				<outline text="callback (false);"></outline>
				<outline text="}"></outline>
				</outline>
			<outline text="else {">
				<outline text="var forwardedhost = httpRequest.headers [&quot;x-forwarded-host&quot;];"></outline>
				<outline text="if (forwardedhost !== undefined) {">
					<outline text="if (utils.endsWith (forwardedhost, userDomain)) {">
						<outline text="var name = getNameFromSubdomain (forwardedhost);"></outline>
						<outline text="lookupName (name, function (data) {">
							<outline text="if (data.opmlUrl === undefined) {">
								<outline text="callback (true, 404, &quot;text/plain&quot;, &quot;Couldn't read the outline because the name \&quot;&quot; + name + &quot;\&quot; is not defined.&quot;);"></outline>
								<outline text="}"></outline>
								</outline>
							<outline text="else {">
								<outline text="opml.readOpmlUrl (data.opmlUrl, function (theOutline) {">
									<outline text="if (theOutline === undefined) {">
										<outline text="callback (true, 404, &quot;text/plain&quot;, &quot;Couldn't read the outline at url == \&quot;&quot; + data.opmlUrl + &quot;.\&quot;&quot;);"></outline>
										<outline text="}"></outline>
										</outline>
									<outline text="else {">
										<outline text="if (id === undefined) {">
											<outline text="console.log (&quot;names.serveThroughName: name == &quot; + name + &quot;, url = &quot; + data.opmlUrl + &quot;, &quot; + utils.secondsSince (whenstart) + &quot; secs.&quot;);"></outline>
											<outline text="serveOutline (theOutline, name);"></outline>
											<outline text="}"></outline>
											</outline>
										<outline text="else {">
											<outline text="searchOutlineForId (theOutline, id, function (theNode) {">
												<outline text="if (theNode === undefined) {">
													<outline text="callback (true, 404, &quot;text/plain&quot;, &quot;Couldn't find a node with id == \&quot;&quot; + id + &quot;.\&quot;&quot;);"></outline>
													<outline text="}"></outline>
													</outline>
												<outline text="else {">
													<outline text="serveOutline (theNode, name);"></outline>
													<outline text="}"></outline>
													</outline>
												<outline text="});"></outline>
												</outline>
											<outline text="}"></outline>
											</outline>
										<outline text="}"></outline>
										</outline>
									<outline text="});"></outline>
									</outline>
								<outline text="}"></outline>
								</outline>
							<outline text="});"></outline>
							</outline>
						<outline text="return;"></outline>
						<outline text="}"></outline>
						</outline>
					<outline text="}"></outline>
					</outline>
				<outline text="callback (false);"></outline>
				<outline text="}"></outline>
				</outline>
			<outline text="}"></outline>
			</outline>
		<outline text=""></outline>
		</body>
	</opml>
