Set serverless examples to return directly the pass instead of uploading them on S3

This commit is contained in:
Alexander Cerutti
2023-07-04 20:05:51 +02:00
parent e9fcf4b95b
commit 4ef2d46413
3 changed files with 29 additions and 26 deletions

View File

@@ -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 |

View File

@@ -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,
},
};
}

View File

@@ -148,7 +148,9 @@ export async function* createPassGenerator(
passOptions?: Object,
): AsyncGenerator<PKPass, ALBResult, PKPass> {
const [template, certificates, s3] = await Promise.all([
modelName ? getModel(modelName) : Promise.resolve({}),
modelName
? getModel(modelName)
: Promise.resolve({} as ReturnType<typeof getModel>),
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,
};
}