@vendure/asset-server-plugin 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +7 -0
  3. package/lib/index.d.ts +4 -0
  4. package/lib/index.js +17 -0
  5. package/lib/index.js.map +1 -0
  6. package/lib/src/common.d.ts +3 -0
  7. package/lib/src/common.js +22 -0
  8. package/lib/src/common.js.map +1 -0
  9. package/lib/src/constants.d.ts +1 -0
  10. package/lib/src/constants.js +5 -0
  11. package/lib/src/constants.js.map +1 -0
  12. package/lib/src/default-asset-storage-strategy-factory.d.ts +6 -0
  13. package/lib/src/default-asset-storage-strategy-factory.js +22 -0
  14. package/lib/src/default-asset-storage-strategy-factory.js.map +1 -0
  15. package/lib/src/file-icon.png +0 -0
  16. package/lib/src/hashed-asset-naming-strategy.d.ts +21 -0
  17. package/lib/src/hashed-asset-naming-strategy.js +39 -0
  18. package/lib/src/hashed-asset-naming-strategy.js.map +1 -0
  19. package/lib/src/local-asset-storage-strategy.d.ts +25 -0
  20. package/lib/src/local-asset-storage-strategy.js +68 -0
  21. package/lib/src/local-asset-storage-strategy.js.map +1 -0
  22. package/lib/src/plugin.d.ts +141 -0
  23. package/lib/src/plugin.js +317 -0
  24. package/lib/src/plugin.js.map +1 -0
  25. package/lib/src/s3-asset-storage-strategy.d.ts +118 -0
  26. package/lib/src/s3-asset-storage-strategy.js +222 -0
  27. package/lib/src/s3-asset-storage-strategy.js.map +1 -0
  28. package/lib/src/sharp-asset-preview-strategy.d.ts +11 -0
  29. package/lib/src/sharp-asset-preview-strategy.js +64 -0
  30. package/lib/src/sharp-asset-preview-strategy.js.map +1 -0
  31. package/lib/src/transform-image.d.ts +24 -0
  32. package/lib/src/transform-image.js +102 -0
  33. package/lib/src/transform-image.js.map +1 -0
  34. package/lib/src/types.d.ts +98 -0
  35. package/lib/src/types.js +3 -0
  36. package/lib/src/types.js.map +1 -0
  37. package/package.json +41 -0
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.resizeToFocalPoint = exports.transformImage = void 0;
7
+ const sharp_1 = __importDefault(require("sharp"));
8
+ /**
9
+ * Applies transforms to the given image according to the query params passed.
10
+ */
11
+ async function transformImage(originalImage, queryParams, presets) {
12
+ let targetWidth = Math.round(+queryParams.w) || undefined;
13
+ let targetHeight = Math.round(+queryParams.h) || undefined;
14
+ let mode = queryParams.mode || 'crop';
15
+ const fpx = +queryParams.fpx || undefined;
16
+ const fpy = +queryParams.fpy || undefined;
17
+ if (queryParams.preset) {
18
+ const matchingPreset = presets.find(p => p.name === queryParams.preset);
19
+ if (matchingPreset) {
20
+ targetWidth = matchingPreset.width;
21
+ targetHeight = matchingPreset.height;
22
+ mode = matchingPreset.mode;
23
+ }
24
+ }
25
+ const options = {};
26
+ if (mode === 'crop') {
27
+ options.position = sharp_1.default.strategy.entropy;
28
+ }
29
+ else {
30
+ options.fit = 'inside';
31
+ }
32
+ const image = sharp_1.default(originalImage);
33
+ if (fpx && fpy && targetWidth && targetHeight && mode === 'crop') {
34
+ const metadata = await image.metadata();
35
+ if (metadata.width && metadata.height) {
36
+ const xCenter = fpx * metadata.width;
37
+ const yCenter = fpy * metadata.height;
38
+ const { width, height, region } = resizeToFocalPoint({ w: metadata.width, h: metadata.height }, { w: targetWidth, h: targetHeight }, { x: xCenter, y: yCenter });
39
+ return image.resize(width, height).extract(region);
40
+ }
41
+ }
42
+ return image.resize(targetWidth, targetHeight, options);
43
+ }
44
+ exports.transformImage = transformImage;
45
+ /**
46
+ * Resize an image but keep it centered on the focal point.
47
+ * Based on the method outlined in https://github.com/lovell/sharp/issues/1198#issuecomment-384591756
48
+ */
49
+ function resizeToFocalPoint(original, target, focalPoint) {
50
+ const { width, height, factor } = getIntermediateDimensions(original, target);
51
+ const region = getExtractionRegion(factor, focalPoint, target, { w: width, h: height });
52
+ return { width, height, region };
53
+ }
54
+ exports.resizeToFocalPoint = resizeToFocalPoint;
55
+ /**
56
+ * Calculates the dimensions of the intermediate (resized) image.
57
+ */
58
+ function getIntermediateDimensions(original, target) {
59
+ const hRatio = original.h / target.h;
60
+ const wRatio = original.w / target.w;
61
+ let factor;
62
+ let width;
63
+ let height;
64
+ if (hRatio < wRatio) {
65
+ factor = hRatio;
66
+ height = Math.round(target.h);
67
+ width = Math.round(original.w / factor);
68
+ }
69
+ else {
70
+ factor = wRatio;
71
+ width = Math.round(target.w);
72
+ height = Math.round(original.h / factor);
73
+ }
74
+ return { width, height, factor };
75
+ }
76
+ /**
77
+ * Calculates the Region to extract from the intermediate image.
78
+ */
79
+ function getExtractionRegion(factor, focalPoint, target, intermediate) {
80
+ const newXCenter = focalPoint.x / factor;
81
+ const newYCenter = focalPoint.y / factor;
82
+ const region = {
83
+ left: 0,
84
+ top: 0,
85
+ width: target.w,
86
+ height: target.h,
87
+ };
88
+ if (intermediate.h < intermediate.w) {
89
+ region.left = clamp(0, intermediate.w - target.w, Math.round(newXCenter - target.w / 2));
90
+ }
91
+ else {
92
+ region.top = clamp(0, intermediate.h - target.h, Math.round(newYCenter - target.h / 2));
93
+ }
94
+ return region;
95
+ }
96
+ /**
97
+ * Limit the input value to the specified min and max values.
98
+ */
99
+ function clamp(min, max, input) {
100
+ return Math.min(Math.max(min, input), max);
101
+ }
102
+ //# sourceMappingURL=transform-image.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transform-image.js","sourceRoot":"","sources":["../../src/transform-image.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAqD;AAOrD;;GAEG;AACI,KAAK,UAAU,cAAc,CAChC,aAAqB,EACrB,WAAmC,EACnC,OAA+B;IAE/B,IAAI,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC;IAC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C;KAC3F;IACD,OAAO,MAAM,CAAC;AAClB,CAAC;AAED;;GAEG;AACH,SAAS,KAAK,CAAC,GAAW,EAAE,GAAW,EAAE,KAAa;IAClD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,98 @@
1
+ import { AssetNamingStrategy, AssetStorageStrategy, RequestContext } from '@vendure/core';
2
+ /**
3
+ * @description
4
+ * Specifies the way in which an asset preview image will be resized to fit in the
5
+ * proscribed dimensions:
6
+ *
7
+ * * crop: crops the image to cover both provided dimensions
8
+ * * resize: Preserving aspect ratio, resizes the image to be as large as possible
9
+ * while ensuring its dimensions are less than or equal to both those specified.
10
+ *
11
+ * @docsCategory AssetServerPlugin
12
+ */
13
+ export declare type ImageTransformMode = 'crop' | 'resize';
14
+ /**
15
+ * @description
16
+ * A configuration option for an image size preset for the AssetServerPlugin.
17
+ *
18
+ * Presets allow a shorthand way to generate a thumbnail preview of an asset. For example,
19
+ * the built-in "tiny" preset generates a 50px x 50px cropped preview, which can be accessed
20
+ * by appending the string `preset=tiny` to the asset url:
21
+ *
22
+ * `http://localhost:3000/assets/some-asset.jpg?preset=tiny`
23
+ *
24
+ * is equivalent to:
25
+ *
26
+ * `http://localhost:3000/assets/some-asset.jpg?w=50&h=50&mode=crop`
27
+ *
28
+ * @docsCategory AssetServerPlugin
29
+ */
30
+ export interface ImageTransformPreset {
31
+ name: string;
32
+ width: number;
33
+ height: number;
34
+ mode: ImageTransformMode;
35
+ }
36
+ /**
37
+ * @description
38
+ * The configuration options for the AssetServerPlugin.
39
+ *
40
+ * @docsCategory AssetServerPlugin
41
+ */
42
+ export interface AssetServerOptions {
43
+ /**
44
+ * @description
45
+ * The route to the asset server.
46
+ */
47
+ route: string;
48
+ /**
49
+ * @description
50
+ * The local directory to which assets will be uploaded when using the {@link LocalAssetStorageStrategy}.
51
+ */
52
+ assetUploadDir: string;
53
+ /**
54
+ * @description
55
+ * The complete URL prefix of the asset files. For example, "https://demo.vendure.io/assets/". A
56
+ * function can also be provided to handle more complex cases, such as serving multiple domains
57
+ * from a single server. In this case, the function should return a string url prefix.
58
+ *
59
+ * If not provided, the plugin will attempt to guess based off the incoming
60
+ * request and the configured route. However, in all but the simplest cases,
61
+ * this guess may not yield correct results.
62
+ */
63
+ assetUrlPrefix?: string | ((ctx: RequestContext, identifier: string) => string);
64
+ /**
65
+ * @description
66
+ * The max width in pixels of a generated preview image.
67
+ *
68
+ * @default 1600
69
+ */
70
+ previewMaxWidth?: number;
71
+ /**
72
+ * @description
73
+ * The max height in pixels of a generated preview image.
74
+ *
75
+ * @default 1600
76
+ */
77
+ previewMaxHeight?: number;
78
+ /**
79
+ * @description
80
+ * An array of additional {@link ImageTransformPreset} objects.
81
+ */
82
+ presets?: ImageTransformPreset[];
83
+ /**
84
+ * @description
85
+ * Defines how asset files and preview images are named before being saved.
86
+ *
87
+ * @default HashedAssetNamingStrategy
88
+ */
89
+ namingStrategy?: AssetNamingStrategy;
90
+ /**
91
+ * @description
92
+ * A function which can be used to configure an {@link AssetStorageStrategy}. This is useful e.g. if you wish to store your assets
93
+ * using a cloud storage provider. By default, the {@link LocalAssetStorageStrategy} is used.
94
+ *
95
+ * @default () => LocalAssetStorageStrategy
96
+ */
97
+ storageStrategyFactory?: (options: AssetServerOptions) => AssetStorageStrategy | Promise<AssetStorageStrategy>;
98
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@vendure/asset-server-plugin",
3
+ "version": "1.2.1",
4
+ "main": "lib/index.js",
5
+ "types": "lib/index.d.ts",
6
+ "files": [
7
+ "lib/**/*"
8
+ ],
9
+ "license": "MIT",
10
+ "scripts": {
11
+ "watch": "tsc -p ./tsconfig.build.json --watch",
12
+ "build": "rimraf lib && tsc -p ./tsconfig.build.json && node build.js",
13
+ "lint": "tslint --fix --project ./",
14
+ "test": "jest --config ./jest.config.js",
15
+ "e2e": "jest --config ../../e2e-common/jest-config.js --runInBand --package=asset-server-plugin"
16
+ },
17
+ "homepage": "https://www.vendure.io/",
18
+ "funding": "https://github.com/sponsors/michaelbromley",
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "devDependencies": {
23
+ "@types/express": "^4.17.8",
24
+ "@types/fs-extra": "^9.0.8",
25
+ "@types/node-fetch": "^2.5.8",
26
+ "@types/sharp": "^0.27.1",
27
+ "@vendure/common": "^1.2.1",
28
+ "@vendure/core": "^1.2.1",
29
+ "aws-sdk": "^2.856.0",
30
+ "express": "^4.17.1",
31
+ "node-fetch": "^2.6.1",
32
+ "rimraf": "^3.0.2",
33
+ "typescript": "4.3.5"
34
+ },
35
+ "dependencies": {
36
+ "file-type": "^16.2.0",
37
+ "fs-extra": "^10.0.0",
38
+ "sharp": "~0.29.0"
39
+ },
40
+ "gitHead": "d764852f00ab55b16ccbf88f6e8bb76bd76e9b08"
41
+ }