@grame/faustwasm 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
|
|
2
|
+
const CACHE_NAME = 'FAUST_DSP-static'; // Cache name without versioning
|
|
3
|
+
|
|
4
|
+
const POLY_EFFECT_RESOURCES = [
|
|
5
|
+
'/FAUST_DSP/',
|
|
6
|
+
'/FAUST_DSP/faust-ui/index.js',
|
|
7
|
+
'/FAUST_DSP/faust-ui/index.css',
|
|
8
|
+
'/FAUST_DSP/faustwasm/index.js',
|
|
9
|
+
'/FAUST_DSP/FAUST_DSP.js',
|
|
10
|
+
'/FAUST_DSP/FAUST_DSP.wasm',
|
|
11
|
+
'/FAUST_DSP/FAUST_DSP.json',
|
|
12
|
+
'/FAUST_DSP/mixerModule.wasm',
|
|
13
|
+
'/FAUST_DSP/FAUST_DSP_effect.wasm',
|
|
14
|
+
'/FAUST_DSP/FAUST_DSP_effect.json',
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const POLY_RESOURCES = [
|
|
18
|
+
'/FAUST_DSP/',
|
|
19
|
+
'/FAUST_DSP/faust-ui/index.js',
|
|
20
|
+
'/FAUST_DSP/faust-ui/index.css',
|
|
21
|
+
'/FAUST_DSP/faustwasm/index.js',
|
|
22
|
+
'/FAUST_DSP/FAUST_DSP.js',
|
|
23
|
+
'/FAUST_DSP/FAUST_DSP.wasm',
|
|
24
|
+
'/FAUST_DSP/FAUST_DSP.json',
|
|
25
|
+
'/FAUST_DSP/mixerModule.wasm',
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Install the service worker and cache the resources
|
|
30
|
+
|
|
31
|
+
*/
|
|
32
|
+
self.addEventListener('install', event => {
|
|
33
|
+
event.waitUntil(
|
|
34
|
+
caches.open(CACHE_NAME).then(cache => {
|
|
35
|
+
console.log("Service worker installed");
|
|
36
|
+
const urlToCheck = '/FAUST_DSP/FAUST_DSP_effect.json';
|
|
37
|
+
return fetchResourceAndCache(cache, urlToCheck);
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Fetch the resources and cache them
|
|
44
|
+
*
|
|
45
|
+
* @param {Cache} cache The cache to store the resources
|
|
46
|
+
* @param {string} urlToCheck The URL of the resource to fetch
|
|
47
|
+
*
|
|
48
|
+
* @returns {Promise<void>} A promise that resolves when the resource is fetched and cached
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
function fetchResourceAndCache(cache, urlToCheck) {
|
|
52
|
+
return fetch(urlToCheck).then(response => {
|
|
53
|
+
if (response.ok) {
|
|
54
|
+
return cacheResources(cache, POLY_EFFECT_RESOURCES);
|
|
55
|
+
} else {
|
|
56
|
+
return cacheResources(cache, POLY_RESOURCES);
|
|
57
|
+
}
|
|
58
|
+
}).catch(fetchError => {
|
|
59
|
+
console.error(`Failed to fetch ${urlToCheck}:`, fetchError);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
* @param {*} cache The cache to store the resources
|
|
65
|
+
* @param {*} resources The resources to cache
|
|
66
|
+
* @returns
|
|
67
|
+
*/
|
|
68
|
+
function cacheResources(cache, resources) {
|
|
69
|
+
return cache.addAll(resources).catch(error => {
|
|
70
|
+
console.error('Failed to cache resources:', error);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
self.addEventListener("activate", event => {
|
|
75
|
+
console.log("Service worker activated");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
self.addEventListener('fetch', event => {
|
|
79
|
+
event.respondWith((async () => {
|
|
80
|
+
const cache = await caches.open(CACHE_NAME);
|
|
81
|
+
const cachedResponse = await cache.match(event.request);
|
|
82
|
+
if (cachedResponse) {
|
|
83
|
+
return cachedResponse;
|
|
84
|
+
} else {
|
|
85
|
+
try {
|
|
86
|
+
const fetchResponse = await fetch(event.request);
|
|
87
|
+
// Ensure the response is valid before caching it
|
|
88
|
+
if (event.request.method === 'GET' && fetchResponse && fetchResponse.status === 200 && fetchResponse.type === 'basic') {
|
|
89
|
+
cache.put(event.request, fetchResponse.clone());
|
|
90
|
+
}
|
|
91
|
+
return fetchResponse;
|
|
92
|
+
} catch (e) {
|
|
93
|
+
// Network access failure
|
|
94
|
+
console.log('Network access error', e);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
})());
|
|
98
|
+
});
|
package/package.json
CHANGED
|
@@ -25,8 +25,13 @@ const copyWebStandaloneAssets = (outputDir, dspName, poly = false) => {
|
|
|
25
25
|
//cpSyncModify(templateHTMLPath, outputDir + `/${dspName}.html`, "FAUST_DSP", dspName);
|
|
26
26
|
cpSyncModify(templateHTMLPath, outputDir + `/index.html`, "FAUST_DSP", dspName);
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
if (poly) {
|
|
29
|
+
const templateWorkerPath = path.join(__dirname, "../assets/standalone/service-worker-poly.js");
|
|
30
|
+
cpSyncModify(templateWorkerPath, outputDir + `/service-worker.js`, "FAUST_DSP", dspName);
|
|
31
|
+
} else {
|
|
32
|
+
const templateWorkerPath = path.join(__dirname, "../assets/standalone/service-worker.js");
|
|
33
|
+
cpSyncModify(templateWorkerPath, outputDir + `/service-worker.js`, "FAUST_DSP", dspName);
|
|
34
|
+
}
|
|
30
35
|
|
|
31
36
|
const templateIconPath = path.join(__dirname, "../assets/standalone/icon.png");
|
|
32
37
|
cpSync(templateIconPath, outputDir + `/icon.png`);
|