Skip to content

Commit e719fc4

Browse files
author
Immanuel Pelzer
committed
Migrate to node 18 and ESM
1 parent ecf0083 commit e719fc4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+661
-3141
lines changed

.eslintrc.js .eslintrc.cjs

File renamed without changes.

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ results.xml
2929

3030
# npm packages
3131
*.tgz
32+
33+
# tests
34+
coverage

.gitlab-ci.yml

+2-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
image: node:14.19-alpine
1+
image: node:18.12-alpine
22

33
include:
44
# See template for docs
@@ -21,7 +21,7 @@ services:
2121

2222
# Overwrite the template test job, so we can wait for rabbitmq to be running
2323
# Moving this to the tests did not work as expected and tests kept still failing
24-
test-node14-19:
24+
test:
2525
stage: test
2626
script:
2727
# Wait for RabbitMQ to be running
@@ -37,31 +37,6 @@ test-node14-19:
3737
reports:
3838
junit: results.xml
3939

40-
test-node16-14:
41-
image: node:16.14-alpine
42-
stage: test
43-
script:
44-
# Wait for RabbitMQ to be running
45-
- while ! nc -z localhost 15692 ; do sleep 1 ; done
46-
- npm i
47-
- npm run test
48-
49-
test-node18-12:
50-
image: node:18.12-alpine
51-
stage: test
52-
script:
53-
# Wait for RabbitMQ to be running
54-
- while ! nc -z localhost 15692 ; do sleep 1 ; done
55-
- npm i
56-
- npm run test
57-
58-
test-pact:
59-
image: node:14.19
60-
stage: test
61-
script:
62-
- npm i
63-
- npm run test-pact
64-
6540
audit:
6641
stage: test
6742
script:

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
# [5.0.0] - 2022-11-15
8+
9+
## Breaking changes
10+
11+
- Updates the module to ESM. Only ESM is supported now.
12+
713
# [4.2.1] - 2022-11-04
814

915
- Fix issues with Taube overwriting critical Prometheus metrics (e.g. `up`)

README.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ This repository does not publish npm packages **yet**.
3232
One service acts as a `Server` providing data and another as a `Client` requesting data.
3333

3434
```javascript
35-
const taube = require('@cloud/taube')
35+
import taube from '@cloud/taube'
3636
taube.http.init()
3737

3838
const server = new taube.Server({})
@@ -52,7 +52,7 @@ server.get(
5252
Any Client can now request data:
5353

5454
```javascript
55-
const taube = require('@cloud/taube')
55+
import taube from '@cloud/taube'
5656

