Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Sep 20, 2024
1 parent 9237fa5 commit 234c58b
Show file tree
Hide file tree
Showing 3 changed files with 400 additions and 106 deletions.
103 changes: 59 additions & 44 deletions packages/busboy/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,22 @@ describe('test/koa.test.ts', function () {
{
assert(singleFileIterator === fileIterator);
const files = [], fields = [];
for await (const file of fileIterator) {
const path = join(resourceDir, `${file.fieldName}.pdf`);
for await (const { data, fieldName, filename } of fileIterator) {
const path = join(resourceDir, `${fieldName}.pdf`);
const stream = createWriteStream(path);
const end = new Promise(resolve => {
stream.on('close', () => {
resolve(void 0)
});
});

file.data.pipe(stream);
data.pipe(stream);
await end;

files.push(file);
files.push({
fieldName,
filename,
});
}

for await (const field of fieldIterator) {
Expand Down Expand Up @@ -367,12 +370,14 @@ describe('test/koa.test.ts', function () {
it('upload stream mode and multi file and trigger limit error', async () => {
@Controller('/')
class HomeController {
@Post('/upload-multi', { middleware: [ createMiddleware(busboy.UploadMiddleware, {
mode: 'asyncIterator',
limits: {
fileSize: 1
}
}) ] })
@Post("/upload-multi", {
middleware: [createMiddleware(busboy.UploadMiddleware, {
mode: "asyncIterator",
limits: {
fileSize: 1
}
})]
})
async uploadMore(@Files() fileIterator: AsyncGenerator<busboy.UploadStreamFileInfo>) {
const files = [];
for await (const file of fileIterator) {
Expand Down Expand Up @@ -423,12 +428,14 @@ describe('test/koa.test.ts', function () {
it('upload stream mode trigger limit error and catch it', async () => {
@Controller('/')
class HomeController {
@Post('/upload-multi', { middleware: [ createMiddleware(busboy.UploadMiddleware, {
mode: 'asyncIterator',
@Post("/upload-multi", {
middleware: [createMiddleware(busboy.UploadMiddleware, {
mode: "asyncIterator",
limits: {
fileSize: 1
}
}) ] })
})]
})
async uploadMore(@Files() fileIterator: AsyncGenerator<busboy.UploadStreamFileInfo>) {
const files = [];
try {
Expand Down Expand Up @@ -476,54 +483,57 @@ describe('test/koa.test.ts', function () {
});

it.skip("should got 204 when iterator not use", async () => {
@Controller('/')
@Controller("/")
class HomeController {
@Post('/upload-multi', { middleware: [ createMiddleware(busboy.UploadMiddleware, {
mode: 'asyncIterator',
limits: {
}
}) ] })
@Post("/upload-multi", {
middleware: [createMiddleware(busboy.UploadMiddleware, {
mode: "asyncIterator",
limits: {}
})]
})
async uploadMore(@Files() fileIterator: AsyncGenerator<busboy.UploadStreamFileInfo>) {
// 这里如果不处理迭代器,会出现 unhandled error
}
}

const app = await createLightApp({
imports: [
koa,
busboy,
HomeController
],
globalConfig: {
keys: '123',
keys: "123",
busboy: {}
},
}
});

const txtPath = join(__dirname, 'fixtures/1.test');
const txtPath = join(__dirname, "fixtures/1.test");
const request = createHttpRequest(app);
const response = await request.post('/upload-multi')
.attach('file', txtPath);
const response = await request.post("/upload-multi")
.attach("file", txtPath);

expect(response.status).toBe(204);
await close(app);
});

it("should check type", async () => {
@Controller('/')
@Controller("/")
class HomeController {
@Post('/upload-multi', { middleware: [ createMiddleware(busboy.UploadMiddleware, {
mode: 'asyncIterator',
limits: {
}
}) ] })
@Post("/upload-multi", {
middleware: [createMiddleware(busboy.UploadMiddleware, {
mode: "asyncIterator",
limits: {}
})]
})
async uploadMore(@Files() fileIterator: AsyncGenerator<busboy.UploadStreamFileInfo>) {
const files = [];
for await (const file of fileIterator) {
const path = join(resourceDir, `${file.fieldName}.pdf`);
const stream = createWriteStream(path);
const end = new Promise(resolve => {
stream.on('close', () => {
resolve(void 0)
stream.on("close", () => {
resolve(void 0);
});
});

Expand All @@ -533,16 +543,17 @@ describe('test/koa.test.ts', function () {
}
}
}

const app = await createLightApp({
imports: [
koa,
busboy,
HomeController
],
globalConfig: {
keys: '123',
keys: "123",
busboy: {}
},
}
});

const txtPath = join(__dirname, 'fixtures/1.test');
Expand All @@ -555,26 +566,30 @@ describe('test/koa.test.ts', function () {
});

it("should check size of result", async () => {
@Controller('/')
@Controller("/")
class HomeController {
@Post('/upload', { middleware: [ createMiddleware(busboy.UploadMiddleware, {
mode: 'file',
}) ] })
@Post("/upload", {
middleware: [createMiddleware(busboy.UploadMiddleware, {
mode: "file"
})]
})
async uploadFile(@File() file) {
return statSync(file.data).size
return statSync(file.data).size;
}

@Post('/upload-multi', { middleware: [ createMiddleware(busboy.UploadMiddleware, {
mode: 'asyncIterator',
}) ] })
@Post("/upload-multi", {
middleware: [createMiddleware(busboy.UploadMiddleware, {
mode: "asyncIterator"
})]
})
async uploadMore(@Files() fileIterator: AsyncGenerator<busboy.UploadStreamFileInfo>) {
const files = [];
for await (const file of fileIterator) {
const path = join(resourceDir, `${file.fieldName}.pdf`);
const stream = createWriteStream(path);
const end = new Promise(resolve => {
stream.on('close', () => {
resolve(void 0)
stream.on("close", () => {
resolve(void 0);
});
});

Expand Down
Loading

0 comments on commit 234c58b

Please sign in to comment.