Skip to content

Commit

Permalink
feat: prepare release (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-charles-chan committed Jan 18, 2023
1 parent 84a5db0 commit 705cab1
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 54 deletions.
5 changes: 0 additions & 5 deletions Dockerfile
Expand Up @@ -9,8 +9,3 @@ COPY test.js .
RUN npm install

USER root

# RUN chown node:node /usr/local/bin/cputil
# RUN chown node:node /usr/src/app
# EXPOSE 3000
CMD ["/bin/sh", "-c", "npm test"]
26 changes: 23 additions & 3 deletions README.md
Expand Up @@ -10,9 +10,13 @@ The npm postinstall script has been removed as it fails on the GH action runner.

```js
import fastifyCloudPrnt from '@autotelic/fastify-cloudprnt'
import view from '@fastify/view'

export default async function basic (fastify, options) {
fastify.register(fastifyCloudPrnt, config)
// @fastify/view must be registered and available before fastify/view
await fastify.register(view, viewConfig)

await fastify.register(fastifyCloudPrnt, config)
}
```

Expand Down Expand Up @@ -41,7 +45,9 @@ See [examples](examples/) for working examples.

### Template Rendering

Templates should be in [Star Document Markup](https://star-m.jp/products/s_print/CloudPRNTSDK/Documentation/en/articles/markup/markupintro.html), and template rendering requires [`cputil`](https://star-m.jp/products/s_print/CloudPRNTSDK/Documentation/en/articles/cputil/cputil.html) to be in the path. The provided [Dockerfile](Dockerfile) builds a container with the app and `cputil` in.
Templates should be in [Star Document Markup](https://star-m.jp/products/s_print/CloudPRNTSDK/Documentation/en/articles/markup/markupintro.html), and template rendering requires [`cputil`](https://star-m.jp/products/s_print/CloudPRNTSDK/Documentation/en/articles/cputil/cputil.html) to be in the path. The provided [Dockerfile](Dockerfile) builds a container with the app and `cputil` in. Additionally, [@fastify/view](https://github.com/fastify/point-of-view) must be registered to fastify before fastify-cloudprnt.

Star Micronics provides a [Star Document Markup Designer](https://star-document-markup-designer.smcs.site/) web app.

## API

Expand All @@ -51,4 +57,18 @@ _@autotelic/fastify-cloudprnt_ exports a fastify plugin. When registered the plu
* `getJob: () => token`: method that returns the url-safe string `token` for the next available print job on the queue.
* `getJobData: (token) => object`: method that returns the data object for the job enqueued with the url-safe string `token`.
* `deleteJob: (token) => any`: method that deletes the job enqueued with the url-safe string `token` from the print queue.
* `viewOptions: object`: object containing configuration options to be passed through to [point-of-view](https://github.com/fastify/point-of-view#options). The default option provided by this plugin is `engine: { nunjucks }`.
* `routePrefix: string`: string which will configure a prefix for all cloudprint routes.

## Examples

### Basic
An [example](./examples/basic/) fastify app using [node-cache](https://github.com/node-cache/node-cache). To run the basic example, use the following command:
```sh
npm run example:basic
```

### Redis
An [example](./examples/redis/) fastify app using [redis](https://www.npmjs.com/package/redis). To run the redis example, use the following command:
```sh
npm run example:redis
```
7 changes: 0 additions & 7 deletions app.js

This file was deleted.

11 changes: 11 additions & 0 deletions docker-compose.basic-example.yaml
@@ -0,0 +1,11 @@
version: '3.5'

services:
fastify-cloudprnt:
container_name: fastify-cloudprnt
build:
context: .
dockerfile: Dockerfile
ports:
- '3000:3000'
command: "npm run example:basic:start"
20 changes: 20 additions & 0 deletions docker-compose.redis-example.yaml
@@ -0,0 +1,20 @@
version: '3.5'

services:
redis:
container_name: cloudprnt-redis
image: redis
ports:
- '6379:6379'
fastify-cloudprnt:
container_name: fastify-cloudprnt
build:
context: .
dockerfile: Dockerfile
ports:
- '3000:3000'
environment:
- REDIS_URL=redis://host.docker.internal:6379
depends_on:
- redis
command: "npm run example:redis:start"
1 change: 1 addition & 0 deletions docker-compose.yml → docker-compose.test.yaml
Expand Up @@ -8,3 +8,4 @@ services:
dockerfile: Dockerfile
ports:
- '3000:3000'
command: "npm run test"
11 changes: 9 additions & 2 deletions examples/basic/index.js
@@ -1,7 +1,15 @@
import NodeCache from 'node-cache'
import view from '@fastify/view'
import nunjucks from 'nunjucks'

import fastifyCloudPrnt from '../../index.js'

export default async function basic (fastify, options) {
await fastify.register(view, {
engine: { nunjucks },
root: './examples'
})

const printQueue = new NodeCache()

fastify.register(fastifyCloudPrnt, {
Expand All @@ -17,7 +25,6 @@ export default async function basic (fastify, options) {
deleteJob: token => {
const deleted = printQueue.del(token)
return deleted > 0
},
viewOptions: { root: './examples' }
}
})
}
44 changes: 44 additions & 0 deletions examples/redis/index.js
@@ -0,0 +1,44 @@
import { createClient } from 'redis'
import view from '@fastify/view'
import nunjucks from 'nunjucks'

import fastifyCloudPrnt from '../../index.js'

const JOB_DATA = 'printJobData'
const PRINT_QUEUE = 'printJobQueue'
const REDIS_URL = process.env.REDIS_URL

export default async function basic (fastify, options) {
// redis connection url defaults to localhost on port 6379
const redis = createClient({ url: REDIS_URL })
await redis.connect()

await fastify.register(view, {
engine: { nunjucks },
root: './examples'
})

await fastify.register(fastifyCloudPrnt, {
queueJob: async (token, jobData) => {
await redis.HSET(JOB_DATA, token, JSON.stringify(jobData))
await redis.LPUSH(PRINT_QUEUE, token)
},
getJob: async () => {
const token = await redis.LINDEX(PRINT_QUEUE, 0)
return token === undefined ? null : token
},
getJobData: async token => {
const jsonJobData = await redis.HGET(JOB_DATA, token)
if (!jsonJobData) {
return null
}
return JSON.parse(jsonJobData)
},
deleteJob: async token => {
await redis.HDEL(JOB_DATA, token)
const deletedPrintJob = await redis.LREM(PRINT_QUEUE, 1, token)

return deletedPrintJob > 0
}
})
}
39 changes: 19 additions & 20 deletions index.js
@@ -1,21 +1,17 @@
'use strict'

import view from '@fastify/view'
import nunjucks from 'nunjucks'
import fp from 'fastify-plugin'

import pollRoute from './routes/main/post.js'
import getJobRoute from './routes/main/get.js'
import queueJobRoute from './routes/job/post.js'
import deleteJobRoute from './routes/main/delete.js'

const defaultViewOptions = { engine: { nunjucks } }

export const defaultOptions = {
getJob: () => null,
getJobData: () => ({}),
queueJob: () => false,
deleteJob: () => false,
viewOptions: defaultViewOptions
deleteJob: () => false
}

async function fastifyCloudPrnt (fastify, options = defaultOptions) {
Expand All @@ -24,26 +20,29 @@ async function fastifyCloudPrnt (fastify, options = defaultOptions) {
getJobData,
queueJob,
deleteJob,
viewOptions
routePrefix
} = {
...defaultOptions,
...options
}

fastify.register(view, {
...defaultViewOptions,
...viewOptions
fastify.decorate('cloudPrnt', {
getJob,
getJobData,
queueJob,
deleteJob
})

fastify.decorate('getJob', getJob)
fastify.decorate('getJobData', getJobData)
fastify.decorate('queueJob', queueJob)
fastify.decorate('deleteJob', deleteJob)

fastify.route(pollRoute)
fastify.route(getJobRoute)
fastify.route(queueJobRoute)
fastify.route(deleteJobRoute)
fastify.register(async function (f) {
f.route(pollRoute)
f.route(getJobRoute)
f.route(queueJobRoute)
f.route(deleteJobRoute)
}, { prefix: routePrefix })
}

export default fastifyCloudPrnt
export default fp(fastifyCloudPrnt, {
name: 'fastify-plugin',
decorators: ['view'],
dependencies: ['@fastify/view']
})
40 changes: 27 additions & 13 deletions package.json
Expand Up @@ -4,48 +4,62 @@
"description": "Fastify plugin to run a server following the Star Micronics CloudPRNT protocol.",
"main": "index.js",
"type": "module",
"directories": {
"test": "test"
},
"scripts": {
"example:basic": "fastify start -w -l info -P -o examples/basic/index.js",
"example:basic": "docker compose -f docker-compose.basic-example.yaml up --build",
"example:basic:start": "fastify start -w -l info -P -o examples/basic/index.js",
"example:redis": "docker compose -f docker-compose.redis-example.yaml up --build",
"example:redis:start": "fastify start -w -l info -P -o examples/redis/index.js",
"test": "c8 --100 ava",
"test:docker": "docker compose -f docker-compose.test.yaml up",
"lint": "standard",
"fix": "standard --fix",
"validate": "npm run lint && npm run test"
},
"files": [
"index.js",
"routes"
],
"repository": {
"type": "git",
"url": "git+https://github.com/autotelic/fastify-plugin-template.git"
"url": "git+https://github.com/autotelic/fastify-cloudprnt.git"
},
"keywords": [],
"keywords": [
"fastify",
"cloudprnt",
"cloud-prnt",
"star-micronics"
],
"author": "Autotelic Development Ltd <info@autotelic.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/autotelic/fastify-plugin-template/issues"
"url": "https://github.com/autotelic/fastify-cloudprnt/issues"
},
"homepage": "https://github.com/autotelic/fastify-plugin-template#readme",
"homepage": "https://github.com/autotelic/fastify-cloudprnt#readme",
"dependencies": {
"@fastify/view": "^7.0.0",
"@rauschma/stringio": "^1.4.0",
"execa": "^6.1.0",
"fastify-cli": "^3.1.0",
"fastify-plugin": "^4.0.0",
"nunjucks": "^3.2.3"
"fastify-plugin": "^4.0.0"
},
"devDependencies": {
"@fastify/view": "^7.0.0",
"ava": "^4.3.0",
"c8": "^7.10.0",
"fastify": "^4.3.0",
"fastify-cli": "^3.1.0",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"node-cache": "^5.1.2",
"nunjucks": "^3.2.3",
"redis": "^4.5.1",
"sinon": "^14.0.0",
"standard": "^17.0.0"
},
"peerDependencies": {
"@fastify/view": "^7.0.0"
},
"lint-staged": {
"*.{js,jsx}": [
"npm run fix"
]
}
}

2 changes: 1 addition & 1 deletion routes/job/post.js
Expand Up @@ -22,7 +22,7 @@ export default {
},
handler: async function queueJobHandler (request, reply) {
const { token, jobData } = request.body
this.queueJob(token, jobData)
this.cloudPrnt.queueJob(token, jobData)
return reply
.code(201)
.send({ token })
Expand Down
2 changes: 1 addition & 1 deletion routes/main/delete.js
Expand Up @@ -22,7 +22,7 @@ export default {
},
handler: async function queueJobHandler (request, reply) {
const { token } = request.query
const deleted = this.deleteJob(token)
const deleted = this.cloudPrnt.deleteJob(token)
const code = deleted
? 200
: 404
Expand Down
2 changes: 1 addition & 1 deletion routes/main/get.js
Expand Up @@ -20,7 +20,7 @@ export default {
},
handler: async function getJobHandler (request, reply) {
const { token } = request.query
const jobData = await this.getJobData(token)
const jobData = await this.cloudPrnt.getJobData(token)
if (jobData === null) {
return reply.code(404).send('Job not found')
}
Expand Down
2 changes: 1 addition & 1 deletion routes/main/post.js
Expand Up @@ -23,7 +23,7 @@ export default {
}
},
handler: async function pollHandler (request, reply) {
const jobToken = await this.getJob()
const jobToken = await this.cloudPrnt.getJob()
const jobReady = jobToken !== null

let jobReadyResponse = {}
Expand Down

0 comments on commit 705cab1

Please sign in to comment.