5757
const client = new taube.Client({
5858
uri: 'http://scooter'
@@ -70,7 +70,7 @@ The `Client` and `Server` components mimic the standard way of sending and routi
7070
`Client` component is a wrapper around [got](https://github.com/sindresorhus/got) that exposes different http methods to send a request.
7171

7272
```javascript
73-
const taube = require('@cloud/taube')
73+
import taube from '@cloud/taube'
7474

7575
const client = new taube.Client({
7676
uri: 'http://scooter'
@@ -122,7 +122,7 @@ client.get(`/?page=2`, { query: { type: 'UNU2' }})
122122
The `Server` component require HTTP to be intialized. Add `taube.http.init()`.
123123

124124
```javascript
125-
const taube = require('@cloud/taube')
125+
import taube from '@cloud/taube'
126126
taube.http.init()
127127

128128
const server = new taube.Server({})
@@ -244,7 +244,7 @@ const response = await client.paginate('/scooters', { page: 3, limit: 10 })
244244
The `Server` component require HTTP to be intialized. Add `taube.http.init()`.
245245

246246
```javascript
247-
const taube = require('@cloud/taube')
247+
import taube from '@cloud/taube'
248248
taube.http.init()
249249

250250
const responder = new taube.Responder({
@@ -267,7 +267,7 @@ responder.on('get user', async({ prop1, prop2 }) => {
267267
### Requesters
268268

269269
```javascript
270-
const taube = require('@cloud/taube')
270+
import taube from '@cloud/taube'
271271

272272
// Creating the requester needs to be one of the first things in your application
273273
// Assuming that a Responder with the given key is set up on the given uri
@@ -301,7 +301,6 @@ The `url` option needs to include `http` or `https` without a `/` at the end.
301301
| Variable | Default | Description
302302
| ------------------ |:----------------:| ---
303303
| TAUBE_HTTP_PORT | 4321 | Port of http server
304-
| TAUBE_DEBUG | undefined | Adds debugging information to Taube responses. See tests for usage. This does change responses and is only designed for development.
305304
| TAUBE_UNIT_TESTS | undefined | If set all requesters default their uri to <http://localhost>
306305
| TAUBE_RETRIES | 3 | Number of retries any Requester does before giving up. 3 is maximum value as retry duration would be over timeout.
307306
| TAUBE_JSON_SIZE_LIMIT |500kb | Size limit for JSON file
@@ -311,15 +310,15 @@ The `url` option needs to include `http` or `https` without a `/` at the end.
311310
@infrastructure/observability can be used to get readiness/liveness checks and signal handling for the taube http server.
312311

313312
```javascript
314-
const observability = require('@infrastructure/observability')
313+
import observability from '@infrastructure/observability'
315314

316315
observability.monitoring.observeServer(taube.http.server, taube.http.app)
317316
```
318317

319318
In order to gracefully handle Signal Handling and add liveness/readyness checks for AMQP, the following code can be used
320319

321320
```javascript
322-
const observability = require('@infrastructure/observability')
321+
import observability from '@infrastructure/observability'
323322

324323
observability.monitoring.addOnSignalHook(taube.shutdown)
325324
```
@@ -548,7 +547,7 @@ And there are two ways of throwing a taube error instance.
548547
#### Throwing an error using constructor name
549548

550549
```javascript
551-
const { Errors } = require('@cloud/taube')
550+
import { Errors } from '@cloud/taube'
552551

553552
// joi/celebrate validation error
554553
if(VALIDATION_FAILURE) {
@@ -561,7 +560,7 @@ if(VALIDATION_FAILURE) {
561560
#### Throwing an error using statusCode
562561

563562
```javascript
564-
const { Errors } = require('@cloud/taube')
563+
import { Errors } from '@cloud/taube'
565564

566565
// your code
567566
if(!scooter) {
@@ -660,6 +659,8 @@ A few tests need to run before any other. These tests are prefixed by 0.X. Do no
660659

661660
## Migrate from cote
662661

662+
You will have to install taube at version 2. `npm install @cloud/taube@2` to follow this part.
663+
663664
Version 0.X is designed to have a clear migration path to remove cote. Taube 0.X is a drop in replacement for cote. Without configuration it functions as a wrapper to cote and keeps using cote for communication. It also sets up http Responders, which means the service using Taube can be targeted by Taube Requesters.
664665

665666
There is 3 modes you can run taube in while migrating from cote to taube.
@@ -728,8 +729,8 @@ Remove this function call to migrate to v4.
728729

729730
This will be required for most Services. Add it after requiring taube in your index.js:
730731

731-
```
732-
const taube = require('@cloud/taube')
732+
```js
733+
import taube from '@cloud/taube'
733734
taube.http.init() // This was added
734735
```
735736

examples/Client_Server/client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Client } = require('../../lib')
1+
import { Client } from '../../lib/index.js'
22

33
const client = new Client({
44
uri: 'http://localhost', // uri of the server, e.g. http://scooter

examples/Client_Server/server.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const taube = require('../../lib')
2-
const { Server, Joi } = require('../../lib')
1+
import taube, { Server, Joi } from '../../lib/index.js'
32

43
const server = new Server({})
54
taube.http.init()

examples/Errors/badrequest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Errors } = require('../../lib')
1+
import { Errors } from '../../lib/index.js'
22

33
function getScooter(vin) {
44
if (!vin) {

examples/Errors/notfound-statuscode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Errors } = require('../../lib')
1+
import { Errors } from '../../lib/index.js'
22

33
function getScooter(vin) {
44
if (!vin) {

examples/Errors/notfound.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Errors } = require('../../lib')
1+
import { Errors } from '../../lib/index.js'
22

33
function getScooter() {
44
throw new Errors.NotFound('vin not found', { details: 'vin abcde not found' })

examples/Publisher_Subscriber/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const taube = require('../../lib')
1+
import taube from '../../lib/index.js'
22

33
async function main() {
44
taube.init()

examples/Queue_Worker/queue-worker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const taube = require('../../lib')
1+
import taube from '../../lib/index.js'
22

33
async function main() {
44
taube.init()

examples/Requester_Responder/server1-requester.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const taube = require('../../lib')
1+
import taube from '../../lib/index.js'
22

33
const userRequester = new taube.Requester({
44
key: 'users', // Key of responder from server2-responder.js

examples/Requester_Responder/server2-responder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const taube = require('../../lib')
1+
import taube from '../../lib/index.js'
22

33
taube.http.init()
44
const userResponder = new taube.Responder({ key: 'users' })

examples/Setup/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const observability = require('@infrastructure/observability')
2-
const taube = require('@cloud/taube')
1+
import observability from '@infrastructure/observability'
2+
import taube from '@cloud/taube'
33

44
taube.http.init()
55

examples/Sockend/responder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const taube = require('../../lib')
1+
import taube from '../../lib/index.js'
22

33
taube.http.init()
44

examples/Sockend/sockend.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
const express = require('express')
2-
const http = require('http')
1+
import express from 'express'
2+
import http from 'http'
3+
34
// eslint-disable-next-line import/no-extraneous-dependencies
4-
const ioServer = require('socket.io')
5-
const taube = require('../../lib')
5+
import ioServer from 'socket.io'
6+
7+
import taube from '../../lib/index.js'
68

79
// Setup the underlying socket.io server
810
const port = 6000

examples/pagination/Client_Server.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const taube = require('../../lib')
1+
import taube from '../../lib/index.js'
22

3-
taube.http.init()
3+
import service from './service.js'
44

5-
const service = require('./service')
5+
taube.http.init()
66

77
const client = new taube.Client({ uri: 'http://scooter' })
88
const server = new taube.Server({})

examples/pagination/service.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const mongoose = require('mongoose')
1+
import mongoose from 'mongoose'
2+
23
// https://github.com/aravindnc/mongoose-paginate-v2
3-
const mongoosePaginate = require('mongoose-paginate-v2')
4+
import mongoosePaginate from 'mongoose-paginate-v2'
45

56
const scooterSchema = new mongoose.Schema({
67
vin: { type: String },
@@ -45,6 +46,6 @@ const getPaginatedScooters = async(query = { page: 1, limit: 20 /** service limi
4546
return paginatedData
4647
}
4748

48-
module.exports = {
49+
export default {
4950
getPaginatedScooters,
5051
}

lib/amqp.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const amqp = require('amqp-connection-manager')
2-
const debug = require('debug')('taube-amqp')
1+
import amqp, { AmqpConnectionManagerClass } from 'amqp-connection-manager'
2+
import debugFactory from 'debug'
3+
import config from './config/index.js'
34

4-
const config = require('./config')
5+
const debug = debugFactory('taube-amqp')
56

67
const connections = {}
78
let channels = []
@@ -33,7 +34,7 @@ async function connection(uri, options = {}) {
3334
errorHandler = options.errorHandler || defaultErrorHandler
3435

3536
debug('AMQP connecting to ', uri)
36-
const manager = new amqp.AmqpConnectionManagerClass(uri, options)
37+
const manager = new AmqpConnectionManagerClass(uri, options)
3738
connections[uri] = manager.connect({ timeout: config.amqp.initialConnectionTimeout })
3839
.then(() => {
3940
debug('AMQP connected to ', uri)
@@ -93,7 +94,7 @@ function getChannels() {
9394
return channels
9495
}
9596

96-
module.exports = {
97+
export default {
9798
amqp,
9899
connection,
99100
shutdown,

lib/components/client.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* eslint-disable no-underscore-dangle */
2-
const got = require('got')
2+
import got from 'got'
33

4-
const config = require('../config')
5-
const pkg = require('../../package.json')
4+
import fs from 'node:fs'
5+
import config from '../config/index.js'
6+
import uriHelper from '../helpers/uri.js'
7+
import { convertToTaubeError } from './errors.js'
68

7-
const uriHelper = require('../helpers/uri')
8-
const Errors = require('./errors')
9+
const pkg = JSON.parse(fs.readFileSync('package.json'))
910

1011
const retry = {
1112
limit: config.got.retries,
@@ -51,7 +52,7 @@ class Client {
5152
})
5253
return response.body
5354
} catch (error) {
54-
throw Errors.convertToTaubeError(error)
55+
throw convertToTaubeError(error)
5556
}
5657
}
5758

@@ -66,7 +67,7 @@ class Client {
6667
})
6768
return response.body
6869
} catch (error) {
69-
throw Errors.convertToTaubeError(error)
70+
throw convertToTaubeError(error)
7071
}
7172
}
7273

@@ -83,7 +84,7 @@ class Client {
8384

8485
return response.body
8586
} catch (error) {
86-
throw Errors.convertToTaubeError(error)
87+
throw convertToTaubeError(error)
8788
}
8889
}
8990

@@ -100,7 +101,7 @@ class Client {
100101

101102
return response.body
102103
} catch (error) {
103-
throw Errors.convertToTaubeError(error)
104+
throw convertToTaubeError(error)
104105
}
105106
}
106107

@@ -116,9 +117,9 @@ class Client {
116117

117118
return response.body
118119
} catch (error) {
119-
throw Errors.convertToTaubeError(error)
120+
throw convertToTaubeError(error)
120121
}
121122
}
122123
}
123124

124-
module.exports = Client
125+
export default Client

0 commit comments

Comments
 (0)