Skip to content

Commit

Permalink
Merge pull request #634 from marp-team/fix-server-mode-concurrent-req…
Browse files Browse the repository at this point in the history
…uests

Fix returning wrong file extension in concurrent requests on server mode
  • Loading branch information
yhatt authored Jan 19, 2025
2 parents 9a8e20b + 037ee4d commit 03e921e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
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

0 comments on commit 03e921e

Please sign in to comment.