[
  {
    "path": ".eslintrc.json",
    "content": "{\n  \"root\": true,\n  \"env\": {\n    \"es2020\": true,\n    \"node\": true\n  },\n  \"parserOptions\": {\n    \"ecmaVersion\": 2020,\n    \"sourceType\": \"module\"\n  },\n  \"rules\": {\n    \"comma-dangle\": 2,\n    \"no-cond-assign\": 2,\n    \"no-console\": 1,\n    \"no-constant-condition\": 2,\n    \"no-control-regex\": 2,\n    \"no-debugger\": 2,\n    \"no-dupe-args\": 2,\n    \"no-dupe-keys\": 2,\n    \"no-duplicate-case\": 2,\n    \"no-empty\": 2,\n    \"no-empty-character-class\": 2,\n    \"no-ex-assign\": 2,\n    \"no-extra-boolean-cast\": 2,\n    \"no-extra-semi\": 2,\n    \"no-func-assign\": 2,\n    \"no-inner-declarations\": 2,\n    \"no-invalid-regexp\": 2,\n    \"no-irregular-whitespace\": 2,\n    \"no-negated-in-lhs\": 0,\n    \"no-obj-calls\": 2,\n    \"no-regex-spaces\": 2,\n    \"no-sparse-arrays\": 2,\n    \"no-unexpected-multiline\": 2,\n    \"no-unreachable\": 2,\n    \"use-isnan\": 2,\n    \"valid-typeof\": 2,\n    \"accessor-pairs\": 2,\n    \"array-callback-return\": 0,\n    \"consistent-return\": 0,\n    \"curly\": [\n      2,\n      \"multi-or-nest\",\n      \"consistent\"\n    ],\n    \"default-case\": 2,\n    \"dot-location\": [\n      2,\n      \"property\"\n    ],\n    \"dot-notation\": [\n      2,\n      {\n        \"allowPattern\": \"^[a-z]+(_[a-z]+)+$\"\n      }\n    ],\n    \"eqeqeq\": [\n      \"error\",\n      \"always\",\n      {\n        \"null\": \"ignore\"\n      }\n    ],\n    \"no-alert\": 2,\n    \"no-caller\": 2,\n    \"no-case-declarations\": 2,\n    \"no-div-regex\": 2,\n    \"no-else-return\": 2,\n    \"no-empty-function\": 2,\n    \"no-empty-pattern\": 2,\n    \"no-eq-null\": 0,\n    \"no-eval\": 2,\n    \"no-extend-native\": 2,\n    \"no-extra-bind\": 2,\n    \"no-extra-label\": 2,\n    \"no-fallthrough\": 2,\n    \"no-floating-decimal\": 2,\n    \"no-implicit-coercion\": 0,\n    \"no-implicit-globals\": 2,\n    \"no-implied-eval\": 2,\n    \"no-invalid-this\": 2,\n    \"no-iterator\": 2,\n    \"no-labels\": 2,\n    \"no-lone-blocks\": 2,\n    \"no-loop-func\": 2,\n    \"no-magic-numbers\": 0,\n    \"no-multi-spaces\": [\n      2,\n      {\n        \"ignoreEOLComments\": true,\n        \"exceptions\": {\n          \"Array\": true,\n          \"Property\": true,\n          \"VariableDeclarator\": true,\n          \"ImportDeclaration\": true,\n          \"TernaryExpressions\": true,\n          \"Comments\": true\n        }\n      }\n    ],\n    \"no-multi-str\": 2,\n    \"no-native-reassign\": 2,\n    \"no-new\": 2,\n    \"no-new-func\": 2,\n    \"no-new-wrappers\": 2,\n    \"no-octal\": 2,\n    \"no-octal-escape\": 2,\n    \"no-param-reassign\": 0,\n    \"no-proto\": 2,\n    \"no-redeclare\": 2,\n    \"no-return-assign\": 0,\n    \"no-script-url\": 2,\n    \"no-self-assign\": 2,\n    \"no-self-compare\": 2,\n    \"no-sequences\": 0,\n    \"no-throw-literal\": 2,\n    \"no-unmodified-loop-condition\": 2,\n    \"no-unused-expressions\": 0,\n    \"no-unused-labels\": 2,\n    \"no-useless-call\": 2,\n    \"no-useless-concat\": 2,\n    \"no-useless-escape\": 2,\n    \"no-void\": 2,\n    \"no-with\": 2,\n    \"wrap-iife\": 2,\n    \"no-delete-var\": 2,\n    \"no-label-var\": 2,\n    \"no-restricted-globals\": 2,\n    \"no-shadow\": 0,\n    \"no-shadow-restricted-names\": 2,\n    \"no-undef\": 2,\n    \"no-undef-init\": 2,\n    \"no-unused-vars\": 2,\n    \"no-use-before-define\": [\n      2,\n      {\n        \"functions\": false,\n        \"variables\": false\n      }\n    ],\n    \"callback-return\": 0,\n    \"global-require\": 2,\n    \"handle-callback-err\": 2,\n    \"no-mixed-requires\": 2,\n    \"no-new-require\": 2,\n    \"no-path-concat\": 2,\n    \"no-process-env\": 2,\n    \"no-process-exit\": 2,\n    \"array-bracket-spacing\": [\n      2,\n      \"never\"\n    ],\n    \"block-spacing\": [\n      2,\n      \"always\"\n    ],\n    \"brace-style\": [\n      2,\n      \"1tbs\",\n      {\n        \"allowSingleLine\": true\n      }\n    ],\n    \"camelcase\": 0,\n    \"comma-spacing\": 2,\n    \"comma-style\": [\n      2,\n      \"first\",\n      {\n        \"exceptions\": {\n          \"ArrayExpression\": true,\n          \"ObjectExpression\": true\n        }\n      }\n    ],\n    \"consistent-this\": [\n      2,\n      \"self\"\n    ],\n    \"eol-last\": 2,\n    \"indent\": [\n      2,\n      2,\n      {\n        \"MemberExpression\": \"off\",\n        \"flatTernaryExpressions\": true,\n        \"VariableDeclarator\": {\n          \"const\": 2\n        },\n        \"FunctionExpression\": {\n          \"parameters\": \"first\"\n        },\n        \"CallExpression\": {\n          \"arguments\": \"off\"\n        },\n        \"ArrayExpression\": \"first\",\n        \"ObjectExpression\": \"first\"\n      }\n    ],\n    \"key-spacing\": [\n      0,\n      {\n        \"beforeColon\": false,\n        \"afterColon\": true,\n        \"mode\": \"minimum\"\n      }\n    ],\n    \"keyword-spacing\": 2,\n    \"linebreak-style\": 2,\n    \"lines-around-comment\": 0,\n    \"max-depth\": [\n      2,\n      5\n    ],\n    \"max-len\": [\n      2,\n      150\n    ],\n    \"max-nested-callbacks\": [\n      2,\n      5\n    ],\n    \"max-params\": [\n      2,\n      5\n    ],\n    \"max-statements-per-line\": 0,\n    \"new-cap\": [\n      2,\n      {\n        \"capIsNew\": false\n      }\n    ],\n    \"new-parens\": 2,\n    \"newline-after-var\": 0,\n    \"newline-before-return\": 0,\n    \"no-array-constructor\": 2,\n    \"no-bitwise\": 0,\n    \"no-continue\": 2,\n    \"no-lonely-if\": 2,\n    \"no-mixed-spaces-and-tabs\": 2,\n    \"no-negated-condition\": 0,\n    \"no-new-object\": 2,\n    \"no-spaced-func\": 2,\n    \"no-trailing-spaces\": 1,\n    \"no-unneeded-ternary\": 2,\n    \"no-whitespace-before-property\": 2,\n    \"object-curly-spacing\": [\n      2,\n      \"always\"\n    ],\n    \"one-var-declaration-per-line\": [\n      2,\n      \"always\"\n    ],\n    \"quote-props\": [\n      2,\n      \"as-needed\"\n    ],\n    \"quotes\": [\n      2,\n      \"single\"\n    ],\n    \"semi\": [\n      2,\n      \"never\"\n    ],\n    \"space-before-blocks\": 2,\n    \"space-before-function-paren\": [\n      2,\n      \"never\"\n    ],\n    \"space-infix-ops\": 2,\n    \"space-unary-ops\": 2,\n    \"spaced-comment\": 2,\n    \"arrow-spacing\": 2,\n    \"constructor-super\": 2,\n    \"no-class-assign\": 2,\n    \"no-confusing-arrow\": 0,\n    \"no-const-assign\": 2,\n    \"no-dupe-class-members\": 2,\n    \"no-duplicate-imports\": 2,\n    \"no-new-symbol\": 2,\n    \"no-this-before-super\": 2,\n    \"no-useless-constructor\": 2,\n    \"no-var\": 2,\n    \"object-shorthand\": 0,\n    \"prefer-arrow-callback\": 0,\n    \"prefer-const\": 2,\n    \"prefer-rest-params\": 0,\n    \"prefer-spread\": 0\n  }\n}\n"
  },
  {
    "path": ".github/workflows/test.yml",
    "content": "name: test\n\non: [push, pull_request]\n\njobs:\n  test:\n    name: Node v${{ matrix.node }} on PostgreSQL v${{ matrix.postgres }}\n    strategy:\n      fail-fast: false\n      matrix:\n        node: ['12', '14', '16', '18', '20', '21', '22', '23', '24']\n        postgres: ['12', '13', '14', '15', '16', '17']\n    runs-on: ubuntu-latest\n    services:\n      postgres:\n        image: postgres:${{ matrix.postgres }}\n        env:\n          POSTGRES_USER: postgres\n          POSTGRES_HOST_AUTH_METHOD: trust\n        ports:\n          - 5433:5432\n        options: >-\n          --health-cmd pg_isready\n          --health-interval 10s\n          --health-timeout 5s\n          --health-retries 5\n    steps:\n      - uses: actions/checkout@v4\n      - run: |\n          date\n          sudo apt purge postgresql-16\n          sudo sh -c 'echo \"deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main\" > /etc/apt/sources.list.d/pgdg.list'\n          wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -\n          sudo apt-get update\n          sudo apt-get -y install \"postgresql-${{ matrix.postgres }}\"\n          sudo cp ./tests/pg_hba.conf /etc/postgresql/${{ matrix.postgres }}/main/pg_hba.conf\n          sudo sed -i 's/.*wal_level.*/wal_level = logical/' /etc/postgresql/${{ matrix.postgres }}/main/postgresql.conf\n          sudo sed -i 's/.*max_prepared_transactions.*/max_prepared_transactions = 100/' /etc/postgresql/${{ matrix.postgres }}/main/postgresql.conf\n          sudo sed -i 's/.*ssl = .*/ssl = on/' /etc/postgresql/${{ matrix.postgres }}/main/postgresql.conf\n          openssl req -new -x509 -nodes -days 365 -text -subj \"/CN=localhost\" -extensions v3_req -config <(cat /etc/ssl/openssl.cnf <(printf \"\\n[v3_req]\\nbasicConstraints=critical,CA:TRUE\\nkeyUsage=nonRepudiation,digitalSignature,keyEncipherment\\nsubjectAltName=DNS:localhost\")) -keyout server.key -out server.crt\n          sudo cp server.key /etc/postgresql/${{ matrix.postgres }}/main/server.key\n          sudo cp server.crt /etc/postgresql/${{ matrix.postgres }}/main/server.crt\n          sudo chmod og-rwx /etc/postgresql/${{ matrix.postgres }}/main/server.key\n          sudo systemctl start postgresql.service\n          sudo systemctl status postgresql.service\n          pg_isready\n          sudo -u postgres psql -c \"SHOW hba_file;\"\n      - uses: denoland/setup-deno@v1\n        with:\n          deno-version: v1.x\n      - uses: actions/setup-node@v4\n        with:\n          node-version: ${{ matrix.node }}\n      - run: npm test\n        env:\n          PGUSER: postgres\n          PGSOCKET: /var/run/postgresql\n"
  },
  {
    "path": "CHANGELOG.md",
    "content": "# Changelog\n\n## v3.2.4 - 25 May 2022\n- Allow setting keep_alive: false  bee62f3\n- Fix support for null in arrays - fixes #371  b04c853\n\n## v3.2.3 - 23 May 2022\n- Fix Only use setKeepAlive in Deno if available  28fbbaf\n- Fix wrong helper match on multiple occurances  02f3854\n\n#### Typescript related\n- Fix Deno assertRejects compatibility (#365)  0f0af92\n- Fix include missing boolean type in JSONValue union (#373)  1817387\n\n## v3.2.2 - 15 May 2022\n- Properly handle errors thrown on commit  99ddae4\n\n## v3.2.1 - 15 May 2022\n- Exclude target_session_attrs from connection obj  43f1442\n\n## v3.2.0 - 15 May 2022\n- Add `sslmode=verify-full` support  e67da29\n- Add support for array of fragments  342bf55\n- Add uri decode of host in url - fixes #346 1adc113\n- Add passing of rest url params to connection (ootb support cockroach urls)  41ed84f\n- Fix Deno partial writes  452a30d\n- Fix `as` dynamic helper  3300c40\n- Fix some nested fragments usage  9bfa902\n- Fix missing columns on `Result` when using simple protocol - fixes #350  1e2e298\n- Fix fragments in transactions - fixes #333  75914c7\n\n#### Typescript related\n- Upgrade/fix types (#357)  1e6d312\n- Add optional `onlisten` callback to `listen()` on TypeScript (#360)  6b749b2\n- Add implicit custom type inference (#361)  28512bf\n- Fix and improve sql() helper types (#338)  c1de3d8\n- Fix update query type def for `.writable()` and `.readable()` to return promises (#347)  51269ce\n- Add bigint to typescript Serializable - fixes #330  f1e41c3\n\n## v3.1.0 - 22 Apr 2022\n- Add close method to close but not end connections forever  94fea8f\n- Add .values() method to return rows as arrays of values  56873c2\n- Support transform.undefined - fixes #314  eab71e5\n- Support nested fragments values and dynamics - fixes #326  86445ca\n- Fix deno close sequence  f76af24\n- Fix subscribe reconnect and add onsubscribe method - fixes #315  5097345\n- Deno ts fix - fixes #327  50403a1\n\n## v3.0.6 - 19 Apr 2022\n- Properly close connections in Deno  cbc6a75\n- Only write end message if socket is open  13950af\n- Improve query cancellation  01c2c68\n- Use monotonically increasing time for timeout - fixes #316  9d7a21d\n- Add support for dynamic columns with `returning` - fixes #317  04644c0\n- Fix type errors in TypeScript deno projects (#313)  822fb21\n- Execute forEach instantly  44e9fbe\n\n## v3.0.5 - 6 Apr 2022\n- Fix transaction execution timing  28bb0b3\n- Add optional onlisten function to listen  1dc2fd2\n- Fix dynamic in helper after insert #305  4d63a59\n\n## v3.0.4 - 5 Apr 2022\n- Ensure drain only dequeues if ready - fixes #303  2e5f017\n\n## v3.0.3 - 4 Apr 2022\n- Run tests with github actions  b536d0d\n- Add custom socket option - fixes #284  5413f0c\n- Fix sql function overload type inference (#294)  3c4e90a\n- Update deno std to 0.132 and enable last tests  50762d4\n- Send proper client-encoding - Fixes #288  e5b8554\n\n## v3.0.2 - 31 Mar 2022\n- Fix BigInt handling  36a70df\n- Fix unsubscribing  (#300)  b6c597f\n- Parse update properly with identity full - Fixes #296  3ed11e7\n\n## v3.0.1 - 30 Mar 2022\n - Improve connection queue handling + fix leak cee1a57\n - Use publications option - fixes #295 b5ceecc\n - Throw proper query error if destroyed e148a0a\n - Transaction rejects with rethrown error - fixes #289 f7c8ae6\n - Only create origin stacktrace for tagged and debug - fixes #290 a782edf\n - Include types and readme in deno release - fixes #287 9068820\n - Disable fetch_types for Subscribe options 72e0cdb\n - Update TypeScript types with v3 changes (#293) db05836\n\n## v3.0.0 - 24 Mar 2022\nThis is a complete rewrite to better support all the features that I was trying to get into v2. There are a few breaking changes from v2 beta, which some (myself included) was using in production, so I'm skipping a stable v2 release and going straight to v3.\n\nHere are some of the new things available, but check the updated docs.\n- Dynamic query builder based on raw sql\n- Realtime subscribe to db changes through logical replication\n- Multi-host support for High Availability setups\n- Postgres input parameter types from `ParameterDescription`\n- Deno support\n- Cursors as async iterators\n- `.describe()` to only get query input types and column definitions\n- Support for Large Objects\n- `max_lifetime` for connections\n- Cancellation of requests\n- Converted to ESM (with CJS support)\n- Typescript support (Credit @minigugus)\n\n### Breaking changes from v2 -> v3\n- Cursors are always called with `Result` arrays (previously cursor 1 would return a row object, where > 1 would return an array of rows)\n- `.writable()` and `.readable()` is now async (returns a Promise that resolves to the stream)\n- Queries now returns a lazy promise instead of being executed immediately. This means the query won't be sent until awaited (.then, .catch, .finally is called) or until `.execute()` is manually called.\n- `.stream()` is renamed to `.forEach`\n- Returned results are now it's own `Result` class extending `Array` instead of an Array with extra properties (actually shouldn't be breaking unless you're doing something funny)\n- Parameters are now cast using the types returned from Postgres ParameterDescription with a fallback to the previously inferred types\n- Only tested with node v12 and up\n- Implicit array value to multiple parameter expansion removed (use sql([...]) instead)\n\n### Breaking changes from v1 -> v2 (v2 never moved on from beta)\n- All identifiers from `sql()` in queries are now always quoted\n- Undefined parameters are no longer allowed\n- Rename timeout option to `idle_timeout`\n- Default to 10 connections instead of number of CPUs\n- Numbers that cannot be safely cast to JS Number are returned as string. This happens for eg, `select count(*)` because `count()` returns a 64 bit integer (int8), so if you know your `count()` won't be too big for a js number just cast in your query to int4 like `select count(*)::int`\n\n## v1.0.2 - 21 Jan 2020\n\n- Fix standard postgres user env var (#20)  cce5ad7\n- Ensure url or options is not falsy  bc549b0\n- Add support for dynamic password  b2ab9fb\n- Fix hiding pass from options  3f76b98\n\n\n## v1.0.1 - 3 Jan 2020\n\n- Fix #3 url without db and trailing slash  45d4233\n- Fix stream promise - resolve with correct result  730df2c\n- Fix return value of unsafe query with multiple statements  748f198\n- Fix destroy before connected  f682ca1\n- Fix params usage for file() call without options  e4f12a4\n- Various Performance improvements\n\n## v1.0.0 - 22 Dec 2019\n\n- Initial release\n"
  },
  {
    "path": "README.md",
    "content": "<img align=\"left\" width=\"440\" height=\"180\" alt=\"Fastest full PostgreSQL nodejs client\" src=\"https://raw.githubusercontent.com/porsager/postgres/master/postgresjs.svg?sanitize=true\">\n\n- [🚀 Fastest full-featured node & deno client](https://github.com/porsager/postgres-benchmarks#results)\n- 🏷 ES6 Tagged Template Strings at the core\n- 🏄‍♀️ Simple surface API\n- 🖊️ Dynamic query support\n- 💬 Chat and help on [Gitter](https://gitter.im/porsager/postgres)\n- 🐦 Follow on [Twitter](https://twitter.com/rporsager)\n\n<br>\n\n## Getting started\n\n<br>\n<img height=\"220\" width=\"458\" alt=\"Good UX with Postgres.js\" src=\"https://raw.githubusercontent.com/porsager/postgres/master/demo.gif\">\n<br>\n\n### Installation\n```bash\n$ npm install postgres\n```\n\n### Usage\nCreate your `sql` database instance\n```js\n// db.js\nimport postgres from 'postgres'\n\nconst sql = postgres({ /* options */ }) // will use psql environment variables\n\nexport default sql\n```\n\nSimply import for use elsewhere\n```js\n// users.js\nimport sql from './db.js'\n\nasync function getUsersOver(age) {\n  const users = await sql`\n    select\n      name,\n      age\n    from users\n    where age > ${ age }\n  `\n  // users = Result [{ name: \"Walter\", age: 80 }, { name: 'Murray', age: 68 }, ...]\n  return users\n}\n\n\nasync function insertUser({ name, age }) {\n  const users = await sql`\n    insert into users\n      (name, age)\n    values\n      (${ name }, ${ age })\n    returning name, age\n  `\n  // users = Result [{ name: \"Murray\", age: 68 }]\n  return users\n}\n```\n\n#### ESM dynamic imports\n\nThe library can be used with ESM dynamic imports as well as shown here.\n\n```js\nconst { default: postgres } = await import('postgres')\n```\n\n## Table of Contents\n\n* [Connection](#connection)\n* [Queries](#queries)\n* [Building queries](#building-queries)\n* [Advanced query methods](#advanced-query-methods)\n* [Transactions](#transactions)\n* [Data Transformation](#data-transformation)\n* [Listen & notify](#listen--notify)\n* [Realtime subscribe](#realtime-subscribe)\n* [Numbers, bigint, numeric](#numbers-bigint-numeric)\n* [Result Array](#result-array)\n* [Connection details](#connection-details)\n* [Custom Types](#custom-types)\n* [Teardown / Cleanup](#teardown--cleanup)\n* [Error handling](#error-handling)\n* [TypeScript support](#typescript-support)\n* [Reserving connections](#reserving-connections)\n* [Changelog](./CHANGELOG.md)\n\n\n## Connection\n\n### `postgres([url], [options])`\n\nYou can use either a `postgres://` url connection string or the options to define your database connection properties. Options in the object will override any present in the url. Options will fall back to the same environment variables as psql.\n\n```js\nconst sql = postgres('postgres://username:password@host:port/database', {\n  host                 : '',            // Postgres ip address[s] or domain name[s]\n  port                 : 5432,          // Postgres server port[s]\n  database             : '',            // Name of database to connect to\n  username             : '',            // Username of database user\n  password             : '',            // Password of database user\n  ...and more\n})\n```\n\nMore options can be found in the [Connection details section](#connection-details).\n\n## Queries\n\n### ```await sql`...` -> Result[]```\n\nPostgres.js utilizes [Tagged template functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) to process query parameters **before** interpolation. Using tagged template literals benefits developers by:\n\n1. **Enforcing** safe query generation\n2. Giving the ` sql`` ` function powerful [utility](#dynamic-inserts) and [query building](#building-queries) features.\n\nAny generic value will be serialized according to an inferred type, and replaced by a PostgreSQL protocol placeholder `$1, $2, ...`. The parameters are then sent separately to the database which handles escaping & casting.\n\nAll queries will return a `Result` array, with objects mapping column names to each row.\n\n```js\nconst xs = await sql`\n  insert into users (\n    name, age\n  ) values (\n    'Murray', 68\n  )\n\n  returning *\n`\n\n// xs = [{ user_id: 1, name: 'Murray', age: 68 }]\n```\n\n> Please note that queries are first executed when `awaited` – or instantly by using [`.execute()`](#execute).\n\n### Query parameters\n\nParameters are automatically extracted and handled by the database so that SQL injection isn't possible. No special handling is necessary, simply use tagged template literals as usual.\n\n```js\nconst name = 'Mur'\n    , age = 60\n\nconst users = await sql`\n  select\n    name,\n    age\n  from users\n  where\n    name like ${ name + '%' }\n    and age > ${ age }\n`\n// users = [{ name: 'Murray', age: 68 }]\n```\n\n> Be careful with quotation marks here. Because Postgres infers column types, you do not need to wrap your interpolated parameters in quotes like `'${name}'`. This will cause an error because the tagged template replaces `${name}` with `$1` in the query string, leaving Postgres to do the interpolation. If you wrap that in a string, Postgres will see `'$1'` and interpret it as a string as opposed to a parameter.\n\n### Dynamic column selection\n\n```js\nconst columns = ['name', 'age']\n\nawait sql`\n  select\n    ${ sql(columns) }\n  from users\n`\n\n// Which results in:\nselect \"name\", \"age\" from users\n```\n\n### Dynamic inserts\n\n```js\nconst user = {\n  name: 'Murray',\n  age: 68\n}\n\nawait sql`\n  insert into users ${\n    sql(user, 'name', 'age')\n  }\n`\n\n// Which results in:\ninsert into users (\"name\", \"age\") values ($1, $2)\n\n// The columns can also be given with an array\nconst columns = ['name', 'age']\n\nawait sql`\n  insert into users ${\n    sql(user, columns)\n  }\n`\n```\n\n**You can omit column names and simply execute `sql(user)` to get all the fields from the object as columns**. Be careful not to allow users to supply columns that you do not want to be inserted.\n\n#### Multiple inserts in one query\nIf you need to insert multiple rows at the same time it's also much faster to do it with a single `insert`. Simply pass an array of objects to `sql()`.\n\n```js\nconst users = [{\n  name: 'Murray',\n  age: 68,\n  garbage: 'ignore'\n},\n{\n  name: 'Walter',\n  age: 80\n}]\n\nawait sql`insert into users ${ sql(users, 'name', 'age') }`\n\n// Is translated to:\ninsert into users (\"name\", \"age\") values ($1, $2), ($3, $4)\n\n// Here you can also omit column names which will use object keys as columns\nawait sql`insert into users ${ sql(users) }`\n\n// Which results in:\ninsert into users (\"name\", \"age\") values ($1, $2), ($3, $4)\n```\n\n### Dynamic columns in updates\nThis is also useful for update queries\n```js\nconst user = {\n  id: 1,\n  name: 'Murray',\n  age: 68\n}\n\nawait sql`\n  update users set ${\n    sql(user, 'name', 'age')\n  }\n  where user_id = ${ user.id }\n`\n\n// Which results in:\nupdate users set \"name\" = $1, \"age\" = $2 where user_id = $3\n\n// The columns can also be given with an array\nconst columns = ['name', 'age']\n\nawait sql`\n  update users set ${\n    sql(user, columns)\n  }\n  where user_id = ${ user.id }\n`\n```\n\n### Multiple updates in one query\nTo create multiple updates in a single query, it is necessary to use arrays instead of objects to ensure that the order of the items correspond with the column names.\n```js\nconst users = [\n  [1, 'John', 34],\n  [2, 'Jane', 27],\n]\n\nawait sql`\n  update users set name = update_data.name, age = (update_data.age)::int\n  from (values ${sql(users)}) as update_data (id, name, age)\n  where users.id = (update_data.id)::int\n  returning users.id, users.name, users.age\n`\n```\n\n### Dynamic values and `where in`\nValue lists can also be created dynamically, making `where in` queries simple too.\n```js\nconst users = await sql`\n  select\n    *\n  from users\n  where age in ${ sql([68, 75, 23]) }\n`\n```\n\nor\n```js\nconst [{ a, b, c }] = await sql`\n  select\n    *\n  from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)\n`\n```\n\n## Building queries\n\nPostgres.js features a simple dynamic query builder by conditionally appending/omitting query fragments.\nIt works by nesting ` sql`` ` fragments within other ` sql`` ` calls or fragments. This allows you to build dynamic queries safely without risking sql injections through usual string concatenation.\n\n### Partial queries\n```js\nconst olderThan = x => sql`and age > ${ x }`\n\nconst filterAge = true\n\nawait sql`\n  select\n   *\n  from users\n  where name is not null ${\n    filterAge\n      ? olderThan(50)\n      : sql``\n  }\n`\n// Which results in:\nselect * from users where name is not null\n// Or\nselect * from users where name is not null and age > 50\n```\n\n### Dynamic filters\n```js\nawait sql`\n  select\n    *\n  from users ${\n    id\n      ? sql`where user_id = ${ id }`\n      : sql``\n  }\n`\n\n// Which results in:\nselect * from users\n// Or\nselect * from users where user_id = $1\n```\n\n### Dynamic ordering\n\n```js\nconst id = 1\nconst order = {\n  username: 'asc'\n  created_at: 'desc'\n}\nawait sql`\n  select \n    * \n  from ticket \n  where account = ${ id }  \n  order by ${\n    Object.entries(order).flatMap(([column, order], i) =>\n      [i ? sql`,` : sql``, sql`${ sql(column) } ${ order === 'desc' ? sql`desc` : sql`asc` }`]\n    )\n  }\n`\n```\n\n### SQL functions\nUsing keywords or calling functions dynamically is also possible by using ``` sql`` ``` fragments.\n```js\nconst date = null\n\nawait sql`\n  update users set updated_at = ${ date || sql`now()` }\n`\n\n// Which results in:\nupdate users set updated_at = now()\n```\n\n### Table names\nDynamic identifiers like table names and column names is also supported like so:\n```js\nconst table = 'users'\n    , column = 'id'\n\nawait sql`\n  select ${ sql(column) } from ${ sql(table) }\n`\n\n// Which results in:\nselect \"id\" from \"users\"\n```\n\n### Quick primer on interpolation\n\nHere's a quick oversight over all the ways to do interpolation in a query template string:\n\n| Interpolation syntax       | Usage                         | Example                                                   |\n| -------------              | -------------                 | -------------                                             |\n| `${ sql`` }`               | for keywords or sql fragments | ``await sql`SELECT * FROM users ${sql`order by age desc` }` ``  |\n| `${ sql(string) }`         | for identifiers               | ``await sql`SELECT * FROM ${sql('table_name')` ``               |\n| `${ sql([] or {}, ...) }`  | for helpers                   | ``await sql`INSERT INTO users ${sql({ name: 'Peter'})}` ``      |\n| `${ 'somevalue' }`         | for values                    | ``await sql`SELECT * FROM users WHERE age = ${42}` ``           |\n\n## Advanced query methods\n\n### Cursors\n\n#### ```await sql``.cursor([rows = 1], [fn])```\n\nUse cursors if you need to throttle the amount of rows being returned from a query. You can use a cursor either as an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) or with a callback function. For a callback function new results won't be requested until the promise / async callback function has resolved.\n\n##### callback function\n```js\nawait sql`\n  select\n    *\n  from generate_series(1,4) as x\n`.cursor(async([row]) => {\n  // row = { x: 1 }\n  await http.request('https://example.com/wat', { row })\n})\n```\n\n##### for await...of\n```js\n// for await...of\nconst cursor = sql`select * from generate_series(1,4) as x`.cursor()\n\nfor await (const [row] of cursor) {\n  // row = { x: 1 }\n  await http.request('https://example.com/wat', { row })\n}\n```\n\nA single row will be returned by default, but you can also request batches by setting the number of rows desired in each batch as the first argument to `.cursor`:\n```js\nawait sql`\n  select\n    *\n  from generate_series(1,1000) as x\n`.cursor(10, async rows => {\n  // rows = [{ x: 1 }, { x: 2 }, ... ]\n  await Promise.all(rows.map(row =>\n    http.request('https://example.com/wat', { row })\n  ))\n})\n```\n\nIf an error is thrown inside the callback function no more rows will be requested and the outer promise will reject with the thrown error.\n\nYou can close the cursor early either by calling `break` in the `for await...of` loop, or by returning the token `sql.CLOSE` from the callback function.\n\n```js\nawait sql`\n  select * from generate_series(1,1000) as x\n`.cursor(row => {\n  return Math.random() > 0.9 && sql.CLOSE // or sql.END\n})\n```\n\n### Instant iteration\n\n#### ```await sql``.forEach(fn)```\n\nIf you want to handle rows returned by a query one by one, you can use `.forEach` which returns a promise that resolves once there are no more rows.\n```js\nawait sql`\n  select created_at, name from events\n`.forEach(row => {\n  // row = { created_at: '2019-11-22T14:22:00Z', name: 'connected' }\n})\n\n// No more rows\n```\n\n### Query Descriptions\n#### ```await sql``.describe() -> Result[]```\n\nRather than executing a given query, `.describe` will return information utilized in the query process. This information can include the query identifier, column types, etc.\n\nThis is useful for debugging and analyzing your Postgres queries. Furthermore, **`.describe` will give you access to the final generated query string that would be executed.**\n\n### Rows as Array of Values\n#### ```sql``.values()```\n\nUsing `.values` will return rows as an array of values for each column, instead of objects.\n\nThis can be useful to receive identically named columns, or for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.\n\n### Rows as Raw Array of Buffers\n#### ```sql``.raw()```\n\nUsing `.raw` will return rows as an array with `Buffer` values for each column, instead of objects.\n\nThis can be useful for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.\n\n### Queries in Files\n#### `await sql.file(path, [args], [options]) -> Result[]`\n\nUsing a file for a query is also supported with optional parameters to use if the file includes `$1, $2, etc`\n\n```js\nconst result = await sql.file('query.sql', ['Murray', 68])\n```\n\n### Multiple statements in one query\n#### ```await sql``.simple()```\n\nThe postgres wire protocol supports [\"simple\"](https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.6.7.4) and [\"extended\"](https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY) queries. \"simple\" queries supports multiple statements, but does not support any dynamic parameters. \"extended\" queries support parameters but only one statement. To use \"simple\" queries you can use\n```sql``.simple()```. That will create it as a simple query.\n\n```js\nawait sql`select 1; select 2;`.simple()\n```\n\n### Copy to/from as Streams\n\nPostgres.js supports [`COPY ...`](https://www.postgresql.org/docs/14/sql-copy.html) queries, which are exposed as [Node.js streams](https://nodejs.org/api/stream.html).\n\n#### ```await sql`copy ... from stdin`.writable() -> Writable```\n\n```js\nimport { pipeline } from 'node:stream/promises'\n\n// Stream of users with the default tab delimitated cells and new-line delimitated rows\nconst userStream = Readable.from([\n  'Murray\\t68\\n',\n  'Walter\\t80\\n'\n])\n\nconst query = await sql`copy users (name, age) from stdin`.writable()\nawait pipeline(userStream, query);\n```\n\n#### ```await sql`copy ... to stdout`.readable() -> Readable```\n\n##### Using Stream Pipeline\n```js\nimport { pipeline } from 'node:stream/promises'\nimport { createWriteStream } from 'node:fs'\n\nconst readableStream = await sql`copy users (name, age) to stdout`.readable()\nawait pipeline(readableStream, createWriteStream('output.tsv'))\n// output.tsv content: `Murray\\t68\\nWalter\\t80\\n`\n```\n\n##### Using `for await...of`\n```js\nconst readableStream = await sql`\n  copy (\n    select name, age\n    from users\n    where age = 68\n  ) to stdout\n`.readable()\nfor await (const chunk of readableStream) {\n  // chunk.toString() === `Murray\\t68\\n`\n}\n```\n\n> **NOTE** This is a low-level API which does not provide any type safety. To make this work, you must match your [`copy query` parameters](https://www.postgresql.org/docs/14/sql-copy.html) correctly to your [Node.js stream read or write](https://nodejs.org/api/stream.html) code. Ensure [Node.js stream backpressure](https://nodejs.org/en/learn/modules/backpressuring-in-streams) is handled correctly to avoid memory exhaustion.\n\n### Canceling Queries in Progress\n\nPostgres.js supports, [canceling queries in progress](https://www.postgresql.org/docs/7.1/protocol-protocol.html#AEN39000). It works by opening a new connection with a protocol level startup message to cancel the current query running on a specific connection. That means there is no guarantee that the query will be canceled, and due to the possible race conditions it might even result in canceling another query. This is fine for long running queries, but in the case of high load and fast queries it might be better to simply ignore results instead of canceling.\n\n```js\nconst query = sql`select pg_sleep 100`.execute()\nsetTimeout(() => query.cancel(), 100)\nconst result = await query\n```\n\n### Execute\n\n#### ```await sql``.execute()```\n\nThe lazy Promise implementation in Postgres.js is what allows it to distinguish [Nested Fragments](#building-queries) from the main outer query. This also means that queries are always executed at the earliest in the following tick. If you have a specific need to execute the query in the same tick, you can call `.execute()`\n\n### Unsafe raw string queries\n\n<details>\n<summary>Advanced unsafe use cases</summary>\n\n### `await sql.unsafe(query, [args], [options]) -> Result[]`\n\nIf you know what you're doing, you can use `unsafe` to pass any string you'd like to postgres. Please note that this can lead to SQL injection if you're not careful.\n\n```js\nsql.unsafe('select ' + danger + ' from users where id = ' + dragons)\n```\n\nBy default, `sql.unsafe` assumes the `query` string is sufficiently dynamic that prepared statements do not make sense, and so defaults them to off. If you'd like to re-enable prepared statements, you can pass `{ prepare: true }`.\n\nYou can also nest `sql.unsafe` within a safe `sql` expression.  This is useful if only part of your fraction has unsafe elements.\n\n```js\nconst triggerName = 'friend_created'\nconst triggerFnName = 'on_friend_created'\nconst eventType = 'insert'\nconst schema_name = 'app'\nconst table_name = 'friends'\n\nawait sql`\n  create or replace trigger ${sql(triggerName)}\n  after ${sql.unsafe(eventType)} on ${sql.unsafe(`${schema_name}.${table_name}`)}\n  for each row\n  execute function ${sql(triggerFnName)}()\n`\n\nawait sql`\n  create role friend_service with login password ${sql.unsafe(`'${password}'`)}\n`\n```\n\n</details>\n\n## Transactions\n\n#### BEGIN / COMMIT `await sql.begin([options = ''], fn) -> fn()`\n\nUse `sql.begin` to start a new transaction. Postgres.js will reserve a connection for the transaction and supply a scoped `sql` instance for all transaction uses in the callback function. `sql.begin` will resolve with the returned value from the callback function.\n\n`BEGIN` is automatically sent with the optional options, and if anything fails `ROLLBACK` will be called so the connection can be released and execution can continue.\n\n```js\nconst [user, account] = await sql.begin(async sql => {\n  const [user] = await sql`\n    insert into users (\n      name\n    ) values (\n      'Murray'\n    )\n    returning *\n  `\n\n  const [account] = await sql`\n    insert into accounts (\n      user_id\n    ) values (\n      ${ user.user_id }\n    )\n    returning *\n  `\n\n  return [user, account]\n})\n```\n\nDo note that you can often achieve the same result using [`WITH` queries (Common Table Expressions)](https://www.postgresql.org/docs/current/queries-with.html) instead of using transactions.\n\nIt's also possible to pipeline the requests in a transaction if needed by returning an array with queries from the callback function like this:\n\n```js\nconst result = await sql.begin(sql => [\n  sql`update ...`,\n  sql`update ...`,\n  sql`insert ...`\n])\n```\n\n#### SAVEPOINT `await sql.savepoint([name], fn) -> fn()`\n\n```js\nsql.begin('read write', async sql => {\n  const [user] = await sql`\n    insert into users (\n      name\n    ) values (\n      'Murray'\n    )\n  `\n\n  const [account] = (await sql.savepoint(sql =>\n    sql`\n      insert into accounts (\n        user_id\n      ) values (\n        ${ user.user_id }\n      )\n    `\n  ).catch(err => {\n    // Account could not be created. ROLLBACK SAVEPOINT is called because we caught the rejection.\n  })) || []\n\n  return [user, account]\n})\n.then(([user, account]) => {\n  // great success - COMMIT succeeded\n})\n.catch(() => {\n  // not so good - ROLLBACK was called\n})\n```\n\n\n#### PREPARE TRANSACTION `await sql.prepare([name]) -> fn()`\n\nIndicates that the transactions should be prepared using the [`PREPARE TRANSACTION [NAME]`](https://www.postgresql.org/docs/current/sql-prepare-transaction.html) statement\ninstead of being committed.\n\n```js\nsql.begin('read write', async sql => {\n  const [user] = await sql`\n    insert into users (\n      name\n    ) values (\n      'Murray'\n    )\n  `\n\n  await sql.prepare('tx1')\n})\n```\n\n## Data Transformation\n\nPostgres.js allows for transformation of the data passed to or returned from a query by using the `transform` option.\n\nBuilt in transformation functions are:\n\n* For camelCase - `postgres.camel`, `postgres.toCamel`, `postgres.fromCamel`\n* For PascalCase - `postgres.pascal`, `postgres.toPascal`, `postgres.fromPascal`\n* For Kebab-Case - `postgres.kebab`, `postgres.toKebab`, `postgres.fromKebab`\n\nThese built in transformations will only convert to/from snake_case. For example, using `{ transform: postgres.toCamel }` will convert the column names to camelCase only if the column names are in snake_case to begin with. `{ transform: postgres.fromCamel }` will convert camelCase only to snake_case.\n\nBy default, using `postgres.camel`, `postgres.pascal` and `postgres.kebab` will perform a two-way transformation - both the data passed to the query and the data returned by the query will be transformed:\n\n```js\n// Transform the column names to and from camel case\nconst sql = postgres({ transform: postgres.camel })\n\nawait sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER, b_test TEXT)`\nawait sql`INSERT INTO camel_case ${ sql([{ aTest: 1, bTest: 1 }]) }`\nconst data = await sql`SELECT ${ sql('aTest', 'bTest') } FROM camel_case`\n\nconsole.log(data) // [ { aTest: 1, bTest: '1' } ]\n```\n\nTo only perform half of the transformation (eg. only the transformation **to** or **from** camel case), use the other transformation functions:\n\n```js\n// Transform the column names only to camel case\n// (for the results that are returned from the query)\npostgres({ transform: postgres.toCamel })\n\nawait sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`\nawait sql`INSERT INTO camel_case ${ sql([{ a_test: 1 }]) }`\nconst data = await sql`SELECT a_test FROM camel_case`\n\nconsole.log(data) // [ { aTest: 1 } ]\n```\n\n```js\n// Transform the column names only from camel case\n// (for interpolated inserts, updates, and selects)\nconst sql = postgres({ transform: postgres.fromCamel })\n\nawait sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`\nawait sql`INSERT INTO camel_case ${ sql([{ aTest: 1 }]) }`\nconst data = await sql`SELECT ${ sql('aTest') } FROM camel_case`\n\nconsole.log(data) // [ { a_test: 1 } ]\n```\n\n> Note that Postgres.js does not rewrite the static parts of the tagged template strings. So to transform column names in your queries, the `sql()` helper must be used - eg. `${ sql('columnName') }` as in the examples above.\n\n### Transform `undefined` Values\n\nBy default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed\n\n```js\n// Transform the column names to and from camel case\nconst sql = postgres({\n  transform: {\n    undefined: null\n  }\n})\n\nawait sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`\nawait sql`INSERT INTO transform_undefined ${ sql([{ a_test: undefined }]) }`\nconst data = await sql`SELECT a_test FROM transform_undefined`\n\nconsole.log(data) // [ { a_test: null } ]\n```\n\nTo combine with the built in transform functions, spread the transform in the `transform` object:\n\n```js\n// Transform the column names to and from camel case\nconst sql = postgres({\n  transform: {\n    ...postgres.camel,\n    undefined: null\n  }\n})\n\nawait sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`\nawait sql`INSERT INTO transform_undefined ${ sql([{ aTest: undefined }]) }`\nconst data = await sql`SELECT ${ sql('aTest') } FROM transform_undefined`\n\nconsole.log(data) // [ { aTest: null } ]\n```\n\n### Custom Transform Functions\n\nTo specify your own transformation functions, you can use the `column`, `value` and `row` options inside of `transform`, each an object possibly including `to` and `from` keys:\n\n* `to`: The function to transform the outgoing query column name to, i.e `SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.\n* `from`: The function to transform the incoming query result column name to, see example below.\n\n> Both parameters are optional, if not provided, the default transformation function will be used.\n\n```js\n// Implement your own functions, look at postgres.toCamel, etc\n// as a reference:\n// https://github.com/porsager/postgres/blob/4241824ffd7aa94ffb482e54ca9f585d9d0a4eea/src/types.js#L310-L328\nfunction transformColumnToDatabase() { /* ... */ }\nfunction transformColumnFromDatabase() { /* ... */ }\n\nconst sql = postgres({\n  transform: {\n    column: {\n      to: transformColumnToDatabase,\n      from: transformColumnFromDatabase,\n    },\n    value: { /* ... */ },\n    row: { /* ... */ }\n  }\n})\n```\n\n## Listen & notify\n\nWhen you call `.listen`, a dedicated connection will be created to ensure that you receive notifications instantly. This connection will be used for any further calls to `.listen`. The connection will automatically reconnect according to a backoff reconnection pattern to not overload the database server.\n\n### Listen `await sql.listen(channel, onnotify, [onlisten]) -> { state }`\n`.listen` takes the channel name, a function to handle each notify, and an optional function to run every time listen is registered and ready (happens on initial connect and reconnects). It returns a promise which resolves once the `LISTEN` query to Postgres completes, or if there is already a listener active.\n\n```js\nawait sql.listen('news', payload => {\n  const json = JSON.parse(payload)\n  console.log(json.this) // logs 'is'\n})\n```\n\nThe optional `onlisten` method is great to use for a very simply queue mechanism:\n\n```js\nawait sql.listen(\n  'jobs',\n  (x) => run(JSON.parse(x)),\n  ( ) => sql`select unfinished_jobs()`.forEach(run)\n)\n\nfunction run(job) {\n  // And here you do the work you please\n}\n```\n### Notify `await sql.notify(channel, payload) -> Result[]`\nNotify can be done as usual in SQL, or by using the `sql.notify` method.\n```js\nsql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))\n```\n\n## Realtime subscribe\n\nPostgres.js implements the logical replication protocol of PostgreSQL to support subscription to real-time updates of `insert`, `update` and `delete` operations.\n\n> **NOTE** To make this work you must [create the proper publications in your database](https://www.postgresql.org/docs/current/sql-createpublication.html), enable logical replication by setting `wal_level = logical` in `postgresql.conf` and connect using either a replication or superuser.\n\n### Quick start\n\n#### Create a publication (eg. in migration)\n```sql\nCREATE PUBLICATION alltables FOR ALL TABLES\n```\n\n#### Subscribe to updates\n```js\nconst sql = postgres({ publications: 'alltables' })\n\nconst { unsubscribe } = await sql.subscribe(\n  'insert:events',\n  (row, { command, relation, key, old }) => {\n    // Callback function for each row change\n    // tell about new event row over eg. websockets or do something else\n  },\n  () => {\n    // Callback on initial connect and potential reconnects\n  }\n)\n```\n\n### Subscribe pattern\n\nYou can subscribe to specific operations, tables, or even rows with primary keys.\n\n#### `operation`      `:` `schema` `.` `table` `=` `primary_key`\n\n**`operation`** is one of ``` * | insert | update | delete ``` and defaults to `*`\n\n**`schema`** defaults to `public`\n\n**`table`** is a specific table name and defaults to `*`\n\n**`primary_key`** can be used to only subscribe to specific rows\n\n### Examples\n\n```js\nsql.subscribe('*',                () => /* everything */ )\nsql.subscribe('insert',           () => /* all inserts */ )\nsql.subscribe('*:users',          () => /* all operations on the public.users table */ )\nsql.subscribe('delete:users',     () => /* all deletes on the public.users table */ )\nsql.subscribe('update:users=1',   () => /* all updates on the users row with a primary key = 1 */ )\n```\n\n## Numbers, bigint, numeric\n\n`Number` in javascript is only able to represent 2<sup>53</sup>-1 safely which means that types in PostgreSQLs like `bigint` and `numeric` won't fit into `Number`.\n\nSince Node.js v10.4 we can use [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) to match the PostgreSQL type `bigint` which is returned for eg. `count(*)`. Unfortunately, it doesn't work with `JSON.stringify` out of the box, so Postgres.js will return it as a string.\n\nIf you want to use `BigInt` you can add this custom type:\n\n```js\nconst sql = postgres({\n  types: {\n    bigint: postgres.BigInt\n  }\n})\n```\n\nThere is currently no guaranteed way to handle `numeric` / `decimal` types in native Javascript. **These [and similar] types will be returned as a `string`**. The best way in this case is to use  [custom types](#custom-types).\n\n## Result Array\n\nThe `Result` Array returned from queries is a custom array allowing for easy destructuring or passing on directly to JSON.stringify or general Array usage. It includes the following properties.\n\n### .count\n\nThe `count` property is the number of affected rows returned by the database. This is useful for insert, update and delete operations to know the number of rows since .length will be 0 in these cases if not using `RETURNING ...`.\n\n### .command\n\nThe `command` run by the query - eg. one of `SELECT`, `UPDATE`, `INSERT`, `DELETE`\n\n### .columns\n\nThe `columns` returned by the query useful to determine types, or map to the result values when using `.values()`\n\n```js\n{\n  name  : String,    // Column name,\n  type  : oid,       // PostgreSQL oid column type\n  parser: Function   // The function used by Postgres.js for parsing\n}\n```\n\n### .statement\n\nThe `statement` contains information about the statement implicitly created by Postgres.js.\n\n```js\n{\n  name    : String,  // The auto generated statement name\n  string  : String,  // The actual query string executed\n  types   : [oid],   // An array of oid expected as input parameters\n  columns : [Column] // Array of columns - same as Result.columns\n}\n```\n\n### .state\n\nThis is the state `{ pid, secret }` of the connection that executed the query.\n\n## Connection details\n\n### All Postgres options\n\n```js\nconst sql = postgres('postgres://username:password@host:port/database', {\n  host                 : '',            // Postgres ip address[es] or domain name[s]\n  port                 : 5432,          // Postgres server port[s]\n  path                 : '',            // unix socket path (usually '/tmp')\n  database             : '',            // Name of database to connect to\n  username             : '',            // Username of database user\n  password             : '',            // Password of database user\n  ssl                  : false,         // true, prefer, require, tls.connect options\n  sslnegotiation       : null,          // direct\n  max                  : 10,            // Max number of connections\n  max_lifetime         : null,          // Max lifetime in seconds (more info below)\n  idle_timeout         : 0,             // Idle connection timeout in seconds\n  connect_timeout      : 30,            // Connect timeout in seconds\n  prepare              : true,          // Automatic creation of prepared statements\n  types                : [],            // Array of custom types, see more below\n  onnotice             : fn,            // Default console.log, set false to silence NOTICE\n  onparameter          : fn,            // (key, value) when server param change\n  debug                : fn,            // Is called with (connection, query, params, types)\n  socket               : fn,            // fn returning custom socket to use\n  transform            : {\n    undefined          : undefined,     // Transforms undefined values (eg. to null)\n    column             : fn,            // Transforms incoming column names\n    value              : fn,            // Transforms incoming row values\n    row                : fn             // Transforms entire rows\n  },\n  connection           : {\n    application_name   : 'postgres.js', // Default application_name\n    ...                                 // Other connection parameters, see https://www.postgresql.org/docs/current/runtime-config-client.html\n  },\n  target_session_attrs : null,          // Use 'read-write' with multiple hosts to\n                                        // ensure only connecting to primary\n  fetch_types          : true,          // Automatically fetches types on connect\n                                        // on initial connection.\n})\n```\n\nNote that `max_lifetime = 60 * (30 + Math.random() * 30)` by default. This resolves to an interval between 30 and 60 minutes to optimize for the benefits of prepared statements **and** working nicely with Linux's OOM killer.\n\n### Dynamic passwords\n\nWhen clients need to use alternative authentication schemes such as access tokens or connections to databases with rotating passwords, provide either a synchronous or asynchronous function that will resolve the dynamic password value at connection time.\n\n```js\nconst sql = postgres(url, {\n  // Other connection config\n  ...\n  // Password function for the database user\n  password : async () => await signer.getAuthToken(),\n})\n```\n\n### SSL\n\nAlthough [vulnerable to MITM attacks](https://security.stackexchange.com/a/229297/174913), a common configuration for the `ssl` option for some cloud providers is to set `rejectUnauthorized` to `false` (if `NODE_ENV` is `production`):\n\n```js\nconst sql =\n  process.env.NODE_ENV === 'production'\n    ? // \"Unless you're using a Private or Shield Heroku Postgres database, Heroku Postgres does not currently support verifiable certificates\"\n      // https://help.heroku.com/3DELT3RK/why-can-t-my-third-party-utility-connect-to-heroku-postgres-with-ssl\n      postgres({ ssl: { rejectUnauthorized: false } })\n    : postgres()\n```\n\nFor more information regarding `ssl` with `postgres`, check out the [Node.js documentation for tls](https://nodejs.org/dist/latest-v16.x/docs/api/tls.html#new-tlstlssocketsocket-options).\n\n\n### Multi-host connections - High Availability (HA)\n\nMultiple connection strings can be passed to `postgres()` in the form of `postgres('postgres://localhost:5432,localhost:5433', ...)`. This works the same as native the `psql` command. Read more at [multiple host URIs](https://www.postgresql.org/docs/13/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS).\n\nConnections will be attempted in order of the specified hosts/ports. On a successful connection, all retries will be reset. This ensures that hosts can come up and down seamlessly.\n\nIf you specify `target_session_attrs: 'primary'` or `PGTARGETSESSIONATTRS=primary` Postgres.js will only connect to the primary host, allowing for zero downtime failovers.\n\n### The Connection Pool\n\nConnections are created lazily once a query is created. This means that simply doing const `sql = postgres(...)` won't have any effect other than instantiating a new `sql` instance.\n\n> No connection will be made until a query is made.\n\nFor example:\n\n```js\nconst sql = postgres() // no connections are opened\n\nawait sql`...` // one connection is now opened\nawait sql`...` // previous opened connection is reused\n\n// two connections are opened now\nawait Promise.all([\n  sql`...`,\n  sql`...`\n])\n```\n\n> When there are high amount of concurrent queries, `postgres` will open as many connections as needed up until `max` number of connections is reached. By default `max` is 10. This can be changed by setting `max` in the `postgres()` call. Example - `postgres('connectionURL', { max: 20 })`.\n\nThis means that we get a much simpler story for error handling and reconnections. Queries will be sent over the wire immediately on the next available connection in the pool. Connections are automatically taken out of the pool if you start a transaction using `sql.begin()`, and automatically returned to the pool once your transaction is done.\n\nAny query which was already sent over the wire will be rejected if the connection is lost. It'll automatically defer to the error handling you have for that query, and since connections are lazy it'll automatically try to reconnect the next time a query is made. The benefit of this is no weird generic \"onerror\" handler that tries to get things back to normal, and also simpler application code since you don't have to handle errors out of context.\n\nThere are no guarantees about queries executing in order unless using a transaction with `sql.begin()` or setting `max: 1`. Of course doing a series of queries, one awaiting the other will work as expected, but that's just due to the nature of js async/promise handling, so it's not necessary for this library to be concerned with ordering.\n\nSince this library automatically creates prepared statements, it also has a default max lifetime for connections to prevent memory bloat on the database itself. This is a random interval for each connection between 45 and 90 minutes. This allows multiple connections to independently come up and down without affecting the service.\n\n### Connection timeout\n\nBy default, connections will not close until `.end()` is called. However, it may be useful to have them close automatically when:\n\n- re-instantiating multiple ` sql`` ` instances\n- using Postgres.js in a Serverless environment (Lambda, etc.)\n- using Postgres.js with a database service that automatically closes connections after some time (see [`ECONNRESET` issue](https://github.com/porsager/postgres/issues/179))\n\nThis can be done using the `idle_timeout` or `max_lifetime` options. These configuration options specify the number of seconds to wait before automatically closing an idle connection and the maximum time a connection can exist, respectively.\n\nFor example, to close a connection that has either been idle for 20 seconds or existed for more than 30 minutes:\n\n```js\nconst sql = postgres({\n  idle_timeout: 20,\n  max_lifetime: 60 * 30\n})\n```\n\n### Cloudflare Workers support\n\nPostgres.js has built-in support for the [TCP socket API](https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/) in Cloudflare Workers, which is [on-track](https://github.com/wintercg/proposal-sockets-api) to be standardized and adopted in Node.js and other JavaScript runtimes, such as Deno.\n\nYou can use Postgres.js directly in a Worker, or to benefit from connection pooling and query caching, via the [Hyperdrive](https://developers.cloudflare.com/hyperdrive/learning/connect-to-postgres/#driver-examples) service available to Workers by passing the Hyperdrive `connectionString` when creating a new `postgres` client as follows:\n\n```ts\n// Requires Postgres.js 3.4.0 or later\nimport postgres from 'postgres'\n\ninterface Env {\n    HYPERDRIVE: Hyperdrive;\n}\n\nexport default async fetch(req: Request, env: Env, ctx: ExecutionContext) {\n    // The Postgres.js library accepts a connection string directly\n    const sql = postgres(env.HYPERDRIVE.connectionString)\n    const results = await sql`SELECT * FROM users LIMIT 10`\n    return Response.json(results)\n}\n```\n\nIn `wrangler.toml` you will need to enable the `nodejs_compat` compatibility flag to allow Postgres.js to operate in the Workers environment:\n\n```toml\ncompatibility_flags = [\"nodejs_compat\"]\n```\n\n### Auto fetching of array types\n\nPostgres.js will automatically fetch table/array-type information when it first connects to a database.\n\nIf you have revoked access to `pg_catalog` this feature will no longer work and will need to be disabled.\n\nYou can disable this feature by setting `fetch_types` to `false`.\n\n### Environmental variables\n\nIt is also possible to connect to the database without a connection string or any options. Postgres.js will fall back to the common environment variables used by `psql` as in the table below:\n\n```js\nconst sql = postgres()\n```\n\n| Option             | Environment Variables    |\n| ------------------ | ------------------------ |\n| `host`             | `PGHOST`                 |\n| `port`             | `PGPORT`                 |\n| `database`         | `PGDATABASE`             |\n| `username`         | `PGUSERNAME` or `PGUSER` |\n| `password`         | `PGPASSWORD`             |\n| `application_name` | `PGAPPNAME`              |\n| `idle_timeout`     | `PGIDLE_TIMEOUT`         |\n| `connect_timeout`  | `PGCONNECT_TIMEOUT`      |\n\n### Prepared statements\n\nPrepared statements will automatically be created for any queries where it can be inferred that the query is static. This can be disabled by using the `prepare: false` option. For instance — this is useful when [using PGBouncer in `transaction mode`](https://github.com/porsager/postgres/issues/93#issuecomment-656290493).\n\n**update**: [since 1.21.0](https://www.pgbouncer.org/2023/10/pgbouncer-1-21-0)\nPGBouncer supports protocol-level named prepared statements when [configured\nproperly](https://www.pgbouncer.org/config.html#max_prepared_statements)\n\n## Custom Types\n\nYou can add ergonomic support for custom types, or simply use `sql.typed(value, type)` inline, where type is the PostgreSQL `oid` for the type and the correctly serialized string. _(`oid` values for types can be found in the `pg_catalog.pg_type` table.)_\n\nAdding Query helpers is the cleanest approach which can be done like this:\n\n```js\nconst sql = postgres({\n  types: {\n    rect: {\n      // The pg_types oid to pass to the db along with the serialized value.\n      to        : 1337,\n\n      // An array of pg_types oids to handle when parsing values coming from the db.\n      from      : [1337],\n\n      //Function that transform values before sending them to the db.\n      serialize : ({ x, y, width, height }) => [x, y, width, height],\n\n      // Function that transforms values coming from the db.\n      parse     : ([x, y, width, height]) => { x, y, width, height }\n    }\n  }\n})\n\n// Now you can use sql.typed.rect() as specified above\nconst [custom] = await sql`\n  insert into rectangles (\n    name,\n    rect\n  ) values (\n    'wat',\n    ${ sql.typed.rect({ x: 13, y: 37, width: 42, height: 80 }) }\n  )\n  returning *\n`\n\n// custom = { name: 'wat', rect: { x: 13, y: 37, width: 42, height: 80 } }\n\n```\n\n### Custom socket\n\nEasily do in-process ssh tunneling to your database by providing a custom socket for Postgres.js to use. The function (optionally async) must return a socket-like duplex stream.\n\nHere's a sample using [ssh2](https://github.com/mscdex/ssh2)\n\n```js\nimport ssh2 from 'ssh2'\n\nconst sql = postgres({\n  ...options,\n  socket: ({ host: [host], port: [port] }) => new Promise((resolve, reject) => {\n    const ssh = new ssh2.Client()\n    ssh\n    .on('error', reject)\n    .on('ready', () =>\n      ssh.forwardOut('127.0.0.1', 12345, host, port,\n        (err, socket) => err ? reject(err) : resolve(socket)\n      )\n    )\n    .connect(sshOptions)\n  })\n})\n```\n\n## Teardown / Cleanup\n\nTo ensure proper teardown and cleanup on server restarts use `await sql.end()` before `process.exit()`.\n\nCalling `sql.end()` will reject new queries and return a Promise which resolves when all queries are finished and the underlying connections are closed. If a `{ timeout }` option is provided any pending queries will be rejected once the timeout (in seconds) is reached and the connections will be destroyed.\n\n#### Sample shutdown using [Prexit](https://github.com/porsager/prexit)\n\n```js\nimport prexit from 'prexit'\n\nprexit(async () => {\n  await sql.end({ timeout: 5 })\n  await new Promise(r => server.close(r))\n})\n```\n\n## Reserving connections\n\n### `await sql.reserve()`\n\nThe `reserve` method pulls out a connection from the pool, and returns a client that wraps the single connection. This can be used for running queries on an isolated connection.\n\n```ts\nconst reserved = await sql.reserve()\nawait reserved`select * from users`\nawait reserved.release()\n```\n\n### `reserved.release()`\n\nOnce you have finished with the reserved connection, call `release` to add it back to the pool.\n\n## Error handling\n\nErrors are all thrown to related queries and never globally. Errors coming from database itself are always in the [native Postgres format](https://www.postgresql.org/docs/current/errcodes-appendix.html), and the same goes for any [Node.js errors](https://nodejs.org/api/errors.html#errors_common_system_errors) eg. coming from the underlying connection.\n\nQuery errors will contain a stored error with the origin of the query to aid in tracing errors.\n\nQuery errors will also contain the `query` string and the `parameters`. These are not enumerable to avoid accidentally leaking confidential information in logs. To log these it is required to specifically access `error.query` and `error.parameters`, or set `debug: true` in options.\n\nThere are also the following errors specifically for this library.\n\n##### UNSAFE_TRANSACTION\n> Only use sql.begin or max: 1\n\nTo ensure statements in a transaction runs on the same connection (which is required for them to run inside the transaction), you must use [`sql.begin(...)`](#transactions) or only allow a single connection in options (`max: 1`).\n\n##### UNDEFINED_VALUE\n> Undefined values are not allowed\n\nPostgres.js won't accept `undefined` as values in tagged template queries since it becomes ambiguous what to do with the value. If you want to set something to null, use `null` explicitly.\n\n##### MESSAGE_NOT_SUPPORTED\n> X (X) is not supported\n\nWhenever a message is received from Postgres which is not supported by this library. Feel free to file an issue if you think something is missing.\n\n##### MAX_PARAMETERS_EXCEEDED\n> Max number of parameters (65534) exceeded\n\nThe postgres protocol doesn't allow more than 65534 (16bit) parameters. If you run into this issue there are various workarounds such as using `sql([...])` to escape values instead of passing them as parameters.\n\n##### SASL_SIGNATURE_MISMATCH\n> Message type X not supported\n\nWhen using SASL authentication the server responds with a signature at the end of the authentication flow which needs to match the one on the client. This is to avoid [man-in-the-middle attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack). If you receive this error the connection was canceled because the server did not reply with the expected signature.\n\n##### NOT_TAGGED_CALL\n> Query not called as a tagged template literal\n\nMaking queries has to be done using the sql function as a [tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates). This is to ensure parameters are serialized and passed to Postgres as query parameters with correct types and to avoid SQL injection.\n\n##### AUTH_TYPE_NOT_IMPLEMENTED\n> Auth type X not implemented\n\nPostgres supports many different authentication types. This one is not supported.\n\n##### CONNECTION_CLOSED\n> write CONNECTION_CLOSED host:port\n\nThis error is thrown if the connection was closed without an error. This should not happen during normal operations, so please create an issue if this was unexpected.\n\n##### CONNECTION_ENDED\n> write CONNECTION_ENDED host:port\n\nThis error is thrown if the user has called [`sql.end()`](#teardown--cleanup) and performed a query afterward.\n\n##### CONNECTION_DESTROYED\n> write CONNECTION_DESTROYED host:port\n\nThis error is thrown for any queries that were pending when the timeout to [`sql.end({ timeout: X })`](#teardown--cleanup) was reached.\n\n##### CONNECT_TIMEOUT\n> write CONNECT_TIMEOUT host:port\n\nThis error is thrown if the startup phase of the connection (tcp, protocol negotiation, and auth) took more than the default 30 seconds or what was specified using `connect_timeout` or `PGCONNECT_TIMEOUT`.\n\n##### COPY_IN_PROGRESS\n> You cannot execute queries during copy\n\nThis error is thrown if trying to run a query during a copy operation (writable / readable).\n\n## TypeScript support\n\n`postgres` has TypeScript support. You can pass a row list type for your queries in this way:\n```ts\ninterface User {\n  id: number\n  name: string\n}\n\nconst users = await sql<User[]>`SELECT * FROM users`\nusers[0].id // ok => number\nusers[1].name // ok => string\nusers[0].invalid // fails: `invalid` does not exists on `User`\n```\n\nHowever, be sure to check the array length to avoid accessing properties of `undefined` rows:\n```ts\nconst users = await sql<User[]>`SELECT * FROM users WHERE id = ${id}`\nif (!users.length)\n  throw new Error('Not found')\nreturn users[0]\n```\n\nYou can also prefer destructuring when you only care about a fixed number of rows.\nIn this case, we recommend you to prefer using tuples to handle `undefined` properly:\n```ts\nconst [user]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}`\nif (!user) // => User | undefined\n  throw new Error('Not found')\nreturn user // => User\n\n// NOTE:\nconst [first, second]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}` // fails: `second` does not exist on `[User?]`\nconst [first, second] = await sql<[User?]>`SELECT * FROM users WHERE id = ${id}` // don't fail : `second: User | undefined`\n```\n\nWe do our best to type all the public API, however types are not always updated when features are added or changed. Feel free to open an issue if you have trouble with types.\n\n## Migration tools\n\nPostgres.js doesn't come with any migration solution since it's way out of scope, but here are some modules that support Postgres.js for migrations:\n\n- https://github.com/porsager/postgres-shift\n- https://github.com/lukeed/ley\n- https://github.com/JAForbes/pgmg\n\n## Thank you\n\nA really big thank you to [@JAForbes](https://twitter.com/jmsfbs) who introduced me to Postgres and still holds my hand navigating all the great opportunities we have.\n\nThanks to [@ACXgit](https://twitter.com/andreacoiutti) for initial tests and dogfooding.\n\nAlso thanks to [Ryan Dahl](https://github.com/ry) for letting me have the `postgres` npm package name.\n"
  },
  {
    "path": "UNLICENSE",
    "content": "This is free and unencumbered software released into the public domain.\n\nAnyone is free to copy, modify, publish, use, compile, sell, or\ndistribute this software, either in source code form or as a compiled\nbinary, for any purpose, commercial or non-commercial, and by any\nmeans.\n\nIn jurisdictions that recognize copyright laws, the author or authors\nof this software dedicate any and all copyright interest in the\nsoftware to the public domain. We make this dedication for the benefit\nof the public at large and to the detriment of our heirs and\nsuccessors. We intend this dedication to be an overt act of\nrelinquishment in perpetuity of all present and future rights to this\nsoftware under copyright law.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR\nOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\nARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\nFor more information, please refer to <https://unlicense.org/>\n"
  },
  {
    "path": "cf/polyfills.js",
    "content": "import { EventEmitter } from 'node:events'\nimport { Buffer } from 'node:buffer'\n\nconst Crypto = globalThis.crypto\n\nlet ids = 1\nconst tasks = new Set()\n\nconst v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'\nconst v4Str = `(${v4Seg}[.]){3}${v4Seg}`\nconst IPv4Reg = new RegExp(`^${v4Str}$`)\n\nconst v6Seg = '(?:[0-9a-fA-F]{1,4})'\nconst IPv6Reg = new RegExp(\n  '^(' +\n  `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +\n  `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +\n  `(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` +\n  `(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` +\n  `(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` +\n  `(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` +\n  `(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` +\n  `(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +\n  ')(%[0-9a-zA-Z-.:]{1,})?$'\n)\n\nconst textEncoder = new TextEncoder()\nexport const crypto = {\n  randomBytes: l => Crypto.getRandomValues(Buffer.alloc(l)),\n  pbkdf2Sync: async(password, salt, iterations, keylen) =>\n    Crypto.subtle.deriveBits(\n      {\n        name: 'PBKDF2',\n        hash: 'SHA-256',\n        salt,\n        iterations\n      },\n      await Crypto.subtle.importKey(\n        'raw',\n        textEncoder.encode(password),\n        'PBKDF2',\n        false,\n        ['deriveBits']\n      ),\n      keylen * 8,\n      ['deriveBits']\n    ),\n  createHash: type => ({\n    update: x => ({\n      digest: encoding => {\n        if (!(x instanceof Uint8Array)) {\n          x = textEncoder.encode(x)\n        }\n        let prom\n        if (type === 'sha256') {\n          prom = Crypto.subtle.digest('SHA-256', x)\n        } else if (type === 'md5') {\n          prom = Crypto.subtle.digest('md5', x)\n        } else {\n          throw Error('createHash only supports sha256 or md5 in this environment, not ${type}.')\n        }\n        if (encoding === 'hex') {\n          return prom.then((arrayBuf) => Buffer.from(arrayBuf).toString('hex'))\n        } else if (encoding) {\n          throw Error(`createHash only supports hex encoding or unencoded in this environment, not ${encoding}`)\n        } else {\n          return prom\n        }\n      }\n    })\n  }),\n  createHmac: (type, key) => ({\n    update: x => ({\n      digest: async() =>\n        Buffer.from(\n          await Crypto.subtle.sign(\n            'HMAC',\n            await Crypto.subtle.importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']),\n            textEncoder.encode(x)\n          )\n        )\n    })\n  })\n}\n\nexport const performance = globalThis.performance\n\nexport const process = {\n  env: {}\n}\n\nexport const os = {\n  userInfo() {\n    return { username: 'postgres' }\n  }\n}\n\nexport const fs = {\n  readFile() {\n    throw new Error('Reading files not supported on CloudFlare')\n  }\n}\n\nexport const net = {\n  isIP: (x) => IPv4Reg.test(x) ? 4 : IPv6Reg.test(x) ? 6 : 0,\n  Socket\n}\n\nexport { setImmediate, clearImmediate }\n\nexport const tls = {\n  connect({ socket: tcp, servername }) {\n    tcp.writer.releaseLock()\n    tcp.reader.releaseLock()\n    tcp.readyState = 'upgrading'\n    tcp.raw = tcp.raw.startTls({ servername })\n    tcp.raw.closed.then(\n      () => tcp.emit('close'),\n      (e) => tcp.emit('error', e)\n    )\n    tcp.writer = tcp.raw.writable.getWriter()\n    tcp.reader = tcp.raw.readable.getReader()\n\n    tcp.writer.ready.then(() => {\n      tcp.read()\n      tcp.readyState = 'upgrade'\n    })\n    return tcp\n  }\n}\n\nfunction Socket() {\n  const tcp = Object.assign(new EventEmitter(), {\n    readyState: 'open',\n    raw: null,\n    writer: null,\n    reader: null,\n    connect,\n    write,\n    end,\n    destroy,\n    read\n  })\n\n  return tcp\n\n  async function connect(port, host) {\n    try {\n      tcp.readyState = 'opening'\n      const { connect } = await import('cloudflare:sockets')\n      tcp.raw = connect(host + ':' + port, tcp.ssl ? { secureTransport: 'starttls' } : {})\n      tcp.raw.closed.then(\n        () => {\n          tcp.readyState !== 'upgrade'\n            ? close()\n            : ((tcp.readyState = 'open'), tcp.emit('secureConnect'))\n        },\n        (e) => tcp.emit('error', e)\n      )\n      tcp.writer = tcp.raw.writable.getWriter()\n      tcp.reader = tcp.raw.readable.getReader()\n\n      tcp.ssl ? readFirst() : read()\n      tcp.writer.ready.then(() => {\n        tcp.readyState = 'open'\n        tcp.emit('connect')\n      })\n    } catch (err) {\n      error(err)\n    }\n  }\n\n  function close() {\n    if (tcp.readyState === 'closed')\n      return\n\n    tcp.readyState = 'closed'\n    tcp.emit('close')\n  }\n\n  function write(data, cb) {\n    tcp.writer.write(data).then(cb, error)\n    return true\n  }\n\n  function end(data) {\n    return data\n      ? tcp.write(data, () => tcp.raw.close())\n      : tcp.raw.close()\n  }\n\n  function destroy() {\n    tcp.destroyed = true\n    tcp.end()\n  }\n\n  async function read() {\n    try {\n      let done\n        , value\n      while (({ done, value } = await tcp.reader.read(), !done))\n        tcp.emit('data', Buffer.from(value))\n    } catch (err) {\n      error(err)\n    }\n  }\n\n  async function readFirst() {\n    const { value } = await tcp.reader.read()\n    tcp.emit('data', Buffer.from(value))\n  }\n\n  function error(err) {\n    tcp.emit('error', err)\n    tcp.emit('close')\n  }\n}\n\nfunction setImmediate(fn) {\n  const id = ids++\n  tasks.add(id)\n  queueMicrotask(() => {\n    if (tasks.has(id)) {\n      fn()\n      tasks.delete(id)\n    }\n  })\n  return id\n}\n\nfunction clearImmediate(id) {\n  tasks.delete(id)\n}\n"
  },
  {
    "path": "cf/src/bytes.js",
    "content": "import { Buffer } from 'node:buffer'\nconst size = 256\nlet buffer = Buffer.allocUnsafe(size)\n\nconst messages = 'BCcDdEFfHPpQSX'.split('').reduce((acc, x) => {\n  const v = x.charCodeAt(0)\n  acc[x] = () => {\n    buffer[0] = v\n    b.i = 5\n    return b\n  }\n  return acc\n}, {})\n\nconst b = Object.assign(reset, messages, {\n  N: String.fromCharCode(0),\n  i: 0,\n  inc(x) {\n    b.i += x\n    return b\n  },\n  str(x) {\n    const length = Buffer.byteLength(x)\n    fit(length)\n    b.i += buffer.write(x, b.i, length, 'utf8')\n    return b\n  },\n  i16(x) {\n    fit(2)\n    buffer.writeUInt16BE(x, b.i)\n    b.i += 2\n    return b\n  },\n  i32(x, i) {\n    if (i || i === 0) {\n      buffer.writeUInt32BE(x, i)\n      return b\n    }\n    fit(4)\n    buffer.writeUInt32BE(x, b.i)\n    b.i += 4\n    return b\n  },\n  z(x) {\n    fit(x)\n    buffer.fill(0, b.i, b.i + x)\n    b.i += x\n    return b\n  },\n  raw(x) {\n    buffer = Buffer.concat([buffer.subarray(0, b.i), x])\n    b.i = buffer.length\n    return b\n  },\n  end(at = 1) {\n    buffer.writeUInt32BE(b.i - at, at)\n    const out = buffer.subarray(0, b.i)\n    b.i = 0\n    buffer = Buffer.allocUnsafe(size)\n    return out\n  }\n})\n\nexport default b\n\nfunction fit(x) {\n  if (buffer.length - b.i < x) {\n    const prev = buffer\n        , length = prev.length\n\n    buffer = Buffer.allocUnsafe(length + (length >> 1) + x)\n    prev.copy(buffer)\n  }\n}\n\nfunction reset() {\n  b.i = 0\n  return b\n}\n"
  },
  {
    "path": "cf/src/connection.js",
    "content": "import { Buffer } from 'node:buffer'\nimport { setImmediate, clearImmediate } from '../polyfills.js'\nimport { net } from '../polyfills.js'\nimport { tls } from '../polyfills.js'\nimport { crypto } from '../polyfills.js'\nimport Stream from 'node:stream'\nimport { performance } from '../polyfills.js'\n\nimport { stringify, handleValue, arrayParser, arraySerializer } from './types.js'\nimport { Errors } from './errors.js'\nimport Result from './result.js'\nimport Queue from './queue.js'\nimport { Query, CLOSE } from './query.js'\nimport b from './bytes.js'\n\nexport default Connection\n\nlet uid = 1\n\nconst Sync = b().S().end()\n    , Flush = b().H().end()\n    , SSLRequest = b().i32(8).i32(80877103).end(8)\n    , ExecuteUnnamed = Buffer.concat([b().E().str(b.N).i32(0).end(), Sync])\n    , DescribeUnnamed = b().D().str('S').str(b.N).end()\n    , noop = () => { /* noop */ }\n\nconst retryRoutines = new Set([\n  'FetchPreparedStatement',\n  'RevalidateCachedQuery',\n  'transformAssignedExpr'\n])\n\nconst errorFields = {\n  83  : 'severity_local',    // S\n  86  : 'severity',          // V\n  67  : 'code',              // C\n  77  : 'message',           // M\n  68  : 'detail',            // D\n  72  : 'hint',              // H\n  80  : 'position',          // P\n  112 : 'internal_position', // p\n  113 : 'internal_query',    // q\n  87  : 'where',             // W\n  115 : 'schema_name',       // s\n  116 : 'table_name',        // t\n  99  : 'column_name',       // c\n  100 : 'data type_name',    // d\n  110 : 'constraint_name',   // n\n  70  : 'file',              // F\n  76  : 'line',              // L\n  82  : 'routine'            // R\n}\n\nfunction Connection(options, queues = {}, { onopen = noop, onend = noop, onclose = noop } = {}) {\n  const {\n    sslnegotiation,\n    ssl,\n    max,\n    user,\n    host,\n    port,\n    database,\n    parsers,\n    transform,\n    onnotice,\n    onnotify,\n    onparameter,\n    max_pipeline,\n    keep_alive,\n    backoff,\n    target_session_attrs\n  } = options\n\n  const sent = Queue()\n      , id = uid++\n      , backend = { pid: null, secret: null }\n      , idleTimer = timer(end, options.idle_timeout)\n      , lifeTimer = timer(end, options.max_lifetime)\n      , connectTimer = timer(connectTimedOut, options.connect_timeout)\n\n  let socket = null\n    , cancelMessage\n    , errorResponse = null\n    , result = new Result()\n    , incoming = Buffer.alloc(0)\n    , needsTypes = options.fetch_types\n    , backendParameters = {}\n    , statements = {}\n    , statementId = Math.random().toString(36).slice(2)\n    , statementCount = 1\n    , closedTime = 0\n    , remaining = 0\n    , hostIndex = 0\n    , retries = 0\n    , length = 0\n    , delay = 0\n    , rows = 0\n    , serverSignature = null\n    , nextWriteTimer = null\n    , terminated = false\n    , incomings = null\n    , results = null\n    , initial = null\n    , ending = null\n    , stream = null\n    , chunk = null\n    , ended = null\n    , nonce = null\n    , query = null\n    , final = null\n\n  const connection = {\n    queue: queues.closed,\n    idleTimer,\n    connect(query) {\n      initial = query\n      reconnect()\n    },\n    terminate,\n    execute,\n    cancel,\n    end,\n    count: 0,\n    id\n  }\n\n  queues.closed && queues.closed.push(connection)\n\n  return connection\n\n  async function createSocket() {\n    let x\n    try {\n      x = options.socket\n        ? (await Promise.resolve(options.socket(options)))\n        : new net.Socket()\n    } catch (e) {\n      error(e)\n      return\n    }\n    x.on('error', error)\n    x.on('close', closed)\n    x.on('drain', drain)\n    return x\n  }\n\n  async function cancel({ pid, secret }, resolve, reject) {\n    try {\n      cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)\n      await connect()\n      socket.once('error', reject)\n      socket.once('close', resolve)\n    } catch (error) {\n      reject(error)\n    }\n  }\n\n  function execute(q) {\n    if (terminated)\n      return queryError(q, Errors.connection('CONNECTION_DESTROYED', options))\n\n    if (stream)\n      return queryError(q, Errors.generic('COPY_IN_PROGRESS', 'You cannot execute queries during copy'))\n\n    if (q.cancelled)\n      return\n\n    try {\n      q.state = backend\n      query\n        ? sent.push(q)\n        : (query = q, query.active = true)\n\n      build(q)\n      return write(toBuffer(q))\n        && !q.describeFirst\n        && !q.cursorFn\n        && sent.length < max_pipeline\n        && (!q.options.onexecute || q.options.onexecute(connection))\n    } catch (error) {\n      sent.length === 0 && write(Sync)\n      errored(error)\n      return true\n    }\n  }\n\n  function toBuffer(q) {\n    if (q.parameters.length >= 65534)\n      throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')\n\n    return q.options.simple\n      ? b().Q().str(q.statement.string + b.N).end()\n      : q.describeFirst\n        ? Buffer.concat([describe(q), Flush])\n        : q.prepare\n          ? q.prepared\n            ? prepared(q)\n            : Buffer.concat([describe(q), prepared(q)])\n          : unnamed(q)\n  }\n\n  function describe(q) {\n    return Buffer.concat([\n      Parse(q.statement.string, q.parameters, q.statement.types, q.statement.name),\n      Describe('S', q.statement.name)\n    ])\n  }\n\n  function prepared(q) {\n    return Buffer.concat([\n      Bind(q.parameters, q.statement.types, q.statement.name, q.cursorName),\n      q.cursorFn\n        ? Execute('', q.cursorRows)\n        : ExecuteUnnamed\n    ])\n  }\n\n  function unnamed(q) {\n    return Buffer.concat([\n      Parse(q.statement.string, q.parameters, q.statement.types),\n      DescribeUnnamed,\n      prepared(q)\n    ])\n  }\n\n  function build(q) {\n    const parameters = []\n        , types = []\n\n    const string = stringify(q, q.strings[0], q.args[0], parameters, types, options)\n\n    !q.tagged && q.args.forEach(x => handleValue(x, parameters, types, options))\n\n    q.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)\n    q.string = string\n    q.signature = q.prepare && types + string\n    q.onlyDescribe && (delete statements[q.signature])\n    q.parameters = q.parameters || parameters\n    q.prepared = q.prepare && q.signature in statements\n    q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared)\n    q.statement = q.prepared\n      ? statements[q.signature]\n      : { string, types, name: q.prepare ? statementId + statementCount++ : '' }\n\n    typeof options.debug === 'function' && options.debug(id, string, parameters, types)\n  }\n\n  function write(x, fn) {\n    chunk = chunk ? Buffer.concat([chunk, x]) : Buffer.from(x)\n    if (fn || chunk.length >= 1024)\n      return nextWrite(fn)\n    nextWriteTimer === null && (nextWriteTimer = setImmediate(nextWrite))\n    return true\n  }\n\n  function nextWrite(fn) {\n    const x = socket.write(chunk, fn)\n    nextWriteTimer !== null && clearImmediate(nextWriteTimer)\n    chunk = nextWriteTimer = null\n    return x\n  }\n\n  function connectTimedOut() {\n    errored(Errors.connection('CONNECT_TIMEOUT', options, socket))\n    socket.destroy()\n  }\n\n  async function secure() {\n    if (sslnegotiation !== 'direct') {\n      write(SSLRequest)\n      const canSSL = await new Promise(r => socket.once('data', x => r(x[0] === 83))) // S\n\n      if (!canSSL && ssl === 'prefer')\n        return connected()\n    }\n\n    const options = {\n      socket,\n      servername: net.isIP(socket.host) ? undefined : socket.host\n    }\n\n    if (sslnegotiation === 'direct')\n      options.ALPNProtocols = ['postgresql']\n\n    if (ssl === 'require' || ssl === 'allow' || ssl === 'prefer')\n      options.rejectUnauthorized = false\n    else if (typeof ssl === 'object')\n      Object.assign(options, ssl)\n\n    socket.removeAllListeners()\n    socket = tls.connect(options)\n    socket.on('secureConnect', connected)\n    socket.on('error', error)\n    socket.on('close', closed)\n    socket.on('drain', drain)\n  }\n\n  /* c8 ignore next 3 */\n  function drain() {\n    !query && onopen(connection)\n  }\n\n  function data(x) {\n    if (incomings) {\n      incomings.push(x)\n      remaining -= x.length\n      if (remaining > 0)\n        return\n    }\n\n    incoming = incomings\n      ? Buffer.concat(incomings, length - remaining)\n      : incoming.length === 0\n        ? x\n        : Buffer.concat([incoming, x], incoming.length + x.length)\n\n    while (incoming.length > 4) {\n      length = incoming.readUInt32BE(1)\n      if (length >= incoming.length) {\n        remaining = length - incoming.length\n        incomings = [incoming]\n        break\n      }\n\n      try {\n        handle(incoming.subarray(0, length + 1))\n      } catch (e) {\n        query && (query.cursorFn || query.describeFirst) && write(Sync)\n        errored(e)\n      }\n      incoming = incoming.subarray(length + 1)\n      remaining = 0\n      incomings = null\n    }\n  }\n\n  async function connect() {\n    terminated = false\n    backendParameters = {}\n    socket || (socket = await createSocket())\n\n    if (!socket)\n      return\n\n    connectTimer.start()\n\n    if (options.socket)\n      return ssl ? secure() : connected()\n\n    socket.on('connect', ssl ? secure : connected)\n\n    if (options.path)\n      return socket.connect(options.path)\n\n    socket.ssl = ssl\n    socket.connect(port[hostIndex], host[hostIndex])\n    socket.host = host[hostIndex]\n    socket.port = port[hostIndex]\n\n    hostIndex = (hostIndex + 1) % port.length\n  }\n\n  function reconnect() {\n    setTimeout(connect, closedTime ? Math.max(0, closedTime + delay - performance.now()) : 0)\n  }\n\n  function connected() {\n    try {\n      statements = {}\n      needsTypes = options.fetch_types\n      statementId = Math.random().toString(36).slice(2)\n      statementCount = 1\n      lifeTimer.start()\n      socket.on('data', data)\n      keep_alive && socket.setKeepAlive && socket.setKeepAlive(true, 1000 * keep_alive)\n      const s = StartupMessage()\n      write(s)\n    } catch (err) {\n      error(err)\n    }\n  }\n\n  function error(err) {\n    if (connection.queue === queues.connecting && options.host[retries + 1])\n      return\n\n    errored(err)\n    while (sent.length)\n      queryError(sent.shift(), err)\n  }\n\n  function errored(err) {\n    stream && (stream.destroy(err), stream = null)\n    query && queryError(query, err)\n    initial && (queryError(initial, err), initial = null)\n  }\n\n  function queryError(query, err) {\n    if (query.reserve)\n      return query.reject(err)\n\n    if (!err || typeof err !== 'object')\n      err = new Error(err)\n\n    'query' in err || 'parameters' in err || Object.defineProperties(err, {\n      stack: { value: err.stack + query.origin.replace(/.*\\n/, '\\n'), enumerable: options.debug },\n      query: { value: query.string, enumerable: options.debug },\n      parameters: { value: query.parameters, enumerable: options.debug },\n      args: { value: query.args, enumerable: options.debug },\n      types: { value: query.statement && query.statement.types, enumerable: options.debug }\n    })\n    query.reject(err)\n  }\n\n  function end() {\n    return ending || (\n      !connection.reserved && onend(connection),\n      !connection.reserved && !initial && !query && sent.length === 0\n        ? (terminate(), new Promise(r => socket && socket.readyState !== 'closed' ? socket.once('close', r) : r()))\n        : ending = new Promise(r => ended = r)\n    )\n  }\n\n  function terminate() {\n    terminated = true\n    if (stream || query || initial || sent.length)\n      error(Errors.connection('CONNECTION_DESTROYED', options))\n\n    clearImmediate(nextWriteTimer)\n    if (socket) {\n      socket.removeListener('data', data)\n      socket.removeListener('connect', connected)\n      socket.readyState === 'open' && socket.end(b().X().end())\n    }\n    ended && (ended(), ending = ended = null)\n  }\n\n  async function closed(hadError) {\n    incoming = Buffer.alloc(0)\n    remaining = 0\n    incomings = null\n    clearImmediate(nextWriteTimer)\n    socket.removeListener('data', data)\n    socket.removeListener('connect', connected)\n    idleTimer.cancel()\n    lifeTimer.cancel()\n    connectTimer.cancel()\n\n    socket.removeAllListeners()\n    socket = null\n\n    if (initial)\n      return reconnect()\n\n    !hadError && (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket))\n    closedTime = performance.now()\n    hadError && options.shared.retries++\n    delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000\n    onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket))\n  }\n\n  /* Handlers */\n  function handle(xs, x = xs[0]) {\n    (\n      x === 68 ? DataRow :                   // D\n      x === 100 ? CopyData :                 // d\n      x === 65 ? NotificationResponse :      // A\n      x === 83 ? ParameterStatus :           // S\n      x === 90 ? ReadyForQuery :             // Z\n      x === 67 ? CommandComplete :           // C\n      x === 50 ? BindComplete :              // 2\n      x === 49 ? ParseComplete :             // 1\n      x === 116 ? ParameterDescription :     // t\n      x === 84 ? RowDescription :            // T\n      x === 82 ? Authentication :            // R\n      x === 110 ? NoData :                   // n\n      x === 75 ? BackendKeyData :            // K\n      x === 69 ? ErrorResponse :             // E\n      x === 115 ? PortalSuspended :          // s\n      x === 51 ? CloseComplete :             // 3\n      x === 71 ? CopyInResponse :            // G\n      x === 78 ? NoticeResponse :            // N\n      x === 72 ? CopyOutResponse :           // H\n      x === 99 ? CopyDone :                  // c\n      x === 73 ? EmptyQueryResponse :        // I\n      x === 86 ? FunctionCallResponse :      // V\n      x === 118 ? NegotiateProtocolVersion : // v\n      x === 87 ? CopyBothResponse :          // W\n      /* c8 ignore next */\n      UnknownMessage\n    )(xs)\n  }\n\n  function DataRow(x) {\n    let index = 7\n    let length\n    let column\n    let value\n\n    const row = query.isRaw ? new Array(query.statement.columns.length) : {}\n    for (let i = 0; i < query.statement.columns.length; i++) {\n      column = query.statement.columns[i]\n      length = x.readInt32BE(index)\n      index += 4\n\n      value = length === -1\n        ? null\n        : query.isRaw === true\n          ? x.subarray(index, index += length)\n          : column.parser === undefined\n            ? x.toString('utf8', index, index += length)\n            : column.parser.array === true\n              ? column.parser(x.toString('utf8', index + 1, index += length))\n              : column.parser(x.toString('utf8', index, index += length))\n\n      query.isRaw\n        ? (row[i] = query.isRaw === true\n          ? value\n          : transform.value.from ? transform.value.from(value, column) : value)\n        : (row[column.name] = transform.value.from ? transform.value.from(value, column) : value)\n    }\n\n    query.forEachFn\n      ? query.forEachFn(transform.row.from ? transform.row.from(row) : row, result)\n      : (result[rows++] = transform.row.from ? transform.row.from(row) : row)\n  }\n\n  function ParameterStatus(x) {\n    const [k, v] = x.toString('utf8', 5, x.length - 1).split(b.N)\n    backendParameters[k] = v\n    if (options.parameters[k] !== v) {\n      options.parameters[k] = v\n      onparameter && onparameter(k, v)\n    }\n  }\n\n  function ReadyForQuery(x) {\n    if (query) {\n      if (errorResponse) {\n        query.retried\n          ? errored(query.retried)\n          : query.prepared && retryRoutines.has(errorResponse.routine)\n            ? retry(query, errorResponse)\n            : errored(errorResponse)\n      } else {\n        query.resolve(results || result)\n      }\n    } else if (errorResponse) {\n      errored(errorResponse)\n    }\n\n    query = results = errorResponse = null\n    result = new Result()\n    connectTimer.cancel()\n\n    if (initial) {\n      if (target_session_attrs) {\n        if (!backendParameters.in_hot_standby || !backendParameters.default_transaction_read_only)\n          return fetchState()\n        else if (tryNext(target_session_attrs, backendParameters))\n          return terminate()\n      }\n\n      if (needsTypes) {\n        initial.reserve && (initial = null)\n        return fetchArrayTypes()\n      }\n\n      initial && !initial.reserve && execute(initial)\n      options.shared.retries = retries = 0\n      initial = null\n      return\n    }\n\n    while (sent.length && (query = sent.shift()) && (query.active = true, query.cancelled))\n      Connection(options).cancel(query.state, query.cancelled.resolve, query.cancelled.reject)\n\n    if (query)\n      return // Consider opening if able and sent.length < 50\n\n    connection.reserved\n      ? !connection.reserved.release && x[5] === 73 // I\n        ? ending\n          ? terminate()\n          : (connection.reserved = null, onopen(connection))\n        : connection.reserved()\n      : ending\n        ? terminate()\n        : onopen(connection)\n  }\n\n  function CommandComplete(x) {\n    rows = 0\n\n    for (let i = x.length - 1; i > 0; i--) {\n      if (x[i] === 32 && x[i + 1] < 58 && result.count === null)\n        result.count = +x.toString('utf8', i + 1, x.length - 1)\n      if (x[i - 1] >= 65) {\n        result.command = x.toString('utf8', 5, i)\n        result.state = backend\n        break\n      }\n    }\n\n    final && (final(), final = null)\n\n    if (result.command === 'BEGIN' && max !== 1 && !connection.reserved)\n      return errored(Errors.generic('UNSAFE_TRANSACTION', 'Only use sql.begin, sql.reserved or max: 1'))\n\n    if (query.options.simple)\n      return BindComplete()\n\n    if (query.cursorFn) {\n      result.count && query.cursorFn(result)\n      write(Sync)\n    }\n  }\n\n  function ParseComplete() {\n    query.parsing = false\n  }\n\n  function BindComplete() {\n    !result.statement && (result.statement = query.statement)\n    result.columns = query.statement.columns\n  }\n\n  function ParameterDescription(x) {\n    const length = x.readUInt16BE(5)\n\n    for (let i = 0; i < length; ++i)\n      !query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))\n\n    query.prepare && (statements[query.signature] = query.statement)\n    query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false)\n  }\n\n  function RowDescription(x) {\n    if (result.command) {\n      results = results || [result]\n      results.push(result = new Result())\n      result.count = null\n      query.statement.columns = null\n    }\n\n    const length = x.readUInt16BE(5)\n    let index = 7\n    let start\n\n    query.statement.columns = Array(length)\n\n    for (let i = 0; i < length; ++i) {\n      start = index\n      while (x[index++] !== 0);\n      const table = x.readUInt32BE(index)\n      const number = x.readUInt16BE(index + 4)\n      const type = x.readUInt32BE(index + 6)\n      query.statement.columns[i] = {\n        name: transform.column.from\n          ? transform.column.from(x.toString('utf8', start, index - 1))\n          : x.toString('utf8', start, index - 1),\n        parser: parsers[type],\n        table,\n        number,\n        type\n      }\n      index += 18\n    }\n\n    result.statement = query.statement\n    if (query.onlyDescribe)\n      return (query.resolve(query.statement), write(Sync))\n  }\n\n  async function Authentication(x, type = x.readUInt32BE(5)) {\n    (\n      type === 3 ? AuthenticationCleartextPassword :\n      type === 5 ? AuthenticationMD5Password :\n      type === 10 ? SASL :\n      type === 11 ? SASLContinue :\n      type === 12 ? SASLFinal :\n      type !== 0 ? UnknownAuth :\n      noop\n    )(x, type)\n  }\n\n  /* c8 ignore next 5 */\n  async function AuthenticationCleartextPassword() {\n    const payload = await Pass()\n    write(\n      b().p().str(payload).z(1).end()\n    )\n  }\n\n  async function AuthenticationMD5Password(x) {\n    const payload = 'md5' + (\n      await md5(\n        Buffer.concat([\n          Buffer.from(await md5((await Pass()) + user)),\n          x.subarray(9)\n        ])\n      )\n    )\n    write(\n      b().p().str(payload).z(1).end()\n    )\n  }\n\n  async function SASL() {\n    nonce = (await crypto.randomBytes(18)).toString('base64')\n    b().p().str('SCRAM-SHA-256' + b.N)\n    const i = b.i\n    write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end())\n  }\n\n  async function SASLContinue(x) {\n    const res = x.toString('utf8', 9).split(',').reduce((acc, x) => (acc[x[0]] = x.slice(2), acc), {})\n\n    const saltedPassword = await crypto.pbkdf2Sync(\n      await Pass(),\n      Buffer.from(res.s, 'base64'),\n      parseInt(res.i), 32,\n      'sha256'\n    )\n\n    const clientKey = await hmac(saltedPassword, 'Client Key')\n\n    const auth = 'n=*,r=' + nonce + ','\n               + 'r=' + res.r + ',s=' + res.s + ',i=' + res.i\n               + ',c=biws,r=' + res.r\n\n    serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64')\n\n    const payload = 'c=biws,r=' + res.r + ',p=' + xor(\n      clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))\n    ).toString('base64')\n\n    write(\n      b().p().str(payload).end()\n    )\n  }\n\n  function SASLFinal(x) {\n    if (x.toString('utf8', 9).split(b.N, 1)[0].slice(2) === serverSignature)\n      return\n    /* c8 ignore next 5 */\n    errored(Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server did not return the correct signature'))\n    socket.destroy()\n  }\n\n  function Pass() {\n    return Promise.resolve(typeof options.pass === 'function'\n      ? options.pass()\n      : options.pass\n    )\n  }\n\n  function NoData() {\n    result.statement = query.statement\n    result.statement.columns = []\n    if (query.onlyDescribe)\n      return (query.resolve(query.statement), write(Sync))\n  }\n\n  function BackendKeyData(x) {\n    backend.pid = x.readUInt32BE(5)\n    backend.secret = x.readUInt32BE(9)\n  }\n\n  async function fetchArrayTypes() {\n    needsTypes = false\n    const types = await new Query([`\n      select b.oid, b.typarray\n      from pg_catalog.pg_type a\n      left join pg_catalog.pg_type b on b.oid = a.typelem\n      where a.typcategory = 'A'\n      group by b.oid, b.typarray\n      order by b.oid\n    `], [], execute)\n    types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))\n  }\n\n  function addArrayType(oid, typarray) {\n    if (!!options.parsers[typarray] && !!options.serializers[typarray]) return\n    const parser = options.parsers[oid]\n    options.shared.typeArrayMap[oid] = typarray\n    options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)\n    options.parsers[typarray].array = true\n    options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)\n  }\n\n  function tryNext(x, xs) {\n    return (\n      (x === 'read-write' && xs.default_transaction_read_only === 'on') ||\n      (x === 'read-only' && xs.default_transaction_read_only === 'off') ||\n      (x === 'primary' && xs.in_hot_standby === 'on') ||\n      (x === 'standby' && xs.in_hot_standby === 'off') ||\n      (x === 'prefer-standby' && xs.in_hot_standby === 'off' && options.host[retries])\n    )\n  }\n\n  function fetchState() {\n    const query = new Query([`\n      show transaction_read_only;\n      select pg_catalog.pg_is_in_recovery()\n    `], [], execute, null, { simple: true })\n    query.resolve = ([[a], [b]]) => {\n      backendParameters.default_transaction_read_only = a.transaction_read_only\n      backendParameters.in_hot_standby = b.pg_is_in_recovery ? 'on' : 'off'\n    }\n    query.execute()\n  }\n\n  function ErrorResponse(x) {\n    if (query) {\n      (query.cursorFn || query.describeFirst) && write(Sync)\n      errorResponse = Errors.postgres(parseError(x))\n    } else {\n      errored(Errors.postgres(parseError(x)))\n    }\n  }\n\n  function retry(q, error) {\n    delete statements[q.signature]\n    q.retried = error\n    execute(q)\n  }\n\n  function NotificationResponse(x) {\n    if (!onnotify)\n      return\n\n    let index = 9\n    while (x[index++] !== 0);\n    onnotify(\n      x.toString('utf8', 9, index - 1),\n      x.toString('utf8', index, x.length - 1)\n    )\n  }\n\n  async function PortalSuspended() {\n    try {\n      const x = await Promise.resolve(query.cursorFn(result))\n      rows = 0\n      x === CLOSE\n        ? write(Close(query.portal))\n        : (result = new Result(), write(Execute('', query.cursorRows)))\n    } catch (err) {\n      write(Sync)\n      query.reject(err)\n    }\n  }\n\n  function CloseComplete() {\n    result.count && query.cursorFn(result)\n    query.resolve(result)\n  }\n\n  function CopyInResponse() {\n    stream = new Stream.Writable({\n      autoDestroy: true,\n      write(chunk, encoding, callback) {\n        socket.write(b().d().raw(chunk).end(), callback)\n      },\n      destroy(error, callback) {\n        callback(error)\n        socket.write(b().f().str(error + b.N).end())\n        stream = null\n      },\n      final(callback) {\n        socket.write(b().c().end())\n        final = callback\n        stream = null\n      }\n    })\n    query.resolve(stream)\n  }\n\n  function CopyOutResponse() {\n    stream = new Stream.Readable({\n      read() { socket.resume() }\n    })\n    query.resolve(stream)\n  }\n\n  /* c8 ignore next 3 */\n  function CopyBothResponse() {\n    stream = new Stream.Duplex({\n      autoDestroy: true,\n      read() { socket.resume() },\n      /* c8 ignore next 11 */\n      write(chunk, encoding, callback) {\n        socket.write(b().d().raw(chunk).end(), callback)\n      },\n      destroy(error, callback) {\n        callback(error)\n        socket.write(b().f().str(error + b.N).end())\n        stream = null\n      },\n      final(callback) {\n        socket.write(b().c().end())\n        final = callback\n      }\n    })\n    query.resolve(stream)\n  }\n\n  function CopyData(x) {\n    stream && (stream.push(x.subarray(5)) || socket.pause())\n  }\n\n  function CopyDone() {\n    stream && stream.push(null)\n    stream = null\n  }\n\n  function NoticeResponse(x) {\n    onnotice\n      ? onnotice(parseError(x))\n      : console.log(parseError(x)) // eslint-disable-line\n\n  }\n\n  /* c8 ignore next 3 */\n  function EmptyQueryResponse() {\n    /* noop */\n  }\n\n  /* c8 ignore next 3 */\n  function FunctionCallResponse() {\n    errored(Errors.notSupported('FunctionCallResponse'))\n  }\n\n  /* c8 ignore next 3 */\n  function NegotiateProtocolVersion() {\n    errored(Errors.notSupported('NegotiateProtocolVersion'))\n  }\n\n  /* c8 ignore next 3 */\n  function UnknownMessage(x) {\n    console.error('Postgres.js : Unknown Message:', x[0]) // eslint-disable-line\n  }\n\n  /* c8 ignore next 3 */\n  function UnknownAuth(x, type) {\n    console.error('Postgres.js : Unknown Auth:', type) // eslint-disable-line\n  }\n\n  /* Messages */\n  function Bind(parameters, types, statement = '', portal = '') {\n    let prev\n      , type\n\n    b().B().str(portal + b.N).str(statement + b.N).i16(0).i16(parameters.length)\n\n    parameters.forEach((x, i) => {\n      if (x === null)\n        return b.i32(0xFFFFFFFF)\n\n      type = types[i]\n      parameters[i] = x = type in options.serializers\n        ? options.serializers[type](x)\n        : '' + x\n\n      prev = b.i\n      b.inc(4).str(x).i32(b.i - prev - 4, prev)\n    })\n\n    b.i16(0)\n\n    return b.end()\n  }\n\n  function Parse(str, parameters, types, name = '') {\n    b().P().str(name + b.N).str(str + b.N).i16(parameters.length)\n    parameters.forEach((x, i) => b.i32(types[i] || 0))\n    return b.end()\n  }\n\n  function Describe(x, name = '') {\n    return b().D().str(x).str(name + b.N).end()\n  }\n\n  function Execute(portal = '', rows = 0) {\n    return Buffer.concat([\n      b().E().str(portal + b.N).i32(rows).end(),\n      Flush\n    ])\n  }\n\n  function Close(portal = '') {\n    return Buffer.concat([\n      b().C().str('P').str(portal + b.N).end(),\n      b().S().end()\n    ])\n  }\n\n  function StartupMessage() {\n    return cancelMessage || b().inc(4).i16(3).z(2).str(\n      Object.entries(Object.assign({\n        user,\n        database,\n        client_encoding: 'UTF8'\n      },\n        options.connection\n      )).filter(([, v]) => v).map(([k, v]) => k + b.N + v).join(b.N)\n    ).z(2).end(0)\n  }\n\n}\n\nfunction parseError(x) {\n  const error = {}\n  let start = 5\n  for (let i = 5; i < x.length - 1; i++) {\n    if (x[i] === 0) {\n      error[errorFields[x[start]]] = x.toString('utf8', start + 1, i)\n      start = i + 1\n    }\n  }\n  return error\n}\n\nfunction md5(x) {\n  return crypto.createHash('md5').update(x).digest('hex')\n}\n\nfunction hmac(key, x) {\n  return crypto.createHmac('sha256', key).update(x).digest()\n}\n\nfunction sha256(x) {\n  return crypto.createHash('sha256').update(x).digest()\n}\n\nfunction xor(a, b) {\n  const length = Math.max(a.length, b.length)\n  const buffer = Buffer.allocUnsafe(length)\n  for (let i = 0; i < length; i++)\n    buffer[i] = a[i] ^ b[i]\n  return buffer\n}\n\nfunction timer(fn, seconds) {\n  seconds = typeof seconds === 'function' ? seconds() : seconds\n  if (!seconds)\n    return { cancel: noop, start: noop }\n\n  let timer\n  return {\n    cancel() {\n      timer && (clearTimeout(timer), timer = null)\n    },\n    start() {\n      timer && clearTimeout(timer)\n      timer = setTimeout(done, seconds * 1000, arguments)\n    }\n  }\n\n  function done(args) {\n    fn.apply(null, args)\n    timer = null\n  }\n}\n"
  },
  {
    "path": "cf/src/errors.js",
    "content": "export class PostgresError extends Error {\n  constructor(x) {\n    super(x.message)\n    this.name = this.constructor.name\n    Object.assign(this, x)\n  }\n}\n\nexport const Errors = {\n  connection,\n  postgres,\n  generic,\n  notSupported\n}\n\nfunction connection(x, options, socket) {\n  const { host, port } = socket || options\n  const error = Object.assign(\n    new Error(('write ' + x + ' ' + (options.path || (host + ':' + port)))),\n    {\n      code: x,\n      errno: x,\n      address: options.path || host\n    }, options.path ? {} : { port: port }\n  )\n  Error.captureStackTrace(error, connection)\n  return error\n}\n\nfunction postgres(x) {\n  const error = new PostgresError(x)\n  Error.captureStackTrace(error, postgres)\n  return error\n}\n\nfunction generic(code, message) {\n  const error = Object.assign(new Error(code + ': ' + message), { code })\n  Error.captureStackTrace(error, generic)\n  return error\n}\n\n/* c8 ignore next 10 */\nfunction notSupported(x) {\n  const error = Object.assign(\n    new Error(x + ' (B) is not supported'),\n    {\n      code: 'MESSAGE_NOT_SUPPORTED',\n      name: x\n    }\n  )\n  Error.captureStackTrace(error, notSupported)\n  return error\n}\n"
  },
  {
    "path": "cf/src/index.js",
    "content": "import { process } from '../polyfills.js'\nimport { os } from '../polyfills.js'\nimport { fs } from '../polyfills.js'\n\nimport {\n  mergeUserTypes,\n  inferType,\n  Parameter,\n  Identifier,\n  Builder,\n  toPascal,\n  pascal,\n  toCamel,\n  camel,\n  toKebab,\n  kebab,\n  fromPascal,\n  fromCamel,\n  fromKebab\n} from './types.js'\n\nimport Connection from './connection.js'\nimport { Query, CLOSE } from './query.js'\nimport Queue from './queue.js'\nimport { Errors, PostgresError } from './errors.js'\nimport Subscribe from './subscribe.js'\nimport largeObject from './large.js'\n\nObject.assign(Postgres, {\n  PostgresError,\n  toPascal,\n  pascal,\n  toCamel,\n  camel,\n  toKebab,\n  kebab,\n  fromPascal,\n  fromCamel,\n  fromKebab,\n  BigInt: {\n    to: 20,\n    from: [20],\n    parse: x => BigInt(x), // eslint-disable-line\n    serialize: x => x.toString()\n  }\n})\n\nexport default Postgres\n\nfunction Postgres(a, b) {\n  const options = parseOptions(a, b)\n      , subscribe = options.no_subscribe || Subscribe(Postgres, { ...options })\n\n  let ending = false\n\n  const queries = Queue()\n      , connecting = Queue()\n      , reserved = Queue()\n      , closed = Queue()\n      , ended = Queue()\n      , open = Queue()\n      , busy = Queue()\n      , full = Queue()\n      , queues = { connecting, reserved, closed, ended, open, busy, full }\n\n  const connections = [...Array(options.max)].map(() => Connection(options, queues, { onopen, onend, onclose }))\n\n  const sql = Sql(handler)\n\n  Object.assign(sql, {\n    get parameters() { return options.parameters },\n    largeObject: largeObject.bind(null, sql),\n    subscribe,\n    CLOSE,\n    END: CLOSE,\n    PostgresError,\n    options,\n    reserve,\n    listen,\n    begin,\n    close,\n    end\n  })\n\n  return sql\n\n  function Sql(handler) {\n    handler.debug = options.debug\n\n    Object.entries(options.types).reduce((acc, [name, type]) => {\n      acc[name] = (x) => new Parameter(x, type.to)\n      return acc\n    }, typed)\n\n    Object.assign(sql, {\n      types: typed,\n      typed,\n      unsafe,\n      notify,\n      array,\n      json,\n      file\n    })\n\n    return sql\n\n    function typed(value, type) {\n      return new Parameter(value, type)\n    }\n\n    function sql(strings, ...args) {\n      const query = strings && Array.isArray(strings.raw)\n        ? new Query(strings, args, handler, cancel)\n        : typeof strings === 'string' && !args.length\n          ? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings)\n          : new Builder(strings, args)\n      return query\n    }\n\n    function unsafe(string, args = [], options = {}) {\n      arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])\n      const query = new Query([string], args, handler, cancel, {\n        prepare: false,\n        ...options,\n        simple: 'simple' in options ? options.simple : args.length === 0\n      })\n      return query\n    }\n\n    function file(path, args = [], options = {}) {\n      arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])\n      const query = new Query([], args, (query) => {\n        fs.readFile(path, 'utf8', (err, string) => {\n          if (err)\n            return query.reject(err)\n\n          query.strings = [string]\n          handler(query)\n        })\n      }, cancel, {\n        ...options,\n        simple: 'simple' in options ? options.simple : args.length === 0\n      })\n      return query\n    }\n  }\n\n  async function listen(name, fn, onlisten) {\n    const listener = { fn, onlisten }\n\n    const sql = listen.sql || (listen.sql = Postgres({\n      ...options,\n      max: 1,\n      idle_timeout: null,\n      max_lifetime: null,\n      fetch_types: false,\n      onclose() {\n        Object.entries(listen.channels).forEach(([name, { listeners }]) => {\n          delete listen.channels[name]\n          Promise.all(listeners.map(l => listen(name, l.fn, l.onlisten).catch(() => { /* noop */ })))\n        })\n      },\n      onnotify(c, x) {\n        c in listen.channels && listen.channels[c].listeners.forEach(l => l.fn(x))\n      }\n    }))\n\n    const channels = listen.channels || (listen.channels = {})\n        , exists = name in channels\n\n    if (exists) {\n      channels[name].listeners.push(listener)\n      const result = await channels[name].result\n      listener.onlisten && listener.onlisten()\n      return { state: result.state, unlisten }\n    }\n\n    channels[name] = { result: sql`listen ${\n      sql.unsafe('\"' + name.replace(/\"/g, '\"\"') + '\"')\n    }`, listeners: [listener] }\n    const result = await channels[name].result\n    listener.onlisten && listener.onlisten()\n    return { state: result.state, unlisten }\n\n    async function unlisten() {\n      if (name in channels === false)\n        return\n\n      channels[name].listeners = channels[name].listeners.filter(x => x !== listener)\n      if (channels[name].listeners.length)\n        return\n\n      delete channels[name]\n      return sql`unlisten ${\n        sql.unsafe('\"' + name.replace(/\"/g, '\"\"') + '\"')\n      }`\n    }\n  }\n\n  async function notify(channel, payload) {\n    return await sql`select pg_notify(${ channel }, ${ '' + payload })`\n  }\n\n  async function reserve() {\n    const queue = Queue()\n    const c = open.length\n      ? open.shift()\n      : await new Promise((resolve, reject) => {\n        const query = { reserve: resolve, reject }\n        queries.push(query)\n        closed.length && connect(closed.shift(), query)\n      })\n\n    move(c, reserved)\n    c.reserved = () => queue.length\n      ? c.execute(queue.shift())\n      : move(c, reserved)\n    c.reserved.release = true\n\n    const sql = Sql(handler)\n    sql.release = () => {\n      c.reserved = null\n      onopen(c)\n    }\n\n    return sql\n\n    function handler(q) {\n      c.queue === full\n        ? queue.push(q)\n        : c.execute(q) || move(c, full)\n    }\n  }\n\n  async function begin(options, fn) {\n    !fn && (fn = options, options = '')\n    const queries = Queue()\n    let savepoints = 0\n      , connection\n      , prepare = null\n\n    try {\n      await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()\n      return await Promise.race([\n        scope(connection, fn),\n        new Promise((_, reject) => connection.onclose = reject)\n      ])\n    } catch (error) {\n      throw error\n    }\n\n    async function scope(c, fn, name) {\n      const sql = Sql(handler)\n      sql.savepoint = savepoint\n      sql.prepare = x => prepare = x.replace(/[^a-z0-9$-_. ]/gi)\n      let uncaughtError\n        , result\n\n      name && await sql`savepoint ${ sql(name) }`\n      try {\n        result = await new Promise((resolve, reject) => {\n          const x = fn(sql)\n          Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)\n        })\n\n        if (uncaughtError)\n          throw uncaughtError\n      } catch (e) {\n        await (name\n          ? sql`rollback to ${ sql(name) }`\n          : sql`rollback`\n        )\n        throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e\n      }\n\n      if (!name) {\n        prepare\n          ? await sql`prepare transaction '${ sql.unsafe(prepare) }'`\n          : await sql`commit`\n      }\n\n      return result\n\n      function savepoint(name, fn) {\n        if (name && Array.isArray(name.raw))\n          return savepoint(sql => sql.apply(sql, arguments))\n\n        arguments.length === 1 && (fn = name, name = null)\n        return scope(c, fn, 's' + savepoints++ + (name ? '_' + name : ''))\n      }\n\n      function handler(q) {\n        q.catch(e => uncaughtError || (uncaughtError = e))\n        c.queue === full\n          ? queries.push(q)\n          : c.execute(q) || move(c, full)\n      }\n    }\n\n    function onexecute(c) {\n      connection = c\n      move(c, reserved)\n      c.reserved = () => queries.length\n        ? c.execute(queries.shift())\n        : move(c, reserved)\n    }\n  }\n\n  function move(c, queue) {\n    c.queue.remove(c)\n    queue.push(c)\n    c.queue = queue\n    queue === open\n      ? c.idleTimer.start()\n      : c.idleTimer.cancel()\n    return c\n  }\n\n  function json(x) {\n    return new Parameter(x, 3802)\n  }\n\n  function array(x, type) {\n    if (!Array.isArray(x))\n      return array(Array.from(arguments))\n\n    return new Parameter(x, type || (x.length ? inferType(x) || 25 : 0), options.shared.typeArrayMap)\n  }\n\n  function handler(query) {\n    if (ending)\n      return query.reject(Errors.connection('CONNECTION_ENDED', options, options))\n\n    if (open.length)\n      return go(open.shift(), query)\n\n    if (closed.length)\n      return connect(closed.shift(), query)\n\n    busy.length\n      ? go(busy.shift(), query)\n      : queries.push(query)\n  }\n\n  function go(c, query) {\n    return c.execute(query)\n      ? move(c, busy)\n      : move(c, full)\n  }\n\n  function cancel(query) {\n    return new Promise((resolve, reject) => {\n      query.state\n        ? query.active\n          ? Connection(options).cancel(query.state, resolve, reject)\n          : query.cancelled = { resolve, reject }\n        : (\n          queries.remove(query),\n          query.cancelled = true,\n          query.reject(Errors.generic('57014', 'canceling statement due to user request')),\n          resolve()\n        )\n    })\n  }\n\n  async function end({ timeout = null } = {}) {\n    if (ending)\n      return ending\n\n    await 1\n    let timer\n    return ending = Promise.race([\n      new Promise(r => timeout !== null && (timer = setTimeout(destroy, timeout * 1000, r))),\n      Promise.all(connections.map(c => c.end()).concat(\n        listen.sql ? listen.sql.end({ timeout: 0 }) : [],\n        subscribe.sql ? subscribe.sql.end({ timeout: 0 }) : []\n      ))\n    ]).then(() => clearTimeout(timer))\n  }\n\n  async function close() {\n    await Promise.all(connections.map(c => c.end()))\n  }\n\n  async function destroy(resolve) {\n    await Promise.all(connections.map(c => c.terminate()))\n    while (queries.length)\n      queries.shift().reject(Errors.connection('CONNECTION_DESTROYED', options))\n    resolve()\n  }\n\n  function connect(c, query) {\n    move(c, connecting)\n    c.connect(query)\n    return c\n  }\n\n  function onend(c) {\n    move(c, ended)\n  }\n\n  function onopen(c) {\n    if (queries.length === 0)\n      return move(c, open)\n\n    let max = Math.ceil(queries.length / (connecting.length + 1))\n      , ready = true\n\n    while (ready && queries.length && max-- > 0) {\n      const query = queries.shift()\n      if (query.reserve)\n        return query.reserve(c)\n\n      ready = c.execute(query)\n    }\n\n    ready\n      ? move(c, busy)\n      : move(c, full)\n  }\n\n  function onclose(c, e) {\n    move(c, closed)\n    c.reserved = null\n    c.onclose && (c.onclose(e), c.onclose = null)\n    options.onclose && options.onclose(c.id)\n    queries.length && connect(c, queries.shift())\n  }\n}\n\nfunction parseOptions(a, b) {\n  if (a && a.shared)\n    return a\n\n  const env = process.env // eslint-disable-line\n      , o = (!a || typeof a === 'string' ? b : a) || {}\n      , { url, multihost } = parseUrl(a)\n      , query = [...url.searchParams].reduce((a, [b, c]) => (a[b] = c, a), {})\n      , host = o.hostname || o.host || multihost || url.hostname || env.PGHOST || 'localhost'\n      , port = o.port || url.port || env.PGPORT || 5432\n      , user = o.user || o.username || url.username || env.PGUSERNAME || env.PGUSER || osUsername()\n\n  o.no_prepare && (o.prepare = false)\n  query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)\n  'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line\n  query.sslrootcert === 'system' && (query.ssl = 'verify-full')\n\n  const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive']\n  const defaults = {\n    max             : globalThis.Cloudflare ? 3 : 10,\n    ssl             : false,\n    sslnegotiation  : null,\n    idle_timeout    : null,\n    connect_timeout : 30,\n    max_lifetime    : max_lifetime,\n    max_pipeline    : 100,\n    backoff         : backoff,\n    keep_alive      : 60,\n    prepare         : true,\n    debug           : false,\n    fetch_types     : true,\n    publications    : 'alltables',\n    target_session_attrs: null\n  }\n\n  return {\n    host            : Array.isArray(host) ? host : host.split(',').map(x => x.split(':')[0]),\n    port            : Array.isArray(port) ? port : host.split(',').map(x => parseInt(x.split(':')[1] || port)),\n    path            : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,\n    database        : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || user,\n    user            : user,\n    pass            : o.pass || o.password || url.password || env.PGPASSWORD || '',\n    ...Object.entries(defaults).reduce(\n      (acc, [k, d]) => {\n        const value = k in o ? o[k] : k in query\n          ? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])\n          : env['PG' + k.toUpperCase()] || d\n        acc[k] = typeof value === 'string' && ints.includes(k)\n          ? +value\n          : value\n        return acc\n      },\n      {}\n    ),\n    connection      : {\n      application_name: env.PGAPPNAME || 'postgres.js',\n      ...o.connection,\n      ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})\n    },\n    types           : o.types || {},\n    target_session_attrs: tsa(o, url, env),\n    onnotice        : o.onnotice,\n    onnotify        : o.onnotify,\n    onclose         : o.onclose,\n    onparameter     : o.onparameter,\n    socket          : o.socket,\n    transform       : parseTransform(o.transform || { undefined: undefined }),\n    parameters      : {},\n    shared          : { retries: 0, typeArrayMap: {} },\n    ...mergeUserTypes(o.types)\n  }\n}\n\nfunction tsa(o, url, env) {\n  const x = o.target_session_attrs || url.searchParams.get('target_session_attrs') || env.PGTARGETSESSIONATTRS\n  if (!x || ['read-write', 'read-only', 'primary', 'standby', 'prefer-standby'].includes(x))\n    return x\n\n  throw new Error('target_session_attrs ' + x + ' is not supported')\n}\n\nfunction backoff(retries) {\n  return (0.5 + Math.random() / 2) * Math.min(3 ** retries / 100, 20)\n}\n\nfunction max_lifetime() {\n  return 60 * (30 + Math.random() * 30)\n}\n\nfunction parseTransform(x) {\n  return {\n    undefined: x.undefined,\n    column: {\n      from: typeof x.column === 'function' ? x.column : x.column && x.column.from,\n      to: x.column && x.column.to\n    },\n    value: {\n      from: typeof x.value === 'function' ? x.value : x.value && x.value.from,\n      to: x.value && x.value.to\n    },\n    row: {\n      from: typeof x.row === 'function' ? x.row : x.row && x.row.from,\n      to: x.row && x.row.to\n    }\n  }\n}\n\nfunction parseUrl(url) {\n  if (!url || typeof url !== 'string')\n    return { url: { searchParams: new Map() } }\n\n  let host = url\n  host = host.slice(host.indexOf('://') + 3).split(/[?/]/)[0]\n  host = decodeURIComponent(host.slice(host.indexOf('@') + 1))\n\n  const urlObj = new URL(url.replace(host, host.split(',')[0]))\n\n  return {\n    url: {\n      username: decodeURIComponent(urlObj.username),\n      password: decodeURIComponent(urlObj.password),\n      host: urlObj.host,\n      hostname: urlObj.hostname,\n      port: urlObj.port,\n      pathname: urlObj.pathname,\n      searchParams: urlObj.searchParams\n    },\n    multihost: host.indexOf(',') > -1 && host\n  }\n}\n\nfunction osUsername() {\n  try {\n    return os.userInfo().username // eslint-disable-line\n  } catch (_) {\n    return process.env.USERNAME || process.env.USER || process.env.LOGNAME  // eslint-disable-line\n  }\n}\n"
  },
  {
    "path": "cf/src/large.js",
    "content": "import Stream from 'node:stream'\n\nexport default function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {\n  return new Promise(async(resolve, reject) => {\n    await sql.begin(async sql => {\n      let finish\n      !oid && ([{ oid }] = await sql`select lo_creat(-1) as oid`)\n      const [{ fd }] = await sql`select lo_open(${ oid }, ${ mode }) as fd`\n\n      const lo = {\n        writable,\n        readable,\n        close     : () => sql`select lo_close(${ fd })`.then(finish),\n        tell      : () => sql`select lo_tell64(${ fd })`,\n        read      : (x) => sql`select loread(${ fd }, ${ x }) as data`,\n        write     : (x) => sql`select lowrite(${ fd }, ${ x })`,\n        truncate  : (x) => sql`select lo_truncate64(${ fd }, ${ x })`,\n        seek      : (x, whence = 0) => sql`select lo_lseek64(${ fd }, ${ x }, ${ whence })`,\n        size      : () => sql`\n          select\n            lo_lseek64(${ fd }, location, 0) as position,\n            seek.size\n          from (\n            select\n              lo_lseek64($1, 0, 2) as size,\n              tell.location\n            from (select lo_tell64($1) as location) tell\n          ) seek\n        `\n      }\n\n      resolve(lo)\n\n      return new Promise(async r => finish = r)\n\n      async function readable({\n        highWaterMark = 2048 * 8,\n        start = 0,\n        end = Infinity\n      } = {}) {\n        let max = end - start\n        start && await lo.seek(start)\n        return new Stream.Readable({\n          highWaterMark,\n          async read(size) {\n            const l = size > max ? size - max : size\n            max -= size\n            const [{ data }] = await lo.read(l)\n            this.push(data)\n            if (data.length < size)\n              this.push(null)\n          }\n        })\n      }\n\n      async function writable({\n        highWaterMark = 2048 * 8,\n        start = 0\n      } = {}) {\n        start && await lo.seek(start)\n        return new Stream.Writable({\n          highWaterMark,\n          write(chunk, encoding, callback) {\n            lo.write(chunk).then(() => callback(), callback)\n          }\n        })\n      }\n    }).catch(reject)\n  })\n}\n"
  },
  {
    "path": "cf/src/query.js",
    "content": "const originCache = new Map()\n    , originStackCache = new Map()\n    , originError = Symbol('OriginError')\n\nexport const CLOSE = {}\nexport class Query extends Promise {\n  constructor(strings, args, handler, canceller, options = {}) {\n    let resolve\n      , reject\n\n    super((a, b) => {\n      resolve = a\n      reject = b\n    })\n\n    this.tagged = Array.isArray(strings.raw)\n    this.strings = strings\n    this.args = args\n    this.handler = handler\n    this.canceller = canceller\n    this.options = options\n\n    this.state = null\n    this.statement = null\n\n    this.resolve = x => (this.active = false, resolve(x))\n    this.reject = x => (this.active = false, reject(x))\n\n    this.active = false\n    this.cancelled = null\n    this.executed = false\n    this.signature = ''\n\n    this[originError] = this.handler.debug\n      ? new Error()\n      : this.tagged && cachedError(this.strings)\n  }\n\n  get origin() {\n    return (this.handler.debug\n      ? this[originError].stack\n      : this.tagged && originStackCache.has(this.strings)\n        ? originStackCache.get(this.strings)\n        : originStackCache.set(this.strings, this[originError].stack).get(this.strings)\n    ) || ''\n  }\n\n  static get [Symbol.species]() {\n    return Promise\n  }\n\n  cancel() {\n    return this.canceller && (this.canceller(this), this.canceller = null)\n  }\n\n  simple() {\n    this.options.simple = true\n    this.options.prepare = false\n    return this\n  }\n\n  async readable() {\n    this.simple()\n    this.streaming = true\n    return this\n  }\n\n  async writable() {\n    this.simple()\n    this.streaming = true\n    return this\n  }\n\n  cursor(rows = 1, fn) {\n    this.options.simple = false\n    if (typeof rows === 'function') {\n      fn = rows\n      rows = 1\n    }\n\n    this.cursorRows = rows\n\n    if (typeof fn === 'function')\n      return (this.cursorFn = fn, this)\n\n    let prev\n    return {\n      [Symbol.asyncIterator]: () => ({\n        next: () => {\n          if (this.executed && !this.active)\n            return { done: true }\n\n          prev && prev()\n          const promise = new Promise((resolve, reject) => {\n            this.cursorFn = value => {\n              resolve({ value, done: false })\n              return new Promise(r => prev = r)\n            }\n            this.resolve = () => (this.active = false, resolve({ done: true }))\n            this.reject = x => (this.active = false, reject(x))\n          })\n          this.execute()\n          return promise\n        },\n        return() {\n          prev && prev(CLOSE)\n          return { done: true }\n        }\n      })\n    }\n  }\n\n  describe() {\n    this.options.simple = false\n    this.onlyDescribe = this.options.prepare = true\n    return this\n  }\n\n  stream() {\n    throw new Error('.stream has been renamed to .forEach')\n  }\n\n  forEach(fn) {\n    this.forEachFn = fn\n    this.handle()\n    return this\n  }\n\n  raw() {\n    this.isRaw = true\n    return this\n  }\n\n  values() {\n    this.isRaw = 'values'\n    return this\n  }\n\n  async handle() {\n    !this.executed && (this.executed = true) && await 1 && this.handler(this)\n  }\n\n  execute() {\n    this.handle()\n    return this\n  }\n\n  then() {\n    this.handle()\n    return super.then.apply(this, arguments)\n  }\n\n  catch() {\n    this.handle()\n    return super.catch.apply(this, arguments)\n  }\n\n  finally() {\n    this.handle()\n    return super.finally.apply(this, arguments)\n  }\n}\n\nfunction cachedError(xs) {\n  if (originCache.has(xs))\n    return originCache.get(xs)\n\n  const x = Error.stackTraceLimit\n  Error.stackTraceLimit = 4\n  originCache.set(xs, new Error())\n  Error.stackTraceLimit = x\n  return originCache.get(xs)\n}\n"
  },
  {
    "path": "cf/src/queue.js",
    "content": "export default Queue\n\nfunction Queue(initial = []) {\n  let xs = initial.slice()\n  let index = 0\n\n  return {\n    get length() {\n      return xs.length - index\n    },\n    remove: (x) => {\n      const index = xs.indexOf(x)\n      return index === -1\n        ? null\n        : (xs.splice(index, 1), x)\n    },\n    push: (x) => (xs.push(x), x),\n    shift: () => {\n      const out = xs[index++]\n\n      if (index === xs.length) {\n        index = 0\n        xs = []\n      } else {\n        xs[index - 1] = undefined\n      }\n\n      return out\n    }\n  }\n}\n"
  },
  {
    "path": "cf/src/result.js",
    "content": "export default class Result extends Array {\n  constructor() {\n    super()\n    Object.defineProperties(this, {\n      count: { value: null, writable: true },\n      state: { value: null, writable: true },\n      command: { value: null, writable: true },\n      columns: { value: null, writable: true },\n      statement: { value: null, writable: true }\n    })\n  }\n\n  static get [Symbol.species]() {\n    return Array\n  }\n}\n"
  },
  {
    "path": "cf/src/subscribe.js",
    "content": "import { Buffer } from 'node:buffer'\nconst noop = () => { /* noop */ }\n\nexport default function Subscribe(postgres, options) {\n  const subscribers = new Map()\n      , slot = 'postgresjs_' + Math.random().toString(36).slice(2)\n      , state = {}\n\n  let connection\n    , stream\n    , ended = false\n\n  const sql = subscribe.sql = postgres({\n    ...options,\n    transform: { column: {}, value: {}, row: {} },\n    max: 1,\n    fetch_types: false,\n    idle_timeout: null,\n    max_lifetime: null,\n    connection: {\n      ...options.connection,\n      replication: 'database'\n    },\n    onclose: async function() {\n      if (ended)\n        return\n      stream = null\n      state.pid = state.secret = undefined\n      connected(await init(sql, slot, options.publications))\n      subscribers.forEach(event => event.forEach(({ onsubscribe }) => onsubscribe()))\n    },\n    no_subscribe: true\n  })\n\n  const end = sql.end\n      , close = sql.close\n\n  sql.end = async() => {\n    ended = true\n    stream && (await new Promise(r => (stream.once('close', r), stream.end())))\n    return end()\n  }\n\n  sql.close = async() => {\n    stream && (await new Promise(r => (stream.once('close', r), stream.end())))\n    return close()\n  }\n\n  return subscribe\n\n  async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {\n    event = parseEvent(event)\n\n    if (!connection)\n      connection = init(sql, slot, options.publications)\n\n    const subscriber = { fn, onsubscribe }\n    const fns = subscribers.has(event)\n      ? subscribers.get(event).add(subscriber)\n      : subscribers.set(event, new Set([subscriber])).get(event)\n\n    const unsubscribe = () => {\n      fns.delete(subscriber)\n      fns.size === 0 && subscribers.delete(event)\n    }\n\n    return connection.then(x => {\n      connected(x)\n      onsubscribe()\n      stream && stream.on('error', onerror)\n      return { unsubscribe, state, sql }\n    })\n  }\n\n  function connected(x) {\n    stream = x.stream\n    state.pid = x.state.pid\n    state.secret = x.state.secret\n  }\n\n  async function init(sql, slot, publications) {\n    if (!publications)\n      throw new Error('Missing publication names')\n\n    const xs = await sql.unsafe(\n      `CREATE_REPLICATION_SLOT ${ slot } TEMPORARY LOGICAL pgoutput NOEXPORT_SNAPSHOT`\n    )\n\n    const [x] = xs\n\n    const stream = await sql.unsafe(\n      `START_REPLICATION SLOT ${ slot } LOGICAL ${\n        x.consistent_point\n      } (proto_version '1', publication_names '${ publications }')`\n    ).writable()\n\n    const state = {\n      lsn: Buffer.concat(x.consistent_point.split('/').map(x => Buffer.from(('00000000' + x).slice(-8), 'hex')))\n    }\n\n    stream.on('data', data)\n    stream.on('error', error)\n    stream.on('close', sql.close)\n\n    return { stream, state: xs.state }\n\n    function error(e) {\n      console.error('Unexpected error during logical streaming - reconnecting', e) // eslint-disable-line\n    }\n\n    function data(x) {\n      if (x[0] === 0x77) {\n        parse(x.subarray(25), state, sql.options.parsers, handle, options.transform)\n      } else if (x[0] === 0x6b && x[17]) {\n        state.lsn = x.subarray(1, 9)\n        pong()\n      }\n    }\n\n    function handle(a, b) {\n      const path = b.relation.schema + '.' + b.relation.table\n      call('*', a, b)\n      call('*:' + path, a, b)\n      b.relation.keys.length && call('*:' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)\n      call(b.command, a, b)\n      call(b.command + ':' + path, a, b)\n      b.relation.keys.length && call(b.command + ':' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)\n    }\n\n    function pong() {\n      const x = Buffer.alloc(34)\n      x[0] = 'r'.charCodeAt(0)\n      x.fill(state.lsn, 1)\n      x.writeBigInt64BE(BigInt(Date.now() - Date.UTC(2000, 0, 1)) * BigInt(1000), 25)\n      stream.write(x)\n    }\n  }\n\n  function call(x, a, b) {\n    subscribers.has(x) && subscribers.get(x).forEach(({ fn }) => fn(a, b, x))\n  }\n}\n\nfunction Time(x) {\n  return new Date(Date.UTC(2000, 0, 1) + Number(x / BigInt(1000)))\n}\n\nfunction parse(x, state, parsers, handle, transform) {\n  const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)\n\n  Object.entries({\n    R: x => {  // Relation\n      let i = 1\n      const r = state[x.readUInt32BE(i)] = {\n        schema: x.toString('utf8', i += 4, i = x.indexOf(0, i)) || 'pg_catalog',\n        table: x.toString('utf8', i + 1, i = x.indexOf(0, i + 1)),\n        columns: Array(x.readUInt16BE(i += 2)),\n        keys: []\n      }\n      i += 2\n\n      let columnIndex = 0\n        , column\n\n      while (i < x.length) {\n        column = r.columns[columnIndex++] = {\n          key: x[i++],\n          name: transform.column.from\n            ? transform.column.from(x.toString('utf8', i, i = x.indexOf(0, i)))\n            : x.toString('utf8', i, i = x.indexOf(0, i)),\n          type: x.readUInt32BE(i += 1),\n          parser: parsers[x.readUInt32BE(i)],\n          atttypmod: x.readUInt32BE(i += 4)\n        }\n\n        column.key && r.keys.push(column)\n        i += 4\n      }\n    },\n    Y: () => { /* noop */ }, // Type\n    O: () => { /* noop */ }, // Origin\n    B: x => { // Begin\n      state.date = Time(x.readBigInt64BE(9))\n      state.lsn = x.subarray(1, 9)\n    },\n    I: x => { // Insert\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      const { row } = tuples(x, relation.columns, i += 7, transform)\n\n      handle(row, {\n        command: 'insert',\n        relation\n      })\n    },\n    D: x => { // Delete\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      i += 4\n      const key = x[i] === 75\n      handle(key || x[i] === 79\n        ? tuples(x, relation.columns, i += 3, transform).row\n        : null\n      , {\n        command: 'delete',\n        relation,\n        key\n      })\n    },\n    U: x => { // Update\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      i += 4\n      const key = x[i] === 75\n      const xs = key || x[i] === 79\n        ? tuples(x, relation.columns, i += 3, transform)\n        : null\n\n      xs && (i = xs.i)\n\n      const { row } = tuples(x, relation.columns, i + 3, transform)\n\n      handle(row, {\n        command: 'update',\n        relation,\n        key,\n        old: xs && xs.row\n      })\n    },\n    T: () => { /* noop */ }, // Truncate,\n    C: () => { /* noop */ }  // Commit\n  }).reduce(char, {})[x[0]](x)\n}\n\nfunction tuples(x, columns, xi, transform) {\n  let type\n    , column\n    , value\n\n  const row = transform.raw ? new Array(columns.length) : {}\n  for (let i = 0; i < columns.length; i++) {\n    type = x[xi++]\n    column = columns[i]\n    value = type === 110 // n\n      ? null\n      : type === 117 // u\n        ? undefined\n        : column.parser === undefined\n          ? x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi))\n          : column.parser.array === true\n            ? column.parser(x.toString('utf8', xi + 5, xi += 4 + x.readUInt32BE(xi)))\n            : column.parser(x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi)))\n\n    transform.raw\n      ? (row[i] = transform.raw === true\n        ? value\n        : transform.value.from ? transform.value.from(value, column) : value)\n      : (row[column.name] = transform.value.from\n        ? transform.value.from(value, column)\n        : value\n      )\n  }\n\n  return { i: xi, row: transform.row.from ? transform.row.from(row) : row }\n}\n\nfunction parseEvent(x) {\n  const xs = x.match(/^(\\*|insert|update|delete)?:?([^.]+?\\.?[^=]+)?=?(.+)?/i) || []\n\n  if (!xs)\n    throw new Error('Malformed subscribe pattern: ' + x)\n\n  const [, command, path, key] = xs\n\n  return (command || '*')\n       + (path ? ':' + (path.indexOf('.') === -1 ? 'public.' + path : path) : '')\n       + (key ? '=' + key : '')\n}\n"
  },
  {
    "path": "cf/src/types.js",
    "content": "import { Buffer } from 'node:buffer'\nimport { Query } from './query.js'\nimport { Errors } from './errors.js'\n\nexport const types = {\n  string: {\n    to: 25,\n    from: null,             // defaults to string\n    serialize: x => '' + x\n  },\n  number: {\n    to: 0,\n    from: [21, 23, 26, 700, 701],\n    serialize: x => '' + x,\n    parse: x => +x\n  },\n  json: {\n    to: 114,\n    from: [114, 3802],\n    serialize: x => JSON.stringify(x),\n    parse: x => JSON.parse(x)\n  },\n  boolean: {\n    to: 16,\n    from: 16,\n    serialize: x => x === true ? 't' : 'f',\n    parse: x => x === 't'\n  },\n  date: {\n    to: 1184,\n    from: [1082, 1114, 1184],\n    serialize: x => (x instanceof Date ? x : new Date(x)).toISOString(),\n    parse: x => new Date(x)\n  },\n  bytea: {\n    to: 17,\n    from: 17,\n    serialize: x => '\\\\x' + Buffer.from(x).toString('hex'),\n    parse: x => Buffer.from(x.slice(2), 'hex')\n  }\n}\n\nclass NotTagged { then() { notTagged() } catch() { notTagged() } finally() { notTagged() }}\n\nexport class Identifier extends NotTagged {\n  constructor(value) {\n    super()\n    this.value = escapeIdentifier(value)\n  }\n}\n\nexport class Parameter extends NotTagged {\n  constructor(value, type, array) {\n    super()\n    this.value = value\n    this.type = type\n    this.array = array\n  }\n}\n\nexport class Builder extends NotTagged {\n  constructor(first, rest) {\n    super()\n    this.first = first\n    this.rest = rest\n  }\n\n  build(before, parameters, types, options) {\n    const keyword = builders.map(([x, fn]) => ({ fn, i: before.search(x) })).sort((a, b) => a.i - b.i).pop()\n    return keyword.i === -1\n      ? escapeIdentifiers(this.first, options)\n      : keyword.fn(this.first, this.rest, parameters, types, options)\n  }\n}\n\nexport function handleValue(x, parameters, types, options) {\n  let value = x instanceof Parameter ? x.value : x\n  if (value === undefined) {\n    x instanceof Parameter\n      ? x.value = options.transform.undefined\n      : value = x = options.transform.undefined\n\n    if (value === undefined)\n      throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')\n  }\n\n  return '$' + (types.push(\n    x instanceof Parameter\n      ? (parameters.push(x.value), x.array\n        ? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)\n        : x.type\n      )\n      : (parameters.push(x), inferType(x))\n  ))\n}\n\nconst defaultHandlers = typeHandlers(types)\n\nexport function stringify(q, string, value, parameters, types, options) { // eslint-disable-line\n  for (let i = 1; i < q.strings.length; i++) {\n    string += (stringifyValue(string, value, parameters, types, options)) + q.strings[i]\n    value = q.args[i]\n  }\n\n  return string\n}\n\nfunction stringifyValue(string, value, parameters, types, o) {\n  return (\n    value instanceof Builder ? value.build(string, parameters, types, o) :\n    value instanceof Query ? fragment(value, parameters, types, o) :\n    value instanceof Identifier ? value.value :\n    value && value[0] instanceof Query ? value.reduce((acc, x) => acc + ' ' + fragment(x, parameters, types, o), '') :\n    handleValue(value, parameters, types, o)\n  )\n}\n\nfunction fragment(q, parameters, types, options) {\n  q.fragment = true\n  return stringify(q, q.strings[0], q.args[0], parameters, types, options)\n}\n\nfunction valuesBuilder(first, parameters, types, columns, options) {\n  return first.map(row =>\n    '(' + columns.map(column =>\n      stringifyValue('values', row[column], parameters, types, options)\n    ).join(',') + ')'\n  ).join(',')\n}\n\nfunction values(first, rest, parameters, types, options) {\n  const multi = Array.isArray(first[0])\n  const columns = rest.length ? rest.flat() : Object.keys(multi ? first[0] : first)\n  return valuesBuilder(multi ? first : [first], parameters, types, columns, options)\n}\n\nfunction select(first, rest, parameters, types, options) {\n  typeof first === 'string' && (first = [first].concat(rest))\n  if (Array.isArray(first))\n    return escapeIdentifiers(first, options)\n\n  let value\n  const columns = rest.length ? rest.flat() : Object.keys(first)\n  return columns.map(x => {\n    value = first[x]\n    return (\n      value instanceof Query ? fragment(value, parameters, types, options) :\n      value instanceof Identifier ? value.value :\n      handleValue(value, parameters, types, options)\n    ) + ' as ' + escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)\n  }).join(',')\n}\n\nconst builders = Object.entries({\n  values,\n  in: (...xs) => {\n    const x = values(...xs)\n    return x === '()' ? '(null)' : x\n  },\n  select,\n  as: select,\n  returning: select,\n  '\\\\(': select,\n\n  update(first, rest, parameters, types, options) {\n    return (rest.length ? rest.flat() : Object.keys(first)).map(x =>\n      escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x) +\n      '=' + stringifyValue('values', first[x], parameters, types, options)\n    )\n  },\n\n  insert(first, rest, parameters, types, options) {\n    const columns = rest.length ? rest.flat() : Object.keys(Array.isArray(first) ? first[0] : first)\n    return '(' + escapeIdentifiers(columns, options) + ')values' +\n    valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, columns, options)\n  }\n}).map(([x, fn]) => ([new RegExp('((?:^|[\\\\s(])' + x + '(?:$|[\\\\s(]))(?![\\\\s\\\\S]*\\\\1)', 'i'), fn]))\n\nfunction notTagged() {\n  throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')\n}\n\nexport const serializers = defaultHandlers.serializers\nexport const parsers = defaultHandlers.parsers\n\nexport const END = {}\n\nfunction firstIsString(x) {\n  if (Array.isArray(x))\n    return firstIsString(x[0])\n  return typeof x === 'string' ? 1009 : 0\n}\n\nexport const mergeUserTypes = function(types) {\n  const user = typeHandlers(types || {})\n  return {\n    serializers: Object.assign({}, serializers, user.serializers),\n    parsers: Object.assign({}, parsers, user.parsers)\n  }\n}\n\nfunction typeHandlers(types) {\n  return Object.keys(types).reduce((acc, k) => {\n    types[k].from && [].concat(types[k].from).forEach(x => acc.parsers[x] = types[k].parse)\n    if (types[k].serialize) {\n      acc.serializers[types[k].to] = types[k].serialize\n      types[k].from && [].concat(types[k].from).forEach(x => acc.serializers[x] = types[k].serialize)\n    }\n    return acc\n  }, { parsers: {}, serializers: {} })\n}\n\nfunction escapeIdentifiers(xs, { transform: { column } }) {\n  return xs.map(x => escapeIdentifier(column.to ? column.to(x) : x)).join(',')\n}\n\nexport const escapeIdentifier = function escape(str) {\n  return '\"' + str.replace(/\"/g, '\"\"').replace(/\\./g, '\".\"') + '\"'\n}\n\nexport const inferType = function inferType(x) {\n  return (\n    x instanceof Parameter ? x.type :\n    x instanceof Date ? 1184 :\n    x instanceof Uint8Array ? 17 :\n    (x === true || x === false) ? 16 :\n    typeof x === 'bigint' ? 20 :\n    Array.isArray(x) ? inferType(x[0]) :\n    0\n  )\n}\n\nconst escapeBackslash = /\\\\/g\nconst escapeQuote = /\"/g\n\nfunction arrayEscape(x) {\n  return x\n    .replace(escapeBackslash, '\\\\\\\\')\n    .replace(escapeQuote, '\\\\\"')\n}\n\nexport const arraySerializer = function arraySerializer(xs, serializer, options, typarray) {\n  if (Array.isArray(xs) === false)\n    return xs\n\n  if (!xs.length)\n    return '{}'\n\n  const first = xs[0]\n  // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter\n  const delimiter = typarray === 1020 ? ';' : ','\n\n  if (Array.isArray(first) && !first.type)\n    return '{' + xs.map(x => arraySerializer(x, serializer, options, typarray)).join(delimiter) + '}'\n\n  return '{' + xs.map(x => {\n    if (x === undefined) {\n      x = options.transform.undefined\n      if (x === undefined)\n        throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')\n    }\n\n    return x === null\n      ? 'null'\n      : '\"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '\"'\n  }).join(delimiter) + '}'\n}\n\nconst arrayParserState = {\n  i: 0,\n  char: null,\n  str: '',\n  quoted: false,\n  last: 0\n}\n\nexport const arrayParser = function arrayParser(x, parser, typarray) {\n  arrayParserState.i = arrayParserState.last = 0\n  return arrayParserLoop(arrayParserState, x, parser, typarray)\n}\n\nfunction arrayParserLoop(s, x, parser, typarray) {\n  const xs = []\n  // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter\n  const delimiter = typarray === 1020 ? ';' : ','\n  for (; s.i < x.length; s.i++) {\n    s.char = x[s.i]\n    if (s.quoted) {\n      if (s.char === '\\\\') {\n        s.str += x[++s.i]\n      } else if (s.char === '\"') {\n        xs.push(parser ? parser(s.str) : s.str)\n        s.str = ''\n        s.quoted = x[s.i + 1] === '\"'\n        s.last = s.i + 2\n      } else {\n        s.str += s.char\n      }\n    } else if (s.char === '\"') {\n      s.quoted = true\n    } else if (s.char === '{') {\n      s.last = ++s.i\n      xs.push(arrayParserLoop(s, x, parser, typarray))\n    } else if (s.char === '}') {\n      s.quoted = false\n      s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))\n      s.last = s.i + 1\n      break\n    } else if (s.char === delimiter && s.p !== '}' && s.p !== '\"') {\n      xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))\n      s.last = s.i + 1\n    }\n    s.p = s.char\n  }\n  s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i + 1)) : x.slice(s.last, s.i + 1))\n  return xs\n}\n\nexport const toCamel = x => {\n  let str = x[0]\n  for (let i = 1; i < x.length; i++)\n    str += x[i] === '_' ? x[++i].toUpperCase() : x[i]\n  return str\n}\n\nexport const toPascal = x => {\n  let str = x[0].toUpperCase()\n  for (let i = 1; i < x.length; i++)\n    str += x[i] === '_' ? x[++i].toUpperCase() : x[i]\n  return str\n}\n\nexport const toKebab = x => x.replace(/_/g, '-')\n\nexport const fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()\nexport const fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()\nexport const fromKebab = x => x.replace(/-/g, '_')\n\nfunction createJsonTransform(fn) {\n  return function jsonTransform(x, column) {\n    return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)\n      ? Array.isArray(x)\n        ? x.map(x => jsonTransform(x, column))\n        : Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})\n      : x\n  }\n}\n\ntoCamel.column = { from: toCamel }\ntoCamel.value = { from: createJsonTransform(toCamel) }\nfromCamel.column = { to: fromCamel }\n\nexport const camel = { ...toCamel }\ncamel.column.to = fromCamel\n\ntoPascal.column = { from: toPascal }\ntoPascal.value = { from: createJsonTransform(toPascal) }\nfromPascal.column = { to: fromPascal }\n\nexport const pascal = { ...toPascal }\npascal.column.to = fromPascal\n\ntoKebab.column = { from: toKebab }\ntoKebab.value = { from: createJsonTransform(toKebab) }\nfromKebab.column = { to: fromKebab }\n\nexport const kebab = { ...toKebab }\nkebab.column.to = fromKebab\n"
  },
  {
    "path": "cf/test.js",
    "content": "// Add your database url and run this file with the below two commands to test pages and workers\n// npx wrangler@latest pages dev ./cf --script-path test.js  --compatibility-date=2023-06-20 --log-level=debug --compatibility-flag=nodejs_compat\n// npx wrangler@latest dev ./cf/test.js --compatibility-date=2023-06-20 --log-level=debug --compatibility-flag=nodejs_compat\n\nimport postgres from './src/index.js'\nconst DATABASE_URL = ''\n\nexport default {\n  async fetch() {\n    const sql = postgres(DATABASE_URL)\n    const rows = await sql`SELECT table_name FROM information_schema.columns`\n    return new Response(rows.map((e) => e.table_name).join('\\n'))\n  }\n}\n"
  },
  {
    "path": "cjs/package.json",
    "content": "{\"type\":\"commonjs\"}"
  },
  {
    "path": "cjs/src/bytes.js",
    "content": "const size = 256\nlet buffer = Buffer.allocUnsafe(size)\n\nconst messages = 'BCcDdEFfHPpQSX'.split('').reduce((acc, x) => {\n  const v = x.charCodeAt(0)\n  acc[x] = () => {\n    buffer[0] = v\n    b.i = 5\n    return b\n  }\n  return acc\n}, {})\n\nconst b = Object.assign(reset, messages, {\n  N: String.fromCharCode(0),\n  i: 0,\n  inc(x) {\n    b.i += x\n    return b\n  },\n  str(x) {\n    const length = Buffer.byteLength(x)\n    fit(length)\n    b.i += buffer.write(x, b.i, length, 'utf8')\n    return b\n  },\n  i16(x) {\n    fit(2)\n    buffer.writeUInt16BE(x, b.i)\n    b.i += 2\n    return b\n  },\n  i32(x, i) {\n    if (i || i === 0) {\n      buffer.writeUInt32BE(x, i)\n      return b\n    }\n    fit(4)\n    buffer.writeUInt32BE(x, b.i)\n    b.i += 4\n    return b\n  },\n  z(x) {\n    fit(x)\n    buffer.fill(0, b.i, b.i + x)\n    b.i += x\n    return b\n  },\n  raw(x) {\n    buffer = Buffer.concat([buffer.subarray(0, b.i), x])\n    b.i = buffer.length\n    return b\n  },\n  end(at = 1) {\n    buffer.writeUInt32BE(b.i - at, at)\n    const out = buffer.subarray(0, b.i)\n    b.i = 0\n    buffer = Buffer.allocUnsafe(size)\n    return out\n  }\n})\n\nmodule.exports = b\n\nfunction fit(x) {\n  if (buffer.length - b.i < x) {\n    const prev = buffer\n        , length = prev.length\n\n    buffer = Buffer.allocUnsafe(length + (length >> 1) + x)\n    prev.copy(buffer)\n  }\n}\n\nfunction reset() {\n  b.i = 0\n  return b\n}\n"
  },
  {
    "path": "cjs/src/connection.js",
    "content": "const net = require('net')\nconst tls = require('tls')\nconst crypto = require('crypto')\nconst Stream = require('stream')\nconst { performance } = require('perf_hooks')\n\nconst { stringify, handleValue, arrayParser, arraySerializer } = require('./types.js')\nconst { Errors } = require('./errors.js')\nconst Result = require('./result.js')\nconst Queue = require('./queue.js')\nconst { Query, CLOSE } = require('./query.js')\nconst b = require('./bytes.js')\n\nmodule.exports = Connection\n\nlet uid = 1\n\nconst Sync = b().S().end()\n    , Flush = b().H().end()\n    , SSLRequest = b().i32(8).i32(80877103).end(8)\n    , ExecuteUnnamed = Buffer.concat([b().E().str(b.N).i32(0).end(), Sync])\n    , DescribeUnnamed = b().D().str('S').str(b.N).end()\n    , noop = () => { /* noop */ }\n\nconst retryRoutines = new Set([\n  'FetchPreparedStatement',\n  'RevalidateCachedQuery',\n  'transformAssignedExpr'\n])\n\nconst errorFields = {\n  83  : 'severity_local',    // S\n  86  : 'severity',          // V\n  67  : 'code',              // C\n  77  : 'message',           // M\n  68  : 'detail',            // D\n  72  : 'hint',              // H\n  80  : 'position',          // P\n  112 : 'internal_position', // p\n  113 : 'internal_query',    // q\n  87  : 'where',             // W\n  115 : 'schema_name',       // s\n  116 : 'table_name',        // t\n  99  : 'column_name',       // c\n  100 : 'data type_name',    // d\n  110 : 'constraint_name',   // n\n  70  : 'file',              // F\n  76  : 'line',              // L\n  82  : 'routine'            // R\n}\n\nfunction Connection(options, queues = {}, { onopen = noop, onend = noop, onclose = noop } = {}) {\n  const {\n    sslnegotiation,\n    ssl,\n    max,\n    user,\n    host,\n    port,\n    database,\n    parsers,\n    transform,\n    onnotice,\n    onnotify,\n    onparameter,\n    max_pipeline,\n    keep_alive,\n    backoff,\n    target_session_attrs\n  } = options\n\n  const sent = Queue()\n      , id = uid++\n      , backend = { pid: null, secret: null }\n      , idleTimer = timer(end, options.idle_timeout)\n      , lifeTimer = timer(end, options.max_lifetime)\n      , connectTimer = timer(connectTimedOut, options.connect_timeout)\n\n  let socket = null\n    , cancelMessage\n    , errorResponse = null\n    , result = new Result()\n    , incoming = Buffer.alloc(0)\n    , needsTypes = options.fetch_types\n    , backendParameters = {}\n    , statements = {}\n    , statementId = Math.random().toString(36).slice(2)\n    , statementCount = 1\n    , closedTime = 0\n    , remaining = 0\n    , hostIndex = 0\n    , retries = 0\n    , length = 0\n    , delay = 0\n    , rows = 0\n    , serverSignature = null\n    , nextWriteTimer = null\n    , terminated = false\n    , incomings = null\n    , results = null\n    , initial = null\n    , ending = null\n    , stream = null\n    , chunk = null\n    , ended = null\n    , nonce = null\n    , query = null\n    , final = null\n\n  const connection = {\n    queue: queues.closed,\n    idleTimer,\n    connect(query) {\n      initial = query\n      reconnect()\n    },\n    terminate,\n    execute,\n    cancel,\n    end,\n    count: 0,\n    id\n  }\n\n  queues.closed && queues.closed.push(connection)\n\n  return connection\n\n  async function createSocket() {\n    let x\n    try {\n      x = options.socket\n        ? (await Promise.resolve(options.socket(options)))\n        : new net.Socket()\n    } catch (e) {\n      error(e)\n      return\n    }\n    x.on('error', error)\n    x.on('close', closed)\n    x.on('drain', drain)\n    return x\n  }\n\n  async function cancel({ pid, secret }, resolve, reject) {\n    try {\n      cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)\n      await connect()\n      socket.once('error', reject)\n      socket.once('close', resolve)\n    } catch (error) {\n      reject(error)\n    }\n  }\n\n  function execute(q) {\n    if (terminated)\n      return queryError(q, Errors.connection('CONNECTION_DESTROYED', options))\n\n    if (stream)\n      return queryError(q, Errors.generic('COPY_IN_PROGRESS', 'You cannot execute queries during copy'))\n\n    if (q.cancelled)\n      return\n\n    try {\n      q.state = backend\n      query\n        ? sent.push(q)\n        : (query = q, query.active = true)\n\n      build(q)\n      return write(toBuffer(q))\n        && !q.describeFirst\n        && !q.cursorFn\n        && sent.length < max_pipeline\n        && (!q.options.onexecute || q.options.onexecute(connection))\n    } catch (error) {\n      sent.length === 0 && write(Sync)\n      errored(error)\n      return true\n    }\n  }\n\n  function toBuffer(q) {\n    if (q.parameters.length >= 65534)\n      throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')\n\n    return q.options.simple\n      ? b().Q().str(q.statement.string + b.N).end()\n      : q.describeFirst\n        ? Buffer.concat([describe(q), Flush])\n        : q.prepare\n          ? q.prepared\n            ? prepared(q)\n            : Buffer.concat([describe(q), prepared(q)])\n          : unnamed(q)\n  }\n\n  function describe(q) {\n    return Buffer.concat([\n      Parse(q.statement.string, q.parameters, q.statement.types, q.statement.name),\n      Describe('S', q.statement.name)\n    ])\n  }\n\n  function prepared(q) {\n    return Buffer.concat([\n      Bind(q.parameters, q.statement.types, q.statement.name, q.cursorName),\n      q.cursorFn\n        ? Execute('', q.cursorRows)\n        : ExecuteUnnamed\n    ])\n  }\n\n  function unnamed(q) {\n    return Buffer.concat([\n      Parse(q.statement.string, q.parameters, q.statement.types),\n      DescribeUnnamed,\n      prepared(q)\n    ])\n  }\n\n  function build(q) {\n    const parameters = []\n        , types = []\n\n    const string = stringify(q, q.strings[0], q.args[0], parameters, types, options)\n\n    !q.tagged && q.args.forEach(x => handleValue(x, parameters, types, options))\n\n    q.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)\n    q.string = string\n    q.signature = q.prepare && types + string\n    q.onlyDescribe && (delete statements[q.signature])\n    q.parameters = q.parameters || parameters\n    q.prepared = q.prepare && q.signature in statements\n    q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared)\n    q.statement = q.prepared\n      ? statements[q.signature]\n      : { string, types, name: q.prepare ? statementId + statementCount++ : '' }\n\n    typeof options.debug === 'function' && options.debug(id, string, parameters, types)\n  }\n\n  function write(x, fn) {\n    chunk = chunk ? Buffer.concat([chunk, x]) : Buffer.from(x)\n    if (fn || chunk.length >= 1024)\n      return nextWrite(fn)\n    nextWriteTimer === null && (nextWriteTimer = setImmediate(nextWrite))\n    return true\n  }\n\n  function nextWrite(fn) {\n    const x = socket.write(chunk, fn)\n    nextWriteTimer !== null && clearImmediate(nextWriteTimer)\n    chunk = nextWriteTimer = null\n    return x\n  }\n\n  function connectTimedOut() {\n    errored(Errors.connection('CONNECT_TIMEOUT', options, socket))\n    socket.destroy()\n  }\n\n  async function secure() {\n    if (sslnegotiation !== 'direct') {\n      write(SSLRequest)\n      const canSSL = await new Promise(r => socket.once('data', x => r(x[0] === 83))) // S\n\n      if (!canSSL && ssl === 'prefer')\n        return connected()\n    }\n\n    const options = {\n      socket,\n      servername: net.isIP(socket.host) ? undefined : socket.host\n    }\n\n    if (sslnegotiation === 'direct')\n      options.ALPNProtocols = ['postgresql']\n\n    if (ssl === 'require' || ssl === 'allow' || ssl === 'prefer')\n      options.rejectUnauthorized = false\n    else if (typeof ssl === 'object')\n      Object.assign(options, ssl)\n\n    socket.removeAllListeners()\n    socket = tls.connect(options)\n    socket.on('secureConnect', connected)\n    socket.on('error', error)\n    socket.on('close', closed)\n    socket.on('drain', drain)\n  }\n\n  /* c8 ignore next 3 */\n  function drain() {\n    !query && onopen(connection)\n  }\n\n  function data(x) {\n    if (incomings) {\n      incomings.push(x)\n      remaining -= x.length\n      if (remaining > 0)\n        return\n    }\n\n    incoming = incomings\n      ? Buffer.concat(incomings, length - remaining)\n      : incoming.length === 0\n        ? x\n        : Buffer.concat([incoming, x], incoming.length + x.length)\n\n    while (incoming.length > 4) {\n      length = incoming.readUInt32BE(1)\n      if (length >= incoming.length) {\n        remaining = length - incoming.length\n        incomings = [incoming]\n        break\n      }\n\n      try {\n        handle(incoming.subarray(0, length + 1))\n      } catch (e) {\n        query && (query.cursorFn || query.describeFirst) && write(Sync)\n        errored(e)\n      }\n      incoming = incoming.subarray(length + 1)\n      remaining = 0\n      incomings = null\n    }\n  }\n\n  async function connect() {\n    terminated = false\n    backendParameters = {}\n    socket || (socket = await createSocket())\n\n    if (!socket)\n      return\n\n    connectTimer.start()\n\n    if (options.socket)\n      return ssl ? secure() : connected()\n\n    socket.on('connect', ssl ? secure : connected)\n\n    if (options.path)\n      return socket.connect(options.path)\n\n    socket.ssl = ssl\n    socket.connect(port[hostIndex], host[hostIndex])\n    socket.host = host[hostIndex]\n    socket.port = port[hostIndex]\n\n    hostIndex = (hostIndex + 1) % port.length\n  }\n\n  function reconnect() {\n    setTimeout(connect, closedTime ? Math.max(0, closedTime + delay - performance.now()) : 0)\n  }\n\n  function connected() {\n    try {\n      statements = {}\n      needsTypes = options.fetch_types\n      statementId = Math.random().toString(36).slice(2)\n      statementCount = 1\n      lifeTimer.start()\n      socket.on('data', data)\n      keep_alive && socket.setKeepAlive && socket.setKeepAlive(true, 1000 * keep_alive)\n      const s = StartupMessage()\n      write(s)\n    } catch (err) {\n      error(err)\n    }\n  }\n\n  function error(err) {\n    if (connection.queue === queues.connecting && options.host[retries + 1])\n      return\n\n    errored(err)\n    while (sent.length)\n      queryError(sent.shift(), err)\n  }\n\n  function errored(err) {\n    stream && (stream.destroy(err), stream = null)\n    query && queryError(query, err)\n    initial && (queryError(initial, err), initial = null)\n  }\n\n  function queryError(query, err) {\n    if (query.reserve)\n      return query.reject(err)\n\n    if (!err || typeof err !== 'object')\n      err = new Error(err)\n\n    'query' in err || 'parameters' in err || Object.defineProperties(err, {\n      stack: { value: err.stack + query.origin.replace(/.*\\n/, '\\n'), enumerable: options.debug },\n      query: { value: query.string, enumerable: options.debug },\n      parameters: { value: query.parameters, enumerable: options.debug },\n      args: { value: query.args, enumerable: options.debug },\n      types: { value: query.statement && query.statement.types, enumerable: options.debug }\n    })\n    query.reject(err)\n  }\n\n  function end() {\n    return ending || (\n      !connection.reserved && onend(connection),\n      !connection.reserved && !initial && !query && sent.length === 0\n        ? (terminate(), new Promise(r => socket && socket.readyState !== 'closed' ? socket.once('close', r) : r()))\n        : ending = new Promise(r => ended = r)\n    )\n  }\n\n  function terminate() {\n    terminated = true\n    if (stream || query || initial || sent.length)\n      error(Errors.connection('CONNECTION_DESTROYED', options))\n\n    clearImmediate(nextWriteTimer)\n    if (socket) {\n      socket.removeListener('data', data)\n      socket.removeListener('connect', connected)\n      socket.readyState === 'open' && socket.end(b().X().end())\n    }\n    ended && (ended(), ending = ended = null)\n  }\n\n  async function closed(hadError) {\n    incoming = Buffer.alloc(0)\n    remaining = 0\n    incomings = null\n    clearImmediate(nextWriteTimer)\n    socket.removeListener('data', data)\n    socket.removeListener('connect', connected)\n    idleTimer.cancel()\n    lifeTimer.cancel()\n    connectTimer.cancel()\n\n    socket.removeAllListeners()\n    socket = null\n\n    if (initial)\n      return reconnect()\n\n    !hadError && (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket))\n    closedTime = performance.now()\n    hadError && options.shared.retries++\n    delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000\n    onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket))\n  }\n\n  /* Handlers */\n  function handle(xs, x = xs[0]) {\n    (\n      x === 68 ? DataRow :                   // D\n      x === 100 ? CopyData :                 // d\n      x === 65 ? NotificationResponse :      // A\n      x === 83 ? ParameterStatus :           // S\n      x === 90 ? ReadyForQuery :             // Z\n      x === 67 ? CommandComplete :           // C\n      x === 50 ? BindComplete :              // 2\n      x === 49 ? ParseComplete :             // 1\n      x === 116 ? ParameterDescription :     // t\n      x === 84 ? RowDescription :            // T\n      x === 82 ? Authentication :            // R\n      x === 110 ? NoData :                   // n\n      x === 75 ? BackendKeyData :            // K\n      x === 69 ? ErrorResponse :             // E\n      x === 115 ? PortalSuspended :          // s\n      x === 51 ? CloseComplete :             // 3\n      x === 71 ? CopyInResponse :            // G\n      x === 78 ? NoticeResponse :            // N\n      x === 72 ? CopyOutResponse :           // H\n      x === 99 ? CopyDone :                  // c\n      x === 73 ? EmptyQueryResponse :        // I\n      x === 86 ? FunctionCallResponse :      // V\n      x === 118 ? NegotiateProtocolVersion : // v\n      x === 87 ? CopyBothResponse :          // W\n      /* c8 ignore next */\n      UnknownMessage\n    )(xs)\n  }\n\n  function DataRow(x) {\n    let index = 7\n    let length\n    let column\n    let value\n\n    const row = query.isRaw ? new Array(query.statement.columns.length) : {}\n    for (let i = 0; i < query.statement.columns.length; i++) {\n      column = query.statement.columns[i]\n      length = x.readInt32BE(index)\n      index += 4\n\n      value = length === -1\n        ? null\n        : query.isRaw === true\n          ? x.subarray(index, index += length)\n          : column.parser === undefined\n            ? x.toString('utf8', index, index += length)\n            : column.parser.array === true\n              ? column.parser(x.toString('utf8', index + 1, index += length))\n              : column.parser(x.toString('utf8', index, index += length))\n\n      query.isRaw\n        ? (row[i] = query.isRaw === true\n          ? value\n          : transform.value.from ? transform.value.from(value, column) : value)\n        : (row[column.name] = transform.value.from ? transform.value.from(value, column) : value)\n    }\n\n    query.forEachFn\n      ? query.forEachFn(transform.row.from ? transform.row.from(row) : row, result)\n      : (result[rows++] = transform.row.from ? transform.row.from(row) : row)\n  }\n\n  function ParameterStatus(x) {\n    const [k, v] = x.toString('utf8', 5, x.length - 1).split(b.N)\n    backendParameters[k] = v\n    if (options.parameters[k] !== v) {\n      options.parameters[k] = v\n      onparameter && onparameter(k, v)\n    }\n  }\n\n  function ReadyForQuery(x) {\n    if (query) {\n      if (errorResponse) {\n        query.retried\n          ? errored(query.retried)\n          : query.prepared && retryRoutines.has(errorResponse.routine)\n            ? retry(query, errorResponse)\n            : errored(errorResponse)\n      } else {\n        query.resolve(results || result)\n      }\n    } else if (errorResponse) {\n      errored(errorResponse)\n    }\n\n    query = results = errorResponse = null\n    result = new Result()\n    connectTimer.cancel()\n\n    if (initial) {\n      if (target_session_attrs) {\n        if (!backendParameters.in_hot_standby || !backendParameters.default_transaction_read_only)\n          return fetchState()\n        else if (tryNext(target_session_attrs, backendParameters))\n          return terminate()\n      }\n\n      if (needsTypes) {\n        initial.reserve && (initial = null)\n        return fetchArrayTypes()\n      }\n\n      initial && !initial.reserve && execute(initial)\n      options.shared.retries = retries = 0\n      initial = null\n      return\n    }\n\n    while (sent.length && (query = sent.shift()) && (query.active = true, query.cancelled))\n      Connection(options).cancel(query.state, query.cancelled.resolve, query.cancelled.reject)\n\n    if (query)\n      return // Consider opening if able and sent.length < 50\n\n    connection.reserved\n      ? !connection.reserved.release && x[5] === 73 // I\n        ? ending\n          ? terminate()\n          : (connection.reserved = null, onopen(connection))\n        : connection.reserved()\n      : ending\n        ? terminate()\n        : onopen(connection)\n  }\n\n  function CommandComplete(x) {\n    rows = 0\n\n    for (let i = x.length - 1; i > 0; i--) {\n      if (x[i] === 32 && x[i + 1] < 58 && result.count === null)\n        result.count = +x.toString('utf8', i + 1, x.length - 1)\n      if (x[i - 1] >= 65) {\n        result.command = x.toString('utf8', 5, i)\n        result.state = backend\n        break\n      }\n    }\n\n    final && (final(), final = null)\n\n    if (result.command === 'BEGIN' && max !== 1 && !connection.reserved)\n      return errored(Errors.generic('UNSAFE_TRANSACTION', 'Only use sql.begin, sql.reserved or max: 1'))\n\n    if (query.options.simple)\n      return BindComplete()\n\n    if (query.cursorFn) {\n      result.count && query.cursorFn(result)\n      write(Sync)\n    }\n  }\n\n  function ParseComplete() {\n    query.parsing = false\n  }\n\n  function BindComplete() {\n    !result.statement && (result.statement = query.statement)\n    result.columns = query.statement.columns\n  }\n\n  function ParameterDescription(x) {\n    const length = x.readUInt16BE(5)\n\n    for (let i = 0; i < length; ++i)\n      !query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))\n\n    query.prepare && (statements[query.signature] = query.statement)\n    query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false)\n  }\n\n  function RowDescription(x) {\n    if (result.command) {\n      results = results || [result]\n      results.push(result = new Result())\n      result.count = null\n      query.statement.columns = null\n    }\n\n    const length = x.readUInt16BE(5)\n    let index = 7\n    let start\n\n    query.statement.columns = Array(length)\n\n    for (let i = 0; i < length; ++i) {\n      start = index\n      while (x[index++] !== 0);\n      const table = x.readUInt32BE(index)\n      const number = x.readUInt16BE(index + 4)\n      const type = x.readUInt32BE(index + 6)\n      query.statement.columns[i] = {\n        name: transform.column.from\n          ? transform.column.from(x.toString('utf8', start, index - 1))\n          : x.toString('utf8', start, index - 1),\n        parser: parsers[type],\n        table,\n        number,\n        type\n      }\n      index += 18\n    }\n\n    result.statement = query.statement\n    if (query.onlyDescribe)\n      return (query.resolve(query.statement), write(Sync))\n  }\n\n  async function Authentication(x, type = x.readUInt32BE(5)) {\n    (\n      type === 3 ? AuthenticationCleartextPassword :\n      type === 5 ? AuthenticationMD5Password :\n      type === 10 ? SASL :\n      type === 11 ? SASLContinue :\n      type === 12 ? SASLFinal :\n      type !== 0 ? UnknownAuth :\n      noop\n    )(x, type)\n  }\n\n  /* c8 ignore next 5 */\n  async function AuthenticationCleartextPassword() {\n    const payload = await Pass()\n    write(\n      b().p().str(payload).z(1).end()\n    )\n  }\n\n  async function AuthenticationMD5Password(x) {\n    const payload = 'md5' + (\n      await md5(\n        Buffer.concat([\n          Buffer.from(await md5((await Pass()) + user)),\n          x.subarray(9)\n        ])\n      )\n    )\n    write(\n      b().p().str(payload).z(1).end()\n    )\n  }\n\n  async function SASL() {\n    nonce = (await crypto.randomBytes(18)).toString('base64')\n    b().p().str('SCRAM-SHA-256' + b.N)\n    const i = b.i\n    write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end())\n  }\n\n  async function SASLContinue(x) {\n    const res = x.toString('utf8', 9).split(',').reduce((acc, x) => (acc[x[0]] = x.slice(2), acc), {})\n\n    const saltedPassword = await crypto.pbkdf2Sync(\n      await Pass(),\n      Buffer.from(res.s, 'base64'),\n      parseInt(res.i), 32,\n      'sha256'\n    )\n\n    const clientKey = await hmac(saltedPassword, 'Client Key')\n\n    const auth = 'n=*,r=' + nonce + ','\n               + 'r=' + res.r + ',s=' + res.s + ',i=' + res.i\n               + ',c=biws,r=' + res.r\n\n    serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64')\n\n    const payload = 'c=biws,r=' + res.r + ',p=' + xor(\n      clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))\n    ).toString('base64')\n\n    write(\n      b().p().str(payload).end()\n    )\n  }\n\n  function SASLFinal(x) {\n    if (x.toString('utf8', 9).split(b.N, 1)[0].slice(2) === serverSignature)\n      return\n    /* c8 ignore next 5 */\n    errored(Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server did not return the correct signature'))\n    socket.destroy()\n  }\n\n  function Pass() {\n    return Promise.resolve(typeof options.pass === 'function'\n      ? options.pass()\n      : options.pass\n    )\n  }\n\n  function NoData() {\n    result.statement = query.statement\n    result.statement.columns = []\n    if (query.onlyDescribe)\n      return (query.resolve(query.statement), write(Sync))\n  }\n\n  function BackendKeyData(x) {\n    backend.pid = x.readUInt32BE(5)\n    backend.secret = x.readUInt32BE(9)\n  }\n\n  async function fetchArrayTypes() {\n    needsTypes = false\n    const types = await new Query([`\n      select b.oid, b.typarray\n      from pg_catalog.pg_type a\n      left join pg_catalog.pg_type b on b.oid = a.typelem\n      where a.typcategory = 'A'\n      group by b.oid, b.typarray\n      order by b.oid\n    `], [], execute)\n    types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))\n  }\n\n  function addArrayType(oid, typarray) {\n    if (!!options.parsers[typarray] && !!options.serializers[typarray]) return\n    const parser = options.parsers[oid]\n    options.shared.typeArrayMap[oid] = typarray\n    options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)\n    options.parsers[typarray].array = true\n    options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)\n  }\n\n  function tryNext(x, xs) {\n    return (\n      (x === 'read-write' && xs.default_transaction_read_only === 'on') ||\n      (x === 'read-only' && xs.default_transaction_read_only === 'off') ||\n      (x === 'primary' && xs.in_hot_standby === 'on') ||\n      (x === 'standby' && xs.in_hot_standby === 'off') ||\n      (x === 'prefer-standby' && xs.in_hot_standby === 'off' && options.host[retries])\n    )\n  }\n\n  function fetchState() {\n    const query = new Query([`\n      show transaction_read_only;\n      select pg_catalog.pg_is_in_recovery()\n    `], [], execute, null, { simple: true })\n    query.resolve = ([[a], [b]]) => {\n      backendParameters.default_transaction_read_only = a.transaction_read_only\n      backendParameters.in_hot_standby = b.pg_is_in_recovery ? 'on' : 'off'\n    }\n    query.execute()\n  }\n\n  function ErrorResponse(x) {\n    if (query) {\n      (query.cursorFn || query.describeFirst) && write(Sync)\n      errorResponse = Errors.postgres(parseError(x))\n    } else {\n      errored(Errors.postgres(parseError(x)))\n    }\n  }\n\n  function retry(q, error) {\n    delete statements[q.signature]\n    q.retried = error\n    execute(q)\n  }\n\n  function NotificationResponse(x) {\n    if (!onnotify)\n      return\n\n    let index = 9\n    while (x[index++] !== 0);\n    onnotify(\n      x.toString('utf8', 9, index - 1),\n      x.toString('utf8', index, x.length - 1)\n    )\n  }\n\n  async function PortalSuspended() {\n    try {\n      const x = await Promise.resolve(query.cursorFn(result))\n      rows = 0\n      x === CLOSE\n        ? write(Close(query.portal))\n        : (result = new Result(), write(Execute('', query.cursorRows)))\n    } catch (err) {\n      write(Sync)\n      query.reject(err)\n    }\n  }\n\n  function CloseComplete() {\n    result.count && query.cursorFn(result)\n    query.resolve(result)\n  }\n\n  function CopyInResponse() {\n    stream = new Stream.Writable({\n      autoDestroy: true,\n      write(chunk, encoding, callback) {\n        socket.write(b().d().raw(chunk).end(), callback)\n      },\n      destroy(error, callback) {\n        callback(error)\n        socket.write(b().f().str(error + b.N).end())\n        stream = null\n      },\n      final(callback) {\n        socket.write(b().c().end())\n        final = callback\n        stream = null\n      }\n    })\n    query.resolve(stream)\n  }\n\n  function CopyOutResponse() {\n    stream = new Stream.Readable({\n      read() { socket.resume() }\n    })\n    query.resolve(stream)\n  }\n\n  /* c8 ignore next 3 */\n  function CopyBothResponse() {\n    stream = new Stream.Duplex({\n      autoDestroy: true,\n      read() { socket.resume() },\n      /* c8 ignore next 11 */\n      write(chunk, encoding, callback) {\n        socket.write(b().d().raw(chunk).end(), callback)\n      },\n      destroy(error, callback) {\n        callback(error)\n        socket.write(b().f().str(error + b.N).end())\n        stream = null\n      },\n      final(callback) {\n        socket.write(b().c().end())\n        final = callback\n      }\n    })\n    query.resolve(stream)\n  }\n\n  function CopyData(x) {\n    stream && (stream.push(x.subarray(5)) || socket.pause())\n  }\n\n  function CopyDone() {\n    stream && stream.push(null)\n    stream = null\n  }\n\n  function NoticeResponse(x) {\n    onnotice\n      ? onnotice(parseError(x))\n      : console.log(parseError(x)) // eslint-disable-line\n\n  }\n\n  /* c8 ignore next 3 */\n  function EmptyQueryResponse() {\n    /* noop */\n  }\n\n  /* c8 ignore next 3 */\n  function FunctionCallResponse() {\n    errored(Errors.notSupported('FunctionCallResponse'))\n  }\n\n  /* c8 ignore next 3 */\n  function NegotiateProtocolVersion() {\n    errored(Errors.notSupported('NegotiateProtocolVersion'))\n  }\n\n  /* c8 ignore next 3 */\n  function UnknownMessage(x) {\n    console.error('Postgres.js : Unknown Message:', x[0]) // eslint-disable-line\n  }\n\n  /* c8 ignore next 3 */\n  function UnknownAuth(x, type) {\n    console.error('Postgres.js : Unknown Auth:', type) // eslint-disable-line\n  }\n\n  /* Messages */\n  function Bind(parameters, types, statement = '', portal = '') {\n    let prev\n      , type\n\n    b().B().str(portal + b.N).str(statement + b.N).i16(0).i16(parameters.length)\n\n    parameters.forEach((x, i) => {\n      if (x === null)\n        return b.i32(0xFFFFFFFF)\n\n      type = types[i]\n      parameters[i] = x = type in options.serializers\n        ? options.serializers[type](x)\n        : '' + x\n\n      prev = b.i\n      b.inc(4).str(x).i32(b.i - prev - 4, prev)\n    })\n\n    b.i16(0)\n\n    return b.end()\n  }\n\n  function Parse(str, parameters, types, name = '') {\n    b().P().str(name + b.N).str(str + b.N).i16(parameters.length)\n    parameters.forEach((x, i) => b.i32(types[i] || 0))\n    return b.end()\n  }\n\n  function Describe(x, name = '') {\n    return b().D().str(x).str(name + b.N).end()\n  }\n\n  function Execute(portal = '', rows = 0) {\n    return Buffer.concat([\n      b().E().str(portal + b.N).i32(rows).end(),\n      Flush\n    ])\n  }\n\n  function Close(portal = '') {\n    return Buffer.concat([\n      b().C().str('P').str(portal + b.N).end(),\n      b().S().end()\n    ])\n  }\n\n  function StartupMessage() {\n    return cancelMessage || b().inc(4).i16(3).z(2).str(\n      Object.entries(Object.assign({\n        user,\n        database,\n        client_encoding: 'UTF8'\n      },\n        options.connection\n      )).filter(([, v]) => v).map(([k, v]) => k + b.N + v).join(b.N)\n    ).z(2).end(0)\n  }\n\n}\n\nfunction parseError(x) {\n  const error = {}\n  let start = 5\n  for (let i = 5; i < x.length - 1; i++) {\n    if (x[i] === 0) {\n      error[errorFields[x[start]]] = x.toString('utf8', start + 1, i)\n      start = i + 1\n    }\n  }\n  return error\n}\n\nfunction md5(x) {\n  return crypto.createHash('md5').update(x).digest('hex')\n}\n\nfunction hmac(key, x) {\n  return crypto.createHmac('sha256', key).update(x).digest()\n}\n\nfunction sha256(x) {\n  return crypto.createHash('sha256').update(x).digest()\n}\n\nfunction xor(a, b) {\n  const length = Math.max(a.length, b.length)\n  const buffer = Buffer.allocUnsafe(length)\n  for (let i = 0; i < length; i++)\n    buffer[i] = a[i] ^ b[i]\n  return buffer\n}\n\nfunction timer(fn, seconds) {\n  seconds = typeof seconds === 'function' ? seconds() : seconds\n  if (!seconds)\n    return { cancel: noop, start: noop }\n\n  let timer\n  return {\n    cancel() {\n      timer && (clearTimeout(timer), timer = null)\n    },\n    start() {\n      timer && clearTimeout(timer)\n      timer = setTimeout(done, seconds * 1000, arguments)\n    }\n  }\n\n  function done(args) {\n    fn.apply(null, args)\n    timer = null\n  }\n}\n"
  },
  {
    "path": "cjs/src/errors.js",
    "content": "const PostgresError = module.exports.PostgresError = class PostgresError extends Error {\n  constructor(x) {\n    super(x.message)\n    this.name = this.constructor.name\n    Object.assign(this, x)\n  }\n}\n\nconst Errors = module.exports.Errors = {\n  connection,\n  postgres,\n  generic,\n  notSupported\n}\n\nfunction connection(x, options, socket) {\n  const { host, port } = socket || options\n  const error = Object.assign(\n    new Error(('write ' + x + ' ' + (options.path || (host + ':' + port)))),\n    {\n      code: x,\n      errno: x,\n      address: options.path || host\n    }, options.path ? {} : { port: port }\n  )\n  Error.captureStackTrace(error, connection)\n  return error\n}\n\nfunction postgres(x) {\n  const error = new PostgresError(x)\n  Error.captureStackTrace(error, postgres)\n  return error\n}\n\nfunction generic(code, message) {\n  const error = Object.assign(new Error(code + ': ' + message), { code })\n  Error.captureStackTrace(error, generic)\n  return error\n}\n\n/* c8 ignore next 10 */\nfunction notSupported(x) {\n  const error = Object.assign(\n    new Error(x + ' (B) is not supported'),\n    {\n      code: 'MESSAGE_NOT_SUPPORTED',\n      name: x\n    }\n  )\n  Error.captureStackTrace(error, notSupported)\n  return error\n}\n"
  },
  {
    "path": "cjs/src/index.js",
    "content": "const os = require('os')\nconst fs = require('fs')\n\nconst {\n  mergeUserTypes,\n  inferType,\n  Parameter,\n  Identifier,\n  Builder,\n  toPascal,\n  pascal,\n  toCamel,\n  camel,\n  toKebab,\n  kebab,\n  fromPascal,\n  fromCamel,\n  fromKebab\n} = require('./types.js')\n\nconst Connection = require('./connection.js')\nconst { Query, CLOSE } = require('./query.js')\nconst Queue = require('./queue.js')\nconst { Errors, PostgresError } = require('./errors.js')\nconst Subscribe = require('./subscribe.js')\nconst largeObject = require('./large.js')\n\nObject.assign(Postgres, {\n  PostgresError,\n  toPascal,\n  pascal,\n  toCamel,\n  camel,\n  toKebab,\n  kebab,\n  fromPascal,\n  fromCamel,\n  fromKebab,\n  BigInt: {\n    to: 20,\n    from: [20],\n    parse: x => BigInt(x), // eslint-disable-line\n    serialize: x => x.toString()\n  }\n})\n\nmodule.exports = Postgres\n\nfunction Postgres(a, b) {\n  const options = parseOptions(a, b)\n      , subscribe = options.no_subscribe || Subscribe(Postgres, { ...options })\n\n  let ending = false\n\n  const queries = Queue()\n      , connecting = Queue()\n      , reserved = Queue()\n      , closed = Queue()\n      , ended = Queue()\n      , open = Queue()\n      , busy = Queue()\n      , full = Queue()\n      , queues = { connecting, reserved, closed, ended, open, busy, full }\n\n  const connections = [...Array(options.max)].map(() => Connection(options, queues, { onopen, onend, onclose }))\n\n  const sql = Sql(handler)\n\n  Object.assign(sql, {\n    get parameters() { return options.parameters },\n    largeObject: largeObject.bind(null, sql),\n    subscribe,\n    CLOSE,\n    END: CLOSE,\n    PostgresError,\n    options,\n    reserve,\n    listen,\n    begin,\n    close,\n    end\n  })\n\n  return sql\n\n  function Sql(handler) {\n    handler.debug = options.debug\n\n    Object.entries(options.types).reduce((acc, [name, type]) => {\n      acc[name] = (x) => new Parameter(x, type.to)\n      return acc\n    }, typed)\n\n    Object.assign(sql, {\n      types: typed,\n      typed,\n      unsafe,\n      notify,\n      array,\n      json,\n      file\n    })\n\n    return sql\n\n    function typed(value, type) {\n      return new Parameter(value, type)\n    }\n\n    function sql(strings, ...args) {\n      const query = strings && Array.isArray(strings.raw)\n        ? new Query(strings, args, handler, cancel)\n        : typeof strings === 'string' && !args.length\n          ? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings)\n          : new Builder(strings, args)\n      return query\n    }\n\n    function unsafe(string, args = [], options = {}) {\n      arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])\n      const query = new Query([string], args, handler, cancel, {\n        prepare: false,\n        ...options,\n        simple: 'simple' in options ? options.simple : args.length === 0\n      })\n      return query\n    }\n\n    function file(path, args = [], options = {}) {\n      arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])\n      const query = new Query([], args, (query) => {\n        fs.readFile(path, 'utf8', (err, string) => {\n          if (err)\n            return query.reject(err)\n\n          query.strings = [string]\n          handler(query)\n        })\n      }, cancel, {\n        ...options,\n        simple: 'simple' in options ? options.simple : args.length === 0\n      })\n      return query\n    }\n  }\n\n  async function listen(name, fn, onlisten) {\n    const listener = { fn, onlisten }\n\n    const sql = listen.sql || (listen.sql = Postgres({\n      ...options,\n      max: 1,\n      idle_timeout: null,\n      max_lifetime: null,\n      fetch_types: false,\n      onclose() {\n        Object.entries(listen.channels).forEach(([name, { listeners }]) => {\n          delete listen.channels[name]\n          Promise.all(listeners.map(l => listen(name, l.fn, l.onlisten).catch(() => { /* noop */ })))\n        })\n      },\n      onnotify(c, x) {\n        c in listen.channels && listen.channels[c].listeners.forEach(l => l.fn(x))\n      }\n    }))\n\n    const channels = listen.channels || (listen.channels = {})\n        , exists = name in channels\n\n    if (exists) {\n      channels[name].listeners.push(listener)\n      const result = await channels[name].result\n      listener.onlisten && listener.onlisten()\n      return { state: result.state, unlisten }\n    }\n\n    channels[name] = { result: sql`listen ${\n      sql.unsafe('\"' + name.replace(/\"/g, '\"\"') + '\"')\n    }`, listeners: [listener] }\n    const result = await channels[name].result\n    listener.onlisten && listener.onlisten()\n    return { state: result.state, unlisten }\n\n    async function unlisten() {\n      if (name in channels === false)\n        return\n\n      channels[name].listeners = channels[name].listeners.filter(x => x !== listener)\n      if (channels[name].listeners.length)\n        return\n\n      delete channels[name]\n      return sql`unlisten ${\n        sql.unsafe('\"' + name.replace(/\"/g, '\"\"') + '\"')\n      }`\n    }\n  }\n\n  async function notify(channel, payload) {\n    return await sql`select pg_notify(${ channel }, ${ '' + payload })`\n  }\n\n  async function reserve() {\n    const queue = Queue()\n    const c = open.length\n      ? open.shift()\n      : await new Promise((resolve, reject) => {\n        const query = { reserve: resolve, reject }\n        queries.push(query)\n        closed.length && connect(closed.shift(), query)\n      })\n\n    move(c, reserved)\n    c.reserved = () => queue.length\n      ? c.execute(queue.shift())\n      : move(c, reserved)\n    c.reserved.release = true\n\n    const sql = Sql(handler)\n    sql.release = () => {\n      c.reserved = null\n      onopen(c)\n    }\n\n    return sql\n\n    function handler(q) {\n      c.queue === full\n        ? queue.push(q)\n        : c.execute(q) || move(c, full)\n    }\n  }\n\n  async function begin(options, fn) {\n    !fn && (fn = options, options = '')\n    const queries = Queue()\n    let savepoints = 0\n      , connection\n      , prepare = null\n\n    try {\n      await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()\n      return await Promise.race([\n        scope(connection, fn),\n        new Promise((_, reject) => connection.onclose = reject)\n      ])\n    } catch (error) {\n      throw error\n    }\n\n    async function scope(c, fn, name) {\n      const sql = Sql(handler)\n      sql.savepoint = savepoint\n      sql.prepare = x => prepare = x.replace(/[^a-z0-9$-_. ]/gi)\n      let uncaughtError\n        , result\n\n      name && await sql`savepoint ${ sql(name) }`\n      try {\n        result = await new Promise((resolve, reject) => {\n          const x = fn(sql)\n          Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)\n        })\n\n        if (uncaughtError)\n          throw uncaughtError\n      } catch (e) {\n        await (name\n          ? sql`rollback to ${ sql(name) }`\n          : sql`rollback`\n        )\n        throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e\n      }\n\n      if (!name) {\n        prepare\n          ? await sql`prepare transaction '${ sql.unsafe(prepare) }'`\n          : await sql`commit`\n      }\n\n      return result\n\n      function savepoint(name, fn) {\n        if (name && Array.isArray(name.raw))\n          return savepoint(sql => sql.apply(sql, arguments))\n\n        arguments.length === 1 && (fn = name, name = null)\n        return scope(c, fn, 's' + savepoints++ + (name ? '_' + name : ''))\n      }\n\n      function handler(q) {\n        q.catch(e => uncaughtError || (uncaughtError = e))\n        c.queue === full\n          ? queries.push(q)\n          : c.execute(q) || move(c, full)\n      }\n    }\n\n    function onexecute(c) {\n      connection = c\n      move(c, reserved)\n      c.reserved = () => queries.length\n        ? c.execute(queries.shift())\n        : move(c, reserved)\n    }\n  }\n\n  function move(c, queue) {\n    c.queue.remove(c)\n    queue.push(c)\n    c.queue = queue\n    queue === open\n      ? c.idleTimer.start()\n      : c.idleTimer.cancel()\n    return c\n  }\n\n  function json(x) {\n    return new Parameter(x, 3802)\n  }\n\n  function array(x, type) {\n    if (!Array.isArray(x))\n      return array(Array.from(arguments))\n\n    return new Parameter(x, type || (x.length ? inferType(x) || 25 : 0), options.shared.typeArrayMap)\n  }\n\n  function handler(query) {\n    if (ending)\n      return query.reject(Errors.connection('CONNECTION_ENDED', options, options))\n\n    if (open.length)\n      return go(open.shift(), query)\n\n    if (closed.length)\n      return connect(closed.shift(), query)\n\n    busy.length\n      ? go(busy.shift(), query)\n      : queries.push(query)\n  }\n\n  function go(c, query) {\n    return c.execute(query)\n      ? move(c, busy)\n      : move(c, full)\n  }\n\n  function cancel(query) {\n    return new Promise((resolve, reject) => {\n      query.state\n        ? query.active\n          ? Connection(options).cancel(query.state, resolve, reject)\n          : query.cancelled = { resolve, reject }\n        : (\n          queries.remove(query),\n          query.cancelled = true,\n          query.reject(Errors.generic('57014', 'canceling statement due to user request')),\n          resolve()\n        )\n    })\n  }\n\n  async function end({ timeout = null } = {}) {\n    if (ending)\n      return ending\n\n    await 1\n    let timer\n    return ending = Promise.race([\n      new Promise(r => timeout !== null && (timer = setTimeout(destroy, timeout * 1000, r))),\n      Promise.all(connections.map(c => c.end()).concat(\n        listen.sql ? listen.sql.end({ timeout: 0 }) : [],\n        subscribe.sql ? subscribe.sql.end({ timeout: 0 }) : []\n      ))\n    ]).then(() => clearTimeout(timer))\n  }\n\n  async function close() {\n    await Promise.all(connections.map(c => c.end()))\n  }\n\n  async function destroy(resolve) {\n    await Promise.all(connections.map(c => c.terminate()))\n    while (queries.length)\n      queries.shift().reject(Errors.connection('CONNECTION_DESTROYED', options))\n    resolve()\n  }\n\n  function connect(c, query) {\n    move(c, connecting)\n    c.connect(query)\n    return c\n  }\n\n  function onend(c) {\n    move(c, ended)\n  }\n\n  function onopen(c) {\n    if (queries.length === 0)\n      return move(c, open)\n\n    let max = Math.ceil(queries.length / (connecting.length + 1))\n      , ready = true\n\n    while (ready && queries.length && max-- > 0) {\n      const query = queries.shift()\n      if (query.reserve)\n        return query.reserve(c)\n\n      ready = c.execute(query)\n    }\n\n    ready\n      ? move(c, busy)\n      : move(c, full)\n  }\n\n  function onclose(c, e) {\n    move(c, closed)\n    c.reserved = null\n    c.onclose && (c.onclose(e), c.onclose = null)\n    options.onclose && options.onclose(c.id)\n    queries.length && connect(c, queries.shift())\n  }\n}\n\nfunction parseOptions(a, b) {\n  if (a && a.shared)\n    return a\n\n  const env = process.env // eslint-disable-line\n      , o = (!a || typeof a === 'string' ? b : a) || {}\n      , { url, multihost } = parseUrl(a)\n      , query = [...url.searchParams].reduce((a, [b, c]) => (a[b] = c, a), {})\n      , host = o.hostname || o.host || multihost || url.hostname || env.PGHOST || 'localhost'\n      , port = o.port || url.port || env.PGPORT || 5432\n      , user = o.user || o.username || url.username || env.PGUSERNAME || env.PGUSER || osUsername()\n\n  o.no_prepare && (o.prepare = false)\n  query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)\n  'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line\n  query.sslrootcert === 'system' && (query.ssl = 'verify-full')\n\n  const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive']\n  const defaults = {\n    max             : globalThis.Cloudflare ? 3 : 10,\n    ssl             : false,\n    sslnegotiation  : null,\n    idle_timeout    : null,\n    connect_timeout : 30,\n    max_lifetime    : max_lifetime,\n    max_pipeline    : 100,\n    backoff         : backoff,\n    keep_alive      : 60,\n    prepare         : true,\n    debug           : false,\n    fetch_types     : true,\n    publications    : 'alltables',\n    target_session_attrs: null\n  }\n\n  return {\n    host            : Array.isArray(host) ? host : host.split(',').map(x => x.split(':')[0]),\n    port            : Array.isArray(port) ? port : host.split(',').map(x => parseInt(x.split(':')[1] || port)),\n    path            : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,\n    database        : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || user,\n    user            : user,\n    pass            : o.pass || o.password || url.password || env.PGPASSWORD || '',\n    ...Object.entries(defaults).reduce(\n      (acc, [k, d]) => {\n        const value = k in o ? o[k] : k in query\n          ? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])\n          : env['PG' + k.toUpperCase()] || d\n        acc[k] = typeof value === 'string' && ints.includes(k)\n          ? +value\n          : value\n        return acc\n      },\n      {}\n    ),\n    connection      : {\n      application_name: env.PGAPPNAME || 'postgres.js',\n      ...o.connection,\n      ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})\n    },\n    types           : o.types || {},\n    target_session_attrs: tsa(o, url, env),\n    onnotice        : o.onnotice,\n    onnotify        : o.onnotify,\n    onclose         : o.onclose,\n    onparameter     : o.onparameter,\n    socket          : o.socket,\n    transform       : parseTransform(o.transform || { undefined: undefined }),\n    parameters      : {},\n    shared          : { retries: 0, typeArrayMap: {} },\n    ...mergeUserTypes(o.types)\n  }\n}\n\nfunction tsa(o, url, env) {\n  const x = o.target_session_attrs || url.searchParams.get('target_session_attrs') || env.PGTARGETSESSIONATTRS\n  if (!x || ['read-write', 'read-only', 'primary', 'standby', 'prefer-standby'].includes(x))\n    return x\n\n  throw new Error('target_session_attrs ' + x + ' is not supported')\n}\n\nfunction backoff(retries) {\n  return (0.5 + Math.random() / 2) * Math.min(3 ** retries / 100, 20)\n}\n\nfunction max_lifetime() {\n  return 60 * (30 + Math.random() * 30)\n}\n\nfunction parseTransform(x) {\n  return {\n    undefined: x.undefined,\n    column: {\n      from: typeof x.column === 'function' ? x.column : x.column && x.column.from,\n      to: x.column && x.column.to\n    },\n    value: {\n      from: typeof x.value === 'function' ? x.value : x.value && x.value.from,\n      to: x.value && x.value.to\n    },\n    row: {\n      from: typeof x.row === 'function' ? x.row : x.row && x.row.from,\n      to: x.row && x.row.to\n    }\n  }\n}\n\nfunction parseUrl(url) {\n  if (!url || typeof url !== 'string')\n    return { url: { searchParams: new Map() } }\n\n  let host = url\n  host = host.slice(host.indexOf('://') + 3).split(/[?/]/)[0]\n  host = decodeURIComponent(host.slice(host.indexOf('@') + 1))\n\n  const urlObj = new URL(url.replace(host, host.split(',')[0]))\n\n  return {\n    url: {\n      username: decodeURIComponent(urlObj.username),\n      password: decodeURIComponent(urlObj.password),\n      host: urlObj.host,\n      hostname: urlObj.hostname,\n      port: urlObj.port,\n      pathname: urlObj.pathname,\n      searchParams: urlObj.searchParams\n    },\n    multihost: host.indexOf(',') > -1 && host\n  }\n}\n\nfunction osUsername() {\n  try {\n    return os.userInfo().username // eslint-disable-line\n  } catch (_) {\n    return process.env.USERNAME || process.env.USER || process.env.LOGNAME  // eslint-disable-line\n  }\n}\n"
  },
  {
    "path": "cjs/src/large.js",
    "content": "const Stream = require('stream')\n\nmodule.exports = largeObject;function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {\n  return new Promise(async(resolve, reject) => {\n    await sql.begin(async sql => {\n      let finish\n      !oid && ([{ oid }] = await sql`select lo_creat(-1) as oid`)\n      const [{ fd }] = await sql`select lo_open(${ oid }, ${ mode }) as fd`\n\n      const lo = {\n        writable,\n        readable,\n        close     : () => sql`select lo_close(${ fd })`.then(finish),\n        tell      : () => sql`select lo_tell64(${ fd })`,\n        read      : (x) => sql`select loread(${ fd }, ${ x }) as data`,\n        write     : (x) => sql`select lowrite(${ fd }, ${ x })`,\n        truncate  : (x) => sql`select lo_truncate64(${ fd }, ${ x })`,\n        seek      : (x, whence = 0) => sql`select lo_lseek64(${ fd }, ${ x }, ${ whence })`,\n        size      : () => sql`\n          select\n            lo_lseek64(${ fd }, location, 0) as position,\n            seek.size\n          from (\n            select\n              lo_lseek64($1, 0, 2) as size,\n              tell.location\n            from (select lo_tell64($1) as location) tell\n          ) seek\n        `\n      }\n\n      resolve(lo)\n\n      return new Promise(async r => finish = r)\n\n      async function readable({\n        highWaterMark = 2048 * 8,\n        start = 0,\n        end = Infinity\n      } = {}) {\n        let max = end - start\n        start && await lo.seek(start)\n        return new Stream.Readable({\n          highWaterMark,\n          async read(size) {\n            const l = size > max ? size - max : size\n            max -= size\n            const [{ data }] = await lo.read(l)\n            this.push(data)\n            if (data.length < size)\n              this.push(null)\n          }\n        })\n      }\n\n      async function writable({\n        highWaterMark = 2048 * 8,\n        start = 0\n      } = {}) {\n        start && await lo.seek(start)\n        return new Stream.Writable({\n          highWaterMark,\n          write(chunk, encoding, callback) {\n            lo.write(chunk).then(() => callback(), callback)\n          }\n        })\n      }\n    }).catch(reject)\n  })\n}\n"
  },
  {
    "path": "cjs/src/query.js",
    "content": "const originCache = new Map()\n    , originStackCache = new Map()\n    , originError = Symbol('OriginError')\n\nconst CLOSE = module.exports.CLOSE = {}\nconst Query = module.exports.Query = class Query extends Promise {\n  constructor(strings, args, handler, canceller, options = {}) {\n    let resolve\n      , reject\n\n    super((a, b) => {\n      resolve = a\n      reject = b\n    })\n\n    this.tagged = Array.isArray(strings.raw)\n    this.strings = strings\n    this.args = args\n    this.handler = handler\n    this.canceller = canceller\n    this.options = options\n\n    this.state = null\n    this.statement = null\n\n    this.resolve = x => (this.active = false, resolve(x))\n    this.reject = x => (this.active = false, reject(x))\n\n    this.active = false\n    this.cancelled = null\n    this.executed = false\n    this.signature = ''\n\n    this[originError] = this.handler.debug\n      ? new Error()\n      : this.tagged && cachedError(this.strings)\n  }\n\n  get origin() {\n    return (this.handler.debug\n      ? this[originError].stack\n      : this.tagged && originStackCache.has(this.strings)\n        ? originStackCache.get(this.strings)\n        : originStackCache.set(this.strings, this[originError].stack).get(this.strings)\n    ) || ''\n  }\n\n  static get [Symbol.species]() {\n    return Promise\n  }\n\n  cancel() {\n    return this.canceller && (this.canceller(this), this.canceller = null)\n  }\n\n  simple() {\n    this.options.simple = true\n    this.options.prepare = false\n    return this\n  }\n\n  async readable() {\n    this.simple()\n    this.streaming = true\n    return this\n  }\n\n  async writable() {\n    this.simple()\n    this.streaming = true\n    return this\n  }\n\n  cursor(rows = 1, fn) {\n    this.options.simple = false\n    if (typeof rows === 'function') {\n      fn = rows\n      rows = 1\n    }\n\n    this.cursorRows = rows\n\n    if (typeof fn === 'function')\n      return (this.cursorFn = fn, this)\n\n    let prev\n    return {\n      [Symbol.asyncIterator]: () => ({\n        next: () => {\n          if (this.executed && !this.active)\n            return { done: true }\n\n          prev && prev()\n          const promise = new Promise((resolve, reject) => {\n            this.cursorFn = value => {\n              resolve({ value, done: false })\n              return new Promise(r => prev = r)\n            }\n            this.resolve = () => (this.active = false, resolve({ done: true }))\n            this.reject = x => (this.active = false, reject(x))\n          })\n          this.execute()\n          return promise\n        },\n        return() {\n          prev && prev(CLOSE)\n          return { done: true }\n        }\n      })\n    }\n  }\n\n  describe() {\n    this.options.simple = false\n    this.onlyDescribe = this.options.prepare = true\n    return this\n  }\n\n  stream() {\n    throw new Error('.stream has been renamed to .forEach')\n  }\n\n  forEach(fn) {\n    this.forEachFn = fn\n    this.handle()\n    return this\n  }\n\n  raw() {\n    this.isRaw = true\n    return this\n  }\n\n  values() {\n    this.isRaw = 'values'\n    return this\n  }\n\n  async handle() {\n    !this.executed && (this.executed = true) && await 1 && this.handler(this)\n  }\n\n  execute() {\n    this.handle()\n    return this\n  }\n\n  then() {\n    this.handle()\n    return super.then.apply(this, arguments)\n  }\n\n  catch() {\n    this.handle()\n    return super.catch.apply(this, arguments)\n  }\n\n  finally() {\n    this.handle()\n    return super.finally.apply(this, arguments)\n  }\n}\n\nfunction cachedError(xs) {\n  if (originCache.has(xs))\n    return originCache.get(xs)\n\n  const x = Error.stackTraceLimit\n  Error.stackTraceLimit = 4\n  originCache.set(xs, new Error())\n  Error.stackTraceLimit = x\n  return originCache.get(xs)\n}\n"
  },
  {
    "path": "cjs/src/queue.js",
    "content": "module.exports = Queue\n\nfunction Queue(initial = []) {\n  let xs = initial.slice()\n  let index = 0\n\n  return {\n    get length() {\n      return xs.length - index\n    },\n    remove: (x) => {\n      const index = xs.indexOf(x)\n      return index === -1\n        ? null\n        : (xs.splice(index, 1), x)\n    },\n    push: (x) => (xs.push(x), x),\n    shift: () => {\n      const out = xs[index++]\n\n      if (index === xs.length) {\n        index = 0\n        xs = []\n      } else {\n        xs[index - 1] = undefined\n      }\n\n      return out\n    }\n  }\n}\n"
  },
  {
    "path": "cjs/src/result.js",
    "content": "module.exports = class Result extends Array {\n  constructor() {\n    super()\n    Object.defineProperties(this, {\n      count: { value: null, writable: true },\n      state: { value: null, writable: true },\n      command: { value: null, writable: true },\n      columns: { value: null, writable: true },\n      statement: { value: null, writable: true }\n    })\n  }\n\n  static get [Symbol.species]() {\n    return Array\n  }\n}\n"
  },
  {
    "path": "cjs/src/subscribe.js",
    "content": "const noop = () => { /* noop */ }\n\nmodule.exports = Subscribe;function Subscribe(postgres, options) {\n  const subscribers = new Map()\n      , slot = 'postgresjs_' + Math.random().toString(36).slice(2)\n      , state = {}\n\n  let connection\n    , stream\n    , ended = false\n\n  const sql = subscribe.sql = postgres({\n    ...options,\n    transform: { column: {}, value: {}, row: {} },\n    max: 1,\n    fetch_types: false,\n    idle_timeout: null,\n    max_lifetime: null,\n    connection: {\n      ...options.connection,\n      replication: 'database'\n    },\n    onclose: async function() {\n      if (ended)\n        return\n      stream = null\n      state.pid = state.secret = undefined\n      connected(await init(sql, slot, options.publications))\n      subscribers.forEach(event => event.forEach(({ onsubscribe }) => onsubscribe()))\n    },\n    no_subscribe: true\n  })\n\n  const end = sql.end\n      , close = sql.close\n\n  sql.end = async() => {\n    ended = true\n    stream && (await new Promise(r => (stream.once('close', r), stream.end())))\n    return end()\n  }\n\n  sql.close = async() => {\n    stream && (await new Promise(r => (stream.once('close', r), stream.end())))\n    return close()\n  }\n\n  return subscribe\n\n  async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {\n    event = parseEvent(event)\n\n    if (!connection)\n      connection = init(sql, slot, options.publications)\n\n    const subscriber = { fn, onsubscribe }\n    const fns = subscribers.has(event)\n      ? subscribers.get(event).add(subscriber)\n      : subscribers.set(event, new Set([subscriber])).get(event)\n\n    const unsubscribe = () => {\n      fns.delete(subscriber)\n      fns.size === 0 && subscribers.delete(event)\n    }\n\n    return connection.then(x => {\n      connected(x)\n      onsubscribe()\n      stream && stream.on('error', onerror)\n      return { unsubscribe, state, sql }\n    })\n  }\n\n  function connected(x) {\n    stream = x.stream\n    state.pid = x.state.pid\n    state.secret = x.state.secret\n  }\n\n  async function init(sql, slot, publications) {\n    if (!publications)\n      throw new Error('Missing publication names')\n\n    const xs = await sql.unsafe(\n      `CREATE_REPLICATION_SLOT ${ slot } TEMPORARY LOGICAL pgoutput NOEXPORT_SNAPSHOT`\n    )\n\n    const [x] = xs\n\n    const stream = await sql.unsafe(\n      `START_REPLICATION SLOT ${ slot } LOGICAL ${\n        x.consistent_point\n      } (proto_version '1', publication_names '${ publications }')`\n    ).writable()\n\n    const state = {\n      lsn: Buffer.concat(x.consistent_point.split('/').map(x => Buffer.from(('00000000' + x).slice(-8), 'hex')))\n    }\n\n    stream.on('data', data)\n    stream.on('error', error)\n    stream.on('close', sql.close)\n\n    return { stream, state: xs.state }\n\n    function error(e) {\n      console.error('Unexpected error during logical streaming - reconnecting', e) // eslint-disable-line\n    }\n\n    function data(x) {\n      if (x[0] === 0x77) {\n        parse(x.subarray(25), state, sql.options.parsers, handle, options.transform)\n      } else if (x[0] === 0x6b && x[17]) {\n        state.lsn = x.subarray(1, 9)\n        pong()\n      }\n    }\n\n    function handle(a, b) {\n      const path = b.relation.schema + '.' + b.relation.table\n      call('*', a, b)\n      call('*:' + path, a, b)\n      b.relation.keys.length && call('*:' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)\n      call(b.command, a, b)\n      call(b.command + ':' + path, a, b)\n      b.relation.keys.length && call(b.command + ':' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)\n    }\n\n    function pong() {\n      const x = Buffer.alloc(34)\n      x[0] = 'r'.charCodeAt(0)\n      x.fill(state.lsn, 1)\n      x.writeBigInt64BE(BigInt(Date.now() - Date.UTC(2000, 0, 1)) * BigInt(1000), 25)\n      stream.write(x)\n    }\n  }\n\n  function call(x, a, b) {\n    subscribers.has(x) && subscribers.get(x).forEach(({ fn }) => fn(a, b, x))\n  }\n}\n\nfunction Time(x) {\n  return new Date(Date.UTC(2000, 0, 1) + Number(x / BigInt(1000)))\n}\n\nfunction parse(x, state, parsers, handle, transform) {\n  const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)\n\n  Object.entries({\n    R: x => {  // Relation\n      let i = 1\n      const r = state[x.readUInt32BE(i)] = {\n        schema: x.toString('utf8', i += 4, i = x.indexOf(0, i)) || 'pg_catalog',\n        table: x.toString('utf8', i + 1, i = x.indexOf(0, i + 1)),\n        columns: Array(x.readUInt16BE(i += 2)),\n        keys: []\n      }\n      i += 2\n\n      let columnIndex = 0\n        , column\n\n      while (i < x.length) {\n        column = r.columns[columnIndex++] = {\n          key: x[i++],\n          name: transform.column.from\n            ? transform.column.from(x.toString('utf8', i, i = x.indexOf(0, i)))\n            : x.toString('utf8', i, i = x.indexOf(0, i)),\n          type: x.readUInt32BE(i += 1),\n          parser: parsers[x.readUInt32BE(i)],\n          atttypmod: x.readUInt32BE(i += 4)\n        }\n\n        column.key && r.keys.push(column)\n        i += 4\n      }\n    },\n    Y: () => { /* noop */ }, // Type\n    O: () => { /* noop */ }, // Origin\n    B: x => { // Begin\n      state.date = Time(x.readBigInt64BE(9))\n      state.lsn = x.subarray(1, 9)\n    },\n    I: x => { // Insert\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      const { row } = tuples(x, relation.columns, i += 7, transform)\n\n      handle(row, {\n        command: 'insert',\n        relation\n      })\n    },\n    D: x => { // Delete\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      i += 4\n      const key = x[i] === 75\n      handle(key || x[i] === 79\n        ? tuples(x, relation.columns, i += 3, transform).row\n        : null\n      , {\n        command: 'delete',\n        relation,\n        key\n      })\n    },\n    U: x => { // Update\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      i += 4\n      const key = x[i] === 75\n      const xs = key || x[i] === 79\n        ? tuples(x, relation.columns, i += 3, transform)\n        : null\n\n      xs && (i = xs.i)\n\n      const { row } = tuples(x, relation.columns, i + 3, transform)\n\n      handle(row, {\n        command: 'update',\n        relation,\n        key,\n        old: xs && xs.row\n      })\n    },\n    T: () => { /* noop */ }, // Truncate,\n    C: () => { /* noop */ }  // Commit\n  }).reduce(char, {})[x[0]](x)\n}\n\nfunction tuples(x, columns, xi, transform) {\n  let type\n    , column\n    , value\n\n  const row = transform.raw ? new Array(columns.length) : {}\n  for (let i = 0; i < columns.length; i++) {\n    type = x[xi++]\n    column = columns[i]\n    value = type === 110 // n\n      ? null\n      : type === 117 // u\n        ? undefined\n        : column.parser === undefined\n          ? x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi))\n          : column.parser.array === true\n            ? column.parser(x.toString('utf8', xi + 5, xi += 4 + x.readUInt32BE(xi)))\n            : column.parser(x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi)))\n\n    transform.raw\n      ? (row[i] = transform.raw === true\n        ? value\n        : transform.value.from ? transform.value.from(value, column) : value)\n      : (row[column.name] = transform.value.from\n        ? transform.value.from(value, column)\n        : value\n      )\n  }\n\n  return { i: xi, row: transform.row.from ? transform.row.from(row) : row }\n}\n\nfunction parseEvent(x) {\n  const xs = x.match(/^(\\*|insert|update|delete)?:?([^.]+?\\.?[^=]+)?=?(.+)?/i) || []\n\n  if (!xs)\n    throw new Error('Malformed subscribe pattern: ' + x)\n\n  const [, command, path, key] = xs\n\n  return (command || '*')\n       + (path ? ':' + (path.indexOf('.') === -1 ? 'public.' + path : path) : '')\n       + (key ? '=' + key : '')\n}\n"
  },
  {
    "path": "cjs/src/types.js",
    "content": "const { Query } = require('./query.js')\nconst { Errors } = require('./errors.js')\n\nconst types = module.exports.types = {\n  string: {\n    to: 25,\n    from: null,             // defaults to string\n    serialize: x => '' + x\n  },\n  number: {\n    to: 0,\n    from: [21, 23, 26, 700, 701],\n    serialize: x => '' + x,\n    parse: x => +x\n  },\n  json: {\n    to: 114,\n    from: [114, 3802],\n    serialize: x => JSON.stringify(x),\n    parse: x => JSON.parse(x)\n  },\n  boolean: {\n    to: 16,\n    from: 16,\n    serialize: x => x === true ? 't' : 'f',\n    parse: x => x === 't'\n  },\n  date: {\n    to: 1184,\n    from: [1082, 1114, 1184],\n    serialize: x => (x instanceof Date ? x : new Date(x)).toISOString(),\n    parse: x => new Date(x)\n  },\n  bytea: {\n    to: 17,\n    from: 17,\n    serialize: x => '\\\\x' + Buffer.from(x).toString('hex'),\n    parse: x => Buffer.from(x.slice(2), 'hex')\n  }\n}\n\nclass NotTagged { then() { notTagged() } catch() { notTagged() } finally() { notTagged() }}\n\nconst Identifier = module.exports.Identifier = class Identifier extends NotTagged {\n  constructor(value) {\n    super()\n    this.value = escapeIdentifier(value)\n  }\n}\n\nconst Parameter = module.exports.Parameter = class Parameter extends NotTagged {\n  constructor(value, type, array) {\n    super()\n    this.value = value\n    this.type = type\n    this.array = array\n  }\n}\n\nconst Builder = module.exports.Builder = class Builder extends NotTagged {\n  constructor(first, rest) {\n    super()\n    this.first = first\n    this.rest = rest\n  }\n\n  build(before, parameters, types, options) {\n    const keyword = builders.map(([x, fn]) => ({ fn, i: before.search(x) })).sort((a, b) => a.i - b.i).pop()\n    return keyword.i === -1\n      ? escapeIdentifiers(this.first, options)\n      : keyword.fn(this.first, this.rest, parameters, types, options)\n  }\n}\n\nmodule.exports.handleValue = handleValue;function handleValue(x, parameters, types, options) {\n  let value = x instanceof Parameter ? x.value : x\n  if (value === undefined) {\n    x instanceof Parameter\n      ? x.value = options.transform.undefined\n      : value = x = options.transform.undefined\n\n    if (value === undefined)\n      throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')\n  }\n\n  return '$' + (types.push(\n    x instanceof Parameter\n      ? (parameters.push(x.value), x.array\n        ? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)\n        : x.type\n      )\n      : (parameters.push(x), inferType(x))\n  ))\n}\n\nconst defaultHandlers = typeHandlers(types)\n\nmodule.exports.stringify = stringify;function stringify(q, string, value, parameters, types, options) { // eslint-disable-line\n  for (let i = 1; i < q.strings.length; i++) {\n    string += (stringifyValue(string, value, parameters, types, options)) + q.strings[i]\n    value = q.args[i]\n  }\n\n  return string\n}\n\nfunction stringifyValue(string, value, parameters, types, o) {\n  return (\n    value instanceof Builder ? value.build(string, parameters, types, o) :\n    value instanceof Query ? fragment(value, parameters, types, o) :\n    value instanceof Identifier ? value.value :\n    value && value[0] instanceof Query ? value.reduce((acc, x) => acc + ' ' + fragment(x, parameters, types, o), '') :\n    handleValue(value, parameters, types, o)\n  )\n}\n\nfunction fragment(q, parameters, types, options) {\n  q.fragment = true\n  return stringify(q, q.strings[0], q.args[0], parameters, types, options)\n}\n\nfunction valuesBuilder(first, parameters, types, columns, options) {\n  return first.map(row =>\n    '(' + columns.map(column =>\n      stringifyValue('values', row[column], parameters, types, options)\n    ).join(',') + ')'\n  ).join(',')\n}\n\nfunction values(first, rest, parameters, types, options) {\n  const multi = Array.isArray(first[0])\n  const columns = rest.length ? rest.flat() : Object.keys(multi ? first[0] : first)\n  return valuesBuilder(multi ? first : [first], parameters, types, columns, options)\n}\n\nfunction select(first, rest, parameters, types, options) {\n  typeof first === 'string' && (first = [first].concat(rest))\n  if (Array.isArray(first))\n    return escapeIdentifiers(first, options)\n\n  let value\n  const columns = rest.length ? rest.flat() : Object.keys(first)\n  return columns.map(x => {\n    value = first[x]\n    return (\n      value instanceof Query ? fragment(value, parameters, types, options) :\n      value instanceof Identifier ? value.value :\n      handleValue(value, parameters, types, options)\n    ) + ' as ' + escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)\n  }).join(',')\n}\n\nconst builders = Object.entries({\n  values,\n  in: (...xs) => {\n    const x = values(...xs)\n    return x === '()' ? '(null)' : x\n  },\n  select,\n  as: select,\n  returning: select,\n  '\\\\(': select,\n\n  update(first, rest, parameters, types, options) {\n    return (rest.length ? rest.flat() : Object.keys(first)).map(x =>\n      escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x) +\n      '=' + stringifyValue('values', first[x], parameters, types, options)\n    )\n  },\n\n  insert(first, rest, parameters, types, options) {\n    const columns = rest.length ? rest.flat() : Object.keys(Array.isArray(first) ? first[0] : first)\n    return '(' + escapeIdentifiers(columns, options) + ')values' +\n    valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, columns, options)\n  }\n}).map(([x, fn]) => ([new RegExp('((?:^|[\\\\s(])' + x + '(?:$|[\\\\s(]))(?![\\\\s\\\\S]*\\\\1)', 'i'), fn]))\n\nfunction notTagged() {\n  throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')\n}\n\nconst serializers = module.exports.serializers = defaultHandlers.serializers\nconst parsers = module.exports.parsers = defaultHandlers.parsers\n\nconst END = module.exports.END = {}\n\nfunction firstIsString(x) {\n  if (Array.isArray(x))\n    return firstIsString(x[0])\n  return typeof x === 'string' ? 1009 : 0\n}\n\nconst mergeUserTypes = module.exports.mergeUserTypes = function(types) {\n  const user = typeHandlers(types || {})\n  return {\n    serializers: Object.assign({}, serializers, user.serializers),\n    parsers: Object.assign({}, parsers, user.parsers)\n  }\n}\n\nfunction typeHandlers(types) {\n  return Object.keys(types).reduce((acc, k) => {\n    types[k].from && [].concat(types[k].from).forEach(x => acc.parsers[x] = types[k].parse)\n    if (types[k].serialize) {\n      acc.serializers[types[k].to] = types[k].serialize\n      types[k].from && [].concat(types[k].from).forEach(x => acc.serializers[x] = types[k].serialize)\n    }\n    return acc\n  }, { parsers: {}, serializers: {} })\n}\n\nfunction escapeIdentifiers(xs, { transform: { column } }) {\n  return xs.map(x => escapeIdentifier(column.to ? column.to(x) : x)).join(',')\n}\n\nconst escapeIdentifier = module.exports.escapeIdentifier = function escape(str) {\n  return '\"' + str.replace(/\"/g, '\"\"').replace(/\\./g, '\".\"') + '\"'\n}\n\nconst inferType = module.exports.inferType = function inferType(x) {\n  return (\n    x instanceof Parameter ? x.type :\n    x instanceof Date ? 1184 :\n    x instanceof Uint8Array ? 17 :\n    (x === true || x === false) ? 16 :\n    typeof x === 'bigint' ? 20 :\n    Array.isArray(x) ? inferType(x[0]) :\n    0\n  )\n}\n\nconst escapeBackslash = /\\\\/g\nconst escapeQuote = /\"/g\n\nfunction arrayEscape(x) {\n  return x\n    .replace(escapeBackslash, '\\\\\\\\')\n    .replace(escapeQuote, '\\\\\"')\n}\n\nconst arraySerializer = module.exports.arraySerializer = function arraySerializer(xs, serializer, options, typarray) {\n  if (Array.isArray(xs) === false)\n    return xs\n\n  if (!xs.length)\n    return '{}'\n\n  const first = xs[0]\n  // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter\n  const delimiter = typarray === 1020 ? ';' : ','\n\n  if (Array.isArray(first) && !first.type)\n    return '{' + xs.map(x => arraySerializer(x, serializer, options, typarray)).join(delimiter) + '}'\n\n  return '{' + xs.map(x => {\n    if (x === undefined) {\n      x = options.transform.undefined\n      if (x === undefined)\n        throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')\n    }\n\n    return x === null\n      ? 'null'\n      : '\"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '\"'\n  }).join(delimiter) + '}'\n}\n\nconst arrayParserState = {\n  i: 0,\n  char: null,\n  str: '',\n  quoted: false,\n  last: 0\n}\n\nconst arrayParser = module.exports.arrayParser = function arrayParser(x, parser, typarray) {\n  arrayParserState.i = arrayParserState.last = 0\n  return arrayParserLoop(arrayParserState, x, parser, typarray)\n}\n\nfunction arrayParserLoop(s, x, parser, typarray) {\n  const xs = []\n  // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter\n  const delimiter = typarray === 1020 ? ';' : ','\n  for (; s.i < x.length; s.i++) {\n    s.char = x[s.i]\n    if (s.quoted) {\n      if (s.char === '\\\\') {\n        s.str += x[++s.i]\n      } else if (s.char === '\"') {\n        xs.push(parser ? parser(s.str) : s.str)\n        s.str = ''\n        s.quoted = x[s.i + 1] === '\"'\n        s.last = s.i + 2\n      } else {\n        s.str += s.char\n      }\n    } else if (s.char === '\"') {\n      s.quoted = true\n    } else if (s.char === '{') {\n      s.last = ++s.i\n      xs.push(arrayParserLoop(s, x, parser, typarray))\n    } else if (s.char === '}') {\n      s.quoted = false\n      s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))\n      s.last = s.i + 1\n      break\n    } else if (s.char === delimiter && s.p !== '}' && s.p !== '\"') {\n      xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))\n      s.last = s.i + 1\n    }\n    s.p = s.char\n  }\n  s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i + 1)) : x.slice(s.last, s.i + 1))\n  return xs\n}\n\nconst toCamel = module.exports.toCamel = x => {\n  let str = x[0]\n  for (let i = 1; i < x.length; i++)\n    str += x[i] === '_' ? x[++i].toUpperCase() : x[i]\n  return str\n}\n\nconst toPascal = module.exports.toPascal = x => {\n  let str = x[0].toUpperCase()\n  for (let i = 1; i < x.length; i++)\n    str += x[i] === '_' ? x[++i].toUpperCase() : x[i]\n  return str\n}\n\nconst toKebab = module.exports.toKebab = x => x.replace(/_/g, '-')\n\nconst fromCamel = module.exports.fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()\nconst fromPascal = module.exports.fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()\nconst fromKebab = module.exports.fromKebab = x => x.replace(/-/g, '_')\n\nfunction createJsonTransform(fn) {\n  return function jsonTransform(x, column) {\n    return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)\n      ? Array.isArray(x)\n        ? x.map(x => jsonTransform(x, column))\n        : Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})\n      : x\n  }\n}\n\ntoCamel.column = { from: toCamel }\ntoCamel.value = { from: createJsonTransform(toCamel) }\nfromCamel.column = { to: fromCamel }\n\nconst camel = module.exports.camel = { ...toCamel }\ncamel.column.to = fromCamel\n\ntoPascal.column = { from: toPascal }\ntoPascal.value = { from: createJsonTransform(toPascal) }\nfromPascal.column = { to: fromPascal }\n\nconst pascal = module.exports.pascal = { ...toPascal }\npascal.column.to = fromPascal\n\ntoKebab.column = { from: toKebab }\ntoKebab.value = { from: createJsonTransform(toKebab) }\nfromKebab.column = { to: fromKebab }\n\nconst kebab = module.exports.kebab = { ...toKebab }\nkebab.column.to = fromKebab\n"
  },
  {
    "path": "cjs/tests/bootstrap.js",
    "content": "const { spawnSync } = require('child_process')\n\nexec('dropdb', ['postgres_js_test'])\n\nexec('psql', ['-c', 'alter system set ssl=on'])\nexec('psql', ['-c', 'drop user postgres_js_test'])\nexec('psql', ['-c', 'create user postgres_js_test'])\nexec('psql', ['-c', 'alter system set password_encryption=md5'])\nexec('psql', ['-c', 'select pg_reload_conf()'])\nexec('psql', ['-c', 'drop user if exists postgres_js_test_md5'])\nexec('psql', ['-c', 'create user postgres_js_test_md5 with password \\'postgres_js_test_md5\\''])\nexec('psql', ['-c', 'alter system set password_encryption=\\'scram-sha-256\\''])\nexec('psql', ['-c', 'select pg_reload_conf()'])\nexec('psql', ['-c', 'drop user if exists postgres_js_test_scram'])\nexec('psql', ['-c', 'create user postgres_js_test_scram with password \\'postgres_js_test_scram\\''])\n\nexec('createdb', ['postgres_js_test'])\nexec('psql', ['-c', 'grant all on database postgres_js_test to postgres_js_test'])\nexec('psql', ['-c', 'alter database postgres_js_test owner to postgres_js_test'])\n\nmodule.exports.exec = exec;function exec(cmd, args) {\n  const { stderr } = spawnSync(cmd, args, { stdio: 'pipe', encoding: 'utf8' })\n  if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist') && !stderr.includes('WARNING:'))\n    throw stderr\n}\n\nasync function execAsync(cmd, args) { // eslint-disable-line\n  let stderr = ''\n  const cp = await spawn(cmd, args, { stdio: 'pipe', encoding: 'utf8' }) // eslint-disable-line\n  cp.stderr.on('data', x => stderr += x)\n  await new Promise(x => cp.on('exit', x))\n  if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist'))\n    throw new Error(stderr)\n}\n"
  },
  {
    "path": "cjs/tests/copy.csv",
    "content": "1\t2\t3\n4\t5\t6\n"
  },
  {
    "path": "cjs/tests/index.js",
    "content": "const { exec } = require('./bootstrap.js')\n\nconst { t, nt, ot } = require('./test.js') // eslint-disable-line\nconst net = require('net')\nconst fs = require('fs')\nconst crypto = require('crypto')\n\nconst postgres = require('../src/index.js')\nconst delay = ms => new Promise(r => setTimeout(r, ms))\n\nconst rel = x => require(\"path\").join(__dirname, x)\nconst idle_timeout = 1\n\nconst login = {\n  user: 'postgres_js_test'\n}\n\nconst login_md5 = {\n  user: 'postgres_js_test_md5',\n  pass: 'postgres_js_test_md5'\n}\n\nconst login_scram = {\n  user: 'postgres_js_test_scram',\n  pass: 'postgres_js_test_scram'\n}\n\nconst options = {\n  db: 'postgres_js_test',\n  user: login.user,\n  pass: login.pass,\n  idle_timeout,\n  connect_timeout: 1,\n  max: 1\n}\n\nconst sql = postgres(options)\n\nt('Connects with no options', async() => {\n  const sql = postgres({ max: 1 })\n\n  const result = (await sql`select 1 as x`)[0].x\n  await sql.end()\n\n  return [1, result]\n})\n\nt('Uses default database without slash', async() => {\n  const sql = postgres('postgres://localhost')\n  return [sql.options.user, sql.options.database]\n})\n\nt('Uses default database with slash', async() => {\n  const sql = postgres('postgres://localhost/')\n  return [sql.options.user, sql.options.database]\n})\n\nt('Result is array', async() =>\n  [true, Array.isArray(await sql`select 1`)]\n)\n\nt('Result has count', async() =>\n  [1, (await sql`select 1`).count]\n)\n\nt('Result has command', async() =>\n  ['SELECT', (await sql`select 1`).command]\n)\n\nt('Create table', async() =>\n  ['CREATE TABLE', (await sql`create table test(int int)`).command, await sql`drop table test`]\n)\n\nt('Drop table', { timeout: 2 }, async() => {\n  await sql`create table test(int int)`\n  return ['DROP TABLE', (await sql`drop table test`).command]\n})\n\nt('null', async() =>\n  [null, (await sql`select ${ null } as x`)[0].x]\n)\n\nt('Integer', async() =>\n  ['1', (await sql`select ${ 1 } as x`)[0].x]\n)\n\nt('String', async() =>\n  ['hello', (await sql`select ${ 'hello' } as x`)[0].x]\n)\n\nt('Boolean false', async() =>\n  [false, (await sql`select ${ false } as x`)[0].x]\n)\n\nt('Boolean true', async() =>\n  [true, (await sql`select ${ true } as x`)[0].x]\n)\n\nt('Date', async() => {\n  const now = new Date()\n  return [0, now - (await sql`select ${ now } as x`)[0].x]\n})\n\nt('Json', async() => {\n  const x = (await sql`select ${ sql.json({ a: 'hello', b: 42 }) } as x`)[0].x\n  return ['hello,42', [x.a, x.b].join()]\n})\n\nt('implicit json', async() => {\n  const x = (await sql`select ${ { a: 'hello', b: 42 } }::json as x`)[0].x\n  return ['hello,42', [x.a, x.b].join()]\n})\n\nt('implicit jsonb', async() => {\n  const x = (await sql`select ${ { a: 'hello', b: 42 } }::jsonb as x`)[0].x\n  return ['hello,42', [x.a, x.b].join()]\n})\n\nt('Empty array', async() =>\n  [true, Array.isArray((await sql`select ${ sql.array([], 1009) } as x`)[0].x)]\n)\n\nt('String array', async() =>\n  ['123', (await sql`select ${ '{1,2,3}' }::int[] as x`)[0].x.join('')]\n)\n\nt('Array of Integer', async() =>\n  ['3', (await sql`select ${ sql.array([1, 2, 3]) } as x`)[0].x[2]]\n)\n\nt('Array of String', async() =>\n  ['c', (await sql`select ${ sql.array(['a', 'b', 'c']) } as x`)[0].x[2]]\n)\n\nt('Array of Date', async() => {\n  const now = new Date()\n  return [now.getTime(), (await sql`select ${ sql.array([now, now, now]) } as x`)[0].x[2].getTime()]\n})\n\nt('Array of Box', async() => [\n  '(3,4),(1,2);(6,7),(4,5)',\n  (await sql`select ${ '{(1,2),(3,4);(4,5),(6,7)}' }::box[] as x`)[0].x.join(';')\n])\n\nt('Nested array n2', async() =>\n  ['4', (await sql`select ${ sql.array([[1, 2], [3, 4]]) } as x`)[0].x[1][1]]\n)\n\nt('Nested array n3', async() =>\n  ['6', (await sql`select ${ sql.array([[[1, 2]], [[3, 4]], [[5, 6]]]) } as x`)[0].x[2][0][1]]\n)\n\nt('Escape in arrays', async() =>\n  ['Hello \"you\",c:\\\\windows', (await sql`select ${ sql.array(['Hello \"you\"', 'c:\\\\windows']) } as x`)[0].x.join(',')]\n)\n\nt('Escapes', async() => {\n  return ['hej\"hej', Object.keys((await sql`select 1 as ${ sql('hej\"hej') }`)[0])[0]]\n})\n\nt('null for int', async() => {\n  await sql`create table test (x int)`\n  return [1, (await sql`insert into test values(${ null })`).count, await sql`drop table test`]\n})\n\nt('Throws on illegal transactions', async() => {\n  const sql = postgres({ ...options, max: 2, fetch_types: false })\n  const error = await sql`begin`.catch(e => e)\n  return [\n    error.code,\n    'UNSAFE_TRANSACTION'\n  ]\n})\n\nt('Transaction throws', async() => {\n  await sql`create table test (a int)`\n  return ['22P02', await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql`insert into test values('hej')`\n  }).catch(x => x.code), await sql`drop table test`]\n})\n\nt('Transaction rolls back', async() => {\n  await sql`create table test (a int)`\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql`insert into test values('hej')`\n  }).catch(() => { /* ignore */ })\n  return [0, (await sql`select a from test`).count, await sql`drop table test`]\n})\n\nt('Transaction throws on uncaught savepoint', async() => {\n  await sql`create table test (a int)`\n\n  return ['fail', (await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoint(async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('fail')\n    })\n  }).catch((err) => err.message)), await sql`drop table test`]\n})\n\nt('Transaction throws on uncaught named savepoint', async() => {\n  await sql`create table test (a int)`\n\n  return ['fail', (await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoit('watpoint', async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('fail')\n    })\n  }).catch(() => 'fail')), await sql`drop table test`]\n})\n\nt('Transaction succeeds on caught savepoint', async() => {\n  await sql`create table test (a int)`\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoint(async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('please rollback')\n    }).catch(() => { /* ignore */ })\n    await sql`insert into test values(3)`\n  })\n\n  return ['2', (await sql`select count(1) from test`)[0].count, await sql`drop table test`]\n})\n\nt('Savepoint returns Result', async() => {\n  let result\n  await sql.begin(async sql => {\n    result = await sql.savepoint(sql =>\n      sql`select 1 as x`\n    )\n  })\n\n  return [1, result[0].x]\n})\n\nt('Prepared transaction', async() => {\n  await sql`create table test (a int)`\n\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.prepare('tx1')\n  })\n\n  await sql`commit prepared 'tx1'`\n\n  return ['1', (await sql`select count(1) from test`)[0].count, await sql`drop table test`]\n})\n\nt('Transaction requests are executed implicitly', async() => {\n  const sql = postgres({ debug: true, idle_timeout: 1, fetch_types: false })\n  return [\n    'testing',\n    (await sql.begin(sql => [\n      sql`select set_config('postgres_js.test', 'testing', true)`,\n      sql`select current_setting('postgres_js.test') as x`\n    ]))[1][0].x\n  ]\n})\n\nt('Uncaught transaction request errors bubbles to transaction', async() => [\n  '42703',\n  (await sql.begin(sql => [\n    sql`select wat`,\n    sql`select current_setting('postgres_js.test') as x, ${ 1 } as a`\n  ]).catch(e => e.code))\n])\n\nt('Fragments in transactions', async() => [\n  true,\n  (await sql.begin(sql => sql`select true as x where ${ sql`1=1` }`))[0].x\n])\n\nt('Transaction rejects with rethrown error', async() => [\n  'WAT',\n  await sql.begin(async sql => {\n    try {\n      await sql`select exception`\n    } catch (ex) {\n      throw new Error('WAT')\n    }\n  }).catch(e => e.message)\n])\n\nt('Parallel transactions', async() => {\n  await sql`create table test (a int)`\n  return ['11', (await Promise.all([\n    sql.begin(sql => sql`select 1`),\n    sql.begin(sql => sql`select 1`)\n  ])).map(x => x.count).join(''), await sql`drop table test`]\n})\n\nt('Many transactions at beginning of connection', async() => {\n  const sql = postgres(options)\n  const xs = await Promise.all(Array.from({ length: 100 }, () => sql.begin(sql => sql`select 1`)))\n  return [100, xs.length]\n})\n\nt('Transactions array', async() => {\n  await sql`create table test (a int)`\n\n  return ['11', (await sql.begin(sql => [\n    sql`select 1`.then(x => x),\n    sql`select 1`\n  ])).map(x => x.count).join(''), await sql`drop table test`]\n})\n\nt('Transaction waits', async() => {\n  await sql`create table test (a int)`\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoint(async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('please rollback')\n    }).catch(() => { /* ignore */ })\n    await sql`insert into test values(3)`\n  })\n\n  return ['11', (await Promise.all([\n    sql.begin(sql => sql`select 1`),\n    sql.begin(sql => sql`select 1`)\n  ])).map(x => x.count).join(''), await sql`drop table test`]\n})\n\nt('Helpers in Transaction', async() => {\n  return ['1', (await sql.begin(async sql =>\n    await sql`select ${ sql({ x: 1 }) }`\n  ))[0].x]\n})\n\nt('Undefined values throws', async() => {\n  let error\n\n  await sql`\n    select ${ undefined } as x\n  `.catch(x => error = x.code)\n\n  return ['UNDEFINED_VALUE', error]\n})\n\nt('Transform undefined', async() => {\n  const sql = postgres({ ...options, transform: { undefined: null } })\n  return [null, (await sql`select ${ undefined } as x`)[0].x]\n})\n\nt('Transform undefined in array', async() => {\n  const sql = postgres({ ...options, transform: { undefined: null } })\n  return [null, (await sql`select * from (values ${ sql([undefined, undefined]) }) as x(x, y)`)[0].y]\n})\n\nt('Null sets to null', async() =>\n  [null, (await sql`select ${ null } as x`)[0].x]\n)\n\nt('Throw syntax error', async() =>\n  ['42601', (await sql`wat 1`.catch(x => x)).code]\n)\n\nt('Connect using uri', async() =>\n  [true, await new Promise((resolve, reject) => {\n    const sql = postgres('postgres://' + login.user + ':' + (login.pass || '') + '@localhost:5432/' + options.db, {\n      idle_timeout\n    })\n    sql`select 1`.then(() => resolve(true), reject)\n  })]\n)\n\nt('Options from uri with special characters in user and pass', async() => {\n  const opt = postgres({ user: 'öla', pass: 'pass^word' }).options\n  return [[opt.user, opt.pass].toString(), 'öla,pass^word']\n})\n\nt('Fail with proper error on no host', async() =>\n  ['ECONNREFUSED', (await new Promise((resolve, reject) => {\n    const sql = postgres('postgres://localhost:33333/' + options.db, {\n      idle_timeout\n    })\n    sql`select 1`.then(reject, resolve)\n  })).code]\n)\n\nt('Connect using SSL', async() =>\n  [true, (await new Promise((resolve, reject) => {\n    postgres({\n      ssl: { rejectUnauthorized: false },\n      idle_timeout\n    })`select 1`.then(() => resolve(true), reject)\n  }))]\n)\n\nt('Connect using SSL require', async() =>\n  [true, (await new Promise((resolve, reject) => {\n    postgres({\n      ssl: 'require',\n      idle_timeout\n    })`select 1`.then(() => resolve(true), reject)\n  }))]\n)\n\nt('Connect using SSL direct', async() => {\n  const [{ supported }] = await sql`select current_setting('server_version_num')::int >= 180000 as supported`\n  return [true, !supported || (await new Promise((resolve, reject) => {\n    postgres({\n      ssl: 'require',\n      sslnegotiation: 'direct',\n      idle_timeout\n    })`select 1`.then(() => resolve(true), reject)\n  }))]\n})\n\nt('Connect using SSL prefer', async() => {\n  await exec('psql', ['-c', 'alter system set ssl=off'])\n  await exec('psql', ['-c', 'select pg_reload_conf()'])\n\n  const sql = postgres({\n    ssl: 'prefer',\n    idle_timeout\n  })\n\n  return [\n    1, (await sql`select 1 as x`)[0].x,\n    await exec('psql', ['-c', 'alter system set ssl=on']),\n    await exec('psql', ['-c', 'select pg_reload_conf()'])\n  ]\n})\n\nt('Reconnect using SSL', { timeout: 2 }, async() => {\n  const sql = postgres({\n    ssl: 'require',\n    idle_timeout: 0.1\n  })\n\n  await sql`select 1`\n  await delay(200)\n\n  return [1, (await sql`select 1 as x`)[0].x]\n})\n\nt('Proper handling of non object Errors', async() => {\n  const sql = postgres({ socket: () => { throw 'wat' } }) // eslint-disable-line\n\n  return [\n    'wat', await sql`select 1 as x`.catch(e => e.message)\n  ]\n})\n\nt('Proper handling of null Errors', async() => {\n  const sql = postgres({ socket: () => { throw null } }) // eslint-disable-line\n\n  return [\n    'null', await sql`select 1 as x`.catch(e => e.message)\n  ]\n})\n\nt('Ensure reserve on connection throws proper error', async() => {\n  const sql = postgres({ socket: () => { throw 'wat' }, idle_timeout }) // eslint-disable-line\n\n  return [\n    'wat', await sql.reserve().catch(e => e)\n  ]\n})\n\nt('Login without password', async() => {\n  return [true, (await postgres({ ...options, ...login })`select true as x`)[0].x]\n})\n\nt('Login using MD5', async() => {\n  return [true, (await postgres({ ...options, ...login_md5 })`select true as x`)[0].x]\n})\n\nt('Login using scram-sha-256', async() => {\n  return [true, (await postgres({ ...options, ...login_scram })`select true as x`)[0].x]\n})\n\nt('Parallel connections using scram-sha-256', {\n  timeout: 2\n}, async() => {\n  const sql = postgres({ ...options, ...login_scram })\n  return [true, (await Promise.all([\n    sql`select true as x, pg_sleep(0.01)`,\n    sql`select true as x, pg_sleep(0.01)`,\n    sql`select true as x, pg_sleep(0.01)`\n  ]))[0][0].x]\n})\n\nt('Support dynamic password function', async() => {\n  return [true, (await postgres({\n    ...options,\n    ...login_scram,\n    pass: () => 'postgres_js_test_scram'\n  })`select true as x`)[0].x]\n})\n\nt('Support dynamic async password function', async() => {\n  return [true, (await postgres({\n    ...options,\n    ...login_scram,\n    pass: () => Promise.resolve('postgres_js_test_scram')\n  })`select true as x`)[0].x]\n})\n\nt('Point type', async() => {\n  const sql = postgres({\n    ...options,\n    types: {\n      point: {\n        to: 600,\n        from: [600],\n        serialize: ([x, y]) => '(' + x + ',' + y + ')',\n        parse: (x) => x.slice(1, -1).split(',').map(x => +x)\n      }\n    }\n  })\n\n  await sql`create table test (x point)`\n  await sql`insert into test (x) values (${ sql.types.point([10, 20]) })`\n  return [20, (await sql`select x from test`)[0].x[1], await sql`drop table test`]\n})\n\nt('Point type array', async() => {\n  const sql = postgres({\n    ...options,\n    types: {\n      point: {\n        to: 600,\n        from: [600],\n        serialize: ([x, y]) => '(' + x + ',' + y + ')',\n        parse: (x) => x.slice(1, -1).split(',').map(x => +x)\n      }\n    }\n  })\n\n  await sql`create table test (x point[])`\n  await sql`insert into test (x) values (${ sql.array([sql.types.point([10, 20]), sql.types.point([20, 30])]) })`\n  return [30, (await sql`select x from test`)[0].x[1][1], await sql`drop table test`]\n})\n\nt('sql file', async() =>\n  [1, (await sql.file(rel('select.sql')))[0].x]\n)\n\nt('sql file has forEach', async() => {\n  let result\n  await sql\n    .file(rel('select.sql'), { cache: false })\n    .forEach(({ x }) => result = x)\n\n  return [1, result]\n})\n\nt('sql file throws', async() =>\n  ['ENOENT', (await sql.file(rel('selectomondo.sql')).catch(x => x.code))]\n)\n\nt('sql file cached', async() => {\n  await sql.file(rel('select.sql'))\n  await delay(20)\n\n  return [1, (await sql.file(rel('select.sql')))[0].x]\n})\n\nt('Parameters in file', async() => {\n  const result = await sql.file(\n    rel('select-param.sql'),\n    ['hello']\n  )\n  return ['hello', result[0].x]\n})\n\nt('Connection ended promise', async() => {\n  const sql = postgres(options)\n\n  await sql.end()\n\n  return [undefined, await sql.end()]\n})\n\nt('Connection ended timeout', async() => {\n  const sql = postgres(options)\n\n  await sql.end({ timeout: 10 })\n\n  return [undefined, await sql.end()]\n})\n\nt('Connection ended error', async() => {\n  const sql = postgres(options)\n  await sql.end()\n  return ['CONNECTION_ENDED', (await sql``.catch(x => x.code))]\n})\n\nt('Connection end does not cancel query', async() => {\n  const sql = postgres(options)\n\n  const promise = sql`select 1 as x`.execute()\n\n  await sql.end()\n\n  return [1, (await promise)[0].x]\n})\n\nt('Connection destroyed', async() => {\n  const sql = postgres(options)\n  process.nextTick(() => sql.end({ timeout: 0 }))\n  return ['CONNECTION_DESTROYED', await sql``.catch(x => x.code)]\n})\n\nt('Connection destroyed with query before', async() => {\n  const sql = postgres(options)\n      , error = sql`select pg_sleep(0.2)`.catch(err => err.code)\n\n  sql.end({ timeout: 0 })\n  return ['CONNECTION_DESTROYED', await error]\n})\n\nt('transform column', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: x => x.split('').reverse().join('') }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['dlrow_olleh', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('column toPascal', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: postgres.toPascal }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['HelloWorld', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('column toCamel', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: postgres.toCamel }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['helloWorld', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('column toKebab', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: postgres.toKebab }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['hello-world', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('Transform nested json in arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  return ['aBcD', (await sql`select '[{\"a_b\":1},{\"c_d\":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')]\n})\n\nt('Transform deeply nested json object in arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  return [\n    'childObj_deeplyNestedObj_grandchildObj',\n    (await sql`\n      select '[{\"nested_obj\": {\"child_obj\": 2, \"deeply_nested_obj\": {\"grandchild_obj\": 3}}}]'::jsonb as x\n    `)[0].x.map(x => {\n      let result\n      for (const key in x)\n        result = [...Object.keys(x[key]), ...Object.keys(x[key].deeplyNestedObj)]\n      return result\n    })[0]\n    .join('_')\n  ]\n})\n\nt('Transform deeply nested json array in arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  return [\n    'childArray_deeplyNestedArray_grandchildArray',\n    (await sql`\n      select '[{\"nested_array\": [{\"child_array\": 2, \"deeply_nested_array\": [{\"grandchild_array\":3}]}]}]'::jsonb AS x\n    `)[0].x.map((x) => {\n      let result\n      for (const key in x)\n        result = [...Object.keys(x[key][0]), ...Object.keys(x[key][0].deeplyNestedArray[0])]\n      return result\n    })[0]\n    .join('_')\n  ]\n})\n\nt('Bypass transform for json primitive', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n\n  const x = (\n    await sql`select 'null'::json as a, 'false'::json as b, '\"a\"'::json as c, '1'::json as d`\n  )[0]\n\n  return [\n    JSON.stringify({ a: null, b: false, c: 'a', d: 1 }),\n    JSON.stringify(x)\n  ]\n})\n\nt('Bypass transform for jsonb primitive', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n\n  const x = (\n    await sql`select 'null'::jsonb as a, 'false'::jsonb as b, '\"a\"'::jsonb as c, '1'::jsonb as d`\n  )[0]\n\n  return [\n    JSON.stringify({ a: null, b: false, c: 'a', d: 1 }),\n    JSON.stringify(x)\n  ]\n})\n\nt('unsafe', async() => {\n  await sql`create table test (x int)`\n  return [1, (await sql.unsafe('insert into test values ($1) returning *', [1]))[0].x, await sql`drop table test`]\n})\n\nt('unsafe simple', async() => {\n  return [1, (await sql.unsafe('select 1 as x'))[0].x]\n})\n\nt('unsafe simple includes columns', async() => {\n  return ['x', (await sql.unsafe('select 1 as x').values()).columns[0].name]\n})\n\nt('unsafe describe', async() => {\n  const q = 'insert into test values (1)'\n  await sql`create table test(a int unique)`\n  await sql.unsafe(q).describe()\n  const x = await sql.unsafe(q).describe()\n  return [\n    q,\n    x.string,\n    await sql`drop table test`\n  ]\n})\n\nt('simple query using unsafe with multiple statements', async() => {\n  return [\n    '1,2',\n    (await sql.unsafe('select 1 as x;select 2 as x')).map(x => x[0].x).join()\n  ]\n})\n\nt('simple query using simple() with multiple statements', async() => {\n  return [\n    '1,2',\n    (await sql`select 1 as x;select 2 as x`.simple()).map(x => x[0].x).join()\n  ]\n})\n\nt('listen and notify', async() => {\n  const sql = postgres(options)\n  const channel = 'hello'\n  const result = await new Promise(async r => {\n    await sql.listen(channel, r)\n    sql.notify(channel, 'works')\n  })\n\n  return [\n    'works',\n    result,\n    sql.end()\n  ]\n})\n\nt('double listen', async() => {\n  const sql = postgres(options)\n      , channel = 'hello'\n\n  let count = 0\n\n  await new Promise((resolve, reject) =>\n    sql.listen(channel, resolve)\n    .then(() => sql.notify(channel, 'world'))\n    .catch(reject)\n  ).then(() => count++)\n\n  await new Promise((resolve, reject) =>\n    sql.listen(channel, resolve)\n    .then(() => sql.notify(channel, 'world'))\n    .catch(reject)\n  ).then(() => count++)\n\n  // for coverage\n  sql.listen('weee', () => { /* noop */ }).then(sql.end)\n\n  return [2, count]\n})\n\nt('multiple listeners work after a reconnect', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const s1 = await sql.listen('test', x => xs.push('1', x))\n  await sql.listen('test', x => xs.push('2', x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await sql`select pg_terminate_backend(${ s1.state.pid })`\n  await delay(200)\n  await sql.notify('test', 'b')\n  await delay(50)\n  sql.end()\n\n  return ['1a2a1b2b', xs.join('')]\n})\n\nt('listen and notify with weird name', async() => {\n  const sql = postgres(options)\n  const channel = 'wat-;.ø.§'\n  const result = await new Promise(async r => {\n    const { unlisten } = await sql.listen(channel, r)\n    sql.notify(channel, 'works')\n    await delay(50)\n    await unlisten()\n  })\n\n  return [\n    'works',\n    result,\n    sql.end()\n  ]\n})\n\nt('listen and notify with upper case', async() => {\n  const sql = postgres(options)\n  const channel = 'withUpperChar'\n  const result = await new Promise(async r => {\n    await sql.listen(channel, r)\n    sql.notify(channel, 'works')\n  })\n\n  return [\n    'works',\n    result,\n    sql.end()\n  ]\n})\n\nt('listen reconnects', { timeout: 2 }, async() => {\n  const sql = postgres(options)\n      , resolvers = {}\n      , a = new Promise(r => resolvers.a = r)\n      , b = new Promise(r => resolvers.b = r)\n\n  let connects = 0\n\n  const { state: { pid } } = await sql.listen(\n    'test',\n    x => x in resolvers && resolvers[x](),\n    () => connects++\n  )\n  await sql.notify('test', 'a')\n  await a\n  await sql`select pg_terminate_backend(${ pid })`\n  await delay(100)\n  await sql.notify('test', 'b')\n  await b\n  sql.end()\n  return [connects, 2]\n})\n\nt('listen result reports correct connection state after reconnection', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const result = await sql.listen('test', x => xs.push(x))\n  const initialPid = result.state.pid\n  await sql.notify('test', 'a')\n  await sql`select pg_terminate_backend(${ initialPid })`\n  await delay(50)\n  sql.end()\n\n  return [result.state.pid !== initialPid, true]\n})\n\nt('unlisten removes subscription', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const { unlisten } = await sql.listen('test', x => xs.push(x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await unlisten()\n  await sql.notify('test', 'b')\n  await delay(50)\n  sql.end()\n\n  return ['a', xs.join('')]\n})\n\nt('listen after unlisten', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const { unlisten } = await sql.listen('test', x => xs.push(x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await unlisten()\n  await sql.notify('test', 'b')\n  await delay(50)\n  await sql.listen('test', x => xs.push(x))\n  await sql.notify('test', 'c')\n  await delay(50)\n  sql.end()\n\n  return ['ac', xs.join('')]\n})\n\nt('multiple listeners and unlisten one', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  await sql.listen('test', x => xs.push('1', x))\n  const s2 = await sql.listen('test', x => xs.push('2', x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await s2.unlisten()\n  await sql.notify('test', 'b')\n  await delay(50)\n  sql.end()\n\n  return ['1a2a1b', xs.join('')]\n})\n\nt('responds with server parameters (application_name)', async() =>\n  ['postgres.js', await new Promise((resolve, reject) => postgres({\n    ...options,\n    onparameter: (k, v) => k === 'application_name' && resolve(v)\n  })`select 1`.catch(reject))]\n)\n\nt('has server parameters', async() => {\n  return ['postgres.js', (await sql`select 1`.then(() => sql.parameters.application_name))]\n})\n\nt('big query body', { timeout: 2 }, async() => {\n  await sql`create table test (x int)`\n  return [50000, (await sql`insert into test ${\n    sql([...Array(50000).keys()].map(x => ({ x })))\n  }`).count, await sql`drop table test`]\n})\n\nt('Throws if more than 65534 parameters', async() => {\n  await sql`create table test (x int)`\n  return ['MAX_PARAMETERS_EXCEEDED', (await sql`insert into test ${\n    sql([...Array(65535).keys()].map(x => ({ x })))\n  }`.catch(e => e.code)), await sql`drop table test`]\n})\n\nt('let postgres do implicit cast of unknown types', async() => {\n  await sql`create table test (x timestamp with time zone)`\n  const [{ x }] = await sql`insert into test values (${ new Date().toISOString() }) returning *`\n  return [true, x instanceof Date, await sql`drop table test`]\n})\n\nt('only allows one statement', async() =>\n  ['42601', await sql`select 1; select 2`.catch(e => e.code)]\n)\n\nt('await sql() throws not tagged error', async() => {\n  let error\n  try {\n    await sql('select 1')\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('sql().then throws not tagged error', async() => {\n  let error\n  try {\n    sql('select 1').then(() => { /* noop */ })\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('sql().catch throws not tagged error', async() => {\n  let error\n  try {\n    await sql('select 1')\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('sql().finally throws not tagged error', async() => {\n  let error\n  try {\n    sql('select 1').finally(() => { /* noop */ })\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('little bobby tables', async() => {\n  const name = 'Robert\\'); DROP TABLE students;--'\n\n  await sql`create table students (name text, age int)`\n  await sql`insert into students (name) values (${ name })`\n\n  return [\n    name, (await sql`select name from students`)[0].name,\n    await sql`drop table students`\n  ]\n})\n\nt('Connection errors are caught using begin()', {\n  timeout: 2\n}, async() => {\n  let error\n  try {\n    const sql = postgres({ host: 'localhost', port: 1 })\n\n    await sql.begin(async(sql) => {\n      await sql`insert into test (label, value) values (${1}, ${2})`\n    })\n  } catch (err) {\n    error = err\n  }\n\n  return [\n    true,\n    error.code === 'ECONNREFUSED' ||\n    error.message === 'Connection refused (os error 61)'\n  ]\n})\n\nt('dynamic table name', async() => {\n  await sql`create table test(a int)`\n  return [\n    0, (await sql`select * from ${ sql('test') }`).count,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic schema name', async() => {\n  await sql`create table test(a int)`\n  return [\n    0, (await sql`select * from ${ sql('public') }.test`).count,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic schema and table name', async() => {\n  await sql`create table test(a int)`\n  return [\n    0, (await sql`select * from ${ sql('public.test') }`).count,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic column name', async() => {\n  return ['!not_valid', Object.keys((await sql`select 1 as ${ sql('!not_valid') }`)[0])[0]]\n})\n\nt('dynamic select as', async() => {\n  return ['2', (await sql`select ${ sql({ a: 1, b: 2 }) }`)[0].b]\n})\n\nt('dynamic select as pluck', async() => {\n  return [undefined, (await sql`select ${ sql({ a: 1, b: 2 }, 'a') }`)[0].b]\n})\n\nt('dynamic insert', async() => {\n  await sql`create table test (a int, b text)`\n  const x = { a: 42, b: 'the answer' }\n\n  return ['the answer', (await sql`insert into test ${ sql(x) } returning *`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic insert pluck', async() => {\n  await sql`create table test (a int, b text)`\n  const x = { a: 42, b: 'the answer' }\n\n  return [null, (await sql`insert into test ${ sql(x, 'a') } returning *`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic in with empty array', async() => {\n  await sql`create table test (a int)`\n  await sql`insert into test values (1)`\n  return [\n    (await sql`select * from test where null in ${ sql([]) }`).count,\n    0,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic in after insert', async() => {\n  await sql`create table test (a int, b text)`\n  const [{ x }] = await sql`\n    with x as (\n      insert into test values (1, 'hej')\n      returning *\n    )\n    select 1 in ${ sql([1, 2, 3]) } as x from x\n  `\n  return [\n    true, x,\n    await sql`drop table test`\n  ]\n})\n\nt('array insert', async() => {\n  await sql`create table test (a int, b int)`\n  return [2, (await sql`insert into test (a, b) values ${ sql([1, 2]) } returning *`)[0].b, await sql`drop table test`]\n})\n\nt('where parameters in()', async() => {\n  await sql`create table test (x text)`\n  await sql`insert into test values ('a')`\n  return [\n    (await sql`select * from test where x in ${ sql(['a', 'b', 'c']) }`)[0].x,\n    'a',\n    await sql`drop table test`\n  ]\n})\n\nt('where parameters in() values before', async() => {\n  return [2, (await sql`\n    with rows as (\n      select * from (values (1), (2), (3), (4)) as x(a)\n    )\n    select * from rows where a in ${ sql([3, 4]) }\n  `).count]\n})\n\nt('dynamic multi row insert', async() => {\n  await sql`create table test (a int, b text)`\n  const x = { a: 42, b: 'the answer' }\n\n  return [\n    'the answer',\n    (await sql`insert into test ${ sql([x, x]) } returning *`)[1].b, await sql`drop table test`\n  ]\n})\n\nt('dynamic update', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (17, 'wrong')`\n\n  return [\n    'the answer',\n    (await sql`update test set ${ sql({ a: 42, b: 'the answer' }) } returning *`)[0].b, await sql`drop table test`\n  ]\n})\n\nt('dynamic update pluck', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (17, 'wrong')`\n\n  return [\n    'wrong',\n    (await sql`update test set ${ sql({ a: 42, b: 'the answer' }, 'a') } returning *`)[0].b, await sql`drop table test`\n  ]\n})\n\nt('dynamic select array', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (42, 'yay')`\n  return ['yay', (await sql`select ${ sql(['a', 'b']) } from test`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic returning array', async() => {\n  await sql`create table test (a int, b text)`\n  return [\n    'yay',\n    (await sql`insert into test (a, b) values (42, 'yay') returning ${ sql(['a', 'b']) }`)[0].b,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic select args', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (42, 'yay')`\n  return ['yay', (await sql`select ${ sql('a', 'b') } from test`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic values single row', async() => {\n  const [{ b }] = await sql`\n    select * from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)\n  `\n\n  return ['b', b]\n})\n\nt('dynamic values multi row', async() => {\n  const [, { b }] = await sql`\n    select * from (values ${ sql([['a', 'b', 'c'], ['a', 'b', 'c']]) }) as x(a, b, c)\n  `\n\n  return ['b', b]\n})\n\nt('connection parameters', async() => {\n  const sql = postgres({\n    ...options,\n    connection: {\n      'some.var': 'yay'\n    }\n  })\n\n  return ['yay', (await sql`select current_setting('some.var') as x`)[0].x]\n})\n\nt('Multiple queries', async() => {\n  const sql = postgres(options)\n\n  return [4, (await Promise.all([\n    sql`select 1`,\n    sql`select 2`,\n    sql`select 3`,\n    sql`select 4`\n  ])).length]\n})\n\nt('Multiple statements', async() =>\n  [2, await sql.unsafe(`\n    select 1 as x;\n    select 2 as a;\n  `).then(([, [x]]) => x.a)]\n)\n\nt('throws correct error when authentication fails', async() => {\n  const sql = postgres({\n    ...options,\n    ...login_md5,\n    pass: 'wrong'\n  })\n  return ['28P01', await sql`select 1`.catch(e => e.code)]\n})\n\nt('notice', async() => {\n  let notice\n  const log = console.log // eslint-disable-line\n  console.log = function(x) { // eslint-disable-line\n    notice = x\n  }\n\n  const sql = postgres(options)\n\n  await sql`create table if not exists users()`\n  await sql`create table if not exists users()`\n\n  console.log = log // eslint-disable-line\n\n  return ['NOTICE', notice.severity]\n})\n\nt('notice hook', async() => {\n  let notice\n  const sql = postgres({\n    ...options,\n    onnotice: x => notice = x\n  })\n\n  await sql`create table if not exists users()`\n  await sql`create table if not exists users()`\n\n  return ['NOTICE', notice.severity]\n})\n\nt('bytea serializes and parses', async() => {\n  const buf = Buffer.from('wat')\n\n  await sql`create table test (x bytea)`\n  await sql`insert into test values (${ buf })`\n\n  return [\n    buf.toString(),\n    (await sql`select x from test`)[0].x.toString(),\n    await sql`drop table test`\n  ]\n})\n\nt('forEach', async() => {\n  let result\n  await sql`select 1 as x`.forEach(({ x }) => result = x)\n  return [1, result]\n})\n\nt('forEach returns empty array', async() => {\n  return [0, (await sql`select 1 as x`.forEach(() => { /* noop */ })).length]\n})\n\nt('Cursor', async() => {\n  const order = []\n  await sql`select 1 as x union select 2 as x`.cursor(async([x]) => {\n    order.push(x.x + 'a')\n    await delay(100)\n    order.push(x.x + 'b')\n  })\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Unsafe cursor', async() => {\n  const order = []\n  await sql.unsafe('select 1 as x union select 2 as x').cursor(async([x]) => {\n    order.push(x.x + 'a')\n    await delay(100)\n    order.push(x.x + 'b')\n  })\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Cursor custom n', async() => {\n  const order = []\n  await sql`select * from generate_series(1,20)`.cursor(10, async(x) => {\n    order.push(x.length)\n  })\n  return ['10,10', order.join(',')]\n})\n\nt('Cursor custom with rest n', async() => {\n  const order = []\n  await sql`select * from generate_series(1,20)`.cursor(11, async(x) => {\n    order.push(x.length)\n  })\n  return ['11,9', order.join(',')]\n})\n\nt('Cursor custom with less results than batch size', async() => {\n  const order = []\n  await sql`select * from generate_series(1,20)`.cursor(21, async(x) => {\n    order.push(x.length)\n  })\n  return ['20', order.join(',')]\n})\n\nt('Cursor cancel', async() => {\n  let result\n  await sql`select * from generate_series(1,10) as x`.cursor(async([{ x }]) => {\n    result = x\n    return sql.CLOSE\n  })\n  return [1, result]\n})\n\nt('Cursor throw', async() => {\n  const order = []\n  await sql`select 1 as x union select 2 as x`.cursor(async([x]) => {\n    order.push(x.x + 'a')\n    await delay(100)\n    throw new Error('watty')\n  }).catch(() => order.push('err'))\n  return ['1aerr', order.join('')]\n})\n\nt('Cursor error', async() => [\n  '42601',\n  await sql`wat`.cursor(() => { /* noop */ }).catch((err) => err.code)\n])\n\nt('Multiple Cursors', { timeout: 2 }, async() => {\n  const result = []\n  await sql.begin(async sql => [\n    await sql`select 1 as cursor, x from generate_series(1,4) as x`.cursor(async([row]) => {\n      result.push(row.x)\n      await new Promise(r => setTimeout(r, 20))\n    }),\n    await sql`select 2 as cursor, x from generate_series(101,104) as x`.cursor(async([row]) => {\n      result.push(row.x)\n      await new Promise(r => setTimeout(r, 10))\n    })\n  ])\n\n  return ['1,2,3,4,101,102,103,104', result.join(',')]\n})\n\nt('Cursor as async iterator', async() => {\n  const order = []\n  for await (const [x] of sql`select generate_series(1,2) as x;`.cursor()) {\n    order.push(x.x + 'a')\n    await delay(10)\n    order.push(x.x + 'b')\n  }\n\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Cursor as async iterator with break', async() => {\n  const order = []\n  for await (const xs of sql`select generate_series(1,2) as x;`.cursor()) {\n    order.push(xs[0].x + 'a')\n    await delay(10)\n    order.push(xs[0].x + 'b')\n    break\n  }\n\n  return ['1a1b', order.join('')]\n})\n\nt('Async Iterator Unsafe cursor', async() => {\n  const order = []\n  for await (const [x] of sql.unsafe('select 1 as x union select 2 as x').cursor()) {\n    order.push(x.x + 'a')\n    await delay(10)\n    order.push(x.x + 'b')\n  }\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Async Iterator Cursor custom n', async() => {\n  const order = []\n  for await (const x of sql`select * from generate_series(1,20)`.cursor(10))\n    order.push(x.length)\n\n  return ['10,10', order.join(',')]\n})\n\nt('Async Iterator Cursor custom with rest n', async() => {\n  const order = []\n  for await (const x of sql`select * from generate_series(1,20)`.cursor(11))\n    order.push(x.length)\n\n  return ['11,9', order.join(',')]\n})\n\nt('Async Iterator Cursor custom with less results than batch size', async() => {\n  const order = []\n  for await (const x of sql`select * from generate_series(1,20)`.cursor(21))\n    order.push(x.length)\n  return ['20', order.join(',')]\n})\n\nt('Transform row', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { row: () => 1 }\n  })\n\n  return [1, (await sql`select 'wat'`)[0]]\n})\n\nt('Transform row forEach', async() => {\n  let result\n  const sql = postgres({\n    ...options,\n    transform: { row: () => 1 }\n  })\n\n  await sql`select 1`.forEach(x => result = x)\n\n  return [1, result]\n})\n\nt('Transform value', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { value: () => 1 }\n  })\n\n  return [1, (await sql`select 'wat' as x`)[0].x]\n})\n\nt('Transform columns from', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.fromCamel\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`\n  await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`\n  return [\n    2,\n    (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].a_test,\n    await sql`drop table test`\n  ]\n})\n\nt('Transform columns to', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.toCamel\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ a_test: 1, b_test: 1 }]) }`\n  await sql`update test set ${ sql({ a_test: 2, b_test: 2 }) }`\n  return [\n    2,\n    (await sql`select a_test, b_test from test`)[0].aTest,\n    await sql`drop table test`\n  ]\n})\n\nt('Transform columns from and to', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`\n  await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`\n  return [\n    2,\n    (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].aTest,\n    await sql`drop table test`\n  ]\n})\n\nt('Transform columns from and to (legacy)', async() => {\n  const sql = postgres({\n    ...options,\n    transform: {\n      column: {\n        to: postgres.fromCamel,\n        from: postgres.toCamel\n      }\n    }\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`\n  await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`\n  return [\n    2,\n    (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].aTest,\n    await sql`drop table test`\n  ]\n})\n\nt('Unix socket', async() => {\n  const sql = postgres({\n    ...options,\n    host: process.env.PGSOCKET || '/tmp' // eslint-disable-line\n  })\n\n  return [1, (await sql`select 1 as x`)[0].x]\n})\n\nt('Big result', async() => {\n  return [100000, (await sql`select * from generate_series(1, 100000)`).count]\n})\n\nt('Debug', async() => {\n  let result\n  const sql = postgres({\n    ...options,\n    debug: (connection_id, str) => result = str\n  })\n\n  await sql`select 1`\n\n  return ['select 1', result]\n})\n\nt('bigint is returned as String', async() => [\n  'string',\n  typeof (await sql`select 9223372036854777 as x`)[0].x\n])\n\nt('int is returned as Number', async() => [\n  'number',\n  typeof (await sql`select 123 as x`)[0].x\n])\n\nt('numeric is returned as string', async() => [\n  'string',\n  typeof (await sql`select 1.2 as x`)[0].x\n])\n\nt('Async stack trace', async() => {\n  const sql = postgres({ ...options, debug: false })\n  return [\n    parseInt(new Error().stack.split('\\n')[1].match(':([0-9]+):')[1]) + 1,\n    parseInt(await sql`error`.catch(x => x.stack.split('\\n').pop().match(':([0-9]+):')[1]))\n  ]\n})\n\nt('Debug has long async stack trace', async() => {\n  const sql = postgres({ ...options, debug: true })\n\n  return [\n    'watyo',\n    await yo().catch(x => x.stack.match(/wat|yo/g).join(''))\n  ]\n\n  function yo() {\n    return wat()\n  }\n\n  function wat() {\n    return sql`error`\n  }\n})\n\nt('Error contains query string', async() => [\n  'selec 1',\n  (await sql`selec 1`.catch(err => err.query))\n])\n\nt('Error contains query serialized parameters', async() => [\n  1,\n  (await sql`selec ${ 1 }`.catch(err => err.parameters[0]))\n])\n\nt('Error contains query raw parameters', async() => [\n  1,\n  (await sql`selec ${ 1 }`.catch(err => err.args[0]))\n])\n\nt('Query and parameters on errorare not enumerable if debug is not set', async() => {\n  const sql = postgres({ ...options, debug: false })\n\n  return [\n    false,\n    (await sql`selec ${ 1 }`.catch(err => err.propertyIsEnumerable('parameters') || err.propertyIsEnumerable('query')))\n  ]\n})\n\nt('Query and parameters are enumerable if debug is set', async() => {\n  const sql = postgres({ ...options, debug: true })\n\n  return [\n    true,\n    (await sql`selec ${ 1 }`.catch(err => err.propertyIsEnumerable('parameters') && err.propertyIsEnumerable('query')))\n  ]\n})\n\nt('connect_timeout', { timeout: 20 }, async() => {\n  const connect_timeout = 0.2\n  const server = net.createServer()\n  server.listen()\n  const sql = postgres({ port: server.address().port, host: '127.0.0.1', connect_timeout })\n  const start = Date.now()\n  let end\n  await sql`select 1`.catch((e) => {\n    if (e.code !== 'CONNECT_TIMEOUT')\n      throw e\n    end = Date.now()\n  })\n  server.close()\n  return [connect_timeout, Math.floor((end - start) / 100) / 10]\n})\n\nt('connect_timeout throws proper error', async() => [\n  'CONNECT_TIMEOUT',\n  await postgres({\n    ...options,\n    ...login_scram,\n    connect_timeout: 0.001\n  })`select 1`.catch(e => e.code)\n])\n\nt('connect_timeout error message includes host:port', { timeout: 20 }, async() => {\n  const connect_timeout = 0.2\n  const server = net.createServer()\n  server.listen()\n  const sql = postgres({ port: server.address().port, host: '127.0.0.1', connect_timeout })\n  const port = server.address().port\n  let err\n  await sql`select 1`.catch((e) => {\n    if (e.code !== 'CONNECT_TIMEOUT')\n      throw e\n    err = e.message\n  })\n  server.close()\n  return [['write CONNECT_TIMEOUT 127.0.0.1:', port].join(''), err]\n})\n\nt('requests works after single connect_timeout', async() => {\n  let first = true\n\n  const sql = postgres({\n    ...options,\n    ...login_scram,\n    connect_timeout: { valueOf() { return first ? (first = false, 0.0001) : 1 } }\n  })\n\n  return [\n    'CONNECT_TIMEOUT,,1',\n    [\n      await sql`select 1 as x`.then(() => 'success', x => x.code),\n      await delay(10),\n      (await sql`select 1 as x`)[0].x\n    ].join(',')\n  ]\n})\n\nt('Postgres errors are of type PostgresError', async() =>\n  [true, (await sql`bad keyword`.catch(e => e)) instanceof sql.PostgresError]\n)\n\nt('Result has columns spec', async() =>\n  ['x', (await sql`select 1 as x`).columns[0].name]\n)\n\nt('forEach has result as second argument', async() => {\n  let x\n  await sql`select 1 as x`.forEach((_, result) => x = result)\n  return ['x', x.columns[0].name]\n})\n\nt('Result as arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: {\n      row: x => Object.values(x)\n    }\n  })\n\n  return ['1,2', (await sql`select 1 as a, 2 as b`)[0].join(',')]\n})\n\nt('Insert empty array', async() => {\n  await sql`create table tester (ints int[])`\n  return [\n    Array.isArray((await sql`insert into tester (ints) values (${ sql.array([]) }) returning *`)[0].ints),\n    true,\n    await sql`drop table tester`\n  ]\n})\n\nt('Insert array in sql()', async() => {\n  await sql`create table tester (ints int[])`\n  return [\n    Array.isArray((await sql`insert into tester ${ sql({ ints: sql.array([]) }) } returning *`)[0].ints),\n    true,\n    await sql`drop table tester`\n  ]\n})\n\nt('Automatically creates prepared statements', async() => {\n  const sql = postgres(options)\n  const result = await sql`select * from pg_prepared_statements`\n  return [true, result.some(x => x.name = result.statement.name)]\n})\n\nt('no_prepare: true disables prepared statements (deprecated)', async() => {\n  const sql = postgres({ ...options, no_prepare: true })\n  const result = await sql`select * from pg_prepared_statements`\n  return [false, result.some(x => x.name = result.statement.name)]\n})\n\nt('prepare: false disables prepared statements', async() => {\n  const sql = postgres({ ...options, prepare: false })\n  const result = await sql`select * from pg_prepared_statements`\n  return [false, result.some(x => x.name = result.statement.name)]\n})\n\nt('prepare: true enables prepared statements', async() => {\n  const sql = postgres({ ...options, prepare: true })\n  const result = await sql`select * from pg_prepared_statements`\n  return [true, result.some(x => x.name = result.statement.name)]\n})\n\nt('prepares unsafe query when \"prepare\" option is true', async() => {\n  const sql = postgres({ ...options, prepare: true })\n  const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ['bla'], { prepare: true })\n  return [true, result.some(x => x.name = result.statement.name)]\n})\n\nt('does not prepare unsafe query by default', async() => {\n  const sql = postgres({ ...options, prepare: true })\n  const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ['bla'])\n  return [false, result.some(x => x.name = result.statement.name)]\n})\n\nt('Recreate prepared statements on transformAssignedExpr error', { timeout: 1 }, async() => {\n  const insert = () => sql`insert into test (name) values (${ '1' }) returning name`\n  await sql`create table test (name text)`\n  await insert()\n  await sql`alter table test alter column name type int using name::integer`\n  return [\n    1,\n    (await insert())[0].name,\n    await sql`drop table test`\n  ]\n})\n\nt('Throws correct error when retrying in transactions', async() => {\n  await sql`create table test(x int)`\n  const error = await sql.begin(sql => sql`insert into test (x) values (${ false })`).catch(e => e)\n  return [\n    error.code,\n    '42804',\n    sql`drop table test`\n  ]\n})\n\nt('Recreate prepared statements on RevalidateCachedQuery error', async() => {\n  const select = () => sql`select name from test`\n  await sql`create table test (name text)`\n  await sql`insert into test values ('1')`\n  await select()\n  await sql`alter table test alter column name type int using name::integer`\n  return [\n    1,\n    (await select())[0].name,\n    await sql`drop table test`\n  ]\n})\n\nt('Properly throws routine error on not prepared statements', async() => {\n  await sql`create table x (x text[])`\n  const { routine } = await sql.unsafe(`\n    insert into x(x) values (('a', 'b'))\n  `).catch(e => e)\n\n  return ['transformAssignedExpr', routine, await sql`drop table x`]\n})\n\nt('Properly throws routine error on not prepared statements in transaction', async() => {\n  const { routine } = await sql.begin(sql => [\n    sql`create table x (x text[])`,\n    sql`insert into x(x) values (('a', 'b'))`\n  ]).catch(e => e)\n\n  return ['transformAssignedExpr', routine]\n})\n\nt('Properly throws routine error on not prepared statements using file', async() => {\n  const { routine } = await sql.unsafe(`\n    create table x (x text[]);\n    insert into x(x) values (('a', 'b'));\n  `, { prepare: true }).catch(e => e)\n\n  return ['transformAssignedExpr', routine]\n})\n\nt('Catches connection config errors', async() => {\n  const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })\n\n  return [\n    'wat',\n    await sql`select 1`.catch((e) => e.message)\n  ]\n})\n\nt('Catches connection config errors with end', async() => {\n  const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })\n\n  return [\n    'wat',\n    await sql`select 1`.catch((e) => e.message),\n    await sql.end()\n  ]\n})\n\nt('Catches query format errors', async() => [\n  'wat',\n  await sql.unsafe({ toString: () => { throw new Error('wat') } }).catch((e) => e.message)\n])\n\nt('Multiple hosts', {\n  timeout: 1\n}, async() => {\n  const s1 = postgres({ idle_timeout })\n      , s2 = postgres({ idle_timeout, port: 5433 })\n      , sql = postgres('postgres://localhost:5432,localhost:5433', { idle_timeout, max: 1 })\n      , result = []\n\n  const id1 = (await s1`select system_identifier as x from pg_control_system()`)[0].x\n  const id2 = (await s2`select system_identifier as x from pg_control_system()`)[0].x\n\n  const x1 = await sql`select 1`\n  result.push((await sql`select system_identifier as x from pg_control_system()`)[0].x)\n  await s1`select pg_terminate_backend(${ x1.state.pid }::int)`\n  await delay(50)\n\n  const x2 = await sql`select 1`\n  result.push((await sql`select system_identifier as x from pg_control_system()`)[0].x)\n  await s2`select pg_terminate_backend(${ x2.state.pid }::int)`\n  await delay(50)\n\n  result.push((await sql`select system_identifier as x from pg_control_system()`)[0].x)\n\n  return [[id1, id2, id1].join(','), result.join(',')]\n})\n\nt('Escaping supports schemas and tables', async() => {\n  await sql`create schema a`\n  await sql`create table a.b (c int)`\n  await sql`insert into a.b (c) values (1)`\n  return [\n    1,\n    (await sql`select ${ sql('a.b.c') } from a.b`)[0].c,\n    await sql`drop table a.b`,\n    await sql`drop schema a`\n  ]\n})\n\nt('Raw method returns rows as arrays', async() => {\n  const [x] = await sql`select 1`.raw()\n  return [\n    Array.isArray(x),\n    true\n  ]\n})\n\nt('Raw method returns values unparsed as Buffer', async() => {\n  const [[x]] = await sql`select 1`.raw()\n  return [\n    x instanceof Uint8Array,\n    true\n  ]\n})\n\nt('Array returns rows as arrays of columns', async() => {\n  return [(await sql`select 1`.values())[0][0], 1]\n})\n\nt('Copy read', async() => {\n  const result = []\n\n  await sql`create table test (x int)`\n  await sql`insert into test select * from generate_series(1,10)`\n  const readable = await sql`copy test to stdout`.readable()\n  readable.on('data', x => result.push(x))\n  await new Promise(r => readable.on('end', r))\n\n  return [\n    result.length,\n    10,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy write', { timeout: 2 }, async() => {\n  await sql`create table test (x int)`\n  const writable = await sql`copy test from stdin`.writable()\n\n  writable.write('1\\n')\n  writable.write('1\\n')\n  writable.end()\n\n  await new Promise(r => writable.on('finish', r))\n\n  return [\n    (await sql`select 1 from test`).length,\n    2,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy write as first', async() => {\n  await sql`create table test (x int)`\n  const first = postgres(options)\n  const writable = await first`COPY test FROM STDIN WITH(FORMAT csv, HEADER false, DELIMITER ',')`.writable()\n  writable.write('1\\n')\n  writable.write('1\\n')\n  writable.end()\n\n  await new Promise(r => writable.on('finish', r))\n\n  return [\n    (await sql`select 1 from test`).length,\n    2,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy from file', async() => {\n  await sql`create table test (x int, y int, z int)`\n  await new Promise(async r => fs\n    .createReadStream(rel('copy.csv'))\n    .pipe(await sql`copy test from stdin`.writable())\n    .on('finish', r)\n  )\n\n  return [\n    JSON.stringify(await sql`select * from test`),\n    '[{\"x\":1,\"y\":2,\"z\":3},{\"x\":4,\"y\":5,\"z\":6}]',\n    await sql`drop table test`\n  ]\n})\n\nt('Copy from works in transaction', async() => {\n  await sql`create table test(x int)`\n  const xs = await sql.begin(async sql => {\n    (await sql`copy test from stdin`.writable()).end('1\\n2')\n    await delay(20)\n    return sql`select 1 from test`\n  })\n\n  return [\n    xs.length,\n    2,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy from abort', async() => {\n  const sql = postgres(options)\n  const readable = fs.createReadStream(rel('copy.csv'))\n\n  await sql`create table test (x int, y int, z int)`\n  await sql`TRUNCATE TABLE test`\n\n  const writable = await sql`COPY test FROM STDIN`.writable()\n\n  let aborted\n\n  readable\n    .pipe(writable)\n    .on('error', (err) => aborted = err)\n\n  writable.destroy(new Error('abort'))\n  await sql.end()\n\n  return [\n    'abort',\n    aborted.message,\n    await postgres(options)`drop table test`\n  ]\n})\n\nt('multiple queries before connect', async() => {\n  const sql = postgres({ ...options, max: 2 })\n  const xs = await Promise.all([\n    sql`select 1 as x`,\n    sql`select 2 as x`,\n    sql`select 3 as x`,\n    sql`select 4 as x`\n  ])\n\n  return [\n    '1,2,3,4',\n    xs.map(x => x[0].x).join()\n  ]\n})\n\nt('subscribe', { timeout: 2 }, async() => {\n  const sql = postgres({\n    database: 'postgres_js_test',\n    publications: 'alltables'\n  })\n\n  await sql.unsafe('create publication alltables for all tables')\n\n  const result = []\n\n  const { unsubscribe } = await sql.subscribe('*', (row, { command, old }) => {\n    result.push(command, row.name, row.id, old && old.name, old && old.id)\n  })\n\n  await sql`\n    create table test (\n      id serial primary key,\n      name text\n    )\n  `\n\n  await sql`alter table test replica identity default`\n  await sql`insert into test (name) values ('Murray')`\n  await sql`update test set name = 'Rothbard'`\n  await sql`update test set id = 2`\n  await sql`delete from test`\n  await sql`alter table test replica identity full`\n  await sql`insert into test (name) values ('Murray')`\n  await sql`update test set name = 'Rothbard'`\n  await sql`delete from test`\n  await delay(10)\n  await unsubscribe()\n  await sql`insert into test (name) values ('Oh noes')`\n  await delay(10)\n  return [\n    'insert,Murray,1,,,update,Rothbard,1,,,update,Rothbard,2,,1,delete,,2,,,insert,Murray,2,,,update,Rothbard,2,Murray,2,delete,Rothbard,2,,', // eslint-disable-line\n    result.join(','),\n    await sql`drop table test`,\n    await sql`drop publication alltables`,\n    await sql.end()\n  ]\n})\n\nt('subscribe with transform', { timeout: 2 }, async() => {\n  const sql = postgres({\n    transform: {\n      column: {\n        from: postgres.toCamel,\n        to: postgres.fromCamel\n      }\n    },\n    database: 'postgres_js_test',\n    publications: 'alltables'\n  })\n\n  await sql.unsafe('create publication alltables for all tables')\n\n  const result = []\n\n  const { unsubscribe } = await sql.subscribe('*', (row, { command, old }) =>\n    result.push(command, row.nameInCamel || row.id, old && old.nameInCamel)\n  )\n\n  await sql`\n    create table test (\n      id serial primary key,\n      name_in_camel text\n    )\n  `\n\n  await sql`insert into test (name_in_camel) values ('Murray')`\n  await sql`update test set name_in_camel = 'Rothbard'`\n  await sql`delete from test`\n  await sql`alter table test replica identity full`\n  await sql`insert into test (name_in_camel) values ('Murray')`\n  await sql`update test set name_in_camel = 'Rothbard'`\n  await sql`delete from test`\n  await delay(10)\n  await unsubscribe()\n  await sql`insert into test (name_in_camel) values ('Oh noes')`\n  await delay(10)\n  return [\n    'insert,Murray,,update,Rothbard,,delete,1,,insert,Murray,,update,Rothbard,Murray,delete,Rothbard,',\n    result.join(','),\n    await sql`drop table test`,\n    await sql`drop publication alltables`,\n    await sql.end()\n  ]\n})\n\nt('subscribe reconnects and calls onsubscribe', { timeout: 4 }, async() => {\n  const sql = postgres({\n    database: 'postgres_js_test',\n    publications: 'alltables',\n    fetch_types: false\n  })\n\n  await sql.unsafe('create publication alltables for all tables')\n\n  const result = []\n  let onsubscribes = 0\n\n  const { unsubscribe, sql: subscribeSql } = await sql.subscribe(\n    '*',\n    (row, { command, old }) => result.push(command, row.name || row.id, old && old.name),\n    () => onsubscribes++\n  )\n\n  await sql`\n    create table test (\n      id serial primary key,\n      name text\n    )\n  `\n\n  await sql`insert into test (name) values ('Murray')`\n  await delay(10)\n  await subscribeSql.close()\n  await delay(500)\n  await sql`delete from test`\n  await delay(100)\n  await unsubscribe()\n  return [\n    '2insert,Murray,,delete,1,',\n    onsubscribes + result.join(','),\n    await sql`drop table test`,\n    await sql`drop publication alltables`,\n    await sql.end()\n  ]\n})\n\nt('Execute', async() => {\n  const result = await new Promise((resolve) => {\n    const sql = postgres({ ...options, fetch_types: false, debug:(id, query) => resolve(query) })\n    sql`select 1`.execute()\n  })\n\n  return [result, 'select 1']\n})\n\nt('Cancel running query', async() => {\n  const query = sql`select pg_sleep(2)`\n  setTimeout(() => query.cancel(), 500)\n  const error = await query.catch(x => x)\n  return ['57014', error.code]\n})\n\nt('Cancel piped query', { timeout: 5 }, async() => {\n  await sql`select 1`\n  const last = sql`select pg_sleep(1)`.execute()\n  const query = sql`select pg_sleep(2) as dig`\n  setTimeout(() => query.cancel(), 500)\n  const error = await query.catch(x => x)\n  await last\n  return ['57014', error.code]\n})\n\nt('Cancel queued query', async() => {\n  const query = sql`select pg_sleep(2) as nej`\n  const tx = sql.begin(sql => (\n    query.cancel(),\n    sql`select pg_sleep(0.5) as hej, 'hejsa'`\n  ))\n  const error = await query.catch(x => x)\n  await tx\n  return ['57014', error.code]\n})\n\nt('Fragments', async() => [\n  1,\n  (await sql`\n    ${ sql`select` } 1 as x\n  `)[0].x\n])\n\nt('Result becomes array', async() => [\n  true,\n  (await sql`select 1`).slice() instanceof Array\n])\n\nt('Describe', async() => {\n  const type = (await sql`select ${ 1 }::int as x`.describe()).types[0]\n  return [23, type]\n})\n\nt('Describe a statement', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`select name, age from tester where name like $1 and age > $2`.describe()\n  return [\n    '25,23/name:25,age:23',\n    `${ r.types.join(',') }/${ r.columns.map(c => `${c.name}:${c.type}`).join(',') }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Include table oid and column number in column details', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`select name, age from tester where name like $1 and age > $2`.describe()\n  const [{ oid }] = await sql`select oid from pg_class where relname = 'tester'`\n\n  return [\n    `table:${oid},number:1|table:${oid},number:2`,\n    `${ r.columns.map(c => `table:${c.table},number:${c.number}`).join('|') }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Describe a statement without parameters', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`select name, age from tester`.describe()\n  return [\n    '0,2',\n    `${ r.types.length },${ r.columns.length }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Describe a statement without columns', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`insert into tester (name, age) values ($1, $2)`.describe()\n  return [\n    '2,0',\n    `${ r.types.length },${ r.columns.length }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Large object', async() => {\n  const file = rel('index.js')\n      , md5 = crypto.createHash('md5').update(fs.readFileSync(file)).digest('hex')\n\n  const lo = await sql.largeObject()\n  await new Promise(async r => fs.createReadStream(file).pipe(await lo.writable()).on('finish', r))\n  await lo.seek(0)\n\n  const out = crypto.createHash('md5')\n  await new Promise(r => lo.readable().then(x => x.on('data', x => out.update(x)).on('end', r)))\n\n  return [\n    md5,\n    out.digest('hex'),\n    await lo.close()\n  ]\n})\n\nt('Catches type serialize errors', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: x => x,\n        serialize: () => { throw new Error('watSerialize') }\n      }\n    }\n  })\n\n  return [\n    'watSerialize',\n    (await sql`select ${ 'wat' }`.catch(e => e.message))\n  ]\n})\n\nt('Catches type parse errors', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: () => { throw new Error('watParse') },\n        serialize: x => x\n      }\n    }\n  })\n\n  return [\n    'watParse',\n    (await sql`select 'wat'`.catch(e => e.message))\n  ]\n})\n\nt('Catches type serialize errors in transactions', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: x => x,\n        serialize: () => { throw new Error('watSerialize') }\n      }\n    }\n  })\n\n  return [\n    'watSerialize',\n    (await sql.begin(sql => (\n      sql`select 1`,\n      sql`select ${ 'wat' }`\n    )).catch(e => e.message))\n  ]\n})\n\nt('Catches type parse errors in transactions', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: () => { throw new Error('watParse') },\n        serialize: x => x\n      }\n    }\n  })\n\n  return [\n    'watParse',\n    (await sql.begin(sql => (\n      sql`select 1`,\n      sql`select 'wat'`\n    )).catch(e => e.message))\n  ]\n})\n\nt('Prevent premature end of connection in transaction', async() => {\n  const sql = postgres({ max_lifetime: 0.01, idle_timeout })\n  const result = await sql.begin(async sql => {\n    await sql`select 1`\n    await delay(20)\n    await sql`select 1`\n    return 'yay'\n  })\n\n\n  return [\n    'yay',\n    result\n  ]\n})\n\nt('Ensure reconnect after max_lifetime with transactions', { timeout: 5 }, async() => {\n  const sql = postgres({\n    max_lifetime: 0.01,\n    idle_timeout,\n    max: 1\n  })\n\n  let x = 0\n  while (x++ < 10) await sql.begin(sql => sql`select 1 as x`)\n\n  return [true, true]\n})\n\n\nt('Ensure transactions throw if connection is closed dwhile there is no query', async() => {\n  const sql = postgres(options)\n  const x = await sql.begin(async() => {\n    setTimeout(() => sql.end({ timeout: 0 }), 10)\n    await new Promise(r => setTimeout(r, 200))\n    return sql`select 1`\n  }).catch(x => x)\n  return ['CONNECTION_CLOSED', x.code]\n})\n\nt('Custom socket', {}, async() => {\n  let result\n  const sql = postgres({\n    socket: () => new Promise((resolve, reject) => {\n      const socket = new net.Socket()\n      socket.connect(5432)\n      socket.once('data', x => result = x[0])\n      socket.on('error', reject)\n      socket.on('connect', () => resolve(socket))\n    }),\n    idle_timeout\n  })\n\n  await sql`select 1`\n\n  return [\n    result,\n    82\n  ]\n})\n\nt('Ensure drain only dequeues if ready', async() => {\n  const sql = postgres(options)\n\n  const res = await Promise.all([\n    sql.unsafe('SELECT 0+$1 --' + '.'.repeat(100000), [1]),\n    sql.unsafe('SELECT 0+$1+$2+$3', [1, 2, 3])\n  ])\n\n  return [res.length, 2]\n})\n\nt('Supports fragments as dynamic parameters', async() => {\n  await sql`create table test (a int, b bool)`\n  await sql`insert into test values(1, true)`\n  await sql`insert into test ${\n    sql({\n      a: 2,\n      b: sql`exists(select 1 from test where b = ${ true })`\n    })\n  }`\n\n  return [\n    '1,t2,t',\n    (await sql`select * from test`.raw()).join(''),\n    await sql`drop table test`\n  ]\n})\n\nt('Supports nested fragments with parameters', async() => {\n  await sql`create table test ${\n    sql`(${ sql('a') } ${ sql`int` })`\n  }`\n  await sql`insert into test values(1)`\n  return [\n    1,\n    (await sql`select a from test`)[0].a,\n    await sql`drop table test`\n  ]\n})\n\nt('Supports multiple nested fragments with parameters', async() => {\n  const [{ b }] = await sql`select * ${\n    sql`from ${\n      sql`(values (2, ${ 1 }::int)) as x(${ sql(['a', 'b']) })`\n    }`\n  }`\n  return [\n    1,\n    b\n  ]\n})\n\nt('Supports arrays of fragments', async() => {\n  const [{ x }] = await sql`\n    ${ [sql`select`, sql`1`, sql`as`, sql`x`] }\n  `\n\n  return [\n    1,\n    x\n  ]\n})\n\nt('Does not try rollback when commit errors', async() => {\n  let notice = null\n  const sql = postgres({ ...options, onnotice: x => notice = x })\n  await sql`create table test(x int constraint test_constraint unique deferrable initially deferred)`\n\n  await sql.begin('isolation level serializable', async sql => {\n    await sql`insert into test values(1)`\n    await sql`insert into test values(1)`\n  }).catch(e => e)\n\n  return [\n    notice,\n    null,\n    await sql`drop table test`\n  ]\n})\n\nt('Last keyword used even with duplicate keywords', async() => {\n  await sql`create table test (x int)`\n  await sql`insert into test values(1)`\n  const [{ x }] = await sql`\n    select\n      1 in (1) as x\n    from test\n    where x in ${ sql([1, 2]) }\n  `\n\n  return [x, true, await sql`drop table test`]\n})\n\nt('Insert array with null', async() => {\n  await sql`create table test (x int[])`\n  await sql`insert into test ${ sql({ x: [1, null, 3] }) }`\n  return [\n    1,\n    (await sql`select x from test`)[0].x[0],\n    await sql`drop table test`\n  ]\n})\n\nt('Insert array with undefined throws', async() => {\n  await sql`create table test (x int[])`\n  return [\n    'UNDEFINED_VALUE',\n    await sql`insert into test ${ sql({ x: [1, undefined, 3] }) }`.catch(e => e.code),\n    await sql`drop table test`\n  ]\n})\n\nt('Insert array with undefined transform', async() => {\n  const sql = postgres({ ...options, transform: { undefined: null } })\n  await sql`create table test (x int[])`\n  await sql`insert into test ${ sql({ x: [1, undefined, 3] }) }`\n  return [\n    1,\n    (await sql`select x from test`)[0].x[0],\n    await sql`drop table test`\n  ]\n})\n\nt('concurrent cursors', async() => {\n  const xs = []\n\n  await Promise.all([...Array(7)].map((x, i) => [\n    sql`select ${ i }::int as a, generate_series(1, 2) as x`.cursor(([x]) => xs.push(x.a + x.x))\n  ]).flat())\n\n  return ['12233445566778', xs.join('')]\n})\n\nt('concurrent cursors multiple connections', async() => {\n  const sql = postgres({ ...options, max: 2 })\n  const xs = []\n\n  await Promise.all([...Array(7)].map((x, i) => [\n    sql`select ${ i }::int as a, generate_series(1, 2) as x`.cursor(([x]) => xs.push(x.a + x.x))\n  ]).flat())\n\n  return ['12233445566778', xs.sort().join('')]\n})\n\nt('reserve connection', async() => {\n  const reserved = await sql.reserve()\n\n  setTimeout(() => reserved.release(), 510)\n\n  const xs = await Promise.all([\n    reserved`select 1 as x`.then(([{ x }]) => ({ time: Date.now(), x })),\n    sql`select 2 as x`.then(([{ x }]) => ({ time: Date.now(), x })),\n    reserved`select 3 as x`.then(([{ x }]) => ({ time: Date.now(), x }))\n  ])\n\n  if (xs[1].time - xs[2].time < 500)\n    throw new Error('Wrong time')\n\n  return [\n    '123',\n    xs.map(x => x.x).join('')\n  ]\n})\n\nt('arrays in reserved connection', async() => {\n  const reserved = await sql.reserve()\n  const [{ x }] = await reserved`select array[1, 2, 3] as x`\n  reserved.release()\n\n  return [\n    '123',\n    x.join('')\n  ]\n})\n\nt('Ensure reserve on query throws proper error', async() => {\n  const sql = postgres({ idle_timeout }) // eslint-disable-line\n  const reserved = await sql.reserve()\n  const [{ x }] = await reserved`select 'wat' as x`\n\n  return [\n    'wat', x, reserved.release()\n  ]\n})\n\nt('query during copy error', async() => {\n  const sql = postgres(options) // eslint-disable-line\n  await sql`create table test (id serial primary key, name text)`\n  const copy = await sql`copy test from stdin`.writable()\n  const error = await sql`select 1`.catch(e => e)\n  await copy.end()\n\n  return [\n    'COPY_IN_PROGRESS', error.code,\n    await sql`drop table test`\n  ]\n})\n"
  },
  {
    "path": "cjs/tests/pg_hba.conf",
    "content": "local   all                 all                                     trust\nhost    all                 postgres                samehost        trust\nhost    postgres_js_test    postgres_js_test        samehost        trust\nhost    postgres_js_test    postgres_js_test_md5    samehost        md5\nhost    postgres_js_test    postgres_js_test_scram  samehost        scram-sha-256\n"
  },
  {
    "path": "cjs/tests/select-param.sql",
    "content": "select $1 as x\n"
  },
  {
    "path": "cjs/tests/select.sql",
    "content": "select 1 as x\n"
  },
  {
    "path": "cjs/tests/test.js",
    "content": "/* eslint no-console: 0 */\n\nconst util = require('util')\n\nlet done = 0\nlet only = false\nlet ignored = 0\nlet failed = false\nlet promise = Promise.resolve()\nconst tests = {}\n    , ignore = {}\n\nconst nt = module.exports.nt = () => ignored++\nconst ot = module.exports.ot = (...rest) => (only = true, test(true, ...rest))\nconst t = module.exports.t = (...rest) => test(false, ...rest)\nt.timeout = 5\n\nasync function test(o, name, options, fn) {\n  typeof options !== 'object' && (fn = options, options = {})\n  const line = new Error().stack.split('\\n')[3].match(':([0-9]+):')[1]\n\n  await 1\n\n  if (only && !o)\n    return\n\n  tests[line] = { fn, line, name }\n  promise = promise.then(() => Promise.race([\n    new Promise((resolve, reject) =>\n      fn.timer = setTimeout(() => reject('Timed out'), (options.timeout || t.timeout) * 1000)\n    ),\n    failed\n      ? (ignored++, ignore)\n      : fn()\n  ]))\n    .then(async x => {\n      clearTimeout(fn.timer)\n      if (x === ignore)\n        return\n\n      if (!Array.isArray(x))\n        throw new Error('Test should return result array')\n\n      const [expected, got] = await Promise.all(x)\n      if (expected !== got) {\n        failed = true\n        throw new Error(util.inspect(expected) + ' != ' + util.inspect(got))\n      }\n\n      tests[line].succeeded = true\n      process.stdout.write('✅')\n    })\n    .catch(err => {\n      tests[line].failed = failed = true\n      tests[line].error = err instanceof Error ? err : new Error(util.inspect(err))\n    })\n    .then(() => {\n      ++done === Object.keys(tests).length && exit()\n    })\n}\n\nfunction exit() {\n  let success = true\n  Object.values(tests).every((x) => {\n    if (x.succeeded)\n      return true\n\n    success = false\n    x.cleanup\n      ? console.error('⛔️', x.name + ' at line', x.line, 'cleanup failed', '\\n', util.inspect(x.cleanup))\n      : console.error('⛔️', x.name + ' at line', x.line, x.failed\n        ? 'failed'\n        : 'never finished', x.error ? '\\n' + util.inspect(x.error) : ''\n      )\n  })\n\n  only\n    ? console.error('⚠️', 'Not all tests were run')\n    : ignored\n      ? console.error('⚠️', ignored, 'ignored test' + (ignored === 1 ? '' : 's', '\\n'))\n      : success\n        ? console.log('🎉')\n        : console.error('⚠️', 'Not good')\n\n  !process.exitCode && (!success || only || ignored) && (process.exitCode = 1)\n}\n\n"
  },
  {
    "path": "deno/README.md",
    "content": "<img align=\"left\" width=\"440\" height=\"180\" alt=\"Fastest full PostgreSQL nodejs client\" src=\"https://raw.githubusercontent.com/porsager/postgres/master/postgresjs.svg?sanitize=true\">\n\n- [🚀 Fastest full-featured node & deno client](https://github.com/porsager/postgres-benchmarks#results)\n- 🏷 ES6 Tagged Template Strings at the core\n- 🏄‍♀️ Simple surface API\n- 🖊️ Dynamic query support\n- 💬 Chat and help on [Gitter](https://gitter.im/porsager/postgres)\n- 🐦 Follow on [Twitter](https://twitter.com/rporsager)\n\n<br>\n\n## Getting started\n\n<br>\n<img height=\"220\" width=\"458\" alt=\"Good UX with Postgres.js\" src=\"https://raw.githubusercontent.com/porsager/postgres/master/demo.gif\">\n<br>\n\n\n### Usage\nCreate your `sql` database instance\n```js\n// db.js\nimport postgres from 'https://deno.land/x/postgresjs/mod.js'\n\nconst sql = postgres({ /* options */ }) // will use psql environment variables\n\nexport default sql\n```\n\nSimply import for use elsewhere\n```js\n// users.js\nimport sql from './db.js'\n\nasync function getUsersOver(age) {\n  const users = await sql`\n    select\n      name,\n      age\n    from users\n    where age > ${ age }\n  `\n  // users = Result [{ name: \"Walter\", age: 80 }, { name: 'Murray', age: 68 }, ...]\n  return users\n}\n\n\nasync function insertUser({ name, age }) {\n  const users = await sql`\n    insert into users\n      (name, age)\n    values\n      (${ name }, ${ age })\n    returning name, age\n  `\n  // users = Result [{ name: \"Murray\", age: 68 }]\n  return users\n}\n```\n\n#### ESM dynamic imports\n\nThe library can be used with ESM dynamic imports as well as shown here.\n\n```js\nconst { default: postgres } = await import('postgres')\n```\n\n## Table of Contents\n\n* [Connection](#connection)\n* [Queries](#queries)\n* [Building queries](#building-queries)\n* [Advanced query methods](#advanced-query-methods)\n* [Transactions](#transactions)\n* [Data Transformation](#data-transformation)\n* [Listen & notify](#listen--notify)\n* [Realtime subscribe](#realtime-subscribe)\n* [Numbers, bigint, numeric](#numbers-bigint-numeric)\n* [Result Array](#result-array)\n* [Connection details](#connection-details)\n* [Custom Types](#custom-types)\n* [Teardown / Cleanup](#teardown--cleanup)\n* [Error handling](#error-handling)\n* [TypeScript support](#typescript-support)\n* [Reserving connections](#reserving-connections)\n* [Changelog](./CHANGELOG.md)\n\n\n## Connection\n\n### `postgres([url], [options])`\n\nYou can use either a `postgres://` url connection string or the options to define your database connection properties. Options in the object will override any present in the url. Options will fall back to the same environment variables as psql.\n\n```js\nconst sql = postgres('postgres://username:password@host:port/database', {\n  host                 : '',            // Postgres ip address[s] or domain name[s]\n  port                 : 5432,          // Postgres server port[s]\n  database             : '',            // Name of database to connect to\n  username             : '',            // Username of database user\n  password             : '',            // Password of database user\n  ...and more\n})\n```\n\nMore options can be found in the [Connection details section](#connection-details).\n\n## Queries\n\n### ```await sql`...` -> Result[]```\n\nPostgres.js utilizes [Tagged template functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) to process query parameters **before** interpolation. Using tagged template literals benefits developers by:\n\n1. **Enforcing** safe query generation\n2. Giving the ` sql`` ` function powerful [utility](#dynamic-inserts) and [query building](#building-queries) features.\n\nAny generic value will be serialized according to an inferred type, and replaced by a PostgreSQL protocol placeholder `$1, $2, ...`. The parameters are then sent separately to the database which handles escaping & casting.\n\nAll queries will return a `Result` array, with objects mapping column names to each row.\n\n```js\nconst xs = await sql`\n  insert into users (\n    name, age\n  ) values (\n    'Murray', 68\n  )\n\n  returning *\n`\n\n// xs = [{ user_id: 1, name: 'Murray', age: 68 }]\n```\n\n> Please note that queries are first executed when `awaited` – or instantly by using [`.execute()`](#execute).\n\n### Query parameters\n\nParameters are automatically extracted and handled by the database so that SQL injection isn't possible. No special handling is necessary, simply use tagged template literals as usual.\n\n```js\nconst name = 'Mur'\n    , age = 60\n\nconst users = await sql`\n  select\n    name,\n    age\n  from users\n  where\n    name like ${ name + '%' }\n    and age > ${ age }\n`\n// users = [{ name: 'Murray', age: 68 }]\n```\n\n> Be careful with quotation marks here. Because Postgres infers column types, you do not need to wrap your interpolated parameters in quotes like `'${name}'`. This will cause an error because the tagged template replaces `${name}` with `$1` in the query string, leaving Postgres to do the interpolation. If you wrap that in a string, Postgres will see `'$1'` and interpret it as a string as opposed to a parameter.\n\n### Dynamic column selection\n\n```js\nconst columns = ['name', 'age']\n\nawait sql`\n  select\n    ${ sql(columns) }\n  from users\n`\n\n// Which results in:\nselect \"name\", \"age\" from users\n```\n\n### Dynamic inserts\n\n```js\nconst user = {\n  name: 'Murray',\n  age: 68\n}\n\nawait sql`\n  insert into users ${\n    sql(user, 'name', 'age')\n  }\n`\n\n// Which results in:\ninsert into users (\"name\", \"age\") values ($1, $2)\n\n// The columns can also be given with an array\nconst columns = ['name', 'age']\n\nawait sql`\n  insert into users ${\n    sql(user, columns)\n  }\n`\n```\n\n**You can omit column names and simply execute `sql(user)` to get all the fields from the object as columns**. Be careful not to allow users to supply columns that you do not want to be inserted.\n\n#### Multiple inserts in one query\nIf you need to insert multiple rows at the same time it's also much faster to do it with a single `insert`. Simply pass an array of objects to `sql()`.\n\n```js\nconst users = [{\n  name: 'Murray',\n  age: 68,\n  garbage: 'ignore'\n},\n{\n  name: 'Walter',\n  age: 80\n}]\n\nawait sql`insert into users ${ sql(users, 'name', 'age') }`\n\n// Is translated to:\ninsert into users (\"name\", \"age\") values ($1, $2), ($3, $4)\n\n// Here you can also omit column names which will use object keys as columns\nawait sql`insert into users ${ sql(users) }`\n\n// Which results in:\ninsert into users (\"name\", \"age\") values ($1, $2), ($3, $4)\n```\n\n### Dynamic columns in updates\nThis is also useful for update queries\n```js\nconst user = {\n  id: 1,\n  name: 'Murray',\n  age: 68\n}\n\nawait sql`\n  update users set ${\n    sql(user, 'name', 'age')\n  }\n  where user_id = ${ user.id }\n`\n\n// Which results in:\nupdate users set \"name\" = $1, \"age\" = $2 where user_id = $3\n\n// The columns can also be given with an array\nconst columns = ['name', 'age']\n\nawait sql`\n  update users set ${\n    sql(user, columns)\n  }\n  where user_id = ${ user.id }\n`\n```\n\n### Multiple updates in one query\nTo create multiple updates in a single query, it is necessary to use arrays instead of objects to ensure that the order of the items correspond with the column names.\n```js\nconst users = [\n  [1, 'John', 34],\n  [2, 'Jane', 27],\n]\n\nawait sql`\n  update users set name = update_data.name, age = (update_data.age)::int\n  from (values ${sql(users)}) as update_data (id, name, age)\n  where users.id = (update_data.id)::int\n  returning users.id, users.name, users.age\n`\n```\n\n### Dynamic values and `where in`\nValue lists can also be created dynamically, making `where in` queries simple too.\n```js\nconst users = await sql`\n  select\n    *\n  from users\n  where age in ${ sql([68, 75, 23]) }\n`\n```\n\nor\n```js\nconst [{ a, b, c }] = await sql`\n  select\n    *\n  from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)\n`\n```\n\n## Building queries\n\nPostgres.js features a simple dynamic query builder by conditionally appending/omitting query fragments.\nIt works by nesting ` sql`` ` fragments within other ` sql`` ` calls or fragments. This allows you to build dynamic queries safely without risking sql injections through usual string concatenation.\n\n### Partial queries\n```js\nconst olderThan = x => sql`and age > ${ x }`\n\nconst filterAge = true\n\nawait sql`\n  select\n   *\n  from users\n  where name is not null ${\n    filterAge\n      ? olderThan(50)\n      : sql``\n  }\n`\n// Which results in:\nselect * from users where name is not null\n// Or\nselect * from users where name is not null and age > 50\n```\n\n### Dynamic filters\n```js\nawait sql`\n  select\n    *\n  from users ${\n    id\n      ? sql`where user_id = ${ id }`\n      : sql``\n  }\n`\n\n// Which results in:\nselect * from users\n// Or\nselect * from users where user_id = $1\n```\n\n### Dynamic ordering\n\n```js\nconst id = 1\nconst order = {\n  username: 'asc'\n  created_at: 'desc'\n}\nawait sql`\n  select \n    * \n  from ticket \n  where account = ${ id }  \n  order by ${\n    Object.entries(order).flatMap(([column, order], i) =>\n      [i ? sql`,` : sql``, sql`${ sql(column) } ${ order === 'desc' ? sql`desc` : sql`asc` }`]\n    )\n  }\n`\n```\n\n### SQL functions\nUsing keywords or calling functions dynamically is also possible by using ``` sql`` ``` fragments.\n```js\nconst date = null\n\nawait sql`\n  update users set updated_at = ${ date || sql`now()` }\n`\n\n// Which results in:\nupdate users set updated_at = now()\n```\n\n### Table names\nDynamic identifiers like table names and column names is also supported like so:\n```js\nconst table = 'users'\n    , column = 'id'\n\nawait sql`\n  select ${ sql(column) } from ${ sql(table) }\n`\n\n// Which results in:\nselect \"id\" from \"users\"\n```\n\n### Quick primer on interpolation\n\nHere's a quick oversight over all the ways to do interpolation in a query template string:\n\n| Interpolation syntax       | Usage                         | Example                                                   |\n| -------------              | -------------                 | -------------                                             |\n| `${ sql`` }`               | for keywords or sql fragments | ``await sql`SELECT * FROM users ${sql`order by age desc` }` ``  |\n| `${ sql(string) }`         | for identifiers               | ``await sql`SELECT * FROM ${sql('table_name')` ``               |\n| `${ sql([] or {}, ...) }`  | for helpers                   | ``await sql`INSERT INTO users ${sql({ name: 'Peter'})}` ``      |\n| `${ 'somevalue' }`         | for values                    | ``await sql`SELECT * FROM users WHERE age = ${42}` ``           |\n\n## Advanced query methods\n\n### Cursors\n\n#### ```await sql``.cursor([rows = 1], [fn])```\n\nUse cursors if you need to throttle the amount of rows being returned from a query. You can use a cursor either as an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) or with a callback function. For a callback function new results won't be requested until the promise / async callback function has resolved.\n\n##### callback function\n```js\nawait sql`\n  select\n    *\n  from generate_series(1,4) as x\n`.cursor(async([row]) => {\n  // row = { x: 1 }\n  await http.request('https://example.com/wat', { row })\n})\n```\n\n##### for await...of\n```js\n// for await...of\nconst cursor = sql`select * from generate_series(1,4) as x`.cursor()\n\nfor await (const [row] of cursor) {\n  // row = { x: 1 }\n  await http.request('https://example.com/wat', { row })\n}\n```\n\nA single row will be returned by default, but you can also request batches by setting the number of rows desired in each batch as the first argument to `.cursor`:\n```js\nawait sql`\n  select\n    *\n  from generate_series(1,1000) as x\n`.cursor(10, async rows => {\n  // rows = [{ x: 1 }, { x: 2 }, ... ]\n  await Promise.all(rows.map(row =>\n    http.request('https://example.com/wat', { row })\n  ))\n})\n```\n\nIf an error is thrown inside the callback function no more rows will be requested and the outer promise will reject with the thrown error.\n\nYou can close the cursor early either by calling `break` in the `for await...of` loop, or by returning the token `sql.CLOSE` from the callback function.\n\n```js\nawait sql`\n  select * from generate_series(1,1000) as x\n`.cursor(row => {\n  return Math.random() > 0.9 && sql.CLOSE // or sql.END\n})\n```\n\n### Instant iteration\n\n#### ```await sql``.forEach(fn)```\n\nIf you want to handle rows returned by a query one by one, you can use `.forEach` which returns a promise that resolves once there are no more rows.\n```js\nawait sql`\n  select created_at, name from events\n`.forEach(row => {\n  // row = { created_at: '2019-11-22T14:22:00Z', name: 'connected' }\n})\n\n// No more rows\n```\n\n### Query Descriptions\n#### ```await sql``.describe() -> Result[]```\n\nRather than executing a given query, `.describe` will return information utilized in the query process. This information can include the query identifier, column types, etc.\n\nThis is useful for debugging and analyzing your Postgres queries. Furthermore, **`.describe` will give you access to the final generated query string that would be executed.**\n\n### Rows as Array of Values\n#### ```sql``.values()```\n\nUsing `.values` will return rows as an array of values for each column, instead of objects.\n\nThis can be useful to receive identically named columns, or for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.\n\n### Rows as Raw Array of Buffers\n#### ```sql``.raw()```\n\nUsing `.raw` will return rows as an array with `Buffer` values for each column, instead of objects.\n\nThis can be useful for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.\n\n### Queries in Files\n#### `await sql.file(path, [args], [options]) -> Result[]`\n\nUsing a file for a query is also supported with optional parameters to use if the file includes `$1, $2, etc`\n\n```js\nconst result = await sql.file('query.sql', ['Murray', 68])\n```\n\n### Multiple statements in one query\n#### ```await sql``.simple()```\n\nThe postgres wire protocol supports [\"simple\"](https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.6.7.4) and [\"extended\"](https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY) queries. \"simple\" queries supports multiple statements, but does not support any dynamic parameters. \"extended\" queries support parameters but only one statement. To use \"simple\" queries you can use\n```sql``.simple()```. That will create it as a simple query.\n\n```js\nawait sql`select 1; select 2;`.simple()\n```\n\n### Copy to/from as Streams\n\nPostgres.js supports [`COPY ...`](https://www.postgresql.org/docs/14/sql-copy.html) queries, which are exposed as [Node.js streams](https://nodejs.org/api/stream.html).\n\n#### ```await sql`copy ... from stdin`.writable() -> Writable```\n\n```js\nimport { pipeline } from 'node:stream/promises'\n\n// Stream of users with the default tab delimitated cells and new-line delimitated rows\nconst userStream = Readable.from([\n  'Murray\\t68\\n',\n  'Walter\\t80\\n'\n])\n\nconst query = await sql`copy users (name, age) from stdin`.writable()\nawait pipeline(userStream, query);\n```\n\n#### ```await sql`copy ... to stdout`.readable() -> Readable```\n\n##### Using Stream Pipeline\n```js\nimport { pipeline } from 'node:stream/promises'\nimport { createWriteStream } from 'node:fs'\n\nconst readableStream = await sql`copy users (name, age) to stdout`.readable()\nawait pipeline(readableStream, createWriteStream('output.tsv'))\n// output.tsv content: `Murray\\t68\\nWalter\\t80\\n`\n```\n\n##### Using `for await...of`\n```js\nconst readableStream = await sql`\n  copy (\n    select name, age\n    from users\n    where age = 68\n  ) to stdout\n`.readable()\nfor await (const chunk of readableStream) {\n  // chunk.toString() === `Murray\\t68\\n`\n}\n```\n\n> **NOTE** This is a low-level API which does not provide any type safety. To make this work, you must match your [`copy query` parameters](https://www.postgresql.org/docs/14/sql-copy.html) correctly to your [Node.js stream read or write](https://nodejs.org/api/stream.html) code. Ensure [Node.js stream backpressure](https://nodejs.org/en/learn/modules/backpressuring-in-streams) is handled correctly to avoid memory exhaustion.\n\n### Canceling Queries in Progress\n\nPostgres.js supports, [canceling queries in progress](https://www.postgresql.org/docs/7.1/protocol-protocol.html#AEN39000). It works by opening a new connection with a protocol level startup message to cancel the current query running on a specific connection. That means there is no guarantee that the query will be canceled, and due to the possible race conditions it might even result in canceling another query. This is fine for long running queries, but in the case of high load and fast queries it might be better to simply ignore results instead of canceling.\n\n```js\nconst query = sql`select pg_sleep 100`.execute()\nsetTimeout(() => query.cancel(), 100)\nconst result = await query\n```\n\n### Execute\n\n#### ```await sql``.execute()```\n\nThe lazy Promise implementation in Postgres.js is what allows it to distinguish [Nested Fragments](#building-queries) from the main outer query. This also means that queries are always executed at the earliest in the following tick. If you have a specific need to execute the query in the same tick, you can call `.execute()`\n\n### Unsafe raw string queries\n\n<details>\n<summary>Advanced unsafe use cases</summary>\n\n### `await sql.unsafe(query, [args], [options]) -> Result[]`\n\nIf you know what you're doing, you can use `unsafe` to pass any string you'd like to postgres. Please note that this can lead to SQL injection if you're not careful.\n\n```js\nsql.unsafe('select ' + danger + ' from users where id = ' + dragons)\n```\n\nBy default, `sql.unsafe` assumes the `query` string is sufficiently dynamic that prepared statements do not make sense, and so defaults them to off. If you'd like to re-enable prepared statements, you can pass `{ prepare: true }`.\n\nYou can also nest `sql.unsafe` within a safe `sql` expression.  This is useful if only part of your fraction has unsafe elements.\n\n```js\nconst triggerName = 'friend_created'\nconst triggerFnName = 'on_friend_created'\nconst eventType = 'insert'\nconst schema_name = 'app'\nconst table_name = 'friends'\n\nawait sql`\n  create or replace trigger ${sql(triggerName)}\n  after ${sql.unsafe(eventType)} on ${sql.unsafe(`${schema_name}.${table_name}`)}\n  for each row\n  execute function ${sql(triggerFnName)}()\n`\n\nawait sql`\n  create role friend_service with login password ${sql.unsafe(`'${password}'`)}\n`\n```\n\n</details>\n\n## Transactions\n\n#### BEGIN / COMMIT `await sql.begin([options = ''], fn) -> fn()`\n\nUse `sql.begin` to start a new transaction. Postgres.js will reserve a connection for the transaction and supply a scoped `sql` instance for all transaction uses in the callback function. `sql.begin` will resolve with the returned value from the callback function.\n\n`BEGIN` is automatically sent with the optional options, and if anything fails `ROLLBACK` will be called so the connection can be released and execution can continue.\n\n```js\nconst [user, account] = await sql.begin(async sql => {\n  const [user] = await sql`\n    insert into users (\n      name\n    ) values (\n      'Murray'\n    )\n    returning *\n  `\n\n  const [account] = await sql`\n    insert into accounts (\n      user_id\n    ) values (\n      ${ user.user_id }\n    )\n    returning *\n  `\n\n  return [user, account]\n})\n```\n\nDo note that you can often achieve the same result using [`WITH` queries (Common Table Expressions)](https://www.postgresql.org/docs/current/queries-with.html) instead of using transactions.\n\nIt's also possible to pipeline the requests in a transaction if needed by returning an array with queries from the callback function like this:\n\n```js\nconst result = await sql.begin(sql => [\n  sql`update ...`,\n  sql`update ...`,\n  sql`insert ...`\n])\n```\n\n#### SAVEPOINT `await sql.savepoint([name], fn) -> fn()`\n\n```js\nsql.begin('read write', async sql => {\n  const [user] = await sql`\n    insert into users (\n      name\n    ) values (\n      'Murray'\n    )\n  `\n\n  const [account] = (await sql.savepoint(sql =>\n    sql`\n      insert into accounts (\n        user_id\n      ) values (\n        ${ user.user_id }\n      )\n    `\n  ).catch(err => {\n    // Account could not be created. ROLLBACK SAVEPOINT is called because we caught the rejection.\n  })) || []\n\n  return [user, account]\n})\n.then(([user, account]) => {\n  // great success - COMMIT succeeded\n})\n.catch(() => {\n  // not so good - ROLLBACK was called\n})\n```\n\n\n#### PREPARE TRANSACTION `await sql.prepare([name]) -> fn()`\n\nIndicates that the transactions should be prepared using the [`PREPARE TRANSACTION [NAME]`](https://www.postgresql.org/docs/current/sql-prepare-transaction.html) statement\ninstead of being committed.\n\n```js\nsql.begin('read write', async sql => {\n  const [user] = await sql`\n    insert into users (\n      name\n    ) values (\n      'Murray'\n    )\n  `\n\n  await sql.prepare('tx1')\n})\n```\n\n## Data Transformation\n\nPostgres.js allows for transformation of the data passed to or returned from a query by using the `transform` option.\n\nBuilt in transformation functions are:\n\n* For camelCase - `postgres.camel`, `postgres.toCamel`, `postgres.fromCamel`\n* For PascalCase - `postgres.pascal`, `postgres.toPascal`, `postgres.fromPascal`\n* For Kebab-Case - `postgres.kebab`, `postgres.toKebab`, `postgres.fromKebab`\n\nThese built in transformations will only convert to/from snake_case. For example, using `{ transform: postgres.toCamel }` will convert the column names to camelCase only if the column names are in snake_case to begin with. `{ transform: postgres.fromCamel }` will convert camelCase only to snake_case.\n\nBy default, using `postgres.camel`, `postgres.pascal` and `postgres.kebab` will perform a two-way transformation - both the data passed to the query and the data returned by the query will be transformed:\n\n```js\n// Transform the column names to and from camel case\nconst sql = postgres({ transform: postgres.camel })\n\nawait sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER, b_test TEXT)`\nawait sql`INSERT INTO camel_case ${ sql([{ aTest: 1, bTest: 1 }]) }`\nconst data = await sql`SELECT ${ sql('aTest', 'bTest') } FROM camel_case`\n\nconsole.log(data) // [ { aTest: 1, bTest: '1' } ]\n```\n\nTo only perform half of the transformation (eg. only the transformation **to** or **from** camel case), use the other transformation functions:\n\n```js\n// Transform the column names only to camel case\n// (for the results that are returned from the query)\npostgres({ transform: postgres.toCamel })\n\nawait sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`\nawait sql`INSERT INTO camel_case ${ sql([{ a_test: 1 }]) }`\nconst data = await sql`SELECT a_test FROM camel_case`\n\nconsole.log(data) // [ { aTest: 1 } ]\n```\n\n```js\n// Transform the column names only from camel case\n// (for interpolated inserts, updates, and selects)\nconst sql = postgres({ transform: postgres.fromCamel })\n\nawait sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`\nawait sql`INSERT INTO camel_case ${ sql([{ aTest: 1 }]) }`\nconst data = await sql`SELECT ${ sql('aTest') } FROM camel_case`\n\nconsole.log(data) // [ { a_test: 1 } ]\n```\n\n> Note that Postgres.js does not rewrite the static parts of the tagged template strings. So to transform column names in your queries, the `sql()` helper must be used - eg. `${ sql('columnName') }` as in the examples above.\n\n### Transform `undefined` Values\n\nBy default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed\n\n```js\n// Transform the column names to and from camel case\nconst sql = postgres({\n  transform: {\n    undefined: null\n  }\n})\n\nawait sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`\nawait sql`INSERT INTO transform_undefined ${ sql([{ a_test: undefined }]) }`\nconst data = await sql`SELECT a_test FROM transform_undefined`\n\nconsole.log(data) // [ { a_test: null } ]\n```\n\nTo combine with the built in transform functions, spread the transform in the `transform` object:\n\n```js\n// Transform the column names to and from camel case\nconst sql = postgres({\n  transform: {\n    ...postgres.camel,\n    undefined: null\n  }\n})\n\nawait sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`\nawait sql`INSERT INTO transform_undefined ${ sql([{ aTest: undefined }]) }`\nconst data = await sql`SELECT ${ sql('aTest') } FROM transform_undefined`\n\nconsole.log(data) // [ { aTest: null } ]\n```\n\n### Custom Transform Functions\n\nTo specify your own transformation functions, you can use the `column`, `value` and `row` options inside of `transform`, each an object possibly including `to` and `from` keys:\n\n* `to`: The function to transform the outgoing query column name to, i.e `SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.\n* `from`: The function to transform the incoming query result column name to, see example below.\n\n> Both parameters are optional, if not provided, the default transformation function will be used.\n\n```js\n// Implement your own functions, look at postgres.toCamel, etc\n// as a reference:\n// https://github.com/porsager/postgres/blob/4241824ffd7aa94ffb482e54ca9f585d9d0a4eea/src/types.js#L310-L328\nfunction transformColumnToDatabase() { /* ... */ }\nfunction transformColumnFromDatabase() { /* ... */ }\n\nconst sql = postgres({\n  transform: {\n    column: {\n      to: transformColumnToDatabase,\n      from: transformColumnFromDatabase,\n    },\n    value: { /* ... */ },\n    row: { /* ... */ }\n  }\n})\n```\n\n## Listen & notify\n\nWhen you call `.listen`, a dedicated connection will be created to ensure that you receive notifications instantly. This connection will be used for any further calls to `.listen`. The connection will automatically reconnect according to a backoff reconnection pattern to not overload the database server.\n\n### Listen `await sql.listen(channel, onnotify, [onlisten]) -> { state }`\n`.listen` takes the channel name, a function to handle each notify, and an optional function to run every time listen is registered and ready (happens on initial connect and reconnects). It returns a promise which resolves once the `LISTEN` query to Postgres completes, or if there is already a listener active.\n\n```js\nawait sql.listen('news', payload => {\n  const json = JSON.parse(payload)\n  console.log(json.this) // logs 'is'\n})\n```\n\nThe optional `onlisten` method is great to use for a very simply queue mechanism:\n\n```js\nawait sql.listen(\n  'jobs',\n  (x) => run(JSON.parse(x)),\n  ( ) => sql`select unfinished_jobs()`.forEach(run)\n)\n\nfunction run(job) {\n  // And here you do the work you please\n}\n```\n### Notify `await sql.notify(channel, payload) -> Result[]`\nNotify can be done as usual in SQL, or by using the `sql.notify` method.\n```js\nsql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))\n```\n\n## Realtime subscribe\n\nPostgres.js implements the logical replication protocol of PostgreSQL to support subscription to real-time updates of `insert`, `update` and `delete` operations.\n\n> **NOTE** To make this work you must [create the proper publications in your database](https://www.postgresql.org/docs/current/sql-createpublication.html), enable logical replication by setting `wal_level = logical` in `postgresql.conf` and connect using either a replication or superuser.\n\n### Quick start\n\n#### Create a publication (eg. in migration)\n```sql\nCREATE PUBLICATION alltables FOR ALL TABLES\n```\n\n#### Subscribe to updates\n```js\nconst sql = postgres({ publications: 'alltables' })\n\nconst { unsubscribe } = await sql.subscribe(\n  'insert:events',\n  (row, { command, relation, key, old }) => {\n    // Callback function for each row change\n    // tell about new event row over eg. websockets or do something else\n  },\n  () => {\n    // Callback on initial connect and potential reconnects\n  }\n)\n```\n\n### Subscribe pattern\n\nYou can subscribe to specific operations, tables, or even rows with primary keys.\n\n#### `operation`      `:` `schema` `.` `table` `=` `primary_key`\n\n**`operation`** is one of ``` * | insert | update | delete ``` and defaults to `*`\n\n**`schema`** defaults to `public`\n\n**`table`** is a specific table name and defaults to `*`\n\n**`primary_key`** can be used to only subscribe to specific rows\n\n### Examples\n\n```js\nsql.subscribe('*',                () => /* everything */ )\nsql.subscribe('insert',           () => /* all inserts */ )\nsql.subscribe('*:users',          () => /* all operations on the public.users table */ )\nsql.subscribe('delete:users',     () => /* all deletes on the public.users table */ )\nsql.subscribe('update:users=1',   () => /* all updates on the users row with a primary key = 1 */ )\n```\n\n## Numbers, bigint, numeric\n\n`Number` in javascript is only able to represent 2<sup>53</sup>-1 safely which means that types in PostgreSQLs like `bigint` and `numeric` won't fit into `Number`.\n\nSince Node.js v10.4 we can use [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) to match the PostgreSQL type `bigint` which is returned for eg. `count(*)`. Unfortunately, it doesn't work with `JSON.stringify` out of the box, so Postgres.js will return it as a string.\n\nIf you want to use `BigInt` you can add this custom type:\n\n```js\nconst sql = postgres({\n  types: {\n    bigint: postgres.BigInt\n  }\n})\n```\n\nThere is currently no guaranteed way to handle `numeric` / `decimal` types in native Javascript. **These [and similar] types will be returned as a `string`**. The best way in this case is to use  [custom types](#custom-types).\n\n## Result Array\n\nThe `Result` Array returned from queries is a custom array allowing for easy destructuring or passing on directly to JSON.stringify or general Array usage. It includes the following properties.\n\n### .count\n\nThe `count` property is the number of affected rows returned by the database. This is useful for insert, update and delete operations to know the number of rows since .length will be 0 in these cases if not using `RETURNING ...`.\n\n### .command\n\nThe `command` run by the query - eg. one of `SELECT`, `UPDATE`, `INSERT`, `DELETE`\n\n### .columns\n\nThe `columns` returned by the query useful to determine types, or map to the result values when using `.values()`\n\n```js\n{\n  name  : String,    // Column name,\n  type  : oid,       // PostgreSQL oid column type\n  parser: Function   // The function used by Postgres.js for parsing\n}\n```\n\n### .statement\n\nThe `statement` contains information about the statement implicitly created by Postgres.js.\n\n```js\n{\n  name    : String,  // The auto generated statement name\n  string  : String,  // The actual query string executed\n  types   : [oid],   // An array of oid expected as input parameters\n  columns : [Column] // Array of columns - same as Result.columns\n}\n```\n\n### .state\n\nThis is the state `{ pid, secret }` of the connection that executed the query.\n\n## Connection details\n\n### All Postgres options\n\n```js\nconst sql = postgres('postgres://username:password@host:port/database', {\n  host                 : '',            // Postgres ip address[es] or domain name[s]\n  port                 : 5432,          // Postgres server port[s]\n  path                 : '',            // unix socket path (usually '/tmp')\n  database             : '',            // Name of database to connect to\n  username             : '',            // Username of database user\n  password             : '',            // Password of database user\n  ssl                  : false,         // true, prefer, require, tls.connect options\n  sslnegotiation       : null,          // direct\n  max                  : 10,            // Max number of connections\n  max_lifetime         : null,          // Max lifetime in seconds (more info below)\n  idle_timeout         : 0,             // Idle connection timeout in seconds\n  connect_timeout      : 30,            // Connect timeout in seconds\n  prepare              : true,          // Automatic creation of prepared statements\n  types                : [],            // Array of custom types, see more below\n  onnotice             : fn,            // Default console.log, set false to silence NOTICE\n  onparameter          : fn,            // (key, value) when server param change\n  debug                : fn,            // Is called with (connection, query, params, types)\n  socket               : fn,            // fn returning custom socket to use\n  transform            : {\n    undefined          : undefined,     // Transforms undefined values (eg. to null)\n    column             : fn,            // Transforms incoming column names\n    value              : fn,            // Transforms incoming row values\n    row                : fn             // Transforms entire rows\n  },\n  connection           : {\n    application_name   : 'postgres.js', // Default application_name\n    ...                                 // Other connection parameters, see https://www.postgresql.org/docs/current/runtime-config-client.html\n  },\n  target_session_attrs : null,          // Use 'read-write' with multiple hosts to\n                                        // ensure only connecting to primary\n  fetch_types          : true,          // Automatically fetches types on connect\n                                        // on initial connection.\n})\n```\n\nNote that `max_lifetime = 60 * (30 + Math.random() * 30)` by default. This resolves to an interval between 30 and 60 minutes to optimize for the benefits of prepared statements **and** working nicely with Linux's OOM killer.\n\n### Dynamic passwords\n\nWhen clients need to use alternative authentication schemes such as access tokens or connections to databases with rotating passwords, provide either a synchronous or asynchronous function that will resolve the dynamic password value at connection time.\n\n```js\nconst sql = postgres(url, {\n  // Other connection config\n  ...\n  // Password function for the database user\n  password : async () => await signer.getAuthToken(),\n})\n```\n\n### SSL\n\nAlthough [vulnerable to MITM attacks](https://security.stackexchange.com/a/229297/174913), a common configuration for the `ssl` option for some cloud providers is to set `rejectUnauthorized` to `false` (if `NODE_ENV` is `production`):\n\n```js\nconst sql =\n  process.env.NODE_ENV === 'production'\n    ? // \"Unless you're using a Private or Shield Heroku Postgres database, Heroku Postgres does not currently support verifiable certificates\"\n      // https://help.heroku.com/3DELT3RK/why-can-t-my-third-party-utility-connect-to-heroku-postgres-with-ssl\n      postgres({ ssl: { rejectUnauthorized: false } })\n    : postgres()\n```\n\nFor more information regarding `ssl` with `postgres`, check out the [Node.js documentation for tls](https://nodejs.org/dist/latest-v16.x/docs/api/tls.html#new-tlstlssocketsocket-options).\n\n\n### Multi-host connections - High Availability (HA)\n\nMultiple connection strings can be passed to `postgres()` in the form of `postgres('postgres://localhost:5432,localhost:5433', ...)`. This works the same as native the `psql` command. Read more at [multiple host URIs](https://www.postgresql.org/docs/13/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS).\n\nConnections will be attempted in order of the specified hosts/ports. On a successful connection, all retries will be reset. This ensures that hosts can come up and down seamlessly.\n\nIf you specify `target_session_attrs: 'primary'` or `PGTARGETSESSIONATTRS=primary` Postgres.js will only connect to the primary host, allowing for zero downtime failovers.\n\n### The Connection Pool\n\nConnections are created lazily once a query is created. This means that simply doing const `sql = postgres(...)` won't have any effect other than instantiating a new `sql` instance.\n\n> No connection will be made until a query is made.\n\nFor example:\n\n```js\nconst sql = postgres() // no connections are opened\n\nawait sql`...` // one connection is now opened\nawait sql`...` // previous opened connection is reused\n\n// two connections are opened now\nawait Promise.all([\n  sql`...`,\n  sql`...`\n])\n```\n\n> When there are high amount of concurrent queries, `postgres` will open as many connections as needed up until `max` number of connections is reached. By default `max` is 10. This can be changed by setting `max` in the `postgres()` call. Example - `postgres('connectionURL', { max: 20 })`.\n\nThis means that we get a much simpler story for error handling and reconnections. Queries will be sent over the wire immediately on the next available connection in the pool. Connections are automatically taken out of the pool if you start a transaction using `sql.begin()`, and automatically returned to the pool once your transaction is done.\n\nAny query which was already sent over the wire will be rejected if the connection is lost. It'll automatically defer to the error handling you have for that query, and since connections are lazy it'll automatically try to reconnect the next time a query is made. The benefit of this is no weird generic \"onerror\" handler that tries to get things back to normal, and also simpler application code since you don't have to handle errors out of context.\n\nThere are no guarantees about queries executing in order unless using a transaction with `sql.begin()` or setting `max: 1`. Of course doing a series of queries, one awaiting the other will work as expected, but that's just due to the nature of js async/promise handling, so it's not necessary for this library to be concerned with ordering.\n\nSince this library automatically creates prepared statements, it also has a default max lifetime for connections to prevent memory bloat on the database itself. This is a random interval for each connection between 45 and 90 minutes. This allows multiple connections to independently come up and down without affecting the service.\n\n### Connection timeout\n\nBy default, connections will not close until `.end()` is called. However, it may be useful to have them close automatically when:\n\n- re-instantiating multiple ` sql`` ` instances\n- using Postgres.js in a Serverless environment (Lambda, etc.)\n- using Postgres.js with a database service that automatically closes connections after some time (see [`ECONNRESET` issue](https://github.com/porsager/postgres/issues/179))\n\nThis can be done using the `idle_timeout` or `max_lifetime` options. These configuration options specify the number of seconds to wait before automatically closing an idle connection and the maximum time a connection can exist, respectively.\n\nFor example, to close a connection that has either been idle for 20 seconds or existed for more than 30 minutes:\n\n```js\nconst sql = postgres({\n  idle_timeout: 20,\n  max_lifetime: 60 * 30\n})\n```\n\n### Cloudflare Workers support\n\nPostgres.js has built-in support for the [TCP socket API](https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/) in Cloudflare Workers, which is [on-track](https://github.com/wintercg/proposal-sockets-api) to be standardized and adopted in Node.js and other JavaScript runtimes, such as Deno.\n\nYou can use Postgres.js directly in a Worker, or to benefit from connection pooling and query caching, via the [Hyperdrive](https://developers.cloudflare.com/hyperdrive/learning/connect-to-postgres/#driver-examples) service available to Workers by passing the Hyperdrive `connectionString` when creating a new `postgres` client as follows:\n\n```ts\n// Requires Postgres.js 3.4.0 or later\nimport postgres from 'postgres'\n\ninterface Env {\n    HYPERDRIVE: Hyperdrive;\n}\n\nexport default async fetch(req: Request, env: Env, ctx: ExecutionContext) {\n    // The Postgres.js library accepts a connection string directly\n    const sql = postgres(env.HYPERDRIVE.connectionString)\n    const results = await sql`SELECT * FROM users LIMIT 10`\n    return Response.json(results)\n}\n```\n\nIn `wrangler.toml` you will need to enable the `nodejs_compat` compatibility flag to allow Postgres.js to operate in the Workers environment:\n\n```toml\ncompatibility_flags = [\"nodejs_compat\"]\n```\n\n### Auto fetching of array types\n\nPostgres.js will automatically fetch table/array-type information when it first connects to a database.\n\nIf you have revoked access to `pg_catalog` this feature will no longer work and will need to be disabled.\n\nYou can disable this feature by setting `fetch_types` to `false`.\n\n### Environmental variables\n\nIt is also possible to connect to the database without a connection string or any options. Postgres.js will fall back to the common environment variables used by `psql` as in the table below:\n\n```js\nconst sql = postgres()\n```\n\n| Option             | Environment Variables    |\n| ------------------ | ------------------------ |\n| `host`             | `PGHOST`                 |\n| `port`             | `PGPORT`                 |\n| `database`         | `PGDATABASE`             |\n| `username`         | `PGUSERNAME` or `PGUSER` |\n| `password`         | `PGPASSWORD`             |\n| `application_name` | `PGAPPNAME`              |\n| `idle_timeout`     | `PGIDLE_TIMEOUT`         |\n| `connect_timeout`  | `PGCONNECT_TIMEOUT`      |\n\n### Prepared statements\n\nPrepared statements will automatically be created for any queries where it can be inferred that the query is static. This can be disabled by using the `prepare: false` option. For instance — this is useful when [using PGBouncer in `transaction mode`](https://github.com/porsager/postgres/issues/93#issuecomment-656290493).\n\n**update**: [since 1.21.0](https://www.pgbouncer.org/2023/10/pgbouncer-1-21-0)\nPGBouncer supports protocol-level named prepared statements when [configured\nproperly](https://www.pgbouncer.org/config.html#max_prepared_statements)\n\n## Custom Types\n\nYou can add ergonomic support for custom types, or simply use `sql.typed(value, type)` inline, where type is the PostgreSQL `oid` for the type and the correctly serialized string. _(`oid` values for types can be found in the `pg_catalog.pg_type` table.)_\n\nAdding Query helpers is the cleanest approach which can be done like this:\n\n```js\nconst sql = postgres({\n  types: {\n    rect: {\n      // The pg_types oid to pass to the db along with the serialized value.\n      to        : 1337,\n\n      // An array of pg_types oids to handle when parsing values coming from the db.\n      from      : [1337],\n\n      //Function that transform values before sending them to the db.\n      serialize : ({ x, y, width, height }) => [x, y, width, height],\n\n      // Function that transforms values coming from the db.\n      parse     : ([x, y, width, height]) => { x, y, width, height }\n    }\n  }\n})\n\n// Now you can use sql.typed.rect() as specified above\nconst [custom] = await sql`\n  insert into rectangles (\n    name,\n    rect\n  ) values (\n    'wat',\n    ${ sql.typed.rect({ x: 13, y: 37, width: 42, height: 80 }) }\n  )\n  returning *\n`\n\n// custom = { name: 'wat', rect: { x: 13, y: 37, width: 42, height: 80 } }\n\n```\n\n### Custom socket\n\nEasily do in-process ssh tunneling to your database by providing a custom socket for Postgres.js to use. The function (optionally async) must return a socket-like duplex stream.\n\nHere's a sample using [ssh2](https://github.com/mscdex/ssh2)\n\n```js\nimport ssh2 from 'ssh2'\n\nconst sql = postgres({\n  ...options,\n  socket: ({ host: [host], port: [port] }) => new Promise((resolve, reject) => {\n    const ssh = new ssh2.Client()\n    ssh\n    .on('error', reject)\n    .on('ready', () =>\n      ssh.forwardOut('127.0.0.1', 12345, host, port,\n        (err, socket) => err ? reject(err) : resolve(socket)\n      )\n    )\n    .connect(sshOptions)\n  })\n})\n```\n\n## Teardown / Cleanup\n\nTo ensure proper teardown and cleanup on server restarts use `await sql.end()` before `process.exit()`.\n\nCalling `sql.end()` will reject new queries and return a Promise which resolves when all queries are finished and the underlying connections are closed. If a `{ timeout }` option is provided any pending queries will be rejected once the timeout (in seconds) is reached and the connections will be destroyed.\n\n#### Sample shutdown using [Prexit](https://github.com/porsager/prexit)\n\n```js\nimport prexit from 'prexit'\n\nprexit(async () => {\n  await sql.end({ timeout: 5 })\n  await new Promise(r => server.close(r))\n})\n```\n\n## Reserving connections\n\n### `await sql.reserve()`\n\nThe `reserve` method pulls out a connection from the pool, and returns a client that wraps the single connection. This can be used for running queries on an isolated connection.\n\n```ts\nconst reserved = await sql.reserve()\nawait reserved`select * from users`\nawait reserved.release()\n```\n\n### `reserved.release()`\n\nOnce you have finished with the reserved connection, call `release` to add it back to the pool.\n\n## Error handling\n\nErrors are all thrown to related queries and never globally. Errors coming from database itself are always in the [native Postgres format](https://www.postgresql.org/docs/current/errcodes-appendix.html), and the same goes for any [Node.js errors](https://nodejs.org/api/errors.html#errors_common_system_errors) eg. coming from the underlying connection.\n\nQuery errors will contain a stored error with the origin of the query to aid in tracing errors.\n\nQuery errors will also contain the `query` string and the `parameters`. These are not enumerable to avoid accidentally leaking confidential information in logs. To log these it is required to specifically access `error.query` and `error.parameters`, or set `debug: true` in options.\n\nThere are also the following errors specifically for this library.\n\n##### UNSAFE_TRANSACTION\n> Only use sql.begin or max: 1\n\nTo ensure statements in a transaction runs on the same connection (which is required for them to run inside the transaction), you must use [`sql.begin(...)`](#transactions) or only allow a single connection in options (`max: 1`).\n\n##### UNDEFINED_VALUE\n> Undefined values are not allowed\n\nPostgres.js won't accept `undefined` as values in tagged template queries since it becomes ambiguous what to do with the value. If you want to set something to null, use `null` explicitly.\n\n##### MESSAGE_NOT_SUPPORTED\n> X (X) is not supported\n\nWhenever a message is received from Postgres which is not supported by this library. Feel free to file an issue if you think something is missing.\n\n##### MAX_PARAMETERS_EXCEEDED\n> Max number of parameters (65534) exceeded\n\nThe postgres protocol doesn't allow more than 65534 (16bit) parameters. If you run into this issue there are various workarounds such as using `sql([...])` to escape values instead of passing them as parameters.\n\n##### SASL_SIGNATURE_MISMATCH\n> Message type X not supported\n\nWhen using SASL authentication the server responds with a signature at the end of the authentication flow which needs to match the one on the client. This is to avoid [man-in-the-middle attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack). If you receive this error the connection was canceled because the server did not reply with the expected signature.\n\n##### NOT_TAGGED_CALL\n> Query not called as a tagged template literal\n\nMaking queries has to be done using the sql function as a [tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates). This is to ensure parameters are serialized and passed to Postgres as query parameters with correct types and to avoid SQL injection.\n\n##### AUTH_TYPE_NOT_IMPLEMENTED\n> Auth type X not implemented\n\nPostgres supports many different authentication types. This one is not supported.\n\n##### CONNECTION_CLOSED\n> write CONNECTION_CLOSED host:port\n\nThis error is thrown if the connection was closed without an error. This should not happen during normal operations, so please create an issue if this was unexpected.\n\n##### CONNECTION_ENDED\n> write CONNECTION_ENDED host:port\n\nThis error is thrown if the user has called [`sql.end()`](#teardown--cleanup) and performed a query afterward.\n\n##### CONNECTION_DESTROYED\n> write CONNECTION_DESTROYED host:port\n\nThis error is thrown for any queries that were pending when the timeout to [`sql.end({ timeout: X })`](#teardown--cleanup) was reached.\n\n##### CONNECT_TIMEOUT\n> write CONNECT_TIMEOUT host:port\n\nThis error is thrown if the startup phase of the connection (tcp, protocol negotiation, and auth) took more than the default 30 seconds or what was specified using `connect_timeout` or `PGCONNECT_TIMEOUT`.\n\n##### COPY_IN_PROGRESS\n> You cannot execute queries during copy\n\nThis error is thrown if trying to run a query during a copy operation (writable / readable).\n\n## TypeScript support\n\n`postgres` has TypeScript support. You can pass a row list type for your queries in this way:\n```ts\ninterface User {\n  id: number\n  name: string\n}\n\nconst users = await sql<User[]>`SELECT * FROM users`\nusers[0].id // ok => number\nusers[1].name // ok => string\nusers[0].invalid // fails: `invalid` does not exists on `User`\n```\n\nHowever, be sure to check the array length to avoid accessing properties of `undefined` rows:\n```ts\nconst users = await sql<User[]>`SELECT * FROM users WHERE id = ${id}`\nif (!users.length)\n  throw new Error('Not found')\nreturn users[0]\n```\n\nYou can also prefer destructuring when you only care about a fixed number of rows.\nIn this case, we recommend you to prefer using tuples to handle `undefined` properly:\n```ts\nconst [user]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}`\nif (!user) // => User | undefined\n  throw new Error('Not found')\nreturn user // => User\n\n// NOTE:\nconst [first, second]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}` // fails: `second` does not exist on `[User?]`\nconst [first, second] = await sql<[User?]>`SELECT * FROM users WHERE id = ${id}` // don't fail : `second: User | undefined`\n```\n\nWe do our best to type all the public API, however types are not always updated when features are added or changed. Feel free to open an issue if you have trouble with types.\n\n## Migration tools\n\nPostgres.js doesn't come with any migration solution since it's way out of scope, but here are some modules that support Postgres.js for migrations:\n\n- https://github.com/porsager/postgres-shift\n- https://github.com/lukeed/ley\n- https://github.com/JAForbes/pgmg\n\n## Thank you\n\nA really big thank you to [@JAForbes](https://twitter.com/jmsfbs) who introduced me to Postgres and still holds my hand navigating all the great opportunities we have.\n\nThanks to [@ACXgit](https://twitter.com/andreacoiutti) for initial tests and dogfooding.\n\nAlso thanks to [Ryan Dahl](https://github.com/ry) for letting me have the `postgres` npm package name.\n"
  },
  {
    "path": "deno/mod.js",
    "content": "// @deno-types=\"./types/index.d.ts\"\nexport { default } from './src/index.js'\n"
  },
  {
    "path": "deno/package.json",
    "content": "{\"type\":\"commonjs\"}"
  },
  {
    "path": "deno/polyfills.js",
    "content": "/* global Deno */\n\nimport { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nimport { isIP } from 'https://deno.land/std@0.132.0/node/net.ts'\n\nconst events = () => ({ data: [], error: [], drain: [], connect: [], secureConnect: [], close: [] })\n\nclass Socket {\n  constructor() {\n    return createSocket()\n  }\n}\n\nfunction createSocket() {\n  let paused\n    , resume\n    , keepAlive\n\n  const socket = {\n    error,\n    success,\n    readyState: 'open',\n    setKeepAlive: x => {\n      keepAlive = x\n      socket.raw && socket.raw.setKeepAlive && socket.raw.setKeepAlive(x)\n    },\n    connect: (port, hostname) => {\n      socket.raw = null\n      socket.readyState = 'connecting'\n      typeof port === 'string'\n        ? Deno.connect({ transport: 'unix', path: socket.path = port }).then(success, error)\n        : Deno.connect({ transport: 'tcp', port: socket.port = port, hostname: socket.hostname = hostname || 'localhost' }).then(success, error) // eslint-disable-line\n      return socket\n    },\n    pause: () => {\n      paused = new Promise(r => resume = r)\n    },\n    resume: () => {\n      resume && resume()\n      paused = null\n    },\n    isPaused: () => !!paused,\n    removeAllListeners: () => socket.events = events(),\n    events: events(),\n    raw: null,\n    on: (x, fn) => socket.events[x].push(fn),\n    once: (x, fn) => {\n      if (x === 'data')\n        socket.break = true\n      const e = socket.events[x]\n      e.push(once)\n      once.once = fn\n      function once(...args) {\n        fn(...args)\n        e.indexOf(once) > -1 && e.splice(e.indexOf(once), 1)\n      }\n    },\n    removeListener: (x, fn) => {\n      socket.events[x] = socket.events[x].filter(x => x !== fn && x.once !== fn)\n    },\n    write: (x, cb) => {\n      socket.raw.write(x).then(l => {\n        l < x.length\n          ? socket.write(x.slice(l), cb)\n          : (cb && cb(null))\n      }).catch(err => {\n        cb && cb()\n        call(socket.events.error, err)\n      })\n      return false\n    },\n    destroy: () => close(),\n    end: (x) => {\n      x && socket.write(x)\n      close()\n    }\n  }\n\n  return socket\n\n  async function success(raw) {\n    if (socket.readyState !== 'connecting')\n      return raw.close()\n\n    const encrypted = socket.encrypted\n    socket.raw = raw\n    keepAlive != null && raw.setKeepAlive && raw.setKeepAlive(keepAlive)\n    socket.readyState = 'open'\n    socket.encrypted\n      ? call(socket.events.secureConnect)\n      : call(socket.events.connect)\n\n    const b = new Uint8Array(1024)\n    let result\n\n    try {\n      while ((result = socket.readyState === 'open' && await raw.read(b))) {\n        call(socket.events.data, Buffer.from(b.subarray(0, result)))\n        if (!encrypted && socket.break && (socket.break = false, b[0] === 83))\n          return socket.break = false\n        paused && await paused\n      }\n    } catch (e) {\n      if (e instanceof Deno.errors.BadResource === false)\n        error(e)\n    }\n\n    if (!socket.encrypted || encrypted)\n      closed()\n  }\n\n  function close() {\n    try {\n      socket.raw && socket.raw.close()\n    } catch (e) {\n      if (e instanceof Deno.errors.BadResource === false)\n        call(socket.events.error, e)\n    }\n  }\n\n  function closed() {\n    if (socket.readyState === 'closed')\n      return\n\n    socket.break = socket.encrypted = false\n    socket.readyState = 'closed'\n    call(socket.events.close)\n  }\n\n  function error(err) {\n    call(socket.events.error, err)\n    socket.raw\n      ? close()\n      : closed()\n  }\n\n  function call(xs, x) {\n    xs.slice().forEach(fn => fn(x))\n  }\n}\n\nexport const net = {\n  isIP,\n  createServer() {\n    const server =  {\n      address() {\n        return { port: 9876 }\n      },\n      async listen() {\n        server.raw = Deno.listen({ port: 9876, transport: 'tcp' })\n        for await (const conn of server.raw)\n          setTimeout(() => conn.close(), 500)\n      },\n      close() {\n        server.raw.close()\n      }\n    }\n    return server\n  },\n  Socket\n}\n\nexport const tls = {\n  connect({ socket, ...options }) {\n    socket.encrypted = true\n    socket.readyState = 'connecting'\n    Deno.startTls(socket.raw, { hostname: socket.hostname, ...options })\n      .then(socket.success, socket.error)\n    socket.raw = null\n    return socket\n  }\n}\n\nlet ids = 1\nconst tasks = new Set()\nexport const setImmediate = fn => {\n  const id = ids++\n  tasks.add(id)\n  queueMicrotask(() => {\n    if (tasks.has(id)) {\n      fn()\n      tasks.delete(id)\n    }\n  })\n  return id\n}\n\nexport const clearImmediate = id => tasks.delete(id)\n\n"
  },
  {
    "path": "deno/src/bytes.js",
    "content": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nconst size = 256\nlet buffer = Buffer.allocUnsafe(size)\n\nconst messages = 'BCcDdEFfHPpQSX'.split('').reduce((acc, x) => {\n  const v = x.charCodeAt(0)\n  acc[x] = () => {\n    buffer[0] = v\n    b.i = 5\n    return b\n  }\n  return acc\n}, {})\n\nconst b = Object.assign(reset, messages, {\n  N: String.fromCharCode(0),\n  i: 0,\n  inc(x) {\n    b.i += x\n    return b\n  },\n  str(x) {\n    const length = Buffer.byteLength(x)\n    fit(length)\n    b.i += buffer.write(x, b.i, length, 'utf8')\n    return b\n  },\n  i16(x) {\n    fit(2)\n    buffer.writeUInt16BE(x, b.i)\n    b.i += 2\n    return b\n  },\n  i32(x, i) {\n    if (i || i === 0) {\n      buffer.writeUInt32BE(x, i)\n      return b\n    }\n    fit(4)\n    buffer.writeUInt32BE(x, b.i)\n    b.i += 4\n    return b\n  },\n  z(x) {\n    fit(x)\n    buffer.fill(0, b.i, b.i + x)\n    b.i += x\n    return b\n  },\n  raw(x) {\n    buffer = Buffer.concat([buffer.subarray(0, b.i), x])\n    b.i = buffer.length\n    return b\n  },\n  end(at = 1) {\n    buffer.writeUInt32BE(b.i - at, at)\n    const out = buffer.subarray(0, b.i)\n    b.i = 0\n    buffer = Buffer.allocUnsafe(size)\n    return out\n  }\n})\n\nexport default b\n\nfunction fit(x) {\n  if (buffer.length - b.i < x) {\n    const prev = buffer\n        , length = prev.length\n\n    buffer = Buffer.allocUnsafe(length + (length >> 1) + x)\n    prev.copy(buffer)\n  }\n}\n\nfunction reset() {\n  b.i = 0\n  return b\n}\n"
  },
  {
    "path": "deno/src/connection.js",
    "content": "import { HmacSha256 } from 'https://deno.land/std@0.132.0/hash/sha256.ts'\nimport { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nimport { setImmediate, clearImmediate } from '../polyfills.js'\nimport { net } from '../polyfills.js'\nimport { tls } from '../polyfills.js'\nimport crypto from 'https://deno.land/std@0.132.0/node/crypto.ts'\nimport Stream from 'https://deno.land/std@0.132.0/node/stream.ts'\n\n\nimport { stringify, handleValue, arrayParser, arraySerializer } from './types.js'\nimport { Errors } from './errors.js'\nimport Result from './result.js'\nimport Queue from './queue.js'\nimport { Query, CLOSE } from './query.js'\nimport b from './bytes.js'\n\nexport default Connection\n\nlet uid = 1\n\nconst Sync = b().S().end()\n    , Flush = b().H().end()\n    , SSLRequest = b().i32(8).i32(80877103).end(8)\n    , ExecuteUnnamed = Buffer.concat([b().E().str(b.N).i32(0).end(), Sync])\n    , DescribeUnnamed = b().D().str('S').str(b.N).end()\n    , noop = () => { /* noop */ }\n\nconst retryRoutines = new Set([\n  'FetchPreparedStatement',\n  'RevalidateCachedQuery',\n  'transformAssignedExpr'\n])\n\nconst errorFields = {\n  83  : 'severity_local',    // S\n  86  : 'severity',          // V\n  67  : 'code',              // C\n  77  : 'message',           // M\n  68  : 'detail',            // D\n  72  : 'hint',              // H\n  80  : 'position',          // P\n  112 : 'internal_position', // p\n  113 : 'internal_query',    // q\n  87  : 'where',             // W\n  115 : 'schema_name',       // s\n  116 : 'table_name',        // t\n  99  : 'column_name',       // c\n  100 : 'data type_name',    // d\n  110 : 'constraint_name',   // n\n  70  : 'file',              // F\n  76  : 'line',              // L\n  82  : 'routine'            // R\n}\n\nfunction Connection(options, queues = {}, { onopen = noop, onend = noop, onclose = noop } = {}) {\n  const {\n    sslnegotiation,\n    ssl,\n    max,\n    user,\n    host,\n    port,\n    database,\n    parsers,\n    transform,\n    onnotice,\n    onnotify,\n    onparameter,\n    max_pipeline,\n    keep_alive,\n    backoff,\n    target_session_attrs\n  } = options\n\n  const sent = Queue()\n      , id = uid++\n      , backend = { pid: null, secret: null }\n      , idleTimer = timer(end, options.idle_timeout)\n      , lifeTimer = timer(end, options.max_lifetime)\n      , connectTimer = timer(connectTimedOut, options.connect_timeout)\n\n  let socket = null\n    , cancelMessage\n    , errorResponse = null\n    , result = new Result()\n    , incoming = Buffer.alloc(0)\n    , needsTypes = options.fetch_types\n    , backendParameters = {}\n    , statements = {}\n    , statementId = Math.random().toString(36).slice(2)\n    , statementCount = 1\n    , closedTime = 0\n    , remaining = 0\n    , hostIndex = 0\n    , retries = 0\n    , length = 0\n    , delay = 0\n    , rows = 0\n    , serverSignature = null\n    , nextWriteTimer = null\n    , terminated = false\n    , incomings = null\n    , results = null\n    , initial = null\n    , ending = null\n    , stream = null\n    , chunk = null\n    , ended = null\n    , nonce = null\n    , query = null\n    , final = null\n\n  const connection = {\n    queue: queues.closed,\n    idleTimer,\n    connect(query) {\n      initial = query\n      reconnect()\n    },\n    terminate,\n    execute,\n    cancel,\n    end,\n    count: 0,\n    id\n  }\n\n  queues.closed && queues.closed.push(connection)\n\n  return connection\n\n  async function createSocket() {\n    let x\n    try {\n      x = options.socket\n        ? (await Promise.resolve(options.socket(options)))\n        : new net.Socket()\n    } catch (e) {\n      error(e)\n      return\n    }\n    x.on('error', error)\n    x.on('close', closed)\n    x.on('drain', drain)\n    return x\n  }\n\n  async function cancel({ pid, secret }, resolve, reject) {\n    try {\n      cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)\n      await connect()\n      socket.once('error', reject)\n      socket.once('close', resolve)\n    } catch (error) {\n      reject(error)\n    }\n  }\n\n  function execute(q) {\n    if (terminated)\n      return queryError(q, Errors.connection('CONNECTION_DESTROYED', options))\n\n    if (stream)\n      return queryError(q, Errors.generic('COPY_IN_PROGRESS', 'You cannot execute queries during copy'))\n\n    if (q.cancelled)\n      return\n\n    try {\n      q.state = backend\n      query\n        ? sent.push(q)\n        : (query = q, query.active = true)\n\n      build(q)\n      return write(toBuffer(q))\n        && !q.describeFirst\n        && !q.cursorFn\n        && sent.length < max_pipeline\n        && (!q.options.onexecute || q.options.onexecute(connection))\n    } catch (error) {\n      sent.length === 0 && write(Sync)\n      errored(error)\n      return true\n    }\n  }\n\n  function toBuffer(q) {\n    if (q.parameters.length >= 65534)\n      throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')\n\n    return q.options.simple\n      ? b().Q().str(q.statement.string + b.N).end()\n      : q.describeFirst\n        ? Buffer.concat([describe(q), Flush])\n        : q.prepare\n          ? q.prepared\n            ? prepared(q)\n            : Buffer.concat([describe(q), prepared(q)])\n          : unnamed(q)\n  }\n\n  function describe(q) {\n    return Buffer.concat([\n      Parse(q.statement.string, q.parameters, q.statement.types, q.statement.name),\n      Describe('S', q.statement.name)\n    ])\n  }\n\n  function prepared(q) {\n    return Buffer.concat([\n      Bind(q.parameters, q.statement.types, q.statement.name, q.cursorName),\n      q.cursorFn\n        ? Execute('', q.cursorRows)\n        : ExecuteUnnamed\n    ])\n  }\n\n  function unnamed(q) {\n    return Buffer.concat([\n      Parse(q.statement.string, q.parameters, q.statement.types),\n      DescribeUnnamed,\n      prepared(q)\n    ])\n  }\n\n  function build(q) {\n    const parameters = []\n        , types = []\n\n    const string = stringify(q, q.strings[0], q.args[0], parameters, types, options)\n\n    !q.tagged && q.args.forEach(x => handleValue(x, parameters, types, options))\n\n    q.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)\n    q.string = string\n    q.signature = q.prepare && types + string\n    q.onlyDescribe && (delete statements[q.signature])\n    q.parameters = q.parameters || parameters\n    q.prepared = q.prepare && q.signature in statements\n    q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared)\n    q.statement = q.prepared\n      ? statements[q.signature]\n      : { string, types, name: q.prepare ? statementId + statementCount++ : '' }\n\n    typeof options.debug === 'function' && options.debug(id, string, parameters, types)\n  }\n\n  function write(x, fn) {\n    chunk = chunk ? Buffer.concat([chunk, x]) : Buffer.from(x)\n    if (fn || chunk.length >= 1024)\n      return nextWrite(fn)\n    nextWriteTimer === null && (nextWriteTimer = setImmediate(nextWrite))\n    return true\n  }\n\n  function nextWrite(fn) {\n    const x = socket.write(chunk, fn)\n    nextWriteTimer !== null && clearImmediate(nextWriteTimer)\n    chunk = nextWriteTimer = null\n    return x\n  }\n\n  function connectTimedOut() {\n    errored(Errors.connection('CONNECT_TIMEOUT', options, socket))\n    socket.destroy()\n  }\n\n  async function secure() {\n    if (sslnegotiation !== 'direct') {\n      write(SSLRequest)\n      const canSSL = await new Promise(r => socket.once('data', x => r(x[0] === 83))) // S\n\n      if (!canSSL && ssl === 'prefer')\n        return connected()\n    }\n\n    const options = {\n      socket,\n      servername: net.isIP(socket.host) ? undefined : socket.host\n    }\n\n    if (sslnegotiation === 'direct')\n      options.ALPNProtocols = ['postgresql']\n\n    if (ssl === 'require' || ssl === 'allow' || ssl === 'prefer')\n      options.rejectUnauthorized = false\n    else if (typeof ssl === 'object')\n      Object.assign(options, ssl)\n\n    socket.removeAllListeners()\n    socket = tls.connect(options)\n    socket.on('secureConnect', connected)\n    socket.on('error', error)\n    socket.on('close', closed)\n    socket.on('drain', drain)\n  }\n\n  /* c8 ignore next 3 */\n  function drain() {\n    !query && onopen(connection)\n  }\n\n  function data(x) {\n    if (incomings) {\n      incomings.push(x)\n      remaining -= x.length\n      if (remaining > 0)\n        return\n    }\n\n    incoming = incomings\n      ? Buffer.concat(incomings, length - remaining)\n      : incoming.length === 0\n        ? x\n        : Buffer.concat([incoming, x], incoming.length + x.length)\n\n    while (incoming.length > 4) {\n      length = incoming.readUInt32BE(1)\n      if (length >= incoming.length) {\n        remaining = length - incoming.length\n        incomings = [incoming]\n        break\n      }\n\n      try {\n        handle(incoming.subarray(0, length + 1))\n      } catch (e) {\n        query && (query.cursorFn || query.describeFirst) && write(Sync)\n        errored(e)\n      }\n      incoming = incoming.subarray(length + 1)\n      remaining = 0\n      incomings = null\n    }\n  }\n\n  async function connect() {\n    terminated = false\n    backendParameters = {}\n    socket || (socket = await createSocket())\n\n    if (!socket)\n      return\n\n    connectTimer.start()\n\n    if (options.socket)\n      return ssl ? secure() : connected()\n\n    socket.on('connect', ssl ? secure : connected)\n\n    if (options.path)\n      return socket.connect(options.path)\n\n    socket.ssl = ssl\n    socket.connect(port[hostIndex], host[hostIndex])\n    socket.host = host[hostIndex]\n    socket.port = port[hostIndex]\n\n    hostIndex = (hostIndex + 1) % port.length\n  }\n\n  function reconnect() {\n    setTimeout(connect, closedTime ? Math.max(0, closedTime + delay - performance.now()) : 0)\n  }\n\n  function connected() {\n    try {\n      statements = {}\n      needsTypes = options.fetch_types\n      statementId = Math.random().toString(36).slice(2)\n      statementCount = 1\n      lifeTimer.start()\n      socket.on('data', data)\n      keep_alive && socket.setKeepAlive && socket.setKeepAlive(true)\n      const s = StartupMessage()\n      write(s)\n    } catch (err) {\n      error(err)\n    }\n  }\n\n  function error(err) {\n    if (connection.queue === queues.connecting && options.host[retries + 1])\n      return\n\n    errored(err)\n    while (sent.length)\n      queryError(sent.shift(), err)\n  }\n\n  function errored(err) {\n    stream && (stream.destroy(err), stream = null)\n    query && queryError(query, err)\n    initial && (queryError(initial, err), initial = null)\n  }\n\n  function queryError(query, err) {\n    if (query.reserve)\n      return query.reject(err)\n\n    if (!err || typeof err !== 'object')\n      err = new Error(err)\n\n    'query' in err || 'parameters' in err || Object.defineProperties(err, {\n      stack: { value: err.stack + query.origin.replace(/.*\\n/, '\\n'), enumerable: options.debug },\n      query: { value: query.string, enumerable: options.debug },\n      parameters: { value: query.parameters, enumerable: options.debug },\n      args: { value: query.args, enumerable: options.debug },\n      types: { value: query.statement && query.statement.types, enumerable: options.debug }\n    })\n    query.reject(err)\n  }\n\n  function end() {\n    return ending || (\n      !connection.reserved && onend(connection),\n      !connection.reserved && !initial && !query && sent.length === 0\n        ? (terminate(), new Promise(r => socket && socket.readyState !== 'closed' ? socket.once('close', r) : r()))\n        : ending = new Promise(r => ended = r)\n    )\n  }\n\n  function terminate() {\n    terminated = true\n    if (stream || query || initial || sent.length)\n      error(Errors.connection('CONNECTION_DESTROYED', options))\n\n    clearImmediate(nextWriteTimer)\n    if (socket) {\n      socket.removeListener('data', data)\n      socket.removeListener('connect', connected)\n      socket.readyState === 'open' && socket.end(b().X().end())\n    }\n    ended && (ended(), ending = ended = null)\n  }\n\n  async function closed(hadError) {\n    incoming = Buffer.alloc(0)\n    remaining = 0\n    incomings = null\n    clearImmediate(nextWriteTimer)\n    socket.removeListener('data', data)\n    socket.removeListener('connect', connected)\n    idleTimer.cancel()\n    lifeTimer.cancel()\n    connectTimer.cancel()\n\n    socket.removeAllListeners()\n    socket = null\n\n    if (initial)\n      return reconnect()\n\n    !hadError && (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket))\n    closedTime = performance.now()\n    hadError && options.shared.retries++\n    delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000\n    onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket))\n  }\n\n  /* Handlers */\n  function handle(xs, x = xs[0]) {\n    (\n      x === 68 ? DataRow :                   // D\n      x === 100 ? CopyData :                 // d\n      x === 65 ? NotificationResponse :      // A\n      x === 83 ? ParameterStatus :           // S\n      x === 90 ? ReadyForQuery :             // Z\n      x === 67 ? CommandComplete :           // C\n      x === 50 ? BindComplete :              // 2\n      x === 49 ? ParseComplete :             // 1\n      x === 116 ? ParameterDescription :     // t\n      x === 84 ? RowDescription :            // T\n      x === 82 ? Authentication :            // R\n      x === 110 ? NoData :                   // n\n      x === 75 ? BackendKeyData :            // K\n      x === 69 ? ErrorResponse :             // E\n      x === 115 ? PortalSuspended :          // s\n      x === 51 ? CloseComplete :             // 3\n      x === 71 ? CopyInResponse :            // G\n      x === 78 ? NoticeResponse :            // N\n      x === 72 ? CopyOutResponse :           // H\n      x === 99 ? CopyDone :                  // c\n      x === 73 ? EmptyQueryResponse :        // I\n      x === 86 ? FunctionCallResponse :      // V\n      x === 118 ? NegotiateProtocolVersion : // v\n      x === 87 ? CopyBothResponse :          // W\n      /* c8 ignore next */\n      UnknownMessage\n    )(xs)\n  }\n\n  function DataRow(x) {\n    let index = 7\n    let length\n    let column\n    let value\n\n    const row = query.isRaw ? new Array(query.statement.columns.length) : {}\n    for (let i = 0; i < query.statement.columns.length; i++) {\n      column = query.statement.columns[i]\n      length = x.readInt32BE(index)\n      index += 4\n\n      value = length === -1\n        ? null\n        : query.isRaw === true\n          ? x.subarray(index, index += length)\n          : column.parser === undefined\n            ? x.toString('utf8', index, index += length)\n            : column.parser.array === true\n              ? column.parser(x.toString('utf8', index + 1, index += length))\n              : column.parser(x.toString('utf8', index, index += length))\n\n      query.isRaw\n        ? (row[i] = query.isRaw === true\n          ? value\n          : transform.value.from ? transform.value.from(value, column) : value)\n        : (row[column.name] = transform.value.from ? transform.value.from(value, column) : value)\n    }\n\n    query.forEachFn\n      ? query.forEachFn(transform.row.from ? transform.row.from(row) : row, result)\n      : (result[rows++] = transform.row.from ? transform.row.from(row) : row)\n  }\n\n  function ParameterStatus(x) {\n    const [k, v] = x.toString('utf8', 5, x.length - 1).split(b.N)\n    backendParameters[k] = v\n    if (options.parameters[k] !== v) {\n      options.parameters[k] = v\n      onparameter && onparameter(k, v)\n    }\n  }\n\n  function ReadyForQuery(x) {\n    if (query) {\n      if (errorResponse) {\n        query.retried\n          ? errored(query.retried)\n          : query.prepared && retryRoutines.has(errorResponse.routine)\n            ? retry(query, errorResponse)\n            : errored(errorResponse)\n      } else {\n        query.resolve(results || result)\n      }\n    } else if (errorResponse) {\n      errored(errorResponse)\n    }\n\n    query = results = errorResponse = null\n    result = new Result()\n    connectTimer.cancel()\n\n    if (initial) {\n      if (target_session_attrs) {\n        if (!backendParameters.in_hot_standby || !backendParameters.default_transaction_read_only)\n          return fetchState()\n        else if (tryNext(target_session_attrs, backendParameters))\n          return terminate()\n      }\n\n      if (needsTypes) {\n        initial.reserve && (initial = null)\n        return fetchArrayTypes()\n      }\n\n      initial && !initial.reserve && execute(initial)\n      options.shared.retries = retries = 0\n      initial = null\n      return\n    }\n\n    while (sent.length && (query = sent.shift()) && (query.active = true, query.cancelled))\n      Connection(options).cancel(query.state, query.cancelled.resolve, query.cancelled.reject)\n\n    if (query)\n      return // Consider opening if able and sent.length < 50\n\n    connection.reserved\n      ? !connection.reserved.release && x[5] === 73 // I\n        ? ending\n          ? terminate()\n          : (connection.reserved = null, onopen(connection))\n        : connection.reserved()\n      : ending\n        ? terminate()\n        : onopen(connection)\n  }\n\n  function CommandComplete(x) {\n    rows = 0\n\n    for (let i = x.length - 1; i > 0; i--) {\n      if (x[i] === 32 && x[i + 1] < 58 && result.count === null)\n        result.count = +x.toString('utf8', i + 1, x.length - 1)\n      if (x[i - 1] >= 65) {\n        result.command = x.toString('utf8', 5, i)\n        result.state = backend\n        break\n      }\n    }\n\n    final && (final(), final = null)\n\n    if (result.command === 'BEGIN' && max !== 1 && !connection.reserved)\n      return errored(Errors.generic('UNSAFE_TRANSACTION', 'Only use sql.begin, sql.reserved or max: 1'))\n\n    if (query.options.simple)\n      return BindComplete()\n\n    if (query.cursorFn) {\n      result.count && query.cursorFn(result)\n      write(Sync)\n    }\n  }\n\n  function ParseComplete() {\n    query.parsing = false\n  }\n\n  function BindComplete() {\n    !result.statement && (result.statement = query.statement)\n    result.columns = query.statement.columns\n  }\n\n  function ParameterDescription(x) {\n    const length = x.readUInt16BE(5)\n\n    for (let i = 0; i < length; ++i)\n      !query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))\n\n    query.prepare && (statements[query.signature] = query.statement)\n    query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false)\n  }\n\n  function RowDescription(x) {\n    if (result.command) {\n      results = results || [result]\n      results.push(result = new Result())\n      result.count = null\n      query.statement.columns = null\n    }\n\n    const length = x.readUInt16BE(5)\n    let index = 7\n    let start\n\n    query.statement.columns = Array(length)\n\n    for (let i = 0; i < length; ++i) {\n      start = index\n      while (x[index++] !== 0);\n      const table = x.readUInt32BE(index)\n      const number = x.readUInt16BE(index + 4)\n      const type = x.readUInt32BE(index + 6)\n      query.statement.columns[i] = {\n        name: transform.column.from\n          ? transform.column.from(x.toString('utf8', start, index - 1))\n          : x.toString('utf8', start, index - 1),\n        parser: parsers[type],\n        table,\n        number,\n        type\n      }\n      index += 18\n    }\n\n    result.statement = query.statement\n    if (query.onlyDescribe)\n      return (query.resolve(query.statement), write(Sync))\n  }\n\n  async function Authentication(x, type = x.readUInt32BE(5)) {\n    (\n      type === 3 ? AuthenticationCleartextPassword :\n      type === 5 ? AuthenticationMD5Password :\n      type === 10 ? SASL :\n      type === 11 ? SASLContinue :\n      type === 12 ? SASLFinal :\n      type !== 0 ? UnknownAuth :\n      noop\n    )(x, type)\n  }\n\n  /* c8 ignore next 5 */\n  async function AuthenticationCleartextPassword() {\n    const payload = await Pass()\n    write(\n      b().p().str(payload).z(1).end()\n    )\n  }\n\n  async function AuthenticationMD5Password(x) {\n    const payload = 'md5' + (\n      await md5(\n        Buffer.concat([\n          Buffer.from(await md5((await Pass()) + user)),\n          x.subarray(9)\n        ])\n      )\n    )\n    write(\n      b().p().str(payload).z(1).end()\n    )\n  }\n\n  async function SASL() {\n    nonce = (await crypto.randomBytes(18)).toString('base64')\n    b().p().str('SCRAM-SHA-256' + b.N)\n    const i = b.i\n    write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end())\n  }\n\n  async function SASLContinue(x) {\n    const res = x.toString('utf8', 9).split(',').reduce((acc, x) => (acc[x[0]] = x.slice(2), acc), {})\n\n    const saltedPassword = await crypto.pbkdf2Sync(\n      await Pass(),\n      Buffer.from(res.s, 'base64'),\n      parseInt(res.i), 32,\n      'sha256'\n    )\n\n    const clientKey = await hmac(saltedPassword, 'Client Key')\n\n    const auth = 'n=*,r=' + nonce + ','\n               + 'r=' + res.r + ',s=' + res.s + ',i=' + res.i\n               + ',c=biws,r=' + res.r\n\n    serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64')\n\n    const payload = 'c=biws,r=' + res.r + ',p=' + xor(\n      clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))\n    ).toString('base64')\n\n    write(\n      b().p().str(payload).end()\n    )\n  }\n\n  function SASLFinal(x) {\n    if (x.toString('utf8', 9).split(b.N, 1)[0].slice(2) === serverSignature)\n      return\n    /* c8 ignore next 5 */\n    errored(Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server did not return the correct signature'))\n    socket.destroy()\n  }\n\n  function Pass() {\n    return Promise.resolve(typeof options.pass === 'function'\n      ? options.pass()\n      : options.pass\n    )\n  }\n\n  function NoData() {\n    result.statement = query.statement\n    result.statement.columns = []\n    if (query.onlyDescribe)\n      return (query.resolve(query.statement), write(Sync))\n  }\n\n  function BackendKeyData(x) {\n    backend.pid = x.readUInt32BE(5)\n    backend.secret = x.readUInt32BE(9)\n  }\n\n  async function fetchArrayTypes() {\n    needsTypes = false\n    const types = await new Query([`\n      select b.oid, b.typarray\n      from pg_catalog.pg_type a\n      left join pg_catalog.pg_type b on b.oid = a.typelem\n      where a.typcategory = 'A'\n      group by b.oid, b.typarray\n      order by b.oid\n    `], [], execute)\n    types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))\n  }\n\n  function addArrayType(oid, typarray) {\n    if (!!options.parsers[typarray] && !!options.serializers[typarray]) return\n    const parser = options.parsers[oid]\n    options.shared.typeArrayMap[oid] = typarray\n    options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)\n    options.parsers[typarray].array = true\n    options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)\n  }\n\n  function tryNext(x, xs) {\n    return (\n      (x === 'read-write' && xs.default_transaction_read_only === 'on') ||\n      (x === 'read-only' && xs.default_transaction_read_only === 'off') ||\n      (x === 'primary' && xs.in_hot_standby === 'on') ||\n      (x === 'standby' && xs.in_hot_standby === 'off') ||\n      (x === 'prefer-standby' && xs.in_hot_standby === 'off' && options.host[retries])\n    )\n  }\n\n  function fetchState() {\n    const query = new Query([`\n      show transaction_read_only;\n      select pg_catalog.pg_is_in_recovery()\n    `], [], execute, null, { simple: true })\n    query.resolve = ([[a], [b]]) => {\n      backendParameters.default_transaction_read_only = a.transaction_read_only\n      backendParameters.in_hot_standby = b.pg_is_in_recovery ? 'on' : 'off'\n    }\n    query.execute()\n  }\n\n  function ErrorResponse(x) {\n    if (query) {\n      (query.cursorFn || query.describeFirst) && write(Sync)\n      errorResponse = Errors.postgres(parseError(x))\n    } else {\n      errored(Errors.postgres(parseError(x)))\n    }\n  }\n\n  function retry(q, error) {\n    delete statements[q.signature]\n    q.retried = error\n    execute(q)\n  }\n\n  function NotificationResponse(x) {\n    if (!onnotify)\n      return\n\n    let index = 9\n    while (x[index++] !== 0);\n    onnotify(\n      x.toString('utf8', 9, index - 1),\n      x.toString('utf8', index, x.length - 1)\n    )\n  }\n\n  async function PortalSuspended() {\n    try {\n      const x = await Promise.resolve(query.cursorFn(result))\n      rows = 0\n      x === CLOSE\n        ? write(Close(query.portal))\n        : (result = new Result(), write(Execute('', query.cursorRows)))\n    } catch (err) {\n      write(Sync)\n      query.reject(err)\n    }\n  }\n\n  function CloseComplete() {\n    result.count && query.cursorFn(result)\n    query.resolve(result)\n  }\n\n  function CopyInResponse() {\n    stream = new Stream.Writable({\n      autoDestroy: true,\n      write(chunk, encoding, callback) {\n        socket.write(b().d().raw(chunk).end(), callback)\n      },\n      destroy(error, callback) {\n        callback(error)\n        socket.write(b().f().str(error + b.N).end())\n        stream = null\n      },\n      final(callback) {\n        socket.write(b().c().end())\n        final = callback\n        stream = null\n      }\n    })\n    query.resolve(stream)\n  }\n\n  function CopyOutResponse() {\n    stream = new Stream.Readable({\n      read() { socket.resume() }\n    })\n    query.resolve(stream)\n  }\n\n  /* c8 ignore next 3 */\n  function CopyBothResponse() {\n    stream = new Stream.Duplex({\n      autoDestroy: true,\n      read() { socket.resume() },\n      /* c8 ignore next 11 */\n      write(chunk, encoding, callback) {\n        socket.write(b().d().raw(chunk).end(), callback)\n      },\n      destroy(error, callback) {\n        callback(error)\n        socket.write(b().f().str(error + b.N).end())\n        stream = null\n      },\n      final(callback) {\n        socket.write(b().c().end())\n        final = callback\n      }\n    })\n    query.resolve(stream)\n  }\n\n  function CopyData(x) {\n    stream && (stream.push(x.subarray(5)) || socket.pause())\n  }\n\n  function CopyDone() {\n    stream && stream.push(null)\n    stream = null\n  }\n\n  function NoticeResponse(x) {\n    onnotice\n      ? onnotice(parseError(x))\n      : console.log(parseError(x)) // eslint-disable-line\n\n  }\n\n  /* c8 ignore next 3 */\n  function EmptyQueryResponse() {\n    /* noop */\n  }\n\n  /* c8 ignore next 3 */\n  function FunctionCallResponse() {\n    errored(Errors.notSupported('FunctionCallResponse'))\n  }\n\n  /* c8 ignore next 3 */\n  function NegotiateProtocolVersion() {\n    errored(Errors.notSupported('NegotiateProtocolVersion'))\n  }\n\n  /* c8 ignore next 3 */\n  function UnknownMessage(x) {\n    console.error('Postgres.js : Unknown Message:', x[0]) // eslint-disable-line\n  }\n\n  /* c8 ignore next 3 */\n  function UnknownAuth(x, type) {\n    console.error('Postgres.js : Unknown Auth:', type) // eslint-disable-line\n  }\n\n  /* Messages */\n  function Bind(parameters, types, statement = '', portal = '') {\n    let prev\n      , type\n\n    b().B().str(portal + b.N).str(statement + b.N).i16(0).i16(parameters.length)\n\n    parameters.forEach((x, i) => {\n      if (x === null)\n        return b.i32(0xFFFFFFFF)\n\n      type = types[i]\n      parameters[i] = x = type in options.serializers\n        ? options.serializers[type](x)\n        : '' + x\n\n      prev = b.i\n      b.inc(4).str(x).i32(b.i - prev - 4, prev)\n    })\n\n    b.i16(0)\n\n    return b.end()\n  }\n\n  function Parse(str, parameters, types, name = '') {\n    b().P().str(name + b.N).str(str + b.N).i16(parameters.length)\n    parameters.forEach((x, i) => b.i32(types[i] || 0))\n    return b.end()\n  }\n\n  function Describe(x, name = '') {\n    return b().D().str(x).str(name + b.N).end()\n  }\n\n  function Execute(portal = '', rows = 0) {\n    return Buffer.concat([\n      b().E().str(portal + b.N).i32(rows).end(),\n      Flush\n    ])\n  }\n\n  function Close(portal = '') {\n    return Buffer.concat([\n      b().C().str('P').str(portal + b.N).end(),\n      b().S().end()\n    ])\n  }\n\n  function StartupMessage() {\n    return cancelMessage || b().inc(4).i16(3).z(2).str(\n      Object.entries(Object.assign({\n        user,\n        database,\n        client_encoding: 'UTF8'\n      },\n        options.connection\n      )).filter(([, v]) => v).map(([k, v]) => k + b.N + v).join(b.N)\n    ).z(2).end(0)\n  }\n\n}\n\nfunction parseError(x) {\n  const error = {}\n  let start = 5\n  for (let i = 5; i < x.length - 1; i++) {\n    if (x[i] === 0) {\n      error[errorFields[x[start]]] = x.toString('utf8', start + 1, i)\n      start = i + 1\n    }\n  }\n  return error\n}\n\nfunction md5(x) {\n  return crypto.createHash('md5').update(x).digest('hex')\n}\n\nfunction hmac(key, x) {\n  return Buffer.from(new HmacSha256(key).update(x).digest())\n}\n\nfunction sha256(x) {\n  return crypto.createHash('sha256').update(x).digest()\n}\n\nfunction xor(a, b) {\n  const length = Math.max(a.length, b.length)\n  const buffer = Buffer.allocUnsafe(length)\n  for (let i = 0; i < length; i++)\n    buffer[i] = a[i] ^ b[i]\n  return buffer\n}\n\nfunction timer(fn, seconds) {\n  seconds = typeof seconds === 'function' ? seconds() : seconds\n  if (!seconds)\n    return { cancel: noop, start: noop }\n\n  let timer\n  return {\n    cancel() {\n      timer && (clearTimeout(timer), timer = null)\n    },\n    start() {\n      timer && clearTimeout(timer)\n      timer = setTimeout(done, seconds * 1000, arguments)\n    }\n  }\n\n  function done(args) {\n    fn.apply(null, args)\n    timer = null\n  }\n}\n"
  },
  {
    "path": "deno/src/errors.js",
    "content": "export class PostgresError extends Error {\n  constructor(x) {\n    super(x.message)\n    this.name = this.constructor.name\n    Object.assign(this, x)\n  }\n}\n\nexport const Errors = {\n  connection,\n  postgres,\n  generic,\n  notSupported\n}\n\nfunction connection(x, options, socket) {\n  const { host, port } = socket || options\n  const error = Object.assign(\n    new Error(('write ' + x + ' ' + (options.path || (host + ':' + port)))),\n    {\n      code: x,\n      errno: x,\n      address: options.path || host\n    }, options.path ? {} : { port: port }\n  )\n  Error.captureStackTrace(error, connection)\n  return error\n}\n\nfunction postgres(x) {\n  const error = new PostgresError(x)\n  Error.captureStackTrace(error, postgres)\n  return error\n}\n\nfunction generic(code, message) {\n  const error = Object.assign(new Error(code + ': ' + message), { code })\n  Error.captureStackTrace(error, generic)\n  return error\n}\n\n/* c8 ignore next 10 */\nfunction notSupported(x) {\n  const error = Object.assign(\n    new Error(x + ' (B) is not supported'),\n    {\n      code: 'MESSAGE_NOT_SUPPORTED',\n      name: x\n    }\n  )\n  Error.captureStackTrace(error, notSupported)\n  return error\n}\n"
  },
  {
    "path": "deno/src/index.js",
    "content": "import process from 'https://deno.land/std@0.132.0/node/process.ts'\nimport os from 'https://deno.land/std@0.132.0/node/os.ts'\nimport fs from 'https://deno.land/std@0.132.0/node/fs.ts'\n\nimport {\n  mergeUserTypes,\n  inferType,\n  Parameter,\n  Identifier,\n  Builder,\n  toPascal,\n  pascal,\n  toCamel,\n  camel,\n  toKebab,\n  kebab,\n  fromPascal,\n  fromCamel,\n  fromKebab\n} from './types.js'\n\nimport Connection from './connection.js'\nimport { Query, CLOSE } from './query.js'\nimport Queue from './queue.js'\nimport { Errors, PostgresError } from './errors.js'\nimport Subscribe from './subscribe.js'\nimport largeObject from './large.js'\n\nObject.assign(Postgres, {\n  PostgresError,\n  toPascal,\n  pascal,\n  toCamel,\n  camel,\n  toKebab,\n  kebab,\n  fromPascal,\n  fromCamel,\n  fromKebab,\n  BigInt: {\n    to: 20,\n    from: [20],\n    parse: x => BigInt(x), // eslint-disable-line\n    serialize: x => x.toString()\n  }\n})\n\nexport default Postgres\n\nfunction Postgres(a, b) {\n  const options = parseOptions(a, b)\n      , subscribe = options.no_subscribe || Subscribe(Postgres, { ...options })\n\n  let ending = false\n\n  const queries = Queue()\n      , connecting = Queue()\n      , reserved = Queue()\n      , closed = Queue()\n      , ended = Queue()\n      , open = Queue()\n      , busy = Queue()\n      , full = Queue()\n      , queues = { connecting, reserved, closed, ended, open, busy, full }\n\n  const connections = [...Array(options.max)].map(() => Connection(options, queues, { onopen, onend, onclose }))\n\n  const sql = Sql(handler)\n\n  Object.assign(sql, {\n    get parameters() { return options.parameters },\n    largeObject: largeObject.bind(null, sql),\n    subscribe,\n    CLOSE,\n    END: CLOSE,\n    PostgresError,\n    options,\n    reserve,\n    listen,\n    begin,\n    close,\n    end\n  })\n\n  return sql\n\n  function Sql(handler) {\n    handler.debug = options.debug\n\n    Object.entries(options.types).reduce((acc, [name, type]) => {\n      acc[name] = (x) => new Parameter(x, type.to)\n      return acc\n    }, typed)\n\n    Object.assign(sql, {\n      types: typed,\n      typed,\n      unsafe,\n      notify,\n      array,\n      json,\n      file\n    })\n\n    return sql\n\n    function typed(value, type) {\n      return new Parameter(value, type)\n    }\n\n    function sql(strings, ...args) {\n      const query = strings && Array.isArray(strings.raw)\n        ? new Query(strings, args, handler, cancel)\n        : typeof strings === 'string' && !args.length\n          ? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings)\n          : new Builder(strings, args)\n      return query\n    }\n\n    function unsafe(string, args = [], options = {}) {\n      arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])\n      const query = new Query([string], args, handler, cancel, {\n        prepare: false,\n        ...options,\n        simple: 'simple' in options ? options.simple : args.length === 0\n      })\n      return query\n    }\n\n    function file(path, args = [], options = {}) {\n      arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])\n      const query = new Query([], args, (query) => {\n        fs.readFile(path, 'utf8', (err, string) => {\n          if (err)\n            return query.reject(err)\n\n          query.strings = [string]\n          handler(query)\n        })\n      }, cancel, {\n        ...options,\n        simple: 'simple' in options ? options.simple : args.length === 0\n      })\n      return query\n    }\n  }\n\n  async function listen(name, fn, onlisten) {\n    const listener = { fn, onlisten }\n\n    const sql = listen.sql || (listen.sql = Postgres({\n      ...options,\n      max: 1,\n      idle_timeout: null,\n      max_lifetime: null,\n      fetch_types: false,\n      onclose() {\n        Object.entries(listen.channels).forEach(([name, { listeners }]) => {\n          delete listen.channels[name]\n          Promise.all(listeners.map(l => listen(name, l.fn, l.onlisten).catch(() => { /* noop */ })))\n        })\n      },\n      onnotify(c, x) {\n        c in listen.channels && listen.channels[c].listeners.forEach(l => l.fn(x))\n      }\n    }))\n\n    const channels = listen.channels || (listen.channels = {})\n        , exists = name in channels\n\n    if (exists) {\n      channels[name].listeners.push(listener)\n      const result = await channels[name].result\n      listener.onlisten && listener.onlisten()\n      return { state: result.state, unlisten }\n    }\n\n    channels[name] = { result: sql`listen ${\n      sql.unsafe('\"' + name.replace(/\"/g, '\"\"') + '\"')\n    }`, listeners: [listener] }\n    const result = await channels[name].result\n    listener.onlisten && listener.onlisten()\n    return { state: result.state, unlisten }\n\n    async function unlisten() {\n      if (name in channels === false)\n        return\n\n      channels[name].listeners = channels[name].listeners.filter(x => x !== listener)\n      if (channels[name].listeners.length)\n        return\n\n      delete channels[name]\n      return sql`unlisten ${\n        sql.unsafe('\"' + name.replace(/\"/g, '\"\"') + '\"')\n      }`\n    }\n  }\n\n  async function notify(channel, payload) {\n    return await sql`select pg_notify(${ channel }, ${ '' + payload })`\n  }\n\n  async function reserve() {\n    const queue = Queue()\n    const c = open.length\n      ? open.shift()\n      : await new Promise((resolve, reject) => {\n        const query = { reserve: resolve, reject }\n        queries.push(query)\n        closed.length && connect(closed.shift(), query)\n      })\n\n    move(c, reserved)\n    c.reserved = () => queue.length\n      ? c.execute(queue.shift())\n      : move(c, reserved)\n    c.reserved.release = true\n\n    const sql = Sql(handler)\n    sql.release = () => {\n      c.reserved = null\n      onopen(c)\n    }\n\n    return sql\n\n    function handler(q) {\n      c.queue === full\n        ? queue.push(q)\n        : c.execute(q) || move(c, full)\n    }\n  }\n\n  async function begin(options, fn) {\n    !fn && (fn = options, options = '')\n    const queries = Queue()\n    let savepoints = 0\n      , connection\n      , prepare = null\n\n    try {\n      await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()\n      return await Promise.race([\n        scope(connection, fn),\n        new Promise((_, reject) => connection.onclose = reject)\n      ])\n    } catch (error) {\n      throw error\n    }\n\n    async function scope(c, fn, name) {\n      const sql = Sql(handler)\n      sql.savepoint = savepoint\n      sql.prepare = x => prepare = x.replace(/[^a-z0-9$-_. ]/gi)\n      let uncaughtError\n        , result\n\n      name && await sql`savepoint ${ sql(name) }`\n      try {\n        result = await new Promise((resolve, reject) => {\n          const x = fn(sql)\n          Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)\n        })\n\n        if (uncaughtError)\n          throw uncaughtError\n      } catch (e) {\n        await (name\n          ? sql`rollback to ${ sql(name) }`\n          : sql`rollback`\n        )\n        throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e\n      }\n\n      if (!name) {\n        prepare\n          ? await sql`prepare transaction '${ sql.unsafe(prepare) }'`\n          : await sql`commit`\n      }\n\n      return result\n\n      function savepoint(name, fn) {\n        if (name && Array.isArray(name.raw))\n          return savepoint(sql => sql.apply(sql, arguments))\n\n        arguments.length === 1 && (fn = name, name = null)\n        return scope(c, fn, 's' + savepoints++ + (name ? '_' + name : ''))\n      }\n\n      function handler(q) {\n        q.catch(e => uncaughtError || (uncaughtError = e))\n        c.queue === full\n          ? queries.push(q)\n          : c.execute(q) || move(c, full)\n      }\n    }\n\n    function onexecute(c) {\n      connection = c\n      move(c, reserved)\n      c.reserved = () => queries.length\n        ? c.execute(queries.shift())\n        : move(c, reserved)\n    }\n  }\n\n  function move(c, queue) {\n    c.queue.remove(c)\n    queue.push(c)\n    c.queue = queue\n    queue === open\n      ? c.idleTimer.start()\n      : c.idleTimer.cancel()\n    return c\n  }\n\n  function json(x) {\n    return new Parameter(x, 3802)\n  }\n\n  function array(x, type) {\n    if (!Array.isArray(x))\n      return array(Array.from(arguments))\n\n    return new Parameter(x, type || (x.length ? inferType(x) || 25 : 0), options.shared.typeArrayMap)\n  }\n\n  function handler(query) {\n    if (ending)\n      return query.reject(Errors.connection('CONNECTION_ENDED', options, options))\n\n    if (open.length)\n      return go(open.shift(), query)\n\n    if (closed.length)\n      return connect(closed.shift(), query)\n\n    busy.length\n      ? go(busy.shift(), query)\n      : queries.push(query)\n  }\n\n  function go(c, query) {\n    return c.execute(query)\n      ? move(c, busy)\n      : move(c, full)\n  }\n\n  function cancel(query) {\n    return new Promise((resolve, reject) => {\n      query.state\n        ? query.active\n          ? Connection(options).cancel(query.state, resolve, reject)\n          : query.cancelled = { resolve, reject }\n        : (\n          queries.remove(query),\n          query.cancelled = true,\n          query.reject(Errors.generic('57014', 'canceling statement due to user request')),\n          resolve()\n        )\n    })\n  }\n\n  async function end({ timeout = null } = {}) {\n    if (ending)\n      return ending\n\n    await 1\n    let timer\n    return ending = Promise.race([\n      new Promise(r => timeout !== null && (timer = setTimeout(destroy, timeout * 1000, r))),\n      Promise.all(connections.map(c => c.end()).concat(\n        listen.sql ? listen.sql.end({ timeout: 0 }) : [],\n        subscribe.sql ? subscribe.sql.end({ timeout: 0 }) : []\n      ))\n    ]).then(() => clearTimeout(timer))\n  }\n\n  async function close() {\n    await Promise.all(connections.map(c => c.end()))\n  }\n\n  async function destroy(resolve) {\n    await Promise.all(connections.map(c => c.terminate()))\n    while (queries.length)\n      queries.shift().reject(Errors.connection('CONNECTION_DESTROYED', options))\n    resolve()\n  }\n\n  function connect(c, query) {\n    move(c, connecting)\n    c.connect(query)\n    return c\n  }\n\n  function onend(c) {\n    move(c, ended)\n  }\n\n  function onopen(c) {\n    if (queries.length === 0)\n      return move(c, open)\n\n    let max = Math.ceil(queries.length / (connecting.length + 1))\n      , ready = true\n\n    while (ready && queries.length && max-- > 0) {\n      const query = queries.shift()\n      if (query.reserve)\n        return query.reserve(c)\n\n      ready = c.execute(query)\n    }\n\n    ready\n      ? move(c, busy)\n      : move(c, full)\n  }\n\n  function onclose(c, e) {\n    move(c, closed)\n    c.reserved = null\n    c.onclose && (c.onclose(e), c.onclose = null)\n    options.onclose && options.onclose(c.id)\n    queries.length && connect(c, queries.shift())\n  }\n}\n\nfunction parseOptions(a, b) {\n  if (a && a.shared)\n    return a\n\n  const env = process.env // eslint-disable-line\n      , o = (!a || typeof a === 'string' ? b : a) || {}\n      , { url, multihost } = parseUrl(a)\n      , query = [...url.searchParams].reduce((a, [b, c]) => (a[b] = c, a), {})\n      , host = o.hostname || o.host || multihost || url.hostname || env.PGHOST || 'localhost'\n      , port = o.port || url.port || env.PGPORT || 5432\n      , user = o.user || o.username || url.username || env.PGUSERNAME || env.PGUSER || osUsername()\n\n  o.no_prepare && (o.prepare = false)\n  query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)\n  'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line\n  query.sslrootcert === 'system' && (query.ssl = 'verify-full')\n\n  const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive']\n  const defaults = {\n    max             : globalThis.Cloudflare ? 3 : 10,\n    ssl             : false,\n    sslnegotiation  : null,\n    idle_timeout    : null,\n    connect_timeout : 30,\n    max_lifetime    : max_lifetime,\n    max_pipeline    : 100,\n    backoff         : backoff,\n    keep_alive      : 60,\n    prepare         : true,\n    debug           : false,\n    fetch_types     : true,\n    publications    : 'alltables',\n    target_session_attrs: null\n  }\n\n  return {\n    host            : Array.isArray(host) ? host : host.split(',').map(x => x.split(':')[0]),\n    port            : Array.isArray(port) ? port : host.split(',').map(x => parseInt(x.split(':')[1] || port)),\n    path            : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,\n    database        : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || user,\n    user            : user,\n    pass            : o.pass || o.password || url.password || env.PGPASSWORD || '',\n    ...Object.entries(defaults).reduce(\n      (acc, [k, d]) => {\n        const value = k in o ? o[k] : k in query\n          ? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])\n          : env['PG' + k.toUpperCase()] || d\n        acc[k] = typeof value === 'string' && ints.includes(k)\n          ? +value\n          : value\n        return acc\n      },\n      {}\n    ),\n    connection      : {\n      application_name: env.PGAPPNAME || 'postgres.js',\n      ...o.connection,\n      ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})\n    },\n    types           : o.types || {},\n    target_session_attrs: tsa(o, url, env),\n    onnotice        : o.onnotice,\n    onnotify        : o.onnotify,\n    onclose         : o.onclose,\n    onparameter     : o.onparameter,\n    socket          : o.socket,\n    transform       : parseTransform(o.transform || { undefined: undefined }),\n    parameters      : {},\n    shared          : { retries: 0, typeArrayMap: {} },\n    ...mergeUserTypes(o.types)\n  }\n}\n\nfunction tsa(o, url, env) {\n  const x = o.target_session_attrs || url.searchParams.get('target_session_attrs') || env.PGTARGETSESSIONATTRS\n  if (!x || ['read-write', 'read-only', 'primary', 'standby', 'prefer-standby'].includes(x))\n    return x\n\n  throw new Error('target_session_attrs ' + x + ' is not supported')\n}\n\nfunction backoff(retries) {\n  return (0.5 + Math.random() / 2) * Math.min(3 ** retries / 100, 20)\n}\n\nfunction max_lifetime() {\n  return 60 * (30 + Math.random() * 30)\n}\n\nfunction parseTransform(x) {\n  return {\n    undefined: x.undefined,\n    column: {\n      from: typeof x.column === 'function' ? x.column : x.column && x.column.from,\n      to: x.column && x.column.to\n    },\n    value: {\n      from: typeof x.value === 'function' ? x.value : x.value && x.value.from,\n      to: x.value && x.value.to\n    },\n    row: {\n      from: typeof x.row === 'function' ? x.row : x.row && x.row.from,\n      to: x.row && x.row.to\n    }\n  }\n}\n\nfunction parseUrl(url) {\n  if (!url || typeof url !== 'string')\n    return { url: { searchParams: new Map() } }\n\n  let host = url\n  host = host.slice(host.indexOf('://') + 3).split(/[?/]/)[0]\n  host = decodeURIComponent(host.slice(host.indexOf('@') + 1))\n\n  const urlObj = new URL(url.replace(host, host.split(',')[0]))\n\n  return {\n    url: {\n      username: decodeURIComponent(urlObj.username),\n      password: decodeURIComponent(urlObj.password),\n      host: urlObj.host,\n      hostname: urlObj.hostname,\n      port: urlObj.port,\n      pathname: urlObj.pathname,\n      searchParams: urlObj.searchParams\n    },\n    multihost: host.indexOf(',') > -1 && host\n  }\n}\n\nfunction osUsername() {\n  try {\n    return os.userInfo().username // eslint-disable-line\n  } catch (_) {\n    return process.env.USERNAME || process.env.USER || process.env.LOGNAME  // eslint-disable-line\n  }\n}\n"
  },
  {
    "path": "deno/src/large.js",
    "content": "import Stream from 'https://deno.land/std@0.132.0/node/stream.ts'\n\nexport default function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {\n  return new Promise(async(resolve, reject) => {\n    await sql.begin(async sql => {\n      let finish\n      !oid && ([{ oid }] = await sql`select lo_creat(-1) as oid`)\n      const [{ fd }] = await sql`select lo_open(${ oid }, ${ mode }) as fd`\n\n      const lo = {\n        writable,\n        readable,\n        close     : () => sql`select lo_close(${ fd })`.then(finish),\n        tell      : () => sql`select lo_tell64(${ fd })`,\n        read      : (x) => sql`select loread(${ fd }, ${ x }) as data`,\n        write     : (x) => sql`select lowrite(${ fd }, ${ x })`,\n        truncate  : (x) => sql`select lo_truncate64(${ fd }, ${ x })`,\n        seek      : (x, whence = 0) => sql`select lo_lseek64(${ fd }, ${ x }, ${ whence })`,\n        size      : () => sql`\n          select\n            lo_lseek64(${ fd }, location, 0) as position,\n            seek.size\n          from (\n            select\n              lo_lseek64($1, 0, 2) as size,\n              tell.location\n            from (select lo_tell64($1) as location) tell\n          ) seek\n        `\n      }\n\n      resolve(lo)\n\n      return new Promise(async r => finish = r)\n\n      async function readable({\n        highWaterMark = 2048 * 8,\n        start = 0,\n        end = Infinity\n      } = {}) {\n        let max = end - start\n        start && await lo.seek(start)\n        return new Stream.Readable({\n          highWaterMark,\n          async read(size) {\n            const l = size > max ? size - max : size\n            max -= size\n            const [{ data }] = await lo.read(l)\n            this.push(data)\n            if (data.length < size)\n              this.push(null)\n          }\n        })\n      }\n\n      async function writable({\n        highWaterMark = 2048 * 8,\n        start = 0\n      } = {}) {\n        start && await lo.seek(start)\n        return new Stream.Writable({\n          highWaterMark,\n          write(chunk, encoding, callback) {\n            lo.write(chunk).then(() => callback(), callback)\n          }\n        })\n      }\n    }).catch(reject)\n  })\n}\n"
  },
  {
    "path": "deno/src/query.js",
    "content": "const originCache = new Map()\n    , originStackCache = new Map()\n    , originError = Symbol('OriginError')\n\nexport const CLOSE = {}\nexport class Query extends Promise {\n  constructor(strings, args, handler, canceller, options = {}) {\n    let resolve\n      , reject\n\n    super((a, b) => {\n      resolve = a\n      reject = b\n    })\n\n    this.tagged = Array.isArray(strings.raw)\n    this.strings = strings\n    this.args = args\n    this.handler = handler\n    this.canceller = canceller\n    this.options = options\n\n    this.state = null\n    this.statement = null\n\n    this.resolve = x => (this.active = false, resolve(x))\n    this.reject = x => (this.active = false, reject(x))\n\n    this.active = false\n    this.cancelled = null\n    this.executed = false\n    this.signature = ''\n\n    this[originError] = this.handler.debug\n      ? new Error()\n      : this.tagged && cachedError(this.strings)\n  }\n\n  get origin() {\n    return (this.handler.debug\n      ? this[originError].stack\n      : this.tagged && originStackCache.has(this.strings)\n        ? originStackCache.get(this.strings)\n        : originStackCache.set(this.strings, this[originError].stack).get(this.strings)\n    ) || ''\n  }\n\n  static get [Symbol.species]() {\n    return Promise\n  }\n\n  cancel() {\n    return this.canceller && (this.canceller(this), this.canceller = null)\n  }\n\n  simple() {\n    this.options.simple = true\n    this.options.prepare = false\n    return this\n  }\n\n  async readable() {\n    this.simple()\n    this.streaming = true\n    return this\n  }\n\n  async writable() {\n    this.simple()\n    this.streaming = true\n    return this\n  }\n\n  cursor(rows = 1, fn) {\n    this.options.simple = false\n    if (typeof rows === 'function') {\n      fn = rows\n      rows = 1\n    }\n\n    this.cursorRows = rows\n\n    if (typeof fn === 'function')\n      return (this.cursorFn = fn, this)\n\n    let prev\n    return {\n      [Symbol.asyncIterator]: () => ({\n        next: () => {\n          if (this.executed && !this.active)\n            return { done: true }\n\n          prev && prev()\n          const promise = new Promise((resolve, reject) => {\n            this.cursorFn = value => {\n              resolve({ value, done: false })\n              return new Promise(r => prev = r)\n            }\n            this.resolve = () => (this.active = false, resolve({ done: true }))\n            this.reject = x => (this.active = false, reject(x))\n          })\n          this.execute()\n          return promise\n        },\n        return() {\n          prev && prev(CLOSE)\n          return { done: true }\n        }\n      })\n    }\n  }\n\n  describe() {\n    this.options.simple = false\n    this.onlyDescribe = this.options.prepare = true\n    return this\n  }\n\n  stream() {\n    throw new Error('.stream has been renamed to .forEach')\n  }\n\n  forEach(fn) {\n    this.forEachFn = fn\n    this.handle()\n    return this\n  }\n\n  raw() {\n    this.isRaw = true\n    return this\n  }\n\n  values() {\n    this.isRaw = 'values'\n    return this\n  }\n\n  async handle() {\n    !this.executed && (this.executed = true) && await 1 && this.handler(this)\n  }\n\n  execute() {\n    this.handle()\n    return this\n  }\n\n  then() {\n    this.handle()\n    return super.then.apply(this, arguments)\n  }\n\n  catch() {\n    this.handle()\n    return super.catch.apply(this, arguments)\n  }\n\n  finally() {\n    this.handle()\n    return super.finally.apply(this, arguments)\n  }\n}\n\nfunction cachedError(xs) {\n  if (originCache.has(xs))\n    return originCache.get(xs)\n\n  const x = Error.stackTraceLimit\n  Error.stackTraceLimit = 4\n  originCache.set(xs, new Error())\n  Error.stackTraceLimit = x\n  return originCache.get(xs)\n}\n"
  },
  {
    "path": "deno/src/queue.js",
    "content": "export default Queue\n\nfunction Queue(initial = []) {\n  let xs = initial.slice()\n  let index = 0\n\n  return {\n    get length() {\n      return xs.length - index\n    },\n    remove: (x) => {\n      const index = xs.indexOf(x)\n      return index === -1\n        ? null\n        : (xs.splice(index, 1), x)\n    },\n    push: (x) => (xs.push(x), x),\n    shift: () => {\n      const out = xs[index++]\n\n      if (index === xs.length) {\n        index = 0\n        xs = []\n      } else {\n        xs[index - 1] = undefined\n      }\n\n      return out\n    }\n  }\n}\n"
  },
  {
    "path": "deno/src/result.js",
    "content": "export default class Result extends Array {\n  constructor() {\n    super()\n    Object.defineProperties(this, {\n      count: { value: null, writable: true },\n      state: { value: null, writable: true },\n      command: { value: null, writable: true },\n      columns: { value: null, writable: true },\n      statement: { value: null, writable: true }\n    })\n  }\n\n  static get [Symbol.species]() {\n    return Array\n  }\n}\n"
  },
  {
    "path": "deno/src/subscribe.js",
    "content": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nconst noop = () => { /* noop */ }\n\nexport default function Subscribe(postgres, options) {\n  const subscribers = new Map()\n      , slot = 'postgresjs_' + Math.random().toString(36).slice(2)\n      , state = {}\n\n  let connection\n    , stream\n    , ended = false\n\n  const sql = subscribe.sql = postgres({\n    ...options,\n    transform: { column: {}, value: {}, row: {} },\n    max: 1,\n    fetch_types: false,\n    idle_timeout: null,\n    max_lifetime: null,\n    connection: {\n      ...options.connection,\n      replication: 'database'\n    },\n    onclose: async function() {\n      if (ended)\n        return\n      stream = null\n      state.pid = state.secret = undefined\n      connected(await init(sql, slot, options.publications))\n      subscribers.forEach(event => event.forEach(({ onsubscribe }) => onsubscribe()))\n    },\n    no_subscribe: true\n  })\n\n  const end = sql.end\n      , close = sql.close\n\n  sql.end = async() => {\n    ended = true\n    stream && (await new Promise(r => (stream.once('close', r), stream.end())))\n    return end()\n  }\n\n  sql.close = async() => {\n    stream && (await new Promise(r => (stream.once('close', r), stream.end())))\n    return close()\n  }\n\n  return subscribe\n\n  async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {\n    event = parseEvent(event)\n\n    if (!connection)\n      connection = init(sql, slot, options.publications)\n\n    const subscriber = { fn, onsubscribe }\n    const fns = subscribers.has(event)\n      ? subscribers.get(event).add(subscriber)\n      : subscribers.set(event, new Set([subscriber])).get(event)\n\n    const unsubscribe = () => {\n      fns.delete(subscriber)\n      fns.size === 0 && subscribers.delete(event)\n    }\n\n    return connection.then(x => {\n      connected(x)\n      onsubscribe()\n      stream && stream.on('error', onerror)\n      return { unsubscribe, state, sql }\n    })\n  }\n\n  function connected(x) {\n    stream = x.stream\n    state.pid = x.state.pid\n    state.secret = x.state.secret\n  }\n\n  async function init(sql, slot, publications) {\n    if (!publications)\n      throw new Error('Missing publication names')\n\n    const xs = await sql.unsafe(\n      `CREATE_REPLICATION_SLOT ${ slot } TEMPORARY LOGICAL pgoutput NOEXPORT_SNAPSHOT`\n    )\n\n    const [x] = xs\n\n    const stream = await sql.unsafe(\n      `START_REPLICATION SLOT ${ slot } LOGICAL ${\n        x.consistent_point\n      } (proto_version '1', publication_names '${ publications }')`\n    ).writable()\n\n    const state = {\n      lsn: Buffer.concat(x.consistent_point.split('/').map(x => Buffer.from(('00000000' + x).slice(-8), 'hex')))\n    }\n\n    stream.on('data', data)\n    stream.on('error', error)\n    stream.on('close', sql.close)\n\n    return { stream, state: xs.state }\n\n    function error(e) {\n      console.error('Unexpected error during logical streaming - reconnecting', e) // eslint-disable-line\n    }\n\n    function data(x) {\n      if (x[0] === 0x77) {\n        parse(x.subarray(25), state, sql.options.parsers, handle, options.transform)\n      } else if (x[0] === 0x6b && x[17]) {\n        state.lsn = x.subarray(1, 9)\n        pong()\n      }\n    }\n\n    function handle(a, b) {\n      const path = b.relation.schema + '.' + b.relation.table\n      call('*', a, b)\n      call('*:' + path, a, b)\n      b.relation.keys.length && call('*:' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)\n      call(b.command, a, b)\n      call(b.command + ':' + path, a, b)\n      b.relation.keys.length && call(b.command + ':' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)\n    }\n\n    function pong() {\n      const x = Buffer.alloc(34)\n      x[0] = 'r'.charCodeAt(0)\n      x.fill(state.lsn, 1)\n      x.writeBigInt64BE(BigInt(Date.now() - Date.UTC(2000, 0, 1)) * BigInt(1000), 25)\n      stream.write(x)\n    }\n  }\n\n  function call(x, a, b) {\n    subscribers.has(x) && subscribers.get(x).forEach(({ fn }) => fn(a, b, x))\n  }\n}\n\nfunction Time(x) {\n  return new Date(Date.UTC(2000, 0, 1) + Number(x / BigInt(1000)))\n}\n\nfunction parse(x, state, parsers, handle, transform) {\n  const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)\n\n  Object.entries({\n    R: x => {  // Relation\n      let i = 1\n      const r = state[x.readUInt32BE(i)] = {\n        schema: x.toString('utf8', i += 4, i = x.indexOf(0, i)) || 'pg_catalog',\n        table: x.toString('utf8', i + 1, i = x.indexOf(0, i + 1)),\n        columns: Array(x.readUInt16BE(i += 2)),\n        keys: []\n      }\n      i += 2\n\n      let columnIndex = 0\n        , column\n\n      while (i < x.length) {\n        column = r.columns[columnIndex++] = {\n          key: x[i++],\n          name: transform.column.from\n            ? transform.column.from(x.toString('utf8', i, i = x.indexOf(0, i)))\n            : x.toString('utf8', i, i = x.indexOf(0, i)),\n          type: x.readUInt32BE(i += 1),\n          parser: parsers[x.readUInt32BE(i)],\n          atttypmod: x.readUInt32BE(i += 4)\n        }\n\n        column.key && r.keys.push(column)\n        i += 4\n      }\n    },\n    Y: () => { /* noop */ }, // Type\n    O: () => { /* noop */ }, // Origin\n    B: x => { // Begin\n      state.date = Time(x.readBigInt64BE(9))\n      state.lsn = x.subarray(1, 9)\n    },\n    I: x => { // Insert\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      const { row } = tuples(x, relation.columns, i += 7, transform)\n\n      handle(row, {\n        command: 'insert',\n        relation\n      })\n    },\n    D: x => { // Delete\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      i += 4\n      const key = x[i] === 75\n      handle(key || x[i] === 79\n        ? tuples(x, relation.columns, i += 3, transform).row\n        : null\n      , {\n        command: 'delete',\n        relation,\n        key\n      })\n    },\n    U: x => { // Update\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      i += 4\n      const key = x[i] === 75\n      const xs = key || x[i] === 79\n        ? tuples(x, relation.columns, i += 3, transform)\n        : null\n\n      xs && (i = xs.i)\n\n      const { row } = tuples(x, relation.columns, i + 3, transform)\n\n      handle(row, {\n        command: 'update',\n        relation,\n        key,\n        old: xs && xs.row\n      })\n    },\n    T: () => { /* noop */ }, // Truncate,\n    C: () => { /* noop */ }  // Commit\n  }).reduce(char, {})[x[0]](x)\n}\n\nfunction tuples(x, columns, xi, transform) {\n  let type\n    , column\n    , value\n\n  const row = transform.raw ? new Array(columns.length) : {}\n  for (let i = 0; i < columns.length; i++) {\n    type = x[xi++]\n    column = columns[i]\n    value = type === 110 // n\n      ? null\n      : type === 117 // u\n        ? undefined\n        : column.parser === undefined\n          ? x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi))\n          : column.parser.array === true\n            ? column.parser(x.toString('utf8', xi + 5, xi += 4 + x.readUInt32BE(xi)))\n            : column.parser(x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi)))\n\n    transform.raw\n      ? (row[i] = transform.raw === true\n        ? value\n        : transform.value.from ? transform.value.from(value, column) : value)\n      : (row[column.name] = transform.value.from\n        ? transform.value.from(value, column)\n        : value\n      )\n  }\n\n  return { i: xi, row: transform.row.from ? transform.row.from(row) : row }\n}\n\nfunction parseEvent(x) {\n  const xs = x.match(/^(\\*|insert|update|delete)?:?([^.]+?\\.?[^=]+)?=?(.+)?/i) || []\n\n  if (!xs)\n    throw new Error('Malformed subscribe pattern: ' + x)\n\n  const [, command, path, key] = xs\n\n  return (command || '*')\n       + (path ? ':' + (path.indexOf('.') === -1 ? 'public.' + path : path) : '')\n       + (key ? '=' + key : '')\n}\n"
  },
  {
    "path": "deno/src/types.js",
    "content": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nimport { Query } from './query.js'\nimport { Errors } from './errors.js'\n\nexport const types = {\n  string: {\n    to: 25,\n    from: null,             // defaults to string\n    serialize: x => '' + x\n  },\n  number: {\n    to: 0,\n    from: [21, 23, 26, 700, 701],\n    serialize: x => '' + x,\n    parse: x => +x\n  },\n  json: {\n    to: 114,\n    from: [114, 3802],\n    serialize: x => JSON.stringify(x),\n    parse: x => JSON.parse(x)\n  },\n  boolean: {\n    to: 16,\n    from: 16,\n    serialize: x => x === true ? 't' : 'f',\n    parse: x => x === 't'\n  },\n  date: {\n    to: 1184,\n    from: [1082, 1114, 1184],\n    serialize: x => (x instanceof Date ? x : new Date(x)).toISOString(),\n    parse: x => new Date(x)\n  },\n  bytea: {\n    to: 17,\n    from: 17,\n    serialize: x => '\\\\x' + Buffer.from(x).toString('hex'),\n    parse: x => Buffer.from(x.slice(2), 'hex')\n  }\n}\n\nclass NotTagged { then() { notTagged() } catch() { notTagged() } finally() { notTagged() }}\n\nexport class Identifier extends NotTagged {\n  constructor(value) {\n    super()\n    this.value = escapeIdentifier(value)\n  }\n}\n\nexport class Parameter extends NotTagged {\n  constructor(value, type, array) {\n    super()\n    this.value = value\n    this.type = type\n    this.array = array\n  }\n}\n\nexport class Builder extends NotTagged {\n  constructor(first, rest) {\n    super()\n    this.first = first\n    this.rest = rest\n  }\n\n  build(before, parameters, types, options) {\n    const keyword = builders.map(([x, fn]) => ({ fn, i: before.search(x) })).sort((a, b) => a.i - b.i).pop()\n    return keyword.i === -1\n      ? escapeIdentifiers(this.first, options)\n      : keyword.fn(this.first, this.rest, parameters, types, options)\n  }\n}\n\nexport function handleValue(x, parameters, types, options) {\n  let value = x instanceof Parameter ? x.value : x\n  if (value === undefined) {\n    x instanceof Parameter\n      ? x.value = options.transform.undefined\n      : value = x = options.transform.undefined\n\n    if (value === undefined)\n      throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')\n  }\n\n  return '$' + (types.push(\n    x instanceof Parameter\n      ? (parameters.push(x.value), x.array\n        ? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)\n        : x.type\n      )\n      : (parameters.push(x), inferType(x))\n  ))\n}\n\nconst defaultHandlers = typeHandlers(types)\n\nexport function stringify(q, string, value, parameters, types, options) { // eslint-disable-line\n  for (let i = 1; i < q.strings.length; i++) {\n    string += (stringifyValue(string, value, parameters, types, options)) + q.strings[i]\n    value = q.args[i]\n  }\n\n  return string\n}\n\nfunction stringifyValue(string, value, parameters, types, o) {\n  return (\n    value instanceof Builder ? value.build(string, parameters, types, o) :\n    value instanceof Query ? fragment(value, parameters, types, o) :\n    value instanceof Identifier ? value.value :\n    value && value[0] instanceof Query ? value.reduce((acc, x) => acc + ' ' + fragment(x, parameters, types, o), '') :\n    handleValue(value, parameters, types, o)\n  )\n}\n\nfunction fragment(q, parameters, types, options) {\n  q.fragment = true\n  return stringify(q, q.strings[0], q.args[0], parameters, types, options)\n}\n\nfunction valuesBuilder(first, parameters, types, columns, options) {\n  return first.map(row =>\n    '(' + columns.map(column =>\n      stringifyValue('values', row[column], parameters, types, options)\n    ).join(',') + ')'\n  ).join(',')\n}\n\nfunction values(first, rest, parameters, types, options) {\n  const multi = Array.isArray(first[0])\n  const columns = rest.length ? rest.flat() : Object.keys(multi ? first[0] : first)\n  return valuesBuilder(multi ? first : [first], parameters, types, columns, options)\n}\n\nfunction select(first, rest, parameters, types, options) {\n  typeof first === 'string' && (first = [first].concat(rest))\n  if (Array.isArray(first))\n    return escapeIdentifiers(first, options)\n\n  let value\n  const columns = rest.length ? rest.flat() : Object.keys(first)\n  return columns.map(x => {\n    value = first[x]\n    return (\n      value instanceof Query ? fragment(value, parameters, types, options) :\n      value instanceof Identifier ? value.value :\n      handleValue(value, parameters, types, options)\n    ) + ' as ' + escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)\n  }).join(',')\n}\n\nconst builders = Object.entries({\n  values,\n  in: (...xs) => {\n    const x = values(...xs)\n    return x === '()' ? '(null)' : x\n  },\n  select,\n  as: select,\n  returning: select,\n  '\\\\(': select,\n\n  update(first, rest, parameters, types, options) {\n    return (rest.length ? rest.flat() : Object.keys(first)).map(x =>\n      escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x) +\n      '=' + stringifyValue('values', first[x], parameters, types, options)\n    )\n  },\n\n  insert(first, rest, parameters, types, options) {\n    const columns = rest.length ? rest.flat() : Object.keys(Array.isArray(first) ? first[0] : first)\n    return '(' + escapeIdentifiers(columns, options) + ')values' +\n    valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, columns, options)\n  }\n}).map(([x, fn]) => ([new RegExp('((?:^|[\\\\s(])' + x + '(?:$|[\\\\s(]))(?![\\\\s\\\\S]*\\\\1)', 'i'), fn]))\n\nfunction notTagged() {\n  throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')\n}\n\nexport const serializers = defaultHandlers.serializers\nexport const parsers = defaultHandlers.parsers\n\nexport const END = {}\n\nfunction firstIsString(x) {\n  if (Array.isArray(x))\n    return firstIsString(x[0])\n  return typeof x === 'string' ? 1009 : 0\n}\n\nexport const mergeUserTypes = function(types) {\n  const user = typeHandlers(types || {})\n  return {\n    serializers: Object.assign({}, serializers, user.serializers),\n    parsers: Object.assign({}, parsers, user.parsers)\n  }\n}\n\nfunction typeHandlers(types) {\n  return Object.keys(types).reduce((acc, k) => {\n    types[k].from && [].concat(types[k].from).forEach(x => acc.parsers[x] = types[k].parse)\n    if (types[k].serialize) {\n      acc.serializers[types[k].to] = types[k].serialize\n      types[k].from && [].concat(types[k].from).forEach(x => acc.serializers[x] = types[k].serialize)\n    }\n    return acc\n  }, { parsers: {}, serializers: {} })\n}\n\nfunction escapeIdentifiers(xs, { transform: { column } }) {\n  return xs.map(x => escapeIdentifier(column.to ? column.to(x) : x)).join(',')\n}\n\nexport const escapeIdentifier = function escape(str) {\n  return '\"' + str.replace(/\"/g, '\"\"').replace(/\\./g, '\".\"') + '\"'\n}\n\nexport const inferType = function inferType(x) {\n  return (\n    x instanceof Parameter ? x.type :\n    x instanceof Date ? 1184 :\n    x instanceof Uint8Array ? 17 :\n    (x === true || x === false) ? 16 :\n    typeof x === 'bigint' ? 20 :\n    Array.isArray(x) ? inferType(x[0]) :\n    0\n  )\n}\n\nconst escapeBackslash = /\\\\/g\nconst escapeQuote = /\"/g\n\nfunction arrayEscape(x) {\n  return x\n    .replace(escapeBackslash, '\\\\\\\\')\n    .replace(escapeQuote, '\\\\\"')\n}\n\nexport const arraySerializer = function arraySerializer(xs, serializer, options, typarray) {\n  if (Array.isArray(xs) === false)\n    return xs\n\n  if (!xs.length)\n    return '{}'\n\n  const first = xs[0]\n  // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter\n  const delimiter = typarray === 1020 ? ';' : ','\n\n  if (Array.isArray(first) && !first.type)\n    return '{' + xs.map(x => arraySerializer(x, serializer, options, typarray)).join(delimiter) + '}'\n\n  return '{' + xs.map(x => {\n    if (x === undefined) {\n      x = options.transform.undefined\n      if (x === undefined)\n        throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')\n    }\n\n    return x === null\n      ? 'null'\n      : '\"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '\"'\n  }).join(delimiter) + '}'\n}\n\nconst arrayParserState = {\n  i: 0,\n  char: null,\n  str: '',\n  quoted: false,\n  last: 0\n}\n\nexport const arrayParser = function arrayParser(x, parser, typarray) {\n  arrayParserState.i = arrayParserState.last = 0\n  return arrayParserLoop(arrayParserState, x, parser, typarray)\n}\n\nfunction arrayParserLoop(s, x, parser, typarray) {\n  const xs = []\n  // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter\n  const delimiter = typarray === 1020 ? ';' : ','\n  for (; s.i < x.length; s.i++) {\n    s.char = x[s.i]\n    if (s.quoted) {\n      if (s.char === '\\\\') {\n        s.str += x[++s.i]\n      } else if (s.char === '\"') {\n        xs.push(parser ? parser(s.str) : s.str)\n        s.str = ''\n        s.quoted = x[s.i + 1] === '\"'\n        s.last = s.i + 2\n      } else {\n        s.str += s.char\n      }\n    } else if (s.char === '\"') {\n      s.quoted = true\n    } else if (s.char === '{') {\n      s.last = ++s.i\n      xs.push(arrayParserLoop(s, x, parser, typarray))\n    } else if (s.char === '}') {\n      s.quoted = false\n      s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))\n      s.last = s.i + 1\n      break\n    } else if (s.char === delimiter && s.p !== '}' && s.p !== '\"') {\n      xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))\n      s.last = s.i + 1\n    }\n    s.p = s.char\n  }\n  s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i + 1)) : x.slice(s.last, s.i + 1))\n  return xs\n}\n\nexport const toCamel = x => {\n  let str = x[0]\n  for (let i = 1; i < x.length; i++)\n    str += x[i] === '_' ? x[++i].toUpperCase() : x[i]\n  return str\n}\n\nexport const toPascal = x => {\n  let str = x[0].toUpperCase()\n  for (let i = 1; i < x.length; i++)\n    str += x[i] === '_' ? x[++i].toUpperCase() : x[i]\n  return str\n}\n\nexport const toKebab = x => x.replace(/_/g, '-')\n\nexport const fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()\nexport const fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()\nexport const fromKebab = x => x.replace(/-/g, '_')\n\nfunction createJsonTransform(fn) {\n  return function jsonTransform(x, column) {\n    return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)\n      ? Array.isArray(x)\n        ? x.map(x => jsonTransform(x, column))\n        : Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})\n      : x\n  }\n}\n\ntoCamel.column = { from: toCamel }\ntoCamel.value = { from: createJsonTransform(toCamel) }\nfromCamel.column = { to: fromCamel }\n\nexport const camel = { ...toCamel }\ncamel.column.to = fromCamel\n\ntoPascal.column = { from: toPascal }\ntoPascal.value = { from: createJsonTransform(toPascal) }\nfromPascal.column = { to: fromPascal }\n\nexport const pascal = { ...toPascal }\npascal.column.to = fromPascal\n\ntoKebab.column = { from: toKebab }\ntoKebab.value = { from: createJsonTransform(toKebab) }\nfromKebab.column = { to: fromKebab }\n\nexport const kebab = { ...toKebab }\nkebab.column.to = fromKebab\n"
  },
  {
    "path": "deno/tests/bootstrap.js",
    "content": "import { spawn } from 'https://deno.land/std@0.132.0/node/child_process.ts'\n\nawait exec('dropdb', ['postgres_js_test'])\n\nawait exec('psql', ['-c', 'alter system set ssl=on'])\nawait exec('psql', ['-c', 'drop user postgres_js_test'])\nawait exec('psql', ['-c', 'create user postgres_js_test'])\nawait exec('psql', ['-c', 'alter system set password_encryption=md5'])\nawait exec('psql', ['-c', 'select pg_reload_conf()'])\nawait exec('psql', ['-c', 'drop user if exists postgres_js_test_md5'])\nawait exec('psql', ['-c', 'create user postgres_js_test_md5 with password \\'postgres_js_test_md5\\''])\nawait exec('psql', ['-c', 'alter system set password_encryption=\\'scram-sha-256\\''])\nawait exec('psql', ['-c', 'select pg_reload_conf()'])\nawait exec('psql', ['-c', 'drop user if exists postgres_js_test_scram'])\nawait exec('psql', ['-c', 'create user postgres_js_test_scram with password \\'postgres_js_test_scram\\''])\n\nawait exec('createdb', ['postgres_js_test'])\nawait exec('psql', ['-c', 'grant all on database postgres_js_test to postgres_js_test'])\nawait exec('psql', ['-c', 'alter database postgres_js_test owner to postgres_js_test'])\n\nfunction ignore(cmd, args) {\n  const { stderr } = spawnSync(cmd, args, { stdio: 'pipe', encoding: 'utf8' })\n  if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist') && !stderr.includes('WARNING:'))\n    throw stderr\n}\n\nexport async function exec(cmd, args) { // eslint-disable-line\n  let stderr = ''\n  const cp = await spawn(cmd, args, { stdio: 'pipe', encoding: 'utf8' }) // eslint-disable-line\n  cp.stderr.on('data', x => stderr += x)\n  await new Promise(x => cp.on('exit', x))\n  if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist'))\n    throw new Error(stderr)\n}\n"
  },
  {
    "path": "deno/tests/copy.csv",
    "content": "1\t2\t3\n4\t5\t6\n"
  },
  {
    "path": "deno/tests/index.js",
    "content": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nimport process from 'https://deno.land/std@0.132.0/node/process.ts'\nimport { exec } from './bootstrap.js'\n\nimport { t, nt, ot } from './test.js' // eslint-disable-line\nimport { net } from '../polyfills.js'\nimport fs from 'https://deno.land/std@0.132.0/node/fs.ts'\nimport crypto from 'https://deno.land/std@0.132.0/node/crypto.ts'\n\nimport postgres from '../src/index.js'\nconst delay = ms => new Promise(r => setTimeout(r, ms))\n\nconst rel = x => new URL(x, import.meta.url)\nconst idle_timeout = 1\n\nconst login = {\n  user: 'postgres_js_test'\n}\n\nconst login_md5 = {\n  user: 'postgres_js_test_md5',\n  pass: 'postgres_js_test_md5'\n}\n\nconst login_scram = {\n  user: 'postgres_js_test_scram',\n  pass: 'postgres_js_test_scram'\n}\n\nconst options = {\n  db: 'postgres_js_test',\n  user: login.user,\n  pass: login.pass,\n  idle_timeout,\n  connect_timeout: 1,\n  max: 1\n}\n\nconst sql = postgres(options)\n\nt('Connects with no options', async() => {\n  const sql = postgres({ max: 1 })\n\n  const result = (await sql`select 1 as x`)[0].x\n  await sql.end()\n\n  return [1, result]\n})\n\nt('Uses default database without slash', async() => {\n  const sql = postgres('postgres://localhost')\n  return [sql.options.user, sql.options.database]\n})\n\nt('Uses default database with slash', async() => {\n  const sql = postgres('postgres://localhost/')\n  return [sql.options.user, sql.options.database]\n})\n\nt('Result is array', async() =>\n  [true, Array.isArray(await sql`select 1`)]\n)\n\nt('Result has count', async() =>\n  [1, (await sql`select 1`).count]\n)\n\nt('Result has command', async() =>\n  ['SELECT', (await sql`select 1`).command]\n)\n\nt('Create table', async() =>\n  ['CREATE TABLE', (await sql`create table test(int int)`).command, await sql`drop table test`]\n)\n\nt('Drop table', { timeout: 2 }, async() => {\n  await sql`create table test(int int)`\n  return ['DROP TABLE', (await sql`drop table test`).command]\n})\n\nt('null', async() =>\n  [null, (await sql`select ${ null } as x`)[0].x]\n)\n\nt('Integer', async() =>\n  ['1', (await sql`select ${ 1 } as x`)[0].x]\n)\n\nt('String', async() =>\n  ['hello', (await sql`select ${ 'hello' } as x`)[0].x]\n)\n\nt('Boolean false', async() =>\n  [false, (await sql`select ${ false } as x`)[0].x]\n)\n\nt('Boolean true', async() =>\n  [true, (await sql`select ${ true } as x`)[0].x]\n)\n\nt('Date', async() => {\n  const now = new Date()\n  return [0, now - (await sql`select ${ now } as x`)[0].x]\n})\n\nt('Json', async() => {\n  const x = (await sql`select ${ sql.json({ a: 'hello', b: 42 }) } as x`)[0].x\n  return ['hello,42', [x.a, x.b].join()]\n})\n\nt('implicit json', async() => {\n  const x = (await sql`select ${ { a: 'hello', b: 42 } }::json as x`)[0].x\n  return ['hello,42', [x.a, x.b].join()]\n})\n\nt('implicit jsonb', async() => {\n  const x = (await sql`select ${ { a: 'hello', b: 42 } }::jsonb as x`)[0].x\n  return ['hello,42', [x.a, x.b].join()]\n})\n\nt('Empty array', async() =>\n  [true, Array.isArray((await sql`select ${ sql.array([], 1009) } as x`)[0].x)]\n)\n\nt('String array', async() =>\n  ['123', (await sql`select ${ '{1,2,3}' }::int[] as x`)[0].x.join('')]\n)\n\nt('Array of Integer', async() =>\n  ['3', (await sql`select ${ sql.array([1, 2, 3]) } as x`)[0].x[2]]\n)\n\nt('Array of String', async() =>\n  ['c', (await sql`select ${ sql.array(['a', 'b', 'c']) } as x`)[0].x[2]]\n)\n\nt('Array of Date', async() => {\n  const now = new Date()\n  return [now.getTime(), (await sql`select ${ sql.array([now, now, now]) } as x`)[0].x[2].getTime()]\n})\n\nt('Array of Box', async() => [\n  '(3,4),(1,2);(6,7),(4,5)',\n  (await sql`select ${ '{(1,2),(3,4);(4,5),(6,7)}' }::box[] as x`)[0].x.join(';')\n])\n\nt('Nested array n2', async() =>\n  ['4', (await sql`select ${ sql.array([[1, 2], [3, 4]]) } as x`)[0].x[1][1]]\n)\n\nt('Nested array n3', async() =>\n  ['6', (await sql`select ${ sql.array([[[1, 2]], [[3, 4]], [[5, 6]]]) } as x`)[0].x[2][0][1]]\n)\n\nt('Escape in arrays', async() =>\n  ['Hello \"you\",c:\\\\windows', (await sql`select ${ sql.array(['Hello \"you\"', 'c:\\\\windows']) } as x`)[0].x.join(',')]\n)\n\nt('Escapes', async() => {\n  return ['hej\"hej', Object.keys((await sql`select 1 as ${ sql('hej\"hej') }`)[0])[0]]\n})\n\nt('null for int', async() => {\n  await sql`create table test (x int)`\n  return [1, (await sql`insert into test values(${ null })`).count, await sql`drop table test`]\n})\n\nt('Throws on illegal transactions', async() => {\n  const sql = postgres({ ...options, max: 2, fetch_types: false })\n  const error = await sql`begin`.catch(e => e)\n  return [\n    error.code,\n    'UNSAFE_TRANSACTION'\n  ]\n})\n\nt('Transaction throws', async() => {\n  await sql`create table test (a int)`\n  return ['22P02', await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql`insert into test values('hej')`\n  }).catch(x => x.code), await sql`drop table test`]\n})\n\nt('Transaction rolls back', async() => {\n  await sql`create table test (a int)`\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql`insert into test values('hej')`\n  }).catch(() => { /* ignore */ })\n  return [0, (await sql`select a from test`).count, await sql`drop table test`]\n})\n\nt('Transaction throws on uncaught savepoint', async() => {\n  await sql`create table test (a int)`\n\n  return ['fail', (await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoint(async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('fail')\n    })\n  }).catch((err) => err.message)), await sql`drop table test`]\n})\n\nt('Transaction throws on uncaught named savepoint', async() => {\n  await sql`create table test (a int)`\n\n  return ['fail', (await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoit('watpoint', async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('fail')\n    })\n  }).catch(() => 'fail')), await sql`drop table test`]\n})\n\nt('Transaction succeeds on caught savepoint', async() => {\n  await sql`create table test (a int)`\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoint(async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('please rollback')\n    }).catch(() => { /* ignore */ })\n    await sql`insert into test values(3)`\n  })\n\n  return ['2', (await sql`select count(1) from test`)[0].count, await sql`drop table test`]\n})\n\nt('Savepoint returns Result', async() => {\n  let result\n  await sql.begin(async sql => {\n    result = await sql.savepoint(sql =>\n      sql`select 1 as x`\n    )\n  })\n\n  return [1, result[0].x]\n})\n\nt('Prepared transaction', async() => {\n  await sql`create table test (a int)`\n\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.prepare('tx1')\n  })\n\n  await sql`commit prepared 'tx1'`\n\n  return ['1', (await sql`select count(1) from test`)[0].count, await sql`drop table test`]\n})\n\nt('Transaction requests are executed implicitly', async() => {\n  const sql = postgres({ debug: true, idle_timeout: 1, fetch_types: false })\n  return [\n    'testing',\n    (await sql.begin(sql => [\n      sql`select set_config('postgres_js.test', 'testing', true)`,\n      sql`select current_setting('postgres_js.test') as x`\n    ]))[1][0].x\n  ]\n})\n\nt('Uncaught transaction request errors bubbles to transaction', async() => [\n  '42703',\n  (await sql.begin(sql => [\n    sql`select wat`,\n    sql`select current_setting('postgres_js.test') as x, ${ 1 } as a`\n  ]).catch(e => e.code))\n])\n\nt('Fragments in transactions', async() => [\n  true,\n  (await sql.begin(sql => sql`select true as x where ${ sql`1=1` }`))[0].x\n])\n\nt('Transaction rejects with rethrown error', async() => [\n  'WAT',\n  await sql.begin(async sql => {\n    try {\n      await sql`select exception`\n    } catch (ex) {\n      throw new Error('WAT')\n    }\n  }).catch(e => e.message)\n])\n\nt('Parallel transactions', async() => {\n  await sql`create table test (a int)`\n  return ['11', (await Promise.all([\n    sql.begin(sql => sql`select 1`),\n    sql.begin(sql => sql`select 1`)\n  ])).map(x => x.count).join(''), await sql`drop table test`]\n})\n\nt('Many transactions at beginning of connection', async() => {\n  const sql = postgres(options)\n  const xs = await Promise.all(Array.from({ length: 100 }, () => sql.begin(sql => sql`select 1`)))\n  return [100, xs.length]\n})\n\nt('Transactions array', async() => {\n  await sql`create table test (a int)`\n\n  return ['11', (await sql.begin(sql => [\n    sql`select 1`.then(x => x),\n    sql`select 1`\n  ])).map(x => x.count).join(''), await sql`drop table test`]\n})\n\nt('Transaction waits', async() => {\n  await sql`create table test (a int)`\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoint(async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('please rollback')\n    }).catch(() => { /* ignore */ })\n    await sql`insert into test values(3)`\n  })\n\n  return ['11', (await Promise.all([\n    sql.begin(sql => sql`select 1`),\n    sql.begin(sql => sql`select 1`)\n  ])).map(x => x.count).join(''), await sql`drop table test`]\n})\n\nt('Helpers in Transaction', async() => {\n  return ['1', (await sql.begin(async sql =>\n    await sql`select ${ sql({ x: 1 }) }`\n  ))[0].x]\n})\n\nt('Undefined values throws', async() => {\n  let error\n\n  await sql`\n    select ${ undefined } as x\n  `.catch(x => error = x.code)\n\n  return ['UNDEFINED_VALUE', error]\n})\n\nt('Transform undefined', async() => {\n  const sql = postgres({ ...options, transform: { undefined: null } })\n  return [null, (await sql`select ${ undefined } as x`)[0].x]\n})\n\nt('Transform undefined in array', async() => {\n  const sql = postgres({ ...options, transform: { undefined: null } })\n  return [null, (await sql`select * from (values ${ sql([undefined, undefined]) }) as x(x, y)`)[0].y]\n})\n\nt('Null sets to null', async() =>\n  [null, (await sql`select ${ null } as x`)[0].x]\n)\n\nt('Throw syntax error', async() =>\n  ['42601', (await sql`wat 1`.catch(x => x)).code]\n)\n\nt('Connect using uri', async() =>\n  [true, await new Promise((resolve, reject) => {\n    const sql = postgres('postgres://' + login.user + ':' + (login.pass || '') + '@localhost:5432/' + options.db, {\n      idle_timeout\n    })\n    sql`select 1`.then(() => resolve(true), reject)\n  })]\n)\n\nt('Options from uri with special characters in user and pass', async() => {\n  const opt = postgres({ user: 'öla', pass: 'pass^word' }).options\n  return [[opt.user, opt.pass].toString(), 'öla,pass^word']\n})\n\nt('Fail with proper error on no host', async() =>\n  ['ECONNREFUSED', (await new Promise((resolve, reject) => {\n    const sql = postgres('postgres://localhost:33333/' + options.db, {\n      idle_timeout\n    })\n    sql`select 1`.then(reject, resolve)\n  })).code]\n)\n\nt('Connect using SSL', async() =>\n  [true, (await new Promise((resolve, reject) => {\n    postgres({\n      ssl: { rejectUnauthorized: false },\n      idle_timeout\n    })`select 1`.then(() => resolve(true), reject)\n  }))]\n)\n\nt('Connect using SSL require', async() =>\n  [true, (await new Promise((resolve, reject) => {\n    postgres({\n      ssl: 'require',\n      idle_timeout\n    })`select 1`.then(() => resolve(true), reject)\n  }))]\n)\n\nt('Connect using SSL direct', async() => {\n  const [{ supported }] = await sql`select current_setting('server_version_num')::int >= 180000 as supported`\n  return [true, !supported || (await new Promise((resolve, reject) => {\n    postgres({\n      ssl: 'require',\n      sslnegotiation: 'direct',\n      idle_timeout\n    })`select 1`.then(() => resolve(true), reject)\n  }))]\n})\n\nt('Connect using SSL prefer', async() => {\n  await exec('psql', ['-c', 'alter system set ssl=off'])\n  await exec('psql', ['-c', 'select pg_reload_conf()'])\n\n  const sql = postgres({\n    ssl: 'prefer',\n    idle_timeout\n  })\n\n  return [\n    1, (await sql`select 1 as x`)[0].x,\n    await exec('psql', ['-c', 'alter system set ssl=on']),\n    await exec('psql', ['-c', 'select pg_reload_conf()'])\n  ]\n})\n\nt('Reconnect using SSL', { timeout: 2 }, async() => {\n  const sql = postgres({\n    ssl: 'require',\n    idle_timeout: 0.1\n  })\n\n  await sql`select 1`\n  await delay(200)\n\n  return [1, (await sql`select 1 as x`)[0].x]\n})\n\nt('Proper handling of non object Errors', async() => {\n  const sql = postgres({ socket: () => { throw 'wat' } }) // eslint-disable-line\n\n  return [\n    'wat', await sql`select 1 as x`.catch(e => e.message)\n  ]\n})\n\nt('Proper handling of null Errors', async() => {\n  const sql = postgres({ socket: () => { throw null } }) // eslint-disable-line\n\n  return [\n    'null', await sql`select 1 as x`.catch(e => e.message)\n  ]\n})\n\nt('Ensure reserve on connection throws proper error', async() => {\n  const sql = postgres({ socket: () => { throw 'wat' }, idle_timeout }) // eslint-disable-line\n\n  return [\n    'wat', await sql.reserve().catch(e => e)\n  ]\n})\n\nt('Login without password', async() => {\n  return [true, (await postgres({ ...options, ...login })`select true as x`)[0].x]\n})\n\nt('Login using MD5', async() => {\n  return [true, (await postgres({ ...options, ...login_md5 })`select true as x`)[0].x]\n})\n\nt('Login using scram-sha-256', async() => {\n  return [true, (await postgres({ ...options, ...login_scram })`select true as x`)[0].x]\n})\n\nt('Parallel connections using scram-sha-256', {\n  timeout: 2\n}, async() => {\n  const sql = postgres({ ...options, ...login_scram })\n  return [true, (await Promise.all([\n    sql`select true as x, pg_sleep(0.01)`,\n    sql`select true as x, pg_sleep(0.01)`,\n    sql`select true as x, pg_sleep(0.01)`\n  ]))[0][0].x]\n})\n\nt('Support dynamic password function', async() => {\n  return [true, (await postgres({\n    ...options,\n    ...login_scram,\n    pass: () => 'postgres_js_test_scram'\n  })`select true as x`)[0].x]\n})\n\nt('Support dynamic async password function', async() => {\n  return [true, (await postgres({\n    ...options,\n    ...login_scram,\n    pass: () => Promise.resolve('postgres_js_test_scram')\n  })`select true as x`)[0].x]\n})\n\nt('Point type', async() => {\n  const sql = postgres({\n    ...options,\n    types: {\n      point: {\n        to: 600,\n        from: [600],\n        serialize: ([x, y]) => '(' + x + ',' + y + ')',\n        parse: (x) => x.slice(1, -1).split(',').map(x => +x)\n      }\n    }\n  })\n\n  await sql`create table test (x point)`\n  await sql`insert into test (x) values (${ sql.types.point([10, 20]) })`\n  return [20, (await sql`select x from test`)[0].x[1], await sql`drop table test`]\n})\n\nt('Point type array', async() => {\n  const sql = postgres({\n    ...options,\n    types: {\n      point: {\n        to: 600,\n        from: [600],\n        serialize: ([x, y]) => '(' + x + ',' + y + ')',\n        parse: (x) => x.slice(1, -1).split(',').map(x => +x)\n      }\n    }\n  })\n\n  await sql`create table test (x point[])`\n  await sql`insert into test (x) values (${ sql.array([sql.types.point([10, 20]), sql.types.point([20, 30])]) })`\n  return [30, (await sql`select x from test`)[0].x[1][1], await sql`drop table test`]\n})\n\nt('sql file', async() =>\n  [1, (await sql.file(rel('select.sql')))[0].x]\n)\n\nt('sql file has forEach', async() => {\n  let result\n  await sql\n    .file(rel('select.sql'), { cache: false })\n    .forEach(({ x }) => result = x)\n\n  return [1, result]\n})\n\nt('sql file throws', async() =>\n  ['ENOENT', (await sql.file(rel('selectomondo.sql')).catch(x => x.code))]\n)\n\nt('sql file cached', async() => {\n  await sql.file(rel('select.sql'))\n  await delay(20)\n\n  return [1, (await sql.file(rel('select.sql')))[0].x]\n})\n\nt('Parameters in file', async() => {\n  const result = await sql.file(\n    rel('select-param.sql'),\n    ['hello']\n  )\n  return ['hello', result[0].x]\n})\n\nt('Connection ended promise', async() => {\n  const sql = postgres(options)\n\n  await sql.end()\n\n  return [undefined, await sql.end()]\n})\n\nt('Connection ended timeout', async() => {\n  const sql = postgres(options)\n\n  await sql.end({ timeout: 10 })\n\n  return [undefined, await sql.end()]\n})\n\nt('Connection ended error', async() => {\n  const sql = postgres(options)\n  await sql.end()\n  return ['CONNECTION_ENDED', (await sql``.catch(x => x.code))]\n})\n\nt('Connection end does not cancel query', async() => {\n  const sql = postgres(options)\n\n  const promise = sql`select 1 as x`.execute()\n\n  await sql.end()\n\n  return [1, (await promise)[0].x]\n})\n\nt('Connection destroyed', async() => {\n  const sql = postgres(options)\n  process.nextTick(() => sql.end({ timeout: 0 }))\n  return ['CONNECTION_DESTROYED', await sql``.catch(x => x.code)]\n})\n\nt('Connection destroyed with query before', async() => {\n  const sql = postgres(options)\n      , error = sql`select pg_sleep(0.2)`.catch(err => err.code)\n\n  sql.end({ timeout: 0 })\n  return ['CONNECTION_DESTROYED', await error]\n})\n\nt('transform column', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: x => x.split('').reverse().join('') }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['dlrow_olleh', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('column toPascal', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: postgres.toPascal }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['HelloWorld', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('column toCamel', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: postgres.toCamel }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['helloWorld', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('column toKebab', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: postgres.toKebab }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['hello-world', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('Transform nested json in arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  return ['aBcD', (await sql`select '[{\"a_b\":1},{\"c_d\":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')]\n})\n\nt('Transform deeply nested json object in arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  return [\n    'childObj_deeplyNestedObj_grandchildObj',\n    (await sql`\n      select '[{\"nested_obj\": {\"child_obj\": 2, \"deeply_nested_obj\": {\"grandchild_obj\": 3}}}]'::jsonb as x\n    `)[0].x.map(x => {\n      let result\n      for (const key in x)\n        result = [...Object.keys(x[key]), ...Object.keys(x[key].deeplyNestedObj)]\n      return result\n    })[0]\n    .join('_')\n  ]\n})\n\nt('Transform deeply nested json array in arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  return [\n    'childArray_deeplyNestedArray_grandchildArray',\n    (await sql`\n      select '[{\"nested_array\": [{\"child_array\": 2, \"deeply_nested_array\": [{\"grandchild_array\":3}]}]}]'::jsonb AS x\n    `)[0].x.map((x) => {\n      let result\n      for (const key in x)\n        result = [...Object.keys(x[key][0]), ...Object.keys(x[key][0].deeplyNestedArray[0])]\n      return result\n    })[0]\n    .join('_')\n  ]\n})\n\nt('Bypass transform for json primitive', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n\n  const x = (\n    await sql`select 'null'::json as a, 'false'::json as b, '\"a\"'::json as c, '1'::json as d`\n  )[0]\n\n  return [\n    JSON.stringify({ a: null, b: false, c: 'a', d: 1 }),\n    JSON.stringify(x)\n  ]\n})\n\nt('Bypass transform for jsonb primitive', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n\n  const x = (\n    await sql`select 'null'::jsonb as a, 'false'::jsonb as b, '\"a\"'::jsonb as c, '1'::jsonb as d`\n  )[0]\n\n  return [\n    JSON.stringify({ a: null, b: false, c: 'a', d: 1 }),\n    JSON.stringify(x)\n  ]\n})\n\nt('unsafe', async() => {\n  await sql`create table test (x int)`\n  return [1, (await sql.unsafe('insert into test values ($1) returning *', [1]))[0].x, await sql`drop table test`]\n})\n\nt('unsafe simple', async() => {\n  return [1, (await sql.unsafe('select 1 as x'))[0].x]\n})\n\nt('unsafe simple includes columns', async() => {\n  return ['x', (await sql.unsafe('select 1 as x').values()).columns[0].name]\n})\n\nt('unsafe describe', async() => {\n  const q = 'insert into test values (1)'\n  await sql`create table test(a int unique)`\n  await sql.unsafe(q).describe()\n  const x = await sql.unsafe(q).describe()\n  return [\n    q,\n    x.string,\n    await sql`drop table test`\n  ]\n})\n\nt('simple query using unsafe with multiple statements', async() => {\n  return [\n    '1,2',\n    (await sql.unsafe('select 1 as x;select 2 as x')).map(x => x[0].x).join()\n  ]\n})\n\nt('simple query using simple() with multiple statements', async() => {\n  return [\n    '1,2',\n    (await sql`select 1 as x;select 2 as x`.simple()).map(x => x[0].x).join()\n  ]\n})\n\nt('listen and notify', async() => {\n  const sql = postgres(options)\n  const channel = 'hello'\n  const result = await new Promise(async r => {\n    await sql.listen(channel, r)\n    sql.notify(channel, 'works')\n  })\n\n  return [\n    'works',\n    result,\n    sql.end()\n  ]\n})\n\nt('double listen', async() => {\n  const sql = postgres(options)\n      , channel = 'hello'\n\n  let count = 0\n\n  await new Promise((resolve, reject) =>\n    sql.listen(channel, resolve)\n    .then(() => sql.notify(channel, 'world'))\n    .catch(reject)\n  ).then(() => count++)\n\n  await new Promise((resolve, reject) =>\n    sql.listen(channel, resolve)\n    .then(() => sql.notify(channel, 'world'))\n    .catch(reject)\n  ).then(() => count++)\n\n  // for coverage\n  sql.listen('weee', () => { /* noop */ }).then(sql.end)\n\n  return [2, count]\n})\n\nt('multiple listeners work after a reconnect', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const s1 = await sql.listen('test', x => xs.push('1', x))\n  await sql.listen('test', x => xs.push('2', x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await sql`select pg_terminate_backend(${ s1.state.pid })`\n  await delay(200)\n  await sql.notify('test', 'b')\n  await delay(50)\n  sql.end()\n\n  return ['1a2a1b2b', xs.join('')]\n})\n\nt('listen and notify with weird name', async() => {\n  const sql = postgres(options)\n  const channel = 'wat-;.ø.§'\n  const result = await new Promise(async r => {\n    const { unlisten } = await sql.listen(channel, r)\n    sql.notify(channel, 'works')\n    await delay(50)\n    await unlisten()\n  })\n\n  return [\n    'works',\n    result,\n    sql.end()\n  ]\n})\n\nt('listen and notify with upper case', async() => {\n  const sql = postgres(options)\n  const channel = 'withUpperChar'\n  const result = await new Promise(async r => {\n    await sql.listen(channel, r)\n    sql.notify(channel, 'works')\n  })\n\n  return [\n    'works',\n    result,\n    sql.end()\n  ]\n})\n\nt('listen reconnects', { timeout: 2 }, async() => {\n  const sql = postgres(options)\n      , resolvers = {}\n      , a = new Promise(r => resolvers.a = r)\n      , b = new Promise(r => resolvers.b = r)\n\n  let connects = 0\n\n  const { state: { pid } } = await sql.listen(\n    'test',\n    x => x in resolvers && resolvers[x](),\n    () => connects++\n  )\n  await sql.notify('test', 'a')\n  await a\n  await sql`select pg_terminate_backend(${ pid })`\n  await delay(100)\n  await sql.notify('test', 'b')\n  await b\n  sql.end()\n  return [connects, 2]\n})\n\nt('listen result reports correct connection state after reconnection', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const result = await sql.listen('test', x => xs.push(x))\n  const initialPid = result.state.pid\n  await sql.notify('test', 'a')\n  await sql`select pg_terminate_backend(${ initialPid })`\n  await delay(50)\n  sql.end()\n\n  return [result.state.pid !== initialPid, true]\n})\n\nt('unlisten removes subscription', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const { unlisten } = await sql.listen('test', x => xs.push(x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await unlisten()\n  await sql.notify('test', 'b')\n  await delay(50)\n  sql.end()\n\n  return ['a', xs.join('')]\n})\n\nt('listen after unlisten', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const { unlisten } = await sql.listen('test', x => xs.push(x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await unlisten()\n  await sql.notify('test', 'b')\n  await delay(50)\n  await sql.listen('test', x => xs.push(x))\n  await sql.notify('test', 'c')\n  await delay(50)\n  sql.end()\n\n  return ['ac', xs.join('')]\n})\n\nt('multiple listeners and unlisten one', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  await sql.listen('test', x => xs.push('1', x))\n  const s2 = await sql.listen('test', x => xs.push('2', x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await s2.unlisten()\n  await sql.notify('test', 'b')\n  await delay(50)\n  sql.end()\n\n  return ['1a2a1b', xs.join('')]\n})\n\nt('responds with server parameters (application_name)', async() =>\n  ['postgres.js', await new Promise((resolve, reject) => postgres({\n    ...options,\n    onparameter: (k, v) => k === 'application_name' && resolve(v)\n  })`select 1`.catch(reject))]\n)\n\nt('has server parameters', async() => {\n  return ['postgres.js', (await sql`select 1`.then(() => sql.parameters.application_name))]\n})\n\nt('big query body', { timeout: 2 }, async() => {\n  await sql`create table test (x int)`\n  return [50000, (await sql`insert into test ${\n    sql([...Array(50000).keys()].map(x => ({ x })))\n  }`).count, await sql`drop table test`]\n})\n\nt('Throws if more than 65534 parameters', async() => {\n  await sql`create table test (x int)`\n  return ['MAX_PARAMETERS_EXCEEDED', (await sql`insert into test ${\n    sql([...Array(65535).keys()].map(x => ({ x })))\n  }`.catch(e => e.code)), await sql`drop table test`]\n})\n\nt('let postgres do implicit cast of unknown types', async() => {\n  await sql`create table test (x timestamp with time zone)`\n  const [{ x }] = await sql`insert into test values (${ new Date().toISOString() }) returning *`\n  return [true, x instanceof Date, await sql`drop table test`]\n})\n\nt('only allows one statement', async() =>\n  ['42601', await sql`select 1; select 2`.catch(e => e.code)]\n)\n\nt('await sql() throws not tagged error', async() => {\n  let error\n  try {\n    await sql('select 1')\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('sql().then throws not tagged error', async() => {\n  let error\n  try {\n    sql('select 1').then(() => { /* noop */ })\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('sql().catch throws not tagged error', async() => {\n  let error\n  try {\n    await sql('select 1')\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('sql().finally throws not tagged error', async() => {\n  let error\n  try {\n    sql('select 1').finally(() => { /* noop */ })\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('little bobby tables', async() => {\n  const name = 'Robert\\'); DROP TABLE students;--'\n\n  await sql`create table students (name text, age int)`\n  await sql`insert into students (name) values (${ name })`\n\n  return [\n    name, (await sql`select name from students`)[0].name,\n    await sql`drop table students`\n  ]\n})\n\nt('Connection errors are caught using begin()', {\n  timeout: 2\n}, async() => {\n  let error\n  try {\n    const sql = postgres({ host: 'localhost', port: 1 })\n\n    await sql.begin(async(sql) => {\n      await sql`insert into test (label, value) values (${1}, ${2})`\n    })\n  } catch (err) {\n    error = err\n  }\n\n  return [\n    true,\n    error.code === 'ECONNREFUSED' ||\n    error.message === 'Connection refused (os error 61)'\n  ]\n})\n\nt('dynamic table name', async() => {\n  await sql`create table test(a int)`\n  return [\n    0, (await sql`select * from ${ sql('test') }`).count,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic schema name', async() => {\n  await sql`create table test(a int)`\n  return [\n    0, (await sql`select * from ${ sql('public') }.test`).count,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic schema and table name', async() => {\n  await sql`create table test(a int)`\n  return [\n    0, (await sql`select * from ${ sql('public.test') }`).count,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic column name', async() => {\n  return ['!not_valid', Object.keys((await sql`select 1 as ${ sql('!not_valid') }`)[0])[0]]\n})\n\nt('dynamic select as', async() => {\n  return ['2', (await sql`select ${ sql({ a: 1, b: 2 }) }`)[0].b]\n})\n\nt('dynamic select as pluck', async() => {\n  return [undefined, (await sql`select ${ sql({ a: 1, b: 2 }, 'a') }`)[0].b]\n})\n\nt('dynamic insert', async() => {\n  await sql`create table test (a int, b text)`\n  const x = { a: 42, b: 'the answer' }\n\n  return ['the answer', (await sql`insert into test ${ sql(x) } returning *`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic insert pluck', async() => {\n  await sql`create table test (a int, b text)`\n  const x = { a: 42, b: 'the answer' }\n\n  return [null, (await sql`insert into test ${ sql(x, 'a') } returning *`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic in with empty array', async() => {\n  await sql`create table test (a int)`\n  await sql`insert into test values (1)`\n  return [\n    (await sql`select * from test where null in ${ sql([]) }`).count,\n    0,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic in after insert', async() => {\n  await sql`create table test (a int, b text)`\n  const [{ x }] = await sql`\n    with x as (\n      insert into test values (1, 'hej')\n      returning *\n    )\n    select 1 in ${ sql([1, 2, 3]) } as x from x\n  `\n  return [\n    true, x,\n    await sql`drop table test`\n  ]\n})\n\nt('array insert', async() => {\n  await sql`create table test (a int, b int)`\n  return [2, (await sql`insert into test (a, b) values ${ sql([1, 2]) } returning *`)[0].b, await sql`drop table test`]\n})\n\nt('where parameters in()', async() => {\n  await sql`create table test (x text)`\n  await sql`insert into test values ('a')`\n  return [\n    (await sql`select * from test where x in ${ sql(['a', 'b', 'c']) }`)[0].x,\n    'a',\n    await sql`drop table test`\n  ]\n})\n\nt('where parameters in() values before', async() => {\n  return [2, (await sql`\n    with rows as (\n      select * from (values (1), (2), (3), (4)) as x(a)\n    )\n    select * from rows where a in ${ sql([3, 4]) }\n  `).count]\n})\n\nt('dynamic multi row insert', async() => {\n  await sql`create table test (a int, b text)`\n  const x = { a: 42, b: 'the answer' }\n\n  return [\n    'the answer',\n    (await sql`insert into test ${ sql([x, x]) } returning *`)[1].b, await sql`drop table test`\n  ]\n})\n\nt('dynamic update', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (17, 'wrong')`\n\n  return [\n    'the answer',\n    (await sql`update test set ${ sql({ a: 42, b: 'the answer' }) } returning *`)[0].b, await sql`drop table test`\n  ]\n})\n\nt('dynamic update pluck', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (17, 'wrong')`\n\n  return [\n    'wrong',\n    (await sql`update test set ${ sql({ a: 42, b: 'the answer' }, 'a') } returning *`)[0].b, await sql`drop table test`\n  ]\n})\n\nt('dynamic select array', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (42, 'yay')`\n  return ['yay', (await sql`select ${ sql(['a', 'b']) } from test`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic returning array', async() => {\n  await sql`create table test (a int, b text)`\n  return [\n    'yay',\n    (await sql`insert into test (a, b) values (42, 'yay') returning ${ sql(['a', 'b']) }`)[0].b,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic select args', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (42, 'yay')`\n  return ['yay', (await sql`select ${ sql('a', 'b') } from test`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic values single row', async() => {\n  const [{ b }] = await sql`\n    select * from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)\n  `\n\n  return ['b', b]\n})\n\nt('dynamic values multi row', async() => {\n  const [, { b }] = await sql`\n    select * from (values ${ sql([['a', 'b', 'c'], ['a', 'b', 'c']]) }) as x(a, b, c)\n  `\n\n  return ['b', b]\n})\n\nt('connection parameters', async() => {\n  const sql = postgres({\n    ...options,\n    connection: {\n      'some.var': 'yay'\n    }\n  })\n\n  return ['yay', (await sql`select current_setting('some.var') as x`)[0].x]\n})\n\nt('Multiple queries', async() => {\n  const sql = postgres(options)\n\n  return [4, (await Promise.all([\n    sql`select 1`,\n    sql`select 2`,\n    sql`select 3`,\n    sql`select 4`\n  ])).length]\n})\n\nt('Multiple statements', async() =>\n  [2, await sql.unsafe(`\n    select 1 as x;\n    select 2 as a;\n  `).then(([, [x]]) => x.a)]\n)\n\nt('throws correct error when authentication fails', async() => {\n  const sql = postgres({\n    ...options,\n    ...login_md5,\n    pass: 'wrong'\n  })\n  return ['28P01', await sql`select 1`.catch(e => e.code)]\n})\n\nt('notice', async() => {\n  let notice\n  const log = console.log // eslint-disable-line\n  console.log = function(x) { // eslint-disable-line\n    notice = x\n  }\n\n  const sql = postgres(options)\n\n  await sql`create table if not exists users()`\n  await sql`create table if not exists users()`\n\n  console.log = log // eslint-disable-line\n\n  return ['NOTICE', notice.severity]\n})\n\nt('notice hook', async() => {\n  let notice\n  const sql = postgres({\n    ...options,\n    onnotice: x => notice = x\n  })\n\n  await sql`create table if not exists users()`\n  await sql`create table if not exists users()`\n\n  return ['NOTICE', notice.severity]\n})\n\nt('bytea serializes and parses', async() => {\n  const buf = Buffer.from('wat')\n\n  await sql`create table test (x bytea)`\n  await sql`insert into test values (${ buf })`\n\n  return [\n    buf.toString(),\n    (await sql`select x from test`)[0].x.toString(),\n    await sql`drop table test`\n  ]\n})\n\nt('forEach', async() => {\n  let result\n  await sql`select 1 as x`.forEach(({ x }) => result = x)\n  return [1, result]\n})\n\nt('forEach returns empty array', async() => {\n  return [0, (await sql`select 1 as x`.forEach(() => { /* noop */ })).length]\n})\n\nt('Cursor', async() => {\n  const order = []\n  await sql`select 1 as x union select 2 as x`.cursor(async([x]) => {\n    order.push(x.x + 'a')\n    await delay(100)\n    order.push(x.x + 'b')\n  })\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Unsafe cursor', async() => {\n  const order = []\n  await sql.unsafe('select 1 as x union select 2 as x').cursor(async([x]) => {\n    order.push(x.x + 'a')\n    await delay(100)\n    order.push(x.x + 'b')\n  })\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Cursor custom n', async() => {\n  const order = []\n  await sql`select * from generate_series(1,20)`.cursor(10, async(x) => {\n    order.push(x.length)\n  })\n  return ['10,10', order.join(',')]\n})\n\nt('Cursor custom with rest n', async() => {\n  const order = []\n  await sql`select * from generate_series(1,20)`.cursor(11, async(x) => {\n    order.push(x.length)\n  })\n  return ['11,9', order.join(',')]\n})\n\nt('Cursor custom with less results than batch size', async() => {\n  const order = []\n  await sql`select * from generate_series(1,20)`.cursor(21, async(x) => {\n    order.push(x.length)\n  })\n  return ['20', order.join(',')]\n})\n\nt('Cursor cancel', async() => {\n  let result\n  await sql`select * from generate_series(1,10) as x`.cursor(async([{ x }]) => {\n    result = x\n    return sql.CLOSE\n  })\n  return [1, result]\n})\n\nt('Cursor throw', async() => {\n  const order = []\n  await sql`select 1 as x union select 2 as x`.cursor(async([x]) => {\n    order.push(x.x + 'a')\n    await delay(100)\n    throw new Error('watty')\n  }).catch(() => order.push('err'))\n  return ['1aerr', order.join('')]\n})\n\nt('Cursor error', async() => [\n  '42601',\n  await sql`wat`.cursor(() => { /* noop */ }).catch((err) => err.code)\n])\n\nt('Multiple Cursors', { timeout: 2 }, async() => {\n  const result = []\n  await sql.begin(async sql => [\n    await sql`select 1 as cursor, x from generate_series(1,4) as x`.cursor(async([row]) => {\n      result.push(row.x)\n      await new Promise(r => setTimeout(r, 20))\n    }),\n    await sql`select 2 as cursor, x from generate_series(101,104) as x`.cursor(async([row]) => {\n      result.push(row.x)\n      await new Promise(r => setTimeout(r, 10))\n    })\n  ])\n\n  return ['1,2,3,4,101,102,103,104', result.join(',')]\n})\n\nt('Cursor as async iterator', async() => {\n  const order = []\n  for await (const [x] of sql`select generate_series(1,2) as x;`.cursor()) {\n    order.push(x.x + 'a')\n    await delay(10)\n    order.push(x.x + 'b')\n  }\n\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Cursor as async iterator with break', async() => {\n  const order = []\n  for await (const xs of sql`select generate_series(1,2) as x;`.cursor()) {\n    order.push(xs[0].x + 'a')\n    await delay(10)\n    order.push(xs[0].x + 'b')\n    break\n  }\n\n  return ['1a1b', order.join('')]\n})\n\nt('Async Iterator Unsafe cursor', async() => {\n  const order = []\n  for await (const [x] of sql.unsafe('select 1 as x union select 2 as x').cursor()) {\n    order.push(x.x + 'a')\n    await delay(10)\n    order.push(x.x + 'b')\n  }\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Async Iterator Cursor custom n', async() => {\n  const order = []\n  for await (const x of sql`select * from generate_series(1,20)`.cursor(10))\n    order.push(x.length)\n\n  return ['10,10', order.join(',')]\n})\n\nt('Async Iterator Cursor custom with rest n', async() => {\n  const order = []\n  for await (const x of sql`select * from generate_series(1,20)`.cursor(11))\n    order.push(x.length)\n\n  return ['11,9', order.join(',')]\n})\n\nt('Async Iterator Cursor custom with less results than batch size', async() => {\n  const order = []\n  for await (const x of sql`select * from generate_series(1,20)`.cursor(21))\n    order.push(x.length)\n  return ['20', order.join(',')]\n})\n\nt('Transform row', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { row: () => 1 }\n  })\n\n  return [1, (await sql`select 'wat'`)[0]]\n})\n\nt('Transform row forEach', async() => {\n  let result\n  const sql = postgres({\n    ...options,\n    transform: { row: () => 1 }\n  })\n\n  await sql`select 1`.forEach(x => result = x)\n\n  return [1, result]\n})\n\nt('Transform value', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { value: () => 1 }\n  })\n\n  return [1, (await sql`select 'wat' as x`)[0].x]\n})\n\nt('Transform columns from', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.fromCamel\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`\n  await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`\n  return [\n    2,\n    (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].a_test,\n    await sql`drop table test`\n  ]\n})\n\nt('Transform columns to', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.toCamel\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ a_test: 1, b_test: 1 }]) }`\n  await sql`update test set ${ sql({ a_test: 2, b_test: 2 }) }`\n  return [\n    2,\n    (await sql`select a_test, b_test from test`)[0].aTest,\n    await sql`drop table test`\n  ]\n})\n\nt('Transform columns from and to', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`\n  await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`\n  return [\n    2,\n    (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].aTest,\n    await sql`drop table test`\n  ]\n})\n\nt('Transform columns from and to (legacy)', async() => {\n  const sql = postgres({\n    ...options,\n    transform: {\n      column: {\n        to: postgres.fromCamel,\n        from: postgres.toCamel\n      }\n    }\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`\n  await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`\n  return [\n    2,\n    (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].aTest,\n    await sql`drop table test`\n  ]\n})\n\nt('Unix socket', async() => {\n  const sql = postgres({\n    ...options,\n    host: process.env.PGSOCKET || '/tmp' // eslint-disable-line\n  })\n\n  return [1, (await sql`select 1 as x`)[0].x]\n})\n\nt('Big result', async() => {\n  return [100000, (await sql`select * from generate_series(1, 100000)`).count]\n})\n\nt('Debug', async() => {\n  let result\n  const sql = postgres({\n    ...options,\n    debug: (connection_id, str) => result = str\n  })\n\n  await sql`select 1`\n\n  return ['select 1', result]\n})\n\nt('bigint is returned as String', async() => [\n  'string',\n  typeof (await sql`select 9223372036854777 as x`)[0].x\n])\n\nt('int is returned as Number', async() => [\n  'number',\n  typeof (await sql`select 123 as x`)[0].x\n])\n\nt('numeric is returned as string', async() => [\n  'string',\n  typeof (await sql`select 1.2 as x`)[0].x\n])\n\nt('Async stack trace', async() => {\n  const sql = postgres({ ...options, debug: false })\n  return [\n    parseInt(new Error().stack.split('\\n')[1].match(':([0-9]+):')[1]) + 1,\n    parseInt(await sql`error`.catch(x => x.stack.split('\\n').pop().match(':([0-9]+):')[1]))\n  ]\n})\n\nt('Debug has long async stack trace', async() => {\n  const sql = postgres({ ...options, debug: true })\n\n  return [\n    'watyo',\n    await yo().catch(x => x.stack.match(/wat|yo/g).join(''))\n  ]\n\n  function yo() {\n    return wat()\n  }\n\n  function wat() {\n    return sql`error`\n  }\n})\n\nt('Error contains query string', async() => [\n  'selec 1',\n  (await sql`selec 1`.catch(err => err.query))\n])\n\nt('Error contains query serialized parameters', async() => [\n  1,\n  (await sql`selec ${ 1 }`.catch(err => err.parameters[0]))\n])\n\nt('Error contains query raw parameters', async() => [\n  1,\n  (await sql`selec ${ 1 }`.catch(err => err.args[0]))\n])\n\nt('Query and parameters on errorare not enumerable if debug is not set', async() => {\n  const sql = postgres({ ...options, debug: false })\n\n  return [\n    false,\n    (await sql`selec ${ 1 }`.catch(err => err.propertyIsEnumerable('parameters') || err.propertyIsEnumerable('query')))\n  ]\n})\n\nt('Query and parameters are enumerable if debug is set', async() => {\n  const sql = postgres({ ...options, debug: true })\n\n  return [\n    true,\n    (await sql`selec ${ 1 }`.catch(err => err.propertyIsEnumerable('parameters') && err.propertyIsEnumerable('query')))\n  ]\n})\n\nt('connect_timeout', { timeout: 20 }, async() => {\n  const connect_timeout = 0.2\n  const server = net.createServer()\n  server.listen()\n  const sql = postgres({ port: server.address().port, host: '127.0.0.1', connect_timeout })\n  const start = Date.now()\n  let end\n  await sql`select 1`.catch((e) => {\n    if (e.code !== 'CONNECT_TIMEOUT')\n      throw e\n    end = Date.now()\n  })\n  server.close()\n  return [connect_timeout, Math.floor((end - start) / 100) / 10]\n})\n\nt('connect_timeout throws proper error', async() => [\n  'CONNECT_TIMEOUT',\n  await postgres({\n    ...options,\n    ...login_scram,\n    connect_timeout: 0.001\n  })`select 1`.catch(e => e.code)\n])\n\nt('connect_timeout error message includes host:port', { timeout: 20 }, async() => {\n  const connect_timeout = 0.2\n  const server = net.createServer()\n  server.listen()\n  const sql = postgres({ port: server.address().port, host: '127.0.0.1', connect_timeout })\n  const port = server.address().port\n  let err\n  await sql`select 1`.catch((e) => {\n    if (e.code !== 'CONNECT_TIMEOUT')\n      throw e\n    err = e.message\n  })\n  server.close()\n  return [['write CONNECT_TIMEOUT 127.0.0.1:', port].join(''), err]\n})\n\nt('requests works after single connect_timeout', async() => {\n  let first = true\n\n  const sql = postgres({\n    ...options,\n    ...login_scram,\n    connect_timeout: { valueOf() { return first ? (first = false, 0.0001) : 1 } }\n  })\n\n  return [\n    'CONNECT_TIMEOUT,,1',\n    [\n      await sql`select 1 as x`.then(() => 'success', x => x.code),\n      await delay(10),\n      (await sql`select 1 as x`)[0].x\n    ].join(',')\n  ]\n})\n\nt('Postgres errors are of type PostgresError', async() =>\n  [true, (await sql`bad keyword`.catch(e => e)) instanceof sql.PostgresError]\n)\n\nt('Result has columns spec', async() =>\n  ['x', (await sql`select 1 as x`).columns[0].name]\n)\n\nt('forEach has result as second argument', async() => {\n  let x\n  await sql`select 1 as x`.forEach((_, result) => x = result)\n  return ['x', x.columns[0].name]\n})\n\nt('Result as arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: {\n      row: x => Object.values(x)\n    }\n  })\n\n  return ['1,2', (await sql`select 1 as a, 2 as b`)[0].join(',')]\n})\n\nt('Insert empty array', async() => {\n  await sql`create table tester (ints int[])`\n  return [\n    Array.isArray((await sql`insert into tester (ints) values (${ sql.array([]) }) returning *`)[0].ints),\n    true,\n    await sql`drop table tester`\n  ]\n})\n\nt('Insert array in sql()', async() => {\n  await sql`create table tester (ints int[])`\n  return [\n    Array.isArray((await sql`insert into tester ${ sql({ ints: sql.array([]) }) } returning *`)[0].ints),\n    true,\n    await sql`drop table tester`\n  ]\n})\n\nt('Automatically creates prepared statements', async() => {\n  const sql = postgres(options)\n  const result = await sql`select * from pg_prepared_statements`\n  return [true, result.some(x => x.name = result.statement.name)]\n})\n\nt('no_prepare: true disables prepared statements (deprecated)', async() => {\n  const sql = postgres({ ...options, no_prepare: true })\n  const result = await sql`select * from pg_prepared_statements`\n  return [false, result.some(x => x.name = result.statement.name)]\n})\n\nt('prepare: false disables prepared statements', async() => {\n  const sql = postgres({ ...options, prepare: false })\n  const result = await sql`select * from pg_prepared_statements`\n  return [false, result.some(x => x.name = result.statement.name)]\n})\n\nt('prepare: true enables prepared statements', async() => {\n  const sql = postgres({ ...options, prepare: true })\n  const result = await sql`select * from pg_prepared_statements`\n  return [true, result.some(x => x.name = result.statement.name)]\n})\n\nt('prepares unsafe query when \"prepare\" option is true', async() => {\n  const sql = postgres({ ...options, prepare: true })\n  const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ['bla'], { prepare: true })\n  return [true, result.some(x => x.name = result.statement.name)]\n})\n\nt('does not prepare unsafe query by default', async() => {\n  const sql = postgres({ ...options, prepare: true })\n  const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ['bla'])\n  return [false, result.some(x => x.name = result.statement.name)]\n})\n\nt('Recreate prepared statements on transformAssignedExpr error', { timeout: 1 }, async() => {\n  const insert = () => sql`insert into test (name) values (${ '1' }) returning name`\n  await sql`create table test (name text)`\n  await insert()\n  await sql`alter table test alter column name type int using name::integer`\n  return [\n    1,\n    (await insert())[0].name,\n    await sql`drop table test`\n  ]\n})\n\nt('Throws correct error when retrying in transactions', async() => {\n  await sql`create table test(x int)`\n  const error = await sql.begin(sql => sql`insert into test (x) values (${ false })`).catch(e => e)\n  return [\n    error.code,\n    '42804',\n    sql`drop table test`\n  ]\n})\n\nt('Recreate prepared statements on RevalidateCachedQuery error', async() => {\n  const select = () => sql`select name from test`\n  await sql`create table test (name text)`\n  await sql`insert into test values ('1')`\n  await select()\n  await sql`alter table test alter column name type int using name::integer`\n  return [\n    1,\n    (await select())[0].name,\n    await sql`drop table test`\n  ]\n})\n\nt('Properly throws routine error on not prepared statements', async() => {\n  await sql`create table x (x text[])`\n  const { routine } = await sql.unsafe(`\n    insert into x(x) values (('a', 'b'))\n  `).catch(e => e)\n\n  return ['transformAssignedExpr', routine, await sql`drop table x`]\n})\n\nt('Properly throws routine error on not prepared statements in transaction', async() => {\n  const { routine } = await sql.begin(sql => [\n    sql`create table x (x text[])`,\n    sql`insert into x(x) values (('a', 'b'))`\n  ]).catch(e => e)\n\n  return ['transformAssignedExpr', routine]\n})\n\nt('Properly throws routine error on not prepared statements using file', async() => {\n  const { routine } = await sql.unsafe(`\n    create table x (x text[]);\n    insert into x(x) values (('a', 'b'));\n  `, { prepare: true }).catch(e => e)\n\n  return ['transformAssignedExpr', routine]\n})\n\nt('Catches connection config errors', async() => {\n  const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })\n\n  return [\n    'wat',\n    await sql`select 1`.catch((e) => e.message)\n  ]\n})\n\nt('Catches connection config errors with end', async() => {\n  const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })\n\n  return [\n    'wat',\n    await sql`select 1`.catch((e) => e.message),\n    await sql.end()\n  ]\n})\n\nt('Catches query format errors', async() => [\n  'wat',\n  await sql.unsafe({ toString: () => { throw new Error('wat') } }).catch((e) => e.message)\n])\n\nt('Multiple hosts', {\n  timeout: 1\n}, async() => {\n  const s1 = postgres({ idle_timeout })\n      , s2 = postgres({ idle_timeout, port: 5433 })\n      , sql = postgres('postgres://localhost:5432,localhost:5433', { idle_timeout, max: 1 })\n      , result = []\n\n  const id1 = (await s1`select system_identifier as x from pg_control_system()`)[0].x\n  const id2 = (await s2`select system_identifier as x from pg_control_system()`)[0].x\n\n  const x1 = await sql`select 1`\n  result.push((await sql`select system_identifier as x from pg_control_system()`)[0].x)\n  await s1`select pg_terminate_backend(${ x1.state.pid }::int)`\n  await delay(50)\n\n  const x2 = await sql`select 1`\n  result.push((await sql`select system_identifier as x from pg_control_system()`)[0].x)\n  await s2`select pg_terminate_backend(${ x2.state.pid }::int)`\n  await delay(50)\n\n  result.push((await sql`select system_identifier as x from pg_control_system()`)[0].x)\n\n  return [[id1, id2, id1].join(','), result.join(',')]\n})\n\nt('Escaping supports schemas and tables', async() => {\n  await sql`create schema a`\n  await sql`create table a.b (c int)`\n  await sql`insert into a.b (c) values (1)`\n  return [\n    1,\n    (await sql`select ${ sql('a.b.c') } from a.b`)[0].c,\n    await sql`drop table a.b`,\n    await sql`drop schema a`\n  ]\n})\n\nt('Raw method returns rows as arrays', async() => {\n  const [x] = await sql`select 1`.raw()\n  return [\n    Array.isArray(x),\n    true\n  ]\n})\n\nt('Raw method returns values unparsed as Buffer', async() => {\n  const [[x]] = await sql`select 1`.raw()\n  return [\n    x instanceof Uint8Array,\n    true\n  ]\n})\n\nt('Array returns rows as arrays of columns', async() => {\n  return [(await sql`select 1`.values())[0][0], 1]\n})\n\nt('Copy read', async() => {\n  const result = []\n\n  await sql`create table test (x int)`\n  await sql`insert into test select * from generate_series(1,10)`\n  const readable = await sql`copy test to stdout`.readable()\n  readable.on('data', x => result.push(x))\n  await new Promise(r => readable.on('end', r))\n\n  return [\n    result.length,\n    10,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy write', { timeout: 2 }, async() => {\n  await sql`create table test (x int)`\n  const writable = await sql`copy test from stdin`.writable()\n\n  writable.write('1\\n')\n  writable.write('1\\n')\n  writable.end()\n\n  await new Promise(r => writable.on('finish', r))\n\n  return [\n    (await sql`select 1 from test`).length,\n    2,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy write as first', async() => {\n  await sql`create table test (x int)`\n  const first = postgres(options)\n  const writable = await first`COPY test FROM STDIN WITH(FORMAT csv, HEADER false, DELIMITER ',')`.writable()\n  writable.write('1\\n')\n  writable.write('1\\n')\n  writable.end()\n\n  await new Promise(r => writable.on('finish', r))\n\n  return [\n    (await sql`select 1 from test`).length,\n    2,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy from file', async() => {\n  await sql`create table test (x int, y int, z int)`\n  await new Promise(async r => fs\n    .createReadStream(rel('copy.csv'))\n    .pipe(await sql`copy test from stdin`.writable())\n    .on('finish', r)\n  )\n\n  return [\n    JSON.stringify(await sql`select * from test`),\n    '[{\"x\":1,\"y\":2,\"z\":3},{\"x\":4,\"y\":5,\"z\":6}]',\n    await sql`drop table test`\n  ]\n})\n\nt('Copy from works in transaction', async() => {\n  await sql`create table test(x int)`\n  const xs = await sql.begin(async sql => {\n    (await sql`copy test from stdin`.writable()).end('1\\n2')\n    await delay(20)\n    return sql`select 1 from test`\n  })\n\n  return [\n    xs.length,\n    2,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy from abort', async() => {\n  const sql = postgres(options)\n  const readable = fs.createReadStream(rel('copy.csv'))\n\n  await sql`create table test (x int, y int, z int)`\n  await sql`TRUNCATE TABLE test`\n\n  const writable = await sql`COPY test FROM STDIN`.writable()\n\n  let aborted\n\n  readable\n    .pipe(writable)\n    .on('error', (err) => aborted = err)\n\n  writable.destroy(new Error('abort'))\n  await sql.end()\n\n  return [\n    'abort',\n    aborted.message,\n    await postgres(options)`drop table test`\n  ]\n})\n\nt('multiple queries before connect', async() => {\n  const sql = postgres({ ...options, max: 2 })\n  const xs = await Promise.all([\n    sql`select 1 as x`,\n    sql`select 2 as x`,\n    sql`select 3 as x`,\n    sql`select 4 as x`\n  ])\n\n  return [\n    '1,2,3,4',\n    xs.map(x => x[0].x).join()\n  ]\n})\n\nt('subscribe', { timeout: 2 }, async() => {\n  const sql = postgres({\n    database: 'postgres_js_test',\n    publications: 'alltables'\n  })\n\n  await sql.unsafe('create publication alltables for all tables')\n\n  const result = []\n\n  const { unsubscribe } = await sql.subscribe('*', (row, { command, old }) => {\n    result.push(command, row.name, row.id, old && old.name, old && old.id)\n  })\n\n  await sql`\n    create table test (\n      id serial primary key,\n      name text\n    )\n  `\n\n  await sql`alter table test replica identity default`\n  await sql`insert into test (name) values ('Murray')`\n  await sql`update test set name = 'Rothbard'`\n  await sql`update test set id = 2`\n  await sql`delete from test`\n  await sql`alter table test replica identity full`\n  await sql`insert into test (name) values ('Murray')`\n  await sql`update test set name = 'Rothbard'`\n  await sql`delete from test`\n  await delay(10)\n  await unsubscribe()\n  await sql`insert into test (name) values ('Oh noes')`\n  await delay(10)\n  return [\n    'insert,Murray,1,,,update,Rothbard,1,,,update,Rothbard,2,,1,delete,,2,,,insert,Murray,2,,,update,Rothbard,2,Murray,2,delete,Rothbard,2,,', // eslint-disable-line\n    result.join(','),\n    await sql`drop table test`,\n    await sql`drop publication alltables`,\n    await sql.end()\n  ]\n})\n\nt('subscribe with transform', { timeout: 2 }, async() => {\n  const sql = postgres({\n    transform: {\n      column: {\n        from: postgres.toCamel,\n        to: postgres.fromCamel\n      }\n    },\n    database: 'postgres_js_test',\n    publications: 'alltables'\n  })\n\n  await sql.unsafe('create publication alltables for all tables')\n\n  const result = []\n\n  const { unsubscribe } = await sql.subscribe('*', (row, { command, old }) =>\n    result.push(command, row.nameInCamel || row.id, old && old.nameInCamel)\n  )\n\n  await sql`\n    create table test (\n      id serial primary key,\n      name_in_camel text\n    )\n  `\n\n  await sql`insert into test (name_in_camel) values ('Murray')`\n  await sql`update test set name_in_camel = 'Rothbard'`\n  await sql`delete from test`\n  await sql`alter table test replica identity full`\n  await sql`insert into test (name_in_camel) values ('Murray')`\n  await sql`update test set name_in_camel = 'Rothbard'`\n  await sql`delete from test`\n  await delay(10)\n  await unsubscribe()\n  await sql`insert into test (name_in_camel) values ('Oh noes')`\n  await delay(10)\n  return [\n    'insert,Murray,,update,Rothbard,,delete,1,,insert,Murray,,update,Rothbard,Murray,delete,Rothbard,',\n    result.join(','),\n    await sql`drop table test`,\n    await sql`drop publication alltables`,\n    await sql.end()\n  ]\n})\n\nt('subscribe reconnects and calls onsubscribe', { timeout: 4 }, async() => {\n  const sql = postgres({\n    database: 'postgres_js_test',\n    publications: 'alltables',\n    fetch_types: false\n  })\n\n  await sql.unsafe('create publication alltables for all tables')\n\n  const result = []\n  let onsubscribes = 0\n\n  const { unsubscribe, sql: subscribeSql } = await sql.subscribe(\n    '*',\n    (row, { command, old }) => result.push(command, row.name || row.id, old && old.name),\n    () => onsubscribes++\n  )\n\n  await sql`\n    create table test (\n      id serial primary key,\n      name text\n    )\n  `\n\n  await sql`insert into test (name) values ('Murray')`\n  await delay(10)\n  await subscribeSql.close()\n  await delay(500)\n  await sql`delete from test`\n  await delay(100)\n  await unsubscribe()\n  return [\n    '2insert,Murray,,delete,1,',\n    onsubscribes + result.join(','),\n    await sql`drop table test`,\n    await sql`drop publication alltables`,\n    await sql.end()\n  ]\n})\n\nt('Execute', async() => {\n  const result = await new Promise((resolve) => {\n    const sql = postgres({ ...options, fetch_types: false, debug:(id, query) => resolve(query) })\n    sql`select 1`.execute()\n  })\n\n  return [result, 'select 1']\n})\n\nt('Cancel running query', async() => {\n  const query = sql`select pg_sleep(2)`\n  setTimeout(() => query.cancel(), 500)\n  const error = await query.catch(x => x)\n  return ['57014', error.code]\n})\n\nt('Cancel piped query', { timeout: 5 }, async() => {\n  await sql`select 1`\n  const last = sql`select pg_sleep(1)`.execute()\n  const query = sql`select pg_sleep(2) as dig`\n  setTimeout(() => query.cancel(), 500)\n  const error = await query.catch(x => x)\n  await last\n  return ['57014', error.code]\n})\n\nt('Cancel queued query', async() => {\n  const query = sql`select pg_sleep(2) as nej`\n  const tx = sql.begin(sql => (\n    query.cancel(),\n    sql`select pg_sleep(0.5) as hej, 'hejsa'`\n  ))\n  const error = await query.catch(x => x)\n  await tx\n  return ['57014', error.code]\n})\n\nt('Fragments', async() => [\n  1,\n  (await sql`\n    ${ sql`select` } 1 as x\n  `)[0].x\n])\n\nt('Result becomes array', async() => [\n  true,\n  (await sql`select 1`).slice() instanceof Array\n])\n\nt('Describe', async() => {\n  const type = (await sql`select ${ 1 }::int as x`.describe()).types[0]\n  return [23, type]\n})\n\nt('Describe a statement', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`select name, age from tester where name like $1 and age > $2`.describe()\n  return [\n    '25,23/name:25,age:23',\n    `${ r.types.join(',') }/${ r.columns.map(c => `${c.name}:${c.type}`).join(',') }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Include table oid and column number in column details', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`select name, age from tester where name like $1 and age > $2`.describe()\n  const [{ oid }] = await sql`select oid from pg_class where relname = 'tester'`\n\n  return [\n    `table:${oid},number:1|table:${oid},number:2`,\n    `${ r.columns.map(c => `table:${c.table},number:${c.number}`).join('|') }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Describe a statement without parameters', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`select name, age from tester`.describe()\n  return [\n    '0,2',\n    `${ r.types.length },${ r.columns.length }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Describe a statement without columns', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`insert into tester (name, age) values ($1, $2)`.describe()\n  return [\n    '2,0',\n    `${ r.types.length },${ r.columns.length }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Large object', async() => {\n  const file = rel('index.js')\n      , md5 = crypto.createHash('md5').update(fs.readFileSync(file)).digest('hex')\n\n  const lo = await sql.largeObject()\n  await new Promise(async r => fs.createReadStream(file).pipe(await lo.writable()).on('finish', r))\n  await lo.seek(0)\n\n  const out = crypto.createHash('md5')\n  await new Promise(r => lo.readable().then(x => x.on('data', x => out.update(x)).on('end', r)))\n\n  return [\n    md5,\n    out.digest('hex'),\n    await lo.close()\n  ]\n})\n\nt('Catches type serialize errors', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: x => x,\n        serialize: () => { throw new Error('watSerialize') }\n      }\n    }\n  })\n\n  return [\n    'watSerialize',\n    (await sql`select ${ 'wat' }`.catch(e => e.message))\n  ]\n})\n\nt('Catches type parse errors', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: () => { throw new Error('watParse') },\n        serialize: x => x\n      }\n    }\n  })\n\n  return [\n    'watParse',\n    (await sql`select 'wat'`.catch(e => e.message))\n  ]\n})\n\nt('Catches type serialize errors in transactions', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: x => x,\n        serialize: () => { throw new Error('watSerialize') }\n      }\n    }\n  })\n\n  return [\n    'watSerialize',\n    (await sql.begin(sql => (\n      sql`select 1`,\n      sql`select ${ 'wat' }`\n    )).catch(e => e.message))\n  ]\n})\n\nt('Catches type parse errors in transactions', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: () => { throw new Error('watParse') },\n        serialize: x => x\n      }\n    }\n  })\n\n  return [\n    'watParse',\n    (await sql.begin(sql => (\n      sql`select 1`,\n      sql`select 'wat'`\n    )).catch(e => e.message))\n  ]\n})\n\nt('Prevent premature end of connection in transaction', async() => {\n  const sql = postgres({ max_lifetime: 0.01, idle_timeout })\n  const result = await sql.begin(async sql => {\n    await sql`select 1`\n    await delay(20)\n    await sql`select 1`\n    return 'yay'\n  })\n\n\n  return [\n    'yay',\n    result\n  ]\n})\n\nt('Ensure reconnect after max_lifetime with transactions', { timeout: 5 }, async() => {\n  const sql = postgres({\n    max_lifetime: 0.01,\n    idle_timeout,\n    max: 1\n  })\n\n  let x = 0\n  while (x++ < 10) await sql.begin(sql => sql`select 1 as x`)\n\n  return [true, true]\n})\n\n\nt('Ensure transactions throw if connection is closed dwhile there is no query', async() => {\n  const sql = postgres(options)\n  const x = await sql.begin(async() => {\n    setTimeout(() => sql.end({ timeout: 0 }), 10)\n    await new Promise(r => setTimeout(r, 200))\n    return sql`select 1`\n  }).catch(x => x)\n  return ['CONNECTION_CLOSED', x.code]\n})\n\nt('Custom socket', {}, async() => {\n  let result\n  const sql = postgres({\n    socket: () => new Promise((resolve, reject) => {\n      const socket = new net.Socket()\n      socket.connect(5432)\n      socket.once('data', x => result = x[0])\n      socket.on('error', reject)\n      socket.on('connect', () => resolve(socket))\n    }),\n    idle_timeout\n  })\n\n  await sql`select 1`\n\n  return [\n    result,\n    82\n  ]\n})\n\nt('Ensure drain only dequeues if ready', async() => {\n  const sql = postgres(options)\n\n  const res = await Promise.all([\n    sql.unsafe('SELECT 0+$1 --' + '.'.repeat(100000), [1]),\n    sql.unsafe('SELECT 0+$1+$2+$3', [1, 2, 3])\n  ])\n\n  return [res.length, 2]\n})\n\nt('Supports fragments as dynamic parameters', async() => {\n  await sql`create table test (a int, b bool)`\n  await sql`insert into test values(1, true)`\n  await sql`insert into test ${\n    sql({\n      a: 2,\n      b: sql`exists(select 1 from test where b = ${ true })`\n    })\n  }`\n\n  return [\n    '1,t2,t',\n    (await sql`select * from test`.raw()).join(''),\n    await sql`drop table test`\n  ]\n})\n\nt('Supports nested fragments with parameters', async() => {\n  await sql`create table test ${\n    sql`(${ sql('a') } ${ sql`int` })`\n  }`\n  await sql`insert into test values(1)`\n  return [\n    1,\n    (await sql`select a from test`)[0].a,\n    await sql`drop table test`\n  ]\n})\n\nt('Supports multiple nested fragments with parameters', async() => {\n  const [{ b }] = await sql`select * ${\n    sql`from ${\n      sql`(values (2, ${ 1 }::int)) as x(${ sql(['a', 'b']) })`\n    }`\n  }`\n  return [\n    1,\n    b\n  ]\n})\n\nt('Supports arrays of fragments', async() => {\n  const [{ x }] = await sql`\n    ${ [sql`select`, sql`1`, sql`as`, sql`x`] }\n  `\n\n  return [\n    1,\n    x\n  ]\n})\n\nt('Does not try rollback when commit errors', async() => {\n  let notice = null\n  const sql = postgres({ ...options, onnotice: x => notice = x })\n  await sql`create table test(x int constraint test_constraint unique deferrable initially deferred)`\n\n  await sql.begin('isolation level serializable', async sql => {\n    await sql`insert into test values(1)`\n    await sql`insert into test values(1)`\n  }).catch(e => e)\n\n  return [\n    notice,\n    null,\n    await sql`drop table test`\n  ]\n})\n\nt('Last keyword used even with duplicate keywords', async() => {\n  await sql`create table test (x int)`\n  await sql`insert into test values(1)`\n  const [{ x }] = await sql`\n    select\n      1 in (1) as x\n    from test\n    where x in ${ sql([1, 2]) }\n  `\n\n  return [x, true, await sql`drop table test`]\n})\n\nt('Insert array with null', async() => {\n  await sql`create table test (x int[])`\n  await sql`insert into test ${ sql({ x: [1, null, 3] }) }`\n  return [\n    1,\n    (await sql`select x from test`)[0].x[0],\n    await sql`drop table test`\n  ]\n})\n\nt('Insert array with undefined throws', async() => {\n  await sql`create table test (x int[])`\n  return [\n    'UNDEFINED_VALUE',\n    await sql`insert into test ${ sql({ x: [1, undefined, 3] }) }`.catch(e => e.code),\n    await sql`drop table test`\n  ]\n})\n\nt('Insert array with undefined transform', async() => {\n  const sql = postgres({ ...options, transform: { undefined: null } })\n  await sql`create table test (x int[])`\n  await sql`insert into test ${ sql({ x: [1, undefined, 3] }) }`\n  return [\n    1,\n    (await sql`select x from test`)[0].x[0],\n    await sql`drop table test`\n  ]\n})\n\nt('concurrent cursors', async() => {\n  const xs = []\n\n  await Promise.all([...Array(7)].map((x, i) => [\n    sql`select ${ i }::int as a, generate_series(1, 2) as x`.cursor(([x]) => xs.push(x.a + x.x))\n  ]).flat())\n\n  return ['12233445566778', xs.join('')]\n})\n\nt('concurrent cursors multiple connections', async() => {\n  const sql = postgres({ ...options, max: 2 })\n  const xs = []\n\n  await Promise.all([...Array(7)].map((x, i) => [\n    sql`select ${ i }::int as a, generate_series(1, 2) as x`.cursor(([x]) => xs.push(x.a + x.x))\n  ]).flat())\n\n  return ['12233445566778', xs.sort().join('')]\n})\n\nt('reserve connection', async() => {\n  const reserved = await sql.reserve()\n\n  setTimeout(() => reserved.release(), 510)\n\n  const xs = await Promise.all([\n    reserved`select 1 as x`.then(([{ x }]) => ({ time: Date.now(), x })),\n    sql`select 2 as x`.then(([{ x }]) => ({ time: Date.now(), x })),\n    reserved`select 3 as x`.then(([{ x }]) => ({ time: Date.now(), x }))\n  ])\n\n  if (xs[1].time - xs[2].time < 500)\n    throw new Error('Wrong time')\n\n  return [\n    '123',\n    xs.map(x => x.x).join('')\n  ]\n})\n\nt('arrays in reserved connection', async() => {\n  const reserved = await sql.reserve()\n  const [{ x }] = await reserved`select array[1, 2, 3] as x`\n  reserved.release()\n\n  return [\n    '123',\n    x.join('')\n  ]\n})\n\nt('Ensure reserve on query throws proper error', async() => {\n  const sql = postgres({ idle_timeout }) // eslint-disable-line\n  const reserved = await sql.reserve()\n  const [{ x }] = await reserved`select 'wat' as x`\n\n  return [\n    'wat', x, reserved.release()\n  ]\n})\n\nt('query during copy error', async() => {\n  const sql = postgres(options) // eslint-disable-line\n  await sql`create table test (id serial primary key, name text)`\n  const copy = await sql`copy test from stdin`.writable()\n  const error = await sql`select 1`.catch(e => e)\n  await copy.end()\n\n  return [\n    'COPY_IN_PROGRESS', error.code,\n    await sql`drop table test`\n  ]\n})\n\n;globalThis.addEventListener(\"unload\", () => Deno.exit(process.exitCode))"
  },
  {
    "path": "deno/tests/pg_hba.conf",
    "content": "local   all                 all                                     trust\nhost    all                 postgres                samehost        trust\nhost    postgres_js_test    postgres_js_test        samehost        trust\nhost    postgres_js_test    postgres_js_test_md5    samehost        md5\nhost    postgres_js_test    postgres_js_test_scram  samehost        scram-sha-256\n"
  },
  {
    "path": "deno/tests/select-param.sql",
    "content": "select $1 as x\n"
  },
  {
    "path": "deno/tests/select.sql",
    "content": "select 1 as x\n"
  },
  {
    "path": "deno/tests/test.js",
    "content": "import process from 'https://deno.land/std@0.132.0/node/process.ts'\n/* eslint no-console: 0 */\n\nimport util from 'https://deno.land/std@0.132.0/node/util.ts'\n\nlet done = 0\nlet only = false\nlet ignored = 0\nlet failed = false\nlet promise = Promise.resolve()\nconst tests = {}\n    , ignore = {}\n\nexport const nt = () => ignored++\nexport const ot = (...rest) => (only = true, test(true, ...rest))\nexport const t = (...rest) => test(false, ...rest)\nt.timeout = 5\n\nasync function test(o, name, options, fn) {\n  typeof options !== 'object' && (fn = options, options = {})\n  const line = new Error().stack.split('\\n')[3].match(':([0-9]+):')[1]\n\n  await 1\n\n  if (only && !o)\n    return\n\n  tests[line] = { fn, line, name }\n  promise = promise.then(() => Promise.race([\n    new Promise((resolve, reject) =>\n      fn.timer = setTimeout(() => reject('Timed out'), (options.timeout || t.timeout) * 1000)\n    ),\n    failed\n      ? (ignored++, ignore)\n      : fn()\n  ]))\n    .then(async x => {\n      clearTimeout(fn.timer)\n      if (x === ignore)\n        return\n\n      if (!Array.isArray(x))\n        throw new Error('Test should return result array')\n\n      const [expected, got] = await Promise.all(x)\n      if (expected !== got) {\n        failed = true\n        throw new Error(util.inspect(expected) + ' != ' + util.inspect(got))\n      }\n\n      tests[line].succeeded = true\n      process.stdout.write('✅')\n    })\n    .catch(err => {\n      tests[line].failed = failed = true\n      tests[line].error = err instanceof Error ? err : new Error(util.inspect(err))\n    })\n    .then(() => {\n      ++done === Object.keys(tests).length && exit()\n    })\n}\n\nfunction exit() {\n  let success = true\n  Object.values(tests).every((x) => {\n    if (x.succeeded)\n      return true\n\n    success = false\n    x.cleanup\n      ? console.error('⛔️', x.name + ' at line', x.line, 'cleanup failed', '\\n', util.inspect(x.cleanup))\n      : console.error('⛔️', x.name + ' at line', x.line, x.failed\n        ? 'failed'\n        : 'never finished', x.error ? '\\n' + util.inspect(x.error) : ''\n      )\n  })\n\n  only\n    ? console.error('⚠️', 'Not all tests were run')\n    : ignored\n      ? console.error('⚠️', ignored, 'ignored test' + (ignored === 1 ? '' : 's', '\\n'))\n      : success\n        ? console.log('🎉')\n        : console.error('⚠️', 'Not good')\n\n  !process.exitCode && (!success || only || ignored) && (process.exitCode = 1)\n}\n\n"
  },
  {
    "path": "deno/types/index.d.ts",
    "content": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nimport process from 'https://deno.land/std@0.132.0/node/process.ts'\nimport { Readable, Writable } from 'https://deno.land/std@0.132.0/node/stream.ts'\n\n/**\n * Establish a connection to a PostgreSQL server.\n * @param options Connection options - default to the same as psql\n * @returns An utility function to make queries to the server\n */\ndeclare function postgres<T extends Record<string, postgres.PostgresType> = {}>(options?: postgres.Options<T> | undefined): postgres.Sql<Record<string, postgres.PostgresType> extends T ? {} : { [type in keyof T]: T[type] extends {\n  serialize: (value: infer R) => any,\n  parse: (raw: any) => infer R\n} ? R : never }>\n/**\n * Establish a connection to a PostgreSQL server.\n * @param url Connection string used for authentication\n * @param options Connection options - default to the same as psql\n * @returns An utility function to make queries to the server\n */\ndeclare function postgres<T extends Record<string, postgres.PostgresType> = {}>(url: string, options?: postgres.Options<T> | undefined): postgres.Sql<Record<string, postgres.PostgresType> extends T ? {} : { [type in keyof T]: T[type] extends {\n  serialize: (value: infer R) => any,\n  parse: (raw: any) => infer R\n} ? R : never }>\n\n/**\n * Connection options of Postgres.\n */\ninterface BaseOptions<T extends Record<string, postgres.PostgresType>> {\n  /** Postgres ip address[s] or domain name[s] */\n  host: string | string[] | undefined;\n  /** Postgres server[s] port[s] */\n  port: number | number[] | undefined;\n  /** unix socket path (usually '/tmp') */\n  path: string | undefined;\n  /**\n   * Name of database to connect to\n   * @default process.env['PGDATABASE'] || options.user\n   */\n  database: string;\n  /**\n   * Username of database user\n   * @default process.env['PGUSERNAME'] || process.env['PGUSER'] || require('os').userInfo().username\n   */\n  user: string;\n  /**\n   * How to deal with ssl (can be a tls.connect option object)\n   * @default false\n  */\n  ssl: 'require' | 'allow' | 'prefer' | 'verify-full' | boolean | object;\n  /**\n   * Max number of connections\n   * @default 10\n   */\n  max: number;\n  /**\n   * Idle connection timeout in seconds\n   * @default process.env['PGIDLE_TIMEOUT']\n   */\n  idle_timeout: number | undefined;\n  /**\n   * Connect timeout in seconds\n   * @default process.env['PGCONNECT_TIMEOUT']\n   */\n  connect_timeout: number;\n  /** Array of custom types; see more in the README */\n  types: T;\n  /**\n   * Enables prepare mode.\n   * @default true\n   */\n  prepare: boolean;\n  /**\n   * Called when a notice is received\n   * @default console.log\n   */\n  onnotice: (notice: postgres.Notice) => void;\n  /** (key; value) when a server param change */\n  onparameter: (key: string, value: any) => void;\n  /** Is called with (connection; query; parameters) */\n  debug: boolean | ((connection: number, query: string, parameters: any[], paramTypes: any[]) => void);\n  /** Transform hooks */\n  transform: {\n    /** Transforms outcoming undefined values */\n    undefined?: any\n\n    /** Transforms incoming and outgoing column names */\n    column?: ((column: string) => string) | {\n      /** Transform function for column names in result rows */\n      from?: ((column: string) => string) | undefined;\n      /** Transform function for column names in interpolated values passed to tagged template literal */\n      to?: ((column: string) => string) | undefined;\n    } | undefined;\n    /** Transforms incoming and outgoing row values */\n    value?: ((value: any) => any) | {\n      /** Transform function for values in result rows */\n      from?: ((value: unknown, column: postgres.Column<string>) => any) | undefined;\n      // to?: ((value: unknown) => any) | undefined; // unused\n    } | undefined;\n    /** Transforms entire rows */\n    row?: ((row: postgres.Row) => any) | {\n      /** Transform function for entire result rows */\n      from?: ((row: postgres.Row) => any) | undefined;\n      // to?: ((row: postgres.Row) => any) | undefined; // unused\n    } | undefined;\n  };\n  /** Connection parameters */\n  connection: Partial<postgres.ConnectionParameters>;\n  /**\n   * Use 'read-write' with multiple hosts to ensure only connecting to primary\n   * @default process.env['PGTARGETSESSIONATTRS']\n   */\n  target_session_attrs: undefined | 'read-write' | 'read-only' | 'primary' | 'standby' | 'prefer-standby';\n  /**\n   * Automatically fetches types on connect\n   * @default true\n   */\n  fetch_types: boolean;\n  /**\n   * Publications to subscribe to (only relevant when calling `sql.subscribe()`)\n   * @default 'alltables'\n   */\n  publications: string\n  onclose: (connId: number) => void;\n  backoff: boolean | ((attemptNum: number) => number);\n  max_lifetime: number | null;\n  keep_alive: number | null;\n}\n\n\ndeclare const PRIVATE: unique symbol;\n\ndeclare class NotAPromise {\n  private [PRIVATE]: never; // prevent user-side interface implementation\n\n  /**\n   * @deprecated This object isn't an SQL query, and therefore not a Promise; use the tagged template string syntax instead: ```await sql\\`...\\`;```\n   * @throws NOT_TAGGED_CALL\n   */\n  private then(): never;\n  /**\n   * @deprecated This object isn't an SQL query, and therefore not a Promise; use the tagged template string syntax instead: ```await sql\\`...\\`;```\n   * @throws NOT_TAGGED_CALL\n   */\n  private catch(): never;\n  /**\n   * @deprecated This object isn't an SQL query, and therefore not a Promise; use the tagged template string syntax instead: ```await sql\\`...\\`;```\n   * @throws NOT_TAGGED_CALL\n   */\n  private finally(): never;\n}\n\ntype UnwrapPromiseArray<T> = T extends any[] ? {\n  [k in keyof T]: T[k] extends Promise<infer R> ? R : T[k]\n} : T;\n\ntype Keys = string\n\ntype SerializableObject<T, K extends readonly any[], TT> =\n  number extends K['length'] ? {} :\n  Partial<(Record<Keys & (keyof T) & (K['length'] extends 0 ? string : K[number]), postgres.ParameterOrJSON<TT> | undefined> & Record<string, any>)>\n\ntype First<T, K extends readonly any[], TT> =\n  // Tagged template string call\n  T extends TemplateStringsArray ? TemplateStringsArray :\n  // Identifiers helper\n  T extends string ? string :\n  // Dynamic values helper (depth 2)\n  T extends readonly any[][] ? readonly postgres.EscapableArray[] :\n  // Insert/update helper (depth 2)\n  T extends readonly (object & infer R)[] ? (R extends postgres.SerializableParameter<TT> ? readonly postgres.SerializableParameter<TT>[] : readonly SerializableObject<R, K, TT>[]) :\n  // Dynamic values/ANY helper (depth 1)\n  T extends readonly any[] ? (readonly postgres.SerializableParameter<TT>[]) :\n  // Insert/update helper (depth 1)\n  T extends object ? SerializableObject<T, K, TT> :\n  // Unexpected type\n  never\n\ntype Rest<T> =\n  T extends TemplateStringsArray ? never : // force fallback to the tagged template function overload\n  T extends string ? readonly string[] :\n  T extends readonly any[][] ? readonly [] :\n  T extends readonly (object & infer R)[] ? (\n    readonly (Keys & keyof R)[]   // sql(data, \"prop\", \"prop2\") syntax\n    |\n    [readonly (Keys & keyof R)[]] // sql(data, [\"prop\", \"prop2\"]) syntax\n  ) :\n  T extends readonly any[] ? readonly [] :\n  T extends object ? (\n    readonly (Keys & keyof T)[]   // sql(data, \"prop\", \"prop2\") syntax\n    |\n    [readonly (Keys & keyof T)[]] // sql(data, [\"prop\", \"prop2\"]) syntax\n  ) :\n  any\n\ntype Return<T, K extends readonly any[]> =\n  [T] extends [TemplateStringsArray] ?\n  [unknown] extends [T] ? postgres.Helper<T, K> : // ensure no `PendingQuery` with `any` types\n  [TemplateStringsArray] extends [T] ? postgres.PendingQuery<postgres.Row[]> :\n  postgres.Helper<T, K> :\n  postgres.Helper<T, K>\n\ndeclare namespace postgres {\n  class PostgresError extends Error {\n    name: 'PostgresError';\n    severity_local: string;\n    severity: string;\n    code: string;\n    position: string;\n    file: string;\n    line: string;\n    routine: string;\n\n    detail?: string | undefined;\n    hint?: string | undefined;\n    internal_position?: string | undefined;\n    internal_query?: string | undefined;\n    where?: string | undefined;\n    schema_name?: string | undefined;\n    table_name?: string | undefined;\n    column_name?: string | undefined;\n    data?: string | undefined;\n    type_name?: string | undefined;\n    constraint_name?: string | undefined;\n\n    /** Only set when debug is enabled */\n    query: string;\n    /** Only set when debug is enabled */\n    parameters: any[];\n  }\n\n  /**\n   * Convert a snake_case string to PascalCase.\n   * @param str The string from snake_case to convert\n   * @returns The new string in PascalCase\n   */\n  function toPascal(str: string): string;\n  namespace toPascal {\n    namespace column { function from(str: string): string; }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a PascalCase string to snake_case.\n   * @param str The string from snake_case to convert\n   * @returns The new string in snake_case\n   */\n  function fromPascal(str: string): string;\n  namespace fromPascal {\n    namespace column { function to(str: string): string }\n  }\n  /**\n   * Convert snake_case to and from PascalCase.\n   */\n   namespace pascal {\n    namespace column {\n      function from(str: string): string;\n      function to(str: string): string;\n    }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a snake_case string to camelCase.\n   * @param str The string from snake_case to convert\n   * @returns The new string in camelCase\n   */\n  function toCamel(str: string): string;\n  namespace toCamel {\n    namespace column { function from(str: string): string; }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a camelCase string to snake_case.\n   * @param str The string from snake_case to convert\n   * @returns The new string in snake_case\n   */\n  function fromCamel(str: string): string;\n  namespace fromCamel {\n    namespace column { function to(str: string): string }\n  }\n  /**\n   * Convert snake_case to and from camelCase.\n   */\n  namespace camel {\n    namespace column {\n      function from(str: string): string;\n      function to(str: string): string;\n    }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a snake_case string to kebab-case.\n   * @param str The string from snake_case to convert\n   * @returns The new string in kebab-case\n   */\n  function toKebab(str: string): string;\n  namespace toKebab {\n    namespace column { function from(str: string): string; }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a kebab-case string to snake_case.\n   * @param str The string from snake_case to convert\n   * @returns The new string in snake_case\n   */\n  function fromKebab(str: string): string;\n  namespace fromKebab {\n    namespace column { function to(str: string): string }\n  }\n  /**\n   * Convert snake_case to and from kebab-case.\n   */\n  namespace kebab {\n    namespace column {\n      function from(str: string): string;\n      function to(str: string): string;\n    }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n\n  const BigInt: PostgresType<bigint>;\n\n  interface PostgresType<T = any> {\n    to: number;\n    from: number[];\n    serialize: (value: T) => unknown;\n    parse: (raw: any) => T;\n  }\n\n  interface ConnectionParameters {\n    /**\n     * Default application_name\n     * @default 'postgres.js'\n     */\n    application_name: string;\n    default_transaction_isolation: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable',\n    default_transaction_read_only: boolean,\n    default_transaction_deferrable: boolean,\n    statement_timeout: number,\n    lock_timeout: number,\n    idle_in_transaction_session_timeout: number,\n    idle_session_timeout: number,\n    DateStyle: string,\n    IntervalStyle: string,\n    TimeZone: string,\n    /** Other connection parameters */\n    [name: string]: string | number | boolean;\n  }\n\n  interface Options<T extends Record<string, postgres.PostgresType>> extends Partial<BaseOptions<T>> {\n    /** @inheritdoc */\n    host?: string | undefined;\n    /** @inheritdoc */\n    port?: number | undefined;\n    /** @inheritdoc */\n    path?: string | undefined;\n    /** Password of database user (an alias for `password`) */\n    pass?: Options<T>['password'] | undefined;\n    /**\n     * Password of database user\n     * @default process.env['PGPASSWORD']\n     */\n    password?: string | (() => string | Promise<string>) | undefined;\n    /** Name of database to connect to (an alias for `database`) */\n    db?: Options<T>['database'] | undefined;\n    /** Username of database user (an alias for `user`) */\n    username?: Options<T>['user'] | undefined;\n    /** Postgres ip address or domain name (an alias for `host`) */\n    hostname?: Options<T>['host'] | undefined;\n    /**\n     * Disable prepared mode\n     * @deprecated use \"prepare\" option instead\n     */\n    no_prepare?: boolean | undefined;\n    /**\n     * Idle connection timeout in seconds\n     * @deprecated use \"idle_timeout\" option instead\n     */\n    timeout?: Options<T>['idle_timeout'] | undefined;\n  }\n\n  interface ParsedOptions<T extends Record<string, unknown> = {}> extends BaseOptions<{ [name in keyof T]: PostgresType<T[name]> }> {\n    /** @inheritdoc */\n    host: string[];\n    /** @inheritdoc */\n    port: number[];\n    /** @inheritdoc */\n    pass: null;\n    /** @inheritdoc */\n    transform: Transform;\n    serializers: Record<number, (value: any) => unknown>;\n    parsers: Record<number, (value: any) => unknown>;\n  }\n\n  interface Transform {\n    /** Transforms outcoming undefined values */\n    undefined: any\n\n    column: {\n      /** Transform function for column names in result rows */\n      from: ((column: string) => string) | undefined;\n      /** Transform function for column names in interpolated values passed to tagged template literal */\n      to: ((column: string) => string) | undefined;\n    };\n    value: {\n      /** Transform function for values in result rows */\n      from: ((value: any, column?: Column<string>) => any) | undefined;\n      /** Transform function for interpolated values passed to tagged template literal */\n      to: undefined; // (value: any) => any\n    };\n    row: {\n      /** Transform function for entire result rows */\n      from: ((row: postgres.Row) => any) | undefined;\n      to: undefined; // (row: postgres.Row) => any\n    };\n  }\n\n  interface Notice {\n    [field: string]: string;\n  }\n\n  interface Parameter<T = SerializableParameter> extends NotAPromise {\n    /**\n     * PostgreSQL OID of the type\n     */\n    type: number;\n    /**\n     * Serialized value\n     */\n    value: string | null;\n    /**\n     * Raw value to serialize\n     */\n    raw: T | null;\n  }\n\n  interface ArrayParameter<T extends readonly any[] = readonly any[]> extends Parameter<T | T[]> {\n    array: true;\n  }\n\n  interface ConnectionError extends globalThis.Error {\n    code:\n    | 'CONNECTION_DESTROYED'\n    | 'CONNECT_TIMEOUT'\n    | 'CONNECTION_CLOSED'\n    | 'CONNECTION_ENDED';\n    errno: this['code'];\n    address: string;\n    port?: number | undefined;\n  }\n\n  interface NotSupportedError extends globalThis.Error {\n    code: 'MESSAGE_NOT_SUPPORTED';\n    name: string;\n  }\n\n  interface GenericError extends globalThis.Error {\n    code:\n    | '57014' // canceling statement due to user request\n    | 'NOT_TAGGED_CALL'\n    | 'UNDEFINED_VALUE'\n    | 'MAX_PARAMETERS_EXCEEDED'\n    | 'SASL_SIGNATURE_MISMATCH'\n    | 'UNSAFE_TRANSACTION';\n    message: string;\n  }\n\n  interface AuthNotImplementedError extends globalThis.Error {\n    code: 'AUTH_TYPE_NOT_IMPLEMENTED';\n    type: number | string;\n    message: string;\n  }\n\n  type Error = never\n    | PostgresError\n    | ConnectionError\n    | NotSupportedError\n    | GenericError\n    | AuthNotImplementedError;\n\n  interface ColumnInfo {\n    key: number;\n    name: string;\n    type: number;\n    parser?(raw: string): unknown;\n    atttypmod: number;\n  }\n\n  interface RelationInfo {\n    schema: string;\n    table: string;\n    columns: ColumnInfo[];\n    keys: ColumnInfo[];\n  }\n\n  type ReplicationEvent =\n    | { command: 'insert', relation: RelationInfo }\n    | { command: 'delete', relation: RelationInfo, key: boolean }\n    | { command: 'update', relation: RelationInfo, key: boolean, old: Row | null };\n\n  interface SubscriptionHandle {\n    unsubscribe(): void;\n  }\n\n  interface LargeObject {\n    writable(options?: {\n      highWaterMark?: number | undefined,\n      start?: number | undefined\n    } | undefined): Promise<Writable>;\n    readable(options?: {\n      highWaterMark?: number | undefined,\n      start?: number | undefined,\n      end?: number | undefined\n    } | undefined): Promise<Readable>;\n\n    close(): Promise<void>;\n    tell(): Promise<void>;\n    read(size: number): Promise<void>;\n    write(buffer: Uint8Array): Promise<[{ data: Uint8Array }]>;\n    truncate(size: number): Promise<void>;\n    seek(offset: number, whence?: number | undefined): Promise<void>;\n    size(): Promise<[{ position: bigint, size: bigint }]>;\n  }\n\n  type EscapableArray = (string | number)[]\n\n  type Serializable = never\n    | null\n    | boolean\n    | number\n    | string\n    | Date\n    | Uint8Array;\n\n  type SerializableParameter<T = never> = never\n    | T\n    | Serializable\n    | Helper<any>\n    | Parameter<any>\n    | ArrayParameter\n    | readonly SerializableParameter<T>[];\n\n  type JSONValue = // using a dedicated type to detect symbols, bigints, and other non serializable types\n    | null\n    | string\n    | number\n    | boolean\n    | Date // serialized as `string`\n    | readonly JSONValue[]\n    | { toJSON(): any } // `toJSON` called by `JSON.stringify`; not typing the return type, types definition is strict enough anyway\n    | {\n      readonly [prop: string | number]:\n      | undefined\n      | JSONValue\n      | ((...args: any) => any) // serialized as `undefined`\n    };\n\n  interface Row {\n    [column: string]: any;\n  }\n\n  type MaybeRow = Row | undefined;\n\n  interface Column<T extends string> {\n    name: T;\n    type: number;\n    table: number;\n    number: number;\n    parser?: ((raw: string) => unknown) | undefined;\n  }\n\n  type ColumnList<T> = (T extends string ? Column<T> : never)[];\n\n  interface State {\n    status: string;\n    pid: number;\n    secret: number;\n  }\n\n  interface Statement {\n    /** statement unique name */\n    name: string;\n    /** sql query */\n    string: string;\n    /** parameters types */\n    types: number[];\n    columns: ColumnList<string>;\n  }\n\n  interface ResultMeta<T extends number | null> {\n    count: T; // For tuples\n    command: string;\n    statement: Statement;\n    state: State;\n  }\n\n  interface ResultQueryMeta<T extends number | null, U> extends ResultMeta<T> {\n    columns: ColumnList<U>;\n  }\n\n  type ExecutionResult<T> = [] & ResultQueryMeta<number, keyof NonNullable<T>>;\n  type ValuesRowList<T extends readonly any[]> = T[number][keyof T[number]][][] & ResultQueryMeta<T['length'], keyof T[number]>;\n  type RawRowList<T extends readonly any[]> = Buffer[][] & Iterable<Buffer[][]> & ResultQueryMeta<T['length'], keyof T[number]>;\n  type RowList<T extends readonly any[]> = T & Iterable<NonNullable<T[number]>> & ResultQueryMeta<T['length'], keyof T[number]>;\n\n  interface PendingQueryModifiers<TRow extends readonly any[]> {\n    simple(): this;\n    readable(): Promise<Readable>;\n    writable(): Promise<Writable>;\n\n    execute(): this;\n    cancel(): void;\n\n    /**\n     * @deprecated `.stream` has been renamed to `.forEach`\n     * @throws\n     */\n    stream(cb: (row: NonNullable<TRow[number]>, result: ExecutionResult<TRow[number]>) => void): never;\n    forEach(cb: (row: NonNullable<TRow[number]>, result: ExecutionResult<TRow[number]>) => void): Promise<ExecutionResult<TRow[number]>>;\n\n    cursor(rows?: number | undefined): AsyncIterable<NonNullable<TRow[number]>[]>;\n    cursor(cb: (row: [NonNullable<TRow[number]>]) => void): Promise<ExecutionResult<TRow[number]>>;\n    cursor(rows: number, cb: (rows: NonNullable<TRow[number]>[]) => void): Promise<ExecutionResult<TRow[number]>>;\n  }\n\n  interface PendingDescribeQuery extends Promise<Statement> {\n  }\n\n  interface PendingValuesQuery<TRow extends readonly MaybeRow[]> extends Promise<ValuesRowList<TRow>>, PendingQueryModifiers<TRow[number][keyof TRow[number]][][]> {\n    describe(): PendingDescribeQuery;\n  }\n\n  interface PendingRawQuery<TRow extends readonly MaybeRow[]> extends Promise<RawRowList<TRow>>, PendingQueryModifiers<Buffer[][]> {\n  }\n\n  interface PendingQuery<TRow extends readonly MaybeRow[]> extends Promise<RowList<TRow>>, PendingQueryModifiers<TRow> {\n    describe(): PendingDescribeQuery;\n    values(): PendingValuesQuery<TRow>;\n    raw(): PendingRawQuery<TRow>;\n  }\n\n  interface PendingRequest extends Promise<[] & ResultMeta<null>> { }\n\n  interface ListenRequest extends Promise<ListenMeta> { }\n  interface ListenMeta extends ResultMeta<null> {\n    unlisten(): Promise<void>\n  }\n\n  interface Helper<T, U extends readonly any[] = T[]> extends NotAPromise {\n    first: T;\n    rest: U;\n  }\n\n  type Fragment = PendingQuery<any>\n\n  type ParameterOrJSON<T> =\n  | SerializableParameter<T>\n  | JSONValue\n\n  type ParameterOrFragment<T> =\n  | SerializableParameter<T>\n  | Fragment\n  | Fragment[]\n\n  interface Sql<TTypes extends Record<string, unknown> = {}> {\n    /**\n     * Query helper\n     * @param first Define how the helper behave\n     * @param rest Other optional arguments, depending on the helper type\n     * @returns An helper object usable as tagged template parameter in sql queries\n     */\n    <T, K extends Rest<T>>(first: T & First<T, K, TTypes[keyof TTypes]>, ...rest: K): Return<T, K>;\n\n    /**\n     * Execute the SQL query passed as a template string. Can only be used as template string tag.\n     * @param template The template generated from the template string\n     * @param parameters Interpoled values of the template string\n     * @returns A promise resolving to the result of your query\n     */\n    <T extends readonly (object | undefined)[] = Row[]>(template: TemplateStringsArray, ...parameters: readonly (ParameterOrFragment<TTypes[keyof TTypes]>)[]): PendingQuery<T>;\n\n    CLOSE: {};\n    END: this['CLOSE'];\n    PostgresError: typeof PostgresError;\n\n    options: ParsedOptions<TTypes>;\n    parameters: ConnectionParameters;\n    types: this['typed'];\n    typed: (<T>(value: T, oid: number) => Parameter<T>) & {\n      [name in keyof TTypes]: (value: TTypes[name]) => postgres.Parameter<TTypes[name]>\n    };\n\n    unsafe<T extends any[] = (Row & Iterable<Row>)[]>(query: string, parameters?: (ParameterOrJSON<TTypes[keyof TTypes]>)[] | undefined, queryOptions?: UnsafeQueryOptions | undefined): PendingQuery<T>;\n    end(options?: { timeout?: number | undefined } | undefined): Promise<void>;\n\n    listen(channel: string, onnotify: (value: string) => void, onlisten?: (() => void) | undefined): ListenRequest;\n    notify(channel: string, payload: string): PendingRequest;\n\n    subscribe(event: string, cb: (row: Row | null, info: ReplicationEvent) => void, onsubscribe?: (() => void), onerror?: (() => any)): Promise<SubscriptionHandle>;\n\n    largeObject(oid?: number | undefined, /** @default 0x00020000 | 0x00040000 */ mode?: number | undefined): Promise<LargeObject>;\n\n    begin<T>(cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;\n    begin<T>(options: string, cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;\n\n    array<T extends SerializableParameter<TTypes[keyof TTypes]>[] = SerializableParameter<TTypes[keyof TTypes]>[]>(value: T, type?: number | undefined): ArrayParameter<T>;\n    file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, options?: { cache?: boolean | undefined } | undefined): PendingQuery<T>;\n    file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, args: (ParameterOrJSON<TTypes[keyof TTypes]>)[], options?: { cache?: boolean | undefined } | undefined): PendingQuery<T>;\n    json(value: JSONValue): Parameter;\n\n    reserve(): Promise<ReservedSql<TTypes>>\n  }\n\n  interface UnsafeQueryOptions {\n    /**\n     * When executes query as prepared statement.\n     * @default false\n     */\n    prepare?: boolean | undefined;\n  }\n\n  interface TransactionSql<TTypes extends Record<string, unknown> = {}> extends Omit<Sql<TTypes>,\n      'parameters' |\n      'largeObject' |\n      'subscribe' |\n      'CLOSE' |\n      'END' |\n      'PostgresError' |\n      'options' |\n      'reserve' |\n      'listen' |\n      'begin' |\n      'close' |\n      'end'\n  > {\n    savepoint<T>(cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;\n    savepoint<T>(name: string, cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;\n\n    prepare<T>(name: string): Promise<UnwrapPromiseArray<T>>;\n  }\n\n  interface ReservedSql<TTypes extends Record<string, unknown> = {}> extends Sql<TTypes> {\n    release(): void;\n  }\n}\n\nexport = postgres;\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"postgres\",\n  \"version\": \"3.4.8\",\n  \"description\": \"Fastest full featured PostgreSQL client for Node.js\",\n  \"type\": \"module\",\n  \"module\": \"src/index.js\",\n  \"main\": \"cjs/src/index.js\",\n  \"exports\": {\n    \"types\": \"./types/index.d.ts\",\n    \"bun\": \"./src/index.js\",\n    \"workerd\": \"./cf/src/index.js\",\n    \"import\": \"./src/index.js\",\n    \"default\": \"./cjs/src/index.js\"\n  },\n  \"types\": \"types/index.d.ts\",\n  \"typings\": \"types/index.d.ts\",\n  \"engines\": {\n    \"node\": \">=12\"\n  },\n  \"scripts\": {\n    \"build\": \"npm run build:cjs && npm run build:deno && npm run build:cf\",\n    \"build:cjs\": \"node transpile.cjs\",\n    \"build:deno\": \"node transpile.deno.js\",\n    \"build:cf\": \"node transpile.cf.js\",\n    \"test\": \"npm run test:esm && npm run test:cjs && npm run test:deno\",\n    \"test:esm\": \"node tests/index.js\",\n    \"test:cjs\": \"npm run build:cjs && cd cjs/tests && node index.js && cd ../../\",\n    \"test:deno\": \"npm run build:deno && cd deno/tests && deno run --no-lock --allow-all --unsafely-ignore-certificate-errors index.js && cd ../../\",\n    \"lint\": \"eslint src && eslint tests\",\n    \"prepare\": \"npm run build\",\n    \"prepublishOnly\": \"npm run lint\"\n  },\n  \"files\": [\n    \"/cf/src\",\n    \"/cf/polyfills.js\",\n    \"/cjs/src\",\n    \"/cjs/package.json\",\n    \"/src\",\n    \"/types\"\n  ],\n  \"author\": \"Rasmus Porsager <rasmus@porsager.com> (https://www.porsager.com)\",\n  \"funding\": {\n    \"type\": \"individual\",\n    \"url\": \"https://github.com/sponsors/porsager\"\n  },\n  \"license\": \"Unlicense\",\n  \"repository\": \"porsager/postgres\",\n  \"homepage\": \"https://github.com/porsager/postgres\",\n  \"bugs\": \"https://github.com/porsager/postgres/issues\",\n  \"keywords\": [\n    \"driver\",\n    \"postgresql\",\n    \"postgres.js\",\n    \"postgres\",\n    \"postrges\",\n    \"postgre\",\n    \"client\",\n    \"sql\",\n    \"db\",\n    \"pg\",\n    \"database\"\n  ]\n}\n"
  },
  {
    "path": "src/bytes.js",
    "content": "const size = 256\nlet buffer = Buffer.allocUnsafe(size)\n\nconst messages = 'BCcDdEFfHPpQSX'.split('').reduce((acc, x) => {\n  const v = x.charCodeAt(0)\n  acc[x] = () => {\n    buffer[0] = v\n    b.i = 5\n    return b\n  }\n  return acc\n}, {})\n\nconst b = Object.assign(reset, messages, {\n  N: String.fromCharCode(0),\n  i: 0,\n  inc(x) {\n    b.i += x\n    return b\n  },\n  str(x) {\n    const length = Buffer.byteLength(x)\n    fit(length)\n    b.i += buffer.write(x, b.i, length, 'utf8')\n    return b\n  },\n  i16(x) {\n    fit(2)\n    buffer.writeUInt16BE(x, b.i)\n    b.i += 2\n    return b\n  },\n  i32(x, i) {\n    if (i || i === 0) {\n      buffer.writeUInt32BE(x, i)\n      return b\n    }\n    fit(4)\n    buffer.writeUInt32BE(x, b.i)\n    b.i += 4\n    return b\n  },\n  z(x) {\n    fit(x)\n    buffer.fill(0, b.i, b.i + x)\n    b.i += x\n    return b\n  },\n  raw(x) {\n    buffer = Buffer.concat([buffer.subarray(0, b.i), x])\n    b.i = buffer.length\n    return b\n  },\n  end(at = 1) {\n    buffer.writeUInt32BE(b.i - at, at)\n    const out = buffer.subarray(0, b.i)\n    b.i = 0\n    buffer = Buffer.allocUnsafe(size)\n    return out\n  }\n})\n\nexport default b\n\nfunction fit(x) {\n  if (buffer.length - b.i < x) {\n    const prev = buffer\n        , length = prev.length\n\n    buffer = Buffer.allocUnsafe(length + (length >> 1) + x)\n    prev.copy(buffer)\n  }\n}\n\nfunction reset() {\n  b.i = 0\n  return b\n}\n"
  },
  {
    "path": "src/connection.js",
    "content": "import net from 'net'\nimport tls from 'tls'\nimport crypto from 'crypto'\nimport Stream from 'stream'\nimport { performance } from 'perf_hooks'\n\nimport { stringify, handleValue, arrayParser, arraySerializer } from './types.js'\nimport { Errors } from './errors.js'\nimport Result from './result.js'\nimport Queue from './queue.js'\nimport { Query, CLOSE } from './query.js'\nimport b from './bytes.js'\n\nexport default Connection\n\nlet uid = 1\n\nconst Sync = b().S().end()\n    , Flush = b().H().end()\n    , SSLRequest = b().i32(8).i32(80877103).end(8)\n    , ExecuteUnnamed = Buffer.concat([b().E().str(b.N).i32(0).end(), Sync])\n    , DescribeUnnamed = b().D().str('S').str(b.N).end()\n    , noop = () => { /* noop */ }\n\nconst retryRoutines = new Set([\n  'FetchPreparedStatement',\n  'RevalidateCachedQuery',\n  'transformAssignedExpr'\n])\n\nconst errorFields = {\n  83  : 'severity_local',    // S\n  86  : 'severity',          // V\n  67  : 'code',              // C\n  77  : 'message',           // M\n  68  : 'detail',            // D\n  72  : 'hint',              // H\n  80  : 'position',          // P\n  112 : 'internal_position', // p\n  113 : 'internal_query',    // q\n  87  : 'where',             // W\n  115 : 'schema_name',       // s\n  116 : 'table_name',        // t\n  99  : 'column_name',       // c\n  100 : 'data type_name',    // d\n  110 : 'constraint_name',   // n\n  70  : 'file',              // F\n  76  : 'line',              // L\n  82  : 'routine'            // R\n}\n\nfunction Connection(options, queues = {}, { onopen = noop, onend = noop, onclose = noop } = {}) {\n  const {\n    sslnegotiation,\n    ssl,\n    max,\n    user,\n    host,\n    port,\n    database,\n    parsers,\n    transform,\n    onnotice,\n    onnotify,\n    onparameter,\n    max_pipeline,\n    keep_alive,\n    backoff,\n    target_session_attrs\n  } = options\n\n  const sent = Queue()\n      , id = uid++\n      , backend = { pid: null, secret: null }\n      , idleTimer = timer(end, options.idle_timeout)\n      , lifeTimer = timer(end, options.max_lifetime)\n      , connectTimer = timer(connectTimedOut, options.connect_timeout)\n\n  let socket = null\n    , cancelMessage\n    , errorResponse = null\n    , result = new Result()\n    , incoming = Buffer.alloc(0)\n    , needsTypes = options.fetch_types\n    , backendParameters = {}\n    , statements = {}\n    , statementId = Math.random().toString(36).slice(2)\n    , statementCount = 1\n    , closedTime = 0\n    , remaining = 0\n    , hostIndex = 0\n    , retries = 0\n    , length = 0\n    , delay = 0\n    , rows = 0\n    , serverSignature = null\n    , nextWriteTimer = null\n    , terminated = false\n    , incomings = null\n    , results = null\n    , initial = null\n    , ending = null\n    , stream = null\n    , chunk = null\n    , ended = null\n    , nonce = null\n    , query = null\n    , final = null\n\n  const connection = {\n    queue: queues.closed,\n    idleTimer,\n    connect(query) {\n      initial = query\n      reconnect()\n    },\n    terminate,\n    execute,\n    cancel,\n    end,\n    count: 0,\n    id\n  }\n\n  queues.closed && queues.closed.push(connection)\n\n  return connection\n\n  async function createSocket() {\n    let x\n    try {\n      x = options.socket\n        ? (await Promise.resolve(options.socket(options)))\n        : new net.Socket()\n    } catch (e) {\n      error(e)\n      return\n    }\n    x.on('error', error)\n    x.on('close', closed)\n    x.on('drain', drain)\n    return x\n  }\n\n  async function cancel({ pid, secret }, resolve, reject) {\n    try {\n      cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)\n      await connect()\n      socket.once('error', reject)\n      socket.once('close', resolve)\n    } catch (error) {\n      reject(error)\n    }\n  }\n\n  function execute(q) {\n    if (terminated)\n      return queryError(q, Errors.connection('CONNECTION_DESTROYED', options))\n\n    if (stream)\n      return queryError(q, Errors.generic('COPY_IN_PROGRESS', 'You cannot execute queries during copy'))\n\n    if (q.cancelled)\n      return\n\n    try {\n      q.state = backend\n      query\n        ? sent.push(q)\n        : (query = q, query.active = true)\n\n      build(q)\n      return write(toBuffer(q))\n        && !q.describeFirst\n        && !q.cursorFn\n        && sent.length < max_pipeline\n        && (!q.options.onexecute || q.options.onexecute(connection))\n    } catch (error) {\n      sent.length === 0 && write(Sync)\n      errored(error)\n      return true\n    }\n  }\n\n  function toBuffer(q) {\n    if (q.parameters.length >= 65534)\n      throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')\n\n    return q.options.simple\n      ? b().Q().str(q.statement.string + b.N).end()\n      : q.describeFirst\n        ? Buffer.concat([describe(q), Flush])\n        : q.prepare\n          ? q.prepared\n            ? prepared(q)\n            : Buffer.concat([describe(q), prepared(q)])\n          : unnamed(q)\n  }\n\n  function describe(q) {\n    return Buffer.concat([\n      Parse(q.statement.string, q.parameters, q.statement.types, q.statement.name),\n      Describe('S', q.statement.name)\n    ])\n  }\n\n  function prepared(q) {\n    return Buffer.concat([\n      Bind(q.parameters, q.statement.types, q.statement.name, q.cursorName),\n      q.cursorFn\n        ? Execute('', q.cursorRows)\n        : ExecuteUnnamed\n    ])\n  }\n\n  function unnamed(q) {\n    return Buffer.concat([\n      Parse(q.statement.string, q.parameters, q.statement.types),\n      DescribeUnnamed,\n      prepared(q)\n    ])\n  }\n\n  function build(q) {\n    const parameters = []\n        , types = []\n\n    const string = stringify(q, q.strings[0], q.args[0], parameters, types, options)\n\n    !q.tagged && q.args.forEach(x => handleValue(x, parameters, types, options))\n\n    q.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)\n    q.string = string\n    q.signature = q.prepare && types + string\n    q.onlyDescribe && (delete statements[q.signature])\n    q.parameters = q.parameters || parameters\n    q.prepared = q.prepare && q.signature in statements\n    q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared)\n    q.statement = q.prepared\n      ? statements[q.signature]\n      : { string, types, name: q.prepare ? statementId + statementCount++ : '' }\n\n    typeof options.debug === 'function' && options.debug(id, string, parameters, types)\n  }\n\n  function write(x, fn) {\n    chunk = chunk ? Buffer.concat([chunk, x]) : Buffer.from(x)\n    if (fn || chunk.length >= 1024)\n      return nextWrite(fn)\n    nextWriteTimer === null && (nextWriteTimer = setImmediate(nextWrite))\n    return true\n  }\n\n  function nextWrite(fn) {\n    const x = socket.write(chunk, fn)\n    nextWriteTimer !== null && clearImmediate(nextWriteTimer)\n    chunk = nextWriteTimer = null\n    return x\n  }\n\n  function connectTimedOut() {\n    errored(Errors.connection('CONNECT_TIMEOUT', options, socket))\n    socket.destroy()\n  }\n\n  async function secure() {\n    if (sslnegotiation !== 'direct') {\n      write(SSLRequest)\n      const canSSL = await new Promise(r => socket.once('data', x => r(x[0] === 83))) // S\n\n      if (!canSSL && ssl === 'prefer')\n        return connected()\n    }\n\n    const options = {\n      socket,\n      servername: net.isIP(socket.host) ? undefined : socket.host\n    }\n\n    if (sslnegotiation === 'direct')\n      options.ALPNProtocols = ['postgresql']\n\n    if (ssl === 'require' || ssl === 'allow' || ssl === 'prefer')\n      options.rejectUnauthorized = false\n    else if (typeof ssl === 'object')\n      Object.assign(options, ssl)\n\n    socket.removeAllListeners()\n    socket = tls.connect(options)\n    socket.on('secureConnect', connected)\n    socket.on('error', error)\n    socket.on('close', closed)\n    socket.on('drain', drain)\n  }\n\n  /* c8 ignore next 3 */\n  function drain() {\n    !query && onopen(connection)\n  }\n\n  function data(x) {\n    if (incomings) {\n      incomings.push(x)\n      remaining -= x.length\n      if (remaining > 0)\n        return\n    }\n\n    incoming = incomings\n      ? Buffer.concat(incomings, length - remaining)\n      : incoming.length === 0\n        ? x\n        : Buffer.concat([incoming, x], incoming.length + x.length)\n\n    while (incoming.length > 4) {\n      length = incoming.readUInt32BE(1)\n      if (length >= incoming.length) {\n        remaining = length - incoming.length\n        incomings = [incoming]\n        break\n      }\n\n      try {\n        handle(incoming.subarray(0, length + 1))\n      } catch (e) {\n        query && (query.cursorFn || query.describeFirst) && write(Sync)\n        errored(e)\n      }\n      incoming = incoming.subarray(length + 1)\n      remaining = 0\n      incomings = null\n    }\n  }\n\n  async function connect() {\n    terminated = false\n    backendParameters = {}\n    socket || (socket = await createSocket())\n\n    if (!socket)\n      return\n\n    connectTimer.start()\n\n    if (options.socket)\n      return ssl ? secure() : connected()\n\n    socket.on('connect', ssl ? secure : connected)\n\n    if (options.path)\n      return socket.connect(options.path)\n\n    socket.ssl = ssl\n    socket.connect(port[hostIndex], host[hostIndex])\n    socket.host = host[hostIndex]\n    socket.port = port[hostIndex]\n\n    hostIndex = (hostIndex + 1) % port.length\n  }\n\n  function reconnect() {\n    setTimeout(connect, closedTime ? Math.max(0, closedTime + delay - performance.now()) : 0)\n  }\n\n  function connected() {\n    try {\n      statements = {}\n      needsTypes = options.fetch_types\n      statementId = Math.random().toString(36).slice(2)\n      statementCount = 1\n      lifeTimer.start()\n      socket.on('data', data)\n      keep_alive && socket.setKeepAlive && socket.setKeepAlive(true, 1000 * keep_alive)\n      const s = StartupMessage()\n      write(s)\n    } catch (err) {\n      error(err)\n    }\n  }\n\n  function error(err) {\n    if (connection.queue === queues.connecting && options.host[retries + 1])\n      return\n\n    errored(err)\n    while (sent.length)\n      queryError(sent.shift(), err)\n  }\n\n  function errored(err) {\n    stream && (stream.destroy(err), stream = null)\n    query && queryError(query, err)\n    initial && (queryError(initial, err), initial = null)\n  }\n\n  function queryError(query, err) {\n    if (query.reserve)\n      return query.reject(err)\n\n    if (!err || typeof err !== 'object')\n      err = new Error(err)\n\n    'query' in err || 'parameters' in err || Object.defineProperties(err, {\n      stack: { value: err.stack + query.origin.replace(/.*\\n/, '\\n'), enumerable: options.debug },\n      query: { value: query.string, enumerable: options.debug },\n      parameters: { value: query.parameters, enumerable: options.debug },\n      args: { value: query.args, enumerable: options.debug },\n      types: { value: query.statement && query.statement.types, enumerable: options.debug }\n    })\n    query.reject(err)\n  }\n\n  function end() {\n    return ending || (\n      !connection.reserved && onend(connection),\n      !connection.reserved && !initial && !query && sent.length === 0\n        ? (terminate(), new Promise(r => socket && socket.readyState !== 'closed' ? socket.once('close', r) : r()))\n        : ending = new Promise(r => ended = r)\n    )\n  }\n\n  function terminate() {\n    terminated = true\n    if (stream || query || initial || sent.length)\n      error(Errors.connection('CONNECTION_DESTROYED', options))\n\n    clearImmediate(nextWriteTimer)\n    if (socket) {\n      socket.removeListener('data', data)\n      socket.removeListener('connect', connected)\n      socket.readyState === 'open' && socket.end(b().X().end())\n    }\n    ended && (ended(), ending = ended = null)\n  }\n\n  async function closed(hadError) {\n    incoming = Buffer.alloc(0)\n    remaining = 0\n    incomings = null\n    clearImmediate(nextWriteTimer)\n    socket.removeListener('data', data)\n    socket.removeListener('connect', connected)\n    idleTimer.cancel()\n    lifeTimer.cancel()\n    connectTimer.cancel()\n\n    socket.removeAllListeners()\n    socket = null\n\n    if (initial)\n      return reconnect()\n\n    !hadError && (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket))\n    closedTime = performance.now()\n    hadError && options.shared.retries++\n    delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000\n    onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket))\n  }\n\n  /* Handlers */\n  function handle(xs, x = xs[0]) {\n    (\n      x === 68 ? DataRow :                   // D\n      x === 100 ? CopyData :                 // d\n      x === 65 ? NotificationResponse :      // A\n      x === 83 ? ParameterStatus :           // S\n      x === 90 ? ReadyForQuery :             // Z\n      x === 67 ? CommandComplete :           // C\n      x === 50 ? BindComplete :              // 2\n      x === 49 ? ParseComplete :             // 1\n      x === 116 ? ParameterDescription :     // t\n      x === 84 ? RowDescription :            // T\n      x === 82 ? Authentication :            // R\n      x === 110 ? NoData :                   // n\n      x === 75 ? BackendKeyData :            // K\n      x === 69 ? ErrorResponse :             // E\n      x === 115 ? PortalSuspended :          // s\n      x === 51 ? CloseComplete :             // 3\n      x === 71 ? CopyInResponse :            // G\n      x === 78 ? NoticeResponse :            // N\n      x === 72 ? CopyOutResponse :           // H\n      x === 99 ? CopyDone :                  // c\n      x === 73 ? EmptyQueryResponse :        // I\n      x === 86 ? FunctionCallResponse :      // V\n      x === 118 ? NegotiateProtocolVersion : // v\n      x === 87 ? CopyBothResponse :          // W\n      /* c8 ignore next */\n      UnknownMessage\n    )(xs)\n  }\n\n  function DataRow(x) {\n    let index = 7\n    let length\n    let column\n    let value\n\n    const row = query.isRaw ? new Array(query.statement.columns.length) : {}\n    for (let i = 0; i < query.statement.columns.length; i++) {\n      column = query.statement.columns[i]\n      length = x.readInt32BE(index)\n      index += 4\n\n      value = length === -1\n        ? null\n        : query.isRaw === true\n          ? x.subarray(index, index += length)\n          : column.parser === undefined\n            ? x.toString('utf8', index, index += length)\n            : column.parser.array === true\n              ? column.parser(x.toString('utf8', index + 1, index += length))\n              : column.parser(x.toString('utf8', index, index += length))\n\n      query.isRaw\n        ? (row[i] = query.isRaw === true\n          ? value\n          : transform.value.from ? transform.value.from(value, column) : value)\n        : (row[column.name] = transform.value.from ? transform.value.from(value, column) : value)\n    }\n\n    query.forEachFn\n      ? query.forEachFn(transform.row.from ? transform.row.from(row) : row, result)\n      : (result[rows++] = transform.row.from ? transform.row.from(row) : row)\n  }\n\n  function ParameterStatus(x) {\n    const [k, v] = x.toString('utf8', 5, x.length - 1).split(b.N)\n    backendParameters[k] = v\n    if (options.parameters[k] !== v) {\n      options.parameters[k] = v\n      onparameter && onparameter(k, v)\n    }\n  }\n\n  function ReadyForQuery(x) {\n    if (query) {\n      if (errorResponse) {\n        query.retried\n          ? errored(query.retried)\n          : query.prepared && retryRoutines.has(errorResponse.routine)\n            ? retry(query, errorResponse)\n            : errored(errorResponse)\n      } else {\n        query.resolve(results || result)\n      }\n    } else if (errorResponse) {\n      errored(errorResponse)\n    }\n\n    query = results = errorResponse = null\n    result = new Result()\n    connectTimer.cancel()\n\n    if (initial) {\n      if (target_session_attrs) {\n        if (!backendParameters.in_hot_standby || !backendParameters.default_transaction_read_only)\n          return fetchState()\n        else if (tryNext(target_session_attrs, backendParameters))\n          return terminate()\n      }\n\n      if (needsTypes) {\n        initial.reserve && (initial = null)\n        return fetchArrayTypes()\n      }\n\n      initial && !initial.reserve && execute(initial)\n      options.shared.retries = retries = 0\n      initial = null\n      return\n    }\n\n    while (sent.length && (query = sent.shift()) && (query.active = true, query.cancelled))\n      Connection(options).cancel(query.state, query.cancelled.resolve, query.cancelled.reject)\n\n    if (query)\n      return // Consider opening if able and sent.length < 50\n\n    connection.reserved\n      ? !connection.reserved.release && x[5] === 73 // I\n        ? ending\n          ? terminate()\n          : (connection.reserved = null, onopen(connection))\n        : connection.reserved()\n      : ending\n        ? terminate()\n        : onopen(connection)\n  }\n\n  function CommandComplete(x) {\n    rows = 0\n\n    for (let i = x.length - 1; i > 0; i--) {\n      if (x[i] === 32 && x[i + 1] < 58 && result.count === null)\n        result.count = +x.toString('utf8', i + 1, x.length - 1)\n      if (x[i - 1] >= 65) {\n        result.command = x.toString('utf8', 5, i)\n        result.state = backend\n        break\n      }\n    }\n\n    final && (final(), final = null)\n\n    if (result.command === 'BEGIN' && max !== 1 && !connection.reserved)\n      return errored(Errors.generic('UNSAFE_TRANSACTION', 'Only use sql.begin, sql.reserved or max: 1'))\n\n    if (query.options.simple)\n      return BindComplete()\n\n    if (query.cursorFn) {\n      result.count && query.cursorFn(result)\n      write(Sync)\n    }\n  }\n\n  function ParseComplete() {\n    query.parsing = false\n  }\n\n  function BindComplete() {\n    !result.statement && (result.statement = query.statement)\n    result.columns = query.statement.columns\n  }\n\n  function ParameterDescription(x) {\n    const length = x.readUInt16BE(5)\n\n    for (let i = 0; i < length; ++i)\n      !query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))\n\n    query.prepare && (statements[query.signature] = query.statement)\n    query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false)\n  }\n\n  function RowDescription(x) {\n    if (result.command) {\n      results = results || [result]\n      results.push(result = new Result())\n      result.count = null\n      query.statement.columns = null\n    }\n\n    const length = x.readUInt16BE(5)\n    let index = 7\n    let start\n\n    query.statement.columns = Array(length)\n\n    for (let i = 0; i < length; ++i) {\n      start = index\n      while (x[index++] !== 0);\n      const table = x.readUInt32BE(index)\n      const number = x.readUInt16BE(index + 4)\n      const type = x.readUInt32BE(index + 6)\n      query.statement.columns[i] = {\n        name: transform.column.from\n          ? transform.column.from(x.toString('utf8', start, index - 1))\n          : x.toString('utf8', start, index - 1),\n        parser: parsers[type],\n        table,\n        number,\n        type\n      }\n      index += 18\n    }\n\n    result.statement = query.statement\n    if (query.onlyDescribe)\n      return (query.resolve(query.statement), write(Sync))\n  }\n\n  async function Authentication(x, type = x.readUInt32BE(5)) {\n    (\n      type === 3 ? AuthenticationCleartextPassword :\n      type === 5 ? AuthenticationMD5Password :\n      type === 10 ? SASL :\n      type === 11 ? SASLContinue :\n      type === 12 ? SASLFinal :\n      type !== 0 ? UnknownAuth :\n      noop\n    )(x, type)\n  }\n\n  /* c8 ignore next 5 */\n  async function AuthenticationCleartextPassword() {\n    const payload = await Pass()\n    write(\n      b().p().str(payload).z(1).end()\n    )\n  }\n\n  async function AuthenticationMD5Password(x) {\n    const payload = 'md5' + (\n      await md5(\n        Buffer.concat([\n          Buffer.from(await md5((await Pass()) + user)),\n          x.subarray(9)\n        ])\n      )\n    )\n    write(\n      b().p().str(payload).z(1).end()\n    )\n  }\n\n  async function SASL() {\n    nonce = (await crypto.randomBytes(18)).toString('base64')\n    b().p().str('SCRAM-SHA-256' + b.N)\n    const i = b.i\n    write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end())\n  }\n\n  async function SASLContinue(x) {\n    const res = x.toString('utf8', 9).split(',').reduce((acc, x) => (acc[x[0]] = x.slice(2), acc), {})\n\n    const saltedPassword = await crypto.pbkdf2Sync(\n      await Pass(),\n      Buffer.from(res.s, 'base64'),\n      parseInt(res.i), 32,\n      'sha256'\n    )\n\n    const clientKey = await hmac(saltedPassword, 'Client Key')\n\n    const auth = 'n=*,r=' + nonce + ','\n               + 'r=' + res.r + ',s=' + res.s + ',i=' + res.i\n               + ',c=biws,r=' + res.r\n\n    serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64')\n\n    const payload = 'c=biws,r=' + res.r + ',p=' + xor(\n      clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))\n    ).toString('base64')\n\n    write(\n      b().p().str(payload).end()\n    )\n  }\n\n  function SASLFinal(x) {\n    if (x.toString('utf8', 9).split(b.N, 1)[0].slice(2) === serverSignature)\n      return\n    /* c8 ignore next 5 */\n    errored(Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server did not return the correct signature'))\n    socket.destroy()\n  }\n\n  function Pass() {\n    return Promise.resolve(typeof options.pass === 'function'\n      ? options.pass()\n      : options.pass\n    )\n  }\n\n  function NoData() {\n    result.statement = query.statement\n    result.statement.columns = []\n    if (query.onlyDescribe)\n      return (query.resolve(query.statement), write(Sync))\n  }\n\n  function BackendKeyData(x) {\n    backend.pid = x.readUInt32BE(5)\n    backend.secret = x.readUInt32BE(9)\n  }\n\n  async function fetchArrayTypes() {\n    needsTypes = false\n    const types = await new Query([`\n      select b.oid, b.typarray\n      from pg_catalog.pg_type a\n      left join pg_catalog.pg_type b on b.oid = a.typelem\n      where a.typcategory = 'A'\n      group by b.oid, b.typarray\n      order by b.oid\n    `], [], execute)\n    types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))\n  }\n\n  function addArrayType(oid, typarray) {\n    if (!!options.parsers[typarray] && !!options.serializers[typarray]) return\n    const parser = options.parsers[oid]\n    options.shared.typeArrayMap[oid] = typarray\n    options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)\n    options.parsers[typarray].array = true\n    options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)\n  }\n\n  function tryNext(x, xs) {\n    return (\n      (x === 'read-write' && xs.default_transaction_read_only === 'on') ||\n      (x === 'read-only' && xs.default_transaction_read_only === 'off') ||\n      (x === 'primary' && xs.in_hot_standby === 'on') ||\n      (x === 'standby' && xs.in_hot_standby === 'off') ||\n      (x === 'prefer-standby' && xs.in_hot_standby === 'off' && options.host[retries])\n    )\n  }\n\n  function fetchState() {\n    const query = new Query([`\n      show transaction_read_only;\n      select pg_catalog.pg_is_in_recovery()\n    `], [], execute, null, { simple: true })\n    query.resolve = ([[a], [b]]) => {\n      backendParameters.default_transaction_read_only = a.transaction_read_only\n      backendParameters.in_hot_standby = b.pg_is_in_recovery ? 'on' : 'off'\n    }\n    query.execute()\n  }\n\n  function ErrorResponse(x) {\n    if (query) {\n      (query.cursorFn || query.describeFirst) && write(Sync)\n      errorResponse = Errors.postgres(parseError(x))\n    } else {\n      errored(Errors.postgres(parseError(x)))\n    }\n  }\n\n  function retry(q, error) {\n    delete statements[q.signature]\n    q.retried = error\n    execute(q)\n  }\n\n  function NotificationResponse(x) {\n    if (!onnotify)\n      return\n\n    let index = 9\n    while (x[index++] !== 0);\n    onnotify(\n      x.toString('utf8', 9, index - 1),\n      x.toString('utf8', index, x.length - 1)\n    )\n  }\n\n  async function PortalSuspended() {\n    try {\n      const x = await Promise.resolve(query.cursorFn(result))\n      rows = 0\n      x === CLOSE\n        ? write(Close(query.portal))\n        : (result = new Result(), write(Execute('', query.cursorRows)))\n    } catch (err) {\n      write(Sync)\n      query.reject(err)\n    }\n  }\n\n  function CloseComplete() {\n    result.count && query.cursorFn(result)\n    query.resolve(result)\n  }\n\n  function CopyInResponse() {\n    stream = new Stream.Writable({\n      autoDestroy: true,\n      write(chunk, encoding, callback) {\n        socket.write(b().d().raw(chunk).end(), callback)\n      },\n      destroy(error, callback) {\n        callback(error)\n        socket.write(b().f().str(error + b.N).end())\n        stream = null\n      },\n      final(callback) {\n        socket.write(b().c().end())\n        final = callback\n        stream = null\n      }\n    })\n    query.resolve(stream)\n  }\n\n  function CopyOutResponse() {\n    stream = new Stream.Readable({\n      read() { socket.resume() }\n    })\n    query.resolve(stream)\n  }\n\n  /* c8 ignore next 3 */\n  function CopyBothResponse() {\n    stream = new Stream.Duplex({\n      autoDestroy: true,\n      read() { socket.resume() },\n      /* c8 ignore next 11 */\n      write(chunk, encoding, callback) {\n        socket.write(b().d().raw(chunk).end(), callback)\n      },\n      destroy(error, callback) {\n        callback(error)\n        socket.write(b().f().str(error + b.N).end())\n        stream = null\n      },\n      final(callback) {\n        socket.write(b().c().end())\n        final = callback\n      }\n    })\n    query.resolve(stream)\n  }\n\n  function CopyData(x) {\n    stream && (stream.push(x.subarray(5)) || socket.pause())\n  }\n\n  function CopyDone() {\n    stream && stream.push(null)\n    stream = null\n  }\n\n  function NoticeResponse(x) {\n    onnotice\n      ? onnotice(parseError(x))\n      : console.log(parseError(x)) // eslint-disable-line\n\n  }\n\n  /* c8 ignore next 3 */\n  function EmptyQueryResponse() {\n    /* noop */\n  }\n\n  /* c8 ignore next 3 */\n  function FunctionCallResponse() {\n    errored(Errors.notSupported('FunctionCallResponse'))\n  }\n\n  /* c8 ignore next 3 */\n  function NegotiateProtocolVersion() {\n    errored(Errors.notSupported('NegotiateProtocolVersion'))\n  }\n\n  /* c8 ignore next 3 */\n  function UnknownMessage(x) {\n    console.error('Postgres.js : Unknown Message:', x[0]) // eslint-disable-line\n  }\n\n  /* c8 ignore next 3 */\n  function UnknownAuth(x, type) {\n    console.error('Postgres.js : Unknown Auth:', type) // eslint-disable-line\n  }\n\n  /* Messages */\n  function Bind(parameters, types, statement = '', portal = '') {\n    let prev\n      , type\n\n    b().B().str(portal + b.N).str(statement + b.N).i16(0).i16(parameters.length)\n\n    parameters.forEach((x, i) => {\n      if (x === null)\n        return b.i32(0xFFFFFFFF)\n\n      type = types[i]\n      parameters[i] = x = type in options.serializers\n        ? options.serializers[type](x)\n        : '' + x\n\n      prev = b.i\n      b.inc(4).str(x).i32(b.i - prev - 4, prev)\n    })\n\n    b.i16(0)\n\n    return b.end()\n  }\n\n  function Parse(str, parameters, types, name = '') {\n    b().P().str(name + b.N).str(str + b.N).i16(parameters.length)\n    parameters.forEach((x, i) => b.i32(types[i] || 0))\n    return b.end()\n  }\n\n  function Describe(x, name = '') {\n    return b().D().str(x).str(name + b.N).end()\n  }\n\n  function Execute(portal = '', rows = 0) {\n    return Buffer.concat([\n      b().E().str(portal + b.N).i32(rows).end(),\n      Flush\n    ])\n  }\n\n  function Close(portal = '') {\n    return Buffer.concat([\n      b().C().str('P').str(portal + b.N).end(),\n      b().S().end()\n    ])\n  }\n\n  function StartupMessage() {\n    return cancelMessage || b().inc(4).i16(3).z(2).str(\n      Object.entries(Object.assign({\n        user,\n        database,\n        client_encoding: 'UTF8'\n      },\n        options.connection\n      )).filter(([, v]) => v).map(([k, v]) => k + b.N + v).join(b.N)\n    ).z(2).end(0)\n  }\n\n}\n\nfunction parseError(x) {\n  const error = {}\n  let start = 5\n  for (let i = 5; i < x.length - 1; i++) {\n    if (x[i] === 0) {\n      error[errorFields[x[start]]] = x.toString('utf8', start + 1, i)\n      start = i + 1\n    }\n  }\n  return error\n}\n\nfunction md5(x) {\n  return crypto.createHash('md5').update(x).digest('hex')\n}\n\nfunction hmac(key, x) {\n  return crypto.createHmac('sha256', key).update(x).digest()\n}\n\nfunction sha256(x) {\n  return crypto.createHash('sha256').update(x).digest()\n}\n\nfunction xor(a, b) {\n  const length = Math.max(a.length, b.length)\n  const buffer = Buffer.allocUnsafe(length)\n  for (let i = 0; i < length; i++)\n    buffer[i] = a[i] ^ b[i]\n  return buffer\n}\n\nfunction timer(fn, seconds) {\n  seconds = typeof seconds === 'function' ? seconds() : seconds\n  if (!seconds)\n    return { cancel: noop, start: noop }\n\n  let timer\n  return {\n    cancel() {\n      timer && (clearTimeout(timer), timer = null)\n    },\n    start() {\n      timer && clearTimeout(timer)\n      timer = setTimeout(done, seconds * 1000, arguments)\n    }\n  }\n\n  function done(args) {\n    fn.apply(null, args)\n    timer = null\n  }\n}\n"
  },
  {
    "path": "src/errors.js",
    "content": "export class PostgresError extends Error {\n  constructor(x) {\n    super(x.message)\n    this.name = this.constructor.name\n    Object.assign(this, x)\n  }\n}\n\nexport const Errors = {\n  connection,\n  postgres,\n  generic,\n  notSupported\n}\n\nfunction connection(x, options, socket) {\n  const { host, port } = socket || options\n  const error = Object.assign(\n    new Error(('write ' + x + ' ' + (options.path || (host + ':' + port)))),\n    {\n      code: x,\n      errno: x,\n      address: options.path || host\n    }, options.path ? {} : { port: port }\n  )\n  Error.captureStackTrace(error, connection)\n  return error\n}\n\nfunction postgres(x) {\n  const error = new PostgresError(x)\n  Error.captureStackTrace(error, postgres)\n  return error\n}\n\nfunction generic(code, message) {\n  const error = Object.assign(new Error(code + ': ' + message), { code })\n  Error.captureStackTrace(error, generic)\n  return error\n}\n\n/* c8 ignore next 10 */\nfunction notSupported(x) {\n  const error = Object.assign(\n    new Error(x + ' (B) is not supported'),\n    {\n      code: 'MESSAGE_NOT_SUPPORTED',\n      name: x\n    }\n  )\n  Error.captureStackTrace(error, notSupported)\n  return error\n}\n"
  },
  {
    "path": "src/index.js",
    "content": "import os from 'os'\nimport fs from 'fs'\n\nimport {\n  mergeUserTypes,\n  inferType,\n  Parameter,\n  Identifier,\n  Builder,\n  toPascal,\n  pascal,\n  toCamel,\n  camel,\n  toKebab,\n  kebab,\n  fromPascal,\n  fromCamel,\n  fromKebab\n} from './types.js'\n\nimport Connection from './connection.js'\nimport { Query, CLOSE } from './query.js'\nimport Queue from './queue.js'\nimport { Errors, PostgresError } from './errors.js'\nimport Subscribe from './subscribe.js'\nimport largeObject from './large.js'\n\nObject.assign(Postgres, {\n  PostgresError,\n  toPascal,\n  pascal,\n  toCamel,\n  camel,\n  toKebab,\n  kebab,\n  fromPascal,\n  fromCamel,\n  fromKebab,\n  BigInt: {\n    to: 20,\n    from: [20],\n    parse: x => BigInt(x), // eslint-disable-line\n    serialize: x => x.toString()\n  }\n})\n\nexport default Postgres\n\nfunction Postgres(a, b) {\n  const options = parseOptions(a, b)\n      , subscribe = options.no_subscribe || Subscribe(Postgres, { ...options })\n\n  let ending = false\n\n  const queries = Queue()\n      , connecting = Queue()\n      , reserved = Queue()\n      , closed = Queue()\n      , ended = Queue()\n      , open = Queue()\n      , busy = Queue()\n      , full = Queue()\n      , queues = { connecting, reserved, closed, ended, open, busy, full }\n\n  const connections = [...Array(options.max)].map(() => Connection(options, queues, { onopen, onend, onclose }))\n\n  const sql = Sql(handler)\n\n  Object.assign(sql, {\n    get parameters() { return options.parameters },\n    largeObject: largeObject.bind(null, sql),\n    subscribe,\n    CLOSE,\n    END: CLOSE,\n    PostgresError,\n    options,\n    reserve,\n    listen,\n    begin,\n    close,\n    end\n  })\n\n  return sql\n\n  function Sql(handler) {\n    handler.debug = options.debug\n\n    Object.entries(options.types).reduce((acc, [name, type]) => {\n      acc[name] = (x) => new Parameter(x, type.to)\n      return acc\n    }, typed)\n\n    Object.assign(sql, {\n      types: typed,\n      typed,\n      unsafe,\n      notify,\n      array,\n      json,\n      file\n    })\n\n    return sql\n\n    function typed(value, type) {\n      return new Parameter(value, type)\n    }\n\n    function sql(strings, ...args) {\n      const query = strings && Array.isArray(strings.raw)\n        ? new Query(strings, args, handler, cancel)\n        : typeof strings === 'string' && !args.length\n          ? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings)\n          : new Builder(strings, args)\n      return query\n    }\n\n    function unsafe(string, args = [], options = {}) {\n      arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])\n      const query = new Query([string], args, handler, cancel, {\n        prepare: false,\n        ...options,\n        simple: 'simple' in options ? options.simple : args.length === 0\n      })\n      return query\n    }\n\n    function file(path, args = [], options = {}) {\n      arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])\n      const query = new Query([], args, (query) => {\n        fs.readFile(path, 'utf8', (err, string) => {\n          if (err)\n            return query.reject(err)\n\n          query.strings = [string]\n          handler(query)\n        })\n      }, cancel, {\n        ...options,\n        simple: 'simple' in options ? options.simple : args.length === 0\n      })\n      return query\n    }\n  }\n\n  async function listen(name, fn, onlisten) {\n    const listener = { fn, onlisten }\n\n    const sql = listen.sql || (listen.sql = Postgres({\n      ...options,\n      max: 1,\n      idle_timeout: null,\n      max_lifetime: null,\n      fetch_types: false,\n      onclose() {\n        Object.entries(listen.channels).forEach(([name, { listeners }]) => {\n          delete listen.channels[name]\n          Promise.all(listeners.map(l => listen(name, l.fn, l.onlisten).catch(() => { /* noop */ })))\n        })\n      },\n      onnotify(c, x) {\n        c in listen.channels && listen.channels[c].listeners.forEach(l => l.fn(x))\n      }\n    }))\n\n    const channels = listen.channels || (listen.channels = {})\n        , exists = name in channels\n\n    if (exists) {\n      channels[name].listeners.push(listener)\n      const result = await channels[name].result\n      listener.onlisten && listener.onlisten()\n      return { state: result.state, unlisten }\n    }\n\n    channels[name] = { result: sql`listen ${\n      sql.unsafe('\"' + name.replace(/\"/g, '\"\"') + '\"')\n    }`, listeners: [listener] }\n    const result = await channels[name].result\n    listener.onlisten && listener.onlisten()\n    return { state: result.state, unlisten }\n\n    async function unlisten() {\n      if (name in channels === false)\n        return\n\n      channels[name].listeners = channels[name].listeners.filter(x => x !== listener)\n      if (channels[name].listeners.length)\n        return\n\n      delete channels[name]\n      return sql`unlisten ${\n        sql.unsafe('\"' + name.replace(/\"/g, '\"\"') + '\"')\n      }`\n    }\n  }\n\n  async function notify(channel, payload) {\n    return await sql`select pg_notify(${ channel }, ${ '' + payload })`\n  }\n\n  async function reserve() {\n    const queue = Queue()\n    const c = open.length\n      ? open.shift()\n      : await new Promise((resolve, reject) => {\n        const query = { reserve: resolve, reject }\n        queries.push(query)\n        closed.length && connect(closed.shift(), query)\n      })\n\n    move(c, reserved)\n    c.reserved = () => queue.length\n      ? c.execute(queue.shift())\n      : move(c, reserved)\n    c.reserved.release = true\n\n    const sql = Sql(handler)\n    sql.release = () => {\n      c.reserved = null\n      onopen(c)\n    }\n\n    return sql\n\n    function handler(q) {\n      c.queue === full\n        ? queue.push(q)\n        : c.execute(q) || move(c, full)\n    }\n  }\n\n  async function begin(options, fn) {\n    !fn && (fn = options, options = '')\n    const queries = Queue()\n    let savepoints = 0\n      , connection\n      , prepare = null\n\n    try {\n      await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()\n      return await Promise.race([\n        scope(connection, fn),\n        new Promise((_, reject) => connection.onclose = reject)\n      ])\n    } catch (error) {\n      throw error\n    }\n\n    async function scope(c, fn, name) {\n      const sql = Sql(handler)\n      sql.savepoint = savepoint\n      sql.prepare = x => prepare = x.replace(/[^a-z0-9$-_. ]/gi)\n      let uncaughtError\n        , result\n\n      name && await sql`savepoint ${ sql(name) }`\n      try {\n        result = await new Promise((resolve, reject) => {\n          const x = fn(sql)\n          Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)\n        })\n\n        if (uncaughtError)\n          throw uncaughtError\n      } catch (e) {\n        await (name\n          ? sql`rollback to ${ sql(name) }`\n          : sql`rollback`\n        )\n        throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e\n      }\n\n      if (!name) {\n        prepare\n          ? await sql`prepare transaction '${ sql.unsafe(prepare) }'`\n          : await sql`commit`\n      }\n\n      return result\n\n      function savepoint(name, fn) {\n        if (name && Array.isArray(name.raw))\n          return savepoint(sql => sql.apply(sql, arguments))\n\n        arguments.length === 1 && (fn = name, name = null)\n        return scope(c, fn, 's' + savepoints++ + (name ? '_' + name : ''))\n      }\n\n      function handler(q) {\n        q.catch(e => uncaughtError || (uncaughtError = e))\n        c.queue === full\n          ? queries.push(q)\n          : c.execute(q) || move(c, full)\n      }\n    }\n\n    function onexecute(c) {\n      connection = c\n      move(c, reserved)\n      c.reserved = () => queries.length\n        ? c.execute(queries.shift())\n        : move(c, reserved)\n    }\n  }\n\n  function move(c, queue) {\n    c.queue.remove(c)\n    queue.push(c)\n    c.queue = queue\n    queue === open\n      ? c.idleTimer.start()\n      : c.idleTimer.cancel()\n    return c\n  }\n\n  function json(x) {\n    return new Parameter(x, 3802)\n  }\n\n  function array(x, type) {\n    if (!Array.isArray(x))\n      return array(Array.from(arguments))\n\n    return new Parameter(x, type || (x.length ? inferType(x) || 25 : 0), options.shared.typeArrayMap)\n  }\n\n  function handler(query) {\n    if (ending)\n      return query.reject(Errors.connection('CONNECTION_ENDED', options, options))\n\n    if (open.length)\n      return go(open.shift(), query)\n\n    if (closed.length)\n      return connect(closed.shift(), query)\n\n    busy.length\n      ? go(busy.shift(), query)\n      : queries.push(query)\n  }\n\n  function go(c, query) {\n    return c.execute(query)\n      ? move(c, busy)\n      : move(c, full)\n  }\n\n  function cancel(query) {\n    return new Promise((resolve, reject) => {\n      query.state\n        ? query.active\n          ? Connection(options).cancel(query.state, resolve, reject)\n          : query.cancelled = { resolve, reject }\n        : (\n          queries.remove(query),\n          query.cancelled = true,\n          query.reject(Errors.generic('57014', 'canceling statement due to user request')),\n          resolve()\n        )\n    })\n  }\n\n  async function end({ timeout = null } = {}) {\n    if (ending)\n      return ending\n\n    await 1\n    let timer\n    return ending = Promise.race([\n      new Promise(r => timeout !== null && (timer = setTimeout(destroy, timeout * 1000, r))),\n      Promise.all(connections.map(c => c.end()).concat(\n        listen.sql ? listen.sql.end({ timeout: 0 }) : [],\n        subscribe.sql ? subscribe.sql.end({ timeout: 0 }) : []\n      ))\n    ]).then(() => clearTimeout(timer))\n  }\n\n  async function close() {\n    await Promise.all(connections.map(c => c.end()))\n  }\n\n  async function destroy(resolve) {\n    await Promise.all(connections.map(c => c.terminate()))\n    while (queries.length)\n      queries.shift().reject(Errors.connection('CONNECTION_DESTROYED', options))\n    resolve()\n  }\n\n  function connect(c, query) {\n    move(c, connecting)\n    c.connect(query)\n    return c\n  }\n\n  function onend(c) {\n    move(c, ended)\n  }\n\n  function onopen(c) {\n    if (queries.length === 0)\n      return move(c, open)\n\n    let max = Math.ceil(queries.length / (connecting.length + 1))\n      , ready = true\n\n    while (ready && queries.length && max-- > 0) {\n      const query = queries.shift()\n      if (query.reserve)\n        return query.reserve(c)\n\n      ready = c.execute(query)\n    }\n\n    ready\n      ? move(c, busy)\n      : move(c, full)\n  }\n\n  function onclose(c, e) {\n    move(c, closed)\n    c.reserved = null\n    c.onclose && (c.onclose(e), c.onclose = null)\n    options.onclose && options.onclose(c.id)\n    queries.length && connect(c, queries.shift())\n  }\n}\n\nfunction parseOptions(a, b) {\n  if (a && a.shared)\n    return a\n\n  const env = process.env // eslint-disable-line\n      , o = (!a || typeof a === 'string' ? b : a) || {}\n      , { url, multihost } = parseUrl(a)\n      , query = [...url.searchParams].reduce((a, [b, c]) => (a[b] = c, a), {})\n      , host = o.hostname || o.host || multihost || url.hostname || env.PGHOST || 'localhost'\n      , port = o.port || url.port || env.PGPORT || 5432\n      , user = o.user || o.username || url.username || env.PGUSERNAME || env.PGUSER || osUsername()\n\n  o.no_prepare && (o.prepare = false)\n  query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)\n  'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line\n  query.sslrootcert === 'system' && (query.ssl = 'verify-full')\n\n  const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive']\n  const defaults = {\n    max             : globalThis.Cloudflare ? 3 : 10,\n    ssl             : false,\n    sslnegotiation  : null,\n    idle_timeout    : null,\n    connect_timeout : 30,\n    max_lifetime    : max_lifetime,\n    max_pipeline    : 100,\n    backoff         : backoff,\n    keep_alive      : 60,\n    prepare         : true,\n    debug           : false,\n    fetch_types     : true,\n    publications    : 'alltables',\n    target_session_attrs: null\n  }\n\n  return {\n    host            : Array.isArray(host) ? host : host.split(',').map(x => x.split(':')[0]),\n    port            : Array.isArray(port) ? port : host.split(',').map(x => parseInt(x.split(':')[1] || port)),\n    path            : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,\n    database        : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || user,\n    user            : user,\n    pass            : o.pass || o.password || url.password || env.PGPASSWORD || '',\n    ...Object.entries(defaults).reduce(\n      (acc, [k, d]) => {\n        const value = k in o ? o[k] : k in query\n          ? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])\n          : env['PG' + k.toUpperCase()] || d\n        acc[k] = typeof value === 'string' && ints.includes(k)\n          ? +value\n          : value\n        return acc\n      },\n      {}\n    ),\n    connection      : {\n      application_name: env.PGAPPNAME || 'postgres.js',\n      ...o.connection,\n      ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})\n    },\n    types           : o.types || {},\n    target_session_attrs: tsa(o, url, env),\n    onnotice        : o.onnotice,\n    onnotify        : o.onnotify,\n    onclose         : o.onclose,\n    onparameter     : o.onparameter,\n    socket          : o.socket,\n    transform       : parseTransform(o.transform || { undefined: undefined }),\n    parameters      : {},\n    shared          : { retries: 0, typeArrayMap: {} },\n    ...mergeUserTypes(o.types)\n  }\n}\n\nfunction tsa(o, url, env) {\n  const x = o.target_session_attrs || url.searchParams.get('target_session_attrs') || env.PGTARGETSESSIONATTRS\n  if (!x || ['read-write', 'read-only', 'primary', 'standby', 'prefer-standby'].includes(x))\n    return x\n\n  throw new Error('target_session_attrs ' + x + ' is not supported')\n}\n\nfunction backoff(retries) {\n  return (0.5 + Math.random() / 2) * Math.min(3 ** retries / 100, 20)\n}\n\nfunction max_lifetime() {\n  return 60 * (30 + Math.random() * 30)\n}\n\nfunction parseTransform(x) {\n  return {\n    undefined: x.undefined,\n    column: {\n      from: typeof x.column === 'function' ? x.column : x.column && x.column.from,\n      to: x.column && x.column.to\n    },\n    value: {\n      from: typeof x.value === 'function' ? x.value : x.value && x.value.from,\n      to: x.value && x.value.to\n    },\n    row: {\n      from: typeof x.row === 'function' ? x.row : x.row && x.row.from,\n      to: x.row && x.row.to\n    }\n  }\n}\n\nfunction parseUrl(url) {\n  if (!url || typeof url !== 'string')\n    return { url: { searchParams: new Map() } }\n\n  let host = url\n  host = host.slice(host.indexOf('://') + 3).split(/[?/]/)[0]\n  host = decodeURIComponent(host.slice(host.indexOf('@') + 1))\n\n  const urlObj = new URL(url.replace(host, host.split(',')[0]))\n\n  return {\n    url: {\n      username: decodeURIComponent(urlObj.username),\n      password: decodeURIComponent(urlObj.password),\n      host: urlObj.host,\n      hostname: urlObj.hostname,\n      port: urlObj.port,\n      pathname: urlObj.pathname,\n      searchParams: urlObj.searchParams\n    },\n    multihost: host.indexOf(',') > -1 && host\n  }\n}\n\nfunction osUsername() {\n  try {\n    return os.userInfo().username // eslint-disable-line\n  } catch (_) {\n    return process.env.USERNAME || process.env.USER || process.env.LOGNAME  // eslint-disable-line\n  }\n}\n"
  },
  {
    "path": "src/large.js",
    "content": "import Stream from 'stream'\n\nexport default function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {\n  return new Promise(async(resolve, reject) => {\n    await sql.begin(async sql => {\n      let finish\n      !oid && ([{ oid }] = await sql`select lo_creat(-1) as oid`)\n      const [{ fd }] = await sql`select lo_open(${ oid }, ${ mode }) as fd`\n\n      const lo = {\n        writable,\n        readable,\n        close     : () => sql`select lo_close(${ fd })`.then(finish),\n        tell      : () => sql`select lo_tell64(${ fd })`,\n        read      : (x) => sql`select loread(${ fd }, ${ x }) as data`,\n        write     : (x) => sql`select lowrite(${ fd }, ${ x })`,\n        truncate  : (x) => sql`select lo_truncate64(${ fd }, ${ x })`,\n        seek      : (x, whence = 0) => sql`select lo_lseek64(${ fd }, ${ x }, ${ whence })`,\n        size      : () => sql`\n          select\n            lo_lseek64(${ fd }, location, 0) as position,\n            seek.size\n          from (\n            select\n              lo_lseek64($1, 0, 2) as size,\n              tell.location\n            from (select lo_tell64($1) as location) tell\n          ) seek\n        `\n      }\n\n      resolve(lo)\n\n      return new Promise(async r => finish = r)\n\n      async function readable({\n        highWaterMark = 2048 * 8,\n        start = 0,\n        end = Infinity\n      } = {}) {\n        let max = end - start\n        start && await lo.seek(start)\n        return new Stream.Readable({\n          highWaterMark,\n          async read(size) {\n            const l = size > max ? size - max : size\n            max -= size\n            const [{ data }] = await lo.read(l)\n            this.push(data)\n            if (data.length < size)\n              this.push(null)\n          }\n        })\n      }\n\n      async function writable({\n        highWaterMark = 2048 * 8,\n        start = 0\n      } = {}) {\n        start && await lo.seek(start)\n        return new Stream.Writable({\n          highWaterMark,\n          write(chunk, encoding, callback) {\n            lo.write(chunk).then(() => callback(), callback)\n          }\n        })\n      }\n    }).catch(reject)\n  })\n}\n"
  },
  {
    "path": "src/query.js",
    "content": "const originCache = new Map()\n    , originStackCache = new Map()\n    , originError = Symbol('OriginError')\n\nexport const CLOSE = {}\nexport class Query extends Promise {\n  constructor(strings, args, handler, canceller, options = {}) {\n    let resolve\n      , reject\n\n    super((a, b) => {\n      resolve = a\n      reject = b\n    })\n\n    this.tagged = Array.isArray(strings.raw)\n    this.strings = strings\n    this.args = args\n    this.handler = handler\n    this.canceller = canceller\n    this.options = options\n\n    this.state = null\n    this.statement = null\n\n    this.resolve = x => (this.active = false, resolve(x))\n    this.reject = x => (this.active = false, reject(x))\n\n    this.active = false\n    this.cancelled = null\n    this.executed = false\n    this.signature = ''\n\n    this[originError] = this.handler.debug\n      ? new Error()\n      : this.tagged && cachedError(this.strings)\n  }\n\n  get origin() {\n    return (this.handler.debug\n      ? this[originError].stack\n      : this.tagged && originStackCache.has(this.strings)\n        ? originStackCache.get(this.strings)\n        : originStackCache.set(this.strings, this[originError].stack).get(this.strings)\n    ) || ''\n  }\n\n  static get [Symbol.species]() {\n    return Promise\n  }\n\n  cancel() {\n    return this.canceller && (this.canceller(this), this.canceller = null)\n  }\n\n  simple() {\n    this.options.simple = true\n    this.options.prepare = false\n    return this\n  }\n\n  async readable() {\n    this.simple()\n    this.streaming = true\n    return this\n  }\n\n  async writable() {\n    this.simple()\n    this.streaming = true\n    return this\n  }\n\n  cursor(rows = 1, fn) {\n    this.options.simple = false\n    if (typeof rows === 'function') {\n      fn = rows\n      rows = 1\n    }\n\n    this.cursorRows = rows\n\n    if (typeof fn === 'function')\n      return (this.cursorFn = fn, this)\n\n    let prev\n    return {\n      [Symbol.asyncIterator]: () => ({\n        next: () => {\n          if (this.executed && !this.active)\n            return { done: true }\n\n          prev && prev()\n          const promise = new Promise((resolve, reject) => {\n            this.cursorFn = value => {\n              resolve({ value, done: false })\n              return new Promise(r => prev = r)\n            }\n            this.resolve = () => (this.active = false, resolve({ done: true }))\n            this.reject = x => (this.active = false, reject(x))\n          })\n          this.execute()\n          return promise\n        },\n        return() {\n          prev && prev(CLOSE)\n          return { done: true }\n        }\n      })\n    }\n  }\n\n  describe() {\n    this.options.simple = false\n    this.onlyDescribe = this.options.prepare = true\n    return this\n  }\n\n  stream() {\n    throw new Error('.stream has been renamed to .forEach')\n  }\n\n  forEach(fn) {\n    this.forEachFn = fn\n    this.handle()\n    return this\n  }\n\n  raw() {\n    this.isRaw = true\n    return this\n  }\n\n  values() {\n    this.isRaw = 'values'\n    return this\n  }\n\n  async handle() {\n    !this.executed && (this.executed = true) && await 1 && this.handler(this)\n  }\n\n  execute() {\n    this.handle()\n    return this\n  }\n\n  then() {\n    this.handle()\n    return super.then.apply(this, arguments)\n  }\n\n  catch() {\n    this.handle()\n    return super.catch.apply(this, arguments)\n  }\n\n  finally() {\n    this.handle()\n    return super.finally.apply(this, arguments)\n  }\n}\n\nfunction cachedError(xs) {\n  if (originCache.has(xs))\n    return originCache.get(xs)\n\n  const x = Error.stackTraceLimit\n  Error.stackTraceLimit = 4\n  originCache.set(xs, new Error())\n  Error.stackTraceLimit = x\n  return originCache.get(xs)\n}\n"
  },
  {
    "path": "src/queue.js",
    "content": "export default Queue\n\nfunction Queue(initial = []) {\n  let xs = initial.slice()\n  let index = 0\n\n  return {\n    get length() {\n      return xs.length - index\n    },\n    remove: (x) => {\n      const index = xs.indexOf(x)\n      return index === -1\n        ? null\n        : (xs.splice(index, 1), x)\n    },\n    push: (x) => (xs.push(x), x),\n    shift: () => {\n      const out = xs[index++]\n\n      if (index === xs.length) {\n        index = 0\n        xs = []\n      } else {\n        xs[index - 1] = undefined\n      }\n\n      return out\n    }\n  }\n}\n"
  },
  {
    "path": "src/result.js",
    "content": "export default class Result extends Array {\n  constructor() {\n    super()\n    Object.defineProperties(this, {\n      count: { value: null, writable: true },\n      state: { value: null, writable: true },\n      command: { value: null, writable: true },\n      columns: { value: null, writable: true },\n      statement: { value: null, writable: true }\n    })\n  }\n\n  static get [Symbol.species]() {\n    return Array\n  }\n}\n"
  },
  {
    "path": "src/subscribe.js",
    "content": "const noop = () => { /* noop */ }\n\nexport default function Subscribe(postgres, options) {\n  const subscribers = new Map()\n      , slot = 'postgresjs_' + Math.random().toString(36).slice(2)\n      , state = {}\n\n  let connection\n    , stream\n    , ended = false\n\n  const sql = subscribe.sql = postgres({\n    ...options,\n    transform: { column: {}, value: {}, row: {} },\n    max: 1,\n    fetch_types: false,\n    idle_timeout: null,\n    max_lifetime: null,\n    connection: {\n      ...options.connection,\n      replication: 'database'\n    },\n    onclose: async function() {\n      if (ended)\n        return\n      stream = null\n      state.pid = state.secret = undefined\n      connected(await init(sql, slot, options.publications))\n      subscribers.forEach(event => event.forEach(({ onsubscribe }) => onsubscribe()))\n    },\n    no_subscribe: true\n  })\n\n  const end = sql.end\n      , close = sql.close\n\n  sql.end = async() => {\n    ended = true\n    stream && (await new Promise(r => (stream.once('close', r), stream.end())))\n    return end()\n  }\n\n  sql.close = async() => {\n    stream && (await new Promise(r => (stream.once('close', r), stream.end())))\n    return close()\n  }\n\n  return subscribe\n\n  async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {\n    event = parseEvent(event)\n\n    if (!connection)\n      connection = init(sql, slot, options.publications)\n\n    const subscriber = { fn, onsubscribe }\n    const fns = subscribers.has(event)\n      ? subscribers.get(event).add(subscriber)\n      : subscribers.set(event, new Set([subscriber])).get(event)\n\n    const unsubscribe = () => {\n      fns.delete(subscriber)\n      fns.size === 0 && subscribers.delete(event)\n    }\n\n    return connection.then(x => {\n      connected(x)\n      onsubscribe()\n      stream && stream.on('error', onerror)\n      return { unsubscribe, state, sql }\n    })\n  }\n\n  function connected(x) {\n    stream = x.stream\n    state.pid = x.state.pid\n    state.secret = x.state.secret\n  }\n\n  async function init(sql, slot, publications) {\n    if (!publications)\n      throw new Error('Missing publication names')\n\n    const xs = await sql.unsafe(\n      `CREATE_REPLICATION_SLOT ${ slot } TEMPORARY LOGICAL pgoutput NOEXPORT_SNAPSHOT`\n    )\n\n    const [x] = xs\n\n    const stream = await sql.unsafe(\n      `START_REPLICATION SLOT ${ slot } LOGICAL ${\n        x.consistent_point\n      } (proto_version '1', publication_names '${ publications }')`\n    ).writable()\n\n    const state = {\n      lsn: Buffer.concat(x.consistent_point.split('/').map(x => Buffer.from(('00000000' + x).slice(-8), 'hex')))\n    }\n\n    stream.on('data', data)\n    stream.on('error', error)\n    stream.on('close', sql.close)\n\n    return { stream, state: xs.state }\n\n    function error(e) {\n      console.error('Unexpected error during logical streaming - reconnecting', e) // eslint-disable-line\n    }\n\n    function data(x) {\n      if (x[0] === 0x77) {\n        parse(x.subarray(25), state, sql.options.parsers, handle, options.transform)\n      } else if (x[0] === 0x6b && x[17]) {\n        state.lsn = x.subarray(1, 9)\n        pong()\n      }\n    }\n\n    function handle(a, b) {\n      const path = b.relation.schema + '.' + b.relation.table\n      call('*', a, b)\n      call('*:' + path, a, b)\n      b.relation.keys.length && call('*:' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)\n      call(b.command, a, b)\n      call(b.command + ':' + path, a, b)\n      b.relation.keys.length && call(b.command + ':' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)\n    }\n\n    function pong() {\n      const x = Buffer.alloc(34)\n      x[0] = 'r'.charCodeAt(0)\n      x.fill(state.lsn, 1)\n      x.writeBigInt64BE(BigInt(Date.now() - Date.UTC(2000, 0, 1)) * BigInt(1000), 25)\n      stream.write(x)\n    }\n  }\n\n  function call(x, a, b) {\n    subscribers.has(x) && subscribers.get(x).forEach(({ fn }) => fn(a, b, x))\n  }\n}\n\nfunction Time(x) {\n  return new Date(Date.UTC(2000, 0, 1) + Number(x / BigInt(1000)))\n}\n\nfunction parse(x, state, parsers, handle, transform) {\n  const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)\n\n  Object.entries({\n    R: x => {  // Relation\n      let i = 1\n      const r = state[x.readUInt32BE(i)] = {\n        schema: x.toString('utf8', i += 4, i = x.indexOf(0, i)) || 'pg_catalog',\n        table: x.toString('utf8', i + 1, i = x.indexOf(0, i + 1)),\n        columns: Array(x.readUInt16BE(i += 2)),\n        keys: []\n      }\n      i += 2\n\n      let columnIndex = 0\n        , column\n\n      while (i < x.length) {\n        column = r.columns[columnIndex++] = {\n          key: x[i++],\n          name: transform.column.from\n            ? transform.column.from(x.toString('utf8', i, i = x.indexOf(0, i)))\n            : x.toString('utf8', i, i = x.indexOf(0, i)),\n          type: x.readUInt32BE(i += 1),\n          parser: parsers[x.readUInt32BE(i)],\n          atttypmod: x.readUInt32BE(i += 4)\n        }\n\n        column.key && r.keys.push(column)\n        i += 4\n      }\n    },\n    Y: () => { /* noop */ }, // Type\n    O: () => { /* noop */ }, // Origin\n    B: x => { // Begin\n      state.date = Time(x.readBigInt64BE(9))\n      state.lsn = x.subarray(1, 9)\n    },\n    I: x => { // Insert\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      const { row } = tuples(x, relation.columns, i += 7, transform)\n\n      handle(row, {\n        command: 'insert',\n        relation\n      })\n    },\n    D: x => { // Delete\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      i += 4\n      const key = x[i] === 75\n      handle(key || x[i] === 79\n        ? tuples(x, relation.columns, i += 3, transform).row\n        : null\n      , {\n        command: 'delete',\n        relation,\n        key\n      })\n    },\n    U: x => { // Update\n      let i = 1\n      const relation = state[x.readUInt32BE(i)]\n      i += 4\n      const key = x[i] === 75\n      const xs = key || x[i] === 79\n        ? tuples(x, relation.columns, i += 3, transform)\n        : null\n\n      xs && (i = xs.i)\n\n      const { row } = tuples(x, relation.columns, i + 3, transform)\n\n      handle(row, {\n        command: 'update',\n        relation,\n        key,\n        old: xs && xs.row\n      })\n    },\n    T: () => { /* noop */ }, // Truncate,\n    C: () => { /* noop */ }  // Commit\n  }).reduce(char, {})[x[0]](x)\n}\n\nfunction tuples(x, columns, xi, transform) {\n  let type\n    , column\n    , value\n\n  const row = transform.raw ? new Array(columns.length) : {}\n  for (let i = 0; i < columns.length; i++) {\n    type = x[xi++]\n    column = columns[i]\n    value = type === 110 // n\n      ? null\n      : type === 117 // u\n        ? undefined\n        : column.parser === undefined\n          ? x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi))\n          : column.parser.array === true\n            ? column.parser(x.toString('utf8', xi + 5, xi += 4 + x.readUInt32BE(xi)))\n            : column.parser(x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi)))\n\n    transform.raw\n      ? (row[i] = transform.raw === true\n        ? value\n        : transform.value.from ? transform.value.from(value, column) : value)\n      : (row[column.name] = transform.value.from\n        ? transform.value.from(value, column)\n        : value\n      )\n  }\n\n  return { i: xi, row: transform.row.from ? transform.row.from(row) : row }\n}\n\nfunction parseEvent(x) {\n  const xs = x.match(/^(\\*|insert|update|delete)?:?([^.]+?\\.?[^=]+)?=?(.+)?/i) || []\n\n  if (!xs)\n    throw new Error('Malformed subscribe pattern: ' + x)\n\n  const [, command, path, key] = xs\n\n  return (command || '*')\n       + (path ? ':' + (path.indexOf('.') === -1 ? 'public.' + path : path) : '')\n       + (key ? '=' + key : '')\n}\n"
  },
  {
    "path": "src/types.js",
    "content": "import { Query } from './query.js'\nimport { Errors } from './errors.js'\n\nexport const types = {\n  string: {\n    to: 25,\n    from: null,             // defaults to string\n    serialize: x => '' + x\n  },\n  number: {\n    to: 0,\n    from: [21, 23, 26, 700, 701],\n    serialize: x => '' + x,\n    parse: x => +x\n  },\n  json: {\n    to: 114,\n    from: [114, 3802],\n    serialize: x => JSON.stringify(x),\n    parse: x => JSON.parse(x)\n  },\n  boolean: {\n    to: 16,\n    from: 16,\n    serialize: x => x === true ? 't' : 'f',\n    parse: x => x === 't'\n  },\n  date: {\n    to: 1184,\n    from: [1082, 1114, 1184],\n    serialize: x => (x instanceof Date ? x : new Date(x)).toISOString(),\n    parse: x => new Date(x)\n  },\n  bytea: {\n    to: 17,\n    from: 17,\n    serialize: x => '\\\\x' + Buffer.from(x).toString('hex'),\n    parse: x => Buffer.from(x.slice(2), 'hex')\n  }\n}\n\nclass NotTagged { then() { notTagged() } catch() { notTagged() } finally() { notTagged() }}\n\nexport class Identifier extends NotTagged {\n  constructor(value) {\n    super()\n    this.value = escapeIdentifier(value)\n  }\n}\n\nexport class Parameter extends NotTagged {\n  constructor(value, type, array) {\n    super()\n    this.value = value\n    this.type = type\n    this.array = array\n  }\n}\n\nexport class Builder extends NotTagged {\n  constructor(first, rest) {\n    super()\n    this.first = first\n    this.rest = rest\n  }\n\n  build(before, parameters, types, options) {\n    const keyword = builders.map(([x, fn]) => ({ fn, i: before.search(x) })).sort((a, b) => a.i - b.i).pop()\n    return keyword.i === -1\n      ? escapeIdentifiers(this.first, options)\n      : keyword.fn(this.first, this.rest, parameters, types, options)\n  }\n}\n\nexport function handleValue(x, parameters, types, options) {\n  let value = x instanceof Parameter ? x.value : x\n  if (value === undefined) {\n    x instanceof Parameter\n      ? x.value = options.transform.undefined\n      : value = x = options.transform.undefined\n\n    if (value === undefined)\n      throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')\n  }\n\n  return '$' + (types.push(\n    x instanceof Parameter\n      ? (parameters.push(x.value), x.array\n        ? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)\n        : x.type\n      )\n      : (parameters.push(x), inferType(x))\n  ))\n}\n\nconst defaultHandlers = typeHandlers(types)\n\nexport function stringify(q, string, value, parameters, types, options) { // eslint-disable-line\n  for (let i = 1; i < q.strings.length; i++) {\n    string += (stringifyValue(string, value, parameters, types, options)) + q.strings[i]\n    value = q.args[i]\n  }\n\n  return string\n}\n\nfunction stringifyValue(string, value, parameters, types, o) {\n  return (\n    value instanceof Builder ? value.build(string, parameters, types, o) :\n    value instanceof Query ? fragment(value, parameters, types, o) :\n    value instanceof Identifier ? value.value :\n    value && value[0] instanceof Query ? value.reduce((acc, x) => acc + ' ' + fragment(x, parameters, types, o), '') :\n    handleValue(value, parameters, types, o)\n  )\n}\n\nfunction fragment(q, parameters, types, options) {\n  q.fragment = true\n  return stringify(q, q.strings[0], q.args[0], parameters, types, options)\n}\n\nfunction valuesBuilder(first, parameters, types, columns, options) {\n  return first.map(row =>\n    '(' + columns.map(column =>\n      stringifyValue('values', row[column], parameters, types, options)\n    ).join(',') + ')'\n  ).join(',')\n}\n\nfunction values(first, rest, parameters, types, options) {\n  const multi = Array.isArray(first[0])\n  const columns = rest.length ? rest.flat() : Object.keys(multi ? first[0] : first)\n  return valuesBuilder(multi ? first : [first], parameters, types, columns, options)\n}\n\nfunction select(first, rest, parameters, types, options) {\n  typeof first === 'string' && (first = [first].concat(rest))\n  if (Array.isArray(first))\n    return escapeIdentifiers(first, options)\n\n  let value\n  const columns = rest.length ? rest.flat() : Object.keys(first)\n  return columns.map(x => {\n    value = first[x]\n    return (\n      value instanceof Query ? fragment(value, parameters, types, options) :\n      value instanceof Identifier ? value.value :\n      handleValue(value, parameters, types, options)\n    ) + ' as ' + escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)\n  }).join(',')\n}\n\nconst builders = Object.entries({\n  values,\n  in: (...xs) => {\n    const x = values(...xs)\n    return x === '()' ? '(null)' : x\n  },\n  select,\n  as: select,\n  returning: select,\n  '\\\\(': select,\n\n  update(first, rest, parameters, types, options) {\n    return (rest.length ? rest.flat() : Object.keys(first)).map(x =>\n      escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x) +\n      '=' + stringifyValue('values', first[x], parameters, types, options)\n    )\n  },\n\n  insert(first, rest, parameters, types, options) {\n    const columns = rest.length ? rest.flat() : Object.keys(Array.isArray(first) ? first[0] : first)\n    return '(' + escapeIdentifiers(columns, options) + ')values' +\n    valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, columns, options)\n  }\n}).map(([x, fn]) => ([new RegExp('((?:^|[\\\\s(])' + x + '(?:$|[\\\\s(]))(?![\\\\s\\\\S]*\\\\1)', 'i'), fn]))\n\nfunction notTagged() {\n  throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')\n}\n\nexport const serializers = defaultHandlers.serializers\nexport const parsers = defaultHandlers.parsers\n\nexport const END = {}\n\nfunction firstIsString(x) {\n  if (Array.isArray(x))\n    return firstIsString(x[0])\n  return typeof x === 'string' ? 1009 : 0\n}\n\nexport const mergeUserTypes = function(types) {\n  const user = typeHandlers(types || {})\n  return {\n    serializers: Object.assign({}, serializers, user.serializers),\n    parsers: Object.assign({}, parsers, user.parsers)\n  }\n}\n\nfunction typeHandlers(types) {\n  return Object.keys(types).reduce((acc, k) => {\n    types[k].from && [].concat(types[k].from).forEach(x => acc.parsers[x] = types[k].parse)\n    if (types[k].serialize) {\n      acc.serializers[types[k].to] = types[k].serialize\n      types[k].from && [].concat(types[k].from).forEach(x => acc.serializers[x] = types[k].serialize)\n    }\n    return acc\n  }, { parsers: {}, serializers: {} })\n}\n\nfunction escapeIdentifiers(xs, { transform: { column } }) {\n  return xs.map(x => escapeIdentifier(column.to ? column.to(x) : x)).join(',')\n}\n\nexport const escapeIdentifier = function escape(str) {\n  return '\"' + str.replace(/\"/g, '\"\"').replace(/\\./g, '\".\"') + '\"'\n}\n\nexport const inferType = function inferType(x) {\n  return (\n    x instanceof Parameter ? x.type :\n    x instanceof Date ? 1184 :\n    x instanceof Uint8Array ? 17 :\n    (x === true || x === false) ? 16 :\n    typeof x === 'bigint' ? 20 :\n    Array.isArray(x) ? inferType(x[0]) :\n    0\n  )\n}\n\nconst escapeBackslash = /\\\\/g\nconst escapeQuote = /\"/g\n\nfunction arrayEscape(x) {\n  return x\n    .replace(escapeBackslash, '\\\\\\\\')\n    .replace(escapeQuote, '\\\\\"')\n}\n\nexport const arraySerializer = function arraySerializer(xs, serializer, options, typarray) {\n  if (Array.isArray(xs) === false)\n    return xs\n\n  if (!xs.length)\n    return '{}'\n\n  const first = xs[0]\n  // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter\n  const delimiter = typarray === 1020 ? ';' : ','\n\n  if (Array.isArray(first) && !first.type)\n    return '{' + xs.map(x => arraySerializer(x, serializer, options, typarray)).join(delimiter) + '}'\n\n  return '{' + xs.map(x => {\n    if (x === undefined) {\n      x = options.transform.undefined\n      if (x === undefined)\n        throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')\n    }\n\n    return x === null\n      ? 'null'\n      : '\"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '\"'\n  }).join(delimiter) + '}'\n}\n\nconst arrayParserState = {\n  i: 0,\n  char: null,\n  str: '',\n  quoted: false,\n  last: 0\n}\n\nexport const arrayParser = function arrayParser(x, parser, typarray) {\n  arrayParserState.i = arrayParserState.last = 0\n  return arrayParserLoop(arrayParserState, x, parser, typarray)\n}\n\nfunction arrayParserLoop(s, x, parser, typarray) {\n  const xs = []\n  // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter\n  const delimiter = typarray === 1020 ? ';' : ','\n  for (; s.i < x.length; s.i++) {\n    s.char = x[s.i]\n    if (s.quoted) {\n      if (s.char === '\\\\') {\n        s.str += x[++s.i]\n      } else if (s.char === '\"') {\n        xs.push(parser ? parser(s.str) : s.str)\n        s.str = ''\n        s.quoted = x[s.i + 1] === '\"'\n        s.last = s.i + 2\n      } else {\n        s.str += s.char\n      }\n    } else if (s.char === '\"') {\n      s.quoted = true\n    } else if (s.char === '{') {\n      s.last = ++s.i\n      xs.push(arrayParserLoop(s, x, parser, typarray))\n    } else if (s.char === '}') {\n      s.quoted = false\n      s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))\n      s.last = s.i + 1\n      break\n    } else if (s.char === delimiter && s.p !== '}' && s.p !== '\"') {\n      xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))\n      s.last = s.i + 1\n    }\n    s.p = s.char\n  }\n  s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i + 1)) : x.slice(s.last, s.i + 1))\n  return xs\n}\n\nexport const toCamel = x => {\n  let str = x[0]\n  for (let i = 1; i < x.length; i++)\n    str += x[i] === '_' ? x[++i].toUpperCase() : x[i]\n  return str\n}\n\nexport const toPascal = x => {\n  let str = x[0].toUpperCase()\n  for (let i = 1; i < x.length; i++)\n    str += x[i] === '_' ? x[++i].toUpperCase() : x[i]\n  return str\n}\n\nexport const toKebab = x => x.replace(/_/g, '-')\n\nexport const fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()\nexport const fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()\nexport const fromKebab = x => x.replace(/-/g, '_')\n\nfunction createJsonTransform(fn) {\n  return function jsonTransform(x, column) {\n    return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)\n      ? Array.isArray(x)\n        ? x.map(x => jsonTransform(x, column))\n        : Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})\n      : x\n  }\n}\n\ntoCamel.column = { from: toCamel }\ntoCamel.value = { from: createJsonTransform(toCamel) }\nfromCamel.column = { to: fromCamel }\n\nexport const camel = { ...toCamel }\ncamel.column.to = fromCamel\n\ntoPascal.column = { from: toPascal }\ntoPascal.value = { from: createJsonTransform(toPascal) }\nfromPascal.column = { to: fromPascal }\n\nexport const pascal = { ...toPascal }\npascal.column.to = fromPascal\n\ntoKebab.column = { from: toKebab }\ntoKebab.value = { from: createJsonTransform(toKebab) }\nfromKebab.column = { to: fromKebab }\n\nexport const kebab = { ...toKebab }\nkebab.column.to = fromKebab\n"
  },
  {
    "path": "tests/bootstrap.js",
    "content": "import { spawnSync } from 'child_process'\n\nexec('dropdb', ['postgres_js_test'])\n\nexec('psql', ['-c', 'alter system set ssl=on'])\nexec('psql', ['-c', 'drop user postgres_js_test'])\nexec('psql', ['-c', 'create user postgres_js_test'])\nexec('psql', ['-c', 'alter system set password_encryption=md5'])\nexec('psql', ['-c', 'select pg_reload_conf()'])\nexec('psql', ['-c', 'drop user if exists postgres_js_test_md5'])\nexec('psql', ['-c', 'create user postgres_js_test_md5 with password \\'postgres_js_test_md5\\''])\nexec('psql', ['-c', 'alter system set password_encryption=\\'scram-sha-256\\''])\nexec('psql', ['-c', 'select pg_reload_conf()'])\nexec('psql', ['-c', 'drop user if exists postgres_js_test_scram'])\nexec('psql', ['-c', 'create user postgres_js_test_scram with password \\'postgres_js_test_scram\\''])\n\nexec('createdb', ['postgres_js_test'])\nexec('psql', ['-c', 'grant all on database postgres_js_test to postgres_js_test'])\nexec('psql', ['-c', 'alter database postgres_js_test owner to postgres_js_test'])\n\nexport function exec(cmd, args) {\n  const { stderr } = spawnSync(cmd, args, { stdio: 'pipe', encoding: 'utf8' })\n  if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist') && !stderr.includes('WARNING:'))\n    throw stderr\n}\n\nasync function execAsync(cmd, args) { // eslint-disable-line\n  let stderr = ''\n  const cp = await spawn(cmd, args, { stdio: 'pipe', encoding: 'utf8' }) // eslint-disable-line\n  cp.stderr.on('data', x => stderr += x)\n  await new Promise(x => cp.on('exit', x))\n  if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist'))\n    throw new Error(stderr)\n}\n"
  },
  {
    "path": "tests/copy.csv",
    "content": "1\t2\t3\n4\t5\t6\n"
  },
  {
    "path": "tests/index.js",
    "content": "import { exec } from './bootstrap.js'\n\nimport { t, nt, ot } from './test.js' // eslint-disable-line\nimport net from 'net'\nimport fs from 'fs'\nimport crypto from 'crypto'\n\nimport postgres from '../src/index.js'\nconst delay = ms => new Promise(r => setTimeout(r, ms))\n\nconst rel = x => new URL(x, import.meta.url)\nconst idle_timeout = 1\n\nconst login = {\n  user: 'postgres_js_test'\n}\n\nconst login_md5 = {\n  user: 'postgres_js_test_md5',\n  pass: 'postgres_js_test_md5'\n}\n\nconst login_scram = {\n  user: 'postgres_js_test_scram',\n  pass: 'postgres_js_test_scram'\n}\n\nconst options = {\n  db: 'postgres_js_test',\n  user: login.user,\n  pass: login.pass,\n  idle_timeout,\n  connect_timeout: 1,\n  max: 1\n}\n\nconst sql = postgres(options)\n\nt('Connects with no options', async() => {\n  const sql = postgres({ max: 1 })\n\n  const result = (await sql`select 1 as x`)[0].x\n  await sql.end()\n\n  return [1, result]\n})\n\nt('Uses default database without slash', async() => {\n  const sql = postgres('postgres://localhost')\n  return [sql.options.user, sql.options.database]\n})\n\nt('Uses default database with slash', async() => {\n  const sql = postgres('postgres://localhost/')\n  return [sql.options.user, sql.options.database]\n})\n\nt('Result is array', async() =>\n  [true, Array.isArray(await sql`select 1`)]\n)\n\nt('Result has count', async() =>\n  [1, (await sql`select 1`).count]\n)\n\nt('Result has command', async() =>\n  ['SELECT', (await sql`select 1`).command]\n)\n\nt('Create table', async() =>\n  ['CREATE TABLE', (await sql`create table test(int int)`).command, await sql`drop table test`]\n)\n\nt('Drop table', { timeout: 2 }, async() => {\n  await sql`create table test(int int)`\n  return ['DROP TABLE', (await sql`drop table test`).command]\n})\n\nt('null', async() =>\n  [null, (await sql`select ${ null } as x`)[0].x]\n)\n\nt('Integer', async() =>\n  ['1', (await sql`select ${ 1 } as x`)[0].x]\n)\n\nt('String', async() =>\n  ['hello', (await sql`select ${ 'hello' } as x`)[0].x]\n)\n\nt('Boolean false', async() =>\n  [false, (await sql`select ${ false } as x`)[0].x]\n)\n\nt('Boolean true', async() =>\n  [true, (await sql`select ${ true } as x`)[0].x]\n)\n\nt('Date', async() => {\n  const now = new Date()\n  return [0, now - (await sql`select ${ now } as x`)[0].x]\n})\n\nt('Json', async() => {\n  const x = (await sql`select ${ sql.json({ a: 'hello', b: 42 }) } as x`)[0].x\n  return ['hello,42', [x.a, x.b].join()]\n})\n\nt('implicit json', async() => {\n  const x = (await sql`select ${ { a: 'hello', b: 42 } }::json as x`)[0].x\n  return ['hello,42', [x.a, x.b].join()]\n})\n\nt('implicit jsonb', async() => {\n  const x = (await sql`select ${ { a: 'hello', b: 42 } }::jsonb as x`)[0].x\n  return ['hello,42', [x.a, x.b].join()]\n})\n\nt('Empty array', async() =>\n  [true, Array.isArray((await sql`select ${ sql.array([], 1009) } as x`)[0].x)]\n)\n\nt('String array', async() =>\n  ['123', (await sql`select ${ '{1,2,3}' }::int[] as x`)[0].x.join('')]\n)\n\nt('Array of Integer', async() =>\n  ['3', (await sql`select ${ sql.array([1, 2, 3]) } as x`)[0].x[2]]\n)\n\nt('Array of String', async() =>\n  ['c', (await sql`select ${ sql.array(['a', 'b', 'c']) } as x`)[0].x[2]]\n)\n\nt('Array of Date', async() => {\n  const now = new Date()\n  return [now.getTime(), (await sql`select ${ sql.array([now, now, now]) } as x`)[0].x[2].getTime()]\n})\n\nt('Array of Box', async() => [\n  '(3,4),(1,2);(6,7),(4,5)',\n  (await sql`select ${ '{(1,2),(3,4);(4,5),(6,7)}' }::box[] as x`)[0].x.join(';')\n])\n\nt('Nested array n2', async() =>\n  ['4', (await sql`select ${ sql.array([[1, 2], [3, 4]]) } as x`)[0].x[1][1]]\n)\n\nt('Nested array n3', async() =>\n  ['6', (await sql`select ${ sql.array([[[1, 2]], [[3, 4]], [[5, 6]]]) } as x`)[0].x[2][0][1]]\n)\n\nt('Escape in arrays', async() =>\n  ['Hello \"you\",c:\\\\windows', (await sql`select ${ sql.array(['Hello \"you\"', 'c:\\\\windows']) } as x`)[0].x.join(',')]\n)\n\nt('Escapes', async() => {\n  return ['hej\"hej', Object.keys((await sql`select 1 as ${ sql('hej\"hej') }`)[0])[0]]\n})\n\nt('null for int', async() => {\n  await sql`create table test (x int)`\n  return [1, (await sql`insert into test values(${ null })`).count, await sql`drop table test`]\n})\n\nt('Throws on illegal transactions', async() => {\n  const sql = postgres({ ...options, max: 2, fetch_types: false })\n  const error = await sql`begin`.catch(e => e)\n  return [\n    error.code,\n    'UNSAFE_TRANSACTION'\n  ]\n})\n\nt('Transaction throws', async() => {\n  await sql`create table test (a int)`\n  return ['22P02', await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql`insert into test values('hej')`\n  }).catch(x => x.code), await sql`drop table test`]\n})\n\nt('Transaction rolls back', async() => {\n  await sql`create table test (a int)`\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql`insert into test values('hej')`\n  }).catch(() => { /* ignore */ })\n  return [0, (await sql`select a from test`).count, await sql`drop table test`]\n})\n\nt('Transaction throws on uncaught savepoint', async() => {\n  await sql`create table test (a int)`\n\n  return ['fail', (await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoint(async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('fail')\n    })\n  }).catch((err) => err.message)), await sql`drop table test`]\n})\n\nt('Transaction throws on uncaught named savepoint', async() => {\n  await sql`create table test (a int)`\n\n  return ['fail', (await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoit('watpoint', async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('fail')\n    })\n  }).catch(() => 'fail')), await sql`drop table test`]\n})\n\nt('Transaction succeeds on caught savepoint', async() => {\n  await sql`create table test (a int)`\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoint(async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('please rollback')\n    }).catch(() => { /* ignore */ })\n    await sql`insert into test values(3)`\n  })\n\n  return ['2', (await sql`select count(1) from test`)[0].count, await sql`drop table test`]\n})\n\nt('Savepoint returns Result', async() => {\n  let result\n  await sql.begin(async sql => {\n    result = await sql.savepoint(sql =>\n      sql`select 1 as x`\n    )\n  })\n\n  return [1, result[0].x]\n})\n\nt('Prepared transaction', async() => {\n  await sql`create table test (a int)`\n\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.prepare('tx1')\n  })\n\n  await sql`commit prepared 'tx1'`\n\n  return ['1', (await sql`select count(1) from test`)[0].count, await sql`drop table test`]\n})\n\nt('Transaction requests are executed implicitly', async() => {\n  const sql = postgres({ debug: true, idle_timeout: 1, fetch_types: false })\n  return [\n    'testing',\n    (await sql.begin(sql => [\n      sql`select set_config('postgres_js.test', 'testing', true)`,\n      sql`select current_setting('postgres_js.test') as x`\n    ]))[1][0].x\n  ]\n})\n\nt('Uncaught transaction request errors bubbles to transaction', async() => [\n  '42703',\n  (await sql.begin(sql => [\n    sql`select wat`,\n    sql`select current_setting('postgres_js.test') as x, ${ 1 } as a`\n  ]).catch(e => e.code))\n])\n\nt('Fragments in transactions', async() => [\n  true,\n  (await sql.begin(sql => sql`select true as x where ${ sql`1=1` }`))[0].x\n])\n\nt('Transaction rejects with rethrown error', async() => [\n  'WAT',\n  await sql.begin(async sql => {\n    try {\n      await sql`select exception`\n    } catch (ex) {\n      throw new Error('WAT')\n    }\n  }).catch(e => e.message)\n])\n\nt('Parallel transactions', async() => {\n  await sql`create table test (a int)`\n  return ['11', (await Promise.all([\n    sql.begin(sql => sql`select 1`),\n    sql.begin(sql => sql`select 1`)\n  ])).map(x => x.count).join(''), await sql`drop table test`]\n})\n\nt('Many transactions at beginning of connection', async() => {\n  const sql = postgres(options)\n  const xs = await Promise.all(Array.from({ length: 100 }, () => sql.begin(sql => sql`select 1`)))\n  return [100, xs.length]\n})\n\nt('Transactions array', async() => {\n  await sql`create table test (a int)`\n\n  return ['11', (await sql.begin(sql => [\n    sql`select 1`.then(x => x),\n    sql`select 1`\n  ])).map(x => x.count).join(''), await sql`drop table test`]\n})\n\nt('Transaction waits', async() => {\n  await sql`create table test (a int)`\n  await sql.begin(async sql => {\n    await sql`insert into test values(1)`\n    await sql.savepoint(async sql => {\n      await sql`insert into test values(2)`\n      throw new Error('please rollback')\n    }).catch(() => { /* ignore */ })\n    await sql`insert into test values(3)`\n  })\n\n  return ['11', (await Promise.all([\n    sql.begin(sql => sql`select 1`),\n    sql.begin(sql => sql`select 1`)\n  ])).map(x => x.count).join(''), await sql`drop table test`]\n})\n\nt('Helpers in Transaction', async() => {\n  return ['1', (await sql.begin(async sql =>\n    await sql`select ${ sql({ x: 1 }) }`\n  ))[0].x]\n})\n\nt('Undefined values throws', async() => {\n  let error\n\n  await sql`\n    select ${ undefined } as x\n  `.catch(x => error = x.code)\n\n  return ['UNDEFINED_VALUE', error]\n})\n\nt('Transform undefined', async() => {\n  const sql = postgres({ ...options, transform: { undefined: null } })\n  return [null, (await sql`select ${ undefined } as x`)[0].x]\n})\n\nt('Transform undefined in array', async() => {\n  const sql = postgres({ ...options, transform: { undefined: null } })\n  return [null, (await sql`select * from (values ${ sql([undefined, undefined]) }) as x(x, y)`)[0].y]\n})\n\nt('Null sets to null', async() =>\n  [null, (await sql`select ${ null } as x`)[0].x]\n)\n\nt('Throw syntax error', async() =>\n  ['42601', (await sql`wat 1`.catch(x => x)).code]\n)\n\nt('Connect using uri', async() =>\n  [true, await new Promise((resolve, reject) => {\n    const sql = postgres('postgres://' + login.user + ':' + (login.pass || '') + '@localhost:5432/' + options.db, {\n      idle_timeout\n    })\n    sql`select 1`.then(() => resolve(true), reject)\n  })]\n)\n\nt('Options from uri with special characters in user and pass', async() => {\n  const opt = postgres({ user: 'öla', pass: 'pass^word' }).options\n  return [[opt.user, opt.pass].toString(), 'öla,pass^word']\n})\n\nt('Fail with proper error on no host', async() =>\n  ['ECONNREFUSED', (await new Promise((resolve, reject) => {\n    const sql = postgres('postgres://localhost:33333/' + options.db, {\n      idle_timeout\n    })\n    sql`select 1`.then(reject, resolve)\n  })).code]\n)\n\nt('Connect using SSL', async() =>\n  [true, (await new Promise((resolve, reject) => {\n    postgres({\n      ssl: { rejectUnauthorized: false },\n      idle_timeout\n    })`select 1`.then(() => resolve(true), reject)\n  }))]\n)\n\nt('Connect using SSL require', async() =>\n  [true, (await new Promise((resolve, reject) => {\n    postgres({\n      ssl: 'require',\n      idle_timeout\n    })`select 1`.then(() => resolve(true), reject)\n  }))]\n)\n\nt('Connect using SSL direct', async() => {\n  const [{ supported }] = await sql`select current_setting('server_version_num')::int >= 180000 as supported`\n  return [true, !supported || (await new Promise((resolve, reject) => {\n    postgres({\n      ssl: 'require',\n      sslnegotiation: 'direct',\n      idle_timeout\n    })`select 1`.then(() => resolve(true), reject)\n  }))]\n})\n\nt('Connect using SSL prefer', async() => {\n  await exec('psql', ['-c', 'alter system set ssl=off'])\n  await exec('psql', ['-c', 'select pg_reload_conf()'])\n\n  const sql = postgres({\n    ssl: 'prefer',\n    idle_timeout\n  })\n\n  return [\n    1, (await sql`select 1 as x`)[0].x,\n    await exec('psql', ['-c', 'alter system set ssl=on']),\n    await exec('psql', ['-c', 'select pg_reload_conf()'])\n  ]\n})\n\nt('Reconnect using SSL', { timeout: 2 }, async() => {\n  const sql = postgres({\n    ssl: 'require',\n    idle_timeout: 0.1\n  })\n\n  await sql`select 1`\n  await delay(200)\n\n  return [1, (await sql`select 1 as x`)[0].x]\n})\n\nt('Proper handling of non object Errors', async() => {\n  const sql = postgres({ socket: () => { throw 'wat' } }) // eslint-disable-line\n\n  return [\n    'wat', await sql`select 1 as x`.catch(e => e.message)\n  ]\n})\n\nt('Proper handling of null Errors', async() => {\n  const sql = postgres({ socket: () => { throw null } }) // eslint-disable-line\n\n  return [\n    'null', await sql`select 1 as x`.catch(e => e.message)\n  ]\n})\n\nt('Ensure reserve on connection throws proper error', async() => {\n  const sql = postgres({ socket: () => { throw 'wat' }, idle_timeout }) // eslint-disable-line\n\n  return [\n    'wat', await sql.reserve().catch(e => e)\n  ]\n})\n\nt('Login without password', async() => {\n  return [true, (await postgres({ ...options, ...login })`select true as x`)[0].x]\n})\n\nt('Login using MD5', async() => {\n  return [true, (await postgres({ ...options, ...login_md5 })`select true as x`)[0].x]\n})\n\nt('Login using scram-sha-256', async() => {\n  return [true, (await postgres({ ...options, ...login_scram })`select true as x`)[0].x]\n})\n\nt('Parallel connections using scram-sha-256', {\n  timeout: 2\n}, async() => {\n  const sql = postgres({ ...options, ...login_scram })\n  return [true, (await Promise.all([\n    sql`select true as x, pg_sleep(0.01)`,\n    sql`select true as x, pg_sleep(0.01)`,\n    sql`select true as x, pg_sleep(0.01)`\n  ]))[0][0].x]\n})\n\nt('Support dynamic password function', async() => {\n  return [true, (await postgres({\n    ...options,\n    ...login_scram,\n    pass: () => 'postgres_js_test_scram'\n  })`select true as x`)[0].x]\n})\n\nt('Support dynamic async password function', async() => {\n  return [true, (await postgres({\n    ...options,\n    ...login_scram,\n    pass: () => Promise.resolve('postgres_js_test_scram')\n  })`select true as x`)[0].x]\n})\n\nt('Point type', async() => {\n  const sql = postgres({\n    ...options,\n    types: {\n      point: {\n        to: 600,\n        from: [600],\n        serialize: ([x, y]) => '(' + x + ',' + y + ')',\n        parse: (x) => x.slice(1, -1).split(',').map(x => +x)\n      }\n    }\n  })\n\n  await sql`create table test (x point)`\n  await sql`insert into test (x) values (${ sql.types.point([10, 20]) })`\n  return [20, (await sql`select x from test`)[0].x[1], await sql`drop table test`]\n})\n\nt('Point type array', async() => {\n  const sql = postgres({\n    ...options,\n    types: {\n      point: {\n        to: 600,\n        from: [600],\n        serialize: ([x, y]) => '(' + x + ',' + y + ')',\n        parse: (x) => x.slice(1, -1).split(',').map(x => +x)\n      }\n    }\n  })\n\n  await sql`create table test (x point[])`\n  await sql`insert into test (x) values (${ sql.array([sql.types.point([10, 20]), sql.types.point([20, 30])]) })`\n  return [30, (await sql`select x from test`)[0].x[1][1], await sql`drop table test`]\n})\n\nt('sql file', async() =>\n  [1, (await sql.file(rel('select.sql')))[0].x]\n)\n\nt('sql file has forEach', async() => {\n  let result\n  await sql\n    .file(rel('select.sql'), { cache: false })\n    .forEach(({ x }) => result = x)\n\n  return [1, result]\n})\n\nt('sql file throws', async() =>\n  ['ENOENT', (await sql.file(rel('selectomondo.sql')).catch(x => x.code))]\n)\n\nt('sql file cached', async() => {\n  await sql.file(rel('select.sql'))\n  await delay(20)\n\n  return [1, (await sql.file(rel('select.sql')))[0].x]\n})\n\nt('Parameters in file', async() => {\n  const result = await sql.file(\n    rel('select-param.sql'),\n    ['hello']\n  )\n  return ['hello', result[0].x]\n})\n\nt('Connection ended promise', async() => {\n  const sql = postgres(options)\n\n  await sql.end()\n\n  return [undefined, await sql.end()]\n})\n\nt('Connection ended timeout', async() => {\n  const sql = postgres(options)\n\n  await sql.end({ timeout: 10 })\n\n  return [undefined, await sql.end()]\n})\n\nt('Connection ended error', async() => {\n  const sql = postgres(options)\n  await sql.end()\n  return ['CONNECTION_ENDED', (await sql``.catch(x => x.code))]\n})\n\nt('Connection end does not cancel query', async() => {\n  const sql = postgres(options)\n\n  const promise = sql`select 1 as x`.execute()\n\n  await sql.end()\n\n  return [1, (await promise)[0].x]\n})\n\nt('Connection destroyed', async() => {\n  const sql = postgres(options)\n  process.nextTick(() => sql.end({ timeout: 0 }))\n  return ['CONNECTION_DESTROYED', await sql``.catch(x => x.code)]\n})\n\nt('Connection destroyed with query before', async() => {\n  const sql = postgres(options)\n      , error = sql`select pg_sleep(0.2)`.catch(err => err.code)\n\n  sql.end({ timeout: 0 })\n  return ['CONNECTION_DESTROYED', await error]\n})\n\nt('transform column', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: x => x.split('').reverse().join('') }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['dlrow_olleh', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('column toPascal', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: postgres.toPascal }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['HelloWorld', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('column toCamel', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: postgres.toCamel }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['helloWorld', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('column toKebab', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { column: postgres.toKebab }\n  })\n\n  await sql`create table test (hello_world int)`\n  await sql`insert into test values (1)`\n  return ['hello-world', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]\n})\n\nt('Transform nested json in arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  return ['aBcD', (await sql`select '[{\"a_b\":1},{\"c_d\":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')]\n})\n\nt('Transform deeply nested json object in arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  return [\n    'childObj_deeplyNestedObj_grandchildObj',\n    (await sql`\n      select '[{\"nested_obj\": {\"child_obj\": 2, \"deeply_nested_obj\": {\"grandchild_obj\": 3}}}]'::jsonb as x\n    `)[0].x.map(x => {\n      let result\n      for (const key in x)\n        result = [...Object.keys(x[key]), ...Object.keys(x[key].deeplyNestedObj)]\n      return result\n    })[0]\n    .join('_')\n  ]\n})\n\nt('Transform deeply nested json array in arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  return [\n    'childArray_deeplyNestedArray_grandchildArray',\n    (await sql`\n      select '[{\"nested_array\": [{\"child_array\": 2, \"deeply_nested_array\": [{\"grandchild_array\":3}]}]}]'::jsonb AS x\n    `)[0].x.map((x) => {\n      let result\n      for (const key in x)\n        result = [...Object.keys(x[key][0]), ...Object.keys(x[key][0].deeplyNestedArray[0])]\n      return result\n    })[0]\n    .join('_')\n  ]\n})\n\nt('Bypass transform for json primitive', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n\n  const x = (\n    await sql`select 'null'::json as a, 'false'::json as b, '\"a\"'::json as c, '1'::json as d`\n  )[0]\n\n  return [\n    JSON.stringify({ a: null, b: false, c: 'a', d: 1 }),\n    JSON.stringify(x)\n  ]\n})\n\nt('Bypass transform for jsonb primitive', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n\n  const x = (\n    await sql`select 'null'::jsonb as a, 'false'::jsonb as b, '\"a\"'::jsonb as c, '1'::jsonb as d`\n  )[0]\n\n  return [\n    JSON.stringify({ a: null, b: false, c: 'a', d: 1 }),\n    JSON.stringify(x)\n  ]\n})\n\nt('unsafe', async() => {\n  await sql`create table test (x int)`\n  return [1, (await sql.unsafe('insert into test values ($1) returning *', [1]))[0].x, await sql`drop table test`]\n})\n\nt('unsafe simple', async() => {\n  return [1, (await sql.unsafe('select 1 as x'))[0].x]\n})\n\nt('unsafe simple includes columns', async() => {\n  return ['x', (await sql.unsafe('select 1 as x').values()).columns[0].name]\n})\n\nt('unsafe describe', async() => {\n  const q = 'insert into test values (1)'\n  await sql`create table test(a int unique)`\n  await sql.unsafe(q).describe()\n  const x = await sql.unsafe(q).describe()\n  return [\n    q,\n    x.string,\n    await sql`drop table test`\n  ]\n})\n\nt('simple query using unsafe with multiple statements', async() => {\n  return [\n    '1,2',\n    (await sql.unsafe('select 1 as x;select 2 as x')).map(x => x[0].x).join()\n  ]\n})\n\nt('simple query using simple() with multiple statements', async() => {\n  return [\n    '1,2',\n    (await sql`select 1 as x;select 2 as x`.simple()).map(x => x[0].x).join()\n  ]\n})\n\nt('listen and notify', async() => {\n  const sql = postgres(options)\n  const channel = 'hello'\n  const result = await new Promise(async r => {\n    await sql.listen(channel, r)\n    sql.notify(channel, 'works')\n  })\n\n  return [\n    'works',\n    result,\n    sql.end()\n  ]\n})\n\nt('double listen', async() => {\n  const sql = postgres(options)\n      , channel = 'hello'\n\n  let count = 0\n\n  await new Promise((resolve, reject) =>\n    sql.listen(channel, resolve)\n    .then(() => sql.notify(channel, 'world'))\n    .catch(reject)\n  ).then(() => count++)\n\n  await new Promise((resolve, reject) =>\n    sql.listen(channel, resolve)\n    .then(() => sql.notify(channel, 'world'))\n    .catch(reject)\n  ).then(() => count++)\n\n  // for coverage\n  sql.listen('weee', () => { /* noop */ }).then(sql.end)\n\n  return [2, count]\n})\n\nt('multiple listeners work after a reconnect', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const s1 = await sql.listen('test', x => xs.push('1', x))\n  await sql.listen('test', x => xs.push('2', x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await sql`select pg_terminate_backend(${ s1.state.pid })`\n  await delay(200)\n  await sql.notify('test', 'b')\n  await delay(50)\n  sql.end()\n\n  return ['1a2a1b2b', xs.join('')]\n})\n\nt('listen and notify with weird name', async() => {\n  const sql = postgres(options)\n  const channel = 'wat-;.ø.§'\n  const result = await new Promise(async r => {\n    const { unlisten } = await sql.listen(channel, r)\n    sql.notify(channel, 'works')\n    await delay(50)\n    await unlisten()\n  })\n\n  return [\n    'works',\n    result,\n    sql.end()\n  ]\n})\n\nt('listen and notify with upper case', async() => {\n  const sql = postgres(options)\n  const channel = 'withUpperChar'\n  const result = await new Promise(async r => {\n    await sql.listen(channel, r)\n    sql.notify(channel, 'works')\n  })\n\n  return [\n    'works',\n    result,\n    sql.end()\n  ]\n})\n\nt('listen reconnects', { timeout: 2 }, async() => {\n  const sql = postgres(options)\n      , resolvers = {}\n      , a = new Promise(r => resolvers.a = r)\n      , b = new Promise(r => resolvers.b = r)\n\n  let connects = 0\n\n  const { state: { pid } } = await sql.listen(\n    'test',\n    x => x in resolvers && resolvers[x](),\n    () => connects++\n  )\n  await sql.notify('test', 'a')\n  await a\n  await sql`select pg_terminate_backend(${ pid })`\n  await delay(100)\n  await sql.notify('test', 'b')\n  await b\n  sql.end()\n  return [connects, 2]\n})\n\nt('listen result reports correct connection state after reconnection', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const result = await sql.listen('test', x => xs.push(x))\n  const initialPid = result.state.pid\n  await sql.notify('test', 'a')\n  await sql`select pg_terminate_backend(${ initialPid })`\n  await delay(50)\n  sql.end()\n\n  return [result.state.pid !== initialPid, true]\n})\n\nt('unlisten removes subscription', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const { unlisten } = await sql.listen('test', x => xs.push(x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await unlisten()\n  await sql.notify('test', 'b')\n  await delay(50)\n  sql.end()\n\n  return ['a', xs.join('')]\n})\n\nt('listen after unlisten', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  const { unlisten } = await sql.listen('test', x => xs.push(x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await unlisten()\n  await sql.notify('test', 'b')\n  await delay(50)\n  await sql.listen('test', x => xs.push(x))\n  await sql.notify('test', 'c')\n  await delay(50)\n  sql.end()\n\n  return ['ac', xs.join('')]\n})\n\nt('multiple listeners and unlisten one', async() => {\n  const sql = postgres(options)\n      , xs = []\n\n  await sql.listen('test', x => xs.push('1', x))\n  const s2 = await sql.listen('test', x => xs.push('2', x))\n  await sql.notify('test', 'a')\n  await delay(50)\n  await s2.unlisten()\n  await sql.notify('test', 'b')\n  await delay(50)\n  sql.end()\n\n  return ['1a2a1b', xs.join('')]\n})\n\nt('responds with server parameters (application_name)', async() =>\n  ['postgres.js', await new Promise((resolve, reject) => postgres({\n    ...options,\n    onparameter: (k, v) => k === 'application_name' && resolve(v)\n  })`select 1`.catch(reject))]\n)\n\nt('has server parameters', async() => {\n  return ['postgres.js', (await sql`select 1`.then(() => sql.parameters.application_name))]\n})\n\nt('big query body', { timeout: 2 }, async() => {\n  await sql`create table test (x int)`\n  return [50000, (await sql`insert into test ${\n    sql([...Array(50000).keys()].map(x => ({ x })))\n  }`).count, await sql`drop table test`]\n})\n\nt('Throws if more than 65534 parameters', async() => {\n  await sql`create table test (x int)`\n  return ['MAX_PARAMETERS_EXCEEDED', (await sql`insert into test ${\n    sql([...Array(65535).keys()].map(x => ({ x })))\n  }`.catch(e => e.code)), await sql`drop table test`]\n})\n\nt('let postgres do implicit cast of unknown types', async() => {\n  await sql`create table test (x timestamp with time zone)`\n  const [{ x }] = await sql`insert into test values (${ new Date().toISOString() }) returning *`\n  return [true, x instanceof Date, await sql`drop table test`]\n})\n\nt('only allows one statement', async() =>\n  ['42601', await sql`select 1; select 2`.catch(e => e.code)]\n)\n\nt('await sql() throws not tagged error', async() => {\n  let error\n  try {\n    await sql('select 1')\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('sql().then throws not tagged error', async() => {\n  let error\n  try {\n    sql('select 1').then(() => { /* noop */ })\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('sql().catch throws not tagged error', async() => {\n  let error\n  try {\n    await sql('select 1')\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('sql().finally throws not tagged error', async() => {\n  let error\n  try {\n    sql('select 1').finally(() => { /* noop */ })\n  } catch (e) {\n    error = e.code\n  }\n  return ['NOT_TAGGED_CALL', error]\n})\n\nt('little bobby tables', async() => {\n  const name = 'Robert\\'); DROP TABLE students;--'\n\n  await sql`create table students (name text, age int)`\n  await sql`insert into students (name) values (${ name })`\n\n  return [\n    name, (await sql`select name from students`)[0].name,\n    await sql`drop table students`\n  ]\n})\n\nt('Connection errors are caught using begin()', {\n  timeout: 2\n}, async() => {\n  let error\n  try {\n    const sql = postgres({ host: 'localhost', port: 1 })\n\n    await sql.begin(async(sql) => {\n      await sql`insert into test (label, value) values (${1}, ${2})`\n    })\n  } catch (err) {\n    error = err\n  }\n\n  return [\n    true,\n    error.code === 'ECONNREFUSED' ||\n    error.message === 'Connection refused (os error 61)'\n  ]\n})\n\nt('dynamic table name', async() => {\n  await sql`create table test(a int)`\n  return [\n    0, (await sql`select * from ${ sql('test') }`).count,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic schema name', async() => {\n  await sql`create table test(a int)`\n  return [\n    0, (await sql`select * from ${ sql('public') }.test`).count,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic schema and table name', async() => {\n  await sql`create table test(a int)`\n  return [\n    0, (await sql`select * from ${ sql('public.test') }`).count,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic column name', async() => {\n  return ['!not_valid', Object.keys((await sql`select 1 as ${ sql('!not_valid') }`)[0])[0]]\n})\n\nt('dynamic select as', async() => {\n  return ['2', (await sql`select ${ sql({ a: 1, b: 2 }) }`)[0].b]\n})\n\nt('dynamic select as pluck', async() => {\n  return [undefined, (await sql`select ${ sql({ a: 1, b: 2 }, 'a') }`)[0].b]\n})\n\nt('dynamic insert', async() => {\n  await sql`create table test (a int, b text)`\n  const x = { a: 42, b: 'the answer' }\n\n  return ['the answer', (await sql`insert into test ${ sql(x) } returning *`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic insert pluck', async() => {\n  await sql`create table test (a int, b text)`\n  const x = { a: 42, b: 'the answer' }\n\n  return [null, (await sql`insert into test ${ sql(x, 'a') } returning *`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic in with empty array', async() => {\n  await sql`create table test (a int)`\n  await sql`insert into test values (1)`\n  return [\n    (await sql`select * from test where null in ${ sql([]) }`).count,\n    0,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic in after insert', async() => {\n  await sql`create table test (a int, b text)`\n  const [{ x }] = await sql`\n    with x as (\n      insert into test values (1, 'hej')\n      returning *\n    )\n    select 1 in ${ sql([1, 2, 3]) } as x from x\n  `\n  return [\n    true, x,\n    await sql`drop table test`\n  ]\n})\n\nt('array insert', async() => {\n  await sql`create table test (a int, b int)`\n  return [2, (await sql`insert into test (a, b) values ${ sql([1, 2]) } returning *`)[0].b, await sql`drop table test`]\n})\n\nt('where parameters in()', async() => {\n  await sql`create table test (x text)`\n  await sql`insert into test values ('a')`\n  return [\n    (await sql`select * from test where x in ${ sql(['a', 'b', 'c']) }`)[0].x,\n    'a',\n    await sql`drop table test`\n  ]\n})\n\nt('where parameters in() values before', async() => {\n  return [2, (await sql`\n    with rows as (\n      select * from (values (1), (2), (3), (4)) as x(a)\n    )\n    select * from rows where a in ${ sql([3, 4]) }\n  `).count]\n})\n\nt('dynamic multi row insert', async() => {\n  await sql`create table test (a int, b text)`\n  const x = { a: 42, b: 'the answer' }\n\n  return [\n    'the answer',\n    (await sql`insert into test ${ sql([x, x]) } returning *`)[1].b, await sql`drop table test`\n  ]\n})\n\nt('dynamic update', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (17, 'wrong')`\n\n  return [\n    'the answer',\n    (await sql`update test set ${ sql({ a: 42, b: 'the answer' }) } returning *`)[0].b, await sql`drop table test`\n  ]\n})\n\nt('dynamic update pluck', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (17, 'wrong')`\n\n  return [\n    'wrong',\n    (await sql`update test set ${ sql({ a: 42, b: 'the answer' }, 'a') } returning *`)[0].b, await sql`drop table test`\n  ]\n})\n\nt('dynamic select array', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (42, 'yay')`\n  return ['yay', (await sql`select ${ sql(['a', 'b']) } from test`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic returning array', async() => {\n  await sql`create table test (a int, b text)`\n  return [\n    'yay',\n    (await sql`insert into test (a, b) values (42, 'yay') returning ${ sql(['a', 'b']) }`)[0].b,\n    await sql`drop table test`\n  ]\n})\n\nt('dynamic select args', async() => {\n  await sql`create table test (a int, b text)`\n  await sql`insert into test (a, b) values (42, 'yay')`\n  return ['yay', (await sql`select ${ sql('a', 'b') } from test`)[0].b, await sql`drop table test`]\n})\n\nt('dynamic values single row', async() => {\n  const [{ b }] = await sql`\n    select * from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)\n  `\n\n  return ['b', b]\n})\n\nt('dynamic values multi row', async() => {\n  const [, { b }] = await sql`\n    select * from (values ${ sql([['a', 'b', 'c'], ['a', 'b', 'c']]) }) as x(a, b, c)\n  `\n\n  return ['b', b]\n})\n\nt('connection parameters', async() => {\n  const sql = postgres({\n    ...options,\n    connection: {\n      'some.var': 'yay'\n    }\n  })\n\n  return ['yay', (await sql`select current_setting('some.var') as x`)[0].x]\n})\n\nt('Multiple queries', async() => {\n  const sql = postgres(options)\n\n  return [4, (await Promise.all([\n    sql`select 1`,\n    sql`select 2`,\n    sql`select 3`,\n    sql`select 4`\n  ])).length]\n})\n\nt('Multiple statements', async() =>\n  [2, await sql.unsafe(`\n    select 1 as x;\n    select 2 as a;\n  `).then(([, [x]]) => x.a)]\n)\n\nt('throws correct error when authentication fails', async() => {\n  const sql = postgres({\n    ...options,\n    ...login_md5,\n    pass: 'wrong'\n  })\n  return ['28P01', await sql`select 1`.catch(e => e.code)]\n})\n\nt('notice', async() => {\n  let notice\n  const log = console.log // eslint-disable-line\n  console.log = function(x) { // eslint-disable-line\n    notice = x\n  }\n\n  const sql = postgres(options)\n\n  await sql`create table if not exists users()`\n  await sql`create table if not exists users()`\n\n  console.log = log // eslint-disable-line\n\n  return ['NOTICE', notice.severity]\n})\n\nt('notice hook', async() => {\n  let notice\n  const sql = postgres({\n    ...options,\n    onnotice: x => notice = x\n  })\n\n  await sql`create table if not exists users()`\n  await sql`create table if not exists users()`\n\n  return ['NOTICE', notice.severity]\n})\n\nt('bytea serializes and parses', async() => {\n  const buf = Buffer.from('wat')\n\n  await sql`create table test (x bytea)`\n  await sql`insert into test values (${ buf })`\n\n  return [\n    buf.toString(),\n    (await sql`select x from test`)[0].x.toString(),\n    await sql`drop table test`\n  ]\n})\n\nt('forEach', async() => {\n  let result\n  await sql`select 1 as x`.forEach(({ x }) => result = x)\n  return [1, result]\n})\n\nt('forEach returns empty array', async() => {\n  return [0, (await sql`select 1 as x`.forEach(() => { /* noop */ })).length]\n})\n\nt('Cursor', async() => {\n  const order = []\n  await sql`select 1 as x union select 2 as x`.cursor(async([x]) => {\n    order.push(x.x + 'a')\n    await delay(100)\n    order.push(x.x + 'b')\n  })\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Unsafe cursor', async() => {\n  const order = []\n  await sql.unsafe('select 1 as x union select 2 as x').cursor(async([x]) => {\n    order.push(x.x + 'a')\n    await delay(100)\n    order.push(x.x + 'b')\n  })\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Cursor custom n', async() => {\n  const order = []\n  await sql`select * from generate_series(1,20)`.cursor(10, async(x) => {\n    order.push(x.length)\n  })\n  return ['10,10', order.join(',')]\n})\n\nt('Cursor custom with rest n', async() => {\n  const order = []\n  await sql`select * from generate_series(1,20)`.cursor(11, async(x) => {\n    order.push(x.length)\n  })\n  return ['11,9', order.join(',')]\n})\n\nt('Cursor custom with less results than batch size', async() => {\n  const order = []\n  await sql`select * from generate_series(1,20)`.cursor(21, async(x) => {\n    order.push(x.length)\n  })\n  return ['20', order.join(',')]\n})\n\nt('Cursor cancel', async() => {\n  let result\n  await sql`select * from generate_series(1,10) as x`.cursor(async([{ x }]) => {\n    result = x\n    return sql.CLOSE\n  })\n  return [1, result]\n})\n\nt('Cursor throw', async() => {\n  const order = []\n  await sql`select 1 as x union select 2 as x`.cursor(async([x]) => {\n    order.push(x.x + 'a')\n    await delay(100)\n    throw new Error('watty')\n  }).catch(() => order.push('err'))\n  return ['1aerr', order.join('')]\n})\n\nt('Cursor error', async() => [\n  '42601',\n  await sql`wat`.cursor(() => { /* noop */ }).catch((err) => err.code)\n])\n\nt('Multiple Cursors', { timeout: 2 }, async() => {\n  const result = []\n  await sql.begin(async sql => [\n    await sql`select 1 as cursor, x from generate_series(1,4) as x`.cursor(async([row]) => {\n      result.push(row.x)\n      await new Promise(r => setTimeout(r, 20))\n    }),\n    await sql`select 2 as cursor, x from generate_series(101,104) as x`.cursor(async([row]) => {\n      result.push(row.x)\n      await new Promise(r => setTimeout(r, 10))\n    })\n  ])\n\n  return ['1,2,3,4,101,102,103,104', result.join(',')]\n})\n\nt('Cursor as async iterator', async() => {\n  const order = []\n  for await (const [x] of sql`select generate_series(1,2) as x;`.cursor()) {\n    order.push(x.x + 'a')\n    await delay(10)\n    order.push(x.x + 'b')\n  }\n\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Cursor as async iterator with break', async() => {\n  const order = []\n  for await (const xs of sql`select generate_series(1,2) as x;`.cursor()) {\n    order.push(xs[0].x + 'a')\n    await delay(10)\n    order.push(xs[0].x + 'b')\n    break\n  }\n\n  return ['1a1b', order.join('')]\n})\n\nt('Async Iterator Unsafe cursor', async() => {\n  const order = []\n  for await (const [x] of sql.unsafe('select 1 as x union select 2 as x').cursor()) {\n    order.push(x.x + 'a')\n    await delay(10)\n    order.push(x.x + 'b')\n  }\n  return ['1a1b2a2b', order.join('')]\n})\n\nt('Async Iterator Cursor custom n', async() => {\n  const order = []\n  for await (const x of sql`select * from generate_series(1,20)`.cursor(10))\n    order.push(x.length)\n\n  return ['10,10', order.join(',')]\n})\n\nt('Async Iterator Cursor custom with rest n', async() => {\n  const order = []\n  for await (const x of sql`select * from generate_series(1,20)`.cursor(11))\n    order.push(x.length)\n\n  return ['11,9', order.join(',')]\n})\n\nt('Async Iterator Cursor custom with less results than batch size', async() => {\n  const order = []\n  for await (const x of sql`select * from generate_series(1,20)`.cursor(21))\n    order.push(x.length)\n  return ['20', order.join(',')]\n})\n\nt('Transform row', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { row: () => 1 }\n  })\n\n  return [1, (await sql`select 'wat'`)[0]]\n})\n\nt('Transform row forEach', async() => {\n  let result\n  const sql = postgres({\n    ...options,\n    transform: { row: () => 1 }\n  })\n\n  await sql`select 1`.forEach(x => result = x)\n\n  return [1, result]\n})\n\nt('Transform value', async() => {\n  const sql = postgres({\n    ...options,\n    transform: { value: () => 1 }\n  })\n\n  return [1, (await sql`select 'wat' as x`)[0].x]\n})\n\nt('Transform columns from', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.fromCamel\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`\n  await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`\n  return [\n    2,\n    (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].a_test,\n    await sql`drop table test`\n  ]\n})\n\nt('Transform columns to', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.toCamel\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ a_test: 1, b_test: 1 }]) }`\n  await sql`update test set ${ sql({ a_test: 2, b_test: 2 }) }`\n  return [\n    2,\n    (await sql`select a_test, b_test from test`)[0].aTest,\n    await sql`drop table test`\n  ]\n})\n\nt('Transform columns from and to', async() => {\n  const sql = postgres({\n    ...options,\n    transform: postgres.camel\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`\n  await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`\n  return [\n    2,\n    (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].aTest,\n    await sql`drop table test`\n  ]\n})\n\nt('Transform columns from and to (legacy)', async() => {\n  const sql = postgres({\n    ...options,\n    transform: {\n      column: {\n        to: postgres.fromCamel,\n        from: postgres.toCamel\n      }\n    }\n  })\n  await sql`create table test (a_test int, b_test text)`\n  await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`\n  await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`\n  return [\n    2,\n    (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].aTest,\n    await sql`drop table test`\n  ]\n})\n\nt('Unix socket', async() => {\n  const sql = postgres({\n    ...options,\n    host: process.env.PGSOCKET || '/tmp' // eslint-disable-line\n  })\n\n  return [1, (await sql`select 1 as x`)[0].x]\n})\n\nt('Big result', async() => {\n  return [100000, (await sql`select * from generate_series(1, 100000)`).count]\n})\n\nt('Debug', async() => {\n  let result\n  const sql = postgres({\n    ...options,\n    debug: (connection_id, str) => result = str\n  })\n\n  await sql`select 1`\n\n  return ['select 1', result]\n})\n\nt('bigint is returned as String', async() => [\n  'string',\n  typeof (await sql`select 9223372036854777 as x`)[0].x\n])\n\nt('int is returned as Number', async() => [\n  'number',\n  typeof (await sql`select 123 as x`)[0].x\n])\n\nt('numeric is returned as string', async() => [\n  'string',\n  typeof (await sql`select 1.2 as x`)[0].x\n])\n\nt('Async stack trace', async() => {\n  const sql = postgres({ ...options, debug: false })\n  return [\n    parseInt(new Error().stack.split('\\n')[1].match(':([0-9]+):')[1]) + 1,\n    parseInt(await sql`error`.catch(x => x.stack.split('\\n').pop().match(':([0-9]+):')[1]))\n  ]\n})\n\nt('Debug has long async stack trace', async() => {\n  const sql = postgres({ ...options, debug: true })\n\n  return [\n    'watyo',\n    await yo().catch(x => x.stack.match(/wat|yo/g).join(''))\n  ]\n\n  function yo() {\n    return wat()\n  }\n\n  function wat() {\n    return sql`error`\n  }\n})\n\nt('Error contains query string', async() => [\n  'selec 1',\n  (await sql`selec 1`.catch(err => err.query))\n])\n\nt('Error contains query serialized parameters', async() => [\n  1,\n  (await sql`selec ${ 1 }`.catch(err => err.parameters[0]))\n])\n\nt('Error contains query raw parameters', async() => [\n  1,\n  (await sql`selec ${ 1 }`.catch(err => err.args[0]))\n])\n\nt('Query and parameters on errorare not enumerable if debug is not set', async() => {\n  const sql = postgres({ ...options, debug: false })\n\n  return [\n    false,\n    (await sql`selec ${ 1 }`.catch(err => err.propertyIsEnumerable('parameters') || err.propertyIsEnumerable('query')))\n  ]\n})\n\nt('Query and parameters are enumerable if debug is set', async() => {\n  const sql = postgres({ ...options, debug: true })\n\n  return [\n    true,\n    (await sql`selec ${ 1 }`.catch(err => err.propertyIsEnumerable('parameters') && err.propertyIsEnumerable('query')))\n  ]\n})\n\nt('connect_timeout', { timeout: 20 }, async() => {\n  const connect_timeout = 0.2\n  const server = net.createServer()\n  server.listen()\n  const sql = postgres({ port: server.address().port, host: '127.0.0.1', connect_timeout })\n  const start = Date.now()\n  let end\n  await sql`select 1`.catch((e) => {\n    if (e.code !== 'CONNECT_TIMEOUT')\n      throw e\n    end = Date.now()\n  })\n  server.close()\n  return [connect_timeout, Math.floor((end - start) / 100) / 10]\n})\n\nt('connect_timeout throws proper error', async() => [\n  'CONNECT_TIMEOUT',\n  await postgres({\n    ...options,\n    ...login_scram,\n    connect_timeout: 0.001\n  })`select 1`.catch(e => e.code)\n])\n\nt('connect_timeout error message includes host:port', { timeout: 20 }, async() => {\n  const connect_timeout = 0.2\n  const server = net.createServer()\n  server.listen()\n  const sql = postgres({ port: server.address().port, host: '127.0.0.1', connect_timeout })\n  const port = server.address().port\n  let err\n  await sql`select 1`.catch((e) => {\n    if (e.code !== 'CONNECT_TIMEOUT')\n      throw e\n    err = e.message\n  })\n  server.close()\n  return [['write CONNECT_TIMEOUT 127.0.0.1:', port].join(''), err]\n})\n\nt('requests works after single connect_timeout', async() => {\n  let first = true\n\n  const sql = postgres({\n    ...options,\n    ...login_scram,\n    connect_timeout: { valueOf() { return first ? (first = false, 0.0001) : 1 } }\n  })\n\n  return [\n    'CONNECT_TIMEOUT,,1',\n    [\n      await sql`select 1 as x`.then(() => 'success', x => x.code),\n      await delay(10),\n      (await sql`select 1 as x`)[0].x\n    ].join(',')\n  ]\n})\n\nt('Postgres errors are of type PostgresError', async() =>\n  [true, (await sql`bad keyword`.catch(e => e)) instanceof sql.PostgresError]\n)\n\nt('Result has columns spec', async() =>\n  ['x', (await sql`select 1 as x`).columns[0].name]\n)\n\nt('forEach has result as second argument', async() => {\n  let x\n  await sql`select 1 as x`.forEach((_, result) => x = result)\n  return ['x', x.columns[0].name]\n})\n\nt('Result as arrays', async() => {\n  const sql = postgres({\n    ...options,\n    transform: {\n      row: x => Object.values(x)\n    }\n  })\n\n  return ['1,2', (await sql`select 1 as a, 2 as b`)[0].join(',')]\n})\n\nt('Insert empty array', async() => {\n  await sql`create table tester (ints int[])`\n  return [\n    Array.isArray((await sql`insert into tester (ints) values (${ sql.array([]) }) returning *`)[0].ints),\n    true,\n    await sql`drop table tester`\n  ]\n})\n\nt('Insert array in sql()', async() => {\n  await sql`create table tester (ints int[])`\n  return [\n    Array.isArray((await sql`insert into tester ${ sql({ ints: sql.array([]) }) } returning *`)[0].ints),\n    true,\n    await sql`drop table tester`\n  ]\n})\n\nt('Automatically creates prepared statements', async() => {\n  const sql = postgres(options)\n  const result = await sql`select * from pg_prepared_statements`\n  return [true, result.some(x => x.name = result.statement.name)]\n})\n\nt('no_prepare: true disables prepared statements (deprecated)', async() => {\n  const sql = postgres({ ...options, no_prepare: true })\n  const result = await sql`select * from pg_prepared_statements`\n  return [false, result.some(x => x.name = result.statement.name)]\n})\n\nt('prepare: false disables prepared statements', async() => {\n  const sql = postgres({ ...options, prepare: false })\n  const result = await sql`select * from pg_prepared_statements`\n  return [false, result.some(x => x.name = result.statement.name)]\n})\n\nt('prepare: true enables prepared statements', async() => {\n  const sql = postgres({ ...options, prepare: true })\n  const result = await sql`select * from pg_prepared_statements`\n  return [true, result.some(x => x.name = result.statement.name)]\n})\n\nt('prepares unsafe query when \"prepare\" option is true', async() => {\n  const sql = postgres({ ...options, prepare: true })\n  const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ['bla'], { prepare: true })\n  return [true, result.some(x => x.name = result.statement.name)]\n})\n\nt('does not prepare unsafe query by default', async() => {\n  const sql = postgres({ ...options, prepare: true })\n  const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ['bla'])\n  return [false, result.some(x => x.name = result.statement.name)]\n})\n\nt('Recreate prepared statements on transformAssignedExpr error', { timeout: 1 }, async() => {\n  const insert = () => sql`insert into test (name) values (${ '1' }) returning name`\n  await sql`create table test (name text)`\n  await insert()\n  await sql`alter table test alter column name type int using name::integer`\n  return [\n    1,\n    (await insert())[0].name,\n    await sql`drop table test`\n  ]\n})\n\nt('Throws correct error when retrying in transactions', async() => {\n  await sql`create table test(x int)`\n  const error = await sql.begin(sql => sql`insert into test (x) values (${ false })`).catch(e => e)\n  return [\n    error.code,\n    '42804',\n    sql`drop table test`\n  ]\n})\n\nt('Recreate prepared statements on RevalidateCachedQuery error', async() => {\n  const select = () => sql`select name from test`\n  await sql`create table test (name text)`\n  await sql`insert into test values ('1')`\n  await select()\n  await sql`alter table test alter column name type int using name::integer`\n  return [\n    1,\n    (await select())[0].name,\n    await sql`drop table test`\n  ]\n})\n\nt('Properly throws routine error on not prepared statements', async() => {\n  await sql`create table x (x text[])`\n  const { routine } = await sql.unsafe(`\n    insert into x(x) values (('a', 'b'))\n  `).catch(e => e)\n\n  return ['transformAssignedExpr', routine, await sql`drop table x`]\n})\n\nt('Properly throws routine error on not prepared statements in transaction', async() => {\n  const { routine } = await sql.begin(sql => [\n    sql`create table x (x text[])`,\n    sql`insert into x(x) values (('a', 'b'))`\n  ]).catch(e => e)\n\n  return ['transformAssignedExpr', routine]\n})\n\nt('Properly throws routine error on not prepared statements using file', async() => {\n  const { routine } = await sql.unsafe(`\n    create table x (x text[]);\n    insert into x(x) values (('a', 'b'));\n  `, { prepare: true }).catch(e => e)\n\n  return ['transformAssignedExpr', routine]\n})\n\nt('Catches connection config errors', async() => {\n  const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })\n\n  return [\n    'wat',\n    await sql`select 1`.catch((e) => e.message)\n  ]\n})\n\nt('Catches connection config errors with end', async() => {\n  const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })\n\n  return [\n    'wat',\n    await sql`select 1`.catch((e) => e.message),\n    await sql.end()\n  ]\n})\n\nt('Catches query format errors', async() => [\n  'wat',\n  await sql.unsafe({ toString: () => { throw new Error('wat') } }).catch((e) => e.message)\n])\n\nt('Multiple hosts', {\n  timeout: 1\n}, async() => {\n  const s1 = postgres({ idle_timeout })\n      , s2 = postgres({ idle_timeout, port: 5433 })\n      , sql = postgres('postgres://localhost:5432,localhost:5433', { idle_timeout, max: 1 })\n      , result = []\n\n  const id1 = (await s1`select system_identifier as x from pg_control_system()`)[0].x\n  const id2 = (await s2`select system_identifier as x from pg_control_system()`)[0].x\n\n  const x1 = await sql`select 1`\n  result.push((await sql`select system_identifier as x from pg_control_system()`)[0].x)\n  await s1`select pg_terminate_backend(${ x1.state.pid }::int)`\n  await delay(50)\n\n  const x2 = await sql`select 1`\n  result.push((await sql`select system_identifier as x from pg_control_system()`)[0].x)\n  await s2`select pg_terminate_backend(${ x2.state.pid }::int)`\n  await delay(50)\n\n  result.push((await sql`select system_identifier as x from pg_control_system()`)[0].x)\n\n  return [[id1, id2, id1].join(','), result.join(',')]\n})\n\nt('Escaping supports schemas and tables', async() => {\n  await sql`create schema a`\n  await sql`create table a.b (c int)`\n  await sql`insert into a.b (c) values (1)`\n  return [\n    1,\n    (await sql`select ${ sql('a.b.c') } from a.b`)[0].c,\n    await sql`drop table a.b`,\n    await sql`drop schema a`\n  ]\n})\n\nt('Raw method returns rows as arrays', async() => {\n  const [x] = await sql`select 1`.raw()\n  return [\n    Array.isArray(x),\n    true\n  ]\n})\n\nt('Raw method returns values unparsed as Buffer', async() => {\n  const [[x]] = await sql`select 1`.raw()\n  return [\n    x instanceof Uint8Array,\n    true\n  ]\n})\n\nt('Array returns rows as arrays of columns', async() => {\n  return [(await sql`select 1`.values())[0][0], 1]\n})\n\nt('Copy read', async() => {\n  const result = []\n\n  await sql`create table test (x int)`\n  await sql`insert into test select * from generate_series(1,10)`\n  const readable = await sql`copy test to stdout`.readable()\n  readable.on('data', x => result.push(x))\n  await new Promise(r => readable.on('end', r))\n\n  return [\n    result.length,\n    10,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy write', { timeout: 2 }, async() => {\n  await sql`create table test (x int)`\n  const writable = await sql`copy test from stdin`.writable()\n\n  writable.write('1\\n')\n  writable.write('1\\n')\n  writable.end()\n\n  await new Promise(r => writable.on('finish', r))\n\n  return [\n    (await sql`select 1 from test`).length,\n    2,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy write as first', async() => {\n  await sql`create table test (x int)`\n  const first = postgres(options)\n  const writable = await first`COPY test FROM STDIN WITH(FORMAT csv, HEADER false, DELIMITER ',')`.writable()\n  writable.write('1\\n')\n  writable.write('1\\n')\n  writable.end()\n\n  await new Promise(r => writable.on('finish', r))\n\n  return [\n    (await sql`select 1 from test`).length,\n    2,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy from file', async() => {\n  await sql`create table test (x int, y int, z int)`\n  await new Promise(async r => fs\n    .createReadStream(rel('copy.csv'))\n    .pipe(await sql`copy test from stdin`.writable())\n    .on('finish', r)\n  )\n\n  return [\n    JSON.stringify(await sql`select * from test`),\n    '[{\"x\":1,\"y\":2,\"z\":3},{\"x\":4,\"y\":5,\"z\":6}]',\n    await sql`drop table test`\n  ]\n})\n\nt('Copy from works in transaction', async() => {\n  await sql`create table test(x int)`\n  const xs = await sql.begin(async sql => {\n    (await sql`copy test from stdin`.writable()).end('1\\n2')\n    await delay(20)\n    return sql`select 1 from test`\n  })\n\n  return [\n    xs.length,\n    2,\n    await sql`drop table test`\n  ]\n})\n\nt('Copy from abort', async() => {\n  const sql = postgres(options)\n  const readable = fs.createReadStream(rel('copy.csv'))\n\n  await sql`create table test (x int, y int, z int)`\n  await sql`TRUNCATE TABLE test`\n\n  const writable = await sql`COPY test FROM STDIN`.writable()\n\n  let aborted\n\n  readable\n    .pipe(writable)\n    .on('error', (err) => aborted = err)\n\n  writable.destroy(new Error('abort'))\n  await sql.end()\n\n  return [\n    'abort',\n    aborted.message,\n    await postgres(options)`drop table test`\n  ]\n})\n\nt('multiple queries before connect', async() => {\n  const sql = postgres({ ...options, max: 2 })\n  const xs = await Promise.all([\n    sql`select 1 as x`,\n    sql`select 2 as x`,\n    sql`select 3 as x`,\n    sql`select 4 as x`\n  ])\n\n  return [\n    '1,2,3,4',\n    xs.map(x => x[0].x).join()\n  ]\n})\n\nt('subscribe', { timeout: 2 }, async() => {\n  const sql = postgres({\n    database: 'postgres_js_test',\n    publications: 'alltables'\n  })\n\n  await sql.unsafe('create publication alltables for all tables')\n\n  const result = []\n\n  const { unsubscribe } = await sql.subscribe('*', (row, { command, old }) => {\n    result.push(command, row.name, row.id, old && old.name, old && old.id)\n  })\n\n  await sql`\n    create table test (\n      id serial primary key,\n      name text\n    )\n  `\n\n  await sql`alter table test replica identity default`\n  await sql`insert into test (name) values ('Murray')`\n  await sql`update test set name = 'Rothbard'`\n  await sql`update test set id = 2`\n  await sql`delete from test`\n  await sql`alter table test replica identity full`\n  await sql`insert into test (name) values ('Murray')`\n  await sql`update test set name = 'Rothbard'`\n  await sql`delete from test`\n  await delay(10)\n  await unsubscribe()\n  await sql`insert into test (name) values ('Oh noes')`\n  await delay(10)\n  return [\n    'insert,Murray,1,,,update,Rothbard,1,,,update,Rothbard,2,,1,delete,,2,,,insert,Murray,2,,,update,Rothbard,2,Murray,2,delete,Rothbard,2,,', // eslint-disable-line\n    result.join(','),\n    await sql`drop table test`,\n    await sql`drop publication alltables`,\n    await sql.end()\n  ]\n})\n\nt('subscribe with transform', { timeout: 2 }, async() => {\n  const sql = postgres({\n    transform: {\n      column: {\n        from: postgres.toCamel,\n        to: postgres.fromCamel\n      }\n    },\n    database: 'postgres_js_test',\n    publications: 'alltables'\n  })\n\n  await sql.unsafe('create publication alltables for all tables')\n\n  const result = []\n\n  const { unsubscribe } = await sql.subscribe('*', (row, { command, old }) =>\n    result.push(command, row.nameInCamel || row.id, old && old.nameInCamel)\n  )\n\n  await sql`\n    create table test (\n      id serial primary key,\n      name_in_camel text\n    )\n  `\n\n  await sql`insert into test (name_in_camel) values ('Murray')`\n  await sql`update test set name_in_camel = 'Rothbard'`\n  await sql`delete from test`\n  await sql`alter table test replica identity full`\n  await sql`insert into test (name_in_camel) values ('Murray')`\n  await sql`update test set name_in_camel = 'Rothbard'`\n  await sql`delete from test`\n  await delay(10)\n  await unsubscribe()\n  await sql`insert into test (name_in_camel) values ('Oh noes')`\n  await delay(10)\n  return [\n    'insert,Murray,,update,Rothbard,,delete,1,,insert,Murray,,update,Rothbard,Murray,delete,Rothbard,',\n    result.join(','),\n    await sql`drop table test`,\n    await sql`drop publication alltables`,\n    await sql.end()\n  ]\n})\n\nt('subscribe reconnects and calls onsubscribe', { timeout: 4 }, async() => {\n  const sql = postgres({\n    database: 'postgres_js_test',\n    publications: 'alltables',\n    fetch_types: false\n  })\n\n  await sql.unsafe('create publication alltables for all tables')\n\n  const result = []\n  let onsubscribes = 0\n\n  const { unsubscribe, sql: subscribeSql } = await sql.subscribe(\n    '*',\n    (row, { command, old }) => result.push(command, row.name || row.id, old && old.name),\n    () => onsubscribes++\n  )\n\n  await sql`\n    create table test (\n      id serial primary key,\n      name text\n    )\n  `\n\n  await sql`insert into test (name) values ('Murray')`\n  await delay(10)\n  await subscribeSql.close()\n  await delay(500)\n  await sql`delete from test`\n  await delay(100)\n  await unsubscribe()\n  return [\n    '2insert,Murray,,delete,1,',\n    onsubscribes + result.join(','),\n    await sql`drop table test`,\n    await sql`drop publication alltables`,\n    await sql.end()\n  ]\n})\n\nt('Execute', async() => {\n  const result = await new Promise((resolve) => {\n    const sql = postgres({ ...options, fetch_types: false, debug:(id, query) => resolve(query) })\n    sql`select 1`.execute()\n  })\n\n  return [result, 'select 1']\n})\n\nt('Cancel running query', async() => {\n  const query = sql`select pg_sleep(2)`\n  setTimeout(() => query.cancel(), 500)\n  const error = await query.catch(x => x)\n  return ['57014', error.code]\n})\n\nt('Cancel piped query', { timeout: 5 }, async() => {\n  await sql`select 1`\n  const last = sql`select pg_sleep(1)`.execute()\n  const query = sql`select pg_sleep(2) as dig`\n  setTimeout(() => query.cancel(), 500)\n  const error = await query.catch(x => x)\n  await last\n  return ['57014', error.code]\n})\n\nt('Cancel queued query', async() => {\n  const query = sql`select pg_sleep(2) as nej`\n  const tx = sql.begin(sql => (\n    query.cancel(),\n    sql`select pg_sleep(0.5) as hej, 'hejsa'`\n  ))\n  const error = await query.catch(x => x)\n  await tx\n  return ['57014', error.code]\n})\n\nt('Fragments', async() => [\n  1,\n  (await sql`\n    ${ sql`select` } 1 as x\n  `)[0].x\n])\n\nt('Result becomes array', async() => [\n  true,\n  (await sql`select 1`).slice() instanceof Array\n])\n\nt('Describe', async() => {\n  const type = (await sql`select ${ 1 }::int as x`.describe()).types[0]\n  return [23, type]\n})\n\nt('Describe a statement', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`select name, age from tester where name like $1 and age > $2`.describe()\n  return [\n    '25,23/name:25,age:23',\n    `${ r.types.join(',') }/${ r.columns.map(c => `${c.name}:${c.type}`).join(',') }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Include table oid and column number in column details', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`select name, age from tester where name like $1 and age > $2`.describe()\n  const [{ oid }] = await sql`select oid from pg_class where relname = 'tester'`\n\n  return [\n    `table:${oid},number:1|table:${oid},number:2`,\n    `${ r.columns.map(c => `table:${c.table},number:${c.number}`).join('|') }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Describe a statement without parameters', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`select name, age from tester`.describe()\n  return [\n    '0,2',\n    `${ r.types.length },${ r.columns.length }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Describe a statement without columns', async() => {\n  await sql`create table tester (name text, age int)`\n  const r = await sql`insert into tester (name, age) values ($1, $2)`.describe()\n  return [\n    '2,0',\n    `${ r.types.length },${ r.columns.length }`,\n    await sql`drop table tester`\n  ]\n})\n\nt('Large object', async() => {\n  const file = rel('index.js')\n      , md5 = crypto.createHash('md5').update(fs.readFileSync(file)).digest('hex')\n\n  const lo = await sql.largeObject()\n  await new Promise(async r => fs.createReadStream(file).pipe(await lo.writable()).on('finish', r))\n  await lo.seek(0)\n\n  const out = crypto.createHash('md5')\n  await new Promise(r => lo.readable().then(x => x.on('data', x => out.update(x)).on('end', r)))\n\n  return [\n    md5,\n    out.digest('hex'),\n    await lo.close()\n  ]\n})\n\nt('Catches type serialize errors', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: x => x,\n        serialize: () => { throw new Error('watSerialize') }\n      }\n    }\n  })\n\n  return [\n    'watSerialize',\n    (await sql`select ${ 'wat' }`.catch(e => e.message))\n  ]\n})\n\nt('Catches type parse errors', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: () => { throw new Error('watParse') },\n        serialize: x => x\n      }\n    }\n  })\n\n  return [\n    'watParse',\n    (await sql`select 'wat'`.catch(e => e.message))\n  ]\n})\n\nt('Catches type serialize errors in transactions', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: x => x,\n        serialize: () => { throw new Error('watSerialize') }\n      }\n    }\n  })\n\n  return [\n    'watSerialize',\n    (await sql.begin(sql => (\n      sql`select 1`,\n      sql`select ${ 'wat' }`\n    )).catch(e => e.message))\n  ]\n})\n\nt('Catches type parse errors in transactions', async() => {\n  const sql = postgres({\n    idle_timeout,\n    types: {\n      text: {\n        from: 25,\n        to: 25,\n        parse: () => { throw new Error('watParse') },\n        serialize: x => x\n      }\n    }\n  })\n\n  return [\n    'watParse',\n    (await sql.begin(sql => (\n      sql`select 1`,\n      sql`select 'wat'`\n    )).catch(e => e.message))\n  ]\n})\n\nt('Prevent premature end of connection in transaction', async() => {\n  const sql = postgres({ max_lifetime: 0.01, idle_timeout })\n  const result = await sql.begin(async sql => {\n    await sql`select 1`\n    await delay(20)\n    await sql`select 1`\n    return 'yay'\n  })\n\n\n  return [\n    'yay',\n    result\n  ]\n})\n\nt('Ensure reconnect after max_lifetime with transactions', { timeout: 5 }, async() => {\n  const sql = postgres({\n    max_lifetime: 0.01,\n    idle_timeout,\n    max: 1\n  })\n\n  let x = 0\n  while (x++ < 10) await sql.begin(sql => sql`select 1 as x`)\n\n  return [true, true]\n})\n\n\nt('Ensure transactions throw if connection is closed dwhile there is no query', async() => {\n  const sql = postgres(options)\n  const x = await sql.begin(async() => {\n    setTimeout(() => sql.end({ timeout: 0 }), 10)\n    await new Promise(r => setTimeout(r, 200))\n    return sql`select 1`\n  }).catch(x => x)\n  return ['CONNECTION_CLOSED', x.code]\n})\n\nt('Custom socket', {}, async() => {\n  let result\n  const sql = postgres({\n    socket: () => new Promise((resolve, reject) => {\n      const socket = new net.Socket()\n      socket.connect(5432)\n      socket.once('data', x => result = x[0])\n      socket.on('error', reject)\n      socket.on('connect', () => resolve(socket))\n    }),\n    idle_timeout\n  })\n\n  await sql`select 1`\n\n  return [\n    result,\n    82\n  ]\n})\n\nt('Ensure drain only dequeues if ready', async() => {\n  const sql = postgres(options)\n\n  const res = await Promise.all([\n    sql.unsafe('SELECT 0+$1 --' + '.'.repeat(100000), [1]),\n    sql.unsafe('SELECT 0+$1+$2+$3', [1, 2, 3])\n  ])\n\n  return [res.length, 2]\n})\n\nt('Supports fragments as dynamic parameters', async() => {\n  await sql`create table test (a int, b bool)`\n  await sql`insert into test values(1, true)`\n  await sql`insert into test ${\n    sql({\n      a: 2,\n      b: sql`exists(select 1 from test where b = ${ true })`\n    })\n  }`\n\n  return [\n    '1,t2,t',\n    (await sql`select * from test`.raw()).join(''),\n    await sql`drop table test`\n  ]\n})\n\nt('Supports nested fragments with parameters', async() => {\n  await sql`create table test ${\n    sql`(${ sql('a') } ${ sql`int` })`\n  }`\n  await sql`insert into test values(1)`\n  return [\n    1,\n    (await sql`select a from test`)[0].a,\n    await sql`drop table test`\n  ]\n})\n\nt('Supports multiple nested fragments with parameters', async() => {\n  const [{ b }] = await sql`select * ${\n    sql`from ${\n      sql`(values (2, ${ 1 }::int)) as x(${ sql(['a', 'b']) })`\n    }`\n  }`\n  return [\n    1,\n    b\n  ]\n})\n\nt('Supports arrays of fragments', async() => {\n  const [{ x }] = await sql`\n    ${ [sql`select`, sql`1`, sql`as`, sql`x`] }\n  `\n\n  return [\n    1,\n    x\n  ]\n})\n\nt('Does not try rollback when commit errors', async() => {\n  let notice = null\n  const sql = postgres({ ...options, onnotice: x => notice = x })\n  await sql`create table test(x int constraint test_constraint unique deferrable initially deferred)`\n\n  await sql.begin('isolation level serializable', async sql => {\n    await sql`insert into test values(1)`\n    await sql`insert into test values(1)`\n  }).catch(e => e)\n\n  return [\n    notice,\n    null,\n    await sql`drop table test`\n  ]\n})\n\nt('Last keyword used even with duplicate keywords', async() => {\n  await sql`create table test (x int)`\n  await sql`insert into test values(1)`\n  const [{ x }] = await sql`\n    select\n      1 in (1) as x\n    from test\n    where x in ${ sql([1, 2]) }\n  `\n\n  return [x, true, await sql`drop table test`]\n})\n\nt('Insert array with null', async() => {\n  await sql`create table test (x int[])`\n  await sql`insert into test ${ sql({ x: [1, null, 3] }) }`\n  return [\n    1,\n    (await sql`select x from test`)[0].x[0],\n    await sql`drop table test`\n  ]\n})\n\nt('Insert array with undefined throws', async() => {\n  await sql`create table test (x int[])`\n  return [\n    'UNDEFINED_VALUE',\n    await sql`insert into test ${ sql({ x: [1, undefined, 3] }) }`.catch(e => e.code),\n    await sql`drop table test`\n  ]\n})\n\nt('Insert array with undefined transform', async() => {\n  const sql = postgres({ ...options, transform: { undefined: null } })\n  await sql`create table test (x int[])`\n  await sql`insert into test ${ sql({ x: [1, undefined, 3] }) }`\n  return [\n    1,\n    (await sql`select x from test`)[0].x[0],\n    await sql`drop table test`\n  ]\n})\n\nt('concurrent cursors', async() => {\n  const xs = []\n\n  await Promise.all([...Array(7)].map((x, i) => [\n    sql`select ${ i }::int as a, generate_series(1, 2) as x`.cursor(([x]) => xs.push(x.a + x.x))\n  ]).flat())\n\n  return ['12233445566778', xs.join('')]\n})\n\nt('concurrent cursors multiple connections', async() => {\n  const sql = postgres({ ...options, max: 2 })\n  const xs = []\n\n  await Promise.all([...Array(7)].map((x, i) => [\n    sql`select ${ i }::int as a, generate_series(1, 2) as x`.cursor(([x]) => xs.push(x.a + x.x))\n  ]).flat())\n\n  return ['12233445566778', xs.sort().join('')]\n})\n\nt('reserve connection', async() => {\n  const reserved = await sql.reserve()\n\n  setTimeout(() => reserved.release(), 510)\n\n  const xs = await Promise.all([\n    reserved`select 1 as x`.then(([{ x }]) => ({ time: Date.now(), x })),\n    sql`select 2 as x`.then(([{ x }]) => ({ time: Date.now(), x })),\n    reserved`select 3 as x`.then(([{ x }]) => ({ time: Date.now(), x }))\n  ])\n\n  if (xs[1].time - xs[2].time < 500)\n    throw new Error('Wrong time')\n\n  return [\n    '123',\n    xs.map(x => x.x).join('')\n  ]\n})\n\nt('arrays in reserved connection', async() => {\n  const reserved = await sql.reserve()\n  const [{ x }] = await reserved`select array[1, 2, 3] as x`\n  reserved.release()\n\n  return [\n    '123',\n    x.join('')\n  ]\n})\n\nt('Ensure reserve on query throws proper error', async() => {\n  const sql = postgres({ idle_timeout }) // eslint-disable-line\n  const reserved = await sql.reserve()\n  const [{ x }] = await reserved`select 'wat' as x`\n\n  return [\n    'wat', x, reserved.release()\n  ]\n})\n\nt('query during copy error', async() => {\n  const sql = postgres(options) // eslint-disable-line\n  await sql`create table test (id serial primary key, name text)`\n  const copy = await sql`copy test from stdin`.writable()\n  const error = await sql`select 1`.catch(e => e)\n  await copy.end()\n\n  return [\n    'COPY_IN_PROGRESS', error.code,\n    await sql`drop table test`\n  ]\n})\n"
  },
  {
    "path": "tests/pg_hba.conf",
    "content": "local   all                 all                                     trust\nhost    all                 postgres                samehost        trust\nhost    postgres_js_test    postgres_js_test        samehost        trust\nhost    postgres_js_test    postgres_js_test_md5    samehost        md5\nhost    postgres_js_test    postgres_js_test_scram  samehost        scram-sha-256\n"
  },
  {
    "path": "tests/select-param.sql",
    "content": "select $1 as x\n"
  },
  {
    "path": "tests/select.sql",
    "content": "select 1 as x\n"
  },
  {
    "path": "tests/test.js",
    "content": "/* eslint no-console: 0 */\n\nimport util from 'util'\n\nlet done = 0\nlet only = false\nlet ignored = 0\nlet failed = false\nlet promise = Promise.resolve()\nconst tests = {}\n    , ignore = {}\n\nexport const nt = () => ignored++\nexport const ot = (...rest) => (only = true, test(true, ...rest))\nexport const t = (...rest) => test(false, ...rest)\nt.timeout = 5\n\nasync function test(o, name, options, fn) {\n  typeof options !== 'object' && (fn = options, options = {})\n  const line = new Error().stack.split('\\n')[3].match(':([0-9]+):')[1]\n\n  await 1\n\n  if (only && !o)\n    return\n\n  tests[line] = { fn, line, name }\n  promise = promise.then(() => Promise.race([\n    new Promise((resolve, reject) =>\n      fn.timer = setTimeout(() => reject('Timed out'), (options.timeout || t.timeout) * 1000)\n    ),\n    failed\n      ? (ignored++, ignore)\n      : fn()\n  ]))\n    .then(async x => {\n      clearTimeout(fn.timer)\n      if (x === ignore)\n        return\n\n      if (!Array.isArray(x))\n        throw new Error('Test should return result array')\n\n      const [expected, got] = await Promise.all(x)\n      if (expected !== got) {\n        failed = true\n        throw new Error(util.inspect(expected) + ' != ' + util.inspect(got))\n      }\n\n      tests[line].succeeded = true\n      process.stdout.write('✅')\n    })\n    .catch(err => {\n      tests[line].failed = failed = true\n      tests[line].error = err instanceof Error ? err : new Error(util.inspect(err))\n    })\n    .then(() => {\n      ++done === Object.keys(tests).length && exit()\n    })\n}\n\nfunction exit() {\n  let success = true\n  Object.values(tests).every((x) => {\n    if (x.succeeded)\n      return true\n\n    success = false\n    x.cleanup\n      ? console.error('⛔️', x.name + ' at line', x.line, 'cleanup failed', '\\n', util.inspect(x.cleanup))\n      : console.error('⛔️', x.name + ' at line', x.line, x.failed\n        ? 'failed'\n        : 'never finished', x.error ? '\\n' + util.inspect(x.error) : ''\n      )\n  })\n\n  only\n    ? console.error('⚠️', 'Not all tests were run')\n    : ignored\n      ? console.error('⚠️', ignored, 'ignored test' + (ignored === 1 ? '' : 's', '\\n'))\n      : success\n        ? console.log('🎉')\n        : console.error('⚠️', 'Not good')\n\n  !process.exitCode && (!success || only || ignored) && (process.exitCode = 1)\n}\n\n"
  },
  {
    "path": "transpile.cf.js",
    "content": "import fs from 'fs'\nimport path from 'path'\n\nconst empty = x => fs.readdirSync(x).forEach(f => fs.unlinkSync(path.join(x, f)))\n    , ensureEmpty = x => !fs.existsSync(x) ? fs.mkdirSync(x) : empty(x)\n    , root = 'cf'\n    , src = path.join(root, 'src')\n\nensureEmpty(src)\n\nfs.readdirSync('src').forEach(name =>\n  fs.writeFileSync(\n    path.join(src, name),\n    transpile(fs.readFileSync(path.join('src', name), 'utf8'), name, 'src')\n  )\n)\n\nfunction transpile(x) {\n  const timers = x.includes('setImmediate')\n    ? 'import { setImmediate, clearImmediate } from \\'../polyfills.js\\'\\n'\n    : ''\n\n  const process = x.includes('process.')\n    ? 'import { process } from \\'../polyfills.js\\'\\n'\n    : ''\n\n  const buffer = x.includes('Buffer')\n    ? 'import { Buffer } from \\'node:buffer\\'\\n'\n    : ''\n\n  return process + buffer + timers + x\n    .replace('import net from \\'net\\'', 'import { net } from \\'../polyfills.js\\'')\n    .replace('import tls from \\'tls\\'', 'import { tls } from \\'../polyfills.js\\'')\n    .replace('import crypto from \\'crypto\\'', 'import { crypto } from \\'../polyfills.js\\'')\n    .replace('import os from \\'os\\'', 'import { os } from \\'../polyfills.js\\'')\n    .replace('import fs from \\'fs\\'', 'import { fs } from \\'../polyfills.js\\'')\n    .replace('import { performance } from \\'perf_hooks\\'', 'import { performance } from \\'../polyfills.js\\'')\n    .replace(/ from '([a-z_]+)'/g, ' from \\'node:$1\\'')\n}\n"
  },
  {
    "path": "transpile.cjs",
    "content": "const fs = require('fs')\n    , path = require('path')\n\nconst empty = x => fs.readdirSync(x).forEach(f => fs.unlinkSync(path.join(x, f)))\n    , ensureEmpty = x => !fs.existsSync(x) ? fs.mkdirSync(x) : empty(x)\n    , root = 'cjs'\n    , src = path.join(root, 'src')\n    , tests = path.join(root, 'tests')\n\n!fs.existsSync(root) && fs.mkdirSync(root)\nensureEmpty(src)\nensureEmpty(tests)\n\nfs.readdirSync('src').forEach(name =>\n  fs.writeFileSync(\n    path.join(src, name),\n    transpile(fs.readFileSync(path.join('src', name), 'utf8'))\n  )\n)\n\nfs.readdirSync('tests').forEach(name =>\n  fs.writeFileSync(\n    path.join(tests, name),\n    name.endsWith('.js')\n      ? transpile(fs.readFileSync(path.join('tests', name), 'utf8'))\n      : fs.readFileSync(path.join('tests', name), 'utf8')\n  )\n)\n\nfs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ type: 'commonjs' }))\n\nfunction transpile(x) {\n  return x.replace(/export default function ([^(]+)/, 'module.exports = $1;function $1')\n          .replace(/export class ([a-z0-9_$]+)/gi, 'const $1 = module.exports.$1 = class $1')\n          .replace(/export default /, 'module.exports = ')\n          .replace(/export {/g, 'module.exports = {')\n          .replace(/export const ([a-z0-9_$]+)/gi, 'const $1 = module.exports.$1')\n          .replace(/export function ([a-z0-9_$]+)/gi, 'module.exports.$1 = $1;function $1')\n          .replace(/import {([^{}]*?)} from (['\"].*?['\"])/gi, 'const {$1} = require($2)')\n          .replace(/import (.*?) from (['\"].*?['\"])/gi, 'const $1 = require($2)')\n          .replace(/import (['\"].*?['\"])/gi, 'require($1)')\n          .replace('new URL(x, import.meta.url)', 'require(\"path\").join(__dirname, x)')\n}\n"
  },
  {
    "path": "transpile.deno.js",
    "content": "import fs from 'fs'\nimport path from 'path'\n\nconst std = 'https://deno.land/std@0.132.0/'\n    , empty = x => fs.readdirSync(x).forEach(f => fs.unlinkSync(path.join(x, f)))\n    , ensureEmpty = x => !fs.existsSync(x) ? fs.mkdirSync(x) : empty(x)\n    , root = 'deno'\n    , src = path.join(root, 'src')\n    , types = path.join(root, 'types')\n    , tests = path.join(root, 'tests')\n\nensureEmpty(src)\nensureEmpty(types)\nensureEmpty(tests)\n\nfs.writeFileSync(\n  path.join(types, 'index.d.ts'),\n  transpile(fs.readFileSync(path.join('types', 'index.d.ts'), 'utf8'), 'index.d.ts', 'types')\n)\n\nfs.writeFileSync(\n  path.join(root, 'README.md'),\n  fs.readFileSync('README.md', 'utf8')\n    .replace(/### Installation(\\n.*){4}/, '')\n    .replace(\n      'import postgres from \\'postgres\\'',\n      'import postgres from \\'https://deno.land/x/postgresjs/mod.js\\''\n    )\n)\n\nfs.readdirSync('src').forEach(name =>\n  fs.writeFileSync(\n    path.join(src, name),\n    transpile(fs.readFileSync(path.join('src', name), 'utf8'), name, 'src')\n  )\n)\n\nfs.readdirSync('tests').forEach(name =>\n  fs.writeFileSync(\n    path.join(tests, name),\n    name.endsWith('.js')\n      ? transpile(fs.readFileSync(path.join('tests', name), 'utf8'), name, 'tests')\n      : fs.readFileSync(path.join('tests', name), 'utf8')\n  )\n)\n\nfs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ type: 'commonjs' }))\n\nfunction transpile(x, name, folder) {\n  if (folder === 'tests') {\n    if (name === 'bootstrap.js') {\n      x = x.replace('export function exec(', 'function ignore(')\n           .replace('async function execAsync(', 'export async function exec(')\n           .replace(/\\nexec\\(/g, '\\nawait exec(')\n           .replace('{ spawnSync }', '{ spawn }')\n    }\n    if (name === 'index.js')\n      x += '\\n;globalThis.addEventListener(\"unload\", () => Deno.exit(process.exitCode))'\n  }\n\n  const buffer = x.includes('Buffer')\n    ? 'import { Buffer } from \\'' + std + 'node/buffer.ts\\'\\n'\n    : ''\n\n  const process = x.includes('process.')\n    ? 'import process from \\'' + std + 'node/process.ts\\'\\n'\n    : ''\n\n  const timers = x.includes('setImmediate')\n    ? 'import { setImmediate, clearImmediate } from \\'../polyfills.js\\'\\n'\n    : ''\n\n  const hmac = x.includes('createHmac')\n    ? 'import { HmacSha256 } from \\'' + std + 'hash/sha256.ts\\'\\n'\n    : ''\n\n  return hmac + buffer + process + timers + x\n    .replace(\n      'crypto.createHmac(\\'sha256\\', key).update(x).digest()',\n      'Buffer.from(new HmacSha256(key).update(x).digest())'\n    )\n    .replace(\n      'query.writable.push({ chunk, callback })',\n      '(query.writable.push({ chunk }), callback())'\n    )\n    .replace('socket.setKeepAlive(true, 1000 * keep_alive)', 'socket.setKeepAlive(true)')\n    .replace('node:stream', std + 'node/stream.ts')\n    .replace('import net from \\'net\\'', 'import { net } from \\'../polyfills.js\\'')\n    .replace('import tls from \\'tls\\'', 'import { tls } from \\'../polyfills.js\\'')\n    .replace('import { performance } from \\'perf_hooks\\'', '')\n    .replace(/ from '([a-z_]+)'/g, ' from \\'' + std + 'node/$1.ts\\'')\n}\n"
  },
  {
    "path": "types/index.d.ts",
    "content": "import { Readable, Writable } from 'node:stream'\n\n/**\n * Establish a connection to a PostgreSQL server.\n * @param options Connection options - default to the same as psql\n * @returns An utility function to make queries to the server\n */\ndeclare function postgres<T extends Record<string, postgres.PostgresType> = {}>(options?: postgres.Options<T> | undefined): postgres.Sql<Record<string, postgres.PostgresType> extends T ? {} : { [type in keyof T]: T[type] extends {\n  serialize: (value: infer R) => any,\n  parse: (raw: any) => infer R\n} ? R : never }>\n/**\n * Establish a connection to a PostgreSQL server.\n * @param url Connection string used for authentication\n * @param options Connection options - default to the same as psql\n * @returns An utility function to make queries to the server\n */\ndeclare function postgres<T extends Record<string, postgres.PostgresType> = {}>(url: string, options?: postgres.Options<T> | undefined): postgres.Sql<Record<string, postgres.PostgresType> extends T ? {} : { [type in keyof T]: T[type] extends {\n  serialize: (value: infer R) => any,\n  parse: (raw: any) => infer R\n} ? R : never }>\n\n/**\n * Connection options of Postgres.\n */\ninterface BaseOptions<T extends Record<string, postgres.PostgresType>> {\n  /** Postgres ip address[s] or domain name[s] */\n  host: string | string[] | undefined;\n  /** Postgres server[s] port[s] */\n  port: number | number[] | undefined;\n  /** unix socket path (usually '/tmp') */\n  path: string | undefined;\n  /**\n   * Name of database to connect to\n   * @default process.env['PGDATABASE'] || options.user\n   */\n  database: string;\n  /**\n   * Username of database user\n   * @default process.env['PGUSERNAME'] || process.env['PGUSER'] || require('os').userInfo().username\n   */\n  user: string;\n  /**\n   * How to deal with ssl (can be a tls.connect option object)\n   * @default false\n  */\n  ssl: 'require' | 'allow' | 'prefer' | 'verify-full' | boolean | object;\n  /**\n   * Max number of connections\n   * @default 10\n   */\n  max: number;\n  /**\n   * Idle connection timeout in seconds\n   * @default process.env['PGIDLE_TIMEOUT']\n   */\n  idle_timeout: number | undefined;\n  /**\n   * Connect timeout in seconds\n   * @default process.env['PGCONNECT_TIMEOUT']\n   */\n  connect_timeout: number;\n  /** Array of custom types; see more in the README */\n  types: T;\n  /**\n   * Enables prepare mode.\n   * @default true\n   */\n  prepare: boolean;\n  /**\n   * Called when a notice is received\n   * @default console.log\n   */\n  onnotice: (notice: postgres.Notice) => void;\n  /** (key; value) when a server param change */\n  onparameter: (key: string, value: any) => void;\n  /** Is called with (connection; query; parameters) */\n  debug: boolean | ((connection: number, query: string, parameters: any[], paramTypes: any[]) => void);\n  /** Transform hooks */\n  transform: {\n    /** Transforms outcoming undefined values */\n    undefined?: any\n\n    /** Transforms incoming and outgoing column names */\n    column?: ((column: string) => string) | {\n      /** Transform function for column names in result rows */\n      from?: ((column: string) => string) | undefined;\n      /** Transform function for column names in interpolated values passed to tagged template literal */\n      to?: ((column: string) => string) | undefined;\n    } | undefined;\n    /** Transforms incoming and outgoing row values */\n    value?: ((value: any) => any) | {\n      /** Transform function for values in result rows */\n      from?: ((value: unknown, column: postgres.Column<string>) => any) | undefined;\n      // to?: ((value: unknown) => any) | undefined; // unused\n    } | undefined;\n    /** Transforms entire rows */\n    row?: ((row: postgres.Row) => any) | {\n      /** Transform function for entire result rows */\n      from?: ((row: postgres.Row) => any) | undefined;\n      // to?: ((row: postgres.Row) => any) | undefined; // unused\n    } | undefined;\n  };\n  /** Connection parameters */\n  connection: Partial<postgres.ConnectionParameters>;\n  /**\n   * Use 'read-write' with multiple hosts to ensure only connecting to primary\n   * @default process.env['PGTARGETSESSIONATTRS']\n   */\n  target_session_attrs: undefined | 'read-write' | 'read-only' | 'primary' | 'standby' | 'prefer-standby';\n  /**\n   * Automatically fetches types on connect\n   * @default true\n   */\n  fetch_types: boolean;\n  /**\n   * Publications to subscribe to (only relevant when calling `sql.subscribe()`)\n   * @default 'alltables'\n   */\n  publications: string\n  onclose: (connId: number) => void;\n  backoff: boolean | ((attemptNum: number) => number);\n  max_lifetime: number | null;\n  keep_alive: number | null;\n}\n\n\ndeclare const PRIVATE: unique symbol;\n\ndeclare class NotAPromise {\n  private [PRIVATE]: never; // prevent user-side interface implementation\n\n  /**\n   * @deprecated This object isn't an SQL query, and therefore not a Promise; use the tagged template string syntax instead: ```await sql\\`...\\`;```\n   * @throws NOT_TAGGED_CALL\n   */\n  private then(): never;\n  /**\n   * @deprecated This object isn't an SQL query, and therefore not a Promise; use the tagged template string syntax instead: ```await sql\\`...\\`;```\n   * @throws NOT_TAGGED_CALL\n   */\n  private catch(): never;\n  /**\n   * @deprecated This object isn't an SQL query, and therefore not a Promise; use the tagged template string syntax instead: ```await sql\\`...\\`;```\n   * @throws NOT_TAGGED_CALL\n   */\n  private finally(): never;\n}\n\ntype UnwrapPromiseArray<T> = T extends any[] ? {\n  [k in keyof T]: T[k] extends Promise<infer R> ? R : T[k]\n} : T;\n\ntype Keys = string\n\ntype SerializableObject<T, K extends readonly any[], TT> =\n  number extends K['length'] ? {} :\n  Partial<(Record<Keys & (keyof T) & (K['length'] extends 0 ? string : K[number]), postgres.ParameterOrJSON<TT> | undefined> & Record<string, any>)>\n\ntype First<T, K extends readonly any[], TT> =\n  // Tagged template string call\n  T extends TemplateStringsArray ? TemplateStringsArray :\n  // Identifiers helper\n  T extends string ? string :\n  // Dynamic values helper (depth 2)\n  T extends readonly any[][] ? readonly postgres.EscapableArray[] :\n  // Insert/update helper (depth 2)\n  T extends readonly (object & infer R)[] ? (R extends postgres.SerializableParameter<TT> ? readonly postgres.SerializableParameter<TT>[] : readonly SerializableObject<R, K, TT>[]) :\n  // Dynamic values/ANY helper (depth 1)\n  T extends readonly any[] ? (readonly postgres.SerializableParameter<TT>[]) :\n  // Insert/update helper (depth 1)\n  T extends object ? SerializableObject<T, K, TT> :\n  // Unexpected type\n  never\n\ntype Rest<T> =\n  T extends TemplateStringsArray ? never : // force fallback to the tagged template function overload\n  T extends string ? readonly string[] :\n  T extends readonly any[][] ? readonly [] :\n  T extends readonly (object & infer R)[] ? (\n    readonly (Keys & keyof R)[]   // sql(data, \"prop\", \"prop2\") syntax\n    |\n    [readonly (Keys & keyof R)[]] // sql(data, [\"prop\", \"prop2\"]) syntax\n  ) :\n  T extends readonly any[] ? readonly [] :\n  T extends object ? (\n    readonly (Keys & keyof T)[]   // sql(data, \"prop\", \"prop2\") syntax\n    |\n    [readonly (Keys & keyof T)[]] // sql(data, [\"prop\", \"prop2\"]) syntax\n  ) :\n  any\n\ntype Return<T, K extends readonly any[]> =\n  [T] extends [TemplateStringsArray] ?\n  [unknown] extends [T] ? postgres.Helper<T, K> : // ensure no `PendingQuery` with `any` types\n  [TemplateStringsArray] extends [T] ? postgres.PendingQuery<postgres.Row[]> :\n  postgres.Helper<T, K> :\n  postgres.Helper<T, K>\n\ndeclare namespace postgres {\n  class PostgresError extends Error {\n    name: 'PostgresError';\n    severity_local: string;\n    severity: string;\n    code: string;\n    position: string;\n    file: string;\n    line: string;\n    routine: string;\n\n    detail?: string | undefined;\n    hint?: string | undefined;\n    internal_position?: string | undefined;\n    internal_query?: string | undefined;\n    where?: string | undefined;\n    schema_name?: string | undefined;\n    table_name?: string | undefined;\n    column_name?: string | undefined;\n    data?: string | undefined;\n    type_name?: string | undefined;\n    constraint_name?: string | undefined;\n\n    /** Only set when debug is enabled */\n    query: string;\n    /** Only set when debug is enabled */\n    parameters: any[];\n  }\n\n  /**\n   * Convert a snake_case string to PascalCase.\n   * @param str The string from snake_case to convert\n   * @returns The new string in PascalCase\n   */\n  function toPascal(str: string): string;\n  namespace toPascal {\n    namespace column { function from(str: string): string; }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a PascalCase string to snake_case.\n   * @param str The string from snake_case to convert\n   * @returns The new string in snake_case\n   */\n  function fromPascal(str: string): string;\n  namespace fromPascal {\n    namespace column { function to(str: string): string }\n  }\n  /**\n   * Convert snake_case to and from PascalCase.\n   */\n   namespace pascal {\n    namespace column {\n      function from(str: string): string;\n      function to(str: string): string;\n    }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a snake_case string to camelCase.\n   * @param str The string from snake_case to convert\n   * @returns The new string in camelCase\n   */\n  function toCamel(str: string): string;\n  namespace toCamel {\n    namespace column { function from(str: string): string; }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a camelCase string to snake_case.\n   * @param str The string from snake_case to convert\n   * @returns The new string in snake_case\n   */\n  function fromCamel(str: string): string;\n  namespace fromCamel {\n    namespace column { function to(str: string): string }\n  }\n  /**\n   * Convert snake_case to and from camelCase.\n   */\n  namespace camel {\n    namespace column {\n      function from(str: string): string;\n      function to(str: string): string;\n    }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a snake_case string to kebab-case.\n   * @param str The string from snake_case to convert\n   * @returns The new string in kebab-case\n   */\n  function toKebab(str: string): string;\n  namespace toKebab {\n    namespace column { function from(str: string): string; }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n  /**\n   * Convert a kebab-case string to snake_case.\n   * @param str The string from snake_case to convert\n   * @returns The new string in snake_case\n   */\n  function fromKebab(str: string): string;\n  namespace fromKebab {\n    namespace column { function to(str: string): string }\n  }\n  /**\n   * Convert snake_case to and from kebab-case.\n   */\n  namespace kebab {\n    namespace column {\n      function from(str: string): string;\n      function to(str: string): string;\n    }\n    namespace value { function from(str: unknown, column: Column<string>): string }\n  }\n\n  const BigInt: PostgresType<bigint>;\n\n  interface PostgresType<T = any> {\n    to: number;\n    from: number[];\n    serialize: (value: T) => unknown;\n    parse: (raw: any) => T;\n  }\n\n  interface ConnectionParameters {\n    /**\n     * Default application_name\n     * @default 'postgres.js'\n     */\n    application_name: string;\n    default_transaction_isolation: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable',\n    default_transaction_read_only: boolean,\n    default_transaction_deferrable: boolean,\n    statement_timeout: number,\n    lock_timeout: number,\n    idle_in_transaction_session_timeout: number,\n    idle_session_timeout: number,\n    DateStyle: string,\n    IntervalStyle: string,\n    TimeZone: string,\n    /** Other connection parameters */\n    [name: string]: string | number | boolean;\n  }\n\n  interface Options<T extends Record<string, postgres.PostgresType>> extends Partial<BaseOptions<T>> {\n    /** @inheritdoc */\n    host?: string | undefined;\n    /** @inheritdoc */\n    port?: number | undefined;\n    /** @inheritdoc */\n    path?: string | undefined;\n    /** Password of database user (an alias for `password`) */\n    pass?: Options<T>['password'] | undefined;\n    /**\n     * Password of database user\n     * @default process.env['PGPASSWORD']\n     */\n    password?: string | (() => string | Promise<string>) | undefined;\n    /** Name of database to connect to (an alias for `database`) */\n    db?: Options<T>['database'] | undefined;\n    /** Username of database user (an alias for `user`) */\n    username?: Options<T>['user'] | undefined;\n    /** Postgres ip address or domain name (an alias for `host`) */\n    hostname?: Options<T>['host'] | undefined;\n    /**\n     * Disable prepared mode\n     * @deprecated use \"prepare\" option instead\n     */\n    no_prepare?: boolean | undefined;\n    /**\n     * Idle connection timeout in seconds\n     * @deprecated use \"idle_timeout\" option instead\n     */\n    timeout?: Options<T>['idle_timeout'] | undefined;\n  }\n\n  interface ParsedOptions<T extends Record<string, unknown> = {}> extends BaseOptions<{ [name in keyof T]: PostgresType<T[name]> }> {\n    /** @inheritdoc */\n    host: string[];\n    /** @inheritdoc */\n    port: number[];\n    /** @inheritdoc */\n    pass: null;\n    /** @inheritdoc */\n    transform: Transform;\n    serializers: Record<number, (value: any) => unknown>;\n    parsers: Record<number, (value: any) => unknown>;\n  }\n\n  interface Transform {\n    /** Transforms outcoming undefined values */\n    undefined: any\n\n    column: {\n      /** Transform function for column names in result rows */\n      from: ((column: string) => string) | undefined;\n      /** Transform function for column names in interpolated values passed to tagged template literal */\n      to: ((column: string) => string) | undefined;\n    };\n    value: {\n      /** Transform function for values in result rows */\n      from: ((value: any, column?: Column<string>) => any) | undefined;\n      /** Transform function for interpolated values passed to tagged template literal */\n      to: undefined; // (value: any) => any\n    };\n    row: {\n      /** Transform function for entire result rows */\n      from: ((row: postgres.Row) => any) | undefined;\n      to: undefined; // (row: postgres.Row) => any\n    };\n  }\n\n  interface Notice {\n    [field: string]: string;\n  }\n\n  interface Parameter<T = SerializableParameter> extends NotAPromise {\n    /**\n     * PostgreSQL OID of the type\n     */\n    type: number;\n    /**\n     * Serialized value\n     */\n    value: string | null;\n    /**\n     * Raw value to serialize\n     */\n    raw: T | null;\n  }\n\n  interface ArrayParameter<T extends readonly any[] = readonly any[]> extends Parameter<T | T[]> {\n    array: true;\n  }\n\n  interface ConnectionError extends globalThis.Error {\n    code:\n    | 'CONNECTION_DESTROYED'\n    | 'CONNECT_TIMEOUT'\n    | 'CONNECTION_CLOSED'\n    | 'CONNECTION_ENDED';\n    errno: this['code'];\n    address: string;\n    port?: number | undefined;\n  }\n\n  interface NotSupportedError extends globalThis.Error {\n    code: 'MESSAGE_NOT_SUPPORTED';\n    name: string;\n  }\n\n  interface GenericError extends globalThis.Error {\n    code:\n    | '57014' // canceling statement due to user request\n    | 'NOT_TAGGED_CALL'\n    | 'UNDEFINED_VALUE'\n    | 'MAX_PARAMETERS_EXCEEDED'\n    | 'SASL_SIGNATURE_MISMATCH'\n    | 'UNSAFE_TRANSACTION';\n    message: string;\n  }\n\n  interface AuthNotImplementedError extends globalThis.Error {\n    code: 'AUTH_TYPE_NOT_IMPLEMENTED';\n    type: number | string;\n    message: string;\n  }\n\n  type Error = never\n    | PostgresError\n    | ConnectionError\n    | NotSupportedError\n    | GenericError\n    | AuthNotImplementedError;\n\n  interface ColumnInfo {\n    key: number;\n    name: string;\n    type: number;\n    parser?(raw: string): unknown;\n    atttypmod: number;\n  }\n\n  interface RelationInfo {\n    schema: string;\n    table: string;\n    columns: ColumnInfo[];\n    keys: ColumnInfo[];\n  }\n\n  type ReplicationEvent =\n    | { command: 'insert', relation: RelationInfo }\n    | { command: 'delete', relation: RelationInfo, key: boolean }\n    | { command: 'update', relation: RelationInfo, key: boolean, old: Row | null };\n\n  interface SubscriptionHandle {\n    unsubscribe(): void;\n  }\n\n  interface LargeObject {\n    writable(options?: {\n      highWaterMark?: number | undefined,\n      start?: number | undefined\n    } | undefined): Promise<Writable>;\n    readable(options?: {\n      highWaterMark?: number | undefined,\n      start?: number | undefined,\n      end?: number | undefined\n    } | undefined): Promise<Readable>;\n\n    close(): Promise<void>;\n    tell(): Promise<void>;\n    read(size: number): Promise<void>;\n    write(buffer: Uint8Array): Promise<[{ data: Uint8Array }]>;\n    truncate(size: number): Promise<void>;\n    seek(offset: number, whence?: number | undefined): Promise<void>;\n    size(): Promise<[{ position: bigint, size: bigint }]>;\n  }\n\n  type EscapableArray = (string | number)[]\n\n  type Serializable = never\n    | null\n    | boolean\n    | number\n    | string\n    | Date\n    | Uint8Array;\n\n  type SerializableParameter<T = never> = never\n    | T\n    | Serializable\n    | Helper<any>\n    | Parameter<any>\n    | ArrayParameter\n    | readonly SerializableParameter<T>[];\n\n  type JSONValue = // using a dedicated type to detect symbols, bigints, and other non serializable types\n    | null\n    | string\n    | number\n    | boolean\n    | Date // serialized as `string`\n    | readonly JSONValue[]\n    | { toJSON(): any } // `toJSON` called by `JSON.stringify`; not typing the return type, types definition is strict enough anyway\n    | {\n      readonly [prop: string | number]:\n      | undefined\n      | JSONValue\n      | ((...args: any) => any) // serialized as `undefined`\n    };\n\n  interface Row {\n    [column: string]: any;\n  }\n\n  type MaybeRow = Row | undefined;\n\n  interface Column<T extends string> {\n    name: T;\n    type: number;\n    table: number;\n    number: number;\n    parser?: ((raw: string) => unknown) | undefined;\n  }\n\n  type ColumnList<T> = (T extends string ? Column<T> : never)[];\n\n  interface State {\n    status: string;\n    pid: number;\n    secret: number;\n  }\n\n  interface Statement {\n    /** statement unique name */\n    name: string;\n    /** sql query */\n    string: string;\n    /** parameters types */\n    types: number[];\n    columns: ColumnList<string>;\n  }\n\n  interface ResultMeta<T extends number | null> {\n    count: T; // For tuples\n    command: string;\n    statement: Statement;\n    state: State;\n  }\n\n  interface ResultQueryMeta<T extends number | null, U> extends ResultMeta<T> {\n    columns: ColumnList<U>;\n  }\n\n  type ExecutionResult<T> = [] & ResultQueryMeta<number, keyof NonNullable<T>>;\n  type ValuesRowList<T extends readonly any[]> = T[number][keyof T[number]][][] & ResultQueryMeta<T['length'], keyof T[number]>;\n  type RawRowList<T extends readonly any[]> = Buffer[][] & Iterable<Buffer[][]> & ResultQueryMeta<T['length'], keyof T[number]>;\n  type RowList<T extends readonly any[]> = T & Iterable<NonNullable<T[number]>> & ResultQueryMeta<T['length'], keyof T[number]>;\n\n  interface PendingQueryModifiers<TRow extends readonly any[]> {\n    simple(): this;\n    readable(): Promise<Readable>;\n    writable(): Promise<Writable>;\n\n    execute(): this;\n    cancel(): void;\n\n    /**\n     * @deprecated `.stream` has been renamed to `.forEach`\n     * @throws\n     */\n    stream(cb: (row: NonNullable<TRow[number]>, result: ExecutionResult<TRow[number]>) => void): never;\n    forEach(cb: (row: NonNullable<TRow[number]>, result: ExecutionResult<TRow[number]>) => void): Promise<ExecutionResult<TRow[number]>>;\n\n    cursor(rows?: number | undefined): AsyncIterable<NonNullable<TRow[number]>[]>;\n    cursor(cb: (row: [NonNullable<TRow[number]>]) => void): Promise<ExecutionResult<TRow[number]>>;\n    cursor(rows: number, cb: (rows: NonNullable<TRow[number]>[]) => void): Promise<ExecutionResult<TRow[number]>>;\n  }\n\n  interface PendingDescribeQuery extends Promise<Statement> {\n  }\n\n  interface PendingValuesQuery<TRow extends readonly MaybeRow[]> extends Promise<ValuesRowList<TRow>>, PendingQueryModifiers<TRow[number][keyof TRow[number]][][]> {\n    describe(): PendingDescribeQuery;\n  }\n\n  interface PendingRawQuery<TRow extends readonly MaybeRow[]> extends Promise<RawRowList<TRow>>, PendingQueryModifiers<Buffer[][]> {\n  }\n\n  interface PendingQuery<TRow extends readonly MaybeRow[]> extends Promise<RowList<TRow>>, PendingQueryModifiers<TRow> {\n    describe(): PendingDescribeQuery;\n    values(): PendingValuesQuery<TRow>;\n    raw(): PendingRawQuery<TRow>;\n  }\n\n  interface PendingRequest extends Promise<[] & ResultMeta<null>> { }\n\n  interface ListenRequest extends Promise<ListenMeta> { }\n  interface ListenMeta extends ResultMeta<null> {\n    unlisten(): Promise<void>\n  }\n\n  interface Helper<T, U extends readonly any[] = T[]> extends NotAPromise {\n    first: T;\n    rest: U;\n  }\n\n  type Fragment = PendingQuery<any>\n\n  type ParameterOrJSON<T> =\n  | SerializableParameter<T>\n  | JSONValue\n\n  type ParameterOrFragment<T> =\n  | SerializableParameter<T>\n  | Fragment\n  | Fragment[]\n\n  interface Sql<TTypes extends Record<string, unknown> = {}> {\n    /**\n     * Query helper\n     * @param first Define how the helper behave\n     * @param rest Other optional arguments, depending on the helper type\n     * @returns An helper object usable as tagged template parameter in sql queries\n     */\n    <T, K extends Rest<T>>(first: T & First<T, K, TTypes[keyof TTypes]>, ...rest: K): Return<T, K>;\n\n    /**\n     * Execute the SQL query passed as a template string. Can only be used as template string tag.\n     * @param template The template generated from the template string\n     * @param parameters Interpoled values of the template string\n     * @returns A promise resolving to the result of your query\n     */\n    <T extends readonly (object | undefined)[] = Row[]>(template: TemplateStringsArray, ...parameters: readonly (ParameterOrFragment<TTypes[keyof TTypes]>)[]): PendingQuery<T>;\n\n    CLOSE: {};\n    END: this['CLOSE'];\n    PostgresError: typeof PostgresError;\n\n    options: ParsedOptions<TTypes>;\n    parameters: ConnectionParameters;\n    types: this['typed'];\n    typed: (<T>(value: T, oid: number) => Parameter<T>) & {\n      [name in keyof TTypes]: (value: TTypes[name]) => postgres.Parameter<TTypes[name]>\n    };\n\n    unsafe<T extends any[] = (Row & Iterable<Row>)[]>(query: string, parameters?: (ParameterOrJSON<TTypes[keyof TTypes]>)[] | undefined, queryOptions?: UnsafeQueryOptions | undefined): PendingQuery<T>;\n    end(options?: { timeout?: number | undefined } | undefined): Promise<void>;\n\n    listen(channel: string, onnotify: (value: string) => void, onlisten?: (() => void) | undefined): ListenRequest;\n    notify(channel: string, payload: string): PendingRequest;\n\n    subscribe(event: string, cb: (row: Row | null, info: ReplicationEvent) => void, onsubscribe?: (() => void), onerror?: (() => any)): Promise<SubscriptionHandle>;\n\n    largeObject(oid?: number | undefined, /** @default 0x00020000 | 0x00040000 */ mode?: number | undefined): Promise<LargeObject>;\n\n    begin<T>(cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;\n    begin<T>(options: string, cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;\n\n    array<T extends SerializableParameter<TTypes[keyof TTypes]>[] = SerializableParameter<TTypes[keyof TTypes]>[]>(value: T, type?: number | undefined): ArrayParameter<T>;\n    file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, options?: { cache?: boolean | undefined } | undefined): PendingQuery<T>;\n    file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, args: (ParameterOrJSON<TTypes[keyof TTypes]>)[], options?: { cache?: boolean | undefined } | undefined): PendingQuery<T>;\n    json(value: JSONValue): Parameter;\n\n    reserve(): Promise<ReservedSql<TTypes>>\n  }\n\n  interface UnsafeQueryOptions {\n    /**\n     * When executes query as prepared statement.\n     * @default false\n     */\n    prepare?: boolean | undefined;\n  }\n\n  interface TransactionSql<TTypes extends Record<string, unknown> = {}> extends Omit<Sql<TTypes>,\n      'parameters' |\n      'largeObject' |\n      'subscribe' |\n      'CLOSE' |\n      'END' |\n      'PostgresError' |\n      'options' |\n      'reserve' |\n      'listen' |\n      'begin' |\n      'close' |\n      'end'\n  > {\n    savepoint<T>(cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;\n    savepoint<T>(name: string, cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;\n\n    prepare<T>(name: string): Promise<UnwrapPromiseArray<T>>;\n  }\n\n  interface ReservedSql<TTypes extends Record<string, unknown> = {}> extends Sql<TTypes> {\n    release(): void;\n  }\n}\n\nexport = postgres;\n"
  },
  {
    "path": "types/package.json",
    "content": "{\n  \"devDependencies\": {\n    \"@types/node\": \"^16\"\n  }\n}\n"
  },
  {
    "path": "types/tsconfig.json",
    "content": "{\n  \"compilerOptions\": {\n    \"lib\": [\n      \"ES2015\"\n    ],\n    \"types\": [\n      \"node\"\n    ],\n    \"esModuleInterop\": true,\n    \"strict\": true,\n    \"noImplicitAny\": true,\n    \"exactOptionalPropertyTypes\": true\n  }\n}"
  }
]