Skip to content

Commit 3d7088c

Browse files
authored
refactor export middleware typing (#1366)
1 parent a8f4452 commit 3d7088c

1 file changed

Lines changed: 35 additions & 43 deletions

File tree

src/RevealServer.ts

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { RevealContext } from './RevealContext'
1212

1313
/** Http server to serve reveal presentation */
1414
export class RevealServer extends Disposable {
15-
1615
public readonly app = express()
1716

1817
private server: http.Server | null = null
@@ -23,7 +22,6 @@ export class RevealServer extends Disposable {
2322
this.configure()
2423
}
2524

26-
2725
/**
2826
* If the server is not listening, start the server and log the server's status.
2927
* @returns The uri of the server.
@@ -58,7 +56,7 @@ export class RevealServer extends Disposable {
5856
/** Current uri for this server, empty is not listening */
5957
public get uri() {
6058
if (!this.isListening || this.server === null) {
61-
return ""
59+
return ''
6260
}
6361

6462
const addr = this.server.address()
@@ -76,8 +74,6 @@ export class RevealServer extends Disposable {
7674
const app = this.app
7775
const context = this.context
7876

79-
80-
8177
//disable cache
8278
app.set('etag', false)
8379
app.use((req, res, next) => {
@@ -86,9 +82,9 @@ export class RevealServer extends Disposable {
8682
})
8783

8884
// Set EJS as view
89-
app.set('view engine', 'ejs');
85+
app.set('view engine', 'ejs')
9086
app.engine('ejs', ejs.__express)
91-
app.set('views', path.resolve(context.extensionPath, 'views'));
87+
app.set('views', path.resolve(context.extensionPath, 'views'))
9288
app.use(cors())
9389
// LOG REQUEST
9490
app.use(morgan(':method :url :status :res[content-length] - :response-time ms', { stream: { write: (str) => context.logger.info(str) } }))
@@ -98,25 +94,23 @@ export class RevealServer extends Disposable {
9894

9995
// STATIC LIBS
10096
const libsPAth = path.join(context.extensionPath, 'libs')
101-
app.use('/libs', express.static(libsPAth, { cacheControl: false, etag: false, immutable: false }));
97+
app.use('/libs', express.static(libsPAth, { cacheControl: false, etag: false, immutable: false }))
10298

10399
// STATIC RELATIVE TO MD FILE if file is saved
104100
if (context.dirname) {
105-
app.use('/', express.static(context.dirname, { cacheControl: false, etag: false, immutable: false }));
101+
app.use('/', express.static(context.dirname, { cacheControl: false, etag: false, immutable: false }))
106102
}
107103

108104
// MAIN FILE
109105
app.use((req, res, next) => {
110106
if (req.path !== '/') {
111107
next()
112-
}
113-
else {
114-
115-
let init: string | null = null;
108+
} else {
109+
let init: string | null = null
116110
if (context.dirname) {
117111
const initPath = path.join(context.dirname, 'init.js')
118112
if (fs.existsSync(initPath)) {
119-
init = fs.readFileSync(initPath, "utf8");
113+
init = fs.readFileSync(initPath, 'utf8')
120114
}
121115
}
122116

@@ -126,74 +120,72 @@ export class RevealServer extends Disposable {
126120
children: s.verticalChildren.map((c) => ({ ...c, html: markdownit.render(c.text) })),
127121
}))
128122
res.render('index', { slides: htmlSlides, ...context.configuration, rootUrl: this.uri, init })
129-
130123
}
131124
})
132125

133126
// ERROR HANDLER
134127
app.use(function (err, req, res, next) {
135128
context.logger.error(err.stack)
136-
res.status(500).send(err.stack);
137-
});
129+
res.status(500).send(err.stack)
130+
})
138131
}
139132

140133
/* A middleware function that is used to export the presentation to a folder. */
141-
private readonly exportMiddleware = (exportfn: (ExportOptions) => Promise<void>, isInExport) => {
142-
134+
private readonly exportMiddleware = (exportfn: (opts: IExportOptions) => Promise<void>, isInExport: () => boolean) => {
143135
const { exportPath } = this.context
144136

145137
return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
146-
147138
if (isInExport()) {
148-
149-
console.log("in export");
139+
this.context.logger.debug('in export')
150140
const oldWrite = res.write
151-
const oldEnd = res.end;
141+
const oldEnd = res.end
152142

153-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
154-
const chunks: any[] = [];
143+
const chunks: Buffer[] | string[] = []
155144

156145
res.write = (chunk, ...args) => {
157-
chunks.push(chunk);
146+
if (typeof chunk === 'string') {
147+
;(chunks as string[]).push(chunk)
148+
} else {
149+
;(chunks as Buffer[]).push(chunk)
150+
}
158151
// @ts-ignore
159-
return oldWrite.apply(res, [chunk, ...args]);
160-
};
152+
return oldWrite.apply(res, [chunk, ...args])
153+
}
161154
// @ts-ignore
162155
res.end = async (chunk, ...args) => {
163156
this.context.logger.info(`${req.originalUrl.split('?')[0]} - ${chunks.length}`)
164157
if (chunk) {
165-
chunks.push(chunk);
158+
if (typeof chunk === 'string') {
159+
;(chunks as string[]).push(chunk)
160+
} else {
161+
;(chunks as Buffer[]).push(chunk)
162+
}
166163
}
167164
try {
168-
let body: string | Buffer;
165+
let body: string | Buffer
169166
if (chunks.length > 0 && typeof chunks[0] === 'string') {
170-
body = "".concat(...(chunks as string[]));
171-
}
172-
else {
173-
body = Buffer.concat(chunks);
167+
body = ''.concat(...(chunks as string[]))
168+
} else {
169+
body = Buffer.concat(chunks as Buffer[])
174170
}
175171

176-
177172
const opts: IExportOptions = { folderPath: exportPath, url: req.originalUrl.split('?')[0], srcFilePath: null, data: body }
178-
await exportfn(opts);
179-
}
180-
catch (error) {
173+
await exportfn(opts)
174+
} catch (error) {
181175
this.context.logger.info(`Error : ${error}`)
182176
}
183177

184178
// @ts-ignore
185-
return oldEnd.apply(res, [chunk, ...args]);
186-
};
179+
return oldEnd.apply(res, [chunk, ...args])
180+
}
187181
}
188182
next()
189-
190-
191183
}
192184
}
193185

194186
dispose(): void {
195187
this.stop()
196188
this.server = null
197-
super.dispose();
189+
super.dispose()
198190
}
199191
}

0 commit comments

Comments
 (0)