Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release #180

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Maxime Bret <[email protected]>",
"license": "MIT",
"engines": {
"node": "18.x"
"node": "20.x"
},
"workspaces": [
"packages/*"
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"postinstall": "rm -rf node_modules/serverless"
},
"engines": {
"node": "18.x"
"node": "20.x"
},
"dependencies": {
"node-unrar-js": "^2.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/functions/corsProxy/handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fetch, { AbortError } from "node-fetch"
import { createHttpError } from "@libs/httpErrors"
import { withMiddy } from "@libs/lambda"
import { APIGatewayProxyEvent } from "aws-lambda"
import AbortController from "abort-controller"
import { withMiddy } from "@libs/middy/withMiddy"

const lambda = async (event: APIGatewayProxyEvent) => {
const params = event.queryStringParameters
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/functions/covers/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { withMiddy } from "@libs/lambda"
import { S3Client } from "@aws-sdk/client-s3"
import sharp from "sharp"
import { getCover } from "./getCover"
import { getCoverPlaceholder } from "./getCoverPlaceholder"
import createError from "http-errors"
import { withMiddy } from "@libs/middy/withMiddy"

const s3 = new S3Client({
region: `us-east-1`
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/functions/refreshMetadata/handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { getAwsLambda, withMiddy } from "@libs/lambda"
import { getAwsLambda } from "@libs/lambda"
import { getNormalizedHeader } from "@libs/utils"
import schema from "./schema"
import { InvokeCommand } from "@aws-sdk/client-lambda"
import { STAGE } from "src/constants"
import { lock } from "@libs/supabase/lock"
import { Logger } from "@libs/logger"
import { withMiddy } from "@libs/middy/withMiddy"

const LOCK_MAX_DURATION_MN = 5

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { getAwsLambda, withMiddy } from "@libs/lambda"
import { getAwsLambda } from "@libs/lambda"
import { getNormalizedHeader } from "@libs/utils"
import schema from "./schema"
import { InvokeCommand } from "@aws-sdk/client-lambda"
import { STAGE } from "src/constants"
import { COLLECTION_METADATA_LOCK_MN } from "@oboku/shared"
import { lock } from "@libs/supabase/lock"
import { Logger } from "@libs/logger"
import { withMiddy } from "@libs/middy/withMiddy"

const logger = Logger.child({ module: "handler" })

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { withMiddy } from "@libs/lambda"
import { withToken } from "@libs/auth"
import { configure as configureGoogleDataSource } from "@libs/plugins/google"
import schema from "./schema"
Expand All @@ -9,6 +8,7 @@ import { deleteLock } from "@libs/supabase/deleteLock"
import { supabase } from "@libs/supabase/client"
import { Logger } from "@libs/logger"
import { refreshMetadata } from "./src/refreshMetadata"
import { withMiddy } from "@libs/middy/withMiddy"

const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
event
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { withMiddy } from "@libs/lambda"
import fs from "fs"
import path from "path"
import { OFFLINE, TMP_DIR } from "../../constants"
Expand All @@ -13,6 +12,7 @@ import { deleteLock } from "@libs/supabase/deleteLock"
import { supabase } from "@libs/supabase/client"
import { Logger } from "@libs/logger"
import { retrieveMetadataAndSaveCover } from "./src/retrieveMetadataAndSaveCover"
import { withMiddy } from "@libs/middy/withMiddy"

const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
event
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/functions/requestAccess/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { createHttpError } from "@libs/httpErrors"
import { withMiddy } from "@libs/lambda"
import schema from "./schema"
import nodemailer from "nodemailer"
import { CONTACT_TO_ADDRESS } from "../../constants"
import { GetParameterCommand, SSMClient } from "@aws-sdk/client-ssm"
import { withMiddy } from "@libs/middy/withMiddy"

const ssm = new SSMClient({ region: "us-east-1" })

Expand Down
1 change: 0 additions & 1 deletion packages/api/src/functions/signin/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* couchdb admin directly or probably by completely pruning db data.
*/
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { withMiddy } from "@libs/lambda"
import schema from "./schema"
import { initializeApp } from "firebase-admin/app"
import { getAuth } from "firebase-admin/auth"
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/functions/syncDataSource/handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { getAwsLambda, withMiddy } from "@libs/lambda"
import { getAwsLambda } from "@libs/lambda"
import { getNormalizedHeader } from "@libs/utils"
import { STAGE } from "../../constants"
import schema from "./schema"
import { InvokeCommand } from "@aws-sdk/client-lambda"
import { lock } from "@libs/supabase/lock"
import { Logger } from "@libs/logger"
import { withMiddy } from "@libs/middy/withMiddy"

const LOCK_MAX_DURATION_MN = 10

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { withMiddy } from "@libs/lambda"
import { AWS_API_URI } from "../../constants"
import { configure as configureGoogleDataSource } from "@libs/plugins/google"
import { withToken } from "@libs/auth"
Expand All @@ -12,6 +11,7 @@ import { deleteLock } from "@libs/supabase/deleteLock"
import { supabase } from "@libs/supabase/client"
import { pluginFacade } from "@libs/plugins/facade"
import { Logger } from "@libs/logger"
import { withMiddy } from "@libs/middy/withMiddy"

const logger = Logger.child({ module: "handler" })

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/functions/syncReports/handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import { withToken } from "@libs/auth"
import { withMiddy } from "@libs/lambda"
import schema from "./schema"
import { getParametersValue } from "@libs/ssm"
import { supabase } from "@libs/supabase/client"
import { withMiddy } from "@libs/middy/withMiddy"

const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
event
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/libs/httpErrors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ObokuErrorCode } from "@oboku/shared"
import createError from "http-errors"

type ErrorEntry = { code: ObokuErrorCode }
type ErrorEntry = { code: ObokuErrorCode; message?: string }
type ErrorEntries = ErrorEntry[]

export const createHttpError = (
Expand Down
101 changes: 0 additions & 101 deletions packages/api/src/libs/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,6 @@
import middy from "@middy/core"
import { jsonSafeParse } from "@middy/util"
import middyJsonBodyParser from "@middy/http-json-body-parser"
import httpErrorHandler from "@middy/http-error-handler"
import httpHeaderNormalizer from "@middy/http-header-normalizer"
import cors from "@middy/http-cors"
import { OFFLINE } from "../constants"
import { transpileSchema } from "@middy/validator/transpile"
import validator from "@middy/validator"
import { LambdaClient } from "@aws-sdk/client-lambda"

export const withMiddy = (
handler: any,
{
withCors = true,
withJsonBodyParser = true,
schema = {}
}: {
/**
* cors middleware only support REST / HTTP format. Some lambda are invoked from
* others lambda and therefore does not comply to the right format. These lambda
* can skip cors
*/
withCors?: boolean
withJsonBodyParser?: boolean
schema?: Parameters<typeof transpileSchema>[0]
} = {}
) => {
const noop = {
before: () => {}
}

return (
middy(handler)
/**
* Some lambda are invoked from others lambda and therefore does not comply to the right format. These lambda.
* We make sure to have headers so the middy json does not fail
*/
.use({
before: (request) => {
if (!request.event.headers) {
request.event.headers = {}
}
}
})
.use(httpHeaderNormalizer())
.use(withJsonBodyParser ? middyJsonBodyParser({}) : noop)
.use(
validator({
eventSchema: transpileSchema(schema)
})
)
/**
* middy onError order changed and cors needs to be before to be executed after.
* Only for onError which is why it's duplicated below as well...
*/
.use({
onError: withCors
? cors({
headers: `*`
}).onError
: () => {
//
}
})
.use(
httpErrorHandler({
// handle non http error with 500 and generic message
fallbackMessage: `An error occurred`
})
)
.use({
onError: async (request) => {
if (request.error) {
console.error("error received", request.error)
}
// we enforce non exposure unless specified
if (request.error && (request.error as any)?.expose === undefined) {
// eslint-disable-next-line no-extra-semi
;(request.error as any).expose = false
}

// we force JSON response for any error that is a simple string
if (
request.error &&
typeof jsonSafeParse(request.error.message) === `string`
) {
request.error.message = JSON.stringify({
message: request.error.message
})
}
}
})
// @todo eventually protect the api and only allow a subset of origins
.use(
withCors
? cors({
headers: `*`
})
: noop
)
)
}

export const getAwsLambda = () =>
new LambdaClient({
region: "us-east-1",
Expand Down
99 changes: 99 additions & 0 deletions packages/api/src/libs/middy/withMiddy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import middy from "@middy/core"
import { jsonSafeParse } from "@middy/util"
import middyJsonBodyParser from "@middy/http-json-body-parser"
import httpErrorHandler from "@middy/http-error-handler"
import httpHeaderNormalizer from "@middy/http-header-normalizer"
import cors from "@middy/http-cors"
import { transpileSchema } from "@middy/validator/transpile"
import validator from "@middy/validator"
import { createHttpError } from "@libs/httpErrors"
import { ObokuErrorCode } from "@oboku/shared"

export const withMiddy = (
handler: any,
{
withCors = true,
withJsonBodyParser = true,
schema = {}
}: {
/**
* cors middleware only support REST / HTTP format. Some lambda are invoked from
* others lambda and therefore does not comply to the right format. These lambda
* can skip cors
*/
withCors?: boolean
withJsonBodyParser?: boolean
schema?: Parameters<typeof transpileSchema>[0]
} = {}
) => {
const noop = {
before: () => {}
}

return (
middy(handler)
/**
* Some lambda are invoked from others lambda and therefore does not comply to the right format. These lambda.
* We make sure to have headers so the middy json does not fail
*/
.use({
before: (request) => {
if (!request.event.headers) {
request.event.headers = {}
}
}
})
.use(httpHeaderNormalizer())
.use(withJsonBodyParser ? middyJsonBodyParser({}) : noop)
.use(
validator({
eventSchema: transpileSchema(schema)
})
)
/**
* middy onError order changed and cors needs to be before to be executed after.
* Only for onError which is why it's duplicated below as well...
*/
.use({
onError: withCors
? cors({
headers: `*`
}).onError
: () => {
//
}
})
.use(
httpErrorHandler({
fallbackMessage: `An error occurred`
})
)
.use({
onError: async (request) => {
if (request.error) {
console.error("error received", request.error)
}

if (request.error) {
request.error = createHttpError(500, {
code: ObokuErrorCode.UNKNOWN,
message: request.error.message
})

// eslint-disable-next-line no-extra-semi
;(request.error as any).expose =
(request.error as any)?.expose ??
process.env.NODE_ENV === "development"
}
}
})
// @todo eventually protect the api and only allow a subset of origins
.use(
withCors
? cors({
headers: `*`
})
: noop
)
)
}
Loading
Loading