1
0
Fork 0

use stream transform

pull/1666/head
bethanyj28 2024-02-23 14:34:39 -05:00
parent ac84a9bee3
commit 614f27a4fb
1 changed files with 45 additions and 38 deletions

View File

@ -1,4 +1,5 @@
import fs from 'fs/promises' import fs from 'fs/promises'
import * as stream from 'stream'
import {createWriteStream} from 'fs' import {createWriteStream} from 'fs'
import * as path from 'path' import * as path from 'path'
import * as github from '@actions/github' import * as github from '@actions/github'
@ -85,8 +86,8 @@ export async function streamExtractExternal(
} }
const timer = setTimeout(timerFn, timeout) const timer = setTimeout(timerFn, timeout)
const promises: Promise<void>[] = []
const createdDirectories = new Set<string>() const createdDirectories = new Set<string>()
createdDirectories.add(directory)
response.message response.message
.on('data', () => { .on('data', () => {
timer.refresh() timer.refresh()
@ -99,46 +100,52 @@ export async function streamExtractExternal(
reject(error) reject(error)
}) })
.pipe(unzip.Parse()) .pipe(unzip.Parse())
.on('entry', (entry: unzip.Entry) => { .pipe(
const fullPath = path.normalize(path.join(directory, entry.path)) new stream.Transform({
if (!fullPath.startsWith(directory)) { objectMode: true,
reject(new Error(`Malformed extraction path: ${fullPath}`)) transform: async (entry, _, callback) => {
} const fullPath = path.normalize(path.join(directory, entry.path))
if (!directory.endsWith(path.sep)) {
directory += path.sep
}
if (!fullPath.startsWith(directory)) {
reject(new Error(`Malformed extraction path: ${fullPath}`))
}
core.debug(`Extracting artifact entry: ${fullPath}`) core.debug(`Extracting artifact entry: ${fullPath}`)
if (entry.type === 'Directory') { if (entry.type === 'Directory') {
if (!createdDirectories.has(fullPath)) { if (!createdDirectories.has(fullPath)) {
promises.push(resolveOrCreateDirectory(fullPath).then(() => {})) createdDirectories.add(fullPath)
createdDirectories.add(fullPath) await resolveOrCreateDirectory(fullPath).then(() => {
} entry.autodrain()
entry.autodrain() callback()
} else { })
if (!createdDirectories.has(path.dirname(fullPath))) { } else {
promises.push( entry.autodrain()
resolveOrCreateDirectory(path.dirname(fullPath)).then(() => {}) callback()
) }
createdDirectories.add(path.dirname(fullPath)) } else {
} if (!createdDirectories.has(path.dirname(fullPath))) {
const writeStream = createWriteStream(fullPath) createdDirectories.add(path.dirname(fullPath))
promises.push( await resolveOrCreateDirectory(path.dirname(fullPath)).then(
new Promise((resolve, reject) => { () => {
writeStream.on('finish', () => { entry.autodrain()
resolve() callback()
}) }
)
}
const writeStream = createWriteStream(fullPath)
writeStream.on('finish', callback)
writeStream.on('error', reject) writeStream.on('error', reject)
}) entry.pipe(writeStream)
) }
entry.pipe(writeStream) }
} })
}) )
.on('end', async () => { .on('finish', async () => {
clearTimeout(timer) clearTimeout(timer)
try { resolve()
await Promise.all(promises)
resolve()
} catch (error) {
reject(error)
}
}) })
.on('error', (error: Error) => { .on('error', (error: Error) => {
reject(error) reject(error)