Open History to see the last commit
1import { spawnSync } from 'node:child_process'
2import { mkdir, readFile, writeFile, readdir, rm } from 'node:fs/promises'
3import { createHash } from 'node:crypto'
4import path from 'node:path'
5import { fileURLToPath } from 'node:url'
6import assert from 'node:assert/strict'
7
8const root = fileURLToPath(new URL('../', import.meta.url))
9const artifacts = path.join(root, '.artifacts/package-compatibility')
10const packs = path.join(artifacts, 'packs')
11await rm(artifacts, { recursive: true, force: true })
12await mkdir(packs, { recursive: true })
13run(
14 'pnpm',
15 ['--dir', 'packages/client', 'pack', '--pack-destination', packs],
16 root
17)
18const packed = (await readdir(packs)).filter((name) => name.endsWith('.tgz'))
19assert.equal(packed.length, 1, 'Expected exactly one candidate tarball')
20const candidate = path.join(packs, packed[0])
21const consumerBytes = await readFile(
22 path.join(root, 'tests/compatibility/consumer.ts')
23)
24const consumerHash = createHash('sha256').update(consumerBytes).digest('hex')
25const reports = {}
26
27for (const kind of ['reference', 'candidate']) {
28 const cwd = path.join(artifacts, kind)
29 await mkdir(cwd, { recursive: true })
30 await writeFile(
31 path.join(cwd, 'package.json'),
32 JSON.stringify(
33 {
34 name: `compatibility-${kind}`,
35 private: true,
36 type: 'module',
37 dependencies: {
38 '@electric-sql/client':
39 kind === 'reference' ? '1.5.28' : `file:${candidate}`,
40 typescript: '5.9.3',
41 'typescript-minimum': 'npm:typescript@5.5.4',
42 },
43 },
44 null,
45 2
46 ) + '\n'
47 )
48 await writeFile(path.join(cwd, 'consumer.ts'), consumerBytes)
49 await writeFile(
50 path.join(cwd, 'tsconfig.json'),
51 JSON.stringify(
52 {
53 compilerOptions: {
54 strict: true,
55 skipLibCheck: false,
56 noEmit: true,
57 target: 'ES2022',
58 lib: ['ES2022', 'DOM', 'DOM.Iterable'],
59 module: 'ESNext',
60 moduleResolution: 'Bundler',
61 types: [],
62 },
63 include: ['consumer.ts'],
64 },
65 null,
66 2
67 ) + '\n'
68 )
69 run(
70 'pnpm',
71 ['install', '--ignore-workspace', '--ignore-scripts', '--prefer-offline'],
72 cwd
73 )
74 for (const compiler of ['typescript', 'typescript-minimum']) {
75 run(
76 process.execPath,
77 [`node_modules/${compiler}/bin/tsc`, '-p', 'tsconfig.json'],
78 cwd
79 )
80 }
81 const packageRoot = path.join(cwd, 'node_modules/@electric-sql/client')
82 const metadata = JSON.parse(
83 await readFile(path.join(packageRoot, 'package.json'), 'utf8')
84 )
85 assert.equal(
86 metadata.name,
87 kind === 'reference'
88 ? '@electric-sql/client'
89 : '@neon/realtime-electric-client'
90 )
91 const probe = (moduleType) => `
92 ${moduleType === 'esm' ? "import * as api from '@electric-sql/client';" : "const api = require('@electric-sql/client');"}
93 const stream = new api.ShapeStream({ url: 'https://example.test/api/shape?tenant=one', subscribe: false });
94 if (stream.hasStarted()) throw new Error('Constructor unexpectedly started the stream');
95 if (typeof stream.isPaused() !== 'boolean') throw new Error('isPaused contract missing');
96 if (typeof stream.fetchSnapshot !== 'function') throw new Error('fetchSnapshot contract missing');
97 console.log(JSON.stringify({
98 exports: Object.keys(api).filter(key => key !== 'default').sort(),
99 stream: Object.getOwnPropertyNames(api.ShapeStream.prototype).sort(),
100 shape: Object.getOwnPropertyNames(api.Shape.prototype).sort(),
101 params: api.ELECTRIC_PROTOCOL_QUERY_PARAMS,
102 replica: api.ShapeStream.Replica,
103 optionsUrl: stream.options.url,
104 mode: stream.mode
105 }));
106 `
107 const esm = JSON.parse(
108 run(
109 process.execPath,
110 ['--input-type=module', '-e', probe('esm')],
111 cwd,
112 true
113 ).trim()
114 )
115 const cjs = JSON.parse(
116 run(
117 process.execPath,
118 ['--input-type=commonjs', '-e', probe('cjs')],
119 cwd,
120 true
121 ).trim()
122 )
123 assert.deepEqual(esm, cjs, `${kind}: ESM/CJS public runtime differences`)
124 for (const condition of ['browser', 'react-native']) {
125 const result = run(
126 process.execPath,
127 [
128 `--conditions=${condition}`,
129 '--input-type=module',
130 '-e',
131 "console.log(import.meta.resolve('@electric-sql/client'))",
132 ],
133 cwd,
134 true
135 ).trim()
136 assert.ok(
137 result.endsWith(
138 condition === 'browser'
139 ? '/dist/index.mjs'
140 : '/dist/index.react-native.mjs'
141 ),
142 result
143 )
144 }
145 const nativeSubpath = run(
146 process.execPath,
147 [
148 '--input-type=module',
149 '-e',
150 "console.log(import.meta.resolve('@electric-sql/client/react-native'))",
151 ],
152 cwd,
153 true
154 ).trim()
155 assert.ok(nativeSubpath.endsWith('/dist/index.react-native.mjs'))
156 reports[kind] = {
157 ...esm,
158 consumerHash,
159 packageName: metadata.name,
160 packageVersion: metadata.version,
161 }
162}
163const {
164 packageName: referenceName,
165 packageVersion: referenceVersion,
166 ...reference
167} = reports.reference
168const {
169 packageName: candidateName,
170 packageVersion: candidateVersion,
171 ...replacement
172} = reports.candidate
173assert.deepEqual(
174 replacement,
175 reference,
176 'Replacement package public runtime differs from reference'
177)
178const interop = path.join(artifacts, 'interop')
179await mkdir(interop, { recursive: true })
180await writeFile(
181 path.join(interop, 'package.json'),
182 JSON.stringify({
183 name: 'compatibility-interop',
184 private: true,
185 type: 'module',
186 dependencies: {
187 'electric-reference': 'npm:@electric-sql/client@1.5.28',
188 'electric-candidate': `file:${candidate}`,
189 typescript: '5.9.3',
190 'typescript-minimum': 'npm:typescript@5.5.4',
191 },
192 }) + '\n'
193)
194const interopConsumer = await readFile(
195 path.join(root, 'tests/compatibility/interop/consumer.ts')
196)
197await writeFile(path.join(interop, 'consumer.ts'), interopConsumer)
198await writeFile(
199 path.join(interop, 'tsconfig.json'),
200 await readFile(path.join(artifacts, 'candidate/tsconfig.json'))
201)
202run(
203 'pnpm',
204 ['install', '--ignore-workspace', '--ignore-scripts', '--prefer-offline'],
205 interop
206)
207for (const compiler of ['typescript', 'typescript-minimum']) {
208 run(
209 process.execPath,
210 [`node_modules/${compiler}/bin/tsc`, '-p', 'tsconfig.json'],
211 interop
212 )
213}
214run(
215 process.execPath,
216 ['--import', 'tsx', 'tests/compatibility/interop/runtime.mjs', interop],
217 root
218)
219reports.interop = {
220 consumerHash: createHash('sha256').update(interopConsumer).digest('hex'),
221 compilers: ['5.5.4', '5.9.3'],
222 runtime: 'ESM/CJS original Shape consuming packed candidate ShapeStream',
223 source:
224 'controlled events through production handler and Durable Streams reader',
225}
226await writeFile(
227 path.join(artifacts, 'report.json'),
228 JSON.stringify(reports, null, 2) + '\n'
229)
230console.log(
231 'Packed replacement: consumer and cross-package types compile on TS 5.5.4/5.9.3; ESM/CJS resolution and original Shape interoperability pass'
232)
233
234function run(command, args, cwd, capture = false) {
235 const result = spawnSync(command, args, {
236 cwd,
237 encoding: 'utf8',
238 stdio: capture ? ['ignore', 'pipe', 'inherit'] : 'inherit',
239 env: { ...process.env, CI: 'true' },
240 })
241 if (result.error) throw result.error
242 assert.equal(result.status, 0, `${command} ${args.join(' ')} failed`)
243 return result.stdout
244}