From 4ef2d464138b07e02014f190c1be8bdcbd090e92 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Tue, 4 Jul 2023 20:05:51 +0200 Subject: [PATCH] Set serverless examples to return directly the pass instead of uploading them on S3 --- examples/serverless/README.md | 16 +++++----- examples/serverless/src/functions/pkpasses.ts | 9 ++++-- examples/serverless/src/shared.ts | 30 +++++++++---------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/examples/serverless/README.md b/examples/serverless/README.md index 80db20a..af4bfdc 100644 --- a/examples/serverless/README.md +++ b/examples/serverless/README.md @@ -49,11 +49,11 @@ Serverless will start, by default, on `0.0.0.0:8080`. All the examples, except fields ones, require a `modelName` to be passed in queryString. The name will be checked against local FS or S3 bucket if example is deployed. Pass in queryString all the pass props you want to apply them to the final result. -| Example name | Endpoint name | Additional notes | -| -------------- | ----------------- | ----------------------------------------------------------------------------------------------------- | -| localize | `/localize` | - | -| fields | `/fields` | - | -| expirationDate | `/expirationDate` | - | -| scratch | `/scratch` | - | -| barcodes | `/barcodes` | Using `?alt=true` query parameter, will lead to barcode string message usage instead of selected ones | -| pkpasses | `/pkpasses` | - | +| Example name | Endpoint name | Additional notes | +| -------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | +| localize | `/localize` | - | +| fields | `/fields` | - | +| expirationDate | `/expirationDate` | - | +| scratch | `/scratch` | - | +| barcodes | `/barcodes` | Using `?alt=true` query parameter, will lead to barcode string message usage instead of selected ones | +| pkpasses | `/pkpasses` | This example shows how to upload the pkpasses file on S3, even if it is discouraged. It has been done just to share the knowledge | diff --git a/examples/serverless/src/functions/pkpasses.ts b/examples/serverless/src/functions/pkpasses.ts index a2e7954..a8a38b8 100644 --- a/examples/serverless/src/functions/pkpasses.ts +++ b/examples/serverless/src/functions/pkpasses.ts @@ -120,6 +120,11 @@ export async function pkpasses(event: ALBEvent) { const pkpasses = PKPass.pack(...passes); + /** + * Although the other passes are served as files, in this example + * we are uploading on s3 (local) just see how it works. + */ + const buffer = pkpasses.getAsBuffer(); const passName = `GeneratedPass-${Math.random()}.pkpasses`; @@ -136,7 +141,7 @@ export async function pkpasses(event: ALBEvent) { /** * Please note that redirection to `Location` does not work - * if you open this code in another device if this is run + * if you open this code in another device if this is is running * offline. This because `Location` is on localhost. Didn't * find yet a way to solve this. */ @@ -145,7 +150,7 @@ export async function pkpasses(event: ALBEvent) { statusCode: 302, headers: { "Content-Type": "application/vnd.apple.pkpass", - Location: Location, + Location, }, }; } diff --git a/examples/serverless/src/shared.ts b/examples/serverless/src/shared.ts index 44ee71b..411906b 100644 --- a/examples/serverless/src/shared.ts +++ b/examples/serverless/src/shared.ts @@ -148,7 +148,9 @@ export async function* createPassGenerator( passOptions?: Object, ): AsyncGenerator { const [template, certificates, s3] = await Promise.all([ - modelName ? getModel(modelName) : Promise.resolve({}), + modelName + ? getModel(modelName) + : Promise.resolve({} as ReturnType), getCertificates(), getS3Instance(), ]); @@ -175,18 +177,6 @@ export async function* createPassGenerator( pass = yield pass; const buffer = pass.getAsBuffer(); - const passName = `GeneratedPass-${Math.random()}.pkpass`; - - const { Location } = await s3 - .upload({ - Bucket: config.PASSES_S3_TEMP_BUCKET, - Key: passName, - ContentType: pass.mimeType, - /** Marking it as expiring in 5 minutes, because passes should not be stored */ - Expires: new Date(Date.now() + 5 * 60 * 1000), - Body: buffer, - }) - .promise(); /** * Please note that redirection to `Location` does not work @@ -196,10 +186,18 @@ export async function* createPassGenerator( */ return { - statusCode: 302, + statusCode: 200, headers: { - "Content-Type": "application/vnd.apple.pkpass", - Location: Location, + "Content-Type": pass.mimeType, }, + /** + * It is required for the file to be served + * as base64, so it won't be altered in AWS. + * + * @see https://aws.amazon.com/it/blogs/compute/handling-binary-data-using-amazon-api-gateway-http-apis/ + * "For the response path, API Gateway inspects the isBase64Encoding flag returned from Lambda." + */ + body: buffer.toString("base64"), + isBase64Encoded: true, }; }