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 🙏

© 2024 – Pkg Stats / Ryan Hefner

kneecap

v1.6.6

Published

KC = kneecap

Downloads

161

Readme

KC = kneecap

Testing

npm test

How it works

A KC instance only allows separate endpoints per http request/response. A single callback can be attached to each endpoint, which must return a promise.

Gotchas

  • You should make sure headers['content-length'] is valid (or removed) when changing the request's (or response's) body, otherwise the data may end up truncated.
  • Getting the preview is a lot faster than getting the full body. Use request.getPreivew() where possible.

Examples

Change request headers

        icapServer.requestHandler('/request', function(request) {
            return request.getRequestHeaders()
                .then(headers => {
                    return {
                        requestHeaders: headers.replace(myHeaderName, `${myHeaderName}-Changed`)
                    };
                });
        });

Change request body

        icapServer.requestHandler('/request', function(request) {
            return Promise.all([request.getRequestHeaders(), request.getRawBody()])
                .then(results => {
                    const [requestHeaders, requestBody] = results;
                    const diff = expectedBodyValue.length - myFormValue.length;
                    const oldContentLength = Number(requestHeaders.match(/content-length: (\d+)/i)[1]);
                    return {
                        requestBody: Buffer.from(requestBody.toString().replace(myFormValue, expectedBodyValue)),
                        requestHeaders: requestHeaders.replace(/content-length: (\d+)/i, `Content-Length: ${oldContentLength + diff}`)
                    };
                });
        });

Don't change anything

        icapServer.requestHandler('/request', function(request) {
            return Promise.all([request.getRequestHeaders(), request.getRawBody()])
                .then(results => {
                    const [requestHeaders, requestBody] = results;
                    console.log('Got request', requestHeaders, requestBody);
                    return;
                });
        });

Set request options

        icapServer.requestHandler('/request', {
            previewBytes: 128,
            transfer: {
                complete: ['html', 'js'],
                ignore: ['jpg', 'ogg', 'mp4', 'gif', 'gifv'],
                preview: ['*'],
            }
        }, function(request) {
            return Promise.resolve(request.getPreview())
                .then(previewBody => {
                    console.log('preview', previewBody.toString());
                });
        });

API

Server instance

listen

Accepts the same params as node.js's net server.listen, except for the final callback.

Returns a promise which resolves when listening.

const kneecap = require('kneecap');

const kc = kneecap();
kc.listen(8008)
    .then(() => {
        console.log('listening');
    });

close

Stops the server from accepting new connections. Resolves when remaining connections have been closed.

kc.close()
    .then(() => {
        console.log('icap server closed');
    });

events

Events are emitted on this emitter.

kc.events.on('error', err => {
    console.log('icap server error', err);
});

requestHandler

Adds a REQMOD handler.

server.requestHandler(path[, options], callback)

The path must be a string where icap REQMOD requests will be listened to.

The options object, when included, will specify the OPTIONS response for the path endpoint. Supported options:

  • transfer object, which must contain at least one child with the all ['*'] setting
    • complete array, will be sent as Transfer-Complete, i.e. ['html', 'js']
    • ignore array, will be sent as Transfer-Ignore, i.e. ['jpg', 'jpeg', 'swf', 'mp4']
    • preview array, will be sent as Transfer-Preview, i.e. ['*']
  • previewBytes integer, how long should the preview be (where available), in bytes.
kc.requestHandler('/request', {
    previewBytes: 128,
    transfer: {
        complete: ['html', 'js'],
        ignore: ['jpg', 'ogg', 'mp4', 'gif', 'gifv'],
        preview: ['*'],
    }
}, handleRequest);

responseHandler

Adds a RESPMOD handler. Same usage as requestHandler.


Request

IcapRequest passed to requestHandler and responseHandler methods.

hasRequestHeaders

Synchronous, returns a boolean whether the icap request has request headers.

getRequestHeaders

Returns a promise which resolves to the request headers as a String, most likely separated by \r\n.

getRawRequestHeaders

Returns a promise which resolves to the request headers as a Buffer.

hasResponseHeaders

Synchronous, returns a boolean whether the icap request has response headers.

getResponseHeaders

Returns a promise which resolves to the response headers as a String, most likely separated by \r\n.

getRawResponseHeaders

Returns a promise which resolves to the response headers as a Buffer.

hasBody

Synchronous, returns a boolean whether the icap request has a request or response body (depends on REQMOD/RESPMOD handler type).

hasPreview

Returns a promise which resolves to a boolean whether the request has a body preview.

getPreview

Returns a promise which resolves to the request/response body preview as a buffer (depends on REQMOD/RESPMOD handler type). The promise may be resolved synchronously, so call Promise.resolve(request.getPreview()) as a best practice.

getRawBody

Returns a promise which resolves to the request/response body as a buffer (depends on REQMOD/RESPMOD handler type).

getRequest

Returns a promise which resolves to an http.ClientRequest.

getIcapDetails

Returns the icapDetails object, which is the ICAP request containing the ICAP method and headers. Useful for reading headers such as x-client-username.


Respond

To respond to an icap Request, the request handler must return a promise which resolves to either:

  • falsey value (undefined, null, false), which will not modify the request, forwarding it as is. If Allow: 204 is sent by the icap client, then 204 No Content will be returned. Otherwise, the request data will be sent back unchanged.
kc.requestHandler('/request', function(request) {
    return null;
});
  • object containing any combination of requestHeaders:String, requestBody:Buffer, responseHeaders:String, responseBody:Buffer. You must only include one body in the response.
kc.requestHandler('/request', function(request) {
    return Promise.all([request.getRequestHeaders(), request.getRawBody()])
        .then(results => {
            const [requestHeaders, requestBody] = results;
            return {
                requestBody: Buffer.from('foo=bar'),
                requestHeaders: requestHeaders
            };
        });
});

License

Apache 2.0

Copyright 2016 Universal Basket

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.