Skip to content

Commit

Permalink
chore: enable noImplicitAny
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut committed Feb 16, 2024
1 parent 3b84f41 commit c64f2ee
Show file tree
Hide file tree
Showing 35 changed files with 264 additions and 173 deletions.
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/api-client-utils/src/camelizeKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function camelizeKeys<T>(
if (!isObject(source)) {
return source
}
const result = {}
const result: Record<string, unknown> = {}
for (const key of Object.keys(source)) {
let value = source[key]
if (ignoreKeys.includes(key)) {
Expand Down
13 changes: 9 additions & 4 deletions packages/image-shrink/src/helper/memoize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export const memoize = (fn, serializer) => {
const cache = {}
return (...args) => {
type FnArgs = [number, number]
type Cache = Record<string, boolean>
type Serializer = (args: FnArgs, cache: Cache) => number
type Fn = (...args: FnArgs) => boolean

export const memoize = (fn: Fn, serializer: Serializer) => {
const cache: Cache = {}
return (...args: FnArgs) => {
const key = serializer(args, cache)
return key in cache ? cache[key] : (cache[key] = fn(...args))
}
Expand All @@ -13,7 +18,7 @@ export const memoize = (fn, serializer) => {
* - Browser supports higher canvas size
* - Browser doesn't support lower canvas size
*/
export const memoKeySerializer = (args, cache) => {
export const memoKeySerializer: Serializer = (args, cache) => {
const [w] = args
const cachedWidths = Object.keys(cache)
.map((val) => parseInt(val, 10))
Expand Down
12 changes: 8 additions & 4 deletions packages/image-shrink/src/utils/canvas/canvasResize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createCanvas } from './createCanvas'

export const canvasResize = (img, w, h) => {
return new Promise((resolve, reject) => {
export const canvasResize = (img: CanvasImageSource, w: number, h: number) => {
return new Promise<HTMLCanvasElement>((resolve, reject) => {
try {
const { ctx, canvas } = createCanvas()

Expand All @@ -11,8 +11,12 @@ export const canvasResize = (img, w, h) => {
ctx.imageSmoothingQuality = 'high'
ctx.drawImage(img, 0, 0, w, h)

img.src = '//:0' // for image
img.width = img.height = 1 // for canvas
if (img instanceof HTMLImageElement) {
img.src = '//:0' // free memory
}
if (img instanceof HTMLCanvasElement) {
img.width = img.height = 1 // free memory
}

resolve(canvas)
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion packages/image-shrink/src/utils/canvas/canvasTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const FILL_STYLE = `rgba(${TestPixel.R}, ${TestPixel.G}, ${TestPixel.B}, ${
})`

type TFillRect = [number, number, number, number]
export const canvasTest = (width, height) => {

export const canvasTest = (width: number, height: number) => {
try {
const fill: TFillRect = [width - 1, height - 1, 1, 1] // x, y, width, height

Expand Down
2 changes: 1 addition & 1 deletion packages/image-shrink/src/utils/canvas/canvasToBlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const canvasToBlob = (
canvas: HTMLCanvasElement,
type: string,
quality: number | undefined,
callback
callback: BlobCallback
): void => {
return canvas.toBlob(callback, type, quality)
}
2 changes: 1 addition & 1 deletion packages/image-shrink/src/utils/canvas/hasTransparency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createCanvas } from './createCanvas'

export const hasTransparency = (img) => {
export const hasTransparency = (img: CanvasImageSource) => {
const canvasSize = 50

// Create a canvas element and get 2D rendering context
Expand Down
6 changes: 3 additions & 3 deletions packages/image-shrink/src/utils/canvas/testCanvasSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { sizes } from '../../constants'
import { memoize, memoKeySerializer } from '../../helper/memoize'
import { canvasTest } from './canvasTest'

function wrapAsync(fn) {
return (...args) => {
function wrapAsync<A extends unknown[], R>(fn: (...args: A) => R) {
return (...args: A) => {
return new Promise((resolve) => {
setTimeout(() => {
const result = fn(...args)
Expand All @@ -16,7 +16,7 @@ function wrapAsync(fn) {
const squareTest = wrapAsync(memoize(canvasTest, memoKeySerializer))
const dimensionTest = wrapAsync(memoize(canvasTest, memoKeySerializer))

export const testCanvasSize = (w, h) => {
export const testCanvasSize = (w: number, h: number) => {
return new Promise((resolve, reject) => {
const testSquareSide = sizes.squareSide.find((side) => side * side >= w * h)
const testDimension = sizes.dimension.find((side) => side >= w && side >= h)
Expand Down
16 changes: 9 additions & 7 deletions packages/image-shrink/src/utils/exif/findExifOrientation.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
export const findExifOrientation = (exif: DataView, exifCallback) => {
export const findExifOrientation = (
exif: DataView,
exifCallback: (offset: number, littleEndian: boolean) => void
) => {
let j, little, offset, ref
if (
!exif ||
exif.byteLength < 14 ||
exif.getUint32(0) !== 0x45786966 ||
exif.getUint16(4) !== 0
) {
return null
return
}
if (exif.getUint16(6) === 0x4949) {
little = true
} else if (exif.getUint16(6) === 0x4d4d) {
little = false
} else {
return null
return
}
if (exif.getUint16(8, little) !== 0x002a) {
return null
return
}
offset = 8 + exif.getUint32(10, little)
const count = exif.getUint16(offset - 2, little)
for (j = 0, ref = count; ref >= 0 ? j < ref : j > ref; ref >= 0 ? ++j : --j) {
if (exif.byteLength < offset + 10) {
return null
return
}
if (exif.getUint16(offset, little) === 0x0112) {
return exifCallback(offset + 8, little)
exifCallback(offset + 8, little)
}
offset += 12
}
return null
}
4 changes: 3 additions & 1 deletion packages/image-shrink/src/utils/exif/isBrowserApplyExif.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// 2x1 pixel image 90CW rotated with orientation header
const base64ImageSrc =
'data:image/jpg;base64,' +
'/9j/4AAQSkZJRgABAQEASABIAAD/4QA6RXhpZgAATU0AKgAAAAgAAwESAAMAAAABAAYAAAEo' +
'AAMAAAABAAIAAAITAAMAAAABAAEAAAAAAAD/2wBDAP//////////////////////////////' +
'////////////////////////////////////////////////////////wAALCAABAAIBASIA' +
'/8QAJgABAAAAAAAAAAAAAAAAAAAAAxABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQAAPwBH/9k='

let isApplied
let isApplied: boolean | undefined = undefined

export const isBrowserApplyExif = () => {
return new Promise((resolve) => {
if (isApplied !== undefined) {
Expand Down
10 changes: 5 additions & 5 deletions packages/image-shrink/src/utils/exif/replaceExif.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { replaceJpegChunk } from '../image/JPEG/replaceJpegChunk'
import { findExifOrientation } from './findExifOrientation'

export const setExifOrientation = (exif, orientation) => {
findExifOrientation(exif, (offset, little) =>
exif.setUint16(offset, orientation, little)
export const setExifOrientation = (exif: DataView, orientation: number) => {
findExifOrientation(exif, (offset, littleEndian) =>
exif.setUint16(offset, orientation, littleEndian)
)
}
export const replaceExif = async (
file: File,
blob: Blob,
exif: DataView,
isExifApplied: boolean | unknown
) => {
if (isExifApplied) {
setExifOrientation(exif, 1)
}

return replaceJpegChunk(file, 0xe1, [exif.buffer])
return replaceJpegChunk(blob, 0xe1, [exif.buffer])
}
43 changes: 24 additions & 19 deletions packages/image-shrink/src/utils/image/JPEG/readJpegChunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ type TChunk = {

export const readJpegChunks = () => {
const stack: TChunk[] = []
const promiseReadJpegChunks = (file) =>
const promiseReadJpegChunks = (blob: Blob) =>
new Promise((resolve, reject) => {
let pos
const readToView = (file, cb) => {
let pos = 2
const readToView = (blob: Blob, cb: (view: DataView) => void) => {
const reader = new FileReader()

reader.addEventListener('load', () => {
Expand All @@ -21,11 +21,11 @@ export const readJpegChunks = () => {
reject(`Reader error: ${e}`)
})

reader.readAsArrayBuffer(file)
reader.readAsArrayBuffer(blob)
}

const readNext = () =>
readToView(file.slice(pos, pos + 128), (view) => {
readToView(blob.slice(pos, pos + 128), (view: DataView) => {
let i, j, ref
for (
i = j = 0, ref = view.byteLength;
Expand All @@ -38,46 +38,51 @@ export const readJpegChunks = () => {
}
}

return readNextChunk()
readNextChunk()
})

const readNextChunk = () => {
const startPos = pos

return readToView(file.slice(pos, (pos += 4)), (view) => {
return readToView(blob.slice(pos, (pos += 4)), (view: DataView) => {
if (view.byteLength !== 4 || view.getUint8(0) !== 0xff) {
return reject('Corrupted')
reject('Corrupted')
return
}

const marker = view?.getUint8(1)

if (marker === 0xda) {
return resolve(true)
resolve(true)
return
}

const length = view.getUint16(2) - 2
return readToView(file.slice(pos, (pos += length)), (view) => {
if (view.byteLength !== length) {
return reject('Corrupted')
return readToView(
blob.slice(pos, (pos += length)),
(view: DataView) => {
if (view.byteLength !== length) {
reject('Corrupted')
return
}

stack.push({ startPos, length, marker, view })
readNext()
}

stack.push({ startPos, length, marker, view })
return readNext()
})
)
})
}

if (!(FileReader && DataView)) {
reject('Not Support')
}

pos = 2
readToView(file.slice(0, 2), function (view) {
readToView(blob.slice(0, 2), (view: DataView) => {
if (view.getUint16(0) !== 0xffd8) {
reject('Not jpeg')
}

return readNext()
readNext()
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { readJpegChunks } from './readJpegChunks'

export const replaceJpegChunk = (blob, marker, chunks) => {
export const replaceJpegChunk = (
blob: Blob,
marker: number,
chunks: ArrayBuffer[]
) => {
return new Promise((resolve, reject) => {
const oldChunkPos: number[] = []
const oldChunkLength: number[] = []
Expand All @@ -17,7 +21,7 @@ export const replaceJpegChunk = (blob, marker, chunks) => {
})
})
.then(() => {
const newChunks = [blob.slice(0, 2)]
const newChunks: (ArrayBuffer | Blob)[] = [blob.slice(0, 2)]

for (const chunk of chunks) {
const intro = new DataView(new ArrayBuffer(4))
Expand Down
Loading

0 comments on commit c64f2ee

Please sign in to comment.