Skip to content

Commit

Permalink
feat: also accept number as size argument
Browse files Browse the repository at this point in the history
  • Loading branch information
buzz committed May 28, 2024
1 parent 4200f57 commit 4405da4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
24 changes: 24 additions & 0 deletions __tests__/args.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mediaInfoFactory from '..'
import { expectToBeDefined } from './utils'
import type { MediaInfo, SizeArg } from '..'

it.each([
['number', 20],
['function', () => 20],
['async function', () => Promise.resolve(20)],
])('should accept %s as size arg', async (_: string, size: SizeArg) => {
let mi: MediaInfo | undefined

try {
mi = await mediaInfoFactory()
const result = await mi.analyzeData(size, () => new Uint8Array(10))
expectToBeDefined(result.media)
const { track } = result.media
expect(track).toHaveLength(1)
expect(track[0].FileSize).toBe('20')
} finally {
if (mi) {
mi.close()
}
}
})
23 changes: 10 additions & 13 deletions src/MediaInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type MediaInfoOptions<TFormat extends FormatType> = Required<
Omit<MediaInfoFactoryOptions<TFormat>, 'locateFile'>
>

type GetSizeFunc = () => Promise<number> | number
type SizeArg = (() => Promise<number> | number) | number

type ReadChunkFunc = (size: number, offset: number) => Promise<Uint8Array> | Uint8Array

Expand Down Expand Up @@ -72,26 +72,22 @@ class MediaInfo<TFormat extends FormatType = typeof DEFAULT_OPTIONS.format> {
/**
* Convenience method for analyzing a buffer chunk by chunk.
*
* @param getSize Return total buffer size in bytes.
* @param size Return total buffer size in bytes.
* @param readChunk Read chunk of data and return an {@link Uint8Array}.
*/
analyzeData(getSize: GetSizeFunc, readChunk: ReadChunkFunc): Promise<ResultMap[TFormat]>
analyzeData(size: SizeArg, readChunk: ReadChunkFunc): Promise<ResultMap[TFormat]>

/**
* Convenience method for analyzing a buffer chunk by chunk.
*
* @param getSize Return total buffer size in bytes.
* @param size Return total buffer size in bytes.
* @param readChunk Read chunk of data and return an {@link Uint8Array}.
* @param callback Function that is called once the processing is done
*/
analyzeData(
getSize: GetSizeFunc,
readChunk: ReadChunkFunc,
callback: ResultCallback<TFormat>
): void
analyzeData(size: SizeArg, readChunk: ReadChunkFunc, callback: ResultCallback<TFormat>): void

analyzeData(
getSize: GetSizeFunc,
size: SizeArg,
readChunk: ReadChunkFunc,
callback?: ResultCallback<TFormat>
): Promise<ResultMap[TFormat] | null> | undefined {
Expand All @@ -105,7 +101,7 @@ class MediaInfo<TFormat extends FormatType = typeof DEFAULT_OPTIONS.format> {
resolve(result)
}
}
this.analyzeData(getSize, readChunk, resultCb)
this.analyzeData(size, readChunk, resultCb)
})
}

Expand Down Expand Up @@ -166,7 +162,8 @@ class MediaInfo<TFormat extends FormatType = typeof DEFAULT_OPTIONS.format> {
getChunk()
}

const fileSizeValue = getSize()
const fileSizeValue = size instanceof Function ? size() : size

if (fileSizeValue instanceof Promise) {
fileSizeValue.then(runReadDataLoop).catch((error: unknown) => {
callback(null, unknownToError(error))
Expand Down Expand Up @@ -300,6 +297,6 @@ class MediaInfo<TFormat extends FormatType = typeof DEFAULT_OPTIONS.format> {
}
}

export type { FormatType, GetSizeFunc, ReadChunkFunc, ResultMap }
export type { FormatType, ReadChunkFunc, ResultMap, SizeArg }
export { DEFAULT_OPTIONS, FORMAT_CHOICES }
export default MediaInfo
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export type {
FormatType,
GetSizeFunc,
default as MediaInfo,
ReadChunkFunc,
ResultMap,
SizeArg,
} from './MediaInfo.js'
export type { MediaInfoFactoryOptions } from './mediaInfoFactory.js'
export { default, default as mediaInfoFactory } from './mediaInfoFactory.js'
Expand Down

0 comments on commit 4405da4

Please sign in to comment.