Skip to content

Commit 9391c96

Browse files
committed
fix: gz+b64 for yarn.lock
1 parent faeef7b commit 9391c96

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

packages/cli/src/commands/gen-manifests/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { tmpdir } from "os"
22
import path from "path"
33
import { fileURLToPath } from "url"
4+
import { gzipSync } from "zlib"
45

56
import { createLogger, getLatestKubernetesVersion } from "@crossplane-js/libs"
67
import { Command } from "commander"
@@ -593,7 +594,17 @@ async function genManifestsAction(
593594

594595
// Add yarn.lock to the manifest if found
595596
if (yarnLock) {
596-
xfuncjsStep.input.spec.source.yarnLock = yarnLock
597+
try {
598+
const gz = gzipSync(Buffer.from(yarnLock, "utf8"))
599+
const b64 = gz.toString("base64")
600+
xfuncjsStep.input.spec.source.yarnLock = b64
601+
moduleLogger.debug(
602+
`Encoded yarn.lock: original ${yarnLock.length} chars -> gz ${gz.length} bytes -> b64 ${b64.length} chars`
603+
)
604+
} catch (e) {
605+
moduleLogger.warn(`Failed to gzip/base64 yarn.lock, embedding raw: ${e}`)
606+
xfuncjsStep.input.spec.source.yarnLock = yarnLock
607+
}
597608
}
598609
}
599610

pkg/node/yarn.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package node
22

33
import (
4+
"bytes"
5+
"compress/gzip"
46
"context"
7+
"encoding/base64"
58
"encoding/json"
69
"fmt"
10+
"io"
711
"os"
812
"os/exec"
913
"path/filepath"
@@ -125,7 +129,32 @@ func (yi *YarnInstaller) PrepareYarnEnvironment(workDir string, yarnLock string,
125129
// If yarn.lock is provided, write it to the temporary directory
126130
if yarnLock != "" {
127131
yarnLockPath := filepath.Join(workDir, "yarn.lock")
128-
if err := os.WriteFile(yarnLockPath, []byte(yarnLock), 0644); err != nil {
132+
133+
// Attempt to base64-decode and gunzip the provided yarn.lock
134+
var toWrite []byte
135+
if decoded, err := base64.StdEncoding.DecodeString(yarnLock); err == nil {
136+
if zr, err := gzip.NewReader(bytes.NewReader(decoded)); err == nil {
137+
defer zr.Close()
138+
if unzipped, err := io.ReadAll(zr); err == nil {
139+
toWrite = unzipped
140+
logger.Info("Decoded base64 and gunzipped yarn.lock")
141+
} else {
142+
logger.WithField("error", err.Error()).
143+
Warn("Failed to read gunzipped yarn.lock, falling back to raw content")
144+
}
145+
} else {
146+
logger.WithField("error", err.Error()).
147+
Warn("Failed to create gzip reader for yarn.lock, falling back to raw content")
148+
}
149+
} else {
150+
// Not base64-encoded; fall back to raw content
151+
}
152+
153+
if toWrite == nil {
154+
toWrite = []byte(yarnLock)
155+
}
156+
157+
if err := os.WriteFile(yarnLockPath, toWrite, 0644); err != nil {
129158
logger.WithField("error", err.Error()).
130159
Warn("Failed to write yarn.lock to temporary directory")
131160
} else {

0 commit comments

Comments
 (0)