|
|
@@ -21,7 +21,7 @@
|
|
|
* Source.
|
|
|
*/
|
|
|
|
|
|
-/* global fetch, btoa */
|
|
|
+/* global fetch, btoa, AbortController */
|
|
|
|
|
|
export { pushGitHub };
|
|
|
|
|
|
@@ -31,29 +31,40 @@ async function pushGitHub(token, userName, repositoryName, branchName, path, con
|
|
|
while (pendingPush) {
|
|
|
await pendingPush;
|
|
|
}
|
|
|
+ const controller = new AbortController();
|
|
|
pendingPush = async () => {
|
|
|
try {
|
|
|
- await createContent({ path, content });
|
|
|
+ await createContent({ path, content }, controller.signal);
|
|
|
} finally {
|
|
|
pendingPush = null;
|
|
|
}
|
|
|
};
|
|
|
- await pendingPush();
|
|
|
+ return {
|
|
|
+ cancelPush: () => controller.abort(),
|
|
|
+ pushPromise: pendingPush()
|
|
|
+ };
|
|
|
|
|
|
- async function createContent({ path, content, message = "" }) {
|
|
|
- const response = await fetch(`https://api.github.com/repos/${userName}/${repositoryName}/contents/${path}`, {
|
|
|
- method: "PUT",
|
|
|
- headers: new Map([
|
|
|
- ["Authorization", `token ${token}`],
|
|
|
- ["Accept", "application/vnd.github.v3+json"]
|
|
|
- ]),
|
|
|
- body: JSON.stringify({ content: btoa(content), message, branch: branchName })
|
|
|
- });
|
|
|
- const responseData = await response.json();
|
|
|
- if (response.status < 400) {
|
|
|
- return responseData;
|
|
|
- } else {
|
|
|
- throw new Error(responseData.message);
|
|
|
+ async function createContent({ path, content, message = "" }, signal) {
|
|
|
+ try {
|
|
|
+ const response = await fetch(`https://api.github.com/repos/${userName}/${repositoryName}/contents/${path}`, {
|
|
|
+ method: "PUT",
|
|
|
+ headers: new Map([
|
|
|
+ ["Authorization", `token ${token}`],
|
|
|
+ ["Accept", "application/vnd.github.v3+json"]
|
|
|
+ ]),
|
|
|
+ body: JSON.stringify({ content: btoa(unescape(encodeURIComponent(content))), message, branch: branchName }),
|
|
|
+ signal
|
|
|
+ });
|
|
|
+ const responseData = await response.json();
|
|
|
+ if (response.status < 400) {
|
|
|
+ return responseData;
|
|
|
+ } else {
|
|
|
+ throw new Error(responseData.message);
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ if (error.name != "AbortError") {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|