5htp-core 0.5.1-6 → 0.5.1-8

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "5htp-core",
3
3
  "description": "Convenient TypeScript framework designed for Performance and Productivity.",
4
- "version": "0.5.1-6",
4
+ "version": "0.5.1-8",
5
5
  "author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
6
6
  "repository": "git://github.com/gaetanlegac/5htp-core.git",
7
7
  "license": "MIT",
@@ -73,7 +73,6 @@
73
73
  "react-textarea-autosize": "^8.3.3",
74
74
  "regenerator-runtime": "^0.13.9",
75
75
  "request": "^2.88.2",
76
- "sharp": "^0.29.1",
77
76
  "slugify": "^1.6.6",
78
77
  "sql-formatter": "^4.0.2",
79
78
  "stopword": "^3.1.1",
@@ -85,7 +84,8 @@
85
84
  "yaml": "^1.10.2",
86
85
  "yargs-parser": "^21.1.1",
87
86
  "youch": "^3.3.3",
88
- "youch-terminal": "^2.2.3"
87
+ "youch-terminal": "^2.2.3",
88
+ "zod": "^3.24.2"
89
89
  },
90
90
  "devDependencies": {
91
91
  "@types/cookie": "^0.4.1",
@@ -99,7 +99,8 @@
99
99
  "@types/webpack-env": "^1.16.2",
100
100
  "@types/ws": "^7.4.7",
101
101
  "@types/yargs-parser": "^21.0.0",
102
- "schema-dts": "^1.1.2"
102
+ "schema-dts": "^1.1.2",
103
+ "sharp": "^0.33.5"
103
104
  },
