From 1e03855b8b4123ce25bd1c46b0ccbf4207acd1fd Mon Sep 17 00:00:00 2001 From: Mike Marcacci Date: Sat, 24 Jul 2021 22:59:42 -0700 Subject: [PATCH] test and support older versions of node --- .github/workflows/ci.yml | 2 +- package.json | 4 +++- src/index.ts | 9 ++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 543e9ed..c704778 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: container: "node:${{matrix.node}}" strategy: matrix: - node: ["16"] + node: ["12", "14", "16"] services: redis-single-instance: image: redis diff --git a/package.json b/package.json index 23f28f6..7f34dc0 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,9 @@ "prepare": "yarn build", "prepublishOnly": "yarn install && yarn lint && yarn build && yarn test" }, - "dependencies": {}, + "dependencies": { + "node-abort-controller": "^2.0.0" + }, "type": "module", "exports": "./dist/index.js" } diff --git a/src/index.ts b/src/index.ts index d77a99c..a038fc9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,10 @@ import { randomBytes, createHash } from "crypto"; import { EventEmitter } from "events"; +// AbortController became available as a global in node version 16. Once version +// 14 reaches its end-of-life, this can be removed. +import PolyfillAbortController from "node-abort-controller"; + import { Redis as IORedisClient } from "ioredis"; type Client = IORedisClient; @@ -679,7 +683,10 @@ export default class Redlock extends EventEmitter { // The AbortController/AbortSignal pattern allows the routine to be notified // of a failure to extend the lock, and subsequent expiration. In the event // of an abort, the error object will be made available at `signal.error`. - const controller = new AbortController(); + const controller = AbortController + ? new AbortController() + : new PolyfillAbortController(); + const signal = controller.signal as RedlockAbortSignal; function queue(): void {