Skip to content

Commit

Permalink
support node 8
Browse files Browse the repository at this point in the history
  • Loading branch information
dianabarsan committed Jun 18, 2024
1 parent ea80e4c commit 6f13918
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"extends": ["@medic", "plugin:node/recommended"],
"env": {
"node": true
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2020
"ecmaVersion": 8
},
"ignorePatterns": [
"**/node_modules/**"
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test
.idea
.github
/.eslintrc
/.eslintrc
.nyc_output/
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const util = require('node:util');
const util = require('util');
const { URL } = require('url');
const { Headers } = require('pouchdb-fetch');

const sessionCookieName = 'AuthSession';
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;
}
Expand Down Expand Up @@ -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) => {
Expand All @@ -45,7 +48,7 @@ const getSessionUrl = (db) => {
};

const authenticate = async (db) => {
if (!db?.credentials?.username) {
if (!db || !db.credentials || !db.credentials.username) {
return;
}

Expand Down Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions test/integration/basic.spec.js
Original file line number Diff line number Diff line change
@@ -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');
}
})();

2 changes: 1 addition & 1 deletion test/integration/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6f13918

Please sign in to comment.