-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
123 lines (109 loc) · 2.82 KB
/
index.js
File metadata and controls
123 lines (109 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const { Octokit } = require("@octokit/rest");
const base64 = require("nodejs-base64-converter");
/**
* Generate a random string of given length
*/
function makeId(length = 10) {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
/**
* Format bytes to human-readable size
*/
function formatSize(bytes) {
if (bytes < 1024) return bytes + " B";
if (bytes < 1024 ** 2) return (bytes / 1024).toFixed(1) + " KB";
if (bytes < 1024 ** 3) return (bytes / 1024 ** 2).toFixed(1) + " MB";
if (bytes < 1024 ** 4) return (bytes / 1024 ** 3).toFixed(1) + " GB";
return (bytes / 1024 ** 4).toFixed(1) + " TB";
}
/**
* Check if input is valid JSON
*/
function isJson(data) {
if (!data || typeof data !== "string") return false;
try {
JSON.parse(data);
return true;
} catch {
return false;
}
}
/**
* Create a GitHub driver instance (functional config)
*/
function createDriver(options = {}) {
const opts = {
owner: options.username,
repo: options.repository,
path: options.path || "file.json",
message: options.message || makeId(10),
...options,
};
const baseUrl = `https://api.github.com/repos/${opts.owner}/${opts.repo}/contents/${opts.path}`;
const octokit = new Octokit({ auth: opts.token });
return { opts, baseUrl, octokit };
}
/**
* Fetch file from GitHub
*/
async function fetchFile(driver) {
const { baseUrl, octokit } = driver;
try {
const { data } = await octokit.request("GET " + baseUrl);
return {
status: true,
size: formatSize(data.size),
sha: data.sha,
content: JSON.parse(base64.decode(data.content)),
};
} catch (err) {
return { status: false, msg: err.message };
}
}
/**
* Save JSON file to GitHub
*/
async function saveFile(driver, data, extraOptions = {}) {
const { baseUrl, octokit, opts } = driver;
const jsonData =
typeof data === "object" || Array.isArray(data)
? JSON.stringify(data)
: data;
if (!isJson(jsonData)) {
return {
status: false,
msg: "data type is invalid, make sure the data is in json format",
};
}
const existing = await fetchFile(driver);
try {
const { data: response } = await octokit.request("PUT " + baseUrl, {
sha: existing.status ? existing.sha : undefined,
...opts,
content: base64.encode(jsonData),
...extraOptions,
});
return {
status: true,
filename: response.content.name,
path: response.content.path,
size: formatSize(response.content.size),
commit: response.commit.message,
};
} catch (err) {
return { status: false, msg: err.message };
}
}
module.exports = {
createDriver,
fetchFile,
saveFile,
formatSize,
isJson,
makeId,
};