初始化

This commit is contained in:
yziiy
2025-08-11 11:06:07 +08:00
parent 083bc37c00
commit 5607d11395
19772 changed files with 3108723 additions and 18 deletions

View File

@@ -0,0 +1,35 @@
# v0.10.0 (Mon Mar 30 2020)
#### 🚀 Enhancement
- Properly split constructor and instance types [#867](https://github.com/oliver-moran/jimp/pull/867) ([@forivall](https://github.com/forivall))
#### Authors: 1
- Emily Marigold Klassen ([@forivall](https://github.com/forivall))
---
# v0.9.6 (Wed Mar 18 2020)
#### 🏠 Internal
- Fix TypeScript error on 'next' [#858](https://github.com/oliver-moran/jimp/pull/858) ([@crutchcorn](https://github.com/crutchcorn))
#### Authors: 1
- Corbin Crutchley ([@crutchcorn](https://github.com/crutchcorn))
---
# v0.9.3 (Tue Nov 26 2019)
#### 🐛 Bug Fix
- `@jimp/cli`, `@jimp/core`, `@jimp/custom`, `jimp`, `@jimp/plugin-blit`, `@jimp/plugin-blur`, `@jimp/plugin-circle`, `@jimp/plugin-color`, `@jimp/plugin-contain`, `@jimp/plugin-cover`, `@jimp/plugin-crop`, `@jimp/plugin-displace`, `@jimp/plugin-dither`, `@jimp/plugin-fisheye`, `@jimp/plugin-flip`, `@jimp/plugin-gaussian`, `@jimp/plugin-invert`, `@jimp/plugin-mask`, `@jimp/plugin-normalize`, `@jimp/plugin-print`, `@jimp/plugin-resize`, `@jimp/plugin-rotate`, `@jimp/plugin-scale`, `@jimp/plugin-shadow`, `@jimp/plugin-threshold`, `@jimp/plugins`, `@jimp/test-utils`, `@jimp/bmp`, `@jimp/gif`, `@jimp/jpeg`, `@jimp/png`, `@jimp/tiff`, `@jimp/types`, `@jimp/utils`
- Fix regeneratorRuntime errors [#815](https://github.com/oliver-moran/jimp/pull/815) ([@crutchcorn](https://github.com/crutchcorn) [@hipstersmoothie](https://github.com/hipstersmoothie))
#### Authors: 2
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
- Corbin Crutchley ([@crutchcorn](https://github.com/crutchcorn))

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Oliver Moran
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,196 @@
<div align="center">
<img width="200" height="200"
src="https://s3.amazonaws.com/pix.iemoji.com/images/emoji/apple/ios-11/256/crayon.png">
<h1>@jimp/custom</h1>
<p>Configure jimp with types and plugins.</p>
</div>
## Useful Defaults
The following wil configure a `jimp` instance with the same functionality as the main `jimp` package.
```js
import configure from '@jimp/custom';
// all of jimp's default types
import types from '@jimp/types';
// all of jimp's default types
import plugins from '@jimp/plugins';
configure({
types: [types],
plugins: [plugins]
});
```
## Available Methods
### configure
Takes a Jimp configuration and applies it to `@jimp/core`.
Sample Jimp configuration:
```js
import types from '@jimp/types';
import bmp from '@jimp/bmp';
import jpeg from '@jimp/types';
...
configure({
types: [types]
})
// or
configure({
types: [bmp, jpeg, ...]
})
```
#### Extending Jimp Further
You can use configure to add more types and plugins to a jimp multiple times.
```js
let jimp = configure({
types: [bmp]
});
jimp = configure(
{
types: [jpeg]
},
jimp
);
```
## Type Definition
To define a new Jimp image type write a function the takes the current Jimp configuration. In this function you can extend Jimp's internal data structures.
This function must return an object whose key is the mime type and value is an array of valid file extensions.
```js
const special = require('special-js');
const MIME_TYPE = 'image/special';
export default () => ({
mime: {[MIME_TYPE], ['spec', 'special']},
constants: {
MIME_SPECIAL: MIME_TYPE
},
decoders: {
[MIME_TYPE]: data => special.decode(data)
},
encoders: {
[MIME_TYPE]: image => special.encode(image.bitmap)
}
});
```
### Constants
A jimp image type can expose as many constants as it wants. Each jimp type is required to expose a mime type.
```js
constants: {
MIME_SPECIAL: MIME_TYPE
},
```
### hasAlpha
A image type can define whether it supports an alpha channel.
```js
hasAlpha: {
MIME_SPECIAL: true
},
```
### Decoder
A function that when supplied with a buffer should return a bitmap with height and width.
```js
decoders: {
[MIME_TYPE]: data => special.decode(data)
},
```
### Encoder
A function that when supplied with a Jimp image should return an encoded buffer.
```js
encoders: {
[MIME_TYPE]: image => special.encode(image.bitmap)
}
```
### Class
Add class properties and function to the Jimp constructor.
```js
class: {
_quality: 100,
quality: function(n, cb) {
if (typeof n !== 'number') {
return throwError.call(this, 'n must be a number', cb);
}
if (n < 0 || n > 100) {
return throwError.call(this, 'n must be a number 0 - 100', cb);
}
this._quality = Math.round(n);
if (isNodePattern(cb)) {
cb.call(this, null, this);
}
return this;
}
};
```
## Plugin Definition
Defining a plugin has access to all the same things in the type definition. Mainly plugins use just the `constants` and `class` config options.
Below is the `invert` plugin. If a plugin doesn return an object with `constants` and `class`, all keys are treated as class functions.
```js
import { isNodePattern } from '@jimp/utils';
/**
* Inverts the image
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
* @returns {Jimp} this for chaining of methods
*/
export default () => ({
invert(cb) {
this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function(
x,
y,
idx
) {
this.bitmap.data[idx] = 255 - this.bitmap.data[idx];
this.bitmap.data[idx + 1] = 255 - this.bitmap.data[idx + 1];
this.bitmap.data[idx + 2] = 255 - this.bitmap.data[idx + 2];
});
if (isNodePattern(cb)) {
cb.call(this, null, this);
}
return this;
}
});
```

View File

@@ -0,0 +1,111 @@
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
require("core-js/modules/es.symbol");
require("core-js/modules/es.array.filter");
require("core-js/modules/es.array.for-each");
require("core-js/modules/es.array.is-array");
require("core-js/modules/es.object.define-properties");
require("core-js/modules/es.object.define-property");
require("core-js/modules/es.object.entries");
require("core-js/modules/es.object.get-own-property-descriptor");
require("core-js/modules/es.object.get-own-property-descriptors");
require("core-js/modules/es.object.keys");
require("core-js/modules/web.dom-collections.for-each");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = configure;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _core = _interopRequireWildcard(require("@jimp/core"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function configure(configuration) {
var jimpInstance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _core["default"];
var jimpConfig = {
hasAlpha: {},
encoders: {},
decoders: {},
"class": {},
constants: {}
};
function addToConfig(newConfig) {
Object.entries(newConfig).forEach(function (_ref) {
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
key = _ref2[0],
value = _ref2[1];
jimpConfig[key] = _objectSpread({}, jimpConfig[key], {}, value);
});
}
function addImageType(typeModule) {
var type = typeModule();
if (Array.isArray(type.mime)) {
_core.addType.apply(void 0, (0, _toConsumableArray2["default"])(type.mime));
} else {
Object.entries(type.mime).forEach(function (mimeType) {
return _core.addType.apply(void 0, (0, _toConsumableArray2["default"])(mimeType));
});
}
delete type.mime;
addToConfig(type);
}
function addPlugin(pluginModule) {
var plugin = pluginModule(_core.jimpEvChange) || {};
if (!plugin["class"] && !plugin.constants) {
// Default to class function
addToConfig({
"class": plugin
});
} else {
addToConfig(plugin);
}
}
if (configuration.types) {
configuration.types.forEach(addImageType);
jimpInstance.decoders = _objectSpread({}, jimpInstance.decoders, {}, jimpConfig.decoders);
jimpInstance.encoders = _objectSpread({}, jimpInstance.encoders, {}, jimpConfig.encoders);
jimpInstance.hasAlpha = _objectSpread({}, jimpInstance.hasAlpha, {}, jimpConfig.hasAlpha);
}
if (configuration.plugins) {
configuration.plugins.forEach(addPlugin);
}
(0, _core.addJimpMethods)(jimpConfig["class"], jimpInstance);
(0, _core.addConstants)(jimpConfig.constants, jimpInstance);
return _core["default"];
}
module.exports = exports.default;
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../src/index.js"],"names":["configure","configuration","jimpInstance","Jimp","jimpConfig","hasAlpha","encoders","decoders","constants","addToConfig","newConfig","Object","entries","forEach","key","value","addImageType","typeModule","type","Array","isArray","mime","addType","mimeType","addPlugin","pluginModule","plugin","jimpEvChange","types","plugins"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;AAOe,SAASA,SAAT,CAAmBC,aAAnB,EAAuD;AAAA,MAArBC,YAAqB,uEAANC,gBAAM;AACpE,MAAMC,UAAU,GAAG;AACjBC,IAAAA,QAAQ,EAAE,EADO;AAEjBC,IAAAA,QAAQ,EAAE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AAY,CAACK,QAAb,qBACKL,YAAY,CAACK,QADlB,MAEKH,UAAU,CAACG,QAFhB;AAIAL,IAAAA,YAAY,CAACI,QAAb,qBACKJ,YAAY,CAACI,QADlB,MAEKF,UAAU,CAACE,QAFhB;AAIAJ,IAAAA,YAAY,CAACG,QAAb,qBACKH,YAAY,CAACG,QADlB,MAEKD,UAAU,CAACC,QAFhB;AAID;;AAED,MAAIJ,aAAa,CAAC4B,OAAlB,EAA2B;AACzB5B,IAAAA,aAAa,CAAC4B,OAAd,CAAsBhB,OAAtB,CAA8BW,SAA9B;AACD;;AAED,4BAAepB,UAAU,SAAzB,EAAiCF,YAAjC;AACA,0BAAaE,UAAU,CAACI,SAAxB,EAAmCN,YAAnC;AAEA,SAAOC,gBAAP;AACD","sourcesContent":["import Jimp, {\n addType,\n addJimpMethods,\n addConstants,\n jimpEvChange\n} from '@jimp/core';\n\nexport default function configure(configuration, jimpInstance = Jimp) {\n const jimpConfig = {\n hasAlpha: {},\n encoders: {},\n decoders: {},\n class: {},\n constants: {}\n };\n\n function addToConfig(newConfig) {\n Object.entries(newConfig).forEach(([key, value]) => {\n jimpConfig[key] = {\n ...jimpConfig[key],\n ...value\n };\n });\n }\n\n function addImageType(typeModule) {\n const type = typeModule();\n\n if (Array.isArray(type.mime)) {\n addType(...type.mime);\n } else {\n Object.entries(type.mime).forEach(mimeType => addType(...mimeType));\n }\n\n delete type.mime;\n addToConfig(type);\n }\n\n function addPlugin(pluginModule) {\n const plugin = pluginModule(jimpEvChange) || {};\n if (!plugin.class && !plugin.constants) {\n // Default to class function\n addToConfig({ class: plugin });\n } else {\n addToConfig(plugin);\n }\n }\n\n if (configuration.types) {\n configuration.types.forEach(addImageType);\n\n jimpInstance.decoders = {\n ...jimpInstance.decoders,\n ...jimpConfig.decoders\n };\n jimpInstance.encoders = {\n ...jimpInstance.encoders,\n ...jimpConfig.encoders\n };\n jimpInstance.hasAlpha = {\n ...jimpInstance.hasAlpha,\n ...jimpConfig.hasAlpha\n };\n }\n\n if (configuration.plugins) {\n configuration.plugins.forEach(addPlugin);\n }\n\n addJimpMethods(jimpConfig.class, jimpInstance);\n addConstants(jimpConfig.constants, jimpInstance);\n\n return Jimp;\n}\n"],"file":"index.js"}

View File

@@ -0,0 +1,87 @@
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = configure;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _core = _interopRequireWildcard(require("@jimp/core"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function configure(configuration) {
var jimpInstance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _core["default"];
var jimpConfig = {
hasAlpha: {},
encoders: {},
decoders: {},
"class": {},
constants: {}
};
function addToConfig(newConfig) {
Object.entries(newConfig).forEach(function (_ref) {
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
key = _ref2[0],
value = _ref2[1];
jimpConfig[key] = _objectSpread({}, jimpConfig[key], {}, value);
});
}
function addImageType(typeModule) {
var type = typeModule();
if (Array.isArray(type.mime)) {
_core.addType.apply(void 0, (0, _toConsumableArray2["default"])(type.mime));
} else {
Object.entries(type.mime).forEach(function (mimeType) {
return _core.addType.apply(void 0, (0, _toConsumableArray2["default"])(mimeType));
});
}
delete type.mime;
addToConfig(type);
}
function addPlugin(pluginModule) {
var plugin = pluginModule(_core.jimpEvChange) || {};
if (!plugin["class"] && !plugin.constants) {
// Default to class function
addToConfig({
"class": plugin
});
} else {
addToConfig(plugin);
}
}
if (configuration.types) {
configuration.types.forEach(addImageType);
jimpInstance.decoders = _objectSpread({}, jimpInstance.decoders, {}, jimpConfig.decoders);
jimpInstance.encoders = _objectSpread({}, jimpInstance.encoders, {}, jimpConfig.encoders);
jimpInstance.hasAlpha = _objectSpread({}, jimpInstance.hasAlpha, {}, jimpConfig.hasAlpha);
}
if (configuration.plugins) {
configuration.plugins.forEach(addPlugin);
}
(0, _core.addJimpMethods)(jimpConfig["class"], jimpInstance);
(0, _core.addConstants)(jimpConfig.constants, jimpInstance);
return _core["default"];
}
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../src/index.js"],"names":["configure","configuration","jimpInstance","Jimp","jimpConfig","hasAlpha","encoders","decoders","constants","addToConfig","newConfig","Object","entries","forEach","key","value","addImageType","typeModule","type","Array","isArray","mime","addType","mimeType","addPlugin","pluginModule","plugin","jimpEvChange","types","plugins"],"mappings":";;;;;;;;;;;;;;;;;AAAA;;;;;;AAOe,SAASA,SAAT,CAAmBC,aAAnB,EAAuD;AAAA,MAArBC,YAAqB,uEAANC,gBAAM;AACpE,MAAMC,UAAU,GAAG;AACjBC,IAAAA,QAAQ,EAAE,EADO;AAEjBC,IAAAA,QAAQ,EAAE,EAFO;AAGjBC,IAAAA,QAAQ,EAAE,EAHO;AAIjB,aAAO,EAJU;AAKjBC,IAAAA,SAAS,EAAE;AALM,GAAnB;;AAQA,WAASC,WAAT,CAAqBC,SAArB,EAAgC;AAC9BC,IAAAA,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AJ,IAAAA,YAAY,CAACG,QAAb,qBACKH,YAAY,CAACG,QADlB,MAEKD,UAAU,CAACC,QAFhB;AAID;;AAED,MAAIJ,aAAa,CAAC4B,OAAlB,EAA2B;AACzB5B,IAAAA,aAAa,CAAC4B,OAAd,CAAsBhB,OAAtB,CAA8BW,SAA9B;AACD;;AAED,4BAAepB,UAAU,SAAzB,EAAiCF,YAAjC;AACA,0BAAaE,UAAU,CAACI,SAAxB,EAAmCN,YAAnC;AAEA,SAAOC,gBAAP;AACD","sourcesContent":["import Jimp, {\n addType,\n addJimpMethods,\n addConstants,\n jimpEvChange\n} from '@jimp/core';\n\nexport default function configure(configuration, jimpInstance = Jimp) {\n const jimpConfig = {\n hasAlpha: {},\n encoders: {},\n decoders: {},\n class: {},\n constants: {}\n };\n\n function addToConfig(newConfig) {\n Object.entries(newConfig).forEach(([key, value]) => {\n jimpConfig[key] = {\n ...jimpConfig[key],\n ...value\n };\n });\n }\n\n function addImageType(typeModule) {\n const type = typeModule();\n\n if (Array.isArray(type.mime)) {\n addType(...type.mime);\n } else {\n Object.entries(type.mime).forEach(mimeType => addType(...mimeType));\n }\n\n delete type.mime;\n addToConfig(type);\n }\n\n function addPlugin(pluginModule) {\n const plugin = pluginModule(jimpEvChange) || {};\n if (!plugin.class && !plugin.constants) {\n // Default to class function\n addToConfig({ class: plugin });\n } else {\n addToConfig(plugin);\n }\n }\n\n if (configuration.types) {\n configuration.types.forEach(addImageType);\n\n jimpInstance.decoders = {\n ...jimpInstance.decoders,\n ...jimpConfig.decoders\n };\n jimpInstance.encoders = {\n ...jimpInstance.encoders,\n ...jimpConfig.encoders\n };\n jimpInstance.hasAlpha = {\n ...jimpInstance.hasAlpha,\n ...jimpConfig.hasAlpha\n };\n }\n\n if (configuration.plugins) {\n configuration.plugins.forEach(addPlugin);\n }\n\n addJimpMethods(jimpConfig.class, jimpInstance);\n addConstants(jimpConfig.constants, jimpInstance);\n\n return Jimp;\n}\n"],"file":"index.js"}

View File

@@ -0,0 +1,28 @@
{
"name": "@jimp/custom",
"version": "0.10.3",
"description": "Interface to customize jimp configuration",
"main": "dist/index.js",
"module": "es/index.js",
"types": "types/index.d.ts",
"scripts": {
"build": "npm run build:node:production && npm run build:module",
"build:watch": "npm run build:node:debug -- -- --watch --verbose",
"build:debug": "npm run build:node:debug",
"build:module": "cross-env BABEL_ENV=module babel src -d es --source-maps --config-file ../../babel.config.js",
"build:node": "babel src -d dist --source-maps --config-file ../../babel.config.js",
"build:node:debug": "cross-env BABEL_ENV=development npm run build:node",
"build:node:production": "cross-env BABEL_ENV=production npm run build:node"
},
"author": "",
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.7.2",
"@jimp/core": "^0.10.3",
"core-js": "^3.4.1"
},
"publishConfig": {
"access": "public"
},
"gitHead": "37197106eae5c26231018dfdc0254422f6b43927"
}

View File

@@ -0,0 +1,74 @@
import Jimp, {
addType,
addJimpMethods,
addConstants,
jimpEvChange
} from '@jimp/core';
export default function configure(configuration, jimpInstance = Jimp) {
const jimpConfig = {
hasAlpha: {},
encoders: {},
decoders: {},
class: {},
constants: {}
};
function addToConfig(newConfig) {
Object.entries(newConfig).forEach(([key, value]) => {
jimpConfig[key] = {
...jimpConfig[key],
...value
};
});
}
function addImageType(typeModule) {
const type = typeModule();
if (Array.isArray(type.mime)) {
addType(...type.mime);
} else {
Object.entries(type.mime).forEach(mimeType => addType(...mimeType));
}
delete type.mime;
addToConfig(type);
}
function addPlugin(pluginModule) {
const plugin = pluginModule(jimpEvChange) || {};
if (!plugin.class && !plugin.constants) {
// Default to class function
addToConfig({ class: plugin });
} else {
addToConfig(plugin);
}
}
if (configuration.types) {
configuration.types.forEach(addImageType);
jimpInstance.decoders = {
...jimpInstance.decoders,
...jimpConfig.decoders
};
jimpInstance.encoders = {
...jimpInstance.encoders,
...jimpConfig.encoders
};
jimpInstance.hasAlpha = {
...jimpInstance.hasAlpha,
...jimpConfig.hasAlpha
};
}
if (configuration.plugins) {
configuration.plugins.forEach(addPlugin);
}
addJimpMethods(jimpConfig.class, jimpInstance);
addConstants(jimpConfig.constants, jimpInstance);
return Jimp;
}

View File

@@ -0,0 +1,34 @@
// TypeScript Version: 3.1
// See the `jimp` package index.d.ts for why the version is not 2.8
import {
FunctionRet,
Jimp,
JimpPlugin,
JimpType,
GetIntersectionFromPlugins,
GetIntersectionFromPluginsStatics,
JimpConstructors
} from '@jimp/core';
type JimpInstance<
TypesFuncArr extends FunctionRet<JimpType> | undefined,
PluginFuncArr extends FunctionRet<JimpPlugin> | undefined,
J extends JimpConstructors
> = J & GetIntersectionFromPluginsStatics<Exclude<TypesFuncArr | PluginFuncArr, undefined>> & {
prototype: JimpType & GetIntersectionFromPlugins<Exclude<TypesFuncArr | PluginFuncArr, undefined>>
};
declare function configure<
TypesFuncArr extends FunctionRet<JimpType> | undefined = undefined,
PluginFuncArr extends FunctionRet<JimpPlugin> | undefined = undefined,
J extends JimpConstructors = JimpConstructors
>(
configuration: {
types?: TypesFuncArr;
plugins?: PluginFuncArr;
},
jimpInstance?: J
// Since JimpInstance is required, we want to use the default `Jimp` type
): JimpInstance<TypesFuncArr, PluginFuncArr, J>;
export default configure;

View File

@@ -0,0 +1,404 @@
import configure from '@jimp/custom';
import gif from '@jimp/gif';
import png from '@jimp/png';
import displace from '@jimp/plugin-displace';
import resize from '@jimp/plugin-resize';
import scale from '@jimp/plugin-scale';
import types from '@jimp/types';
import plugins from '@jimp/plugins';
import * as Jimp from 'jimp';
// configure should return a valid Jimp type with addons
const CustomJimp = configure({
types: [gif, png],
plugins: [displace, resize]
});
test('should function the same as the `jimp` types', () => {
const FullCustomJimp = configure({
types: [types],
plugins: [plugins]
});
const jimpInst = new FullCustomJimp('test');
// Main Jimp export should already have all of these already applied
// $ExpectError
jimpInst.read('Test');
jimpInst.displace(jimpInst, 2);
jimpInst.resize(40, 40);
jimpInst.displace(jimpInst, 2);
jimpInst.shadow((err, val, coords) => {});
jimpInst.fishEye({r: 12});
jimpInst.circle({radius: 12, x: 12, y: 12});
// $ExpectError
jimpInst.PNG_FILTER_NONE;
// $ExpectError
jimpInst.test;
// $ExpectError
jimpInst.func();
// Main Jimp export should already have all of these already applied
FullCustomJimp.read('Test');
// $ExpectType 0
FullCustomJimp.PNG_FILTER_NONE;
// $ExpectError
FullCustomJimp.test;
// $ExpectError
FullCustomJimp.func();
test('can clone properly', async () => {
const baseImage = await FullCustomJimp.read('filename');
const cloneBaseImage = baseImage.clone();
// $ExpectType number
cloneBaseImage._deflateLevel;
test('can handle `this` returns on the core type properly', () => {
// $ExpectType number
cloneBaseImage.posterize(3)._quality
});
test('can handle `this` returns properly', () => {
cloneBaseImage
.resize(1, 1)
.crop(0, 0, 0, 0)
.mask(cloneBaseImage, 2, 2)
.print('a' as any, 2, 2, 'a' as any)
.resize(1, 1)
.quality(1)
.deflateLevel(2)
._filterType;
});
test('can handle imageCallbacks `this` properly', () => {
cloneBaseImage.rgba(false, (_, jimpCBIn) => {
// $ExpectError
jimpCBIn.read('Test');
jimpCBIn.displace(jimpInst, 2);
jimpCBIn.resize(40, 40);
// $ExpectType number
jimpCBIn._filterType;
// $ExpectError
jimpCBIn.test;
// $ExpectError
jimpCBIn.func();
})
})
});
test('Can handle callback with constructor', () => {
const myBmpBuffer: Buffer = {} as any;
Jimp.read(myBmpBuffer, (err, cbJimpInst) => {
// $ExpectError
cbJimpInst.read('Test');
cbJimpInst.displace(jimpInst, 2);
cbJimpInst.resize(40, 40);
// $ExpectType number
cbJimpInst._filterType;
// $ExpectError
cbJimpInst.test;
// $ExpectError
cbJimpInst.func();
});
});
});
test('can handle custom jimp', () => {
// Constants from types should be applied
// $ExpectType 0
CustomJimp.PNG_FILTER_NONE;
// Core functions should still work from Jimp
CustomJimp.read('Test');
// Constants should not(?) be applied from ill-formed plugins
// $ExpectError
CustomJimp.displace(CustomJimp, 2);
// Methods should be applied from well-formed plugins only to the instance
// $ExpectError
CustomJimp.resize(40, 40)
// Constants should be applied from well-formed plugins
CustomJimp.RESIZE_NEAREST_NEIGHBOR
// $ExpectError
CustomJimp.test;
// $ExpectError
CustomJimp.func();
const Jiimp = new CustomJimp('test');
// Methods from types should be applied
Jiimp.deflateLevel(4);
// Constants from types should be applied to the static only
// $ExpectError
Jiimp.PNG_FILTER_NONE;
// Core functions should still work from Jimp
Jiimp.getPixelColor(1, 1);
// Constants should be applied from ill-formed plugins
Jiimp.displace(Jiimp, 2);
// Methods should be applied from well-formed plugins
Jiimp.resize(40, 40)
// Constants should not be applied to the object
// $ExpectError
Jiimp.RESIZE_NEAREST_NEIGHBOR
// $ExpectError
Jiimp.test;
// $ExpectError
Jiimp.func();
});
test('can compose', () => {
const OtherCustomJimp = configure({
plugins: [scale]
}, CustomJimp);
// Constants from types should be applied
// $ExpectType 0
OtherCustomJimp.PNG_FILTER_NONE;
// Core functions should still work from Jimp
OtherCustomJimp.read('Test');
// Constants should not be applied to the static instance from ill-formed plugins
// $ExpectError
OtherCustomJimp.displace(OtherCustomJimp, 2);
// Methods should not be applied to the static instance from well-formed plugins
// $ExpectError
OtherCustomJimp.resize(40, 40);
// Constants should be applied from well-formed plugins
OtherCustomJimp.RESIZE_NEAREST_NEIGHBOR;
// $ExpectError
OtherCustomJimp.test;
// $ExpectError
OtherCustomJimp.func();
const Jiimp = new OtherCustomJimp('test');
// Methods from types should be applied
Jiimp.deflateLevel(4);
// Constants from types should not be applied to objects
// $ExpectError
Jiimp.PNG_FILTER_NONE;
// Methods from new plugins should be applied
Jiimp.scale(3);
// Methods from types should be applied
Jiimp.filterType(4);
// Core functions should still work from Jimp
Jiimp.getPixelColor(1, 1);
// Constants should be applied from ill-formed plugins
Jiimp.displace(Jiimp, 2);
// Methods should be applied from well-formed plugins
Jiimp.resize(40, 40)
// Constants should not be applied from well-formed plugins to objects
// $ExpectError
Jiimp.RESIZE_NEAREST_NEIGHBOR
// $ExpectError
Jiimp.test;
// $ExpectError
Jiimp.func();
});
test('can handle only plugins', () => {
const PluginsJimp = configure({
plugins: [plugins]
});
// Core functions should still work from Jimp
PluginsJimp.read('Test');
// Constants should not be applied from ill-formed plugins
// $ExpectError
PluginsJimp.displace(PluginsJimp, 2);
// Methods should be not be applied to from well-formed plugins to the top level
// $ExpectError
PluginsJimp.resize(40, 40);
// Constants should be applied from well-formed plugins
// $ExpectType "nearestNeighbor"
PluginsJimp.RESIZE_NEAREST_NEIGHBOR;
// $ExpectError
PluginsJimp.test;
// $ExpectError
PluginsJimp.func();
const Jiimp = new PluginsJimp('test');
// Core functions should still work from Jimp
Jiimp.getPixelColor(1, 1);
// Constants should be applied from ill-formed plugins
Jiimp.displace(Jiimp, 2);
// Methods should be applied from well-formed plugins
Jiimp.resize(40, 40)
// Constants should be not applied to objects from well-formed plugins
// $ExpectError
Jiimp.RESIZE_NEAREST_NEIGHBOR
// $ExpectError
Jiimp.test;
// $ExpectError
Jiimp.func();
})
test('can handle only all types', () => {
const TypesJimp = configure({
types: [types]
});
// Methods from types should not be applied
// $ExpectError
TypesJimp.filterType(4);
// Constants from types should be applied
// $ExpectType 0
TypesJimp.PNG_FILTER_NONE;
// $ExpectError
TypesJimp.test;
// $ExpectError
TypesJimp.func();
const Jiimp = new TypesJimp('test');
// Methods from types should be applied
Jiimp.filterType(4);
// Constants from types should be not applied to objects
// $ExpectError
Jiimp.PNG_FILTER_NONE;
// $ExpectError
Jiimp.test;
// $ExpectError
Jiimp.func();
});
test('can handle only one type', () => {
const PngJimp = configure({
types: [png]
});
// Constants from other types should be not applied
// $ExpectError
PngJimp.MIME_TIFF;
// Constants from types should be applied
// $ExpectType 0
PngJimp.PNG_FILTER_NONE;
// $ExpectError
PngJimp.test;
// $ExpectError
PngJimp.func();
const Jiimp = new PngJimp('test');
// Constants from other types should be not applied
// $ExpectError
Jiimp.MIME_TIFF;
// Constants from types should not be applied to objects
// $ExpectError
Jiimp.PNG_FILTER_NONE;
// $ExpectError
Jiimp.test;
// $ExpectError
Jiimp.func();
});
test('can handle only one plugin', () => {
const ResizeJimp = configure({
plugins: [resize]
});
// Constants from other plugins should be not applied
// $ExpectError
ResizeJimp.FONT_SANS_8_BLACK;
// Constants from plugin should be applied
// $ExpectType "nearestNeighbor"
ResizeJimp.RESIZE_NEAREST_NEIGHBOR;
// $ExpectError
ResizeJimp.resize(2, 2);
// $ExpectError
ResizeJimp.test;
// $ExpectError
ResizeJimp.func();
const Jiimp: InstanceType<typeof ResizeJimp> = new ResizeJimp('test');
// Constants from other plugins should be not applied
// $ExpectError
Jiimp.FONT_SANS_8_BLACK;
// Constants from plugin should not be applied to the object
// $ExpectError
Jiimp.RESIZE_NEAREST_NEIGHBOR;
Jiimp.resize(2, 2);
// $ExpectError
Jiimp.test;
// $ExpectError
Jiimp.func();
});
test('Can handle appendConstructorOption', () => {
const AppendJimp = configure({});
AppendJimp.appendConstructorOption(
'Name of Option',
args => args.hasSomeCustomThing,
function(resolve, reject, args) {
// $ExpectError
this.bitmap = 3;
// $ExpectError
AppendJimp.resize(2, 2);
resolve();
}
);
});

View File

@@ -0,0 +1,18 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noEmit": true,
// If the library is an external module (uses `export`), this allows your test file to import "mylib" instead of "./index".
// If the library is global (cannot be imported via `import` or `require`), leave this out.
"baseUrl": ".",
"paths": {
"mylib": ["."]
}
}
}