decryption 0.5.1 → 0.6.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/LICENSE +21 -0
- package/README.md +17 -325
- package/index.js +3 -66
- package/package.json +8 -24
- package/commands/account.d.ts +0 -4
- package/commands/account.js +0 -470
- package/commands/cosmology.d.ts +0 -4
- package/commands/cosmology.js +0 -97
- package/commands/doctor.d.ts +0 -3
- package/commands/doctor.js +0 -42
- package/commands/encrypt.d.ts +0 -12
- package/commands/encrypt.js +0 -251
- package/commands/guard.d.ts +0 -3
- package/commands/guard.js +0 -92
- package/commands/keychain.d.ts +0 -4
- package/commands/keychain.js +0 -92
- package/commands/keys.d.ts +0 -4
- package/commands/keys.js +0 -74
- package/commands/passkey.d.ts +0 -4
- package/commands/passkey.js +0 -135
- package/commands/salt.d.ts +0 -4
- package/commands/salt.js +0 -113
- package/commands/secrets.d.ts +0 -4
- package/commands/secrets.js +0 -214
- package/commands/shamir.d.ts +0 -4
- package/commands/shamir.js +0 -62
- package/commands/vault.d.ts +0 -7
- package/commands/vault.js +0 -382
- package/commands/wallet.d.ts +0 -5
- package/commands/wallet.js +0 -129
- package/commands/workspace.d.ts +0 -8
- package/commands/workspace.js +0 -289
- package/commands.d.ts +0 -8
- package/commands.js +0 -126
- package/esm/commands/account.js +0 -466
- package/esm/commands/cosmology.js +0 -93
- package/esm/commands/doctor.js +0 -38
- package/esm/commands/encrypt.js +0 -245
- package/esm/commands/guard.js +0 -88
- package/esm/commands/keychain.js +0 -88
- package/esm/commands/keys.js +0 -70
- package/esm/commands/passkey.js +0 -131
- package/esm/commands/salt.js +0 -109
- package/esm/commands/secrets.js +0 -210
- package/esm/commands/shamir.js +0 -58
- package/esm/commands/vault.js +0 -376
- package/esm/commands/wallet.js +0 -125
- package/esm/commands/workspace.js +0 -283
- package/esm/commands.js +0 -120
- package/esm/index.js +0 -50
- package/esm/utils/bytes.js +0 -286
- package/esm/utils/dispatch.js +0 -52
- package/esm/utils/env.js +0 -44
- package/esm/utils/errors.js +0 -44
- package/esm/utils/header.js +0 -14
- package/esm/utils/io.js +0 -49
- package/esm/utils/passphrase.js +0 -49
- package/esm/utils/recipients.js +0 -74
- package/esm/utils/stash.js +0 -61
- package/esm/utils/temp.js +0 -51
- package/esm/utils/text.js +0 -2
- package/esm/workspace/bundle.js +0 -239
- package/esm/workspace/doctor.js +0 -104
- package/esm/workspace/git.js +0 -72
- package/esm/workspace/guard.js +0 -111
- package/esm/workspace/layout.js +0 -106
- package/esm/workspace/manifest.js +0 -147
- package/esm/workspace/recipients.js +0 -37
- package/esm/workspace/snippets.js +0 -54
- package/esm/workspace/state.js +0 -46
- package/esm/workspace/status.js +0 -93
- package/esm/workspace/tar.js +0 -235
- package/index.d.ts +0 -12
- package/utils/bytes.d.ts +0 -86
- package/utils/bytes.js +0 -301
- package/utils/dispatch.d.ts +0 -24
- package/utils/dispatch.js +0 -58
- package/utils/env.d.ts +0 -26
- package/utils/env.js +0 -49
- package/utils/errors.d.ts +0 -25
- package/utils/errors.js +0 -50
- package/utils/header.d.ts +0 -4
- package/utils/header.js +0 -18
- package/utils/io.d.ts +0 -10
- package/utils/io.js +0 -57
- package/utils/passphrase.d.ts +0 -15
- package/utils/passphrase.js +0 -53
- package/utils/recipients.d.ts +0 -28
- package/utils/recipients.js +0 -80
- package/utils/stash.d.ts +0 -40
- package/utils/stash.js +0 -76
- package/utils/temp.d.ts +0 -10
- package/utils/temp.js +0 -59
- package/utils/text.d.ts +0 -2
- package/utils/text.js +0 -6
- package/workspace/bundle.d.ts +0 -50
- package/workspace/bundle.js +0 -248
- package/workspace/doctor.d.ts +0 -8
- package/workspace/doctor.js +0 -108
- package/workspace/git.d.ts +0 -21
- package/workspace/git.js +0 -83
- package/workspace/guard.d.ts +0 -23
- package/workspace/guard.js +0 -118
- package/workspace/layout.d.ts +0 -47
- package/workspace/layout.js +0 -113
- package/workspace/manifest.d.ts +0 -52
- package/workspace/manifest.js +0 -157
- package/workspace/recipients.d.ts +0 -16
- package/workspace/recipients.js +0 -45
- package/workspace/snippets.d.ts +0 -29
- package/workspace/snippets.js +0 -58
- package/workspace/state.d.ts +0 -13
- package/workspace/state.js +0 -52
- package/workspace/status.d.ts +0 -54
- package/workspace/status.js +0 -99
- package/workspace/tar.d.ts +0 -29
- package/workspace/tar.js +0 -240
package/esm/workspace/tar.js
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import { ByteReader } from '../utils/bytes';
|
|
2
|
-
import { CliError, EXIT } from '../utils/errors';
|
|
3
|
-
/**
|
|
4
|
-
* A deterministic subset of POSIX tar (ustar, plus a pax `path` record where a name does not fit).
|
|
5
|
-
*
|
|
6
|
-
* The same files always produce the same bytes: entries are written in the order given, and
|
|
7
|
-
* every field that describes the machine rather than the content is fixed. There is no owner, no
|
|
8
|
-
* group, no timestamp, and the mode is one of two values. Only regular files are stored.
|
|
9
|
-
*
|
|
10
|
-
* The output is ordinary tar. `dcrypt decrypt --in section.tar.dcrypt --out section.tar` followed
|
|
11
|
-
* by `tar -xf` recovers a section without any of the code in this folder.
|
|
12
|
-
*/
|
|
13
|
-
const BLOCK_SIZE = 512;
|
|
14
|
-
const NAME_LENGTH = 100;
|
|
15
|
-
const MAX_PAX_LENGTH = 64 * 1024;
|
|
16
|
-
/** The largest file an 11-digit octal size field can describe: one byte under 8 GiB. */
|
|
17
|
-
export const MAX_TAR_FILE_SIZE = 0o77777777777;
|
|
18
|
-
const TYPE_FILE = '0';
|
|
19
|
-
const TYPE_PAX = 'x';
|
|
20
|
-
const MODE_FILE = 0o644;
|
|
21
|
-
const MODE_EXECUTABLE = 0o755;
|
|
22
|
-
const OWNER_EXECUTE = 0o100;
|
|
23
|
-
/** Offset and length of each header field this format reads or writes. */
|
|
24
|
-
const FIELD = {
|
|
25
|
-
name: [0, NAME_LENGTH],
|
|
26
|
-
mode: [100, 8],
|
|
27
|
-
uid: [108, 8],
|
|
28
|
-
gid: [116, 8],
|
|
29
|
-
size: [124, 12],
|
|
30
|
-
mtime: [136, 12],
|
|
31
|
-
checksum: [148, 8],
|
|
32
|
-
type: [156, 1],
|
|
33
|
-
magic: [257, 6],
|
|
34
|
-
version: [263, 2],
|
|
35
|
-
prefix: [345, 155],
|
|
36
|
-
};
|
|
37
|
-
const corrupt = (message) => new CliError(`the archive is corrupt: ${message}`, EXIT.corrupt);
|
|
38
|
-
const paddingFor = (length) => (BLOCK_SIZE - (length % BLOCK_SIZE)) % BLOCK_SIZE;
|
|
39
|
-
/** Sum of the header's bytes, with the checksum field itself counted as spaces. */
|
|
40
|
-
const checksumOf = (block) => {
|
|
41
|
-
const [start, length] = FIELD.checksum;
|
|
42
|
-
let sum = 0;
|
|
43
|
-
for (let i = 0; i < BLOCK_SIZE; i++) {
|
|
44
|
-
sum += i >= start && i < start + length ? 0x20 : block[i];
|
|
45
|
-
}
|
|
46
|
-
return sum;
|
|
47
|
-
};
|
|
48
|
-
const octal = (value, [, length]) => value.toString(8).padStart(length - 1, '0') + '\0';
|
|
49
|
-
const headerBlock = (name, size, mode, type) => {
|
|
50
|
-
const block = Buffer.alloc(BLOCK_SIZE);
|
|
51
|
-
const put = (field, value) => void block.write(value, field[0], 'latin1');
|
|
52
|
-
put(FIELD.name, name);
|
|
53
|
-
put(FIELD.mode, octal(mode, FIELD.mode));
|
|
54
|
-
put(FIELD.uid, octal(0, FIELD.uid));
|
|
55
|
-
put(FIELD.gid, octal(0, FIELD.gid));
|
|
56
|
-
put(FIELD.size, octal(size, FIELD.size));
|
|
57
|
-
put(FIELD.mtime, octal(0, FIELD.mtime));
|
|
58
|
-
put(FIELD.type, type);
|
|
59
|
-
put(FIELD.magic, 'ustar\0');
|
|
60
|
-
put(FIELD.version, '00');
|
|
61
|
-
put(FIELD.checksum, checksumOf(block).toString(8).padStart(6, '0') + '\0 ');
|
|
62
|
-
return block;
|
|
63
|
-
};
|
|
64
|
-
/** True when `path` can be stored in the 100-byte name field as it is. */
|
|
65
|
-
const fitsNameField = (path) => /^[\x20-\x7e]{1,100}$/.test(path);
|
|
66
|
-
/** A pax record is `<length> <key>=<value>\n`, where the length counts its own digits. */
|
|
67
|
-
const paxRecord = (key, value) => {
|
|
68
|
-
const body = Buffer.from(` ${key}=${value}\n`, 'utf8');
|
|
69
|
-
let length = body.length;
|
|
70
|
-
while (String(length).length + body.length !== length) {
|
|
71
|
-
length = String(length).length + body.length;
|
|
72
|
-
}
|
|
73
|
-
return Buffer.concat([Buffer.from(String(length), 'latin1'), body]);
|
|
74
|
-
};
|
|
75
|
-
/** The header blocks that introduce one file: a pax header first when the path needs one. */
|
|
76
|
-
const entryHeader = ({ path, size, executable }) => {
|
|
77
|
-
const mode = executable ? MODE_EXECUTABLE : MODE_FILE;
|
|
78
|
-
if (fitsNameField(path))
|
|
79
|
-
return headerBlock(path, size, mode, TYPE_FILE);
|
|
80
|
-
const record = paxRecord('path', path);
|
|
81
|
-
// What a reader without pax support would see: the tail of the path, in printable ASCII.
|
|
82
|
-
const fallback = path.replace(/[^\x20-\x7e]/g, '_').slice(-NAME_LENGTH);
|
|
83
|
-
return Buffer.concat([
|
|
84
|
-
headerBlock('PaxHeader', record.length, MODE_FILE, TYPE_PAX),
|
|
85
|
-
record,
|
|
86
|
-
Buffer.alloc(paddingFor(record.length)),
|
|
87
|
-
headerBlock(fallback, size, mode, TYPE_FILE),
|
|
88
|
-
]);
|
|
89
|
-
};
|
|
90
|
-
/** Writes `files` as a tar archive, in the order given. */
|
|
91
|
-
export async function* writeTar(files) {
|
|
92
|
-
for await (const file of files) {
|
|
93
|
-
if (!Number.isSafeInteger(file.size) || file.size < 0 || file.size > MAX_TAR_FILE_SIZE) {
|
|
94
|
-
throw new CliError(`${file.path} is too large to archive (the limit is 8 GiB per file)`);
|
|
95
|
-
}
|
|
96
|
-
yield entryHeader(file);
|
|
97
|
-
let written = 0;
|
|
98
|
-
for await (const piece of file.content) {
|
|
99
|
-
written += piece.length;
|
|
100
|
-
if (written > file.size)
|
|
101
|
-
break;
|
|
102
|
-
yield piece;
|
|
103
|
-
}
|
|
104
|
-
if (written !== file.size) {
|
|
105
|
-
throw new CliError(`${file.path} changed size while it was being archived; run the command again`);
|
|
106
|
-
}
|
|
107
|
-
if (paddingFor(file.size))
|
|
108
|
-
yield Buffer.alloc(paddingFor(file.size));
|
|
109
|
-
}
|
|
110
|
-
yield Buffer.alloc(2 * BLOCK_SIZE);
|
|
111
|
-
}
|
|
112
|
-
const takeBlock = async (reader) => {
|
|
113
|
-
const held = await reader.fill(BLOCK_SIZE);
|
|
114
|
-
if (held.length < BLOCK_SIZE)
|
|
115
|
-
throw corrupt('it ends in the middle of a block');
|
|
116
|
-
const block = held.subarray(0, BLOCK_SIZE);
|
|
117
|
-
reader.skip(BLOCK_SIZE);
|
|
118
|
-
return block;
|
|
119
|
-
};
|
|
120
|
-
/** Streams the next `count.remaining` bytes, keeping the count current as they are handed out. */
|
|
121
|
-
async function* takeBytes(reader, count) {
|
|
122
|
-
while (count.remaining > 0) {
|
|
123
|
-
const held = await reader.fill(1);
|
|
124
|
-
if (!held.length)
|
|
125
|
-
throw corrupt('it ends in the middle of a file');
|
|
126
|
-
const piece = held.subarray(0, Math.min(held.length, count.remaining));
|
|
127
|
-
reader.skip(piece.length);
|
|
128
|
-
count.remaining -= piece.length;
|
|
129
|
-
yield piece;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
const skipBytes = async (reader, length) => {
|
|
133
|
-
for await (const piece of takeBytes(reader, { remaining: length }))
|
|
134
|
-
void piece;
|
|
135
|
-
};
|
|
136
|
-
const textOf = (block, [offset, length]) => {
|
|
137
|
-
const end = block.indexOf(0, offset);
|
|
138
|
-
const stop = end === -1 || end > offset + length ? offset + length : end;
|
|
139
|
-
return block.toString('utf8', offset, stop);
|
|
140
|
-
};
|
|
141
|
-
const numberOf = (block, field, name) => {
|
|
142
|
-
const digits = textOf(block, field).trim();
|
|
143
|
-
if (!/^[0-7]*$/.test(digits))
|
|
144
|
-
throw corrupt(`a header has an unreadable ${name}`);
|
|
145
|
-
return parseInt(digits || '0', 8);
|
|
146
|
-
};
|
|
147
|
-
const parseHeader = (block) => {
|
|
148
|
-
if (textOf(block, FIELD.magic) !== 'ustar')
|
|
149
|
-
throw corrupt('a header is not in ustar format');
|
|
150
|
-
if (numberOf(block, FIELD.checksum, 'checksum') !== checksumOf(block)) {
|
|
151
|
-
throw corrupt('a header fails its checksum');
|
|
152
|
-
}
|
|
153
|
-
const name = textOf(block, FIELD.name);
|
|
154
|
-
const prefix = textOf(block, FIELD.prefix);
|
|
155
|
-
return {
|
|
156
|
-
path: prefix ? `${prefix}/${name}` : name,
|
|
157
|
-
size: numberOf(block, FIELD.size, 'size'),
|
|
158
|
-
mode: numberOf(block, FIELD.mode, 'mode'),
|
|
159
|
-
// Archives older than ustar mark a regular file with a NUL instead of "0".
|
|
160
|
-
type: textOf(block, FIELD.type) || TYPE_FILE,
|
|
161
|
-
};
|
|
162
|
-
};
|
|
163
|
-
/** Reads the one pax record this format writes. Anything else is refused, not skipped. */
|
|
164
|
-
const paxPath = (data) => {
|
|
165
|
-
let path;
|
|
166
|
-
for (let offset = 0; offset < data.length;) {
|
|
167
|
-
const space = data.indexOf(0x20, offset);
|
|
168
|
-
const length = space === -1 ? NaN : Number(data.toString('latin1', offset, space));
|
|
169
|
-
const end = offset + length;
|
|
170
|
-
if (!Number.isInteger(length) || end <= space + 1 || end > data.length || data[end - 1] !== 0x0a) {
|
|
171
|
-
throw corrupt('an extended header is malformed');
|
|
172
|
-
}
|
|
173
|
-
const record = data.toString('utf8', space + 1, end - 1);
|
|
174
|
-
const equals = record.indexOf('=');
|
|
175
|
-
if (record.slice(0, Math.max(equals, 0)) !== 'path') {
|
|
176
|
-
throw corrupt('an extended header carries something other than a path');
|
|
177
|
-
}
|
|
178
|
-
path = record.slice(equals + 1);
|
|
179
|
-
offset = end;
|
|
180
|
-
}
|
|
181
|
-
if (path === undefined)
|
|
182
|
-
throw corrupt('an extended header is empty');
|
|
183
|
-
return path;
|
|
184
|
-
};
|
|
185
|
-
const readPaxPath = async (reader, size) => {
|
|
186
|
-
if (size > MAX_PAX_LENGTH)
|
|
187
|
-
throw corrupt('an extended header is implausibly large');
|
|
188
|
-
const pieces = [];
|
|
189
|
-
for await (const piece of takeBytes(reader, { remaining: size }))
|
|
190
|
-
pieces.push(piece);
|
|
191
|
-
await skipBytes(reader, paddingFor(size));
|
|
192
|
-
return paxPath(Buffer.concat(pieces));
|
|
193
|
-
};
|
|
194
|
-
/**
|
|
195
|
-
* Reads a tar archive one file at a time. An entry's `content` has to be consumed before the
|
|
196
|
-
* next entry is requested; whatever is left of it then is skipped.
|
|
197
|
-
*
|
|
198
|
-
* The generator only finishes once `source` itself has ended. When the source is a decryptor,
|
|
199
|
-
* that is what makes it check its final chunk: an archive that completes normally came from a
|
|
200
|
-
* ciphertext that was complete.
|
|
201
|
-
*/
|
|
202
|
-
export async function* readTar(source) {
|
|
203
|
-
const reader = new ByteReader(source);
|
|
204
|
-
let path;
|
|
205
|
-
for (;;) {
|
|
206
|
-
const block = await takeBlock(reader);
|
|
207
|
-
if (block.every((byte) => byte === 0))
|
|
208
|
-
break;
|
|
209
|
-
const header = parseHeader(block);
|
|
210
|
-
if (header.type === TYPE_PAX) {
|
|
211
|
-
path = await readPaxPath(reader, header.size);
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
if (header.type !== TYPE_FILE) {
|
|
215
|
-
throw corrupt(`"${header.path}" is not a regular file (links and devices are not accepted)`);
|
|
216
|
-
}
|
|
217
|
-
const count = { remaining: header.size };
|
|
218
|
-
yield {
|
|
219
|
-
path: path ?? header.path,
|
|
220
|
-
size: header.size,
|
|
221
|
-
executable: (header.mode & OWNER_EXECUTE) !== 0,
|
|
222
|
-
content: takeBytes(reader, count),
|
|
223
|
-
};
|
|
224
|
-
path = undefined;
|
|
225
|
-
await skipBytes(reader, count.remaining + paddingFor(header.size));
|
|
226
|
-
}
|
|
227
|
-
if (path !== undefined)
|
|
228
|
-
throw corrupt('an extended header is not followed by a file');
|
|
229
|
-
// The end marker is two empty blocks; nothing but padding may follow it.
|
|
230
|
-
do {
|
|
231
|
-
if (reader.buffered.some((byte) => byte !== 0))
|
|
232
|
-
throw corrupt('data follows the end marker');
|
|
233
|
-
reader.skip(reader.buffered.length);
|
|
234
|
-
} while (await reader.more());
|
|
235
|
-
}
|
package/index.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { CLIOptions } from 'inquirerer';
|
|
3
|
-
export * from './commands';
|
|
4
|
-
export * from './utils/bytes';
|
|
5
|
-
export * from './utils/dispatch';
|
|
6
|
-
export * from './utils/errors';
|
|
7
|
-
export * from './utils/io';
|
|
8
|
-
export * from './utils/passphrase';
|
|
9
|
-
export * from './utils/recipients';
|
|
10
|
-
export * from './utils/stash';
|
|
11
|
-
export * from './utils/temp';
|
|
12
|
-
export declare const options: Partial<CLIOptions>;
|
package/utils/bytes.d.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { ParsedArgs } from 'minimist';
|
|
2
|
-
import { Readable } from 'stream';
|
|
3
|
-
/**
|
|
4
|
-
* Byte-level input and output for commands that must not hold a whole file in memory.
|
|
5
|
-
*
|
|
6
|
-
* Everything here is a stream of byte pieces with backpressure: input is a Node `Readable`,
|
|
7
|
-
* transforms are async generators, and output goes through `stream.pipeline`. File output is
|
|
8
|
-
* written to a temporary sibling and renamed over the destination only once every byte has been
|
|
9
|
-
* written and synced. That is what keeps a wrong passphrase, a truncated download or an
|
|
10
|
-
* interrupt from replacing a good file with a partial one.
|
|
11
|
-
*/
|
|
12
|
-
export declare const IO_CHUNK_SIZE: number;
|
|
13
|
-
/** Pieces of a byte sequence, in order: an array of buffers, a generator, a `Readable`. */
|
|
14
|
-
export type ByteStream = Iterable<Uint8Array> | AsyncIterable<Uint8Array>;
|
|
15
|
-
export interface ByteInput {
|
|
16
|
-
/** True when the bytes come from stdin, which then cannot also carry a passphrase. */
|
|
17
|
-
readonly stdin: boolean;
|
|
18
|
-
readonly stream: Readable;
|
|
19
|
-
/** Releases the input. Safe to call at any point, and more than once. */
|
|
20
|
-
close(): void;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Opens the command's input as bytes: `--in <file>`, `-` (stdin), the inline value, then piped
|
|
24
|
-
* stdin — the same order as `readInput`.
|
|
25
|
-
*/
|
|
26
|
-
export declare const openInput: (argv: ParsedArgs, inline?: string) => Promise<ByteInput>;
|
|
27
|
-
/** stdin can carry the data or the passphrase, never both. */
|
|
28
|
-
export declare const assertStdinFree: (argv: ParsedArgs, input: ByteInput) => void;
|
|
29
|
-
/**
|
|
30
|
-
* Reads the front of a byte stream, then hands on whatever was not consumed.
|
|
31
|
-
*
|
|
32
|
-
* A command has to look at the first bytes of its input to know what it was given, and a header
|
|
33
|
-
* has no fixed length. This buffers only as much as it is asked for; {@link ByteReader.rest}
|
|
34
|
-
* streams the remainder untouched.
|
|
35
|
-
*/
|
|
36
|
-
export declare class ByteReader {
|
|
37
|
-
private readonly pieces;
|
|
38
|
-
private held;
|
|
39
|
-
constructor(source: ByteStream);
|
|
40
|
-
/** What has been read from the stream and not yet consumed. */
|
|
41
|
-
get buffered(): Buffer;
|
|
42
|
-
/** Buffers one more piece. Resolves to false once the stream has ended. */
|
|
43
|
-
more(): Promise<boolean>;
|
|
44
|
-
/** Buffers until at least `length` bytes are held, or the stream ends, and returns them. */
|
|
45
|
-
fill(length: number): Promise<Buffer>;
|
|
46
|
-
/** Drops `length` bytes from the front of the buffer. */
|
|
47
|
-
skip(length: number): void;
|
|
48
|
-
/** Reads everything that is left into memory. For inputs that are small by construction. */
|
|
49
|
-
all(): Promise<Buffer>;
|
|
50
|
-
/** Everything that is left, as a stream: the buffered bytes, then the rest of the source. */
|
|
51
|
-
rest(): AsyncGenerator<Uint8Array>;
|
|
52
|
-
}
|
|
53
|
-
/** A chunked cipher: update() for each piece, finish() once. The shape of core's stream classes. */
|
|
54
|
-
export interface ByteTransform {
|
|
55
|
-
update(data: Uint8Array): Uint8Array;
|
|
56
|
-
finish(): Uint8Array;
|
|
57
|
-
}
|
|
58
|
-
/** Runs `source` through `transform`, one piece at a time. */
|
|
59
|
-
export declare function transformBytes(source: ByteStream, transform: ByteTransform): AsyncGenerator<Uint8Array>;
|
|
60
|
-
/** Joins byte streams end to end. */
|
|
61
|
-
export declare function chainBytes(...sources: ByteStream[]): AsyncGenerator<Uint8Array>;
|
|
62
|
-
/** Forces a file that has already been written to disk. */
|
|
63
|
-
export declare const syncFile: (path: string) => Promise<void>;
|
|
64
|
-
/** Best effort: makes a rename durable. Not every platform can fsync a directory. */
|
|
65
|
-
export declare const syncDirectory: (path: string) => Promise<void>;
|
|
66
|
-
/**
|
|
67
|
-
* Writes `path` atomically. The bytes go to a temporary file in the same directory, created
|
|
68
|
-
* owner-only; once all of them are written and synced it is renamed over `path`. If `bytes`
|
|
69
|
-
* fails part-way, or the process is interrupted, `path` is left exactly as it was.
|
|
70
|
-
*/
|
|
71
|
-
export declare const writeFileAtomically: (path: string, bytes: ByteStream) => Promise<void>;
|
|
72
|
-
export interface OutputOptions {
|
|
73
|
-
/** Refuse to write to a terminal: the output is binary. */
|
|
74
|
-
binary?: boolean;
|
|
75
|
-
}
|
|
76
|
-
/** Fails, before any prompt, when binary output would land on a terminal. */
|
|
77
|
-
export declare const assertNotTerminal: (argv: ParsedArgs, options: OutputOptions) => void;
|
|
78
|
-
/** Writes `bytes` to `--out <file>` when given (atomically), otherwise to stdout. */
|
|
79
|
-
export declare const writeBytes: (argv: ParsedArgs, bytes: ByteStream, options?: OutputOptions) => Promise<void>;
|
|
80
|
-
/**
|
|
81
|
-
* Writes decrypted bytes exactly as they were encrypted. Only on a terminal is text that does not
|
|
82
|
-
* end its last line given a newline, so that the prompt does not follow it on the same line.
|
|
83
|
-
*/
|
|
84
|
-
export declare const writePlaintext: (argv: ParsedArgs, plaintext: Uint8Array) => Promise<void>;
|
|
85
|
-
/** Writes text the way {@link writeBytes} writes bytes, ending it with a newline. */
|
|
86
|
-
export declare const writeText: (argv: ParsedArgs, text: string) => Promise<void>;
|
package/utils/bytes.js
DELETED
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.writeText = exports.writePlaintext = exports.writeBytes = exports.assertNotTerminal = exports.writeFileAtomically = exports.syncDirectory = exports.syncFile = exports.ByteReader = exports.assertStdinFree = exports.openInput = exports.IO_CHUNK_SIZE = void 0;
|
|
4
|
-
exports.transformBytes = transformBytes;
|
|
5
|
-
exports.chainBytes = chainBytes;
|
|
6
|
-
const fs_1 = require("fs");
|
|
7
|
-
const promises_1 = require("fs/promises");
|
|
8
|
-
const path_1 = require("path");
|
|
9
|
-
const stream_1 = require("stream");
|
|
10
|
-
const promises_2 = require("stream/promises");
|
|
11
|
-
const errors_1 = require("./errors");
|
|
12
|
-
const temp_1 = require("./temp");
|
|
13
|
-
/**
|
|
14
|
-
* Byte-level input and output for commands that must not hold a whole file in memory.
|
|
15
|
-
*
|
|
16
|
-
* Everything here is a stream of byte pieces with backpressure: input is a Node `Readable`,
|
|
17
|
-
* transforms are async generators, and output goes through `stream.pipeline`. File output is
|
|
18
|
-
* written to a temporary sibling and renamed over the destination only once every byte has been
|
|
19
|
-
* written and synced. That is what keeps a wrong passphrase, a truncated download or an
|
|
20
|
-
* interrupt from replacing a good file with a partial one.
|
|
21
|
-
*/
|
|
22
|
-
exports.IO_CHUNK_SIZE = 64 * 1024;
|
|
23
|
-
const memoryInput = (bytes, stdin = false) => ({
|
|
24
|
-
stdin,
|
|
25
|
-
stream: stream_1.Readable.from([bytes]),
|
|
26
|
-
close() { },
|
|
27
|
-
});
|
|
28
|
-
const fileInput = async (path) => {
|
|
29
|
-
const unreadable = () => new errors_1.CliError(`cannot read ${path}`, errors_1.EXIT.notFound);
|
|
30
|
-
const handle = await (0, promises_1.open)(path, 'r').catch(() => {
|
|
31
|
-
throw unreadable();
|
|
32
|
-
});
|
|
33
|
-
if ((await handle.stat()).isDirectory()) {
|
|
34
|
-
await handle.close();
|
|
35
|
-
throw unreadable();
|
|
36
|
-
}
|
|
37
|
-
// The stream owns the handle from here on: it is closed when the stream ends or is destroyed.
|
|
38
|
-
const stream = handle.createReadStream({ highWaterMark: exports.IO_CHUNK_SIZE });
|
|
39
|
-
return { stdin: false, stream, close: () => void stream.destroy() };
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* stdin as a byte stream.
|
|
43
|
-
*
|
|
44
|
-
* Nothing else may read stdin while it carries the input, which is why the entry point keeps the
|
|
45
|
-
* prompter off a stdin that is not a terminal. A terminal is shared with the prompter, so what
|
|
46
|
-
* is typed there is read in one blocking pass, before control returns to the event loop.
|
|
47
|
-
*/
|
|
48
|
-
const stdinInput = () => {
|
|
49
|
-
if (!process.stdin.isTTY) {
|
|
50
|
-
return { stdin: true, stream: process.stdin, close: () => void process.stdin.pause() };
|
|
51
|
-
}
|
|
52
|
-
try {
|
|
53
|
-
return memoryInput((0, fs_1.readFileSync)(0), true);
|
|
54
|
-
}
|
|
55
|
-
catch {
|
|
56
|
-
throw new errors_1.CliError('failed to read stdin');
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Opens the command's input as bytes: `--in <file>`, `-` (stdin), the inline value, then piped
|
|
61
|
-
* stdin — the same order as `readInput`.
|
|
62
|
-
*/
|
|
63
|
-
const openInput = async (argv, inline) => {
|
|
64
|
-
if (typeof argv.in === 'string' && argv.in.length) {
|
|
65
|
-
return argv.in === '-' ? stdinInput() : fileInput(argv.in);
|
|
66
|
-
}
|
|
67
|
-
if (inline === '-' || argv.stdin)
|
|
68
|
-
return stdinInput();
|
|
69
|
-
if (inline !== undefined && inline !== '')
|
|
70
|
-
return memoryInput(Buffer.from(String(inline), 'utf8'));
|
|
71
|
-
if (!process.stdin.isTTY)
|
|
72
|
-
return stdinInput();
|
|
73
|
-
throw new errors_1.CliError('no input: pass a value, --in <file>, or pipe on stdin');
|
|
74
|
-
};
|
|
75
|
-
exports.openInput = openInput;
|
|
76
|
-
/** stdin can carry the data or the passphrase, never both. */
|
|
77
|
-
const assertStdinFree = (argv, input) => {
|
|
78
|
-
if (input.stdin && (argv['passphrase-stdin'] || argv.passphraseStdin)) {
|
|
79
|
-
throw new errors_1.CliError('stdin cannot carry both the input and the passphrase; use --in <file> or --passphrase-file <path>');
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
exports.assertStdinFree = assertStdinFree;
|
|
83
|
-
const NO_BYTES = Buffer.alloc(0);
|
|
84
|
-
const asBuffer = (bytes) => Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.length);
|
|
85
|
-
/**
|
|
86
|
-
* Reads the front of a byte stream, then hands on whatever was not consumed.
|
|
87
|
-
*
|
|
88
|
-
* A command has to look at the first bytes of its input to know what it was given, and a header
|
|
89
|
-
* has no fixed length. This buffers only as much as it is asked for; {@link ByteReader.rest}
|
|
90
|
-
* streams the remainder untouched.
|
|
91
|
-
*/
|
|
92
|
-
class ByteReader {
|
|
93
|
-
pieces;
|
|
94
|
-
held = NO_BYTES;
|
|
95
|
-
constructor(source) {
|
|
96
|
-
this.pieces =
|
|
97
|
-
Symbol.asyncIterator in source ? source[Symbol.asyncIterator]() : source[Symbol.iterator]();
|
|
98
|
-
}
|
|
99
|
-
/** What has been read from the stream and not yet consumed. */
|
|
100
|
-
get buffered() {
|
|
101
|
-
return this.held;
|
|
102
|
-
}
|
|
103
|
-
/** Buffers one more piece. Resolves to false once the stream has ended. */
|
|
104
|
-
async more() {
|
|
105
|
-
const next = await this.pieces.next();
|
|
106
|
-
if (next.done)
|
|
107
|
-
return false;
|
|
108
|
-
this.held = this.held.length ? Buffer.concat([this.held, next.value]) : asBuffer(next.value);
|
|
109
|
-
return true;
|
|
110
|
-
}
|
|
111
|
-
/** Buffers until at least `length` bytes are held, or the stream ends, and returns them. */
|
|
112
|
-
async fill(length) {
|
|
113
|
-
while (this.held.length < length && (await this.more()))
|
|
114
|
-
;
|
|
115
|
-
return this.held;
|
|
116
|
-
}
|
|
117
|
-
/** Drops `length` bytes from the front of the buffer. */
|
|
118
|
-
skip(length) {
|
|
119
|
-
this.held = this.held.subarray(length);
|
|
120
|
-
}
|
|
121
|
-
/** Reads everything that is left into memory. For inputs that are small by construction. */
|
|
122
|
-
async all() {
|
|
123
|
-
while (await this.more())
|
|
124
|
-
;
|
|
125
|
-
const everything = this.held;
|
|
126
|
-
this.held = NO_BYTES;
|
|
127
|
-
return everything;
|
|
128
|
-
}
|
|
129
|
-
/** Everything that is left, as a stream: the buffered bytes, then the rest of the source. */
|
|
130
|
-
async *rest() {
|
|
131
|
-
if (this.held.length) {
|
|
132
|
-
const head = this.held;
|
|
133
|
-
this.held = NO_BYTES;
|
|
134
|
-
yield head;
|
|
135
|
-
}
|
|
136
|
-
for (let next = await this.pieces.next(); !next.done; next = await this.pieces.next()) {
|
|
137
|
-
yield next.value;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
exports.ByteReader = ByteReader;
|
|
142
|
-
/** Runs `source` through `transform`, one piece at a time. */
|
|
143
|
-
async function* transformBytes(source, transform) {
|
|
144
|
-
for await (const piece of source) {
|
|
145
|
-
const output = transform.update(piece);
|
|
146
|
-
if (output.length)
|
|
147
|
-
yield output;
|
|
148
|
-
}
|
|
149
|
-
yield transform.finish();
|
|
150
|
-
}
|
|
151
|
-
/** Node streams speak Buffer: a bare Uint8Array reads as a list of numbers to anything that inspects a chunk. */
|
|
152
|
-
async function* asBuffers(source) {
|
|
153
|
-
for await (const piece of source)
|
|
154
|
-
yield asBuffer(piece);
|
|
155
|
-
}
|
|
156
|
-
/** Joins byte streams end to end. */
|
|
157
|
-
async function* chainBytes(...sources) {
|
|
158
|
-
for (const source of sources)
|
|
159
|
-
yield* source;
|
|
160
|
-
}
|
|
161
|
-
const statOrUndefined = (path) => {
|
|
162
|
-
try {
|
|
163
|
-
return (0, fs_1.statSync)(path);
|
|
164
|
-
}
|
|
165
|
-
catch {
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
const sync = async (path, flags) => {
|
|
170
|
-
const handle = await (0, promises_1.open)(path, flags);
|
|
171
|
-
try {
|
|
172
|
-
await handle.sync();
|
|
173
|
-
}
|
|
174
|
-
finally {
|
|
175
|
-
await handle.close();
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
/** Forces a file that has already been written to disk. */
|
|
179
|
-
const syncFile = (path) => sync(path, 'r+');
|
|
180
|
-
exports.syncFile = syncFile;
|
|
181
|
-
/** Best effort: makes a rename durable. Not every platform can fsync a directory. */
|
|
182
|
-
const syncDirectory = async (path) => {
|
|
183
|
-
try {
|
|
184
|
-
await sync(path, 'r');
|
|
185
|
-
}
|
|
186
|
-
catch {
|
|
187
|
-
// ignored on purpose
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
exports.syncDirectory = syncDirectory;
|
|
191
|
-
/**
|
|
192
|
-
* Writes `path` atomically. The bytes go to a temporary file in the same directory, created
|
|
193
|
-
* owner-only; once all of them are written and synced it is renamed over `path`. If `bytes`
|
|
194
|
-
* fails part-way, or the process is interrupted, `path` is left exactly as it was.
|
|
195
|
-
*/
|
|
196
|
-
const writeFileAtomically = async (path, bytes) => {
|
|
197
|
-
const existing = statOrUndefined(path);
|
|
198
|
-
if (existing?.isDirectory())
|
|
199
|
-
throw new errors_1.CliError(`cannot write ${path}: it is a directory`);
|
|
200
|
-
if (existing && !existing.isFile()) {
|
|
201
|
-
// A pipe, device or other special file: there is nothing to rename over, so write straight in.
|
|
202
|
-
await (0, promises_2.pipeline)(bytes, (0, fs_1.createWriteStream)(path));
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
// Write through a symlink, as a plain overwrite would, instead of replacing the link.
|
|
206
|
-
const target = existing ? (0, fs_1.realpathSync)(path) : (0, path_1.resolve)(path);
|
|
207
|
-
const temp = (0, temp_1.tempPathFor)(target);
|
|
208
|
-
const handle = await (0, promises_1.open)(temp, 'wx', 0o600).catch(() => {
|
|
209
|
-
throw new errors_1.CliError(`cannot write ${path}`);
|
|
210
|
-
});
|
|
211
|
-
(0, temp_1.trackTemp)(temp);
|
|
212
|
-
try {
|
|
213
|
-
// The stream owns the handle: `flush` syncs the file to disk before the handle is closed, and
|
|
214
|
-
// pipeline() only resolves after that, so the rename never exposes bytes that are not there.
|
|
215
|
-
await (0, promises_2.pipeline)(bytes, handle.createWriteStream({ flush: true }));
|
|
216
|
-
await (0, promises_1.rename)(temp, target);
|
|
217
|
-
}
|
|
218
|
-
catch (error) {
|
|
219
|
-
(0, temp_1.discardTemp)(temp);
|
|
220
|
-
throw error;
|
|
221
|
-
}
|
|
222
|
-
(0, temp_1.untrackTemp)(temp);
|
|
223
|
-
await (0, exports.syncDirectory)((0, path_1.dirname)(target));
|
|
224
|
-
};
|
|
225
|
-
exports.writeFileAtomically = writeFileAtomically;
|
|
226
|
-
const outputFile = (argv) => typeof argv.out === 'string' && argv.out.length && argv.out !== '-' ? argv.out : undefined;
|
|
227
|
-
/** Fails, before any prompt, when binary output would land on a terminal. */
|
|
228
|
-
const assertNotTerminal = (argv, options) => {
|
|
229
|
-
if (options.binary && outputFile(argv) === undefined && process.stdout.isTTY) {
|
|
230
|
-
throw new errors_1.CliError('refusing to write binary ciphertext to a terminal; use --out <file> or redirect stdout');
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
exports.assertNotTerminal = assertNotTerminal;
|
|
234
|
-
/** How many bytes at the end of `bytes` begin a UTF-8 character without finishing it. */
|
|
235
|
-
const unfinished = (bytes) => {
|
|
236
|
-
for (let back = 1; back <= Math.min(3, bytes.length); back++) {
|
|
237
|
-
const byte = bytes[bytes.length - back];
|
|
238
|
-
if ((byte & 0xc0) === 0x80)
|
|
239
|
-
continue; // a continuation byte: its lead byte is further back
|
|
240
|
-
const length = byte >= 0xf0 ? 4 : byte >= 0xe0 ? 3 : byte >= 0xc0 ? 2 : 1;
|
|
241
|
-
return length > back ? back : 0;
|
|
242
|
-
}
|
|
243
|
-
return 0;
|
|
244
|
-
};
|
|
245
|
-
const isText = (bytes) => {
|
|
246
|
-
if (bytes.includes(0))
|
|
247
|
-
return false;
|
|
248
|
-
try {
|
|
249
|
-
new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
250
|
-
return true;
|
|
251
|
-
}
|
|
252
|
-
catch {
|
|
253
|
-
return false;
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
/**
|
|
257
|
-
* Passes text through, and stops at what a terminal should not be sent: a NUL byte, or bytes that
|
|
258
|
-
* are not UTF-8. Only whole characters are passed on: the start of one that is split between two
|
|
259
|
-
* pieces waits for the rest of it, and is never written if the rest does not come.
|
|
260
|
-
*/
|
|
261
|
-
async function* textOnly(bytes) {
|
|
262
|
-
const refusal = () => new errors_1.CliError('refusing to write binary data to a terminal; use --out <file> or redirect stdout');
|
|
263
|
-
let pending = new Uint8Array(0);
|
|
264
|
-
for await (const piece of bytes) {
|
|
265
|
-
const joined = pending.length ? Buffer.concat([pending, piece]) : piece;
|
|
266
|
-
const end = joined.length - unfinished(joined);
|
|
267
|
-
const whole = joined.subarray(0, end);
|
|
268
|
-
if (!isText(whole))
|
|
269
|
-
throw refusal();
|
|
270
|
-
pending = joined.slice(end);
|
|
271
|
-
if (whole.length)
|
|
272
|
-
yield whole;
|
|
273
|
-
}
|
|
274
|
-
if (pending.length)
|
|
275
|
-
throw refusal();
|
|
276
|
-
}
|
|
277
|
-
/** Writes `bytes` to `--out <file>` when given (atomically), otherwise to stdout. */
|
|
278
|
-
const writeBytes = async (argv, bytes, options = {}) => {
|
|
279
|
-
const path = outputFile(argv);
|
|
280
|
-
if (path !== undefined) {
|
|
281
|
-
await (0, exports.writeFileAtomically)(path, bytes);
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
284
|
-
(0, exports.assertNotTerminal)(argv, options);
|
|
285
|
-
// stdout belongs to the process: it is written to, never ended.
|
|
286
|
-
await (0, promises_2.pipeline)(asBuffers(process.stdout.isTTY ? textOnly(bytes) : bytes), process.stdout, { end: false });
|
|
287
|
-
};
|
|
288
|
-
exports.writeBytes = writeBytes;
|
|
289
|
-
const NEWLINE = Buffer.from('\n');
|
|
290
|
-
/**
|
|
291
|
-
* Writes decrypted bytes exactly as they were encrypted. Only on a terminal is text that does not
|
|
292
|
-
* end its last line given a newline, so that the prompt does not follow it on the same line.
|
|
293
|
-
*/
|
|
294
|
-
const writePlaintext = (argv, plaintext) => {
|
|
295
|
-
const shown = outputFile(argv) === undefined && process.stdout.isTTY;
|
|
296
|
-
return (0, exports.writeBytes)(argv, shown && plaintext.at(-1) !== NEWLINE[0] ? [plaintext, NEWLINE] : [plaintext]);
|
|
297
|
-
};
|
|
298
|
-
exports.writePlaintext = writePlaintext;
|
|
299
|
-
/** Writes text the way {@link writeBytes} writes bytes, ending it with a newline. */
|
|
300
|
-
const writeText = (argv, text) => (0, exports.writeBytes)(argv, [Buffer.from(text.endsWith('\n') ? text : `${text}\n`, 'utf8')]);
|
|
301
|
-
exports.writeText = writeText;
|
package/utils/dispatch.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Inquirerer } from 'inquirerer';
|
|
2
|
-
import { ParsedArgs } from 'minimist';
|
|
3
|
-
export type Handler = (argv: ParsedArgs, prompter: Inquirerer) => Promise<void> | void;
|
|
4
|
-
/**
|
|
5
|
-
* `extractFirst` with the argv type preserved — upstream narrows the rest of argv to
|
|
6
|
-
* `{ _: string[] }`, which loses every flag.
|
|
7
|
-
*/
|
|
8
|
-
export declare const takeFirst: (argv: ParsedArgs) => {
|
|
9
|
-
first?: string;
|
|
10
|
-
newArgv: ParsedArgs;
|
|
11
|
-
};
|
|
12
|
-
export interface SubcommandOptions {
|
|
13
|
-
/** Name of the parent command, used in error messages. */
|
|
14
|
-
name: string;
|
|
15
|
-
usage: string;
|
|
16
|
-
handlers: Record<string, Handler>;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Dispatches `dcrypt <group> <subcommand>`: extracts the positional subcommand, prints per-command
|
|
20
|
-
* help on `--help`, and prompts for the subcommand when running interactively without one.
|
|
21
|
-
*/
|
|
22
|
-
export declare const runSubcommand: (argv: ParsedArgs, prompter: Inquirerer, { name, usage, handlers }: SubcommandOptions) => Promise<void>;
|
|
23
|
-
/** Prints `usage` and returns true when the user asked for help. */
|
|
24
|
-
export declare const wantsHelp: (argv: ParsedArgs, usage: string) => boolean;
|