Skip to content

Commit

Permalink
Compress and decompress search index (#1735)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeregrine authored Jul 11, 2023
1 parent 9426bb7 commit 2cfea60
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 65 deletions.
60 changes: 51 additions & 9 deletions assets/js/search-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export function initialize () {
}
}

function search (value) {
async function search (value) {
if (isBlank(value)) {
renderResults({ value })
} else {
setSearchInputValue(value)

const index = getIndex()
const index = await getIndex()

try {
const results = searchResultsToDecoratedSearchNodes(index.search(value))
Expand All @@ -44,25 +44,26 @@ function renderResults ({ value, results, errorMessage }) {
searchContainer.innerHTML = resultsHtml
}

function getIndex () {
async function getIndex () {
lunr.QueryLexer.termSeparator = /\s+/
lunr.Pipeline.registerFunction(elixirTokenFunction, 'elixirTokenSplitter')
lunr.Pipeline.registerFunction(elixirTrimmerFunction, 'elixirTrimmer')
lunr.Pipeline.registerFunction(hyphenSearchFunction, 'hyphenSearch')

const cachedIndex = loadIndex()
const cachedIndex = await loadIndex()
if (cachedIndex) { return cachedIndex }

const index = createIndex()
saveIndex(index)
return index
}

function loadIndex () {
async function loadIndex () {
try {
const serializedIndex = sessionStorage.getItem(indexStorageKey())
if (serializedIndex) {
return lunr.Index.load(JSON.parse(serializedIndex))
const index = await decompress(serializedIndex)
return lunr.Index.load(index)
} else {
return null
}
Expand All @@ -72,17 +73,58 @@ function loadIndex () {
}
}

function saveIndex (index) {
async function saveIndex (index) {
try {
const serializedIndex = JSON.stringify(index)
const serializedIndex = await compress(index)
sessionStorage.setItem(indexStorageKey(), serializedIndex)
} catch (error) {
console.error('Failed to save index: ', error)
}
}

async function compress(index) {

const stream = new Blob([JSON.stringify(index)], {
type: 'application/json',
}).stream().pipeThrough(new CompressionStream('gzip'));

const blob = await new Response(stream).blob()
const buffer = await blob.arrayBuffer()
return b64encode(buffer)
}


async function decompress(index) {
const stream = new Blob([b64decode(index)], {
type: "application/json",
}).stream().pipeThrough(new DecompressionStream('gzip'));

const blob = await new Response(stream).text()
return JSON.parse(blob)
}

function b64encode(buffer) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}

function b64decode(str) {
const binary_string = window.atob(str);
const len = binary_string.length;
const bytes = new Uint8Array(new ArrayBuffer(len));
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes;
}

function indexStorageKey () {
return `index:${getProjectNameAndVersion()}`
return `idv2:${getProjectNameAndVersion()}`
}

function createIndex () {
Expand Down
56 changes: 0 additions & 56 deletions formatters/html/dist/html-WC6MMJK3.js

This file was deleted.

0 comments on commit 2cfea60

Please sign in to comment.