Skip to content

Commit 940479b

Browse files
briancmesqueeb
andauthored
Add esm exports (#3423)
* build: add esm exports * fix: add defaults as per arethetypeswrong report * fix: add missing types * lint * Fix broken tests * Add (failing) test for esm compat * Begin moving files to proper extension and adding tests * Add tests for connection-string and fix cloudflare module type and esm compat * Add query-stream and cursor as esm exports * Update PR copilot review * Publish - [email protected] - [email protected] - [email protected] - [email protected] - [email protected] - [email protected] - [email protected] - [email protected] - [email protected] * More cf compat work * Publish - [email protected] - [email protected] - [email protected] - [email protected] - [email protected] - [email protected] * Add more cf compat and update tests * Make tests pass - update exports for esm * Use env vars for test connection in cf tests * Fix lint * Fit vitest into existing legacy framework * Skip worker tests on node below 18 * Revert doc changes for now * Remove legacy worker test in favor of vitest --------- Co-authored-by: Luca Ban <[email protected]>
1 parent 81ec063 commit 940479b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1684
-463
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"packages/*"
1111
],
1212
"scripts": {
13-
"test": "yarn lerna exec yarn test",
13+
"test": "yarn lerna exec --concurrency 1 yarn test",
1414
"build": "tsc --build",
1515
"build:watch": "tsc --build --watch",
1616
"docs:build": "cd docs && yarn build",

packages/pg-cloudflare/esm/index.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import cf from '../dist/index.js'
2+
3+
export const CloudflareSocket = cf.CloudflareSocket

packages/pg-cloudflare/package.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
{
22
"name": "pg-cloudflare",
3-
"version": "1.1.1",
3+
"version": "1.1.2-alpha.1",
44
"description": "A socket implementation that can run on Cloudflare Workers using native TCP connections.",
5-
"main": "dist/empty.js",
5+
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"license": "MIT",
88
"devDependencies": {
99
"ts-node": "^8.5.4",
1010
"typescript": "^4.0.3"
1111
},
1212
"exports": {
13-
"workerd": "./dist/index.js",
14-
"default": "./dist/empty.js"
13+
".": {
14+
"import": "./esm/index.mjs",
15+
"require": "./dist/index.js",
16+
"default": "./dist/index.js"
17+
}
1518
},
1619
"scripts": {
1720
"build": "tsc",
@@ -26,6 +29,7 @@
2629
},
2730
"files": [
2831
"/dist/*{js,ts,map}",
29-
"/src"
32+
"/src",
33+
"/esm"
3034
]
3135
}

packages/pg-cloudflare/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export class CloudflareSocket extends EventEmitter {
3737
if (connectListener) this.once('connect', connectListener)
3838

3939
const options: SocketOptions = this.ssl ? { secureTransport: 'starttls' } : {}
40-
const { connect } = await import('cloudflare:sockets')
40+
const mod = await import('cloudflare:sockets')
41+
const connect = mod.connect
4142
this._cfSocket = connect(`${host}:${port}`, options)
4243
this._cfWriter = this._cfSocket.writable.getWriter()
4344
this._addClosedHandler()

packages/pg-cloudflare/tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"compilerOptions": {
3-
"module": "ES2020",
3+
"module": "node16",
44
"esModuleInterop": true,
55
"allowSyntheticDefaultImports": true,
66
"strict": true,
7-
"target": "ES2020",
7+
"target": "es2020",
88
"noImplicitAny": true,
9-
"moduleResolution": "node",
9+
"moduleResolution": "node16",
1010
"sourceMap": true,
1111
"outDir": "dist",
1212
"incremental": true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ESM wrapper for pg-connection-string
2+
import connectionString from '../index.js'
3+
4+
// Re-export the parse function
5+
export const parse = connectionString.parse
6+
export const toClientConfig = connectionString.toClientConfig
7+
export const parseIntoClientConfig = connectionString.parseIntoClientConfig

packages/pg-connection-string/package.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
{
22
"name": "pg-connection-string",
3-
"version": "2.7.0",
3+
"version": "2.7.1-alpha.0",
44
"description": "Functions for dealing with a PostgresSQL connection string",
55
"main": "./index.js",
66
"types": "./index.d.ts",
7+
"exports": {
8+
".": {
9+
"types": "./index.d.ts",
10+
"import": "./esm/index.mjs",
11+
"require": "./index.js",
12+
"default": "./index.js"
13+
}
14+
},
715
"scripts": {
816
"test": "istanbul cover _mocha && npm run check-coverage",
917
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --lines 100 --functions 100",
@@ -34,6 +42,7 @@
3442
},
3543
"files": [
3644
"index.js",
37-
"index.d.ts"
45+
"index.d.ts",
46+
"esm"
3847
]
3948
}

packages/pg-cursor/esm/index.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ESM wrapper for pg-cursor
2+
import Cursor from '../index.js'
3+
4+
// Export as default only to match CJS module
5+
export default Cursor

packages/pg-cursor/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
2-
const Result = require('pg/lib/result.js')
3-
const prepare = require('pg/lib/utils.js').prepareValue
2+
const pg = require('pg')
3+
const { Result, utils } = pg
4+
const prepare = utils.prepareValue
45
const EventEmitter = require('events').EventEmitter
56
const util = require('util')
67

packages/pg-cursor/package.json

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
{
22
"name": "pg-cursor",
3-
"version": "2.13.1",
3+
"version": "2.13.2-alpha.1",
44
"description": "Query cursor extension for node-postgres",
55
"main": "index.js",
6+
"exports": {
7+
".": {
8+
"import": "./esm/index.mjs",
9+
"require": "./index.js",
10+
"default": "./index.js"
11+
}
12+
},
613
"directories": {
714
"test": "test"
815
},
@@ -18,9 +25,13 @@
1825
"license": "MIT",
1926
"devDependencies": {
2027
"mocha": "^10.5.2",
21-
"pg": "^8.14.1"
28+
"pg": "^8.14.2-alpha.1"
2229
},
2330
"peerDependencies": {
2431
"pg": "^8"
25-
}
32+
},
33+
"files": [
34+
"index.js",
35+
"esm"
36+
]
2637
}

packages/pg-esm-test/package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "pg-esm-test",
3+
"version": "1.0.1-alpha.1",
4+
"description": "A test module for PostgreSQL with ESM support",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "node --test"
9+
},
10+
"keywords": [
11+
"postgres",
12+
"postgresql",
13+
"esm",
14+
"test"
15+
],
16+
"devDependencies": {
17+
"pg": "^8.14.2-alpha.1",
18+
"pg-cloudflare": "^1.1.2-alpha.1",
19+
"pg-cursor": "^2.13.2-alpha.1",
20+
"pg-native": "^3.3.1-alpha.0",
21+
"pg-pool": "^3.8.1-alpha.1",
22+
"pg-query-stream": "^4.8.2-alpha.1"
23+
},
24+
"author": "Brian M. Carlson <[email protected]>",
25+
"license": "MIT"
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import assert from 'node:assert'
2+
import { describe, it } from 'node:test'
3+
import { CloudflareSocket } from 'pg-cloudflare'
4+
5+
describe('pg-cloudflare', () => {
6+
it('should export CloudflareSocket constructor', () => {
7+
assert.ok(new CloudflareSocket())
8+
})
9+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import assert from 'node:assert'
2+
import { describe, it } from 'node:test'
3+
import { parse, toClientConfig, parseIntoClientConfig } from 'pg-connection-string'
4+
5+
describe('pg-connection-string', () => {
6+
it('should export parse function', () => {
7+
assert.strictEqual(typeof parse, 'function')
8+
})
9+
10+
it('should export toClientConfig function', () => {
11+
assert.strictEqual(typeof toClientConfig, 'function')
12+
})
13+
14+
it('should export parseIntoClientConfig function', () => {
15+
assert.strictEqual(typeof parseIntoClientConfig, 'function')
16+
})
17+
})
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import assert from 'node:assert'
2+
import { describe, it } from 'node:test'
3+
import Cursor from 'pg-cursor'
4+
5+
describe('pg-cursor', () => {
6+
it('should export Cursor constructor as default', () => {
7+
assert.ok(new Cursor())
8+
})
9+
})
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import assert from 'node:assert'
2+
import { describe, it } from 'node:test'
3+
import Client from 'pg-native'
4+
5+
describe('pg-native', () => {
6+
it('should export Client constructor', () => {
7+
assert.ok(new Client())
8+
})
9+
})

packages/pg-esm-test/pg-pool.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import assert from 'node:assert'
2+
import { describe, it } from 'node:test'
3+
import Pool from 'pg-pool'
4+
5+
describe('pg-pool', () => {
6+
it('should export Pool constructor', () => {
7+
assert.ok(new Pool())
8+
})
9+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import assert from 'node:assert'
2+
import { describe, it } from 'node:test'
3+
import QueryStream from 'pg-query-stream'
4+
5+
describe('pg-query-stream', () => {
6+
it('should export QueryStream constructor as default', () => {
7+
assert.ok(new QueryStream())
8+
})
9+
})

packages/pg-esm-test/pg.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import assert from 'node:assert'
2+
import { describe, it } from 'node:test'
3+
import pg, { Client, Pool } from 'pg'
4+
5+
describe('pg', () => {
6+
it('should export Client constructor', () => {
7+
assert.ok(new Client())
8+
})
9+
10+
it('should export Pool constructor', () => {
11+
assert.ok(new Pool())
12+
})
13+
14+
it('should still provide default export', () => {
15+
assert.ok(new pg.Pool())
16+
})
17+
})

packages/pg-native/esm/index.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ESM wrapper for pg-native
2+
import Client from '../index.js'
3+
4+
// Export as default only to match CJS module
5+
export default Client

packages/pg-native/package.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
{
22
"name": "pg-native",
3-
"version": "3.3.0",
3+
"version": "3.3.1-alpha.0",
44
"description": "A slightly nicer interface to Postgres over node-libpq",
55
"main": "index.js",
6+
"exports": {
7+
".": {
8+
"import": "./esm/index.mjs",
9+
"require": "./index.js",
10+
"default": "./index.js"
11+
}
12+
},
613
"scripts": {
714
"test": "mocha"
815
},
@@ -34,5 +41,9 @@
3441
"node-gyp": ">=10.x",
3542
"okay": "^0.3.0",
3643
"semver": "^4.1.0"
37-
}
44+
},
45+
"files": [
46+
"index.js",
47+
"esm"
48+
]
3849
}

packages/pg-pool/esm/index.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// ESM wrapper for pg-pool
2+
import Pool from '../index.js'
3+
4+
// Export as default only to match CJS module
5+
export default Pool

packages/pg-pool/package.json

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
{
22
"name": "pg-pool",
3-
"version": "3.8.0",
3+
"version": "3.8.1-alpha.1",
44
"description": "Connection pool for node-postgres",
55
"main": "index.js",
6+
"exports": {
7+
".": {
8+
"import": "./esm/index.mjs",
9+
"require": "./index.js",
10+
"default": "./index.js"
11+
}
12+
},
613
"directories": {
714
"test": "test"
815
},
@@ -32,9 +39,13 @@
3239
"expect.js": "0.3.1",
3340
"lodash": "^4.17.11",
3441
"mocha": "^10.5.2",
35-
"pg-cursor": "^1.3.0"
42+
"pg-cursor": "^2.13.2-alpha.1"
3643
},
3744
"peerDependencies": {
3845
"pg": ">=8.0"
39-
}
46+
},
47+
"files": [
48+
"index.js",
49+
"esm"
50+
]
4051
}

packages/pg-protocol/esm/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ESM wrapper for pg-protocol
2+
import protocol from '../dist/index.js'
3+
4+
// Re-export all the properties
5+
export const DatabaseError = protocol.DatabaseError
6+
export const SASL = protocol.SASL
7+
export const serialize = protocol.serialize
8+
export const parse = protocol.parse
9+
10+
// Re-export the default
11+
export default protocol

packages/pg-protocol/package.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
{
22
"name": "pg-protocol",
3-
"version": "1.8.0",
3+
"version": "1.8.1-alpha.0",
44
"description": "The postgres client/server binary protocol, implemented in TypeScript",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
7+
"exports": {
8+
".": {
9+
"import": "./esm/index.js",
10+
"require": "./dist/index.js",
11+
"default": "./dist/index.js"
12+
}
13+
},
714
"license": "MIT",
815
"devDependencies": {
916
"@types/chai": "^4.2.7",
@@ -29,6 +36,7 @@
2936
},
3037
"files": [
3138
"/dist/*{js,ts,map}",
32-
"/src"
39+
"/src",
40+
"/esm"
3341
]
3442
}

packages/pg-protocol/tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
3+
"module": "node16",
44
"esModuleInterop": true,
55
"allowSyntheticDefaultImports": true,
66
"strict": true,
77
"target": "es6",
88
"noImplicitAny": true,
9-
"moduleResolution": "node",
9+
"moduleResolution": "node16",
1010
"sourceMap": true,
1111
"outDir": "dist",
1212
"incremental": true,

0 commit comments

Comments
 (0)