arcadible 1.0.22 → 1.0.24

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.
Files changed (308) hide show
  1. package/README.md +2 -1
  2. package/bin/arc.js +1 -85
  3. package/dist/account.js +197 -0
  4. package/dist/action/build/tool.js +65 -0
  5. package/dist/action/delete/tool.js +46 -0
  6. package/dist/action/deploy/rebuilt.js +25 -0
  7. package/dist/action/deploy/result.js +36 -0
  8. package/dist/action/deploy/timeout.js +18 -0
  9. package/dist/action/deploy/tool-git.js +54 -0
  10. package/dist/action/deploy/tool.js +54 -0
  11. package/dist/action/deploy/wait.js +104 -0
  12. package/dist/action/function/tool.js +127 -0
  13. package/dist/action/game.js +61 -0
  14. package/dist/action/init/manifest.js +64 -0
  15. package/dist/action/init/tool-patch.js +15 -0
  16. package/dist/action/init/tool.js +64 -0
  17. package/dist/action/inspect/tool.js +13 -0
  18. package/dist/action/key/tool.js +87 -0
  19. package/dist/action/login/flow.js +69 -0
  20. package/dist/action/login/tool.js +8 -0
  21. package/dist/action/logout/tool.js +8 -0
  22. package/dist/action/open/tool.js +29 -0
  23. package/dist/action/pack/limits.js +46 -0
  24. package/dist/action/pack/tool.js +150 -0
  25. package/dist/action/pack/unsound.js +51 -0
  26. package/dist/action/play/tool.js +83 -0
  27. package/dist/action/predeploy/ac.js +75 -0
  28. package/dist/action/predeploy/game.js +12 -0
  29. package/dist/action/predeploy/git.js +151 -0
  30. package/dist/action/predeploy/pm.js +20 -0
  31. package/dist/action/predeploy/published.js +54 -0
  32. package/dist/action/predeploy/secrets.js +35 -0
  33. package/dist/action/predeploy/tool-git.js +108 -0
  34. package/dist/action/predeploy/tool.js +16 -0
  35. package/dist/action/secret/tool.js +91 -0
  36. package/dist/action/serve/tool.js +249 -0
  37. package/dist/action/validate/checked.js +28 -0
  38. package/dist/action/validate/tool.js +56 -0
  39. package/dist/action/validate/unknown.js +58 -0
  40. package/dist/action/validate/unsound.js +99 -0
  41. package/dist/action/whoami/tool.js +7 -0
  42. package/dist/api.js +160 -0
  43. package/dist/arc.js +41 -0
  44. package/dist/cli/argv.js +61 -0
  45. package/dist/cli/channel.js +29 -0
  46. package/dist/cli/command.js +163 -0
  47. package/dist/cli/confirm.js +39 -0
  48. package/dist/cli/options.js +62 -0
  49. package/dist/cli/rc.js +103 -0
  50. package/dist/cli/yargs.js +40 -0
  51. package/dist/command/build.js +68 -0
  52. package/dist/command/delete.js +66 -0
  53. package/dist/command/deploy.js +308 -0
  54. package/dist/command/function/index.js +17 -0
  55. package/dist/command/function/pause.js +18 -0
  56. package/dist/command/function/resume.js +18 -0
  57. package/dist/command/function/run.js +41 -0
  58. package/dist/command/function/secret/index.js +15 -0
  59. package/dist/command/function/secret/list.js +27 -0
  60. package/dist/command/function/secret/name.js +8 -0
  61. package/dist/command/function/secret/remove.js +26 -0
  62. package/dist/command/function/secret/run.js +52 -0
  63. package/dist/command/function/secret/set.js +48 -0
  64. package/dist/command/function/status.js +40 -0
  65. package/dist/command/init.js +128 -0
  66. package/dist/command/inspect.js +38 -0
  67. package/dist/command/login.js +108 -0
  68. package/dist/command/logout.js +24 -0
  69. package/dist/command/open.js +48 -0
  70. package/dist/command/pack.js +90 -0
  71. package/dist/command/play.js +80 -0
  72. package/dist/command/serve.js +176 -0
  73. package/dist/command/server/index.js +17 -0
  74. package/dist/command/server/key/create.js +36 -0
  75. package/dist/command/server/key/index.js +15 -0
  76. package/dist/command/server/key/list.js +34 -0
  77. package/dist/command/server/key/revoke.js +24 -0
  78. package/dist/command/server/key/run.js +54 -0
  79. package/dist/command/validate.js +47 -0
  80. package/dist/command/whoami.js +55 -0
  81. package/dist/commands.js +93 -0
  82. package/dist/config/ai.js +2 -0
  83. package/dist/config/art.js +13 -0
  84. package/dist/config/audience.js +2 -0
  85. package/dist/config/code.js +129 -0
  86. package/dist/config/file.js +13 -0
  87. package/dist/config/genre.js +19 -0
  88. package/{src → dist}/config/html.js +2 -7
  89. package/dist/config/image.js +5 -0
  90. package/dist/config/input.js +11 -0
  91. package/dist/config/json.js +23 -0
  92. package/dist/config/limit.js +14 -0
  93. package/dist/config/orientation.js +11 -0
  94. package/dist/config/package.js +8 -0
  95. package/dist/config/product.js +14 -0
  96. package/dist/config/template.js +37 -0
  97. package/dist/config/uri.js +18 -0
  98. package/dist/config/url.js +11 -0
  99. package/dist/config/video.js +5 -0
  100. package/dist/config/visibility.js +2 -0
  101. package/dist/config/workflow.js +7 -0
  102. package/dist/parser/env.js +61 -0
  103. package/dist/parser/given.js +8 -0
  104. package/dist/parser/manifest.js +45 -0
  105. package/dist/parser/media.js +57 -0
  106. package/dist/parser/project.js +129 -0
  107. package/dist/parser/readme-file.js +44 -0
  108. package/dist/parser/readme.js +70 -0
  109. package/dist/parser/sections.js +92 -0
  110. package/dist/parser/self.js +16 -0
  111. package/dist/phrase/account.js +35 -0
  112. package/dist/phrase/common.js +130 -0
  113. package/dist/phrase/deploy.js +184 -0
  114. package/dist/phrase/function.js +135 -0
  115. package/dist/phrase/game.js +27 -0
  116. package/dist/phrase/index.js +15 -0
  117. package/dist/phrase/init.js +53 -0
  118. package/dist/phrase/internal.js +61 -0
  119. package/dist/phrase/manifest.js +120 -0
  120. package/dist/phrase/media.js +90 -0
  121. package/dist/phrase/pack.js +69 -0
  122. package/dist/phrase/serve.js +84 -0
  123. package/dist/phrase/server.js +38 -0
  124. package/dist/runner/bundle.js +95 -0
  125. package/dist/runner/outbound.js +69 -0
  126. package/dist/schema/check/game.js +29 -0
  127. package/dist/schema/check/workflow.js +57 -0
  128. package/dist/schema/game.js +462 -0
  129. package/dist/schema/message.js +61 -0
  130. package/dist/schema/readme.js +27 -0
  131. package/dist/schema/runner.js +43 -0
  132. package/dist/schema/server.js +91 -0
  133. package/dist/schema/workflow.js +99 -0
  134. package/dist/serve/files.js +39 -0
  135. package/dist/serve/game.js +172 -0
  136. package/dist/serve/page.js +490 -0
  137. package/dist/serve/reload.js +55 -0
  138. package/dist/serve/remote.js +114 -0
  139. package/dist/serve/runner.js +296 -0
  140. package/dist/serve/server.js +163 -0
  141. package/dist/serve/ticket.js +232 -0
  142. package/dist/util/bytes.js +16 -0
  143. package/dist/util/chars.js +19 -0
  144. package/dist/util/command.js +11 -0
  145. package/dist/util/delay.js +36 -0
  146. package/dist/util/doc.js +59 -0
  147. package/dist/util/error.js +35 -0
  148. package/dist/util/http.js +13 -0
  149. package/dist/util/image.js +64 -0
  150. package/dist/util/links.js +45 -0
  151. package/dist/util/lockfile.js +17 -0
  152. package/dist/util/log.js +101 -0
  153. package/dist/util/md.js +37 -0
  154. package/dist/util/media.js +27 -0
  155. package/dist/util/objs.js +29 -0
  156. package/dist/util/os.js +13 -0
  157. package/dist/util/output.js +133 -0
  158. package/dist/util/paths.js +8 -0
  159. package/dist/util/progress.js +57 -0
  160. package/dist/util/promise.js +18 -0
  161. package/dist/util/request.js +91 -0
  162. package/dist/util/s3.js +67 -0
  163. package/dist/util/schema.js +151 -0
  164. package/dist/util/screenshot.js +24 -0
  165. package/dist/util/shell.js +64 -0
  166. package/dist/util/size.js +32 -0
  167. package/dist/util/spinner.js +35 -0
  168. package/dist/util/strs.js +46 -0
  169. package/dist/util/table.js +18 -0
  170. package/dist/util/term.js +21 -0
  171. package/dist/util/time.js +16 -0
  172. package/dist/util/toml.js +28 -0
  173. package/{src → dist}/util/uris.js +8 -14
  174. package/dist/util/video.js +113 -0
  175. package/dist/util/zip.js +62 -0
  176. package/npm-shrinkwrap.json +3256 -0
  177. package/package.json +42 -40
  178. package/bin/arc-build.js +0 -139
  179. package/bin/arc-default.js +0 -65
  180. package/bin/arc-deploy.js +0 -456
  181. package/bin/arc-init.js +0 -187
  182. package/bin/arc-inspect.js +0 -97
  183. package/bin/arc-login.js +0 -165
  184. package/bin/arc-logout.js +0 -75
  185. package/bin/arc-open.js +0 -104
  186. package/bin/arc-pack.js +0 -146
  187. package/bin/arc-play.js +0 -140
  188. package/bin/arc-serve.js +0 -214
  189. package/bin/arc-validate.js +0 -112
  190. package/bin/arc-whoami.js +0 -119
  191. package/src/account.js +0 -249
  192. package/src/action/build/tool.js +0 -80
  193. package/src/action/deploy/tool-git.js +0 -90
  194. package/src/action/deploy/tool.js +0 -88
  195. package/src/action/deploy/wait.js +0 -151
  196. package/src/action/init/manifest.js +0 -83
  197. package/src/action/init/tool-patch.js +0 -32
  198. package/src/action/init/tool.js +0 -97
  199. package/src/action/inspect/tool.js +0 -26
  200. package/src/action/login/flow.js +0 -101
  201. package/src/action/login/tool.js +0 -16
  202. package/src/action/logout/tool.js +0 -15
  203. package/src/action/open/tool.js +0 -38
  204. package/src/action/pack/limits.js +0 -51
  205. package/src/action/pack/tool.js +0 -171
  206. package/src/action/pack/unsound.js +0 -69
  207. package/src/action/play/tool.js +0 -109
  208. package/src/action/predeploy/ac.js +0 -112
  209. package/src/action/predeploy/git.js +0 -223
  210. package/src/action/predeploy/pm.js +0 -37
  211. package/src/action/predeploy/published.js +0 -82
  212. package/src/action/predeploy/tool-git.js +0 -154
  213. package/src/action/predeploy/tool.js +0 -29
  214. package/src/action/serve/tool.js +0 -209
  215. package/src/action/validate/checked.js +0 -37
  216. package/src/action/validate/tool.js +0 -67
  217. package/src/action/validate/unknown.js +0 -86
  218. package/src/action/validate/unsound.js +0 -138
  219. package/src/action/whoami/tool.js +0 -15
  220. package/src/api.js +0 -163
  221. package/src/cli/build.js +0 -15
  222. package/src/cli/deploy.js +0 -21
  223. package/src/cli/init.js +0 -17
  224. package/src/cli/inspect.js +0 -15
  225. package/src/cli/login.js +0 -15
  226. package/src/cli/logout.js +0 -15
  227. package/src/cli/open.js +0 -15
  228. package/src/cli/pack.js +0 -15
  229. package/src/cli/play.js +0 -15
  230. package/src/cli/predeploy.js +0 -21
  231. package/src/cli/serve.js +0 -15
  232. package/src/cli/validate.js +0 -15
  233. package/src/cli/whoami.js +0 -15
  234. package/src/config/ai.js +0 -5
  235. package/src/config/art.js +0 -16
  236. package/src/config/audience.js +0 -5
  237. package/src/config/code.js +0 -142
  238. package/src/config/file.js +0 -23
  239. package/src/config/genre.js +0 -22
  240. package/src/config/image.js +0 -7
  241. package/src/config/input.js +0 -14
  242. package/src/config/limit.js +0 -21
  243. package/src/config/orientation.js +0 -14
  244. package/src/config/package.js +0 -5
  245. package/src/config/product.js +0 -5
  246. package/src/config/template.js +0 -45
  247. package/src/config/uri.js +0 -27
  248. package/src/config/url.js +0 -20
  249. package/src/config/video.js +0 -7
  250. package/src/config/visibility.js +0 -5
  251. package/src/config/workflow.js +0 -18
  252. package/src/parser/env.js +0 -84
  253. package/src/parser/given.js +0 -17
  254. package/src/parser/manifest.js +0 -80
  255. package/src/parser/media.js +0 -90
  256. package/src/parser/project.js +0 -166
  257. package/src/parser/readme-file.js +0 -76
  258. package/src/parser/readme.js +0 -96
  259. package/src/parser/sections.js +0 -107
  260. package/src/parser/self.js +0 -27
  261. package/src/phrase.js +0 -1294
  262. package/src/schema/check/game.js +0 -61
  263. package/src/schema/check/workflow.js +0 -99
  264. package/src/schema/game.js +0 -649
  265. package/src/schema/message.js +0 -85
  266. package/src/schema/readme.js +0 -44
  267. package/src/schema/workflow.js +0 -150
  268. package/src/serve/files.js +0 -61
  269. package/src/serve/game.js +0 -191
  270. package/src/serve/page.js +0 -345
  271. package/src/serve/reload.js +0 -68
  272. package/src/serve/remote.js +0 -121
  273. package/src/serve/server.js +0 -128
  274. package/src/util/argv.js +0 -116
  275. package/src/util/bytes.js +0 -25
  276. package/src/util/chars.js +0 -30
  277. package/src/util/command.js +0 -18
  278. package/src/util/delay.js +0 -52
  279. package/src/util/doc.js +0 -72
  280. package/src/util/error.js +0 -50
  281. package/src/util/http.js +0 -25
  282. package/src/util/image.js +0 -93
  283. package/src/util/links.js +0 -78
  284. package/src/util/lockfile.js +0 -35
  285. package/src/util/log.js +0 -149
  286. package/src/util/md.js +0 -61
  287. package/src/util/media.js +0 -40
  288. package/src/util/objs.js +0 -45
  289. package/src/util/os.js +0 -27
  290. package/src/util/output.js +0 -174
  291. package/src/util/paths.js +0 -21
  292. package/src/util/progress.js +0 -85
  293. package/src/util/promise.js +0 -29
  294. package/src/util/rc.js +0 -144
  295. package/src/util/request.js +0 -89
  296. package/src/util/s3.js +0 -92
  297. package/src/util/schema.js +0 -189
  298. package/src/util/screenshot.js +0 -37
  299. package/src/util/shell.js +0 -84
  300. package/src/util/size.js +0 -53
  301. package/src/util/spinner.js +0 -52
  302. package/src/util/strs.js +0 -73
  303. package/src/util/term.js +0 -28
  304. package/src/util/time.js +0 -29
  305. package/src/util/toml.js +0 -14
  306. package/src/util/video.js +0 -157
  307. package/src/util/zip.js +0 -88
  308. /package/{src/serve → assets}/favicon.ico +0 -0
