Skip to content

Commit e1293e3

Browse files
committed
Allow to read runtime information from zip files.
This reads metadata information generated by zip-it-and-ship-it to send the right runtime information when uploading zipped functions. Signed-off-by: David Calavera <[email protected]>
1 parent 68f2861 commit e1293e3

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

go/internal/data/hello-world.zip

903 KB
Binary file not shown.

go/porcelain/deploy.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,11 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
582582

583583
switch {
584584
case zipFile(i):
585-
file, err := newFunctionFile(filePath, i, jsRuntime, observer)
585+
runtime, err := readZipRuntime(filePath)
586+
if err != nil {
587+
return nil, err
588+
}
589+
file, err := newFunctionFile(filePath, i, runtime, observer)
586590
if err != nil {
587591
return nil, err
588592
}
@@ -609,6 +613,43 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
609613
return functions, nil
610614
}
611615

616+
func readZipRuntime(filePath string) (string, error) {
617+
zf, err := zip.OpenReader(filePath)
618+
if err != nil {
619+
return "", err
620+
}
621+
defer zf.Close()
622+
623+
for _, file := range zf.File {
624+
if file.Name == "netlify-toolchain" {
625+
fc, err := file.Open()
626+
if err != nil {
627+
// Ignore any errors and choose the default runtime.
628+
// This preserves the current behavior in this library.
629+
return jsRuntime, nil
630+
}
631+
defer fc.Close()
632+
633+
reader := bufio.NewReader(fc)
634+
for {
635+
line, err := reader.ReadString('\n')
636+
if err != nil && err != io.EOF {
637+
// Ignore any errors and choose the default runtime.
638+
// This preserves the current behavior in this library.
639+
return jsRuntime, nil
640+
}
641+
642+
if strings.HasPrefix(line, "runtime=") {
643+
split := strings.SplitN(line, "=", 2)
644+
return split[1], nil
645+
}
646+
}
647+
}
648+
}
649+
650+
return jsRuntime, nil
651+
}
652+
612653
func newFunctionFile(filePath string, i os.FileInfo, runtime string, observer DeployObserver) (*FileBundle, error) {
613654
file := &FileBundle{
614655
Name: strings.TrimSuffix(i.Name(), filepath.Ext(i.Name())),

go/porcelain/deploy_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,14 @@ func TestConcurrentFileUpload(t *testing.T) {
130130
}()
131131
}
132132
}
133+
134+
func TestReadZipRuntime(t *testing.T) {
135+
runtime, err := readZipRuntime("../internal/data/hello-world.zip")
136+
if err != nil {
137+
t.Fatalf("unexpected error reading zip file: %v", err)
138+
}
139+
140+
if runtime != "rs" {
141+
t.Fatalf("unexpected runtime value, expected='rs', got='%s'", runtime)
142+
}
143+
}

0 commit comments

Comments
 (0)