-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23a4042
commit ff96362
Showing
8 changed files
with
154 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is a file that is being sent in a multipart request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// @ts-check | ||
import { createServer } from 'node:http' | ||
import formidable from 'formidable' | ||
import { createReadStream } from 'node:fs' | ||
|
||
const form = formidable({}) | ||
|
||
const server = createServer((req, res) => { | ||
form.parse(req, (_, __, files) => { | ||
// @ts-expect-error this is JS | ||
const file = createReadStream(files.file[0].filepath) | ||
|
||
file.pipe(res) | ||
}) | ||
}) | ||
|
||
server.listen(3005) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// @ts-check | ||
|
||
import { createServer } from 'node:http' | ||
import * as bodyParser from '../dist/index.js' | ||
const mw = bodyParser.multipart() | ||
|
||
const server = createServer((req, res) => { | ||
mw(req, res, () => { | ||
/** | ||
* @type {File} | ||
*/ | ||
// @ts-ignore | ||
const file = req.body.file | ||
|
||
const stream = file.stream() | ||
|
||
res.writeHead(200, { | ||
'Content-Type': file.type, | ||
'Content-Disposition': `attachment; filename="${file.name}"` | ||
}) | ||
|
||
// Pipe the stream to the response | ||
stream.pipeTo( | ||
new WritableStream({ | ||
write(chunk) { | ||
res.write(chunk) | ||
}, | ||
close() { | ||
res.end() | ||
}, | ||
abort(err) { | ||
console.error('Stream error:', err) | ||
res.writeHead(500) | ||
res.end('Error streaming file') | ||
} | ||
}) | ||
) | ||
}) | ||
}) | ||
|
||
server.listen(3004) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters