-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathnode.go
More file actions
171 lines (143 loc) · 4.02 KB
/
node.go
File metadata and controls
171 lines (143 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package langs
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
)
type NodeLangHelper struct {
BaseHelper
Version string
}
func (h *NodeLangHelper) Handles(lang string) bool {
return defaultHandles(h, lang)
}
func (h *NodeLangHelper) Runtime() string {
return h.LangStrings()[0]
}
func (lh *NodeLangHelper) LangStrings() []string {
return []string{"node", fmt.Sprintf("node%s", lh.Version)}
}
func (lh *NodeLangHelper) Extensions() []string {
// this won't be chosen by default
return []string{".js"}
}
func (lh *NodeLangHelper) BuildFromImage() (string, error) {
return fmt.Sprintf("fnproject/node:%s-dev", lh.Version), nil
}
func (lh *NodeLangHelper) RunFromImage() (string, error) {
return fmt.Sprintf("fnproject/node:%s", lh.Version), nil
}
const funcJsContent = `const fdk=require('@fnproject/fdk');
fdk.handle(function(input){
let name = 'World';
if (input.name) {
name = input.name;
}
console.log('\nInside Node Hello World function')
return {'message': 'Hello ' + name}
})
`
const packageJsonContent = `{
"name": "hellofn",
"version": "1.0.0",
"description": "example function",
"main": "func.js",
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@fnproject/fdk": ">=%s"
}
}
`
func (h *NodeLangHelper) GenerateBoilerplate(path string) error {
fdkVersion, err := h.GetLatestFDKVersion()
if err != nil {
return err
}
pathToPackageJsonFile := filepath.Join(path, "package.json")
pathToFuncJs := filepath.Join(path, "func.js")
if exists(pathToPackageJsonFile) || exists(pathToFuncJs) {
return ErrBoilerplateExists
}
packageJsonBoilerplate := fmt.Sprintf(packageJsonContent, fdkVersion)
err = ioutil.WriteFile(pathToPackageJsonFile, []byte(packageJsonBoilerplate), os.FileMode(0644))
if err != nil {
return err
}
err = ioutil.WriteFile(pathToFuncJs, []byte(funcJsContent), os.FileMode(0644))
if err != nil {
return err
}
return nil
}
func (h *NodeLangHelper) HasBoilerplate() bool { return true }
// CustomMemory - no memory override here.
func (h *NodeLangHelper) CustomMemory() uint64 { return 0 }
func (h *NodeLangHelper) Entrypoint() (string, error) {
return "node func.js", nil
}
func (h *NodeLangHelper) DockerfileBuildCmds() []string {
r := []string{}
// skip npm -install if node_modules is local - allows local development
if exists("dist") {
// Do nothing
} else if exists("package.json") && !exists("node_modules") {
if exists("package-lock.json") {
r = append(r, "ADD package-lock.json /function/")
}
r = append(r,
"ADD package.json /function/",
"RUN npm install",
)
}
return r
}
func (h *NodeLangHelper) DockerfileCopyCmds() []string {
// If they have a dist folder (likely from webpack) let's just include that
if exists("dist") {
r := []string{"ADD dist/main.js /function/func.js"}
return r
}
// excessive but content could be anything really
r := []string{"ADD . /function/"}
if exists("package.json") && !exists("node_modules") {
r = append(r, "COPY --from=build-stage /function/node_modules/ /function/node_modules/")
}
r = append(r, "RUN chmod -R o+r /function")
return r
}
func (h *NodeLangHelper) GetLatestFDKVersion() (string, error) {
const versionURL = "https://registry.npmjs.org/@fnproject/fdk"
const versionEnv = "FN_NODE_FDK_VERSION"
fetchError := fmt.Errorf("failed to fetch latest Node FDK version from %v. "+
"Check your network settings or manually override the Node FDK version by setting %s", versionURL, versionEnv)
version := os.Getenv(versionEnv)
if version != "" {
return version, nil
}
resp, err := http.Get(versionURL)
if err != nil || resp.StatusCode != 200 {
return "", fetchError
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fetchError
}
parsedResp := struct {
DistTags struct {
Latest string `json:"latest"`
} `json:"dist-tags"`
}{}
err = json.Unmarshal(body, &parsedResp)
if err != nil {
return "", fetchError
}
return parsedResp.DistTags.Latest, nil
}
func (h *NodeLangHelper) FixImagesOnInit() bool {
return true
}