1.0.1 • Published 3 years ago

@lucets/commands v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@lucets/commands

Message hook which allows setting hooks for individual commands.

Designed to be used with Luce.

Install

npm i @lucets/commands

Example

'use strict'

import { Server } from 'http'

import Application, { WebSocketError } from '@lucets/luce'
import Commands from './index'

const { name, version } = require('../package')

const app = new Application()
const commands = new Commands({ key: 'cmd' }) // message key, default 'cmd'
const server = new Server()

// Implement a catch-all hook
commands.use('*', async (message, ctx) => {
  return ctx.send({
    cmd: 'error',
    message: `Unknown command: ${message[commands.key]}`
  })
})

// Implement a ping command
commands.use('ping', async (message, ctx, next) => {
  await ctx.send({ cmd: 'pong' })
})

// Implement a version info command
commands.use('version-info', async (message, ctx, next) => {
  await ctx.send({
    cmd: 'version-info',
    name,
    version
  })
})

// Implement a publish command
commands.use('publish', async (message, ctx, next) => {
  if (!ctx.state.authorized) {
    // 4401 - Unauthorized
    throw new WebSocketError(4401)
  }
  return next()
}, async (message, ctx, next) => {
  if (!message.package) {
    // 4400 - Bad Request
    throw new WebSocketError(4400)
  }
  return next()
}, async (message, _ctx, next) => {
  console.log(`[publish]: now publishing ${message.package}`)
  return next()
})

// Compose the commands hook
// If the commands hook does not handle the message,
// send an error to the client
app.useMessage(commands.compose(), async (message, ctx, next) => {
  await ctx.send({
    cmd: 'error',
    message: `unknown command: ${message[commands.key] ?? 'none'}`
  })
})

// Add the upgrade method to the server
server.on('upgrade', app.onUpgrade())
server.listen(3004, () => {
  console.log('Server listening on port 3004')
})

License

Copyright 2021 Michiel van der Velde.

This software is licensed under the MIT License.

1.0.1

3 years ago

1.0.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago