Skip to content

Commit

Permalink
fix: Convert TimeInterval.js back to CommonJS
Browse files Browse the repository at this point in the history
Previously, this file was `TimeInterval.mjs` and exported things with
ESM. This was originally done to make Vite happy, but however, this
didn't actually work server-side. This is because the server is written
in CommonJS - and to import ESM from CommonJS, one needs to use dynamic
import. And, since the imports are at the top of the file, TLA
(top-level await) must be active so the module is implemented
(rather than Promise<Module>, which was previously occuring). And, TLA
is only available in ECMAScript Modules. Migrating everything to ESM would
take a lot of work, and it's not guaranteed everything will work the same.

So revisit keeping `TimeInterval.js` CommonJS. This was previously not
possible becaues Vite errored out. But now, Vite accepts this perfectly
fine.
  • Loading branch information
hyperupcall committed May 19, 2023
1 parent 78ed31e commit 9455185
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 7 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"react-dom": "$react-dom"
},
"@formatjs/ecma402-abstract": "1.13.0"

},
"dependencies": {
"@formatjs/intl": "~1.17.0",
Expand Down
105 changes: 105 additions & 0 deletions src/common/TimeInterval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const moment = require('moment')

exports.TimeInterval = class TimeInterval {
constructor(startTimestamp, endTimestamp) {
// utc keeps the moments from changing timezone.
this.startTimestamp = startTimestamp && moment.utc(startTimestamp);
this.endTimestamp = endTimestamp && moment.utc(endTimestamp);
this.isBounded = (this.startTimestamp !== null) && (this.endTimestamp !== null);
}

toString() {
if (this.isBounded) {
// Using '_' as a separator character since it doesn't appear in ISO dates
if (this.startTimestamp === undefined || this.endTimestamp === undefined) {
throw Error('startTimestamp or endTimestamp was undefined in a bounded TimeInterval');
}
return `${this.startTimestamp.format()}_${this.endTimestamp.format()}`;
}
return 'all';
}

equals(other) {
return (other instanceof TimeInterval) && this.toString() === other.toString();
}

/**
* Returns the duration of the time interval
* @param specifier - Optional parameter, defaults to milliseconds
* @returns {number}
*/
duration(specifier) {
if (specifier) {
return this.endTimestamp.diff(this.startTimestamp, specifier);
}
return this.endTimestamp.diff(this.startTimestamp);
}

/**
* Test if this time interval is contains another.
* Intervals are considered to contain equal intervals.
* @param other
* @return {boolean}
*/
contains(other) {
if (!(other instanceof TimeInterval)) {
throw new Error('TimeInterval objects can only be compared to other TimeInterval objects');
}
/* The logic here is:
*
* THIS starts at -∞ OR not after OTHER
* AND
* THIS ends at +∞ OR not before OTHER
*/
return (
((this.startTimestamp === null) || (this.startTimestamp <= other.startTimestamp))
&&
((this.endTimestamp === null) || (this.endTimestamp >= other.endTimestamp))
);
}

/**
* Returns TimeInterval.toString() so that using a time interval as an object key will
* have reasonable behaviour.
* @return {*}
*/
valueOf() {
return this.toString();
}

getStartTimestamp() {
return this.startTimestamp;
}

getEndTimestamp() {
return this.endTimestamp;
}

/**
* Creates a new unbounded time interval
* @return {TimeInterval}
*/
static unbounded() {
return new TimeInterval(null, null);
}

/**
* Creates a new TimeInterval from its string representation
* @param {string} stringified the string representation
* @return {TimeInterval}
*/
static fromString(stringified) {
if (stringified === 'all') {
return TimeInterval.unbounded();
}
// Using '_' as a separator character since it doesn't appear in ISO dates
const [startTimestamp, endTimestamp] = stringified.split('_').map(timestamp => moment(timestamp));
return new TimeInterval(startTimestamp, endTimestamp);
}
}
2 changes: 1 addition & 1 deletion src/server/models/Baseline.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
const database = require('./database');
const { TimeInterval } = import('../../common/TimeInterval.mjs');
const { TimeInterval } = require('../../common/TimeInterval.js');
const sqlFile = database.sqlFile;
class Baseline {
constructor(meterID, applyStart, applyEnd, calcStart, calcEnd, note = null, baselineValue = null) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/readings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const _ = require('lodash');
const moment = require('moment');
const Meter = require('../models/Meter');
const Reading = require('../models/Reading');
const { TimeInterval } = import('../../common/TimeInterval.mjs');
const { TimeInterval } = require('../../common/TimeInterval.js');
const { log } = require('../log');
const validate = require('jsonschema').validate;
const { getConnection } = require('../db');
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/unitReadings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const _ = require('lodash');

const { getConnection } = require('../db');
const Reading = require('../models/Reading');
const { TimeInterval } = import('../../common/TimeInterval.mjs');
const { TimeInterval } = require('../../common/TimeInterval.js');

function validateMeterLineReadingsParams(params) {
const validParams = {
Expand Down
2 changes: 1 addition & 1 deletion src/server/test/routes/unitReadingsRouteTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const { meterLineReadings,
validateBarReadingsQueryParams
} = require('../../routes/unitReadings');

const { TimeInterval } = import('../../../common/TimeInterval.mjs');
const { TimeInterval } = require('../../../common/TimeInterval.js');

function mockResponse() {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/server/test/timeIntervalTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
const expect = chai.expect;
const { TimeInterval } = import('../../common/TimeInterval.mjs');
const { TimeInterval } = require('../../common/TimeInterval.js');


mocha.describe('Time Intervals', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/server/test/web/readings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

const { chai, mocha, expect, app, testDB } = require('../common');
const { TimeInterval } = import('../../../common/TimeInterval.mjs');
const { TimeInterval } = require('../../../common/TimeInterval.js');
const { insertUnits, insertConversions, insertMeters, insertGroups } = require('../../util/insertData');
const Unit = require('../../models/Unit');
const { redoCik } = require('../../services/graph/redoCik');
Expand Down

0 comments on commit 9455185

Please sign in to comment.