Skip to content

Commit

Permalink
Recator to use modules + eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Jan 18, 2024
1 parent d4ba037 commit 818c407
Show file tree
Hide file tree
Showing 105 changed files with 659 additions and 643 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# EditorConfig is awesome: https://EditorConfig.org

[*.{js,json,yml}]
charset = utf-8
end_of_line = crlf
indent_style = tabs
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
15 changes: 15 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
env:
node: true
extends:
- eslint:recommended
- plugin:n/recommended
parserOptions:
ecmaVersion: 2022
sourceType: module
globals:
ee: readonly
rules:
n/no-extraneous-import:
- error
- allowModules:
- restify-errors
20 changes: 0 additions & 20 deletions jest.config.js

This file was deleted.

11 changes: 11 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"collectCoverage": true,
"coverageDirectory": "coverage",
"errorOnDeprecated": true,
"reporters": [
"default",
["./node_modules/jest-html-reporter", {
"pageTitle": "Test Report for openeo-earthengine-driver"
}]
]
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"stac_version": "1.0.0",
"description": "An openEO driver for Google Earth Engine.",
"main": "src/server.js",
"type": "module",
"scripts": {
"dev": "nodemon src/server.js --trace-warnings",
"up": "npm run sync && pm2 start src/server.js",
"down": "pm2 stop src/server.js",
"sync": "node src/sync.js",
"adduser": "node src/adduser.js",
"test": "jest --env=node",
"debug": "node --nolazy --inspect src/server.js"
"debug": "node --nolazy --inspect src/server.js",
"lint": "eslint src/"
},
"author": "Matthias Mohr",
"license": "Apache-2.0",
Expand All @@ -24,6 +26,8 @@
"url": "https://github.com/Open-EO/openeo-earthengine-editor.git"
},
"devDependencies": {
"eslint": "^8.56.0",
"eslint-plugin-n": "^16.6.2",
"jest": "^29.7.0",
"jest-html-reporter": "^3.10.2",
"nodemon": "^3.0.2",
Expand All @@ -43,8 +47,8 @@
"proj4": "^2.10.0",
"restify": "^11.1.0"
},
"engines" : {
"node" : ">=17.0.0"
"engines": {
"node": ">=17.0.0"
},
"nodemonConfig": {
"watch": [
Expand Down
73 changes: 35 additions & 38 deletions src/adduser.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
const ServerContext = require("./servercontext");
const { createInterface } = require('node:readline/promises');
/*eslint n/no-process-exit: "off"*/
import ServerContext from "./utils/servercontext.js";
import { createInterface } from 'node:readline/promises';

async function run() {
const serverContext = new ServerContext();
const users = serverContext.users();
const serverContext = new ServerContext();
const users = serverContext.users();

const rl = createInterface({
input: process.stdin,
output: process.stdout
});
const stop = (code) => {
rl.close();
process.exit(code);
}

const username = await rl.question('Enter a username: ');
if (username && username.length < 4) {
console.error("Username must be at least 4 characters long.");
stop(1);
}
const exists = await users.exists(username);
if (exists) {
console.error("User with the given name already exists.");
stop(1);
}
const rl = createInterface({
input: process.stdin,
output: process.stdout
});
const stop = (code) => {
rl.close();
process.exit(code);
}

const password = await rl.question('Enter a password: ');
if (password && password.length < 4) {
console.error("Password must be at least 4 characters long.");
stop(1);
}
const username = await rl.question('Enter a username: ');
if (username && username.length < 4) {
console.error("Username must be at least 4 characters long.");
stop(1);
}
const exists = await users.exists(username);
if (exists) {
console.error("User with the given name already exists.");
stop(1);
}

try {
await users.register(username, password);
console.log('User created!');
stop(0);
} catch (err) {
console.error(err);
stop(1);
}
const password = await rl.question('Enter a password: ');
if (password && password.length < 4) {
console.error("Password must be at least 4 characters long.");
stop(1);
}

run();
try {
await users.register(username, password);
console.log('User created!');
stop(0);
} catch (err) {
console.error(err);
stop(1);
}
8 changes: 4 additions & 4 deletions src/api/capabilities.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Utils = require('../utils/utils');
const packageInfo = require('../../package.json');
import Utils from '../utils/utils.js';
const packageInfo = Utils.require('../../package.json');

module.exports = class CapabilitiesAPI {
export default class CapabilitiesAPI {

constructor(context) {
this.endpoints = [];
Expand Down Expand Up @@ -134,4 +134,4 @@ module.exports = class CapabilitiesAPI {
output: this.context.outputFormats
});
}
};
}
14 changes: 7 additions & 7 deletions src/api/collections.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Utils = require('../utils/utils');
const Errors = require('../utils/errors');
import Utils from '../utils/utils.js';
import Errors from '../utils/errors.js';

module.exports = class Data {
export default class Data {

constructor(context) {
this.catalog = context.collections();

this.geeSourceCatalogLink = {
href: 'https://earthengine-stac.storage.googleapis.com/catalog/catalog.json',
href: 'https://earthengine-stac.storage.googleapis.com/catalog/catalog.json',
rel: 'alternate',
type: 'application/json',
title: 'Machine-readable Earth Engine Data Catalog'
Expand Down Expand Up @@ -65,7 +65,7 @@ module.exports = class Data {
]
});
}

async getCollectionById(req, res) {
const id = req.params['*'];
if (id.length === 0) {
Expand All @@ -92,7 +92,7 @@ module.exports = class Data {
if (req.params['*'] && !req.params.collection_id) {
id = req.params['*'].replace(/\/queryables$/, '');
}

const collection = this.catalog.getData(id);
if (collection === null) {
throw new Errors.CollectionNotFound();
Expand All @@ -108,4 +108,4 @@ module.exports = class Data {
});
}

};
}
18 changes: 9 additions & 9 deletions src/api/files.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const fse = require('fs-extra');
const path = require('path');
const Errors = require('../utils/errors');
const Utils = require('../utils/utils');
const HttpUtils = require('../utils/http');
import fse from 'fs-extra';
import path from 'path';
import Errors from '../utils/errors.js';
import Utils from '../utils/utils.js';
import HttpUtils from '../utils/http.js';

// ToDo files: This is a mock and only uploads to the driver workspace, but not into the actual
// Google cloud storage, which would be required to use it in processes. #11
module.exports = class FilesAPI {
export default class FilesAPI {

constructor(context) {
this.workspace = context.files();
Expand Down Expand Up @@ -61,10 +61,10 @@ module.exports = class FilesAPI {
throw new Errors.FilePathInvalid();
}
} catch (e) {
// File doesn't exist => not a problem for uploading; create missing folders and continue process chain.
// File doesn't exist => not a problem for uploading; create missing folders and continue process chain.
await fse.ensureDir(path.dirname(p));
}

let octetStream = 'application/octet-stream';
if (req.contentType() !== octetStream) {
throw new Errors.ContentTypeInvalid({types: octetStream});
Expand Down Expand Up @@ -137,4 +137,4 @@ module.exports = class FilesAPI {
});
}

};
}
38 changes: 19 additions & 19 deletions src/api/jobs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Utils = require('../utils/utils');
const HttpUtils = require('../utils/http');
const fse = require('fs-extra');
const path = require('path');
const Errors = require('../utils/errors');
const ProcessGraph = require('../processgraph/processgraph');
const packageInfo = require('../../package.json');
const Logs = require('../models/logs');
import Utils from '../utils/utils.js';
import HttpUtils from '../utils/http.js';
import fse from 'fs-extra';
import path from 'path';
import Errors from '../utils/errors.js';
import ProcessGraph from '../processgraph/processgraph.js';
import Logs from '../models/logs.js';
const packageInfo = Utils.require('../../package.json');

module.exports = class JobsAPI {
export default class JobsAPI {

constructor(context) {
this.storage = context.jobs();
Expand Down Expand Up @@ -57,7 +57,7 @@ module.exports = class JobsAPI {

async deliverFile(res, path) {
await HttpUtils.isFile(path);

res.header('Content-Type', Utils.extensionToMediaType(path));
return await new Promise((resolve, reject) => {
var stream = fse.createReadStream(path);
Expand Down Expand Up @@ -85,7 +85,7 @@ module.exports = class JobsAPI {
const db = this.storage.database();
const jobs = (await db.findAsync(query))
.map(job => this.makeJobResponse(job, false));

res.json({
jobs: jobs,
links: []
Expand Down Expand Up @@ -168,7 +168,7 @@ module.exports = class JobsAPI {

logger.info("Queueing batch job");
await this.storage.updateJobStatus(query, 'queued');

res.send(202);

// ToDo sync: move all the following to a worker #77
Expand All @@ -190,7 +190,7 @@ module.exports = class JobsAPI {
url: url,
responseType: 'stream'
});

const extension = context.getExtension(cube.getOutputFormat());
const filePath = this.storage.getJobFile(job._id, Utils.generateHash() + "." + extension);
logger.debug("Storing result to: " + filePath);
Expand All @@ -201,7 +201,7 @@ module.exports = class JobsAPI {
writer.on('error', reject);
writer.on('close', resolve);
});

logger.info("Finished");
this.storage.updateJobStatus(query, 'finished');
} catch(e) {
Expand Down Expand Up @@ -308,7 +308,7 @@ module.exports = class JobsAPI {

async patchJob(req, res) {
this.init(req);

if (!Utils.isObject(req.body)) {
throw new Errors.RequestBodyMissing();
}
Expand Down Expand Up @@ -389,7 +389,7 @@ module.exports = class JobsAPI {
const db = this.storage.database();
const job = await db.insertAsync(data);

// Create logs at creation time to avoid issues described in #51
// Create logs at creation time to avoid issues described in #51
await this.storage.getLogsById(job._id, job.log_level);

res.header('OpenEO-Identifier', job._id);
Expand All @@ -403,8 +403,8 @@ module.exports = class JobsAPI {
throw new Errors.RequestBodyMissing();
}

const plan = req.body.plan || this.context.plans.default;
const budget = req.body.budget || null;
// const plan = req.body.plan || this.context.plans.default;
// const budget = req.body.budget || null;
// ToDo: Validate data, handle budget and plan input #73

const id = Utils.timeId();
Expand Down Expand Up @@ -463,4 +463,4 @@ module.exports = class JobsAPI {
return response;
}

};
}
6 changes: 3 additions & 3 deletions src/api/processes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = class Processes {
export default class Processes {

constructor(context) {
this.registry = context.processes();
Expand All @@ -18,5 +18,5 @@ module.exports = class Processes {
links: []
});
}
};

}
Loading

0 comments on commit 818c407

Please sign in to comment.