Open phelicksg opened 4 days ago
Hey, no worries!
So the issue here is actually the await
in for await
- that's a JavaScript language feature that loops over an "async iterable", which is basically an iterable (object that produces multiple values) that produces each next value as a promise that needs to be await
'd. Kinda like a stream of values. In the example, for await (const message of rpc.responses)
basically says "for each response from the API".
Since it's using await
though, you will likely need to run it inside an async function. There is something called "top level await" support that lets you use await
outside of async functions, but it's experimental in the latest versions of Node.
That could look something like this:
const spiffe = require('spiffe');
// Connect to a specific endpoint
const client = spiffe.createClient('unix:///tmp/spire-agent/public/api.sock')
async function main() {
// Read x509 credentials from Workload API
const rpc = client.fetchX509SVID()
for await (const message of rpc.responses) {
...
}
}
main().catch(err => {
console.error(err)
})
Basically you will want to use the for await
inside of an async
function, either a new one you make or an existing one in your codebase.
import
vs require
is a whole separate topic , but this library should support both ways the same.
Hi,
apologies if this is a newbie mistake, as I'm not very fluent in Express/Node yet. There is one example which works great, when used standalone. However, that example relies on using the "import" statement, which is not allowed when used in an Express app. Instead, one would have to rewrite the example to use the "require" statement.
I am banging my head against the wall with the line:
for await (const message of rpc.responses) {
I've changed the first lines to:
[...]
Which renders the error:
Obviously I am doing something wrong, but this is not trivial. Do you think you could provide a short example that uses require instead of import to use the SPIFFE library? That would be super appreciated. Thanks so much in advance.