Microlink Function:
How to compress
September 3, 2022 ()
The query parameter function allows you dynamic code execution with remote headless browser access on runtime:
- CLI
- JavaScript
- Shell
- Python
- Ruby
const mql = require('@microlink/mql')
const { status, data } = await mql('https://microlink.io', { function: '({ page }) => page.evaluate("jQuery.fn.jquery")', meta: false, scripts: ['https://code.jquery.com/jquery-3.5.0.min.js'] })
mql.render(data.function)
The function body can be pass in plain text, but also multiple compression algorithms are supported.
Brotli
brotli
is a modern and general purpose compression algorithm with a well compression ratio.const mql = require('@microlink/mql') const { promisify } = require('util') const zlib = require('zlib') const brotli = promisify(zlib.brotliCompress) const toBrotli = async code => { const buffer = await brotli(code) return buffer.toString('base64url') } const code = '({ page }) => page.evaluate("jQuery.fn.jquery")' const { status, data } = await mql('https://microlink.io', { function: `br#${await toBrotli(code)}`, meta: false, scripts: 'https://code.jquery.com/jquery-3.5.0.min.js' }) mql.render(data.function)
Lz-string
lz-string
is designed to be efficient for compressing text.const { compressToURI } = require('lz-ts') const mql = require('@microlink/mql') const code = compressToURI('({ page }) => page.evaluate("jQuery.fn.jquery")') const { status, data } = await mql('https://microlink.io', { function: `lz#${code}`, meta: false, scripts: 'https://code.jquery.com/jquery-3.5.0.min.js' }) mql.render(data.function)
Gzip
gzip is the predecessor of brotli and it's widely supported.
const mql = require('@microlink/mql') const { promisify } = require('util') const zlib = require('zlib') const gzip = promisify(zlib.gzip) const toGzip = async code => { const buffer = await gzip(code) return buffer.toString('base64url') } const code = '({ page }) => page.evaluate("jQuery.fn.jquery")' const { status, data } = await mql('https://microlink.io', { function: `gz#${await toGzip(code)}`, meta: false, scripts: 'https://code.jquery.com/jquery-3.5.0.min.js' }) mql.render(data.function)
It's possible to compress using gzip directly in the browser, using
Compression Streams API
.Join the community
All of these improvements or features are community driven: We listen to your feedback and act accordingly.
Whether you are are building a product and you need fancy previews, you’re an indie hacker or simply you like frontend stuff, come chat with us 🙂.