From 151ba9a1ce9d9832e9f984d4af7e38499005dd02 Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Thu, 18 Jan 2024 19:15:12 +0100 Subject: [PATCH] Refactor to use ES modules, add eslint, fix issues and CI (#83) --- .editorconfig | 17 +++ .eslintrc.yml | 15 +++ .github/workflows/tests.yml | 16 ++- config.json | 58 ++++----- docker-compose.yml | 18 +-- jest.config.js | 20 ---- jest.config.json | 17 +++ package.json | 110 +++++++++--------- src/adduser.js | 73 ++++++------ src/api/capabilities.js | 8 +- src/api/collections.js | 14 +-- src/api/files.js | 18 +-- src/api/jobs.js | 40 +++---- src/api/processes.js | 6 +- src/api/services.js | 29 ++--- src/api/storedprocessgraphs.js | 14 +-- src/api/users.js | 10 +- src/models/catalog.js | 12 +- src/models/jobstore.js | 16 +-- src/models/logs.js | 15 ++- src/models/processgraphstore.js | 9 +- src/models/servicestore.js | 14 +-- src/models/userstore.js | 14 +-- src/models/workspace.js | 21 ++-- src/processes/absolute.js | 14 +-- src/processes/add.js | 8 +- src/processes/add_dimension.js | 8 +- src/processes/aggregate_temporal_frequency.js | 13 +-- src/processes/anomaly.js | 8 +- src/processes/apply.js | 10 +- src/processes/arccos.js | 8 +- src/processes/arcosh.js | 8 +- src/processes/arcsin.js | 8 +- src/processes/arctan.js | 8 +- src/processes/array_element.js | 8 +- src/processes/arsinh.js | 8 +- src/processes/artanh.js | 10 +- src/processes/ceil.js | 8 +- src/processes/climatological_normal.js | 33 +++--- src/processes/clip.js | 8 +- src/processes/cos.js | 8 +- src/processes/cosh.js | 8 +- src/processes/create_raster_cube.js | 8 +- src/processes/debug.js | 19 ++- src/processes/dimension_labels.js | 6 +- src/processes/divide.js | 8 +- src/processes/drop_dimension.js | 8 +- src/processes/e.js | 6 +- src/processes/exp.js | 8 +- src/processes/filter_bands.js | 8 +- src/processes/filter_bbox.js | 8 +- src/processes/filter_spatial.js | 8 +- src/processes/filter_temporal.js | 8 +- src/processes/first.js | 7 +- src/processes/floor.js | 8 +- src/processes/if.js | 6 +- src/processes/int.js | 8 +- src/processes/last.js | 7 +- src/processes/linear_scale_range.js | 8 +- src/processes/ln.js | 8 +- src/processes/load_collection.js | 10 +- src/processes/log.js | 8 +- src/processes/mask.js | 6 +- src/processes/max.js | 8 +- src/processes/mean.js | 9 +- src/processes/median.js | 15 ++- src/processes/merge_cubes.js | 6 +- src/processes/min.js | 8 +- src/processes/multiply.js | 8 +- src/processes/normalized_difference.js | 8 +- src/processes/pi.js | 6 +- src/processes/power.js | 8 +- src/processes/product.js | 8 +- src/processes/reduce_dimension.js | 8 +- src/processes/rename_dimension.js | 8 +- src/processes/rename_labels.js | 8 +- src/processes/round.js | 8 +- src/processes/save_result.js | 9 +- src/processes/sd.js | 9 +- src/processes/sin.js | 8 +- src/processes/sinh.js | 8 +- src/processes/sqrt.js | 8 +- src/processes/subtract.js | 8 +- src/processes/sum.js | 10 +- src/processes/tan.js | 8 +- src/processes/tanh.js | 8 +- src/processes/text_begins.js | 6 +- src/processes/text_contains.js | 6 +- src/processes/text_ends.js | 6 +- src/processes/text_merge.js | 6 +- src/processes/variance.js | 9 +- src/processgraph/commons.js | 66 ++++++----- src/processgraph/context.js | 14 +-- src/processgraph/datacube.js | 16 +-- src/processgraph/dimension.js | 4 +- src/processgraph/jsonschema.js | 20 ++-- src/processgraph/node.js | 17 +-- src/processgraph/processgraph.js | 18 +-- src/processgraph/registry.js | 25 ++-- src/server.js | 44 +++---- src/sync.js | 30 +++-- src/utils/config.js | 10 +- src/utils/db.js | 18 +-- src/utils/errors.js | 18 +-- src/utils/http.js | 9 +- src/utils/servercontext.js | 24 ++-- src/utils/utils.js | 31 ++--- tests/processgraph.test.js | 23 ++-- 108 files changed, 802 insertions(+), 749 deletions(-) create mode 100644 .editorconfig create mode 100644 .eslintrc.yml delete mode 100644 jest.config.js create mode 100644 jest.config.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..50f3a56 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# 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 + +[*.{yaml,yml}] +charset = utf-8 +end_of_line = crlf +indent_style = spaces +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true \ No newline at end of file diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..66c64f9 --- /dev/null +++ b/.eslintrc.yml @@ -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 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6d38e5d..4fbe154 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -3,10 +3,18 @@ on: [push, pull_request] jobs: deploy: runs-on: ubuntu-latest + strategy: + matrix: + node-version: + - 17 # minimum req. version right now + - lts/* + - latest steps: - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v4 with: - node-version: '16' - - uses: actions/checkout@v2 + node-version: ${{ matrix.node-version }} + - uses: actions/checkout@v4 - run: npm install - - run: npm test \ No newline at end of file + - run: npm run lint + - run: npm run sync + - run: npm test diff --git a/config.json b/config.json index 63c41bc..9d14d30 100644 --- a/config.json +++ b/config.json @@ -1,30 +1,30 @@ { - "debug": true, - "hostname": "127.0.0.1", - "port": 8080, - "exposePort": null, - "apiPath": "/v1", - "ssl": { - "port": 443, - "exposePort": null, - "key": null, - "certificate": null - }, - "serviceAccountCredentialsFile": "privatekey.json", - "googleProjectId": "", - "id": "openeo-earthengine-driver", - "title": "Google Earth Engine Proxy for openEO", - "description": "This is the Google Earth Engine Driver for openEO.\n\nGoogle Earth Engine is a planetary-scale platform for Earth science data & analysis. It is powered by Google's cloud infrastructure and combines a multi-petabyte catalog of satellite imagery and geospatial datasets with planetary-scale analysis capabilities. Google makes it available for scientists, researchers, and developers to detect changes, map trends, and quantify differences on the Earth's surface. Google Earth Engine is free for research, education, and nonprofit use.", - "currency": "USD", - "plans": { - "default": "free", - "options": [ - { - "name": "free", - "description": "Earth Engine is free for research, education, and nonprofit use. For commercial applications, Google offers paid commercial licenses. Please contact earthengine-commercial@google.com for details.", - "paid": false - } - ] - }, - "otherVersions": [] -} \ No newline at end of file + "debug": true, + "hostname": "127.0.0.1", + "port": 8080, + "exposePort": null, + "apiPath": "/v1", + "ssl": { + "port": 443, + "exposePort": null, + "key": null, + "certificate": null + }, + "serviceAccountCredentialsFile": "privatekey.json", + "googleProjectId": "", + "id": "openeo-earthengine-driver", + "title": "Google Earth Engine Proxy for openEO", + "description": "This is the Google Earth Engine Driver for openEO.\n\nGoogle Earth Engine is a planetary-scale platform for Earth science data & analysis. It is powered by Google's cloud infrastructure and combines a multi-petabyte catalog of satellite imagery and geospatial datasets with planetary-scale analysis capabilities. Google makes it available for scientists, researchers, and developers to detect changes, map trends, and quantify differences on the Earth's surface. Google Earth Engine is free for research, education, and nonprofit use.", + "currency": "USD", + "plans": { + "default": "free", + "options": [ + { + "name": "free", + "description": "Earth Engine is free for research, education, and nonprofit use. For commercial applications, Google offers paid commercial licenses. Please contact earthengine-commercial@google.com for details.", + "paid": false + } + ] + }, + "otherVersions": [] +} diff --git a/docker-compose.yml b/docker-compose.yml index 8bc3fb5..ca87314 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,11 @@ version: '3' services: - openeo-gee-driver: - build: - context: . - dockerfile: DOCKERFILE - image: openeo-gee:0.3.0 - container_name: openeo-earthengine-driver - ports: - - "80:80" - - "443:443" \ No newline at end of file + openeo-gee-driver: + build: + context: . + dockerfile: DOCKERFILE + image: openeo-gee:0.3.0 + container_name: openeo-earthengine-driver + ports: + - "80:80" + - "443:443" diff --git a/jest.config.js b/jest.config.js deleted file mode 100644 index b7cad3f..0000000 --- a/jest.config.js +++ /dev/null @@ -1,20 +0,0 @@ -module.exports = { - - // Indicates whether the coverage information should be collected while executing the test - collectCoverage: true, - - // The directory where Jest should output its coverage files - coverageDirectory: "coverage", - - // Make calling deprecated APIs throw helpful error messages - errorOnDeprecated: true, - - // Use this configuration option to add custom reporters to Jest - "reporters": [ - "default", - ["./node_modules/jest-html-reporter", { - "pageTitle": "Test Report for openeo-earthengine-driver" - }] - ] - -}; diff --git a/jest.config.json b/jest.config.json new file mode 100644 index 0000000..28ed764 --- /dev/null +++ b/jest.config.json @@ -0,0 +1,17 @@ +{ + "testEnvironment": "node", + "collectCoverage": true, + "coverageDirectory": "coverage", + "errorOnDeprecated": true, + "reporters": [ + "default", + [ + "./node_modules/jest-html-reporter", + { "pageTitle": "Test Report for openeo-earthengine-driver" } + ] + ], + "transform": {}, + "fakeTimers": { + "enableGlobally": true + } +} diff --git a/package.json b/package.json index a699f8f..9f0b802 100644 --- a/package.json +++ b/package.json @@ -1,55 +1,59 @@ { - "name": "@openeo/earthengine-driver", - "version": "1.1.0-dev20240116", - "stac_version": "1.0.0", - "description": "An openEO driver for Google Earth Engine.", - "main": "src/server.js", - "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" - }, - "author": "Matthias Mohr", - "license": "Apache-2.0", - "homepage": "http://openeo.org", - "bugs": { - "url": "https://github.com/Open-EO/openeo-earthengine-editor/issues" - }, - "repository": { - "type": "git", - "url": "https://github.com/Open-EO/openeo-earthengine-editor.git" - }, - "devDependencies": { - "jest": "^29.7.0", - "jest-html-reporter": "^3.10.2", - "nodemon": "^3.0.2", - "pm2": "^5.3.0" - }, - "dependencies": { - "@google-cloud/storage": "^7.7.0", - "@google/earthengine": "^0.1.385", - "@openeo/js-commons": "^1.4.1", - "@openeo/js-processgraphs": "^1.3.0", - "@seald-io/nedb": "^4.0.4", - "ajv": "^6.12.6", - "axios": "^1.6.5", - "check-disk-space": "^3.4.0", - "epsg-index": "^2.0.0", - "fs-extra": "^11.2.0", - "proj4": "^2.10.0", - "restify": "^11.1.0" - }, - "engines" : { - "node" : ">=17.0.0" - }, - "nodemonConfig": { - "watch": [ - "src/", - "server.js" - ] - } + "name": "@openeo/earthengine-driver", + "version": "1.1.0-dev20240116", + "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": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --detectOpenHandles", + "debug": "node --nolazy --inspect src/server.js", + "lint": "eslint src/" + }, + "author": "Matthias Mohr", + "license": "Apache-2.0", + "homepage": "http://openeo.org", + "bugs": { + "url": "https://github.com/Open-EO/openeo-earthengine-editor/issues" + }, + "repository": { + "type": "git", + "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", + "pm2": "^5.3.0" + }, + "dependencies": { + "@google-cloud/storage": "^7.7.0", + "@google/earthengine": "^0.1.385", + "@openeo/js-commons": "^1.4.1", + "@openeo/js-processgraphs": "^1.3.0", + "@seald-io/nedb": "^4.0.4", + "ajv": "^6.12.6", + "axios": "^1.6.5", + "check-disk-space": "^3.4.0", + "epsg-index": "^2.0.0", + "fs-extra": "^11.2.0", + "proj4": "^2.10.0", + "restify": "^11.1.0" + }, + "engines": { + "node": ">=17.0.0" + }, + "nodemonConfig": { + "watch": [ + "src/", + "server.js" + ] + } } diff --git a/src/adduser.js b/src/adduser.js index 701441f..be3d764 100644 --- a/src/adduser.js +++ b/src/adduser.js @@ -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(); \ No newline at end of file +try { + await users.register(username, password); + console.log('User created!'); + stop(0); +} catch (err) { + console.error(err); + stop(1); +} diff --git a/src/api/capabilities.js b/src/api/capabilities.js index f9d8f39..bc749b4 100644 --- a/src/api/capabilities.js +++ b/src/api/capabilities.js @@ -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 = []; @@ -134,4 +134,4 @@ module.exports = class CapabilitiesAPI { output: this.context.outputFormats }); } -}; \ No newline at end of file +} diff --git a/src/api/collections.js b/src/api/collections.js index f414d58..cab9889 100644 --- a/src/api/collections.js +++ b/src/api/collections.js @@ -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' @@ -65,7 +65,7 @@ module.exports = class Data { ] }); } - + async getCollectionById(req, res) { const id = req.params['*']; if (id.length === 0) { @@ -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(); @@ -108,4 +108,4 @@ module.exports = class Data { }); } -}; +} diff --git a/src/api/files.js b/src/api/files.js index 063ef3a..811152e 100644 --- a/src/api/files.js +++ b/src/api/files.js @@ -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(); @@ -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}); @@ -137,4 +137,4 @@ module.exports = class FilesAPI { }); } -}; +} diff --git a/src/api/jobs.js b/src/api/jobs.js index 104cba5..64a2953 100644 --- a/src/api/jobs.js +++ b/src/api/jobs.js @@ -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(); @@ -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); @@ -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: [] @@ -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 @@ -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); @@ -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) { @@ -235,7 +235,7 @@ module.exports = class JobsAPI { if (job.status === 'error') { const manager = await this.storage.getLogsById(job._id, job.log_level); const logs = await manager.get(null, 1, 'error'); - let error = new Errors.Internal(); + let error = new Errors.Internal({message: 'Unknown failure'}); if (Array.isArray(logs.logs) && logs.logs.length > 0) { error = logs.logs[0]; } @@ -308,7 +308,7 @@ module.exports = class JobsAPI { async patchJob(req, res) { this.init(req); - + if (!Utils.isObject(req.body)) { throw new Errors.RequestBodyMissing(); } @@ -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); @@ -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(); @@ -463,4 +463,4 @@ module.exports = class JobsAPI { return response; } -}; \ No newline at end of file +} diff --git a/src/api/processes.js b/src/api/processes.js index 2d89c3d..7cb5d30 100644 --- a/src/api/processes.js +++ b/src/api/processes.js @@ -1,4 +1,4 @@ -module.exports = class Processes { +export default class Processes { constructor(context) { this.registry = context.processes(); @@ -18,5 +18,5 @@ module.exports = class Processes { links: [] }); } - -}; + +} diff --git a/src/api/services.js b/src/api/services.js index c3a90db..66d616d 100644 --- a/src/api/services.js +++ b/src/api/services.js @@ -1,8 +1,9 @@ -const Utils = require('../utils/utils'); -const Errors = require('../utils/errors'); -const ProcessGraph = require('../processgraph/processgraph'); +import Utils from '../utils/utils.js'; +import Errors from '../utils/errors.js'; +import ProcessGraph from '../processgraph/processgraph.js'; +import Logs from '../models/logs.js'; -module.exports = class ServicesAPI { +export default class ServicesAPI { constructor(context) { this.storage = context.webServices(); @@ -79,10 +80,10 @@ module.exports = class ServicesAPI { links: [] }); } - + async deleteService(req, res) { this.init(req); - + const query = { _id: req.params.service_id, user_id: req.user._id @@ -100,13 +101,13 @@ module.exports = class ServicesAPI { console.error(e); } } - + res.send(204); } async patchService(req, res) { this.init(req); - + if (!Utils.isObject(req.body)) { throw new Errors.RequestBodyMissing(); } @@ -155,12 +156,12 @@ module.exports = class ServicesAPI { } await Promise.all(promises); - + const { numAffected } = await db.updateAsync(query, { $set: data }); if (numAffected === 0) { throw new Errors.Internal({message: 'Number of changed services was zero.'}); } - + res.send(204); const logger = this.storage.getLogsById(req.params.service_id, data.log_level || service.log_level); @@ -188,7 +189,7 @@ module.exports = class ServicesAPI { async postService(req, res) { this.init(req); - + if (!Utils.isObject(req.body)) { throw new Errors.RequestBodyMissing(); } @@ -220,7 +221,7 @@ module.exports = class ServicesAPI { const db = this.storage.database(); const service = 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(service._id, service.log_level); res.header('OpenEO-Identifier', service._id); @@ -259,5 +260,5 @@ module.exports = class ServicesAPI { makeServiceUrl(service) { return Utils.getApiUrl('/' + service.type.toLowerCase() + '/' + service._id + "/{z}/{x}/{y}"); } - -}; + +} diff --git a/src/api/storedprocessgraphs.js b/src/api/storedprocessgraphs.js index 0df28d8..f295363 100644 --- a/src/api/storedprocessgraphs.js +++ b/src/api/storedprocessgraphs.js @@ -1,8 +1,8 @@ -const Utils = require('../utils/utils'); -const Errors = require('../utils/errors'); -const ProcessGraph = require('../processgraph/processgraph'); +import Utils from '../utils/utils.js'; +import Errors from '../utils/errors.js'; +import ProcessGraph from '../processgraph/processgraph.js'; -module.exports = class StoredProcessGraphs { +export default class StoredProcessGraphs { constructor(context) { this.storage = context.storedProcessGraphs(); @@ -51,7 +51,7 @@ module.exports = class StoredProcessGraphs { async putProcessGraph(req, res) { this.init(req); - + if (!Utils.isObject(req.body)) { throw new Errors.RequestBodyMissing(); } @@ -86,7 +86,7 @@ module.exports = class StoredProcessGraphs { async deleteProcessGraph(req, res) { this.init(req); - + const query = { id: req.params.process_graph_id, user_id: req.user._id @@ -147,4 +147,4 @@ module.exports = class StoredProcessGraphs { return response; } -}; +} diff --git a/src/api/users.js b/src/api/users.js index 41768ab..322525a 100644 --- a/src/api/users.js +++ b/src/api/users.js @@ -1,11 +1,11 @@ -const Errors = require('../utils/errors'); -const checkDiskSpace = require('check-disk-space'); +import Errors from '../utils/errors.js'; +import checkDiskSpace from 'check-disk-space'; -module.exports = class UsersAPI { +export default class UsersAPI { constructor(context) { this.storage = context.users(); - this.context = context; + this.context = context; } beforeServerStart(server) { @@ -95,4 +95,4 @@ module.exports = class UsersAPI { res.json(data); } -}; \ No newline at end of file +} diff --git a/src/models/catalog.js b/src/models/catalog.js index c07761e..694127e 100644 --- a/src/models/catalog.js +++ b/src/models/catalog.js @@ -1,7 +1,7 @@ -const Utils = require('../utils/utils'); -const fse = require('fs-extra'); -const path = require('path'); -const { Storage } = require('@google-cloud/storage'); +import Utils from '../utils/utils.js'; +import fse from 'fs-extra'; +import path from 'path'; +import { Storage } from '@google-cloud/storage'; // Rough auto mapping for common band names until GEE lists them. // Optimized for Copernicus S2 data @@ -24,7 +24,7 @@ const commonNames = { lwir12: [11.5, 12.5] }; -module.exports = class DataCatalog { +export default class DataCatalog { constructor(context) { this.dataFolder = 'storage/collections/'; @@ -237,4 +237,4 @@ module.exports = class DataCatalog { return c; } -}; \ No newline at end of file +} diff --git a/src/models/jobstore.js b/src/models/jobstore.js index a34a20c..2fae86f 100644 --- a/src/models/jobstore.js +++ b/src/models/jobstore.js @@ -1,11 +1,11 @@ -const Utils = require('../utils/utils'); -const DB = require('../utils/db'); -const fse = require('fs-extra'); -const path = require('path'); -const Errors = require('../utils/errors'); -const Logs = require('./logs'); +import Utils from '../utils/utils.js'; +import DB from '../utils/db.js'; +import fse from 'fs-extra'; +import path from 'path'; +import Errors from '../utils/errors.js'; +import Logs from './logs.js'; -module.exports = class JobStore { +export default class JobStore { constructor() { this.db = DB.load('jobs'); @@ -102,4 +102,4 @@ module.exports = class JobStore { return p; } -}; \ No newline at end of file +} diff --git a/src/models/logs.js b/src/models/logs.js index a0a47e5..81dcdbe 100644 --- a/src/models/logs.js +++ b/src/models/logs.js @@ -1,13 +1,12 @@ -const fse = require('fs-extra'); -const path = require('path'); -const Errors = require('../utils/errors'); -const Datastore = require('@seald-io/nedb'); -const Utils = require('../utils/utils'); +import fse from 'fs-extra'; +import path from 'path'; +import Datastore from '@seald-io/nedb'; +import Utils from '../utils/utils.js'; const LOG_LEVELS = ['error', 'warning', 'info', 'debug']; var LOG_CACHE = {}; -module.exports = class Logs { +export default class Logs { static checkLevel(level, defaulValue = null) { if (typeof level === 'string') { @@ -54,7 +53,7 @@ module.exports = class Logs { async init() { await fse.ensureDir(path.dirname(this.file)); - + this.db = new Datastore({ filename: this.file }); this.db.stopAutocompaction(); await this.db.loadDatabaseAsync(); @@ -170,4 +169,4 @@ module.exports = class Logs { return {level, logs, links}; } -}; \ No newline at end of file +} diff --git a/src/models/processgraphstore.js b/src/models/processgraphstore.js index 4d9b4cc..e3c06c0 100644 --- a/src/models/processgraphstore.js +++ b/src/models/processgraphstore.js @@ -1,6 +1,5 @@ -const Errors = require('../utils/errors'); -const Utils = require('../utils/utils'); -const DB = require('../utils/db'); +import Errors from '../utils/errors.js'; +import DB from '../utils/db.js'; class ProcessGraphStore { @@ -43,7 +42,7 @@ class ProcessGraphStore { return pg; } -}; +} ProcessGraphStore.FIELDS = [ 'id', @@ -60,4 +59,4 @@ ProcessGraphStore.FIELDS = [ 'process_graph' ]; -module.exports = ProcessGraphStore; \ No newline at end of file +export default ProcessGraphStore; diff --git a/src/models/servicestore.js b/src/models/servicestore.js index db1d3d3..3f8556b 100644 --- a/src/models/servicestore.js +++ b/src/models/servicestore.js @@ -1,10 +1,10 @@ -const Utils = require('../utils/utils'); -const DB = require('../utils/db'); -const path = require('path'); -const fse = require('fs-extra'); -const Logs = require('./logs'); +import Utils from '../utils/utils.js'; +import DB from '../utils/db.js'; +import path from 'path'; +import fse from 'fs-extra'; +import Logs from './logs.js'; -module.exports = class ServiceStore { +export default class ServiceStore { constructor() { this.db = DB.load('services'); @@ -71,4 +71,4 @@ module.exports = class ServiceStore { }; } -}; \ No newline at end of file +} diff --git a/src/models/userstore.js b/src/models/userstore.js index 7ac2f89..086ace4 100644 --- a/src/models/userstore.js +++ b/src/models/userstore.js @@ -1,10 +1,10 @@ -const Utils = require('../utils/utils'); -const DB = require('../utils/db'); -const Errors = require('../utils/errors'); -const crypto = require("crypto"); +import Utils from '../utils/utils.js'; +import DB from '../utils/db.js'; +import Errors from '../utils/errors.js'; +import crypto from "crypto"; + +export default class UserStore { -module.exports = class UserStore { - constructor() { this.db = DB.load('users'); this.tokenDb = DB.load('token'); @@ -124,4 +124,4 @@ module.exports = class UserStore { return user; } -}; \ No newline at end of file +} diff --git a/src/models/workspace.js b/src/models/workspace.js index 734b44e..8ea6b6a 100644 --- a/src/models/workspace.js +++ b/src/models/workspace.js @@ -1,8 +1,9 @@ -const Utils = require('../utils/utils'); -const HttpUtils = require('../utils/http'); -const path = require('path'); +import HttpUtils from '../utils/http.js'; +import Errors from '../utils/errors.js'; +import path from 'path'; +import fse from 'fs-extra'; -module.exports = class FileWorkspace { +export default class FileWorkspace { constructor(folder = './storage/user_files') { this.folder = folder; @@ -38,13 +39,13 @@ module.exports = class FileWorkspace { if (!user_id) { throw new Errors.FilePathInvalid(); } - var p = this.getPath(user_id, p); - if (!p) { + const path = this.getPath(user_id, p); + if (!path) { throw new Errors.FilePathInvalid(); } - - await HttpUtils.isFile(p); - return await fse.readFile(p); + + await HttpUtils.isFile(path); + return await fse.readFile(path); } -} \ No newline at end of file +} diff --git a/src/processes/absolute.js b/src/processes/absolute.js index 4156263..ca7f244 100644 --- a/src/processes/absolute.js +++ b/src/processes/absolute.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class absolute extends BaseProcess { +export default class absolute extends BaseProcess { - async execute(node) { - return Commons.applyInCallback(node, image => image.abs(), x => Math.abs(x)); - } + async execute(node) { + return Commons.applyInCallback(node, image => image.abs(), x => Math.abs(x)); + } -}; \ No newline at end of file +} diff --git a/src/processes/add.js b/src/processes/add.js index 0241c66..e28ae86 100644 --- a/src/processes/add.js +++ b/src/processes/add.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class add extends BaseProcess { +export default class add extends BaseProcess { async execute(node) { return Commons.reduceBinaryInCallback( @@ -11,4 +11,4 @@ module.exports = class add extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/add_dimension.js b/src/processes/add_dimension.js index 330bc25..1330ea3 100644 --- a/src/processes/add_dimension.js +++ b/src/processes/add_dimension.js @@ -1,7 +1,7 @@ -const Errors = require('../utils/errors'); -const { BaseProcess } = require('@openeo/js-processgraphs'); +import Errors from '../utils/errors.js'; +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class add_dimension extends BaseProcess { +export default class add_dimension extends BaseProcess { async execute(node) { var dc = node.getDataCube("data"); @@ -27,4 +27,4 @@ module.exports = class add_dimension extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/aggregate_temporal_frequency.js b/src/processes/aggregate_temporal_frequency.js index e8184ca..30b4f3f 100644 --- a/src/processes/aggregate_temporal_frequency.js +++ b/src/processes/aggregate_temporal_frequency.js @@ -1,15 +1,14 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess, ProcessGraph } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; +import Errors from '../utils/errors.js'; -module.exports = class aggregate_temporal_frequency extends BaseProcess { +export default class aggregate_temporal_frequency extends BaseProcess { /*async*/ reduce(node, imageCollection) { // ToDo processes: Execute reducer, see also #36 // Use ... await Commons.reduce(...); - var callback = node.getArgument('reducer'); - var context = node.getArgument('context'); - + let callback = node.getArgument('reducer'); if (!(callback instanceof ProcessGraph)) { throw new Errors.ProcessArgumentInvalid({ process: this.id, @@ -76,4 +75,4 @@ module.exports = class aggregate_temporal_frequency extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/anomaly.js b/src/processes/anomaly.js index 6dc4624..9b9435b 100644 --- a/src/processes/anomaly.js +++ b/src/processes/anomaly.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class anomaly extends BaseProcess { +export default class anomaly extends BaseProcess { async execute(node) { var dc = node.getDataCube('data'); @@ -24,4 +24,4 @@ module.exports = class anomaly extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/apply.js b/src/processes/apply.js index 0a71ef7..959e2df 100644 --- a/src/processes/apply.js +++ b/src/processes/apply.js @@ -1,8 +1,8 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Errors = require('../utils/errors'); -const ProcessGraph = require('../processgraph/processgraph'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Errors from '../utils/errors.js'; +import ProcessGraph from '../processgraph/processgraph.js'; -module.exports = class apply extends BaseProcess { +export default class apply extends BaseProcess { async execute(node) { var dc = node.getDataCube("data"); @@ -24,4 +24,4 @@ module.exports = class apply extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/arccos.js b/src/processes/arccos.js index 101febb..dde4041 100644 --- a/src/processes/arccos.js +++ b/src/processes/arccos.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class arccos extends BaseProcess { +export default class arccos extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.acos(), x => Math.acos(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/arcosh.js b/src/processes/arcosh.js index 82d6ed9..cbd2049 100644 --- a/src/processes/arcosh.js +++ b/src/processes/arcosh.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class arcosh extends BaseProcess { +export default class arcosh extends BaseProcess { async execute(node) { return Commons.applyInCallback( @@ -18,4 +18,4 @@ module.exports = class arcosh extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/arcsin.js b/src/processes/arcsin.js index f5e926b..601090a 100644 --- a/src/processes/arcsin.js +++ b/src/processes/arcsin.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class arcsin extends BaseProcess { +export default class arcsin extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.asin(), x => Math.asin(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/arctan.js b/src/processes/arctan.js index afe52a4..1629e08 100644 --- a/src/processes/arctan.js +++ b/src/processes/arctan.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class arctan extends BaseProcess { +export default class arctan extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.atan(), x => Math.atan(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/array_element.js b/src/processes/array_element.js index fc73e6d..d35f367 100644 --- a/src/processes/array_element.js +++ b/src/processes/array_element.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Errors = require('../utils/errors'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Errors from '../utils/errors.js'; -module.exports = class array_element extends BaseProcess { +export default class array_element extends BaseProcess { async execute(node) { var data = node.getArgument("data"); @@ -53,4 +53,4 @@ module.exports = class array_element extends BaseProcess { } } -}; \ No newline at end of file +} diff --git a/src/processes/arsinh.js b/src/processes/arsinh.js index 45d18ce..a31f7e6 100644 --- a/src/processes/arsinh.js +++ b/src/processes/arsinh.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class arsinh extends BaseProcess { +export default class arsinh extends BaseProcess { async execute(node) { return Commons.applyInCallback( @@ -18,4 +18,4 @@ module.exports = class arsinh extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/artanh.js b/src/processes/artanh.js index bf74789..dce5078 100644 --- a/src/processes/artanh.js +++ b/src/processes/artanh.js @@ -1,11 +1,11 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class artanh extends BaseProcess { +export default class artanh extends BaseProcess { async execute(node) { return Commons.applyInCallback( - node, + node, image => { // Using artanh formula for calculation (see wikipedia ;) var img_p1 = image.add(1); @@ -19,4 +19,4 @@ module.exports = class artanh extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/ceil.js b/src/processes/ceil.js index 5bdb76d..d6d9ed7 100644 --- a/src/processes/ceil.js +++ b/src/processes/ceil.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class ceil extends BaseProcess { +export default class ceil extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.ceil(), x => Math.ceil(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/climatological_normal.js b/src/processes/climatological_normal.js index 0ff754b..380c637 100644 --- a/src/processes/climatological_normal.js +++ b/src/processes/climatological_normal.js @@ -1,19 +1,19 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); -const Utils = require('../utils/utils'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; +import Utils from '../utils/utils.js'; -module.exports = class climatological_normal extends BaseProcess { +export default class climatological_normal extends BaseProcess { async execute(node) { - var dc = node.getDataCube('data'); - var frequency = node.getArgument('frequency'); + const dc = node.getDataCube('data'); + const frequency = node.getArgument('frequency'); // Get a data cube restricted to the climatological period (default: from 1981 to 2010) - var climatologyPeriod = node.getArgument('climatology_period', ["1981","2010"]).map(x => parseInt(x, 10)); - var start = ee.Date(climatologyPeriod[0] + "-01-01"); - var end = ee.Date(climatologyPeriod[1] + "-12-31"); + const climatologyPeriod = node.getArgument('climatology_period', ["1981","2010"]).map(x => parseInt(x, 10)); + let start = ee.Date(climatologyPeriod[0] + "-01-01"); + let end = ee.Date(climatologyPeriod[1] + "-12-31"); - var labels, range, geeFrequencyName, seasons, geeSeasons; + let labels, range, geeFrequencyName, seasons, geeSeasons, earlyStart; switch (frequency) { case 'daily': labels = range = Utils.sequence(1, 365); @@ -31,7 +31,7 @@ module.exports = class climatological_normal extends BaseProcess { range = geeSeasons.values(); geeFrequencyName = "month"; // Adopt start and end time of climatology period depending on data availability - var earlyStart = start.advance(-1, 'month'); + earlyStart = start.advance(-1, 'month'); start = ee.Algorithms.If( // ToDo processes: Document in process definition dc.imageCollection().filter(ee.Filter.date(earlyStart, start)).size(), earlyStart, @@ -47,7 +47,7 @@ module.exports = class climatological_normal extends BaseProcess { range = geeSeasons.values(); geeFrequencyName = "month"; // Adopt start and end time of climatology period depending on data availability - var earlyStart = start.advance(-2, 'month'); + earlyStart = start.advance(-2, 'month'); start = ee.Algorithms.If( dc.imageCollection().filter(ee.Filter.date(earlyStart, start)).size(), earlyStart, @@ -66,7 +66,7 @@ module.exports = class climatological_normal extends BaseProcess { var filteredData = Commons.filterTemporal(dc, [start, end.advance(1, "day")], 'anomaly', 'climatology_period').imageCollection(); var normals = ee.List(range).map(x => { - var calFilter = null; + let calFilter = null; switch(frequency) { case 'climatology_period': case 'yearly': // alias for climatology_period @@ -74,6 +74,9 @@ module.exports = class climatological_normal extends BaseProcess { case 'tropical_seasons': x = ee.List(x); calFilter = ee.Filter.calendarRange(x.get(0), x.get(-1), geeFrequencyName); + break; + } + switch(frequency) { case 'seasons': case 'tropical_seasons': calFilter = ee.Filter(ee.Algorithms.If( @@ -89,11 +92,11 @@ module.exports = class climatological_normal extends BaseProcess { var firstImg = periodData.first(); return periodData.mean().copyProperties({source: firstImg, properties: firstImg.propertyNames()}); }); - + dc.setData(ee.ImageCollection(normals)); dc.dimT().setValues(labels); return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/clip.js b/src/processes/clip.js index 16a3936..0ec3a90 100644 --- a/src/processes/clip.js +++ b/src/processes/clip.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class clip extends BaseProcess { +export default class clip extends BaseProcess { async execute(node) { var min = node.getArgument('min'); @@ -13,4 +13,4 @@ module.exports = class clip extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/cos.js b/src/processes/cos.js index 99b00fb..1dea472 100644 --- a/src/processes/cos.js +++ b/src/processes/cos.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class cos extends BaseProcess { +export default class cos extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.cos(), x => Math.cos(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/cosh.js b/src/processes/cosh.js index b517651..fb884c0 100644 --- a/src/processes/cosh.js +++ b/src/processes/cosh.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class cosh extends BaseProcess { +export default class cosh extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.cosh(), x => Math.cosh(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/create_raster_cube.js b/src/processes/create_raster_cube.js index b0f51dd..3d46062 100644 --- a/src/processes/create_raster_cube.js +++ b/src/processes/create_raster_cube.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const DataCube = require('../processgraph/datacube'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import DataCube from '../processgraph/datacube.js'; -module.exports = class create_raster_cube extends BaseProcess { +export default class create_raster_cube extends BaseProcess { async execute(node) { let dc = new DataCube(); @@ -9,4 +9,4 @@ module.exports = class create_raster_cube extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/debug.js b/src/processes/debug.js index 53cb4de..8dff502 100644 --- a/src/processes/debug.js +++ b/src/processes/debug.js @@ -1,21 +1,20 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class debug extends BaseProcess { +export default class debug extends BaseProcess { async execute(node) { - var dc = node.getArgument('data'); - var code = node.getArgument('data'); - var level = node.getArgument('data', 'info'); - var message = node.getArgument('data'); + const data = node.getArgument('data'); + const code = node.getArgument('data'); + const level = node.getArgument('data', 'info'); + const message = node.getArgument('data'); - var logger = node.getLogger(); + const logger = node.getLogger(); logger[level](message, data, code); // ToDo 1.2: rename to inspect #81 // ToDo processes: Implement that if GEE objects are passed into data, it requests gee.getInfo on them and logs the result. #81 - return dc; + return data; } -}; \ No newline at end of file +} diff --git a/src/processes/dimension_labels.js b/src/processes/dimension_labels.js index 3b507a6..ce2a7b9 100644 --- a/src/processes/dimension_labels.js +++ b/src/processes/dimension_labels.js @@ -1,6 +1,6 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class dimension_labels extends BaseProcess { +export default class dimension_labels extends BaseProcess { async execute(node) { var dc = node.getArgument('data'); @@ -10,4 +10,4 @@ module.exports = class dimension_labels extends BaseProcess { return Array.from(dimension.values); } -}; \ No newline at end of file +} diff --git a/src/processes/divide.js b/src/processes/divide.js index fb2efcc..2e5063b 100644 --- a/src/processes/divide.js +++ b/src/processes/divide.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class divide extends BaseProcess { +export default class divide extends BaseProcess { //TODO processes: Introducing DivisionByZero error async execute(node) { @@ -12,4 +12,4 @@ module.exports = class divide extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/drop_dimension.js b/src/processes/drop_dimension.js index 7dd5b92..1461c70 100644 --- a/src/processes/drop_dimension.js +++ b/src/processes/drop_dimension.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Errors = require('../utils/errors'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Errors from '../utils/errors.js'; -module.exports = class drop_dimension extends BaseProcess { +export default class drop_dimension extends BaseProcess { async execute(node) { var dc = node.getArgument('data'); @@ -28,4 +28,4 @@ module.exports = class drop_dimension extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/e.js b/src/processes/e.js index 46dae4d..2f79562 100644 --- a/src/processes/e.js +++ b/src/processes/e.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class e extends BaseProcess { +export default class e extends BaseProcess { async execute(node) { var dc = node.getDataCube('data'); dc.setData(Math.E); return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/exp.js b/src/processes/exp.js index b9dd126..bde2481 100644 --- a/src/processes/exp.js +++ b/src/processes/exp.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class exp extends BaseProcess { +export default class exp extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.exp(), x => Math.exp(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/filter_bands.js b/src/processes/filter_bands.js index de5dc78..4e677b9 100644 --- a/src/processes/filter_bands.js +++ b/src/processes/filter_bands.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class filter_bands extends BaseProcess { +export default class filter_bands extends BaseProcess { process(data, bands, node){ return Commons.filterBands(data, bands, node); @@ -13,4 +13,4 @@ module.exports = class filter_bands extends BaseProcess { return this.process(dc, bands, node); } -}; \ No newline at end of file +} diff --git a/src/processes/filter_bbox.js b/src/processes/filter_bbox.js index f5499d3..bb627e6 100644 --- a/src/processes/filter_bbox.js +++ b/src/processes/filter_bbox.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class filter_bbox extends BaseProcess { +export default class filter_bbox extends BaseProcess { async execute(node) { return Commons.filterBbox(node.getDataCube("data"), node.getArgument("extent"), this.id, 'extent'); } -}; \ No newline at end of file +} diff --git a/src/processes/filter_spatial.js b/src/processes/filter_spatial.js index 1cb7625..86f73b4 100644 --- a/src/processes/filter_spatial.js +++ b/src/processes/filter_spatial.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class filter_spatial extends BaseProcess { +export default class filter_spatial extends BaseProcess { async execute(node) { return Commons.filterGeoJSON(node.getData("data"), node.getArgument("geometries"), this.id, 'geometries'); } -}; \ No newline at end of file +} diff --git a/src/processes/filter_temporal.js b/src/processes/filter_temporal.js index 06caf9a..4320f81 100644 --- a/src/processes/filter_temporal.js +++ b/src/processes/filter_temporal.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class filter_temporal extends BaseProcess { +export default class filter_temporal extends BaseProcess { async execute(node) { return Commons.filterTemporal(node.getDataCube("data"), node.getArgument("extent"), node.getArgument("dimension")); } -}; \ No newline at end of file +} diff --git a/src/processes/first.js b/src/processes/first.js index f35a2a6..4cb9fdd 100644 --- a/src/processes/first.js +++ b/src/processes/first.js @@ -1,6 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Errors from '../utils/errors.js'; -module.exports = class first extends BaseProcess { +export default class first extends BaseProcess { geeReducer(node) { return node.getArgument('ignore_nodata', true) ? 'firstNonNull' : 'first'; @@ -23,4 +24,4 @@ module.exports = class first extends BaseProcess { } } -}; \ No newline at end of file +} diff --git a/src/processes/floor.js b/src/processes/floor.js index 41104d6..da400d1 100644 --- a/src/processes/floor.js +++ b/src/processes/floor.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class floor extends BaseProcess { +export default class floor extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.floor(), x => Math.floor(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/if.js b/src/processes/if.js index 1e9e829..a90bbde 100644 --- a/src/processes/if.js +++ b/src/processes/if.js @@ -1,6 +1,6 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class If extends BaseProcess { +export default class If extends BaseProcess { async execute(node) { var value = node.getArgument('value'); @@ -16,4 +16,4 @@ module.exports = class If extends BaseProcess { //} } -}; \ No newline at end of file +} diff --git a/src/processes/int.js b/src/processes/int.js index 744c8fb..6883ada 100644 --- a/src/processes/int.js +++ b/src/processes/int.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class int extends BaseProcess { +export default class int extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.int(), x => parseInt(x, 10)); } -}; \ No newline at end of file +} diff --git a/src/processes/last.js b/src/processes/last.js index ef25b6b..adda86b 100644 --- a/src/processes/last.js +++ b/src/processes/last.js @@ -1,6 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Errors from '../utils/errors.js'; -module.exports = class last extends BaseProcess { +export default class last extends BaseProcess { geeReducer(node) { return node.getArgument('ignore_nodata', true) ? 'lastNonNull' : 'last'; @@ -25,4 +26,4 @@ module.exports = class last extends BaseProcess { } } -}; \ No newline at end of file +} diff --git a/src/processes/linear_scale_range.js b/src/processes/linear_scale_range.js index 6af03df..8d24a88 100644 --- a/src/processes/linear_scale_range.js +++ b/src/processes/linear_scale_range.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class linear_scale_range extends BaseProcess { +export default class linear_scale_range extends BaseProcess { async execute(node) { var inputMin = node.getArgument('inputMin'); @@ -21,4 +21,4 @@ module.exports = class linear_scale_range extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/ln.js b/src/processes/ln.js index 5f30f74..c47f68c 100644 --- a/src/processes/ln.js +++ b/src/processes/ln.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class ln extends BaseProcess { +export default class ln extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.log(), x => Math.log(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/load_collection.js b/src/processes/load_collection.js index 574a7ae..6ec5886 100644 --- a/src/processes/load_collection.js +++ b/src/processes/load_collection.js @@ -1,8 +1,8 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const DataCube = require('../processgraph/datacube'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import DataCube from '../processgraph/datacube.js'; +import Commons from '../processgraph/commons.js'; -module.exports = class load_collection extends BaseProcess { +export default class load_collection extends BaseProcess { async execute(node) { // Load data @@ -48,4 +48,4 @@ module.exports = class load_collection extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/log.js b/src/processes/log.js index 7f6c082..43f0676 100644 --- a/src/processes/log.js +++ b/src/processes/log.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class log extends BaseProcess { +export default class log extends BaseProcess { async execute(node) { var base = node.getArgument('base'); @@ -28,4 +28,4 @@ module.exports = class log extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/mask.js b/src/processes/mask.js index 7fcca05..9aed1c9 100644 --- a/src/processes/mask.js +++ b/src/processes/mask.js @@ -1,6 +1,6 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class mask extends BaseProcess { +export default class mask extends BaseProcess { async execute(node) { var data = node.getArgument("data"); @@ -31,4 +31,4 @@ module.exports = class mask extends BaseProcess { })); } -}; \ No newline at end of file +} diff --git a/src/processes/max.js b/src/processes/max.js index bdb7a8e..ec98659 100644 --- a/src/processes/max.js +++ b/src/processes/max.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class max extends BaseProcess { +export default class max extends BaseProcess { geeReducer() { return 'max'; @@ -15,4 +15,4 @@ module.exports = class max extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/mean.js b/src/processes/mean.js index 4b17143..ce576c3 100644 --- a/src/processes/mean.js +++ b/src/processes/mean.js @@ -1,14 +1,13 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class mean extends BaseProcess { +export default class mean extends BaseProcess { geeReducer() { return 'mean'; } - async execute(node) { + async execute() { throw "Not implemented yet."; } -}; \ No newline at end of file +} diff --git a/src/processes/median.js b/src/processes/median.js index 4cd09e6..9bd741e 100644 --- a/src/processes/median.js +++ b/src/processes/median.js @@ -1,14 +1,13 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class median extends BaseProcess { +export default class median extends BaseProcess { - geeReducer() { - return 'median'; - } + geeReducer() { + return 'median'; + } - async execute(node) { + async execute() { throw "Not implemented yet."; } -}; \ No newline at end of file +} diff --git a/src/processes/merge_cubes.js b/src/processes/merge_cubes.js index 8c0af47..e4d82aa 100644 --- a/src/processes/merge_cubes.js +++ b/src/processes/merge_cubes.js @@ -1,6 +1,6 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class merge_cubes extends BaseProcess { +export default class merge_cubes extends BaseProcess { async execute(node) { var dc1 = node.getArgument("cube1"); @@ -12,4 +12,4 @@ module.exports = class merge_cubes extends BaseProcess { return dc1.merge(dc2); } -}; \ No newline at end of file +} diff --git a/src/processes/min.js b/src/processes/min.js index e3c9f58..12bc8ce 100644 --- a/src/processes/min.js +++ b/src/processes/min.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class min extends BaseProcess { +export default class min extends BaseProcess { geeReducer() { return 'min'; @@ -15,4 +15,4 @@ module.exports = class min extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/multiply.js b/src/processes/multiply.js index 4d34bee..e68de6e 100644 --- a/src/processes/multiply.js +++ b/src/processes/multiply.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class multiply extends BaseProcess { +export default class multiply extends BaseProcess { async execute(node) { return Commons.reduceBinaryInCallback( @@ -11,4 +11,4 @@ module.exports = class multiply extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/normalized_difference.js b/src/processes/normalized_difference.js index 7d93616..dac7c8d 100644 --- a/src/processes/normalized_difference.js +++ b/src/processes/normalized_difference.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class normalized_difference extends BaseProcess { +export default class normalized_difference extends BaseProcess { async execute(node) { return Commons.reduceBinaryInCallback( @@ -11,4 +11,4 @@ module.exports = class normalized_difference extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/pi.js b/src/processes/pi.js index 3f3873e..0487b1c 100644 --- a/src/processes/pi.js +++ b/src/processes/pi.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class pi extends BaseProcess { +export default class pi extends BaseProcess { async execute(node) { var dc = node.getDataCube('data'); dc.setData(Math.PI); return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/power.js b/src/processes/power.js index 3050cda..2ce6c06 100644 --- a/src/processes/power.js +++ b/src/processes/power.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class power extends BaseProcess { +export default class power extends BaseProcess { async execute(node) { var power = node.getArgument('p'); @@ -13,4 +13,4 @@ module.exports = class power extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/product.js b/src/processes/product.js index a54906c..5b3917a 100644 --- a/src/processes/product.js +++ b/src/processes/product.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class product extends BaseProcess { +export default class product extends BaseProcess { geeReducer() { return 'product'; @@ -15,4 +15,4 @@ module.exports = class product extends BaseProcess { (a,b) => a * b ); } -}; \ No newline at end of file +} diff --git a/src/processes/reduce_dimension.js b/src/processes/reduce_dimension.js index 988c289..b3e955c 100644 --- a/src/processes/reduce_dimension.js +++ b/src/processes/reduce_dimension.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class reduce_dimension extends BaseProcess { +export default class reduce_dimension extends BaseProcess { async execute(node) { var dc = node.getDataCube("data"); @@ -12,4 +12,4 @@ module.exports = class reduce_dimension extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/rename_dimension.js b/src/processes/rename_dimension.js index 93fbaad..7619700 100644 --- a/src/processes/rename_dimension.js +++ b/src/processes/rename_dimension.js @@ -1,7 +1,7 @@ -const Errors = require('../utils/errors'); -const { BaseProcess } = require('@openeo/js-processgraphs'); +import Errors from '../utils/errors.js'; +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class rename_dimension extends BaseProcess { +export default class rename_dimension extends BaseProcess { async execute(node) { var dc = node.getDataCube("data"); var srcName = node.getArgument('source'); @@ -24,4 +24,4 @@ module.exports = class rename_dimension extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/rename_labels.js b/src/processes/rename_labels.js index bc5d9ce..018270f 100644 --- a/src/processes/rename_labels.js +++ b/src/processes/rename_labels.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Errors = require('../utils/errors'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Errors from '../utils/errors.js'; -module.exports = class rename_labels extends BaseProcess { +export default class rename_labels extends BaseProcess { process(oldLabels, newLabels){ @@ -39,4 +39,4 @@ module.exports = class rename_labels extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/round.js b/src/processes/round.js index edf58a9..678fdee 100644 --- a/src/processes/round.js +++ b/src/processes/round.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class round extends BaseProcess { +export default class round extends BaseProcess { // ToDo processes: Check whether GEE and JS really follow IEEE 754 rounding behavior async execute(node) { @@ -28,4 +28,4 @@ module.exports = class round extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/save_result.js b/src/processes/save_result.js index 3fc2650..b318304 100644 --- a/src/processes/save_result.js +++ b/src/processes/save_result.js @@ -1,6 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Errors from '../utils/errors.js'; -module.exports = class save_result extends BaseProcess { +export default class save_result extends BaseProcess { async validate(node) { await super.validate(node); @@ -9,7 +10,7 @@ module.exports = class save_result extends BaseProcess { if (!node.getServerContext().isValidOutputFormat(format)) { throw new Errors.FormatUnsupported(); } - var options = node.getArgument("options"); + // var options = node.getArgument("options"); // ToDo processes: Validate the options } @@ -22,4 +23,4 @@ module.exports = class save_result extends BaseProcess { return data; } -}; \ No newline at end of file +} diff --git a/src/processes/sd.js b/src/processes/sd.js index 68c583f..87b44ad 100644 --- a/src/processes/sd.js +++ b/src/processes/sd.js @@ -1,14 +1,13 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class sd extends BaseProcess { +export default class sd extends BaseProcess { // ToDo processes: ignore_nodata parameter geeReducer() { return 'stdDev'; } - async execute(node) { + async execute() { throw "Not implemented yet."; } -}; \ No newline at end of file +} diff --git a/src/processes/sin.js b/src/processes/sin.js index d1242df..84f02ed 100644 --- a/src/processes/sin.js +++ b/src/processes/sin.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class sin extends BaseProcess { +export default class sin extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.sin(), x => Math.sin(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/sinh.js b/src/processes/sinh.js index 2f72185..a99fb36 100644 --- a/src/processes/sinh.js +++ b/src/processes/sinh.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class sinh extends BaseProcess { +export default class sinh extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.sinh(), x => Math.sinh(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/sqrt.js b/src/processes/sqrt.js index 739bcab..198f311 100644 --- a/src/processes/sqrt.js +++ b/src/processes/sqrt.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class sqrt extends BaseProcess { +export default class sqrt extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.sqrt(), x => Math.sqrt(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/subtract.js b/src/processes/subtract.js index fd8ac03..5c3e514 100644 --- a/src/processes/subtract.js +++ b/src/processes/subtract.js @@ -1,7 +1,7 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class subtract extends BaseProcess { +export default class subtract extends BaseProcess { async execute(node) { return Commons.reduceBinaryInCallback( @@ -11,4 +11,4 @@ module.exports = class subtract extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/sum.js b/src/processes/sum.js index 674f536..886afe0 100644 --- a/src/processes/sum.js +++ b/src/processes/sum.js @@ -1,12 +1,12 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class sum extends BaseProcess { +export default class sum extends BaseProcess { geeReducer() { return 'sum'; } - + //ToDo processes: ignore_nodata parameter async execute(node) { return Commons.reduceInCallback( @@ -16,4 +16,4 @@ module.exports = class sum extends BaseProcess { ); } -}; \ No newline at end of file +} diff --git a/src/processes/tan.js b/src/processes/tan.js index 59d6eed..14bb95a 100644 --- a/src/processes/tan.js +++ b/src/processes/tan.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class tan extends BaseProcess { +export default class tan extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.tan(), x => Math.tan(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/tanh.js b/src/processes/tanh.js index 95b5224..a943a4b 100644 --- a/src/processes/tanh.js +++ b/src/processes/tanh.js @@ -1,10 +1,10 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; +import Commons from '../processgraph/commons.js'; -module.exports = class tanh extends BaseProcess { +export default class tanh extends BaseProcess { async execute(node) { return Commons.applyInCallback(node, image => image.tanh(), x => Math.tanh(x)); } -}; \ No newline at end of file +} diff --git a/src/processes/text_begins.js b/src/processes/text_begins.js index 85aba42..f0ac49e 100644 --- a/src/processes/text_begins.js +++ b/src/processes/text_begins.js @@ -1,6 +1,6 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class text_begins extends BaseProcess { +export default class text_begins extends BaseProcess { async execute(node) { var data = node.getArgument('data'); @@ -15,4 +15,4 @@ module.exports = class text_begins extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/text_contains.js b/src/processes/text_contains.js index 96f3fab..db89659 100644 --- a/src/processes/text_contains.js +++ b/src/processes/text_contains.js @@ -1,6 +1,6 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class text_contains extends BaseProcess { +export default class text_contains extends BaseProcess { async execute(node) { var data = node.getArgument('data'); @@ -16,4 +16,4 @@ module.exports = class text_contains extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/text_ends.js b/src/processes/text_ends.js index 1cf55ac..63f91d8 100644 --- a/src/processes/text_ends.js +++ b/src/processes/text_ends.js @@ -1,6 +1,6 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class text_ends extends BaseProcess { +export default class text_ends extends BaseProcess { async execute(node) { var data = node.getArgument('data'); @@ -16,4 +16,4 @@ module.exports = class text_ends extends BaseProcess { return dc; } -}; \ No newline at end of file +} diff --git a/src/processes/text_merge.js b/src/processes/text_merge.js index 0eaaa10..29d9293 100755 --- a/src/processes/text_merge.js +++ b/src/processes/text_merge.js @@ -1,6 +1,6 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class text_merge extends BaseProcess { +export default class text_merge extends BaseProcess { async execute(node) { var data = node.getArgument('data'); @@ -13,4 +13,4 @@ module.exports = class text_merge extends BaseProcess { return data.join(separator); } -}; \ No newline at end of file +} diff --git a/src/processes/variance.js b/src/processes/variance.js index 0f79f43..53512c8 100644 --- a/src/processes/variance.js +++ b/src/processes/variance.js @@ -1,14 +1,13 @@ -const { BaseProcess } = require('@openeo/js-processgraphs'); -const Commons = require('../processgraph/commons'); +import { BaseProcess } from '@openeo/js-processgraphs'; -module.exports = class variance extends BaseProcess { +export default class variance extends BaseProcess { geeReducer() { return 'variance'; } - async execute(node) { + async execute() { throw "Not implemented yet."; } -}; \ No newline at end of file +} diff --git a/src/processgraph/commons.js b/src/processgraph/commons.js index 2eb9c9d..8b65dbf 100644 --- a/src/processgraph/commons.js +++ b/src/processgraph/commons.js @@ -1,10 +1,10 @@ -const Errors = require('../utils/errors'); -const Utils = require('../utils/utils'); -const DataCube = require('./datacube'); -const ProcessGraph = require('./processgraph'); +import Errors from '../utils/errors.js'; +import Utils from '../utils/utils.js'; +import DataCube from './datacube.js'; +import ProcessGraph from './processgraph.js'; -module.exports = class Commons { +export default class Commons { // ToDo processes: Also implement ee.Array.* instead only ee.Image.* #35 @@ -80,7 +80,7 @@ module.exports = class Commons { if (!dc.isImageCollection() && !dc.isImage()) { throw new Error("Calculating " + reducerName + " not supported for given data type: " + dc.objectType()); } - + dc.imageCollection(data => data.reduce(reducerFunc)); // revert renaming of the bands following to the GEE convention @@ -92,7 +92,7 @@ module.exports = class Commons { let geeBandName = bandName + "_" + reducerName; rename[geeBandName] = bandName; } - + dc.imageCollection(data => data.map( img => { // Create a GEE list with expected band names @@ -100,7 +100,7 @@ module.exports = class Commons { for(var geeBandName in rename) { geeBands = geeBands.replace(geeBandName, rename[geeBandName]); } - + // Rename bands return img.rename(geeBands); } @@ -150,11 +150,15 @@ module.exports = class Commons { static _reduceBinary(node, eeImgReducer, jsReducer, valA, valB, dataArg = "data") { let result; - var dataCubeA = new DataCube(null, valA); - dataCubeA.setLogger(node.getLogger()); - var dataCubeB = new DataCube(null, valB); - dataCubeA.setLogger(node.getLogger()); - var imgReducer = (a,b) => eeImgReducer(a,b).copyProperties({source: a, properties: a.propertyNames()}); + + let dataCubeA = new DataCube(null, valA); + dataCubeA.setLogger(node.getLogger()); + + let dataCubeB = new DataCube(null, valB); + dataCubeA.setLogger(node.getLogger()); + + let imgReducer = (a,b) => eeImgReducer(a,b).copyProperties({source: a, properties: a.propertyNames()}); + if (typeof valA === 'undefined' && typeof valB === 'undefined') { // Should be caught by reduce(Binary)InCallback already... throw new Errors.UndefinedElements({ @@ -168,7 +172,7 @@ module.exports = class Commons { return valA; } else if (typeof valA === 'number') { - var imgA = ee.Image(valA); + let imgA = ee.Image(valA); if (typeof valB === 'number') { result = jsReducer(valA, valB); } @@ -188,17 +192,17 @@ module.exports = class Commons { } else if (dataCubeA.isImageCollection()) { if (typeof valB === 'number' || dataCubeB.isImage()) { - var imgB = typeof valB === 'number' ? ee.Image(valB) : dataCubeB.image(); + let imgB = typeof valB === 'number' ? ee.Image(valB) : dataCubeB.image(); result = dataCubeA.imageCollection().map(imgA => imgReducer(imgA, imgB)); } else if (dataCubeB.isImageCollection()) { - var collA = dataCubeA.imageCollection(); - var collB = dataCubeB.imageCollection(); - var listA = collA.toList(collA.size()); - var listB = collB.toList(collB.size()); + let collA = dataCubeA.imageCollection(); + let collB = dataCubeB.imageCollection(); + let listA = collA.toList(collA.size()); + let listB = collB.toList(collB.size()); result = collA.map(imgA => { - var index = listA.indexOf(imgA); - var imgB = listB.get(index); + let index = listA.indexOf(imgA); + let imgB = listB.get(index); return imgReducer(imgA, imgB); }); } @@ -212,7 +216,7 @@ module.exports = class Commons { } else if (dataCubeA.isImage()) { if (typeof valB === 'number' || dataCubeB.isImage()) { - var imgB = typeof valB === 'number' ? ee.Image(valB) : dataCubeB.image(); + let imgB = typeof valB === 'number' ? ee.Image(valB) : dataCubeB.image(); result = imgReducer(dataCubeA.image(), imgB); } else if (dataCubeB.isImageCollection()) { @@ -237,10 +241,10 @@ module.exports = class Commons { } static applyInCallback(node, eeImgProcess, jsProcess = null, dataArg = "x") { - var data = node.getArgument(dataArg); - var dc = new DataCube(null, data); - dc.setLogger(node.getLogger()); - var imgProcess = a => eeImgProcess(a).copyProperties({source: a, properties: a.propertyNames()}); + let data = node.getArgument(dataArg); + let dc = new DataCube(null, data); + dc.setLogger(node.getLogger()); + let imgProcess = a => eeImgProcess(a).copyProperties({source: a, properties: a.propertyNames()}); if (dc.isNull()) { return null; } @@ -259,8 +263,8 @@ module.exports = class Commons { } static restrictToSpatialExtent(dc) { - var bbox = dc.getSpatialExtent(); - var geom = ee.Geometry.Rectangle([bbox.west, bbox.south, bbox.east, bbox.north], Utils.crsToString(bbox.crs, 4326)); + const bbox = dc.getSpatialExtent(); + const geom = ee.Geometry.Rectangle([bbox.west, bbox.south, bbox.east, bbox.north], Utils.crsToString(bbox.crs, 4326)); dc.imageCollection(ic => ic.filterBounds(geom)); return dc; } @@ -377,7 +381,7 @@ module.exports = class Commons { } // prepare image collection with aggregation labels - var images = images.sort('system:time_start'); + images = images.sort('system:time_start'); switch (frequency) { case 'hourly': case 'daily': @@ -393,7 +397,7 @@ module.exports = class Commons { // This is are lists with relative months, e.g. 0 is december of the prev. year, -1 is november etc. seasons = ee.Dictionary(seasons); // Convert the relative months like -1 to their absolute values like 11. - var realSeasons = seasons.map(label, months => { + var realSeasons = seasons.map((label, months) => { return ee.List(months).map(m => { var num = ee.Number(m); return ee.Algorithms.If(num.lt(1), num.add(12), num); @@ -421,4 +425,4 @@ module.exports = class Commons { } } -}; +} diff --git a/src/processgraph/context.js b/src/processgraph/context.js index 3e3fc47..de0d3d6 100644 --- a/src/processgraph/context.js +++ b/src/processgraph/context.js @@ -1,10 +1,10 @@ -const Utils = require('../utils/utils'); -const HttpUtils = require('../utils/http'); -const Errors = require('../utils/errors'); -const path = require('path'); -const fse = require('fs-extra'); +import Utils from '../utils/utils.js'; +import HttpUtils from '../utils/http.js'; +import Errors from '../utils/errors.js'; +import path from 'path'; +import fse from 'fs-extra'; -module.exports = class ProcessingContext { +export default class ProcessingContext { constructor(serverContext, userId = null) { this.serverContext = serverContext; @@ -181,4 +181,4 @@ module.exports = class ProcessingContext { } } -}; \ No newline at end of file +} diff --git a/src/processgraph/datacube.js b/src/processgraph/datacube.js index 0e825f0..27536d2 100644 --- a/src/processgraph/datacube.js +++ b/src/processgraph/datacube.js @@ -1,8 +1,8 @@ -const Dimension = require('./dimension'); -const Utils = require('../utils/utils'); -const Errors = require('../utils/errors'); +import Dimension from './dimension.js'; +import Utils from '../utils/utils.js'; +import Errors from '../utils/errors.js'; -module.exports = class DataCube { +export default class DataCube { constructor(sourceDataCube = null, data = undefined) { // Don't set this data directly, always use setData() to reset the type cache! @@ -135,7 +135,7 @@ module.exports = class DataCube { } return this.data; } - + imageCollection(callback = null, ...args) { if (this.isImageCollection()){ // no operation @@ -154,7 +154,7 @@ module.exports = class DataCube { } return this.data; } - + array(callback = null, ...args) { if (this.isEarthEngineArray()) { // no operation @@ -488,7 +488,7 @@ module.exports = class DataCube { } // ToDO processes: add code for overlap resolver and inplace - merge(otherDataCube, overlapResolver=null, inplace=true, context=null){ + merge(otherDataCube){ if (otherDataCube instanceof DataCube) { if (this.isImageCollection() && otherDataCube.isImageCollection()) { this.setData(this.stackCollection(this.data.merge(otherDataCube.data))); @@ -537,4 +537,4 @@ module.exports = class DataCube { return this } -}; \ No newline at end of file +} diff --git a/src/processgraph/dimension.js b/src/processgraph/dimension.js index 4b584ce..23f2360 100644 --- a/src/processgraph/dimension.js +++ b/src/processgraph/dimension.js @@ -1,4 +1,4 @@ -module.exports = class Dimension { +export default class Dimension { constructor(datacube, options) { this.datacube = datacube; @@ -108,4 +108,4 @@ module.exports = class Dimension { } } -}; \ No newline at end of file +} diff --git a/src/processgraph/jsonschema.js b/src/processgraph/jsonschema.js index e7f6cee..7b6cfcf 100644 --- a/src/processgraph/jsonschema.js +++ b/src/processgraph/jsonschema.js @@ -1,7 +1,7 @@ -const { JsonSchemaValidator } = require('@openeo/js-processgraphs'); -const ajv = require('ajv'); +import { JsonSchemaValidator } from '@openeo/js-processgraphs'; +import ajv from 'ajv'; -module.exports = class GeeJsonSchemaValidator extends JsonSchemaValidator { +export default class GeeJsonSchemaValidator extends JsonSchemaValidator { constructor(context) { super(); @@ -16,8 +16,8 @@ module.exports = class GeeJsonSchemaValidator extends JsonSchemaValidator { } return true; } - - async validateJobId() { + + async validateJobId(data) { var job = await this.context.getJob(data); if (job === null) { throw new ajv.ValidationError([{ @@ -26,7 +26,7 @@ module.exports = class GeeJsonSchemaValidator extends JsonSchemaValidator { } return true; } - + async validateOutputFormat(data) { if (!this.context.server().isValidOutputFormat(data)) { throw new ajv.ValidationError([{ @@ -35,7 +35,7 @@ module.exports = class GeeJsonSchemaValidator extends JsonSchemaValidator { } return true; } - + async validateInputFormat(data) { if (!this.context.server().isValidInputFormat(data)) { throw new ajv.ValidationError([{ @@ -44,8 +44,8 @@ module.exports = class GeeJsonSchemaValidator extends JsonSchemaValidator { } return true; } - - async validateProcessGraphId() { + + async validateProcessGraphId(data) { var pg = await this.context.getStoredProcessGraph(data); if (pg === null) { throw new ajv.ValidationError([{ @@ -55,4 +55,4 @@ module.exports = class GeeJsonSchemaValidator extends JsonSchemaValidator { return true; } -} \ No newline at end of file +} diff --git a/src/processgraph/node.js b/src/processgraph/node.js index b1e71b1..9faed7f 100644 --- a/src/processgraph/node.js +++ b/src/processgraph/node.js @@ -1,19 +1,10 @@ -const DataCube = require('./datacube'); -const { ProcessGraphNode } = require('@openeo/js-processgraphs'); +import DataCube from './datacube.js'; +import { ProcessGraphNode } from '@openeo/js-processgraphs'; -module.exports = class GeeProcessGraphNode extends ProcessGraphNode { +export default class GeeProcessGraphNode extends ProcessGraphNode { constructor(json, id, parent) { super(json, id, parent); - this.provision = {}; - } - - setProvision(name, data) { - this.provision[name] = data; - } - - getProvision() { - return this.provision[name]; } getLogger() { @@ -62,4 +53,4 @@ module.exports = class GeeProcessGraphNode extends ProcessGraphNode { return new DataCube(this.getArgument(name)); } -} \ No newline at end of file +} diff --git a/src/processgraph/processgraph.js b/src/processgraph/processgraph.js index d169245..e99928c 100644 --- a/src/processgraph/processgraph.js +++ b/src/processgraph/processgraph.js @@ -1,11 +1,11 @@ -const { ProcessGraph } = require('@openeo/js-processgraphs'); -const GeeJsonSchemaValidator = require('./jsonschema'); -const GeeProcessGraphNode = require('./node'); -const Errors = require('../utils/errors'); -const Utils = require('../utils/utils'); -const epsg = require('epsg-index/all.json'); +import { ProcessGraph } from '@openeo/js-processgraphs'; +import GeeJsonSchemaValidator from './jsonschema.js'; +import GeeProcessGraphNode from './node.js'; +import Errors from '../utils/errors.js'; +import Utils from '../utils/utils.js'; +const epsg = Utils.require('../../package.json'); -module.exports = class GeeProcessGraph extends ProcessGraph { +export default class GeeProcessGraph extends ProcessGraph { constructor(process, context, jsonSchemaValidator = null) { super(process, context.server().processes(), jsonSchemaValidator); @@ -48,7 +48,7 @@ module.exports = class GeeProcessGraph extends ProcessGraph { } createProcessInstance(process) { - var impl = require('../processes/' + process.id + '.js'); + const impl = this.processRegistry.getImplementation(process.id); return new impl(process); } @@ -72,4 +72,4 @@ module.exports = class GeeProcessGraph extends ProcessGraph { this.errors.add(Errors.wrap(error)); } -}; \ No newline at end of file +} diff --git a/src/processgraph/registry.js b/src/processgraph/registry.js index 2bbec6b..ef021b4 100644 --- a/src/processgraph/registry.js +++ b/src/processgraph/registry.js @@ -1,32 +1,41 @@ -const Utils = require('../utils/utils'); -const fse = require('fs-extra'); -const path = require('path'); -const ProcessRegistry = require('@openeo/js-commons/src/processRegistry'); +import Utils from '../utils/utils.js'; +import fse from 'fs-extra'; +import path from 'path'; +import ProcessRegistry from '@openeo/js-commons/src/processRegistry.js'; -module.exports = class GeeProcessRegistry extends ProcessRegistry { +export default class GeeProcessRegistry extends ProcessRegistry { constructor(serverContext) { super(); this.serverContext = serverContext; + this.implementations = {}; } async addFromFolder(folder) { const promises = (await fse.readdir(folder)) .filter(file => file.endsWith('.js')) .map(file => this.addFromFile(path.basename(file, '.js'))); - + await Promise.all(promises); return Utils.size(this.namespace('backend')); } async addFromFile(id) { - let spec = await fse.readJSON('src/processes/' + id + '.json'); + const spec = await fse.readJSON('src/processes/' + id + '.json'); delete spec.process_graph; + this.add(spec, 'backend'); + + const impl = await import('../processes/' + id + '.js'); + this.implementations[id] = impl.default; + } + + getImplementation(id) { + return this.implementations[id] || null; } getServerContext() { return this.serverContext; } -}; \ No newline at end of file +} diff --git a/src/server.js b/src/server.js index 3bd7310..7a115be 100644 --- a/src/server.js +++ b/src/server.js @@ -1,18 +1,20 @@ -const CapabilitiesAPI = require('./api/capabilities'); -const CollectionsAPI = require('./api/collections'); -const FilesAPI = require('./api/files'); -const JobsAPI = require('./api/jobs'); -const ProcessesAPI = require('./api/processes'); -const ProcessGraphsAPI = require('./api/storedprocessgraphs'); -const ServicesAPI = require('./api/services'); -const UsersAPI = require('./api/users'); - -const Utils = require('./utils/utils'); -const ServerContext = require('./utils/servercontext'); - -const fse = require('fs-extra'); -const restify = require('restify'); -global.ee = require('@google/earthengine'); +/*eslint n/no-process-exit: "off"*/ +import CapabilitiesAPI from './api/capabilities.js'; +import CollectionsAPI from './api/collections.js'; +import FilesAPI from './api/files.js'; +import JobsAPI from './api/jobs.js'; +import ProcessesAPI from './api/processes.js'; +import ProcessGraphsAPI from './api/storedprocessgraphs.js'; +import ServicesAPI from './api/services.js'; +import UsersAPI from './api/users.js'; + +import Utils from './utils/utils.js'; +import ServerContext from './utils/servercontext.js'; + +import fse from 'fs-extra'; +import restify from 'restify'; + +global.ee = Utils.require('@google/earthengine'); class Server { @@ -42,7 +44,7 @@ class Server { const privateKey = fse.readJsonSync(this.serverContext.serviceAccountCredentialsFile); ee.data.authenticateViaPrivateKey(privateKey, - () => { + () => { console.info("GEE Authentication succeeded."); ee.initialize(); this.startServer(); @@ -149,8 +151,8 @@ class Server { } - startServerHttp() { - return new Promise((resolve, reject) => { + async startServerHttp() { + return new Promise((resolve) => { const port = process.env.PORT || this.serverContext.port; this.http_server.listen(port, () => { var exposePortStr = this.serverContext.exposePort != 80 ? ":" + this.serverContext.exposePort : ""; @@ -163,7 +165,7 @@ class Server { } startServerHttps() { - return new Promise((resolve, reject) => { + return new Promise((resolve) => { if (this.isHttpsEnabled()) { var sslport = process.env.SSL_PORT || this.serverContext.ssl.port; this.https_server.listen(sslport, () => { @@ -212,6 +214,6 @@ class Server { // Don't call next, this ends execution, nothing more to send. } -}; +} -global.server = new Server(); \ No newline at end of file +global.server = new Server(); diff --git a/src/sync.js b/src/sync.js index e125f79..f7d2aa6 100644 --- a/src/sync.js +++ b/src/sync.js @@ -1,19 +1,15 @@ -const ServerContext = require("./servercontext"); +/*eslint n/no-process-exit: "off"*/ +import ServerContext from "./utils/servercontext.js"; -async function run() { - const serverContext = new ServerContext(); - const catalog = serverContext.collections(); - console.log("Refreshing GEE catalog..."); - catalog.updateCatalog(true) - .then(() => catalog.readLocalCatalog()) - .then(num => { - console.log("Updated catalog with " + num + " collections."); - process.exit(0); - }) - .catch((err) => { - console.error("ERROR: Failed to update catalogs: " + err); - process.exit(1); - }); +const serverContext = new ServerContext(); +const catalog = serverContext.collections(); +console.log("Refreshing GEE catalog..."); +try { + await catalog.updateCatalog(true); + const num = await catalog.readLocalCatalog(); + console.log("Updated catalog with " + num + " collections."); + process.exit(0); +} catch (err) { + console.error("ERROR: Failed to update catalogs: " + err); + process.exit(1); } - -run(); \ No newline at end of file diff --git a/src/utils/config.js b/src/utils/config.js index 0e28df1..16d4816 100644 --- a/src/utils/config.js +++ b/src/utils/config.js @@ -1,4 +1,6 @@ -module.exports = class Config { +import Utils from "./utils.js"; + +export default class Config { constructor() { // Set default that can be overriden by the config.json @@ -19,7 +21,7 @@ module.exports = class Config { port: 443, exposePort: null, key: null, - certificate: null + certificate: null }; this.serviceAccountCredentialsFile = "privatekey.json"; @@ -136,7 +138,7 @@ module.exports = class Config { // Path to check disk usage for (e.g. C: on Windows, / on *nix) this.diskUsagePath = null; - let config = require('../../config.json'); + const config = Utils.require('../../config.json'); for(var c in config) { this[c] = config[c]; } @@ -145,4 +147,4 @@ module.exports = class Config { this.exposePort = this.exposePort || this.port; } -} \ No newline at end of file +} diff --git a/src/utils/db.js b/src/utils/db.js index d081578..9bbb3bc 100644 --- a/src/utils/db.js +++ b/src/utils/db.js @@ -1,19 +1,19 @@ -const Datastore = require('@seald-io/nedb'); +import Datastore from '@seald-io/nedb'; -module.exports = { +export default { - all: [], + all: [], load(name, folder = './storage/database/') { var db = new Datastore({ filename: folder + name + '.db', autoload: true }); db.setAutocompactionInterval(24 * 60 * 60 * 1000); - this.all.push(db); + this.all.push(db); return db; }, - closeAll() { - this.all.forEach(db => db.stopAutocompaction()); - this.all = []; - } + closeAll() { + this.all.forEach(db => db.stopAutocompaction()); + this.all = []; + } -} \ No newline at end of file +} diff --git a/src/utils/errors.js b/src/utils/errors.js index 56cebb4..ec4ce71 100644 --- a/src/utils/errors.js +++ b/src/utils/errors.js @@ -1,11 +1,11 @@ -const openeo_errors = require('../../storage/errors/errors.json'); -const custom_errors = require('../../storage/errors/custom.json'); -const restify_errors = require('restify-errors'); -const { Utils: CommonUtils } = require('@openeo/js-commons'); -const { ErrorList } = require('@openeo/js-processgraphs'); -const util = require('util'); -const Utils = require('./utils'); +import restify_errors from 'restify-errors/lib/index.js'; +import { Utils as CommonUtils } from '@openeo/js-commons'; +import { ErrorList } from '@openeo/js-processgraphs'; +import util from 'util'; +import Utils from './utils.js'; +const openeo_errors = Utils.require('../../storage/errors/errors.json'); +const custom_errors = Utils.require('../../storage/errors/custom.json'); const errors = Object.assign(openeo_errors, custom_errors); const ABOUT_URL = "https://openeo.org/documentation/1.0/developers/api/errors.html"; @@ -71,8 +71,8 @@ restify_errors.wrap = function(e, callback) { return e; // An openEO error } else { - return callback ? callback(e) : new restify_errors.Internal(e); // Probably an internal error + return callback ? callback(e) : new restify_errors.Internal({message: e.message}); // Probably an internal error } }; -module.exports = restify_errors; \ No newline at end of file +export default restify_errors; diff --git a/src/utils/http.js b/src/utils/http.js index c0696a9..2e8438d 100644 --- a/src/utils/http.js +++ b/src/utils/http.js @@ -1,6 +1,6 @@ -const axios = require('axios'); -const Errors = require('./errors'); -const fse = require('fs-extra'); +import axios from 'axios'; +import Errors from './errors.js'; +import fse from 'fs-extra'; var HttpUtils = { @@ -39,5 +39,4 @@ var HttpUtils = { }, }; -module.exports = HttpUtils; - \ No newline at end of file +export default HttpUtils; diff --git a/src/utils/servercontext.js b/src/utils/servercontext.js index d641852..a3ead1f 100644 --- a/src/utils/servercontext.js +++ b/src/utils/servercontext.js @@ -1,16 +1,16 @@ -const Config = require('./config'); -const GeeProcessRegistry = require('../processgraph/registry'); -const ProcessingContext = require('../processgraph/context'); -const Utils = require('./utils'); +import Config from './config.js'; +import GeeProcessRegistry from '../processgraph/registry.js'; +import ProcessingContext from '../processgraph/context.js'; +import Utils from './utils.js'; -const DataCatalog = require('../models/catalog'); -const ProcessGraphStore = require('../models/processgraphstore'); -const FileWorkspace = require('../models/workspace'); -const JobStore = require('../models/jobstore'); -const UserStore = require('../models/userstore'); -const ServiceStore = require('../models/servicestore'); +import DataCatalog from '../models/catalog.js'; +import ProcessGraphStore from '../models/processgraphstore.js'; +import FileWorkspace from '../models/workspace.js'; +import JobStore from '../models/jobstore.js'; +import UserStore from '../models/userstore.js'; +import ServiceStore from '../models/servicestore.js'; -module.exports = class ServerContext extends Config { +export default class ServerContext extends Config { constructor() { super(); @@ -71,4 +71,4 @@ module.exports = class ServerContext extends Config { return new ProcessingContext(this, req.user._id); } -}; \ No newline at end of file +} diff --git a/src/utils/utils.js b/src/utils/utils.js index fbfeb83..010b0d1 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -1,16 +1,21 @@ +import crypto from "crypto"; +import fse from 'fs-extra'; +import path from 'path'; +import { Utils as CommonUtils } from '@openeo/js-commons'; +import proj4 from 'proj4'; +import { createRequire } from "module"; -const crypto = require("crypto"); -const fse = require('fs-extra'); -const path = require('path'); -const { Utils: CommonUtils } = require('@openeo/js-commons'); -const proj4 = require('proj4'); - -var Utils = { +const Utils = { crsBboxes: {}, serverUrl: null, apiPath: null, + require(file) { + const require = createRequire(import.meta.url); + return require(file); + }, + noop(x) { return x; }, @@ -56,7 +61,7 @@ var Utils = { toISODate(timestamp) { return (new Date(timestamp)).toISOString(); }, - + encodeQueryParams(data) { let ret = []; for (let d in data) { @@ -72,15 +77,15 @@ var Utils = { isObject(obj) { return CommonUtils.isObject(obj); }, - + size(obj) { return CommonUtils.size(obj); }, - + omitFromObject(obj, omit) { return CommonUtils.omitFromObject(obj, omit); }, - + pickFromObject(obj, pick) { return CommonUtils.pickFromObject(obj, pick); }, @@ -346,7 +351,7 @@ var Utils = { try { let epsgCode = this.crsToNumber(crs); - const def = require('epsg-index/s/' + epsgCode + '.json'); + const def = Utils.require('epsg-index/s/' + epsgCode + '.json'); proj4.defs(crs, def.proj4); this.crsBboxes[crs] = def.bbox; } catch (error) { @@ -356,4 +361,4 @@ var Utils = { }; -module.exports = Utils; +export default Utils; diff --git a/tests/processgraph.test.js b/tests/processgraph.test.js index d1e68b3..b5affb8 100644 --- a/tests/processgraph.test.js +++ b/tests/processgraph.test.js @@ -1,17 +1,24 @@ -const ServerContext = require('../src/utils/servercontext'); -const DB = require('../src/utils/db'); -const ProcessingContext = require('../src/processgraph/context'); -const GeeProcessGraph = require('../src/processgraph/processgraph'); -const json = require('./data/sample-processgraph.json'); +/* global describe, beforeAll, afterAll, test, expect */ +import ServerContext from '../src/utils/servercontext.js'; +import DB from '../src/utils/db.js'; +import ProcessingContext from '../src/processgraph/context.js'; +import GeeProcessGraph from '../src/processgraph/processgraph.js'; +import Utils from '../src/utils/utils.js'; +import fse from 'fs-extra'; describe('Process Graph Registry', () => { - let serverContext; + let serverContext, json; beforeAll(async () => { + Utils.serverUrl = 'http://localhost:8080'; + Utils.apiPath = '/v1'; + serverContext = new ServerContext(); await serverContext.collections().loadCatalog(); await serverContext.processes().addFromFolder('./src/processes/'); - }); + + json = await fse.readJSON('./tests/data/sample-processgraph.json'); + }, 2 * 60 * 1000); afterAll(() => { DB.closeAll(); @@ -40,4 +47,4 @@ describe('Process Graph Registry', () => { expect(p.getResultNode().id).toBe("save"); }); -}); \ No newline at end of file +});