Skip to content

Commit

Permalink
fix: switch to async event-driven I/O
Browse files Browse the repository at this point in the history
consuming the full POST data is sometimes incomplete otherwise
  • Loading branch information
alee committed Jul 19, 2023
1 parent 2c13484 commit 9f3e33f
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions app/pages/dataset/_id/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ import Map from "@/components/dataset/Map.vue";
import { initializeDataset, saveGeoJson, clearGeoJson } from "@/store/actions";
function getBody(request) {
const qs = require("querystring");
return new Promise((resolve) => {
const bodyArray = [];
let body;
request
.on("data", (chunk) => {
bodyArray.push(chunk);
})
.on("end", () => {
body = Buffer.concat(bodyArray).toString();
resolve(qs.parse(body));
});
});
}
@Component({
layout: "DefaultLayout",
components: {
Expand Down Expand Up @@ -150,20 +166,11 @@ class SelectDatasetArea extends Vue {
}
}
asyncData({ req, res }) {
async asyncData({ req, res }) {
if (process.server) {
if (req.method === "POST") {
const qs = require("querystring");
let body = "";
let data = "";
while ((data = req.read())) {
body += data;
}
const postData = qs.parse(body);
console.log(
"XXX: posted geojson, now do something with it: ",
postData
);
const postData = await getBody(req);
console.log("Received POST data: ", postData);
if (postData) {
try {
const studyAreaGeoJson = JSON.parse(postData.studyArea);
Expand All @@ -172,6 +179,8 @@ class SelectDatasetArea extends Vue {
console.log("error parsing geojson: ", e);
}
}
let body = "";
let chunk = "";
} else {
console.log("not a post request");
}
Expand Down

0 comments on commit 9f3e33f

Please sign in to comment.