Skip to content
This repository was archived by the owner on Dec 8, 2023. It is now read-only.

Commit d15f46d

Browse files
authored
fix: remove unnecessary TextEncoder from Fermyon (#191)
1 parent d99d568 commit d15f46d

File tree

1 file changed

+19
-42
lines changed

1 file changed

+19
-42
lines changed

.grit/patterns/serverless_to_spin.md

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ predicate insert_statement($statement) {
2121
pattern spin_fix_response() {
2222
or {
2323
object($properties) where {
24-
$properties <: contains bubble {
25-
pair($key, $value) where {
26-
$key <: "body",
27-
$value <: $old => js"encoder.encode($old).buffer"
28-
}
29-
},
3024
$properties <: contains bubble {
3125
pair($key, $value) where {
3226
$key <: "statusCode",
@@ -37,7 +31,7 @@ pattern spin_fix_response() {
3731
object() as $obj where {
3832
$obj => js"{
3933
status: 200,
40-
body: encoder.encode($obj).buffer
34+
body: JSON.stringify($obj)
4135
}"
4236
}
4337
}
@@ -98,16 +92,9 @@ pattern spin_main_fix_request() {
9892
}
9993
}
10094
101-
pattern spin_add_encoder() {
102-
`encoder.encode` where {
103-
insert_statement(statement=js"const encoder = new TextEncoder('utf-8');")
104-
}
105-
}
106-
10795
sequential {
10896
contains spin_main_fix_handler(),
10997
maybe contains spin_main_fix_request(),
110-
maybe contains spin_add_encoder(),
11198
maybe contains spin_remove_lambda(),
11299
}
113100
```
@@ -131,21 +118,17 @@ module.exports.handler = async (event) => {
131118
```
132119

133120
```js
134-
const encoder = new TextEncoder('utf-8');
135-
136121
export async function handleRequest(request) {
137122
return {
138123
status: 200,
139-
body: encoder.encode(
140-
JSON.stringify(
141-
{
142-
message: 'Go Serverless v3.0! Your function executed successfully!',
143-
input: request,
144-
},
145-
null,
146-
2,
147-
),
148-
).buffer,
124+
body: JSON.stringify(
125+
{
126+
message: 'Go Serverless v3.0! Your function executed successfully!',
127+
input: request,
128+
},
129+
null,
130+
2,
131+
),
149132
};
150133
}
151134
```
@@ -173,21 +156,17 @@ export const hello = async (event: APIGatewayProxyEvent): Promise<APIGatewayProx
173156
```ts
174157
import { HttpRequest, HttpResponse } from '@fermyon/spin-sdk';
175158

176-
const encoder = new TextEncoder('utf-8');
177-
178159
export async function handleRequest(request: HttpRequest): Promise<HttpResponse> {
179160
return {
180161
status: 200,
181-
body: encoder.encode(
182-
JSON.stringify(
183-
{
184-
message: 'Go Serverless v3.0! Your function executed successfully!',
185-
input: request,
186-
},
187-
null,
188-
2,
189-
),
190-
).buffer,
162+
body: JSON.stringify(
163+
{
164+
message: 'Go Serverless v3.0! Your function executed successfully!',
165+
input: request,
166+
},
167+
null,
168+
2,
169+
),
191170
};
192171
}
193172
```
@@ -228,14 +207,12 @@ const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) +
228207

229208
const decoder = new TextDecoder('utf-8');
230209

231-
const encoder = new TextEncoder('utf-8');
232-
233210
export async function handleRequest(request) {
234211
const upperLimit = JSON.parse(decoder.decode(request.body)).intent.slots.UpperLimit.value || 100;
235212
const number = getRandomInt(0, upperLimit);
236213
const response = {
237214
status: 200,
238-
body: encoder.encode({
215+
body: JSON.stringify({
239216
version: '1.0',
240217
response: {
241218
outputSpeech: {
@@ -244,7 +221,7 @@ export async function handleRequest(request) {
244221
},
245222
shouldEndSession: false,
246223
},
247-
}).buffer,
224+
}),
248225
};
249226

250227
return response;

0 commit comments

Comments
 (0)