@fastify/elasticsearch 2.1.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,49 +1,80 @@
1
1
  name: CI
2
- 'on':
2
+
3
+ on:
3
4
  push:
4
5
  paths-ignore:
5
- - docs/**
6
+ - 'docs/**'
6
7
  - '*.md'
7
8
  pull_request:
8
9
  paths-ignore:
9
- - docs/**
10
+ - 'docs/**'
10
11
  - '*.md'
12
+
11
13
  jobs:
14
+ dependency-review:
15
+ name: Dependency Review
16
+ if: github.event_name == 'pull_request'
17
+ runs-on: ubuntu-latest
18
+ permissions:
19
+ contents: read
20
+ steps:
21
+ - name: Check out repo
22
+ uses: actions/checkout@v3
23
+ with:
24
+ persist-credentials: false
25
+
26
+ - name: Dependency review
27
+ uses: actions/dependency-review-action@v3
28
+
12
29
  test:
13
30
  runs-on: ubuntu-latest
31
+ permissions:
32
+ contents: read
14
33
  strategy:
15
34
  matrix:
16
- node-version:
17
- - 10
18
- - 12
19
- - 14
20
- - 16
21
- services:
22
- elasticsearch:
23
- image: docker.elastic.co/elasticsearch/elasticsearch:7.10.0
24
- ports:
25
- - '9200:9200'
26
- env:
27
- discovery.type: single-node
35
+ node-version: [14, 16, 18]
28
36
  steps:
29
- - uses: actions/checkout@v3
30
- - name: Use Node.js
37
+ - name: Check out repo
38
+ uses: actions/checkout@v3
39
+ with:
40
+ persist-credentials: false
41
+
42
+ - name: Setup Node ${{ matrix.node-version }}
31
43
  uses: actions/setup-node@v3
32
44
  with:
33
45
  node-version: ${{ matrix.node-version }}
34
- - name: Install Dependencies
35
- run: |
36
- npm install --ignore-scripts
37
- - name: Run Tests
46
+
47
+ - name: Configure sysctl limits
38
48
  run: |
39
- npm test
49
+ sudo swapoff -a
50
+ sudo sysctl -w vm.swappiness=1
51
+ sudo sysctl -w fs.file-max=262144
52
+ sudo sysctl -w vm.max_map_count=262144
53
+
54
+ - name: Runs Elasticsearch
55
+ uses: elastic/elastic-github-actions/elasticsearch@562b8b6ae4677da97273ff6bc4d630ce96ecbaa5
56
+ with:
57
+ stack-version: 8.3.2
58
+ security-enabled: false
59
+
60
+ - name: Install dependencies
61
+ run: npm i --ignore-scripts
62
+
63
+ - name: Run tests
64
+ run: npm test
65
+
40
66
  automerge:
67
+ name: Automerge Dependabot PRs
68
+ if: >
69
+ github.event_name == 'pull_request' &&
70
+ github.event.pull_request.user.login == 'dependabot[bot]'
41
71
  needs: test
42
- runs-on: ubuntu-latest
43
72
  permissions:
44
73
  pull-requests: write
45
74
  contents: write
75
+ runs-on: ubuntu-latest
46
76
  steps:
47
77
  - uses: fastify/github-action-merge-dependabot@v3
48
78
  with:
49
79
  github-token: ${{ secrets.GITHUB_TOKEN }}
80
+ target: major
package/.taprc ADDED
@@ -0,0 +1,2 @@
1
+ files:
2
+ - test/**/*.test.js
package/README.md CHANGED
@@ -1,8 +1,7 @@
1
- # fastify-elasticsearch
1
+ # @fastify/elasticsearch
2
2
 
