Skip to content

Commit

Permalink
Merge pull request #4200 from thematters/feat/deprecate-ipfs
Browse files Browse the repository at this point in the history
feat(ipfs): remove unused ipfs envs and fix type
  • Loading branch information
robertu7 authored Oct 25, 2024
2 parents 605c6b2 + 6778047 commit 9f98821
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 28 deletions.
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ MATTERS_MATTY_ID=6
MATTERS_MATTY_CHOICE_TAG_ID=3906
MATTERS_EMAIL_FROM_ASK=Matters<[email protected]>

# the IPFS_HOST & IPFS_PORT will be deprecated
MATTERS_IPFS_HOST=ipfs
MATTERS_IPFS_PORT=5001
MATTERS_IPFS_SERVERS="http://ipfs.dev.vpc:5001, http://10.0.0.169:5001"

MATTERS_SENTRY_DSN=
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ jobs:
MATTERS_JWT_SECRET: QsNmu9
MATTERS_ELASTICSEARCH_HOST: localhost
MATTERS_ELASTICSEARCH_PORT: 9200
MATTERS_IPFS_HOST: localhost
MATTERS_IPFS_PORT: 5001
MATTERS_IPFS_SERVERS: 'http://localhost:5001'
MATTERS_OICD_PRIVATE_KEY: .ebextensions/oicd_rsa_private_local.pem
MATTERS_STRIPE_SECRET: sk_test_foobar
MATTERS_SENDGRID_API_KEY: SG.0-_abcabcabc.
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
- name: Test
run: npm run test
env:
NODE_OPTIONS: "--no-experimental-fetch"
NODE_OPTIONS: '--no-experimental-fetch'
CODECOV_TOKEN: de5ab681-0837-4a24-b614-0a29225a7e4c
MATTERS_ENV: test
MATTERS_LOGGING_LEVEL: critical
Expand All @@ -80,8 +80,7 @@ jobs:
MATTERS_JWT_SECRET: QsNmu9
MATTERS_ELASTICSEARCH_HOST: localhost
MATTERS_ELASTICSEARCH_PORT: 9200
MATTERS_IPFS_HOST: localhost
MATTERS_IPFS_PORT: 5001
MATTERS_IPFS_SERVERS: 'http://localhost:5001'
MATTERS_OICD_PRIVATE_KEY: .ebextensions/oicd_rsa_private_local.pem
MATTERS_STRIPE_SECRET: sk_test_foobar
MATTERS_SENDGRID_API_KEY: SG.0-_abcabcabc.
Expand Down
2 changes: 0 additions & 2 deletions src/common/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ export const environment = {
process.env.MATTERS_SEARCH_PG_TAG_COEFFICIENTS || '[1,1,1,1]'
),

ipfsHost: process.env.MATTERS_IPFS_HOST || '',
ipfsPort: process.env.MATTERS_IPFS_PORT || '5001',
ipfsServers: process.env.MATTERS_IPFS_SERVERS || '',
queueHost: process.env.MATTERS_QUEUE_HOST as string,
queuePort: (process.env.MATTERS_QUEUE_PORT || 6379) as number,
Expand Down
13 changes: 7 additions & 6 deletions src/connectors/articleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,11 +875,12 @@ export class ArticleService extends BaseService<Article> {
try {
const results = []
for await (const result of ipfs.addAll(
bundle.map((file) =>
file
? { ...file, path: `${directoryName}/${file.path}` }
: undefined
)
bundle
.filter((file): file is NonNullable<typeof file> => !!file)
.map((file) => ({
...file,
path: `${directoryName}/${file.path}`,
}))
)) {
results.push(result)
}
Expand Down Expand Up @@ -1247,7 +1248,7 @@ export class ArticleService extends BaseService<Article> {
[articleId, APPRECIATION_PURPOSE.appreciateSubsidy],
]
)
.sum('amount', { as: 'sum' })
.sum('amount as sum')
.first()
return parseInt(result?.sum || '0', 10)
}
Expand Down
21 changes: 9 additions & 12 deletions src/connectors/ipfs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { generateKeyPair } from 'crypto'
import FormData from 'form-data'
import { create } from 'ipfs-http-client'
import { create, IPFSHTTPClient } from 'ipfs-http-client'
import fetch from 'node-fetch'
import { Readable } from 'stream'
import { promisify } from 'util'
Expand All @@ -12,41 +12,38 @@ const logger = getLogger('service-ipfs')

const generateKeyPairPromisified = promisify(generateKeyPair)

const ipfsServerUrls = (
environment.ipfsServers ||
`http://${environment.ipfsHost}:${environment.ipfsPort}/api/v0`
)
const ipfsServerUrls = environment.ipfsServers
.trim()
.split(/,\s*/)
.filter(Boolean)

// In-App load-balancer, instead of transparent EC2 load balancer
export class IPFSServer {
// 1 active primary + multiple backup secondaries
clients: any[]
public clients: IPFSHTTPClient[]

constructor() {
public constructor() {
this.clients = ipfsServerUrls.map((url) => create({ url }))
}

get size() {
public get size() {
return this.clients.length
}
get client() {
public get client() {
// const idx = active ? 0 : Math.floor(1 + Math.random() * (this.size - 1))
return this.clients[0]
}
get backupClient() {
public get backupClient() {
const idx = Math.floor(1 + Math.random() * (this.size - 1))
return this.clients[idx]
}

// same as `openssl genpkey -algorithm ED25519`
genKey = async () => generateKeyPairPromisified('ed25519') // {
public genKey = async () => generateKeyPairPromisified('ed25519') // {

// JS implementation of IPFS KEY is incompatible between JS-IPFS vs Go-IPFS (Kubo)
// https://github.com/ipfs/js-ipfs/issues/3547
importKey = async ({
public importKey = async ({
name,
pem,
useActive = true,
Expand Down

0 comments on commit 9f98821

Please sign in to comment.