Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: fix typeerror #2

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/post/index.js.map

Large diffs are not rendered by default.

49 changes: 26 additions & 23 deletions dist/setup/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

24 changes: 20 additions & 4 deletions src/lib/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import {
statSync
} from 'node:fs'
import { getCacheClient } from './utils'
import { cacheVersion, getCacheKey, getFsCachePath } from '../constants'
import {
cacheVersion,
getCacheKey,
getFsCachePath,
getTempCachePath
} from '../constants'
import streamToPromise from 'stream-to-promise'
import { unlink } from 'node:fs/promises'

type RequestContext = {
log: {
Expand All @@ -31,9 +38,16 @@ export async function saveCache(
return
}
const client = getCacheClient()
//* Create a temporary file to store the cache
const cacheKey = getCacheKey(hash, tag)
const tempFile = getTempCachePath(cacheKey)
const writeStream = createWriteStream(tempFile)
await streamToPromise(stream.pipe(writeStream))
const size = statSync(tempFile).size
const existingCacheResponse = await client.reserve(
getCacheKey(hash, tag),
cacheVersion
cacheKey,
cacheVersion,
size
)

// Silently exit when we have not been able to receive a cache-hit
Expand All @@ -52,8 +66,10 @@ export async function saveCache(
)
}
ctx.log.info(`Reserved cache ${id}`)
await client.save(parseInt(id), uploadId, uploadUrls, stream)
await client.save(id, uploadId, uploadUrls, tempFile)
ctx.log.info(`Saved cache ${id} for ${hash}`)
//* Remove the temporary file
await unlink(tempFile)
}

export async function getCache(
Expand Down
33 changes: 16 additions & 17 deletions src/lib/cache/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as core from '@actions/core'
import * as cacheHttpClient from '@actions/cache/lib/internal/cacheHttpClient'
import streamToPromise from 'stream-to-promise'
import { createWriteStream } from 'node:fs'
import { unlink } from 'node:fs/promises'
import { getTempCachePath } from '../constants'

class HandledError extends Error {
Expand Down Expand Up @@ -38,16 +37,22 @@ export function getCacheClient() {

const reserve = async (
key: string,
version: string
version: string,
size: number
): Promise<{
success: boolean
data?: { cacheId: string; uploadId: string; uploadUrls: string[] }
data?: { cacheId: number; uploadId: string; uploadUrls: string[] }
}> => {
try {
const reserveCacheResponse = await cacheHttpClient.reserveCache(key, [
version
])
const reserveCacheResponse = await cacheHttpClient.reserveCache(
key,
[version],
{
cacheSize: size
}
)
if (reserveCacheResponse?.result?.cacheId) {
core.info(`Reserved cache ${reserveCacheResponse.result.cacheId}`)
return {
success: true,
data: {
Expand All @@ -60,7 +65,7 @@ export function getCacheClient() {
return { success: false }
} else {
const { statusCode, statusText } = reserveCacheResponse
const data = await reserveCacheResponse.readBody()
const data = reserveCacheResponse.result
const buildedError = new HandledError(statusCode, statusText, data)
return handleFetchError('Unable to reserve cache')(buildedError)
}
Expand All @@ -73,20 +78,11 @@ export function getCacheClient() {
cacheId: number,
uploadId: string,
uploadUrls: string[],
stream: Readable
tempFile: string
): Promise<void> => {
try {
//* Create a temporary file to store the cache
const tempFile = getTempCachePath(cacheId)
const writeStream = createWriteStream(tempFile)
await streamToPromise(stream.pipe(writeStream))
core.info(`Saved cache to ${tempFile}`)

await cacheHttpClient.saveCache(cacheId, tempFile, uploadUrls, uploadId)
core.info(`Saved cache ${cacheId}`)

//* Remove the temporary file
await unlink(tempFile)
} catch (error) {
handleFetchError('Unable to upload cache')(error)
}
Expand Down Expand Up @@ -118,6 +114,9 @@ export function getCacheClient() {
}
}
} catch (error) {
if (error instanceof Error && error.toString().includes('404')) {
return { success: false }
}
return handleFetchError('Unable to query cache')(error)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export const serverLogFile = env.RUNNER_TEMP
: '/tmp/turbogha.log'
export const getFsCachePath = (hash: string): string =>
join(env.RUNNER_TEMP || '/tmp', `${hash}.tg.bin`)
export const getTempCachePath = (id: number): string =>
export const getTempCachePath = (id: number | string): string =>
join(env.RUNNER_TEMP || '/tmp', `cache-${id}.tg.bin`)
Loading