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,8 +100,14 @@ export async function streamExtractExternal(
reject(error) reject(error)
}) })
.pipe(unzip.Parse()) .pipe(unzip.Parse())
.on('entry', (entry: unzip.Entry) => { .pipe(
new stream.Transform({
objectMode: true,
transform: async (entry, _, callback) => {
const fullPath = path.normalize(path.join(directory, entry.path)) const fullPath = path.normalize(path.join(directory, entry.path))
if (!directory.endsWith(path.sep)) {
directory += path.sep
}
if (!fullPath.startsWith(directory)) { if (!fullPath.startsWith(directory)) {
reject(new Error(`Malformed extraction path: ${fullPath}`)) reject(new Error(`Malformed extraction path: ${fullPath}`))
} }
@ -108,38 +115,38 @@ export async function streamExtractExternal(
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 {
entry.autodrain()
callback()
}
} else { } else {
if (!createdDirectories.has(path.dirname(fullPath))) { if (!createdDirectories.has(path.dirname(fullPath))) {
promises.push(
resolveOrCreateDirectory(path.dirname(fullPath)).then(() => {})
)
createdDirectories.add(path.dirname(fullPath)) createdDirectories.add(path.dirname(fullPath))
await resolveOrCreateDirectory(path.dirname(fullPath)).then(
() => {
entry.autodrain()
callback()
} }
const writeStream = createWriteStream(fullPath)
promises.push(
new Promise((resolve, reject) => {
writeStream.on('finish', () => {
resolve()
})
writeStream.on('error', reject)
})
) )
}
const writeStream = createWriteStream(fullPath)
writeStream.on('finish', callback)
writeStream.on('error', reject)
entry.pipe(writeStream) entry.pipe(writeStream)
} }
})
.on('end', async () => {
clearTimeout(timer)
try {
await Promise.all(promises)
resolve()
} catch (error) {
reject(error)
} }
}) })
)
.on('finish', async () => {
clearTimeout(timer)
resolve()
})
.on('error', (error: Error) => { .on('error', (error: Error) => {
reject(error) reject(error)
}) })