8
8
"crypto/sha256"
9
9
"debug/elf"
10
10
"encoding/hex"
11
+ "encoding/json"
11
12
"fmt"
12
13
"io"
13
14
"io/ioutil"
@@ -103,6 +104,10 @@ type FileBundle struct {
103
104
Buffer io.ReadSeeker
104
105
}
105
106
107
+ type toolchainSpec struct {
108
+ Runtime string `json:"runtime"`
109
+ }
110
+
106
111
func (f * FileBundle ) Read (p []byte ) (n int , err error ) {
107
112
return f .Buffer .Read (p )
108
113
}
@@ -582,7 +587,11 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
582
587
583
588
switch {
584
589
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 )
586
595
if err != nil {
587
596
return nil , err
588
597
}
@@ -609,6 +618,36 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
609
618
return functions , nil
610
619
}
611
620
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
+
612
651
func newFunctionFile (filePath string , i os.FileInfo , runtime string , observer DeployObserver ) (* FileBundle , error ) {
613
652
file := & FileBundle {
614
653
Name : strings .TrimSuffix (i .Name (), filepath .Ext (i .Name ())),
0 commit comments