diff --git a/.eslintrc b/.eslintrc index ffbfebf..43ff231 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,10 +1,11 @@ { "extends": ["@medic", "plugin:node/recommended"], "env": { - "node": true + "node": true, + "es6": true }, "parserOptions": { - "ecmaVersion": 2020 + "ecmaVersion": 8 }, "ignorePatterns": [ "**/node_modules/**" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 17a9563..8aa3d25 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,3 +27,21 @@ jobs: - run: echo $(docker-compose -v) - run: npm ci - run: npm run integration + + basic-usage: + strategy: + fail-fast: false + matrix: + version: [ '8.x', '16.x', '18.x' ] + + name: Smoke test for older node versions + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - name: Use Node.js ${{ matrix.version }} + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.version }} + - run: echo $(docker-compose -v) + - run: npm ci + - run: npm run integration-basic diff --git a/.npmignore b/.npmignore index dd4b07a..f656073 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,5 @@ test .idea .github -/.eslintrc \ No newline at end of file +/.eslintrc +.nyc_output/ diff --git a/package.json b/package.json index 1d56fbf..abe47a0 100644 --- a/package.json +++ b/package.json @@ -12,10 +12,11 @@ "integration-auth": "npm run stop-couch && npm run start-couch && mocha ./test/integration/credentials*.spec.js && npm run stop-couch", "integration-url": "npm run stop-couch && npm run start-couch && AUTH_TYPE=url mocha ./test/integration/credentials*.spec.js && npm run stop-couch", "integration-session": "npm run stop-couch && npm run start-couch && mocha ./test/integration/session*.spec.js && npm run stop-couch", - "integration": "npm run integration-auth && npm run integration-url && npm run integration-session" + "integration": "npm run integration-auth && npm run integration-url && npm run integration-session", + "integration-basic": "npm run stop-couch && npm run start-couch && node ./test/integration/basic.spec.js" }, "engines": { - "node": ">=16.12.0", + "node": ">=8.10.0", "npm": ">=8.3.1" }, "author": "Diana Barsan", diff --git a/src/index.js b/src/index.js index 17708bc..8e85a94 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ -const util = require('node:util'); +const util = require('util'); +const { URL } = require('url'); const { Headers } = require('pouchdb-fetch'); const sessionCookieName = 'AuthSession'; @@ -6,7 +7,7 @@ const cookieRegex = new RegExp(`${sessionCookieName}=(.*)`); const sessions = {}; const parseCookie = (response) => { - const cookie = response?.headers?.get('set-cookie'); + const cookie = response && response.headers && response.headers.get('set-cookie'); if (!cookie) { return; } @@ -35,7 +36,9 @@ const parseCookie = (response) => { const getSessionKey = (db) => { const sessionUrl = getSessionUrl(db); - return `${db.credentials?.username}:${db.credentials?.password}:${db.session}:${sessionUrl}`; + const username = db.credentials && db.credentials.username; + const password = db.credentials && db.credentials.password; + return `${username}:${password}:${db.session}:${sessionUrl}`; }; const getSessionUrl = (db) => { @@ -45,7 +48,7 @@ const getSessionUrl = (db) => { }; const authenticate = async (db) => { - if (!db?.credentials?.username) { + if (!db || !db.credentials || !db.credentials.username) { return; } @@ -101,7 +104,7 @@ const extractAuth = (opts) => { }; const isValid = (session) => { - if (!session?.expires) { + if (!session || !session.expires) { return false; } const isExpired = Date.now() > session.expires; diff --git a/test/integration/basic.spec.js b/test/integration/basic.spec.js new file mode 100644 index 0000000..8b603ba --- /dev/null +++ b/test/integration/basic.spec.js @@ -0,0 +1,19 @@ +const PouchDb = require('pouchdb-core'); +const utils = require('./utils'); +PouchDb.plugin(require('pouchdb-adapter-http')); +PouchDb.plugin(require('../../src/index')); + +const dbName = 'testdb'; + +(async () => { + await utils.setupCouch(dbName); + const db = new PouchDb(`${utils.baseUrl}/${dbName}`, { skip_setup: true, auth: utils.dbAuth }); + + const docs = [{ _id: '1', e: 1 }, { _id: '2', e: 2 }]; + await db.bulkDocs(docs); + const allDocs = await db.allDocs(); + if (allDocs.rows.length !== docs.length) { + throw new Error('Invalid number of docs'); + } +})(); + diff --git a/test/integration/utils.js b/test/integration/utils.js index faa79f9..3df7d80 100644 --- a/test/integration/utils.js +++ b/test/integration/utils.js @@ -138,7 +138,7 @@ const getDbRequest = (username, logs, dbName, path, success = true, method = 'GE const request = async (opts) => { const authString = `${auth.username}:${auth.password}`; - const token = btoa(decodeURIComponent(encodeURIComponent(authString))); + const token = Buffer.from(decodeURIComponent(encodeURIComponent(authString))).toString('base64'); const headers = new Headers({ 'Authorization': 'Basic ' + token }); if (opts.json) {