How TypeScript Playground shares code via URL

TypeScript Playground makes it easy to share code just by sharing a URL.
Since the code is not stored on any external server, I was curious about how the sharing works, so I looked into it.

How it works

The code sharing works like this:

  1. The code written in the editor is compressed using a string compression library
  2. The compressed string is inserted into the URL
  3. When the URL is opened, the compressed string is expanded and displayed as code in the editor

Compressing the code

The compression is done in this code, using a string compression library called lz-string.

  const hash = `code/${sandbox.lzstring.compressToEncodedURIComponent(sandbox.getText())}`