somark-js 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 +156 -0
- package/README.md +32 -0
- package/README_CN.md +31 -0
- package/dist/chunk-RWOUZTFQ.mjs +236 -0
- package/dist/cli.mjs +1503 -0
- package/dist/config-GOZJZCRT.mjs +26 -0
- package/dist/index.cjs +1304 -0
- package/dist/index.d.cts +258 -0
- package/dist/index.d.ts +258 -0
- package/dist/index.mjs +1245 -0
- package/package.json +66 -0
- package/vendor/somarkdown-viewer/LICENSE +21 -0
- package/vendor/somarkdown-viewer/README.md +63 -0
- package/vendor/somarkdown-viewer/README_CN.md +63 -0
- package/vendor/somarkdown-viewer/index.html +112 -0
- package/vendor/somarkdown-viewer/lib/bi-direction-jump.js +161 -0
- package/vendor/somarkdown-viewer/lib/deps/highlight.js.default.min.css +11 -0
- package/vendor/somarkdown-viewer/lib/deps/katex.min.css +6 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_AMS-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_AMS-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_AMS-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Caligraphic-Bold.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Caligraphic-Bold.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Caligraphic-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Caligraphic-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Fraktur-Bold.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Fraktur-Bold.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Fraktur-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Fraktur-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-Bold.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-Bold.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-Bold.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-BoldItalic.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-BoldItalic.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-Italic.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-Italic.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-Italic.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Main-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Math-BoldItalic.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Math-BoldItalic.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Math-Italic.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Math-Italic.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Math-Italic.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_SansSerif-Bold.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_SansSerif-Bold.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_SansSerif-Italic.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_SansSerif-Italic.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_SansSerif-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_SansSerif-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Script-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Script-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Script-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size1-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size1-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size1-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size2-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size2-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size2-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size3-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size3-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size3-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size4-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size4-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Size4-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Typewriter-Regular.ttf +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Typewriter-Regular.woff +0 -0
- package/vendor/somarkdown-viewer/lib/deps/katex_fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/vendor/somarkdown-viewer/lib/i18n.js +81 -0
- package/vendor/somarkdown-viewer/lib/index.js +299 -0
- package/vendor/somarkdown-viewer/lib/somarkdown/VERSION +1 -0
- package/vendor/somarkdown-viewer/lib/somarkdown/somarkdown.css +591 -0
- package/vendor/somarkdown-viewer/lib/somarkdown/somarkdown.umd.min.js +59 -0
- package/vendor/somarkdown-viewer/lib/sync-scroll.js +147 -0
- package/vendor/somarkdown-viewer/style/index.css +527 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
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, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made
|
|
34
|
+
available under the License, as indicated by a copyright notice that is included
|
|
35
|
+
in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on or derived from the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works,
|
|
46
|
+
that is intentionally submitted to Licensor for inclusion in the Work by the
|
|
47
|
+
copyright owner or by an individual or Legal Entity authorized to submit on
|
|
48
|
+
behalf of the copyright owner. For the purposes of this definition, "submitted"
|
|
49
|
+
means any form of electronic, verbal, or written communication sent to the
|
|
50
|
+
Licensor or its representatives, including but not limited to communication on
|
|
51
|
+
electronic mailing lists, source code control systems, and issue tracking
|
|
52
|
+
systems that are managed by, or on behalf of, the Licensor for the purpose of
|
|
53
|
+
discussing and improving the Work, but excluding communication that is
|
|
54
|
+
conspicuously marked or otherwise designated in writing by the copyright owner
|
|
55
|
+
as "Not a Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
66
|
+
Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable patent license to make, have made, use,
|
|
71
|
+
offer to sell, sell, import, and otherwise transfer the Work, where such license
|
|
72
|
+
applies only to those patent claims licensable by such Contributor that are
|
|
73
|
+
necessarily infringed by their Contribution(s) alone or by combination of their
|
|
74
|
+
Contribution(s) with the Work to which such Contribution(s) was submitted. If
|
|
75
|
+
You institute patent litigation against any entity alleging that the Work or a
|
|
76
|
+
Contribution incorporated within the Work constitutes direct or contributory
|
|
77
|
+
patent infringement, then any patent licenses granted to You under this License
|
|
78
|
+
for that Work shall terminate as of the date such litigation is filed.
|
|
79
|
+
|
|
80
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
81
|
+
Derivative Works in any medium, with or without modifications, and in Source or
|
|
82
|
+
Object form, provided that You meet the following conditions:
|
|
83
|
+
|
|
84
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
85
|
+
this License; and
|
|
86
|
+
|
|
87
|
+
(b) You must cause any modified files to carry prominent notices stating that You
|
|
88
|
+
changed the files; and
|
|
89
|
+
|
|
90
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
91
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
92
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
93
|
+
of the Derivative Works; and
|
|
94
|
+
|
|
95
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
96
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
97
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
98
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
99
|
+
following places: within a NOTICE text file distributed as part of the Derivative
|
|
100
|
+
Works; within the Source form or documentation, if provided along with the
|
|
101
|
+
Derivative Works; or within a display generated by the Derivative Works, if and
|
|
102
|
+
wherever such third-party notices normally appear. The contents of the NOTICE
|
|
103
|
+
file are for informational purposes only and do not modify the License. You may
|
|
104
|
+
add Your own attribution notices within Derivative Works that You distribute,
|
|
105
|
+
alongside or as an addendum to the NOTICE text from the Work, provided that such
|
|
106
|
+
additional attribution notices cannot be construed as modifying the License.
|
|
107
|
+
|
|
108
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
109
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
110
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
111
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
112
|
+
with the conditions stated in this License.
|
|
113
|
+
|
|
114
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
115
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
116
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
117
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
118
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
119
|
+
executed with Licensor regarding such Contributions.
|
|
120
|
+
|
|
121
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
122
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
123
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
124
|
+
reproducing the content of the NOTICE file.
|
|
125
|
+
|
|
126
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
127
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
128
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
129
|
+
either express or implied, including, without limitation, any warranties or
|
|
130
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
131
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
132
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
133
|
+
associated with Your exercise of permissions under this License.
|
|
134
|
+
|
|
135
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
136
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
137
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
138
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
139
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
140
|
+
arising as a result of this License or out of the use or inability to use the
|
|
141
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
142
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
143
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
144
|
+
damages.
|
|
145
|
+
|
|
146
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
147
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
148
|
+
acceptance of support, warranty, indemnity, or other liability obligations and/or
|
|
149
|
+
rights consistent with this License. However, in accepting such obligations, You
|
|
150
|
+
may act only on Your own behalf and on Your sole responsibility, not on behalf
|
|
151
|
+
of any other Contributor, and only if You agree to indemnify, defend, and hold
|
|
152
|
+
each Contributor harmless for any liability incurred by, or claims asserted
|
|
153
|
+
against, such Contributor by reason of your accepting any such warranty or
|
|
154
|
+
additional liability.
|
|
155
|
+
|
|
156
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# somark-js
|
|
2
|
+
|
|
3
|
+
[](https://nodejs.org/)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
[](https://www.npmjs.com/package/somark-js)
|
|
6
|
+
|
|
7
|
+
The official JavaScript/TypeScript SDK and CLI for [SoMark](https://somark.ai).
|
|
8
|
+
|
|
9
|
+
This repository is the JavaScript/TypeScript implementation of the shared SoMark SDK + CLI contract. The Python implementation lives in [`somark-py`](https://github.com/SoMarkAI/somark-py). Same product, different runtime.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install somark-js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Documentation
|
|
18
|
+
|
|
19
|
+
Read the official documentation at [docs.somark.tech/documentation](https://docs.somark.tech/open-source-tools).
|
|
20
|
+
|
|
21
|
+
The docs cover authentication, CLI usage, SDK examples, async tasks, local preview, PDF utilities, API parameters, and error handling. This README stays intentionally short so it does not become a second, worse documentation site.
|
|
22
|
+
|
|
23
|
+
## Requirements
|
|
24
|
+
|
|
25
|
+
- Node.js 18+
|
|
26
|
+
- A SoMark API key for remote parsing and usage queries
|
|
27
|
+
- No API key required for local PDF utilities or SoMarkDown preview
|
|
28
|
+
- Docker/Ubuntu Python PDF rendering may require system libraries: `apt-get install -y --no-install-recommends libxcb1 libgl1 libglib2.0-0`
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
Apache-2.0
|
package/README_CN.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# somark-js
|
|
2
|
+
|
|
3
|
+
[](https://nodejs.org/)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
[](https://www.npmjs.com/package/somark-js)
|
|
6
|
+
|
|
7
|
+
[SoMark](https://somark.ai) 官方 JavaScript/TypeScript SDK 与命令行工具。
|
|
8
|
+
|
|
9
|
+
这个仓库是 SoMark SDK + CLI 统一规范的 JavaScript/TypeScript 实现。Python 实现在 [`somark-py`](https://github.com/SoMarkAI/somark-py)。同一个产品目标,两种语言生态。
|
|
10
|
+
|
|
11
|
+
## 安装
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install somark-js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 文档
|
|
18
|
+
|
|
19
|
+
官方文档请阅读:[docs.somark.tech/documentation](https://docs.somark.tech/open-source-tools)。
|
|
20
|
+
|
|
21
|
+
认证、CLI、SDK 示例、异步任务、本地预览、PDF 工具、API 参数和错误处理都在那里。README 只保留入口信息,避免长成另一份更难维护的文档。
|
|
22
|
+
|
|
23
|
+
## 环境要求
|
|
24
|
+
|
|
25
|
+
- Node.js 18+
|
|
26
|
+
- 远程解析和用量查询需要 SoMark API Key
|
|
27
|
+
- 本地 PDF 工具与 SoMarkDown 预览不需要 API Key
|
|
28
|
+
|
|
29
|
+
## 许可证
|
|
30
|
+
|
|
31
|
+
Apache-2.0
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import * as os from "os";
|
|
5
|
+
var DEFAULT_BASE_URL = "https://somark.tech/api/v1";
|
|
6
|
+
var DEFAULT_TIMEOUT = 60;
|
|
7
|
+
var DEFAULT_MAX_RETRIES = 2;
|
|
8
|
+
var CONFIG_FILE = path.join(os.homedir(), ".somark", "config.toml");
|
|
9
|
+
var VALID_CONFIG_KEYS = ["api_key", "base_url", "timeout", "max_retries"];
|
|
10
|
+
function parseOptionalString(value) {
|
|
11
|
+
return value === void 0 || value === null ? void 0 : String(value);
|
|
12
|
+
}
|
|
13
|
+
function parseString(value) {
|
|
14
|
+
return String(value);
|
|
15
|
+
}
|
|
16
|
+
function parseNumber(value) {
|
|
17
|
+
return Number(value);
|
|
18
|
+
}
|
|
19
|
+
function parseInteger(value) {
|
|
20
|
+
return Number.parseInt(String(value), 10);
|
|
21
|
+
}
|
|
22
|
+
function validateAny(_key, _value) {
|
|
23
|
+
}
|
|
24
|
+
function validateBaseUrl(key, value) {
|
|
25
|
+
if (!/^https?:\/\//i.test(String(value))) {
|
|
26
|
+
throw new Error(`Invalid ${key} "${value}". Must start with http:// or https://`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function maskIdentity(value) {
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
function maskApiKey(value) {
|
|
33
|
+
return value.length > 8 ? `${value.slice(0, 4)}...${value.slice(-4)}` : "***";
|
|
34
|
+
}
|
|
35
|
+
function formatValue(value) {
|
|
36
|
+
return String(value);
|
|
37
|
+
}
|
|
38
|
+
var CONFIG_SCHEMA = [
|
|
39
|
+
{
|
|
40
|
+
key: "api_key",
|
|
41
|
+
envVar: "SOMARK_API_KEY",
|
|
42
|
+
optionKey: "apiKey",
|
|
43
|
+
defaultValue: void 0,
|
|
44
|
+
parse: parseOptionalString,
|
|
45
|
+
validate: validateAny,
|
|
46
|
+
mask: maskApiKey,
|
|
47
|
+
format: formatValue,
|
|
48
|
+
showWhenUnset: false
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
key: "base_url",
|
|
52
|
+
envVar: "SOMARK_BASE_URL",
|
|
53
|
+
optionKey: "baseUrl",
|
|
54
|
+
defaultValue: DEFAULT_BASE_URL,
|
|
55
|
+
parse: parseString,
|
|
56
|
+
validate: validateBaseUrl,
|
|
57
|
+
mask: maskIdentity,
|
|
58
|
+
format: formatValue,
|
|
59
|
+
showWhenUnset: true
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
key: "timeout",
|
|
63
|
+
envVar: "SOMARK_TIMEOUT",
|
|
64
|
+
optionKey: "timeout",
|
|
65
|
+
defaultValue: DEFAULT_TIMEOUT,
|
|
66
|
+
parse: parseNumber,
|
|
67
|
+
validate: validateAny,
|
|
68
|
+
mask: maskIdentity,
|
|
69
|
+
format: formatValue,
|
|
70
|
+
showWhenUnset: true
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
key: "max_retries",
|
|
74
|
+
envVar: "SOMARK_MAX_RETRIES",
|
|
75
|
+
optionKey: "maxRetries",
|
|
76
|
+
defaultValue: DEFAULT_MAX_RETRIES,
|
|
77
|
+
parse: parseInteger,
|
|
78
|
+
validate: validateAny,
|
|
79
|
+
mask: maskIdentity,
|
|
80
|
+
format: formatValue,
|
|
81
|
+
showWhenUnset: true
|
|
82
|
+
}
|
|
83
|
+
];
|
|
84
|
+
var CONFIG_SCHEMA_BY_KEY = new Map(
|
|
85
|
+
CONFIG_SCHEMA.map((entry) => [entry.key, entry])
|
|
86
|
+
);
|
|
87
|
+
function stripTomlComment(line) {
|
|
88
|
+
let quote;
|
|
89
|
+
let escaping = false;
|
|
90
|
+
for (let i = 0; i < line.length; i += 1) {
|
|
91
|
+
const char = line[i];
|
|
92
|
+
if (quote === '"') {
|
|
93
|
+
if (escaping) {
|
|
94
|
+
escaping = false;
|
|
95
|
+
} else if (char === "\\") {
|
|
96
|
+
escaping = true;
|
|
97
|
+
} else if (char === quote) {
|
|
98
|
+
quote = void 0;
|
|
99
|
+
}
|
|
100
|
+
} else if (quote === "'") {
|
|
101
|
+
if (char === quote) quote = void 0;
|
|
102
|
+
} else if (char === '"' || char === "'") {
|
|
103
|
+
quote = char;
|
|
104
|
+
} else if (char === "#") {
|
|
105
|
+
return line.slice(0, i);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return line;
|
|
109
|
+
}
|
|
110
|
+
function parseTomlValue(rawValue) {
|
|
111
|
+
const value = stripTomlComment(rawValue).trim();
|
|
112
|
+
if (value.startsWith('"') && value.endsWith('"')) {
|
|
113
|
+
return JSON.parse(value);
|
|
114
|
+
}
|
|
115
|
+
if (value.startsWith("'") && value.endsWith("'")) {
|
|
116
|
+
return value.slice(1, -1);
|
|
117
|
+
}
|
|
118
|
+
if (/^[+-]?(?:\d+\.?\d*|\.\d+)$/.test(value)) {
|
|
119
|
+
return Number(value);
|
|
120
|
+
}
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
function loadTomlConfig() {
|
|
124
|
+
if (!fs.existsSync(CONFIG_FILE)) return {};
|
|
125
|
+
try {
|
|
126
|
+
const content = fs.readFileSync(CONFIG_FILE, "utf-8");
|
|
127
|
+
const result = {};
|
|
128
|
+
for (const line of content.split("\n")) {
|
|
129
|
+
const trimmed = stripTomlComment(line).trim();
|
|
130
|
+
if (!trimmed || trimmed.startsWith("[")) continue;
|
|
131
|
+
const separatorIndex = trimmed.indexOf("=");
|
|
132
|
+
if (separatorIndex === -1) continue;
|
|
133
|
+
const key = trimmed.slice(0, separatorIndex).trim();
|
|
134
|
+
if (!VALID_CONFIG_KEYS.includes(key)) continue;
|
|
135
|
+
result[key] = parseTomlValue(trimmed.slice(separatorIndex + 1));
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
} catch {
|
|
139
|
+
return {};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function hasFileValue(file, key) {
|
|
143
|
+
const value = file[key];
|
|
144
|
+
return value !== void 0 && value !== null && value !== "";
|
|
145
|
+
}
|
|
146
|
+
function resolveSchemaEntry(entry, opts, file) {
|
|
147
|
+
let rawValue;
|
|
148
|
+
let source;
|
|
149
|
+
if (opts[entry.optionKey] !== void 0) {
|
|
150
|
+
rawValue = opts[entry.optionKey];
|
|
151
|
+
source = "explicit";
|
|
152
|
+
} else if (process.env[entry.envVar]) {
|
|
153
|
+
rawValue = process.env[entry.envVar];
|
|
154
|
+
source = "env";
|
|
155
|
+
} else if (hasFileValue(file, entry.key)) {
|
|
156
|
+
rawValue = file[entry.key];
|
|
157
|
+
source = "file";
|
|
158
|
+
} else {
|
|
159
|
+
rawValue = entry.defaultValue;
|
|
160
|
+
source = "default";
|
|
161
|
+
}
|
|
162
|
+
const value = entry.parse(rawValue);
|
|
163
|
+
entry.validate(entry.key, value);
|
|
164
|
+
return { value, source };
|
|
165
|
+
}
|
|
166
|
+
function resolveSchema(opts = {}) {
|
|
167
|
+
const file = loadTomlConfig();
|
|
168
|
+
return Object.fromEntries(
|
|
169
|
+
CONFIG_SCHEMA.map((entry) => [entry.key, resolveSchemaEntry(entry, opts, file)])
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
function resolveConfig(opts = {}) {
|
|
173
|
+
const resolved = resolveSchema(opts);
|
|
174
|
+
return {
|
|
175
|
+
apiKey: resolved.api_key.value,
|
|
176
|
+
baseUrl: resolved.base_url.value,
|
|
177
|
+
timeout: resolved.timeout.value,
|
|
178
|
+
maxRetries: resolved.max_retries.value
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function saveConfig(values) {
|
|
182
|
+
for (const key of Object.keys(values)) {
|
|
183
|
+
const entry = CONFIG_SCHEMA_BY_KEY.get(key);
|
|
184
|
+
if (!entry) {
|
|
185
|
+
throw new Error(`Unknown config key "${key}". Valid keys: ${VALID_CONFIG_KEYS.join(", ")}`);
|
|
186
|
+
}
|
|
187
|
+
const parsedValue = entry.parse(values[key]);
|
|
188
|
+
entry.validate(key, parsedValue);
|
|
189
|
+
}
|
|
190
|
+
const dir = path.dirname(CONFIG_FILE);
|
|
191
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
192
|
+
const existing = loadTomlConfig();
|
|
193
|
+
const merged = { ...existing, ...values };
|
|
194
|
+
const lines = Object.entries(merged).map(([k, v]) => `${k} = "${v}"`);
|
|
195
|
+
fs.writeFileSync(CONFIG_FILE, lines.join("\n") + "\n", "utf-8");
|
|
196
|
+
try {
|
|
197
|
+
fs.chmodSync(CONFIG_FILE, 384);
|
|
198
|
+
} catch {
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function getConfigValue(key) {
|
|
202
|
+
const value = loadTomlConfig()[key];
|
|
203
|
+
return value === void 0 ? void 0 : String(value);
|
|
204
|
+
}
|
|
205
|
+
function maskConfigValue(key, value) {
|
|
206
|
+
return CONFIG_SCHEMA_BY_KEY.get(key)?.mask(value) ?? value;
|
|
207
|
+
}
|
|
208
|
+
function listConfig() {
|
|
209
|
+
return Object.fromEntries(
|
|
210
|
+
Object.entries(loadTomlConfig()).map(([key, value]) => [key, String(value)])
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
function listResolvedConfig() {
|
|
214
|
+
const result = {};
|
|
215
|
+
const resolved = resolveSchema();
|
|
216
|
+
for (const entry of CONFIG_SCHEMA) {
|
|
217
|
+
const { value, source } = resolved[entry.key];
|
|
218
|
+
if (value === void 0 && !entry.showWhenUnset) continue;
|
|
219
|
+
result[entry.key] = { value: entry.format(value), source };
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export {
|
|
225
|
+
DEFAULT_BASE_URL,
|
|
226
|
+
DEFAULT_TIMEOUT,
|
|
227
|
+
DEFAULT_MAX_RETRIES,
|
|
228
|
+
CONFIG_FILE,
|
|
229
|
+
VALID_CONFIG_KEYS,
|
|
230
|
+
resolveConfig,
|
|
231
|
+
saveConfig,
|
|
232
|
+
getConfigValue,
|
|
233
|
+
maskConfigValue,
|
|
234
|
+
listConfig,
|
|
235
|
+
listResolvedConfig
|
|
236
|
+
};
|