-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b5ff21
commit cde3857
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// @ts-check | ||
import process from 'process'; | ||
import { makeMarshal } from '@endo/marshal'; | ||
import { decodeToJustin } from '@endo/marshal/src/marshal-justin.js'; | ||
|
||
import { | ||
delay, | ||
iterateLatest, | ||
makeChainStream, | ||
makeLeader, | ||
makeStoreKey, | ||
} from '@agoric/chain-streams'; | ||
|
||
export default async function streamMain(progname, rawArgs, powers, opts) { | ||
const { anylogger } = powers; | ||
const console = anylogger('agoric:follow'); | ||
|
||
const { | ||
integrity, | ||
output, | ||
bootstrap = 'http://localhost:26657', | ||
verbose, | ||
} = opts; | ||
|
||
let unserializer; | ||
/** @type {undefined | ((buf: Uint8Array) => any)} */ | ||
let decode; | ||
let formatOutput; | ||
switch (output) { | ||
case 'justinlines': | ||
case 'justin': { | ||
const { serialize } = makeMarshal(undefined, undefined, { | ||
// Don't save errors, we just display the neutered form. | ||
marshalSaveError: () => {}, | ||
}); | ||
const pretty = !output.endsWith('lines'); | ||
formatOutput = obj => { | ||
const { body } = serialize(harden(obj)); | ||
const marshalled = JSON.parse(body); | ||
return decodeToJustin(marshalled, pretty); | ||
}; | ||
break; | ||
} | ||
case 'jsonlines': | ||
case 'json': { | ||
const spaces = output.endsWith('lines') ? undefined : 2; | ||
const bigintToStringReplacer = (_, arg) => { | ||
if (typeof arg === 'bigint') { | ||
return `${arg}`; | ||
} | ||
return arg; | ||
}; | ||
formatOutput = obj => JSON.stringify(obj, bigintToStringReplacer, spaces); | ||
break; | ||
} | ||
case 'hex': { | ||
// Dump as hex strings. | ||
decode = buf => buf; | ||
unserializer = null; | ||
formatOutput = buf => | ||
buf.reduce((acc, b) => acc + b.toString(16).padStart(2, '0'), ''); | ||
break; | ||
} | ||
case 'text': { | ||
decode = buf => new TextDecoder().decode(buf); | ||
unserializer = null; | ||
formatOutput = buf => buf; | ||
break; | ||
} | ||
default: { | ||
console.error(`Unknown output format: ${output}`); | ||
return 1; | ||
} | ||
} | ||
|
||
/** @type {import('@agoric/chain-streams').ChainLeaderOptions} */ | ||
const leaderOptions = { | ||
retryCallback: e => { | ||
verbose && console.warn('Retrying due to:', e); | ||
return delay(1000 + Math.random() * 1000); | ||
}, | ||
}; | ||
/** @type {import('@agoric/chain-streams').ChainStreamOptions} */ | ||
const streamOptions = { | ||
integrity, | ||
unserializer, | ||
decode, | ||
}; | ||
|
||
const [_cmd, ...specs] = rawArgs; | ||
|
||
verbose && console.warn('Following leader at', bootstrap); | ||
const leader = makeLeader(bootstrap, leaderOptions); | ||
await Promise.all( | ||
specs.map(async spec => { | ||
verbose && console.warn('Consuming', spec); | ||
const storeKey = makeStoreKey(spec); | ||
const stream = makeChainStream(leader, storeKey, streamOptions); | ||
for await (const value of iterateLatest(stream)) { | ||
process.stdout.write(`${formatOutput(value)}\n`); | ||
} | ||
}), | ||
); | ||
return 0; | ||
} |