refactor: extract zip from stream
This commit is contained in:
parent
7fc7d62cc2
commit
293507fead
|
@ -5,7 +5,7 @@
|
|||
"build": "node src/build.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"extract-zip": "^2.0.1"
|
||||
"unzipper": "^0.12.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 18.12.0"
|
||||
|
|
28
src/build.js
28
src/build.js
|
@ -2,19 +2,16 @@
|
|||
|
||||
// for deployment outside of GitLab CI, e.g. Cloudflare Pages and Netlify
|
||||
|
||||
import unzip from 'extract-zip'
|
||||
import { Extract } from 'unzipper'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { mkdir, rm } from 'node:fs/promises'
|
||||
import { createWriteStream } from 'node:fs'
|
||||
import { pipeline } from 'node:stream/promises'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { Readable } from 'node:stream'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const rootPath = join(__dirname, '..')
|
||||
const tmpPath = join(rootPath, 'tmp')
|
||||
const publicPath = join(rootPath, 'public')
|
||||
const zipPath = join(tmpPath, 'artifacts.zip')
|
||||
const artifactsUrl = 'https://gitlab.com/malware-filter/phishing-filter/-/jobs/artifacts/main/download?job=pages'
|
||||
const pipelineUrl = 'https://gitlab.com/malware-filter/phishing-filter/badges/main/pipeline.svg'
|
||||
const ghMirror = 'https://nightly.link/curbengh/phishing-filter/workflows/pages/main/public.zip'
|
||||
|
@ -30,15 +27,11 @@ const pipelineStatus = async (url) => {
|
|||
}
|
||||
|
||||
const f = async () => {
|
||||
let isMirror = false
|
||||
|
||||
await mkdir(tmpPath, { recursive: true })
|
||||
|
||||
console.log(`Downloading artifacts.zip from "${artifactsUrl}"`)
|
||||
try {
|
||||
await pipeline(
|
||||
Readable.fromWeb((await fetch(artifactsUrl)).body),
|
||||
createWriteStream(zipPath)
|
||||
Extract({ path: rootPath })
|
||||
)
|
||||
await pipelineStatus(pipelineUrl)
|
||||
} catch ({ message }) {
|
||||
|
@ -48,12 +41,13 @@ const f = async () => {
|
|||
}))
|
||||
|
||||
console.log(`Downloading artifacts.zip from "${ghMirror}"`)
|
||||
isMirror = true
|
||||
|
||||
await mkdir(publicPath, { recursive: true })
|
||||
|
||||
try {
|
||||
await pipeline(
|
||||
Readable.fromWeb((await fetch(ghMirror)).body),
|
||||
createWriteStream(zipPath)
|
||||
Extract({ path: publicPath })
|
||||
)
|
||||
} catch ({ message }) {
|
||||
throw new Error(JSON.stringify({
|
||||
|
@ -63,16 +57,8 @@ const f = async () => {
|
|||
}
|
||||
}
|
||||
|
||||
console.log('Extracting artifacts.zip...')
|
||||
if (isMirror === false) {
|
||||
await unzip(zipPath, { dir: rootPath })
|
||||
// snort2.rules is over 25MB limit of CF Pages
|
||||
await rm(join(publicPath, 'phishing-filter-snort2.rules'), { force: true })
|
||||
} else {
|
||||
await mkdir(publicPath, { recursive: true })
|
||||
await unzip(zipPath, { dir: publicPath })
|
||||
await rm(join(publicPath, 'phishing-filter-snort2.rules'), { force: true })
|
||||
}
|
||||
// snort2.rules is over 25MB limit of CF Pages
|
||||
await rm(join(publicPath, 'phishing-filter-snort2.rules'), { force: true })
|
||||
}
|
||||
|
||||
f()
|
||||
|
|
Loading…
Reference in New Issue