3
3
  ![CI](https://github.com/fastify/fastify-elasticsearch/workflows/CI/badge.svg)
4
- [![NPM version](https://img.shields.io/npm/v/fastify-elasticsearch.svg?style=flat)](https://www.npmjs.com/package/fastify-elasticsearch)
5
- [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-elasticsearch/badge.svg)](https://snyk.io/test/github/fastify/fastify-elasticsearch)
4
+ [![NPM version](https://img.shields.io/npm/v/@fastify/elasticsearch.svg?style=flat)](https://www.npmjs.com/package/@fastify/elasticsearch)
6
5
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
7
6
 
8
7
  Fastify plugin for [Elasticsearch](https://www.elastic.co/elasticsearch/) for sharing the same ES client in every part of your server.
@@ -12,7 +11,7 @@ Under the hood, the official [elasticsearch](https://www.npmjs.com/package/@elas
12
11
  ## Install
13
12
 
14
13
  ```
15
- npm i fastify-elasticsearch
14
+ npm i @fastify/elasticsearch
16
15
  ```
17
16
 
18
17
  ## Usage
@@ -22,7 +21,7 @@ The plugin accepts the [same options](https://github.com/elastic/elasticsearch-j
22
21
  ```js
23
22
  const fastify = require('fastify')()
24
23
 
25
- fastify.register(require('fastify-elasticsearch'), { node: 'http://localhost:9200' })
24
+ fastify.register(require('@fastify/elasticsearch'), { node: 'http://localhost:9200' })
26
25
 
27
26
  fastify.get('/user', async function (req, reply) {
28
27
  const { body } = await this.elastic.search({
@@ -35,14 +34,14 @@ fastify.get('/user', async function (req, reply) {
35
34
  return body.hits.hits
36
35
  })
37
36
 
38
- fastify.listen(3000, err => {
37
+ fastify.listen({ port: 3000 }, err => {
39
38
  if (err) throw err
40
39
  })
41
40
  ```
42
41
 
43
- By default, `fastify-elasticsearch` will try to ping the cluster as soon as you start Fastify, but in some cases pinging may not be supported due to the user permissions. If you want, you can disable the initial ping with the `healthcheck` option:
42
+ By default, `@fastify/elasticsearch` will try to ping the cluster as soon as you start Fastify, but in some cases pinging may not be supported due to the user permissions. If you want, you can disable the initial ping with the `healthcheck` option:
44
43
  ```js
45
- fastify.register(require('fastify-elasticsearch'), {
44
+ fastify.register(require('@fastify/elasticsearch'), {
46
45
  node: 'http://localhost:9200',
47
46
  healthcheck: false
48
47
  })
@@ -52,12 +51,12 @@ If you need to connect to different clusters, you can also pass a `namespace` op
52
51
  ```js
53
52
  const fastify = require('fastify')()
54
53
 
55
- fastify.register(require('fastify-elasticsearch'), {
54
+ fastify.register(require('@fastify/elasticsearch'), {
56
55
  node: 'http://localhost:9200',
57
56
  namespace: 'cluster1'
58
57
  })
59
58
 
60
- fastify.register(require('fastify-elasticsearch'), {
59
+ fastify.register(require('@fastify/elasticsearch'), {
61
60
  node: 'http://localhost:9201',
62
61
  namespace: 'cluster2'
63
62
  })
@@ -73,7 +72,7 @@ fastify.get('/user', async function (req, reply) {
73
72
  return body.hits.hits
74
73
  })
75
74
 
76
- fastify.listen(3000, err => {
75
+ fastify.listen({ port: 3000 }, err => {
77
76
  if (err) throw err
78
77
  })
79
78
  ```
@@ -85,7 +84,7 @@ If it is not, you can pass a custom client via the `client` option.
85
84
  const fastify = require('fastify')()
86
85
  const { Client } = require('@elastic/elasticsearch')
87
86
 
88
- fastify.register(require('fastify-elasticsearch'), {
87
+ fastify.register(require('@fastify/elasticsearch'), {
89
88
  client: new Client({ node: 'http://localhost:9200' })
90
89
  })
91
90
 
@@ -100,7 +99,7 @@ fastify.get('/user', async function (req, reply) {
100
99
  return body.hits.hits
101
100
  })
102
101
 
103
- fastify.listen(3000, err => {
102
+ fastify.listen({ port: 3000 }, err => {
104
103
  if (err) throw err
105
104
  })
106
105
  ```
package/es-docker.sh CHANGED
@@ -6,5 +6,6 @@
6
6
  exec docker run \
7
7
  --rm \
8
8
  -e "discovery.type=single-node" \
9
+ -e "xpack.security.enabled=false" \
9
10
  -p 9200:9200 \
10
- docker.elastic.co/elasticsearch/elasticsearch:7.4.0
11
+ docker.elastic.co/elasticsearch/elasticsearch:8.3.2
package/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const fp = require('fastify-plugin')
4
4
  const { Client } = require('@elastic/elasticsearch')
5
+ const isElasticsearchClient = require('./lib/isElasticsearchClient')
5
6
 
6
7
  async function fastifyElasticsearch (fastify, options) {
7
8
  const { namespace, healthcheck } = options
@@ -25,19 +26,24 @@ async function fastifyElasticsearch (fastify, options) {
25
26
 
26
27
  fastify.elastic[namespace] = client
27
28
 
28
- fastify.addHook('onClose', (instance, done) => {
29
- instance.elastic[namespace].close(done)
29
+ fastify.addHook('onClose', async (instance) => {
30
+ // v8 client.close returns a promise and does not accept a callback
31
+ await instance.elastic[namespace].close()
30
32
  })
31
33
  } else {
32
34
  fastify
33
35
  .decorate('elastic', client)
34
- .addHook('onClose', (instance, done) => {
35
- instance.elastic.close(done)
36
+ .addHook('onClose', async (instance) => {
37
+ await instance.elastic.close()
36
38
  })
37
39
  }
38
40
  }
39
41
 
40
42
  module.exports = fp(fastifyElasticsearch, {
41
- fastify: '3.x',
42
- name: 'fastify-elasticsearch'
43
+ fastify: '4.x',
44
+ name: '@fastify/elasticsearch'
43
45
  })
46
+ module.exports.default = fastifyElasticsearch
47
+ module.exports.fastifyElasticsearch = fastifyElasticsearch
48
+
49
+ module.exports.isElasticsearchClient = isElasticsearchClient
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ const { Client } = require('@elastic/elasticsearch')
4
+
5
+ module.exports = function isElasticsearchClient (value) {
6
+ return value instanceof Client
7
+ }
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@fastify/elasticsearch",
3
- "version": "2.1.0",
3
+ "version": "3.1.0",
4
4
  "description": "Fastify plugin for elastic search",
5
5
  "main": "index.js",
6
+ "types": "types/index.d.ts",
6
7
  "scripts": {
7
- "lint": "standard | snazzy",
8
- "test": "npm run lint && npm run unit",
9
- "unit": "tap test.js"
8
+ "lint": "standard",
9
+ "test": "npm run test:unit && npm run test:typescript",
10
+ "test:unit": "tap",
11
+ "test:typescript": "tsd"
10
12
  },
11
13
  "repository": {
12
14
  "type": "git",
@@ -25,19 +27,17 @@
25
27
  "url": "https://github.com/fastify/fastify-elasticsearch/issues"
26
28
  },
27
29
  "homepage": "https://github.com/fastify/fastify-elasticsearch#readme",
28
- "engines": {
29
- "node": ">=10.0.0"
30
- },
31
30
  "dependencies": {
32
- "@elastic/elasticsearch": "^7.1.0",
33
- "fastify-plugin": "^3.0.0"
31
+ "@elastic/elasticsearch": "^8.2.1",
32
+ "fastify-plugin": "^4.0.0"
34
33
  },
35
34
  "devDependencies": {
36
- "fastify": "^3.0.0-rc.1",
37
- "pre-commit": "^1.2.2",
38
- "snazzy": "^9.0.0",
35
+ "@types/node": "^18.6.4",
36
+ "@fastify/pre-commit": "^2.0.2",
37
+ "fastify": "^4.0.0-rc.2",
39
38
  "standard": "^17.0.0",
40
- "tap": "^16.0.0"
39
+ "tap": "^16.0.0",
40
+ "tsd": "^0.22.0"
41
41
  },
42
42
  "publishConfig": {
43
43
  "access": "public"
@@ -3,19 +3,21 @@
3
3
  const { test } = require('tap')
4
4
  const { Client } = require('@elastic/elasticsearch')
5
5
  const Fastify = require('fastify')
6
- const fastifyElasticsearch = require('./index')
6
+ const fastifyElasticsearch = require('..')
7
+ const isElasticsearchClient = require('..').isElasticsearchClient
7
8
 
8
9
  test('with reachable cluster', async t => {
9
10
  const fastify = Fastify()
11
+ t.teardown(() => fastify.close())
10
12
  fastify.register(fastifyElasticsearch, { node: 'http://localhost:9200' })
11
13
 
12
14
  await fastify.ready()
13
- t.strictEqual(fastify.elastic.name, 'elasticsearch-js')
14
- await fastify.close()
15
+ t.equal(fastify.elastic.name, 'elasticsearch-js')
15
16
  })
16
17
 
17
18
  test('with unreachable cluster', async t => {
18
19
  const fastify = Fastify()
20
+ t.teardown(() => fastify.close())
19
21
  fastify.register(fastifyElasticsearch, { node: 'http://localhost:9201' })
20
22
 
21
23
  try {
@@ -23,12 +25,12 @@ test('with unreachable cluster', async t => {
23
25
  t.fail('should not boot successfully')
24
26
  } catch (err) {
25
27
  t.ok(err)
26
- await fastify.close()
27
28
  }
28
29
  })
29
30
 
30
31
  test('with unreachable cluster and healthcheck disabled', async t => {
31
32
  const fastify = Fastify()
33
+ t.teardown(() => fastify.close())
32
34
  fastify.register(fastifyElasticsearch, {
33
35
  node: 'http://localhost:9201',
34
36
  healthcheck: false
@@ -36,15 +38,15 @@ test('with unreachable cluster and healthcheck disabled', async t => {
36
38
 
37
39
  try {
38
40
  await fastify.ready()
39
- t.strictEqual(fastify.elastic.name, 'elasticsearch-js')
41
+ t.equal(fastify.elastic.name, 'elasticsearch-js')
40
42
  } catch (err) {
41
43
  t.fail('should not error')
42
44
  }
43
- await fastify.close()
44
45
  })
45
46
 
46
47
  test('namespaced', async t => {
47
48
  const fastify = Fastify()
49
+ t.teardown(() => fastify.close())
48
50
  fastify.register(fastifyElasticsearch, {
49
51
  node: 'http://localhost:9200',
50
52
  namespace: 'cluster'
@@ -52,11 +54,14 @@ test('namespaced', async t => {
52
54
 
53
55
  await fastify.ready()
54
56
  t.strictEqual(fastify.elastic.cluster.name, 'elasticsearch-js')
57
+ t.equal(isElasticsearchClient(fastify.elastic), false)
58
+ t.equal(isElasticsearchClient(fastify.elastic.cluster), true)
55
59
  await fastify.close()
56
60
  })
57
61
 
58
62
  test('namespaced (errored)', async t => {
59
63
  const fastify = Fastify()
64
+ t.teardown(() => fastify.close())
60
65
  fastify.register(fastifyElasticsearch, {
61
66
  node: 'http://localhost:9200',
62
67
  namespace: 'cluster'
@@ -72,7 +77,6 @@ test('namespaced (errored)', async t => {
72
77
  t.fail('should not boot successfully')
73
78
  } catch (err) {
74
79
  t.ok(err)
75
- await fastify.close()
76
80
  }
77
81
  })
78
82
 
@@ -83,15 +87,18 @@ test('custom client', async t => {
83
87
  })
84
88
 
85
89
  const fastify = Fastify()
90
+ t.teardown(() => fastify.close())
86
91
  fastify.register(fastifyElasticsearch, { client })
87
92
 
88
93
  await fastify.ready()
94
+ t.equal(isElasticsearchClient(fastify.elastic), true)
89
95
  t.strictEqual(fastify.elastic.name, 'custom')
90
96
  await fastify.close()
91
97
  })
92
98
 
93
99
  test('Missing configuration', async t => {
94
100
  const fastify = Fastify()
101
+ t.teardown(() => fastify.close())
95
102
  fastify.register(fastifyElasticsearch)
96
103
 
97
104
  try {
@@ -99,6 +106,5 @@ test('Missing configuration', async t => {
99
106
  t.fail('should not boot successfully')
100
107
  } catch (err) {
101
108
  t.ok(err)
102
- await fastify.close()
103
109
  }
104
110
  })
@@ -0,0 +1,27 @@
1
+ import type { FastifyPluginAsync } from 'fastify';
2
+ import type { Client, ClientOptions } from '@elastic/elasticsearch';
3
+
4
+ declare module 'fastify' {
5
+ interface FastifyInstance {
6
+ elastic: Client & Record<string, Client>;
7
+ isElasticsearchClient(value: unknown): value is Client
8
+ }
9
+ }
10
+
11
+ type FastifyElasticsearch = FastifyPluginAsync<fastifyElasticsearch.FastifyElasticsearchOptions> & {
12
+ isElasticsearchClient: (value: any) => value is Client
13
+ }
14
+
15
+ declare namespace fastifyElasticsearch {
16
+ export interface FastifyElasticsearchOptions extends ClientOptions {
17
+ namespace?: string;
18
+ healthcheck?: boolean;
19
+ client?: Client;
20
+ }
21
+
22
+ export const fastifyElasticsearch: FastifyElasticsearch
23
+ export { fastifyElasticsearch as default }
24
+ }
25
+
26
+ declare function fastifyElasticsearch(...params: Parameters<FastifyElasticsearch>): ReturnType<FastifyElasticsearch>
27
+ export = fastifyElasticsearch
@@ -0,0 +1,13 @@
1
+ import fastifyElasticsearch from '..';
2
+ import Fastify from 'fastify';
3
+ import { expectAssignable, expectType } from 'tsd';
4
+ import { Client } from '@elastic/elasticsearch';
5
+
6
+
7
+ const fastify = Fastify()
8
+ fastify.register(fastifyElasticsearch, { node: 'http://localhost:9200' })
9
+
10
+ expectType<boolean>(fastify.isElasticsearchClient(fastify.elastic))
11
+ expectType<boolean>(fastify.isElasticsearchClient(fastify.elastic.asyncSearch))
12
+ expectType<boolean>(fastify.isElasticsearchClient(fastify.elastic.aasdf))
13
+ expectAssignable<(value: any) => value is Client>(fastifyElasticsearch.isElasticsearchClient)