@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.
Files changed (127) hide show
  1. package/dist/src/cli.d.ts +3 -0
  2. package/dist/src/cli.d.ts.map +1 -0
  3. package/dist/src/cli.js +19 -0
  4. package/dist/src/cli.js.map +1 -0
  5. package/dist/src/commands/datasets.d.ts +9 -0
  6. package/dist/src/commands/datasets.d.ts.map +1 -0
  7. package/dist/src/commands/datasets.js +59 -0
  8. package/dist/src/commands/datasets.js.map +1 -0
  9. package/dist/src/commands/providers.d.ts +9 -0
  10. package/dist/src/commands/providers.d.ts.map +1 -0
  11. package/dist/src/commands/providers.js +84 -0
  12. package/dist/src/commands/providers.js.map +1 -0
  13. package/dist/src/commands/repair.d.ts +9 -0
  14. package/dist/src/commands/repair.d.ts.map +1 -0
  15. package/dist/src/commands/repair.js +170 -0
  16. package/dist/src/commands/repair.js.map +1 -0
  17. package/dist/src/commands/setup.d.ts +11 -0
  18. package/dist/src/commands/setup.d.ts.map +1 -0
  19. package/dist/src/commands/setup.js +127 -0
  20. package/dist/src/commands/setup.js.map +1 -0
  21. package/dist/src/commands/wallet.d.ts +9 -0
  22. package/dist/src/commands/wallet.d.ts.map +1 -0
  23. package/dist/src/commands/wallet.js +150 -0
  24. package/dist/src/commands/wallet.js.map +1 -0
  25. package/dist/src/db/get-pieces.d.ts +23 -0
  26. package/dist/src/db/get-pieces.d.ts.map +1 -0
  27. package/dist/src/db/get-pieces.js +84 -0
  28. package/dist/src/db/get-pieces.js.map +1 -0
  29. package/dist/src/db/get-providers-by-cid.d.ts +10 -0
  30. package/dist/src/db/get-providers-by-cid.d.ts.map +1 -0
  31. package/dist/src/db/get-providers-by-cid.js +45 -0
  32. package/dist/src/db/get-providers-by-cid.js.map +1 -0
  33. package/dist/src/db/get-repair-dataset.d.ts +9 -0
  34. package/dist/src/db/get-repair-dataset.d.ts.map +1 -0
  35. package/dist/src/db/get-repair-dataset.js +15 -0
  36. package/dist/src/db/get-repair-dataset.js.map +1 -0
  37. package/dist/src/db/get-repair-provider.d.ts +6 -0
  38. package/dist/src/db/get-repair-provider.d.ts.map +1 -0
  39. package/dist/src/db/get-repair-provider.js +28 -0
  40. package/dist/src/db/get-repair-provider.js.map +1 -0
  41. package/dist/src/db/get-target-dataset.d.ts +7 -0
  42. package/dist/src/db/get-target-dataset.d.ts.map +1 -0
  43. package/dist/src/db/get-target-dataset.js +27 -0
  44. package/dist/src/db/get-target-dataset.js.map +1 -0
  45. package/dist/src/db/repair-create.d.ts +7 -0
  46. package/dist/src/db/repair-create.d.ts.map +1 -0
  47. package/dist/src/db/repair-create.js +69 -0
  48. package/dist/src/db/repair-create.js.map +1 -0
  49. package/dist/src/db/repair-delete.d.ts +11 -0
  50. package/dist/src/db/repair-delete.d.ts.map +1 -0
  51. package/dist/src/db/repair-delete.js +22 -0
  52. package/dist/src/db/repair-delete.js.map +1 -0
  53. package/dist/src/db/repair-update.d.ts +10 -0
  54. package/dist/src/db/repair-update.d.ts.map +1 -0
  55. package/dist/src/db/repair-update.js +13 -0
  56. package/dist/src/db/repair-update.js.map +1 -0
  57. package/dist/src/db/sync-pieces-onchain.d.ts +10 -0
  58. package/dist/src/db/sync-pieces-onchain.d.ts.map +1 -0
  59. package/dist/src/db/sync-pieces-onchain.js +35 -0
  60. package/dist/src/db/sync-pieces-onchain.js.map +1 -0
  61. package/dist/src/db/update-operation.d.ts +11 -0
  62. package/dist/src/db/update-operation.d.ts.map +1 -0
  63. package/dist/src/db/update-operation.js +14 -0
  64. package/dist/src/db/update-operation.js.map +1 -0
  65. package/dist/src/db/upsert-operations.d.ts +8 -0
  66. package/dist/src/db/upsert-operations.d.ts.map +1 -0
  67. package/dist/src/db/upsert-operations.js +13 -0
  68. package/dist/src/db/upsert-operations.js.map +1 -0
  69. package/dist/src/error.d.ts +14 -0
  70. package/dist/src/error.d.ts.map +1 -0
  71. package/dist/src/error.js +27 -0
  72. package/dist/src/error.js.map +1 -0
  73. package/dist/src/indexer-schema.d.ts +549 -0
  74. package/dist/src/indexer-schema.d.ts.map +1 -0
  75. package/dist/src/indexer-schema.js +56 -0
  76. package/dist/src/indexer-schema.js.map +1 -0
  77. package/dist/src/local-schema.d.ts +456 -0
  78. package/dist/src/local-schema.d.ts.map +1 -0
  79. package/dist/src/local-schema.js +63 -0
  80. package/dist/src/local-schema.js.map +1 -0
  81. package/dist/src/middleware.d.ts +19 -0
  82. package/dist/src/middleware.d.ts.map +1 -0
  83. package/dist/src/middleware.js +28 -0
  84. package/dist/src/middleware.js.map +1 -0
  85. package/dist/src/pipeline/create-datasets.d.ts +10 -0
  86. package/dist/src/pipeline/create-datasets.d.ts.map +1 -0
  87. package/dist/src/pipeline/create-datasets.js +48 -0
  88. package/dist/src/pipeline/create-datasets.js.map +1 -0
  89. package/dist/src/pipeline/pull.d.ts +30 -0
  90. package/dist/src/pipeline/pull.d.ts.map +1 -0
  91. package/dist/src/pipeline/pull.js +169 -0
  92. package/dist/src/pipeline/pull.js.map +1 -0
  93. package/dist/src/types.d.ts +34 -0
  94. package/dist/src/types.d.ts.map +1 -0
  95. package/dist/src/types.js +1 -0
  96. package/dist/src/types.js.map +1 -0
  97. package/dist/src/utils.d.ts +11945 -0
  98. package/dist/src/utils.d.ts.map +1 -0
  99. package/dist/src/utils.js +121 -0
  100. package/dist/src/utils.js.map +1 -0
  101. package/package.json +135 -0
  102. package/readme.md +250 -0
  103. package/src/cli.ts +20 -0
  104. package/src/commands/datasets.ts +62 -0
  105. package/src/commands/providers.ts +99 -0
  106. package/src/commands/repair.ts +177 -0
  107. package/src/commands/setup.ts +142 -0
  108. package/src/commands/wallet.ts +159 -0
  109. package/src/db/get-pieces.ts +189 -0
  110. package/src/db/get-providers-by-cid.ts +75 -0
  111. package/src/db/get-repair-dataset.ts +44 -0
  112. package/src/db/get-repair-provider.ts +47 -0
  113. package/src/db/get-target-dataset.ts +47 -0
  114. package/src/db/repair-create.ts +101 -0
  115. package/src/db/repair-delete.ts +39 -0
  116. package/src/db/repair-update.ts +20 -0
  117. package/src/db/sync-pieces-onchain.ts +53 -0
  118. package/src/db/update-operation.ts +26 -0
  119. package/src/db/upsert-operations.ts +23 -0
  120. package/src/error.ts +33 -0
  121. package/src/indexer-schema.ts +77 -0
  122. package/src/local-schema.ts +91 -0
  123. package/src/middleware.ts +34 -0
  124. package/src/pipeline/create-datasets.ts +70 -0
  125. package/src/pipeline/pull.ts +255 -0
  126. package/src/types.ts +41 -0
  127. 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
+ }