<commit_before>'use strict';

const preval = require('preval.macro');
const tryToCatch = require('try-to-catch/legacy');
const currify = require('currify/legacy');

const wait = currify((f, e) => e.waitUntil(f()));
const respondWith = currify((f, e) => e.respondWith(f(e)));
const getPathName = (url) => new URL(url).pathname;

const date = preval`module.exports = Date()`;
const NAME = `cloudcmd: ${date}`;

const isGet = (a) => a.method === 'GET';
const isBasic = (a) => a.type === 'basic';

const createRequest = (a) => new Request(a, {
    credentials: 'same-origin'
});

self.addEventListener('install', wait(onInstall));
self.addEventListener('fetch', respondWith(onFetch));
self.addEventListener('activate', wait(onActivate));

async function onActivate() {
    console.info(`cloudcmd: sw: activate: ${NAME}`);
    
    await self.clients.claim();
    const keys = await caches.keys();
    const deleteCache = caches.delete.bind(caches);
    
    await Promise.all(keys.map(deleteCache));
}

async function onInstall() {
    console.info(`cloudcmd: sw: install: ${NAME}`);
    
    await self.skipWaiting();
    
    const cache = await caches.open(NAME);
    
    const urls = [
        '/favicon.ico',
    ];
    
    const requests = urls.map(createRequest);
    
    return cache.addAll(requests);
}

async function onFetch(event) {
    const {request} = event;
    const url = request.url;
    const pathname = getPathName(url);
    
    const cache = await caches.open(NAME);
    const response = await cache.match(request);
    
    if (response)
        return response;
     
    const [e, resp] = await tryToCatch(fetch, request.clone());
    
    if (e)
        return console.error(e, pathname);
    
    if (!isGet(request) || !resp.ok || !isBasic(resp))
        return resp;
    
    if (/\/$/.test(pathname))
        return resp;
    
    if (/^\/api/.test(pathname))
        return resp;
    
    if (/^socket.io/.test(pathname))
        return resp;
    
    await addToCache(request, resp.clone());
     
    return resp;
}<commit_msg>fix(sw) "only-if-cached" can be set only with "same-origin" mode<commit_after>'use strict';

const preval = require('preval.macro');
const tryToCatch = require('try-to-catch/legacy');
const currify = require('currify/legacy');

const wait = currify((f, e) => e.waitUntil(f()));
const respondWith = currify((f, e) => e.respondWith(f(e)));
const getPathName = (url) => new URL(url).pathname;

const date = preval`module.exports = Date()`;
const NAME = `cloudcmd: ${date}`;

const isGet = (a) => a.method === 'GET';
const isBasic = (a) => a.type === 'basic';

const createRequest = (a) => new Request(a, {
    credentials: 'same-origin'
});

const getRequest = (a, request) => {
    if (a !== '/')
        return request;
    
    return createRequest('/');
};

self.addEventListener('install', wait(onInstall));
self.addEventListener('fetch', respondWith(onFetch));
self.addEventListener('activate', wait(onActivate));

async function onActivate() {
    console.info(`cloudcmd: sw: activate: ${NAME}`);
    
    await self.clients.claim();
    const keys = await caches.keys();
    const deleteCache = caches.delete.bind(caches);
    
    await Promise.all(keys.map(deleteCache));
}

async function onInstall() {
    console.info(`cloudcmd: sw: install: ${NAME}`);
    
    await self.skipWaiting();
    
    const cache = await caches.open(NAME);
    
    const urls = [
        '/favicon.ico',
    ];
    
    const requests = urls.map(createRequest);
    return cache.addAll(requests);
}

async function onFetch(event) {
    const url = event.request.url;
    const pathname = getPathName(url);
    const request = getRequest(pathname, event.request);
    
    const cache = await caches.open(NAME);
    const response = await cache.match(request);
    
    if (response)
        return response;
     
    const [e, resp] = await tryToCatch(fetch, request.clone());
    
    if (e)
        return console.error(e, response, pathname);
    
    if (!isGet(request) || !resp.ok || !isBasic(resp))
        return resp;
    
    if (/\/$/.test(pathname))
        return resp;
    
    if (/^\/api/.test(pathname))
        return resp;
    
    if (/^socket.io/.test(pathname))
        return resp;
    
    await addToCache(request, resp.clone());
     
    return resp;
}
{'fraction_non_alphanumeric': 0.16143187066974596, 'fraction_numerical': 0.0, 'mean_word_length': 3.066666666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '3572911', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1401, 'n_words': 328}