@canonical/macaroon-bakery

中文文档
1.3.2发布于 2023/01/06LGPL-3.0TypeScriptES Module近 30 天下载 8180

Perform macaroon aware network requests

原始使用文档
文档目录5 节
  1. bakeryjs~Bakery
  2. bakeryjs~BakeryStorage
  3. bakeryjs~InMemoryStore
  4. bakeryjs~serialize(macaroons) ⇒ string
  5. bakeryjs~deserialize(serialized) ⇒ Array

view on npm npm module downloads

bakeryjs

API Reference

bakeryjs.

Example

import { Bakery } from "@canonical/macaroon-bakery";
const bakery = new Bakery();

bakery.get(url, headers, callback);

bakeryjs~Bakery

A macaroon bakery implementation.

The bakery implements the protocol used to acquire and discharge macaroons over HTTP.

Kind: inner class of bakeryjs

new Bakery(config)

Initialize a macaroon bakery with the given parameters.

ParamTypeDescription
configObjectoptional config.
config.onSuccessfunctiona function to be called when the request completes properly.
config.protocolVersionnumberthe macaroon protocol version that the bakery should use.
config.storagefunctionthe storage used to persist macaroons. It must implement the following interface:
config.storage.getfunctionget(key) -> value.
config.storage.setfunctionset(key, value, callback): the callback is called without arguments when the set operation has been performed. If not provided, it defaults to BakeryStorage using an in memory store.
config.visitPagefunctionthe function used to visit the identity provider page when required, defaulting to opening a pop up window. It receives an error object.
config.visitPage.InfoObjectan object containing relevant info for the visit handling.
config.visitPage.Info.WaitUrlStringthe url to wait on for IdM discharge.
config.visitPage.Info.VisitUrlStringthe url to visit to authenticate with the IdM.
config.visitPage.jujuguifunctionan optional value specifying a method to use against idm to authenticate. Used in non interactive authentication scenarios.
config.sendRequestfunctiona function used to make XHR HTTP requests, with the following signature: func(path, method, headers, body, withCredentials, callback) -> xhr. By default an internal function is used. This is mostly for testing.

bakery.sendRequest(url, method, headers, body, callback) ⇒ Object

Send an HTTP request to the given URL with the given HTTP method, headers and body. The given callback receives an error and a response when the request is complete.

Kind: instance method of Bakery
Returns: Object - the XHR instance.

ParamTypeDescription
urlStringThe URL to which to send the request.
methodStringThe HTTP method, like "get" or "POST".
headersObjectHeaders that must be included in the request. Note that bakery specific headers are automatically added internally.
bodyStringThe request body if it applies, or null.
callbackfunctionA function called when the response is received from the remote URL. It receives a tuple (error, response). If the request succeeds the error is null.

bakery.get()

Send an HTTP GET request to the given URL with the given headers. The given callback receives an error and a response when the request is complete.

See the "sendRequest" method above for a description of the parameters.

Kind: instance method of Bakery

bakery.delete()

Send an HTTP DELETE request to the given URL with the given headers and body. The given callback receives an error and a response when the request is complete.

See the "sendRequest" method above for a description of the parameters.

Kind: instance method of Bakery

bakery.post()

Send an HTTP POST request to the given URL with the given headers and body. The given callback receives an error and a response when the request is complete.

See the "sendRequest" method above for a description of the parameters.

Kind: instance method of Bakery

bakery.put()

Send an HTTP PUT request to the given URL with the given headers and body. The given callback receives an error and a response when the request is complete.

See the "sendRequest" method above for a description of the parameters.

Kind: instance method of Bakery

bakery.patch()

Send an HTTP PATCH request to the given URL with the given headers and body. The given callback receives an error and a response when the request is complete.

See the "sendRequest" method above for a description of the parameters.

Kind: instance method of Bakery

bakery.discharge(macaroon, onSuccess, onFailure)

