Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch events to AbortSignal properly #78

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [14.x, 16.x, 18.x, 19.x, 20.x]
node-version: [16.x, 18.x, 19.x, 20.x]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ npm install @elastic/transport

### Node.js support

NOTE: The minimum supported version of Node.js is `v14`.
NOTE: The minimum supported version of Node.js is `v16`.

The client versioning follows the Elastc Stack versioning, this means that
major, minor, and patch releases are done following a precise schedule that
Expand All @@ -23,9 +23,7 @@ often does not coincide with the [Node.js release](https://nodejs.org/en/about/r
To avoid support insecure and unsupported versions of Node.js, the
client **will drop the support of EOL versions of Node.js between minor releases**.
Typically, as soon as a Node.js version goes into EOL, the client will continue
to support that version for at least another minor release. If you are using the client
with a version of Node.js that will be unsupported soon, you will see a warning
in your logs (the client will start logging the warning with two minors in advance).
to support that version for at least another minor release.

Unless you are **always** using a supported version of Node.js,
we recommend defining the client dependency in your
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/elastic/elastic-transport-js#readme",
"engines": {
"node": ">=14"
"node": ">=16"
},
"devDependencies": {
"@sinonjs/fake-timers": "github:sinonjs/fake-timers#0bfffc1",
Expand Down
3 changes: 2 additions & 1 deletion src/connection/UndiciConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ export default class Connection extends BaseConnection {
timeoutId = setTimeout(() => {
timedout = true
if (options.signal != null) {
options.signal.dispatchEvent('abort')
// @ts-expect-error Event is a Node.js global
options.signal.dispatchEvent(new Event('abort'))
} else {
this[kEmitter].emit('abort')
}
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/events-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import { test } from 'tap'
import * as http from 'http'
import { AbortController } from 'node-abort-controller'
import {
HttpConnection,
UndiciConnection,
Expand Down
7 changes: 5 additions & 2 deletions test/unit/http-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { gzipSync, deflateSync } from 'zlib'
import { Readable } from 'stream'
import hpagent from 'hpagent'
import intoStream from 'into-stream'
import { AbortController } from 'node-abort-controller'
import { AbortController as LegacyAbortController } from 'node-abort-controller'
import { buildServer } from '../utils'
import { HttpConnection, errors, ConnectionOptions } from '../../'
import net from "net";
Expand Down Expand Up @@ -1245,7 +1245,10 @@ test('as stream', async t => {
test('Cleanup abort listener', async t => {
t.plan(2)

const controller = new AbortController()
// uses legacy node-abort-controller polyfill package because the global
// AbortController's signal does not let expose an `eventEmitter` property for
// us to inspect, but the legacy package does!
const controller = new LegacyAbortController()

function handler (req: http.IncomingMessage, res: http.ServerResponse) {
// @ts-expect-error
Expand Down
1 change: 0 additions & 1 deletion test/unit/transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import os from 'os'
import { Readable } from 'stream'
import intoStream from 'into-stream'
import * as http from 'http'
import { AbortController } from 'node-abort-controller'
import {
Transport,
Serializer,
Expand Down
5 changes: 3 additions & 2 deletions test/unit/undici-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import buffer from 'buffer'
import { gzipSync, deflateSync } from 'zlib'
import { Readable } from 'stream'
import intoStream from 'into-stream'
import { AbortController } from 'node-abort-controller'
import { buildServer } from '../utils'
import { UndiciConnection, errors, ConnectionOptions } from '../../'

Expand Down Expand Up @@ -221,6 +220,7 @@ test('Timeout support / 4', async t => {
timeout: 50,
...options
})
t.fail('Timeout was not reached')
} catch (err: any) {
t.ok(err instanceof TimeoutError)
t.equal(err.message, 'Request timed out')
Expand Down Expand Up @@ -448,7 +448,7 @@ test('Should disallow two-byte characters in URL path', async t => {
})

test('Abort a request syncronously', async t => {
t.plan(1)
t.plan(2)

function handler (req: http.IncomingMessage, res: http.ServerResponse) {
t.fail('The server should not be contacted')
Expand All @@ -469,6 +469,7 @@ test('Abort a request syncronously', async t => {
...options
}).catch(err => {
t.ok(err instanceof RequestAbortedError)
t.ok(controller.signal.aborted, 'Signal should be aborted')
server.stop()
})

Expand Down
Loading