
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
@hqoss/agent
Advanced tools
A light-weight, performant, composable blueprint for writing consistent and re-usable Node.js HTTP clients.
A light-weight, performant, composable blueprint for writing consistent and re-usable Node.js HTTP clients.
Extends node-fetch
, therefore 100% compatible with the underlying APIs.
agent
... as opposed to request
or node-fetch
?
request
is/was great, but it has entered maintenance mode.node-fetch
and request
are relatively low-level (in JavaScript terms) implementations and as such lack certain convenience methods/APIs that help design maintainable and consistent HTTP clients. This is especially true in the microservices architecture context, where consistency is paramount.agent
builds on node-fetch
to enable composable and re-usable HTTP client implementations.
Enforces a consistent approach to writing HTTP clients.
Greatly reduces common boilerplate, expressly
It is written in TypeScript.
⚠️ NOTE: The project is configured to target ES2018
and the library uses commonjs
module resolution. Read more in the Node version support section.
npm install @hqoss/agent
# Additionally, for TypeScript users
npm install @types/node-fetch --save-dev
⚠️ WARNING: Unlike request
, agent
(using node-fetch
under the hood) does NOT reject non-ok responses by default as per the whatwg spec. You can, however, mimic this behaviour with a custom responseTransformer
(see Transforming responses).
Define:
import { HttpClient } from "@asri/agent";
class GitHubClient extends HttpClient {
constructor() {
super({
baseUrl: "https://api.github.com/",
baseHeaders: { "accept": "application/vnd.github.v3+json" },
// Set any `node-fetch` supported `Request` options.
// Note that headers MUST be set in `baseHeaders`.
baseOptions: { timeout: 5000 },
// Automatically includes `accept: application/json` and
// `content-type: application/json` headers and parses responses to json.
// It will also use the default `jsonResponseTransformer`
// to parse responses into json. Does NOT reject non-ok responses.
json: true,
});
}
// Expose pre-configured, provider-specific methods to your consumers / API users.
getOrganisationDetails = (orgId: string) => this.get(`/orgs/${orgId}`);
getOrganisationRepositories = (orgId: string) => this.get(`/orgs/${orgId}/repos`, { timeout: 2500 });
}
export default GitHubClient;
Consume:
// Suppose you have a shared library for Http clients.
import { GitHubClient } from "@clients/github";
// You can also use the client's constructor to provide additional configuration here.
const gitHubClient = new GitHubClient();
// Resulting headers:
// `accept: application/vnd.github.v3+json` -> provided in `baseHeaders`
// `content-type: application/json` -> set internally due to `json: true`.
// Will warn because there is no `x-correlation-id` header set.
// Will use `timeout: 5000` as defined in `baseOptions`.
const organisationDetails = await gitHubClient.getOrganisationDetails();
// Same as above, but uses `timeout: 2500` – see `getOrganisationRepositories` implementation.
const organisationRepositories = await gitHubClient.getOrganisationRepositories();
You can intercept every request by implementing the willSendRequest
lifecycle method.
import { HeaderKey, HttpClient, RequestInterceptor } from "@asri/agent";
class GitHubClient extends HttpClient {
constructor() {
super({
baseUrl: "https://api.github.com/",
baseHeaders: { "accept": "application/vnd.github.v3+json" },
json: true,
});
}
private static correlationIdHeader = HeaderKey.CorrelationId;
// Inspired by Apollo's REST Data Source, this lifecycle method
// can be used to perform useful actions before a request is sent.
protected willSendRequest: RequestInterceptor = (url, { headers }) => {
const { correlationIdHeader } = HttpStatClient;
console.info(`Outgoing request to ${url}`);
if (!(correlationIdHeader in headers)) {
console.warn(`missing ${correlationIdHeader} header`);
};
}
// ... pre-configured methods follow.
}
export default GitHubClient;
There is a great deal of discussion on whether fetch
should or should not reject non-ok responses [1,2].
We believe such design choices should ultimately be made by the consumers, so the HttpClient
base class exposes a convenient mechanism to transform responses via the transformResponse
method.
import { HttpClient, ResponseTransformer } from "@asri/agent";
class GitHubClient extends HttpClient {
constructor() {
super({ baseUrl: "https://api.github.com/", json: true });
}
// Mmimics the default behaviour of request, e.g. non-ok responses
// are rejected rather than resolved.
protected transformResponse: ResponseTransformer = async (response) => {
// You need to be careful with 204 No Content, please consider
// using our pre-built `jsonResponseTransformer` here instead.
const jsonResponse = await response.json();
if (response.ok) {
return jsonResponse;
} else {
throw jsonResponse;
}
};
// ... pre-configured methods follow.
}
export default GitHubClient;
Consume:
// Suppose you have a shared library for Http clients.
import { GitHubClient } from "@clients/github";
// You can also use the client's constructor to provide additional configuration here.
const gitHubClient = new GitHubClient();
// Non-ok responses, for example 404, will now reject.
const organisationDetails = gitHubClient.getOrganisationDetails()
.then(console.log)
// A non-ok response will now end up here.
.catch(console.error);
We ship the default HttpClient
with a pre-configured (Node.js) Agent
, which may lead to a huge increase in throughput.
For reference, we performed a number of benchmarks comparing the out-of-the-box request
, node-fetch
, and agent
clients. To fetch a list of 100 users from one service to another (see diagram below), these were the results:
| wrk | -HTTP-> | Server A -> HttpClient | -HTTP-> | Server B -> data in memory |
request
setup (used by most projects): 10,893 requests in 30.08s; 362.19 requests/secnode-fetch
setup (used by many projects): 8,632 requests in 30.08s; 286.98 requests/secagent
setup: 71,359 requests in 30.10s; 2,370.72 requests/secPlease note that these benchmarks were run through wrk
, each lasting 30 seconds, using 5 threads and keeping 500 connections open.
This is the default Agent
configuration, which can easily be overriden in the HttpClient
constructor. You can simply provide your own Agent
instance in baseOptions
.
const opts = {
keepAlive: true,
maxSockets: 64,
keepAliveMsecs: 5000,
};
Code quality; This package may end up being used in mission-critical software, so it's important that the code is performant, secure, and battle-tested.
Developer experience; Developers must be able to use this package with no significant barriers to entry. It has to be easy-to-find, well-documented, and pleasant to use.
Modularity & Configurability; It's important that users can compose and easily change the ways in which they consume and work with this package.
The project is configured to target ES2018. In practice, this means consumers should run on Node 12 or higher, unless additional compilation/transpilation steps are in place to ensure compatibility with the target runtime.
Please see https://node.green/#ES2018 for reference.
Firstly, according to the official Node release schedule, Node 12.x entered LTS on 2019-10-21 and is scheduled to enter Maintenance on 2020-10-20. With the End-of-Life scheduled for April 2022, we are confident that most users will now be running 12.x or higher.
Secondly, the 7.3 release of V8 (ships with Node 12.x or higher) includes "zero-cost async stack traces".
From the release notes:
We are turning on the --async-stack-traces flag by default. Zero-cost async stack traces make it easier to diagnose problems in production with heavily asynchronous code, as the error.stack property that is usually sent to log files/services now provides more insight into what caused the problem.
Ava and Jest were considered. Jest was chosen as it is very easy to configure and includes most of the features we need out-of-the-box.
Further investigation will be launched in foreseeable future to consider moving to Ava.
We prefer using Nock over mocking.
A quick and dirty tech debt tracker before we move to Issues.
npm doctor
, npm audit
, npm outdated
, ignore-scripts
in .npmrc
, etc.willSendRequest
and reponseTransformer
FAQs
A light-weight, performant, composable blueprint for writing consistent and re-usable Node.js HTTP clients.
The npm package @hqoss/agent receives a total of 4 weekly downloads. As such, @hqoss/agent popularity was classified as not popular.
We found that @hqoss/agent demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.