Skip to content

Commit ee4e511

Browse files
Merge pull request #28 from ikhsanalatsary/development
Fix sharp v0.25
2 parents b1a9e09 + c759c5e commit ee4e511

15 files changed

+6692
-4882
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
yarn-error.log
33
coverage
4+
.env

.travis.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
sudo: false
21
language: node_js
3-
matrix:
2+
jobs:
43
include:
54
- os: linux
6-
dist: trusty
7-
sudo: false
8-
node_js: "10"
5+
dist: bionic
6+
node_js: "12"
97
install:
10-
- yarn
8+
- npm install
119
script:
12-
- yarn test
10+
- npm test
1311
after_success:
14-
- yarn report-coverage
12+
- npm run report-coverage

__tests__/implementation.spec.ts

Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import * as supertest from 'supertest'
77
import * as multer from 'multer'
88
import * as aws from 'aws-sdk'
99
import * as crypto from 'crypto'
10+
import * as sharp from 'sharp'
1011

1112
import multerSharp from '../src/main'
1213
const config = {
1314
uploads: {
1415
aws: {
1516
Bucket: process.env.AWS_BUCKET,
16-
ACL: 'public-read',
17+
ACL: 'private',
1718
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
1819
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
1920
region: process.env.AWS_REGION,
@@ -54,11 +55,9 @@ const storage = multerSharp({
5455
width: 400,
5556
height: 400,
5657
options: {
57-
kernel: 'lanczos2',
58-
interpolator: 'nohalo',
58+
kernel: sharp.kernel.lanczos2,
5959
},
6060
},
61-
max: true,
6261
})
6362
const upload = multer({ storage })
6463
const storage2 = multerSharp({
@@ -97,10 +96,6 @@ const storage4 = multerSharp({
9796
Key: `${config.uploads.aws.Bucket}/test/${Date.now()}-myPic`,
9897
ACL: config.uploads.aws.ACL,
9998
resize: { width: 200 },
100-
crop: 16, // crop strategy
101-
background: { r: 0, g: 0, b: 100, alpha: 0 },
102-
withoutEnlargement: true,
103-
ignoreAspectRatio: true,
10499
trim: 50,
105100
flatten: true,
106101
extend: { top: 10, bottom: 20, left: 10, right: 10 },
@@ -129,13 +124,6 @@ const storage5 = multerSharp({
129124
Key: `${config.uploads.aws.Bucket}/test/${Date.now()}-myPic`,
130125
ACL: config.uploads.aws.ACL,
131126
resize: { width: 400, height: 400 },
132-
crop: 'north',
133-
background: { r: 0, g: 0, b: 0, alpha: 0 },
134-
embed: true,
135-
max: true,
136-
min: true,
137-
withoutEnlargement: true,
138-
ignoreAspectRatio: true,
139127
extract: { left: 0, top: 2, width: 50, height: 100 },
140128
trim: 50,
141129
flatten: true,
@@ -152,6 +140,7 @@ const storage5 = multerSharp({
152140
toFormat: 'jpeg',
153141
withMetadata: {
154142
orientation: 4,
143+
chromaSubsampling: '4:4:4',
155144
},
156145
convolve: {
157146
width: 3,
@@ -172,7 +161,6 @@ const storage6 = multerSharp({
172161
width: 400,
173162
height: 400,
174163
},
175-
max: true,
176164
extract: { left: 0, top: 2, width: 400, height: 400 },
177165
})
178166
const upload6 = multer({ storage: storage6 });
@@ -181,7 +169,7 @@ const storage8 = multerSharp({
181169
s3,
182170
Bucket: config.uploads.aws.Bucket,
183171
Key: `${config.uploads.aws.Bucket}/test/${Date.now()}-myPic`,
184-
// ACL: config.uploads.aws.ACL,
172+
ACL: config.uploads.aws.ACL,
185173
multiple: true,
186174
resize: [
187175
// { suffix: 'xlg', width: 1200, height: 1200 },
@@ -249,14 +237,14 @@ app.post('/upload', (req, res, next) => {
249237

250238
// express setup
251239
app.post('/uploadwitherrorkey', (req, res, next) => {
252-
upload2.single('myPic')(req, res, (errorFile) => {
240+
upload2.single('myPic')(req, res, (errorFile) => {
253241
lastReq = req
254242
lastRes = res
255243
// console.log(errorFile.stack);
256244
res.status(400).json({ message: errorFile.message })
257245

258246
next()
259-
})
247+
})
260248
});
261249

262250
// express setup
@@ -278,10 +266,10 @@ app.post('/uploadwithsharpsetting', upload4.single('myPic'), (req, res, next) =>
278266
// // express setup
279267
app.post('/uploadanddelete', (req, res, next) => {
280268
upload5.single('myPic')(req, res, (err) => {
281-
if (err) {next(err)}
269+
if (err) { next(err) }
282270
storage5._removeFile(req, req.file, (err) => {
283271
// eslint-disable-line no-underscore-dangle
284-
if (err) {next(err)}
272+
if (err) { next(err) }
285273
res.sendStatus(200)
286274
next()
287275
})
@@ -330,15 +318,22 @@ app.post('/uploadfilewithdefaultkey', upload11.single('myFile'), (req, res, next
330318
// express setup
331319
app.post(
332320
'/uploadwithmultiplesize',
333-
upload8.single('myPic'),
334321
(req, res, next) => {
335-
lastReq = req
336-
lastRes = res
337-
338-
if (lastReq && lastReq.file) {
322+
upload8.single('myPic')(req, res, (err) => {
323+
lastReq = req
324+
lastRes = res
325+
if (err) { throw err };
339326
res.sendStatus(200)
340-
}
341-
next()
327+
next()
328+
})
329+
// lastReq = req
330+
// lastRes = res
331+
// console.log('req ', req.file)
332+
333+
// if (lastReq && lastReq.file) {
334+
// res.sendStatus(200)
335+
// }
336+
// next()
342337
}
343338
)
344339

@@ -396,7 +391,7 @@ describe('S3Storage', () => {
396391
})
397392

398393
describe('Upload test', () => {
399-
// this.timeout(15000);
394+
jest.setTimeout(15000);
400395
it('initial server', (done) => {
401396
supertest(app)
402397
.get('/book')
@@ -408,6 +403,8 @@ describe('Upload test', () => {
408403
.attach('myPic', '__tests__/nodejs-512.png')
409404
.end((err) => {
410405
const file = lastReq.file
406+
// console.log('filee ', file);
407+
expect(file).toBeDefined()
411408
expect(file).toHaveProperty('Location')
412409
expect(file).toHaveProperty('fieldname');
413410
expect(file).toHaveProperty('encoding');
@@ -418,6 +415,50 @@ describe('Upload test', () => {
418415
})
419416
// .expect(200, done);
420417
})
418+
it('return a req.file with multiple sizes', (done) => {
419+
// jest.setTimeout(done, 1000);
420+
supertest(app)
421+
.post('/uploadwithmultiplesize')
422+
.attach('myPic', '__tests__/nodejs-512.png')
423+
.end(() => {
424+
const file = lastReq.file
425+
expect(file).toHaveProperty('original')
426+
expect(file).toHaveProperty('md')
427+
expect(file).toHaveProperty('sm')
428+
expect(file).toHaveProperty('xs')
429+
expect(file).toHaveProperty('fieldname');
430+
expect(file).toHaveProperty('encoding');
431+
expect(file).toHaveProperty('mimetype');
432+
expect(file).toHaveProperty('originalname');
433+
expect(file.xs).toHaveProperty('Key')
434+
expect(file.md).toHaveProperty('Key')
435+
expect(file.sm).toHaveProperty('Key')
436+
expect(file.original).toHaveProperty('Key')
437+
expect(file.xs).toHaveProperty('Location')
438+
expect(file.md).toHaveProperty('Location')
439+
expect(file.sm).toHaveProperty('Location')
440+
expect(file.original).toHaveProperty('Location')
441+
done()
442+
})
443+
})
444+
445+
it('upload file without Key', (done) => {
446+
supertest(app)
447+
.post('/uploadfilewithdefaultkey')
448+
.attach('myFile', '.travis.yml')
449+
.end((err, res) => {
450+
const { file } = lastReq
451+
expect(file).toHaveProperty('Key')
452+
expect(file).toHaveProperty('fieldname')
453+
expect(file).toHaveProperty('encoding')
454+
expect(file).toHaveProperty('mimetype')
455+
expect(file).toHaveProperty('originalname')
456+
expect(file.mimetype).toMatch('text/yaml')
457+
expect(file.fieldname).toMatch('myFile')
458+
expect(file.Location).toMatch('aws')
459+
done()
460+
})
461+
})
421462

422463
// it('returns a req.file with the Google Cloud Storage filename and path', (done) => {
423464
// supertest(app)
@@ -508,49 +549,6 @@ describe('Upload test', () => {
508549
done()
509550
})
510551
});
511-
it('return a req.file with multiple sizes', (done) => {
512-
// jest.setTimeout(done, 1000);
513-
supertest(app)
514-
.post('/uploadwithmultiplesize')
515-
.attach('myPic', '__tests__/nodejs-512.png')
516-
.end(() => {
517-
const file = lastReq.file
518-
expect(file).toHaveProperty('original')
519-
expect(file).toHaveProperty('md')
520-
expect(file).toHaveProperty('sm')
521-
expect(file).toHaveProperty('xs')
522-
expect(file).toHaveProperty('fieldname');
523-
expect(file).toHaveProperty('encoding');
524-
expect(file).toHaveProperty('mimetype');
525-
expect(file).toHaveProperty('originalname');
526-
expect(file.xs).toHaveProperty('Key')
527-
expect(file.md).toHaveProperty('Key')
528-
expect(file.sm).toHaveProperty('Key')
529-
expect(file.original).toHaveProperty('Key')
530-
expect(file.xs).toHaveProperty('Location')
531-
expect(file.md).toHaveProperty('Location')
532-
expect(file.sm).toHaveProperty('Location')
533-
expect(file.original).toHaveProperty('Location')
534-
done()
535-
})
536-
})
537-
it('upload file without Key', (done) => {
538-
supertest(app)
539-
.post('/uploadfilewithdefaultkey')
540-
.attach('myFile', '.travis.yml')
541-
.end((err, res) => {
542-
const { file } = lastReq
543-
expect(file).toHaveProperty('Key')
544-
expect(file).toHaveProperty('fieldname')
545-
expect(file).toHaveProperty('encoding')
546-
expect(file).toHaveProperty('mimetype')
547-
expect(file).toHaveProperty('originalname')
548-
expect(file.mimetype).toMatch('text/yaml')
549-
expect(file.fieldname).toMatch('myFile')
550-
expect(file.Location).toMatch('aws')
551-
done()
552-
})
553-
});
554552
// it('upload multisize and return error, cause transform/resize error', (done) => {
555553
// supertest(app)
556554
// .post('/uploadwithmultiplesizetransformerror')

dist/types/main.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="node" />
12
import * as sharp from 'sharp';
23
import { ManagedUpload } from 'aws-sdk/lib/s3/managed_upload';
34
import { StorageEngine } from 'multer';
@@ -6,7 +7,7 @@ import { S3 } from 'aws-sdk';
67
import defaultKey from './get-filename';
78
import { S3StorageOptions, SharpOptions } from './types';
89
export declare type EStream = {
9-
stream: NodeJS.ReadableStream & sharp.SharpInstance;
10+
stream: NodeJS.ReadableStream & sharp.Sharp;
1011
};
1112
export declare type EFile = Express.Multer.File & EStream & Partial<S3.Types.PutObjectRequest>;
1213
export declare type Info = Partial<Express.Multer.File & ManagedUpload.SendData & S3.Types.PutObjectRequest & sharp.OutputInfo>;

dist/types/main.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/types/transformer.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as sharp from 'sharp';
22
import { ResizeOption, SharpOptions } from './types';
33
export default transformer;
4-
declare function transformer(options: SharpOptions, size: ResizeOption): sharp.SharpInstance;
4+
declare function transformer(options: SharpOptions, size: ResizeOption): sharp.Sharp;
55
//# sourceMappingURL=transformer.d.ts.map

dist/types/transformer.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/types/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
PngOptions,
1111
Metadata,
1212
Kernel,
13-
SharpInstance,
13+
Sharp,
1414
} from 'sharp'
1515
import { S3 } from 'aws-sdk'
1616

@@ -38,7 +38,7 @@ export declare interface Format {
3838

3939
export declare interface ExtendSize {
4040
suffix: string
41-
Body?: NodeJS.ReadableStream & SharpInstance
41+
Body?: NodeJS.ReadableStream & Sharp
4242
}
4343

4444
export declare type SharpOption<T = string> = T

0 commit comments

Comments
 (0)