@fedify/postgres 2.4.0-dev.2155 → 2.4.0-dev.2159

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.
package/dist/kv.cjs CHANGED
@@ -95,7 +95,7 @@ var PostgresKvStore = class {
95
95
  */
96
96
  async cas(key, expectedValue, newValue, options) {
97
97
  await this.initialize();
98
- const ttl = options?.ttl == null ? null : options.ttl.toString();
98
+ const ttl = options?.ttl == null ? null : Temporal.Duration.from(options.ttl).toString();
99
99
  if (expectedValue === void 0 && newValue === void 0) return await this.get(key) === void 0;
100
100
  let result;
101
101
  if (expectedValue === void 0) result = await this.#sql`
@@ -221,6 +221,7 @@ var PostgresKvStore = class {
221
221
  await this.#sql`DROP TABLE IF EXISTS ${this.#sql(this.#tableName)};`;
222
222
  }
223
223
  #json(value) {
224
+ if (value === null) return this.#sql`'null'::jsonb`;
224
225
  if (this.#driverSerializesJson) return this.#sql.json(value);
225
226
  return this.#sql.json(JSON.stringify(value));
226
227
  }
package/dist/kv.js CHANGED
@@ -92,7 +92,7 @@ var PostgresKvStore = class {
92
92
  */
93
93
  async cas(key, expectedValue, newValue, options) {
94
94
  await this.initialize();
95
- const ttl = options?.ttl == null ? null : options.ttl.toString();
95
+ const ttl = options?.ttl == null ? null : Temporal.Duration.from(options.ttl).toString();
96
96
  if (expectedValue === void 0 && newValue === void 0) return await this.get(key) === void 0;
97
97
  let result;
98
98
  if (expectedValue === void 0) result = await this.#sql`
@@ -218,6 +218,7 @@ var PostgresKvStore = class {
218
218
  await this.#sql`DROP TABLE IF EXISTS ${this.#sql(this.#tableName)};`;
219
219
  }
220
220
  #json(value) {
221
+ if (value === null) return this.#sql`'null'::jsonb`;
221
222
  if (this.#driverSerializesJson) return this.#sql.json(value);
222
223
  return this.#sql.json(JSON.stringify(value));
223
224
  }
package/dist/mq.cjs CHANGED
@@ -161,6 +161,7 @@ var PostgresMessageQueue = class {
161
161
  async listen(handler, options = {}) {
162
162
  await this.initialize();
163
163
  const { signal } = options;
164
+ if (signal?.aborted) return;
164
165
  const poll = async () => {
165
166
  while (!signal?.aborted) {
166
167
  let processed = false;
@@ -254,6 +255,7 @@ var PostgresMessageQueue = class {
254
255
  };
255
256
  const timeouts = /* @__PURE__ */ new Set();
256
257
  const listen = await this.#sql.listen(this.#channelName, async (delay) => {
258
+ if (signal?.aborted) return;
257
259
  try {
258
260
  const durationMs = Temporal.Duration.from(delay).total("millisecond");
259
261
  if (durationMs < 1) await safeSerializedPoll("notify-immediate");
@@ -277,31 +279,35 @@ var PostgresMessageQueue = class {
277
279
  timeouts.clear();
278
280
  };
279
281
  signal?.addEventListener("abort", clearTimeouts, { once: true });
280
- let unlistenError;
281
282
  try {
282
283
  while (!signal?.aborted) {
283
- let timeout;
284
284
  await new Promise((resolve) => {
285
- signal?.addEventListener("abort", resolve, { once: true });
286
- timeout = setTimeout(() => {
287
- signal?.removeEventListener("abort", resolve);
288
- resolve(0);
285
+ const onAbort = () => {
286
+ clearTimeout(timeout);
287
+ signal?.removeEventListener("abort", onAbort);
288
+ resolve();
289
+ };
290
+ const timeout = setTimeout(() => {
291
+ signal?.removeEventListener("abort", onAbort);
292
+ resolve();
289
293
  }, this.#pollIntervalMs);
290
- timeouts.add(timeout);
294
+ signal?.addEventListener("abort", onAbort, { once: true });
291
295
  });
292
- if (timeout != null) timeouts.delete(timeout);
293
296
  await safeSerializedPoll("interval");
294
297
  }
295
298
  } finally {
296
299
  signal?.removeEventListener("abort", clearTimeouts);
297
300
  clearTimeouts();
301
+ await pollLock;
298
302
  try {
299
303
  await listen.unlisten();
300
304
  } catch (error) {
301
- if (!isConnectionDestroyedError(error)) unlistenError = error;
305
+ if (!isConnectionDestroyedError(error)) logger.error("Error while unlistening from channel {channelName}: {error}", {
306
+ channelName: this.#channelName,
307
+ error
308
+ });
302
309
  }
303
310
  }
304
- if (unlistenError != null) throw unlistenError;
305
311
  }
306
312
  /**
307
313
  * Initializes the message queue table if it does not already exist.
package/dist/mq.js CHANGED
@@ -156,6 +156,7 @@ var PostgresMessageQueue = class {
156
156
  async listen(handler, options = {}) {
157
157
  await this.initialize();
158
158
  const { signal } = options;
159
+ if (signal?.aborted) return;
159
160
  const poll = async () => {
160
161
  while (!signal?.aborted) {
161
162
  let processed = false;
@@ -249,6 +250,7 @@ var PostgresMessageQueue = class {
249
250
  };
250
251
  const timeouts = /* @__PURE__ */ new Set();
251
252
  const listen = await this.#sql.listen(this.#channelName, async (delay) => {
253
+ if (signal?.aborted) return;
252
254
  try {
253
255
  const durationMs = Temporal.Duration.from(delay).total("millisecond");
254
256
  if (durationMs < 1) await safeSerializedPoll("notify-immediate");
@@ -272,31 +274,35 @@ var PostgresMessageQueue = class {
272
274
  timeouts.clear();
273
275
  };
274
276
  signal?.addEventListener("abort", clearTimeouts, { once: true });
275
- let unlistenError;
276
277
  try {
277
278
  while (!signal?.aborted) {
278
- let timeout;
279
279
  await new Promise((resolve) => {
280
- signal?.addEventListener("abort", resolve, { once: true });
281
- timeout = setTimeout(() => {
282
- signal?.removeEventListener("abort", resolve);
283
- resolve(0);
280
+ const onAbort = () => {
281
+ clearTimeout(timeout);
282
+ signal?.removeEventListener("abort", onAbort);
283
+ resolve();
284
+ };
285
+ const timeout = setTimeout(() => {
286
+ signal?.removeEventListener("abort", onAbort);
287
+ resolve();
284
288
  }, this.#pollIntervalMs);
285
- timeouts.add(timeout);
289
+ signal?.addEventListener("abort", onAbort, { once: true });
286
290
  });
287
- if (timeout != null) timeouts.delete(timeout);
288
291
  await safeSerializedPoll("interval");
289
292
  }
290
293
  } finally {
291
294
  signal?.removeEventListener("abort", clearTimeouts);
292
295
  clearTimeouts();
296
+ await pollLock;
293
297
  try {
294
298
  await listen.unlisten();
295
299
  } catch (error) {
296
- if (!isConnectionDestroyedError(error)) unlistenError = error;
300
+ if (!isConnectionDestroyedError(error)) logger.error("Error while unlistening from channel {channelName}: {error}", {
301
+ channelName: this.#channelName,
302
+ error
303
+ });
297
304
  }
298
305
  }
299
- if (unlistenError != null) throw unlistenError;
300
306
  }
301
307
  /**
302
308
  * Initializes the message queue table if it does not already exist.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/postgres",
3
- "version": "2.4.0-dev.2155+2ccc5085",
3
+ "version": "2.4.0-dev.2159+bb072eda",
4
4
  "description": "PostgreSQL drivers for Fedify",
5
5
  "keywords": [
6
6
  "fedify",
@@ -71,7 +71,7 @@
71
71
  },
72
72
  "peerDependencies": {
73
73
  "postgres": "^3.4.7",
74
- "@fedify/fedify": "^2.4.0-dev.2155+2ccc5085"
74
+ "@fedify/fedify": "^2.4.0-dev.2159+bb072eda"
75
75
  },
76
76
  "devDependencies": {
77
77
  "@js-temporal/polyfill": "^0.5.1",
@@ -79,7 +79,7 @@
79
79
  "tsdown": "^0.22.0",
80
80
  "typescript": "^6.0.0",
81
81
  "@fedify/fixture": "^2.0.0",
82
- "@fedify/testing": "^2.4.0-dev.2155+2ccc5085"
82
+ "@fedify/testing": "^2.4.0-dev.2159+bb072eda"
83
83
  },
84
84
  "scripts": {
85
85
  "build:self": "tsdown",