<commit_before>﻿self.importScripts(self.assetsUrl || '/service-worker-assets.js');

const VERSION = self.assetsManifest.version;
const CACHE_NAME_PREFIX = 'bit-bswup-';
const CACHE_NAME = `${CACHE_NAME_PREFIX}${VERSION}`;

self.addEventListener('install', e => e.waitUntil(handleInstall(e)));
self.addEventListener('activate', e => e.waitUntil(handleActivate(e)));
self.addEventListener('fetch', e => e.respondWith(handleFetch(e)));
self.addEventListener('message', handleMessage);

async function handleInstall(e) {
    log(`installing version (${VERSION})...`);
    postMessage({ type: 'installing', data: { version: VERSION } });

    await createNewCache();

    log(`installed version (${VERSION})`);
    postMessage({ type: 'installed', data: { version: VERSION } });
}

async function handleActivate(e) {
    log(`activate version (${VERSION})`);

    await deleteOldCaches();

    postMessage({ type: 'activate', data: { version: VERSION } });
}

async function handleFetch(e) {
    if (e.request.method !== 'GET') return fetch(e.request);

    // For all navigation requests, try to serve index.html from cache
    // If you need some URLs to be server-rendered, edit the following check to exclude those URLs
    const shouldServeIndexHtml = e.request.mode === 'navigate';
    const requestUrl = shouldServeIndexHtml ? (self.defaultUrl || 'index.html') : e.request.url;

    if ((self.prohibitedUrls || []).some(url => url.test(requestUrl))) {
        return new Response(new Blob(), { status: 405, "statusText": `prohibited URL: ${requestUrl}` });
    }

    const caseMethod = self.caseInsensitiveUrl ? 'toLowerCase' : 'toString';
    const asset = self.assetsManifest.assets.find(a => shouldServeIndexHtml
        ? a.url[caseMethod]() === requestUrl[caseMethod]()
        : new URL(requestUrl).pathname.endsWith(a.url));
    const cacheUrl = asset && `${asset.url}.${asset.hash || ''}`;

    const cache = await caches.open(CACHE_NAME);
    const cachedResponse = await cache.match(cacheUrl || requestUrl);

    return cachedResponse || fetch(e.request);
}

function handleMessage(e) {
    if (e.data == 'SKIP_WAITING') {
        self.skipWaiting();
    }
}

// ============================================================================

async function createNewCache() {
    const assetsInclude = [/\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/, /\.blat$/, /\.dat$/, /\.svg$/, /\.woff2$/]
        .concat(self.assetsInclude || []);
    const assetsExclude = [/^_content\/Bit.Tooling.Bswup\/bit-bswup.sw.js$/, /^service-worker\.js$/]
        .concat(self.assetsExclude || []);

    const assets = self.assetsManifest.assets
        .filter(asset => assetsInclude.some(pattern => pattern.test(asset.url)))
        .filter(asset => !assetsExclude.some(pattern => pattern.test(asset.url)))
        .concat(self.externalAssets || []);

    let current = 0;
    const total = assets.length;

    const cacheKeys = await caches.keys();
    const oldCacheKey = cacheKeys.find(key => key.startsWith(CACHE_NAME_PREFIX));
    let oldCache;
    if (oldCacheKey) {
        oldCache = await caches.open(oldCacheKey);
    }

    const cache = await caches.open(CACHE_NAME);
    return Promise.all(assets.map(addCache));<commit_msg>add .ttf to the cached file extensions list of bswup #1541 (#1542)<commit_after>﻿self.importScripts(self.assetsUrl || '/service-worker-assets.js');

const VERSION = self.assetsManifest.version;
const CACHE_NAME_PREFIX = 'bit-bswup-';
const CACHE_NAME = `${CACHE_NAME_PREFIX}${VERSION}`;

self.addEventListener('install', e => e.waitUntil(handleInstall(e)));
self.addEventListener('activate', e => e.waitUntil(handleActivate(e)));
self.addEventListener('fetch', e => e.respondWith(handleFetch(e)));
self.addEventListener('message', handleMessage);

async function handleInstall(e) {
    log(`installing version (${VERSION})...`);
    postMessage({ type: 'installing', data: { version: VERSION } });

    await createNewCache();

    log(`installed version (${VERSION})`);
    postMessage({ type: 'installed', data: { version: VERSION } });
}

async function handleActivate(e) {
    log(`activate version (${VERSION})`);

    await deleteOldCaches();

    postMessage({ type: 'activate', data: { version: VERSION } });
}

async function handleFetch(e) {
    if (e.request.method !== 'GET') return fetch(e.request);

    // For all navigation requests, try to serve index.html from cache
    // If you need some URLs to be server-rendered, edit the following check to exclude those URLs
    const shouldServeIndexHtml = e.request.mode === 'navigate';
    const requestUrl = shouldServeIndexHtml ? (self.defaultUrl || 'index.html') : e.request.url;

    if ((self.prohibitedUrls || []).some(url => url.test(requestUrl))) {
        return new Response(new Blob(), { status: 405, "statusText": `prohibited URL: ${requestUrl}` });
    }

    const caseMethod = self.caseInsensitiveUrl ? 'toLowerCase' : 'toString';
    const asset = self.assetsManifest.assets.find(a => shouldServeIndexHtml
        ? a.url[caseMethod]() === requestUrl[caseMethod]()
        : new URL(requestUrl).pathname.endsWith(a.url));
    const cacheUrl = asset && `${asset.url}.${asset.hash || ''}`;

    const cache = await caches.open(CACHE_NAME);
    const cachedResponse = await cache.match(cacheUrl || requestUrl);

    return cachedResponse || fetch(e.request);
}

function handleMessage(e) {
    if (e.data == 'SKIP_WAITING') {
        self.skipWaiting();
    }
}

// ============================================================================

async function createNewCache() {
    const assetsInclude = [/\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/, /\.blat$/, /\.dat$/, /\.svg$/, /\.woff2$/, /\.ttf$/]
        .concat(self.assetsInclude || []);
    const assetsExclude = [/^_content\/Bit.Tooling.Bswup\/bit-bswup.sw.js$/, /^service-worker\.js$/]
        .concat(self.assetsExclude || []);

    const assets = self.assetsManifest.assets
        .filter(asset => assetsInclude.some(pattern => pattern.test(asset.url)))
        .filter(asset => !assetsExclude.some(pattern => pattern.test(asset.url)))
        .concat(self.externalAssets || []);

    let current = 0;
    const total = assets.length;

    const cacheKeys = await caches.keys();
    const oldCacheKey = cacheKeys.find(key => key.startsWith(CACHE_NAME_PREFIX));
    let oldCache;
    if (oldCacheKey) {
        oldCache = await caches.open(oldCacheKey);
    }

    const cache = await caches.open(CACHE_NAME);
    return Promise.all(assets.map(addCache));
{'fraction_non_alphanumeric': 0.19748465339122623, 'fraction_numerical': 0.0026950142236861806, 'mean_word_length': 3.7175141242937855, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6347603', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2063, 'n_words': 443}