Skip to content

Fix returning wrong file extension in concurrent requests on server mode #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Improve LibreOffice handling in experimental `--pptx-editable` option ([#632](https://github.com/marp-team/marp-cli/pull/632))
- Detect LibreOffice automatically that was installed in Windows by Scoop ([#631](https://github.com/marp-team/marp-cli/issues/631), [#632](https://github.com/marp-team/marp-cli/pull/632))
- Fix returning wrong file extension in concurrent requests on server mode ([#633](https://github.com/marp-team/marp-cli/issues/633), [#634](https://github.com/marp-team/marp-cli/pull/634))

## v4.1.0 - 2025-01-15

Expand Down
24 changes: 12 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ export class Server extends (EventEmitter as new () => TypedEmitter<Server.Event
filename: string,
query: querystring.ParsedUrlQuery = {}
) {
this.converter.options.output = false
this.converter.options.pages = false
this.converter.options.type = ((): ConvertType => {
const type = ((): ConvertType => {
const queryKeys = Object.keys(query)

if (queryKeys.includes('pdf')) return ConvertType.pdf
Expand All @@ -104,10 +102,14 @@ export class Server extends (EventEmitter as new () => TypedEmitter<Server.Event
return ConvertType.html
})()

const ret = await this.converter.convertFile(new File(filename))
this.emit('converted', ret)
this.converter.options.output = false
this.converter.options.pages = false
this.converter.options.type = type

const result = await this.converter.convertFile(new File(filename))
this.emit('converted', result)

return ret
return { result, type }
}

private async loadScript() {
Expand All @@ -127,20 +129,18 @@ export class Server extends (EventEmitter as new () => TypedEmitter<Server.Event
if (!pathname) return

const qs = querystring.parse(query || '')
const response = async (fn) => {
const response = async (fn: string) => {
try {
const ret = await this.convertMarkdown(fn, qs)
const { result, type } = await this.convertMarkdown(fn, qs)

if (!ret.newFile)
if (!result.newFile)
throw new Error('Converter must return a converted file to serve.')

const { type } = this.converter.options

// Download pptx document as an attachment
if (type === ConvertType.pptx)
res.attachment(`${path.basename(fn, path.extname(fn))}.pptx`)

res.type(mimeTypes[type]).end(ret.newFile.buffer)
res.type(mimeTypes[type]).end(result.newFile.buffer)
} catch (e: unknown) {
let errorString = 'Internal server error'

Expand Down