Skip to content

Commit 2d3c9ad

Browse files
committed
upload to cloudflare store
1 parent 834d0af commit 2d3c9ad

File tree

2 files changed

+100
-25
lines changed

2 files changed

+100
-25
lines changed

dist/main.js

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ var require$$1 = require('events');
77
var childProcess = require('child_process');
88
var util_1 = require('util');
99
var assert_1 = require('assert');
10+
var https = require('https');
11+
var http = require('http');
12+
var url = require('url');
1013

1114
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
1215

@@ -1580,6 +1583,72 @@ var exec_1 = /*#__PURE__*/Object.defineProperty({
15801583
exec: exec_2
15811584
}, '__esModule', {value: true});
15821585

1586+
function toError(rej, res, err) {
1587+
err = err || new Error(res.statusMessage);
1588+
err.statusMessage = res.statusMessage;
1589+
err.statusCode = res.statusCode;
1590+
err.headers = res.headers;
1591+
err.data = res.data;
1592+
rej(err);
1593+
}
1594+
1595+
function send(method, uri, opts={}) {
1596+
return new Promise((res, rej) => {
1597+
let out = '';
1598+
opts.method = method;
1599+
let { redirect=true } = opts;
1600+
if (uri && !!uri.toJSON) uri = uri.toJSON();
1601+
Object.assign(opts, typeof uri === 'string' ? url.parse(uri) : uri);
1602+
opts.agent = opts.protocol === 'http:' ? http.globalAgent : void 0;
1603+
1604+
let req = https.request(opts, r => {
1605+
r.setEncoding('utf8');
1606+
1607+
r.on('data', d => {
1608+
out += d;
1609+
});
1610+
1611+
r.on('end', () => {
1612+
let type = r.headers['content-type'];
1613+
if (type && out && type.includes('application/json')) {
1614+
try {
1615+
out = JSON.parse(out, opts.reviver);
1616+
} catch (err) {
1617+
return toError(rej, r, err);
1618+
}
1619+
}
1620+
r.data = out;
1621+
if (r.statusCode >= 400) {
1622+
toError(rej, r);
1623+
} else if (r.statusCode > 300 && redirect && r.headers.location) {
1624+
opts.path = url.resolve(opts.path, r.headers.location);
1625+
return send(method, opts.path.startsWith('/') ? opts : opts.path, opts).then(res, rej);
1626+
} else {
1627+
res(r);
1628+
}
1629+
});
1630+
});
1631+
1632+
req.on('error', rej);
1633+
1634+
if (opts.body) {
1635+
let isObj = typeof opts.body === 'object' && !Buffer.isBuffer(opts.body);
1636+
let str = isObj ? JSON.stringify(opts.body) : opts.body;
1637+
isObj && req.setHeader('content-type', 'application/json');
1638+
req.setHeader('content-length', Buffer.byteLength(str));
1639+
req.write(str);
1640+
}
1641+
1642+
req.end();
1643+
});
1644+
}
1645+
1646+
send.bind(null, 'GET');
1647+
send.bind(null, 'POST');
1648+
send.bind(null, 'PATCH');
1649+
send.bind(null, 'DELETE');
1650+
const put = send.bind(null, 'PUT');
1651+
15831652
async function rc_read_file(file_path) {
15841653
let file_or_dir = {
15851654
name: file_path.split("/").pop(),
@@ -26074,6 +26143,12 @@ async function transform(files, project) {
2607426143

2607526144
// docs: Array<Record<string, unknown>>, { project, type, keyby, version }
2607626145

26146+
const CF_ACC_ID = "32a8245cd45a24083dd0acae1d482048";
26147+
const CF_NS_ID = "20394261e26444aaa7ad8292db818037";
26148+
26149+
const API_ROOT = "https://api.cloudflare.com/client/v4/";
26150+
const KV_WRITE = `accounts/${CF_ACC_ID}/storage/kv/namespaces/${CF_NS_ID}/bulk`;
26151+
2607726152
async function get_repo(
2607826153
target_repo,
2607926154
target_branch,
@@ -26116,7 +26191,7 @@ async function get_repo(
2611626191
async function run() {
2611726192
const target_repo = core$4.getInput("repo");
2611826193
const target_branch = core$4.getInput("branch");
26119-
core$4.getInput("token");
26194+
const CF_TOKEN = core$4.getInput("token");
2612026195
const docs_path = core$4.getInput("docs_path");
2612126196
const pkg_path = core$4.getInput("pkg_path");
2612226197

@@ -26161,18 +26236,18 @@ async function run() {
2616126236

2616226237
console.log(JSON.stringify(ready_for_cf, null, 2));
2616326238

26164-
// try {
26165-
// const x = await put(`${API_ROOT}${KV_WRITE}`, {
26166-
// body: ready_for_cf,
26167-
// headers: {
26168-
// Authorization: `Bearer ${CF_TOKEN}`,
26169-
// },
26170-
// });
26171-
// console.log("put: ", x);
26172-
// } catch (e) {
26173-
// console.log("it didn't work", e.message);
26174-
// throw e;
26175-
// }
26239+
try {
26240+
const x = await put(`${API_ROOT}${KV_WRITE}`, {
26241+
body: ready_for_cf,
26242+
headers: {
26243+
Authorization: `Bearer ${CF_TOKEN}`,
26244+
},
26245+
});
26246+
console.log("put: ", x);
26247+
} catch (e) {
26248+
console.log("it didn't work", e.message);
26249+
throw e;
26250+
}
2617626251
}
2617726252

2617826253
run();

src/main.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ async function run() {
100100

101101
console.log(JSON.stringify(ready_for_cf, null, 2));
102102

103-
// try {
104-
// const x = await put(`${API_ROOT}${KV_WRITE}`, {
105-
// body: ready_for_cf,
106-
// headers: {
107-
// Authorization: `Bearer ${CF_TOKEN}`,
108-
// },
109-
// });
110-
// console.log("put: ", x);
111-
// } catch (e) {
112-
// console.log("it didn't work", e.message);
113-
// throw e;
114-
// }
103+
try {
104+
const x = await put(`${API_ROOT}${KV_WRITE}`, {
105+
body: ready_for_cf,
106+
headers: {
107+
Authorization: `Bearer ${CF_TOKEN}`,
108+
},
109+
});
110+
console.log("put: ", x);
111+
} catch (e) {
112+
console.log("it didn't work", e.message);
113+
throw e;
114+
}
115115
}
116116

117117
run();

0 commit comments

Comments
 (0)