diff --git a/handlers/refresh-ipns-gw3.ts b/handlers/refresh-ipns-gw3.ts index db37e61..22283b7 100644 --- a/handlers/refresh-ipns-gw3.ts +++ b/handlers/refresh-ipns-gw3.ts @@ -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 => { - 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[] = [] @@ -49,11 +72,6 @@ export const handler = async ( } // await purgeIPNS() - - return { - statusCode: 200, - body: JSON.stringify({ message: 'done.', data }), - } } async function processUser( diff --git a/lib/refresh-ipns-gw3.ts b/lib/refresh-ipns-gw3.ts index c60f5f1..014fda6 100644 --- a/lib/refresh-ipns-gw3.ts +++ b/lib/refresh-ipns-gw3.ts @@ -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() } @@ -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() } @@ -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() } @@ -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() } @@ -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() } @@ -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) { @@ -212,7 +203,6 @@ class GW3Client { res.ok, res.status, res.statusText, - res.headers, await res.text() ) return @@ -250,7 +240,6 @@ class GW3Client { res.ok, res.status, res.statusText, - res.headers, reqBody ) return res.json() @@ -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, @@ -282,7 +270,6 @@ class GW3Client { res.ok, res.status, res.statusText, - res.headers, { ipnsKey, cid } // reqBody ) @@ -321,7 +308,6 @@ class GW3Client { res.ok, res.status, res.statusText, - res.headers, { ipnsKey, cid, alias } // reqBody ) @@ -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() } @@ -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() } @@ -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() } @@ -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() }