Skip to content

Commit

Permalink
chore(#6250): use fetch in all places except api and sentinel (#9708)
Browse files Browse the repository at this point in the history
Switches to fetch is support or build scripts.
Removes deprecated generate-form-attachments script. As of 4.x, reports no longer have xml attachments.
#6250
  • Loading branch information
dianabarsan authored Jan 10, 2025
1 parent cfa682f commit b3cee4a
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 1,093 deletions.
37 changes: 25 additions & 12 deletions scripts/build/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs');
const spawn = require('child_process').spawn;
const path = require('path');
const rpn = require('request-promise-native');
const mustache = require('mustache');

const packageJson = require('../../package.json');
Expand Down Expand Up @@ -34,8 +33,18 @@ const getApiUrl = (pathname = '') => {
const apiUrl = new URL(COUCH_URL);
apiUrl.port = API_PORT || DEFAULT_API_PORT;
apiUrl.pathname = pathname;

return apiUrl.toString();
const basicAuth = btoa(`${apiUrl.username}:${apiUrl.password}`);
apiUrl.username = '';
apiUrl.password = '';

return {
url: apiUrl.toString(),
options: {
headers: {
Authorization: `Basic ${basicAuth}`,
}
}
};
};

const releaseName = TAG || versions.escapeBranchName(BRANCH) || `${packageJson.version}-local-development`;
Expand Down Expand Up @@ -157,24 +166,28 @@ const saveServiceTags = () => {
fs.writeFileSync(tagsFilePath, JSON.stringify(tags));
};

const updateServiceWorker = () => {
const updateSWUrl = getApiUrl('/api/v2/upgrade/service-worker');
const updateServiceWorker = async () => {
const { url, options } = getApiUrl('/api/v2/upgrade/service-worker');

return rpn.get(updateSWUrl).catch(err => {
if (err.status === 401) {
throw new Error('Environment variable COUCH_URL has invalid authentication');
try {
const response = await fetch(url, options);
if (response.ok) {
return;
}
if (err.status === 403) {
throw new Error('Environment variable COUCH_URL must have admin authentication');

throw response;
} catch (err) {
if (err.status === 401 || err.status === 403) {
throw new Error('Environment variable COUCH_URL does not have valid authentication');
}

if (err.error && err.error.code === 'ECONNREFUSED') {
if (err.cause?.code === 'ECONNREFUSED') {
console.warn('API could not be reached, so the service-worker has not been updated. ');
return;
}

throw err;
});
}
};

const setDdocsVersion = () => {
Expand Down
15 changes: 6 additions & 9 deletions scripts/bulk-password-update-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED=0;
const minimist = require('minimist');
const {promises: fsPromises} = require('fs');
const readline = require('readline');
const rpn = require('request-promise-native');
const {randomInt} = require('crypto');
const csvSync = require('csv-parse/sync');

Expand Down Expand Up @@ -46,10 +45,10 @@ const user = argv.user;
const password = argv.password;

const options = {
uri: url.href,
json: true,
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from(`${user}:${password}`).toString('base64')
'Authorization': 'Basic ' + btoa(`${user}:${password}`),
'Content-Type': 'application/json'
}
};

Expand All @@ -73,18 +72,16 @@ Do you want to continue? [y/N]

const changeUserPass = async (user, options) => {
const postOptions = {...options};
postOptions.body = {
'password': user.pass
};
postOptions.uri = `${options.uri}/${user.name}`;
postOptions.body = JSON.stringify({ password: user.pass });
const uri = `${url.href}/${user.name}`;
try {
if (admins.includes(user.name)) {
throw new Error(`403 - Password change for "${user.name}" not allowed .`);
}
if (user.name.toString().trim() === '') {
throw new Error(`404 - Username is blank - check CSV and run again.`);
}
await rpn.post(postOptions);
await fetch(uri, postOptions);
console.log('SUCCESS', user.name, user.pass);
} catch (e) {
console.log('ERROR', user.name, e.message);
Expand Down
13 changes: 0 additions & 13 deletions scripts/generate-form-attachments/.eslintrc.json

This file was deleted.

39 changes: 0 additions & 39 deletions scripts/generate-form-attachments/README.md

This file was deleted.

13 changes: 0 additions & 13 deletions scripts/generate-form-attachments/package-lock.json

This file was deleted.

12 changes: 0 additions & 12 deletions scripts/generate-form-attachments/package.json

This file was deleted.

163 changes: 0 additions & 163 deletions scripts/generate-form-attachments/src/create-attachments.js

This file was deleted.

17 changes: 0 additions & 17 deletions scripts/generate-form-attachments/src/index.js

This file was deleted.

5 changes: 0 additions & 5 deletions scripts/generate-form-attachments/test/.eslintrc.json

This file was deleted.

Loading

0 comments on commit b3cee4a

Please sign in to comment.