mirror of
https://github.com/scratchfoundation/scratch-asset-types.git
synced 2025-06-16 21:50:19 -04:00
Add linting rules, add synchronous and asynchronous checks and appropriate
tests
This commit is contained in:
parent
3744b54733
commit
05524834e3
7 changed files with 37 additions and 20 deletions
6
.eslintignore
Normal file
6
.eslintignore
Normal file
|
@ -0,0 +1,6 @@
|
|||
node_modules/*
|
||||
static/*
|
||||
build/*
|
||||
intl/*
|
||||
locales/*
|
||||
**/*.min.js
|
4
.eslintrc.js
Normal file
4
.eslintrc.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
module.exports = {
|
||||
extends: ['scratch', 'scratch/node','scratch/es6'],
|
||||
};
|
||||
|
|
@ -1 +1,7 @@
|
|||
# scratch-asset-types
|
||||
|
||||
A library for detecting types for Scratch backend services that is optimized for the specific file types that Scratch services depend on.
|
||||
|
||||
### Thanks to file-type
|
||||
|
||||
This library is derived from the more general [file-type](https://www.npmjs.com/package/file-type) npm module.
|
||||
|
|
19
index.js
19
index.js
|
@ -3,7 +3,7 @@
|
|||
const typesList = require('./lib/typeslist');
|
||||
const readChunk = require('read-chunk');
|
||||
|
||||
module.exports = input => {
|
||||
module.exports.bufferCheck = input => {
|
||||
const buf = (input instanceof Uint8Array) ? input : new Uint8Array(input);
|
||||
|
||||
if (!(buf && buf.length > 1)) {
|
||||
|
@ -80,14 +80,13 @@ module.exports = input => {
|
|||
return null;
|
||||
};
|
||||
|
||||
module.exports.async = (fileName) => {
|
||||
return new Promise(function(resolve, reject) {
|
||||
const chunkPromise = readChunk(fileName, 0, 128);
|
||||
chunkPromise.then(function(result) {
|
||||
resolve(module.exports(result));
|
||||
}, function(err) {
|
||||
reject(err);
|
||||
});
|
||||
module.exports.asyncCheck = fileName => new Promise((resolve, reject) => {
|
||||
const chunkPromise = readChunk(fileName, 0, 128);
|
||||
chunkPromise.then(result => {
|
||||
resolve(module.exports.bufferCheck(result));
|
||||
}, err => {
|
||||
reject(err);
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
module.exports.syncCheck = fileName => module.exports.bufferCheck(readChunk.sync(fileName, 0, 128));
|
||||
|
|
5
package-lock.json
generated
5
package-lock.json
generated
|
@ -871,11 +871,6 @@
|
|||
"object-assign": "4.1.1"
|
||||
}
|
||||
},
|
||||
"file-type": {
|
||||
"version": "7.4.0",
|
||||
"resolved": "https://registry.npmjs.org/file-type/-/file-type-7.4.0.tgz",
|
||||
"integrity": "sha1-KnyU9ioAMBULt9m2xwz6HT51nIY="
|
||||
},
|
||||
"flat-cache": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-1.3.0.tgz",
|
||||
|
|
|
@ -15,7 +15,9 @@
|
|||
"babel-eslint": "^8.0.3",
|
||||
"eslint": "^4.13.1",
|
||||
"eslint-config-scratch": "^5.0.0",
|
||||
"read-chunk": "^2.1.0",
|
||||
"tap": "^11.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"read-chunk": "^2.1.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const tap = require('tap');
|
||||
const readChunk = require('read-chunk');
|
||||
const fileType = require('../../index');
|
||||
const typesList = require('../../lib/typeslist');
|
||||
|
||||
|
@ -7,10 +6,8 @@ const checkList = [
|
|||
'gif', 'jpg', 'json', 'png', 'webp', 'zip'];
|
||||
|
||||
tap.test('check-types', t => {
|
||||
|
||||
checkList.forEach(thisType => {
|
||||
const buffer = readChunk.sync(`./test/fixtures/test.${thisType}`, 0, 128);
|
||||
const detectedType = fileType(buffer);
|
||||
const detectedType = fileType.syncCheck(`./test/fixtures/test.${thisType}`);
|
||||
t.ok(detectedType);
|
||||
t.equals(detectedType.ext, typesList[thisType].ext);
|
||||
t.equals(detectedType.mime, typesList[thisType].mime);
|
||||
|
@ -18,3 +15,11 @@ tap.test('check-types', t => {
|
|||
|
||||
t.end();
|
||||
});
|
||||
|
||||
checkList.forEach(thisType => fileType.asyncCheck(`./test/fixtures/test.${thisType}`)
|
||||
.then(result => tap.test('check async results', t => {
|
||||
t.ok(result);
|
||||
t.equals(result.ext, typesList[thisType].ext);
|
||||
t.equals(result.mime, typesList[thisType].mime);
|
||||
t.end();
|
||||
})));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue