Skip to content

Commit

Permalink
*: fix typeerror (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushshah15 authored Jan 27, 2025
1 parent 31b40cc commit fe36458
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 47 deletions.
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`)

0 comments on commit fe36458

Please sign in to comment.