Files
toolkit/packages/artifact/src/internal/upload/zip.ts
T

128 lines
3.7 KiB
TypeScript
Raw Normal View History

import * as stream from 'stream'
import * as archiver from 'archiver'
2024-03-28 14:32:03 +00:00
import * as packer from 'zip-stream'
import * as core from '@actions/core'
import {createReadStream} from 'fs'
import {UploadZipSpecification} from './upload-zip-specification'
import {getUploadChunkSize} from '../shared/config'
export const DEFAULT_COMPRESSION_LEVEL = 6
// Custom stream transformer so we can set the highWaterMark property
// See https://github.com/nodejs/node/issues/8855
export class ZipUploadStream extends stream.Transform {
constructor(bufferSize: number) {
super({
highWaterMark: bufferSize
})
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_transform(chunk: any, enc: any, cb: any): void {
cb(null, chunk)
}
}
export async function createZipUploadStream(
uploadSpecification: UploadZipSpecification[],
compressionLevel: number = DEFAULT_COMPRESSION_LEVEL
): Promise<ZipUploadStream> {
2023-11-20 16:27:35 +00:00
core.debug(
`Creating Artifact archive with compressionLevel: ${compressionLevel}`
)
2024-03-28 14:32:03 +00:00
// const zip2 = archiver.create('zip', {
// highWaterMark: getUploadChunkSize(),
// zlib: {level: compressionLevel}
// })
2024-03-28 14:32:03 +00:00
const zlibOptions = {
zlib: {level: compressionLevel, bufferSize: getUploadChunkSize()}
}
const zip = new packer(zlibOptions)
// zip.entry('string contents', {name: 'string.txt'}, function (err, entry) {
// if (err) throw err
// zip.entry(null, {name: 'directory/'}, function (err, entry) {
// if (err) throw err
// zip.finish()
// })
// })
// register callbacks for various events during the zip lifecycle
zip.on('error', zipErrorCallback)
zip.on('warning', zipWarningCallback)
zip.on('finish', zipFinishCallback)
zip.on('end', zipEndCallback)
for (const file of uploadSpecification) {
if (file.sourcePath !== null) {
// Add a normal file to the zip
2024-03-28 14:44:20 +00:00
// zip.append(createReadStream(file.sourcePath), {
// name: file.destinationPath
// })
zip.entry(
createReadStream(file.sourcePath),
{name: file.destinationPath},
function (err, entry) {
core.debug(`Entry is: ${entry}`)
if (err) throw err
}
)
} else {
2024-03-28 14:44:20 +00:00
zip.entry(null, {name: file.destinationPath}, function (err, entry) {
core.debug(`Entry is: ${entry}`)
if (err) throw err
})
// Add a directory to the zip
2024-03-28 14:44:20 +00:00
// zip.append('', {name: file.destinationPath})
}
2024-03-28 14:44:20 +00:00
zip.finish()
}
const bufferSize = getUploadChunkSize()
const zipUploadStream = new ZipUploadStream(bufferSize)
core.debug(
`Zip write high watermark value ${zipUploadStream.writableHighWaterMark}`
)
core.debug(
`Zip read high watermark value ${zipUploadStream.readableHighWaterMark}`
)
zip.pipe(zipUploadStream)
zip.finalize()
return zipUploadStream
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const zipErrorCallback = (error: any): void => {
core.error('An error has occurred while creating the zip file for upload')
core.info(error)
throw new Error('An error has occurred during zip creation for the artifact')
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const zipWarningCallback = (error: any): void => {
if (error.code === 'ENOENT') {
core.warning(
'ENOENT warning during artifact zip creation. No such file or directory'
)
core.info(error)
} else {
core.warning(
`A non-blocking warning has occurred during artifact zip creation: ${error.code}`
)
core.info(error)
}
}
const zipFinishCallback = (): void => {
core.debug('Zip stream for upload has finished.')
}
const zipEndCallback = (): void => {
core.debug('Zip stream for upload has ended.')
}