This commit is contained in:
SheetJS 2023-06-25 00:01:34 -04:00
commit 21c6a97c64
14 changed files with 2318 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

17
README.md Normal file
View File

@ -0,0 +1,17 @@
# sheetjs-csp-test
Entry points:
- `cdn.html`: sync load SheetJS script from CDN
- `local.html`: sync load vendored script
- `rjs-cdn.html`: async load SheetJS script from CDN using RequireJS
- `rjs-local.html`: async load vendored script using RequireJS
Usage:
```
npm install
npm start
```
Server will start on port 7262.

9
app-rjs-cdn.js Normal file
View File

@ -0,0 +1,9 @@
requirejs.config({
paths: {
xlsx: [ 'https://cdn.sheetjs.com/xlsx-0.20.0/package/dist/xlsx.full.min' ]
}
});
require(['xlsx'], function(XLSX) {
document.getElementById("app").innerText = XLSX.version;
});

9
app-rjs-local.js Normal file
View File

@ -0,0 +1,9 @@
requirejs.config({
paths: {
xlsx: [ './xlsx.full.min' ]
}
});
require(['xlsx'], function(XLSX) {
document.getElementById("app").innerText = XLSX.version;
});

37
app.js Normal file
View File

@ -0,0 +1,37 @@
document.getElementById("app").innerText = XLSX.version;
/* switch to `true` to enable the download */
if(false) (async() => {
/* fetch JSON data and parse */
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
/* sort by first presidential term */
prez.forEach(row => row.start = row.terms.find(term => term.type === "prez").start);
prez.sort((l,r) => l.start.localeCompare(r.start));
/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = XLSX.utils.json_to_sheet(rows);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
XLSX.utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
XLSX.writeFile(workbook, "Presidents.xlsx", { compression: true });
})();

8
cdn.html Normal file
View File

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<body>
<div id="app"></div>
<script src="https://cdn.sheetjs.com/xlsx-0.20.0/package/dist/xlsx.full.min.js"></script>
<script src="app.js"></script>
</body>
</html>

6
index.html Normal file
View File

@ -0,0 +1,6 @@
<ul>
<li><a href="cdn.html">Sync load from SheetJS CDN</a></li>
<li><a href="local.html">Sync load vendored copy</a></li>
<li><a href="rjs-cdn.html">Async load from SheetJS CDN with Require.js</a></li>
<li><a href="rjs-local.html">Async load from vendored copy with Require.js</a></li>
</ul>

22
index.js Normal file
View File

@ -0,0 +1,22 @@
const express = require('express');
const app = express();
/* set CSP header */
app.use((req, res, next) => {
/* allow local and CDN scripts */
res.setHeader('Content-Security-Policy', "script-src 'self' https://cdn.sheetjs.com data: blob:;");
/* if this is used, loading from the SheetJS CDN will fail */
//res.setHeader('Content-Security-Policy', "script-src 'self' data: blob:;");
/* if this is used, loading local scripts will fail */
//res.setHeader('Content-Security-Policy', "script-src https://cdn.sheetjs.com data: blob:;");
next();
});
/* serve folder */
app.use(express.static(__dirname));
/* start server */
app.listen(7262, () => { console.log(`Listening on port 7262`); });

8
local.html Normal file
View File

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<body>
<div id="app"></div>
<script src="xlsx.full.min.js"></script>
<script src="app.js"></script>
</body>
</html>

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "sheetjs-csp-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "npx nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"nodemon": "^2.0.22"
}
}

2145
require.js Normal file

File diff suppressed because it is too large Load Diff

8
rjs-cdn.html Normal file
View File

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<body>
<div id="app"></div>
<script src="require.js"></script>
<script src="app-rjs-cdn.js"></script>
</body>
</html>

8
rjs-local.html Normal file
View File

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<body>
<div id="app"></div>
<script src="require.js"></script>
<script src="app-rjs-local.js"></script>
</body>
</html>

24
xlsx.full.min.js vendored Normal file

File diff suppressed because one or more lines are too long