Git Product home page Git Product logo

node-h2-auto-push's Introduction

HTTP/2 automatic server push

Greenkeeper badge

This is not an official Google product.

HTTP/2 is a major revision of the HTTP protocol. One of its differences from HTTP/1 is server push, which allows a server to pre-emptively send responses to a client in association with a previous client-initiated request. This can be useful when the server knows the client will need to have those responses available in order to fully process the response to the original request.

It sounds simple and easy but is quite tricky for service developers to manually figure out and configure what resources to push in association with another resource. There are also many pitfalls the implementors must know about. See Rules of Thumb for HTTP/2 Push for the details.

This project is for automating server push and getting rid of the need for manual configurations from service developers. It is meant as a helper library for building middlewares for various Node.js web servers, such as Express, fastify, etc.

This library assumes that the middlewares built on top of it act as a static file server. That is because static file serving is one of the most common use cases for HTTP/2 server push.

See https://github.com/google/node-fastify-auto-push for an example. It is a fastify plugin for supporting auto-push.

This package currently works only with Node >=9.4.0.

Interface

class AutoPush

Constructor

constructor(rootDir: string, cacheConfig?: AssetCacheConfig);

Constructs an AutoPush object.

  • rootDir: Top-level directory that contains the static files to be served.
  • cacheConfig: Configuration options for customizing the caching behavior.

preprocessRequest()

preprocessRequest(
    reqPath: string,
    stream: http2.ServerHttp2Stream,
    cacheCookie?: string): Promise<PreprocessResult>;

interface PreprocessResult {
  newCacheCookie: string;
  pushFn: () => Promise<void>;
}

This method must be called for every request from the client. It determines which other resources must be pushed, if any, in association with the current request path. It also checks whether any resources to be pushed are already cached in the browser side. It is done by storing and reading the related information in a cookie value.

  • reqPath: The request path given from the client.
  • stream: The current ServerHttp2Stream object.
  • cacheCookie: The value of the cookie used for storing the information on which resources are cached in the browser. It is up to the middleware which cookie to use for this.

Returns a promise for the result (PreprocessResult). The middleware must use the newCacheCookie value to store it in the browser cookie, and use pushFn to push static resources that are associated with the current request.

This method (and recordRequestPath() described below) must be called for non-static file requests as well as static files that are being served by the middleware. That's because there may be cases where a set of static files must be pushed in association with a non-static resource. For example, when index.html is a non-static file that is dynamically generated by the application, it probably wants to push related resources such as stylesheets, images, JavaScript files, etc. that are needed for the browser to render the page.

When there is an error while pushing resources, a 'pushError' will be emitted on the parent stream, whose argument is an error object that caused it.

recordRequestPath()

recordRequestPath(
    session: http2.Http2Session,
    reqPath: string,
    isStatic: boolean): void;

This method must be called for every request from the client. It must be called for non-static file requests as well as static files, as explained for preprocessRequest() above.

  • session: The current session object.
  • reqPath: The request path given from the client.
  • isStatic: true if the request is for a static file that is being served by the middleware, false otherwise.

AssetCacheConfig

This can be passed to the constructor of AutoPush to customize the caching behavior.

interface AssetCacheConfig {
  warmupDuration: number;
  promotionRatio: number;
  demotionRatio: number;
  minimumRequests: number;
}

warmupDuration

The time duration (in milliseconds) after a client request during which to record additional requests. That record will be used for determining the associated resources that may be pushed for a request path.

promotionRatio

If an additional request is frequently made for a certain original request and its hit ratio is over the promotionRatio value, that request path is considered one of the associated resources of the original request, and it'll be pushed when a request is made for the same original request path later.

demotionRatio

Similar to promotionRatio, but if the hit ratio is lower than demotionRatio, the request path will not be considered an associated resource anymore.

minimumRequests

The minimum number of requests for a certain path before being considered as a candidate for an associated resource.

node-h2-auto-push's People

Contributors

dependabot[bot] avatar douglascvas avatar greenkeeper[bot] avatar jinwoo avatar justinbeckwith avatar ofrobots avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-h2-auto-push's Issues

Make the push functionality independent from AssetCache

In cases where one already has reliable push-manifest.json or something like that, they probably want to use only the push functionality (with the Cookie management) of this library without relying on the AssetCache behavior.

Making the push functionality independent from AssetCache would greatly improve the flexibility of this library.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Preset name not found within published preset config (monorepo:angularcli). Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Push fails when using javascript modules

Version: v0.4.0
Browser: Chrome

Problem:
When importing resources using javascript modules Chrome fails to receive and parse the server pushed resources because there is no content-type header for them.

<script type="module" src="myJsFile.js"></script>

Message:
Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

Dependency Dashboard

This issue provides visibility into Renovate updates and their statuses. Learn more

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update actions/setup-node action to v2
  • chore(deps): update codecov/codecov-action action to v2

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.


  • Check this box to trigger a request for Renovate to run again on this repository

Broken with Node v9.4.0

nodejs/node#17406 changed the ServerHttp2Stream#pushStream() function such that its callback function used to be (pushedStream, headers) => {} but now is (err, pushedStream, headers) => {}.

That breaks this h2-auto-push package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.