Skip to content

Commit 5295273

Browse files
committed
use cli subdir. more re-use of code
1 parent 8c4af32 commit 5295273

21 files changed

+189
-289
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Integration tests
1+
name: e2e tests
22

33
on:
44
workflow_dispatch:
@@ -12,7 +12,7 @@ jobs:
1212
test:
1313
runs-on: ubuntu-latest
1414
strategy:
15-
# Integration tests are not yet ready to run in parallel
15+
# e2e tests are not yet ready to run in parallel
1616
max-parallel: 1
1717
matrix:
1818
node:
@@ -33,7 +33,7 @@ jobs:
3333
# - name: Setup tmate session
3434
# uses: mxschmitt/action-tmate@v3
3535

36-
- run: corepack yarn test:all
36+
- run: corepack yarn test
3737
env:
3838
TRANSLOADIT_KEY: ${{ secrets.TRANSLOADIT_KEY }}
3939
TRANSLOADIT_SECRET: ${{ secrets.TRANSLOADIT_SECRET }}

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ yarn test:unit
5050

5151
This will also generate a coverage report in the `coverage` directory.
5252

53-
### Integration tests
53+
### e2e tests
5454

55-
Integration tests are in the [`test/integration`](test/integration) folder. They require some extra setup.
55+
e2e tests are in the [`test/e2e`](test/e2e) folder. They require some extra setup.
5656

5757
Firstly, these tests require the Cloudflare executable. You can download this with:
5858

@@ -63,10 +63,10 @@ chmod +x cloudflared-linux-amd64
6363

6464
They also require a Transloadit key and secret, which you can get from https://transloadit.com/c/credentials.
6565

66-
You can run the integration tests with:
66+
You can run the e2e tests with:
6767

6868
```sh
69-
TRANSLOADIT_KEY='YOUR_TRANSLOADIT_KEY' TRANSLOADIT_SECRET='YOUR_TRANSLOADIT_SECRET' CLOUDFLARED_PATH='./cloudflared-linux-amd64' yarn test:integration
69+
TRANSLOADIT_KEY='YOUR_TRANSLOADIT_KEY' TRANSLOADIT_SECRET='YOUR_TRANSLOADIT_SECRET' CLOUDFLARED_PATH='./cloudflared-linux-amd64' yarn test:e2e
7070
```
7171

7272
### Code Coverage

