Skip to content
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
10 changes: 7 additions & 3 deletions packages/pg-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ export class CloudflareSocket extends EventEmitter {
this.emit('data', Buffer.from(value))
}

write(data: Uint8Array | string, callback?: (error?: unknown) => void): true | void
write(data: Uint8Array | string, encoding?: BufferEncoding, callback?: (error?: unknown) => void): true | void
write(
data: Uint8Array | string,
encoding: BufferEncoding = 'utf8',
callback: (...args: unknown[]) => void = () => {}
) {
encodingOrCallback: BufferEncoding | ((error?: unknown) => void) = 'utf8',
callback: (error?: unknown) => void = () => {}
): true | void {
const encoding = typeof encodingOrCallback === 'function' ? 'utf8' : encodingOrCallback
if (typeof encodingOrCallback === 'function') callback = encodingOrCallback
if (data.length === 0) return callback()
if (typeof data === 'string') data = Buffer.from(data, encoding)

Expand Down
19 changes: 19 additions & 0 deletions packages/pg-esm-test/pg-cloudflare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,23 @@ describe('pg-cloudflare', () => {

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

it('should call the write(data, callback) callback exactly once', async () => {
const socket = new CloudflareSocket()
socket._cfWriter = { write: () => Promise.resolve() }

let resolve
const promise = new Promise((resolvePromise) => {
resolve = resolvePromise
})
let called = false
socket.write(Buffer.from('x'), (error) => {
assert.ifError(error)
assert(!called)
called = true
resolve()
})

await promise
})
})
Loading