diff --git a/src/services/github-service.ts b/src/services/github-service.ts index a87843f..2ce2f12 100644 --- a/src/services/github-service.ts +++ b/src/services/github-service.ts @@ -1,4 +1,3 @@ -/* global Buffer */ import { requestUrl, RequestUrlResponse, RequestUrlParam } from 'obsidian'; import { GitServiceInterface } from './git-service-interface'; @@ -88,7 +87,7 @@ export class GitHubService implements GitServiceInterface { } const data = (response.json as unknown) as GitHubFileResponse; - const decodedContent = Buffer.from(data.content, 'base64').toString('utf8'); + const decodedContent = this.fromBase64(data.content); return { content: decodedContent, @@ -103,7 +102,7 @@ export class GitHubService implements GitServiceInterface { const body: Record = { message: commitMessage, - content: Buffer.from(content, 'utf8').toString('base64'), + content: this.toBase64(content), branch }; @@ -266,4 +265,22 @@ export class GitHubService implements GitServiceInterface { .filter(item => item.type === 'blob' && item.path.endsWith('.gitignore')) .map(item => item.path); } + + private toBase64(str: string): string { + const bytes = new TextEncoder().encode(str); + let binary = ''; + bytes.forEach((byte) => { + binary += String.fromCharCode(byte); + }); + return btoa(binary); + } + + private fromBase64(base64: string): string { + const binary = atob(base64.replace(/\s/g, '')); + const bytes = new Uint8Array(binary.length); + for (let i = 0; i < binary.length; i++) { + bytes[i] = binary.charCodeAt(i); + } + return new TextDecoder().decode(bytes); + } } diff --git a/src/services/gitlab-service.ts b/src/services/gitlab-service.ts index 9442fcf..1e785f6 100644 --- a/src/services/gitlab-service.ts +++ b/src/services/gitlab-service.ts @@ -1,4 +1,4 @@ -/* global Buffer */ + import { requestUrl, RequestUrlResponse, RequestUrlParam } from 'obsidian'; import { GitServiceInterface } from './git-service-interface'; @@ -58,7 +58,7 @@ export class GitLabService implements GitServiceInterface { const status = error.status || 0; const responseData = error.response; const text = responseData?.text || (responseData?.json ? JSON.stringify(responseData.json) : ''); - + // Re-throw as a standardized response-like object if it looks like one if (status) { return { @@ -94,7 +94,7 @@ export class GitLabService implements GitServiceInterface { } const data = (response.json as unknown) as GitLabFileResponse; - const decodedContent = Buffer.from(data.content, 'base64').toString('utf8'); + const decodedContent = this.fromBase64(data.content); return { content: decodedContent, @@ -118,7 +118,7 @@ export class GitLabService implements GitServiceInterface { body: JSON.stringify({ branch, commit_message: commitMessage, - content: Buffer.from(content, 'utf8').toString('base64'), + content: this.toBase64(content), encoding: 'base64' }) }); @@ -239,4 +239,22 @@ export class GitLabService implements GitServiceInterface { .filter(item => item.type === 'blob' && item.path.endsWith('.gitignore')) .map(item => item.path); } + + private toBase64(str: string): string { + const bytes = new TextEncoder().encode(str); + let binary = ''; + bytes.forEach((byte) => { + binary += String.fromCharCode(byte); + }); + return btoa(binary); + } + + private fromBase64(base64: string): string { + const binary = atob(base64.replace(/\s/g, '')); + const bytes = new Uint8Array(binary.length); + for (let i = 0; i < binary.length; i++) { + bytes[i] = binary.charCodeAt(i); + } + return new TextDecoder().decode(bytes); + } }