0.26.9 • Published 17 days ago

presidium v0.26.9

Weekly downloads
117
License
MIT
Repository
github
Last release
17 days ago

Presidium

Node.js CI codecov npm version

A library for creating web services.

Handle Http

const { HttpServer, Http } = require('presidium')

new HttpServer((request, response) => {
  response.writeHead(200, { 'Content-Type': 'application/json' })
  response.write(JSON.stringify({ greeting: 'Hello World' }))
  response.end()
}).listen(3000)

const http = new Http('http://localhost:3000/')

http.get('/')
  .then(response => response.json())
  .then(console.log) // { greeting: 'Hello World' }

Handle WebSocket

const { WebSocketServer, WebSocket } = require('presidium')

new WebSocketServer(socket => {
  socket.on('message', message => {
    console.log('Got message:', message)
  })
  socket.on('close', () => {
    console.log('Socket closed')
  })
}).listen(1337)


const socket = new WebSocket('ws://localhost:1337/')
socket.addEventListener('open', function (event) {
  socket.send('Hello Server!')
})
socket.addEventListener('message', function (event) {
  console.log('Message from server:', event.data)
})

CRUD and Query DynamoDB

const { DynamoTable, DynamoIndex } = require('presidium')

const awsCreds = {
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: process.env.AWS_REGION,
}

;(async function() {
  const myTable = new DynamoTable({
    name: 'my-table',
    key: [{ id: 'string' }],
    ...awsCreds,
  })
  const myIndex = new DynamoIndex({
    table: 'my-table',
    key: [{ name: 'string' }, { age: 'number' }],
    ...awsCreds,
  })
  const myStream = new DynamoStream({
    table: 'my-table',
    ...awsCreds,
  })

  await myTable.ready
  await myIndex.ready
  await myStream.ready

  await myTable.putItem({ id: '1', name: 'George' })
  await myTable.updateItem({ id: '1' }, { age: 32 })
  console.log(
    await myTable.getItem({ id: '1' }),
  ) // { Item: { id: { S: '1' }, ... } }

  console.log(
    await myIndex.query('name = :name AND age < :age', {
      name: 'George',
      age: 33,
    }),
  ) // [{ Items: [{ id: { S: '1' }, ... }, ...] }]

  for await (const record of myStream) {
    console.log(record) // { dynamodb: { NewImage: {...}, OldImage: {...} }, ... }
  }
})()

Consume Kinesis Streams

const { KinesisStream } = require('presidium')

const awsCreds = {
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: process.env.AWS_REGION,
}

const myStream = new KinesisStream({
  name: 'my-stream',
  ...awsCreds,
})

;(async function() {
  await myStream.ready

  await myStream.putRecord('hey')
  await myStream.putRecord('hey')
  await myStream.putRecord('hey')

  for await (const item of myStream) {
    console.log(item) /*
    {
      SequenceNumber: '49614...',
      ApproximateArrivalTimestamp: 2021-01-12T16:01:24.432Z,
      Data: <Buffer ...>, // hey
      PartitionKey: 'hey',
    }
    {
      SequenceNumber: '...',
      SequenceNumber: '49614...',
      ApproximateArrivalTimestamp: 2021-01-12T16:01:24.432Z,
      Data: <Buffer ...>, // hey
      PartitionKey: 'hey',
    }
    {
      SequenceNumber: '49614...',
      ApproximateArrivalTimestamp: 2021-01-12T16:01:24.432Z,
      Data: <Buffer ...>, // hey
      PartitionKey: 'hey',
    }
    */
  }
})

Upload to S3

const { S3Bucket } = require('presidium')

const awsCreds = {
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: process.env.AWS_REGION,
}

const myBucket = new S3Bucket({
  name: 'my-bucket',
  ...awsCreds,
})

;(async function () {
  await myBucket.ready

  await myBucket.putObject('some-key', '{"hello":"world"}', {
    ContentType: 'application/json',
  })
  console.log(
    await myBucket.getObject('some-key'),
  ) // { Etag: ..., Body: '{"hello":"world"}', ContentType: 'application/json' }
  await myBucket.deleteAllObjects()
  await myBucket.delete()
})()

Build and Push Docker Images

Stop using --build-arg for that npm token

const { DockerImage } = require('presidium')

const myImage = new DockerImage('my-app:1.0.0')

