weweb-microservice-token
v0.1.19
Published
microservices token management for Weweb
Keywords
Readme
Api token management for Weweb
https://www.npmjs.com/package/weweb-microservice-token
npm install weweb-microservice-token --save
Network error diagnostics
Set preserveNetworkError: true in request options to receive a NetworkError
with code: 'NETWORK_ERROR' instead of the default 'NETWORK_ERROR' string:
await wwmt.get(url, query, headers, { preserveNetworkError: true })
await wwmt.post(url, body, query, headers, { preserveNetworkError: true })The error's details include the original transport code (for example,
ECONNRESET, ECONNREFUSED, or ECONNABORTED), and available system error,
address, port, and timeout fields. They also include the destination hostname,
HTTP method, configured timeout, elapsed milliseconds across retries, and attempt
count. Missing transport fields are omitted.
The error is rebuilt from these fields. It excludes the original request and response objects, headers, body, URL credentials, path, query, and fragment. The message is generated from the transport code rather than copying the original message, which can contain the full URL. Existing retry behavior and HTTP errors are unchanged. A network failure has no HTTP response; these diagnostics identify the transport failure rather than an error returned by the destination service.
Bounded responses and redirects
Version 0.1.18 adds opt-in request options:
const response = await wwmt.post(url, body, query, headers, {
maxResponseBytes: 1024 * 1024,
rejectRedirects: true,
timeout: 10000,
maxAttemps: 1,
})maxResponseBytes must be a positive safe integer. It buffers the response,
counts bytes after HTTP decompression and before text decoding or JSON parsing,
and applies to success and error responses. JSON and text responses use the
existing Superagent parsers (missing Content-Type defaults to text); binary and
unrecognized MIME responses return a
Buffer. Multipart responses are also returned as a bounded Buffer.
Exceeding the limit cancels the response and rejects with an Error whose
code is RESPONSE_TOO_LARGE. It is never retried and carries no partial
response, request, authentication headers or body. Small HTTP error responses
and their existing retry policy are preserved. Malformed bounded JSON rejects
with code: RESPONSE_PARSE_ERROR and its HTTP status, without raw response text.
rejectRedirects: true disables following redirects and rejects with an Error
whose code is REDIRECT_REJECTED, without forwarding authentication to the
redirect target. It can be used independently of the response limit.
Both options are disabled by default; existing callers retain their behavior.
Callers of a route
Version 0.1.19 lets a route name the services that may call it:
app.route('/microservice/ask/chat/stop').post(wwmt.verifyWwServiceToken(['weweb-back']), ctrl.stopGeneration)The token is checked first, as before. A valid token from an allowed service that
is not in the list then gets 403 { message: 'WW_SERVICE_ACCESS_DENIED' }. The list
must be a non-empty array of service names, or declaring the route throws.
Used directly as a middleware (wwmt.verifyWwServiceToken), it still accepts every
service of allowedServices.
In local (WW_ENV=local) the token is still not checked, but a request naming a
service outside the list is refused, so a missing caller shows up before deploy.
init takes an optional onAccessDenied({ serviceName, method, path }) hook,
called on each of those 403s (path without the query string), to log them:
wwmt.init({
name: 'weweb-ai',
seed: process.env.WW_SEED_AI,
allowedServices: [{ name: 'weweb-back', seed: process.env.WW_SEED_BACK }],
onAccessDenied: details => log.warn({ message: 'WW_SERVICE_ACCESS_DENIED', ...details }),
})