@livestore/utils-dev 0.0.0-snapshot-8452e32b7fbfc129741b253b9c853f866b52129f.1 → 0.0.0-snapshot-f257c8dcda25d0c2b1d1e65e64e723ec930e992e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/.tsbuildinfo.json +1 -1
  2. package/dist/node-vitest/Vitest.d.ts +1 -1
  3. package/dist/node-vitest/Vitest.d.ts.map +1 -1
  4. package/dist/node-vitest/Vitest.js +3 -6
  5. package/dist/node-vitest/Vitest.js.map +1 -1
  6. package/dist/node-vitest/mod.d.ts +0 -2
  7. package/dist/node-vitest/mod.d.ts.map +1 -1
  8. package/dist/node-vitest/mod.js +0 -2
  9. package/dist/node-vitest/mod.js.map +1 -1
  10. package/package.json +4 -4
  11. package/src/node-vitest/Vitest.ts +4 -6
  12. package/src/node-vitest/mod.ts +0 -2
  13. package/dist/node-vitest/polyfill.d.ts +0 -2
  14. package/dist/node-vitest/polyfill.d.ts.map +0 -1
  15. package/dist/node-vitest/polyfill.js +0 -3
  16. package/dist/node-vitest/polyfill.js.map +0 -1
  17. package/dist/node-vitest/vitest-docker-compose-setup.d.ts +0 -31
  18. package/dist/node-vitest/vitest-docker-compose-setup.d.ts.map +0 -1
  19. package/dist/node-vitest/vitest-docker-compose-setup.js +0 -124
  20. package/dist/node-vitest/vitest-docker-compose-setup.js.map +0 -1
  21. package/dist/node-vitest/vitest-wrangler-setup copy.d.ts +0 -7
  22. package/dist/node-vitest/vitest-wrangler-setup copy.d.ts.map +0 -1
  23. package/dist/node-vitest/vitest-wrangler-setup copy.js +0 -77
  24. package/dist/node-vitest/vitest-wrangler-setup copy.js.map +0 -1
  25. package/dist/node-vitest/vitest-wrangler-setup.d.ts +0 -27
  26. package/dist/node-vitest/vitest-wrangler-setup.d.ts.map +0 -1
  27. package/dist/node-vitest/vitest-wrangler-setup.js +0 -93
  28. package/dist/node-vitest/vitest-wrangler-setup.js.map +0 -1
  29. package/src/node-vitest/vitest-docker-compose-setup.ts +0 -171
  30. package/src/node-vitest/vitest-wrangler-setup.ts +0 -131
