Skip to content

Commit

Permalink
mod thin webserver, add dir listing
Browse files Browse the repository at this point in the history
add babel-cli into polyfill to do transpiling
  • Loading branch information
DAS committed Nov 22, 2017
1 parent 12ecc9e commit e97e786
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 47 deletions.
32 changes: 32 additions & 0 deletions polyfill/0ws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const [http, port] = [require('http'), 8080];
const [fs, $path, $url] = [require('fs'), require('path'), require('url')];

const requestHandler = (request, response) => {
let url = request.url;
fpath = $path.join(__dirname, url);
fstat = $stat(fpath);
console.log(new Date(), url, fstat.isFile()?'f':fstat.isDirectory()?'d':'-')
if(fstat.isFile(fpath)) {
fs.createReadStream(fpath).pipe(response);
} else if(fstat.isDirectory()) {
response.write(`<h3>Directory listing of '${url}'</h3>`);
for(let i of fs.readdirSync(fpath)) response.write(
`<div><a href='${$join(url, i)}'>${i}</a></div>`);
response.end();
} else response.end('404 not found!');
}

const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) return console.log('something bad happened', err);
console.log(`server is listening on ${port}, ${__dirname}`);
});

function $stat(fn) {
if(fs.existsSync(fn)) return fs.lstatSync(fpath);
else return {
isFile() { return false;},
isDirectory() { return false;}
};
};
function $join(a,b) { return $path.posix.normalize($path.posix.join(a,b)); }
File renamed without changes.
File renamed without changes.
21 changes: 15 additions & 6 deletions polyfill/es6_poly.js → polyfill/dist/test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
// https://babeljs.io/repl/
'use strict';

var body = document.body;
var adiv = function adiv(str) {
var el = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.body;

console.log(str);
var div = document.createElement('div');
div.innerHTML = str;
el.appendChild(div);
};

var x = new Promise(function (r, e) {
setTimeout(function (x) {
console.log('one');
adiv('one');
r('two');
}, 500);
}).then(function (d) {
return new Promise(function (r, e) {
setTimeout(function (x) {
console.log(d);
adiv(d);
r('three');
}, 500);
});
}).then(function (d) {
return new Promise(function (r, e) {
setTimeout(function (x) {
console.log(d);
adiv(d);
r('four');
}, 500);
});
}).then(function (d) {
console.log(d);
});
adiv(d);
});
17 changes: 0 additions & 17 deletions polyfill/es6_native.js

This file was deleted.

8 changes: 4 additions & 4 deletions polyfill/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
}

if(!es6ops()) {
if(!has_bind()) script_include('es5-shim.js');
script_include('es6-shim.js');
script_include('es6_poly.js');
} else script_include('es6_native.js');
if(!has_bind()) script_include('dist/es5-shim.js');
script_include('dist/es6-shim.js');
script_include('dist/test.js');
} else script_include('lib/test.js');
</script>

</head>
Expand Down
24 changes: 24 additions & 0 deletions polyfill/lib/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let body = document.body;
let adiv = (str, el=document.body)=> {
console.log(str);
var div = document.createElement('div');
div.innerHTML = str;
el.appendChild(div);
}

let x = new Promise((r,e)=> {
setTimeout(x=> {
adiv('one');
r('two');
}, 500);
}).then(d=> new Promise((r,e)=> {
setTimeout(x=> {
adiv(d);
r('three');
}, 500);
})).then(d=> new Promise((r,e)=> {
setTimeout(x=> {
adiv(d);
r('four');
}, 500);
})).then(d=> { adiv(d); });
17 changes: 17 additions & 0 deletions polyfill/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "polyfill",
"version": "1.0.0",
"description": "I was curious about having two separate libraries, one which had been transpiled for pre-es6 compatibility (essentially just IE and Opera Mobile), and one for all major browsers which have near full es6 support inbuilt.",
"main": "0ws.js",
"dependencies": {},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1"
},
"scripts": {
"start": "node 0ws.js",
"build": "babel lib -d dist --presets env"
},
"author": "",
"license": "MIT"
}
46 changes: 26 additions & 20 deletions react/0ws.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
// content of index.js
const http = require('http');
const path = require('path');
const fs = require('fs');
const port = 8080;
const [http, port] = [require('http'), 8080];
const [fs, $path, $url] = [require('fs'), require('path'), require('url')];

const requestHandler = (request, response) => {
console.log(request.url)
fpath = path.join(__dirname, request.url);
if(fs.existsSync(fpath)) {
var rstream = fs.createReadStream(fpath);
rstream.pipe(response);
} else {
response.end('Hello Node.js Server!')
}
let url = request.url;
fpath = $path.join(__dirname, url);
fstat = $stat(fpath);
console.log(new Date(), url, fstat.isFile()?'f':fstat.isDirectory()?'d':'-')
if(fstat.isFile(fpath)) {
fs.createReadStream(fpath).pipe(response);
} else if(fstat.isDirectory()) {
response.write(`<h3>Directory listing of '${url}'</h3>`);
for(let i of fs.readdirSync(fpath)) response.write(
`<div><a href='${$join(url, i)}'>${i}</a></div>`);
response.end();
} else response.end('404 not found!');
}

const server = http.createServer(requestHandler)

const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
if (err) return console.log('something bad happened', err);
console.log(`server is listening on ${port}, ${__dirname}`);
});

console.log(`server is listening on ${port}`)
})
function $stat(fn) {
if(fs.existsSync(fn)) return fs.lstatSync(fpath);
else return {
isFile() { return false;},
isDirectory() { return false;}
};
};
function $join(a,b) { return $path.posix.normalize($path.posix.join(a,b)); }

0 comments on commit e97e786

Please sign in to comment.