dewctl 0.7.0 → 0.8.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 +1 -1
- package/src/client.js +22 -46
- package/src/main.js +2 -3
- package/src/project.js +3 -11
- package/src/tar.js +2 -5
- package/src/watch.js +2 -3
package/package.json
CHANGED
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,16 +15,17 @@ export function sleep(ms,signal) {
|
|
|
16
15
|
if(signal?.aborted)cancel();else signal?.addEventListener('abort',cancel,{once:true});
|
|
17
16
|
});
|
|
18
17
|
}
|
|
19
|
-
//
|
|
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 —
|
|
23
23
|
// sends the manifest alone and lets the answer name what is missing.
|
|
24
24
|
export const PROBE_BYTES=1024*1024;
|
|
25
|
-
// How long a poll asks
|
|
25
|
+
// How long a poll asks Dewdrop to hold it for news (`Prefer: wait`), within how long a request may stay 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:
|
|
28
|
-
//
|
|
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;
|
|
42
|
-
// `pollMs` is the least a wait on
|
|
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
|
+
// `pollMs` is the least a wait on Dewdrop takes a round: a Dewdrop 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
|
-
// `
|
|
45
|
-
|
|
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
|
|
94
|
-
//
|
|
95
|
-
//
|
|
92
|
+
// asking for them again, are remembered for the run, so the next push's first request carries only what changed.
|
|
93
|
+
// `progress` is told what is being sent or checked, what Dewdrop turned out to lack, and each phase of a file stored
|
|
94
|
+
// 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))
|
|
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,
|
|
139
|
-
//
|
|
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,39 +143,17 @@ 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
|
-
//
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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;
|
|
177
153
|
while(result.bytes===undefined) {
|
|
178
154
|
if(Date.now()>=deadline)throw new Error(`Dewdrop is taking longer than fifteen minutes to store the upload. Run ${RUN} releases to see whether it arrived.`);
|
|
179
155
|
if(result.phase!==undefined&&result.phase!==phase){phase=result.phase;progress?.(phase);}
|
|
180
|
-
// Held by
|
|
156
|
+
// Held by Dewdrop until the completion ends, or answered at once by one that does not hold a poll.
|
|
181
157
|
const asked=Date.now();
|
|
182
158
|
result=await this.api(path,{signal,prefer:`wait=${WAIT_S}`});
|
|
183
159
|
if(result.bytes===undefined)await this.#rest(asked,signal);
|
|
@@ -207,7 +183,7 @@ export class Client {
|
|
|
207
183
|
if(seen.release===release&&!seen.ready&&!seen.error)await this.#rest(asked,signal);
|
|
208
184
|
}
|
|
209
185
|
}
|
|
210
|
-
// The game as
|
|
186
|
+
// The game as Dewdrop answers it once `version` is ready or refused, holding the request for it; a Dewdrop from
|
|
211
187
|
// before that route answers the game as it stands, and is asked that way from then on.
|
|
212
188
|
async held(game,version,signal) {
|
|
213
189
|
if(!this.holds)return this.game(game,signal);
|
|
@@ -218,7 +194,7 @@ export class Client {
|
|
|
218
194
|
return this.game(game,signal);
|
|
219
195
|
}
|
|
220
196
|
}
|
|
221
|
-
// The rest of a round that came back at once: a
|
|
197
|
+
// The rest of a round that came back at once: a Dewdrop holding the request has answered with news, one that
|
|
222
198
|
// answered at once is asked again `pollMs` after it was asked.
|
|
223
199
|
async #rest(asked,signal) {
|
|
224
200
|
const left=this.pollMs-(Date.now()-asked);
|
package/src/main.js
CHANGED
|
@@ -29,8 +29,7 @@ const HELP=`${RUN} — make three.js games on Dewdrop
|
|
|
29
29
|
share EMAIL Let another developer play and update the dev version
|
|
30
30
|
unshare EMAIL Take that back
|
|
31
31
|
status Show the dev and published versions and where to play them
|
|
32
|
-
releases List
|
|
33
|
-
each member's latest upload, and every other dev build for two weeks;
|
|
32
|
+
releases List every upload Dewdrop keeps, which is all of them until deleted;
|
|
34
33
|
any id, or its first 8 characters, feeds --release and delete
|
|
35
34
|
delete ID Delete an upload nothing runs, freeing its space (owner only)
|
|
36
35
|
delete-game NAME Delete this game, its name typed again to confirm: both versions stop
|
|
@@ -152,7 +151,7 @@ async function main() {
|
|
|
152
151
|
const [{releases},state]=await Promise.all([client.releases(game),client.game(game)]);
|
|
153
152
|
if(!releases.length){console.log(`No uploads of ${game} yet. Run ${RUN} dev to make one.`);return;}
|
|
154
153
|
// What each release is to the game, the reason Dewdrop keeps it, and for an old dev build when it goes.
|
|
155
|
-
const status=release=>[release.sha256===state.dev.release&&'dev',release.sha256===state.live.release&&'published'].filter(Boolean).join(', ')||(release.published?'published before':release.
|
|
154
|
+
const status=release=>[release.sha256===state.dev.release&&'dev',release.sha256===state.live.release&&'published'].filter(Boolean).join(', ')||(release.published?'published before':release.devBuild?'dev build':'latest upload');
|
|
156
155
|
console.log(columns([['RELEASE','UPLOADED (UTC)','SIZE','STATUS','BY'],...releases.map(release=>[release.sha256.slice(0,12),new Date(release.created).toISOString().slice(0,16).replace('T',' '),size(release.bytes),status(release),release.email])]));
|
|
157
156
|
return;
|
|
158
157
|
}
|
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 {
|
|
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
|
|
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
|
|
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.
|
|
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
|
@@ -11,15 +11,14 @@ export function size(bytes) {
|
|
|
11
11
|
return `${value<10?value.toFixed(1):Math.round(value)} ${unit}`;
|
|
12
12
|
}
|
|
13
13
|
// What Dewdrop is doing with an upload that takes a while, by the phase the client sees it in.
|
|
14
|
-
const PHASES={waiting:'Waiting for Dewdrop to take the upload…',
|
|
14
|
+
const PHASES={waiting:'Waiting for Dewdrop to take the upload…',checking:'Dewdrop is checking the upload…',storing:'Dewdrop is storing 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
|
|
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
|
};
|