Skip to content

Instantly share code, notes, and snippets.

@mrandri19 mrandri19/jenga.ts
Last active Jun 24, 2018

Embed
What would you like to do?
import fetch from "node-fetch"
export interface PingData {
total_rows: number
offset: number
rows: Row[]
}
type packageid = string
export interface Row {
id: packageid
key: string[]
value: number
}
interface TreeArray extends Array<DependencyTree> { }
type DependencyTree = packageid | TreeArray
let requests = 0
let already_downloaded: packageid[] = []
async function get_dependents(package_name: packageid): Promise<packageid[]> {
const url = `https://skimdb.npmjs.com/registry/_design/app/_view/dependedUpon?startkey=["${package_name}"]&endkey=["${package_name}",{}]&reduce=false`
const encoded_url = encodeURI(url)
const res = await fetch(encoded_url)
const json: PingData = await res.json()
console.log(`downloading package: ${package_name}, request number: ${requests++}`)
return json.rows.map(row => row.id).filter(id => !id.startsWith("@"))
}
async function main() {
async function count_dependents(
package_name: packageid,
level: number
): Promise<number> {
if (already_downloaded.indexOf(package_name) > -1) {
return 1
}
const deps = await get_dependents(package_name)
already_downloaded.push(package_name)
if (deps.length == 0) {
return 1
}
return (await Promise.all(
deps.map(package_name => count_dependents(package_name, level + 1))
)).reduce((acc, n) => acc + n)
}
console.log(await count_dependents("is-odd", 0))
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.