npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ectsm-node

v1.1.11

Published

js version implementation of ECTSM

Downloads

41

Readme

ECTSM-node

js version implementation of ECTSM

use in html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="https://cdn.jsdelivr.net/npm/ectsm-node/dist/httpclient-min.js"></script>
    <script>
        async function HttpRequest() {
            //new ecthttpclient instance as a global single instance
            //publicKeyUrl endpoint to get unix time and public key form server
            await hc.Init("http://127.0.0.1:8080/ectminfo");
            if (hc == null) {
                console.error("new ECTHttpClient error");
            }

            //get
            {
                const url = "http://127.0.0.1:8080/test/get";
                //send request with default timeout and token 'usertoken'
                const result = await hc.ECTGet(url, "usertoken");
                if (result.Err != null) {
                    console.log("err", result.Err);
                } else {
                    console.log("status", result.Rs.status);
                    console.log("get request reponse string", result.ToString());
                    console.log("get request reponse json", result.ToJson());
                }
            }

            //post
            {
                const sendData = {
                    Name: "Jack",
                    Email: "[email protected]",
                    Phone: "123456789",
                    Age: 18,
                };

                const url = "http://127.0.0.1:8080/test/post";
                const result = await hc.ECTPost(url, sendData, "token");
                if (result.Err != null) {
                    console.log("err", result.Err);
                } else {
                    console.log("status", result.Rs.status);
                    console.log("get request reponse string", result.ToString());
                    console.log("get request reponse json", result.ToJson());
                }
            }
        }

        HttpRequest();
    </script>
</body>

</html>

use in nodejs

npm install --save ectsm-node

client example

const { ECTHttpClient } = require("ectsm-node");

async function HttpRequest() {
    //new ecthttpclient instance as a global single instance
    //publicKeyUrl endpoint to get unix time and public key form server
    const hc = new ECTHttpClient();
    await hc.Init("http://127.0.0.1:8080/ectminfo");
    if (hc == null) {
        console.error("new ECTHttpClient error");
    }

    //get
    {
        const url = "http://127.0.0.1:8080/test/get";
        //send request with default timeout and token 'usertoken'
        const result = await hc.ECTGet(url, "usertoken", { timeout: 30000 });
        if (result.Err != null) {
            console.log("err", result.Err);
        } else {
            console.log("status", result.Rs.status);
            console.log("get request reponse string", result.ToString());
            console.log("get request reponse json", result.ToJson());
        }
    }

    //post
    {
        const sendData = {
            Name: "Jack",
            Email: "[email protected]",
            Phone: "123456789",
            Age: 18,
        };

        const url = "http://127.0.0.1:8080/test/post";
        const result = await hc.ECTPost(url, sendData, "token");
        if (result.Err != null) {
            console.log("err", result.Err);
        } else {
            console.log("status", result.Rs.status);
            console.log("get request reponse string", result.ToString());
            console.log("get request reponse json", result.ToJson());
        }
    }
}

HttpRequest();

server example(use koa)

// koa for example
const Koa =require("koa")
const Router =require("koa-router")
const cors =require('koa2-cors')
const os =require("os")

//ECTServer
const {ECTHttpServer,ecthttp} = require("ectsm-node")

const privateKeyBase64Str = "bhbb4EC96zx2uUsWDtSYivzaZUzdeDKMfn+dSV9VwUI=";
const publicKeyBase64Str = "BJJlxQFcPuVTjaB/PvbqmN0py98C2iScUQlvpRUm+kpAgqJmnofCely42Hczgb7cqwTZtFTfPwm2ImdmDtvFMH4=";
let hs = null;

function InitEctHttpServer() {
    console.log("InitEctHttpServer");
    hs = new ECTHttpServer(privateKeyBase64Str);
    if (hs == null) {
        console.log("InitEctHttpServer error");
        os.exit(1);
    }
}

//use as middleware to get body buffer
async function GetRawBody(ctx, next) {
    let data = Buffer.from("");

    ctx.rawBody = await new Promise((resolve, reject) => {
        ctx.req.on("data", (chunk) => {
            //data+=chunk; 
            data = Buffer.concat([data, chunk]);
        });
        ctx.req.on("end", () => {
            if (data.length == 0) {
                console.log("no body");
                resolve(null);
            } else {
                resolve(data);
                console.log("body", data);
            }
        });
    });

    await next();
}

function StartKoaServer() {
    const app = new Koa();
    const router = new Router();

    //cors for html use
    app.use(
        cors({
            exposeHeaders: ["Ectm_key", "ectm_key", "Ectm_time", "ectm_time", "Ectm_token", "ectm_token"],
        })
    );

    app.use(router.routes());

    router.get("/ectminfo", async (ctx) => {
        console.log("GET /ectminfo");

        ctx.body = {
            UnixTime: Math.floor(Date.now() / 1000),
            PublicKey: publicKeyBase64Str,
        };
    });

    router.get("/test/get", async (ctx) => {
        //check header
        const ectReq = await hs.HandleGet(ctx.headers);
        if (ectReq.Err != null) {
            ctx.status = 500;
            ctx.body = Buffer.from("decrypt header error");
            return;
        }

        //do something
        //...
        console.log("symmetricKey:", ectReq.GetSymmetricKey());
        console.log("token:", ectReq.GetToken());

        //responseData example
        const data = {
            Status: 0,
            Msg: "get success",
            Data: null,
        };

        const ECTResponseObj = ECTHttpServer.ECTSendBack(ctx.res, ectReq.SymmetricKey, data);
        if (ECTResponseObj.err != null) {
            ctx.status = 500;
            ctx.body = Buffer.from(ECTResponseObj.err);
            return;
        }

        console.log("response data:", ECTResponseObj.encryptedBodyBuffer);

        ctx.body = ECTResponseObj.encryptedBodyBuffer;
    });

    //user GetRawBody to get body buffer
    //this example in ctx.rawBody
    router.post("/test/post", GetRawBody, async (ctx) => {
        //check header
        const ectReq = await hs.HandlePost(ctx.headers, ctx.rawBody);
        if (ectReq.Err != null) {
            ctx.status = 500;
            ctx.body = Buffer.from("decrypt header error");
            return;
        }

        //do something
        //...
        console.log("symmetricKey:", ectReq.GetSymmetricKey());
        console.log("token:", ectReq.GetToken());
        console.log("decryptedBody string:", ectReq.ToString());
        console.log("decryptedBody json:", ectReq.ToJson());


        //responseData example
        const data = {
            Status: 0,
            Msg: "post success",
            Data: null,
        };

        const ECTResponseObj = ECTHttpServer.ECTSendBack(ctx.res, ectReq.SymmetricKey, data);
        if (ECTResponseObj.err != null) {
            ctx.status = 500;
            ctx.body = Buffer.from(ECTResponseObj.err);
            return;
        }

        console.log("response data:", ECTResponseObj.encryptedBodyBuffer);

        ctx.body = ECTResponseObj.encryptedBodyBuffer;
    });

    app.listen(8080);
    console.log("server start:8080");
}

async function main() {
    //init EctHttpServer instance
    InitEctHttpServer();

    //start http server
    StartKoaServer();
}

main();