Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removing dependancy for node-requests #311

Merged
merged 7 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"brace-style": ["error", "stroustrup", { "allowSingleLine": true }],
"no-param-reassign": ["off"],
"func-names": ["off"],
no-plusplus: ["error", { "allowForLoopAfterthoughts": true }],
"no-plusplus": ["error", { "allowForLoopAfterthoughts": true }]
}
}
}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [12.x, 14.x, 16.x, 18.x]
node: [16.x, 18.x, 20.x]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [12.x]
node: [18.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node }}
Expand All @@ -23,3 +23,5 @@ jobs:
run: npm run report-coverage
- name: Run codecov
uses: codecov/codecov-action@v1
with:
files: ./mocha.lcov,./jasmine.lcov
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ sample/*/package-lock.json
out/
.nyc_output
coverage.lcov
mocha.lcov
jasmine.lcov
148 changes: 90 additions & 58 deletions lib/archiving.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var request = require('request');
var errors = require('./errors');
var pkg = require('../package.json');
var _ = require('lodash');
var generateJwt = require('./generateJwt');
var fetch = require('node-fetch');

// functions
var api;
Expand All @@ -11,7 +11,7 @@ var generateHeaders = function generateHeaders(config) {
return {
'User-Agent': 'OpenTok-Node-SDK/' + pkg.version,
'X-OPENTOK-AUTH': generateJwt(config),
Accept: 'application/json'
Accept: 'application/json',
};
};

Expand Down Expand Up @@ -191,17 +191,35 @@ function Archive(config, properties) {

api = function (config, method, path, body, callback) {
var rurl = config.apiEndpoint + '/v2/project/' + config.apiKey + path;
request.defaults(_.pick(config, 'proxy', 'timeout'))(

const headers = generateHeaders(config);

if (body && ['POST', 'PATCH', 'PUT'].includes(method)) {
headers['Content-Type'] = 'application/json';
}

Promise.resolve(fetch(
rurl,
{
url: rurl,
method: method,
headers: generateHeaders(config),
json: body
},
callback
);
body: body ? JSON.stringify(body) : null,
headers: headers,
}
))
.then(async (response) => {
const otResponse = {
statusCode: response.status,
}
let body = await response.text();

callback(null, otResponse, body)
})
.catch(async (error) => {
callback(error);
});
};


exports.listArchives = function (config, options, callback) {
var qs = [];

Expand Down Expand Up @@ -287,26 +305,32 @@ exports.startArchive = function (ot, config, sessionId, options, callback) {
function startArchiveCallback(err, response, body) {
if (err) {
callback(err);
return;
}
else if (response.statusCode !== 200) {
if (response && response.statusCode === 404) {
try {
body = JSON.parse(body);
} catch {}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter complains that this (and other instances of catch {}) results in a parsing error. (Replace with catch (error) { }).

There are already existing linting errors in the main branch. But if you add /* eslint-env es2018 */ to the top of this file, you will see more linting errors.

switch (response.statusCode) {
case 404:
callback(new errors.ArchiveError('Session not found'));
}
else if (response && response.statusCode === 403) {
return;
case 403:
callback(new errors.AuthError('Invalid API key or secret'));
}
else if (response && response.statusCode === 409) {
return;
case 409:
callback(new errors.ArchiveError('Recording already in progress or session not using OpenTok Media Router'));
}
else {
return;
case 200:
if (body.status !== 'started') {
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || { statusCode: response.statusCode, statusMessage: response.statusMessage })));
return;
}

callback(null, new Archive(config, body));
return;
default:
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || { statusCode: response.statusCode, statusMessage: response.statusMessage })));
}
}
else if (body.status !== 'started') {
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || { statusCode: response.statusCode, statusMessage: response.statusMessage })));
}
else {
callback(null, new Archive(config, body));
}
}
);
Expand All @@ -328,23 +352,30 @@ exports.stopArchive = function (config, archiveId, callback) {
function stopArchiveCallback(err, response, body) {
if (err) {
callback(err);
return;
}

let responseJson = {};
try {
responseJson = JSON.parse(body);
} catch {

}
else if (response.statusCode !== 200) {
if (response && response.statusCode === 404) {
switch (response?.statusCode) {
case 200:
callback(null, new Archive(config, responseJson));
break;
case 404:
callback(new errors.ArchiveError('Archive not found'));
}
else if (response && response.statusCode === 409) {
callback(new errors.ArchiveError(body.message));
}
else if (response && response.statusCode === 403) {
break;
case 409:
callback(new errors.ArchiveError(responseJson.message));
break;
case 403:
callback(new errors.AuthError('Invalid API key or secret'));
}
else {
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || { statusCode: response.statusCode, statusMessage: response.statusMessage })));
}
}
else {
callback(null, new Archive(config, body));
break;
default:
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify({ statusCode: response.statusCode, statusMessage: response.statusMessage })));
}
}
);
Expand Down Expand Up @@ -372,20 +403,21 @@ exports.getArchive = function (config, archiveId, callback) {
err = _err;
}
}
if (err || response.statusCode !== 200) {
if (response && response.statusCode === 404) {

switch(response.statusCode) {
case 200:
callback(null, new Archive(config, body));
return;
case 404:
callback(new errors.ArchiveError('Archive not found'));
}
else if (response && response.statusCode === 403) {
return;
case 403:
callback(new errors.AuthError('Invalid API key or secret'));
}
else {
return;
default:
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || { statusCode: response.statusCode, statusMessage: response.statusMessage })));
}
}
else {
callback(null, new Archive(config, body));
}

}
);
};
Expand Down Expand Up @@ -501,19 +533,19 @@ exports.deleteArchive = function (config, archiveId, callback) {
'/archive/' + encodeURIComponent(archiveId),
null,
function deleteArchiveCallback(err, response, body) {
if (err || response.statusCode !== 204) {
if (response && response.statusCode === 404) {
switch (response.statusCode) {
case 204:
callback(null);
return;
case 404:
callback(new errors.ArchiveError('Archive not found'));
}
else if (response && response.statusCode === 403) {
return
case 403:
callback(new errors.AuthError('Invalid API key or secret'));
}
else {
return;
default:
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || { statusCode: response.statusCode, statusMessage: response.statusMessage })));
}
}
else {
callback(null);
return;
}
}
);
Expand Down
Loading
Loading