-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
2 changed files
with
75 additions
and
63 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
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 |
---|---|---|
@@ -1,52 +1,67 @@ | ||
import path from 'path'; | ||
import { Server } from 'http'; | ||
|
||
// Create bridge and start listening | ||
const { Server } = require('http') as typeof import('http'); | ||
const { Bridge } = | ||
require('./vercel__bridge.js') as typeof import('@vercel/node-bridge/bridge'); | ||
let listener: any; | ||
|
||
try { | ||
process.chdir(__dirname); | ||
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'production'; | ||
if (process.env.DEV) { | ||
console.log('err dev mode,auto change to prod'); | ||
throw new Error('process.env.DEV is true'); | ||
} | ||
async function createLauncher(event: any, context: any) { | ||
//@ts-ignore | ||
const { Bridge } = (await import('./vercel__bridge.js')) | ||
.default as typeof import('@vercel/node-bridge/bridge'); | ||
|
||
listener = require(path.join(__dirname, 'index.js')); | ||
if (listener.default) listener = listener.default; | ||
if (typeof listener !== 'function' && listener.handler) | ||
listener = listener.handler; | ||
if (typeof listener?.then === 'function') { | ||
listener.then((res: any) => { | ||
listener = res; | ||
}); | ||
} else { | ||
const listenerType = typeof listener; | ||
const oldListener = listener; | ||
console.log('listener:', typeof listener, listener); | ||
if (typeof listener !== 'function') { | ||
listener = (req: any, res: any) => { | ||
res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
res.write(`This is vercel-quasar, your Vercel builder. Turns out we couldn't find your server instance. Did you write \`module.exports = app\`? | ||
typeof: ${listenerType} (expected 'function') | ||
String: ${String(oldListener)} | ||
Read the docs or create an issue: https://github.com/dongwa/vercel-quasar`); | ||
res.end(); | ||
}; | ||
try { | ||
process.chdir(__dirname); | ||
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'production'; | ||
if (process.env.DEV) { | ||
console.log('err dev mode,auto change to prod'); | ||
throw new Error('process.env.DEV is true'); | ||
} | ||
} | ||
} catch (error) { | ||
console.error('Server is not listening', error); | ||
process.exit(1); | ||
} | ||
|
||
const server = new Server(listener); | ||
const quasarServerModule = (await import(path.join(__dirname, 'index.js'))) | ||
.default; | ||
|
||
listener = quasarServerModule; | ||
if (listener.default) listener = listener.default; | ||
if (typeof listener?.then === 'function') listener = await listener; | ||
if (typeof listener !== 'function' && listener.handler) | ||
listener = listener.handler; | ||
else { | ||
const listenerType = typeof listener; | ||
const oldListener = listener; | ||
if (typeof listener !== 'function') { | ||
listener = (req: any, res: any) => { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.write(`This is vercel-quasar, your Vercel builder. Turns out we couldn't find your server instance. Did you write \`module.exports = app\`? | ||
typeof: ${listenerType} (expected 'function') | ||
String: ${String(oldListener)} | ||
Read the docs or create an issue: https://github.com/dongwa/vercel-quasar`); | ||
res.end(); | ||
}; | ||
} | ||
} | ||
|
||
const bridge = new Bridge(server); | ||
bridge.listen(); | ||
const server = new Server(listener); | ||
|
||
const bridge = new Bridge(server); | ||
bridge.listen(); | ||
return bridge.launcher(event, context); | ||
} catch (error) { | ||
console.error('Server is not listening', error); | ||
listener = (req: any, res: any) => { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.write(`This is vercel-quasar, your Vercel builder. | ||
${error} | ||
Read the docs or create an issue: https://github.com/dongwa/vercel-quasar`); | ||
res.end(); | ||
}; | ||
const server = new Server(listener); | ||
|
||
const bridge = new Bridge(server); | ||
bridge.listen(); | ||
return bridge.launcher(event, context); | ||
} | ||
} | ||
|
||
export const launcher: typeof bridge.launcher = bridge.launcher; | ||
export const launcher = createLauncher; |