rsnext/packages/next/server/lib/error-overlay-middleware.ts
Joe Haddad e9b2e25c44
Convert Dev Server to TypeScript (#8966)
* Convert Dev Server to TypeScript
This converts the Next.js Development Server to TypeScript in prep for upcoming changes.

* Convert remaining necessary

* Fix some type errors

* Adjust types
2019-10-04 12:11:39 -04:00

26 lines
896 B
TypeScript

import url from 'url'
import launchEditor from 'launch-editor'
import fs from 'fs'
import path from 'path'
import { IncomingMessage, ServerResponse } from 'http'
export default function errorOverlayMiddleware(options: { dir: string }) {
return (req: IncomingMessage, res: ServerResponse, next: Function) => {
if (req.url!.startsWith('/_next/development/open-stack-frame-in-editor')) {
const query = url.parse(req.url!, true).query
const lineNumber = parseInt(query.lineNumber as string, 10) || 1
const colNumber = parseInt(query.colNumber as string, 10) || 1
let resolvedFileName = query.fileName
if (!fs.existsSync(resolvedFileName as string)) {
resolvedFileName = path.join(options.dir, resolvedFileName as string)
}
launchEditor(`${resolvedFileName}:${lineNumber}:${colNumber}`)
res.end()
} else {
next()
}
}
}