-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simple node http server for webpack dev (#1729)
- Loading branch information
1 parent
771273f
commit f61f801
Showing
6 changed files
with
565 additions
and
454 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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import { createReadStream } from "fs"; | ||
import { createServer, IncomingMessage, RequestListener, ServerResponse } from "http"; | ||
import * as Url from "url"; | ||
import { resolve } from "path"; | ||
const Middleware = require("./middleware"); | ||
|
||
export interface DevHttpServerOptions { | ||
port: number; | ||
host: string; | ||
protocol?: string; | ||
} | ||
|
||
export type HttpRequestEvent = "connect" | "response" | "timeout" | "close" | "finish"; | ||
export type HttpServerEvent = "open" | "close" | "listening" | "error"; | ||
|
||
export interface DevHttpServer { | ||
webpackDevHttpPlugin: RequestListener; | ||
start: () => void; | ||
stop?: () => void; | ||
addRequestListener: (event: HttpRequestEvent, handler: any) => void; | ||
addServerEventListener: (event: HttpServerEvent, hander: any) => void; | ||
} | ||
|
||
export const setupHttpDevServer = function({ | ||
port, | ||
host, | ||
protocol = "http" | ||
}: DevHttpServerOptions): DevHttpServer { | ||
const middleware = new Middleware({ | ||
baseUrl: () => { | ||
return Url.format({ | ||
hostname: host || process.env.HOST || "localhost", | ||
protocol: protocol, // doesn't matter since it's a downstream call anyway.. | ||
port: port || process.env.PORT | ||
}); | ||
} | ||
}); | ||
|
||
middleware.setup(); | ||
|
||
const requestEventHooks = {}; | ||
|
||
const webpackDevHttpPlugin: RequestListener = function( | ||
req: IncomingMessage, | ||
res: ServerResponse | ||
) { | ||
Object.keys(requestEventHooks).map(eventName => { | ||
req.addListener(eventName, event => requestEventHooks[eventName]({ ...event, ...req })); | ||
}); | ||
middleware.process(req, res, { | ||
skip: () => Promise.resolve(), | ||
replyHtml: html => { | ||
res | ||
.writeHead(200, { | ||
"Content-Type": "text/html" | ||
}) | ||
.end(`<!DOCTYPE html>${html}`); | ||
}, | ||
replyError: err => { | ||
res.writeHead(500, err); | ||
}, | ||
replyNotFound: () => res.writeHead(404, "dev server express Not Found"), //res.status(404).send("dev server express Not Found"), | ||
replyStaticData: data => { | ||
const type = require("mime").getType(req.url); | ||
res.writeHead(200, { | ||
"Content-Type": type | ||
}); | ||
res.end(data); | ||
}, | ||
replyFile: file => createReadStream(resolve(file)).pipe(res) | ||
}); | ||
}; | ||
const server = createServer(webpackDevHttpPlugin); | ||
|
||
return { | ||
start: () => server.listen(port, host), | ||
webpackDevHttpPlugin, | ||
addServerEventListener: (event: HttpServerEvent, cb) => { | ||
server.addListener(event.toString(), cb); | ||
}, | ||
stop: () => { | ||
server.close(function() { | ||
/* eslint-disable no-console */ | ||
console.log("Server closed!"); | ||
}); | ||
}, | ||
addRequestListener: (event: HttpRequestEvent, cb: any) => (requestEventHooks[event] = cb) | ||
}; | ||
}; | ||
export const setup = setupHttpDevServer; |
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
50 changes: 50 additions & 0 deletions
50
packages/xarc-app-dev/test/spec/dev-admin/dev-http.spec.ts
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// /* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-empty-function */ | ||
// /* eslint-disable @typescript-eslint/ban-ts-ignore, no-invalid-this, @typescript-eslint/class-name-casing */ | ||
|
||
// const mockRequire = require("mock-require"); | ||
|
||
// const moduleName = "../../../lib/dev-admin/dev-http"; | ||
|
||
// import { before, beforeEach, describe, it, after, afterEach } from "mocha"; | ||
// import { expect } from "chai"; | ||
// import * as Axios from "axios"; | ||
|
||
// describe("dev-http", function() { | ||
// this.timeout(10000); | ||
// let server; | ||
// ] | ||
// before(() => {}); | ||
|
||
// beforeEach(() => { | ||
// const setupServer = require("../../../lib/dev-admin/dev-http").setup; | ||
|
||
// const textCycle = () => {}; | ||
// server = setupServer({ port: 3003, host: "localhost" }); | ||
// server.start(); | ||
// }); | ||
|
||
// afterEach(() => { | ||
// delete require.cache[require.resolve(moduleName)]; | ||
// server.stop(); | ||
// }); | ||
|
||
// after(() => { | ||
// mockRequire.stop("@xarc/app/config/archetype"); | ||
// mockRequire.stop("../../src/lib/dev-admin/middleware"); | ||
// }); | ||
|
||
// describe("setup", () => { | ||
// it.skip("http server: if replyFile is called with a valid file then return 200", () => { | ||
// Axios.default.get("http://localhost:3003/").then(resp => { | ||
// expect(resp.status).to.equal(200); | ||
// }); | ||
// }); | ||
|
||
// // it("if replyFile is called with an invalid file then return 404", () => { | ||
// // textCycle(cycle => { | ||
// // const { result } = cycle.replyFile("./xclap.bs"); | ||
// // expect(result.code).to.equal(404); | ||
// // }); | ||
// // }); | ||
// }); | ||
// }); |
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
Oops, something went wrong.