decryption 0.0.0-stage → 0.5.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.
Files changed (116) hide show
  1. package/README.md +348 -2
  2. package/commands/account.d.ts +4 -0
  3. package/commands/account.js +470 -0
  4. package/commands/cosmology.d.ts +4 -0
  5. package/commands/cosmology.js +97 -0
  6. package/commands/doctor.d.ts +3 -0
  7. package/commands/doctor.js +42 -0
  8. package/commands/encrypt.d.ts +12 -0
  9. package/commands/encrypt.js +251 -0
  10. package/commands/guard.d.ts +3 -0
  11. package/commands/guard.js +92 -0
  12. package/commands/keychain.d.ts +4 -0
  13. package/commands/keychain.js +92 -0
  14. package/commands/keys.d.ts +4 -0
  15. package/commands/keys.js +74 -0
  16. package/commands/passkey.d.ts +4 -0
  17. package/commands/passkey.js +135 -0
  18. package/commands/salt.d.ts +4 -0
  19. package/commands/salt.js +113 -0
  20. package/commands/secrets.d.ts +4 -0
  21. package/commands/secrets.js +214 -0
  22. package/commands/shamir.d.ts +4 -0
  23. package/commands/shamir.js +62 -0
  24. package/commands/vault.d.ts +7 -0
  25. package/commands/vault.js +382 -0
  26. package/commands/wallet.d.ts +5 -0
  27. package/commands/wallet.js +129 -0
  28. package/commands/workspace.d.ts +8 -0
  29. package/commands/workspace.js +289 -0
  30. package/commands.d.ts +8 -0
  31. package/commands.js +126 -0
  32. package/esm/commands/account.js +466 -0
  33. package/esm/commands/cosmology.js +93 -0
  34. package/esm/commands/doctor.js +38 -0
  35. package/esm/commands/encrypt.js +245 -0
  36. package/esm/commands/guard.js +88 -0
  37. package/esm/commands/keychain.js +88 -0
  38. package/esm/commands/keys.js +70 -0
  39. package/esm/commands/passkey.js +131 -0
  40. package/esm/commands/salt.js +109 -0
  41. package/esm/commands/secrets.js +210 -0
  42. package/esm/commands/shamir.js +58 -0
  43. package/esm/commands/vault.js +376 -0
  44. package/esm/commands/wallet.js +125 -0
  45. package/esm/commands/workspace.js +283 -0
  46. package/esm/commands.js +120 -0
  47. package/esm/index.js +50 -0
  48. package/esm/utils/bytes.js +286 -0
  49. package/esm/utils/dispatch.js +52 -0
  50. package/esm/utils/env.js +44 -0
  51. package/esm/utils/errors.js +44 -0
  52. package/esm/utils/header.js +14 -0
  53. package/esm/utils/io.js +49 -0
  54. package/esm/utils/passphrase.js +49 -0
  55. package/esm/utils/recipients.js +74 -0
  56. package/esm/utils/stash.js +61 -0
  57. package/esm/utils/temp.js +51 -0
  58. package/esm/utils/text.js +2 -0
  59. package/esm/workspace/bundle.js +239 -0
  60. package/esm/workspace/doctor.js +104 -0
  61. package/esm/workspace/git.js +72 -0
  62. package/esm/workspace/guard.js +111 -0
  63. package/esm/workspace/layout.js +106 -0
  64. package/esm/workspace/manifest.js +147 -0
  65. package/esm/workspace/recipients.js +37 -0
  66. package/esm/workspace/snippets.js +54 -0
  67. package/esm/workspace/state.js +46 -0
  68. package/esm/workspace/status.js +93 -0
  69. package/esm/workspace/tar.js +235 -0
  70. package/index.d.ts +12 -0
  71. package/index.js +67 -0
  72. package/package.json +53 -4
  73. package/utils/bytes.d.ts +86 -0
  74. package/utils/bytes.js +301 -0
  75. package/utils/dispatch.d.ts +24 -0
  76. package/utils/dispatch.js +58 -0
  77. package/utils/env.d.ts +26 -0
  78. package/utils/env.js +49 -0
  79. package/utils/errors.d.ts +25 -0
  80. package/utils/errors.js +50 -0
  81. package/utils/header.d.ts +4 -0
  82. package/utils/header.js +18 -0
  83. package/utils/io.d.ts +10 -0
  84. package/utils/io.js +57 -0
  85. package/utils/passphrase.d.ts +15 -0
  86. package/utils/passphrase.js +53 -0
  87. package/utils/recipients.d.ts +28 -0
  88. package/utils/recipients.js +80 -0
  89. package/utils/stash.d.ts +40 -0
  90. package/utils/stash.js +76 -0
  91. package/utils/temp.d.ts +10 -0
  92. package/utils/temp.js +59 -0
  93. package/utils/text.d.ts +2 -0
  94. package/utils/text.js +6 -0
  95. package/workspace/bundle.d.ts +50 -0
  96. package/workspace/bundle.js +248 -0
  97. package/workspace/doctor.d.ts +8 -0
  98. package/workspace/doctor.js +108 -0
  99. package/workspace/git.d.ts +21 -0
  100. package/workspace/git.js +83 -0
  101. package/workspace/guard.d.ts +23 -0
  102. package/workspace/guard.js +118 -0
  103. package/workspace/layout.d.ts +47 -0
  104. package/workspace/layout.js +113 -0
  105. package/workspace/manifest.d.ts +52 -0
  106. package/workspace/manifest.js +157 -0
  107. package/workspace/recipients.d.ts +16 -0
  108. package/workspace/recipients.js +45 -0
  109. package/workspace/snippets.d.ts +29 -0
  110. package/workspace/snippets.js +58 -0
  111. package/workspace/state.d.ts +13 -0
  112. package/workspace/state.js +52 -0
  113. package/workspace/status.d.ts +54 -0
  114. package/workspace/status.js +99 -0
  115. package/workspace/tar.d.ts +29 -0
  116. package/workspace/tar.js +240 -0
