Skip to content

Commit 1bbce6c

Browse files
committed
Remove extraneous parseStreamAsJson function
1 parent 010518e commit 1bbce6c

File tree

3 files changed

+8
-37
lines changed

3 files changed

+8
-37
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Serverless Functions
22

3-
Serverless functions that NUSMods uses. These used to run on https://code.stdlib.com/ but that is dead so now we're looking into other alternatives.
4-
For one, venue-github-hook is now hosted on Cloudflare Workers.
3+
Serverless functions that NUSMods uses. They run on Cloudflare Workers.
4+
Currently, we have two functions:
5+
6+
- [venue-github-hook](./nusmods-venue-github-hook/) for handling venue data updates.
7+
- [module-error-reporting](./nusmods-module-error-reporting/) for handling course error reports.

nusmods-module-error-reporting/index.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,6 @@ async function getFacultyEmail(contactId) {
99
return contact ? contact.email : null;
1010
}
1111

12-
async function parseStreamAsJson(stream) {
13-
const chunks = [];
14-
for await (const chunk of stream) {
15-
chunks.push(chunk);
16-
}
17-
const totalLen = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
18-
if (totalLen === 0) {
19-
return null;
20-
}
21-
const mergedChunks = new Uint8Array(totalLen);
22-
for (let i = 0, offset = 0; i < chunks.length; offset += chunks[i].length, ++i) {
23-
mergedChunks.set(chunks[i], offset);
24-
}
25-
return JSON.parse(new TextDecoder().decode(mergedChunks));
26-
}
27-
2812
/**
2913
* @typedef {Object} Env
3014
*/
@@ -51,7 +35,7 @@ export default {
5135
return new Response(null, { status: 400, headers: respHeaders });
5236
}
5337

54-
const parsedBody = await parseStreamAsJson(request.body);
38+
const parsedBody = await request.json();
5539
if (parsedBody === null) {
5640
return new Response(null, { status: 400, headers: respHeaders });
5741
}

nusmods-venue-github-hook/index.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ function toDataList(data) {
1515
return unorderedList(dataList);
1616
}
1717

18-
async function parseStreamAsJson(stream) {
19-
const chunks = [];
20-
for await (const chunk of stream) {
21-
chunks.push(chunk);
22-
}
23-
const totalLen = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
24-
if (totalLen === 0) {
25-
return null;
26-
}
27-
const mergedChunks = new Uint8Array(totalLen);
28-
for (let i = 0, offset = 0; i < chunks.length; offset += chunks[i].length, ++i) {
29-
mergedChunks.set(chunks[i], offset);
30-
}
31-
return JSON.parse(new TextDecoder().decode(mergedChunks));
32-
}
33-
3418
/**
3519
* @typedef {Object} Env
3620
*/
@@ -57,7 +41,7 @@ export default {
5741
return new Response(null, { status: 400, headers: respHeaders });
5842
}
5943

60-
const parsedBody = await parseStreamAsJson(request.body);
44+
const parsedBody = await request.json();
6145
if (parsedBody === null) {
6246
return new Response(null, { status: 400, headers: respHeaders });
6347
}
@@ -68,7 +52,7 @@ export default {
6852

6953
try {
7054
const response = await fetch('https://github.nusmods.com/venues');
71-
const currentVenues = await parseStreamAsJson(response.body);
55+
const currentVenues = await response.json();
7256
currentVenue = currentVenues[venue];
7357
} catch (e) {
7458
currentVenueError = e;

0 commit comments

Comments
 (0)