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

Avoid using Promise.prototype.finally to better support older devices #1224

Merged
merged 2 commits into from
Feb 27, 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
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ module.exports = {
"plugins": [
"eslint-plugin-import",
"eslint-plugin-jsdoc",
"ban",
"@typescript-eslint",
"@typescript-eslint/tslint"
],
"rules": {
"ban/ban": [
2,
{
"name": ["*", "finally"],
"message": "Promise.prototype.finally is forbidden due to poor support from older devices.\nNote that this linting rule just bans naively all \"finally\" method calls, if in this case it wasn't called on a Promise, you can safely ignore this error",
}
],
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": [
"error",
Expand Down
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"core-js": "3.28.0",
"esbuild": "0.17.10",
"eslint": "8.34.0",
"eslint-plugin-ban": "1.6.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-jsdoc": "40.0.0",
"eslint-plugin-react": "7.32.2",
Expand Down
45 changes: 27 additions & 18 deletions src/core/decrypt/session_events_listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,36 @@ export default function SessionEventsListener(
) : Promise<BufferSource | null> {
let timeoutId : number | undefined;
return new Promise<BufferSource | null>((res, rej) => {
log.debug("DRM: Calling `getLicense`", messageType);
const getLicense = keySystemOptions.getLicense(message, messageType);
const getLicenseTimeout = isNullOrUndefined(getLicenseConfig.timeout) ?
10 * 1000 :
getLicenseConfig.timeout;

if (getLicenseTimeout >= 0) {
timeoutId = setTimeout(() => {
rej(new GetLicenseTimeoutError(
`"getLicense" timeout exceeded (${getLicenseTimeout} ms)`
));
}, getLicenseTimeout) as unknown as number;
}
try {
Promise.resolve(getLicense).then(res, rej);
log.debug("DRM: Calling `getLicense`", messageType);
const getLicense = keySystemOptions.getLicense(message, messageType);
const getLicenseTimeout = isNullOrUndefined(getLicenseConfig.timeout) ?
10 * 1000 :
getLicenseConfig.timeout;

if (getLicenseTimeout >= 0) {
timeoutId = setTimeout(() => {
rej(new GetLicenseTimeoutError(
`"getLicense" timeout exceeded (${getLicenseTimeout} ms)`
));
}, getLicenseTimeout) as unknown as number;
}
Promise.resolve(getLicense)
.then(clearTimeoutAndResolve, clearTimeoutAndReject);
} catch (err) {
rej(err);
clearTimeoutAndReject(err);
}
}).finally(() => {
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
function clearTimeoutAndResolve(data : BufferSource | null) {
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
}
res(data);
}
function clearTimeoutAndReject(err : unknown) {
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
}
rej(err);
}
});
}
Expand Down
18 changes: 14 additions & 4 deletions src/core/fetchers/utils/schedule_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,28 @@ export async function scheduleRequestWithCdns<T>(
throw cancellationSignal.cancellationError;
}
if (updatedPrioritaryCdn === undefined) {
return rej(prevRequestError);
return cleanAndReject(prevRequestError);
}
if (updatedPrioritaryCdn !== nextWantedCdn) {
canceller.cancel();
waitPotentialBackoffAndRequest(updatedPrioritaryCdn, prevRequestError)
.then(res, rej);
.then(cleanAndResolve, cleanAndReject);
}
}, canceller.signal);

cancellableSleep(blockedFor, canceller.signal)
.then(() => requestCdn(nextWantedCdn).then(res, rej), noop);
}).finally(unlinkCanceller);
.then(() => requestCdn(nextWantedCdn)
.then(cleanAndResolve, cleanAndReject), noop);

function cleanAndResolve(response : T) {
unlinkCanceller();
res(response);
}
function cleanAndReject(err : unknown) {
unlinkCanceller();
rej(err);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ export default class VideoThumbnailLoader {
segmentBuffer,
lastRepInfo.segmentFetcher,
requestCanceller.signal)
.finally(unlinkSignal);
.then(unlinkSignal, (err) => {
unlinkSignal();
throw err;
});
const newReq = {
segmentId: segment.id,
canceller: requestCanceller,
Expand Down
7 changes: 5 additions & 2 deletions src/transports/dash/add_segment_integrity_checks_to_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export default function addSegmentIntegrityChecks<T>(
}
},
})
.finally(() => cleanUpCancellers())
.then(
(info) => {
cleanUpCancellers();
if (requestCanceller.isUsed()) {
return;
}
Expand All @@ -69,7 +69,10 @@ export default function addSegmentIntegrityChecks<T>(
}
resolve(info);
},
reject
(err : unknown) => {
cleanUpCancellers();
reject(err);
}
);

function cleanUpCancellers() {
Expand Down