Skip to content

Commit

Permalink
chore: apply prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
felixerdy committed Dec 20, 2024
1 parent ec0982a commit 91f1e2a
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ updates:
schedule:
interval: "daily"
assignees:
- "mpfeil"
- "mpfeil"
2 changes: 1 addition & 1 deletion .github/workflows/registry-pr-purge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
token: ${{ secrets.GHCR_TOKEN}}
organization: ${{ github.repository_owner}}
container: ${{ github.event.repository.name }}
tag-regex: pr-${{github.event.pull_request.number}}$
tag-regex: pr-${{github.event.pull_request.number}}$
2 changes: 1 addition & 1 deletion .github/workflows/registry-purge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
token: ${{ secrets.GHCR_TOKEN}}
organization: ${{ github.repository_owner}}
container: ${{ github.event.repository.name }}
untagged: true
untagged: true
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"quoteProps": "preserve"
}
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
"program": "${workspaceFolder}/src/index.js"
}
]
}
}
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# A container based compiler for senseBox Blockly sketches
# A container based compiler for senseBox Blockly sketches

![Build Status](https://github.com/sensebox/sensebox-sketches/actions/workflows/registry-build-publish.yaml/badge.svg)

## Usage
Expand Down Expand Up @@ -32,6 +33,7 @@ You can also run the container image mutliple times. See [Scaling with docker-co
### Endpoints

#### `POST /compile`

- have `application/json` as `content-type`
- contain a valid JSON string with keys `board` and `sketch` with non-empty values.

Expand All @@ -40,22 +42,25 @@ Possible `board` values are `sensebox-mcu` for the new senseBox MCU, `sensebox`
The `sketch` value should be a valid Arduino sketch.

Responses have a `content-type: application/json` header and contains the following response body:

```json
{
"code":201,
"message":"Sketch successfully compiled and created!",
"data":{
"id":"77c1df527a874bd909b56bf1e3906604"
}
"code": 201,
"message": "Sketch successfully compiled and created!",
"data": {
"id": "77c1df527a874bd909b56bf1e3906604"
}
}
```

The `id` is the identifier for your compiled sketch and must be used in the `GET /download/:id` route.

#### `GET /download`

Downloads a compiled sketch.

Parameters:

- `id` is the returned `id` from `/compile`
- `board` specifies which compiled file should be downloaded. Posibile values `sensebox-mcu` or `sensebox`
- `filename` name for the sketch. Default value is `sketch`
Expand Down
4 changes: 2 additions & 2 deletions mocha-reporters.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"reporterEnabled": "spec, mocha-ctrf-json-reporter"
}
"reporterEnabled": "spec, mocha-ctrf-json-reporter"
}
60 changes: 40 additions & 20 deletions src/download.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,65 @@
const fs = require('fs');
const { boardBinaryFileextensions } = require('./builder');
const { HTTPError, rimraf_promise } = require('./utils');
const fs = require("fs");
const { boardBinaryFileextensions } = require("./builder");
const { HTTPError, rimraf_promise } = require("./utils");

const readFile = async function readFile ({ id, board }) {
return Promise.resolve(fs.createReadStream(`/tmp/${id}/sketch.ino.${boardBinaryFileextensions[board]}`));
}
const readFile = async function readFile({ id, board }) {
return Promise.resolve(
fs.createReadStream(
`/tmp/${id}/sketch.ino.${boardBinaryFileextensions[board]}`
)
);
};

