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+
106111func (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+
612651func 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 ())),
0 commit comments