Skip to content

Commit a39f601

Browse files
committed
Fix packaging
1 parent b0dc546 commit a39f601

File tree

6 files changed

+58
-16
lines changed

6 files changed

+58
-16
lines changed

Diff for: ava.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module.exports = {
22
files: ["src/**/*.test.ts"],
33
extensions: ["ts"],
44
require: ["esbuild-register"],
5+
environmentVariables: {
6+
IS_TESTING_AVA_POSTGRES: "true",
7+
},
58
}

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"dist"
1818
],
1919
"version": "0.1.0",
20-
"main": "index.js",
20+
"main": "dist/index.js",
21+
"module": "./dist/index.mjs",
22+
"types": "./dist/index.d.ts",
2123
"repository": "[email protected]:seamapi/ava-postgres.git",
2224
"author": "Max Isom <[email protected]>",
2325
"license": "MIT",

Diff for: src/index.ts

+33-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { SharedWorker } from "ava/plugin"
1+
import { registerSharedWorker, SharedWorker } from "ava/plugin"
22
import hash from "object-hash"
33
import path from "node:path"
4-
import { registerSharedTypeScriptWorker } from "ava-typescript-worker"
54
import {
65
ConnectionDetailsFromWorker,
76
InitialWorkerData,
@@ -25,6 +24,36 @@ const mapWorkerConnectionDetailsToConnectionDetails = (
2524
}),
2625
})
2726

27+
const getWorker = async (
28+
initialData: InitialWorkerData,
29+
options?: GetTestPostgresDatabaseFactoryOptions<any>
30+
) => {
31+
const key = hash({
32+
initialData,
33+
key: options?.key,
34+
})
35+
36+
if (process.env.IS_TESTING_AVA_POSTGRES) {
37+
const { registerSharedTypeScriptWorker } = await import(
38+
"ava-typescript-worker"
39+
)
40+
return registerSharedTypeScriptWorker({
41+
filename: new URL(
42+
`file:${path.resolve(__dirname, "worker-wrapper.ts")}#${key}`
43+
),
44+
initialData: initialData as any,
45+
})
46+
}
47+
48+
return registerSharedWorker({
49+
filename: new URL(
50+
`file:${path.resolve(__dirname, "worker-wrapper.mjs")}#${key}`
51+
),
52+
initialData: initialData as any,
53+
supportedProtocols: ["ava-4"],
54+
})
55+
}
56+
2857
export const getTestPostgresDatabaseFactory = <
2958
Params extends JsonObject = never
3059
>(
@@ -35,19 +64,12 @@ export const getTestPostgresDatabaseFactory = <
3564
containerOptions: options?.container,
3665
}
3766

38-
const worker = registerSharedTypeScriptWorker({
39-
filename: new URL(
40-
`file:${path.resolve(__dirname, "worker-wrapper.ts")}#${hash({
41-
initialData,
42-
key: options?.key,
43-
})}`
44-
),
45-
initialData: initialData as any,
46-
})
67+
const workerPromise = getWorker(initialData, options)
4768

4869
const getTestPostgresDatabase: GetTestPostgresDatabase<Params> = async (
4970
params
5071
) => {
72+
const worker = await workerPromise
5173
await worker.available
5274

5375
const waitForAndHandleReply = async (

Diff for: src/worker-wrapper.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import { SharedWorker } from "ava/plugin"
22
import { Worker } from "./worker"
33

4-
const workerWrapper = async (protocol: SharedWorker.Protocol) => {
4+
const needsToNegotiateProtocol = (
5+
arg: SharedWorker.FactoryOptions | SharedWorker.Protocol
6+
): arg is SharedWorker.FactoryOptions => {
7+
return (
8+
typeof (arg as SharedWorker.FactoryOptions).negotiateProtocol === "function"
9+
)
10+
}
11+
12+
const workerWrapper = async (
13+
arg: SharedWorker.FactoryOptions | SharedWorker.Protocol
14+
) => {
15+
const protocol = needsToNegotiateProtocol(arg)
16+
? arg.negotiateProtocol(["ava-4"]).ready()
17+
: arg
18+
519
const { initialData } = protocol
620

721
const worker = new Worker(initialData as any)

Diff for: src/worker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Pool } from "pg"
1+
import pg from "pg"
22
import { GenericContainer } from "testcontainers"
33
import { Mutex } from "async-mutex"
44
import hash from "object-hash"
@@ -216,7 +216,7 @@ export class Worker {
216216

217217
return {
218218
container: startedContainer,
219-
postgresClient: new Pool({
219+
postgresClient: new pg.Pool({
220220
connectionString: `postgresql://postgres:@${startedContainer.getHost()}:${startedContainer.getMappedPort(
221221
5432
222222
)}/postgres`,

Diff for: tsup.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { defineConfig } from "tsup"
22

33
export default defineConfig({
4-
entry: ["src/index.ts"],
4+
entry: ["src/index.ts", "src/worker-wrapper.ts"],
55
dts: true,
66
sourcemap: true,
77
format: ["cjs", "esm"],
8+
external: ["ava-typescript-worker"],
89
})

0 commit comments

Comments
 (0)