Skip to content

Commit c48a1ec

Browse files
committed
f
1 parent 998a6d1 commit c48a1ec

File tree

12 files changed

+289
-289
lines changed

12 files changed

+289
-289
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@
9898
"src"
9999
],
100100
"types": "./dist/commonjs/index.d.ts",
101-
"main": "./dist/commonjs/index.js"
101+
"main": "./dist/commonjs/index.js",
102+
"module": "./dist/esm/index.js"
102103
}

src/app/middleware/multipart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { pathMatching } from 'egg-path-matching';
22
import type { Context, Next, EggCore } from '@eggjs/core';
3-
import type { MultipartConfig } from '/src/config/config.default.js';
3+
import type { MultipartConfig } from '../../config/config.default.js';
44

55
// eslint-disable-next-line @typescript-eslint/no-unused-vars
66
export default (options: MultipartConfig, _app: EggCore) => {

src/lib/utils.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const whitelist = [
1515
'.psd',
1616
// text
1717
'.svg',
18-
'.js', '.jsx',
18+
'.js', '.jsx', '.ts', '.tsx',
1919
'.json',
2020
'.css', '.less',
2121
'.html', '.htm',
@@ -27,7 +27,7 @@ export const whitelist = [
2727
'.mp3',
2828
'.mp4',
2929
'.avi',
30-
] as const;
30+
];
3131

3232
export function humanizeBytes(size: number | string) {
3333
if (typeof size === 'number') {
@@ -62,11 +62,15 @@ export function normalizeOptions(options: MultipartConfig) {
6262
}
6363

6464
function checkExt(fileName: string) {
65-
if (typeof options.whitelist === 'function') return options.whitelist(fileName);
65+
if (typeof options.whitelist === 'function') {
66+
return options.whitelist(fileName);
67+
}
6668
const extname = path.extname(fileName).toLowerCase();
67-
if (Array.isArray(options.whitelist)) return options.whitelist.includes(extname);
69+
if (Array.isArray(options.whitelist)) {
70+
return options.whitelist.includes(extname);
71+
}
6872
// only if user don't provide whitelist, we will use default whitelist + fileExtensions
69-
return exports.whitelist.includes(extname) || options.fileExtensions.includes(extname);
73+
return whitelist.includes(extname) || options.fileExtensions.includes(extname);
7074
}
7175

7276
options.checkFile = (_fieldName: string, fileStream: any, fileName: string): void | Error => {
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict';
1+
import { strict as assert } from 'node:assert';
2+
import fs from 'node:fs/promises';
3+
import formstream from 'formstream';
4+
import urllib from 'urllib';
5+
import { mm, MockApplication } from '@eggjs/mock';
26

3-
const assert = require('assert');
4-
const formstream = require('formstream');
5-
const urllib = require('urllib');
6-
const mock = require('egg-mock');
7-
const fs = require('fs').promises;
8-
9-
describe('test/dynamic-option.test.js', () => {
10-
let app;
11-
let server;
12-
let host;
7+
describe('test/dynamic-option.test.ts', () => {
8+
let app: MockApplication;
9+
let server: any;
10+
let host: string;
1311
before(() => {
14-
app = mock.app({
12+
app = mm.app({
1513
baseDir: 'apps/dynamic-option',
1614
});
1715
return app.ready();
@@ -26,7 +24,7 @@ describe('test/dynamic-option.test.js', () => {
2624
after(() => app.close());
2725
after(() => server.close());
2826
beforeEach(() => app.mockCsrf());
29-
afterEach(mock.restore);
27+
afterEach(() => mm.restore());
3028

3129
it('should work with saveRequestFiles options', async () => {
3230
const form = formstream();
@@ -36,11 +34,11 @@ describe('test/dynamic-option.test.js', () => {
3634
const res = await urllib.request(host + '/upload', {
3735
method: 'POST',
3836
headers,
39-
stream: form,
37+
stream: form as any,
4038
// dataType: 'json',
4139
});
4240

43-
assert(res.status === 413);
41+
assert.equal(res.status, 413);
4442
assert.match(res.data.toString(), /Error: Reach fileSize limit/);
4543
});
4644
});

test/file-mode.test.js renamed to test/file-mode.test.ts

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
'use strict';
2-
3-
const assert = require('assert');
4-
const formstream = require('formstream');
5-
const urllib = require('urllib');
6-
const path = require('path');
7-
const mock = require('egg-mock');
8-
const fs = require('fs').promises;
9-
const dayjs = require('dayjs');
10-
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
11-
12-
describe('test/file-mode.test.js', () => {
13-
let app;
14-
let server;
15-
let host;
1+
import assert from 'node:assert';
2+
import path from 'node:path';
3+
import fs from 'node:fs/promises';
4+
import { scheduler } from 'node:timers/promises';
5+
import { fileURLToPath } from 'node:url';
6+
import { mm, mock, MockApplication } from '@eggjs/mock';
7+
import dayjs from 'dayjs';
8+
import formstream from 'formstream';
9+
import urllib from 'urllib';
10+
11+
const __filename = fileURLToPath(import.meta.url);
12+
const __dirname = path.dirname(__filename);
13+
14+
describe('test/file-mode.test.ts', () => {
15+
let app: MockApplication;
16+
let server: any;
17+
let host: string;
1618
before(() => {
17-
app = mock.app({
19+
app = mm.app({
1820
baseDir: 'apps/file-mode',
1921
});
2022
return app.ready();
@@ -29,7 +31,7 @@ describe('test/file-mode.test.js', () => {
2931
after(() => app.close());
3032
after(() => server.close());
3133
beforeEach(() => app.mockCsrf());
32-
afterEach(mock.restore);
34+
afterEach(mm.restore);
3335

3436
it('should ignore non multipart request', async () => {
3537
const res = await app.httpRequest()
@@ -62,32 +64,32 @@ describe('test/file-mode.test.js', () => {
6264
const res = await urllib.request(host + '/upload', {
6365
method: 'POST',
6466
headers,
65-
stream: form,
67+
stream: form as any,
6668
});
6769

6870
assert(res.status === 200);
6971
const data = JSON.parse(res.data);
7072
assert.deepStrictEqual(data.body, { foo: 'fengmk2', love: 'egg', work: 'with Node.js' });
7173
assert(data.files.length === 3);
7274
assert(data.files[0].field === 'file1');
73-
assert(data.files[0].filename === 'foooooooo.js');
75+
assert.equal(data.files[0].filename, 'foooooooo.js');
7476
assert(data.files[0].encoding === '7bit');
7577
assert(data.files[0].mime === 'application/javascript');
7678
assert(data.files[0].filepath.startsWith(app.config.multipart.tmpdir));
7779

7880
assert(data.files[1].field === 'file2');
7981
assert(data.files[1].fieldname === 'file2');
80-
assert(data.files[1].filename === 'file-mode.test.js');
82+
assert.equal(data.files[1].filename, 'file-mode.test.ts');
8183
assert(data.files[1].encoding === '7bit');
8284
assert(data.files[1].transferEncoding === '7bit');
83-
assert(data.files[1].mime === 'application/javascript');
84-
assert(data.files[1].mimeType === 'application/javascript');
85+
assert.equal(data.files[1].mime, 'video/mp2t');
86+
assert.equal(data.files[1].mimeType, 'video/mp2t');
8587
assert(data.files[1].filepath.startsWith(app.config.multipart.tmpdir));
8688

8789
assert(data.files[2].field === 'bigfile');
88-
assert(data.files[2].filename === 'bigfile.js');
90+
assert.equal(data.files[2].filename, 'bigfile.js');
8991
assert(data.files[2].encoding === '7bit');
90-
assert(data.files[2].mime === 'application/javascript');
92+
assert.equal(data.files[2].mime, 'application/javascript');
9193
assert(data.files[2].filepath.startsWith(app.config.multipart.tmpdir));
9294
});
9395

@@ -98,18 +100,18 @@ describe('test/file-mode.test.js', () => {
98100
const res = await urllib.request(host + '/upload', {
99101
method: 'POST',
100102
headers,
101-
stream: form,
103+
stream: form as any,
102104
});
103105
assert(res.status === 200);
104106
const data = JSON.parse(res.data);
105-
assert(data.files.length === 1);
106-
assert(data.files[0].field === 'file');
107-
assert(data.files[0].filename === '10mb.js');
108-
assert(data.files[0].encoding === '7bit');
109-
assert(data.files[0].mime === 'application/octet-stream');
107+
assert.equal(data.files.length, 1);
108+
assert.equal(data.files[0].field, 'file');
109+
assert.equal(data.files[0].filename, '10mb.js');
110+
assert.equal(data.files[0].encoding, '7bit');
111+
assert.equal(data.files[0].mime, 'application/octet-stream');
110112
assert(data.files[0].filepath.startsWith(app.config.multipart.tmpdir));
111113
const stat = await fs.stat(data.files[0].filepath);
112-
assert(stat.size === 10 * 1024 * 1024 - 1);
114+
assert.equal(stat.size, 10 * 1024 * 1024 - 1);
113115
});
114116

115117
it('should 200 when field size just 100kb', async () => {
@@ -120,7 +122,7 @@ describe('test/file-mode.test.js', () => {
120122
const res = await urllib.request(host + '/upload', {
121123
method: 'POST',
122124
headers,
123-
stream: form,
125+
stream: form as any,
124126
});
125127

126128
assert(res.status === 200);
@@ -138,7 +140,7 @@ describe('test/file-mode.test.js', () => {
138140
const res = await urllib.request(host + '/upload', {
139141
method: 'POST',
140142
headers,
141-
stream: form,
143+
stream: form as any,
142144
});
143145

144146
assert(res.status === 200);
@@ -156,7 +158,7 @@ describe('test/file-mode.test.js', () => {
156158
const res = await urllib.request(host + '/upload', {
157159
method: 'POST',
158160
headers,
159-
stream: form,
161+
stream: form as any,
160162
});
161163

162164
assert(res.status === 200);
@@ -183,7 +185,7 @@ describe('test/file-mode.test.js', () => {
183185
const res = await urllib.request(host + '/upload', {
184186
method: 'POST',
185187
headers,
186-
stream: form,
188+
stream: form as any,
187189
});
188190

189191
assert(res.status === 413);
@@ -201,7 +203,7 @@ describe('test/file-mode.test.js', () => {
201203
const res = await urllib.request(host + '/upload', {
202204
method: 'POST',
203205
headers,
204-
stream: form,
206+
stream: form as any,
205207
});
206208

207209
assert(res.status === 413);
@@ -216,7 +218,7 @@ describe('test/file-mode.test.js', () => {
216218
const res = await urllib.request(host + '/upload', {
217219
method: 'POST',
218220
headers,
219-
stream: form,
221+
stream: form as any,
220222
});
221223

222224
assert(res.status === 413);
@@ -233,7 +235,7 @@ describe('test/file-mode.test.js', () => {
233235
const res = await urllib.request(host + '/upload', {
234236
method: 'POST',
235237
headers,
236-
stream: form,
238+
stream: form as any,
237239
});
238240

239241
assert(res.status === 413);
@@ -252,7 +254,7 @@ describe('test/file-mode.test.js', () => {
252254
const res = await urllib.request(host + '/upload', {
253255
method: 'POST',
254256
headers,
255-
stream: form,
257+
stream: form as any,
256258
});
257259

258260
assert(res.status === 413);
@@ -271,7 +273,7 @@ describe('test/file-mode.test.js', () => {
271273
const res = await urllib.request(host + '/upload', {
272274
method: 'POST',
273275
headers,
274-
stream: form,
276+
stream: form as any,
275277
});
276278

277279
assert(res.status === 400);
@@ -289,10 +291,10 @@ describe('test/file-mode.test.js', () => {
289291
const res = await urllib.request(host + '/upload?call_multipart_twice=1', {
290292
method: 'POST',
291293
headers,
292-
stream: form,
294+
stream: form as any,
293295
});
294296

295-
assert(res.status === 500);
297+
assert.equal(res.status, 500);
296298
assert(res.data.toString().includes('the multipart request can\'t be consumed twice'));
297299
});
298300

@@ -307,7 +309,7 @@ describe('test/file-mode.test.js', () => {
307309
const res = await urllib.request(host + '/upload?cleanup=true', {
308310
method: 'POST',
309311
headers,
310-
stream: form,
312+
stream: form as any,
311313
});
312314

313315
assert(res.status === 200);
@@ -326,7 +328,7 @@ describe('test/file-mode.test.js', () => {
326328
const res = await urllib.request(host + '/upload?async_cleanup=true', {
327329
method: 'POST',
328330
headers,
329-
stream: form,
331+
stream: form as any,
330332
});
331333

332334
assert(res.status === 200);
@@ -339,15 +341,15 @@ describe('test/file-mode.test.js', () => {
339341
// [egg-schedule]: register schedule /hello/egg-multipart/app/schedule/clean_tmpdir.js
340342
const logger = app.loggers.scheduleLogger;
341343
const content = await fs.readFile(logger.options.file, 'utf8');
342-
assert(/\[egg-schedule\]: register schedule .+clean_tmpdir\.js/.test(content));
344+
assert.match(content, /\[@eggjs\/schedule\]: register schedule .+clean_tmpdir\.ts/);
343345
});
344346

345347
it('should remove nothing', async () => {
346348
app.mockLog();
347-
await app.runSchedule(path.join(__dirname, '../app/schedule/clean_tmpdir'));
348-
await sleep(1000);
349-
app.expectLog('[egg-multipart:CleanTmpdir] start clean tmpdir: "', 'coreLogger');
350-
app.expectLog('[egg-multipart:CleanTmpdir] end', 'coreLogger');
349+
await app.runSchedule(path.join(__dirname, '../src/app/schedule/clean_tmpdir'));
350+
await scheduler.wait(1000);
351+
app.expectLog('[@eggjs/multipart:CleanTmpdir] start clean tmpdir: "', 'coreLogger');
352+
app.expectLog('[@eggjs/multipart:CleanTmpdir] end', 'coreLogger');
351353
});
352354

353355
it('should remove old dirs', async () => {
@@ -366,7 +368,7 @@ describe('test/file-mode.test.js', () => {
366368
const currentMonth = new Date().getMonth();
367369
const fourMonthBefore = path.join(app.config.multipart.tmpdir, dayjs().subtract(4, 'months').format('YYYY/MM/DD/HH'));
368370
if (currentMonth < 4) {
369-
// if current month is less than April, four months before shoule be last year.
371+
// if current month is less than April, four months before should be last year.
370372
oldDirs.push(fourMonthBefore);
371373
} else {
372374
shouldKeepDirs.push(fourMonthBefore);
@@ -380,7 +382,7 @@ describe('test/file-mode.test.js', () => {
380382
}));
381383

382384
app.mockLog();
383-
await app.runSchedule(path.join(__dirname, '../app/schedule/clean_tmpdir'));
385+
await app.runSchedule(path.join(__dirname, '../src/app/schedule/clean_tmpdir'));
384386
for (const dir of oldDirs) {
385387
const exists = await fs.access(dir).then(() => true).catch(() => false);
386388
assert(!exists, dir);
@@ -389,8 +391,8 @@ describe('test/file-mode.test.js', () => {
389391
const exists = await fs.access(dir).then(() => true).catch(() => false);
390392
assert(exists, dir);
391393
}
392-
app.expectLog('[egg-multipart:CleanTmpdir] removing tmpdir: "', 'coreLogger');
393-
app.expectLog('[egg-multipart:CleanTmpdir:success] tmpdir: "', 'coreLogger');
394+
app.expectLog('[@eggjs/multipart:CleanTmpdir] removing tmpdir: "', 'coreLogger');
395+
app.expectLog('[@eggjs/multipart:CleanTmpdir:success] tmpdir: "', 'coreLogger');
394396
});
395397
});
396398

@@ -405,7 +407,7 @@ describe('test/file-mode.test.js', () => {
405407
const res = await urllib.request(host + '/upload', {
406408
method: 'POST',
407409
headers,
408-
stream: form,
410+
stream: form as any,
409411
dataType: 'json',
410412
});
411413
assert.deepStrictEqual(res.data.body, { foo: 'egg' });
@@ -423,7 +425,7 @@ describe('test/file-mode.test.js', () => {
423425
const res = await urllib.request(host + '/upload', {
424426
method: 'POST',
425427
headers,
426-
stream: form,
428+
stream: form as any,
427429
dataType: 'json',
428430
});
429431
assert.deepStrictEqual(res.data.body, { foo: [ 'fengmk2', 'like', 'egg' ] });

0 commit comments

Comments
 (0)