Skip to content

Commit

Permalink
fix: re-enable fileTypeFromBlob
Browse files Browse the repository at this point in the history
Release-As: 1.1.0
  • Loading branch information
SgtPooki committed Feb 6, 2024
1 parent bdad55d commit 7ad796a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export async function fileTypeFromBuffer (input) {
return new FileTypeParser().fromBuffer(input)
}

// export async function fileTypeFromBlob(blob) {
// return new FileTypeParser().fromBlob(blob);
// }
export async function fileTypeFromBlob (blob) {
return new FileTypeParser().fromBlob(blob)
}

function _check (buffer, headers, options) {
options = {
Expand Down
4 changes: 2 additions & 2 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fileTypeFromBuffer } from './index.js'

const [file] = process.argv.slice(2)

if (!file) {
if (file == null) {
console.error('Expected path of the file to examine')
process.exit()
}
Expand All @@ -15,7 +15,7 @@ const buffer = await readFile(file)

const fileType = await fileTypeFromBuffer(buffer)

if (fileType) {
if (fileType != null) {
console.log(`MIME-type: ${fileType.mime}`)
console.log(`Extension: ${fileType.ext}`)
} else {
Expand Down
36 changes: 20 additions & 16 deletions test/file-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import {
FileTypeParser,
supportedExtensions,
supportedMimeTypes,
type FileTypeResult
type FileTypeResult,
fileTypeFromBlob
} from '../src/index.js'
import { getFixtureDataUint8Array } from './get-fixture-data.js'
import { getFixtureDataBlob, getFixtureDataUint8Array } from './get-fixture-data.js'

const missingTests = new Set([
'mpc'
Expand Down Expand Up @@ -256,18 +257,17 @@ const failingFixture = new Set([
'fixture-password-protected'
])

async function checkBufferLike (type, bufferLike): Promise<void> {
async function checkBufferLike (type: string, bufferLike: ArrayBufferLike): Promise<void> {
const { ext, mime } = await fileTypeFromBuffer(bufferLike) ?? {}
expect(ext).to.equal(type)
expect(typeof mime).to.equal('string')
}

// async function checkBlobLike(t, type, bufferLike) {
// const blob = new Blob([bufferLike]);
// const {ext, mime} = await fileTypeFromBlob(blob) ?? {};
// t.is(ext, type);
// t.is(typeof mime, 'string');
// }
async function checkBlobLike (type: string, blob: Blob): Promise<void> {
const { ext, mime } = await fileTypeFromBlob(blob) ?? {}
expect(ext).to.equal(type)
expect(typeof mime).to.equal('string')
}

// async function checkFile(t, type, filePath) {
// const {ext, mime} = await fileTypeFromFile(filePath) ?? {};
Expand All @@ -289,13 +289,12 @@ async function testFromBuffer (ext: string, name?: string): Promise<void> {
await checkBufferLike(ext, chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength))
}

// async function testFromBlob (t, ext, name) {
// const fixtureName = `${(name ?? 'fixture')}.${ext}`
async function testFromBlob (ext: string, name?: string): Promise<void> {
const fixtureName = `${(name ?? 'fixture')}.${ext}`

// const file = path.join(fixturePath, fixtureName)
// const chunk = fs.readFileSync(file)
// await checkBlobLike(t, ext, chunk)
// }
const blob = await getFixtureDataBlob(fixtureName)
await checkBlobLike(ext, blob)
}

async function testFalsePositive (ext, name): Promise<void> {
const chunk = await getFixtureDataUint8Array(`${name}.${ext}`)
Expand Down Expand Up @@ -350,7 +349,9 @@ for (const type of types) {
await testFromBuffer(type, name)
})
// })
// _test(`${name}.${type} ${i++} .fileTypeFromBlob() method - same fileType`, testFromBlob, type, name);
_test(`${name}.${type} ${i++} .fileTypeFromBlob() method - same fileType`, async () => {
await testFromBlob(type, name)
})
// _test(`${name}.${type} ${i++} .fileTypeFromStream() method - same fileType`, testFileFromStream, type, name);
// it(`${name}.${type} ${i++} .fileTypeStream() - identical streams`, testStream, type, name);
}
Expand All @@ -365,6 +366,9 @@ for (const type of types) {
_test(`${type} ${i++} .fileTypeFromBuffer()`, async () => {
await testFromBuffer(type)
})
_test(`${type} ${i++} .fileTypeFromBlob()`, async () => {
await testFromBlob(type)
})
// })
// _test(`${type} ${i++} .fileTypeFromStream()`, testFileFromStream, type);
// it(`${type} ${i++} .fileTypeStream() - identical streams`, testStream, type);
Expand Down
6 changes: 6 additions & 0 deletions test/get-fixture-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ export async function getFixtureDataUint8Array (filename: string): Promise<Uint8

return new Uint8Array(await fixtureDataResp.arrayBuffer())
}

export async function getFixtureDataBlob (filename: string): Promise<Blob> {
const fixtureDataResp = await getFixtureData(filename)

return fixtureDataResp.blob()
}

0 comments on commit 7ad796a

Please sign in to comment.