Skip to content

Commit 186d700

Browse files
authored
Merge pull request #400 from electerious/copilot/refactor-nodejs-server-to-esm
Refactor Node.js server from CommonJS to ESM
2 parents 8303588 + 2b1baf3 commit 186d700

167 files changed

Lines changed: 1014 additions & 1444 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#!/usr/bin/env node
2-
'use strict'
3-
require('dotenv').config()
2+
import 'dotenv/config'
43

5-
const config = require('./src/utils/config')
6-
const customTracker = require('./src/utils/customTracker')
7-
const { index, styles, scripts, tracker, build } = require('./src/ui/index')
4+
import config from './src/utils/config.js'
5+
import * as customTracker from './src/utils/customTracker.js'
6+
import { index, styles, scripts, tracker, build } from './src/ui/index.js'
87

98
// Build files that are identical on every installation
109
if (config.isPreBuildMode === true) {

functions/api.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
31
/**
42
* A serverless function handler for the '/api' route, for use with Netlify.
53
*
64
* See:
75
* - https://docs.netlify.com/functions/overview/
86
*/
9-
exports.handler = require('../src/serverless').handler
7+
export { handler } from '../src/serverless.js'

package-lock.json

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"Tobias Reich <tobias@electerious.com>"
77
],
88
"description": "Self-hosted, Node.js based analytics tool for those who care about privacy",
9+
"type": "module",
910
"main": "src/index.js",
1011
"keywords": [
1112
"server",

src/aggregations/aggregateActions.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
1+
import { INTERVALS_DAILY, INTERVALS_MONTHLY, INTERVALS_YEARLY } from '../constants/intervals.js'
2+
import matchEvents from '../stages/matchEvents.js'
23

3-
const intervals = require('../constants/intervals')
4-
const matchEvents = require('../stages/matchEvents')
5-
6-
module.exports = (ids, average, interval, limit, dateDetails) => {
4+
export default (ids, average, interval, limit, dateDetails) => {
75
const aggregation = [
86
matchEvents(ids),
97
{
@@ -17,9 +15,9 @@ module.exports = (ids, average, interval, limit, dateDetails) => {
1715
aggregation[1].$group.count = average === true ? { $avg: '$value' } : { $sum: '$value' }
1816

1917
const dateExpression = { date: '$created', timezone: dateDetails.userTimeZone }
20-
const matchDay = [ intervals.INTERVALS_DAILY ].includes(interval)
21-
const matchMonth = [ intervals.INTERVALS_DAILY, intervals.INTERVALS_MONTHLY ].includes(interval)
22-
const matchYear = [ intervals.INTERVALS_DAILY, intervals.INTERVALS_MONTHLY, intervals.INTERVALS_YEARLY ].includes(interval)
18+
const matchDay = [ INTERVALS_DAILY ].includes(interval)
19+
const matchMonth = [ INTERVALS_DAILY, INTERVALS_MONTHLY ].includes(interval)
20+
const matchYear = [ INTERVALS_DAILY, INTERVALS_MONTHLY, INTERVALS_YEARLY ].includes(interval)
2321

2422
if (matchDay === true) aggregation[1].$group._id.day = { $dayOfMonth: dateExpression }
2523
if (matchMonth === true) aggregation[1].$group._id.month = { $month: dateExpression }

src/aggregations/aggregateActiveVisitors.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
1+
import { DURATIONS_LIMIT, DURATIONS_INTERVAL } from '../constants/durations.js'
2+
import matchDomains from '../stages/matchDomains.js'
23

3-
const { DURATIONS_LIMIT, DURATIONS_INTERVAL } = require('../constants/durations')
4-
const matchDomains = require('../stages/matchDomains')
5-
6-
module.exports = (ids, dateDetails) => {
4+
export default (ids, dateDetails) => {
75
const aggregation = [
86
matchDomains(ids),
97
{

src/aggregations/aggregateDurations.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
1+
import { INTERVALS_DAILY, INTERVALS_MONTHLY, INTERVALS_YEARLY } from '../constants/intervals.js'
2+
import matchDomains from '../stages/matchDomains.js'
3+
import projectDuration from '../stages/projectDuration.js'
4+
import projectMinInterval from '../stages/projectMinInterval.js'
5+
import matchLimit from '../stages/matchLimit.js'
26

3-
const intervals = require('../constants/intervals')
4-
const matchDomains = require('../stages/matchDomains')
5-
const projectDuration = require('../stages/projectDuration')
6-
const projectMinInterval = require('../stages/projectMinInterval')
7-
const matchLimit = require('../stages/matchLimit')
8-
9-
module.exports = (ids, interval, limit, dateDetails) => {
7+
export default (ids, interval, limit, dateDetails) => {
108
const aggregation = [
119
matchDomains(ids),
1210
projectDuration(),
@@ -25,9 +23,9 @@ module.exports = (ids, interval, limit, dateDetails) => {
2523
aggregation[0].$match.created = { $gte: dateDetails.includeFnByInterval(interval)(limit) }
2624

2725
const dateExpression = { date: '$created', timezone: dateDetails.userTimeZone }
28-
const matchDay = [ intervals.INTERVALS_DAILY ].includes(interval)
29-
const matchMonth = [ intervals.INTERVALS_DAILY, intervals.INTERVALS_MONTHLY ].includes(interval)
30-
const matchYear = [ intervals.INTERVALS_DAILY, intervals.INTERVALS_MONTHLY, intervals.INTERVALS_YEARLY ].includes(interval)
26+
const matchDay = [ INTERVALS_DAILY ].includes(interval)
27+
const matchMonth = [ INTERVALS_DAILY, INTERVALS_MONTHLY ].includes(interval)
28+
const matchYear = [ INTERVALS_DAILY, INTERVALS_MONTHLY, INTERVALS_YEARLY ].includes(interval)
3129

3230
if (matchDay === true) aggregation[4].$group._id.day = { $dayOfMonth: dateExpression }
3331
if (matchMonth === true) aggregation[4].$group._id.month = { $month: dateExpression }

src/aggregations/aggregateNewActions.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
1+
import matchEvents from '../stages/matchEvents.js'
22

3-
const matchEvents = require('../stages/matchEvents')
4-
5-
module.exports = (ids, limit) => {
3+
export default (ids, limit) => {
64
const aggregation = [
75
matchEvents(ids),
86
{

src/aggregations/aggregateNewRecords.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
1+
import matchDomains from '../stages/matchDomains.js'
22

3-
const matchDomains = require('../stages/matchDomains')
4-
5-
module.exports = (ids, properties, limit, or) => {
3+
export default (ids, properties, limit, or) => {
64
const aggregation = [
75
matchDomains(ids),
86
{

src/aggregations/aggregateRecentActions.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
1+
import matchEvents from '../stages/matchEvents.js'
22

3-
const matchEvents = require('../stages/matchEvents')
4-
5-
module.exports = (ids, limit) => {
3+
export default (ids, limit) => {
64
const aggregation = [
75
matchEvents(ids),
86
{

0 commit comments

Comments
 (0)