From 86261ba0b645b0d08cd21435a2fcc495bf890375 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 6 Aug 2026 03:04:57 -0700 Subject: [PATCH] Refactor environment parsing for RPC values. --- sample.env | 22 +++++++++++++++++++--- sample.env.mjs | 24 +++++++++++++++++++----- src/lib/rpc/index.ts | 23 ++++++++++++++++++----- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/sample.env b/sample.env index b1f6ab2..229237e 100644 --- a/sample.env +++ b/sample.env @@ -1,6 +1,22 @@ # SPDX-FileCopyrightText: 2025 Chris Duncan # SPDX-License-Identifier: GPL-3.0-or-later -# Save this file as `.env` and replace the following with real values -LIBNEMO_RPC_URL="https://rpc.example.com" -LIBNEMO_RPC_AUTHORIZATION="fedcba9876543210fedcba9876543210" +## Save this file as `.env` and replace the examples with real values. + +## Specify a valid URL pointing to a live Nano node. +## +LIBNEMO_RPC_URL='https://rpc.example.com' + +## Pass a plain string to use the default "Authorization" HTTP header. Examples: +## +## 'Basic dXNlckBleGFtcGxlLmNvbTpwYXNzd29yZDEyMyE=' +## 'Bearer fedcba9876543210fedcba9876543210' +## +## Alternatively, pass a JSON string to use a custom header for authentication. +## The expected JSON format is as follows: +## +## '{ "header": "custom-auth-header", "value": "fedcba9876543210fedcba9876543210" }' +## +## Replace the example values for `"header"` and `"value"` with your own. +## +LIBNEMO_RPC_AUTHORIZATION='Bearer fedcba9876543210fedcba9876543210' diff --git a/sample.env.mjs b/sample.env.mjs index 1b61f5f..dde66e3 100644 --- a/sample.env.mjs +++ b/sample.env.mjs @@ -1,10 +1,24 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -'use strict' - -// Save this file as `env.mjs` and replace the following with real values +// Save this file as `env.mjs` and replace the examples with real values export const env = { - LIBNEMO_RPC_URL: "https://rpc.example.com", - LIBNEMO_RPC_AUTHORIZATION: "fedcba9876543210fedcba9876543210" + + // Specify a valid URL pointing to a live Nano node. + // + LIBNEMO_RPC_URL: 'https://rpc.example.com', + + // Pass a string to use the default "Authorization" HTTP header. Examples: + // + // 'Basic dXNlckBleGFtcGxlLmNvbTpwYXNzd29yZDEyMyE=' + // 'Bearer fedcba9876543210fedcba9876543210' + // + // Alternatively, pass an object to use a custom header for authentication. + // The expected format is as follows: + // + // { header: "custom-auth-header", value: "fedcba9876543210fedcba9876543210" } + // + // Replace the example values for`header` and `value` with your own. + // + LIBNEMO_RPC_AUTHORIZATION: 'Bearer fedcba9876543210fedcba9876543210' } diff --git a/src/lib/rpc/index.ts b/src/lib/rpc/index.ts index c38c2ad..afe1767 100644 --- a/src/lib/rpc/index.ts +++ b/src/lib/rpc/index.ts @@ -15,13 +15,21 @@ export { Schema } */ export class Rpc { #url: URL - #auth?: string + #auth?: string | { header: string, value: string } /** * @param {(string|URL)} url Nano network node - * @param {string} [auth] optional API key + * @param {string} [auth] "Authorization" token HTTP header for an API key */ - constructor (url: string | URL, auth?: string) { + constructor (url: string | URL, auth?: string) + /** + * @param {(string|URL)} url Nano network node + * @param {object} [auth] custom HTTP header for an API key + * @param {string} auth.header custom authorization HTTP header name + * @param {string} auth.value authorization token value + */ + constructor (url: string | URL, auth?: { header: string, value: string }) + constructor (url: string | URL, auth?: string | { header: string, value: string }) { this.#url = new URL(url) this.#url.protocol = 'https:' this.#auth = auth @@ -42,8 +50,13 @@ export class Rpc { 'Content-Type': 'application/json' } const auth = this.#auth ?? env?.LIBNEMO_RPC_AUTHORIZATION - if (auth) { - headers['Authorization'] = auth + if (typeof auth === 'string') { + const token = /^\{.*\}$/.test(auth.trim()) ? JSON.parse(auth) : auth + headers['Authorization'] = token + } else if (typeof auth?.header === 'string' && typeof auth.value === 'string') { + headers[auth.header] = auth.value + } else { + console.warn('Unrecognized RPC authorization format') } if (data !== undefined && typeof data !== 'object') { throw new TypeError('Invalid RPC post data') -- 2.52.0