-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only support promises API, and remove the queue dependency
- Loading branch information
Showing
17 changed files
with
212 additions
and
370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as fs from 'node:fs' | ||
import * as path from 'node:path' | ||
|
||
import { lookup } from './lookup' | ||
import type { ISizeCalculationResult } from './types/interface' | ||
|
||
// Maximum input size, with a default of 512 kilobytes. | ||
// TO-DO: make this adaptive based on the initial signature of the image | ||
const MaxInputSize = 512 * 1024 | ||
|
||
type Job = { | ||
filePath: string | ||
resolve: (value: ISizeCalculationResult) => void | ||
reject: (error: Error) => void | ||
} | ||
|
||
// This queue is for async `fs` operations, to avoid reaching file-descriptor limits | ||
const queue: Job[] = [] | ||
|
||
let concurrency = 100 | ||
export const setConcurrency = (c: number): void => { | ||
concurrency = c | ||
} | ||
|
||
const processQueue = async () => { | ||
const jobs = queue.splice(0, concurrency) | ||
const promises = jobs.map(async ({ filePath, resolve, reject }) => { | ||
let handle: fs.promises.FileHandle | ||
try { | ||
handle = await fs.promises.open(path.resolve(filePath), 'r') | ||
} catch (err) { | ||
return reject(err as Error) | ||
} | ||
try { | ||
const { size } = await handle.stat() | ||
if (size <= 0) { | ||
throw new Error('Empty file') | ||
} | ||
const inputSize = Math.min(size, MaxInputSize) | ||
const input = new Uint8Array(inputSize) | ||
await handle.read(input, 0, inputSize, 0) | ||
resolve(lookup(input)) | ||
} catch (err) { | ||
reject(err as Error) | ||
} finally { | ||
await handle.close() | ||
} | ||
}) | ||
|
||
await Promise.allSettled(promises) | ||
|
||
if (queue.length) setTimeout(processQueue, 100) | ||
} | ||
|
||
/** | ||
* @param {string} filePath - relative/absolute path of the image file | ||
*/ | ||
export const imageSize = async (filePath: string) => | ||
new Promise<ISizeCalculationResult>((resolve, reject) => { | ||
queue.push({ filePath, resolve, reject }) | ||
processQueue() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1 @@ | ||
import type { ISizeCalculationResult } from './types/interface' | ||
import { lookup } from './lookup' | ||
|
||
export default imageSize | ||
export function imageSize(input: Uint8Array): ISizeCalculationResult | ||
|
||
/** | ||
* @param {Uint8Array} input - Uint8Array of the image file | ||
*/ | ||
export function imageSize(input: Uint8Array): ISizeCalculationResult { | ||
// Handle Uint8Array input | ||
return lookup(input) | ||
} | ||
export { lookup as imageSize, disableTypes, types } from './lookup' |
Oops, something went wrong.