Skip to content
Open
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 packages/pg-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class CloudflareSocket extends EventEmitter {
end(data = Buffer.alloc(0), encoding: BufferEncoding = 'utf8', callback: (...args: unknown[]) => void = () => {}) {
log('ending CF socket')
this.write(data, encoding, (err) => {
this._cfSocket!.close()
this._cfSocket?.close()
if (callback) callback(err)
})
return this
Expand Down
12 changes: 12 additions & 0 deletions packages/pg-esm-test/pg-cloudflare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@ describe('pg-cloudflare', () => {
it('should export CloudflareSocket constructor', () => {
assert.ok(new CloudflareSocket())
})

it('should safely end after the underlying socket has closed', async () => {
const socket = new CloudflareSocket()
const underlyingSocket = { closed: Promise.resolve() }
socket._cfSocket = underlyingSocket
socket._addClosedHandler()

await underlyingSocket.closed
assert.equal(socket._cfSocket, null)

assert.doesNotThrow(() => socket.end())
})
})
Loading