MIGRATION.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Version 4 focuses on type-safety, clearer errors, and modern Node support. Most
1919
```js
2020
// CommonJS import example
2121
async function getClient() {
22-
const { Transloadit } = await import("transloadit");
22+
const { Transloadit } = await import('transloadit')
2323
return new Transloadit({
24-
authKey: process.env.TRANSLOADIT_KEY ?? "",
25-
authSecret: process.env.TRANSLOADIT_SECRET ?? "",
26-
});
24+
authKey: process.env.TRANSLOADIT_KEY ?? '',
25+
authSecret: process.env.TRANSLOADIT_SECRET ?? '',
26+
})
2727
}
2828
```
2929
@@ -48,16 +48,16 @@ The package also exports `AssemblyInstructionsInput`, `AssemblyIndexItem`, `Asse
4848
const params: AssemblyInstructionsInput = {
4949
steps: {
5050
resize: {
51-
use: ":original",
52-
robot: "/image/resize",
51+
use: ':original',
52+
robot: '/image/resize',
5353
width: 320,
5454
height: 240,
5555
result: true,
5656
},
5757
},
58-
};
58+
}
5959

60-
await transloadit.createAssembly({ params, waitForCompletion: true });
60+
await transloadit.createAssembly({ params, waitForCompletion: true })
6161
```
6262
6363
## 3. Adjust API result handling
@@ -82,15 +82,15 @@ const createdAssembly = await transloadit.createAssembly(...);
8282
8383
```ts
8484
try {
85-
await transloadit.createAssembly({ params });
85+
await transloadit.createAssembly({ params })
8686
} catch (error) {
8787
if (error instanceof ApiError && error.response.assembly_id) {
8888
console.error(
89-
"Troubleshoot at https://transloadit.com/c/assemblies/" +
89+
'Troubleshoot at https://transloadit.com/c/assemblies/' +
9090
error.response.assembly_id
91-
);
91+
)
9292
}
93-
throw error;
93+
throw error
9494
}
9595
```
9696
@@ -103,18 +103,18 @@ try {
103103
authKey,
104104
authSecret,
105105
validateResponses: true,
106-
});
106+
})
107107
```
108108
109109
- `getSignedSmartCDNUrl` generates Smart CDN URLs with signatures that match the server-side implementation:
110110
111111
```ts
112112
const signedUrl = transloadit.getSignedSmartCDNUrl({
113-
workspace: "my-team",
114-
template: "hero-image",
115-
input: "landing.jpg",
116-
urlParams: { format: "webp" },
117-
});
113+
workspace: 'my-team',
114+
template: 'hero-image',
115+
input: 'landing.jpg',
116+
urlParams: { format: 'webp' },
117+
})
118118
```
119119
120120
## 6. Removed `createAssembly` callback support
@@ -135,7 +135,7 @@ As a consequence of upgrading `got` to v14, the `gotRetry` option no longer acce
135135
136136
## Testing & troubleshooting
137137
138-
- Run your existing integration tests on Node 20+. If you relied on CommonJS `require`, convert those modules or wrap calls in `import()` shims as shown above.
138+
- Run your existing e2e tests on Node 20+. If you relied on CommonJS `require`, convert those modules or wrap calls in `import()` shims as shown above.
139139
- If TypeScript raises errors about unfamiliar properties, import the respective types from `transloadit` instead of redefining them.
140140
- Schemas intentionally mirror the current public API. Some properties remain permissive while we tighten validation in the API itself; report gaps if the SDK raises or misses invalid data.
141141

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,10 @@
6767
"lint:js": "biome check .",
6868
"lint": "npm-run-all --parallel 'lint:js'",
6969
"fix": "npm-run-all --serial 'fix:js'",
70-
"next:update": "next-update --keep true --tldr",
7170
"prepack": "rm -f tsconfig.tsbuildinfo tsconfig.build.tsbuildinfo && tsc --build tsconfig.build.json",
7271
"test:unit": "vitest run --coverage ./test/unit",
73-
"test:integration": "vitest run ./test/integration",
7472
"test:e2e": "vitest run ./test/e2e",
75-
"test:all": "vitest run --coverage",
76-
"test": "yarn test:unit"
73+
"test": "vitest run --coverage"
7774
},
7875
"license": "MIT",
7976
"main": "./dist/Transloadit.js",

src/cli/JobsPromise.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
1-
import { EventEmitter } from 'node:events'
1+
/**
2+
* Tracks a collection of promises and emits errors as they occur.
3+
* Used to run multiple async operations in parallel while:
4+
* 1. Reporting errors as they happen (via onError callback)
5+
* 2. Waiting for all operations to complete at the end
6+
*/
7+
export default class JobsPromise {
8+
private promises: Set<Promise<unknown>> = new Set()
9+
private onError: ((err: unknown) => void) | null = null
210

3-
export default class JobsPromise extends EventEmitter {
4-
private promises: Set<Promise<unknown>>
5-
6-
constructor() {
7-
super()
8-
this.promises = new Set()
11+
/**
12+
* Set the error handler for individual promise rejections.
13+
* Errors are reported immediately when promises reject.
14+
*/
15+
setErrorHandler(handler: (err: unknown) => void): void {
16+
this.onError = handler
917
}
1018

19+
/**
20+
* Add a promise to track. If the promise rejects,
21+
* the error handler will be called.
22+
*/
1123
add(promise: Promise<unknown>): void {
1224
this.promises.add(promise)
1325
promise
14-
.finally(() => this.promises.delete(promise))
1526
.catch((err: unknown) => {
16-
this.emit('error', err)
27+
this.onError?.(err)
28+
})
29+
.finally(() => {
30+
this.promises.delete(promise)
1731
})
1832
}
1933

20-
promise(): Promise<unknown[]> {
21-
const promises: Promise<unknown>[] = []
22-
for (const promise of this.promises) {
23-
promises.push(promise)
24-
}
25-
return Promise.all(promises)
34+
/**
35+
* Wait for all tracked promises to settle.
36+
* Returns array of fulfilled values (rejects are filtered out
37+
* since errors were already handled via the error handler).
38+
*/
39+
async allSettled(): Promise<unknown[]> {
40+
const promises = [...this.promises]
41+
const results = await Promise.allSettled(promises)
42+
return results
43+
.filter((r): r is PromiseFulfilledResult<unknown> => r.status === 'fulfilled')
44+
.map((r) => r.value)
2645
}
2746
}

src/cli/assemblies-create.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ function dismissStaleJobs(jobEmitter: EventEmitter): MyEventEmitter {
449449

450450
const jobsPromise = new JobsPromise()
451451

452-
jobEmitter.on('end', () => jobsPromise.promise().then(() => emitter.emit('end')))
452+
jobEmitter.on('end', () => jobsPromise.allSettled().then(() => emitter.emit('end')))
453453
jobEmitter.on('error', (err: Error) => emitter.emit('error', err))
454454
jobEmitter.on('job', (job: Job) => {
455455
if (job.in == null || job.out == null) {
@@ -730,8 +730,8 @@ export default async function run(
730730
}
731731
})
732732

733-
jobsPromise.on('error', (err: Error) => {
734-
outputctl.error(err)
733+
jobsPromise.setErrorHandler((err: unknown) => {
734+
outputctl.error(err as Error)
735735
})
736736

737737
emitter.on('error', (err: Error) => {
@@ -740,7 +740,7 @@ export default async function run(
740740
})
741741

742742
emitter.on('end', () => {
743-
resolve(jobsPromise.promise())
743+
resolve(jobsPromise.allSettled())
744744
})
745745
})
746746
}

src/cli/assemblies.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { z } from 'zod'
22
import { tryCatch } from '../alphalib/tryCatch.ts'
33
import type { Transloadit } from '../Transloadit.ts'
44
import assembliesCreate from './assemblies-create.ts'
5-
import { createReadStream, formatAPIError, stream2buf } from './helpers.ts'
5+
import { createReadStream, formatAPIError, streamToBuffer } from './helpers.ts'
66
import type { IOutputCtl } from './OutputCtl.ts'
77
import { ensureError } from './types.ts'
88

@@ -112,13 +112,7 @@ export async function replay(
112112
): Promise<void> {
113113
if (steps) {
114114
try {
115-
const buf = await new Promise<Buffer>((resolve, reject) => {
116-
stream2buf(createReadStream(steps), (err, buf) => {
117-
if (err) reject(err)
118-
else if (buf) resolve(buf)
119-
else reject(new Error('No buffer received'))
120-
})
121-
})
115+
const buf = await streamToBuffer(createReadStream(steps))
122116
const parsed: unknown = JSON.parse(buf.toString())
123117
const validated = StepsSchema.safeParse(parsed)
124118
if (!validated.success) {

0 commit comments

Comments
 (0)