-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·180 lines (163 loc) · 5.63 KB
/
Copy pathcli.js
File metadata and controls
executable file
·180 lines (163 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
import sade from 'sade'
import fs from 'node:fs'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import * as readline from 'node:readline/promises'
import { Parallel } from 'parallel-transform-web'
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { S3Client } from '@aws-sdk/client-s3'
import dotenv from 'dotenv'
import { checkDenyList } from './deny.js'
import { dudewhere } from './dudewhere.js'
import { findIndexesByCid, base58btcMultihash } from './dynamo.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url).toString())
dotenv.config({ path: path.join(__dirname, '/.env') })
const concurrency = 50
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'))
const cli = sade('w3stat')
cli
.version(pkg.version)
.example('dudewhere [cid]')
cli.command('cid [cid]', 'find out what we know about that cid', { default: true })
.option('--json', 'format output as json')
.action(async (cidStr, opts) => {
const s3 = new S3Client({
region: 'auto',
endpoint: 'https://fffa4b4363a7e5250af8357087263b3a.r2.cloudflarestorage.com',
credentials: {
accessKeyId: env('R2_ACCESS_KEY_ID'),
secretAccessKey: env('R2_SECRET_ACCESS_KEY')
}
})
const dynamo = new DynamoDBClient({})
inputStream(cidStr, opts._)
.pipeThrough(new Parallel(concurrency, async cidStr => {
const [dude, indexes, deny] = await Promise.all([
dudewhere(cidStr, s3),
findIndexesByCid(cidStr, dynamo, opts.table),
checkDenyList(cidStr)
])
return { cid: cidStr, dude, indexes, deny }
}))
.pipeTo(new WritableStream({
write ({ cid, dude, indexes, deny }) {
if (opts.json) {
console.log(JSON.stringify({ cid, dudewhere: dude, dynamo: indexes, deny }))
} else {
const mh = base58btcMultihash(cid)
const cars = dude.length === 0 ? '🚘 missing dudewhere mapping' : dude.map(str => `🚘 ${str.split('/').at(1)}`).join('\n')
const idx = indexes.length === 0 ? `ℹ️ no block index in dynamo for ${mh}` : indexes.map(idx => `ℹ️ ${JSON.stringify(idx)}`).join('\n')
const denylist = deny.onDenyList ? '❌ blocked. cid found on denylist' : '🟢 ok. cid not on denylist'
console.log(`${cid}\n${denylist}\n${cars}\n${idx}`)
}
}
}))
})
cli.command('denylist [cid]', 'check if cid is on the w3s denylist')
.action(async (cidStr, opts) => {
inputStream(cidStr, opts._)
.pipeThrough(new Parallel(concurrency, async cidStr => {
return checkDenyList(cidStr)
}))
.pipeTo(new WritableStream({
write (obj) {
console.log(JSON.stringify(obj))
}
}))
})
cli.command('dudewhere [cid]', 'check the dudewhere map from root cid to car cid')
.option('-b, --bucket', 'bucket name', 'dudewhere-prod-0')
.action(async (cidStr, opts) => {
const s3 = new S3Client({
region: 'auto',
endpoint: 'https://fffa4b4363a7e5250af8357087263b3a.r2.cloudflarestorage.com',
credentials: {
accessKeyId: env('R2_ACCESS_KEY_ID'),
secretAccessKey: env('R2_SECRET_ACCESS_KEY')
}
})
inputStream(cidStr, opts._)
.pipeThrough(new Parallel(concurrency, async cid => {
const res = await dudewhere(cid, s3, opts.bucket)
return { cid, res }
}))
.pipeTo(new WritableStream({
write (obj) {
console.log(JSON.stringify(obj))
}
}))
})
cli.command('dynamo [cid]', 'check a cid is in dynamodb index')
.option('--table', 'table name', 'prod-ep-v1-blocks-cars-position')
.action(async (cidStr, opts) => {
const dynamo = new DynamoDBClient({})
inputStream(cidStr, opts._)
.pipeThrough(new TransformStream({
transform: async (cidStr, controller) => {
const items = await findIndexesByCid(cidStr, dynamo, opts.table)
controller.enqueue(items)
}
}))
.pipeTo(new WritableStream({
write (obj) {
console.log(JSON.stringify(obj))
}
}))
})
cli.command('mh [cid]', 'convert a cid to a multihash with base58btc multibase prefix')
.action((cidStr, opts) => {
inputStream(cidStr, opts)
.pipeTo(new WritableStream({
write (cidStr) {
console.log(base58btcMultihash(cidStr))
}
}))
})
cli.parse(process.argv)
/**
* @param {Record<string, string|undefined>} obj
* @param {string} key
*/
function notNully (obj, key, msg = 'unexpected null value') {
const value = obj[key]
if (!value) throw new Error(`${msg}: ${key}`)
return value
}
/**
* @param {string} key
*/
function env (key) {
return notNully(process.env, key, `${key} must be set in ENV`)
}
/**
* ReadableStream from params or stdin
*
* @param {string} first
* @param {string[]} rest
*/
function inputStream (first, rest = []) {
/** @type {ReadableStream<string>} */
const source = new ReadableStream({
async start (controller) {
// input param passed
if (first) {
controller.enqueue(first)
// maybe more then one
for (const item of rest) {
controller.enqueue(item)
}
return
}
// note: "having asynchronous operations between interface creation and asynchronous iteration may result in missed lines."
// https://nodejs.org/docs/latest-v18.x/api/readline.html#rlsymbolasynciterator
const rl = readline.createInterface({ input: process.stdin })
for await (const line of rl) {
controller.enqueue(line)
}
rl.close()
}
})
return source
}