@nxtedition/lib 23.1.0 → 23.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/app.js +16 -2
  2. package/couch.d.ts +2 -0
  3. package/package.json +1 -1
  4. package/time.js +71 -16
package/app.js CHANGED
@@ -7,6 +7,7 @@ import cluster from 'node:cluster'
7
7
  import stream from 'node:stream'
8
8
  import { Buffer } from 'node:buffer'
9
9
  import { getDockerSecretsSync } from './docker-secrets.js'
10
+ import { getUTCRangeForLocalTime } from './time.js'
10
11
  import fp from 'lodash/fp.js'
11
12
  import {
12
13
  isMainThread,
@@ -135,11 +136,12 @@ export function makeApp(appConfig, onTerminate) {
135
136
  } (module:${serviceModule}; instance:${serviceInstanceId}) worker:${serviceWorkerId} Node/${process.version}`) ??
136
137
  null)
137
138
 
138
- const dailyOffpeakTime = config.dailyOffpeakTime ?? '00:00-04:00'
139
+ const dailyOffpeakTime = config.dailyOffpeakTime ?? getUTCRangeForLocalTime('00:00-04:00')
139
140
 
140
141
  if (dailyOffpeakTime) {
141
- const [start, end] = dailyOffpeakTime.split('-')
142
+ // TODO (fix): This is ugly...
142
143
 
144
+ const [start, end] = dailyOffpeakTime.split('-')
143
145
  let wasOffpeak = null
144
146
  setInterval(() => {
145
147
  if (isTimeBetween(new Date(), start, end)) {
@@ -993,6 +995,18 @@ export function makeApp(appConfig, onTerminate) {
993
995
  const server = http
994
996
  .createServer(async (req, res) => {
995
997
  try {
998
+ if (req.method === 'GET' && req.url === '/stats') {
999
+ res.setHeader('content-type', 'application/json')
1000
+ res.end(JSON.stringify(stats$.value))
1001
+ return
1002
+ }
1003
+
1004
+ if (req.method === 'GET' && req.url === '/status') {
1005
+ res.setHeader('content-type', 'application/json')
1006
+ res.end(JSON.stringify(status$.value))
1007
+ return
1008
+ }
1009
+
996
1010
  if (req.method === 'POST' && req.url === '/gc') {
997
1011
  utilsBC.postMessage({ type: 'utils:gc' })
998
1012
  res.end()
package/couch.d.ts CHANGED
@@ -11,6 +11,7 @@ export interface CouchRequestOptions<Stream extends boolean = false> {
11
11
  url?: URL
12
12
  pathname?: string
13
13
  path?: string
14
+ headers?: Record<string, string>
14
15
  method?: string
15
16
  body?: unknown
16
17
  query?: {
@@ -19,6 +20,7 @@ export interface CouchRequestOptions<Stream extends boolean = false> {
19
20
  update?: boolean
20
21
  seq?: string
21
22
  }
23
+ blocking?: boolean
22
24
  seq?: string
23
25
  signal?: AbortSignal
24
26
  logger?: Logger
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.1.0",
3
+ "version": "23.2.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/time.js CHANGED
@@ -2,31 +2,86 @@ export function isTimeBetween(date, startTime, endTime) {
2
2
  const currentHours = date.getHours()
3
3
  const currentMinutes = date.getMinutes()
4
4
 
5
- let [startHours, startMinutes] = startTime?.split(':').map(Number) ?? [null, 0]
6
- let [endHours, endMinutes] = endTime?.split(':').map(Number) ?? [null, 0]
5
+ // Validate and parse start and end times
6
+ if (!startTime) startTime = '00:00' // Default start at midnight
7
+ if (!endTime) endTime = '23:59' // Default end at 23:59
7
8
 
8
- if (startHours == null) {
9
- startHours = -Number.MAX_SAFE_INTEGER
10
- }
9
+ const [startHours, startMinutes] = startTime.split(':').map(Number)
10
+ const [endHours, endMinutes] = endTime.split(':').map(Number)
11
11
 
12
- if (endHours == null) {
13
- endHours = Number.MAX_SAFE_INTEGER
12
+ // Ensure valid hour/minute values
13
+ if (
14
+ isNaN(startHours) ||
15
+ isNaN(startMinutes) ||
16
+ isNaN(endHours) ||
17
+ isNaN(endMinutes) ||
18
+ startHours < 0 ||
19
+ startHours > 23 ||
20
+ startMinutes < 0 ||
21
+ startMinutes > 59 ||
22
+ endHours < 0 ||
23
+ endHours > 23 ||
24
+ endMinutes < 0 ||
25
+ endMinutes > 59
26
+ ) {
27
+ throw new Error("Invalid time format. Use 'HH:MM'")
14
28
  }
15
29
 
16
30
  if (startHours < endHours || (startHours === endHours && startMinutes <= endMinutes)) {
17
- // The time range is within the same day
18
- return (currentHours > startHours ||
19
- (currentHours === startHours && currentMinutes >= startMinutes)) &&
31
+ // Case 1: The time range is within the same day
32
+ return (
33
+ (currentHours > startHours ||
34
+ (currentHours === startHours && currentMinutes >= startMinutes)) &&
20
35
  (currentHours < endHours || (currentHours === endHours && currentMinutes <= endMinutes))
21
- ? { currentHours, currentMinutes, startHours, startMinutes, endHours, endMinutes }
22
- : null
36
+ )
23
37
  } else {
24
- // The time range spans across two days
25
- return currentHours > startHours ||
38
+ // Case 2: The time range spans across midnight
39
+ return (
40
+ currentHours > startHours ||
26
41
  (currentHours === startHours && currentMinutes >= startMinutes) ||
27
42
  currentHours < endHours ||
28
43
  (currentHours === endHours && currentMinutes <= endMinutes)
29
- ? { currentHours, currentMinutes, startHours, startMinutes, endHours, endMinutes }
30
- : null
44
+ )
45
+ }
46
+ }
47
+
48
+ export function getUTCRangeForLocalTime(range) {
49
+ // Validate input format (hh:mm-hh:mm)
50
+ const timeFormat = /^\d{2}:\d{2}-\d{2}:\d{2}$/
51
+ if (!timeFormat.test(range)) {
52
+ throw new Error("Invalid format. Use 'hh:mm-hh:mm' (e.g., '01:00-05:00').")
53
+ }
54
+
55
+ const [startTime, endTime] = range.split('-')
56
+ const [startHour, startMinute] = startTime.split(':').map(Number)
57
+ const [endHour, endMinute] = endTime.split(':').map(Number)
58
+
59
+ // Validate time values
60
+ if (
61
+ startHour < 0 ||
62
+ startHour > 23 ||
63
+ startMinute < 0 ||
64
+ startMinute > 59 ||
65
+ endHour < 0 ||
66
+ endHour > 23 ||
67
+ endMinute < 0 ||
68
+ endMinute > 59
69
+ ) {
70
+ throw new Error('Invalid time values. Hours must be 00-23 and minutes 00-59.')
31
71
  }
72
+
73
+ const now = new Date()
74
+
75
+ // Create local Date objects
76
+ const start = new Date(now)
77
+ start.setHours(startHour, startMinute, 0, 0)
78
+
79
+ const end = new Date(now)
80
+ end.setHours(endHour, endMinute, 0, 0)
81
+
82
+ // Convert to UTC
83
+ const startUTC = `${String(start.getUTCHours()).padStart(2, '0')}:${String(start.getUTCMinutes()).padStart(2, '0')}`
84
+ const endUTC = `${String(end.getUTCHours()).padStart(2, '0')}:${String(end.getUTCMinutes()).padStart(2, '0')}`
85
+
86
+ return `${startUTC}-${endUTC}`
32
87
  }