@@ -1,171 +0,0 @@
1
- import { spawn } from 'node:child_process'
2
- import path from 'node:path'
3
- import { Effect, UnknownError } from '@livestore/utils/effect'
4
-
5
- import { afterAll } from 'vitest'
6
-
7
- export const startDockerComposeServices = (args: StartDockerComposeServicesArgs) =>
8
- Effect.tryPromise({
9
- try: () => startDockerComposeServicesPromise(args),
10
- catch: (error) => UnknownError.make({ cause: new Error(`Failed to start Docker Compose: ${error}`) }),
11
- }).pipe(Effect.withSpan('startDockerComposeServices'))
12
-
13
- type StartDockerComposeServicesArgs = {
14
- composeFilePath?: string
15
- cwd: string
16
- serviceName?: string
17
- waitForLog?: string
18
- env?: Record<string, string>
19
- healthCheck?: {
20
- url: string // URL template with ${port} placeholder
21
- expectedStatus?: number // default 200
22
- maxAttempts?: number // default 30
23
- delayMs?: number // default 1000
24
- }
25
- /** @default false */
26
- forwardLogs?: boolean
27
- }
28
-
29
- /**
30
- * Starts Docker Compose services for testing with automatic cleanup.
31
- * Automatically allocates a free port and passes it as EXPOSED_PORT environment variable.
32
- *
33
- * @param composeFilePath - Path to docker-compose.yml file (defaults to `${cwd}/docker-compose.yml`)
34
- * @param cwd - Working directory for Docker Compose commands
35
- * @param env - Environment variables to pass to the Docker Compose commands (useful for passing ports)
36
- * @param serviceName - Optional specific service to start (omit to start all)
37
- * @param waitForLog - Log pattern to wait for before considering services ready
38
- * @param healthCheck - Health check configuration for waiting until service is ready
39
- */
40
- // TODO refactor implementation with Effect
41
- // TODO add test for this
42
- export const startDockerComposeServicesPromise = async ({
43
- composeFilePath,
44
- cwd,
45
- serviceName,
46
- waitForLog,
47
- healthCheck,
48
- env,
49
- forwardLogs = false,
50
- }: StartDockerComposeServicesArgs) => {
51
- let dockerComposeProcess: ReturnType<typeof spawn> | undefined
52
-
53
- const setup = async () => {
54
- const resolvedComposeFilePath = path.resolve(composeFilePath ?? path.join(cwd, 'docker-compose.yml'))
55
-
56
- // Build the docker compose command arguments
57
- const composeArgs = ['compose', '-f', resolvedComposeFilePath, 'up']
58
-
59
- // Add service name if specified
60
- if (serviceName) {
61
- composeArgs.push(serviceName)
62
- }
63
-
64
- dockerComposeProcess = spawn('docker', composeArgs, {
65
- stdio: ['ignore', 'pipe', 'pipe'],
66
- cwd,
67
- env: { ...process.env, ...env },
68
- })
69
-
70
- dockerComposeProcess.stdout?.setEncoding('utf8')
71
- dockerComposeProcess.stderr?.setEncoding('utf8')
72
-
73
- if (forwardLogs) {
74
- dockerComposeProcess.stdout?.on('data', (data: string) => {
75
- console.log(data)
76
- })
77
-
78
- dockerComposeProcess.stderr?.on('data', (data: string) => {
79
- console.error(data)
80
- })
81
- }
82
-
83
- // Wait for the service to be ready
84
- if (healthCheck) {
85
- // Use health check approach
86
- const maxAttempts = healthCheck.maxAttempts ?? 30
87
- const delayMs = healthCheck.delayMs ?? 1000
88
- const expectedStatus = healthCheck.expectedStatus ?? 200
89
- const healthUrl = healthCheck.url
90
-
91
- console.log(`Waiting for health check at ${healthUrl}...`)
92
-
93
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
94
- try {
95
- const response = await fetch(healthUrl)
96
- if (response.status === expectedStatus) {
97
- console.log(`Health check passed after ${attempt} attempts`)
98
- break
99
- }
100
- console.log(`Health check attempt ${attempt}/${maxAttempts}: status ${response.status}`)
101
- } catch (error) {
102
- console.log(
103
- `Health check attempt ${attempt}/${maxAttempts}: ${error instanceof Error ? error.message : 'failed'}`,
104
- )
105
- }
106
-
107
- if (attempt === maxAttempts) {
108
- throw new Error(`Health check failed after ${maxAttempts} attempts at ${healthUrl}`)
109
- }
110
-
111
- await new Promise((resolve) => setTimeout(resolve, delayMs))
112
- }
113
- } else if (waitForLog) {
114
- // Fallback to log-based waiting
115
- await new Promise<void>((resolve) => {
116
- const onData = (data: string) => {
117
- if (data.includes(waitForLog)) {
118
- dockerComposeProcess?.stdout?.off('data', onData)
119
- resolve()
120
- }
121
- }
122
- dockerComposeProcess?.stdout?.on('data', onData)
123
- dockerComposeProcess?.stderr?.on('data', onData)
124
- })
125
- } else {
126
- // No wait condition, just give it a moment to start
127
- await new Promise((resolve) => setTimeout(resolve, 2000))
128
- }
129
-
130
- console.log(`Docker Compose services ready${serviceName ? ` (${serviceName})` : ''}`)
131
- }
132
-
133
- const stopDockerComposeServices = async () => {
134
- if (dockerComposeProcess) {
135
- console.log('Stopping Docker Compose services...')
136
-
137
- const resolvedComposeFilePath = path.resolve(composeFilePath ?? path.join(cwd, 'docker-compose.yml'))
138
-
139
- // Use docker compose down to properly stop and clean up
140
- const downProcess = spawn('docker', ['compose', '-f', resolvedComposeFilePath, 'down'], {
141
- stdio: 'inherit',
142
- cwd,
143
- })
144
-
145
- await new Promise<void>((resolve) => {
146
- downProcess.on('close', () => {
147
- resolve()
148
- })
149
- })
150
-
151
- dockerComposeProcess.kill('SIGTERM')
152
- dockerComposeProcess = undefined
153
- }
154
- }
155
-
156
- process.on('exit', () => {
157
- stopDockerComposeServices()
158
- })
159
- process.on('SIGINT', () => {
160
- stopDockerComposeServices()
161
- })
162
- process.on('SIGTERM', () => {
163
- stopDockerComposeServices()
164
- })
165
-
166
- afterAll(async () => {
167
- await stopDockerComposeServices()
168
- })
169
-
170
- await setup()
171
- }
@@ -1,131 +0,0 @@
1
- import { spawn } from 'node:child_process'
2
- import net from 'node:net'
3
- import path from 'node:path'
4
- import { Effect, UnknownError } from '@livestore/utils/effect'
5
-
6
- import { afterAll } from 'vitest'
7
-
8
- export type StartWranglerDevServerArgs = {
9
- wranglerConfigPath?: string
10
- cwd: string
11
- port?: number
12
- }
13
-
14
- export const startWranglerDevServer = (args: StartWranglerDevServerArgs) =>
15
- Effect.tryPromise({
16
- try: (abortSignal) => startWranglerDevServerPromise({ ...args, abortSignal }),
17
- catch: (error) => UnknownError.make({ cause: new Error(`Failed to start Wrangler: ${error}`) }),
18
- }).pipe(Effect.withSpan('startWranglerDevServer'))
19
-
20
- // TODO refactor implementation with Effect
21
- // TODO add test for this
22
- // TODO allow for config to be passed in via code instead of `wrangler.toml` file
23
- // TODO fix zombie workerd processes causing high CPU usage - see https://github.com/livestorejs/livestore/issues/568
24
- /**
25
- * Starts a Wrangler dev server for testing with automatic cleanup.
26
- *
27
- * @param wranglerConfigPath - Path to wrangler.toml file (defaults to `${cwd}/wrangler.toml`)
28
- * @param cwd - Working directory for Wrangler commands
29
- * @returns Object with allocated port for the dev server
30
- */
31
- export const startWranglerDevServerPromise = async ({
32
- wranglerConfigPath,
33
- abortSignal,
34
- cwd,
35
- port: inputPort,
36
- }: {
37
- wranglerConfigPath?: string
38
- abortSignal?: AbortSignal
39
- cwd: string
40
- port?: number
41
- }) => {
42
- let wranglerProcess: ReturnType<typeof spawn> | undefined
43
-
44
- const getFreePort = (): Promise<number> => {
45
- return new Promise((resolve, reject) => {
46
- const server = net.createServer()
47
- server.listen(0, () => {
48
- const port = (server.address() as net.AddressInfo)?.port
49
- server.close(() => {
50
- if (port) {
51
- resolve(port)
52
- } else {
53
- reject(new Error('Could not get port'))
54
- }
55
- })
56
- })
57
- server.on('error', reject)
58
- })
59
- }
60
-
61
- const setup = async () => {
62
- const syncPort = inputPort ?? (await getFreePort())
63
-
64
- const resolvedWranglerConfigPath = path.resolve(wranglerConfigPath ?? path.join(cwd, 'wrangler.toml'))
65
-
66
- wranglerProcess = spawn(
67
- 'bunx',
68
- ['wrangler', 'dev', '--port', syncPort.toString(), '--config', resolvedWranglerConfigPath],
69
- {
70
- stdio: ['ignore', 'pipe', 'pipe'],
71
- cwd,
72
- env: {
73
- ...process.env,
74
- // NODE_OPTIONS: '--inspect --inspect-port=9233',
75
- },
76
- signal: abortSignal,
77
- },
78
- )
79
-
80
- wranglerProcess.stdout?.setEncoding('utf8')
81
- wranglerProcess.stderr?.setEncoding('utf8')
82
-
83
- wranglerProcess.stdout?.on('data', (data: string) => {
84
- // console.log(`[wrangler] ${data}`)
85
- console.log(data)
86
- })
87
-
88
- wranglerProcess.stderr?.on('data', (data: string) => {
89
- // console.error(`[wrangler] ${data}`)
90
- console.error(data)
91
- })
92
-
93
- await new Promise<void>((resolve) => {
94
- const onData = (data: string) => {
95
- if (data.includes('Ready on')) {
96
- wranglerProcess?.stdout?.off('data', onData)
97
- resolve()
98
- }
99
- }
100
- wranglerProcess?.stdout?.on('data', onData)
101
- })
102
-
103
- console.log(`Wrangler dev server ready on port ${syncPort}`)
104
-
105
- // Wait longer for the Cloudflare Workers runtime to fully initialize
106
- // console.log('Waiting for Cloudflare Workers runtime to fully initialize...')
107
- // await new Promise(resolve => setTimeout(resolve, 10000))
108
-
109
- return { port: syncPort }
110
- }
111
-
112
- const killWranglerProcess = () => {
113
- if (wranglerProcess) {
114
- console.log('Killing wrangler process...')
115
- wranglerProcess.kill('SIGTERM')
116
- wranglerProcess = undefined
117
- }
118
- }
119
-
120
- process.on('exit', killWranglerProcess)
121
- process.on('SIGINT', killWranglerProcess)
122
- process.on('SIGTERM', killWranglerProcess)
123
-
124
- afterAll(() => {
125
- killWranglerProcess()
126
- })
127
-
128
- const { port } = await setup()
129
-
130
- return { port, kill: killWranglerProcess }
131
- }