const buildStream = myImage.build(__dirname, { ignore: '.github', 'node_modules', archive: { Dockerfile: FROM node:15-alpine WORKDIR /opt COPY . . RUN echo //registry.npmjs.org/:_authToken=${myNpmToken} > .npmrc \ && npm i \ && rm .npmrc && rm Dockerfile EXPOSE 8080 CMD ["npm", "start"], }, })

buildStream.on('end', () => { const pushStream = myImage.push('my-registry.io') pushStream.pipe(process.stdout) }) buildStream.pipe(process.stdout)

## Execute Docker Containers
```javascript
const { DockerContainer } = require('presidium')

const container = new DockerContainer({
  image: 'node:15-alpine',
  env: { FOO: 'foo' },
  cmd: ['node', '-e', 'console.log(process.env.FOO)'],
  rm: true,
})

container.run().pipe(process.stdout) // foo

Deploy Docker Swarm Services

const { DockerSwarm, DockerService } = require('presidium')

;(async function() {
  const mySwarm = new DockerSwarm('eth0:2377')
  await mySwarm.ready // initiated new docker swarm

  const myService = new DockerService({
    name: 'my-service',
    image: 'nginx:1.19',
    publish: { 80: 80 },
    healthCheck: ['curl', '[::1]'],
    replicas: 5,
  })
  await myService.ready // new nginx service is up running
})()
0.26.9

17 days ago

0.26.8

1 month ago

0.26.7

1 month ago

0.26.6

2 months ago

0.26.5

3 months ago

0.26.3

3 months ago

0.26.4

3 months ago

0.26.2

3 months ago

0.26.1

3 months ago

0.26.0

3 months ago

0.25.2

4 months ago

0.25.1

4 months ago

0.25.0

4 months ago

0.23.0

5 months ago

0.24.0

5 months ago

0.21.2

6 months ago

0.21.1

6 months ago

0.21.0

7 months ago

0.22.0

5 months ago

0.20.1

11 months ago

0.20.0

11 months ago

0.20.3

9 months ago

0.20.2

10 months ago

0.18.7

1 year ago

0.19.8

1 year ago

0.19.0

1 year ago

0.19.1

1 year ago

0.19.2

1 year ago

0.19.3

1 year ago

0.19.5

1 year ago

0.19.6

1 year ago

0.19.7

1 year ago

0.18.6

1 year ago

0.17.0

1 year ago

0.18.1

1 year ago

0.18.2

1 year ago

0.18.5

1 year ago

0.18.0

1 year ago

0.16.18

1 year ago

0.16.19

1 year ago

0.16.16

2 years ago

0.16.17

1 year ago

0.16.21

1 year ago

0.16.22

1 year ago

0.16.20

1 year ago

0.16.25

1 year ago

0.16.26

1 year ago

0.16.23

1 year ago

0.16.27

1 year ago

0.16.28

1 year ago

0.16.15

2 years ago

0.16.12

2 years ago

0.16.10

2 years ago

0.16.11

2 years ago

0.16.3

2 years ago

0.16.5

2 years ago

0.16.6

2 years ago

0.16.7

2 years ago

0.16.8

2 years ago

0.16.9

2 years ago

0.16.0

2 years ago

0.16.1

2 years ago

0.16.2

2 years ago

0.15.35

2 years ago

0.15.36

2 years ago

0.15.20

2 years ago

0.15.21

2 years ago

0.15.23

2 years ago

0.15.28

2 years ago

0.15.29

2 years ago

0.15.26

2 years ago

0.15.27

2 years ago

0.15.31

2 years ago

0.15.32

2 years ago

0.15.30

2 years ago

0.15.33

2 years ago

0.15.17

2 years ago

0.15.18

2 years ago

0.15.16

2 years ago

0.15.19

2 years ago

0.15.15

3 years ago

0.15.11

3 years ago

0.15.12

3 years ago

0.15.9

3 years ago

0.15.10

3 years ago

0.15.8

3 years ago

0.15.7

3 years ago

0.15.5

3 years ago

0.15.6

3 years ago

0.15.1

3 years ago

0.15.2

3 years ago

0.15.3

3 years ago

0.13.0

3 years ago

0.15.0

3 years ago

0.12.0

3 years ago

0.11.0

3 years ago

0.11.1

3 years ago

0.11.2

3 years ago

0.11.3

3 years ago

0.11.5

3 years ago

0.10.26

3 years ago

0.10.25

3 years ago

0.10.23

3 years ago

0.10.18

3 years ago

0.10.19

3 years ago

0.10.20

3 years ago

0.10.21

3 years ago

0.10.22

3 years ago

0.10.16

3 years ago

0.10.17

3 years ago

0.10.15

3 years ago

0.10.14

3 years ago

0.10.10

3 years ago

0.10.11

3 years ago

0.10.12

3 years ago

0.10.13

3 years ago

0.10.9

3 years ago

0.10.8

3 years ago

0.10.7

3 years ago

0.10.6

3 years ago

0.10.3

3 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.9.12

3 years ago

0.9.11

3 years ago

0.9.10

3 years ago

0.9.9

3 years ago

0.9.8

3 years ago

0.9.7

3 years ago

0.9.6

3 years ago

0.9.5

3 years ago

0.9.3

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.8

3 years ago

0.8.7

3 years ago

0.8.6

3 years ago

0.8.5

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.1.0

4 years ago

0.0.0

4 years ago