|
3 | 3 | // to nginx config.
|
4 | 4 |
|
5 | 5 | import { createHash } from 'node:crypto'
|
6 |
| -import { readFileSync, writeFileSync } from 'node:fs' |
| 6 | +import { readFile, writeFile } from 'node:fs/promises' |
7 | 7 | import { join } from 'node:path'
|
8 | 8 |
|
9 | 9 | const NGINX = join(import.meta.dirname, '../nginx.conf')
|
10 |
| -const HTML = join(import.meta.dirname, '../dist/index.html') |
| 10 | +const LOADER = join(import.meta.dirname, '../dist/index.html') |
| 11 | +const ERROR = join(import.meta.dirname, '../dist/404.html') |
11 | 12 |
|
12 |
| -function sha512(content: string): string { |
13 |
| - return createHash('sha512').update(content, 'utf8').digest('base64') |
| 13 | +function hash(content: string): string { |
| 14 | + return `'sha256-${createHash('sha256').update(content, 'utf8').digest('base64')}'` |
14 | 15 | }
|
15 | 16 |
|
16 |
| -let html = readFileSync(HTML, 'utf8') |
17 |
| -let css = html.match(/<style>([\s\S]*?)<\/style>/)![1]! |
18 |
| -let js = html.match(/<script>([\s\S]*?)<\/script>/)![1]! |
19 |
| - |
20 |
| -let nginx = readFileSync(NGINX, 'utf8') |
| 17 | +let [loader, error, nginx] = await Promise.all([ |
| 18 | + readFile(LOADER, 'utf8'), |
| 19 | + readFile(ERROR, 'utf8'), |
| 20 | + readFile(NGINX, 'utf8') |
| 21 | +]) |
| 22 | +let loaderCSS = loader.match(/<style>([\s\S]*?)<\/style>/)![1]! |
| 23 | +let errorCSS = error.match(/<style>([\s\S]*?)<\/style>/)![1]! |
| 24 | +let loaderJS = loader.match(/<script>([\s\S]*?)<\/script>/)![1]! |
21 | 25 |
|
22 | 26 | nginx = nginx
|
23 | 27 | .toString()
|
24 |
| - .replace(/(style-src 'sha512-)[^']+'/g, `$1${sha512(css)}'`) |
25 |
| - .replace(/(script-src 'sha512-)[^']+'/g, `$1${sha512(js)}'`) |
| 28 | + .replace( |
| 29 | + /style-src 'sha\d+-[^']+' 'sha\d+-[^']+'/g, |
| 30 | + `style-src ${hash(loaderCSS)} ${hash(errorCSS)}` |
| 31 | + ) |
| 32 | + .replace(/script-src 'sha\d+-[^']+'/g, `script-src ${hash(loaderJS)}`) |
26 | 33 |
|
27 |
| -writeFileSync(NGINX, nginx) |
| 34 | +await writeFile(NGINX, nginx) |
0 commit comments