109 Commits

Author SHA1 Message Date
Brian C
940479bc4b
Add esm exports (#3423)
* build: add esm exports

* fix: add defaults as per arethetypeswrong report

* fix: add missing types

* lint

* Fix broken tests

* Add (failing) test for esm compat

* Begin moving files to proper extension and adding tests

* Add tests for connection-string and fix cloudflare module type and esm compat

* Add query-stream and cursor as esm exports

* Update PR copilot review

* Publish

 - pg-cloudflare@1.1.2-alpha.0
 - pg-connection-string@2.7.1-alpha.0
 - pg-cursor@2.13.2-alpha.0
 - pg-esm-test@1.0.1-alpha.0
 - pg-native@3.3.1-alpha.0
 - pg-pool@3.8.1-alpha.0
 - pg-protocol@1.8.1-alpha.0
 - pg-query-stream@4.8.2-alpha.0
 - pg@8.14.2-alpha.0

* More cf compat work

* Publish

 - pg-cloudflare@1.1.2-alpha.1
 - pg-cursor@2.13.2-alpha.1
 - pg-esm-test@1.0.1-alpha.1
 - pg-pool@3.8.1-alpha.1
 - pg-query-stream@4.8.2-alpha.1
 - pg@8.14.2-alpha.1

* Add more cf compat and update tests

* Make tests pass - update exports for esm

* Use env vars for test connection in cf tests

* Fix lint

* Fit vitest into existing legacy framework

* Skip worker tests on node below 18

* Revert doc changes for now

* Remove legacy worker test in favor of vitest

---------

Co-authored-by: Luca Ban <mesqueeb@users.noreply.github.com>
2025-04-22 10:53:22 -05:00
Alex Anderson
9b510373a6
eslint: enable recommended ruleset (#3263) 2025-04-12 08:17:33 +00:00
Brian C
c53a472a60
Batch network packets in prepared statements (#3402)
* Batch network packets in prepared statements

Fixes #3340
Fixes #3325
Fixes #3098

* Fix type-o but mostly retrigger build for CF pages preview
2025-03-17 10:28:46 -05:00
George MacKerron
b4022aa5c0
Add support for SCRAM-SHA-256-PLUS i.e. channel binding (#3356)
* Added support for SCRAM-SHA-256-PLUS i.e. channel binding

* Requested tweaks to channel binding

* Additional tweaks to channel binding

* Fixed lint complaints

* Update packages/pg/lib/crypto/sasl.js

Co-authored-by: Charmander <~@charmander.me>

* Update packages/pg/lib/crypto/sasl.js

Co-authored-by: Charmander <~@charmander.me>

* Update packages/pg/lib/client.js

Co-authored-by: Charmander <~@charmander.me>

* Tweaks to channel binding

* Now using homegrown certificate signature algorithm identification

* Update ssl.mdx with channel binding changes

* Allow for config object being undefined when assigning enableChannelBinding

* Fixed a test failing on an updated error message

* Removed - from hash names like SHA-256 for legacy crypto (Node 14 and below)

* Removed packageManager key from package.json

* Added some SASL/channel binding unit tests

* Added a unit test for continueSession to check expected SASL session data

* Modify tests: don't require channel binding (which cannot then work) if not using SSL

---------

Co-authored-by: Charmander <~@charmander.me>
2025-03-10 13:13:32 -05:00
francesco
1876f2000a
Add "unref" to timers (#3396)
* chore: add unref to timer

see https://nodejs.org/api/timers.html#timeoutunref

* chore: add unref to timer

see https://nodejs.org/api/timers.html#timeoutunref

* fix: lint

* fix: lint

* fix: lint

* fix: lint
2025-03-10 12:16:57 -05:00
francesco
f6e829c564
fix: revert PR #3379 (avoid useless spread) (#3382)
* Update result.js

* Update build-result.js

* fix: lint

* fix: lint
2025-02-12 18:28:05 -06:00
francesco
5f6a6e6596
perf(pg): avoid useless spread (#3379)
* perf(pg): avoid useless spread

* fix: missing initialization
2025-02-11 10:13:15 -06:00
francesco
751e7410d9
perf(utils): fast prepareValue (#3370)
* perf(utils): fast prepareValue

This PR add a performance improvements at prepare Value for non-object by skipping useless condition

* fix: lint

* fix: case of undefined

* fix: review
2025-02-10 16:26:33 -06:00
francesco
f10f569a8a
perf(utils): replace pad with String.padStart (#3371) 2025-02-07 23:59:58 +00:00
Nigro Simone
3c48f22b22
perf: pre allocate array instead of push item (#3250)
* fix: typo

* perf: pre allocate array instead of push item

* perf: refractoring missing push

* perf: avoid useless varible declaration

* perf: short control flow

* fix: lint

* more precise bench

* fix: lint
2025-01-17 15:27:35 -06:00
Antariksh Mahajan
1af6321219
fix: use existing Result types for new Result (#3310)
* fix: use existing Result types for new Result

* test: add test for multiple results with custom type parser

* chore: empty commit to trigger tests
2024-10-23 17:05:33 -05:00
Brian C
f73b22f96e
Handle bad message ordering - make it catchable. Fixes 3174 (#3289)
* Handle bad message ordering - make it catchable. Fixes 3174

* Close client in test

* Mess w/ github action settings

* update ci config

* Remove redundant tests

* Update code to use handle error event

* Add tests for commandComplete message being out of order

* Lint fix

* Fix native tests

* Fix lint again...airport computer not my friend

* Not a native issue
2024-09-17 09:50:17 -05:00
Pete Bacon Darwin
f7e484ed61
refactor: tighten up cloudflare detection (#3170)
* refactor: tighten up cloudflare detection

The previous approach to detecting whether to use Cloudflare's sockets was to check for missing polyfills.
But as we improve the polyfills that Wrangler can provide these checks are no longer valid.

Now we just try to use the Cloudflare API first and fallback to Node.js if those are not available.

* fixup! refactor: tighten up cloudflare detection
2024-06-19 13:28:01 -05:00
srieding
9baa56eaa2
feat: allow specifying a timeout on a per query base (#3074) 2024-06-19 09:36:35 -05:00
Alex Anderson
d650741eea
lib/client: remove no-op statement (#3240)
This looks like it was a refactoring of

  var connectionTimeoutHandle;

Co-authored-by: alxndrsn <alxndrsn>
2024-06-05 11:00:04 -05:00
Alex Anderson
1625861981
Remove unused imports (#3241)
* Remove unused imports

* reinstate test helper

---------

Co-authored-by: alxndrsn <alxndrsn>
2024-06-05 10:50:52 -05:00
Alex Anderson
ff47a97f28
Add option to force use of Extended Queries (#3214)
This feature can be used as follows:

```
client.query({ text: 'SELECT 1', queryMode: 'extended' })
```

This will force the query to be sent with parse/bind/execute even when it has no parameters and disallows multiple statements being executed.  This can be useful in scenarios where you want to enforce more security & help prevent sql injection attacks...particularly by library authors.

---------

Co-authored-by: alxndrsn <alxndrsn>
Co-authored-by: Brian Carlson <brian.m.carlson@gmail.com>
2024-06-04 11:14:04 -05:00
Alex Anderson
2ab6f367c0
query: remove unused prop: isPreparedStatement (#3177)
Use of this property was removed in 2020 (dd3ce616d0fbdb92a7e146ecf4171bf3c1b3ea97).

Co-authored-by: alxndrsn <alxndrsn>
2024-03-28 10:28:30 -05:00
Alex Anderson
b400d33c33
query: remove unused prop: _promise (#3178)
Use of this prop was probably removed in 2017 (a0eb36d81938e488b3bc5369faee74fe22f36949).

Co-authored-by: alxndrsn <alxndrsn>
2024-03-28 10:26:00 -05:00
Shane da Silva
81c287a49b
Serialize arrays of Uint8Array objects as hex escape sequences (#2930)
Previously, if you attempted to pass an array of `Uint8Array` objects to
a prepared statement, it would render each literal numeric value of that
array.

Since `Uint8Array` (and `TypedArray` types) represent views over raw
bytes, ensure these are serialized to Postgres as a byte representation.
2024-02-09 21:25:41 -08:00
Alex Anderson
16322c2d50
Fix eslint violations (#3078)
Co-authored-by: alxndrsn <alxndrsn>
2023-10-19 10:27:25 -05:00
Brian C
da0f5c5eb2
Remove 1 loop on rowDescription event (#3056)
* Remove 1 loop on rowDescription event

* Update packages/pg/lib/result.js

Co-authored-by: Charmander <~@charmander.me>

---------

Co-authored-by: Charmander <~@charmander.me>
2023-09-15 16:21:45 -05:00
Sehrope Sarkuni
106ca8a178
Fix get value of last column with same name in result rows (#3063)
* Add failing test for result rows with the same column names

* Fix handling of duplicate column names in results to ensure last value is populated

Fixes handling of result rows that have the same column name duplicated in the results to ensure
that the last value is the one returned to the user. This was the old behavior but unintentionally
broken when the pre-built object optimization was added.
2023-09-14 14:43:14 -05:00
Koen
b5c5e52aa0
Option to use pre-shaped result rows; fixes #3042 (#3043)
* Add property usePrebuiltEmptyResultObjects to Query constructor which generates pre-shaped result rows

* Remove option and test for prebuiltEmptyResultObject

* Remove errorneously added newline

* Move all logic for prebuilding objects to Result

* Move prebuilding to addFields

* Use a clone as clone-base

---------

Co-authored-by: HZ111 / Dev2 <hz111@wielick.nl>
2023-08-15 08:42:54 -07:00
Ben Reinhart
3644730d2b
Remove early return for non commonjs environments (#3033) 2023-07-18 19:01:07 -05:00
phiresky
d59cd15ed2
fix stack traces of query() to include the async context (#1762) (#2983)
* fix stack traces of query() to include the async context (#1762)

* rename tests so they are actually run

* conditionally only run async stack trace tests on node 16+

* add stack trace to pg-native

---------

Co-authored-by: Charmander <~@charmander.me>
2023-05-31 11:28:53 -05:00
Novikov Evgeniy
dee3ae5cd6
feat: add connection parameter nativeConnectionString (#2941)
Co-authored-by: Evgeniy Novikov <e.p.novikov@tinkoff.ru>
2023-05-31 11:16:36 -05:00
Charmander
3039f1da77 Revert "Update utils.js (#2981)"
This reverts commit 522e2dcb76f92d0096177b10204bdc385375020d.
2023-05-23 08:42:54 +00:00
sudarshanvn
522e2dcb76
Update utils.js (#2981)
Fixed following error

    ReferenceError: TextEncoder is not defined
2023-05-19 16:53:52 -05:00
Pete Bacon Darwin
07553428e9 Add Cloudflare Worker compatible socket 2023-05-15 07:29:07 +01:00
Pete Bacon Darwin
5532ca51db Use WebCrypto APIs where possible
The only place we are stuck with node's original crypto API
is for generating md5 hashes, which are not supported by WebCrypto.
2023-05-15 07:29:07 +01:00
Pete Bacon Darwin
2b469d01da avoid accessing Node specific requires when not needed 2023-05-15 07:29:07 +01:00
Pete Bacon Darwin
f305419676 Use URL rather than url.parse() in pg-connection-string
Swapping the deprecated Node.js API for the modern cross
environment API.
2023-05-15 07:29:07 +01:00
Conner
249182ea9f
Document client.escapeIdentifier and client.escapeLiteral (#2954)
* Document client.escapeIdentifier and client.escapeLiteral

Per #1978 it seems that these client APIs are undocumented. Added documentation for these functions along with some examples and relevant links.

* Fix typos in new docs

* Migrate escapeIdentifier and escapeLiteral from Client to PG

These are standalone utility functions, they do not need a client instance to function.

Changes made:
- Refactored escapeIdentifer and escapeLiteral from client class to functions in utils
- Update PG to export  escapeIdentifier and escapeLiteral
- Migrated tests for Client.escapeIdentifier and Client.escapeLiteral to tests for utils
- Updated documentation, added a "utilities" page where these helpers are discussed

**note** this is a breaking change. Users who used these functions (previously undocumented) on instances of Client, or via Client.prototype.

* Export escapeIdentifier and escapeLiteral from PG

These are standalone utility functions, they should not depend on a client instance.

Changes made:
- Refactored escapeIdentifer and escapeLiteral from client class to functions in utils
- Re-exported functions on client for backwards compatibility
- Update PG to export  escapeIdentifier and escapeLiteral
- Updated tests to validate the newly exported functions from both entry points
- Updated documentation, added a "utilities" page where these helpers are discussed

* Ensure escape functions work via Client.prototype

Updated changes such that escapeIdentifier and escapeLiteral are usable via the client prototype
Updated tests to check for both entry points in client
2023-05-02 07:55:59 -05:00
Cody Greene
5703791640
fix: double client.end() hang (#2717)
* fix: double client.end() hang

fixes https://github.com/brianc/node-postgres/issues/2716

`client.end()` will resolve early if the connection is already dead,
rather than waiting for an "end" event that will never arrive.

* fix: client.end() resolves when socket is fully closed
2023-03-06 12:10:07 -06:00
Sehrope Sarkuni
bb8745b215
Fix SASL to bubble up errors, enable SASL tests in CI, and add informative empty SASL password message (#2901)
* Enable SASL tests in GitHub actions CI

* Add SASL test to ensure that client password is a string

* Fix SASL error handling to emit and bubble up errors

* Add informative error when SASL password is empty string
2023-01-23 10:03:51 -08:00
Ruy Adorno
f82f39c20c
Add support to stream factory (#2898)
This changeset enables declaring the `stream` config value as a factory
method. Providing a much more flexible control of the socket connection.

Defining a custom `stream` config value allows the postgres driver to
support a larger variety of environments/setups such as proxy servers
and secure socket connections that are used by cloud providers such as
GCP.

Currently, usage of the `stream` config value is only viable for single
connections given that it's only possible to define a single socket
stream instance per new Client/Pool instance. By adding support to a
factory function, it becomes possible to enable usage of custom socket
streams for connection pools.

For reference, see the `mysql2` driver for MySQL (linked below) for
prior art example of this pattern.

Refs: ba15fe2570/lib/connection.js (L63-L65)
Refs: https://cloud.google.com/sql/docs/postgres/connect-overview
Signed-off-by: Ruy Adorno <ruyadorno@google.com>

Signed-off-by: Ruy Adorno <ruyadorno@google.com>
2023-01-23 10:02:39 -08:00
Frazer Smith
15b502d4c1
refactor(pg): remove unused imports (#2854) 2022-11-05 18:26:42 -07:00
Knut Olav Løite
406f141a1a
perf: remove superfluous flush message (#2842) 2022-10-15 12:57:16 -05:00
David Matějka
9dfb3dccbf
perf(pg): use native crypto.pbkdf2Sync in sasl auth (#2815) 2022-09-27 05:38:28 -05:00
Martin Kubliniak
3e53d06cd8
Support lock_timeout (#2779) 2022-08-10 16:15:06 -05:00
Peter Rust
68160a29bd
Fix #2556 by keeping callback errors from interfering with cleanup (#2753)
* Fix #2556 (handleRowDescription of null) by keeping callback errors from interfering with cleanup

* Added regression test for #2556
2022-06-20 08:25:12 -05:00
Brian C
779803fbce
Add ref/unref noop to native client (#2581)
* Add ref/unref noop to native client

* Use promise.catch in test

* Make partition test not flake on old node

* Fix test flake on old node
2021-07-27 12:23:30 -05:00
Brian Crowell
684cd09bce
Allow Node to exit if the pool is idle (#2568)
Based on the suggestion from #2078. This adds ref/unref methods to the
Connection and Client classes and then uses them to allow the process to
exit if all of the connections in the pool are idle. This behavior is
controlled by the allowExitOnIdle flag to the Pool constructor; it defaults
to the old behavior.
2021-07-27 11:29:07 -05:00
Juan M Martínez
3dc79b605c
util in connection not used (#2507) 2021-04-02 22:37:39 +00:00
Kannan Goundan
4b229275cf
pg: Re-export DatabaseError from 'pg-protocol' (#2445)
* pg: Re-export DatabaseError from 'pg-protocol'

Before, users would have to import DatabaseError from 'pg-protocol'.  If
there are multiple versions of 'pg-protocol', you might end up using the
wrong one.

Closes #2378

* Update error-handling-tests.js

* Update query-error-handling-tests.js

Co-authored-by: Brian C <brian.m.carlson@gmail.com>
2021-03-22 13:07:05 -05:00
Sehrope Sarkuni
a109e8c6d2
Add more SASL validation and fix tests (#2436)
* Add sha256 SASL helper

* Rename internal createHMAC(...) to hmacSha256(...)

* Add parseAttributePairs(...) helper for SASL

* Tighten arg checks in SASL xorBuffers(...)

* Add SASL nonce check for printable chars

* Add SASL server salt and server signature base64 validation

* Add check for non-empty SASL server nonce

* Rename SASL helper to parseServerFirstMessage(...)

* Add parameter validation to SASL continueSession(...)

* Split out SASL final message parsing into parseServerFinalMessage(...)

* Fix SCRAM tests

Removes custom assert.throws(...) so that the real one from the assert package is used and
fixes the SCRAM tests to reflect the updated error messages and actual checking of errors.

Previously the custom assert.throws(...) was ignoring the error signature validation.
2020-12-30 04:19:27 -06:00
Brian C
ebe412cf24
Support "true" as string for ssl (#2407)
Fixes 2406
2020-11-11 10:41:20 -06:00
Charmander
0012a43d95
Forward options’ ssl.key even when non-enumerable (#2394)
* Test client certificate authentication

* Forward options’ ssl.key even when non-enumerable
2020-11-09 11:30:40 -06:00
Brian C
07988f985a
Speed up bind functionality (#2286)
Move from 3 loops (prepareValue, check for buffers, write param types, write param values) to a single loop. This speeds up the insert benchmark by around 100 queries per second. Performance improvement depends on number of parameters being bound.
2020-11-04 08:27:40 -06:00