Comparing version 0.3.2 to 0.4.0-alpha.1
@@ -1,43 +0,45 @@ | ||
const fs = require("fs"); | ||
const ice = require("./lib.js"); | ||
const lib = require("./lib.js"); | ||
const router = lib.router; | ||
const app = new ice.Application({ | ||
disable_request_logging: true | ||
}); | ||
let server = new lib.HttpServer( | ||
new lib.HttpServerConfig().setNumExecutors(4).setListenAddr("127.0.0.1:6851") | ||
); | ||
let template = ` | ||
<p>Template OK: {{ param }}</p> | ||
`.trim(); | ||
let rt = new router.Router(); | ||
app.loadCervusModule("test", fs.readFileSync("test_module.bc")); | ||
rt.use("/info/", (req) => { | ||
req.mwHit = true; | ||
}); | ||
app.addTemplate("test.html", template); | ||
rt.route("GET", "/info/uri", (req) => { | ||
if(!req.mwHit) { | ||
throw new Error("Middleware not called"); | ||
} | ||
app.route("GET", "/hello_world", (req, resp) => { | ||
resp.body("Hello world!"); | ||
return req.uri; | ||
}); | ||
app.route("GET", "/echo_param/:p", (req, resp) => { | ||
resp.body(req.params.p); | ||
}); | ||
app.route("GET", "/request_id", (req, resp) => resp.body(req.custom.request_id)); | ||
app.route("GET", "/hello_world_detached", (req, resp) => { | ||
resp.detach(); | ||
setImmediate(() => resp.body("Hello world! (Detached)").send()); | ||
}); | ||
rt.route("POST", "/echo", (req) => { | ||
let result = []; | ||
app.route("GET", "/hello_world_stream", (req, resp) => { | ||
resp.stream(stream => { | ||
stream.write(Buffer.from("Hello world! (stream)")); | ||
stream.close(); | ||
req.intoBody((data) => { | ||
result.push(data); | ||
return true; | ||
}, (ok) => { | ||
req.createResponse().setBody(Buffer.concat(result)).send(); | ||
}); | ||
return new router.Detached(); | ||
}); | ||
rt.route("GET", "/hello_world", (req) => { | ||
return req.createResponse().setBody("Hello world!\n"); | ||
}); | ||
rt.route("GET", "/some_file", (req) => { | ||
return req.createResponse().sendFile("lib_test.js"); | ||
}); | ||
rt.route("GET", "/delay", (req) => new Promise(cb => setTimeout(() => cb( | ||
req.createResponse().setBody("OK") | ||
), 1000))); | ||
rt.build(server); | ||
app.route("GET", "/leak_request", (req, resp) => resp.detach()); | ||
app.route("GET", "/render_template", (req, resp) => resp.renderTemplate("test.html", { | ||
param: new Date().toLocaleString() | ||
})); | ||
app.prepare(); | ||
app.listen("127.0.0.1:1479"); | ||
server.start(); |
202
lib.js
@@ -1,6 +0,198 @@ | ||
const base = require("./base.js"); | ||
const express = require("./express_api.js"); | ||
const core = require("./build/Release/ice_node_v4_core"); | ||
const assert = require("assert"); | ||
const router = require("./router.js"); | ||
Object.assign(module.exports, base); | ||
module.exports.express = express.createApplication; | ||
Object.assign(module.exports.express, express); | ||
module.exports.router = router; | ||
class HttpServer { | ||
constructor(cfg) { | ||
assert((cfg instanceof HttpServerConfig) && cfg.inst); | ||
this.inst = core.http_server_create(cfg.inst); | ||
cfg.inst = null; | ||
this.started = false; | ||
} | ||
start() { | ||
assert(!this.started); | ||
core.http_server_start(this.inst); | ||
this.started = true; | ||
} | ||
route(path, target) { | ||
let rt = core.http_server_route_create(path, function (ctx, rawReq) { | ||
let req = new HttpRequest(ctx, rawReq); | ||
target(req); | ||
}); | ||
core.http_server_add_route(this.inst, rt); | ||
} | ||
routeDefault(target) { | ||
let rt = core.http_server_route_create("", function (ctx, rawReq) { | ||
let req = new HttpRequest(ctx, rawReq); | ||
target(req); | ||
}); | ||
core.http_server_set_default_route(this.inst, rt); | ||
} | ||
} | ||
class HttpServerConfig { | ||
constructor() { | ||
this.inst = core.http_server_config_create(); | ||
} | ||
destroy() { | ||
assert(this.inst); | ||
core.http_server_config_destroy(this.inst); | ||
this.inst = null; | ||
} | ||
setNumExecutors(n) { | ||
assert(this.inst); | ||
assert(typeof(n) == "number" && n > 0); | ||
core.http_server_config_set_num_executors(this.inst, n); | ||
return this; | ||
} | ||
setListenAddr(addr) { | ||
assert(this.inst); | ||
assert(typeof(addr) == "string"); | ||
core.http_server_config_set_listen_addr(this.inst, addr); | ||
return this; | ||
} | ||
} | ||
class HttpRequest { | ||
constructor(ctx, req) { | ||
this.ctx = ctx; | ||
this.inst = req; | ||
this._cache = { | ||
uri: null, | ||
method: null, | ||
remoteAddr: null | ||
}; | ||
} | ||
createResponse() { | ||
return new HttpResponse(this.ctx, this); | ||
} | ||
intoBody(onData, onEnd) { | ||
assert(this.inst); | ||
let ownedInst = core.http_server_endpoint_context_take_request(this.ctx); | ||
this.inst = null; | ||
core.http_request_take_and_read_body(ownedInst, onData, onEnd); | ||
} | ||
getMethod() { | ||
assert(this.inst); | ||
return core.http_request_get_method(this.inst); | ||
} | ||
getUri() { | ||
assert(this.inst); | ||
return core.http_request_get_uri(this.inst); | ||
} | ||
getRemoteAddr() { | ||
assert(this.inst); | ||
return core.http_request_get_remote_addr(this.inst); | ||
} | ||
getHeader(k) { | ||
assert(this.inst); | ||
assert(typeof(k) == "string"); | ||
return core.http_request_get_header(this.inst, k); | ||
} | ||
get uri() { | ||
return (this._cache.uri || (this._cache.uri = this.getUri())); | ||
} | ||
get method() { | ||
return (this._cache.method || (this._cache.method = this.getMethod())); | ||
} | ||
get remoteAddr() { | ||
return (this._cache.remoteAddr || (this._cache.remoteAddr = this.getRemoteAddr())); | ||
} | ||
} | ||
class HttpResponse { | ||
constructor(ctx, req) { | ||
this.ctx = ctx; | ||
this.req = req; | ||
this.inst = core.http_response_create(); | ||
core.http_response_set_header(this.inst, "X-Powered-By", "Ice-node"); | ||
} | ||
destroy() { | ||
assert(this.inst); | ||
core.http_response_destroy(this.inst); | ||
this.inst = null; | ||
} | ||
send() { | ||
assert(this.inst); | ||
core.http_server_endpoint_context_end_with_response(this.ctx, this.inst); | ||
this.inst = null; | ||
} | ||
setBody(data) { | ||
assert(this.inst); | ||
if(!(data instanceof Buffer)) { | ||
data = Buffer.from(data); | ||
} | ||
assert(data); | ||
core.http_response_set_body(this.inst, data); | ||
return this; | ||
} | ||
setStatus(status) { | ||
assert(this.inst); | ||
assert(typeof(status) == "number"); | ||
core.http_response_set_status(this.inst, status); | ||
return this; | ||
} | ||
setHeader(k, v) { | ||
assert(this.inst); | ||
assert(typeof(k) == "string" && typeof(v) == "string"); | ||
core.http_response_set_header(this.inst, k, v); | ||
return this; | ||
} | ||
appendHeader(k, v) { | ||
assert(this.inst); | ||
assert(typeof(k) == "string" && typeof(v) == "string"); | ||
core.http_response_append_header(this.inst, k, v); | ||
return this; | ||
} | ||
sendFile(path) { | ||
assert(this.inst && this.req.inst); | ||
assert(typeof(path) == "string"); | ||
let ret = core.storage_file_http_response_begin_send(this.req.inst, this.inst, path); | ||
if(!ret) { | ||
throw new Error("Unable to send file: " + path); | ||
} | ||
return this; | ||
} | ||
} | ||
module.exports.HttpServer = HttpServer; | ||
module.exports.HttpServerConfig = HttpServerConfig; | ||
module.exports.HttpRequest = HttpRequest; | ||
module.exports.HttpResponse = HttpResponse; |
{ | ||
"name": "ice-node", | ||
"version": "0.3.2", | ||
"description": "Bindings for the Ice Web Framework", | ||
"version": "0.4.0-alpha.1", | ||
"description": "Node bindings for Ice Core v0.4", | ||
"main": "lib.js", | ||
"scripts": { | ||
"test": "node ./auto_tests/run.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"install": "node-gyp rebuild" | ||
@@ -12,26 +12,11 @@ }, | ||
"type": "git", | ||
"url": "git+https://github.com/losfair/ice-node.git" | ||
"url": "git+https://github.com/losfair/ice-node-v4.git" | ||
}, | ||
"keywords": [ | ||
"web", | ||
"framework", | ||
"performance", | ||
"http", | ||
"network" | ||
], | ||
"author": "Heyang Zhou", | ||
"license": "LGPL-3.0", | ||
"license": "MIT", | ||
"gypfile": true, | ||
"bugs": { | ||
"url": "https://github.com/losfair/ice-node/issues" | ||
"url": "https://github.com/losfair/ice-node-v4/issues" | ||
}, | ||
"homepage": "https://github.com/losfair/ice-node#readme", | ||
"dependencies": { | ||
"mime": "^1.3.6" | ||
}, | ||
"devDependencies": { | ||
"randomstring": "^1.1.5", | ||
"request": "^2.81.0", | ||
"request-promise": "^4.2.1" | ||
} | ||
"homepage": "https://github.com/losfair/ice-node-v4#readme" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
0
0
0
100
0
41792
18
337
2
3
2
0
2
- Removedmime@^1.3.6
- Removedmime@1.6.0(transitive)