Web Bundles
This is a Node.js module for serializing and parsing the application/webbundle
format defined in the Web
Bundles
draft spec.
Currently this library doesn't support origin-signed bundles, but bundles generated by this library can be signed with the sign-bundle
Go tool.
Installation
Using npm:
npm install wbn
Usage
Please be aware that the API is not yet stable and is subject to change any time.
Creating a Bundle:
const wbn = require('wbn');
const fs = require('fs');
const builder = new wbn.BundleBuilder();
builder.addExchange(
exampleURL, // URL
200, // response code
{'Content-Type': 'text/html'}, // response headers
'<html>Hello, Web Bundle!</html>'); // response body (string or Uint8Array)
// Have as many builder.addExchange() for resource URLs as needed for the package.
fs.writeFileSync('out.wbn', builder.createBundle());
Reading a Bundle:
const wbn = require('wbn');
const fs = require('fs');
const buf = fs.readFileSync('out.wbn');
const bundle = new wbn.Bundle(buf);
const exchanges = [];
for (const url of bundle.urls) {
const resp = bundle.getResponse(url);
exchanges.push({
url,
status: resp.status,
headers: resp.headers,
body: resp.body.toString('utf-8')
});
}
console.log(JSON.stringify({
version: bundle.version, // format version
exchanges
}, null, 2));
CLI
This package also includes wbn
command which lets you build a web bundle from a local directory. For example, if you have all the necessary files for https://example.com/
in static/
directory, run the following command:
$ wbn --dir static \
--baseURL https://example.com/ \
--output out.wbn
Run wbn --help
for full options.
Note: currently this CLI only covers a subset of the functionality offered by gen-bundle
Go tool.
Backwards compatibility
This module supports creating and parsing Web Bundles that follow different draft versions of the format specification. In particular:
- version
b2
follows the latest version of the Web Bundles spec (default) - version
b1
follows the previous version of the Web Bundles spec
To create a new bundle with the b1
format, pass the version value to the constructor:
const builder = (new wbn.BundleBuilder('b1'))
.setPrimaryURL('https://example.com/')
.setManifestURL('https://example.com/manifest.json')
.addExchange(...);
fs.writeFileSync('out_b1.wbn', builder.createBundle());
Likewise, the wbn
command can optionally take a --formatVersion b1
parameter when creating a new Web Bundle.
This module also takes care of selecting the right format version automatically when reading a bundle. Check the property bundle.version
to know the decoded bundle's format version.
Using Bundles
Generated bundles can be opened with web browsers supporting web bundles.
Chrome (79+) experimentally supports navigation to Web Bundles with some limitations. See this document for more details.
Chrome (90+) experimentally supports subresource loading with Web Bundles. See this document for more details.
Release Notes
0.0.6
- Now BundleBuilder generates bundles of format
b2
by default. See Backwards compatibility section above.