Skip to content

Commit

Permalink
Merge pull request #21 from charoitel/dev
Browse files Browse the repository at this point in the history
Maintenance for canvas@2.11.2
  • Loading branch information
charoitel authored Aug 19, 2023
2 parents 7af8874 + 3fce629 commit 43e8b9f
Show file tree
Hide file tree
Showing 62 changed files with 1,896 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.idea/
docs/.jekyll-cache
docs/_site
docs/Gemfile.lock
nodejs/node_modules
nodejs/node*/node_modules
nodejs/package*.json
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@

![GitHub](https://img.shields.io/github/license/charoitel/lambda-layer-canvas-nodejs)

[lambda-layer-canvas-nodejs](https://github.com/charoitel/lambda-layer-canvas-nodejs) published on [AWS Serverless Application Repository](https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:990551184979:applications~lambda-layer-canvas-nodejs) packages node-canvas and its dependencies as AWS Lambda Layer.
Canvas Layer for AWS Lambda is published and available on [AWS Serverless Application Repository](https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:990551184979:applications~lambda-layer-canvas-nodejs), and GitHub at [charoitel/lambda-layer-canvas-nodejs](https://github.com/charoitel/lambda-layer-canvas-nodejs). The layer aims to provide a Cairo backed Mozilla Web Canvas API implementation layer for AWS Lambda, powered by [node-canvas](https://github.com/Automattic/node-canvas).

## About node-canvas

[node-canvas](https://github.com/Automattic/node-canvas) is a Cairo backed Canvas implementation for Node.js. It implements the [Mozilla Web Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) as closely as possible. For the current API compliance, please check [Compatibility Status](https://github.com/Automattic/node-canvas/wiki/Compatibility-Status).
[node-canvas](https://github.com/Automattic/node-canvas) is a Cairo backed Canvas implementation for Node.js. It implements the [Mozilla Web Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) as closely as possible. For the latest API compliance, you may check [Compatibility Status](https://github.com/Automattic/node-canvas/wiki/Compatibility-Status).

## How this layer is built?

The Lambda Layer is built from source of node-canvas npm package on [amazonlinux](https://hub.docker.com/_/amazonlinux) dev container instance, with following native dependencies installed. Please check ``` build-layer.sh ``` for details.
The layer is built from source of node-canvas npm package on [amazonlinux](https://hub.docker.com/_/amazonlinux) dev container instance, with following native dependencies installed. You may check the build layer script, ``` build-layer.sh ```, which is available in repository, for details.

```bash
gcc-c++ cairo-devel pango-devel libjpeg-turbo-devel giflib-devel librsvg2-devel pango-devel bzip2-devel jq python3
```

Since AWS Lambda is a secure and isolated runtime and execution environment, this layer aims to target AWS Lambda compatible build. As there are canvas libraries and frameworks relying on node-canvas running on Node.js runtime, this layer also tries to include and support these libraries and frameworks.
Since AWS Lambda is a secure and isolated runtime and execution environment, the layer aims to target AWS Lambda compatible and native build. As there are canvas libraries and frameworks relying on node-canvas running on Node.js runtime, this layer may also try to include and support those libraries and frameworks. Currently, following libraries and frameworks are included when building and packaging the layer:

### Fabric.js support

[Fabric.js](https://github.com/fabricjs/fabric.js) is a framework that makes it easy to work with HTML5 canvas element. It is an interactive object model on top of canvas element. It is also an SVG-to-canvas (and canvas-to-SVG) parser.
- [Chart.js](#chartjs-support)
- [Fabric.js](#fabricjs-support)
- [Konva](#konva-support)

### Konva support
### Chart.js support

[Konva](https://github.com/konvajs/konva) is a framework that enables high performance animations, transitions, node nesting, layering, filtering, caching, event handling for desktop and mobile applications, and much more.
[Chart.js](https://github.com/chartjs/chart.js) provides a set of frequently used chart types, plugins, and customization options. In addition to a reasonable set of built-in chart types, there are also community-maintained chart types.

> **Note:** From Canvas Layer v2.9.3 release, since konva-node package is deprecated, konva package is used directly through build script.
### Fabric.js support

## Getting started
[Fabric.js](https://github.com/fabricjs/fabric.js) provides a missing and interactive object model for canvas, as well as an SVG parser, layer of interactivity, and a whole suite of other indispensable tools.

To get started, please visit https://github.com/charoitel/lambda-layer-canvas-nodejs/wiki/Getting-Started
### Konva support

Made with ❤️ by Charoite Lee and contributors. Available on the [AWS Serverless Application Repository](https://aws.amazon.com/serverless)
[Konva](https://github.com/konvajs/konva) enables high performance animations, transitions, node nesting, layering, filtering, caching, event handling for desktop and mobile applications, and much more.
7 changes: 5 additions & 2 deletions build-layer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ set -e

LAYER_NAME=canvas-nodejs
LAYER_DESCRIPTION="AWS Lambda Layer with node-canvas and its dependencies packaged, provides a Cairo backed Mozilla Web Canvas API implementation with additional features."
LAYER_VERSION=2.11.2
LAYER_VERSION=2.11.3
LAYER_AUTHOR="Charoite Lee"

DOT_CHAR="."
NODE_VERSION=$(node -v)
NODE_VERSION=${NODE_VERSION:1}
SEMVER_VERSION=7.5.4

# Remove packaged layer if exists
if [ -n "$(find . -name 'canvas-nodejs_v*.zip')" ]; then
Expand All @@ -36,13 +37,15 @@ npm init -y
npm install canvas --build-from-source
npm install fabric
npm install konva
npm install chart.js
npm install mocha --save-dev
jq --arg LAYER_NAME "$LAYER_NAME" --arg LAYER_DESCRIPTION "$LAYER_DESCRIPTION" --arg LAYER_VERSION "$LAYER_VERSION" --arg LAYER_AUTHOR "$LAYER_AUTHOR" '.name = $LAYER_NAME | .description = $LAYER_DESCRIPTION | .version = $LAYER_VERSION | .license = "MIT" | .author = $LAYER_AUTHOR | .scripts.test = "mocha"' package.json > package-tmp.json
jq --arg LAYER_NAME "$LAYER_NAME" --arg LAYER_DESCRIPTION "$LAYER_DESCRIPTION" --arg LAYER_VERSION "$LAYER_VERSION" --arg LAYER_AUTHOR "$LAYER_AUTHOR" --arg SEMVER_VERSION "$SEMVER_VERSION" '.name = $LAYER_NAME | .description = $LAYER_DESCRIPTION | .version = $LAYER_VERSION | .license = "MIT" | .author = $LAYER_AUTHOR | .scripts.test = "mocha" | .overrides.semver = $SEMVER_VERSION ' package.json > package-tmp.json
mv -f package-tmp.json package.json

# Test if installed modules and dependencies work fine
npm test
cp package-lock.json ..
npm rm mocha

# Prepare and package layer
mkdir node${NODE_VERSION%%$DOT_CHAR*}
Expand Down
99 changes: 99 additions & 0 deletions docs/00-setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
layout: page
title: Setup
permalink: /setup/
---

## Prerequisites

In order to start to use [lambda-layer-canvas-nodejs](https://github.com/charoitel/lambda-layer-canvas-nodejs) published on [AWS Serverless Application Repository](https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:990551184979:applications~lambda-layer-canvas-nodejs), you must have your AWS account with following services available:

- [AWS Serverless Application Repository](https://aws.amazon.com/serverless/serverlessrepo)
- [AWS Lambda](https://aws.amazon.com/lambda)

## Serverless application deployment

Once you have your AWS account ready, there are two ways to deploy [lambda-layer-canvas-nodejs](https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:990551184979:applications~lambda-layer-canvas-nodejs) and make it available in your AWS Lambda console:

- [Deploy through AWS Serverless Application Repository](#deploy-through-aws-serverless-application-repository)
- [Deploy through AWS Lambda console](#deploy-through-aws-lambda-console)

### Deploy through AWS Serverless Application Repository

1. Open https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:990551184979:applications~lambda-layer-canvas-nodejs
2. Click **Deploy** button
3. Login to your AWS account if you haven't login yet
4. Edit **_Name_** (Optional)
5. Click **Deploy** button
6. Deployment is started and in progress
7. Check your AWS Lambda console once the deployment is completed

### Deploy through AWS Lambda console

1. Login to your AWS account and open your AWS Lambda console
2. Click **Create application** button
3. Select **_Serverless application_**
4. Input `lambda-layer-canvas-nodejs` into search box and press _Enter_ key
5. Click on the title of `lambda-layer-canvas-nodejs` card
6. Edit **_Name_** (Optional)
7. Click **Deploy** button
8. Deployment is started and in progress
9. Check your AWS Lambda console once the deployment is completed

### Using canvas layer

After the deployment is completed, you may refer [usage example](/lambda-layer-canvas-nodejs/{% link 01-use.md %}) and follow the example where a Lambda function uses the canvas layer to generate PNG graphic with colored text and circle rendered.

## Setup environment to build the layer

Alternately, you may setup your own environment to build the layer according to your specific needs. When using the layer with nodejs-18.x in Amazon Linux, it requires glibc-2.28, meanwhile, compiling glibc-2.28 requires make-4.x or later[^1].

```console
$ wget https://ftp.gnu.org/gnu/make/make-4.3.tar.gz
$ tar -xzvf make-4.3.tar.gz
$ cd make-4.3/
$ ./configure --prefix=/usr
$ make
$ make install
```
Once make-4.x or later is ready, we may start to compiling glibc-2.28 on Amazon Linux. However, during `make install` there would be an error due to `cannot found -lnss_test2` which could be ignored[^2].

```console
$ wget https://ftp.gnu.org/gnu/glibc/glibc-2.28.tar.gz
$ tar -xzvf glibc-2.28.tar.gz
$ cd glibc-2.28
$ mkdir build && cd build
$ ../configure --prefix=/usr --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
$ make
$ make install
...
/usr/bin/ld: cannot find -lnss_test2
collect2: error: ld returned 1 exit status
Execution of gcc -B/usr/bin/ failed!
...
```

To test if the installation of the compiled glibc-2.28 is success or not, we may check if `GLIBC_2.28` is enabled[^1] [^2].

```console
$ ls -l /lib64/libc.so.6
lrwxrwxrwx 1 root root 12 Jul 3 15:14 /lib64/libc.so.6 -> libc-2.28.so
$ strings /lib64/libc.so.6 | grep GLIBC
...
GLIBC_2.22
GLIBC_2.23
GLIBC_2.24
GLIBC_2.25
GLIBC_2.26
GLIBC_2.27
GLIBC_2.28
GLIBC_PRIVATE
...
```

Once your environment is ready, you may execute the build layer script, ``` build-layer.sh ```, to build the layer and deploy through the AWS Lambda console.

---

[^1]: [Centos 7 升级 Glibc-2.28](https://cloud.tencent.com/developer/article/2021784)
[^2]: [CentOS 7.6 编译安装最新版本glibc2.30 实录](https://www.jianshu.com/p/1070373a50f6)
Loading

0 comments on commit 43e8b9f

Please sign in to comment.