104
105
  "peerDependencies": {
105
106
  "5htp": "0.4.5"
@@ -154,6 +154,27 @@ export class AuthRequired extends CoreError {
154
154
  public static msgDefaut = "Please Login to Continue.";
155
155
  }
156
156
 
157
+ export class UpgradeRequired extends CoreError {
158
+ public http = 402;
159
+ public title = "Upgrade Required";
160
+ public static msgDefaut = "Please Upgrade to Continue.";
161
+
162
+ public constructor(
163
+ message: string,
164
+ public feature: string,
165
+ details?: TErrorDetails
166
+ ) {
167
+ super(message, details);
168
+ }
169
+
170
+ public json(): TJsonError & { feature: string } {
171
+ return {
172
+ ...super.json(),
173
+ feature: this.feature,
174
+ }
175
+ }
176
+ }
177
+
157
178
  export class Forbidden extends CoreError {
158
179
  public http = 403;
159
180
  public title = "Access Denied";
@@ -235,6 +256,8 @@ export const fromJson = ({ code, message, ...details }: TJsonError) => {
235
256
 
236
257
  case 401: return new AuthRequired( message, details );
237
258
 
259
+ case 402: return new UpgradeRequired( message, details.feature, details );
260
+
238
261
  case 403: return new Forbidden( message, details );
239
262
 
240
263
  case 404: return new NotFound( message, details );
@@ -318,9 +318,11 @@ export default abstract class Service<
318
318
  return Promise.all(
319
319
  callbacks.map(
320
320
  cb => cb(...args).catch(e => {
321
- console.error(`[hook] Error while executing hook ${name}:`, e);
321
+
322
322
  if (name !== 'error')
323
323
  this.runHook('error', e);
324
+ else
325
+ console.error(`[hook] Error while executing hook ${name}:`, e);
324
326
  })
325
327
  )
326
328
  ).then(() => {
@@ -3,7 +3,7 @@
3
3
  ----------------------------------*/
4
4
 
5
5
  // Npm
6
- import sharp from 'sharp';
6
+ import type { default as sharp, Sharp } from 'sharp';
7
7
  import fs from 'fs-extra';
8
8
  import got, { Method, Options } from 'got';
9
9
 
@@ -43,6 +43,7 @@ export type Services = {
43
43
  ----------------------------------*/
44
44
 
45
45
  export type TImageConfig = {
46
+ sharp: typeof sharp,
46
47
  width: number,
47
48
  height: number,
48
49
  fit: keyof sharp.FitEnum,
@@ -172,7 +173,7 @@ export default class FetchService extends Service<Config, Hooks, Application, Se
172
173
 
173
174
  public async image(
174
175
  imageFileUrl: string,
175
- { width, height, fit, quality }: TImageConfig,
176
+ imageMod: TImageConfig,
176
177
  saveToBucket: string,
177
178
  saveToPath?: string,
178
179
  disk?: string
@@ -183,7 +184,7 @@ export default class FetchService extends Service<Config, Hooks, Application, Se
183
184
  throw new Error(`Please provide a Disks service in order to download files.`);
184
185
 
185
186
  // Download
186
- let imageBuffer: Buffer;
187
+ let imageBuffer: Buffer | null;
187
188
  try {
188
189
  imageBuffer = await this.toBuffer( imageFileUrl );
189
190
  } catch (error) {
@@ -191,21 +192,27 @@ export default class FetchService extends Service<Config, Hooks, Application, Se
191
192
  return null;
192
193
  }
193
194
 
194
- // Resize
195
- const processing = sharp( imageBuffer )
196
- // Max dimensions (save space)
197
- .resize(width, height, { fit })
195
+ if (imageMod) {
198
196
 
199
- // Convert to webp and finalize
200
- const processedBuffer = await processing.webp({ quality }).toBuffer().catch(e => {
201
- console.error(LogPrefix, `Error while processing image at ${imageFileUrl}:`, e);
202
- return null;
203
- })
197
+ const { sharp, width, height, fit, quality } = imageMod;
198
+
199
+ // Resize
200
+ const processing = sharp( imageBuffer )
201
+ // Max dimensions (save space)
202
+ .resize(width, height, { fit })
203
+
204
+ // Convert to webp and finalize
205
+ imageBuffer = await processing.webp({ quality }).toBuffer().catch(e => {
206
+ console.error(LogPrefix, `Error while processing image at ${imageFileUrl}:`, e);
207
+ return null;
208
+ })
209
+
210
+ }
204
211
 
205
212
  // Save file
206
- if (saveToPath !== undefined && processedBuffer !== null) {
213
+ if (saveToPath !== undefined && imageBuffer !== null) {
207
214
  console.log(LogPrefix, `Saving ${imageFileUrl} logo to ${saveToPath}`);
208
- await this.disk.outputFile(saveToBucket, saveToPath, processedBuffer);
215
+ await this.disk.outputFile(saveToBucket, saveToPath, imageBuffer);
209
216
  }
210
217
 
211
218
  // We return the original, because Vibrant.js doesn't support webp
@@ -164,7 +164,7 @@ export default class HttpServer {
164
164
  // Décodage des données post
165
165
  express.json({
166
166
  // TODO: prendre en considération les upload de fichiers
167
- limit: '2mb',
167
+ limit: bytes(this.config.upload.maxSize),
168
168
  verify: (req, res, buf, encoding) => {
169
169
  // Store the raw request body so we can access it later
170
170
  req.rawBody = buf;
@@ -583,11 +583,8 @@ declare type Routes = {
583
583
  private async handleError( e: CoreError, request: ServerRequest<ServerRouter> ) {
584
584
 
585
585
  const code = 'http' in e ? e.http : 500;
586
- const route = this.errors[code];
587
- if (route === undefined)
588
- throw new Error(`No route for error code ${code}`);
589
586
 
590
- const response = new ServerResponse(request).status(code).setRoute(route);
587
+ const response = new ServerResponse(request).status(code)
591
588
 
592
589
  // Rapport / debug
593
590
  if (code === 500) {
@@ -613,10 +610,16 @@ declare type Routes = {
613
610
 
614
611
  // Return error based on the request format
615
612
  if (request.accepts("html")) {
613
+
614
+ const route = this.errors[code];
615
+ if (route === undefined)
616
+ throw new Error(`No route for error code ${code}`);
617
+
616
618
  const jsonError = errorToJson(e);
617
- await response.runController(route, {
619
+ await response.setRoute(route).runController(route, {
618
620
  error: jsonError
619
621
  });
622
+
620
623
  } else if (request.accepts("json")) {
621
624
  const jsonError = errorToJson(e);
622
625
  await response.json(jsonError);