@filoz/repair-cli 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/cli.js +19 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/commands/datasets.d.ts +9 -0
- package/dist/src/commands/datasets.d.ts.map +1 -0
- package/dist/src/commands/datasets.js +59 -0
- package/dist/src/commands/datasets.js.map +1 -0
- package/dist/src/commands/providers.d.ts +9 -0
- package/dist/src/commands/providers.d.ts.map +1 -0
- package/dist/src/commands/providers.js +84 -0
- package/dist/src/commands/providers.js.map +1 -0
- package/dist/src/commands/repair.d.ts +9 -0
- package/dist/src/commands/repair.d.ts.map +1 -0
- package/dist/src/commands/repair.js +170 -0
- package/dist/src/commands/repair.js.map +1 -0
- package/dist/src/commands/setup.d.ts +11 -0
- package/dist/src/commands/setup.d.ts.map +1 -0
- package/dist/src/commands/setup.js +127 -0
- package/dist/src/commands/setup.js.map +1 -0
- package/dist/src/commands/wallet.d.ts +9 -0
- package/dist/src/commands/wallet.d.ts.map +1 -0
- package/dist/src/commands/wallet.js +150 -0
- package/dist/src/commands/wallet.js.map +1 -0
- package/dist/src/db/get-pieces.d.ts +23 -0
- package/dist/src/db/get-pieces.d.ts.map +1 -0
- package/dist/src/db/get-pieces.js +84 -0
- package/dist/src/db/get-pieces.js.map +1 -0
- package/dist/src/db/get-providers-by-cid.d.ts +10 -0
- package/dist/src/db/get-providers-by-cid.d.ts.map +1 -0
- package/dist/src/db/get-providers-by-cid.js +45 -0
- package/dist/src/db/get-providers-by-cid.js.map +1 -0
- package/dist/src/db/get-repair-dataset.d.ts +9 -0
- package/dist/src/db/get-repair-dataset.d.ts.map +1 -0
- package/dist/src/db/get-repair-dataset.js +15 -0
- package/dist/src/db/get-repair-dataset.js.map +1 -0
- package/dist/src/db/get-repair-provider.d.ts +6 -0
- package/dist/src/db/get-repair-provider.d.ts.map +1 -0
- package/dist/src/db/get-repair-provider.js +28 -0
- package/dist/src/db/get-repair-provider.js.map +1 -0
- package/dist/src/db/get-target-dataset.d.ts +7 -0
- package/dist/src/db/get-target-dataset.d.ts.map +1 -0
- package/dist/src/db/get-target-dataset.js +27 -0
- package/dist/src/db/get-target-dataset.js.map +1 -0
- package/dist/src/db/repair-create.d.ts +7 -0
- package/dist/src/db/repair-create.d.ts.map +1 -0
- package/dist/src/db/repair-create.js +69 -0
- package/dist/src/db/repair-create.js.map +1 -0
- package/dist/src/db/repair-delete.d.ts +11 -0
- package/dist/src/db/repair-delete.d.ts.map +1 -0
- package/dist/src/db/repair-delete.js +22 -0
- package/dist/src/db/repair-delete.js.map +1 -0
- package/dist/src/db/repair-update.d.ts +10 -0
- package/dist/src/db/repair-update.d.ts.map +1 -0
- package/dist/src/db/repair-update.js +13 -0
- package/dist/src/db/repair-update.js.map +1 -0
- package/dist/src/db/sync-pieces-onchain.d.ts +10 -0
- package/dist/src/db/sync-pieces-onchain.d.ts.map +1 -0
- package/dist/src/db/sync-pieces-onchain.js +35 -0
- package/dist/src/db/sync-pieces-onchain.js.map +1 -0
- package/dist/src/db/update-operation.d.ts +11 -0
- package/dist/src/db/update-operation.d.ts.map +1 -0
- package/dist/src/db/update-operation.js +14 -0
- package/dist/src/db/update-operation.js.map +1 -0
- package/dist/src/db/upsert-operations.d.ts +8 -0
- package/dist/src/db/upsert-operations.d.ts.map +1 -0
- package/dist/src/db/upsert-operations.js +13 -0
- package/dist/src/db/upsert-operations.js.map +1 -0
- package/dist/src/error.d.ts +14 -0
- package/dist/src/error.d.ts.map +1 -0
- package/dist/src/error.js +27 -0
- package/dist/src/error.js.map +1 -0
- package/dist/src/indexer-schema.d.ts +549 -0
- package/dist/src/indexer-schema.d.ts.map +1 -0
- package/dist/src/indexer-schema.js +56 -0
- package/dist/src/indexer-schema.js.map +1 -0
- package/dist/src/local-schema.d.ts +456 -0
- package/dist/src/local-schema.d.ts.map +1 -0
- package/dist/src/local-schema.js +63 -0
- package/dist/src/local-schema.js.map +1 -0
- package/dist/src/middleware.d.ts +19 -0
- package/dist/src/middleware.d.ts.map +1 -0
- package/dist/src/middleware.js +28 -0
- package/dist/src/middleware.js.map +1 -0
- package/dist/src/pipeline/create-datasets.d.ts +10 -0
- package/dist/src/pipeline/create-datasets.d.ts.map +1 -0
- package/dist/src/pipeline/create-datasets.js +48 -0
- package/dist/src/pipeline/create-datasets.js.map +1 -0
- package/dist/src/pipeline/pull.d.ts +30 -0
- package/dist/src/pipeline/pull.d.ts.map +1 -0
- package/dist/src/pipeline/pull.js +169 -0
- package/dist/src/pipeline/pull.js.map +1 -0
- package/dist/src/types.d.ts +34 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +1 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils.d.ts +11945 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +121 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +135 -0
- package/readme.md +250 -0
- package/src/cli.ts +20 -0
- package/src/commands/datasets.ts +62 -0
- package/src/commands/providers.ts +99 -0
- package/src/commands/repair.ts +177 -0
- package/src/commands/setup.ts +142 -0
- package/src/commands/wallet.ts +159 -0
- package/src/db/get-pieces.ts +189 -0
- package/src/db/get-providers-by-cid.ts +75 -0
- package/src/db/get-repair-dataset.ts +44 -0
- package/src/db/get-repair-provider.ts +47 -0
- package/src/db/get-target-dataset.ts +47 -0
- package/src/db/repair-create.ts +101 -0
- package/src/db/repair-delete.ts +39 -0
- package/src/db/repair-update.ts +20 -0
- package/src/db/sync-pieces-onchain.ts +53 -0
- package/src/db/update-operation.ts +26 -0
- package/src/db/upsert-operations.ts +23 -0
- package/src/error.ts +33 -0
- package/src/indexer-schema.ts +77 -0
- package/src/local-schema.ts +91 -0
- package/src/middleware.ts +34 -0
- package/src/pipeline/create-datasets.ts +70 -0
- package/src/pipeline/pull.ts +255 -0
- package/src/types.ts +41 -0
- package/src/utils.ts +190 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import type { MetadataObject } from '@filoz/synapse-core'
|
|
2
|
+
import { type Chain, getChain } from '@filoz/synapse-core/chains'
|
|
3
|
+
import Conf from 'conf'
|
|
4
|
+
import { pushSQLiteSchema } from 'drizzle-kit/api'
|
|
5
|
+
import { getTableColumns, type SQL, sql } from 'drizzle-orm'
|
|
6
|
+
import { drizzle } from 'drizzle-orm/libsql'
|
|
7
|
+
import type { PgTable } from 'drizzle-orm/pg-core'
|
|
8
|
+
import type { SQLiteTable } from 'drizzle-orm/sqlite-core'
|
|
9
|
+
import { z } from 'incur'
|
|
10
|
+
import { request } from 'iso-web/http'
|
|
11
|
+
import pLocate from 'p-locate'
|
|
12
|
+
import terminalLink from 'terminal-link'
|
|
13
|
+
import { createWalletClient, http } from 'viem'
|
|
14
|
+
import { privateKeyToAccount } from 'viem/accounts'
|
|
15
|
+
import packageJson from '../package.json' with { type: 'json' }
|
|
16
|
+
import * as schema from './local-schema.ts'
|
|
17
|
+
import type { Config, LocalDatabase } from './types.ts'
|
|
18
|
+
|
|
19
|
+
export const EARLY_REPAIR_SOURCE = 'early-repair6'
|
|
20
|
+
|
|
21
|
+
export const config = new Conf<Config>({
|
|
22
|
+
projectName: packageJson.name,
|
|
23
|
+
projectSuffix: '',
|
|
24
|
+
schema: {
|
|
25
|
+
privateKey: {
|
|
26
|
+
type: 'string',
|
|
27
|
+
},
|
|
28
|
+
dbPath: {
|
|
29
|
+
type: 'string',
|
|
30
|
+
},
|
|
31
|
+
indexerMainnetUrl: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
},
|
|
34
|
+
indexerCalibrationUrl: {
|
|
35
|
+
type: 'string',
|
|
36
|
+
},
|
|
37
|
+
chainId: {
|
|
38
|
+
type: 'number',
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
export const name = packageJson.name
|
|
44
|
+
export const version = packageJson.version
|
|
45
|
+
|
|
46
|
+
function privateKeyFromConfig() {
|
|
47
|
+
const privateKey = config.get('privateKey')
|
|
48
|
+
if (!privateKey) {
|
|
49
|
+
throw new Error('Private key not found. Please run `repair-cli setup` first.')
|
|
50
|
+
}
|
|
51
|
+
return privateKey
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Create a private key client
|
|
56
|
+
* If the private key is not found, it will throw an error
|
|
57
|
+
*
|
|
58
|
+
* @param chainId - The chain ID to use
|
|
59
|
+
*/
|
|
60
|
+
export function getClient(chainId: number) {
|
|
61
|
+
const chain = getChain(chainId)
|
|
62
|
+
|
|
63
|
+
const privateKey = privateKeyFromConfig()
|
|
64
|
+
|
|
65
|
+
const account = privateKeyToAccount(privateKey)
|
|
66
|
+
const client = createWalletClient({
|
|
67
|
+
account,
|
|
68
|
+
chain,
|
|
69
|
+
transport: http(),
|
|
70
|
+
})
|
|
71
|
+
return {
|
|
72
|
+
client,
|
|
73
|
+
chain,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Global options for the CLI
|
|
79
|
+
* - debug - Debug mode
|
|
80
|
+
*/
|
|
81
|
+
export const globalOptions = z.object({
|
|
82
|
+
debug: z.boolean().optional().default(false).describe('Debug mode'),
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
export async function createLocalDatabase(dbPath: string): Promise<LocalDatabase> {
|
|
86
|
+
const localDb = drizzle(`file:${dbPath}`, {
|
|
87
|
+
schema,
|
|
88
|
+
}) as LocalDatabase
|
|
89
|
+
|
|
90
|
+
await localDb.$client.execute('PRAGMA journal_mode = WAL')
|
|
91
|
+
|
|
92
|
+
return localDb
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function migrateLocalDatabase(db: LocalDatabase) {
|
|
96
|
+
// @ts-expect-error - TODO: fix this
|
|
97
|
+
const result = await pushSQLiteSchema(schema, db)
|
|
98
|
+
if (result.hasDataLoss) {
|
|
99
|
+
throw new Error('Data loss detected during migration')
|
|
100
|
+
}
|
|
101
|
+
if (result.warnings.length > 0) {
|
|
102
|
+
throw new Error(`Warnings detected during migration:\n${result.warnings.join('\n')}`)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
await result.apply()
|
|
106
|
+
return result
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Create a link to the hash on the block explorer
|
|
111
|
+
*
|
|
112
|
+
* @param hash - The hash to create a link for
|
|
113
|
+
* @param chain - The chain to use
|
|
114
|
+
* @returns The link
|
|
115
|
+
*/
|
|
116
|
+
export function hashLink(hash: string, chain: Chain) {
|
|
117
|
+
const link = terminalLink(hash, `${chain.blockExplorers?.default?.url}/tx/${hash}`)
|
|
118
|
+
return link
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Get metadata for the single IPFS-enabled repair dataset. */
|
|
122
|
+
export function getRepairDatasetMetadata(): MetadataObject {
|
|
123
|
+
return {
|
|
124
|
+
source: EARLY_REPAIR_SOURCE,
|
|
125
|
+
withIPFSIndexing: '',
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Get a piece from a service URL
|
|
131
|
+
*/
|
|
132
|
+
export async function getPiece({ pieceCid, serviceUrl }: { pieceCid: string; serviceUrl: string }) {
|
|
133
|
+
const url = new URL(`/piece/${pieceCid}`, serviceUrl)
|
|
134
|
+
const response = await request.head(url, {
|
|
135
|
+
retry: {
|
|
136
|
+
retries: 2,
|
|
137
|
+
minTimeout: 250,
|
|
138
|
+
},
|
|
139
|
+
timeout: 3000,
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
if (response.error) {
|
|
143
|
+
// console.log(response.error.message, url.toString())
|
|
144
|
+
throw response.error
|
|
145
|
+
}
|
|
146
|
+
return pieceCid
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Find the piece on the providers
|
|
151
|
+
*
|
|
152
|
+
* @param providers - {@link string[]}
|
|
153
|
+
* @param pieceCid - {@link string}
|
|
154
|
+
* @returns The piece URL
|
|
155
|
+
*/
|
|
156
|
+
export async function findPieceOnProviders(providers: string[], pieceCid: string) {
|
|
157
|
+
const result = await pLocate(
|
|
158
|
+
providers.map((p) =>
|
|
159
|
+
getPiece({
|
|
160
|
+
serviceUrl: p,
|
|
161
|
+
pieceCid,
|
|
162
|
+
}).then(
|
|
163
|
+
() => p,
|
|
164
|
+
() => undefined
|
|
165
|
+
)
|
|
166
|
+
),
|
|
167
|
+
(p) => p !== undefined,
|
|
168
|
+
{ concurrency: 5 }
|
|
169
|
+
)
|
|
170
|
+
return result
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export const buildConflictUpdateColumns = <T extends PgTable | SQLiteTable, Q extends keyof T['_']['columns']>(
|
|
174
|
+
table: T,
|
|
175
|
+
columns?: Q[]
|
|
176
|
+
) => {
|
|
177
|
+
const cls = getTableColumns(table)
|
|
178
|
+
const cols = columns ?? (Object.keys(cls) as Q[])
|
|
179
|
+
const r = cols.reduce(
|
|
180
|
+
(acc, column) => {
|
|
181
|
+
const colName = cls[column].name
|
|
182
|
+
|
|
183
|
+
acc[column] = sql.raw(`excluded.${colName}`)
|
|
184
|
+
return acc
|
|
185
|
+
},
|
|
186
|
+
{} as Record<Q, SQL>
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
return r
|
|
190
|
+
}
|