dewctl 0.9.0 → 0.9.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dewctl",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Build and publish three.js games on Dewdrop.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/client.js CHANGED
@@ -56,11 +56,14 @@ export class Client {
56
56
  const reason=error.name==='TimeoutError'?'no answer in two minutes':error.cause?.code??error.cause?.message??error.message;
57
57
  throw Object.assign(new Error(`Couldn't reach ${this.origin} (${reason}). Check your connection.`),{transient:true});
58
58
  }
59
- const contentType=response.headers.get('content-type')??'';
60
- if(!contentType.includes('application/json'))throw new ApiError(response.status,`${this.origin} didn't answer as Dewdrop does (HTTP ${response.status}). Try again in a moment.`);
61
- const data=await response.json();
59
+ // An answer in JSON is Dewdrop's, and a refusal in it is said in Dewdrop's words whatever its status, a 504
60
+ // included; anything else — a page from something between here and Dewdrop — is no answer of Dewdrop's.
61
+ const strange=new ApiError(response.status,`${this.origin} didn't answer as Dewdrop does (HTTP ${response.status}). Try again in a moment.`);
62
+ if(!(response.headers.get('content-type')??'').includes('application/json'))throw strange;
63
+ let data;
64
+ try { data=await response.json(); } catch(error) { if(signal?.aborted)throw error; throw strange; }
62
65
  if(response.status===401&&token)throw new ApiError(401,`Your sign-in has ended: it expired or was signed out. Run ${RUN} login.`,data);
63
- if(!response.ok)throw new ApiError(response.status,data.error??`HTTP ${response.status}`,data);
66
+ if(!response.ok)throw new ApiError(response.status,typeof data?.error==='string'&&data.error?data.error:`HTTP ${response.status}`,data);
64
67
  return data;
65
68
  }
66
69
  // The session this computer holds, or why there is none to use.
@@ -202,9 +205,10 @@ export class Client {
202
205
  }
203
206
  // Sets the game's thumbnail to the image, sent as itself under its type.
204
207
  thumbnail(game,bytes,type,signal){return this.api(`/v1/games/${game}/thumbnail`,{method:'PUT',body:bytes,type,signal});}
205
- // The dev version's console lines after `after`, held up to `wait` ms for new ones. A refusal says why in the
206
- // developer's terms.
207
- devLogs(game,after,wait,signal){return this.#dev(game,`logs?after=${after}&wait=${wait}`,{signal});}
208
+ // The dev version's console lines after cursor `after` — none for the start, or a `next` Dewdrop answered, which
209
+ // is its own and goes back as it came — held up to `wait` ms for new ones. A refusal says why in the developer's
210
+ // terms.
211
+ devLogs(game,after,wait,signal){return this.#dev(game,`logs?${after==null?'':`after=${encodeURIComponent(after)}&`}wait=${wait}`,{signal});}
208
212
  // A seat pass for the dev version: one use, within minutes, by whatever the developer's own tools are.
209
213
  seat(game,signal){return this.#dev(game,'seat',{method:'POST',body:{},signal});}
210
214
  async #dev(game,path,options) {
package/src/logs.js CHANGED
@@ -19,8 +19,9 @@ export function format(line,maps) {
19
19
  // Dewdrop that can't be reached, a dev version not running and a limit reached are waited out, resting longer after
20
20
  // each failure in a row; any other refusal, or any refusal at all without `follow`, is thrown. `note` says what
21
21
  // happened to the lines rather than printing one.
22
+ // Where the read has got to is Dewdrop's own: none at first, then each answer's `next`, passed back as it came.
22
23
  export async function printLogs(client,game,{signal,follow=false,maps,log=console.log,note=log,restMs=1000,waitMs=WAIT_MS}={}) {
23
- let after=0,failures=0,printed=0,restarts=0;
24
+ let after=null,failures=0,printed=0,restarts=0;
24
25
  while(!signal?.aborted) {
25
26
  const asked=Date.now();
26
27
  let answer;
@@ -34,18 +35,22 @@ export async function printLogs(client,game,{signal,follow=false,maps,log=consol
34
35
  }
35
36
  failures=0;
36
37
  const lines=Array.isArray(answer.lines)?answer.lines:[];
38
+ // A Dewdrop from before the cursor was its own answers a number, passed back as the same digits.
39
+ const next=typeof answer.next==='string'&&answer.next?answer.next:Number.isSafeInteger(answer.next)&&answer.next>=0?String(answer.next):null;
37
40
  if(answer.restarted) {
38
41
  // Without `follow`, a dev version that keeps restarting under the read gives what it had.
39
42
  if(!follow&&++restarts>2)break;
40
- if(follow&&after>0)note(`(${game}'s dev version restarted.)`);
41
- after=0;
42
- } else {
43
- if(answer.lost>0)note(`(${answer.lost} line${answer.lost===1?' was':'s were'} dropped here, written faster than Dewdrop keeps them)`);
44
- for(const line of lines)log(format(line,maps));
45
- printed+=lines.length;
46
- const next=Number.isSafeInteger(answer.next)?answer.next:after;
47
- if(!follow&&(!lines.length||next<=after))break;
48
- after=next;
43
+ if(follow&&after!==null&&after!=='0')note(`(${game}'s dev version restarted.)`);
44
+ }
45
+ if(answer.lost>0)note(`(${answer.lost} line${answer.lost===1?' was':'s were'} dropped here, written faster than Dewdrop keeps them)`);
46
+ for(const line of lines)log(format(line,maps));
47
+ printed+=lines.length;
48
+ // A restart is read again from where its answer says, or from the start; otherwise, without `follow`, the read
49
+ // ends once an answer brings nothing new.
50
+ if(answer.restarted)after=next;
51
+ else {
52
+ if(!follow&&(!lines.length||next===null||next===after))break;
53
+ if(next!==null)after=next;
49
54
  }
50
55
  // An answer with nothing new that came at once is asked again after a rest, so a Dewdrop that doesn't hold the
51
56
  // request isn't asked in a loop.