Discharge the given macaroon. Acquire any third party discharges.

Kind: instance method of Bakery

ParamTypeDescription
macaroonObjectThe decoded macaroon to be discharged.
onSuccessfunctionThe function to be called if the discharge succeeds. It receives the resulting macaroons array.
onFailurefunctionThe function to be called if the discharge fails. It receives an error message.

bakeryjs~BakeryStorage

A storage for the macaroon bakery.

The storage is used to persist macaroons.

Kind: inner class of bakeryjs

new BakeryStorage(store, config)

Initialize a bakery storage with the given underlaying store and params.

ParamTypeDescription
storeObjectA store object implement the following interface: - getItem(key) -> value; - setItem(key, value); - clear().
configObjectOptional configuration.
config.initialObjecta map of key/value pairs that must be initially included in
config.servicesObjecta map of service names (like "charmstore" or "terms") to the base URL of their corresponding API endpoints. This is used to simplify and reduce the URLs passed as keys to the storage.
config.charmstoreCookieSetterfunctiona function that can be used to register macaroons to the charm store service. The function accepts a value and a callback, which receives an error and a response.

bakeryStorage.get(key) ⇒ String

Retrieve and return the value for the provided key.

Kind: instance method of BakeryStorage
Returns: String - The corresponding value, usually a serialized macaroon.

ParamTypeDescription
keyStringThe storage key, usually a URL.

bakeryStorage.set(key, value, callback)

Store the given value in the given storage key.

Call the callback when done.

Kind: instance method of BakeryStorage

ParamTypeDescription
keyStringThe storage key, usually a URL.
valueStringThe value, usually a serialized macaroon.
callbackfunctionA function called without arguments when the value is properly stored.

bakeryStorage.clear()

Remove all key/value pairs from the storage.

Kind: instance method of BakeryStorage

bakeryjs~InMemoryStore

An in-memory store for the BakeryStorage.

Kind: inner class of bakeryjs

bakeryjs~serialize(macaroons) ⇒ string

Serialize the given macaroons.

Kind: inner method of bakeryjs
Returns: string - The resulting serialized string.

ParamTypeDescription
macaroonsArrayThe macaroons to be serialized.

bakeryjs~deserialize(serialized) ⇒ Array

De-serialize the given serialized macaroons.

Kind: inner method of bakeryjs
Returns: Array - The resulting macaroon slice.

ParamTypeDescription
serializedstringThe serialized macaroons.

版本与发布记录

@canonical/macaroon-bakery 共发布 10 个版本,首次发布于 2019/06/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.2latest2023/01/063 年前
1.3.12023/01/063 年前
1.3.02022/12/063 年前
1.2.22022/07/224 年前
1.2.12022/07/194 年前
1.2.02022/06/174 年前

其他发布通道:beta → 1.2.0-beta.1。

关于 @canonical/macaroon-bakery 的常见问题

@canonical/macaroon-bakery 是什么?

Perform macaroon aware network requests

如何安装 @canonical/macaroon-bakery?

在项目目录运行 npm install @canonical/macaroon-bakery,或使用 pnpm add / yarn add / bun add。

@canonical/macaroon-bakery 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@canonical/macaroon-bakery 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@canonical/macaroon-bakery 可以商用吗?

@canonical/macaroon-bakery 使用 LGPL-3.0 许可。弱 Copyleft:作为库被调用时通常不要求开源你自己的代码,但修改库本身需以 LGPL 开源;前端打包合并后的适用边界存在不同理解。以上为概括,不构成法律意见,请以许可原文为准。

@canonical/macaroon-bakery 的下载量有多少?

根据 npm 官方下载统计,@canonical/macaroon-bakery 在 2026/10/09 – 2026/11/07 这 30 天内被下载 8,180 次,日均约 273 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@canonical/macaroon-bakery 还在维护吗?

最新版本 1.3.2 发布于 2023/01/06(3 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/06/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。