dewctl 0.7.0 → 0.8.0

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.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Build and publish three.js games on Dewdrop.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/client.js CHANGED
@@ -1,5 +1,4 @@
1
1
  // Requests to Dewdrop, only ever to the address in dewdrop.json, signed in with the session login stored.
2
- import {createHash} from 'node:crypto';
3
2
  import {tarChunks} from './tar.js';
4
3
  import {RUN} from './command.js';
5
4
  // A refusal as Dewdrop answered it: its status, its words, and the whole answer for what else it says.
@@ -16,7 +15,8 @@ export function sleep(ms,signal) {
16
15
  if(signal?.aborted)cancel();else signal?.addEventListener('abort',cancel,{once:true});
17
16
  });
18
17
  }
19
- // An upload is sent in parts of at most this many bytes.
18
+ // A push's request carries at most this many bytes of files; a file over this many is sent on its own, in parts of
19
+ // at most this many.
20
20
  export const PART_BYTES=32*1024*1024;
21
21
  // A push's first request carries the files not yet confirmed only up to this many bytes of them: a save's few changed
22
22
  // files go in one round trip, while a run's first push — the whole game, which Dewdrop mostly has from the last run —
@@ -24,8 +24,8 @@ export const PART_BYTES=32*1024*1024;
24
24
  export const PROBE_BYTES=1024*1024;
25
25
  // How long a poll asks the platform to hold it for news (`Prefer: wait`), under what the edge holds a request open.
26
26
  const WAIT_S=25;
