@vendure/asset-server-plugin 3.1.0-next.3 → 3.1.0-next.4
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.
- package/lib/index.d.ts +6 -2
- package/lib/index.js +6 -2
- package/lib/index.js.map +1 -1
- package/lib/src/asset-server.d.ts +50 -0
- package/lib/src/asset-server.js +303 -0
- package/lib/src/asset-server.js.map +1 -0
- package/lib/src/{default-asset-storage-strategy-factory.d.ts → config/default-asset-storage-strategy-factory.d.ts} +1 -1
- package/lib/src/{default-asset-storage-strategy-factory.js → config/default-asset-storage-strategy-factory.js} +1 -1
- package/lib/src/config/default-asset-storage-strategy-factory.js.map +1 -0
- package/lib/src/config/hashed-asset-naming-strategy.js.map +1 -0
- package/lib/src/config/image-transform-strategy.d.ts +58 -0
- package/lib/src/config/image-transform-strategy.js +3 -0
- package/lib/src/config/image-transform-strategy.js.map +1 -0
- package/lib/src/config/local-asset-storage-strategy.js.map +1 -0
- package/lib/src/config/preset-only-strategy.d.ts +82 -0
- package/lib/src/config/preset-only-strategy.js +70 -0
- package/lib/src/config/preset-only-strategy.js.map +1 -0
- package/lib/src/{s3-asset-storage-strategy.d.ts → config/s3-asset-storage-strategy.d.ts} +1 -1
- package/lib/src/{s3-asset-storage-strategy.js → config/s3-asset-storage-strategy.js} +2 -2
- package/lib/src/config/s3-asset-storage-strategy.js.map +1 -0
- package/lib/src/{sharp-asset-preview-strategy.js → config/sharp-asset-preview-strategy.js} +2 -2
- package/lib/src/config/sharp-asset-preview-strategy.js.map +1 -0
- package/lib/src/constants.d.ts +1 -0
- package/lib/src/constants.js +2 -1
- package/lib/src/constants.js.map +1 -1
- package/lib/src/plugin.d.ts +39 -31
- package/lib/src/plugin.js +100 -206
- package/lib/src/plugin.js.map +1 -1
- package/lib/src/transform-image.d.ts +2 -2
- package/lib/src/transform-image.js +9 -24
- package/lib/src/transform-image.js.map +1 -1
- package/lib/src/types.d.ts +15 -0
- package/package.json +4 -4
- package/lib/src/default-asset-storage-strategy-factory.js.map +0 -1
- package/lib/src/hashed-asset-naming-strategy.js.map +0 -1
- package/lib/src/local-asset-storage-strategy.js.map +0 -1
- package/lib/src/s3-asset-storage-strategy.js.map +0 -1
- package/lib/src/sharp-asset-preview-strategy.js.map +0 -1
- /package/lib/src/{hashed-asset-naming-strategy.d.ts → config/hashed-asset-naming-strategy.d.ts} +0 -0
- /package/lib/src/{hashed-asset-naming-strategy.js → config/hashed-asset-naming-strategy.js} +0 -0
- /package/lib/src/{local-asset-storage-strategy.d.ts → config/local-asset-storage-strategy.d.ts} +0 -0
- /package/lib/src/{local-asset-storage-strategy.js → config/local-asset-storage-strategy.js} +0 -0
- /package/lib/src/{sharp-asset-preview-strategy.d.ts → config/sharp-asset-preview-strategy.d.ts} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preset-only-strategy.js","sourceRoot":"","sources":["../../../src/config/preset-only-strategy.ts"],"names":[],"mappings":";;;AA8CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAa,kBAAkB;IAC3B,YAAoB,OAAkC;QAAlC,YAAO,GAAP,OAAO,CAA2B;IAAG,CAAC;IAE1D,2BAA2B,CAAC,EACxB,KAAK,EACL,gBAAgB,GACc;;QAC9B,MAAM,UAAU,GAAG,MAAA,KAAK,CAAC,MAAM,mCAAI,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC;QAC9D,MAAM,cAAc,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;QACzE,IAAI,CAAC,cAAc,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CAAC,WAAW,UAAU,aAAa,CAAC,CAAC;QACxD,CAAC;QACD,MAAM,gBAAgB,GAAG,MAAA,IAAI,CAAC,OAAO,CAAC,gBAAgB,mCAAI,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;QAC9E,MAAM,gBAAgB,GAAG,MAAA,IAAI,CAAC,OAAO,CAAC,gBAAgB,mCAAI,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QAClF,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,IAAI,gBAAgB,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;QACtG,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,IAAI,gBAAgB,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;QAClG,OAAO;YACH,KAAK,EAAE,cAAc,CAAC,KAAK;YAC3B,MAAM,EAAE,cAAc,CAAC,MAAM;YAC7B,IAAI,EAAE,cAAc,CAAC,IAAI;YACzB,OAAO;YACP,MAAM;YACN,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS;YACzD,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS;YACzD,MAAM,EAAE,KAAK,CAAC,MAAM;SACvB,CAAC;IACN,CAAC;CACJ;AA3BD,gDA2BC"}
|
|
@@ -4,7 +4,7 @@ import { AwsCredentialIdentity, AwsCredentialIdentityProvider } from '@aws-sdk/t
|
|
|
4
4
|
import { AssetStorageStrategy } from '@vendure/core';
|
|
5
5
|
import { Request } from 'express';
|
|
6
6
|
import { Readable } from 'node:stream';
|
|
7
|
-
import { AssetServerOptions } from '
|
|
7
|
+
import { AssetServerOptions } from '../types';
|
|
8
8
|
/**
|
|
9
9
|
* @description
|
|
10
10
|
* Configuration for connecting to AWS S3.
|
|
@@ -34,8 +34,8 @@ exports.S3AssetStorageStrategy = exports.configureS3AssetStorage = void 0;
|
|
|
34
34
|
const core_1 = require("@vendure/core");
|
|
35
35
|
const path = __importStar(require("node:path"));
|
|
36
36
|
const node_stream_1 = require("node:stream");
|
|
37
|
-
const common_1 = require("
|
|
38
|
-
const constants_1 = require("
|
|
37
|
+
const common_1 = require("../common");
|
|
38
|
+
const constants_1 = require("../constants");
|
|
39
39
|
/**
|
|
40
40
|
* @description
|
|
41
41
|
* Returns a configured instance of the {@link S3AssetStorageStrategy} which can then be passed to the {@link AssetServerOptions}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3-asset-storage-strategy.js","sourceRoot":"","sources":["../../../src/config/s3-asset-storage-strategy.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,wCAA6D;AAE7D,gDAAkC;AAClC,6CAAuC;AAEvC,sCAAgD;AAChD,4CAAyC;AAwCzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AACH,SAAgB,uBAAuB,CAAC,QAAkB;IACtD,OAAO,CAAC,OAA2B,EAAE,EAAE;QACnC,MAAM,QAAQ,GAAG,IAAA,4BAAmB,EAAC,OAAO,CAAC,CAAC;QAC9C,MAAM,eAAe,GAAG,CAAC,OAAgB,EAAE,UAAkB,EAAU,EAAE;YACrE,IAAI,CAAC,UAAU,EAAE,CAAC;gBACd,OAAO,EAAE,CAAC;YACd,CAAC;YACD,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;YAC7C,OAAO,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,UAAU,EAAE,CAAC;QACjF,CAAC,CAAC;QACF,OAAO,IAAI,sBAAsB,CAAC,QAAQ,EAAE,eAAe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EACT,GAAG,CAAC,KAAK,CACZ,CAAC;QACN,CAAC;IACL,CAAC;IAEO,KAAK,CAAC,cAAc;QACxB,IAAI,IAAI,CAAC,QAAQ,CAAC,WAAW,IAAI,IAAI,EAAE,CAAC;YACpC,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,IAAI,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;YACvD,aAAM,CAAC,IAAI,CACP,sEAAsE;gBAClE,qIAAqI,EACzI,qBAAS,CACZ,CAAC;YACF,OAAO,CAAC,MAAM,MAAM,CAAC,kCAAkC,CAAC,CAAC,CAAC,OAAO,CAAC;gBAC9D,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,OAAO;aAC7C,CAAC,CAAC;QACP,CAAC;QAED,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC;IACrC,CAAC;IAEO,oBAAoB,CACxB,WAAkE;QAElE,OAAO,CACH,WAAW,KAAK,IAAI;YACpB,OAAO,WAAW,KAAK,QAAQ;YAC/B,SAAS,IAAI,WAAW;YACxB,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,KAAK,CAAC,CACxC,CAAC;IACN,CAAC;CACJ;AA5LD,wDA4LC"}
|
|
@@ -8,7 +8,7 @@ const generated_types_1 = require("@vendure/common/lib/generated-types");
|
|
|
8
8
|
const core_1 = require("@vendure/core");
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
10
|
const sharp_1 = __importDefault(require("sharp"));
|
|
11
|
-
const constants_1 = require("
|
|
11
|
+
const constants_1 = require("../constants");
|
|
12
12
|
/**
|
|
13
13
|
* @description
|
|
14
14
|
* This {@link AssetPreviewStrategy} uses the [Sharp library](https://sharp.pixelplumbing.com/) to generate
|
|
@@ -106,7 +106,7 @@ class SharpAssetPreviewStrategy {
|
|
|
106
106
|
</svg>`);
|
|
107
107
|
}
|
|
108
108
|
generateBinaryFilePreview(mimeType) {
|
|
109
|
-
return (0, sharp_1.default)(path_1.default.join(__dirname, 'file-icon.png'))
|
|
109
|
+
return (0, sharp_1.default)(path_1.default.join(__dirname, '..', 'file-icon.png'))
|
|
110
110
|
.resize(800, 800, { fit: 'outside' })
|
|
111
111
|
.composite([
|
|
112
112
|
{
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sharp-asset-preview-strategy.js","sourceRoot":"","sources":["../../../src/config/sharp-asset-preview-strategy.ts"],"names":[],"mappings":";;;;;;AAAA,yEAAgE;AAChE,wCAA2F;AAC3F,gDAAwB;AACxB,kDAA0B;AAE1B,4CAAyC;AA+DzC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAa,yBAAyB;IAYlC,YAAY,MAAgC;QAX3B,kBAAa,GAAsC;YAChE,SAAS,EAAE,IAAI;YACf,QAAQ,EAAE,IAAI;YACd,WAAW,EAAE,EAAE;YACf,UAAU,EAAE,EAAE;YACd,WAAW,EAAE,EAAE;YACf,UAAU,EAAE,EAAE;YACd,WAAW,EAAE,EAAE;SAClB,CAAC;QAIE,IAAI,CAAC,MAAM,mCACJ,IAAI,CAAC,aAAa,GAClB,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,CACpB,CAAC;IACN,CAAC;IAED,KAAK,CAAC,oBAAoB,CAAC,GAAmB,EAAE,QAAgB,EAAE,IAAY;QAC1E,MAAM,SAAS,GAAG,IAAA,mBAAY,EAAC,QAAQ,CAAC,CAAC;QAEzC,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;QAE5C,IAAI,SAAS,KAAK,2BAAS,CAAC,KAAK,EAAE,CAAC;YAChC,IAAI,CAAC;gBACD,MAAM,KAAK,GAAG,IAAA,eAAK,EAAC,IAAI,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC;gBAC5D,MAAM,QAAQ,GAA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AAO,GAAQ,EAAE,CAAC;gBAChB,aAAM,CAAC,KAAK,CACR,qEAAqE,QAAQ,KAAK,IAAI,CAAC,SAAS,CAC5F,GAAG,CAAC,OAAO,CACd,EAAE,EACH,qBAAS,CACZ,CAAC;gBACF,OAAO,IAAI,CAAC,yBAAyB,CAAC,QAAQ,CAAC,CAAC;YACpD,CAAC;QACL,CAAC;aAAM,CAAC;YACJ,OAAO,IAAI,CAAC,yBAAyB,CAAC,QAAQ,CAAC,CAAC;QACpD,CAAC;IACL,CAAC;IAEO,uBAAuB,CAAC,QAAgB;QAC5C,OAAO,MAAM,CAAC,IAAI,CAAC;;;;;;;;;;wEAU6C,QAAQ;mBAC7D,CAAC,CAAC;IACjB,CAAC;IAEO,yBAAyB,CAAC,QAAgB;QAC9C,OAAO,IAAA,eAAK,EAAC,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,eAAe,CAAC,CAAC;aACpD,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC;aACpC,SAAS,CAAC;YACP;gBACI,KAAK,EAAE,IAAI,CAAC,uBAAuB,CAAC,QAAQ,CAAC;gBAC7C,OAAO,EAAE,eAAK,CAAC,OAAO,CAAC,MAAM;aAChC;SACJ,CAAC;aACD,QAAQ,EAAE,CAAC;IACpB,CAAC;CACJ;AA7FD,8DA6FC"}
|
package/lib/src/constants.d.ts
CHANGED
package/lib/src/constants.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_CACHE_HEADER = exports.loggerCtx = void 0;
|
|
3
|
+
exports.ASSET_SERVER_PLUGIN_INIT_OPTIONS = exports.DEFAULT_CACHE_HEADER = exports.loggerCtx = void 0;
|
|
4
4
|
exports.loggerCtx = 'AssetServerPlugin';
|
|
5
5
|
exports.DEFAULT_CACHE_HEADER = 'public, max-age=15552000';
|
|
6
|
+
exports.ASSET_SERVER_PLUGIN_INIT_OPTIONS = Symbol('ASSET_SERVER_PLUGIN_INIT_OPTIONS');
|
|
6
7
|
//# sourceMappingURL=constants.js.map
|
package/lib/src/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,SAAS,GAAG,mBAAmB,CAAC;AAChC,QAAA,oBAAoB,GAAG,0BAA0B,CAAC"}
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,SAAS,GAAG,mBAAmB,CAAC;AAChC,QAAA,oBAAoB,GAAG,0BAA0B,CAAC;AAClD,QAAA,gCAAgC,GAAG,MAAM,CAAC,kCAAkC,CAAC,CAAC"}
|
package/lib/src/plugin.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { MiddlewareConsumer, NestModule, OnApplicationBootstrap } from '@nestjs/common';
|
|
1
|
+
import { MiddlewareConsumer, NestModule, OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { ModuleRef } from '@nestjs/core';
|
|
2
3
|
import { Type } from '@vendure/common/lib/shared-types';
|
|
3
|
-
import { ProcessContext
|
|
4
|
+
import { ProcessContext } from '@vendure/core';
|
|
5
|
+
import { AssetServer } from './asset-server';
|
|
4
6
|
import { AssetServerOptions } from './types';
|
|
5
7
|
/**
|
|
6
8
|
* @description
|
|
@@ -123,45 +125,51 @@ import { AssetServerOptions } from './types';
|
|
|
123
125
|
* By default, the AssetServerPlugin will cache every transformed image, so that the transformation only needs to be performed a single time for
|
|
124
126
|
* a given configuration. Caching can be disabled per-request by setting the `?cache=false` query parameter.
|
|
125
127
|
*
|
|
128
|
+
* ### Limiting transformations
|
|
129
|
+
*
|
|
130
|
+
* By default, the AssetServerPlugin will allow any transformation to be performed on an image. However, it is possible to restrict the transformations
|
|
131
|
+
* which can be performed by using an {@link ImageTransformStrategy}. This can be used to limit the transformations to a known set of presets, for example.
|
|
132
|
+
*
|
|
133
|
+
* This is advisable in order to prevent abuse of the image transformation feature, as it can be computationally expensive.
|
|
134
|
+
*
|
|
135
|
+
* Since v3.1.0 we ship with a {@link PresetOnlyStrategy} which allows only transformations using a known set of presets.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* import { AssetServerPlugin, PresetOnlyStrategy } from '\@vendure/core';
|
|
140
|
+
*
|
|
141
|
+
* // ...
|
|
142
|
+
*
|
|
143
|
+
* AssetServerPlugin.init({
|
|
144
|
+
* //...
|
|
145
|
+
* imageTransformStrategy: new PresetOnlyStrategy({
|
|
146
|
+
* defaultPreset: 'thumbnail',
|
|
147
|
+
* permittedQuality: [0, 50, 75, 85, 95],
|
|
148
|
+
* permittedFormats: ['jpg', 'webp', 'avif'],
|
|
149
|
+
* allowFocalPoint: false,
|
|
150
|
+
* }),
|
|
151
|
+
* });
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
126
154
|
* @docsCategory core plugins/AssetServerPlugin
|
|
127
155
|
*/
|
|
128
|
-
export declare class AssetServerPlugin implements NestModule, OnApplicationBootstrap {
|
|
156
|
+
export declare class AssetServerPlugin implements NestModule, OnApplicationBootstrap, OnApplicationShutdown {
|
|
157
|
+
private options;
|
|
129
158
|
private processContext;
|
|
130
|
-
private
|
|
131
|
-
private
|
|
132
|
-
private presets;
|
|
159
|
+
private moduleRef;
|
|
160
|
+
private assetServer;
|
|
133
161
|
private static options;
|
|
134
|
-
private
|
|
162
|
+
private readonly defaultPresets;
|
|
135
163
|
/**
|
|
136
164
|
* @description
|
|
137
165
|
* Set the plugin options.
|
|
138
166
|
*/
|
|
139
167
|
static init(options: AssetServerOptions): Type<AssetServerPlugin>;
|
|
168
|
+
constructor(options: AssetServerOptions, processContext: ProcessContext, moduleRef: ModuleRef, assetServer: AssetServer);
|
|
140
169
|
/** @internal */
|
|
141
|
-
|
|
142
|
-
constructor(processContext: ProcessContext);
|
|
170
|
+
onApplicationBootstrap(): Promise<void>;
|
|
143
171
|
/** @internal */
|
|
144
|
-
|
|
172
|
+
onApplicationShutdown(): Promise<void>;
|
|
145
173
|
configure(consumer: MiddlewareConsumer): void;
|
|
146
|
-
|
|
147
|
-
* Creates the image server instance
|
|
148
|
-
*/
|
|
149
|
-
private createAssetServer;
|
|
150
|
-
/**
|
|
151
|
-
* Reads the file requested and send the response to the browser.
|
|
152
|
-
*/
|
|
153
|
-
private sendAsset;
|
|
154
|
-
/**
|
|
155
|
-
* If an exception was thrown by the first handler, then it may be because a transformed image
|
|
156
|
-
* is being requested which does not yet exist. In this case, this handler will generate the
|
|
157
|
-
* transformed image, save it to cache, and serve the result as a response.
|
|
158
|
-
*/
|
|
159
|
-
private generateTransformedImage;
|
|
160
|
-
private getFileNameFromRequest;
|
|
161
|
-
private md5;
|
|
162
|
-
private addSuffix;
|
|
163
|
-
/**
|
|
164
|
-
* Attempt to get the mime type from the file name.
|
|
165
|
-
*/
|
|
166
|
-
private getMimeType;
|
|
174
|
+
private getImageTransformStrategyArray;
|
|
167
175
|
}
|
package/lib/src/plugin.js
CHANGED
|
@@ -8,27 +8,20 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
|
-
var
|
|
12
|
-
return (
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
14
|
var AssetServerPlugin_1;
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.AssetServerPlugin = void 0;
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const core_1 = require("@nestjs/core");
|
|
19
|
+
const core_2 = require("@vendure/core");
|
|
20
|
+
const asset_server_1 = require("./asset-server");
|
|
21
|
+
const default_asset_storage_strategy_factory_1 = require("./config/default-asset-storage-strategy-factory");
|
|
22
|
+
const hashed_asset_naming_strategy_1 = require("./config/hashed-asset-naming-strategy");
|
|
23
|
+
const sharp_asset_preview_strategy_1 = require("./config/sharp-asset-preview-strategy");
|
|
23
24
|
const constants_1 = require("./constants");
|
|
24
|
-
const default_asset_storage_strategy_factory_1 = require("./default-asset-storage-strategy-factory");
|
|
25
|
-
const hashed_asset_naming_strategy_1 = require("./hashed-asset-naming-strategy");
|
|
26
|
-
const sharp_asset_preview_strategy_1 = require("./sharp-asset-preview-strategy");
|
|
27
|
-
const transform_image_1 = require("./transform-image");
|
|
28
|
-
async function getFileType(buffer) {
|
|
29
|
-
const { fileTypeFromBuffer } = await import('file-type');
|
|
30
|
-
return fileTypeFromBuffer(buffer);
|
|
31
|
-
}
|
|
32
25
|
/**
|
|
33
26
|
* @description
|
|
34
27
|
* The `AssetServerPlugin` serves assets (images and other files) from the local file system, and can also be configured to use
|
|
@@ -150,6 +143,32 @@ async function getFileType(buffer) {
|
|
|
150
143
|
* By default, the AssetServerPlugin will cache every transformed image, so that the transformation only needs to be performed a single time for
|
|
151
144
|
* a given configuration. Caching can be disabled per-request by setting the `?cache=false` query parameter.
|
|
152
145
|
*
|
|
146
|
+
* ### Limiting transformations
|
|
147
|
+
*
|
|
148
|
+
* By default, the AssetServerPlugin will allow any transformation to be performed on an image. However, it is possible to restrict the transformations
|
|
149
|
+
* which can be performed by using an {@link ImageTransformStrategy}. This can be used to limit the transformations to a known set of presets, for example.
|
|
150
|
+
*
|
|
151
|
+
* This is advisable in order to prevent abuse of the image transformation feature, as it can be computationally expensive.
|
|
152
|
+
*
|
|
153
|
+
* Since v3.1.0 we ship with a {@link PresetOnlyStrategy} which allows only transformations using a known set of presets.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* import { AssetServerPlugin, PresetOnlyStrategy } from '\@vendure/core';
|
|
158
|
+
*
|
|
159
|
+
* // ...
|
|
160
|
+
*
|
|
161
|
+
* AssetServerPlugin.init({
|
|
162
|
+
* //...
|
|
163
|
+
* imageTransformStrategy: new PresetOnlyStrategy({
|
|
164
|
+
* defaultPreset: 'thumbnail',
|
|
165
|
+
* permittedQuality: [0, 50, 75, 85, 95],
|
|
166
|
+
* permittedFormats: ['jpg', 'webp', 'avif'],
|
|
167
|
+
* allowFocalPoint: false,
|
|
168
|
+
* }),
|
|
169
|
+
* });
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
153
172
|
* @docsCategory core plugins/AssetServerPlugin
|
|
154
173
|
*/
|
|
155
174
|
let AssetServerPlugin = AssetServerPlugin_1 = class AssetServerPlugin {
|
|
@@ -161,25 +180,12 @@ let AssetServerPlugin = AssetServerPlugin_1 = class AssetServerPlugin {
|
|
|
161
180
|
AssetServerPlugin_1.options = options;
|
|
162
181
|
return this;
|
|
163
182
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
var _a;
|
|
167
|
-
const storageStrategyFactory = this.options.storageStrategyFactory || default_asset_storage_strategy_factory_1.defaultAssetStorageStrategyFactory;
|
|
168
|
-
this.assetStorage = await storageStrategyFactory(this.options);
|
|
169
|
-
config.assetOptions.assetPreviewStrategy =
|
|
170
|
-
(_a = this.options.previewStrategy) !== null && _a !== void 0 ? _a : new sharp_asset_preview_strategy_1.SharpAssetPreviewStrategy({
|
|
171
|
-
maxWidth: this.options.previewMaxWidth,
|
|
172
|
-
maxHeight: this.options.previewMaxHeight,
|
|
173
|
-
});
|
|
174
|
-
config.assetOptions.assetStorageStrategy = this.assetStorage;
|
|
175
|
-
config.assetOptions.assetNamingStrategy =
|
|
176
|
-
this.options.namingStrategy || new hashed_asset_naming_strategy_1.HashedAssetNamingStrategy();
|
|
177
|
-
return config;
|
|
178
|
-
}
|
|
179
|
-
constructor(processContext) {
|
|
183
|
+
constructor(options, processContext, moduleRef, assetServer) {
|
|
184
|
+
this.options = options;
|
|
180
185
|
this.processContext = processContext;
|
|
181
|
-
this.
|
|
182
|
-
this.
|
|
186
|
+
this.moduleRef = moduleRef;
|
|
187
|
+
this.assetServer = assetServer;
|
|
188
|
+
this.defaultPresets = [
|
|
183
189
|
{ name: 'tiny', width: 50, height: 50, mode: 'crop' },
|
|
184
190
|
{ name: 'thumb', width: 150, height: 150, mode: 'crop' },
|
|
185
191
|
{ name: 'small', width: 300, height: 300, mode: 'resize' },
|
|
@@ -188,202 +194,90 @@ let AssetServerPlugin = AssetServerPlugin_1 = class AssetServerPlugin {
|
|
|
188
194
|
];
|
|
189
195
|
}
|
|
190
196
|
/** @internal */
|
|
191
|
-
onApplicationBootstrap() {
|
|
197
|
+
async onApplicationBootstrap() {
|
|
192
198
|
if (this.processContext.isWorker) {
|
|
193
199
|
return;
|
|
194
200
|
}
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
this.presets.push(preset);
|
|
201
|
+
if (this.options.imageTransformStrategy != null) {
|
|
202
|
+
const injector = new core_2.Injector(this.moduleRef);
|
|
203
|
+
for (const strategy of this.getImageTransformStrategyArray()) {
|
|
204
|
+
if (typeof strategy.init === 'function') {
|
|
205
|
+
await strategy.init(injector);
|
|
203
206
|
}
|
|
204
207
|
}
|
|
205
208
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
209
|
+
}
|
|
210
|
+
/** @internal */
|
|
211
|
+
async onApplicationShutdown() {
|
|
212
|
+
if (this.processContext.isWorker) {
|
|
213
|
+
return;
|
|
210
214
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
this.cacheHeader = [cacheHeader.restriction, `max-age: ${cacheHeader.maxAge}`]
|
|
217
|
-
.filter(value => !!value)
|
|
218
|
-
.join(', ');
|
|
215
|
+
if (this.options.imageTransformStrategy != null) {
|
|
216
|
+
for (const strategy of this.getImageTransformStrategyArray()) {
|
|
217
|
+
if (typeof strategy.destroy === 'function') {
|
|
218
|
+
await strategy.destroy();
|
|
219
|
+
}
|
|
219
220
|
}
|
|
220
221
|
}
|
|
221
|
-
const cachePath = path_1.default.join(AssetServerPlugin_1.options.assetUploadDir, this.cacheDir);
|
|
222
|
-
fs_extra_1.default.ensureDirSync(cachePath);
|
|
223
222
|
}
|
|
224
223
|
configure(consumer) {
|
|
225
224
|
if (this.processContext.isWorker) {
|
|
226
225
|
return;
|
|
227
226
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
*/
|
|
235
|
-
createAssetServer() {
|
|
236
|
-
const assetServer = express_1.default.Router();
|
|
237
|
-
assetServer.use(this.sendAsset(), this.generateTransformedImage());
|
|
238
|
-
return assetServer;
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Reads the file requested and send the response to the browser.
|
|
242
|
-
*/
|
|
243
|
-
sendAsset() {
|
|
244
|
-
return async (req, res, next) => {
|
|
245
|
-
var _a;
|
|
246
|
-
const key = this.getFileNameFromRequest(req);
|
|
247
|
-
try {
|
|
248
|
-
const file = await AssetServerPlugin_1.assetStorage.readFileToBuffer(key);
|
|
249
|
-
let mimeType = this.getMimeType(key);
|
|
250
|
-
if (!mimeType) {
|
|
251
|
-
mimeType = ((_a = (await getFileType(file))) === null || _a === void 0 ? void 0 : _a.mime) || 'application/octet-stream';
|
|
227
|
+
const presets = [...this.defaultPresets];
|
|
228
|
+
if (this.options.presets) {
|
|
229
|
+
for (const preset of this.options.presets) {
|
|
230
|
+
const existingIndex = presets.findIndex(p => p.name === preset.name);
|
|
231
|
+
if (-1 < existingIndex) {
|
|
232
|
+
presets.splice(existingIndex, 1, preset);
|
|
252
233
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
res.setHeader('Cache-Control', this.cacheHeader);
|
|
256
|
-
res.send(file);
|
|
257
|
-
}
|
|
258
|
-
catch (e) {
|
|
259
|
-
const err = new Error('File not found');
|
|
260
|
-
err.status = 404;
|
|
261
|
-
return next(err);
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* If an exception was thrown by the first handler, then it may be because a transformed image
|
|
267
|
-
* is being requested which does not yet exist. In this case, this handler will generate the
|
|
268
|
-
* transformed image, save it to cache, and serve the result as a response.
|
|
269
|
-
*/
|
|
270
|
-
generateTransformedImage() {
|
|
271
|
-
return async (err, req, res, next) => {
|
|
272
|
-
var _a;
|
|
273
|
-
if (err && (err.status === 404 || err.statusCode === 404)) {
|
|
274
|
-
if (req.query) {
|
|
275
|
-
const decodedReqPath = decodeURIComponent(req.path);
|
|
276
|
-
core_1.Logger.debug(`Pre-cached Asset not found: ${decodedReqPath}`, constants_1.loggerCtx);
|
|
277
|
-
let file;
|
|
278
|
-
try {
|
|
279
|
-
file = await AssetServerPlugin_1.assetStorage.readFileToBuffer(decodedReqPath);
|
|
280
|
-
}
|
|
281
|
-
catch (_err) {
|
|
282
|
-
res.status(404).send('Resource not found');
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
const image = await (0, transform_image_1.transformImage)(file, req.query, this.presets || []);
|
|
286
|
-
try {
|
|
287
|
-
const imageBuffer = await image.toBuffer();
|
|
288
|
-
const cachedFileName = this.getFileNameFromRequest(req);
|
|
289
|
-
if (!req.query.cache || req.query.cache === 'true') {
|
|
290
|
-
await AssetServerPlugin_1.assetStorage.writeFileFromBuffer(cachedFileName, imageBuffer);
|
|
291
|
-
core_1.Logger.debug(`Saved cached asset: ${cachedFileName}`, constants_1.loggerCtx);
|
|
292
|
-
}
|
|
293
|
-
let mimeType = this.getMimeType(cachedFileName);
|
|
294
|
-
if (!mimeType) {
|
|
295
|
-
mimeType = ((_a = (await getFileType(imageBuffer))) === null || _a === void 0 ? void 0 : _a.mime) || 'image/jpeg';
|
|
296
|
-
}
|
|
297
|
-
res.set('Content-Type', mimeType);
|
|
298
|
-
res.setHeader('content-security-policy', "default-src 'self'");
|
|
299
|
-
res.send(imageBuffer);
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
catch (e) {
|
|
303
|
-
core_1.Logger.error(e.message, constants_1.loggerCtx, e.stack);
|
|
304
|
-
res.status(500).send('An error occurred when generating the image');
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
234
|
+
else {
|
|
235
|
+
presets.push(preset);
|
|
307
236
|
}
|
|
308
237
|
}
|
|
309
|
-
next();
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
getFileNameFromRequest(req) {
|
|
313
|
-
const { w, h, mode, preset, fpx, fpy, format, q } = req.query;
|
|
314
|
-
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
|
315
|
-
const focalPoint = fpx && fpy ? `_fpx${fpx}_fpy${fpy}` : '';
|
|
316
|
-
const quality = q ? `_q${q}` : '';
|
|
317
|
-
const imageFormat = (0, common_1.getValidFormat)(format);
|
|
318
|
-
let imageParamsString = '';
|
|
319
|
-
if (w || h) {
|
|
320
|
-
const width = w || '';
|
|
321
|
-
const height = h || '';
|
|
322
|
-
imageParamsString = `_transform_w${width}_h${height}_m${mode}`;
|
|
323
|
-
}
|
|
324
|
-
else if (preset) {
|
|
325
|
-
if (this.presets && !!this.presets.find(p => p.name === preset)) {
|
|
326
|
-
imageParamsString = `_transform_pre_${preset}`;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
if (focalPoint) {
|
|
330
|
-
imageParamsString += focalPoint;
|
|
331
|
-
}
|
|
332
|
-
if (imageFormat) {
|
|
333
|
-
imageParamsString += imageFormat;
|
|
334
|
-
}
|
|
335
|
-
if (quality) {
|
|
336
|
-
imageParamsString += quality;
|
|
337
|
-
}
|
|
338
|
-
/* eslint-enable @typescript-eslint/restrict-template-expressions */
|
|
339
|
-
const decodedReqPath = decodeURIComponent(req.path);
|
|
340
|
-
if (imageParamsString !== '') {
|
|
341
|
-
const imageParamHash = this.md5(imageParamsString);
|
|
342
|
-
return path_1.default.join(this.cacheDir, this.addSuffix(decodedReqPath, imageParamHash, imageFormat));
|
|
343
|
-
}
|
|
344
|
-
else {
|
|
345
|
-
return decodedReqPath;
|
|
346
238
|
}
|
|
239
|
+
core_2.Logger.info('Creating asset server middleware', constants_1.loggerCtx);
|
|
240
|
+
const assetServerRouter = this.assetServer.createAssetServer({
|
|
241
|
+
presets,
|
|
242
|
+
imageTransformStrategies: this.getImageTransformStrategyArray(),
|
|
243
|
+
});
|
|
244
|
+
consumer.apply(assetServerRouter).forRoutes(this.options.route);
|
|
245
|
+
(0, core_2.registerPluginStartupMessage)('Asset server', this.options.route);
|
|
347
246
|
}
|
|
348
|
-
|
|
349
|
-
return
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
const baseName = path_1.default.basename(fileName, originalExt);
|
|
355
|
-
const dirName = path_1.default.dirname(fileName);
|
|
356
|
-
return path_1.default.join(dirName, `${baseName}${suffix}${effectiveExt}`);
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
* Attempt to get the mime type from the file name.
|
|
360
|
-
*/
|
|
361
|
-
getMimeType(fileName) {
|
|
362
|
-
const ext = path_1.default.extname(fileName);
|
|
363
|
-
switch (ext) {
|
|
364
|
-
case '.jpg':
|
|
365
|
-
case '.jpeg':
|
|
366
|
-
return 'image/jpeg';
|
|
367
|
-
case '.png':
|
|
368
|
-
return 'image/png';
|
|
369
|
-
case '.gif':
|
|
370
|
-
return 'image/gif';
|
|
371
|
-
case '.svg':
|
|
372
|
-
return 'image/svg+xml';
|
|
373
|
-
case '.tiff':
|
|
374
|
-
return 'image/tiff';
|
|
375
|
-
case '.webp':
|
|
376
|
-
return 'image/webp';
|
|
377
|
-
}
|
|
247
|
+
getImageTransformStrategyArray() {
|
|
248
|
+
return this.options.imageTransformStrategy
|
|
249
|
+
? Array.isArray(this.options.imageTransformStrategy)
|
|
250
|
+
? this.options.imageTransformStrategy
|
|
251
|
+
: [this.options.imageTransformStrategy]
|
|
252
|
+
: [];
|
|
378
253
|
}
|
|
379
254
|
};
|
|
380
255
|
exports.AssetServerPlugin = AssetServerPlugin;
|
|
381
256
|
exports.AssetServerPlugin = AssetServerPlugin = AssetServerPlugin_1 = __decorate([
|
|
382
|
-
(0,
|
|
383
|
-
imports: [
|
|
384
|
-
configuration: config =>
|
|
257
|
+
(0, core_2.VendurePlugin)({
|
|
258
|
+
imports: [core_2.PluginCommonModule],
|
|
259
|
+
configuration: async (config) => {
|
|
260
|
+
var _a;
|
|
261
|
+
const options = AssetServerPlugin.options;
|
|
262
|
+
const storageStrategyFactory = options.storageStrategyFactory || default_asset_storage_strategy_factory_1.defaultAssetStorageStrategyFactory;
|
|
263
|
+
config.assetOptions.assetPreviewStrategy =
|
|
264
|
+
(_a = options.previewStrategy) !== null && _a !== void 0 ? _a : new sharp_asset_preview_strategy_1.SharpAssetPreviewStrategy({
|
|
265
|
+
maxWidth: options.previewMaxWidth,
|
|
266
|
+
maxHeight: options.previewMaxHeight,
|
|
267
|
+
});
|
|
268
|
+
config.assetOptions.assetStorageStrategy = await storageStrategyFactory(options);
|
|
269
|
+
config.assetOptions.assetNamingStrategy = options.namingStrategy || new hashed_asset_naming_strategy_1.HashedAssetNamingStrategy();
|
|
270
|
+
return config;
|
|
271
|
+
},
|
|
272
|
+
providers: [
|
|
273
|
+
{ provide: constants_1.ASSET_SERVER_PLUGIN_INIT_OPTIONS, useFactory: () => AssetServerPlugin.options },
|
|
274
|
+
asset_server_1.AssetServer,
|
|
275
|
+
],
|
|
385
276
|
compatibility: '^3.0.0',
|
|
386
277
|
}),
|
|
387
|
-
|
|
278
|
+
__param(0, (0, common_1.Inject)(constants_1.ASSET_SERVER_PLUGIN_INIT_OPTIONS)),
|
|
279
|
+
__metadata("design:paramtypes", [Object, core_2.ProcessContext,
|
|
280
|
+
core_1.ModuleRef,
|
|
281
|
+
asset_server_1.AssetServer])
|
|
388
282
|
], AssetServerPlugin);
|
|
389
283
|
//# sourceMappingURL=plugin.js.map
|
package/lib/src/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAMwB;AACxB,uCAAyC;AAEzC,wCAOuB;AAEvB,iDAA6C;AAC7C,4GAAqG;AACrG,wFAAkF;AAElF,wFAAkF;AAClF,2CAA0E;AAG1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoJG;AAsBI,IAAM,iBAAiB,yBAAvB,MAAM,iBAAiB;IAU1B;;;OAGG;IACH,MAAM,CAAC,IAAI,CAAC,OAA2B;QACnC,mBAAiB,CAAC,OAAO,GAAG,OAAO,CAAC;QACpC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,YAC8C,OAAmC,EACrE,cAA8B,EAC9B,SAAoB,EACpB,WAAwB;QAHkB,YAAO,GAAP,OAAO,CAAoB;QACrE,mBAAc,GAAd,cAAc,CAAgB;QAC9B,cAAS,GAAT,SAAS,CAAW;QACpB,gBAAW,GAAX,WAAW,CAAa;QArBnB,mBAAc,GAA2B;YACtD,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAA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iBAAiB,CAsF7B"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import sharp, { Region } from 'sharp';
|
|
3
|
-
import {
|
|
3
|
+
import { ImageTransformParameters } from './config/image-transform-strategy';
|
|
4
4
|
export type Dimensions = {
|
|
5
5
|
w: number;
|
|
6
6
|
h: number;
|
|
@@ -12,7 +12,7 @@ export type Point = {
|
|
|
12
12
|
/**
|
|
13
13
|
* Applies transforms to the given image according to the query params passed.
|
|
14
14
|
*/
|
|
15
|
-
export declare function transformImage(originalImage: Buffer,
|
|
15
|
+
export declare function transformImage(originalImage: Buffer, parameters: ImageTransformParameters): Promise<sharp.Sharp>;
|
|
16
16
|
/**
|
|
17
17
|
* Resize an image but keep it centered on the focal point.
|
|
18
18
|
* Based on the method outlined in https://github.com/lovell/sharp/issues/1198#issuecomment-384591756
|