@@ -0,0 +1,235 @@
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 ADDED
@@ -0,0 +1,12 @@
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/index.js ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.options = void 0;
19
+ const inquirerer_1 = require("inquirerer");
20
+ const commands_1 = require("./commands");
21
+ __exportStar(require("./commands"), exports);
22
+ __exportStar(require("./utils/bytes"), exports);
23
+ __exportStar(require("./utils/dispatch"), exports);
24
+ __exportStar(require("./utils/errors"), exports);
25
+ __exportStar(require("./utils/io"), exports);
26
+ __exportStar(require("./utils/passphrase"), exports);
27
+ __exportStar(require("./utils/recipients"), exports);
28
+ __exportStar(require("./utils/stash"), exports);
29
+ __exportStar(require("./utils/temp"), exports);
30
+ exports.options = {
31
+ minimistOpts: {
32
+ alias: { v: 'version', h: 'help' },
33
+ // everything after `--` belongs to `dcrypt secrets run`
34
+ '--': true,
35
+ string: [
36
+ 'in',
37
+ 'out',
38
+ 'passphrase-file',
39
+ 'salt-file',
40
+ 'aad',
41
+ 'vault',
42
+ 'file',
43
+ 'format',
44
+ 'kdf',
45
+ 'recipient',
46
+ 'recipients-file',
47
+ 'workspace',
48
+ ],
49
+ // declared so that a flag never takes the positional after it as its value:
50
+ // `dcrypt encrypt --binary "some text"`, `dcrypt decrypt-workspace --force finance`
51
+ boolean: ['binary', 'force', 'json', 'staged', 'all'],
52
+ },
53
+ };
54
+ if (require.main === module) {
55
+ const app = new inquirerer_1.CLI(commands_1.commands, {
56
+ ...exports.options,
57
+ // The prompter attaches a line reader to stdin as soon as it is constructed. When stdin is a
58
+ // pipe or a file it is data (or nothing), never a keyboard: a reader there would take bytes
59
+ // away from `--in -`, and a prompt would read the data as its answer.
60
+ noTty: !process.stdin.isTTY,
61
+ version: `dcrypt@${(0, inquirerer_1.getPackageJson)(__dirname).version}`,
62
+ });
63
+ app.run().catch((error) => {
64
+ process.stderr.write(`Unexpected error: ${String(error)}\n`);
65
+ process.exit(1);
66
+ });
67
+ }
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "decryption",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.5.1",
4
+ "description": "dcrypt — local-first CLI for encryption, BIP39 wallets and team secrets",
5
+ "author": "Constructive <developers@constructive.io>",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/constructive-io/decryption",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/constructive-io/decryption"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/constructive-io/decryption/issues"
21
+ },
22
+ "keywords": [
23
+ "cli",
24
+ "dcrypt",
25
+ "encryption",
26
+ "secrets",
27
+ "wallet",
28
+ "bip39",
29
+ "age"
30
+ ],
31
+ "dependencies": {
32
+ "@decryption/accounts": "0.2.1",
33
+ "@decryption/base": "0.4.0",
34
+ "@decryption/core": "0.5.1",
35
+ "@decryption/cosmology-compat": "0.4.2",
36
+ "@decryption/hashes": "0.4.0",
37
+ "@decryption/keys": "0.5.1",
38
+ "@decryption/secrets": "0.4.2",
39
+ "@decryption/shamir": "0.4.1",
40
+ "@decryption/vault": "0.4.1",
41
+ "@decryption/wallet": "0.4.1",
42
+ "@decryption/webauthn": "0.2.1",
43
+ "appstash": "^0.7.0",
44
+ "inquirerer": "^4.9.1",
45
+ "yanse": "^0.2.1"
46
+ },
47
+ "devDependencies": {
48
+ "@types/minimist": "^1.2.5",
49
+ "makage": "0.3.0"
50
+ },
51
+ "bin": {
52
+ "dcrypt": "index.js"
53
+ },
54
+ "gitHead": "d29d5f2e516edf049a285b30f868360d3b996db0"
55
+ }
@@ -0,0 +1,86 @@
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>;