conjure-js 0.0.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.
- package/conjure +0 -0
- package/dist/assets/codicon-ngg6Pgfi.ttf +0 -0
- package/dist/assets/editor.worker-CdQrwHl8.js +26 -0
- package/dist/assets/main-A7ZMId9A.css +1 -0
- package/dist/assets/main-CmI-7epE.js +3137 -0
- package/dist/index.html +195 -0
- package/dist/vite.svg +1 -0
- package/package.json +68 -0
- package/src/bin/__fixtures__/smoke/app/lib.clj +4 -0
- package/src/bin/__fixtures__/smoke/app/main.clj +4 -0
- package/src/bin/__fixtures__/smoke/repl-smoke.ts +12 -0
- package/src/bin/bencode.ts +205 -0
- package/src/bin/cli.ts +250 -0
- package/src/bin/nrepl-utils.ts +59 -0
- package/src/bin/nrepl.ts +393 -0
- package/src/bin/version.ts +4 -0
- package/src/clojure/core.clj +620 -0
- package/src/clojure/core.clj.d.ts +189 -0
- package/src/clojure/demo/math.clj +16 -0
- package/src/clojure/demo/math.clj.d.ts +4 -0
- package/src/clojure/demo.clj +42 -0
- package/src/clojure/demo.clj.d.ts +0 -0
- package/src/clojure/generated/builtin-namespace-registry.ts +14 -0
- package/src/clojure/generated/clojure-core-source.ts +623 -0
- package/src/clojure/generated/clojure-string-source.ts +196 -0
- package/src/clojure/string.clj +192 -0
- package/src/clojure/string.clj.d.ts +25 -0
- package/src/core/assertions.ts +134 -0
- package/src/core/conversions.ts +108 -0
- package/src/core/core-env.ts +58 -0
- package/src/core/env.ts +78 -0
- package/src/core/errors.ts +39 -0
- package/src/core/evaluator/apply.ts +114 -0
- package/src/core/evaluator/arity.ts +174 -0
- package/src/core/evaluator/collections.ts +25 -0
- package/src/core/evaluator/destructure.ts +247 -0
- package/src/core/evaluator/dispatch.ts +73 -0
- package/src/core/evaluator/evaluate.ts +100 -0
- package/src/core/evaluator/expand.ts +79 -0
- package/src/core/evaluator/index.ts +72 -0
- package/src/core/evaluator/quasiquote.ts +87 -0
- package/src/core/evaluator/recur-check.ts +109 -0
- package/src/core/evaluator/special-forms.ts +517 -0
- package/src/core/factories.ts +155 -0
- package/src/core/gensym.ts +9 -0
- package/src/core/index.ts +76 -0
- package/src/core/positions.ts +38 -0
- package/src/core/printer.ts +86 -0
- package/src/core/reader.ts +559 -0
- package/src/core/scanners.ts +93 -0
- package/src/core/session.ts +610 -0
- package/src/core/stdlib/arithmetic.ts +361 -0
- package/src/core/stdlib/atoms.ts +88 -0
- package/src/core/stdlib/collections.ts +784 -0
- package/src/core/stdlib/errors.ts +81 -0
- package/src/core/stdlib/hof.ts +307 -0
- package/src/core/stdlib/meta.ts +48 -0
- package/src/core/stdlib/predicates.ts +240 -0
- package/src/core/stdlib/regex.ts +238 -0
- package/src/core/stdlib/strings.ts +311 -0
- package/src/core/stdlib/transducers.ts +256 -0
- package/src/core/stdlib/utils.ts +287 -0
- package/src/core/tokenizer.ts +437 -0
- package/src/core/transformations.ts +75 -0
- package/src/core/types.ts +258 -0
- package/src/main.ts +1 -0
- package/src/monaco-esm.d.ts +7 -0
- package/src/playground/clojure-tokens.ts +67 -0
- package/src/playground/editor.worker.ts +5 -0
- package/src/playground/find-form.ts +138 -0
- package/src/playground/playground.ts +342 -0
- package/src/playground/samples/00-welcome.clj +385 -0
- package/src/playground/samples/01-collections.clj +191 -0
- package/src/playground/samples/02-higher-order-functions.clj +215 -0
- package/src/playground/samples/03-destructuring.clj +194 -0
- package/src/playground/samples/04-strings-and-regex.clj +202 -0
- package/src/playground/samples/05-error-handling.clj +212 -0
- package/src/repl/repl.ts +116 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.json +31 -0
package/dist/index.html
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>Regibyte — Clojure Playground</title>
|
|
7
|
+
<style>
|
|
8
|
+
/* ── VS Code-like palette tokens ─────────────────── */
|
|
9
|
+
:root {
|
|
10
|
+
--bg: #1e1e1e;
|
|
11
|
+
--bg-header: #181818;
|
|
12
|
+
--border: #2d2d2d;
|
|
13
|
+
--border-muted: #252526;
|
|
14
|
+
--text: #d4d4d4;
|
|
15
|
+
--text-dim: #a6a6a6;
|
|
16
|
+
--text-subtle: #808080;
|
|
17
|
+
--accent: #569cd6;
|
|
18
|
+
--green: #89d185;
|
|
19
|
+
--red: #f14c4c;
|
|
20
|
+
--yellow: #d7ba7d;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
24
|
+
|
|
25
|
+
html, body { height: 100%; overflow: hidden; }
|
|
26
|
+
|
|
27
|
+
body {
|
|
28
|
+
background: var(--bg);
|
|
29
|
+
color: var(--text);
|
|
30
|
+
font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', 'SF Mono', ui-monospace, monospace;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* ── Layout ─────────────────────────────────────── */
|
|
34
|
+
|
|
35
|
+
.pg { display: flex; flex-direction: column; height: 100vh; }
|
|
36
|
+
|
|
37
|
+
.pg-header {
|
|
38
|
+
display: flex;
|
|
39
|
+
align-items: center;
|
|
40
|
+
justify-content: space-between;
|
|
41
|
+
padding: 0 1.5rem;
|
|
42
|
+
height: 52px;
|
|
43
|
+
background: var(--bg-header);
|
|
44
|
+
border-bottom: 1px solid var(--border);
|
|
45
|
+
flex-shrink: 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.pg-header__left { display: flex; align-items: center; gap: 1rem; }
|
|
49
|
+
.pg-header__title { color: var(--accent); font-size: 0.9rem; font-weight: 600; letter-spacing: 0.02em; }
|
|
50
|
+
.pg-header__hint { color: var(--text-subtle); font-size: 0.78rem; }
|
|
51
|
+
|
|
52
|
+
.pg-header__actions { display: flex; gap: 0.6rem; align-items: center; }
|
|
53
|
+
|
|
54
|
+
.pg-body { display: flex; flex: 1; overflow: hidden; }
|
|
55
|
+
|
|
56
|
+
.pg-editor-wrap { flex: 3; min-width: 0; }
|
|
57
|
+
|
|
58
|
+
.pg-output {
|
|
59
|
+
flex: 2;
|
|
60
|
+
min-width: 0;
|
|
61
|
+
overflow-y: auto;
|
|
62
|
+
border-left: 1px solid var(--border);
|
|
63
|
+
background: var(--bg);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.pg-output-inner { padding: 1.25rem; }
|
|
67
|
+
|
|
68
|
+
/* ── Empty state ────────────────────────────────── */
|
|
69
|
+
|
|
70
|
+
.pg-empty {
|
|
71
|
+
color: var(--text-subtle);
|
|
72
|
+
font-size: 0.82rem;
|
|
73
|
+
text-align: center;
|
|
74
|
+
padding-top: 3rem;
|
|
75
|
+
line-height: 1.7;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/* ── Output entries ─────────────────────────────── */
|
|
79
|
+
|
|
80
|
+
.pg-entry { margin-bottom: 1.5rem; }
|
|
81
|
+
|
|
82
|
+
.pg-entry__source {
|
|
83
|
+
font-size: 0.78rem;
|
|
84
|
+
color: var(--text-subtle);
|
|
85
|
+
margin-bottom: 0.35rem;
|
|
86
|
+
padding: 0 0.25rem;
|
|
87
|
+
white-space: pre-wrap;
|
|
88
|
+
word-break: break-all;
|
|
89
|
+
line-height: 1.4;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.pg-entry__output {
|
|
93
|
+
font-size: 0.85rem;
|
|
94
|
+
color: var(--yellow);
|
|
95
|
+
padding: 0.4rem 0.75rem;
|
|
96
|
+
border-left: 2px solid color-mix(in srgb, var(--yellow) 30%, transparent);
|
|
97
|
+
margin-bottom: 0.25rem;
|
|
98
|
+
white-space: pre-wrap;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
.pg-entry__result {
|
|
102
|
+
font-size: 0.88rem;
|
|
103
|
+
color: var(--green);
|
|
104
|
+
padding: 0.5rem 0.75rem;
|
|
105
|
+
border-left: 2px solid var(--green);
|
|
106
|
+
background: color-mix(in srgb, var(--green) 4%, transparent);
|
|
107
|
+
white-space: pre-wrap;
|
|
108
|
+
word-break: break-all;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.pg-entry__result--error {
|
|
112
|
+
color: var(--red);
|
|
113
|
+
border-left-color: var(--red);
|
|
114
|
+
background: color-mix(in srgb, var(--red) 4%, transparent);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.pg-entry__duration {
|
|
118
|
+
color: var(--text-subtle);
|
|
119
|
+
font-size: 0.92em;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* ── Buttons ────────────────────────────────────── */
|
|
123
|
+
|
|
124
|
+
.pg-btn {
|
|
125
|
+
background: var(--bg-header);
|
|
126
|
+
color: var(--text-dim);
|
|
127
|
+
border: 1px solid var(--border);
|
|
128
|
+
padding: 0.3rem 0.85rem;
|
|
129
|
+
border-radius: 6px;
|
|
130
|
+
font-size: 0.78rem;
|
|
131
|
+
cursor: pointer;
|
|
132
|
+
font-family: inherit;
|
|
133
|
+
transition: background 0.15s, border-color 0.15s, color 0.15s;
|
|
134
|
+
}
|
|
135
|
+
.pg-btn:hover { background: #1c2128; border-color: #8b949e; color: var(--text); }
|
|
136
|
+
|
|
137
|
+
.pg-btn--primary {
|
|
138
|
+
color: var(--accent);
|
|
139
|
+
border-color: color-mix(in srgb, var(--accent) 30%, transparent);
|
|
140
|
+
background: color-mix(in srgb, var(--accent) 8%, transparent);
|
|
141
|
+
}
|
|
142
|
+
.pg-btn--primary:hover {
|
|
143
|
+
background: color-mix(in srgb, var(--accent) 15%, transparent);
|
|
144
|
+
border-color: var(--accent);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.pg-btn--danger:hover { color: var(--red); border-color: color-mix(in srgb, var(--red) 40%, transparent); }
|
|
148
|
+
|
|
149
|
+
.pg-sample-select {
|
|
150
|
+
appearance: none;
|
|
151
|
+
-webkit-appearance: none;
|
|
152
|
+
padding-right: 1.75rem;
|
|
153
|
+
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23808080'/%3E%3C/svg%3E");
|
|
154
|
+
background-repeat: no-repeat;
|
|
155
|
+
background-position: right 0.5rem center;
|
|
156
|
+
background-size: 8px 5px;
|
|
157
|
+
cursor: pointer;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
kbd {
|
|
161
|
+
background: var(--bg-header);
|
|
162
|
+
border: 1px solid var(--border);
|
|
163
|
+
border-radius: 4px;
|
|
164
|
+
padding: 0.1rem 0.45rem;
|
|
165
|
+
font-size: 0.72rem;
|
|
166
|
+
color: var(--text-dim);
|
|
167
|
+
font-family: inherit;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/* ── Inline eval result widget ──────────────────────── */
|
|
171
|
+
|
|
172
|
+
/* Content widgets are placed in Monaco's .contentWidgets layer (absolute
|
|
173
|
+
positioning, always in the DOM). We match Monaco's editor font here so
|
|
174
|
+
the text visually sits on the same baseline as the code. */
|
|
175
|
+
.pg-inline-result,
|
|
176
|
+
.pg-inline-error {
|
|
177
|
+
pointer-events: none;
|
|
178
|
+
white-space: pre;
|
|
179
|
+
font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', 'SF Mono', ui-monospace, monospace;
|
|
180
|
+
font-size: 14px;
|
|
181
|
+
font-style: italic;
|
|
182
|
+
opacity: 0.80;
|
|
183
|
+
padding-left: 0.5ch;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
.pg-inline-result { color: #7ee787; }
|
|
187
|
+
.pg-inline-error { color: #f85149; }
|
|
188
|
+
</style>
|
|
189
|
+
<script type="module" crossorigin src="/conjure-js/assets/main-CmI-7epE.js"></script>
|
|
190
|
+
<link rel="stylesheet" crossorigin href="/conjure-js/assets/main-A7ZMId9A.css">
|
|
191
|
+
</head>
|
|
192
|
+
<body>
|
|
193
|
+
<div id="app"></div>
|
|
194
|
+
</body>
|
|
195
|
+
</html>
|
package/dist/vite.svg
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "conjure-js",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/regibyte/conjure-js.git"
|
|
9
|
+
},
|
|
10
|
+
"author": "Regibyte",
|
|
11
|
+
"homepage": "https://github.com/regibyte/conjure-js",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"bin": {
|
|
14
|
+
"conjure": "./conjure"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"conjure",
|
|
18
|
+
"dist/**/*",
|
|
19
|
+
"src/**/*.ts",
|
|
20
|
+
"src/**/*.clj",
|
|
21
|
+
"!src/**/__tests__/**",
|
|
22
|
+
"!src/**/*.spec.ts",
|
|
23
|
+
"!src/**/*.test.ts",
|
|
24
|
+
"!src/demo-entry.ts",
|
|
25
|
+
"!src/vite-plugin-clj/**",
|
|
26
|
+
"tsconfig.json",
|
|
27
|
+
"tsconfig.build.json"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"clojure",
|
|
31
|
+
"lisp",
|
|
32
|
+
"interpreter",
|
|
33
|
+
"nrepl",
|
|
34
|
+
"repl"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"dev": "vite",
|
|
38
|
+
"build": "node scripts/gen-version.mjs && tsc -p tsconfig.build.json && vite build",
|
|
39
|
+
"repl": "bun run src/bin/cli.ts repl",
|
|
40
|
+
"run": "bun run src/bin/cli.ts run",
|
|
41
|
+
"nrepl": "bun run src/bin/cli.ts nrepl-server",
|
|
42
|
+
"build-cli": "node scripts/gen-version.mjs && bun build --compile src/bin/cli.ts --outfile conjure",
|
|
43
|
+
"typecheck": "tsc --noEmit",
|
|
44
|
+
"preview": "vite preview",
|
|
45
|
+
"test": "vitest",
|
|
46
|
+
"gen:core-source": "bun run scripts/gen-core-source.mjs",
|
|
47
|
+
"gen:version": "node scripts/gen-version.mjs"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@bithero/monaco-editor-vite-plugin": "^1.0.3",
|
|
51
|
+
"@types/bun": "^1.3.10",
|
|
52
|
+
"@types/node": "^25.3.5",
|
|
53
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
54
|
+
"@vitest/ui": "^4.0.18",
|
|
55
|
+
"prettier": "^3.8.1",
|
|
56
|
+
"typescript": "~5.9.3",
|
|
57
|
+
"vite": "^7.3.1",
|
|
58
|
+
"vitest": "^4.0.18"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"monaco-editor": "^0.55.1"
|
|
62
|
+
},
|
|
63
|
+
"conjure": {
|
|
64
|
+
"sourceRoots": [
|
|
65
|
+
"src/clojure"
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const input = '(def x 41)\n(+ x 1)\n(exit)\n'
|
|
2
|
+
const result = Bun.spawnSync(
|
|
3
|
+
['bun', 'run', 'src/bin/cli.ts', 'repl'],
|
|
4
|
+
{
|
|
5
|
+
cwd: process.cwd(),
|
|
6
|
+
stdin: Buffer.from(input),
|
|
7
|
+
}
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
process.stdout.write(new TextDecoder().decode(result.stdout))
|
|
11
|
+
process.stderr.write(new TextDecoder().decode(result.stderr))
|
|
12
|
+
process.exit(result.exitCode ?? 1)
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A bencode encoder and incremental decoder using nodejs streams.
|
|
3
|
+
*
|
|
4
|
+
* Author: Matt Seddon
|
|
5
|
+
* Source: https://github.com/BetterThanTomorrow/calva (src/nrepl/bencode.ts)
|
|
6
|
+
* Copied with permission from Peter Strömberg (pez). No logic changes; only
|
|
7
|
+
* the `bencode` argument has been given an explicit TypeScript type.
|
|
8
|
+
*/
|
|
9
|
+
import * as stream from 'stream'
|
|
10
|
+
import { Buffer } from 'buffer'
|
|
11
|
+
|
|
12
|
+
/** Bencode the given value */
|
|
13
|
+
const bencode = (value: unknown): string => {
|
|
14
|
+
if (value === null || value === undefined) {
|
|
15
|
+
value = 0
|
|
16
|
+
}
|
|
17
|
+
if (typeof value == 'boolean') {
|
|
18
|
+
value = value ? 1 : 0
|
|
19
|
+
}
|
|
20
|
+
if (typeof value == 'number') {
|
|
21
|
+
return 'i' + value + 'e'
|
|
22
|
+
}
|
|
23
|
+
if (typeof value == 'string') {
|
|
24
|
+
return Buffer.byteLength(value, 'utf8') + ':' + value
|
|
25
|
+
}
|
|
26
|
+
if (value instanceof Array) {
|
|
27
|
+
return 'l' + value.map(bencode).join('') + 'e'
|
|
28
|
+
}
|
|
29
|
+
let out = 'd'
|
|
30
|
+
for (const prop in value as object) {
|
|
31
|
+
out += bencode(prop) + bencode((value as Record<string, unknown>)[prop])
|
|
32
|
+
}
|
|
33
|
+
return out + 'e'
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A transformation stream that takes a series of objects and bencodes them. */
|
|
37
|
+
export class BEncoderStream extends stream.Transform {
|
|
38
|
+
data: unknown[] = []
|
|
39
|
+
constructor() {
|
|
40
|
+
super({ objectMode: true })
|
|
41
|
+
}
|
|
42
|
+
_transform(object: unknown, _encoding: string, cb: () => void) {
|
|
43
|
+
const enc = bencode(object)
|
|
44
|
+
this.push(enc)
|
|
45
|
+
cb()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The states the incremental decoder can be in.
|
|
51
|
+
*/
|
|
52
|
+
type State =
|
|
53
|
+
| StringStartState
|
|
54
|
+
| StringBodyState
|
|
55
|
+
| IntState
|
|
56
|
+
| ListState
|
|
57
|
+
| DictState
|
|
58
|
+
| ReadyState
|
|
59
|
+
|
|
60
|
+
interface ReadyState {
|
|
61
|
+
id: 'ready'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface StringStartState {
|
|
65
|
+
id: 'string-start'
|
|
66
|
+
accum: string
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface StringBodyState {
|
|
70
|
+
id: 'string-body'
|
|
71
|
+
accum: number[]
|
|
72
|
+
length: number
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface IntState {
|
|
76
|
+
id: 'int'
|
|
77
|
+
accum: string
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface ListState {
|
|
81
|
+
id: 'list'
|
|
82
|
+
accum: unknown[]
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface DictState {
|
|
86
|
+
id: 'dict'
|
|
87
|
+
/** When null, we are reading a key; when a string, we must read the value */
|
|
88
|
+
key: string | null
|
|
89
|
+
accum: { [id: string]: unknown }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
class BIncrementalDecoder {
|
|
93
|
+
state: State = { id: 'ready' }
|
|
94
|
+
stack: State[] = []
|
|
95
|
+
|
|
96
|
+
private complete(data: unknown): unknown {
|
|
97
|
+
if (this.stack.length) {
|
|
98
|
+
this.state = this.stack.pop()!
|
|
99
|
+
if (this.state.id == 'list') {
|
|
100
|
+
this.state.accum.push(data)
|
|
101
|
+
this.stack.push(this.state)
|
|
102
|
+
this.state = { id: 'ready' }
|
|
103
|
+
} else if (this.state.id == 'dict') {
|
|
104
|
+
if (this.state.key !== null) {
|
|
105
|
+
this.state.accum[this.state.key] = data
|
|
106
|
+
this.state.key = null
|
|
107
|
+
} else {
|
|
108
|
+
this.state.key = data as string
|
|
109
|
+
}
|
|
110
|
+
this.stack.push(this.state)
|
|
111
|
+
this.state = { id: 'ready' }
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
this.state = { id: 'ready' }
|
|
115
|
+
return data
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
write(byte: number): unknown {
|
|
120
|
+
const ch = String.fromCharCode(byte)
|
|
121
|
+
if (this.state.id == 'ready') {
|
|
122
|
+
switch (ch) {
|
|
123
|
+
case 'i':
|
|
124
|
+
this.state = { id: 'int', accum: '' }
|
|
125
|
+
break
|
|
126
|
+
case 'd':
|
|
127
|
+
this.stack.push({ id: 'dict', accum: {}, key: null })
|
|
128
|
+
break
|
|
129
|
+
case 'l':
|
|
130
|
+
this.stack.push({ id: 'list', accum: [] })
|
|
131
|
+
break
|
|
132
|
+
case 'e':
|
|
133
|
+
if (!this.stack.length) {
|
|
134
|
+
throw 'unexpected end'
|
|
135
|
+
}
|
|
136
|
+
this.state = this.stack.pop()!
|
|
137
|
+
if (this.state.id == 'dict') {
|
|
138
|
+
if (this.state.key !== null) {
|
|
139
|
+
throw 'Missing value in dict'
|
|
140
|
+
}
|
|
141
|
+
return this.complete(this.state.accum)
|
|
142
|
+
} else if (this.state.id == 'list') {
|
|
143
|
+
return this.complete(this.state.accum)
|
|
144
|
+
}
|
|
145
|
+
break
|
|
146
|
+
default:
|
|
147
|
+
if (ch >= '0' && ch <= '9') {
|
|
148
|
+
this.state = { id: 'string-start', accum: ch }
|
|
149
|
+
} else {
|
|
150
|
+
throw 'Malformed input in bencode'
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} else if (this.state.id == 'int') {
|
|
154
|
+
if (ch == 'e') {
|
|
155
|
+
return this.complete(parseInt(this.state.accum))
|
|
156
|
+
} else {
|
|
157
|
+
this.state.accum += ch
|
|
158
|
+
}
|
|
159
|
+
} else if (this.state.id == 'string-start') {
|
|
160
|
+
if (ch == ':') {
|
|
161
|
+
if (!isFinite(+this.state.accum)) {
|
|
162
|
+
throw new Error('Invalid string length: ' + this.state.accum)
|
|
163
|
+
}
|
|
164
|
+
if (+this.state.accum == 0) {
|
|
165
|
+
return this.complete('')
|
|
166
|
+
}
|
|
167
|
+
this.state = {
|
|
168
|
+
id: 'string-body',
|
|
169
|
+
accum: [],
|
|
170
|
+
length: +this.state.accum,
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
this.state.accum += ch
|
|
174
|
+
}
|
|
175
|
+
} else if (this.state.id == 'string-body') {
|
|
176
|
+
this.state.accum.push(byte)
|
|
177
|
+
if (this.state.accum.length >= this.state.length) {
|
|
178
|
+
return this.complete(Buffer.from(this.state.accum).toString('utf8'))
|
|
179
|
+
}
|
|
180
|
+
} else if (this.state.id == 'list') {
|
|
181
|
+
return this.complete(this.state.accum)
|
|
182
|
+
} else if (this.state.id == 'dict') {
|
|
183
|
+
return this.complete(this.state.accum)
|
|
184
|
+
} else {
|
|
185
|
+
throw 'Junk in bencode'
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export class BDecoderStream extends stream.Transform {
|
|
191
|
+
decoder = new BIncrementalDecoder()
|
|
192
|
+
constructor() {
|
|
193
|
+
super({ objectMode: true })
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
_transform(data: Buffer, _encoding: string, cb: () => void) {
|
|
197
|
+
for (let i = 0; i < data.length; i++) {
|
|
198
|
+
const res = this.decoder.write(data[i])
|
|
199
|
+
if (res) {
|
|
200
|
+
this.push(res)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
cb()
|
|
204
|
+
}
|
|
205
|
+
}
|