@envseal/prompters 0.1.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 ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined in Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
24
+ permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or Object
36
+ form, made available under the License, as indicated by a copyright
37
+ notice that is included in or attached to the work (an example is
38
+ provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original Work and any Derivative Works thereof, that is
50
+ intentionally submitted to, or received by, Licensor for inclusion
51
+ in the Work by the copyright owner or by an individual or Legal
52
+ Entity authorized to submit on behalf of the copyright owner. For
53
+ the purposes of this definition, "submitted" means any form of
54
+ electronic, verbal, or written communication sent to the Licensor
55
+ or its representatives, including but not limited to communication
56
+ on electronic mailing lists, source code control systems, and issue
57
+ tracking systems that are managed by, or on behalf of, the Licensor
58
+ for the purpose of discussing and improving the Work, but excluding
59
+ communication that is conspicuously marked or otherwise designated
60
+ in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
63
+ which a Contribution has been received by Licensor and subsequently
64
+ incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file, then any
107
+ Derivative Works that You distribute must include a readable
108
+ copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE from the Work, provided that
120
+ such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions of this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contribution.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in appropriate
184
+ comment syntax for the file type. We also recommend that you
185
+ include a file or class name and date of last modification in
186
+ the header comment to make it easier to identify the original
187
+ author and when the file was changed.
188
+
189
+ Copyright 2026 The envseal authors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/dist/ide.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { PromptRequest, PromptResponse } from './types.js';
2
+ export declare class IdePrompter {
3
+ readonly id: 'ide';
4
+ available(): Promise<boolean>;
5
+ cancel(ticket: string): Promise<void>;
6
+ prompt(req: PromptRequest): Promise<PromptResponse>;
7
+ }
8
+ //# sourceMappingURL=ide.d.ts.map
package/dist/ide.js ADDED
@@ -0,0 +1,173 @@
1
+ import { randomBytes } from 'node:crypto';
2
+ import { chmodSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
3
+ import { createConnection } from 'node:net';
4
+ import { homedir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import { asSecret } from '@envseal/protocol';
7
+ // The VS Code side is a later phase; this adapter only discovers a registered
8
+ // broker socket and speaks one-JSON-line-per-message on top of it.
9
+ function isWindows() {
10
+ return process.platform === 'win32';
11
+ }
12
+ const stateDir = join(homedir(), '.envseal');
13
+ const tokenPath = join(stateDir, 'ide-token');
14
+ // --- shared token ----------------------------------------------------------
15
+ // Both this prompter and the extension read `~/.envseal/ide-token`; whichever
16
+ // runs first creates it (32 random hex chars, mode 0600). Every prompt request
17
+ // carries it so an arbitrary local process cannot register itself as the
18
+ // prompter and harvest what the user types. Mirrors the extension's
19
+ // loadOrCreateToken step for step so both sides converge on the same secret no
20
+ // matter who got there first.
21
+ const tokenRegex = /^[0-9a-f]{32}$/;
22
+ let cachedToken;
23
+ function loadOrCreateToken() {
24
+ if (cachedToken !== undefined) {
25
+ return cachedToken;
26
+ }
27
+ try {
28
+ const existing = readFileSync(tokenPath).toString('utf8').trim();
29
+ if (tokenRegex.test(existing)) {
30
+ cachedToken = existing;
31
+ return cachedToken;
32
+ }
33
+ }
34
+ catch (error) {
35
+ const code = error.code;
36
+ if (code !== undefined && code !== 'ENOENT') {
37
+ throw error;
38
+ }
39
+ }
40
+ mkdirSync(stateDir, { recursive: true, mode: 0o700 });
41
+ if (!isWindows()) {
42
+ chmodSync(stateDir, 0o700);
43
+ }
44
+ const fresh = randomBytes(16).toString('hex'); // 32 random hex bytes
45
+ writeFileSync(tokenPath, fresh, { mode: 0o600 });
46
+ if (!isWindows()) {
47
+ chmodSync(tokenPath, 0o600);
48
+ }
49
+ cachedToken = fresh;
50
+ return cachedToken;
51
+ }
52
+ function socketPath() {
53
+ if (isWindows()) {
54
+ return '\\\\.\\pipe\\envseal-ide';
55
+ }
56
+ return join(homedir(), '.envseal', 'ide.sock');
57
+ }
58
+ function tryConnect(timeoutMs) {
59
+ return new Promise((resolve) => {
60
+ let socket = null;
61
+ let settled = false;
62
+ const finish = () => {
63
+ if (settled) {
64
+ return;
65
+ }
66
+ settled = true;
67
+ resolve(socket);
68
+ };
69
+ socket = createConnection({ path: socketPath() });
70
+ socket.once('connect', () => finish());
71
+ socket.once('error', () => {
72
+ socket = null;
73
+ finish();
74
+ });
75
+ setTimeout(() => {
76
+ socket?.destroy();
77
+ socket = null;
78
+ finish();
79
+ }, timeoutMs).unref();
80
+ });
81
+ }
82
+ function parseResult(raw) {
83
+ const r = raw;
84
+ const key = typeof r.key === 'string' ? r.key : '<unknown>';
85
+ const outcomeRaw = r.outcome;
86
+ const outcome = outcomeRaw === 'entered' || outcomeRaw === 'skipped' || outcomeRaw === 'cancelled' || outcomeRaw === 'timeout'
87
+ ? outcomeRaw
88
+ : 'cancelled';
89
+ if (outcome === 'entered') {
90
+ const value = typeof r.value === 'string'
91
+ ? Buffer.from(r.value, 'utf8')
92
+ : Array.isArray(r.value)
93
+ ? Buffer.from(r.value)
94
+ : Buffer.alloc(0);
95
+ return { key, outcome: 'entered', value: asSecret(value) };
96
+ }
97
+ return { key, outcome };
98
+ }
99
+ export class IdePrompter {
100
+ id = 'ide';
101
+ async available() {
102
+ const socket = await tryConnect(750);
103
+ if (socket === null) {
104
+ return false;
105
+ }
106
+ socket.destroy();
107
+ return true;
108
+ }
109
+ async cancel(ticket) {
110
+ const socket = await tryConnect(750);
111
+ if (socket === null) {
112
+ return;
113
+ }
114
+ try {
115
+ socket.write(`${JSON.stringify({ type: 'cancel', ticket })}\n`);
116
+ }
117
+ finally {
118
+ socket.destroy();
119
+ }
120
+ }
121
+ async prompt(req) {
122
+ // Loaded before dialing so a token-file failure never leaves a socket open.
123
+ const token = loadOrCreateToken();
124
+ const socket = await tryConnect(5000);
125
+ if (socket === null) {
126
+ throw new Error('no IDE prompter registered');
127
+ }
128
+ return new Promise((resolve, reject) => {
129
+ let buffer = '';
130
+ socket.on('data', (chunk) => {
131
+ buffer += chunk.toString('utf8');
132
+ const newlineAt = buffer.indexOf('\n');
133
+ if (newlineAt === -1) {
134
+ return;
135
+ }
136
+ const line = buffer.slice(0, newlineAt);
137
+ let parsed;
138
+ try {
139
+ parsed = JSON.parse(line);
140
+ }
141
+ catch {
142
+ socket.destroy();
143
+ reject(new Error('invalid response from IDE prompter'));
144
+ return;
145
+ }
146
+ // A null ticket carrying an error is the extension refusing the
147
+ // connection outright (bad or missing token). Fail fast instead of
148
+ // falling through to the ticket-mismatch guard below, which would sit
149
+ // here waiting for a reply that never comes.
150
+ if (parsed.ticket === null && typeof parsed.error === 'string') {
151
+ socket.destroy();
152
+ reject(new Error(`IDE prompter rejected the request: ${parsed.error}`));
153
+ return;
154
+ }
155
+ if (parsed.ticket !== req.ticket || !Array.isArray(parsed.results)) {
156
+ return;
157
+ }
158
+ socket.destroy();
159
+ resolve({ ticket: req.ticket, results: parsed.results.map(parseResult) });
160
+ });
161
+ socket.once('error', (err) => {
162
+ socket.destroy();
163
+ reject(err);
164
+ });
165
+ // The shared token authenticates the request line; without it the
166
+ // extension answers {ticket: null, error: 'unauthenticated'}. cancel()
167
+ // deliberately stays token-less — the extension closes those
168
+ // connections without answering.
169
+ socket.write(`${JSON.stringify({ type: 'prompt', token, ...req })}\n`);
170
+ });
171
+ }
172
+ }
173
+ //# sourceMappingURL=ide.js.map
@@ -0,0 +1,11 @@
1
+ export type { Prompter, PromptRequest, PromptResponse, PromptKeyRequest, PromptKeyResult, } from './types.js';
2
+ export { makeDisplayNonce } from './types.js';
3
+ export { selectPrompter, allPrompters } from './registry.js';
4
+ export type { SelectOptions } from './registry.js';
5
+ export { LoopbackPrompter } from './loopback.js';
6
+ export type { LoopbackPrompterOptions, LoopbackResult } from './loopback.js';
7
+ export { IdePrompter } from './ide.js';
8
+ export { NativePrompter } from './native.js';
9
+ export { NonePrompter } from './none.js';
10
+ export { TtyPrompter } from './tty.js';
11
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ export { makeDisplayNonce } from './types.js';
2
+ export { selectPrompter, allPrompters } from './registry.js';
3
+ // W3-05: the concrete surfaces are part of the public API. Without these a
4
+ // Tier-2 consumer cannot construct a LoopbackPrompter with options — it cannot
5
+ // suppress the browser launch or observe the listening port — and the repo's
6
+ // own probes had to deep-import `dist/loopback.js`, which is not a public entry
7
+ // point and would break under a package.json `exports` map without subpaths.
8
+ export { LoopbackPrompter } from './loopback.js';
9
+ export { IdePrompter } from './ide.js';
10
+ export { NativePrompter } from './native.js';
11
+ export { NonePrompter } from './none.js';
12
+ export { TtyPrompter } from './tty.js';
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,25 @@
1
+ import type { Prompter, PromptRequest, PromptResponse } from './types.js';
2
+ export interface LoopbackResult extends PromptResponse {
3
+ /** Set when the platform opener could not be launched; the caller should print it. */
4
+ url?: string;
5
+ }
6
+ export interface LoopbackPrompterOptions {
7
+ openBrowser?: boolean;
8
+ onListening?: (info: {
9
+ port: number;
10
+ pathNonce: string;
11
+ url: string;
12
+ }) => void;
13
+ }
14
+ export declare class LoopbackPrompter implements Prompter {
15
+ readonly id: 'loopback-browser';
16
+ private readonly options;
17
+ private openerProbe;
18
+ private current;
19
+ constructor(options?: LoopbackPrompterOptions);
20
+ available(): Promise<boolean>;
21
+ cancel(ticket: string): Promise<void>;
22
+ prompt(req: PromptRequest): Promise<PromptResponse>;
23
+ promptWithUrl(req: PromptRequest): Promise<LoopbackResult>;
24
+ }
25
+ //# sourceMappingURL=loopback.d.ts.map