27
- // `chunks` cut into runs of exactly `bytes`, but the last, which is what remains and never empty: an archive given
28
- // as a run of buffers of any lengths is sent in parts of one size, holding about one part at a time.
27
+ // `chunks` cut into runs of exactly `bytes`, but the last, which is what remains and never empty: a file given as a
28
+ // run of buffers of any lengths is sent in parts of one size, holding about one part at a time.
29
29
  export function* parts(chunks,bytes=PART_BYTES) {
30
30
  let part=Buffer.allocUnsafe(bytes),filled=0;
31
31
  for(let chunk of chunks) {
@@ -38,12 +38,11 @@ export function* parts(chunks,bytes=PART_BYTES) {
38
38
  if(filled)yield part.subarray(0,filled);
39
39
  }
40
40
  export class Client {
41
- // `partBytes` is for the tests, which cut at a few bytes; an upload is sent in parts of PART_BYTES.
41
+ // `partBytes` is for the tests, which cut at a few bytes; a push's requests and a file's parts are PART_BYTES.
42
42
  // `pollMs` is the least a wait on the platform takes a round: a platform that holds the request answers when there
43
43
  // is news, within WAIT_S; one that answers at once instead is asked again after that long.
44
- // `pushes` is whether Dewdrop takes a release as its tree, until its answer says otherwise; `confirmed` holds the
45
- // hashes of the files it has been seen to have, for this run.
46
- constructor(origin,credentials,{fetcher=fetch,pollMs=1000,timeoutMs=180000,storeMs=15*60*1000,partBytes=PART_BYTES}={}){this.origin=origin;this.credentials=credentials;this.fetcher=fetcher;this.pollMs=pollMs;this.timeoutMs=timeoutMs;this.storeMs=storeMs;this.partBytes=partBytes;this.holds=true;this.pushes=true;this.confirmed=new Set();}
44
+ // `confirmed` holds the hashes of the files Dewdrop has been seen to have, for this run.
45
+ constructor(origin,credentials,{fetcher=fetch,pollMs=1000,timeoutMs=180000,storeMs=15*60*1000,partBytes=PART_BYTES}={}){this.origin=origin;this.credentials=credentials;this.fetcher=fetcher;this.pollMs=pollMs;this.timeoutMs=timeoutMs;this.storeMs=storeMs;this.partBytes=partBytes;this.holds=true;this.confirmed=new Set();}
47
46
  // A request signed in with `token`. When Dewdrop can't be reached the failure is marked transient, which a watcher
48
47
  // or a sign-in waits out; a refused token asks for a new sign-in, whatever Dewdrop's words.
49
48
  async request(path,{method='GET',body,token,signal,type,prefer}={}) {
@@ -90,11 +89,10 @@ export class Client {
90
89
  // or which of the manifest's files it still lacks for this game, sent in further requests of PART_BYTES of files —
91
90
  // only those, never a file it did not ask for — until it records the release, which must carry the same id. A file
92
91
  // over PART_BYTES goes up on its own first, in parts. The files Dewdrop confirms, by recording the release or by not
93
- // asking for them again, are remembered for the run, so the next push's first request carries only what changed; a
94
- // Dewdrop from before trees refuses the first request, and takes the release whole from then on. `progress` is told
95
- // what is being sent or checked, what Dewdrop turned out to lack, and each phase of a file stored over minutes.
92
+ // asking for them again, are remembered for the run, so the next push's first request carries only what changed. A
93
+ // Dewdrop from before trees refuses the first request, which is older than this dewctl. `progress` is told what is
94
+ // being sent or checked, what Dewdrop turned out to lack, and each phase of a file stored over minutes.
96
95
  async push(game,bundle,signal,progress) {
97
- if(!this.pushes)return this.whole(game,bundle,signal,progress);
98
96
  const path=`/v1/games/${game}/releases/${bundle.id}`;
99
97
  // `.manifest` sorts before every path a manifest allows, so the writer puts it first.
100
98
  const manifest={path:'.manifest',size:bundle.manifest.length,read:()=>[bundle.manifest]};
@@ -116,7 +114,7 @@ export class Client {
116
114
  let result;
117
115
  try { result=await this.api(path,{method:'PUT',body,type:'application/x-tar',signal}); }
118
116
  catch(error) {
119
- if(first&&[405,413,415].includes(error.status)){this.pushes=false;return this.whole(game,bundle,signal,progress);}
117
+ if(first&&[405,413,415].includes(error.status))throw new Error('This Dewdrop is older than this dewctl and can\'t take the release yet. Try again later.');
120
118
  if(error.status!==409||!Array.isArray(error.body?.missing))throw error;
121
119
  const missing=new Set(error.body.missing);
122
120
  for(const {sha256} of sent)if(!missing.has(sha256))this.confirmed.add(sha256);
@@ -135,8 +133,8 @@ export class Client {
135
133
  return result;
136
134
  }
137
135
  }
138
- // A file over PART_BYTES, sent on its own under its hash: in parts, as a release is, completed and followed the same
139
- // way, and Dewdrop must confirm the same hash.
136
+ // A file over PART_BYTES, sent on its own under its hash: in parts, then completed and followed, and Dewdrop must
137
+ // confirm the same hash.
140
138
  async blob(game,entry,signal,progress) {
141
139
  const path=`/v1/games/${game}/blobs/${entry.sha256}`;
142
140
  let count=0;
@@ -145,32 +143,10 @@ export class Client {
145
143
  if(result.blob!==entry.sha256)throw new Error('Dewdrop stored a different file than was uploaded, so nothing was changed. Try again.');
146
144
  return result;
147
145
  }
148
- // The release sent whole, as a Dewdrop from before trees takes it: the archive of every file, in parts.
149
- whole(game,bundle,signal,progress) {
150
- const archive=bundle.archive();
151
- progress?.({whole:Math.max(1,Math.ceil(archive.size/this.partBytes)),files:bundle.files,bytes:bundle.bytes});
152
- return this.upload(game,archive,signal,phase=>progress?.({phase}));
153
- }
154
- // Uploads a build of a claimed game under its id — its archive read through again as `bundle.chunks()` gives it,
155
- // cut into parts of PART_BYTES, so a bundle of gigabytes is never held whole; what is sent is hashed as it goes,
156
- // and an archive that no longer hashes to the id changed since the id was taken, and is refused — then completes
157
- // it with the number of parts. Dewdrop must confirm the same id.
158
- async upload(game,bundle,signal,progress) {
159
- let count=0;
160
- const hash=createHash('sha256');
161
- for(const part of parts(bundle.chunks(),this.partBytes)) {
162
- hash.update(part);
163
- await this.api(`/v1/games/${game}/releases/${bundle.id}/parts/${count++}`,{method:'PUT',body:part,signal});
164
- }
165
- if(hash.digest('hex')!==bundle.id)throw new Error('The build\'s output changed while it was being uploaded. Try again.');
166
- const result=await this.#complete(`/v1/games/${game}/releases/${bundle.id}`,count,signal,progress);
167
- if(result.release!==bundle.id)throw new Error('Dewdrop stored a different release than was uploaded, so nothing was changed. Try again.');
168
- return result;
169
- }
170
- // Completes an upload at `path` with the number of its parts, asking for the answer at once: Dewdrop takes a large
171
- // one in over minutes, longer than a request stays open, so an answer at once names the `phase` it is in, and this
172
- // follows at the same address, telling `progress` each phase it sees, until Dewdrop records the upload — the answer
173
- // with its `bytes` — or refuses it. A Dewdrop from before the answer at once records it in the request itself.
146
+ // Completes a file's upload at `path` with the number of its parts, asking for the answer at once: Dewdrop takes a
147
+ // large one in over minutes, longer than a request stays open, so an answer at once names the `phase` it is in, and
148
+ // this follows at the same address, telling `progress` each phase it sees, until Dewdrop records the file — the
149
+ // answer with its `bytes` — or refuses it. A Dewdrop that is done at once records it in the request itself.
174
150
  async #complete(path,count,signal,progress) {
175
151
  const deadline=Date.now()+this.storeMs;
176
152
  let result=await this.api(path,{method:'POST',body:{parts:count},signal,prefer:'respond-async'}),phase;
package/src/project.js CHANGED
@@ -6,7 +6,7 @@ import {spawn} from 'node:child_process';
6
6
  import {fileURLToPath} from 'node:url';
7
7
  import {gunzipSync} from 'node:zlib';
8
8
  import {createInterface} from 'node:readline';
9
- import {tarChunks,tarLength,readTar} from './tar.js';
9
+ import {readTar} from './tar.js';
10
10
  import {MAX_BYTES,entryBytes,hashFiles} from './hash.js';
11
11
  import {RUN} from './command.js';
12
12
 
@@ -89,8 +89,7 @@ export function fingerprint(project) {
89
89
  let hashes=new Map();
90
90
  export function forgetHashes() { hashes=new Map(); }
91
91
  // The bundle as a release: the manifest of its files — a line each of hash, size and path, in the order of the
92
- // paths' bytes — which names the release by its own hash, with each file's bytes to read for the upload, and the
93
- // archive of them all for a Dewdrop that takes releases whole.
92
+ // paths' bytes — which names the release by its own hash, with each file's bytes to read for the upload.
94
93
  export async function snapshot(project) {
95
94
  const directory=projectPath(project.root,project.bundle);
96
95
  if (!existsSync(directory)) throw new Error(project.build
@@ -122,14 +121,7 @@ export async function snapshot(project) {
122
121
  return {path,size,sha256,read:()=>entryBytes(path,file,root,size,sha256)};
123
122
  });
124
123
  const manifest=Buffer.from(entries.map(({sha256,size,path})=>`${sha256} ${size} ${path}\n`).join(''));
125
- return {id:createHash('sha256').update(manifest).digest('hex'),bytes,files:entries.length,manifest,entries,
126
- // The same files always make the same archive, which names it: `chunks` is the archive as a run of buffers,
127
- // read through here for the id and again by the upload that sends it, `size` bytes in all.
128
- archive() {
129
- const chunks=()=>tarChunks(entries),digest=createHash('sha256');
130
- for (const chunk of chunks()) digest.update(chunk);
131
- return {id:digest.digest('hex'),size:tarLength(entries),chunks};
132
- }};
124
+ return {id:createHash('sha256').update(manifest).digest('hex'),bytes,files:entries.length,manifest,entries};
133
125
  }
134
126
  export async function build(project, signal) {
135
127
  if (!project.build) return;
package/src/tar.js CHANGED
@@ -1,4 +1,4 @@
1
- // The archive an upload is sent as: ustar, regular files only, written byte for byte the same for the same files.
1
+ // The archive a push's request is sent as: ustar, regular files only, written byte for byte the same for the same files.
2
2
  const BLOCK=512;
3
3
  const octal=(value,length)=>value.toString(8).padStart(length-1,'0')+'\0';
4
4
  const field=(block,offset,length,text)=>block.write(text,offset,length,'latin1');
@@ -31,8 +31,7 @@ function header(path,size) {
31
31
 
32
32
  // One archive of `entries` — `{path, size, read}` each, `read()` giving the file's `size` bytes as a run of buffers —
33
33
  // in path order, with no owner, time or mode but the fixed ones, as a run of buffers: each file's header, its bytes
34
- // as `read` gives them, and its padding, then the two blocks that end the archive. The run is read through once for
35
- // the archive's id and once more to send it, a few MiB at a time, so an upload of gigabytes is never held whole.
34
+ // as `read` gives them, and its padding, then the two blocks that end the archive.
36
35
  const padded=size=>size+(size%BLOCK?BLOCK-size%BLOCK:0);
37
36
  export function* tarChunks(entries) {
38
37
  const sorted=[...entries].sort((a,b)=>a.path<b.path?-1:a.path>b.path?1:0);
@@ -43,8 +42,6 @@ export function* tarChunks(entries) {
43
42
  }
44
43
  yield Buffer.alloc(2*BLOCK);
45
44
  }
46
- // The archive's length, from its entries' sizes alone.
47
- export const tarLength=entries=>entries.reduce((sum,{size})=>sum+BLOCK+padded(size),0)+2*BLOCK;
48
45
  // The archive as one buffer, for the small ones the tests make.
49
46
  export const packTar=entries=>Buffer.concat([...tarChunks(entries)]);
50
47
 
package/src/watch.js CHANGED
@@ -14,12 +14,11 @@ export function size(bytes) {
14
14
  const PHASES={waiting:'Waiting for Dewdrop to take the upload…',assembling:'Dewdrop is assembling the upload…',checking:'Dewdrop is checking the upload…',storing:'Dewdrop is storing the upload…',copying:'Dewdrop is copying the upload to its regions…',recording:'Dewdrop is recording the upload…'};
15
15
  const count=(n,noun)=>`${n} ${noun}${n===1?'':'s'}`;
16
16
  // What a push says as it goes, in the developer's terms: the files going up, or checked with Dewdrop first, the ones
17
- // Dewdrop turned out to lack, each phase of an upload stored over minutes, and a Dewdrop that takes the release whole.
17
+ // Dewdrop turned out to lack, and each phase of a file stored over minutes.
18
18
  const telling=log=>event=>{
19
19
  if(event.phase!==undefined)log(PHASES[event.phase]??`Dewdrop is ${event.phase} the upload…`);
20
20
  else if(event.checking!==undefined)log(`Checking ${count(event.checking,'file')} (${size(event.bytes)}) with Dewdrop…`);
21
21
  else if(event.missing!==undefined)log(`Uploading ${count(event.missing,'changed file')} (${size(event.bytes)})…`);
22
- else if(event.whole!==undefined)log(`Dewdrop takes the release whole here: uploading ${count(event.files,'file')} (${size(event.bytes)})${event.whole>1?` in ${event.whole} parts`:''}…`);
23
22
  else if(event.files===0)log('Dewdrop has every file of the build; recording the release…');
24
23
  else log(`Uploading ${count(event.files,'file')} (${size(event.bytes)})…`);
25
24
  };