@wp-playground/storage 0.9.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +339 -0
- package/README.md +11 -0
- package/index.cjs +1 -0
- package/index.d.ts +1084 -0
- package/index.js +305 -0
- package/package.json +47 -0
package/index.js
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { Semaphore as d, normalizePath as g, joinPaths as m } from "@php-wasm/util";
|
|
2
|
+
import { Octokit as b } from "octokit";
|
|
3
|
+
function B(t) {
|
|
4
|
+
return new b({
|
|
5
|
+
auth: t
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
function D(t, e = "") {
|
|
9
|
+
e.length && !e.endsWith("/") && (e += "/");
|
|
10
|
+
const a = {};
|
|
11
|
+
for (const r of t)
|
|
12
|
+
r.path.startsWith(e) && (a[r.path.substring(e.length)] = r.content);
|
|
13
|
+
return a;
|
|
14
|
+
}
|
|
15
|
+
async function T(t, e, a, r, n, s = {}) {
|
|
16
|
+
s.progress || (s.progress = {
|
|
17
|
+
foundFiles: 0,
|
|
18
|
+
downloadedFiles: 0
|
|
19
|
+
});
|
|
20
|
+
const { onProgress: i } = s, o = [], f = [], { data: u } = await t.rest.repos.getContent({
|
|
21
|
+
owner: e,
|
|
22
|
+
repo: a,
|
|
23
|
+
path: n,
|
|
24
|
+
ref: r
|
|
25
|
+
});
|
|
26
|
+
if (!Array.isArray(u))
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Expected the list of files to be an array, but got ${typeof u}`
|
|
29
|
+
);
|
|
30
|
+
for (const c of u)
|
|
31
|
+
c.type === "file" ? (++s.progress.foundFiles, i == null || i(s.progress), o.push(
|
|
32
|
+
q(t, e, a, r, c).then((y) => (++s.progress.downloadedFiles, i == null || i(s.progress), y))
|
|
33
|
+
)) : c.type === "dir" && f.push(
|
|
34
|
+
T(
|
|
35
|
+
t,
|
|
36
|
+
e,
|
|
37
|
+
a,
|
|
38
|
+
r,
|
|
39
|
+
c.path,
|
|
40
|
+
s
|
|
41
|
+
)
|
|
42
|
+
);
|
|
43
|
+
const l = await Promise.all(o), p = (await Promise.all(f)).flatMap(
|
|
44
|
+
(c) => c
|
|
45
|
+
);
|
|
46
|
+
return [...l, ...p];
|
|
47
|
+
}
|
|
48
|
+
const A = new d({ concurrency: 15 });
|
|
49
|
+
async function q(t, e, a, r, n) {
|
|
50
|
+
const s = await A.acquire();
|
|
51
|
+
try {
|
|
52
|
+
const { data: i } = await t.rest.repos.getContent({
|
|
53
|
+
owner: e,
|
|
54
|
+
repo: a,
|
|
55
|
+
ref: r,
|
|
56
|
+
path: n.path
|
|
57
|
+
});
|
|
58
|
+
if (!("content" in i))
|
|
59
|
+
throw new Error(`No content found for ${n.path}`);
|
|
60
|
+
return {
|
|
61
|
+
name: n.name,
|
|
62
|
+
path: n.path,
|
|
63
|
+
content: F(i.content)
|
|
64
|
+
};
|
|
65
|
+
} finally {
|
|
66
|
+
s();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function F(t) {
|
|
70
|
+
const e = window.atob(t), a = e.length, r = new Uint8Array(a);
|
|
71
|
+
for (let n = 0; n < a; n++)
|
|
72
|
+
r[n] = e.charCodeAt(n);
|
|
73
|
+
return r;
|
|
74
|
+
}
|
|
75
|
+
async function R(t, e, a, r, n) {
|
|
76
|
+
var l;
|
|
77
|
+
const { data: s } = await t.rest.pulls.get({
|
|
78
|
+
owner: e,
|
|
79
|
+
repo: a,
|
|
80
|
+
pull_number: r
|
|
81
|
+
}), o = (l = (await t.rest.actions.listWorkflowRuns({
|
|
82
|
+
owner: e,
|
|
83
|
+
repo: a,
|
|
84
|
+
branch: s.head.ref,
|
|
85
|
+
workflow_id: n
|
|
86
|
+
})).data.workflow_runs[0]) == null ? void 0 : l.id, f = await t.rest.actions.listWorkflowRunArtifacts({
|
|
87
|
+
owner: e,
|
|
88
|
+
repo: a,
|
|
89
|
+
run_id: o
|
|
90
|
+
});
|
|
91
|
+
return (await t.rest.actions.downloadArtifact({
|
|
92
|
+
owner: e,
|
|
93
|
+
repo: a,
|
|
94
|
+
artifact_id: f.data.artifacts[0].id,
|
|
95
|
+
archive_format: "zip"
|
|
96
|
+
})).data;
|
|
97
|
+
}
|
|
98
|
+
async function _(t, e, a) {
|
|
99
|
+
var s;
|
|
100
|
+
const { data: r, headers: n } = await t.request(
|
|
101
|
+
"GET /repos/{owner}/{repo}",
|
|
102
|
+
{
|
|
103
|
+
owner: e,
|
|
104
|
+
repo: a
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
return !(!n["x-oauth-scopes"] || !((s = r.permissions) != null && s.push));
|
|
108
|
+
}
|
|
109
|
+
async function k(t, e, a, r, n) {
|
|
110
|
+
await t.request("GET /repos/{owner}/{repo}/branches/{branch}", {
|
|
111
|
+
owner: e,
|
|
112
|
+
repo: a,
|
|
113
|
+
branch: r
|
|
114
|
+
}).then(
|
|
115
|
+
() => !0,
|
|
116
|
+
() => !1
|
|
117
|
+
) ? await t.request("PATCH /repos/{owner}/{repo}/git/refs/{ref}", {
|
|
118
|
+
owner: e,
|
|
119
|
+
repo: a,
|
|
120
|
+
sha: n,
|
|
121
|
+
ref: `heads/${r}`
|
|
122
|
+
}) : await t.request("POST /repos/{owner}/{repo}/git/refs", {
|
|
123
|
+
owner: e,
|
|
124
|
+
repo: a,
|
|
125
|
+
sha: n,
|
|
126
|
+
ref: `refs/heads/${r}`
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
async function G(t, e, a) {
|
|
130
|
+
const r = await t.request("GET /user");
|
|
131
|
+
return (await t.request("GET /repos/{owner}/{repo}/forks", {
|
|
132
|
+
owner: e,
|
|
133
|
+
repo: a
|
|
134
|
+
})).data.find(
|
|
135
|
+
(i) => i.owner && i.owner.login === r.data.login
|
|
136
|
+
) || await t.request("POST /repos/{owner}/{repo}/forks", {
|
|
137
|
+
owner: e,
|
|
138
|
+
repo: a
|
|
139
|
+
}), r.data.login;
|
|
140
|
+
}
|
|
141
|
+
async function W(t, e, a, r, n, s) {
|
|
142
|
+
const {
|
|
143
|
+
data: { sha: i }
|
|
144
|
+
} = await t.request("POST /repos/{owner}/{repo}/git/commits", {
|
|
145
|
+
owner: e,
|
|
146
|
+
repo: a,
|
|
147
|
+
message: r,
|
|
148
|
+
tree: s,
|
|
149
|
+
parents: [n]
|
|
150
|
+
});
|
|
151
|
+
return i;
|
|
152
|
+
}
|
|
153
|
+
async function $(t, e, a, r, n) {
|
|
154
|
+
const s = await P(
|
|
155
|
+
t,
|
|
156
|
+
e,
|
|
157
|
+
a,
|
|
158
|
+
r,
|
|
159
|
+
n
|
|
160
|
+
);
|
|
161
|
+
if (s.length === 0)
|
|
162
|
+
return null;
|
|
163
|
+
const {
|
|
164
|
+
data: { sha: i }
|
|
165
|
+
} = await t.request("POST /repos/{owner}/{repo}/git/trees", {
|
|
166
|
+
owner: e,
|
|
167
|
+
repo: a,
|
|
168
|
+
base_tree: r,
|
|
169
|
+
tree: s
|
|
170
|
+
});
|
|
171
|
+
return i;
|
|
172
|
+
}
|
|
173
|
+
async function P(t, e, a, r, n) {
|
|
174
|
+
const s = [];
|
|
175
|
+
for (const [i, o] of n.create)
|
|
176
|
+
s.push(h(t, e, a, i, o));
|
|
177
|
+
for (const [i, o] of n.update)
|
|
178
|
+
s.push(h(t, e, a, i, o));
|
|
179
|
+
for (const i of n.delete)
|
|
180
|
+
s.push(E(t, e, a, r, i));
|
|
181
|
+
return Promise.all(s).then(
|
|
182
|
+
(i) => i.filter((o) => !!o)
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
const w = new d({ concurrency: 10 });
|
|
186
|
+
async function h(t, e, a, r, n) {
|
|
187
|
+
const s = await w.acquire();
|
|
188
|
+
try {
|
|
189
|
+
if (ArrayBuffer.isView(n))
|
|
190
|
+
try {
|
|
191
|
+
const i = new TextDecoder("utf-8", {
|
|
192
|
+
fatal: !0
|
|
193
|
+
}).decode(n);
|
|
194
|
+
return {
|
|
195
|
+
path: r,
|
|
196
|
+
content: i,
|
|
197
|
+
mode: "100644"
|
|
198
|
+
};
|
|
199
|
+
} catch {
|
|
200
|
+
const {
|
|
201
|
+
data: { sha: o }
|
|
202
|
+
} = await t.rest.git.createBlob({
|
|
203
|
+
owner: e,
|
|
204
|
+
repo: a,
|
|
205
|
+
encoding: "base64",
|
|
206
|
+
content: C(n)
|
|
207
|
+
});
|
|
208
|
+
return {
|
|
209
|
+
path: r,
|
|
210
|
+
sha: o,
|
|
211
|
+
mode: "100644"
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
else
|
|
215
|
+
return {
|
|
216
|
+
path: r,
|
|
217
|
+
content: n,
|
|
218
|
+
mode: "100644"
|
|
219
|
+
};
|
|
220
|
+
} finally {
|
|
221
|
+
s();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async function E(t, e, a, r, n) {
|
|
225
|
+
const s = await w.acquire();
|
|
226
|
+
try {
|
|
227
|
+
return await t.request("HEAD /repos/{owner}/{repo}/contents/:path", {
|
|
228
|
+
owner: e,
|
|
229
|
+
repo: a,
|
|
230
|
+
ref: r,
|
|
231
|
+
path: n
|
|
232
|
+
}), {
|
|
233
|
+
path: n,
|
|
234
|
+
mode: "100644",
|
|
235
|
+
sha: null
|
|
236
|
+
};
|
|
237
|
+
} catch {
|
|
238
|
+
return;
|
|
239
|
+
} finally {
|
|
240
|
+
s();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function C(t) {
|
|
244
|
+
const e = [], a = t.byteLength;
|
|
245
|
+
for (let r = 0; r < a; r++)
|
|
246
|
+
e.push(String.fromCharCode(t[r]));
|
|
247
|
+
return window.btoa(e.join(""));
|
|
248
|
+
}
|
|
249
|
+
async function* j(t, e, { exceptPaths: a = [] } = {}) {
|
|
250
|
+
if (e = g(e), !await t.isDir(e)) {
|
|
251
|
+
await t.fileExists(e) && (yield {
|
|
252
|
+
path: e,
|
|
253
|
+
read: async () => await t.readFileAsBuffer(e)
|
|
254
|
+
});
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const r = [e];
|
|
258
|
+
for (; r.length; ) {
|
|
259
|
+
const n = r.pop();
|
|
260
|
+
if (!n)
|
|
261
|
+
return;
|
|
262
|
+
const s = await t.listFiles(n);
|
|
263
|
+
for (const i of s) {
|
|
264
|
+
const o = m(n, i);
|
|
265
|
+
a.includes(o.substring(e.length + 1)) || (await t.isDir(o) ? r.push(o) : yield {
|
|
266
|
+
path: o,
|
|
267
|
+
read: async () => await t.readFileAsBuffer(o)
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async function v(t, e) {
|
|
273
|
+
const a = {
|
|
274
|
+
create: /* @__PURE__ */ new Map(),
|
|
275
|
+
update: /* @__PURE__ */ new Map(),
|
|
276
|
+
delete: /* @__PURE__ */ new Set()
|
|
277
|
+
}, r = /* @__PURE__ */ new Set();
|
|
278
|
+
for await (const n of e) {
|
|
279
|
+
r.add(n.path);
|
|
280
|
+
const s = t.get(n.path), i = await n.read();
|
|
281
|
+
s ? S(s, i) || a.update.set(n.path, i) : a.create.set(n.path, i);
|
|
282
|
+
}
|
|
283
|
+
for (const n of t.keys())
|
|
284
|
+
r.has(n) || a.delete.add(n);
|
|
285
|
+
return a;
|
|
286
|
+
}
|
|
287
|
+
function S(t, e) {
|
|
288
|
+
return t.length === e.length && t.every((a, r) => a === e[r]);
|
|
289
|
+
}
|
|
290
|
+
export {
|
|
291
|
+
v as changeset,
|
|
292
|
+
B as createClient,
|
|
293
|
+
W as createCommit,
|
|
294
|
+
k as createOrUpdateBranch,
|
|
295
|
+
$ as createTree,
|
|
296
|
+
h as createTreeNode,
|
|
297
|
+
P as createTreeNodes,
|
|
298
|
+
E as deleteFile,
|
|
299
|
+
D as filesListToObject,
|
|
300
|
+
G as fork,
|
|
301
|
+
R as getArtifact,
|
|
302
|
+
T as getFilesFromDirectory,
|
|
303
|
+
j as iterateFiles,
|
|
304
|
+
_ as mayPush
|
|
305
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wp-playground/storage",
|
|
3
|
+
"version": "0.9.16",
|
|
4
|
+
"description": "Bindings for storing WordPress Playground on different backends.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/WordPress/wordpress-playground"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://developer.wordpress.org/playground",
|
|
10
|
+
"author": "The WordPress contributors",
|
|
11
|
+
"contributors": [
|
|
12
|
+
{
|
|
13
|
+
"name": "Adam Zielinski",
|
|
14
|
+
"email": "adam@adamziel.com",
|
|
15
|
+
"url": "https://github.com/adamziel"
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./index.js",
|
|
21
|
+
"require": "./index.cjs"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"typedoc": {
|
|
26
|
+
"entryPoint": "./src/index.ts",
|
|
27
|
+
"readmeFile": "./README.md",
|
|
28
|
+
"displayName": "@wp-playground/storage",
|
|
29
|
+
"tsconfig": "./tsconfig.lib.json"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public",
|
|
33
|
+
"directory": "../../../dist/packages/playground/storage"
|
|
34
|
+
},
|
|
35
|
+
"license": "GPL-2.0-or-later",
|
|
36
|
+
"type": "module",
|
|
37
|
+
"gitHead": "47110de9a9efc876f7b432c88d904ef081d1365c",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"comlink": "^4.4.1",
|
|
40
|
+
"ini": "4.1.2",
|
|
41
|
+
"octokit": "3.1.1",
|
|
42
|
+
"@php-wasm/universal": "0.9.16",
|
|
43
|
+
"@php-wasm/util": "0.9.16",
|
|
44
|
+
"@php-wasm/node-polyfills": "0.9.16"
|
|
45
|
+
},
|
|
46
|
+
"main": "index.js"
|
|
47
|
+
}
|