Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ dist
# TernJS port file
.tern-port

# VSCode settings
.vscode
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏


/data/*
!/data/.gitkeep
73 changes: 35 additions & 38 deletions js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { valid } from './util';

const app = express();

const PORT = 8000;

const ROOT = 'js.bottle.remotehack.space';

const SUBREG = /^(?<subdomain>\w[\w-]*)\.js\.bottle\.remotehack\.space$/;
Expand All @@ -19,67 +21,62 @@ app.all('/', urlencodedParser, async (req, res, next) => {

const {host} = req.headers;

if(host === ROOT) {
//
if(req.method === 'GET') {
res.sendFile(join(__dirname, '/../www/landing.html'))
return;
}

if(req.method === 'POST') {

const {subdomain} = req.body
if (host !== ROOT) {
next();
return;
}

if(valid(subdomain)) {
if(await store.exists({subdomain})) {
if(req.method === 'GET') {
res.sendFile(join(__dirname, '/../www/landing.html'))
return;
}

res.redirect(subdomain + ROOT)

return;
} else {
if (req.method === 'POST') {

await store.createStore({subdomain})
const {subdomain} = req.body

res.send(`CREATED SUBDOMAIN: ${req.body.subdomain}`)
return;
}
if (valid(subdomain)) {
if (!await store.exists({subdomain})) {
await store.createStore({subdomain})
}


return;
res.redirect(`http://${subdomain}.${ROOT}`)
} else {
res.status(404).send(`${subdomain} is not a valid subdomain name!`);
}

return;
}

next();

})

app.all('*', async (req, res) => {
const {host} = req.headers;

const match =host?.match(SUBREG);
if(match) {

const {subdomain} = (match.groups as any)
const match = host?.match(SUBREG);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always knew seeing my first optional chaining in the wild would be special...but this is BEYOND!!!
🤩🤩🤩

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...I also obviously was not paying attention during the initial coding session on Saturday

const {subdomain} = match?.groups || {};

if (!match || !subdomain) {
const msg = `Failed to match for subdomain on host ${host}`;
console.error(msg);
res.status(404).send(msg);
return;
}

const timestamp = Date.now().toString();
const content = JSON.stringify([req.headers, req.url])

const timestamp = Date.now().toString();
const content = JSON.stringify([req.headers, req.url])
try {
await store.write({subdomain, timestamp, content})

const result = await store.read({subdomain});

res.contentType('text/plain');
res.send(result);

return;
} catch (e) {
console.error(e);
res.status(404).send(`Nobody has created this domain yet.`);
}

res.send(`(NODE) Cool `)
})

app.listen(8000, () => {
console.log("started")
app.listen(PORT, () => {
console.log(`started on port ${PORT}`);
});
2 changes: 1 addition & 1 deletion js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
// "module": "ES2020",
"target": "ES2015",
"target": "ES2020",
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "node",
Expand Down