const downloadHandler = async function downloadHandler (req, res, next) {
if (req.method !== 'GET') {
return next(new HTTPError({ code: 405, error: 'Invalid HTTP method. Only GET requests allowed on /download.' }));
const downloadHandler = async function downloadHandler(req, res, next) {
if (req.method !== "GET") {
return next(
new HTTPError({
code: 405,
error: "Invalid HTTP method. Only GET requests allowed on /download.",
})
);
}

const { id, board } = req._url.query;

if (!id || !board) {
return next(new HTTPError({ code: 422, error: 'Parameters \'id\' and \'board\' are required' }));
return next(
new HTTPError({
code: 422,
error: "Parameters 'id' and 'board' are required",
})
);
}

// execute builder with parameters from user
try {
const stream = await readFile(req._url.query);
const filename = req._url.query.filename || 'sketch';
stream.on('error', function (err) {
const filename = req._url.query.filename || "sketch";
stream.on("error", function (err) {
return next(err);
});
stream.on('end', async () => {
stream.on("end", async () => {
try {
await rimraf_promise(`/tmp/${req._url.query.id}`)
await rimraf_promise(`/tmp/${req._url.query.id}`);
} catch (error) {
console.log(`Error deleting compile sketch folder with ${req._url.query.id}: `, error);
console.log(
`Error deleting compile sketch folder with ${req._url.query.id}: `,
error
);
}
});

res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Disposition', `attachment; filename=${filename}.${boardBinaryFileextensions[req._url.query.board]}`);
res.setHeader("Content-Type", "application/octet-stream");
res.setHeader(
"Content-Disposition",
`attachment; filename=${filename}.${boardBinaryFileextensions[req._url.query.board]}`
);
stream.pipe(res);
} catch (err) {
return next(new HTTPError({ error: err.message }));
}
}
};

module.exports = {
downloadHandler
}
downloadHandler,
};
10 changes: 5 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const util = require('util');
const rimraf = require('rimraf');
const util = require("util");
const rimraf = require("rimraf");
const rimraf_promise = util.promisify(rimraf);

const HTTPError = function HTTPError ({ code = 500, error = '' }) {
const HTTPError = function HTTPError({ code = 500, error = "" }) {
const err = new Error(error);
err.statusCode = code;

Expand All @@ -11,5 +11,5 @@ const HTTPError = function HTTPError ({ code = 500, error = '' }) {

module.exports = {
HTTPError,
rimraf_promise
};
rimraf_promise,
};
24 changes: 18 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("Compiler", () => {
chai
.request(server)
.post("/compile")
.send({ sketch: 'void setup() {} void loop() {}' })
.send({ sketch: "void setup() {} void loop() {}" })
.end((err, res) => {
res.should.have.status(422);
res.body.should.have
Expand Down Expand Up @@ -51,12 +51,17 @@ describe("Compiler", () => {
chai
.request(server)
.post("/compile")
.send({ board: "esp8266", sketch: 'void setup() {} void loop() {}' })
.send({
board: "esp8266",
sketch: "void setup() {} void loop() {}",
})
.end((err, res) => {
res.should.have.status(422);
res.body.should.have
.property("message")
.eql("Invalid board parameter. Valid values are: sensebox-mcu,sensebox,sensebox-esp32s2");
.eql(
"Invalid board parameter. Valid values are: sensebox-mcu,sensebox,sensebox-esp32s2"
);
done();
});
});
Expand All @@ -71,7 +76,9 @@ describe("Compiler", () => {
res.should.have.status(415);
res.body.should.have
.property("message")
.eql("Invalid Content-Type. Only application/json Content-Type allowed.");
.eql(
"Invalid Content-Type. Only application/json Content-Type allowed."
);
done();
});
});
Expand All @@ -80,12 +87,17 @@ describe("Compiler", () => {
chai
.request(server)
.get("/compile")
.send({ board: "sensebox-mcu", sketch: 'void setup() {} void loop() {}' })
.send({
board: "sensebox-mcu",
sketch: "void setup() {} void loop() {}",
})
.end((err, res) => {
res.should.have.status(405);
res.body.should.have
.property("message")
.eql("Invalid HTTP method. Only POST requests allowed on /compile.");
.eql(
"Invalid HTTP method. Only POST requests allowed on /compile."
);
done();
});
});
Expand Down
5 changes: 3 additions & 2 deletions test/mcu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const server = require("../src/index");
const should = chai.should();
const fs = require("fs");


chai.use(chaiHttp);

describe("Compiler - MCU", () => {
Expand Down Expand Up @@ -62,7 +61,9 @@ describe("Compiler - MCU", () => {
.query({ board: "sensebox-mcu", id: downloadId_mcu })
.end((err, res) => {
res.should.have.status(200);
res.header.should.have.property("content-disposition").eql("attachment; filename=sketch.bin");
res.header.should.have
.property("content-disposition")
.eql("attachment; filename=sketch.bin");
done();
});
});
Expand Down
9 changes: 7 additions & 2 deletions test/mcu_s2.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ describe("Compiler - MCU S2 (ESP32S2)", () => {
});

it("should compile the tof-distance-display sketch for senseBox MCU-S2 ESP32S2", (done) => {
const sketch = fs.readFileSync("test/sketches/mcu_s2/tof-distance-display.ino", "utf8");
const sketch = fs.readFileSync(
"test/sketches/mcu_s2/tof-distance-display.ino",
"utf8"
);
chai
.request(server)
.post("/compile")
Expand All @@ -60,7 +63,9 @@ describe("Compiler - MCU S2 (ESP32S2)", () => {
.query({ board: "sensebox-esp32s2", id: downloadId_esp32s2 })
.end((err, res) => {
res.should.have.status(200);
res.header.should.have.property("content-disposition").eql("attachment; filename=sketch.bin");
res.header.should.have
.property("content-disposition")
.eql("attachment; filename=sketch.bin");
done();
});
});
Expand Down
12 changes: 7 additions & 5 deletions test/uno.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe("Compiler - UNO", () => {
res.should.have.status(200);
res.body.should.be.a("object");
res.body.should.have
.property("message")
.eql("Sketch successfully compiled and created!");
.property("message")
.eql("Sketch successfully compiled and created!");
res.body.should.have.property("data");
res.body.data.should.be.a("object");
res.body.data.should.have.property("id");
Expand All @@ -41,8 +41,8 @@ describe("Compiler - UNO", () => {
res.should.have.status(200);
res.body.should.be.a("object");
res.body.should.have
.property("message")
.eql("Sketch successfully compiled and created!");
.property("message")
.eql("Sketch successfully compiled and created!");
res.body.should.have.property("data");
res.body.data.should.be.a("object");
res.body.data.should.have.property("id");
Expand All @@ -58,7 +58,9 @@ describe("Compiler - UNO", () => {
.query({ board: "sensebox", id: downloadId_uno })
.end((err, res) => {
res.should.have.status(200);
res.header.should.have.property("content-disposition").eql("attachment; filename=sketch.hex");
res.header.should.have
.property("content-disposition")
.eql("attachment; filename=sketch.hex");
done();
});
});
Expand Down

0 comments on commit 91f1e2a

Please sign in to comment.