Skip to content

Instantly share code, notes, and snippets.

@javaarchive
Last active October 16, 2020 16:50
Show Gist options
  • Save javaarchive/8c70c489162165ec7e16d5c905821b3b to your computer and use it in GitHub Desktop.
Save javaarchive/8c70c489162165ec7e16d5c905821b3b to your computer and use it in GitHub Desktop.

Going static isn't so bad

In fact some people called it a stack called JAMStack. Here are ways you can make static apps that are still as cool as server side apps.

How do I make stuff only show when someone enters a password

  1. Encrypt something with your password as the key
  2. Put it on the webpage. When someone enters the password attempt to decrypt using the same key(please use non-bruteforcable encryption so don't use the Vigenere cipher for this). If it's successful show the decrypted content.

Suppose someone is answering a math problem how do I make it so they can't just hack and find the answer without checking the answer on the server side

Hash the answer and when the user presses a button to check their answers just hash their input and compare. In addition, use a similiar method like the blockchain and add a really long salt to make computation slower so it isn't bruteforcable.

How do I make it so people can load data and save data from other people

  • You can have people save and load files from their computers
  • For easy sharing with small data(like make8bitart) you can encode the data in base64 or urlencode it and put it after the hash or url parameter like. https://xkcd-printer.glitch.me/?top=2353&bottom=2340 The data here is 2353 and 2340 but you can put whatever you want that you can put in urls. Keep in mind that some browsers have a limit on how long your url can be.

What about stuff like chat and other things that need to be networked together

In this case you should look at peer to peer which is decentralized which means you don't have to host a single server to get everyones messages. Unfortunately firewalls and security protocols often make this not work so you might need to setup a relay server. Another problem with peer to peer is your ip can be leaked if you're not using a relay. Setting up relays requires a non-static server unfortunately. Discord uses peer to peer voice/video chat with media relays. If you want to download torrents, you can check out WebTorrent which uses a different protocol than actual torrent clients because it runs in the browser but can still be useful for sharing files.

Ok that paragraph was written a while ago. Recently I noticed I overlooked a specific tool called ipfs which I first saw in action in the brave browser. It was brought to my attention sometime within the last two months by a user called aboutDavid (I think) reminded me of it through posting it somewhere. After that, aboutDavid demoed some pretty neat apps built on top of ipfs and some being static. WritePad P2PBin How to make a cdn with IPFS

IPFS also has a lot of libraries that can give you familiar apis like Orbit db . You can find more libraries in Awesome IPFS.

What about nodejs modules

Modules that don't use filesystem or any nodejs-specific apis can be bundled using a bundler, simply bundle everything locally and upload your bundle to the server. Then use a script tag to include your bundle. Someone once use cors-anywhere + ytdl-core to build a youtube downloader. Also instead of using cors-anywhere to download the video you can also have your user click a link to the download which is how most youtube downloaders work. If you want an mp3 file ffmpegjs can convert m4a files to mp3 in the browser. Here's a cool tool I found to check the cost of loading a module into a browser and if it can be bundled.

If an api doesn't have a nodejs module you can try interacting with it via the fetch api or the older XMLHTTPRequest api if they have cors headers. If they don't you can try cors-anywhere to get around this but beware the rate limit.

What about ai apps

You can also perform AI in the browser with libraries like TensorFlow.js. There are also some nice ai libraries that you should check out in Awesome Mad Science. There you can find awesome libraries that allow you to do stuff like handwritten input and even access wikipedia peer to peer.

Can I utilize the gpu in the browser but not for 3D graphics

Yes you can! There's a library that's quite simple for parallelizing operations that converts your js to shaders so it runs on gpus.

How do I do end to end to encryption

I'm not an expert on cryptography but I can explain a bit of RSA. To use RSA you have two things

  1. A public key which gives people the ability to encrypt messages
  2. A private key which can decrypt messages sent by people using your public key

Unfortunately you will have to send that public key through a trusted service that you need to make sure isn't doing a MITM attack on you. If you already have a secure channel of communication, great! If not you might use another end to end chat app that has more big brain security to send the public key.

After your friend receives your public key and a url of where your website is, they can load the public key either by uploading it as an file, copy & pasting it or some other method. For public keys that aren't too long you could also put them in the url parameters but some browsers have a strict url length limit so you should have a fallback.

Can I implement a search feature onto my website

Sure! You have a few options

  1. Have an external service handle it for you like google
  2. Implement your own. Below I'll show how to implement two types of search engines for your site.

Trie based

A trie is like a graph of characters. The best way to understand what it is, is to put a picture. It looks like a tree of characters. Trie example using the words node and html But what happens when we have strings with same prefixes? To answer that, here's what a trie looks like with the words node and note trie with words node and note To store these we assign each node a id and store them in an adjancey-list like method. Here's what I normally use to store tries in json.

[
  {
    "value": "",
    "children": {
      "n": 1
    }
  },
  {
    "value": "n",
    "children": {
      "o": 2
    }
  },
  {
    "value": "o",
    "children": {
      "d": 3,
      "o": 5
    }
  },
  {
    "value": "d",
    "children": {
      "e": 4
    }
  },
  {
    "value": "e",
    "data": "Some data here"
  },
  {
    "value": "o",
    "children": {
      "t": 6
    }
  },
  {
    "value": "t",
    "children": {
      "e": 7
    }
  },
  {
    "value": "e",
    "data": "More data here"
  }
]

To make a search for this we traverse the nodes in the order of the characters in the input. If you cannot find a node show no results. Otherwise keep going deeper until you find some data. Repeat this for each result you want to be listed by backtracking from the previous search. I will probaly detail this step a lot more in the future.

Inverse Indexing

Assume I have a sentence like this

The interval between to motions is the sum of the interval lengths

We split it by spaces to get a list of words(consider using an advanced nlp library to do this). We would build our inverse index as this

{
"The":[0],
"interval":[1,10]
...
"the":[6,9]
...
}

An index normally gets us the word, however in the inverse index the opposite happens. When we get asked for a query we split it into words again. Then for each word we download the indexes it appears in. We then initialize a set which is a copy of the indexes of the first word. Then we do the following

for each word:
    get it's indexes as a set but subtract by how many indexes it is away from the start
    remove all from the initial set that are not in this set
    

Finally, the initial set is now full of every index the sequence of words is seen. The one thing left to do to bring this to the web is add the webpage link along with the indexes.

Note: This is also called inverted indexes iirc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment