Skip to content

Commit d0edf2f

Browse files
authored
Merge pull request #272 from netlify/zip_functions_runtime
Allow to read runtime information from zip files.
2 parents 956eec6 + d3f81a9 commit d0edf2f

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed
903 KB
Binary file not shown.

go/porcelain/deploy.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"crypto/sha256"
99
"debug/elf"
1010
"encoding/hex"
11+
"encoding/json"
1112
"fmt"
1213
"io"
1314
"io/ioutil"
@@ -103,6 +104,10 @@ type FileBundle struct {
103104
Buffer io.ReadSeeker
104105
}
105106

107+
type toolchainSpec struct {
108+
Runtime string `json:"runtime"`
109+
}
110+
106111
func (f *FileBundle) Read(p []byte) (n int, err error) {
107112
return f.Buffer.Read(p)
108113
}
@@ -582,7 +587,11 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
582587

583588
switch {
584589
case zipFile(i):
585-
file, err := newFunctionFile(filePath, i, jsRuntime, observer)
590+
runtime, err := readZipRuntime(filePath)
591+
if err != nil {
592+
return nil, err
593+
}
594+
file, err := newFunctionFile(filePath, i, runtime, observer)
586595
if err != nil {
587596
return nil, err
588597
}
@@ -609,6 +618,36 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
609618
return functions, nil
610619
}
611620

621+
func readZipRuntime(filePath string) (string, error) {
622+
zf, err := zip.OpenReader(filePath)
623+
if err != nil {
624+
return "", err
625+
}
626+
defer zf.Close()
627+
628+
for _, file := range zf.File {
629+
if file.Name == "netlify-toolchain" {
630+
fc, err := file.Open()
631+
if err != nil {
632+
// Ignore any errors and choose the default runtime.
633+
// This preserves the current behavior in this library.
634+
return jsRuntime, nil
635+
}
636+
defer fc.Close()
637+
638+
var tc toolchainSpec
639+
if err := json.NewDecoder(fc).Decode(&tc); err != nil {
640+
// Ignore any errors and choose the default runtime.
641+
// This preserves the current behavior in this library.
642+
return jsRuntime, nil
643+
}
644+
return tc.Runtime, nil
645+
}
646+
}
647+
648+
return jsRuntime, nil
649+
}
650+
612651
func newFunctionFile(filePath string, i os.FileInfo, runtime string, observer DeployObserver) (*FileBundle, error) {
613652
file := &FileBundle{
614653
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-rs-function-test.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)