Skip to content

Commit

Permalink
fix(ipns): fix handler response
Browse files Browse the repository at this point in the history
  • Loading branch information
gitwoz committed Nov 6, 2024
1 parent dc63cab commit 47c761d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
60 changes: 39 additions & 21 deletions handlers/refresh-ipns-gw3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,48 @@ import { Context, APIGatewayProxyResult, SQSEvent } from 'aws-lambda'

import { refreshIPNSFeed } from '../lib/refresh-ipns-gw3.js'

export const handler = async (
event: SQSEvent,
context: Context
): Promise<APIGatewayProxyResult> => {
const body = JSON.parse(event.Records[0].body) as {
userName?: string | string[]
limit?: number
concurrency?: number
forceReplace?: boolean
useMattersIPNS?: boolean
type EvenyPayload = {
userName?: string | string[]
limit?: number
concurrency?: number
forceReplace?: boolean
useMattersIPNS?: boolean
}

export const handler = async (event: SQSEvent) => {
console.log(event.Records)

const results = await Promise.allSettled(
event.Records.map(({ body }: { body: string }) =>
refreshIPNS(JSON.parse(body) as EvenyPayload)
)
)
// print failed reseaon
results.map((res) => {
if (res.status === 'rejected') {
console.error(res.reason)
}
})

// https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting
return {
batchItemFailures: results
.map((res, index) => {
if (res.status === 'rejected') {
return { itemIdentifier: event.Records[index].messageId }
}
})
.filter(Boolean),
}
console.log('event:', event)
}

const { limit = 50, concurrency = 5, forceReplace, useMattersIPNS } = body
const refreshIPNS = async (payload: EvenyPayload) => {
const { limit = 50, concurrency = 5, forceReplace, useMattersIPNS } = payload
let names: string[] = []
if (Array.isArray(body?.userName)) {
names = Array.from(body?.userName)
} else if (typeof body?.userName === 'string') {
names = body.userName.trim().split(/\s+/).filter(Boolean)
if (Array.isArray(payload?.userName)) {
names = Array.from(payload?.userName)
} else if (typeof payload?.userName === 'string') {
names = payload.userName.trim().split(/\s+/).filter(Boolean)
}

const data: any[] = []
Expand Down Expand Up @@ -49,11 +72,6 @@ export const handler = async (
}

// await purgeIPNS()

return {
statusCode: 200,
body: JSON.stringify({ message: 'done.', data }),
}
}

async function processUser(
Expand Down
44 changes: 12 additions & 32 deletions lib/refresh-ipns-gw3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class GW3Client {
method: 'POST',
headers: this.#authHeaders,
})
// console.log(new Date(), "addPin res:", res.ok, res.status, res.headers);
console.log(new Date(), 'addPin res:', res.ok, res.status)

return res.json()
}
Expand All @@ -66,7 +66,7 @@ class GW3Client {
method: 'POST',
headers: this.#authHeaders,
})
// console.log(new Date(), "rmPin res:", res.ok, res.status, res.headers);
console.log(new Date(), 'rmPin res:', res.ok, res.status)

return res.json()
}
Expand Down Expand Up @@ -94,7 +94,7 @@ class GW3Client {
method: 'GET',
headers: this.#authHeaders,
})
// console.log(new Date(), "getPin res:", res.ok, res.status, res.headers);
console.log(new Date(), 'getPin res:', res.ok, res.status)

return res.json()
}
Expand All @@ -106,7 +106,7 @@ class GW3Client {
headers: this.#authHeaders,
body: JSON.stringify({ cid, name }),
})
// console.log(new Date(), "getPin res:", res.ok, res.status, res.headers);
console.log(new Date(), 'getPin res:', res.ok, res.status)

return res.json()
}
Expand All @@ -132,14 +132,7 @@ class GW3Client {
method: 'GET',
headers: this.#authHeaders,
})
console.log(
new Date(),
'getDAG res:',
res.ok,
res.status,
res.statusText,
res.headers
)
console.log(new Date(), 'getDAG res:', res.ok, res.status, res.statusText)

return res.json()
}
Expand All @@ -159,9 +152,7 @@ class GW3Client {
'dag import res:',
res.ok,
res.status,
res.statusText,
res.headers
// await res.text()
res.statusText
)
const resImport = await res.json()
if (resImport?.code !== 200 || !resImport?.data?.url) {
Expand Down Expand Up @@ -212,7 +203,6 @@ class GW3Client {
res.ok,
res.status,
res.statusText,
res.headers,
await res.text()
)
return
Expand Down Expand Up @@ -250,7 +240,6 @@ class GW3Client {
res.ok,
res.status,
res.statusText,
res.headers,
reqBody
)
return res.json()
Expand All @@ -268,9 +257,8 @@ class GW3Client {
const u = new URL(
`https://gw3.io/api/v0/name/publish?key=${ipnsKey}&arg=${cid}&ts=${getTs()}`
)
if (pin === true || pin === false)
// not undefined
u.searchParams.set('pin', `${pin}`)
if (pin === true || pin === false) u.searchParams.set('pin', `${pin}`)

const res = await fetch(u, {
method: 'POST',
headers: this.#authHeaders,
Expand All @@ -282,7 +270,6 @@ class GW3Client {
res.ok,
res.status,
res.statusText,
res.headers,
{ ipnsKey, cid }
// reqBody
)
Expand Down Expand Up @@ -321,7 +308,6 @@ class GW3Client {
res.ok,
res.status,
res.statusText,
res.headers,
{ ipnsKey, cid, alias }
// reqBody
)
Expand All @@ -335,7 +321,7 @@ class GW3Client {
method: 'POST',
headers: this.#authHeaders,
})
// console.log(new Date(), "rmIPNS res:", res.ok, res.status, res.headers);
console.log(new Date(), 'rmIPNS res:', res.ok, res.status)

return res.json()
}
Expand All @@ -346,7 +332,7 @@ class GW3Client {
method: 'GET',
headers: this.#authHeaders,
})
console.log(new Date(), 'getIpns res:', res.ok, res.status, res.headers)
console.log(new Date(), 'getIpns res:', res.ok, res.status)

return res.json()
}
Expand All @@ -360,13 +346,7 @@ class GW3Client {
method: 'GET',
headers: this.#authHeaders,
})
console.log(
new Date(),
'getIpnsByName res:',
res.ok,
res.status,
res.headers
)
console.log(new Date(), 'getIpnsByName res:', res.ok, res.status)

return res.json()
}
Expand All @@ -377,7 +357,7 @@ class GW3Client {
method: 'GET',
headers: this.#authHeaders,
})
// console.log( new Date(), "get usage stats res:", res.ok, res.status, res.headers);
console.log(new Date(), 'get usage stats res:', res.ok, res.status)

return res.json()
}
Expand Down

0 comments on commit 47c761d

Please sign in to comment.