Skip to content

Commit

Permalink
update pixlet and add version params (#17)
Browse files Browse the repository at this point in the history
* update pixlet and add version param

* update pixlet version on pixlet readme

* add version check details and link to readme
  • Loading branch information
btjones authored Apr 18, 2022
1 parent ad99f08 commit 279b98e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 25 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ _note: all query parameters are optional_
| output | `html` | Output type returned by the API. `html`, `image`, or `base64`|
| applet | _AXILLA_ | URL of a Pixlet applet (which should be a [Starlark](https://github.com/bazelbuild/starlark) `.star` file.). Maximum file size 10MB. |

### Special Parameters

_note: when present, other params are ignored and an applet is not executed_

| Parameter | Default | Description |
|-----------|-------------|-------------|
| version | `undefined` | When `true` returns Axilla and Pixlet version information as plain text. |

## Applet Query Parameters

Query parameters can be passed to Pixlet for use within an applet (i.e. `config.get('param')`). These parameters are defined by the applet itself. _Note: Axilla query parameters listed above are reserved and will not be passed to the applet._
Expand Down
67 changes: 47 additions & 20 deletions functions/axilla/axilla.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const util = require('util')
const fetch = require('node-fetch')
const execFile = util.promisify(require('child_process').execFile)

const PARAMS = [
const RESERVERD_PARAMS = [
'format',
'output',
'applet',
'version',
]

const FORMATS = {
Expand Down Expand Up @@ -36,7 +37,16 @@ const DEFAULT_APPLET_PATH = path.join(ASSETS_PATH, 'default.star')
const INPUT_APPLET_PATH = path.join(TMP_PATH, 'input.star')
const HTML_TEMPLATE_PATH = path.join(ASSETS_PATH, 'basic.html')

// executes pixlet with the given arguments
const executePixlet = async (args) => {
const command = PIXLET_BINARY_PATH ? path.join(PIXLET_BINARY_PATH, PIXLET_BINARY) : PIXLET_BINARY
const opts = LD_LIBRARY_PATH ? { env: { LD_LIBRARY_PATH } } : undefined
return execFile(command, args, opts)
}

// helper functions
const getOutputPath = (format) => path.join(TMP_PATH, `output.${format}`)
const getPixletVersion = async () => (await executePixlet(['version'])).stdout

exports.handler = async (event) => {
// query params
Expand All @@ -45,7 +55,39 @@ exports.handler = async (event) => {
const appletPath = appletUrl ? INPUT_APPLET_PATH : DEFAULT_APPLET_PATH
const format = (params.format && FORMATS[params.format.toUpperCase()]) || FORMATS.WEBP
const output = (params.output && OUTPUTS[params.output.toUpperCase()]) || OUTPUTS.HTML
console.log('params', params) // eslint-disable-line no-console
const isVersionRequest = params.version === 'true'

// setup pixlet
const outputPath = getOutputPath(format)
const args = ['render', appletPath, `--output=${outputPath}`]
if (format === FORMATS.GIF) {
args.push('--gif=true')
}

// return the pixlet version when the `version` param is true
if (isVersionRequest) {
try {
const pixletVersion = await getPixletVersion()
return {
statusCode: 200,
headers: { 'content-type': 'text/plain' },
body: pixletVersion,
}
} catch (error) {
return {
statusCode: 500,
body: `Error: Could not get version info. ${error.message}`,
}
}
}

// pass non-reserved params to pixlet
// don't allow params that begin with `-`
Object.keys(params).forEach((key) => {
if (!RESERVERD_PARAMS.includes(key) && key.charAt(0) !== '-') {
args.push(`${key}=${params[key]}`)
}
})

// download the applet if provided
if (!!appletUrl) {
Expand All @@ -71,26 +113,9 @@ exports.handler = async (event) => {
}
}

// setup pixlet
const command = PIXLET_BINARY_PATH ? path.join(PIXLET_BINARY_PATH, PIXLET_BINARY) : PIXLET_BINARY
const opts = LD_LIBRARY_PATH ? { env: { LD_LIBRARY_PATH } } : undefined
const outputPath = getOutputPath(format)
const args = ['render', appletPath, `--output=${outputPath}`]
if (format === FORMATS.GIF) {
args.push('--gif=true')
}

// pass non-reserved params to pixlet
// don't allow params that begin with `-`
Object.keys(params).forEach((key) => {
if (!PARAMS.includes(key) && key.charAt(0) !== '-') {
args.push(`${key}=${params[key]}`)
}
})

// run pixlet
try {
await execFile(command, args, opts)
await executePixlet(args)
} catch (error) {
const appletMessage = !!appletUrl ? 'Ensure the provided applet is valid.' : ''
return {
Expand Down Expand Up @@ -169,6 +194,8 @@ exports.handler = async (event) => {

// for use with unit tests
exports.test = {
executePixlet,
getPixletVersion,
getOutputPath,
INPUT_APPLET_PATH,
}
6 changes: 5 additions & 1 deletion functions/axilla/pixlet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## `pixlet-aws`

Version: 0.17.0 (commit: [aaf1fd4](https://github.com/tidbyt/pixlet/commit/aaf1fd45e6ff83d1e590b82787090e7b6c3f5d3d))
Version: 0.17.2 (commit: [2fd1466](https://github.com/tidbyt/pixlet/commit/2fd1466d2ca6a4c9757a1d6b3c9edbf7989a24bd))

`pixlet-aws` is [built from source](https://github.com/tidbyt/pixlet/blob/2eaa1e34257b954a778a7878e21d9837e3befb52/BUILD.md) using an Amazon EC2 instance.

Expand All @@ -11,3 +11,7 @@ Version: 0.17.0 (commit: [aaf1fd4](https://github.com/tidbyt/pixlet/commit/aaf1f
Version: 0.8.2 ([official release](https://github.com/tidbyt/pixlet/releases))

`pixlet-github` uses an [official pixlet linux_amd64 release](https://github.com/tidbyt/pixlet/releases). Note: Versions newer than 0.8.2 fail to execute within Github Actions (Ubuntu 20.04 aka `ubuntu-latest`). Axilla unit tests currently don't require a newer version of `pixlet` so this is ok for now.

## Check Current Version

Axilla also accepts a `version` param which when set to `true` will return the Pixlet version being used. [`axilla.netlify.app/?version=true`](https://axilla.netlify.app/?version=true)
Binary file modified functions/axilla/pixlet/pixlet-aws
Binary file not shown.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "A web-based pixlet implementation",
"main": "",
"scripts": {
"dev": "PIXLET_BINARY=pixlet netlify dev",
"dev": "PIXLET_BINARY=pixlet IS_DEV=true netlify dev",
"lint": "eslint --ext .js ./",
"test": "PIXLET_BINARY=pixlet jest",
"test:watch": "PIXLET_BINARY=pixlet jest --watch",
"test": "PIXLET_BINARY=pixlet IS_DEV=true jest",
"test:watch": "PIXLET_BINARY=pixlet IS_DEV=true jest --watch",
"test:ci": "jest"
},
"repository": {
Expand Down
34 changes: 33 additions & 1 deletion test/axilla.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('axilla', () => {

describe('defaults', () => {

it('returns defaults when no parametrs are provided', async () => {
it('returns defaults when no parameters are provided', async () => {
await lambdaTester(handler)
.event(getEvent())
.expectResolve((result) => {
Expand Down Expand Up @@ -358,4 +358,36 @@ describe('axilla', () => {
it.todo('Could not generate html')
})

describe('version', () => {

// we currently use an older version of pixlet for our github workflow
// the old version of pixlet does not support the "version" param
// if we upgrade pixlet for the github workflow, we can run these there
if (process.env.IS_DEV) {

it('getPixletVersion returns formatted version info', async () => {
const pixletVersion = await test.getPixletVersion()
expect(pixletVersion).toBeDefined()
expect(pixletVersion.length).toBeGreaterThan(0)
})

it('returns version info when the "version" param is "true"', async () => {
await lambdaTester(handler)
.event(getEvent({ version: 'true' }))
.expectResolve((result) => {
expect(result.statusCode).toEqual(200)
expect(result.headers['content-type']).toEqual('text/plain')
expect(result.body.indexOf('Pixlet version:')).toBeGreaterThan(-1)
})
})

} else {

it.todo('getPixletVersion returns formatted version info (requires pixlet update)')
it.todo('returns version info when the "version" param is "true" (requires pixlet update)')

}

})

})

0 comments on commit 279b98e

Please sign in to comment.