Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(image-shrink): add fallback test #525

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/image-shrink/src/utils/render/fallback.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// <reference types="vite/client" />
import { describe, expect, it } from 'vitest'
import { fallback } from './fallback'
import { loadImageAsBlob } from '../../test/helpers/loadImageAsBlob'
import { imageLoader } from '../../utils/image/imageLoader'

describe('fallback', () => {
it('should work', async () => {
const originalFile = await loadImageAsBlob(
() => import('../../test/samples/2000x2000.jpeg')
)
const image = await imageLoader(URL.createObjectURL(originalFile))
URL.revokeObjectURL(image.src)

const canvas = await fallback({
img: image,
sourceW: image.width,
targetW: 100,
targetH: 100,
step: 0.71
})

expect(canvas.width).toBe(100)
})

it('should throw if not supported', async () => {
const originalFile = await loadImageAsBlob(
() => import('../../test/samples/2000x2000.jpeg')
)
const image = await imageLoader(URL.createObjectURL(originalFile))
URL.revokeObjectURL(image.src)

const promise = fallback({
img: image,
sourceW: image.width,
targetW: 65535 + 1,
targetH: 65535 + 1,
step: 0.71
})
expect(promise).rejects.toThrow('Not supported')
})
})
17 changes: 10 additions & 7 deletions packages/image-shrink/src/utils/render/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ export const fallback = ({
const steps = calcShrinkSteps({ sourceW, targetW, targetH, step })

return steps.reduce(
(chain, [w, h]) => {
(chain, [w, h], idx) => {
return chain.then((canvas) => {
return (
testCanvasSize(w, h)
.then(() => canvasResize(canvas, w, h))
// Here we assume that at least one step will be supported and HTMLImageElement will be converted to HTMLCanvasElement
.catch(() => canvas as unknown as HTMLCanvasElement)
)
return testCanvasSize(w, h)
.then(() => canvasResize(canvas, w, h))
.catch(() => {
if (idx === steps.length - 1) {
// If the last step is failed then we assume that we can't shrink the image at all
throw new Error('Not supported')
}
return canvas as unknown as HTMLCanvasElement
})
})
},
Promise.resolve(img as HTMLCanvasElement | HTMLImageElement)
Expand Down
5 changes: 4 additions & 1 deletion packages/rest-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ export {
export { getUserAgent } from '@uploadcare/api-client-utils'

/** Tools */
export { RestClientError } from './tools/RestClientError'
export {
RestClientError,
RestClientErrorOptions
} from './tools/RestClientError'
export { paginate, Paginator } from './tools/paginate'
export { addonJobPoller, AddonJobPollerOptions } from './tools/addonJobPoller'
export {
Expand Down
5 changes: 3 additions & 2 deletions packages/upload-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export {
CancelError,
UploadcareError
} from '@uploadcare/api-client-utils'
export { Queue } from './tools/Queue'
export { Queue, Task } from './tools/Queue'

import { NetworkError } from '@uploadcare/api-client-utils'
/** @deprecated Please use NetworkError instead. */
Expand All @@ -90,6 +90,7 @@ export { Headers, ErrorRequestInfo } from './request/types'
export { UploadcareFile } from './tools/UploadcareFile'
export { UploadcareGroup } from './tools/UploadcareGroup'
export { UploadError, ErrorResponseInfo } from './tools/UploadError'
export { ServerErrorCode } from './tools/ServerErrorCode'

import { UploadError } from './tools/UploadError'
/** @deprecated Please use UploadError instead. */
Expand All @@ -114,4 +115,4 @@ export {
ComputableProgressInfo,
UnknownProgressInfo
} from './api/types'
export { isReadyPoll } from './tools/isReadyPoll'
export { isReadyPoll, IsReadyPoolOptions } from './tools/isReadyPoll'
2 changes: 1 addition & 1 deletion packages/upload-client/src/tools/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Task<T = unknown> = () => Promise<T>
export type Task<T = unknown> = () => Promise<T>
type Resolver = (value: unknown) => void
type Rejector = (error: unknown) => void

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"strictNullChecks": true,
"strict": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
Expand Down
Loading