Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ice-node

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ice-node - npm Package Compare versions

Comparing version 0.3.2 to 0.4.0-alpha.1

core_test.js

66

lib_test.js

@@ -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();

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc