openvibers/openvibe.community Commits
JSON times carry their zone (ISO 8601 Z) on the API edge (#403)
Changes
@@@ -103,6 +103,8 @@ async function createApp(opts = {}) {const metrics = require('openvibe-shared/metrics').instrument(app, { service: 'community', release: release.release });app.use(require('openvibe-sdk/service').telemetryMiddleware); // request telemetry: a no-op until server/index.js calls telemetry.init()app.locals.metrics = metrics.registry;++ // Times leave with their zone (openvibe.design rule 148): the SQLite-era text columns are UTC.++ app.use(require('./http/iso-times').isoJsonTimes);app.use(helmet({contentSecurityPolicy: {nnew file mode 100644@@@ -0,0 +1,46 @@++'use strict';++/**++ * Times leave with their zone (openvibe.design rule 148).++ *++ * The tables created from SQLite (migrations/0001_initial.sql) keep timestamps as text in the++ * SQLite shape `YYYY-MM-DD HH:MM:SS`, which ov_now() writes in UTC. A browser reads that string as++ * local time, so west of UTC every event looked hours in the future. This is the edge: JSON fields++ * whose name says they are a time are rendered as ISO 8601 with Z, and nothing else changes — the++ * stored text, cursors built from it and every query that compares it stay exactly as they are.++ *++ * Already-ISO values (with Z or an offset), dates (`YYYY-MM-DD`) and non-strings pass through.++ * `POST /api/v1/...` cursors echo their own stored text, not one of these fields, so pagination is++ * unaffected.++ */++const SQL_TEXT_TIME = /^\d{4}-\d\d-\d\d[ T]\d\d:\d\d(?::\d\d(?:\.\d+)?)?$/;++// created_at, updated_at, started_at, ended_at, expires_at, last_message_at, timestamp, …++// and the bare `created` / `updated` / `started` / `ended` / `expires` some responses use.++const TIME_KEY = /(^|_)(at|created|updated|started|ended|expires|timestamp)$|^last_|^(since|until)$/;++++/** One stored time as ISO 8601 with Z; anything not in the SQLite shape is returned untouched. */++function isoTime(value) {++ return typeof value === 'string' && SQL_TEXT_TIME.test(value) ? `${value.replace(' ', 'T')}Z` : value;++}++++/** Copy `value`, rendering time-named fields; arrays and plain objects are walked, a Date is not. */++function normalizeTimes(value, depth = 0) {++ if (depth > 8 || value === null || typeof value !== 'object') return value;++ if (Array.isArray(value)) return value.map((item) => normalizeTimes(item, depth + 1));++ const proto = Object.getPrototypeOf(value);++ if (proto !== Object.prototype && proto !== null) return value;++ const out = {};++ for (const [key, item] of Object.entries(value)) {++ out[key] = TIME_KEY.test(key) ? isoTime(item) : normalizeTimes(item, depth + 1);++ }++ return out;++}++++/** Express middleware: JSON answers carry ISO times. Internal service routes keep their SQL shape. */++function isoJsonTimes(req, res, next) {++ if (req.path.startsWith('/internal/')) return next();++ const json = res.json.bind(res);++ res.json = (body) => json(normalizeTimes(body));++ next();++}++++module.exports = { TIME_KEY, SQL_TEXT_TIME, isoTime, normalizeTimes, isoJsonTimes };nnew file mode 100644@@@ -0,0 +1,51 @@++'use strict';++/**++ * Times leave with their zone (openvibe.design rule 148): Community's SQLite-era text timestamps are++ * UTC but a browser reads `YYYY-MM-DD HH:MM:SS` as local time. The JSON edge renders time fields as++ * ISO 8601 with Z; the stored text and every query that compares it stay unchanged.++ */++const assert = require('assert');++const { boot, check, done } = require('./helpers/app');++const { isoTime, normalizeTimes } = require('../server/http/iso-times');++++assert.strictEqual(isoTime('2026-11-12 05:01:02'), '2026-11-12T05:01:02Z');++assert.strictEqual(isoTime('2026-11-12T05:01:02.123Z'), '2026-11-12T05:01:02.123Z', 'ISO with Z is left alone');++assert.strictEqual(isoTime('2026-11-12T05:01:02+02:00'), '2026-11-12T05:01:02+02:00', 'ISO with an offset is left alone');++assert.strictEqual(isoTime('2026-11-12'), '2026-11-12', 'a date is not a time');++assert.strictEqual(isoTime(null), null);++assert.deepStrictEqual(++ normalizeTimes({ id: 1, created_at: '2026-11-12 05:01:02', updated_at: null, when: '2026-11-12 05:01:02', meta: { last_seen: '2026-11-12 05:01:02' }, tags: [{ expires_at: '2026-11-12 05:01:02' }] }),++ { id: 1, created_at: '2026-11-12T05:01:02Z', updated_at: null, when: '2026-11-12 05:01:02', meta: { last_seen: '2026-11-12T05:01:02Z' }, tags: [{ expires_at: '2026-11-12T05:01:02Z' }] },++ 'time-named fields are rendered; body text that looks like a time is not',++);++++(async () => {++ const t = await boot();++ const db = t.app.locals.db || t.db;++ const owner = require('openvibe-contracts').ids.newId('user');++ await db.prepare(`INSERT INTO pastes (slug, owner_subject, title, content, visibility, created_at, updated_at)++ VALUES ('iso-time', ?, 'When', 'body', 'public', '2026-10-01 10:00:00', '2026-10-02 11:30:00')`).run(owner);++ await db.prepare("INSERT INTO shares (service, kind, resource_id, owner_subject, audience) VALUES ('community', 'paste', 'iso-time', ?::jsonb, 'public')").run(JSON.stringify({ type: 'user', id: owner }));++++ await check('a paste\'s times are ISO 8601 with Z in the JSON API', async () => {++ const r = await t.get('/api/pastes/iso-time');++ assert.strictEqual(r.status, 200, r.text);++ const body = r.json().paste;++ assert.strictEqual(body.created_at, '2026-10-01T10:00:00Z');++ assert.strictEqual(body.updated_at, '2026-10-02T11:30:00Z');++ assert.match(body.created_at, /Z$/);++ assert.match(body.updated_at, /Z$/);++ });++++ await check('the stored text and the API cursor shape are unchanged', async () => {++ const row = await db.prepare("SELECT created_at, updated_at FROM pastes WHERE slug = 'iso-time'").get();++ assert.deepStrictEqual(row, { created_at: '2026-10-01 10:00:00', updated_at: '2026-10-02 11:30:00' });++ const list = await t.get('/api/v1/pulse?limit=1');++ assert.strictEqual(list.status, 200, list.text);++ const items = list.json().items || [];++ if (items.length) assert.doesNotMatch(String(items[0].occurred_at), /^\d{4}-\d\d-\d\d \d\d:/, 'Pulse times are ISO too');++ });++++ await t.close();++ done();++})().catch((err) => { console.error(err); process.exit(1); });
@@ -103,6 +103,8 @@ async function createApp(opts = {}) {
@@ -103,6 +103,8 @@ async function createApp(opts = {}) {
const metrics = require('openvibe-shared/metrics').instrument(app, { service: 'community', release: release.release });
const metrics = require('openvibe-shared/metrics').instrument(app, { service: 'community', release: release.release });
app.use(require('openvibe-sdk/service').telemetryMiddleware); // request telemetry: a no-op until server/index.js calls telemetry.init()
app.use(require('openvibe-sdk/service').telemetryMiddleware); // request telemetry: a no-op until server/index.js calls telemetry.init()
app.locals.metrics = metrics.registry;
app.locals.metrics = metrics.registry;
+ // Times leave with their zone (openvibe.design rule 148): the SQLite-era text columns are UTC.
+ app.use(require('./http/iso-times').isoJsonTimes);
app.use(helmet({
app.use(helmet({
contentSecurityPolicy: {
contentSecurityPolicy: {
new file mode 100644
new file mode 100644
@@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
+'use strict';
+/**
+ * Times leave with their zone (openvibe.design rule 148).
+ *
+ * The tables created from SQLite (migrations/0001_initial.sql) keep timestamps as text in the
+ * SQLite shape `YYYY-MM-DD HH:MM:SS`, which ov_now() writes in UTC. A browser reads that string as
+ * local time, so west of UTC every event looked hours in the future. This is the edge: JSON fields
+ * whose name says they are a time are rendered as ISO 8601 with Z, and nothing else changes — the
+ * stored text, cursors built from it and every query that compares it stay exactly as they are.
+ *
+ * Already-ISO values (with Z or an offset), dates (`YYYY-MM-DD`) and non-strings pass through.
+ * `POST /api/v1/...` cursors echo their own stored text, not one of these fields, so pagination is
+ * unaffected.
+ */
+const SQL_TEXT_TIME = /^\d{4}-\d\d-\d\d[ T]\d\d:\d\d(?::\d\d(?:\.\d+)?)?$/;
+// created_at, updated_at, started_at, ended_at, expires_at, last_message_at, timestamp, …
+// and the bare `created` / `updated` / `started` / `ended` / `expires` some responses use.
+const TIME_KEY = /(^|_)(at|created|updated|started|ended|expires|timestamp)$|^last_|^(since|until)$/;
+
+/** One stored time as ISO 8601 with Z; anything not in the SQLite shape is returned untouched. */
+function isoTime(value) {
+ return typeof value === 'string' && SQL_TEXT_TIME.test(value) ? `${value.replace(' ', 'T')}Z` : value;
+}
+
+/** Copy `value`, rendering time-named fields; arrays and plain objects are walked, a Date is not. */
+function normalizeTimes(value, depth = 0) {
+ if (depth > 8 || value === null || typeof value !== 'object') return value;
+ if (Array.isArray(value)) return value.map((item) => normalizeTimes(item, depth + 1));
+ const proto = Object.getPrototypeOf(value);
+ if (proto !== Object.prototype && proto !== null) return value;
+ const out = {};
+ for (const [key, item] of Object.entries(value)) {
+ out[key] = TIME_KEY.test(key) ? isoTime(item) : normalizeTimes(item, depth + 1);
+ }
+ return out;
+}
+
+/** Express middleware: JSON answers carry ISO times. Internal service routes keep their SQL shape. */
+function isoJsonTimes(req, res, next) {
+ if (req.path.startsWith('/internal/')) return next();
+ const json = res.json.bind(res);
+ res.json = (body) => json(normalizeTimes(body));
+ next();
+}
+
+module.exports = { TIME_KEY, SQL_TEXT_TIME, isoTime, normalizeTimes, isoJsonTimes };
new file mode 100644
new file mode 100644
@@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
+'use strict';
+/**
+ * Times leave with their zone (openvibe.design rule 148): Community's SQLite-era text timestamps are
+ * UTC but a browser reads `YYYY-MM-DD HH:MM:SS` as local time. The JSON edge renders time fields as
+ * ISO 8601 with Z; the stored text and every query that compares it stay unchanged.
+ */
+const assert = require('assert');
+const { boot, check, done } = require('./helpers/app');
+const { isoTime, normalizeTimes } = require('../server/http/iso-times');
+
+assert.strictEqual(isoTime('2026-11-12 05:01:02'), '2026-11-12T05:01:02Z');
+assert.strictEqual(isoTime('2026-11-12T05:01:02.123Z'), '2026-11-12T05:01:02.123Z', 'ISO with Z is left alone');
+assert.strictEqual(isoTime('2026-11-12T05:01:02+02:00'), '2026-11-12T05:01:02+02:00', 'ISO with an offset is left alone');
+assert.strictEqual(isoTime('2026-11-12'), '2026-11-12', 'a date is not a time');
+assert.strictEqual(isoTime(null), null);
+assert.deepStrictEqual(
+ normalizeTimes({ id: 1, created_at: '2026-11-12 05:01:02', updated_at: null, when: '2026-11-12 05:01:02', meta: { last_seen: '2026-11-12 05:01:02' }, tags: [{ expires_at: '2026-11-12 05:01:02' }] }),
+ { id: 1, created_at: '2026-11-12T05:01:02Z', updated_at: null, when: '2026-11-12 05:01:02', meta: { last_seen: '2026-11-12T05:01:02Z' }, tags: [{ expires_at: '2026-11-12T05:01:02Z' }] },
+ 'time-named fields are rendered; body text that looks like a time is not',
+);
+
+(async () => {
+ const t = await boot();
+ const db = t.app.locals.db || t.db;
+ const owner = require('openvibe-contracts').ids.newId('user');
+ await db.prepare(`INSERT INTO pastes (slug, owner_subject, title, content, visibility, created_at, updated_at)
+ VALUES ('iso-time', ?, 'When', 'body', 'public', '2026-10-01 10:00:00', '2026-10-02 11:30:00')`).run(owner);
+ await db.prepare("INSERT INTO shares (service, kind, resource_id, owner_subject, audience) VALUES ('community', 'paste', 'iso-time', ?::jsonb, 'public')").run(JSON.stringify({ type: 'user', id: owner }));
+
+ await check('a paste\'s times are ISO 8601 with Z in the JSON API', async () => {
+ const r = await t.get('/api/pastes/iso-time');
+ assert.strictEqual(r.status, 200, r.text);
+ const body = r.json().paste;
+ assert.strictEqual(body.created_at, '2026-10-01T10:00:00Z');
+ assert.strictEqual(body.updated_at, '2026-10-02T11:30:00Z');
+ assert.match(body.created_at, /Z$/);
+ assert.match(body.updated_at, /Z$/);
+ });
+
+ await check('the stored text and the API cursor shape are unchanged', async () => {
+ const row = await db.prepare("SELECT created_at, updated_at FROM pastes WHERE slug = 'iso-time'").get();
+ assert.deepStrictEqual(row, { created_at: '2026-10-01 10:00:00', updated_at: '2026-10-02 11:30:00' });
+ const list = await t.get('/api/v1/pulse?limit=1');
+ assert.strictEqual(list.status, 200, list.text);
+ const items = list.json().items || [];
+ if (items.length) assert.doesNotMatch(String(items[0].occurred_at), /^\d{4}-\d\d-\d\d \d\d:/, 'Pulse times are ISO too');
+ });
+
+ await t.close();
+ done();
+})().catch((err) => { console.error(err); process.exit(1); });