@@ -0,0 +1,104 @@
1
+ // [import]
2
+ import { ended } from "../../account.js";
3
+ import { auth, discoverGameReleases, release as apiRelease, } from "../../api.js";
4
+ import { sleep } from "../../util/delay.js";
5
+ import { git } from "../predeploy/git.js";
6
+ import { HttpError } from "../../util/http.js";
7
+ import { studioUrl } from "../../util/links.js";
8
+ import { m, s } from "../../util/time.js";
9
+ // [declare]
10
+ // how often a release is checked.
11
+ const INTERVAL = s(2);
12
+ const FINAL = new Set(["failed", "published"]);
13
+ const SELECT = "id,game,status,reason,message,build,source,version,channel,created_at";
14
+ // a git release is found by version among the newest few; one from before
15
+ // this deploy (a tag deleted and pushed again) is left out, allowing for
16
+ // the machine's clock being off.
17
+ const SKEW = m(10);
18
+ // [function]
19
+ // a failure worth retrying: the network, or the API having a moment.
20
+ function transient(err) {
21
+ return !(err instanceof HttpError) || !err.status || err.status >= 500;
22
+ }
23
+ // [function]
24
+ // the repository's GitHub Actions page, from origin's URL, or null when
25
+ // origin isn't on GitHub.
26
+ function actionsUrl(root) {
27
+ return git(root, ["remote", "get-url", "origin"]).then((stdout) => {
28
+ const match = /github\.com[:/]([^/\s]+)\/([^/\s]+?)(?:\.git)?\s*$/u.exec(stdout);
29
+ return match
30
+ ? `https://github.com/${match[1]}/${match[2]}/actions`
31
+ : null;
32
+ }, () => null);
33
+ }
34
+ // [function]
35
+ // a direct deploy's release, by its upload's id.
36
+ function findDirect(id, headers) {
37
+ return () => apiRelease(id, { select: SELECT }, headers);
38
+ }
39
+ // [function]
40
+ // a git deploy's release: GitHub's, for this version, made since the tag was
41
+ // pushed. Null until GitHub Actions has built it. Looked for among the
42
+ // game's releases, which anyone can read, with no token: a git deploy asks
43
+ // what's true for whoever runs it. A release with no game (a repository that
44
+ // isn't connected, which the predeploy refuses first) isn't among them.
45
+ function findGit({ game, since, version, }) {
46
+ return () => discoverGameReleases(game, { limit: 10, select: SELECT, version }).then((rows) => (rows ?? []).find((r) => r.source === "github" &&
47
+ r.version === version &&
48
+ r.game === game &&
49
+ Date.parse(r.created_at ?? "") >= since - SKEW) ?? null);
50
+ }
51
+ // [function]
52
+ // checks the release until it's published or failed, or `timeout` seconds
53
+ // pass. Resolves { release, timedOut }; `release` is null when it was never
54
+ // found. `onRelease` hears each release it finds.
55
+ function pollRelease(find, timeout, onRelease) {
56
+ const deadline = Date.now() + timeout * 1000;
57
+ // one check, then the next, each after the last: `last` is the newest
58
+ // release found, kept through a check that fails for a moment.
59
+ const tick = async function (last) {
60
+ let release = last;
61
+ try {
62
+ release = (await find()) ?? last;
63
+ }
64
+ catch (err) {
65
+ if (!transient(err)) {
66
+ throw err;
67
+ }
68
+ }
69
+ if (release && onRelease) {
70
+ onRelease(release);
71
+ }
72
+ if (release && FINAL.has(release.status)) {
73
+ return { release, timedOut: false };
74
+ }
75
+ const left = deadline - Date.now();
76
+ if (left <= 0) {
77
+ return { release, timedOut: true };
78
+ }
79
+ await sleep(Math.min(INTERVAL, left));
80
+ return tick(release);
81
+ };
82
+ return tick(null);
83
+ }
84
+ // [function]
85
+ // waits on a deploy's release. `deployed` is what the deploy resolved: a
86
+ // direct deploy's has the release's id, and a git deploy's its game. A git
87
+ // deploy has no `current`: it never uses a login. Studio's link is for
88
+ // everyone, since Studio asks whoever opens it to log in.
89
+ export function wait(current, deployed, { git: isGit, root, since, timeout }, { onRelease } = {}) {
90
+ const game = deployed.game;
91
+ const find = isGit || !("release" in deployed)
92
+ ? findGit({ game, since, version: deployed.version.to })
93
+ : findDirect(deployed.release, auth(current?.session?.access_token));
94
+ return Promise.all([
95
+ pollRelease(find, timeout, onRelease),
96
+ isGit ? actionsUrl(root) : null,
97
+ ])
98
+ .then(([polled, actions]) => ({
99
+ ...polled,
100
+ actions,
101
+ studio: studioUrl(game, "builds"),
102
+ }))
103
+ .catch(ended(current));
104
+ }
@@ -0,0 +1,127 @@
1
+ // a game's function, paused and resumed: stopped on a channel, every
2
+ // build's, without a release, by the game's owner. Arcadible can pause one
3
+ // too, and so can its allowance, spent: a pause has a reason, and an owner
4
+ // lifts only their own.
5
+ import { deleteGamePause, discoverGameBuilds, gamePauses, gameUsages, putGamePause, } from "../../api.js";
6
+ import { API_REFUSED, GAME_UNAVAILABLE } from "../../config/code.js";
7
+ import { ArcError } from "../../util/error.js";
8
+ import { channelOf, CHANNELS, gameOf, headersOf, isStatus, needOf, } from "../game.js";
9
+ import { functionNotPaused, gameUnavailable } from "../../phrase/index.js";
10
+ // [declare]
11
+ // the reasons a function is paused for, the one that holds longest first:
12
+ // what a status says when there's more than one.
13
+ const REASONS = ["arcadible", "limit", "owner"];
14
+ // [function]
15
+ // why a channel's function is paused, of the game's pauses: the reason that
16
+ // holds longest, or null for one that's running.
17
+ function pausedOf(pauses, channel) {
18
+ const reasons = (pauses ?? [])
19
+ .filter((pause) => pause.channel === channel)
20
+ .map((pause) => pause.reason);
21
+ return REASONS.find((reason) => reasons.includes(reason)) ?? null;
22
+ }
23
+ // [function]
24
+ // whether a channel's live build has a function: true, false, or null when
25
+ // that can't be read, which never stands in a pause's way.
26
+ async function hasFunction(id, channel) {
27
+ try {
28
+ const builds = await discoverGameBuilds(id, {
29
+ channel,
30
+ limit: 1,
31
+ select: "manifest,version",
32
+ });
33
+ const [live] = builds ?? [];
34
+ return live ? Boolean(live.manifest?.function) : false;
35
+ }
36
+ catch {
37
+ return null;
38
+ }
39
+ }
40
+ // [function]
41
+ // pauses the game's function on a channel, as `{ channel, function, paused
42
+ // }`. One that's paused already stays as it was. A channel whose live build
43
+ // has no function is paused all the same, since a stop is never refused,
44
+ // and `function` says so (false), so the answer doesn't read as if
45
+ // something was stopped.
46
+ export async function pause(current, { channel, ...conf }) {
47
+ const where = needOf(channel);
48
+ const id = await gameOf(conf);
49
+ const headers = headersOf(current);
50
+ try {
51
+ const kept = await putGamePause(id, where, null, headers);
52
+ return {
53
+ channel: kept.channel,
54
+ function: await hasFunction(id, where),
55
+ paused: kept.reason,
56
+ };
57
+ }
58
+ catch (err) {
59
+ if (isStatus(err, 404)) {
60
+ throw new ArcError(GAME_UNAVAILABLE, gameUnavailable(id), {
61
+ cause: err,
62
+ });
63
+ }
64
+ throw err;
65
+ }
66
+ }
67
+ // [function]
68
+ // resumes the game's function on a channel: lifts its owner's pause, and
69
+ // resolves with `{ channel, paused }`, `paused` the reason it's still paused
70
+ // for, or null. With no pause of the owner's to lift, it says why: one that
71
+ // is Arcadible's or its allowance's isn't theirs to lift.
72
+ export async function resume(current, { channel, ...conf }) {
73
+ const where = needOf(channel);
74
+ const id = await gameOf(conf);
75
+ const headers = headersOf(current);
76
+ let lifted = true;
77
+ try {
78
+ await deleteGamePause(id, where, null, headers);
79
+ }
80
+ catch (err) {
81
+ if (!isStatus(err, 404)) {
82
+ throw err;
83
+ }
84
+ lifted = false;
85
+ }
86
+ const paused = pausedOf(await gamePauses(id, null, headers), where);
87
+ if (!lifted) {
88
+ throw new ArcError(API_REFUSED, functionNotPaused({ channel: where, paused }));
89
+ }
90
+ return { channel: where, paused };
91
+ }
92
+ // [function]
93
+ // the game's function on each channel, or on one: `{ channel, function,
94
+ // paused, requests }`, `function` whether the channel's live build has one
95
+ // (a pause and a usage are the channel's, so neither says), `paused` the
96
+ // reason it's paused for, or null for none, and `requests` what it answered
97
+ // this month. With them, the game's month: `{ allowance, requests }`, its
98
+ // channels together, whichever were asked for, since the allowance is the
99
+ // game's.
100
+ export async function status(current, { channel, ...conf }) {
101
+ const where = channelOf(channel);
102
+ const id = await gameOf(conf);
103
+ const headers = headersOf(current);
104
+ const [pauses, usages] = await Promise.all([
105
+ gamePauses(id, null, headers),
106
+ gameUsages(id, null, headers),
107
+ ]);
108
+ const shown = CHANNELS.filter((each) => !where || each === where);
109
+ // asked once the owner's reads have answered, so another account's game
110
+ // is refused before anything else is asked about it.
111
+ const has = await Promise.all(shown.map((each) => hasFunction(id, each)));
112
+ const rows = usages ?? [];
113
+ const [first] = rows;
114
+ const sum = (of) => of.reduce((total, each) => total + Number(each.requests ?? 0), 0);
115
+ return {
116
+ channels: shown.map((each, i) => ({
117
+ channel: each,
118
+ function: has[i] ?? null,
119
+ paused: pausedOf(pauses, each),
120
+ requests: sum(rows.filter((row) => row.channel === each)),
121
+ })),
122
+ month: {
123
+ allowance: first ? Number(first.allowance) : null,
124
+ requests: sum(rows),
125
+ },
126
+ };
127
+ }
@@ -0,0 +1,61 @@
1
+ // what the actions on a game's own things share (its function, its
2
+ // function's secrets, its server's keys): the project's game, the channel a
3
+ // command names, the session's headers, and telling the API's refusals
4
+ // apart.
5
+ import { auth } from "../api.js";
6
+ import { ARGUMENT_INVALID, GAME_ID_MISSING, NOT_LOGGED_IN, } from "../config/code.js";
7
+ import { ArcError } from "../util/error.js";
8
+ import { given } from "../parser/given.js";
9
+ import { HttpError } from "../util/http.js";
10
+ import { channelInvalid, gameIdMissing, notLoggedIn } from "../phrase/index.js";
11
+ // [declare]
12
+ export const CHANNELS = ["production", "staging"];
13
+ // [function]
14
+ // the project's game's id, which it must have: a function, a secret, and a
15
+ // key are each a game's. Every action reads it before the session
16
+ // (`headersOf`), as `arc deploy` reads the project before it checks the
17
+ // login, so the same mistake has the same name whichever command makes it.
18
+ export function gameOf(conf) {
19
+ return given(conf).then(({ game }) => {
20
+ if (!game.id) {
21
+ throw new ArcError(GAME_ID_MISSING, gameIdMissing());
22
+ }
23
+ return String(game.id);
24
+ });
25
+ }
26
+ // [function]
27
+ // the channel a command names, or null for none; anything else is refused
28
+ // before the API is asked. A command that changes something needs one
29
+ // (`needOf`): the CLI asks for it by its flags (src/cli/channel.ts).
30
+ export function channelOf(channel) {
31
+ if ((channel ?? null) === null) {
32
+ return null;
33
+ }
34
+ const where = CHANNELS.find((each) => each === channel);
35
+ if (!where) {
36
+ throw new ArcError(ARGUMENT_INVALID, channelInvalid());
37
+ }
38
+ return where;
39
+ }
40
+ // [function]
41
+ export function needOf(channel) {
42
+ const where = channelOf(channel);
43
+ if (!where) {
44
+ throw new ArcError(ARGUMENT_INVALID, channelInvalid());
45
+ }
46
+ return where;
47
+ }
48
+ // [function]
49
+ // the session's headers, or a failure when there's none: only a game's owner
50
+ // pauses its function, and has its secrets and its keys.
51
+ export function headersOf(current) {
52
+ if (!current) {
53
+ throw new ArcError(NOT_LOGGED_IN, notLoggedIn());
54
+ }
55
+ return auth(current.session?.access_token);
56
+ }
57
+ // [function]
58
+ // whether a failure is the API's refusal with this status.
59
+ export function isStatus(err, status) {
60
+ return err instanceof HttpError && err.status === status;
61
+ }
@@ -0,0 +1,64 @@
1
+ // [import]
2
+ import { randomUUID } from "node:crypto";
3
+ import { writeFile } from "node:fs/promises";
4
+ import { join } from "node:path";
5
+ // [import]
6
+ import { PARSE_FAILED } from "../../config/code.js";
7
+ import { env } from "../../parser/env.js";
8
+ import { ArcError } from "../../util/error.js";
9
+ import { MANIFEST } from "../../config/file.js";
10
+ import { unableToParse } from "../../phrase/index.js";
11
+ import { read } from "../../parser/manifest.js";
12
+ import { isTable } from "../../util/objs.js";
13
+ import { stringify } from "../../util/toml.js";
14
+ // [function]
15
+ // the version the project's package.json gives, if any.
16
+ export function detectVersion(dest) {
17
+ return env(dest).then(({ game }) => game?.["version"] ?? null);
18
+ }
19
+ // [function]
20
+ function pickVersion(version, envVersion) {
21
+ if (version) {
22
+ return { version };
23
+ }
24
+ return envVersion ? {} : { version: "0.1.0-beta.1" };
25
+ }
26
+ // [function]
27
+ // a new project always gets its own id: a template's, such as an existing
28
+ // game's, belongs to that game.
29
+ function patchGame(envVersion, game = {}, { fresh = false } = {}) {
30
+ // `game` is the manifest's, as written: a table, or it's refused, and the
31
+ // file left as it is. Text read as the fields it has is a key a letter
32
+ // (`game = "abc"` written back as `0`, `1`, `2`).
33
+ if (!isTable(game)) {
34
+ throw new ArcError(PARSE_FAILED, unableToParse(MANIFEST, "`game` must be a table."));
35
+ }
36
+ const { id, version, ...rest } = game;
37
+ return {
38
+ id: fresh ? randomUUID() : (id ?? randomUUID()),
39
+ ...pickVersion(version, envVersion),
40
+ ...rest,
41
+ };
42
+ }
43
+ // [function]
44
+ // a field of the manifest's game, when it's a table.
45
+ function field(game, name) {
46
+ return isTable(game) ? game[name] : undefined;
47
+ }
48
+ // [function]
49
+ function shouldWrite(written, next) {
50
+ return (field(written["game"], "id") !== field(next["game"], "id") ||
51
+ field(written["game"], "version") !== field(next["game"], "version"));
52
+ }
53
+ // [function]
54
+ export function prepare(dest, envVersion, opts) {
55
+ const file = join(dest, MANIFEST);
56
+ return read(dest).then((written) => {
57
+ const { game, ...rest } = written;
58
+ const next = { game: patchGame(envVersion, game, opts), ...rest };
59
+ if (shouldWrite(written, next)) {
60
+ return writeFile(file, stringify(next), "utf8");
61
+ }
62
+ return null;
63
+ });
64
+ }
@@ -0,0 +1,15 @@
1
+ // [import]
2
+ import { resolve } from "node:path";
3
+ // [import]
4
+ import { detectVersion, prepare } from "./manifest.js";
5
+ // [function]
6
+ // an existing project: add a manifest, and copy nothing from the template.
7
+ export function initToolPatch({ project, root }, { onInitBegin }) {
8
+ const dest = resolve(root, project);
9
+ if (onInitBegin) {
10
+ onInitBegin({ dest });
11
+ }
12
+ return detectVersion(dest)
13
+ .then((version) => prepare(dest, version))
14
+ .then(() => ({ path: dest }));
15
+ }
@@ -0,0 +1,64 @@
1
+ // [import]
2
+ import { readdir } from "node:fs/promises";
3
+ import { createRequire } from "node:module";
4
+ import { join, resolve } from "node:path";
5
+ import fs from "fs-extra";
6
+ // [import]
7
+ import { TEMPLATE_UNAVAILABLE } from "../../config/code.js";
8
+ import { ArcError } from "../../util/error.js";
9
+ import { templateUnavailable } from "../../phrase/index.js";
10
+ import { detectVersion, prepare } from "./manifest.js";
11
+ import { choices } from "../../config/template.js";
12
+ // [use]
13
+ const remove = fs.remove;
14
+ // [declare]
15
+ const LICENSE = "LICENSE";
16
+ // [define]
17
+ // degit is required, not imported: it's a
18
+ // CommonJS module, and the tests' sandbox answers `require("degit")` with a
19
+ // fake that copies a local directory (test/support/preload.js), which an
20
+ // import would never reach.
21
+ const degit = createRequire(import.meta.url)("degit");
22
+ // [function]
23
+ function removeLicense(dest) {
24
+ return remove(join(dest, LICENSE));
25
+ }
26
+ // [function]
27
+ // a starter's name on its own, such as `blank`, suggests the starter.
28
+ function unavailable(src, cause) {
29
+ const starter = choices.find(({ value }) => value.split("/").pop() === src);
30
+ return new ArcError(TEMPLATE_UNAVAILABLE, templateUnavailable(src, starter?.value), { cause });
31
+ }
32
+ // [function]
33
+ // degit copies nothing, without failing, from a folder a repository doesn't
34
+ // have, so an empty project fails as a missing repository does.
35
+ async function ensureCopied(src, dest) {
36
+ const names = await readdir(dest).catch(() => []);
37
+ if (names.length === 0) {
38
+ await remove(dest);
39
+ throw unavailable(src);
40
+ }
41
+ }
42
+ // [function]
43
+ function scaffold(src, dest, { onInitBegin }) {
44
+ if (onInitBegin) {
45
+ onInitBegin({ dest, src });
46
+ }
47
+ // degit throws as it's made when it can't parse `src`.
48
+ return Promise.resolve()
49
+ .then(() => degit(src, { cache: false, force: true, verbose: false }).clone(dest))
50
+ .catch((ex) => {
51
+ throw unavailable(src, ex);
52
+ })
53
+ .then(() => ensureCopied(src, dest));
54
+ }
55
+ // [function]
56
+ // a new project: `arc init` runs tool-patch.ts when the directory exists.
57
+ export function initTool({ project, root, template, }, handler) {
58
+ const dest = resolve(root, project);
59
+ return scaffold(template, dest, handler)
60
+ .then(() => removeLicense(dest))
61
+ .then(() => detectVersion(dest))
62
+ .then((version) => prepare(dest, version, { fresh: true }))
63
+ .then(() => ({ path: dest, template }));
64
+ }
@@ -0,0 +1,13 @@
1
+ // [import]
2
+ import dotProp from "dot-prop";
3
+ // [import]
4
+ import { project } from "../../parser/project.js";
5
+ // [function]
6
+ function select(proj, key) {
7
+ return key ? dotProp.get(proj, key, null) : proj;
8
+ }
9
+ // [function]
10
+ // the project's resolved manifest, or the value at `selector` in it.
11
+ export function inspectTool({ root, selector, }) {
12
+ return project(root).then((data) => select(data, selector));
13
+ }
@@ -0,0 +1,87 @@
1
+ // a game's keys: what its own server proves itself to Arcadible with. A key
2
+ // is for one game and one channel, made by the game's owner, shown once, and
3
+ // revoked at once. Nothing here keeps one.
4
+ import { deleteGameKey, gameKeys, postGameKey, } from "../../api.js";
5
+ import { API_REFUSED, ARGUMENT_INVALID, GAME_UNAVAILABLE, } from "../../config/code.js";
6
+ import { ArcError } from "../../util/error.js";
7
+ import { channelOf, gameOf, headersOf, isStatus, needOf } from "../game.js";
8
+ import { gameUnavailable, keyLimitReached, keyNameInvalid, keyNotFound, } from "../../phrase/index.js";
9
+ // [declare]
10
+ const NAME_MAX = 64;
11
+ // the name a key gets when none is given.
12
+ const NAME = "Server";
13
+ // [function]
14
+ // a new key, as `{ id, name, channel, key }`: the only time the key is had.
15
+ export async function create(current, { channel, name, ...conf }) {
16
+ const given = name ?? NAME;
17
+ // a name that isn't text (a number in a JSON `.arcrc`) is refused as any
18
+ // other that won't do.
19
+ const named = typeof given === "string" ? given.trim() : "";
20
+ if (named === "" || named.length > NAME_MAX) {
21
+ throw new ArcError(ARGUMENT_INVALID, keyNameInvalid());
22
+ }
23
+ const where = needOf(channel);
24
+ const id = await gameOf(conf);
25
+ const form = new FormData();
26
+ form.append("name", named);
27
+ form.append("channel", where);
28
+ try {
29
+ const made = await postGameKey(id, form, null, headersOf(current));
30
+ return {
31
+ channel: made.channel,
32
+ id: made.id,
33
+ key: made.key,
34
+ name: made.name,
35
+ };
36
+ }
37
+ catch (err) {
38
+ if (isStatus(err, 404)) {
39
+ throw new ArcError(GAME_UNAVAILABLE, gameUnavailable(id), {
40
+ cause: err,
41
+ });
42
+ }
43
+ if (isStatus(err, 409)) {
44
+ throw new ArcError(API_REFUSED, keyLimitReached(where), {
45
+ cause: err,
46
+ });
47
+ }
48
+ throw err;
49
+ }
50
+ }
51
+ // [function]
52
+ // the game's live keys, newest first, of one channel or both: each as
53
+ // `{ id, name, channel, shown, created_at, used_at }`.
54
+ export async function list(current, { channel, ...conf }) {
55
+ const where = channelOf(channel);
56
+ const id = await gameOf(conf);
57
+ const keys = await gameKeys(id, null, headersOf(current));
58
+ return (keys ?? [])
59
+ .filter((key) => !where || key.channel === where)
60
+ .map((key) => ({
61
+ channel: key.channel,
62
+ created_at: key.created_at,
63
+ id: key.id,
64
+ name: key.name,
65
+ shown: key.shown,
66
+ used_at: key.used_at,
67
+ }));
68
+ }
69
+ // [function]
70
+ // revokes the key with this id, and resolves with `{ id }`. One that's gone
71
+ // already, isn't the game's, or isn't an id at all is the same to its owner:
72
+ // not found.
73
+ export async function revoke(current, { id, ...conf }) {
74
+ const game = await gameOf(conf);
75
+ try {
76
+ const gone = await deleteGameKey(game, id, null, headersOf(current));
77
+ return { id: gone.id };
78
+ }
79
+ catch (err) {
80
+ if (isStatus(err, 404) || isStatus(err, 400)) {
81
+ throw new ArcError(API_REFUSED, keyNotFound(id), {
82
+ cause: err,
83
+ });
84
+ }
85
+ throw err;
86
+ }
87
+ }
@@ -0,0 +1,69 @@
1
+ // [import]
2
+ import { randomUUID } from "node:crypto";
3
+ // [import]
4
+ import { auth, flow as apiFlow, postFlow as apiPostFlow, } from "../../api.js";
5
+ import { LOGIN_CODE_EXPIRED, LOGIN_FAILED } from "../../config/code.js";
6
+ import { poll } from "../../util/delay.js";
7
+ import { ArcError } from "../../util/error.js";
8
+ import { loginCodeExpired, loginFailed } from "../../phrase/index.js";
9
+ import { m, s } from "../../util/time.js";
10
+ import { LINK } from "../../config/url.js";
11
+ // [declare]
12
+ const POLL = {
13
+ delay: s(1.5),
14
+ factor: 1.5,
15
+ interval: s(15),
16
+ maxDelay: s(5),
17
+ timeoutError: () => new ArcError(LOGIN_CODE_EXPIRED, loginCodeExpired()),
18
+ };
19
+ // [function]
20
+ function postFlow({ expiresAt, secret, type }) {
21
+ const form = new FormData();
22
+ form.append("secret", secret);
23
+ form.append("type", type);
24
+ form.append("expires_at", expiresAt.toISOString());
25
+ return apiPostFlow(form);
26
+ }
27
+ // [function]
28
+ function getFlow({ id, secret: token }) {
29
+ return apiFlow(id, null, auth(token));
30
+ }
31
+ // [function]
32
+ function createFlow({ expiresAt, secret, type }, onLoginCreated) {
33
+ return postFlow({ expiresAt, secret, type }).then(({ code, id }) => {
34
+ if (onLoginCreated) {
35
+ onLoginCreated({ code, secret, url: LINK });
36
+ }
37
+ return { id };
38
+ });
39
+ }
40
+ // [function]
41
+ function pollFlow(value) {
42
+ return getFlow(value).then(({ data, status }) => {
43
+ switch (status) {
44
+ case "pending":
45
+ return null;
46
+ case "completed":
47
+ return data;
48
+ case "failed":
49
+ throw new ArcError(LOGIN_FAILED, loginFailed());
50
+ default:
51
+ throw new ArcError(LOGIN_CODE_EXPIRED, loginCodeExpired());
52
+ }
53
+ });
54
+ }
55
+ // [function]
56
+ // the device flow: makes a login, tells `onLoginCreated` its code and link,
57
+ // and resolves with the account once it's completed at the link.
58
+ export function flow({ onLoginCreated, }) {
59
+ const expiresAt = new Date(Date.now() + m(10));
60
+ const secret = randomUUID();
61
+ return createFlow({
62
+ expiresAt,
63
+ secret,
64
+ type: "cli-login",
65
+ }, onLoginCreated).then(({ id }) => {
66
+ const timeout = Math.max(0, expiresAt.getTime() - Date.now() - s(15));
67
+ return poll(pollFlow, { id, secret }, { ...POLL, timeout });
68
+ });
69
+ }
@@ -0,0 +1,8 @@
1
+ // [import]
2
+ import { save } from "../../account.js";
3
+ import { flow } from "./flow.js";
4
+ // [function]
5
+ // logs in, and saves the session.
6
+ export function login(_conf, handler) {
7
+ return flow(handler).then(save);
8
+ }
@@ -0,0 +1,8 @@
1
+ // [import]
2
+ import { end } from "../../account.js";
3
+ // [function]
4
+ // ends the saved session, and resolves with the user it was for, or null
5
+ // when there was none.
6
+ export function logout() {
7
+ return end();
8
+ }
@@ -0,0 +1,29 @@
1
+ // [import]
2
+ import { storePage, storeUrl } from "../../util/links.js";
3
+ import { self } from "../../parser/self.js";
4
+ // [function]
5
+ // the store page's path, which the app opens, and its URL on the web. `id`
6
+ // is a handle or an id as it's written, in the manifest or on the command
7
+ // line.
8
+ function openUrl(id) {
9
+ if (!id) {
10
+ return null;
11
+ }
12
+ const game = String(id);
13
+ return { page: storePage(game), url: storeUrl(game) };
14
+ }
15
+ // [function]
16
+ function open(id) {
17
+ return Promise.resolve(openUrl(id));
18
+ }
19
+ // [function]
20
+ function openSelf(root) {
21
+ return self(root).then((game) => openUrl(game?.handle ?? game?.id));
22
+ }
23
+ // [function]
24
+ export function openTool({ game: id, root, }) {
25
+ if (id === "self") {
26
+ return openSelf(root);
27
+ }
28
+ return open(id);
29
+ }