-
Notifications
You must be signed in to change notification settings - Fork 301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
simple node http server for webpack dev #1729
Merged
jchip
merged 12 commits into
electrode-io:master
from
yishengjiang99:839-vanilla-dev-admin
Sep 1, 2020
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8e95008
https://jira.walmart.com/browse/CEECORE-1839
yishengjiangcollab c376da4
move to its own file and left original files alow
yishengjiangcollab e86010f
save prog
yishengjiangcollab 79b044f
m
yishengjiangcollab 1fb7a20
unit test and bug fix
yishengjiangcollab 2176861
add missed files
yishengjiangcollab 32343c0
fix eslint errors
yishengjiangcollab 1daabcc
missed one
yishengjiangcollab 046118d
use import on http
yishengjiangcollab 6e03423
devhttp test skip fix coming
yishengjiangcollab b8cbebf
revert bck fyn.
yishengjiangcollab 8183c59
turn off not ready test
yishengjiangcollab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is res.end() needed? |
||
}, | ||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is asynchronous, it's a good idea to make this return a promise? |
||
/* 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think middleware is typed, so you can import it normally