@michinarai/cli 0.0.0-stage → 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 +202 -0
- package/README.md +150 -2
- package/bin/run.js +4 -0
- package/dist/api/client.d.ts +24 -0
- package/dist/api/client.js +56 -0
- package/dist/api/client.js.map +1 -0
- package/dist/api/tool-flag.d.ts +9 -0
- package/dist/api/tool-flag.js +15 -0
- package/dist/api/tool-flag.js.map +1 -0
- package/dist/api/tools.d.ts +17 -0
- package/dist/api/tools.js +39 -0
- package/dist/api/tools.js.map +1 -0
- package/dist/auth/loopback.d.ts +11 -0
- package/dist/auth/loopback.js +54 -0
- package/dist/auth/loopback.js.map +1 -0
- package/dist/auth/oauth.d.ts +52 -0
- package/dist/auth/oauth.js +101 -0
- package/dist/auth/oauth.js.map +1 -0
- package/dist/auth/session.d.ts +7 -0
- package/dist/auth/session.js +41 -0
- package/dist/auth/session.js.map +1 -0
- package/dist/base-command.d.ts +29 -0
- package/dist/base-command.js +65 -0
- package/dist/base-command.js.map +1 -0
- package/dist/commands/build.d.ts +9 -0
- package/dist/commands/build.js +34 -0
- package/dist/commands/build.js.map +1 -0
- package/dist/commands/dev.d.ts +14 -0
- package/dist/commands/dev.js +55 -0
- package/dist/commands/dev.js.map +1 -0
- package/dist/commands/init.d.ts +12 -0
- package/dist/commands/init.js +63 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/login.d.ts +10 -0
- package/dist/commands/login.js +37 -0
- package/dist/commands/login.js.map +1 -0
- package/dist/commands/logout.d.ts +5 -0
- package/dist/commands/logout.js +19 -0
- package/dist/commands/logout.js.map +1 -0
- package/dist/commands/tools/list.d.ts +8 -0
- package/dist/commands/tools/list.js +23 -0
- package/dist/commands/tools/list.js.map +1 -0
- package/dist/commands/tools/push.d.ts +11 -0
- package/dist/commands/tools/push.js +53 -0
- package/dist/commands/tools/push.js.map +1 -0
- package/dist/commands/tools/register.d.ts +13 -0
- package/dist/commands/tools/register.js +66 -0
- package/dist/commands/tools/register.js.map +1 -0
- package/dist/commands/tools/tokens/create.d.ts +12 -0
- package/dist/commands/tools/tokens/create.js +26 -0
- package/dist/commands/tools/tokens/create.js.map +1 -0
- package/dist/commands/tools/tokens/list.d.ts +8 -0
- package/dist/commands/tools/tokens/list.js +13 -0
- package/dist/commands/tools/tokens/list.js.map +1 -0
- package/dist/commands/tools/tokens/revoke.d.ts +11 -0
- package/dist/commands/tools/tokens/revoke.js +18 -0
- package/dist/commands/tools/tokens/revoke.js.map +1 -0
- package/dist/commands/validate.d.ts +6 -0
- package/dist/commands/validate.js +27 -0
- package/dist/commands/validate.js.map +1 -0
- package/dist/commands.d.ts +26 -0
- package/dist/commands.js +29 -0
- package/dist/commands.js.map +1 -0
- package/dist/generated/api.d.ts +1499 -0
- package/dist/generated/api.js +6 -0
- package/dist/generated/api.js.map +1 -0
- package/dist/lib/credentials.d.ts +8 -0
- package/dist/lib/credentials.js +45 -0
- package/dist/lib/credentials.js.map +1 -0
- package/dist/lib/deps.d.ts +12 -0
- package/dist/lib/deps.js +23 -0
- package/dist/lib/deps.js.map +1 -0
- package/dist/lib/digest.d.ts +10 -0
- package/dist/lib/digest.js +37 -0
- package/dist/lib/digest.js.map +1 -0
- package/dist/lib/errors.d.ts +26 -0
- package/dist/lib/errors.js +39 -0
- package/dist/lib/errors.js.map +1 -0
- package/dist/lib/http.d.ts +2 -0
- package/dist/lib/http.js +11 -0
- package/dist/lib/http.js.map +1 -0
- package/dist/lib/lock.d.ts +4 -0
- package/dist/lib/lock.js +58 -0
- package/dist/lib/lock.js.map +1 -0
- package/dist/lib/output.d.ts +21 -0
- package/dist/lib/output.js +44 -0
- package/dist/lib/output.js.map +1 -0
- package/dist/lib/plan.d.ts +13 -0
- package/dist/lib/plan.js +10 -0
- package/dist/lib/plan.js.map +1 -0
- package/dist/lib/project.d.ts +8 -0
- package/dist/lib/project.js +37 -0
- package/dist/lib/project.js.map +1 -0
- package/package.json +59 -4
- package/templates/tool/.claude/skills/narai-tool/SKILL.md +16 -0
- package/templates/tool/AGENTS.md +30 -0
- package/templates/tool/narai.tool.json +25 -0
- package/templates/tool/package.json +14 -0
- package/templates/tool/scripts/build.mjs +12 -0
- package/templates/tool/src/index.html +19 -0
- package/templates/tool/src/tool.css +2 -0
- package/templates/tool/src/tool.js +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,151 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @michinarai/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`narai`, the command line for building tools that run inside Narai lessons, on the public
|
|
4
|
+
[`@michinarai/sdk`](https://github.com/Monster-Orange/mo-narai-sdk).
|
|
5
|
+
|
|
6
|
+
New to Narai tools? The course [Build a tool for Narai](https://beta.michinar.ai/go/course/narai-tools) teaches the
|
|
7
|
+
platform one step at a time, and [mo-narai-labs](https://github.com/Monster-Orange/mo-narai-labs) has Narai's own tools
|
|
8
|
+
to read.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install -g @michinarai/cli
|
|
14
|
+
narai --help
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Node 20 or later. Or run any command without installing: `npx @michinarai/cli <command>`.
|
|
18
|
+
|
|
19
|
+
## Five minutes: a tool running in the harness
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
narai init hello --publisher you # a new tool in ./hello, its id you/hello
|
|
23
|
+
cd hello
|
|
24
|
+
npm install # the SDK, from npm
|
|
25
|
+
npm run dev # builds it, then `narai dev`: open the host address it prints
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The harness page is Narai's host, with your tool in a sandboxed frame on another origin, the way a lesson mounts it.
|
|
29
|
+
It shows every message the tool sends, with the verdict Narai's host would give it, and sends the manifest's
|
|
30
|
+
commands as the Tutor would. Change `src/tool.js`, run `npm run dev` again.
|
|
31
|
+
|
|
32
|
+
## Commands
|
|
33
|
+
|
|
34
|
+
Every command takes `--json`, `--yes` and `--no-input`. Results go to stdout, progress to stderr. Exit codes: 0 ok,
|
|
35
|
+
1 error, 2 usage, 3 auth. `--narai-url` (or `NARAI_URL`) picks the Narai to talk to; the default is
|
|
36
|
+
`https://beta.michinar.ai`.
|
|
37
|
+
|
|
38
|
+
**`narai init <name> --publisher <slug>`** writes a new tool from the template: `narai.tool.json` (the manifest), the
|
|
39
|
+
page (`src/`), its build, an `AGENTS.md` and a skill for a coding agent.
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
narai init fretboard --publisher you --dir tools/fretboard
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**`narai validate`** checks `narai.tool.json` against the contract, as Narai's registry does: the schema, then what a
|
|
46
|
+
schema can't say (every payload named is declared, no answer carries a verdict, commands need an embed that runs the
|
|
47
|
+
SDK). It prints every reason, and the SDK's warnings.
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
narai validate --json
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
**`narai build`** seals a release after the tool's own build: every file in `dist/` with its sha256, and the bundle
|
|
54
|
+
digest, written into `dist/narai.tool.json`.
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
node scripts/build.mjs && narai build
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**`narai dev`** runs the built tool in the local harness.
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
narai dev --locale ja --task task.json # task.json: the task Narai would start it on ({id, kind, assist, prompt})
|
|
64
|
+
narai dev --preset preset.json --port 4000
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**`narai login`** signs in to Narai in the browser. The answer comes back to this machine only (127.0.0.1), and the
|
|
68
|
+
session stays in the OS keychain. **`narai logout`** revokes it.
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
narai login # or --no-browser, to print the address instead
|
|
72
|
+
narai logout
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**`narai tools register`**, **`push`** and **`list`** work with Narai's registry; sign in first. `register` sets up your
|
|
76
|
+
publisher (once per organization) and the tool. `push` uploads the sealed build (`narai build`'s `dist/`) as a draft
|
|
77
|
+
version, once per version: the same build again changes nothing, and a different build under the same version is
|
|
78
|
+
refused (raise the version). Both plan, then apply after a confirmation. `--plan` only shows the changes, and with
|
|
79
|
+
`--detailed-exitcode` it exits 2 when there are some, 0 when there are none. In CI, set `NARAI_TOKEN` (a tool's token)
|
|
80
|
+
and pass `--yes`.
|
|
81
|
+
|
|
82
|
+
```sh
|
|
83
|
+
narai tools register --org "Your Org" --plan # what would change
|
|
84
|
+
narai tools register
|
|
85
|
+
narai tools push
|
|
86
|
+
narai tools list --publisher you
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**`narai tools tokens create`**, **`list`** and **`revoke`** manage a tool's CI tokens: what `NARAI_TOKEN` holds in
|
|
90
|
+
CI. A token reads and uploads this tool's versions, in this environment only. `create` prints the token once.
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
narai tools tokens create --label github-actions --days 90
|
|
94
|
+
narai tools tokens list
|
|
95
|
+
narai tools tokens revoke <id>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Today a tool is self-hosted: `surfaces.embed.url` is its own `https://` address. Bundles hosted by Narai (a relative
|
|
99
|
+
`embed.url`, such as `index.html`) need a newer Narai. A command that reaches a Narai without the route it needs says
|
|
100
|
+
so and stops; it changes nothing.
|
|
101
|
+
|
|
102
|
+
## Develop
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
npm ci
|
|
106
|
+
npm test
|
|
107
|
+
npm run lint
|
|
108
|
+
npm run typecheck
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Stability
|
|
112
|
+
|
|
113
|
+
0.x: the contract (the manifest, the wire, the registry's API) can still change between minor versions, and the
|
|
114
|
+
changelog says how. 1.0 will hold it.
|
|
115
|
+
|
|
116
|
+
## Releasing
|
|
117
|
+
|
|
118
|
+
npm's trusted publishing only: no npm token, in CI or anywhere else. The workflow is `.github/workflows/release.yml`.
|
|
119
|
+
|
|
120
|
+
**Every release.** Merge a PR that bumps `version` in package.json (and says what changed in `CHANGELOG.md`): that's
|
|
121
|
+
the release. On every push to main, the workflow asks npm whether that version is published. When it isn't, the
|
|
122
|
+
workflow runs the gates, packs the tarball and its `SHA256SUMS`, tags the commit `v<version>`, **publishes** it on npm
|
|
123
|
+
over OIDC (`npm publish`), and makes a GitHub release with both files. A version already on npm: nothing happens.
|
|
124
|
+
There's no step for a person, no tag to push and nothing to approve.
|
|
125
|
+
|
|
126
|
+
**The first publish of the package, once.** Trusted publishing can't create a package, so a maintainer publishes the
|
|
127
|
+
first version (0.1.0) by hand, with one line, from a fresh clone of main:
|
|
128
|
+
|
|
129
|
+
```sh
|
|
130
|
+
rm -rf /tmp/narai-cli && git clone -q --depth 1 https://github.com/Monster-Orange/mo-narai-cli.git /tmp/narai-cli && cd /tmp/narai-cli && npm ci && npm test && npm login --auth-type=web && npm publish --access public && npm logout
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Then, on npmjs.com, the package › Settings › Trusted publisher › GitHub Actions: organization `Monster-Orange`,
|
|
134
|
+
repository `mo-narai-cli`, workflow `release.yml`, allowed to publish directly; and Publishing access: **Require
|
|
135
|
+
two-factor authentication and disallow tokens**. Until the package exists, the workflow's check finds it missing and
|
|
136
|
+
stops with a warning (no failed run).
|
|
137
|
+
|
|
138
|
+
The CLI depends on `@michinarai/sdk` from npm, at one exact version: the workflow refuses a git, file or URL
|
|
139
|
+
dependency.
|
|
140
|
+
|
|
141
|
+
Mind:
|
|
142
|
+
|
|
143
|
+
- The trusted-publisher fields are case-sensitive, and npm doesn't check them when they're saved. A mismatch fails
|
|
144
|
+
only at publish time, as an `E404` that looks like a missing package. package.json's `repository.url` must be this
|
|
145
|
+
repository exactly.
|
|
146
|
+
- A new trusted-publisher configuration must publish within 2 days, or it expires.
|
|
147
|
+
- npm trusts the workflow by its file name, `release.yml`: renaming it breaks publishing (an `E404`) until the
|
|
148
|
+
trusted publisher is changed to match.
|
|
149
|
+
- Provenance is on: npm generates it under trusted publishing for a public repository and a public package, with no
|
|
150
|
+
flag. package.json's `repository.url` is what npm checks it against. The one-time first
|
|
151
|
+
publish, from a maintainer's machine, has none.
|
package/bin/run.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import createClient from "openapi-fetch";
|
|
2
|
+
import { NaraiError } from "../lib/errors.js";
|
|
3
|
+
import type { components, paths } from "../generated/api.js";
|
|
4
|
+
export type Registry = ReturnType<typeof createClient<paths>>;
|
|
5
|
+
export type Refusal = components["schemas"]["Refusal"];
|
|
6
|
+
/** The registry's client, generated from its OpenAPI (src/generated/api.ts): every call with the session's (or
|
|
7
|
+
* NARAI_TOKEN's) bearer token, no redirects followed. */
|
|
8
|
+
export declare function registry(naraiUrl: string): Registry;
|
|
9
|
+
/** A refusal as the CLI reports it: 401/403 exit 3 (auth); a 404 says the registry may be off, or the route isn't
|
|
10
|
+
* served yet; 422 carries reasons. */
|
|
11
|
+
export declare function refused(response: Response, error: unknown, what: string): NaraiError;
|
|
12
|
+
/** The data of a call that must succeed, or its refusal. */
|
|
13
|
+
export declare function ok<T>(call: Promise<{
|
|
14
|
+
data?: T | undefined;
|
|
15
|
+
error?: unknown;
|
|
16
|
+
response: Response;
|
|
17
|
+
}>, what: string): Promise<T>;
|
|
18
|
+
/** The data of a lookup, null when it isn't there (404 not_found), else its refusal. A route this Narai doesn't serve is
|
|
19
|
+
* never "not there": push would take it for a version not uploaded yet. */
|
|
20
|
+
export declare function found<T>(call: Promise<{
|
|
21
|
+
data?: T | undefined;
|
|
22
|
+
error?: unknown;
|
|
23
|
+
response: Response;
|
|
24
|
+
}>, what: string): Promise<T | null>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import createClient from "openapi-fetch";
|
|
2
|
+
import { bearer } from "../auth/session.js";
|
|
3
|
+
import { AuthError, ExitCode, NaraiError } from "../lib/errors.js";
|
|
4
|
+
/** The registry's client, generated from its OpenAPI (src/generated/api.ts): every call with the session's (or
|
|
5
|
+
* NARAI_TOKEN's) bearer token, no redirects followed. */
|
|
6
|
+
export function registry(naraiUrl) {
|
|
7
|
+
const client = createClient({ baseUrl: naraiUrl, redirect: "manual" });
|
|
8
|
+
client.use({
|
|
9
|
+
async onRequest({ request }) {
|
|
10
|
+
request.headers.set("authorization", `Bearer ${await bearer(naraiUrl)}`);
|
|
11
|
+
return request;
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
return client;
|
|
15
|
+
}
|
|
16
|
+
/** A 404 that isn't Narai's refusal but its web framework's: the route isn't there, on a Narai older than this CLI. */
|
|
17
|
+
function notServed(response, error) {
|
|
18
|
+
const body = (error ?? {});
|
|
19
|
+
return response.status === 404 && body.error === undefined && body.detail === "Not Found";
|
|
20
|
+
}
|
|
21
|
+
/** A refusal as the CLI reports it: 401/403 exit 3 (auth); a 404 says the registry may be off, or the route isn't
|
|
22
|
+
* served yet; 422 carries reasons. */
|
|
23
|
+
export function refused(response, error, what) {
|
|
24
|
+
const body = (error ?? {});
|
|
25
|
+
if (notServed(response, error)) {
|
|
26
|
+
return new NaraiError(`${what}: ${new URL(response.url || "http://narai").host} doesn't serve this yet; it needs a newer Narai. Nothing was changed`, "not_served", ExitCode.error, { status: 404 });
|
|
27
|
+
}
|
|
28
|
+
const code = typeof body.error === "string" ? body.error : `http_${response.status}`;
|
|
29
|
+
const message = typeof body.message === "string" ? body.message : `HTTP ${response.status}`;
|
|
30
|
+
const details = { status: response.status };
|
|
31
|
+
if (Array.isArray(body.reasons))
|
|
32
|
+
details["reasons"] = body.reasons;
|
|
33
|
+
if (response.status === 401)
|
|
34
|
+
return new AuthError(`${what}: not signed in, or the session ended (${message}); run narai login`, code);
|
|
35
|
+
if (response.status === 403)
|
|
36
|
+
return new NaraiError(`${what}: not allowed (${message})`, code, ExitCode.auth, details);
|
|
37
|
+
return new NaraiError(`${what}: ${message}`, code, ExitCode.error, details);
|
|
38
|
+
}
|
|
39
|
+
/** The data of a call that must succeed, or its refusal. */
|
|
40
|
+
export async function ok(call, what) {
|
|
41
|
+
const { data, error, response } = await call;
|
|
42
|
+
if (!response.ok || data === undefined)
|
|
43
|
+
throw refused(response, error, what);
|
|
44
|
+
return data;
|
|
45
|
+
}
|
|
46
|
+
/** The data of a lookup, null when it isn't there (404 not_found), else its refusal. A route this Narai doesn't serve is
|
|
47
|
+
* never "not there": push would take it for a version not uploaded yet. */
|
|
48
|
+
export async function found(call, what) {
|
|
49
|
+
const { data, error, response } = await call;
|
|
50
|
+
if (response.status === 404 && !notServed(response, error) && error?.error !== "tools_unavailable")
|
|
51
|
+
return null;
|
|
52
|
+
if (!response.ok || data === undefined)
|
|
53
|
+
throw refused(response, error, what);
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/api/client.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAMnE;0DAC0D;AAC1D,MAAM,UAAU,QAAQ,CAAC,QAAgB;IACvC,MAAM,MAAM,GAAG,YAAY,CAAQ,EAAE,OAA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,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,KAAK,CAAC,IAAK,KAAsC,EAAE,KAAK,KAAK,mBAAmB;QAAE,OAAO,IAAI,CAAC;IAClJ,IAAI,CAAC,QAAQ,CAAC,EAAE,IAAI,IAAI,KAAK,SAAS;QAAE,MAAM,OAAO,CAAC,QAAQ,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IAC7E,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const toolFlag: {
|
|
2
|
+
tool: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
3
|
+
};
|
|
4
|
+
/** The tool a command acts on: --tool, else the manifest here. */
|
|
5
|
+
export declare function whichTool(given: string | undefined): Promise<{
|
|
6
|
+
publisher: string;
|
|
7
|
+
name: string;
|
|
8
|
+
id: string;
|
|
9
|
+
}>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Flags } from "@oclif/core";
|
|
2
|
+
import { NaraiError } from "../lib/errors.js";
|
|
3
|
+
import { readManifest, toolRoot } from "../lib/project.js";
|
|
4
|
+
export const toolFlag = {
|
|
5
|
+
tool: Flags.string({ description: "The tool, publisher/name (default: this repository's manifest)." }),
|
|
6
|
+
};
|
|
7
|
+
/** The tool a command acts on: --tool, else the manifest here. */
|
|
8
|
+
export async function whichTool(given) {
|
|
9
|
+
const id = given ?? String((await readManifest(await toolRoot(process.cwd()))).id ?? "");
|
|
10
|
+
const [publisher, name, ...rest] = id.split("/");
|
|
11
|
+
if (!publisher || !name || rest.length)
|
|
12
|
+
throw new NaraiError(`${id || "(none)"} isn't publisher/name`, "bad_tool", 2);
|
|
13
|
+
return { publisher, name, id };
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=tool-flag.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-flag.js","sourceRoot":"","sources":["../../src/api/tool-flag.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE3D,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,iEAAiE,EAAE,CAAC;CACvG,CAAC;AAEF,kEAAkE;AAClE,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,KAAyB;IACvD,MAAM,EAAE,GAAG,KAAK,IAAI,MAAM,CAAC,CAAC,MAAM,YAAY,CAAC,MAAM,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAsB,CAAA,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7G,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACjD,IAAI,CAAC,SAAS,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM;QAAE,MAAM,IAAI,UAAU,CAAC,GAAG,EAAE,IAAI,QAAQ,uBAAuB,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC;IACtH,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;AACjC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ToolManifest } from "@michinarai/sdk";
|
|
2
|
+
import { type Registry } from "./client.js";
|
|
3
|
+
import type { components } from "../generated/api.js";
|
|
4
|
+
export type Publisher = components["schemas"]["PublisherOut"];
|
|
5
|
+
/** The organizations you can develop for, each with its publisher (slug null: none yet). A 404 here means tool
|
|
6
|
+
* development isn't open on this Narai. */
|
|
7
|
+
export declare function publishers(api: Registry, host: string): Promise<Publisher[]>;
|
|
8
|
+
/** `publisher/name` of a manifest. */
|
|
9
|
+
export declare function idParts(manifest: ToolManifest): {
|
|
10
|
+
publisher: string;
|
|
11
|
+
name: string;
|
|
12
|
+
};
|
|
13
|
+
/** The sealed release in `dir` (narai build), checked to be what's there now and what the manifest says now. */
|
|
14
|
+
export declare function sealedRelease(root: string, dir: string, manifest: ToolManifest): Promise<ToolManifest & {
|
|
15
|
+
files: NonNullable<ToolManifest["files"]>;
|
|
16
|
+
bundle_digest: string;
|
|
17
|
+
}>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import {} from "@michinarai/sdk";
|
|
4
|
+
import { bundleDigest, digestFiles } from "../lib/digest.js";
|
|
5
|
+
import { NaraiError } from "../lib/errors.js";
|
|
6
|
+
import { MANIFEST } from "../lib/project.js";
|
|
7
|
+
import { ok } from "./client.js";
|
|
8
|
+
/** The organizations you can develop for, each with its publisher (slug null: none yet). A 404 here means tool
|
|
9
|
+
* development isn't open on this Narai. */
|
|
10
|
+
export async function publishers(api, host) {
|
|
11
|
+
const answer = await api.GET("/api/tools/publishers");
|
|
12
|
+
if (answer.response.status === 404)
|
|
13
|
+
throw new NaraiError(`tool development isn't open on ${host}`, "tools_unavailable");
|
|
14
|
+
return (await ok(Promise.resolve(answer), "publishers")).publishers;
|
|
15
|
+
}
|
|
16
|
+
/** `publisher/name` of a manifest. */
|
|
17
|
+
export function idParts(manifest) {
|
|
18
|
+
const [publisher = "", name = ""] = manifest.id.split("/");
|
|
19
|
+
return { publisher, name };
|
|
20
|
+
}
|
|
21
|
+
/** The sealed release in `dir` (narai build), checked to be what's there now and what the manifest says now. */
|
|
22
|
+
export async function sealedRelease(root, dir, manifest) {
|
|
23
|
+
let release;
|
|
24
|
+
try {
|
|
25
|
+
release = JSON.parse(await readFile(join(root, dir, MANIFEST), "utf8"));
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
throw new NaraiError(`${dir}/${MANIFEST} isn't there: run narai build`, "not_built");
|
|
29
|
+
}
|
|
30
|
+
const files = await digestFiles(join(root, dir));
|
|
31
|
+
const { files: _f, bundle_digest: _d, ...sealed } = release;
|
|
32
|
+
const stale = JSON.stringify(files) !== JSON.stringify(release.files) || bundleDigest(files) !== release.bundle_digest
|
|
33
|
+
|| JSON.stringify(sealed) !== JSON.stringify(manifest);
|
|
34
|
+
if (stale || !release.bundle_digest || !release.files) {
|
|
35
|
+
throw new NaraiError(`${dir}/ or ${MANIFEST} changed since narai build: run it again`, "stale_build");
|
|
36
|
+
}
|
|
37
|
+
return release;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/api/tools.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAqB,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,EAAE,EAAiB,MAAM,aAAa,CAAC;AAKhD;4CAC4C;AAC5C,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,GAAa,EAAE,IAAY;IAC1D,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IACtD,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,KAAK,GAAG;QAAE,MAAM,IAAI,UAAU,CAAC,kCAAkC,IAAI,EAAE,EAAE,mBAAmB,CAAC,CAAC;IACxH,OAAO,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,YAAY,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D,MAAM,IAAI,UAAU,CAAC,GAAG,GAAG,QAAQ,QAAQ,0CAA0C,EAAE,aAAa,CAAC,CAAC;IACxG,CAAC;IACD,OAAO,OAA8F,CAAC;AACxG,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const CALLBACK = "/callback";
|
|
2
|
+
export interface Loopback {
|
|
3
|
+
redirectUri: string;
|
|
4
|
+
code: Promise<string>;
|
|
5
|
+
close(): void;
|
|
6
|
+
}
|
|
7
|
+
export declare function startLoopback(o: {
|
|
8
|
+
state: string;
|
|
9
|
+
issuer: string | null;
|
|
10
|
+
timeoutMs: number;
|
|
11
|
+
}): Promise<Loopback>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// The loopback redirect of RFC 8252 §7.3: a one-shot server on 127.0.0.1 and a free port, the only place the code
|
|
2
|
+
// goes. It answers one request to /callback, checks `state` (and `iss`, RFC 9207) before it trusts anything in it,
|
|
3
|
+
// and gives up after a timeout.
|
|
4
|
+
import { timingSafeEqual } from "node:crypto";
|
|
5
|
+
import { createServer } from "node:http";
|
|
6
|
+
import { NaraiError } from "../lib/errors.js";
|
|
7
|
+
import { OAuthError } from "./oauth.js";
|
|
8
|
+
export const CALLBACK = "/callback";
|
|
9
|
+
const page = (title, body) => `<!doctype html><meta charset="utf-8"><title>${title}</title><body style="font-family:system-ui;margin:4rem auto;max-width:32rem"><h1>${title}</h1><p>${body}</p></body>`;
|
|
10
|
+
const same = (a, b) => {
|
|
11
|
+
const [x, y] = [Buffer.from(a), Buffer.from(b)];
|
|
12
|
+
return x.length === y.length && timingSafeEqual(x, y);
|
|
13
|
+
};
|
|
14
|
+
export async function startLoopback(o) {
|
|
15
|
+
let settle;
|
|
16
|
+
const code = new Promise((resolve, reject) => { settle = { resolve, reject }; });
|
|
17
|
+
code.catch(() => undefined); // the caller awaits it; a rejection before then isn't unhandled
|
|
18
|
+
let done = false;
|
|
19
|
+
const server = createServer((req, res) => {
|
|
20
|
+
const port = server.address().port;
|
|
21
|
+
const url = new URL(req.url ?? "/", `http://127.0.0.1:${port}`);
|
|
22
|
+
// a page elsewhere can make the browser request a loopback URL, but not with this Host (DNS rebinding sends its own)
|
|
23
|
+
if (done || req.method !== "GET" || url.pathname !== CALLBACK || req.headers.host !== `127.0.0.1:${port}`) {
|
|
24
|
+
res.writeHead(404, { "content-type": "text/plain" }).end("Not found");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
done = true;
|
|
28
|
+
const finish = (status, title, body, outcome) => {
|
|
29
|
+
res.writeHead(status, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" });
|
|
30
|
+
res.end(page(title, body), () => { close(); outcome(); });
|
|
31
|
+
};
|
|
32
|
+
const q = url.searchParams;
|
|
33
|
+
if (!same(q.get("state") ?? "", o.state)) {
|
|
34
|
+
finish(400, "Sign-in failed", "This answer isn't for the sign-in narai started.", () => settle.reject(new NaraiError("sign-in failed: the answer's state doesn't match", "oauth_state_mismatch", 3)));
|
|
35
|
+
}
|
|
36
|
+
else if (o.issuer !== null && q.get("iss") !== o.issuer) {
|
|
37
|
+
finish(400, "Sign-in failed", "This answer came from another server.", () => settle.reject(new NaraiError("sign-in failed: the answer names another issuer", "oauth_issuer_mismatch", 3)));
|
|
38
|
+
}
|
|
39
|
+
else if (q.get("error") !== null) {
|
|
40
|
+
finish(400, "Sign-in cancelled", "You can close this tab.", () => settle.reject(new OAuthError(q.get("error") ?? "", q.get("error_description") ?? "")));
|
|
41
|
+
}
|
|
42
|
+
else if (!q.get("code")) {
|
|
43
|
+
finish(400, "Sign-in failed", "The answer carried no code.", () => settle.reject(new NaraiError("sign-in failed: no code", "oauth_no_code", 3)));
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
finish(200, "Signed in", "You can close this tab and go back to the terminal.", () => settle.resolve(q.get("code") ?? ""));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
await new Promise((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", () => resolve()); });
|
|
50
|
+
const timer = setTimeout(() => { done = true; close(); settle.reject(new NaraiError("sign-in timed out: no answer from the browser", "oauth_timeout", 3)); }, o.timeoutMs);
|
|
51
|
+
function close() { clearTimeout(timer); server.close(); server.closeAllConnections(); }
|
|
52
|
+
return { redirectUri: `http://127.0.0.1:${server.address().port}${CALLBACK}`, code, close };
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=loopback.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loopback.js","sourceRoot":"","sources":["../../src/auth/loopback.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,mHAAmH;AACnH,gCAAgC;AAEhC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,CAAC,MAAM,QAAQ,GAAG,WAAW,CAAC;AAIpC,MAAM,IAAI,GAAG,CAAC,KAAa,EAAE,IAAY,EAAE,EAAE,CAC3C,+CAA+C,KAAK,oFAAoF,KAAK,WAAW,IAAI,aAAa,CAAC;AAE5K,MAAM,IAAI,GAAG,CAAC,CAAS,EAAE,CAAS,EAAE,EAAE;IACpC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAChD,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,IAAI,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACxD,CAAC,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,CAA8D;IAChG,IAAI,MAAoE,CAAC;IACzE,MAAM,IAAI,GAAG,IAAI,OAAO,CAAS,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