@samitouri / QOSami-HFS / commits / a53220bf

fix: (regression beta) bad console timestamps on Windows system that have set a non-default region

Massimo Melina committed Nov 21, 2024 at 12:53 UTC a53220bf18ca943f876bc7436a0d5de3a59acd7c
6 files changed +23 -6
admin/src/misc.ts
+4
@@ -22,3 +22,7 @@ export function err2msg(code: string) {
22 ENOTDIR: "Not a folder",
23 }[code] || HTTP_MESSAGES[code as any] || code
24 }
25 +
26 +export function formatTimestamp(x: number | string | Date) {
27 + return !x ? '' : (x instanceof Date ? x : new Date(x)).toLocaleString()
28 +}
src/consoleLog.ts
+2 -1
@@ -1,4 +1,5 @@
1 import events from './events'
2 +import { formatTime } from './cross'
3
4 export const consoleLog: Array<{ ts: Date, k: string, msg: string }> = []
5 for (const k of ['log','warn','error']) {
@@ -10,6 +11,6 @@ for (const k of ['log','warn','error']) {
11 if (consoleLog.length > 100_000) // limit to avoid infinite space
12 consoleLog.splice(0, 1_000)
13 events.emit('console', rec)
13 - return original(ts.toLocaleTimeString(undefined, { hourCycle: 'h24' }), ...args) // bundled nodejs doesn't have locales (and apparently uses en-US)
14 + return original(formatTime(ts), ...args) // bundled nodejs doesn't have locales (and apparently uses en-US)
15 }
16 }
src/const.ts
+2 -1
@@ -5,6 +5,7 @@ import * as fs from 'fs'
5 import { homedir } from 'os'
6 import _ from 'lodash'
7 import { basename, dirname, join } from 'path'
8 +import { formatTimestamp } from './cross'
9 export * from './cross-const'
10
11 export const API_VERSION = 10.1
@@ -43,7 +44,7 @@ if (DEV) console.clear()
44 else console.debug = ()=>{}
45 console.log(`HFS ~ HTTP File Server`)
46 console.log(`© Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt`)
46 -console.log('started', HFS_STARTED.toLocaleString(), DEV)
47 +console.log('started', formatTimestamp(HFS_STARTED), DEV)
48 console.log('version', VERSION||'-')
49 console.log('build', BUILD_TIMESTAMP||'-')
50 console.debug('arguments', argv)
src/cross.ts
+12 -1
@@ -351,7 +351,18 @@ export function repeat(everyMs: number, cb: Callback<Callback>): Callback {
351 }
352
353 export function formatTimestamp(x: number | string | Date) {
354 - return !x ? '' : (x instanceof Date ? x : new Date(x)).toLocaleString()
354 + if (!x) return ''
355 + if (!(x instanceof Date))
356 + x = new Date(x)
357 + return formatDate(x) + ' ' + formatTime(x)
358 +}
359 +
360 +export function formatTime(d: Date) {
361 + return [d.getHours(), d.getMinutes(), d.getSeconds()].map(x => x.toString().padStart(2, '0')).join(':') // bundled nodejs doesn't have locales
362 +}
363 +
364 +export function formatDate(d: Date) {
365 + return [d.getFullYear(), d.getMonth() + 1, d.getDate()].map(x => x.toString().padStart(2, '0')).join('-')
366 }
367
368 export function isNumeric(x: unknown) {
src/index.ts
+1 -1
@@ -60,7 +60,7 @@ function errorHandler(err:Error & { code:string, path:string }) {
60 || code === 'ERR_STREAM_PREMATURE_CLOSE' // happens when many files are sent (not locally), but I checked that the files are written completely. Introduced after node18.5.0 and is thrown by pipeline() used by PUT method handler.
61 || code === 'HPE_INVALID_METHOD' // cannot serve you like that
62 || code === 'HPE_INVALID_EOF_STATE') return // someone interrupted, don't care
63 - console.error(new Date().toLocaleString('en-uk'), 'server error', err)
63 + console.error('server error', err)
64 }
65
66 process.on('uncaughtException', (err: any) => {
src/log.ts
+2 -2
@@ -9,7 +9,7 @@ import { stat } from 'fs/promises'
9 import _ from 'lodash'
10 import { createFileWithPath, prepareFolder } from './util-files'
11 import { getCurrentUsername } from './auth'
12 -import { DAY, makeNetMatcher, tryJson, Dict, Falsy, CFG, strinsert, repeat, HTTP_NOT_FOUND } from './misc'
12 +import { DAY, makeNetMatcher, tryJson, Dict, Falsy, CFG, strinsert, repeat, formatTimestamp, HTTP_NOT_FOUND } from './misc'
13 import { extname } from 'path'
14 import events from './events'
15 import { getConnection } from './connections'
@@ -172,7 +172,7 @@ debugLogFile.once('open', () => {
172 console.error = function(...args: any[]) {
173 was.apply(this, args)
174 args = args.map(x => typeof x === 'string' ? x : (tryJson(x) ?? String(x)))
175 - debugLogFile.write(new Date().toLocaleString() + ': ' + args.join(' ') + '\n')
175 + debugLogFile.write(formatTimestamp(new Date) + ' - ' + args.join(' ') + '\n')
176 }
177 // limit log size
178 const LIMIT = 1_000_000