1
1
/** @module @twilio -labs/serverless-api/dist/api */
2
2
3
3
import debug from 'debug' ;
4
- import { extname } from 'path' ;
5
4
import {
6
- FileInfo ,
7
5
FunctionApiResource ,
6
+ ServerlessResourceConfig ,
8
7
FunctionList ,
9
8
FunctionResource ,
10
9
GotClient ,
11
- RawFunctionWithPath ,
12
10
Sid ,
13
11
VersionResource ,
14
12
} from '../types' ;
15
13
import { uploadToAws } from '../utils/aws-upload' ;
16
- import { getPathAndAccessFromFileInfo , readFile } from '../utils/fs' ;
17
14
18
15
const log = debug ( 'twilio-serverless-api:functions' ) ;
19
16
@@ -76,29 +73,21 @@ export async function listFunctionResources(
76
73
* @returns {Promise<FunctionResource[]> }
77
74
*/
78
75
export async function getOrCreateFunctionResources (
79
- functions : FileInfo [ ] ,
76
+ functions : ServerlessResourceConfig [ ] ,
80
77
serviceSid : string ,
81
78
client : GotClient
82
79
) : Promise < FunctionResource [ ] > {
83
80
const output : FunctionResource [ ] = [ ] ;
84
81
const existingFunctions = await listFunctionResources ( serviceSid , client ) ;
85
- const functionsToCreate : RawFunctionWithPath [ ] = [ ] ;
82
+ const functionsToCreate : ServerlessResourceConfig [ ] = [ ] ;
86
83
87
84
functions . forEach ( fn => {
88
- const { path : functionPath , access } = getPathAndAccessFromFileInfo (
89
- fn ,
90
- '.js'
91
- ) ;
92
- const existingFn = existingFunctions . find (
93
- f => functionPath === f . friendly_name
94
- ) ;
85
+ const existingFn = existingFunctions . find ( f => fn . name === f . friendly_name ) ;
95
86
if ( ! existingFn ) {
96
- functionsToCreate . push ( { ...fn , functionPath , access } ) ;
87
+ functionsToCreate . push ( { ...fn } ) ;
97
88
} else {
98
89
output . push ( {
99
90
...fn ,
100
- functionPath,
101
- access,
102
91
sid : existingFn . sid ,
103
92
} ) ;
104
93
}
@@ -107,7 +96,7 @@ export async function getOrCreateFunctionResources(
107
96
const createdFunctions = await Promise . all (
108
97
functionsToCreate . map ( async fn => {
109
98
const newFunction = await createFunctionResource (
110
- fn . functionPath ,
99
+ fn . name ,
111
100
serviceSid ,
112
101
client
113
102
) ;
@@ -135,19 +124,16 @@ async function createFunctionVersion(
135
124
client : GotClient
136
125
) : Promise < VersionResource > {
137
126
if ( fn . access === 'private' ) {
138
- throw new Error ( `Function ${ fn . functionPath } cannnot be "private".
139
- Please rename the file "${ fn . name } " to "${ fn . name . replace (
140
- '.private.' ,
141
- '.protected.'
142
- ) } " or deploy it as an asset.`) ;
127
+ throw new Error ( `Function ${ fn . name } cannnot be "private".
128
+ Please change it to have 'protected' access or deploy it as an asset.` ) ;
143
129
}
144
130
try {
145
131
const resp = await client . post (
146
132
`/Services/${ serviceSid } /Functions/${ fn . sid } /Versions` ,
147
133
{
148
134
form : true ,
149
135
body : {
150
- Path : fn . functionPath ,
136
+ Path : fn . path ,
151
137
Visibility : fn . access ,
152
138
} ,
153
139
}
@@ -156,7 +142,7 @@ Please rename the file "${fn.name}" to "${fn.name.replace(
156
142
return ( resp . body as unknown ) as VersionResource ;
157
143
} catch ( err ) {
158
144
log ( '%O' , err ) ;
159
- throw new Error ( `Failed to upload Function ${ fn . functionPath } ` ) ;
145
+ throw new Error ( `Failed to upload Function ${ fn . name } ` ) ;
160
146
}
161
147
}
162
148
@@ -174,22 +160,12 @@ export async function uploadFunction(
174
160
serviceSid : string ,
175
161
client : GotClient
176
162
) : Promise < Sid > {
177
- let content : Buffer | string | undefined ;
178
- if ( typeof fn . content !== 'undefined' ) {
179
- content = fn . content ;
180
- } else if ( typeof fn . path !== 'undefined' ) {
181
- const encoding = extname ( fn . path ) === '.js' ? 'utf8' : undefined ;
182
- content = await readFile ( fn . path , encoding ) ;
183
- } else {
184
- throw new Error ( 'Missing either content or path for file' ) ;
185
- }
186
-
187
163
const version = await createFunctionVersion ( fn , serviceSid , client ) ;
188
164
const { pre_signed_upload_url : awsData } = version ;
189
165
const awsResult = await uploadToAws (
190
166
awsData . url ,
191
167
awsData . kmsARN ,
192
- content ,
168
+ fn . content ,
193
169
fn . name
194
170
) ;
195
171
return version . sid ;
0 commit comments